PageController.php 6.3 KB

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