ContentController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <?php
  2. namespace Typemill\Controllers;
  3. use Slim\Http\Request;
  4. use Slim\Http\Response;
  5. use Psr\Container\ContainerInterface;
  6. use Typemill\Models\Validation;
  7. use Typemill\Models\Folder;
  8. use Typemill\Models\Write;
  9. use Typemill\Models\WriteCache;
  10. use Typemill\Models\WriteYaml;
  11. use Typemill\Models\WriteMeta;
  12. abstract class ContentController
  13. {
  14. # holds the pimple container
  15. protected $c;
  16. # holds the params from request
  17. protected $params;
  18. # holds the slim-uri-object
  19. protected $uri;
  20. # holds the errors to output in frontend
  21. protected $errors = false;
  22. # holds a write object to write files
  23. protected $write;
  24. # holds the structure of content folder as a serialized array of objects
  25. protected $structure;
  26. # holds the name of the structure-file with drafts for author environment
  27. protected $structureDraftName;
  28. # holds the name of the structure-file without drafts for live site
  29. protected $structureLiveName;
  30. # holds informations about the homepage
  31. protected $homepage;
  32. # hold the page-item as an object
  33. protected $item;
  34. # hold the breadcrumb as an object
  35. protected $breadcrumb;
  36. # holds the path to the requested file
  37. protected $path = false;
  38. # holds the content of the page
  39. protected $content;
  40. # holds the ownership (my content or not my content)
  41. protected $mycontent = false;
  42. public function __construct(ContainerInterface $c)
  43. {
  44. $this->c = $c;
  45. $this->settings = $this->c->get('settings');
  46. $this->structureLiveName = 'structure.txt';
  47. $this->structureDraftName = 'structure-draft.txt';
  48. $this->c->dispatcher->dispatch('onTwigLoaded');
  49. }
  50. # admin ui rendering
  51. protected function render($response, $route, $data)
  52. {
  53. if(isset($_SESSION['old']))
  54. {
  55. unset($_SESSION['old']);
  56. }
  57. $response = $response->withoutHeader('Server');
  58. $response = $response->withoutHeader('X-Powered-By');
  59. if($this->c->request->getUri()->getScheme() == 'https')
  60. {
  61. $response = $response->withAddedHeader('Strict-Transport-Security', 'max-age=63072000');
  62. }
  63. $response = $response->withAddedHeader('X-Content-Type-Options', 'nosniff');
  64. $response = $response->withAddedHeader('X-Frame-Options', 'SAMEORIGIN');
  65. $response = $response->withAddedHeader('X-XSS-Protection', '1;mode=block');
  66. $response = $response->withAddedHeader('Referrer-Policy', 'no-referrer-when-downgrade');
  67. return $this->c->view->render($response, $route, $data);
  68. }
  69. protected function render404($response, $data = NULL)
  70. {
  71. return $this->c->view->render($response->withStatus(404), '/404.twig', $data);
  72. }
  73. protected function renderIntern404($response, $data = NULL)
  74. {
  75. return $this->c->view->render($response->withStatus(404), '/intern404.twig', $data);
  76. }
  77. protected function getValidator()
  78. {
  79. return new Validation();
  80. }
  81. protected function validateEditorInput()
  82. {
  83. $validate = new Validation();
  84. $vResult = $validate->editorInput($this->params);
  85. if(is_array($vResult))
  86. {
  87. $message = reset($vResult);
  88. $this->errors = ['errors' => $vResult];
  89. if(isset($message[0])){ $this->errors['errors']['message'] = $message[0]; }
  90. return false;
  91. }
  92. return true;
  93. }
  94. protected function validateBlockInput()
  95. {
  96. $validate = new Validation();
  97. $vResult = $validate->blockInput($this->params);
  98. if(is_array($vResult))
  99. {
  100. $message = reset($vResult);
  101. $this->errors = ['errors' => $vResult];
  102. if(isset($message[0])){ $this->errors['errors']['message'] = $message[0]; }
  103. return false;
  104. }
  105. return true;
  106. }
  107. protected function validateNavigationSort()
  108. {
  109. $validate = new Validation();
  110. $vResult = $validate->navigationSort($this->params);
  111. if(is_array($vResult))
  112. {
  113. $message = reset($vResult);
  114. $this->errors = ['errors' => $vResult];
  115. if(isset($message[0])){ $this->errors['errors']['message'] = $message[0]; }
  116. return false;
  117. }
  118. return true;
  119. }
  120. protected function validateNaviItem()
  121. {
  122. $validate = new Validation();
  123. $vResult = $validate->navigationItem($this->params);
  124. if(is_array($vResult))
  125. {
  126. $message = reset($vResult);
  127. $this->errors = ['errors' => $vResult];
  128. if(isset($message[0])){ $this->errors['errors']['message'] = $message[0]; }
  129. return false;
  130. }
  131. return true;
  132. }
  133. protected function validateBaseNaviItem()
  134. {
  135. $validate = new Validation();
  136. $vResult = $validate->navigationBaseItem($this->params);
  137. if(is_array($vResult))
  138. {
  139. $message = reset($vResult);
  140. $this->errors = ['errors' => $vResult];
  141. if(isset($message[0])){ $this->errors['errors']['message'] = $message[0]; }
  142. return false;
  143. }
  144. return true;
  145. }
  146. protected function setStructure($draft = false, $cache = true)
  147. {
  148. # set initial structure to false
  149. $structure = false;
  150. # name of structure-file for draft or live
  151. $filename = $draft ? $this->structureDraftName : $this->structureLiveName;
  152. # set variables and objects
  153. $this->write = new writeCache();
  154. # check, if cached structure is still valid
  155. if($cache && $this->write->validate('cache', 'lastCache.txt', 600))
  156. {
  157. # get the cached structure
  158. $structure = $this->write->getCache('cache', $filename);
  159. }
  160. # if no structure was found or cache is deactivated
  161. if(!$structure)
  162. {
  163. # scan the content of the folder
  164. $structure = Folder::scanFolder($this->settings['rootPath'] . $this->settings['contentFolder'], $draft);
  165. # if there is content, then get the content details
  166. if(count($structure) > 0)
  167. {
  168. # get the extended structure files with changes like navigation title or hidden pages
  169. $yaml = new writeYaml();
  170. $extended = $yaml->getYaml('cache', 'structure-extended.yaml');
  171. # create an array of object with the whole content of the folder and changes from extended file
  172. $structure = Folder::getFolderContentDetails($structure, $extended, $this->uri->getBaseUrl(), $this->uri->getBasePath());
  173. }
  174. # cache navigation
  175. $this->write->updateCache('cache', $filename, 'lastCache.txt', $structure);
  176. }
  177. $this->structure = $structure;
  178. return true;
  179. }
  180. protected function renameExtended($item, $newFolder)
  181. {
  182. # get the extended structure files with changes like navigation title or hidden pages
  183. $yaml = new writeYaml();
  184. $extended = $yaml->getYaml('cache', 'structure-extended.yaml');
  185. if(isset($extended[$item->urlRelWoF]))
  186. {
  187. $newUrl = $newFolder->urlRelWoF . '/' . $item->slug;
  188. $entry = $extended[$item->urlRelWoF];
  189. unset($extended[$item->urlRelWoF]);
  190. $extended[$newUrl] = $entry;
  191. $yaml->updateYaml('cache', 'structure-extended.yaml', $extended);
  192. }
  193. return true;
  194. }
  195. protected function deleteFromExtended()
  196. {
  197. # get the extended structure files with changes like navigation title or hidden pages
  198. $yaml = new writeYaml();
  199. $extended = $yaml->getYaml('cache', 'structure-extended.yaml');
  200. if($this->item->elementType == "file" && isset($extended[$this->item->urlRelWoF]))
  201. {
  202. unset($extended[$this->item->urlRelWoF]);
  203. $yaml->updateYaml('cache', 'structure-extended.yaml', $extended);
  204. }
  205. if($this->item->elementType == "folder")
  206. {
  207. $changed = false;
  208. # delete all entries with that folder url
  209. foreach($extended as $url => $entries)
  210. {
  211. if( strpos($url, $this->item->urlRelWoF) !== false )
  212. {
  213. $changed = true;
  214. unset($extended[$url]);
  215. }
  216. }
  217. if($changed)
  218. {
  219. $yaml->updateYaml('cache', 'structure-extended.yaml', $extended);
  220. }
  221. }
  222. }
  223. # this is only set by content backend controller
  224. protected function setHomepage($args)
  225. {
  226. $contentFolder = Folder::scanFolderFlat($this->settings['rootPath'] . $this->settings['contentFolder']);
  227. if(in_array('index.md', $contentFolder))
  228. {
  229. $md = true;
  230. $status = 'published';
  231. }
  232. if(in_array('index.txt', $contentFolder))
  233. {
  234. $txt = true;
  235. $status = 'unpublished';
  236. }
  237. if(isset($txt) && isset($md))
  238. {
  239. $status = 'modified';
  240. }
  241. $active = false;
  242. if($this->params['url'] == '/' || (is_array($args) && empty($args)))
  243. {
  244. $active = 'active';
  245. }
  246. $this->homepage = ['status' => $status, 'active' => $active];
  247. }
  248. protected function setItem()
  249. {
  250. # home is only set by backend controller, not by api calls
  251. $home = isset($this->homepage['active']) ? $this->homepage['active'] : false;
  252. # search for the url in the structure
  253. $item = Folder::getItemForUrl($this->structure, $this->params['url'], $this->uri->getBaseUrl(), NULL, $home);
  254. if($item)
  255. {
  256. $this->item = $item;
  257. return true;
  258. }
  259. $this->errors = ['errors' => ['message' => 'requested page-url not found']];
  260. return false;
  261. }
  262. # determine if you want to write to published file (md) or to draft (txt)
  263. protected function setItemPath($fileType)
  264. {
  265. $this->path = $this->item->pathWithoutType . '.' . $fileType;
  266. }
  267. protected function setPublishStatus()
  268. {
  269. $this->item->published = false;
  270. $this->item->drafted = false;
  271. if(file_exists($this->settings['rootPath'] . $this->settings['contentFolder'] . $this->item->pathWithoutType . '.md'))
  272. {
  273. $this->item->published = true;
  274. # add file-type in case it is a folder
  275. $this->item->fileType = "md";
  276. }
  277. if(file_exists($this->settings['rootPath'] . $this->settings['contentFolder'] . $this->item->pathWithoutType . '.txt'))
  278. {
  279. $this->item->drafted = true;
  280. # add file-type in case it is a folder
  281. $this->item->fileType = "txt";
  282. }
  283. if(!$this->item->drafted && !$this->item->published && $this->item->elementType == "folder")
  284. {
  285. # set txt as default for a folder, so that we can create an index.txt for a folder.
  286. $this->item->fileType = "txt";
  287. }
  288. }
  289. protected function deleteContentFiles($fileTypes, $folder = false)
  290. {
  291. $basePath = $this->settings['rootPath'] . $this->settings['contentFolder'];
  292. foreach($fileTypes as $fileType)
  293. {
  294. if(file_exists($basePath . $this->item->pathWithoutType . '.' . $fileType) && !unlink($basePath . $this->item->pathWithoutType . '.' . $fileType) )
  295. {
  296. $this->errors = ['message' => 'We could not delete the file, please check, if the file is writable.'];
  297. }
  298. }
  299. if($this->errors)
  300. {
  301. return false;
  302. }
  303. return true;
  304. }
  305. protected function deleteContentFolder()
  306. {
  307. $basePath = $this->settings['rootPath'] . $this->settings['contentFolder'];
  308. $path = $basePath . $this->item->path;
  309. if(file_exists($path))
  310. {
  311. $files = array_diff(scandir($path), array('.', '..'));
  312. # check if there are published pages or folders inside, then stop the operation
  313. foreach ($files as $file)
  314. {
  315. if(is_dir(realpath($path) . DIRECTORY_SEPARATOR . $file))
  316. {
  317. $this->errors = ['message' => 'Please delete the sub-folder first.'];
  318. }
  319. if(substr($file, -3) == '.md' && $file != 'index.md')
  320. {
  321. $this->errors = ['message' => 'Please unpublish all pages in the folder first.'];
  322. }
  323. }
  324. if(!$this->errors)
  325. {
  326. foreach ($files as $file)
  327. {
  328. unlink(realpath($path) . DIRECTORY_SEPARATOR . $file);
  329. }
  330. return rmdir($path);
  331. }
  332. # delete all files from the extended file
  333. $this->deleteFromExtended();
  334. }
  335. return false;
  336. }
  337. protected function setContent()
  338. {
  339. # if the file exists
  340. if($this->item->published OR $this->item->drafted)
  341. {
  342. $content = $this->write->getFile($this->settings['contentFolder'], $this->path);
  343. if($this->item->fileType == 'txt')
  344. {
  345. # decode the json-draft to an array
  346. $content = json_decode($content);
  347. }
  348. }
  349. elseif($this->item->elementType == "folder")
  350. {
  351. $content = '';
  352. }
  353. else
  354. {
  355. $this->errors = ['errors' => ['message' => 'requested file not found']];
  356. return false;
  357. }
  358. $this->content = $content;
  359. return true;
  360. }
  361. protected function checkContentOwnership()
  362. {
  363. # get page meta
  364. $writeMeta = new writeMeta();
  365. $pagemeta = $writeMeta->getPageMeta($this->settings, $this->item);
  366. # owner assertion, not
  367. if(isset($pagemeta['meta']['owner']) && $pagemeta['meta']['owner'] == $_SESSION['user'])
  368. {
  369. $this->mycontent = true;
  370. return true;
  371. }
  372. return false;
  373. }
  374. }