ContentController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace Typemill\Controllers;
  3. use Slim\Views\Twig;
  4. use Slim\Http\Request;
  5. use Slim\Http\Response;
  6. use Typemill\Models\Validation;
  7. use Typemill\Models\Folder;
  8. use Typemill\Models\Write;
  9. use Typemill\Models\WriteYaml;
  10. use Typemill\Models\WriteCache;
  11. use \Symfony\Component\Yaml\Yaml;
  12. use Typemill\Models\Helpers;
  13. use Typemill\Extensions\ParsedownExtension;
  14. use \Parsedown;
  15. class ContentController extends Controller
  16. {
  17. /**
  18. * Show Content
  19. *
  20. * @param obj $request the slim request object
  21. * @param obj $response the slim response object
  22. * @return obje $response with redirect to route
  23. */
  24. public function showContent(Request $request, Response $response, $args)
  25. {
  26. $settings = $this->c->get('settings');
  27. $pathToContent = $settings['rootPath'] . $settings['contentFolder'];
  28. $uri = $request->getUri();
  29. /* scan the content of the folder */
  30. $structure = Folder::scanFolder($pathToContent);
  31. /* if there is no content, render an empty page */
  32. if(count($structure) == 0)
  33. {
  34. return $this->render($response, 'content/content.twig', array( 'navigation' => true, 'content' => 'Nothing found in content folder.' ));
  35. }
  36. /* create an array of object with the whole content of the folder */
  37. $structure = Folder::getFolderContentDetails($structure, $uri->getBaseUrl(), $uri->getBasePath());
  38. /* if there is no structure at all, the content folder is probably empty */
  39. if(!$structure)
  40. {
  41. return $this->render($response, 'content/content.twig', array( 'navigation' => true, 'content' => 'Nothing found in content folder.' ));
  42. }
  43. /* if it is the startpage */
  44. if(empty($args))
  45. {
  46. /* check, if there is an index-file in the root of the content folder */
  47. $contentMD = file_exists($pathToContent . DIRECTORY_SEPARATOR . 'index.md') ? file_get_contents($pathToContent . DIRECTORY_SEPARATOR . 'index.md') : NULL;
  48. /* if there is content (index.md), then add a marker for frontend, so ajax calls for homepage-index-urls work */
  49. if($contentMD)
  50. {
  51. $item = new \stdClass;
  52. $item->urlRel = 'is_homepage_index';
  53. }
  54. }
  55. else
  56. {
  57. /* get the request url */
  58. $urlRel = $uri->getBasePath() . '/' . $args['params'];
  59. /* find the url in the content-item-tree and return the item-object for the file */
  60. $item = Folder::getItemForUrl($structure, $urlRel);
  61. /* if there is still no item, return a 404-page */
  62. if(!$item)
  63. {
  64. return $this->render404($response, array( 'navigation' => $structure, 'settings' => $settings, 'base_url' => $base_url ));
  65. }
  66. /* add the paging to the item */
  67. $item = Folder::getPagingForItem($structure, $item);
  68. /* check if url is a folder. If so, check if there is an index-file in that folder */
  69. if($item->elementType == 'folder' && $item->index)
  70. {
  71. $filePath = $pathToContent . $item->path . DIRECTORY_SEPARATOR . 'index.md';
  72. }
  73. elseif($item->elementType == 'file')
  74. {
  75. $filePath = $pathToContent . $item->path;
  76. }
  77. /* add the modified date for the file */
  78. $item->modified = isset($filePath) ? filemtime($filePath) : false;
  79. /* read the content of the file */
  80. $contentMD = isset($filePath) ? file_get_contents($filePath) : false;
  81. }
  82. $title = false;
  83. $content = $contentMD;
  84. $content = str_replace(array("\r\n", "\r"), "\n", $content);
  85. $content = trim($content, "\n");
  86. if($contentMD[0] == '#')
  87. {
  88. $contentParts = explode("\n", $contentMD, 2);
  89. $title = trim($contentParts[0], "# \t\n\r\0\x0B");
  90. $content = trim($contentParts[1]);
  91. }
  92. return $this->render($response, 'content/content.twig', array('navigation' => $structure, 'title' => $title, 'content' => $content, 'item' => $item, 'settings' => $settings ));
  93. }
  94. public function updateArticle(Request $request, Response $response, $args)
  95. {
  96. /* Extract the parameters from get-call */
  97. $params = $request->getParams();
  98. /* validate input */
  99. $validate = new Validation();
  100. $vResult = $validate->editorInput($params);
  101. if(is_array($vResult))
  102. {
  103. return $response->withJson(['errors' => $vResult], 422);
  104. }
  105. /* initiate variables and objects that we need */
  106. $settings = $this->c->get('settings');
  107. $pathToContent = $settings['rootPath'] . $settings['contentFolder'];
  108. $uri = $request->getUri();
  109. $base_url = $uri->getBaseUrl();
  110. $write = new writeCache();
  111. /* we will use the cached structure to find the url for the page-update. It acts as whitelist and is more secure than a file-path, for example. */
  112. $structure = $write->getCache('cache', 'structure.txt');
  113. /* if there is no structure, create a fresh structure */
  114. if(!$structure)
  115. {
  116. $structure = $this->getFreshStructure($pathToContent, $write, $uri);
  117. if(!$structure)
  118. {
  119. return $response->withJson(['errors' => ['content folder is empty']], 404);
  120. }
  121. }
  122. /* if it is the homepage */
  123. if($params['url'] == 'is_homepage_index')
  124. {
  125. $item = new \stdClass;
  126. $item->elementType = 'folder';
  127. $item->path = '';
  128. }
  129. else
  130. {
  131. /* search for the url in the structure */
  132. $item = Folder::getItemForUrl($structure, $params['url']);
  133. }
  134. if(!$item)
  135. {
  136. return $response->withJson(['errors' => ['requested page-url not found']], 404);
  137. }
  138. if($item->elementType == 'folder')
  139. {
  140. $path = $item->path . DIRECTORY_SEPARATOR . 'index.md';
  141. }
  142. elseif($item->elementType == 'file')
  143. {
  144. $path = $item->path;
  145. }
  146. /* get the markdown file */
  147. $mdFile = $write->getFile($settings['contentFolder'], $path);
  148. if($mdFile)
  149. {
  150. /* merge title with content for complete markdown document */
  151. $updatedContent = '# ' . $params['title'] . "\r\n\r\n" . $params['content'];
  152. /* update the file */
  153. $write->writeFile($settings['contentFolder'], $path, $updatedContent);
  154. return $response->withJson(['success'], 200);
  155. }
  156. return $response->withJson(['errors' => ['requested markdown-file not found']], 404);
  157. }
  158. protected function getFreshStructure($pathToContent, $cache, $uri)
  159. {
  160. /* scan the content of the folder */
  161. $structure = Folder::scanFolder($pathToContent);
  162. /* if there is no content, render an empty page */
  163. if(count($structure) == 0)
  164. {
  165. return false;
  166. }
  167. /* create an array of object with the whole content of the folder */
  168. $structure = Folder::getFolderContentDetails($structure, $uri->getBaseUrl(), $uri->getBasePath());
  169. /* cache navigation */
  170. $cache->updateCache('cache', 'structure.txt', 'lastCache.txt', $structure);
  171. return $structure;
  172. }
  173. }