PageController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 version check is not needed
  60. # $this->updateVersion($uri->getBaseUrl());
  61. }
  62. }
  63. /* dispatch event and let others manipulate the structure */
  64. $structure = $this->c->dispatcher->dispatch('onPagetreeLoaded', new OnPagetreeLoaded($structure))->getData();
  65. }
  66. catch (Exception $e)
  67. {
  68. echo $e->getMessage();
  69. exit(1);
  70. }
  71. # get the cached navigation here (structure without hidden files )
  72. $navigation = $cache->getCache('cache', 'navigation.txt');
  73. if(!$navigation)
  74. {
  75. # use the structure as navigation if there is no difference
  76. $navigation = $structure;
  77. }
  78. # if the user is on startpage
  79. if(empty($args))
  80. {
  81. $home = true;
  82. $item = Folder::getItemForUrl($structure, $uri->getBasePath(), $uri->getBasePath());
  83. }
  84. else
  85. {
  86. /* get the request url */
  87. $urlRel = $uri->getBasePath() . '/' . $args['params'];
  88. /* find the url in the content-item-tree and return the item-object for the file */
  89. $item = Folder::getItemForUrl($structure, $urlRel, $uri->getBasePath());
  90. /* if there is still no item, return a 404-page */
  91. if(!$item)
  92. {
  93. return $this->render404($response, array( 'navigation' => $navigation, 'settings' => $settings, 'base_url' => $base_url ));
  94. }
  95. /* get breadcrumb for page */
  96. $breadcrumb = Folder::getBreadcrumb($structure, $item->keyPathArray);
  97. $breadcrumb = $this->c->dispatcher->dispatch('onBreadcrumbLoaded', new OnBreadcrumbLoaded($breadcrumb))->getData();
  98. # set pages active for navigation again
  99. Folder::getBreadcrumb($navigation, $item->keyPathArray);
  100. /* add the paging to the item */
  101. $item = Folder::getPagingForItem($structure, $item);
  102. }
  103. # dispatch the item
  104. $item = $this->c->dispatcher->dispatch('onItemLoaded', new OnItemLoaded($item))->getData();
  105. # set the filepath
  106. $filePath = $pathToContent . $item->path;
  107. # check if url is a folder and add index.md
  108. if($item->elementType == 'folder')
  109. {
  110. $filePath = $filePath . DIRECTORY_SEPARATOR . 'index.md';
  111. }
  112. # read the content of the file
  113. $contentMD = file_exists($filePath) ? file_get_contents($filePath) : false;
  114. # dispatch the original content without plugin-manipulations for case anyone wants to use it
  115. $this->c->dispatcher->dispatch('onOriginalLoaded', new OnOriginalLoaded($contentMD));
  116. # get meta-Information
  117. $writeYaml = new WriteYaml();
  118. $metatabs = $writeYaml->getPageMeta($settings, $item);
  119. if(!$metatabs)
  120. {
  121. $metatabs = $writeYaml->getPageMetaDefaults($contentMD, $settings, $item);
  122. }
  123. # dispatch meta
  124. $metatabs = $this->c->dispatcher->dispatch('onMetaLoaded', new OnMetaLoaded($metatabs))->getData();
  125. # dispatch content
  126. $contentMD = $this->c->dispatcher->dispatch('onMarkdownLoaded', new OnMarkdownLoaded($contentMD))->getData();
  127. /* initialize parsedown */
  128. $parsedown = new ParsedownExtension();
  129. /* set safe mode to escape javascript and html in markdown */
  130. $parsedown->setSafeMode(true);
  131. /* parse markdown-file to content-array */
  132. $contentArray = $parsedown->text($contentMD);
  133. $contentArray = $this->c->dispatcher->dispatch('onContentArrayLoaded', new OnContentArrayLoaded($contentArray))->getData();
  134. /* get the first image from content array */
  135. $firstImage = $this->getFirstImage($contentArray);
  136. $itemUrl = isset($item->urlRel) ? $item->urlRel : false;
  137. /* parse markdown-content-array to content-string */
  138. $contentHTML = $parsedown->markup($contentArray, $itemUrl);
  139. $contentHTML = $this->c->dispatcher->dispatch('onHtmlLoaded', new OnHtmlLoaded($contentHTML))->getData();
  140. /* extract the h1 headline*/
  141. $contentParts = explode("</h1>", $contentHTML);
  142. $title = isset($contentParts[0]) ? strip_tags($contentParts[0]) : $settings['title'];
  143. $contentHTML = isset($contentParts[1]) ? $contentParts[1] : $contentHTML;
  144. # if there is not meta description
  145. if(!isset($metatabs['meta']['description']) or !$metatabs['meta']['description'])
  146. {
  147. # create excerpt from html
  148. $excerpt = substr($contentHTML,0,500);
  149. # create description from excerpt
  150. $description = isset($excerpt) ? strip_tags($excerpt) : false;
  151. if($description)
  152. {
  153. $description = trim(preg_replace('/\s+/', ' ', $description));
  154. $description = substr($description, 0, 300);
  155. $lastSpace = strrpos($description, ' ');
  156. $metatabs['meta']['description'] = substr($description, 0, $lastSpace);
  157. }
  158. }
  159. /* get url and alt-tag for first image, if exists */
  160. if($firstImage)
  161. {
  162. preg_match('#\((.*?)\)#', $firstImage, $img_url);
  163. if($img_url[1])
  164. {
  165. preg_match('#\[(.*?)\]#', $firstImage, $img_alt);
  166. $firstImage = array('img_url' => $base_url . '/' . $img_url[1], 'img_alt' => $img_alt[1]);
  167. }
  168. }
  169. $theme = $settings['theme'];
  170. $route = empty($args) && isset($settings['themes'][$theme]['cover']) ? '/cover.twig' : '/index.twig';
  171. # check if there is a custom theme css
  172. $customcss = $writeYaml->checkFile('cache', $theme . '-custom.css');
  173. if($customcss)
  174. {
  175. $this->c->assets->addCSS($base_url . '/cache/' . $theme . '-custom.css');
  176. }
  177. $logo = false;
  178. if(isset($settings['logo']) && $settings['logo'] != '')
  179. {
  180. $logo = 'media/files/' . $settings['logo'];
  181. }
  182. $favicon = false;
  183. if(isset($settings['favicon']) && $settings['favicon'] != '')
  184. {
  185. $favicon = true;
  186. }
  187. return $this->render($response, $route, [
  188. 'home' => $home,
  189. 'navigation' => $navigation,
  190. 'title' => $title,
  191. 'content' => $contentHTML,
  192. 'item' => $item,
  193. 'breadcrumb' => $breadcrumb,
  194. 'settings' => $settings,
  195. 'metatabs' => $metatabs,
  196. 'base_url' => $base_url,
  197. 'image' => $firstImage,
  198. 'logo' => $logo,
  199. 'favicon' => $favicon
  200. ]);
  201. }
  202. protected function getCachedStructure($cache)
  203. {
  204. return $cache->getCache('cache', 'structure.txt');
  205. }
  206. protected function getFreshStructure($pathToContent, $cache, $uri)
  207. {
  208. /* scan the content of the folder */
  209. $structure = Folder::scanFolder($pathToContent);
  210. /* if there is no content, render an empty page */
  211. if(count($structure) == 0)
  212. {
  213. return false;
  214. }
  215. # get the extended structure files with changes like navigation title or hidden pages
  216. $yaml = new writeYaml();
  217. $extended = $yaml->getYaml('cache', 'structure-extended.yaml');
  218. /* create an array of object with the whole content of the folder */
  219. $structure = Folder::getFolderContentDetails($structure, $extended, $uri->getBaseUrl(), $uri->getBasePath());
  220. /* cache structure */
  221. $cache->updateCache('cache', 'structure.txt', 'lastCache.txt', $structure);
  222. if($extended && $this->containsHiddenPages($extended))
  223. {
  224. # generate the navigation (delete empty pages)
  225. $navigation = $this->createNavigationFromStructure($structure);
  226. # cache navigation
  227. $cache->updateCache('cache', 'navigation.txt', false, $navigation);
  228. }
  229. else
  230. {
  231. # make sure no separate navigation file is set
  232. $cache->deleteFileWithPath('cache' . DIRECTORY_SEPARATOR . 'navigation.txt');
  233. }
  234. return $structure;
  235. }
  236. protected function containsHiddenPages($extended)
  237. {
  238. foreach($extended as $element)
  239. {
  240. if(isset($element['hide']) && $element['hide'] === true)
  241. {
  242. return true;
  243. }
  244. }
  245. return false;
  246. }
  247. protected function createNavigationFromStructure($navigation)
  248. {
  249. foreach ($navigation as $key => $element)
  250. {
  251. if($element->hide === true)
  252. {
  253. unset($navigation[$key]);
  254. }
  255. elseif(isset($element->folderContent))
  256. {
  257. $navigation[$key]->folderContent = $this->createNavigationFromStructure($element->folderContent);
  258. }
  259. }
  260. return $navigation;
  261. }
  262. # not in use, stored the latest version in user settings, but that does not make sense because checkd on the fly with api in admin
  263. protected function updateVersion($baseUrl)
  264. {
  265. /* check the latest public typemill version */
  266. $version = new VersionCheck();
  267. $latestVersion = $version->checkVersion($baseUrl);
  268. if($latestVersion)
  269. {
  270. /* store latest version */
  271. \Typemill\Settings::updateSettings(array('latestVersion' => $latestVersion));
  272. }
  273. }
  274. protected function getFirstImage(array $contentBlocks)
  275. {
  276. foreach($contentBlocks as $block)
  277. {
  278. /* is it a paragraph? */
  279. if(isset($block['name']) && $block['name'] == 'p')
  280. {
  281. if(isset($block['handler']['argument']) && substr($block['handler']['argument'], 0, 2) == '![' )
  282. {
  283. return $block['handler']['argument'];
  284. }
  285. }
  286. }
  287. return false;
  288. }
  289. }