PageController.php 5.4 KB

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