BlockApiController.php 26 KB

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