BlockApiController.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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\Models\ProcessFile;
  10. use Typemill\Extensions\ParsedownExtension;
  11. use \URLify;
  12. class BlockApiController extends ContentController
  13. {
  14. public function addBlock(Request $request, Response $response, $args)
  15. {
  16. /* get params from call */
  17. $this->params = $request->getParams();
  18. $this->uri = $request->getUri()->withUserInfo('');
  19. # minimum permission is that user is allowed to update his own content
  20. if(!$this->c->acl->isAllowed($_SESSION['role'], 'mycontent', 'update'))
  21. {
  22. return $response->withJson(array('data' => false, 'errors' => ['message' => 'You are not allowed to publish content.']), 403);
  23. }
  24. /* validate input */
  25. if(!$this->validateBlockInput()){ return $response->withJson($this->errors,422); }
  26. # set structure
  27. if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  28. $this->setHomepage($args = false);
  29. /* set item */
  30. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  31. # if user has no right to delete content from others (eg admin or editor)
  32. if(!$this->c->acl->isAllowed($_SESSION['role'], 'content', 'update'))
  33. {
  34. # check ownership. This code should nearly never run, because there is no button/interface to trigger it.
  35. if(!$this->checkContentOwnership())
  36. {
  37. return $response->withJson(array('data' => false, 'errors' => ['message' => 'You are not allowed to edit content.']), 403);
  38. }
  39. }
  40. # set the status for published and drafted
  41. $this->setPublishStatus();
  42. # set path
  43. $this->setItemPath($this->item->fileType);
  44. # read content from file
  45. if(!$this->setContent()){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  46. # make it more clear which content we have
  47. $pageMarkdown = $this->content;
  48. $blockMarkdown = $this->params['markdown'];
  49. # standardize line breaks
  50. $blockMarkdown = str_replace(array("\r\n", "\r"), "\n", $blockMarkdown);
  51. # remove surrounding line breaks
  52. $blockMarkdown = trim($blockMarkdown, "\n");
  53. if($pageMarkdown == '')
  54. {
  55. $pageMarkdown = [];
  56. }
  57. # initialize parsedown extension
  58. $parsedown = new ParsedownExtension($this->uri->getBaseUrl());
  59. # if content is not an array, then transform it
  60. if(!is_array($pageMarkdown))
  61. {
  62. # turn markdown into an array of markdown-blocks
  63. $pageMarkdown = $parsedown->markdownToArrayBlocks($pageMarkdown);
  64. }
  65. # if it is a new content-block
  66. if($this->params['block_id'] == 99999)
  67. {
  68. # set the id of the markdown-block (it will be one more than the actual array, so count is perfect)
  69. $id = count($pageMarkdown);
  70. # add the new markdown block to the page content
  71. $pageMarkdown[] = $blockMarkdown;
  72. }
  73. elseif(($this->params['block_id'] == 0) OR !isset($pageMarkdown[$this->params['block_id']]))
  74. {
  75. # if the block does not exists, return an error
  76. return $response->withJson(array('data' => false, 'errors' => ['message' => 'The ID of the content-block is wrong.']), 404);
  77. }
  78. else
  79. {
  80. # insert new markdown block
  81. array_splice( $pageMarkdown, $this->params['block_id'], 0, $blockMarkdown );
  82. $id = $this->params['block_id'];
  83. }
  84. # encode the content into json
  85. $pageJson = json_encode($pageMarkdown);
  86. # set path for the file (or folder)
  87. $this->setItemPath('txt');
  88. /* update the file */
  89. if($this->write->writeFile($this->settings['contentFolder'], $this->path, $pageJson))
  90. {
  91. # update the internal structure
  92. $this->setStructure($draft = true, $cache = false);
  93. $this->content = $pageMarkdown;
  94. }
  95. else
  96. {
  97. return $response->withJson(['errors' => ['message' => 'Could not write to file. Please check if the file is writable']], 404);
  98. }
  99. /* set safe mode to escape javascript and html in markdown */
  100. $parsedown->setSafeMode(true);
  101. /* parse markdown-file to content-array */
  102. $blockArray = $parsedown->text($blockMarkdown);
  103. # we assume that toc is not relevant
  104. $toc = false;
  105. # needed for ToC links
  106. $relurl = '/tm/content/' . $this->settings['editor'] . '/' . $this->item->urlRel;
  107. if($blockMarkdown == '[TOC]')
  108. {
  109. # if block is table of content itself, then generate the table of content
  110. $tableofcontent = $this->generateToc();
  111. # and only use the html-markup
  112. $blockHTML = $tableofcontent['html'];
  113. }
  114. else
  115. {
  116. # parse markdown-content-array to content-string
  117. $blockHTML = $parsedown->markup($blockArray);
  118. # if it is a headline
  119. if($blockMarkdown[0] == '#')
  120. {
  121. # then the TOC holds either false (if no toc used in the page) or it holds an object with the id and toc-markup
  122. $toc = $this->generateToc();
  123. }
  124. }
  125. return $response->withJson(array('content' => [ 'id' => $id, 'html' => $blockHTML ] , 'markdown' => $blockMarkdown, 'id' => $id, 'toc' => $toc, 'errors' => false));
  126. }
  127. protected function generateToc()
  128. {
  129. # we assume that page has no table of content
  130. $toc = false;
  131. # make sure $this->content is updated
  132. $content = $this->content;
  133. if($content == '')
  134. {
  135. $content = [];
  136. }
  137. # initialize parsedown extension
  138. $parsedown = new ParsedownExtension($this->uri->getBaseUrl());
  139. # if content is not an array, then transform it
  140. if(!is_array($content))
  141. {
  142. # turn markdown into an array of markdown-blocks
  143. $content = $parsedown->markdownToArrayBlocks($content);
  144. }
  145. # needed for ToC links
  146. $relurl = '/tm/content/' . $this->settings['editor'] . '/' . $this->item->urlRel;
  147. # loop through mardkown-array and create html-blocks
  148. foreach($content as $key => $block)
  149. {
  150. # parse markdown-file to content-array
  151. $contentArray = $parsedown->text($block);
  152. if($block == '[TOC]')
  153. {
  154. # toc is true and holds the key of the table of content now
  155. $toc = $key;
  156. }
  157. # parse markdown-content-array to content-string
  158. $content[$key] = ['id' => $key, 'html' => $parsedown->markup($contentArray)];
  159. }
  160. # if page has a table of content
  161. if($toc)
  162. {
  163. # generate the toc markup
  164. $tocMarkup = $parsedown->buildTOC($parsedown->headlines);
  165. # toc holds the id of the table of content and the html-markup now
  166. $toc = ['id' => $toc, 'html' => $tocMarkup];
  167. }
  168. return $toc;
  169. }
  170. public function updateBlock(Request $request, Response $response, $args)
  171. {
  172. /* get params from call */
  173. $this->params = $request->getParams();
  174. $this->uri = $request->getUri()->withUserInfo('');
  175. # minimum permission is that user is allowed to update his own content
  176. if(!$this->c->acl->isAllowed($_SESSION['role'], 'mycontent', 'update'))
  177. {
  178. return $response->withJson(array('data' => false, 'errors' => ['message' => 'You are not allowed to publish content.']), 403);
  179. }
  180. /* validate input */
  181. if(!$this->validateBlockInput()){ return $response->withJson($this->errors,422); }
  182. # set structure
  183. if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  184. $this->setHomepage($args = false);
  185. /* set item */
  186. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  187. # if user has no right to delete content from others (eg admin or editor)
  188. if(!$this->c->acl->isAllowed($_SESSION['role'], 'content', 'update'))
  189. {
  190. # check ownership. This code should nearly never run, because there is no button/interface to trigger it.
  191. if(!$this->checkContentOwnership())
  192. {
  193. return $response->withJson(array('data' => false, 'errors' => ['message' => 'You are not allowed to edit content.']), 403);
  194. }
  195. }
  196. # set the status for published and drafted
  197. $this->setPublishStatus();
  198. # set path
  199. $this->setItemPath($this->item->fileType);
  200. # read content from file
  201. if(!$this->setContent()){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  202. # make it more clear which content we have
  203. $pageMarkdown = $this->content;
  204. $blockMarkdown = $this->params['markdown'];
  205. # standardize line breaks
  206. $blockMarkdown = str_replace(array("\r\n", "\r"), "\n", $blockMarkdown);
  207. # remove surrounding line breaks
  208. $blockMarkdown = trim($blockMarkdown, "\n");
  209. if($pageMarkdown == '')
  210. {
  211. $pageMarkdown = [];
  212. }
  213. # initialize parsedown extension
  214. $parsedown = new ParsedownExtension($this->uri->getBaseUrl());
  215. $parsedown->setVisualMode();
  216. # if content is not an array, then transform it
  217. if(!is_array($pageMarkdown))
  218. {
  219. # turn markdown into an array of markdown-blocks
  220. $pageMarkdown = $parsedown->markdownToArrayBlocks($pageMarkdown);
  221. }
  222. if(!isset($pageMarkdown[$this->params['block_id']]))
  223. {
  224. # if the block does not exists, return an error
  225. return $response->withJson(array('data' => false, 'errors' => ['message' => 'The ID of the content-block is wrong.']), 404);
  226. }
  227. elseif($this->params['block_id'] == 0)
  228. {
  229. # if it is the title, then delete the "# " if it exists
  230. $blockMarkdown = trim($blockMarkdown, "# ");
  231. # store the markdown-headline in a separate variable
  232. $blockMarkdownTitle = '# ' . $blockMarkdown;
  233. # add the markdown-headline to the page-markdown
  234. $pageMarkdown[0] = $blockMarkdownTitle;
  235. $id = 0;
  236. }
  237. else
  238. {
  239. # update the markdown block in the page content
  240. $pageMarkdown[$this->params['block_id']] = $blockMarkdown;
  241. $id = $this->params['block_id'];
  242. }
  243. # encode the content into json
  244. $pageJson = json_encode($pageMarkdown);
  245. # set path for the file (or folder)
  246. $this->setItemPath('txt');
  247. /* update the file */
  248. if($this->write->writeFile($this->settings['contentFolder'], $this->path, $pageJson))
  249. {
  250. # update the internal structure
  251. $this->setStructure($draft = true, $cache = false);
  252. # updated the content variable
  253. $this->content = $pageMarkdown;
  254. }
  255. else
  256. {
  257. return $response->withJson(['errors' => ['message' => 'Could not write to file. Please check if the file is writable']], 404);
  258. }
  259. /* parse markdown-file to content-array, if title parse title. */
  260. if($this->params['block_id'] == 0)
  261. {
  262. $blockArray = $parsedown->text($blockMarkdownTitle);
  263. }
  264. else
  265. {
  266. /* set safe mode to escape javascript and html in markdown */
  267. $parsedown->setSafeMode(true);
  268. $blockArray = $parsedown->text($blockMarkdown);
  269. }
  270. # we assume that toc is not relevant
  271. $toc = false;
  272. # needed for ToC links
  273. $relurl = '/tm/content/' . $this->settings['editor'] . '/' . $this->item->urlRel;
  274. if($blockMarkdown == '[TOC]')
  275. {
  276. # if block is table of content itself, then generate the table of content
  277. $tableofcontent = $this->generateToc();
  278. # and only use the html-markup
  279. $blockHTML = $tableofcontent['html'];
  280. }
  281. else
  282. {
  283. # parse markdown-content-array to content-string
  284. $blockHTML = $parsedown->markup($blockArray);
  285. # if it is a headline
  286. if($blockMarkdown[0] == '#')
  287. {
  288. # then the TOC holds either false (if no toc used in the page) or it holds an object with the id and toc-markup
  289. $toc = $this->generateToc();
  290. }
  291. }
  292. return $response->withJson(array('content' => [ 'id' => $id, 'html' => $blockHTML ] , 'markdown' => $blockMarkdown, 'id' => $id, 'toc' => $toc, 'errors' => false));
  293. }
  294. public function moveBlock(Request $request, Response $response, $args)
  295. {
  296. # get params from call
  297. $this->params = $request->getParams();
  298. $this->uri = $request->getUri()->withUserInfo('');
  299. # minimum permission is that user is allowed to update his own content
  300. if(!$this->c->acl->isAllowed($_SESSION['role'], 'mycontent', 'update'))
  301. {
  302. return $response->withJson(array('data' => false, 'errors' => ['message' => 'You are not allowed to publish content.']), 403);
  303. }
  304. # validate input
  305. # if(!$this->validateBlockInput()){ return $response->withJson($this->errors,422); }
  306. # set structure
  307. if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  308. $this->setHomepage($args = false);
  309. # set item
  310. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  311. # if user has no right to delete content from others (eg admin or editor)
  312. if(!$this->c->acl->isAllowed($_SESSION['role'], 'content', 'update'))
  313. {
  314. # check ownership. This code should nearly never run, because there is no button/interface to trigger it.
  315. if(!$this->checkContentOwnership())
  316. {
  317. return $response->withJson(array('data' => false, 'errors' => ['message' => 'You are not allowed to delete content.']), 403);
  318. }
  319. }
  320. # set the status for published and drafted
  321. $this->setPublishStatus();
  322. # set path
  323. $this->setItemPath($this->item->fileType);
  324. # read content from file
  325. if(!$this->setContent()){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  326. # make it more clear which content we have
  327. $pageMarkdown = $this->content;
  328. if($pageMarkdown == '')
  329. {
  330. $pageMarkdown = [];
  331. }
  332. # initialize parsedown extension
  333. $parsedown = new ParsedownExtension($this->uri->getBaseUrl());
  334. # if content is not an array, then transform it
  335. if(!is_array($pageMarkdown))
  336. {
  337. # turn markdown into an array of markdown-blocks
  338. $pageMarkdown = $parsedown->markdownToArrayBlocks($pageMarkdown);
  339. }
  340. $oldIndex = ($this->params['old_index'] + 1);
  341. $newIndex = ($this->params['new_index'] + 1);
  342. if(!isset($pageMarkdown[$oldIndex]))
  343. {
  344. # if the block does not exists, return an error
  345. return $response->withJson(array('data' => false, 'errors' => ['message' => 'The ID of the content-block is wrong.']), 404);
  346. }
  347. $extract = array_splice($pageMarkdown, $oldIndex, 1);
  348. array_splice($pageMarkdown, $newIndex, 0, $extract);
  349. # encode the content into json
  350. $pageJson = json_encode($pageMarkdown);
  351. # set path for the file (or folder)
  352. $this->setItemPath('txt');
  353. /* update the file */
  354. if($this->write->writeFile($this->settings['contentFolder'], $this->path, $pageJson))
  355. {
  356. # update the internal structure
  357. $this->setStructure($draft = true, $cache = false);
  358. # update this content
  359. $this->content = $pageMarkdown;
  360. }
  361. else
  362. {
  363. return $response->withJson(['errors' => ['message' => 'Could not write to file. Please check if the file is writable']], 404);
  364. }
  365. # we assume that toc is not relevant
  366. $toc = false;
  367. # needed for ToC links
  368. $relurl = '/tm/content/' . $this->settings['editor'] . '/' . $this->item->urlRel;
  369. # if the moved item is a headline
  370. if($extract[0][0] == '#')
  371. {
  372. $toc = $this->generateToc();
  373. }
  374. # if it is the title, then delete the "# " if it exists
  375. $pageMarkdown[0] = trim($pageMarkdown[0], "# ");
  376. return $response->withJson(array('markdown' => $pageMarkdown, 'toc' => $toc, 'errors' => false));
  377. }
  378. public function deleteBlock(Request $request, Response $response, $args)
  379. {
  380. /* get params from call */
  381. $this->params = $request->getParams();
  382. $this->uri = $request->getUri()->withUserInfo('');
  383. $errors = false;
  384. # minimum permission is that user is allowed to update his own content
  385. if(!$this->c->acl->isAllowed($_SESSION['role'], 'mycontent', 'update'))
  386. {
  387. return $response->withJson(array('data' => false, 'errors' => ['message' => 'You are not allowed to publish content.']), 403);
  388. }
  389. # set structure
  390. if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  391. $this->setHomepage($args = false);
  392. # set item
  393. if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
  394. # if user has no right to delete content from others (eg admin or editor)
  395. if(!$this->c->acl->isAllowed($_SESSION['role'], 'content', 'update'))
  396. {
  397. # check ownership. This code should nearly never run, because there is no button/interface to trigger it.
  398. if(!$this->checkContentOwnership())
  399. {
  400. return $response->withJson(array('data' => false, 'errors' => ['message' => 'You are not allowed to delete content.']), 403);
  401. }
  402. }
  403. # set the status for published and drafted
  404. $this->setPublishStatus();
  405. # set path
  406. $this->setItemPath($this->item->fileType);
  407. # read content from file
  408. if(!$this->setContent()){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
  409. # get content
  410. $this->content;
  411. if($this->content == '')
  412. {
  413. $this->content = [];
  414. }
  415. # initialize parsedown extension
  416. $parsedown = new ParsedownExtension($this->uri->getBaseUrl());
  417. # if content is not an array, then transform it
  418. if(!is_array($this->content))
  419. {
  420. # turn markdown into an array of markdown-blocks
  421. $this->content = $parsedown->markdownToArrayBlocks($this->content);
  422. }
  423. # check if id exists
  424. if(!isset($this->content[$this->params['block_id']])){ return $response->withJson(array('data' => false, 'errors' => 'The ID of the content-block is wrong.'), 404); }
  425. $contentBlock = $this->content[$this->params['block_id']];
  426. # delete the block
  427. unset($this->content[$this->params['block_id']]);
  428. $this->content = array_values($this->content);
  429. $pageMarkdown = $this->content;
  430. # delete markdown from title
  431. if(isset($pageMarkdown[0]))
  432. {
  433. $pageMarkdown[0] = trim($pageMarkdown[0], "# ");
  434. }
  435. # encode the content into json
  436. $pageJson = json_encode($this->content);
  437. # set path for the file (or folder)
  438. $this->setItemPath('txt');
  439. /* update the file */
  440. if($this->write->writeFile($this->settings['contentFolder'], $this->path, $pageJson))
  441. {
  442. # update the internal structure
  443. $this->setStructure($draft = true, $cache = false);
  444. }
  445. else
  446. {
  447. return $response->withJson(['errors' => ['message' => 'Could not write to file. Please check if the file is writable']], 404);
  448. }
  449. $toc = false;
  450. if($contentBlock[0] == '#')
  451. {
  452. $toc = $this->generateToc();
  453. }
  454. return $response->withJson(array('markdown' => $pageMarkdown, 'toc' => $toc, 'errors' => $errors));
  455. }
  456. public function getMediaLibImages(Request $request, Response $response, $args)
  457. {
  458. # get params from call
  459. $this->params = $request->getParams();
  460. $this->uri = $request->getUri()->withUserInfo('');
  461. $imageProcessor = new ProcessImage($this->settings['images']);
  462. if(!$imageProcessor->checkFolders('images'))
  463. {
  464. return $response->withJson(['errors' => ['message' => 'Please check if your media-folder exists and all folders inside are writable.']], 500);
  465. }
  466. $imagelist = $imageProcessor->scanMediaFlat();
  467. return $response->withJson(array('images' => $imagelist));
  468. }
  469. public function getMediaLibFiles(Request $request, Response $response, $args)
  470. {
  471. # get params from call
  472. $this->params = $request->getParams();
  473. $this->uri = $request->getUri()->withUserInfo('');
  474. $fileProcessor = new ProcessFile();
  475. if(!$fileProcessor->checkFolders())
  476. {
  477. return $response->withJson(['errors' => ['message' => 'Please check if your media-folder exists and all folders inside are writable.']], 500);
  478. }
  479. $filelist = $fileProcessor->scanFilesFlat();
  480. return $response->withJson(array('files' => $filelist));
  481. }
  482. public function getImage(Request $request, Response $response, $args)
  483. {
  484. # get params from call
  485. $this->params = $request->getParams();
  486. $this->uri = $request->getUri()->withUserInfo('');
  487. $this->setStructure($draft = true, $cache = false);
  488. $imageProcessor = new ProcessImage($this->settings['images']);
  489. if(!$imageProcessor->checkFolders('images'))
  490. {
  491. return $response->withJson(['errors' => ['message' => 'Please check if your media-folder exists and all folders inside are writable.']], 500);
  492. }
  493. $imageDetails = $imageProcessor->getImageDetails($this->params['name'], $this->structure);
  494. if($imageDetails)
  495. {
  496. return $response->withJson(array('image' => $imageDetails));
  497. }
  498. # return $response->withJson(array('image' => false, 'errors' => 'image name invalid or not found'));
  499. return $response->withJson(['errors' => ['message' => 'Image name invalid or not found.']], 404);
  500. }
  501. public function getFile(Request $request, Response $response, $args)
  502. {
  503. # get params from call
  504. $this->params = $request->getParams();
  505. $this->uri = $request->getUri()->withUserInfo('');
  506. $this->setStructure($draft = true, $cache = false);
  507. $fileProcessor = new ProcessFile();
  508. if(!$fileProcessor->checkFolders())
  509. {
  510. return $response->withJson(['errors' => ['message' => 'Please check if your media-folder exists and all folders inside are writable.']], 500);
  511. }
  512. $fileDetails = $fileProcessor->getFileDetails($this->params['name'], $this->structure);
  513. if($fileDetails)
  514. {
  515. return $response->withJson(['file' => $fileDetails]);
  516. }
  517. return $response->withJson(['errors' => ['message' => 'file name invalid or not found']],404);
  518. }
  519. public function createImage(Request $request, Response $response, $args)
  520. {
  521. # get params from call
  522. $this->params = $request->getParams();
  523. $this->uri = $request->getUri()->withUserInfo('');
  524. # do this shit in the model ...
  525. $imagename = explode('.', $this->params['name']);
  526. array_pop($imagename);
  527. $imagename = implode('-', $imagename);
  528. $name = URLify::filter(iconv(mb_detect_encoding($imagename, mb_detect_order(), true), "UTF-8", $imagename));
  529. $imageProcessor = new ProcessImage($this->settings['images']);
  530. if(!$imageProcessor->checkFolders('images'))
  531. {
  532. return $response->withJson(['errors' => ['message' => 'Please check if your media-folder exists and all folders inside are writable.']], 500);
  533. }
  534. if($imageProcessor->createImage($this->params['image'], $name, $this->settings['images']))
  535. {
  536. return $response->withJson(array('errors' => false));
  537. }
  538. return $response->withJson(array('errors' => ['message' => 'could not store image to temporary folder']));
  539. }
  540. public function createFile(Request $request, Response $response, $args)
  541. {
  542. # get params from call
  543. $this->params = $request->getParams();
  544. $this->uri = $request->getUri()->withUserInfo('');
  545. $finfo = finfo_open( FILEINFO_MIME_TYPE );
  546. $mtype = finfo_file( $finfo, $this->params['file'] );
  547. finfo_close( $finfo );
  548. $allowedMimes = $this->getAllowedMtypes();
  549. if(!in_array($mtype, $allowedMimes))
  550. {
  551. return $response->withJson(array('errors' => ['message' => 'File-type is not allowed']));
  552. }
  553. # sanitize file name
  554. $filename = basename($this->params['name']);
  555. $filename = explode('.', $this->params['name']);
  556. array_pop($filename);
  557. $filename = implode('-', $filename);
  558. $name = URLify::filter(iconv(mb_detect_encoding($filename, mb_detect_order(), true), "UTF-8", $filename));
  559. $fileProcessor = new ProcessFile();
  560. if(!$fileProcessor->checkFolders())
  561. {
  562. return $response->withJson(['errors' => ['message' => 'Please check if your media-folder exists and all folders inside are writable.']], 500);
  563. }
  564. if($fileProcessor->createFile($this->params['file'], $name))
  565. {
  566. return $response->withJson(array('errors' => false, 'name' => $name));
  567. }
  568. return $response->withJson(array('errors' => ['message' => 'could not store file to temporary folder']));
  569. }
  570. public function publishImage(Request $request, Response $response, $args)
  571. {
  572. $params = $request->getParsedBody();
  573. $imageProcessor = new ProcessImage($this->settings['images']);
  574. if(!$imageProcessor->checkFolders())
  575. {
  576. return $response->withJson(['errors' => ['message' => 'Please check if your media-folder exists and all folders inside are writable.']], 500);
  577. }
  578. $imageUrl = $imageProcessor->publishImage();
  579. if($imageUrl)
  580. {
  581. # replace the image placeholder in markdown with the image url
  582. $params['markdown'] = str_replace('imgplchldr', $imageUrl, $params['markdown']);
  583. $request = $request->withParsedBody($params);
  584. if($params['new'])
  585. {
  586. return $this->addBlock($request, $response, $args);
  587. }
  588. return $this->updateBlock($request, $response, $args);
  589. }
  590. return $response->withJson(array('errors' => ['message' => 'could not store image to media folder']));
  591. }
  592. public function deleteImage(Request $request, Response $response, $args)
  593. {
  594. # get params from call
  595. $this->params = $request->getParams();
  596. $this->uri = $request->getUri()->withUserInfo('');
  597. if(!isset($this->params['name']))
  598. {
  599. return $response->withJson(array('errors' => ['message' => 'image name is missing']));
  600. }
  601. $imageProcessor = new ProcessImage($this->settings['images']);
  602. if(!$imageProcessor->checkFolders())
  603. {
  604. return $response->withJson(['errors' => ['message' => 'Please check if your media-folder exists and all folders inside are writable.']], 500);
  605. }
  606. $errors = $imageProcessor->deleteImage($this->params['name']);
  607. return $response->withJson(array('errors' => $errors));
  608. }
  609. public function deleteFile(Request $request, Response $response, $args)
  610. {
  611. # get params from call
  612. $this->params = $request->getParams();
  613. $this->uri = $request->getUri()->withUserInfo('');
  614. if(!isset($this->params['name']))
  615. {
  616. return $response->withJson(array('errors' => ['message' => 'file name is missing']));
  617. }
  618. $fileProcessor = new ProcessFile();
  619. $errors = false;
  620. if($fileProcessor->deleteFile($this->params['name']))
  621. {
  622. return $response->withJson(array('errors' => false));
  623. }
  624. return $response->withJson(array('errors' => ['message' => 'could not delete the file']));
  625. }
  626. public function saveVideoImage(Request $request, Response $response, $args)
  627. {
  628. /* get params from call */
  629. $this->params = $request->getParams();
  630. $this->uri = $request->getUri()->withUserInfo('');
  631. $class = false;
  632. $imageUrl = $this->params['markdown'];
  633. if(strpos($imageUrl, 'https://www.youtube.com/watch?v=') !== false)
  634. {
  635. $videoID = str_replace('https://www.youtube.com/watch?v=', '', $imageUrl);
  636. $videoID = strpos($videoID, '&') ? substr($videoID, 0, strpos($videoID, '&')) : $videoID;
  637. $class = 'youtube';
  638. }
  639. if(strpos($imageUrl, 'https://youtu.be/') !== false)
  640. {
  641. $videoID = str_replace('https://youtu.be/', '', $imageUrl);
  642. $videoID = strpos($videoID, '?') ? substr($videoID, 0, strpos($videoID, '?')) : $videoID;
  643. $class = 'youtube';
  644. }
  645. if($class == 'youtube')
  646. {
  647. $videoURLmaxres = 'https://i1.ytimg.com/vi/' . $videoID . '/maxresdefault.jpg';
  648. $videoURL0 = 'https://i1.ytimg.com/vi/' . $videoID . '/0.jpg';
  649. }
  650. $ctx = stream_context_create(array(
  651. 'https' => array(
  652. 'timeout' => 1
  653. )
  654. )
  655. );
  656. $imageData = @file_get_contents($videoURLmaxres, 0, $ctx);
  657. if($imageData === false)
  658. {
  659. $imageData = @file_get_contents($videoURL0, 0, $ctx);
  660. if($imageData === false)
  661. {
  662. return $response->withJson(['errors' => ['message' => 'We did not find that video or could not get a preview image.']], 500);
  663. }
  664. }
  665. $imageData64 = 'data:image/jpeg;base64,' . base64_encode($imageData);
  666. $desiredSizes = $this->settings['images'];
  667. $desiredSizes['live'] = ['width' => 560, 'height' => 315];
  668. $imageProcessor = new ProcessImage($desiredSizes);
  669. if(!$imageProcessor->checkFolders('images'))
  670. {
  671. return $response->withJson(['errors' => ['message' => 'Please check if your media-folder exists and all folders inside are writable.']], 500);
  672. }
  673. if(!$imageProcessor->createImage($imageData64, 'youtube-' . $videoID, $desiredSizes, $overwrite = true))
  674. {
  675. return $response->withJson(['errors' => ['message' => 'We could not create the image.']], 500);
  676. }
  677. $imageUrl = $imageProcessor->publishImage();
  678. if($imageUrl)
  679. {
  680. $this->params['markdown'] = '![' . $class . '-video](' . $imageUrl . ' "click to load video"){#' . $videoID. ' .' . $class . '}';
  681. $request = $request->withParsedBody($this->params);
  682. if($this->params['new'])
  683. {
  684. return $this->addBlock($request, $response, $args);
  685. }
  686. return $this->updateBlock($request, $response, $args);
  687. }
  688. return $response->withJson(array('errors' => ['message' => 'could not store the preview image']));
  689. }
  690. private function getAllowedMtypes()
  691. {
  692. return array(
  693. 'application/zip',
  694. 'application/gzip',
  695. 'application/vnd.rar',
  696. 'application/vnd.visio',
  697. 'application/vnd.ms-excel',
  698. 'application/vnd.ms-powerpoint',
  699. 'application/vnd.ms-word.document.macroEnabled.12',
  700. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  701. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  702. 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  703. 'application/vnd.apple.keynote',
  704. 'application/vnd.apple.mpegurl',
  705. 'application/vnd.apple.numbers',
  706. 'application/vnd.apple.pages',
  707. 'application/vnd.amazon.mobi8-ebook',
  708. 'application/epub+zip',
  709. 'application/pdf',
  710. 'image/png',
  711. 'image/jpeg',
  712. 'image/jpg',
  713. 'image/gif',
  714. 'image/svg+xml',
  715. 'font/*',
  716. 'audio/mpeg',
  717. 'audio/mp4',
  718. 'audio/ogg',
  719. 'video/mpeg',
  720. 'video/mp4',
  721. 'video/ogg',
  722. );
  723. }
  724. }