ContentController.php 8.2 KB

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