BlockApiController.php 29 KB

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