ContentController.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. abstract class ContentController
  11. {
  12. # holds the pimple container
  13. protected $c;
  14. # holds the params from request
  15. protected $params;
  16. # holds the slim-uri-object
  17. protected $uri;
  18. # holds the errors to output in frontend
  19. protected $errors = false;
  20. # holds a write object to write files
  21. protected $write;
  22. # holds the structure of content folder as a serialized array of objects
  23. protected $structure;
  24. # holds the name of the structure-file with drafts for author environment
  25. protected $structureDraftName;
  26. # holds the name of the structure-file without drafts for live site
  27. protected $structureLiveName;
  28. # holds informations about the homepage
  29. protected $homepage;
  30. # hold the page-item as an object
  31. protected $item;
  32. # hold the breadcrumb as an object
  33. protected $breadcrumb;
  34. # holds the path to the requested file
  35. protected $path = false;
  36. # holds the content of the page
  37. protected $content;
  38. public function __construct(ContainerInterface $c)
  39. {
  40. $this->c = $c;
  41. $this->settings = $this->c->get('settings');
  42. $this->structureLiveName = 'structure.txt';
  43. $this->structureDraftName = 'structure-draft.txt';
  44. }
  45. protected function render($response, $route, $data)
  46. {
  47. if(isset($_SESSION['old']))
  48. {
  49. unset($_SESSION['old']);
  50. }
  51. $response = $response->withoutHeader('Server');
  52. $response = $response->withoutHeader('X-Powered-By');
  53. if($this->c->request->getUri()->getScheme() == 'https')
  54. {
  55. $response = $response->withAddedHeader('Strict-Transport-Security', 'max-age=63072000');
  56. }
  57. $response = $response->withAddedHeader('X-Content-Type-Options', 'nosniff');
  58. $response = $response->withAddedHeader('X-Frame-Options', 'SAMEORIGIN');
  59. $response = $response->withAddedHeader('X-XSS-Protection', '1;mode=block');
  60. $response = $response->withAddedHeader('Referrer-Policy', 'no-referrer-when-downgrade');
  61. return $this->c->view->render($response, $route, $data);
  62. }
  63. protected function render404($response, $data = NULL)
  64. {
  65. return $this->c->view->render($response->withStatus(404), '/404.twig', $data);
  66. }
  67. protected function renderIntern404($response, $data = NULL)
  68. {
  69. return $this->c->view->render($response->withStatus(404), '/intern404.twig', $data);
  70. }
  71. protected function getValidator()
  72. {
  73. return new Validation();
  74. }
  75. protected function validateEditorInput()
  76. {
  77. $validate = new Validation();
  78. $vResult = $validate->editorInput($this->params);
  79. if(is_array($vResult))
  80. {
  81. $message = reset($vResult);
  82. $this->errors = ['errors' => $vResult];
  83. if(isset($message[0])){ $this->errors['errors']['message'] = $message[0]; }
  84. return false;
  85. }
  86. return true;
  87. }
  88. protected function validateBlockInput()
  89. {
  90. $validate = new Validation();
  91. $vResult = $validate->blockInput($this->params);
  92. if(is_array($vResult))
  93. {
  94. $message = reset($vResult);
  95. $this->errors = ['errors' => $vResult];
  96. if(isset($message[0])){ $this->errors['errors']['message'] = $message[0]; }
  97. return false;
  98. }
  99. return true;
  100. }
  101. protected function validateNavigationSort()
  102. {
  103. $validate = new Validation();
  104. $vResult = $validate->navigationSort($this->params);
  105. if(is_array($vResult))
  106. {
  107. $message = reset($vResult);
  108. $this->errors = ['errors' => $vResult];
  109. if(isset($message[0])){ $this->errors['errors']['message'] = $message[0]; }
  110. return false;
  111. }
  112. return true;
  113. }
  114. protected function validateNaviItem()
  115. {
  116. $validate = new Validation();
  117. $vResult = $validate->navigationItem($this->params);
  118. if(is_array($vResult))
  119. {
  120. $message = reset($vResult);
  121. $this->errors = ['errors' => $vResult];
  122. if(isset($message[0])){ $this->errors['errors']['message'] = $message[0]; }
  123. return false;
  124. }
  125. return true;
  126. }
  127. protected function validateBaseNaviItem()
  128. {
  129. $validate = new Validation();
  130. $vResult = $validate->navigationBaseItem($this->params);
  131. if(is_array($vResult))
  132. {
  133. $message = reset($vResult);
  134. $this->errors = ['errors' => $vResult];
  135. if(isset($message[0])){ $this->errors['errors']['message'] = $message[0]; }
  136. return false;
  137. }
  138. return true;
  139. }
  140. protected function setStructure($draft = false, $cache = true)
  141. {
  142. # set initial structure to false
  143. $structure = false;
  144. # name of structure-file for draft or live
  145. $filename = $draft ? $this->structureDraftName : $this->structureLiveName;
  146. # set variables and objects
  147. $this->write = new writeCache();
  148. # check, if cached structure is still valid
  149. if($cache && $this->write->validate('cache', 'lastCache.txt', 600))
  150. {
  151. # get the cached structure
  152. $structure = $this->write->getCache('cache', $filename);
  153. }
  154. # if no structure was found or cache is deactivated
  155. if(!$structure)
  156. {
  157. # scan the content of the folder
  158. $structure = Folder::scanFolder($this->settings['rootPath'] . $this->settings['contentFolder'], $draft);
  159. # if there is content, then get the content details
  160. if(count($structure) > 0)
  161. {
  162. # create an array of object with the whole content of the folder
  163. $structure = Folder::getFolderContentDetails($structure, $this->uri->getBaseUrl(), $this->uri->getBasePath());
  164. }
  165. # cache navigation
  166. $this->write->updateCache('cache', $filename, 'lastCache.txt', $structure);
  167. }
  168. $this->structure = $structure;
  169. return true;
  170. }
  171. protected function setHomepage()
  172. {
  173. $contentFolder = Folder::scanFolderFlat($this->settings['rootPath'] . $this->settings['contentFolder']);
  174. if(in_array('index.md', $contentFolder))
  175. {
  176. $md = true;
  177. $status = 'published';
  178. }
  179. if(in_array('index.txt', $contentFolder))
  180. {
  181. $txt = true;
  182. $status = 'unpublished';
  183. }
  184. if(isset($txt) && isset($md))
  185. {
  186. $status = 'modified';
  187. }
  188. $active = false;
  189. if($this->params['url'] == '/' || $this->params['url'] == $this->uri->getBasePath() )
  190. {
  191. $active = 'active';
  192. }
  193. $this->homepage = ['status' => $status, 'active' => $active];
  194. }
  195. protected function setItem()
  196. {
  197. # search for the url in the structure
  198. $item = Folder::getItemForUrl($this->structure, $this->params['url'], $this->uri->getBasePath());
  199. if($item)
  200. {
  201. $this->item = $item;
  202. return true;
  203. }
  204. $this->errors = ['errors' => ['message' => 'requested page-url not found']];
  205. return false;
  206. }
  207. # determine if you want to write to published file (md) or to draft (txt)
  208. protected function setItemPath($fileType)
  209. {
  210. $this->path = $this->item->pathWithoutType . '.' . $fileType;
  211. }
  212. protected function setPublishStatus()
  213. {
  214. $this->item->published = false;
  215. $this->item->drafted = false;
  216. if(file_exists($this->settings['rootPath'] . $this->settings['contentFolder'] . $this->item->pathWithoutType . '.md'))
  217. {
  218. $this->item->published = true;
  219. # add file-type in case it is a folder
  220. $this->item->fileType = "md";
  221. }
  222. if(file_exists($this->settings['rootPath'] . $this->settings['contentFolder'] . $this->item->pathWithoutType . '.txt'))
  223. {
  224. $this->item->drafted = true;
  225. # add file-type in case it is a folder
  226. $this->item->fileType = "txt";
  227. }
  228. if(!$this->item->drafted && !$this->item->published && $this->item->elementType == "folder")
  229. {
  230. # set txt as default for a folder, so that we can create an index.txt for a folder.
  231. $this->item->fileType = "txt";
  232. }
  233. }
  234. protected function deleteContentFiles($fileTypes, $folder = false)
  235. {
  236. $basePath = $this->settings['rootPath'] . $this->settings['contentFolder'];
  237. foreach($fileTypes as $fileType)
  238. {
  239. if(file_exists($basePath . $this->item->pathWithoutType . '.' . $fileType) && !unlink($basePath . $this->item->pathWithoutType . '.' . $fileType) )
  240. {
  241. $this->errors = ['message' => 'We could not delete the file, please check, if the file is writable.'];
  242. }
  243. }
  244. if($this->errors)
  245. {
  246. return false;
  247. }
  248. return true;
  249. }
  250. protected function deleteContentFolder()
  251. {
  252. $basePath = $this->settings['rootPath'] . $this->settings['contentFolder'];
  253. $path = $basePath . $this->item->path;
  254. if(file_exists($path))
  255. {
  256. $files = array_diff(scandir($path), array('.', '..'));
  257. # check if there are folders first, then stop the operation
  258. foreach ($files as $file)
  259. {
  260. if(is_dir(realpath($path) . DIRECTORY_SEPARATOR . $file))
  261. {
  262. $this->errors = ['message' => 'Please delete the sub-folder first.'];
  263. }
  264. }
  265. if(!$this->errors)
  266. {
  267. foreach ($files as $file)
  268. {
  269. unlink(realpath($path) . DIRECTORY_SEPARATOR . $file);
  270. }
  271. return rmdir($path);
  272. }
  273. }
  274. return false;
  275. }
  276. protected function setContent()
  277. {
  278. # if the file exists
  279. if($this->item->published OR $this->item->drafted)
  280. {
  281. $content = $this->write->getFile($this->settings['contentFolder'], $this->path);
  282. if($this->item->fileType == 'txt')
  283. {
  284. # decode the json-draft to an array
  285. $content = json_decode($content);
  286. }
  287. }
  288. elseif($this->item->elementType == "folder")
  289. {
  290. $content = '';
  291. }
  292. else
  293. {
  294. $this->errors = ['errors' => ['message' => 'requested file not found']];
  295. return false;
  296. }
  297. $this->content = $content;
  298. return true;
  299. }
  300. }