PageController.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace Typemill\Controllers;
  3. use Typemill\Models\Folder;
  4. use Typemill\Models\WriteCache;
  5. use Typemill\Models\WriteSitemap;
  6. use Typemill\Models\WriteYaml;
  7. use \Symfony\Component\Yaml\Yaml;
  8. use Typemill\Models\VersionCheck;
  9. use Typemill\Models\Helpers;
  10. use Typemill\Models\Markdown;
  11. use Typemill\Events\OnPagetreeLoaded;
  12. use Typemill\Events\OnBreadcrumbLoaded;
  13. use Typemill\Events\OnItemLoaded;
  14. use Typemill\Events\OnOriginalLoaded;
  15. use Typemill\Events\OnMarkdownLoaded;
  16. use Typemill\Events\OnContentArrayLoaded;
  17. use Typemill\Events\OnHtmlLoaded;
  18. use Typemill\Extensions\ParsedownExtension;
  19. class PageController extends Controller
  20. {
  21. public function index($request, $response, $args)
  22. {
  23. /* Initiate Variables */
  24. $structure = false;
  25. $contentHTML = false;
  26. $item = false;
  27. $breadcrumb = false;
  28. $description = '';
  29. $settings = $this->c->get('settings');
  30. $pathToContent = $settings['rootPath'] . $settings['contentFolder'];
  31. $cache = new WriteCache();
  32. $uri = $request->getUri();
  33. $base_url = $uri->getBaseUrl();
  34. try
  35. {
  36. /* if the cached structure is still valid, use it */
  37. if($cache->validate('cache', 'lastCache.txt',600))
  38. {
  39. $structure = $this->getCachedStructure($cache);
  40. }
  41. if(!isset($structure) OR !$structure)
  42. {
  43. /* if not, get a fresh structure of the content folder */
  44. $structure = $this->getFreshStructure($pathToContent, $cache, $uri);
  45. /* if there is no structure at all, the content folder is probably empty */
  46. if(!$structure)
  47. {
  48. $content = '<h1>No Content</h1><p>Your content folder is empty.</p>';
  49. return $this->render($response, '/index.twig', array( 'content' => $content ));
  50. }
  51. elseif(!$cache->validate('cache', 'lastSitemap.txt', 86400))
  52. {
  53. /* update sitemap */
  54. $sitemap = new WriteSitemap();
  55. $sitemap->updateSitemap('cache', 'sitemap.xml', 'lastSitemap.txt', $structure, $uri->getBaseUrl());
  56. /* check and update the typemill-version in the user settings */
  57. $this->updateVersion($uri->getBaseUrl());
  58. }
  59. }
  60. /* dispatch event and let others manipulate the structure */
  61. $structure = $this->c->dispatcher->dispatch('onPagetreeLoaded', new OnPagetreeLoaded($structure))->getData();
  62. }
  63. catch (Exception $e)
  64. {
  65. echo $e->getMessage();
  66. exit(1);
  67. }
  68. /* if the user is on startpage */
  69. if(empty($args))
  70. {
  71. /* check, if there is an index-file in the root of the content folder */
  72. $contentMD = file_exists($pathToContent . DIRECTORY_SEPARATOR . 'index.md') ? file_get_contents($pathToContent . DIRECTORY_SEPARATOR . 'index.md') : NULL;
  73. }
  74. else
  75. {
  76. /* get the request url */
  77. $urlRel = $uri->getBasePath() . '/' . $args['params'];
  78. /* find the url in the content-item-tree and return the item-object for the file */
  79. $item = Folder::getItemForUrl($structure, $urlRel);
  80. /* if there is still no item, return a 404-page */
  81. if(!$item)
  82. {
  83. return $this->render404($response, array( 'navigation' => $structure, 'settings' => $settings, 'base_url' => $base_url ));
  84. }
  85. /* get breadcrumb for page */
  86. $breadcrumb = Folder::getBreadcrumb($structure, $item->keyPathArray);
  87. $breadcrumb = $this->c->dispatcher->dispatch('onBreadcrumbLoaded', new OnBreadcrumbLoaded($breadcrumb))->getData();
  88. /* add the paging to the item */
  89. $item = Folder::getPagingForItem($structure, $item);
  90. $item = $this->c->dispatcher->dispatch('onItemLoaded', new OnItemLoaded($item))->getData();
  91. /* check if url is a folder. If so, check if there is an index-file in that folder */
  92. if($item->elementType == 'folder')
  93. {
  94. $filePath = $pathToContent . $item->path . DIRECTORY_SEPARATOR . 'index.md';
  95. }
  96. elseif($item->elementType == 'file')
  97. {
  98. $filePath = $pathToContent . $item->path;
  99. }
  100. /* add the modified date for the file */
  101. $item->modified = isset($filePath) ? filemtime($filePath) : false;
  102. /* read the content of the file */
  103. $contentMD = isset($filePath) ? file_get_contents($filePath) : false;
  104. }
  105. # dispatch the original content without plugin-manipulations for case anyone wants to use it
  106. $this->c->dispatcher->dispatch('onOriginalLoaded', new OnOriginalLoaded($contentMD));
  107. $contentMD = $this->c->dispatcher->dispatch('onMarkdownLoaded', new OnMarkdownLoaded($contentMD))->getData();
  108. /* initialize parsedown */
  109. $parsedown = new ParsedownExtension();
  110. /* set safe mode to escape javascript and html in markdown */
  111. $parsedown->setSafeMode(true);
  112. /* parse markdown-file to content-array */
  113. $contentArray = $parsedown->text($contentMD);
  114. $contentArray = $this->c->dispatcher->dispatch('onContentArrayLoaded', new OnContentArrayLoaded($contentArray))->getData();
  115. /* get the first image from content array */
  116. $firstImage = $this->getFirstImage($contentArray);
  117. $itemUrl = isset($item->urlRel) ? $item->urlRel : false;
  118. /* parse markdown-content-array to content-string */
  119. $contentHTML = $parsedown->markup($contentArray, $itemUrl);
  120. $contentHTML = $this->c->dispatcher->dispatch('onHtmlLoaded', new OnHtmlLoaded($contentHTML))->getData();
  121. /* extract the h1 headline*/
  122. $contentParts = explode("</h1>", $contentHTML);
  123. $title = isset($contentParts[0]) ? strip_tags($contentParts[0]) : $settings['title'];
  124. $contentHTML = isset($contentParts[1]) ? $contentParts[1] : $contentHTML;
  125. /* create excerpt from content */
  126. $excerpt = substr($contentHTML,0,500);
  127. /* create description from excerpt */
  128. $description = isset($excerpt) ? strip_tags($excerpt) : false;
  129. if($description)
  130. {
  131. $description = trim(preg_replace('/\s+/', ' ', $description));
  132. $description = substr($description, 0, 300);
  133. $lastSpace = strrpos($description, ' ');
  134. $description = substr($description, 0, $lastSpace);
  135. }
  136. /* get url and alt-tag for first image, if exists */
  137. if($firstImage)
  138. {
  139. preg_match('#\((.*?)\)#', $firstImage, $img_url);
  140. if($img_url[1])
  141. {
  142. preg_match('#\[(.*?)\]#', $firstImage, $img_alt);
  143. $firstImage = array('img_url' => $base_url . '/' . $img_url[1], 'img_alt' => $img_alt[1]);
  144. }
  145. }
  146. $home = empty($args) ? true : false;
  147. $theme = $settings['theme'];
  148. $route = empty($args) && isset($settings['themes'][$theme]['cover']) ? '/cover.twig' : '/index.twig';
  149. return $this->render($response, $route, [
  150. 'home' => $home,
  151. 'navigation' => $structure,
  152. 'content' => $contentHTML,
  153. 'item' => $item,
  154. 'breadcrumb' => $breadcrumb,
  155. 'settings' => $settings,
  156. 'title' => $title,
  157. 'description' => $description,
  158. 'base_url' => $base_url,
  159. 'image' => $firstImage ]);
  160. }
  161. protected function getCachedStructure($cache)
  162. {
  163. return $cache->getCache('cache', 'structure.txt');
  164. }
  165. protected function getFreshStructure($pathToContent, $cache, $uri)
  166. {
  167. /* scan the content of the folder */
  168. $structure = Folder::scanFolder($pathToContent);
  169. /* if there is no content, render an empty page */
  170. if(count($structure) == 0)
  171. {
  172. return false;
  173. }
  174. /* create an array of object with the whole content of the folder */
  175. $structure = Folder::getFolderContentDetails($structure, $uri->getBaseUrl(), $uri->getBasePath());
  176. /* cache navigation */
  177. $cache->updateCache('cache', 'structure.txt', 'lastCache.txt', $structure);
  178. return $structure;
  179. }
  180. protected function updateVersion($baseUrl)
  181. {
  182. /* check the latest public typemill version */
  183. $version = new VersionCheck();
  184. $latestVersion = $version->checkVersion($baseUrl);
  185. if($latestVersion)
  186. {
  187. /* store latest version */
  188. \Typemill\Settings::updateSettings(array('latestVersion' => $latestVersion));
  189. }
  190. }
  191. protected function getFirstImage(array $contentBlocks)
  192. {
  193. foreach($contentBlocks as $block)
  194. {
  195. /* is it a paragraph? */
  196. if(isset($block['name']) && $block['name'] == 'p')
  197. {
  198. if(isset($block['handler']['argument']) && substr($block['handler']['argument'], 0, 2) == '![' )
  199. {
  200. return $block['handler']['argument'];
  201. }
  202. }
  203. }
  204. return false;
  205. }
  206. }