ContentApiController.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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\ProcessImage;
  8. use Typemill\Extensions\ParsedownExtension;
  9. class ContentApiController extends ContentController
  10. {
  11. public function publishArticle(Request $request, Response $response, $args)
  12. {
  13. # get params from call
  14. $this->params = $request->getParams();
  15. $this->uri = $request->getUri();
  16. # validate input only if raw mode
  17. if($this->params['raw'])
  18. {
  19. if(!$this->validateEditorInput()){ return $response->withJson($this->errors,422); }
  20. }
  21. # set structure
  22. if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
  23. # set item
  24. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  25. # set the status for published and drafted
  26. $this->setPublishStatus();
  27. # set path
  28. $this->setItemPath($this->item->fileType);
  29. # if raw mode, use the content from request
  30. if($this->params['raw'])
  31. {
  32. $this->content = '# ' . $this->params['title'] . "\r\n\r\n" . $this->params['content'];
  33. }
  34. else
  35. {
  36. # read content from file
  37. if(!$this->setContent()){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  38. # If it is a draft, then create clean markdown content
  39. if(is_array($this->content))
  40. {
  41. # initialize parsedown extension
  42. $parsedown = new ParsedownExtension();
  43. # turn markdown into an array of markdown-blocks
  44. $this->content = $parsedown->arrayBlocksToMarkdown($this->content);
  45. }
  46. }
  47. # set path for the file (or folder)
  48. $this->setItemPath('md');
  49. # update the file
  50. if($this->write->writeFile($this->settings['contentFolder'], $this->path, $this->content))
  51. {
  52. # update the file
  53. $delete = $this->deleteContentFiles(['txt']);
  54. # update the internal structure
  55. $this->setStructure($draft = true, $cache = false);
  56. # update the public structure
  57. $this->setStructure($draft = false, $cache = false);
  58. return $response->withJson(['success'], 200);
  59. }
  60. else
  61. {
  62. return $response->withJson(['errors' => ['message' => 'Could not write to file. Please check if the file is writable']], 404);
  63. }
  64. }
  65. public function unpublishArticle(Request $request, Response $response, $args)
  66. {
  67. # get params from call
  68. $this->params = $request->getParams();
  69. $this->uri = $request->getUri();
  70. # set structure
  71. if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
  72. # set item
  73. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  74. # set the status for published and drafted
  75. $this->setPublishStatus();
  76. # check if draft exists, if not, create one.
  77. if(!$this->item->drafted)
  78. {
  79. # set path for the file (or folder)
  80. $this->setItemPath('md');
  81. # set content of markdown-file
  82. if(!$this->setContent()){ return $response->withJson($this->errors, 404); }
  83. # initialize parsedown extension
  84. $parsedown = new ParsedownExtension();
  85. # turn markdown into an array of markdown-blocks
  86. $contentArray = $parsedown->markdownToArrayBlocks($this->content);
  87. # encode the content into json
  88. $contentJson = json_encode($contentArray);
  89. # set path for the file (or folder)
  90. $this->setItemPath('txt');
  91. /* update the file */
  92. if(!$this->write->writeFile($this->settings['contentFolder'], $this->path, $contentJson))
  93. {
  94. return $response->withJson(['errors' => ['message' => 'Could not create a draft of the page. Please check if the folder is writable']], 404);
  95. }
  96. }
  97. # update the file
  98. $delete = $this->deleteContentFiles(['md']);
  99. if($delete)
  100. {
  101. # update the internal structure
  102. $this->setStructure($draft = true, $cache = false);
  103. # update the live structure
  104. $this->setStructure($draft = false, $cache = false);
  105. return $response->withJson(['success'], 200);
  106. }
  107. else
  108. {
  109. return $response->withJson(['errors' => ['message' => "Could not delete some files. Please check if the files exists and are writable"]], 404);
  110. }
  111. }
  112. public function deleteArticle(Request $request, Response $response, $args)
  113. {
  114. # get params from call
  115. $this->params = $request->getParams();
  116. $this->uri = $request->getUri();
  117. # set url to base path initially
  118. $url = $this->uri->getBaseUrl() . '/tm/content/' . $this->settings['editor'];
  119. # set structure
  120. if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
  121. # set item
  122. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  123. if($this->item->elementType == 'file')
  124. {
  125. $delete = $this->deleteContentFiles(['md','txt']);
  126. }
  127. elseif($this->item->elementType == 'folder')
  128. {
  129. $delete = $this->deleteContentFolder();
  130. }
  131. if($delete)
  132. {
  133. # check if it is a subfile or subfolder and set the redirect-url to the parent item
  134. if(count($this->item->keyPathArray) > 1)
  135. {
  136. # get the parent item
  137. $parentItem = Folder::getParentItem($this->structure, $this->item->keyPathArray);
  138. if($parentItem)
  139. {
  140. # an active file has been moved to another folder
  141. $url .= $parentItem->urlRelWoF;
  142. }
  143. }
  144. # update the live structure
  145. $this->setStructure($draft = false, $cache = false);
  146. #update the backend structure
  147. $this->setStructure($draft = true, $cache = false);
  148. return $response->withJson(array('data' => $this->structure, 'errors' => false, 'url' => $url), 200);
  149. }
  150. else
  151. {
  152. return $response->withJson(array('data' => $this->structure, 'errors' => $this->errors), 404);
  153. }
  154. }
  155. public function updateArticle(Request $request, Response $response, $args)
  156. {
  157. # get params from call
  158. $this->params = $request->getParams();
  159. $this->uri = $request->getUri();
  160. # validate input
  161. if(!$this->validateEditorInput()){ return $response->withJson($this->errors,422); }
  162. # set structure
  163. if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
  164. # set item
  165. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  166. # set path for the file (or folder)
  167. $this->setItemPath('txt');
  168. # merge title with content for complete markdown document
  169. $updatedContent = '# ' . $this->params['title'] . "\r\n\r\n" . $this->params['content'];
  170. # initialize parsedown extension
  171. $parsedown = new ParsedownExtension();
  172. # turn markdown into an array of markdown-blocks
  173. $contentArray = $parsedown->markdownToArrayBlocks($updatedContent);
  174. # encode the content into json
  175. $contentJson = json_encode($contentArray);
  176. /* update the file */
  177. if($this->write->writeFile($this->settings['contentFolder'], $this->path, $contentJson))
  178. {
  179. # update the internal structure
  180. $this->setStructure($draft = true, $cache = false);
  181. return $response->withJson(['success'], 200);
  182. }
  183. else
  184. {
  185. return $response->withJson(['errors' => ['message' => 'Could not write to file. Please check if the file is writable']], 404);
  186. }
  187. }
  188. public function sortArticle(Request $request, Response $response, $args)
  189. {
  190. # get params from call
  191. $this->params = $request->getParams();
  192. $this->uri = $request->getUri();
  193. # url is only needed, if an active page is moved
  194. $url = false;
  195. # set structure
  196. if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors, 'url' => $url), 404); }
  197. # validate input
  198. if(!$this->validateNavigationSort()){ return $response->withJson(array('data' => $this->structure, 'errors' => 'Data not valid. Please refresh the page and try again.', 'url' => $url), 422); }
  199. # get the ids (key path) for item, old folder and new folder
  200. $itemKeyPath = explode('.', $this->params['item_id']);
  201. $parentKeyFrom = explode('.', $this->params['parent_id_from']);
  202. $parentKeyTo = explode('.', $this->params['parent_id_to']);
  203. # get the item from structure
  204. $item = Folder::getItemWithKeyPath($this->structure, $itemKeyPath);
  205. if(!$item){ return $response->withJson(array('data' => $this->structure, 'errors' => 'We could not find this page. Please refresh and try again.', 'url' => $url), 404); }
  206. # if a folder is moved on the first level
  207. if($this->params['parent_id_from'] == 'navi')
  208. {
  209. # create empty and default values so that the logic below still works
  210. $newFolder = new \stdClass();
  211. $newFolder->path = '';
  212. $folderContent = $this->structure;
  213. }
  214. else
  215. {
  216. # get the target folder from structure
  217. $newFolder = Folder::getItemWithKeyPath($this->structure, $parentKeyTo);
  218. # get the content of the target folder
  219. $folderContent = $newFolder->folderContent;
  220. }
  221. # if the item has been moved within the same folder
  222. if($this->params['parent_id_from'] == $this->params['parent_id_to'])
  223. {
  224. # get key of item
  225. $itemKey = end($itemKeyPath);
  226. reset($itemKeyPath);
  227. # delete item from folderContent
  228. unset($folderContent[$itemKey]);
  229. }
  230. elseif($this->params['active'] == 'active')
  231. {
  232. # an active file has been moved to another folder
  233. $url = $this->uri->getBaseUrl() . '/tm/content' . $newFolder->urlRelWoF . '/' . $item->slug;
  234. }
  235. # add item to newFolder
  236. array_splice($folderContent, $this->params['index_new'], 0, array($item));
  237. # initialize index
  238. $index = 0;
  239. # initialise write object
  240. $write = new Write();
  241. # iterate through the whole content of the new folder
  242. $writeError = false;
  243. foreach($folderContent as $folderItem)
  244. {
  245. if(!$write->moveElement($folderItem, $newFolder->path, $index))
  246. {
  247. $writeError = true;
  248. }
  249. $index++;
  250. }
  251. 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); }
  252. # update the structure for editor
  253. $this->setStructure($draft = true, $cache = false);
  254. # get item for url and set it active again
  255. if(isset($this->params['url']))
  256. {
  257. $activeItem = Folder::getItemForUrl($this->structure, $this->params['url']);
  258. }
  259. # keep the internal structure for response
  260. $internalStructure = $this->structure;
  261. # update the structure for website
  262. $this->setStructure($draft = false, $cache = false);
  263. return $response->withJson(array('data' => $internalStructure, 'errors' => false, 'url' => $url));
  264. }
  265. public function createArticle(Request $request, Response $response, $args)
  266. {
  267. # get params from call
  268. $this->params = $request->getParams();
  269. $this->uri = $request->getUri();
  270. # url is only needed, if an active page is moved
  271. $url = false;
  272. # set structure
  273. if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors, 'url' => $url), 404); }
  274. # validate input
  275. if(!$this->validateNaviItem()){ return $response->withJson(array('data' => $this->structure, 'errors' => 'Special Characters not allowed. Length between 1 and 20 chars.', 'url' => $url), 422); }
  276. # get the ids (key path) for item, old folder and new folder
  277. $folderKeyPath = explode('.', $this->params['folder_id']);
  278. # get the item from structure
  279. $folder = Folder::getItemWithKeyPath($this->structure, $folderKeyPath);
  280. if(!$folder){ return $response->withJson(array('data' => $this->structure, 'errors' => 'We could not find this page. Please refresh and try again.', 'url' => $url), 404); }
  281. # Rename all files within the folder to make sure, that namings and orders are correct
  282. # get the content of the target folder
  283. $folderContent = $folder->folderContent;
  284. # create the name for the new item
  285. $nameParts = Folder::getStringParts($this->params['item_name']);
  286. $name = implode("-", $nameParts);
  287. $slug = $name;
  288. # initialize index
  289. $index = 0;
  290. # initialise write object
  291. $write = new Write();
  292. # iterate through the whole content of the new folder
  293. $writeError = false;
  294. foreach($folderContent as $folderItem)
  295. {
  296. # check, if the same name as new item, then return an error
  297. if($folderItem->slug == $slug)
  298. {
  299. return $response->withJson(array('data' => $this->structure, 'errors' => 'There is already a page with this name. Please choose another name.', 'url' => $url), 404);
  300. }
  301. if(!$write->moveElement($folderItem, $folder->path, $index))
  302. {
  303. $writeError = true;
  304. }
  305. $index++;
  306. }
  307. 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); }
  308. # add prefix number to the name
  309. $namePath = $index > 9 ? $index . '-' . $name : '0' . $index . '-' . $name;
  310. $folderPath = 'content' . $folder->path;
  311. # create default content
  312. $content = json_encode(['# Add Title', 'Add Content']);
  313. if($this->params['type'] == 'file')
  314. {
  315. if(!$write->writeFile($folderPath, $namePath . '.txt', $content))
  316. {
  317. 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);
  318. }
  319. }
  320. elseif($this->params['type'] == 'folder')
  321. {
  322. if(!$write->checkPath($folderPath . DIRECTORY_SEPARATOR . $namePath))
  323. {
  324. 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);
  325. }
  326. $write->writeFile($folderPath . DIRECTORY_SEPARATOR . $namePath, 'index.txt', $content);
  327. }
  328. # update the structure for editor
  329. $this->setStructure($draft = true, $cache = false);
  330. # get item for url and set it active again
  331. if(isset($this->params['url']))
  332. {
  333. $activeItem = Folder::getItemForUrl($this->structure, $this->params['url']);
  334. }
  335. # activate this if you want to redirect after creating the page...
  336. # $url = $this->uri->getBaseUrl() . '/tm/content' . $folder->urlRelWoF . '/' . $name;
  337. return $response->withJson(array('data' => $this->structure, 'errors' => false, 'url' => $url));
  338. }
  339. public function createBaseFolder(Request $request, Response $response, $args)
  340. {
  341. # get params from call
  342. $this->params = $request->getParams();
  343. $this->uri = $request->getUri();
  344. # url is only needed, if an active page is moved
  345. $url = false;
  346. # set structure
  347. if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors, 'url' => $url), 404); }
  348. # validate input
  349. #if(!$this->validateBaseFolder()){ return $response->withJson(array('data' => $this->structure, 'errors' => 'Special Characters not allowed. Length between 1 and 20 chars.', 'url' => $url), 422); }
  350. # create the name for the new item
  351. $nameParts = Folder::getStringParts($this->params['item_name']);
  352. $name = implode("-", $nameParts);
  353. $slug = $name;
  354. # initialize index
  355. $index = 0;
  356. # initialise write object
  357. $write = new Write();
  358. # iterate through the whole content of the new folder
  359. $writeError = false;
  360. foreach($this->structure as $folder)
  361. {
  362. # check, if the same name as new item, then return an error
  363. if($folder->slug == $slug)
  364. {
  365. return $response->withJson(array('data' => $this->structure, 'errors' => 'There is already a page with this name. Please choose another name.', 'url' => $url), 404);
  366. }
  367. if(!$write->moveElement($folder, '', $index))
  368. {
  369. $writeError = true;
  370. }
  371. $index++;
  372. }
  373. 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); }
  374. # add prefix number to the name
  375. $namePath = $index > 9 ? $index . '-' . $name : '0' . $index . '-' . $name;
  376. $folderPath = 'content';
  377. if(!$write->checkPath($folderPath . DIRECTORY_SEPARATOR . $namePath))
  378. {
  379. 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);
  380. }
  381. # create default content
  382. $content = json_encode(['# Add Title', 'Add Content']);
  383. $write->writeFile($folderPath . DIRECTORY_SEPARATOR . $namePath, 'index.txt', $content);
  384. # update the structure for editor
  385. $this->setStructure($draft = true, $cache = false);
  386. # get item for url and set it active again
  387. if(isset($this->params['url']))
  388. {
  389. $activeItem = Folder::getItemForUrl($this->structure, $this->params['url']);
  390. }
  391. return $response->withJson(array('data' => $this->structure, 'errors' => false, 'url' => $url));
  392. }
  393. public function getArticleMarkdown(Request $request, Response $response, $args)
  394. {
  395. /* get params from call */
  396. $this->params = $request->getParams();
  397. $this->uri = $request->getUri();
  398. # set structure
  399. if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  400. /* set item */
  401. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  402. # set the status for published and drafted
  403. $this->setPublishStatus();
  404. # set path
  405. $this->setItemPath($this->item->fileType);
  406. # read content from file
  407. if(!$this->setContent()){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  408. $content = $this->content;
  409. if($content == '')
  410. {
  411. $content = [];
  412. }
  413. # if content is not an array, then transform it
  414. if(!is_array($content))
  415. {
  416. # initialize parsedown extension
  417. $parsedown = new ParsedownExtension();
  418. # turn markdown into an array of markdown-blocks
  419. $content = $parsedown->markdownToArrayBlocks($content);
  420. }
  421. # delete markdown from title
  422. if(isset($content[0]))
  423. {
  424. $content[0] = trim($content[0], "# ");
  425. }
  426. return $response->withJson(array('data' => $content, 'errors' => false));
  427. }
  428. public function updateBlock(Request $request, Response $response, $args)
  429. {
  430. /* get params from call */
  431. $this->params = $request->getParams();
  432. $this->uri = $request->getUri();
  433. /* validate input */
  434. if(!$this->validateBlockInput()){ return $response->withJson($this->errors,422); }
  435. # set structure
  436. if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  437. /* set item */
  438. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  439. # set the status for published and drafted
  440. $this->setPublishStatus();
  441. # set path
  442. $this->setItemPath($this->item->fileType);
  443. # read content from file
  444. if(!$this->setContent()){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  445. # make it more clear which content we have
  446. $pageMarkdown = $this->content;
  447. $blockMarkdown = $this->params['markdown'];
  448. # standardize line breaks
  449. $blockMarkdown = str_replace(array("\r\n", "\r"), "\n", $blockMarkdown);
  450. # remove surrounding line breaks
  451. $blockMarkdown = trim($blockMarkdown, "\n");
  452. if($pageMarkdown == '')
  453. {
  454. $pageMarkdown = [];
  455. }
  456. # initialize parsedown extension
  457. $parsedown = new ParsedownExtension();
  458. # if content is not an array, then transform it
  459. if(!is_array($pageMarkdown))
  460. {
  461. # turn markdown into an array of markdown-blocks
  462. $pageMarkdown = $parsedown->markdownToArrayBlocks($pageMarkdown);
  463. }
  464. # if it is a new content-block
  465. if($this->params['block_id'] == 99999)
  466. {
  467. # set the id of the markdown-block (it will be one more than the actual array, so count is perfect)
  468. $id = count($pageMarkdown);
  469. # set the id with prefix "blox-"
  470. $blockId = 'blox-' . $id;
  471. # add the new markdown block to the page content
  472. $pageMarkdown[] = $blockMarkdown;
  473. }
  474. elseif(!isset($pageMarkdown[$this->params['block_id']]))
  475. {
  476. # if the block does not exists, return an error
  477. return $response->withJson(array('data' => false, 'errors' => 'The ID of the content-block is wrong.'), 404);
  478. }
  479. elseif($this->params['block_id'] == 0)
  480. {
  481. # if it is the title, then delete the "# " if it exists
  482. $blockMarkdown = trim($blockMarkdown, "# ");
  483. # store the markdown-headline in a separate variable
  484. $blockMarkdownTitle = '# ' . $blockMarkdown;
  485. # add the markdown-headline to the page-markdown
  486. $pageMarkdown[0] = $blockMarkdownTitle;
  487. $id = 0;
  488. $blockId = 0;
  489. }
  490. else
  491. {
  492. # update the markdown block in the page content
  493. $pageMarkdown[$this->params['block_id']] = $blockMarkdown;
  494. $id = $this->params['block_id'];
  495. $blockId = $this->params['block_id'];
  496. }
  497. # encode the content into json
  498. $pageJson = json_encode($pageMarkdown);
  499. # set path for the file (or folder)
  500. $this->setItemPath('txt');
  501. /* update the file */
  502. if($this->write->writeFile($this->settings['contentFolder'], $this->path, $pageJson))
  503. {
  504. # update the internal structure
  505. $this->setStructure($draft = true, $cache = false);
  506. }
  507. else
  508. {
  509. return $response->withJson(['errors' => ['message' => 'Could not write to file. Please check if the file is writable']], 404);
  510. }
  511. /* set safe mode to escape javascript and html in markdown */
  512. $parsedown->setSafeMode(true);
  513. /* parse markdown-file to content-array, if title parse title. */
  514. if($this->params['block_id'] == 0)
  515. {
  516. $blockArray = $parsedown->text($blockMarkdownTitle);
  517. }
  518. else
  519. {
  520. $blockArray = $parsedown->text($blockMarkdown);
  521. }
  522. # needed for ToC links
  523. $relurl = '/tm/content/' . $this->settings['editor'] . '/' . $this->item->urlRel;
  524. /* parse markdown-content-array to content-string */
  525. $blockHTML = $parsedown->markup($blockArray, $relurl);
  526. return $response->withJson(array('content' => $blockHTML, 'markdown' => $blockMarkdown, 'blockId' => $blockId, 'id' => $id, 'errors' => false));
  527. }
  528. public function moveBlock(Request $request, Response $response, $args)
  529. {
  530. # get params from call
  531. $this->params = $request->getParams();
  532. $this->uri = $request->getUri();
  533. # validate input
  534. # if(!$this->validateBlockInput()){ return $response->withJson($this->errors,422); }
  535. # set structure
  536. if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  537. # set item
  538. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  539. # set the status for published and drafted
  540. $this->setPublishStatus();
  541. # set path
  542. $this->setItemPath($this->item->fileType);
  543. # read content from file
  544. if(!$this->setContent()){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  545. # make it more clear which content we have
  546. $pageMarkdown = $this->content;
  547. if($pageMarkdown == '')
  548. {
  549. $pageMarkdown = [];
  550. }
  551. # initialize parsedown extension
  552. $parsedown = new ParsedownExtension();
  553. # if content is not an array, then transform it
  554. if(!is_array($pageMarkdown))
  555. {
  556. # turn markdown into an array of markdown-blocks
  557. $pageMarkdown = $parsedown->markdownToArrayBlocks($pageMarkdown);
  558. }
  559. $oldIndex = ($this->params['old_index'] + 1);
  560. $newIndex = ($this->params['new_index'] + 1);
  561. if(!isset($pageMarkdown[$oldIndex]))
  562. {
  563. # if the block does not exists, return an error
  564. return $response->withJson(array('data' => false, 'errors' => 'The ID of the content-block is wrong.'), 404);
  565. }
  566. $extract = array_splice($pageMarkdown, $oldIndex, 1);
  567. array_splice($pageMarkdown, $newIndex, 0, $extract);
  568. # encode the content into json
  569. $pageJson = json_encode($pageMarkdown);
  570. # set path for the file (or folder)
  571. $this->setItemPath('txt');
  572. /* update the file */
  573. if($this->write->writeFile($this->settings['contentFolder'], $this->path, $pageJson))
  574. {
  575. # update the internal structure
  576. $this->setStructure($draft = true, $cache = false);
  577. }
  578. else
  579. {
  580. return $response->withJson(['errors' => ['message' => 'Could not write to file. Please check if the file is writable']], 404);
  581. }
  582. # if it is the title, then delete the "# " if it exists
  583. $pageMarkdown[0] = trim($pageMarkdown[0], "# ");
  584. return $response->withJson(array('markdown' => $pageMarkdown, 'errors' => false));
  585. }
  586. public function deleteBlock(Request $request, Response $response, $args)
  587. {
  588. /* get params from call */
  589. $this->params = $request->getParams();
  590. $this->uri = $request->getUri();
  591. $errors = false;
  592. # set structure
  593. if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  594. # set item
  595. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  596. # set the status for published and drafted
  597. $this->setPublishStatus();
  598. # set path
  599. $this->setItemPath($this->item->fileType);
  600. # read content from file
  601. if(!$this->setContent()){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  602. # get content
  603. $this->content;
  604. if($this->content == '')
  605. {
  606. $this->content = [];
  607. }
  608. # initialize parsedown extension
  609. $parsedown = new ParsedownExtension();
  610. # if content is not an array, then transform it
  611. if(!is_array($this->content))
  612. {
  613. # turn markdown into an array of markdown-blocks
  614. $this->content = $parsedown->markdownToArrayBlocks($this->content);
  615. }
  616. # check if id exists
  617. if(!isset($this->content[$this->params['block_id']])){ return $response->withJson(array('data' => false, 'errors' => 'The ID of the content-block is wrong.'), 404); }
  618. # check if block is image
  619. $contentBlock = $this->content[$this->params['block_id']];
  620. $contentBlockStart = substr($contentBlock, 0, 2);
  621. if($contentBlockStart == '[!' OR $contentBlockStart == '![')
  622. {
  623. # extract image path
  624. preg_match("/\((.*?)\)/",$contentBlock,$matches);
  625. if(isset($matches[1]))
  626. {
  627. $imageBaseName = explode('-', $matches[1]);
  628. $imageBaseName = str_replace('media/live/', '', $imageBaseName[0]);
  629. $processImage = new ProcessImage();
  630. if(!$processImage->deleteImage($imageBaseName))
  631. {
  632. $errors = 'Could not delete some of the images, please check manually';
  633. }
  634. }
  635. }
  636. # delete the block
  637. unset($this->content[$this->params['block_id']]);
  638. $this->content = array_values($this->content);
  639. $pageMarkdown = $this->content;
  640. # delete markdown from title
  641. if(isset($pageMarkdown[0]))
  642. {
  643. $pageMarkdown[0] = trim($pageMarkdown[0], "# ");
  644. }
  645. # encode the content into json
  646. $pageJson = json_encode($this->content);
  647. # set path for the file (or folder)
  648. $this->setItemPath('txt');
  649. /* update the file */
  650. if($this->write->writeFile($this->settings['contentFolder'], $this->path, $pageJson))
  651. {
  652. # update the internal structure
  653. $this->setStructure($draft = true, $cache = false);
  654. }
  655. else
  656. {
  657. return $response->withJson(['errors' => ['message' => 'Could not write to file. Please check if the file is writable']], 404);
  658. }
  659. return $response->withJson(array('markdown' => $pageMarkdown, 'errors' => $errors));
  660. }
  661. public function createImage(Request $request, Response $response, $args)
  662. {
  663. /* get params from call */
  664. $this->params = $request->getParams();
  665. $this->uri = $request->getUri();
  666. $imageProcessor = new ProcessImage();
  667. if($imageProcessor->createImage($this->params['image'], $this->settings['images']))
  668. {
  669. return $response->withJson(array('errors' => false));
  670. }
  671. return $response->withJson(array('errors' => 'could not store image to temporary folder'));
  672. }
  673. public function publishImage(Request $request, Response $response, $args)
  674. {
  675. $params = $request->getParsedBody();
  676. $imageProcessor = new ProcessImage();
  677. $imageUrl = $imageProcessor->publishImage($this->settings['images'], $name = false);
  678. if($imageUrl)
  679. {
  680. $params['markdown'] = str_replace('imgplchldr', $imageUrl, $params['markdown']);
  681. $request = $request->withParsedBody($params);
  682. return $this->updateBlock($request, $response, $args);
  683. }
  684. return $response->withJson(array('errors' => 'could not store image to temporary folder'));
  685. }
  686. public function saveVideoImage(Request $request, Response $response, $args)
  687. {
  688. /* get params from call */
  689. $this->params = $request->getParams();
  690. $this->uri = $request->getUri();
  691. $class = false;
  692. $imageUrl = $this->params['markdown'];
  693. if(strpos($imageUrl, 'https://www.youtube.com/watch?v=') !== false)
  694. {
  695. $videoID = str_replace('https://www.youtube.com/watch?v=', '', $imageUrl);
  696. $videoID = strpos($videoID, '&') ? substr($videoID, 0, strpos($videoID, '&')) : $videoID;
  697. $class = 'youtube';
  698. }
  699. if(strpos($imageUrl, 'https://youtu.be/') !== false)
  700. {
  701. $videoID = str_replace('https://youtu.be/', '', $imageUrl);
  702. $videoID = strpos($videoID, '?') ? substr($videoID, 0, strpos($videoID, '?')) : $videoID;
  703. $class = 'youtube';
  704. }
  705. if($class == 'youtube')
  706. {
  707. $videoURLmaxres = 'https://i1.ytimg.com/vi/' . $videoID . '/maxresdefault.jpg';
  708. $videoURL0 = 'https://i1.ytimg.com/vi/' . $videoID . '/0.jpg';
  709. }
  710. $ctx = stream_context_create(array(
  711. 'https' => array(
  712. 'timeout' => 1
  713. )
  714. )
  715. );
  716. $imageData = @file_get_contents($videoURLmaxres, 0, $ctx);
  717. if($imageData === false)
  718. {
  719. $imageData = @file_get_contents($videoURL0, 0, $ctx);
  720. if($imageData === false)
  721. {
  722. return $response->withJson(array('errors' => 'could not get the video image'));
  723. }
  724. }
  725. $imageData64 = 'data:image/jpeg;base64,' . base64_encode($imageData);
  726. $desiredSizes = ['live' => ['width' => 560, 'height' => 315]];
  727. $imageProcessor = new ProcessImage();
  728. $tmpImage = $imageProcessor->createImage($imageData64, $desiredSizes);
  729. if(!$tmpImage)
  730. {
  731. return $response->withJson(array('errors' => 'could not create temporary image'));
  732. }
  733. $imageUrl = $imageProcessor->publishImage($desiredSizes, $videoID);
  734. if($imageUrl)
  735. {
  736. $this->params['markdown'] = '![' . $class . '-video](' . $imageUrl . ' "click to load video"){#' . $videoID. ' .' . $class . '}';
  737. $request = $request->withParsedBody($this->params);
  738. return $this->updateBlock($request, $response, $args);
  739. }
  740. return $response->withJson(array('errors' => 'could not store the preview image'));
  741. }
  742. }