PageController.php 8.4 KB

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