ContentController.php 8.7 KB

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