ContentApiController.php 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  1. <?php
  2. namespace Typemill\Controllers;
  3. use Slim\Http\Request;
  4. use Slim\Http\Response;
  5. use Typemill\Models\Folder;
  6. use Typemill\Models\Write;
  7. use Typemill\Models\WriteYaml;
  8. use Typemill\Models\ProcessImage;
  9. use Typemill\Extensions\ParsedownExtension;
  10. use Typemill\Events\OnPagePublished;
  11. use Typemill\Events\OnPageUnpublished;
  12. use Typemill\Events\OnPageDeleted;
  13. use Typemill\Events\OnPageSorted;
  14. use \URLify;
  15. class ContentApiController extends ContentController
  16. {
  17. public function publishArticle(Request $request, Response $response, $args)
  18. {
  19. # get params from call
  20. $this->params = $request->getParams();
  21. $this->uri = $request->getUri();
  22. # validate input only if raw mode
  23. if($this->params['raw'])
  24. {
  25. if(!$this->validateEditorInput()){ return $response->withJson($this->errors,422); }
  26. }
  27. # set structure
  28. if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
  29. # set item
  30. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  31. # set the status for published and drafted
  32. $this->setPublishStatus();
  33. # set path
  34. $this->setItemPath($this->item->fileType);
  35. # if raw mode, use the content from request
  36. if($this->params['raw'])
  37. {
  38. $this->content = '# ' . $this->params['title'] . "\r\n\r\n" . $this->params['content'];
  39. }
  40. else
  41. {
  42. # read content from file
  43. if(!$this->setContent()){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  44. # If it is a draft, then create clean markdown content
  45. if(is_array($this->content))
  46. {
  47. # initialize parsedown extension
  48. $parsedown = new ParsedownExtension();
  49. # turn markdown into an array of markdown-blocks
  50. $this->content = $parsedown->arrayBlocksToMarkdown($this->content);
  51. }
  52. }
  53. # set path for the file (or folder)
  54. $this->setItemPath('md');
  55. # update the file
  56. if($this->write->writeFile($this->settings['contentFolder'], $this->path, $this->content))
  57. {
  58. # update the file
  59. $delete = $this->deleteContentFiles(['txt']);
  60. # update the internal structure
  61. $this->setStructure($draft = true, $cache = false);
  62. # update the public structure
  63. $this->setStructure($draft = false, $cache = false);
  64. # dispatch event
  65. $this->c->dispatcher->dispatch('onPagePublished', new OnPagePublished($this->item));
  66. return $response->withJson(['success'], 200);
  67. }
  68. else
  69. {
  70. return $response->withJson(['errors' => ['message' => 'Could not write to file. Please check if the file is writable']], 404);
  71. }
  72. }
  73. public function unpublishArticle(Request $request, Response $response, $args)
  74. {
  75. # get params from call
  76. $this->params = $request->getParams();
  77. $this->uri = $request->getUri();
  78. # set structure
  79. if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
  80. # set item
  81. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  82. # set the status for published and drafted
  83. $this->setPublishStatus();
  84. # check if draft exists, if not, create one.
  85. if(!$this->item->drafted)
  86. {
  87. # set path for the file (or folder)
  88. $this->setItemPath('md');
  89. # set content of markdown-file
  90. if(!$this->setContent()){ return $response->withJson($this->errors, 404); }
  91. # initialize parsedown extension
  92. $parsedown = new ParsedownExtension();
  93. # turn markdown into an array of markdown-blocks
  94. $contentArray = $parsedown->markdownToArrayBlocks($this->content);
  95. # encode the content into json
  96. $contentJson = json_encode($contentArray);
  97. # set path for the file (or folder)
  98. $this->setItemPath('txt');
  99. /* update the file */
  100. if(!$this->write->writeFile($this->settings['contentFolder'], $this->path, $contentJson))
  101. {
  102. return $response->withJson(['errors' => ['message' => 'Could not create a draft of the page. Please check if the folder is writable']], 404);
  103. }
  104. }
  105. # check if it is a folder and if the folder has published pages.
  106. $message = false;
  107. if($this->item->elementType == 'folder')
  108. {
  109. foreach($this->item->folderContent as $folderContent)
  110. {
  111. if($folderContent->status == 'published')
  112. {
  113. $message = 'There are published pages within this folder. The pages are not visible on your website anymore.';
  114. }
  115. }
  116. }
  117. # update the file
  118. $delete = $this->deleteContentFiles(['md']);
  119. if($delete)
  120. {
  121. # update the internal structure
  122. $this->setStructure($draft = true, $cache = false);
  123. # update the live structure
  124. $this->setStructure($draft = false, $cache = false);
  125. # dispatch event
  126. $this->c->dispatcher->dispatch('onPageUnpublished', new OnPageUnpublished($this->item));
  127. return $response->withJson(['success' => ['message' => $message]], 200);
  128. }
  129. else
  130. {
  131. return $response->withJson(['errors' => ['message' => "Could not delete some files. Please check if the files exists and are writable"]], 404);
  132. }
  133. }
  134. public function discardArticleChanges(Request $request, Response $response, $args)
  135. {
  136. # get params from call
  137. $this->params = $request->getParams();
  138. $this->uri = $request->getUri();
  139. # set structure
  140. if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
  141. # set item
  142. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  143. # remove the unpublished changes
  144. $delete = $this->deleteContentFiles(['txt']);
  145. # set redirect url to edit page
  146. $url = $this->uri->getBaseUrl() . '/tm/content/' . $this->settings['editor'];
  147. if(isset($this->item->urlRelWoF))
  148. {
  149. $url = $url . $this->item->urlRelWoF;
  150. }
  151. # remove the unpublished changes
  152. $delete = $this->deleteContentFiles(['txt']);
  153. if($delete)
  154. {
  155. # update the backend structure
  156. $this->setStructure($draft = true, $cache = false);
  157. return $response->withJson(['data' => $this->structure, 'errors' => false, 'url' => $url], 200);
  158. }
  159. else
  160. {
  161. return $response->withJson(['data' => $this->structure, 'errors' => $this->errors], 404);
  162. }
  163. }
  164. public function deleteArticle(Request $request, Response $response, $args)
  165. {
  166. # get params from call
  167. $this->params = $request->getParams();
  168. $this->uri = $request->getUri();
  169. # set url to base path initially
  170. $url = $this->uri->getBaseUrl() . '/tm/content/' . $this->settings['editor'];
  171. # set structure
  172. if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
  173. # set item
  174. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  175. if($this->item->elementType == 'file')
  176. {
  177. $delete = $this->deleteContentFiles(['md','txt', 'yaml']);
  178. }
  179. elseif($this->item->elementType == 'folder')
  180. {
  181. $delete = $this->deleteContentFolder();
  182. }
  183. if($delete)
  184. {
  185. # check if it is a subfile or subfolder and set the redirect-url to the parent item
  186. if(count($this->item->keyPathArray) > 1)
  187. {
  188. # get the parent item
  189. $parentItem = Folder::getParentItem($this->structure, $this->item->keyPathArray);
  190. if($parentItem)
  191. {
  192. # an active file has been moved to another folder
  193. $url .= $parentItem->urlRelWoF;
  194. }
  195. }
  196. # update the live structure
  197. $this->setStructure($draft = false, $cache = false);
  198. # update the backend structure
  199. $this->setStructure($draft = true, $cache = false);
  200. # check if page is in extended structure and delete it
  201. $this->deleteFromExtended();
  202. # dispatch event
  203. $this->c->dispatcher->dispatch('onPageDeleted', new OnPageDeleted($this->item));
  204. return $response->withJson(array('data' => $this->structure, 'errors' => false, 'url' => $url), 200);
  205. }
  206. else
  207. {
  208. return $response->withJson(array('data' => $this->structure, 'errors' => $this->errors), 404);
  209. }
  210. }
  211. public function updateArticle(Request $request, Response $response, $args)
  212. {
  213. # get params from call
  214. $this->params = $request->getParams();
  215. $this->uri = $request->getUri();
  216. # validate input
  217. if(!$this->validateEditorInput()){ return $response->withJson($this->errors,422); }
  218. # set structure
  219. if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
  220. # set item
  221. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  222. # set path for the file (or folder)
  223. $this->setItemPath('txt');
  224. # merge title with content for complete markdown document
  225. $updatedContent = '# ' . $this->params['title'] . "\r\n\r\n" . $this->params['content'];
  226. # initialize parsedown extension
  227. $parsedown = new ParsedownExtension();
  228. # turn markdown into an array of markdown-blocks
  229. $contentArray = $parsedown->markdownToArrayBlocks($updatedContent);
  230. # encode the content into json
  231. $contentJson = json_encode($contentArray);
  232. /* update the file */
  233. if($this->write->writeFile($this->settings['contentFolder'], $this->path, $contentJson))
  234. {
  235. # update the internal structure
  236. $this->setStructure($draft = true, $cache = false);
  237. return $response->withJson(['success'], 200);
  238. }
  239. else
  240. {
  241. return $response->withJson(['errors' => ['message' => 'Could not write to file. Please check if the file is writable']], 404);
  242. }
  243. }
  244. public function sortArticle(Request $request, Response $response, $args)
  245. {
  246. # get params from call
  247. $this->params = $request->getParams();
  248. $this->uri = $request->getUri();
  249. # url is only needed, if an active page is moved to another folder, so user has to be redirected to the new url
  250. $url = false;
  251. # set structure
  252. if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors, 'url' => $url), 404); }
  253. # validate input
  254. if(!$this->validateNavigationSort()){ return $response->withJson(array('data' => $this->structure, 'errors' => 'Data not valid. Please refresh the page and try again.', 'url' => $url), 422); }
  255. # get the ids (key path) for item, old folder and new folder
  256. $itemKeyPath = explode('.', $this->params['item_id']);
  257. $parentKeyFrom = explode('.', $this->params['parent_id_from']);
  258. $parentKeyTo = explode('.', $this->params['parent_id_to']);
  259. # get the item from structure
  260. $item = Folder::getItemWithKeyPath($this->structure, $itemKeyPath);
  261. if(!$item){ return $response->withJson(array('data' => $this->structure, 'errors' => 'We could not find this page. Please refresh and try again.', 'url' => $url), 404); }
  262. # if an item is moved to the first level
  263. if($this->params['parent_id_to'] == 'navi')
  264. {
  265. # create empty and default values so that the logic below still works
  266. $newFolder = new \stdClass();
  267. $newFolder->path = '';
  268. $folderContent = $this->structure;
  269. }
  270. else
  271. {
  272. # get the target folder from structure
  273. $newFolder = Folder::getItemWithKeyPath($this->structure, $parentKeyTo);
  274. # get the content of the target folder
  275. $folderContent = $newFolder->folderContent;
  276. }
  277. # if the item has been moved within the same folder
  278. if($this->params['parent_id_from'] == $this->params['parent_id_to'])
  279. {
  280. # get key of item
  281. $itemKey = end($itemKeyPath);
  282. reset($itemKeyPath);
  283. # delete item from folderContent
  284. unset($folderContent[$itemKey]);
  285. }
  286. else
  287. {
  288. # rename links in extended file
  289. $this->renameExtended($item, $newFolder);
  290. # an active file has been moved to another folder, so send new url with response
  291. if($this->params['active'] == 'active')
  292. {
  293. $url = $this->uri->getBaseUrl() . '/tm/content/' . $this->settings['editor'] . $newFolder->urlRelWoF . '/' . $item->slug;
  294. }
  295. }
  296. # add item to newFolder
  297. array_splice($folderContent, $this->params['index_new'], 0, array($item));
  298. # initialize index
  299. $index = 0;
  300. # initialise write object
  301. $write = new Write();
  302. # iterate through the whole content of the new folder to rename the files
  303. $writeError = false;
  304. foreach($folderContent as $folderItem)
  305. {
  306. if(!$write->moveElement($folderItem, $newFolder->path, $index))
  307. {
  308. $writeError = true;
  309. }
  310. $index++;
  311. }
  312. if($writeError){ return $response->withJson(array('data' => $this->structure, 'errors' => 'Something went wrong. Please refresh the page and check, if all folders and files are writable.', 'url' => $url), 404); }
  313. # update the structure for editor
  314. $this->setStructure($draft = true, $cache = false);
  315. # get item for url and set it active again
  316. if(isset($this->params['url']))
  317. {
  318. $activeItem = Folder::getItemForUrl($this->structure, $this->params['url'], $this->uri->getBaseUrl());
  319. }
  320. # keep the internal structure for response
  321. $internalStructure = $this->structure;
  322. # update the structure for website
  323. $this->setStructure($draft = false, $cache = false);
  324. # dispatch event
  325. $this->c->dispatcher->dispatch('onPageSorted', new OnPageSorted($this->params));
  326. return $response->withJson(array('data' => $internalStructure, 'errors' => false, 'url' => $url));
  327. }
  328. public function createPost(Request $request, Response $response, $args)
  329. {
  330. # get params from call
  331. $this->params = $request->getParams();
  332. $this->uri = $request->getUri();
  333. # url is only needed, if an active page is moved
  334. $url = false;
  335. # set structure
  336. if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors, 'url' => $url), 404); }
  337. # validate input
  338. if(!$this->validateNaviItem()){ return $response->withJson(array('data' => $this->structure, 'errors' => 'Special Characters not allowed. Length between 1 and 60 chars.', 'url' => $url), 422); }
  339. # get the ids (key path) for item, old folder and new folder
  340. $folderKeyPath = explode('.', $this->params['folder_id']);
  341. # get the item from structure
  342. $folder = Folder::getItemWithKeyPath($this->structure, $folderKeyPath);
  343. if(!$folder){ return $response->withJson(array('data' => $this->structure, 'errors' => 'We could not find this page. Please refresh and try again.', 'url' => $url), 404); }
  344. $name = $this->params['item_name'];
  345. $slug = URLify::filter(iconv(mb_detect_encoding($this->params['item_name'], mb_detect_order(), true), "UTF-8", $this->params['item_name']));
  346. $namePath = date("YmdHi") . '-' . $slug;
  347. $folderPath = 'content' . $folder->path;
  348. $content = json_encode(['# ' . $name, 'Content']);
  349. # initialise write object
  350. $write = new WriteYaml();
  351. # check, if name exists
  352. if($write->checkFile($folderPath, $namePath . '.txt') OR $write->checkFile($folderPath, $namePath . '.md'))
  353. {
  354. return $response->withJson(array('data' => $this->structure, 'errors' => 'There is already a page with this name. Please choose another name.', 'url' => $url), 404);
  355. }
  356. if(!$write->writeFile($folderPath, $namePath . '.txt', $content))
  357. {
  358. return $response->withJson(array('data' => $this->structure, 'errors' => 'We could not create the file. Please refresh the page and check, if all folders and files are writable.', 'url' => $url), 404);
  359. }
  360. # get extended structure
  361. $extended = $write->getYaml('cache', 'structure-extended.yaml');
  362. # create the url for the item
  363. $urlWoF = $folder->urlRelWoF . '/' . $slug;
  364. # add the navigation name to the item htmlspecialchars needed for frensh language
  365. $extended[$urlWoF] = ['hide' => false, 'navtitle' => $name];
  366. # store the extended structure
  367. $write->updateYaml('cache', 'structure-extended.yaml', $extended);
  368. # update the structure for editor
  369. $this->setStructure($draft = true, $cache = false);
  370. $folder = Folder::getItemWithKeyPath($this->structure, $folderKeyPath);
  371. # activate this if you want to redirect after creating the page...
  372. # $url = $this->uri->getBaseUrl() . '/tm/content/' . $this->settings['editor'] . $folder->urlRelWoF . '/' . $slug;
  373. return $response->withJson(array('posts' => $folder, $this->structure, 'errors' => false, 'url' => $url));
  374. }
  375. public function createArticle(Request $request, Response $response, $args)
  376. {
  377. # get params from call
  378. $this->params = $request->getParams();
  379. $this->uri = $request->getUri();
  380. # url is only needed, if an active page is moved
  381. $url = false;
  382. # set structure
  383. if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors, 'url' => $url), 404); }
  384. # validate input
  385. if(!$this->validateNaviItem()){ return $response->withJson(array('data' => $this->structure, 'errors' => 'Special Characters not allowed. Length between 1 and 60 chars.', 'url' => $url), 422); }
  386. # get the ids (key path) for item, old folder and new folder
  387. $folderKeyPath = explode('.', $this->params['folder_id']);
  388. # get the item from structure
  389. $folder = Folder::getItemWithKeyPath($this->structure, $folderKeyPath);
  390. if(!$folder){ return $response->withJson(array('data' => $this->structure, 'errors' => 'We could not find this page. Please refresh and try again.', 'url' => $url), 404); }
  391. # Rename all files within the folder to make sure, that namings and orders are correct
  392. # get the content of the target folder
  393. $folderContent = $folder->folderContent;
  394. $name = $this->params['item_name'];
  395. $slug = URLify::filter(iconv(mb_detect_encoding($this->params['item_name'], mb_detect_order(), true), "UTF-8", $this->params['item_name']));
  396. # create the name for the new item
  397. # $nameParts = Folder::getStringParts($this->params['item_name']);
  398. # $name = implode("-", $nameParts);
  399. # $slug = $name;
  400. # initialize index
  401. $index = 0;
  402. # initialise write object
  403. $write = new WriteYaml();
  404. # iterate through the whole content of the new folder
  405. $writeError = false;
  406. foreach($folderContent as $folderItem)
  407. {
  408. # check, if the same name as new item, then return an error
  409. if($folderItem->slug == $slug)
  410. {
  411. return $response->withJson(array('data' => $this->structure, 'errors' => 'There is already a page with this name. Please choose another name.', 'url' => $url), 404);
  412. }
  413. if(!$write->moveElement($folderItem, $folder->path, $index))
  414. {
  415. $writeError = true;
  416. }
  417. $index++;
  418. }
  419. if($writeError){ return $response->withJson(array('data' => $this->structure, 'errors' => 'Something went wrong. Please refresh the page and check, if all folders and files are writable.', 'url' => $url), 404); }
  420. # add prefix number to the name
  421. # $namePath = $index > 9 ? $index . '-' . $name : '0' . $index . '-' . $name;
  422. $namePath = $index > 9 ? $index . '-' . $slug : '0' . $index . '-' . $slug;
  423. $folderPath = 'content' . $folder->path;
  424. # $title = implode(" ", $nameParts);
  425. # create default content
  426. # $content = json_encode(['# ' . $title, 'Content']);
  427. $content = json_encode(['# ' . $name, 'Content']);
  428. if($this->params['type'] == 'file')
  429. {
  430. if(!$write->writeFile($folderPath, $namePath . '.txt', $content))
  431. {
  432. return $response->withJson(array('data' => $this->structure, 'errors' => 'We could not create the file. Please refresh the page and check, if all folders and files are writable.', 'url' => $url), 404);
  433. }
  434. }
  435. elseif($this->params['type'] == 'folder')
  436. {
  437. if(!$write->checkPath($folderPath . DIRECTORY_SEPARATOR . $namePath))
  438. {
  439. return $response->withJson(array('data' => $this->structure, 'errors' => 'We could not create the folder. Please refresh the page and check, if all folders and files are writable.', 'url' => $url), 404);
  440. }
  441. $write->writeFile($folderPath . DIRECTORY_SEPARATOR . $namePath, 'index.txt', $content);
  442. # always redirect to a folder
  443. $url = $this->uri->getBaseUrl() . '/tm/content/' . $this->settings['editor'] . $folder->urlRelWoF . '/' . $slug;
  444. }
  445. # get extended structure
  446. $extended = $write->getYaml('cache', 'structure-extended.yaml');
  447. # create the url for the item
  448. $urlWoF = $folder->urlRelWoF . '/' . $slug;
  449. # add the navigation name to the item htmlspecialchars needed for frensh language
  450. $extended[$urlWoF] = ['hide' => false, 'navtitle' => $name];
  451. # store the extended structure
  452. $write->updateYaml('cache', 'structure-extended.yaml', $extended);
  453. # update the structure for editor
  454. $this->setStructure($draft = true, $cache = false);
  455. # get item for url and set it active again
  456. if(isset($this->params['url']))
  457. {
  458. $activeItem = Folder::getItemForUrl($this->structure, $this->params['url'], $this->uri->getBaseUrl());
  459. }
  460. # activate this if you want to redirect after creating the page...
  461. # $url = $this->uri->getBaseUrl() . '/tm/content/' . $this->settings['editor'] . $folder->urlRelWoF . '/' . $slug;
  462. return $response->withJson(array('data' => $this->structure, 'errors' => false, 'url' => $url));
  463. }
  464. public function createBaseItem(Request $request, Response $response, $args)
  465. {
  466. # get params from call
  467. $this->params = $request->getParams();
  468. $this->uri = $request->getUri();
  469. # url is only needed, if an active page is moved
  470. $url = false;
  471. # set structure
  472. if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors, 'url' => $url), 404); }
  473. # validate input
  474. if(!$this->validateBaseNaviItem()){ return $response->withJson(array('data' => $this->structure, 'errors' => 'Special Characters not allowed. Length between 1 and 20 chars.', 'url' => $url), 422); }
  475. # create the name for the new item
  476. # $nameParts = Folder::getStringParts($this->params['item_name']);
  477. # $name = implode("-", $nameParts);
  478. # $slug = $name;
  479. $name = $this->params['item_name'];
  480. $slug = URLify::filter(iconv(mb_detect_encoding($this->params['item_name'], mb_detect_order(), true), "UTF-8", $this->params['item_name']));
  481. # initialize index
  482. $index = 0;
  483. # initialise write object
  484. $write = new WriteYaml();
  485. # iterate through the whole content of the new folder
  486. $writeError = false;
  487. foreach($this->structure as $item)
  488. {
  489. # check, if the same name as new item, then return an error
  490. if($item->slug == $slug)
  491. {
  492. return $response->withJson(array('data' => $this->structure, 'errors' => 'There is already a page with this name. Please choose another name.', 'url' => $url), 404);
  493. }
  494. if(!$write->moveElement($item, '', $index))
  495. {
  496. $writeError = true;
  497. }
  498. $index++;
  499. }
  500. if($writeError){ return $response->withJson(array('data' => $this->structure, 'errors' => 'Something went wrong. Please refresh the page and check, if all folders and files are writable.', 'url' => $url), 404); }
  501. # add prefix number to the name
  502. $namePath = $index > 9 ? $index . '-' . $slug : '0' . $index . '-' . $slug;
  503. $folderPath = 'content';
  504. # create default content
  505. # $content = json_encode(['# Add Title', 'Add Content']);
  506. $content = json_encode(['# ' . $name, 'Content']);
  507. if($this->params['type'] == 'file')
  508. {
  509. if(!$write->writeFile($folderPath, $namePath . '.txt', $content))
  510. {
  511. return $response->withJson(array('data' => $this->structure, 'errors' => 'We could not create the file. Please refresh the page and check, if all folders and files are writable.', 'url' => $url), 404);
  512. }
  513. }
  514. elseif($this->params['type'] == 'folder')
  515. {
  516. if(!$write->checkPath($folderPath . DIRECTORY_SEPARATOR . $namePath))
  517. {
  518. return $response->withJson(array('data' => $this->structure, 'errors' => 'We could not create the folder. Please refresh the page and check, if all folders and files are writable.', 'url' => $url), 404);
  519. }
  520. $write->writeFile($folderPath . DIRECTORY_SEPARATOR . $namePath, 'index.txt', $content);
  521. # activate this if you want to redirect after creating the page...
  522. $url = $this->uri->getBaseUrl() . '/tm/content/' . $this->settings['editor'] . '/' . $slug;
  523. }
  524. # get extended structure
  525. $extended = $write->getYaml('cache', 'structure-extended.yaml');
  526. # create the url for the item
  527. $urlWoF = '/' . $slug;
  528. # add the navigation name to the item htmlspecialchars needed for frensh language
  529. $extended[$urlWoF] = ['hide' => false, 'navtitle' => $name];
  530. # store the extended structure
  531. $write->updateYaml('cache', 'structure-extended.yaml', $extended);
  532. # update the structure for editor
  533. $this->setStructure($draft = true, $cache = false);
  534. # get item for url and set it active again
  535. if(isset($this->params['url']))
  536. {
  537. $activeItem = Folder::getItemForUrl($this->structure, $this->params['url'], $this->uri->getBaseUrl());
  538. }
  539. return $response->withJson(array('data' => $this->structure, 'errors' => false, 'url' => $url));
  540. }
  541. public function getNavigation(Request $request, Response $response, $args)
  542. {
  543. # get params from call
  544. $this->params = $request->getParams();
  545. $this->uri = $request->getUri();
  546. # set structure
  547. if(!$this->setStructure($draft = true, $cache = false)){ return $response->withJson(array('data' => false, 'errors' => $this->errors, 'url' => $url), 404); }
  548. # set information for homepage
  549. $this->setHomepage();
  550. # get item for url and set it active again
  551. if(isset($this->params['url']))
  552. {
  553. $activeItem = Folder::getItemForUrl($this->structure, $this->params['url'], $this->uri->getBaseUrl());
  554. }
  555. return $response->withJson(array('data' => $this->structure, 'homepage' => $this->homepage, 'errors' => false));
  556. }
  557. public function getArticleMarkdown(Request $request, Response $response, $args)
  558. {
  559. /* get params from call */
  560. $this->params = $request->getParams();
  561. $this->uri = $request->getUri();
  562. # set structure
  563. if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  564. /* set item */
  565. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  566. # set the status for published and drafted
  567. $this->setPublishStatus();
  568. # set path
  569. $this->setItemPath($this->item->fileType);
  570. # read content from file
  571. if(!$this->setContent()){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  572. $content = $this->content;
  573. if($content == '')
  574. {
  575. $content = [];
  576. }
  577. # if content is not an array, then transform it
  578. if(!is_array($content))
  579. {
  580. # initialize parsedown extension
  581. $parsedown = new ParsedownExtension();
  582. # turn markdown into an array of markdown-blocks
  583. $content = $parsedown->markdownToArrayBlocks($content);
  584. }
  585. # delete markdown from title
  586. if(isset($content[0]))
  587. {
  588. $content[0] = trim($content[0], "# ");
  589. }
  590. return $response->withJson(array('data' => $content, 'errors' => false));
  591. }
  592. public function getArticleHtml(Request $request, Response $response, $args)
  593. {
  594. /* get params from call */
  595. $this->params = $request->getParams();
  596. $this->uri = $request->getUri();
  597. # set structure
  598. if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  599. /* set item */
  600. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  601. # set the status for published and drafted
  602. $this->setPublishStatus();
  603. # set path
  604. $this->setItemPath($this->item->fileType);
  605. # read content from file
  606. if(!$this->setContent()){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  607. $content = $this->content;
  608. if($content == '')
  609. {
  610. $content = [];
  611. }
  612. # initialize parsedown extension
  613. $parsedown = new ParsedownExtension();
  614. # fix footnotes in parsedown, might break with complicated footnotes
  615. $parsedown->setVisualMode();
  616. # if content is not an array, then transform it
  617. if(!is_array($content))
  618. {
  619. # turn markdown into an array of markdown-blocks
  620. $content = $parsedown->markdownToArrayBlocks($content);
  621. }
  622. # needed for ToC links
  623. $relurl = '/tm/content/' . $this->settings['editor'] . '/' . $this->item->urlRel;
  624. # flag for TOC
  625. $toc = false;
  626. # loop through mardkown-array and create html-blocks
  627. foreach($content as $key => $block)
  628. {
  629. # parse markdown-file to content-array
  630. $contentArray = $parsedown->text($block);
  631. if($block == '[TOC]')
  632. {
  633. $toc = $key;
  634. }
  635. # parse markdown-content-array to content-string
  636. $content[$key] = ['id' => $key, 'html' => $parsedown->markup($contentArray, $relurl)];
  637. }
  638. if($toc)
  639. {
  640. $tocMarkup = $parsedown->buildTOC($parsedown->headlines);
  641. $content[$toc] = ['id' => $toc, 'html' => $tocMarkup];
  642. }
  643. return $response->withJson(array('data' => $content, 'errors' => false));
  644. }
  645. public function addBlock(Request $request, Response $response, $args)
  646. {
  647. /* get params from call */
  648. $this->params = $request->getParams();
  649. $this->uri = $request->getUri();
  650. /* validate input */
  651. if(!$this->validateBlockInput()){ return $response->withJson($this->errors,422); }
  652. # set structure
  653. if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  654. /* set item */
  655. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  656. # set the status for published and drafted
  657. $this->setPublishStatus();
  658. # set path
  659. $this->setItemPath($this->item->fileType);
  660. # read content from file
  661. if(!$this->setContent()){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  662. # make it more clear which content we have
  663. $pageMarkdown = $this->content;
  664. $blockMarkdown = $this->params['markdown'];
  665. # standardize line breaks
  666. $blockMarkdown = str_replace(array("\r\n", "\r"), "\n", $blockMarkdown);
  667. # remove surrounding line breaks
  668. $blockMarkdown = trim($blockMarkdown, "\n");
  669. if($pageMarkdown == '')
  670. {
  671. $pageMarkdown = [];
  672. }
  673. # initialize parsedown extension
  674. $parsedown = new ParsedownExtension();
  675. # if content is not an array, then transform it
  676. if(!is_array($pageMarkdown))
  677. {
  678. # turn markdown into an array of markdown-blocks
  679. $pageMarkdown = $parsedown->markdownToArrayBlocks($pageMarkdown);
  680. }
  681. # if it is a new content-block
  682. if($this->params['block_id'] == 99999)
  683. {
  684. # set the id of the markdown-block (it will be one more than the actual array, so count is perfect)
  685. $id = count($pageMarkdown);
  686. # add the new markdown block to the page content
  687. $pageMarkdown[] = $blockMarkdown;
  688. }
  689. elseif(($this->params['block_id'] == 0) OR !isset($pageMarkdown[$this->params['block_id']]))
  690. {
  691. # if the block does not exists, return an error
  692. return $response->withJson(array('data' => false, 'errors' => 'The ID of the content-block is wrong.'), 404);
  693. }
  694. else
  695. {
  696. # insert new markdown block
  697. array_splice( $pageMarkdown, $this->params['block_id'], 0, $blockMarkdown );
  698. $id = $this->params['block_id'];
  699. }
  700. # encode the content into json
  701. $pageJson = json_encode($pageMarkdown);
  702. # set path for the file (or folder)
  703. $this->setItemPath('txt');
  704. /* update the file */
  705. if($this->write->writeFile($this->settings['contentFolder'], $this->path, $pageJson))
  706. {
  707. # update the internal structure
  708. $this->setStructure($draft = true, $cache = false);
  709. $this->content = $pageMarkdown;
  710. }
  711. else
  712. {
  713. return $response->withJson(['errors' => ['message' => 'Could not write to file. Please check if the file is writable']], 404);
  714. }
  715. /* set safe mode to escape javascript and html in markdown */
  716. $parsedown->setSafeMode(true);
  717. /* parse markdown-file to content-array */
  718. $blockArray = $parsedown->text($blockMarkdown);
  719. # we assume that toc is not relevant
  720. $toc = false;
  721. # needed for ToC links
  722. $relurl = '/tm/content/' . $this->settings['editor'] . '/' . $this->item->urlRel;
  723. if($blockMarkdown == '[TOC]')
  724. {
  725. # if block is table of content itself, then generate the table of content
  726. $tableofcontent = $this->generateToc();
  727. # and only use the html-markup
  728. $blockHTML = $tableofcontent['html'];
  729. }
  730. else
  731. {
  732. # parse markdown-content-array to content-string
  733. $blockHTML = $parsedown->markup($blockArray, $relurl);
  734. # if it is a headline
  735. if($blockMarkdown[0] == '#')
  736. {
  737. # then the TOC holds either false (if no toc used in the page) or it holds an object with the id and toc-markup
  738. $toc = $this->generateToc();
  739. }
  740. }
  741. return $response->withJson(array('content' => [ 'id' => $id, 'html' => $blockHTML ] , 'markdown' => $blockMarkdown, 'id' => $id, 'toc' => $toc, 'errors' => false));
  742. }
  743. protected function generateToc()
  744. {
  745. # we assume that page has no table of content
  746. $toc = false;
  747. # make sure $this->content is updated
  748. $content = $this->content;
  749. if($content == '')
  750. {
  751. $content = [];
  752. }
  753. # initialize parsedown extension
  754. $parsedown = new ParsedownExtension();
  755. # if content is not an array, then transform it
  756. if(!is_array($content))
  757. {
  758. # turn markdown into an array of markdown-blocks
  759. $content = $parsedown->markdownToArrayBlocks($content);
  760. }
  761. # needed for ToC links
  762. $relurl = '/tm/content/' . $this->settings['editor'] . '/' . $this->item->urlRel;
  763. # loop through mardkown-array and create html-blocks
  764. foreach($content as $key => $block)
  765. {
  766. # parse markdown-file to content-array
  767. $contentArray = $parsedown->text($block);
  768. if($block == '[TOC]')
  769. {
  770. # toc is true and holds the key of the table of content now
  771. $toc = $key;
  772. }
  773. # parse markdown-content-array to content-string
  774. $content[$key] = ['id' => $key, 'html' => $parsedown->markup($contentArray, $relurl)];
  775. }
  776. # if page has a table of content
  777. if($toc)
  778. {
  779. # generate the toc markup
  780. $tocMarkup = $parsedown->buildTOC($parsedown->headlines);
  781. # toc holds the id of the table of content and the html-markup now
  782. $toc = ['id' => $toc, 'html' => $tocMarkup];
  783. }
  784. return $toc;
  785. }
  786. public function updateBlock(Request $request, Response $response, $args)
  787. {
  788. /* get params from call */
  789. $this->params = $request->getParams();
  790. $this->uri = $request->getUri();
  791. /* validate input */
  792. if(!$this->validateBlockInput()){ return $response->withJson($this->errors,422); }
  793. # set structure
  794. if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  795. /* set item */
  796. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  797. # set the status for published and drafted
  798. $this->setPublishStatus();
  799. # set path
  800. $this->setItemPath($this->item->fileType);
  801. # read content from file
  802. if(!$this->setContent()){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  803. # make it more clear which content we have
  804. $pageMarkdown = $this->content;
  805. $blockMarkdown = $this->params['markdown'];
  806. # standardize line breaks
  807. $blockMarkdown = str_replace(array("\r\n", "\r"), "\n", $blockMarkdown);
  808. # remove surrounding line breaks
  809. $blockMarkdown = trim($blockMarkdown, "\n");
  810. if($pageMarkdown == '')
  811. {
  812. $pageMarkdown = [];
  813. }
  814. # initialize parsedown extension
  815. $parsedown = new ParsedownExtension();
  816. $parsedown->setVisualMode();
  817. # if content is not an array, then transform it
  818. if(!is_array($pageMarkdown))
  819. {
  820. # turn markdown into an array of markdown-blocks
  821. $pageMarkdown = $parsedown->markdownToArrayBlocks($pageMarkdown);
  822. }
  823. if(!isset($pageMarkdown[$this->params['block_id']]))
  824. {
  825. # if the block does not exists, return an error
  826. return $response->withJson(array('data' => false, 'errors' => 'The ID of the content-block is wrong.'), 404);
  827. }
  828. elseif($this->params['block_id'] == 0)
  829. {
  830. # if it is the title, then delete the "# " if it exists
  831. $blockMarkdown = trim($blockMarkdown, "# ");
  832. # store the markdown-headline in a separate variable
  833. $blockMarkdownTitle = '# ' . $blockMarkdown;
  834. # add the markdown-headline to the page-markdown
  835. $pageMarkdown[0] = $blockMarkdownTitle;
  836. $id = 0;
  837. }
  838. else
  839. {
  840. # update the markdown block in the page content
  841. $pageMarkdown[$this->params['block_id']] = $blockMarkdown;
  842. $id = $this->params['block_id'];
  843. }
  844. # encode the content into json
  845. $pageJson = json_encode($pageMarkdown);
  846. # set path for the file (or folder)
  847. $this->setItemPath('txt');
  848. /* update the file */
  849. if($this->write->writeFile($this->settings['contentFolder'], $this->path, $pageJson))
  850. {
  851. # update the internal structure
  852. $this->setStructure($draft = true, $cache = false);
  853. # updated the content variable
  854. $this->content = $pageMarkdown;
  855. }
  856. else
  857. {
  858. return $response->withJson(['errors' => ['message' => 'Could not write to file. Please check if the file is writable']], 404);
  859. }
  860. /* parse markdown-file to content-array, if title parse title. */
  861. if($this->params['block_id'] == 0)
  862. {
  863. $blockArray = $parsedown->text($blockMarkdownTitle);
  864. }
  865. else
  866. {
  867. /* set safe mode to escape javascript and html in markdown */
  868. $parsedown->setSafeMode(true);
  869. $blockArray = $parsedown->text($blockMarkdown);
  870. }
  871. # we assume that toc is not relevant
  872. $toc = false;
  873. # needed for ToC links
  874. $relurl = '/tm/content/' . $this->settings['editor'] . '/' . $this->item->urlRel;
  875. if($blockMarkdown == '[TOC]')
  876. {
  877. # if block is table of content itself, then generate the table of content
  878. $tableofcontent = $this->generateToc();
  879. # and only use the html-markup
  880. $blockHTML = $tableofcontent['html'];
  881. }
  882. else
  883. {
  884. # parse markdown-content-array to content-string
  885. $blockHTML = $parsedown->markup($blockArray, $relurl);
  886. # if it is a headline
  887. if($blockMarkdown[0] == '#')
  888. {
  889. # then the TOC holds either false (if no toc used in the page) or it holds an object with the id and toc-markup
  890. $toc = $this->generateToc();
  891. }
  892. }
  893. return $response->withJson(array('content' => [ 'id' => $id, 'html' => $blockHTML ] , 'markdown' => $blockMarkdown, 'id' => $id, 'toc' => $toc, 'errors' => false));
  894. }
  895. public function moveBlock(Request $request, Response $response, $args)
  896. {
  897. # get params from call
  898. $this->params = $request->getParams();
  899. $this->uri = $request->getUri();
  900. # validate input
  901. # if(!$this->validateBlockInput()){ return $response->withJson($this->errors,422); }
  902. # set structure
  903. if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  904. # set item
  905. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  906. # set the status for published and drafted
  907. $this->setPublishStatus();
  908. # set path
  909. $this->setItemPath($this->item->fileType);
  910. # read content from file
  911. if(!$this->setContent()){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  912. # make it more clear which content we have
  913. $pageMarkdown = $this->content;
  914. if($pageMarkdown == '')
  915. {
  916. $pageMarkdown = [];
  917. }
  918. # initialize parsedown extension
  919. $parsedown = new ParsedownExtension();
  920. # if content is not an array, then transform it
  921. if(!is_array($pageMarkdown))
  922. {
  923. # turn markdown into an array of markdown-blocks
  924. $pageMarkdown = $parsedown->markdownToArrayBlocks($pageMarkdown);
  925. }
  926. $oldIndex = ($this->params['old_index'] + 1);
  927. $newIndex = ($this->params['new_index'] + 1);
  928. if(!isset($pageMarkdown[$oldIndex]))
  929. {
  930. # if the block does not exists, return an error
  931. return $response->withJson(array('data' => false, 'errors' => 'The ID of the content-block is wrong.'), 404);
  932. }
  933. $extract = array_splice($pageMarkdown, $oldIndex, 1);
  934. array_splice($pageMarkdown, $newIndex, 0, $extract);
  935. # encode the content into json
  936. $pageJson = json_encode($pageMarkdown);
  937. # set path for the file (or folder)
  938. $this->setItemPath('txt');
  939. /* update the file */
  940. if($this->write->writeFile($this->settings['contentFolder'], $this->path, $pageJson))
  941. {
  942. # update the internal structure
  943. $this->setStructure($draft = true, $cache = false);
  944. # update this content
  945. $this->content = $pageMarkdown;
  946. }
  947. else
  948. {
  949. return $response->withJson(['errors' => ['message' => 'Could not write to file. Please check if the file is writable']], 404);
  950. }
  951. # we assume that toc is not relevant
  952. $toc = false;
  953. # needed for ToC links
  954. $relurl = '/tm/content/' . $this->settings['editor'] . '/' . $this->item->urlRel;
  955. # if the moved item is a headline
  956. if($extract[0][0] == '#')
  957. {
  958. $toc = $this->generateToc();
  959. }
  960. # if it is the title, then delete the "# " if it exists
  961. $pageMarkdown[0] = trim($pageMarkdown[0], "# ");
  962. return $response->withJson(array('markdown' => $pageMarkdown, 'toc' => $toc, 'errors' => false));
  963. }
  964. public function deleteBlock(Request $request, Response $response, $args)
  965. {
  966. /* get params from call */
  967. $this->params = $request->getParams();
  968. $this->uri = $request->getUri();
  969. $errors = false;
  970. # set structure
  971. if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  972. # set item
  973. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  974. # set the status for published and drafted
  975. $this->setPublishStatus();
  976. # set path
  977. $this->setItemPath($this->item->fileType);
  978. # read content from file
  979. if(!$this->setContent()){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  980. # get content
  981. $this->content;
  982. if($this->content == '')
  983. {
  984. $this->content = [];
  985. }
  986. # initialize parsedown extension
  987. $parsedown = new ParsedownExtension();
  988. # if content is not an array, then transform it
  989. if(!is_array($this->content))
  990. {
  991. # turn markdown into an array of markdown-blocks
  992. $this->content = $parsedown->markdownToArrayBlocks($this->content);
  993. }
  994. # check if id exists
  995. if(!isset($this->content[$this->params['block_id']])){ return $response->withJson(array('data' => false, 'errors' => 'The ID of the content-block is wrong.'), 404); }
  996. # check if block is image
  997. $contentBlock = $this->content[$this->params['block_id']];
  998. $contentBlockStart = substr($contentBlock, 0, 2);
  999. if($contentBlockStart == '[!' OR $contentBlockStart == '![')
  1000. {
  1001. # extract image path
  1002. preg_match("/\((.*?)\)/",$contentBlock,$matches);
  1003. if(isset($matches[1]))
  1004. {
  1005. $imageBaseName = explode('-', $matches[1]);
  1006. $imageBaseName = str_replace('media/live/', '', $imageBaseName[0]);
  1007. $processImage = new ProcessImage();
  1008. if(!$processImage->deleteImage($imageBaseName))
  1009. {
  1010. $errors = 'Could not delete some of the images, please check manually';
  1011. }
  1012. }
  1013. }
  1014. # delete the block
  1015. unset($this->content[$this->params['block_id']]);
  1016. $this->content = array_values($this->content);
  1017. $pageMarkdown = $this->content;
  1018. # delete markdown from title
  1019. if(isset($pageMarkdown[0]))
  1020. {
  1021. $pageMarkdown[0] = trim($pageMarkdown[0], "# ");
  1022. }
  1023. # encode the content into json
  1024. $pageJson = json_encode($this->content);
  1025. # set path for the file (or folder)
  1026. $this->setItemPath('txt');
  1027. /* update the file */
  1028. if($this->write->writeFile($this->settings['contentFolder'], $this->path, $pageJson))
  1029. {
  1030. # update the internal structure
  1031. $this->setStructure($draft = true, $cache = false);
  1032. }
  1033. else
  1034. {
  1035. return $response->withJson(['errors' => ['message' => 'Could not write to file. Please check if the file is writable']], 404);
  1036. }
  1037. $toc = false;
  1038. if($contentBlock[0] == '#')
  1039. {
  1040. $toc = $this->generateToc();
  1041. }
  1042. return $response->withJson(array('markdown' => $pageMarkdown, 'toc' => $toc, 'errors' => $errors));
  1043. }
  1044. public function createImage(Request $request, Response $response, $args)
  1045. {
  1046. /* get params from call */
  1047. $this->params = $request->getParams();
  1048. $this->uri = $request->getUri();
  1049. $imageProcessor = new ProcessImage();
  1050. if($imageProcessor->createImage($this->params['image'], $this->settings['images']))
  1051. {
  1052. return $response->withJson(array('errors' => false));
  1053. }
  1054. return $response->withJson(array('errors' => 'could not store image to temporary folder'));
  1055. }
  1056. public function publishImage(Request $request, Response $response, $args)
  1057. {
  1058. $params = $request->getParsedBody();
  1059. $imageProcessor = new ProcessImage();
  1060. $imageUrl = $imageProcessor->publishImage($this->settings['images'], $name = false);
  1061. if($imageUrl)
  1062. {
  1063. $params['markdown'] = str_replace('imgplchldr', $imageUrl, $params['markdown']);
  1064. $request = $request->withParsedBody($params);
  1065. return $this->addBlock($request, $response, $args);
  1066. }
  1067. return $response->withJson(array('errors' => 'could not store image to media folder'));
  1068. }
  1069. public function saveVideoImage(Request $request, Response $response, $args)
  1070. {
  1071. /* get params from call */
  1072. $this->params = $request->getParams();
  1073. $this->uri = $request->getUri();
  1074. $class = false;
  1075. $imageUrl = $this->params['markdown'];
  1076. if(strpos($imageUrl, 'https://www.youtube.com/watch?v=') !== false)
  1077. {
  1078. $videoID = str_replace('https://www.youtube.com/watch?v=', '', $imageUrl);
  1079. $videoID = strpos($videoID, '&') ? substr($videoID, 0, strpos($videoID, '&')) : $videoID;
  1080. $class = 'youtube';
  1081. }
  1082. if(strpos($imageUrl, 'https://youtu.be/') !== false)
  1083. {
  1084. $videoID = str_replace('https://youtu.be/', '', $imageUrl);
  1085. $videoID = strpos($videoID, '?') ? substr($videoID, 0, strpos($videoID, '?')) : $videoID;
  1086. $class = 'youtube';
  1087. }
  1088. if($class == 'youtube')
  1089. {
  1090. $videoURLmaxres = 'https://i1.ytimg.com/vi/' . $videoID . '/maxresdefault.jpg';
  1091. $videoURL0 = 'https://i1.ytimg.com/vi/' . $videoID . '/0.jpg';
  1092. }
  1093. $ctx = stream_context_create(array(
  1094. 'https' => array(
  1095. 'timeout' => 1
  1096. )
  1097. )
  1098. );
  1099. $imageData = @file_get_contents($videoURLmaxres, 0, $ctx);
  1100. if($imageData === false)
  1101. {
  1102. $imageData = @file_get_contents($videoURL0, 0, $ctx);
  1103. if($imageData === false)
  1104. {
  1105. return $response->withJson(array('errors' => 'could not get the video image'));
  1106. }
  1107. }
  1108. $imageData64 = 'data:image/jpeg;base64,' . base64_encode($imageData);
  1109. $desiredSizes = ['live' => ['width' => 560, 'height' => 315]];
  1110. $imageProcessor = new ProcessImage();
  1111. $tmpImage = $imageProcessor->createImage($imageData64, $desiredSizes);
  1112. if(!$tmpImage)
  1113. {
  1114. return $response->withJson(array('errors' => 'could not create temporary image'));
  1115. }
  1116. $imageUrl = $imageProcessor->publishImage($desiredSizes, $videoID);
  1117. if($imageUrl)
  1118. {
  1119. $this->params['markdown'] = '![' . $class . '-video](' . $imageUrl . ' "click to load video"){#' . $videoID. ' .' . $class . '}';
  1120. $request = $request->withParsedBody($this->params);
  1121. return $this->addBlock($request, $response, $args);
  1122. }
  1123. return $response->withJson(array('errors' => 'could not store the preview image'));
  1124. }
  1125. }