PageController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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\Events\LoadPagetreeEvent;
  11. use Typemill\Events\LoadMarkdownEvent;
  12. use Typemill\Events\ParseHtmlEvent;
  13. use Typemill\Extensions\ParsedownExtension;
  14. class PageController extends Controller
  15. {
  16. public function index($request, $response, $args)
  17. {
  18. /* Initiate Variables */
  19. $structure = false;
  20. $contentHTML = false;
  21. $item = false;
  22. $breadcrumb = false;
  23. $description = '';
  24. $settings = $this->c->get('settings');
  25. $pathToContent = $settings['rootPath'] . $settings['contentFolder'];
  26. $cache = new WriteCache();
  27. $uri = $request->getUri();
  28. $base_url = $uri->getBaseUrl();
  29. try
  30. {
  31. /* if the cached structure is still valid, use it */
  32. if($cache->validate('cache', 'lastCache.txt',600))
  33. {
  34. $structure = $this->getCachedStructure($cache);
  35. $cached = true;
  36. }
  37. else
  38. {
  39. /* if not, get a fresh structure of the content folder */
  40. $structure = $this->getFreshStructure($pathToContent, $cache, $uri);
  41. $cached = false;
  42. /* if there is no structure at all, the content folder is probably empty */
  43. if(!$structure)
  44. {
  45. $content = '<h1>No Content</h1><p>Your content folder is empty.</p>';
  46. $this->render($response, '/index.twig', [ 'content' => $content ]);
  47. }
  48. elseif(!$cache->validate('cache', 'lastSitemap.txt', 86400))
  49. {
  50. /* update sitemap */
  51. $sitemap = new WriteSitemap();
  52. $sitemap->updateSitemap('cache', 'sitemap.xml', 'lastSitemap.txt', $structure, $uri->getBaseUrl());
  53. /* check and update the typemill-version in the user settings */
  54. $this->updateVersion($uri->getBaseUrl());
  55. }
  56. }
  57. /* dispatch event and let others manipulate the structure */
  58. $structure = $this->c->dispatcher->dispatch('onPagetreeLoaded', new LoadPagetreeEvent($structure))->getData();
  59. }
  60. catch (Exception $e)
  61. {
  62. echo $e->getMessage();
  63. exit(1);
  64. }
  65. /* if the user is on startpage */
  66. if(empty($args))
  67. {
  68. /* check, if there is an index-file in the root of the content folder */
  69. $contentMD = file_exists($pathToContent . DIRECTORY_SEPARATOR . 'index.md') ? file_get_contents($pathToContent . DIRECTORY_SEPARATOR . 'index.md') : NULL;
  70. }
  71. else
  72. {
  73. /* get the request url */
  74. $urlRel = $uri->getBasePath() . '/' . $args['params'];
  75. /* find the url in the content-item-tree and return the item-object for the file */
  76. $item = Folder::getItemForUrl($structure, $urlRel);
  77. /* if structure is cached and there is no item
  78. if($cached && !$item)
  79. {
  80. /* get a fresh structure and search for the item again
  81. $structure = $this->getFreshStructure($pathToContent, $cache, $uri);
  82. $item = Folder::getItemForUrl($structure, $urlRel);
  83. }
  84. /* if there is still no item, return a 404-page */
  85. if(!$item)
  86. {
  87. return $this->render404($response, array( 'navigation' => $structure, 'settings' => $settings, 'base_url' => $base_url ));
  88. }
  89. /* get breadcrumb for page */
  90. $breadcrumb = Folder::getBreadcrumb($structure, $item->keyPathArray);
  91. /* add the paging to the item */
  92. $item = Folder::getPagingForItem($structure, $item);
  93. /* check if url is a folder. If so, check if there is an index-file in that folder */
  94. if($item->elementType == 'folder' && $item->index)
  95. {
  96. $filePath = $pathToContent . $item->path . DIRECTORY_SEPARATOR . 'index.md';
  97. }
  98. elseif($item->elementType == 'file')
  99. {
  100. $filePath = $pathToContent . $item->path;
  101. }
  102. /* read the content of the file */
  103. $contentMD = isset($filePath) ? file_get_contents($filePath) : false;
  104. }
  105. $contentMD = $this->c->dispatcher->dispatch('onMarkdownLoaded', new LoadMarkdownEvent($contentMD))->getData();
  106. /* initialize parsedown */
  107. // $Parsedown = new \ParsedownExtra();
  108. $Parsedown = new ParsedownExtension();
  109. /* parse markdown-file to html-string */
  110. $contentHTML = $Parsedown->text($contentMD);
  111. $contentHTML = $this->c->dispatcher->dispatch('onHtmlParsed', new ParseHtmlEvent($contentHTML))->getData();
  112. $excerpt = substr($contentHTML,0,200);
  113. $excerpt = explode("</h1>", $excerpt);
  114. $title = isset($excerpt[0]) ? strip_tags($excerpt[0]) : $settings['title'];
  115. $description = isset($excerpt[1]) ? strip_tags($excerpt[1]) : false;
  116. $description = $description ? trim(preg_replace('/\s+/', ' ', $description)) : false;
  117. /*
  118. $timer['topiccontroller']=microtime(true);
  119. $timer['end topiccontroller']=microtime(true);
  120. Helpers::printTimer($timer);
  121. */
  122. $route = empty($args) && $settings['startpage'] ? '/cover.twig' : '/index.twig';
  123. $this->render($response, $route, array('navigation' => $structure, 'content' => $contentHTML, 'item' => $item, 'breadcrumb' => $breadcrumb, 'settings' => $settings, 'title' => $title, 'description' => $description, 'base_url' => $base_url ));
  124. }
  125. protected function getCachedStructure($cache)
  126. {
  127. return $cache->getCache('cache', 'structure.txt');
  128. }
  129. protected function getFreshStructure($pathToContent, $cache, $uri)
  130. {
  131. /* scan the content of the folder */
  132. $structure = Folder::scanFolder($pathToContent);
  133. /* if there is no content, render an empty page */
  134. if(count($structure) == 0)
  135. {
  136. return false;
  137. }
  138. /* create an array of object with the whole content of the folder */
  139. $structure = Folder::getFolderContentDetails($structure, $uri->getBaseUrl(), $uri->getBasePath());
  140. /* cache navigation */
  141. $cache->updateCache('cache', 'structure.txt', 'lastCache.txt', $structure);
  142. return $structure;
  143. }
  144. protected function updateVersion($baseUrl)
  145. {
  146. /* check the latest public typemill version */
  147. $version = new VersionCheck();
  148. $latestVersion = $version->checkVersion($baseUrl);
  149. if($latestVersion)
  150. {
  151. /* check, if user-settings exist */
  152. $yaml = new WriteYaml();
  153. $userSettings = $yaml->getYaml('settings', 'settings.yaml');
  154. if($userSettings)
  155. {
  156. /* if there is no version info in the settings or if the version info is outdated */
  157. if(!isset($userSettings['latestVersion']) || $userSettings['latestVersion'] != $latestVersion)
  158. {
  159. /* write the latest version into the user-settings */
  160. $userSettings['latestVersion'] = $latestVersion;
  161. $yaml->updateYaml('settings', 'settings.yaml', $userSettings);
  162. }
  163. }
  164. }
  165. }
  166. }