ContentController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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;
  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. # holds the path to the requested file
  31. protected $path = false;
  32. # holds the content of the page
  33. protected $content;
  34. public function __construct(ContainerInterface $c)
  35. {
  36. $this->c = $c;
  37. $this->settings = $this->c->get('settings');
  38. $this->structureLiveName = 'structure.txt';
  39. $this->structureDraftName = 'structure-draft.txt';
  40. }
  41. protected function render($response, $route, $data)
  42. {
  43. if(isset($_SESSION['old']))
  44. {
  45. unset($_SESSION['old']);
  46. }
  47. if($this->c->request->getUri()->getScheme() == 'https')
  48. {
  49. $response = $response->withAddedHeader('Strict-Transport-Security', 'max-age=63072000');
  50. }
  51. $response = $response->withAddedHeader('X-Content-Type-Options', 'nosniff');
  52. $response = $response->withAddedHeader('X-Frame-Options', 'SAMEORIGIN');
  53. $response = $response->withAddedHeader('X-XSS-Protection', '1;mode=block');
  54. $response = $response->withAddedHeader('Referrer-Policy', 'no-referrer-when-downgrade');
  55. return $this->c->view->render($response, $route, $data);
  56. }
  57. protected function render404($response, $data = NULL)
  58. {
  59. return $this->c->view->render($response->withStatus(404), '/404.twig', $data);
  60. }
  61. protected function validateEditorInput()
  62. {
  63. $validate = new Validation();
  64. $vResult = $validate->editorInput($this->params);
  65. if(is_array($vResult))
  66. {
  67. $this->errors = ['errors' => $vResult];
  68. return false;
  69. }
  70. return true;
  71. }
  72. protected function setStructure($draft = false, $cache = true)
  73. {
  74. # set initial structure to false
  75. $structure = false;
  76. # name of structure-file for draft or live
  77. $filename = $draft ? $this->structureDraftName : $this->structureLiveName;
  78. # set variables and objects
  79. $this->write = new writeCache();
  80. # check, if cached structure is still valid
  81. if($cache && $this->write->validate('cache', 'lastCache.txt', 600))
  82. {
  83. # get the cached structure
  84. $structure = $this->write->getCache('cache', $filename);
  85. }
  86. # if no structure was found or cache is deactivated
  87. if(!$structure)
  88. {
  89. # scan the content of the folder
  90. $structure = Folder::scanFolder($this->settings['rootPath'] . $this->settings['contentFolder'], $draft);
  91. # if there is no content, render an empty page
  92. if(count($structure) == 0)
  93. {
  94. $this->errors = ['errors' => ['message' => 'content folder is empty']];
  95. return false;
  96. }
  97. # create an array of object with the whole content of the folder
  98. $structure = Folder::getFolderContentDetails($structure, $this->uri->getBaseUrl(), $this->uri->getBasePath());
  99. # cache navigation
  100. $this->write->updateCache('cache', $filename, 'lastCache.txt', $structure);
  101. }
  102. $this->structure = $structure;
  103. return true;
  104. }
  105. protected function setItem()
  106. {
  107. # if it is the homepage
  108. if($this->params['url'] == $this->uri->getBasePath() OR $this->params['url'] == '/')
  109. {
  110. $item = new \stdClass;
  111. $item->elementType = 'folder';
  112. $item->path = '';
  113. $item->urlRel = '/';
  114. }
  115. else
  116. {
  117. # search for the url in the structure
  118. $item = Folder::getItemForUrl($this->structure, $this->params['url']);
  119. }
  120. if($item)
  121. {
  122. if($item->elementType == 'file')
  123. {
  124. $pathParts = explode('.', $item->path);
  125. $fileType = array_pop($pathParts);
  126. $pathWithoutType = implode('.', $pathParts);
  127. $item->pathWithoutType = $pathWithoutType;
  128. }
  129. elseif($item->elementType == 'folder')
  130. {
  131. $item->path = $item->path . DIRECTORY_SEPARATOR . 'index';
  132. $item->pathWithoutType = $item->path;
  133. }
  134. $this->item = $item;
  135. return true;
  136. }
  137. $this->errors = ['errors' => ['message' => 'requested page-url not found']];
  138. return false;
  139. }
  140. # determine if you want to write to published file (md) or to draft (txt)
  141. protected function setItemPath($fileType)
  142. {
  143. $this->path = $this->item->pathWithoutType . '.' . $fileType;
  144. }
  145. protected function setPublishStatus()
  146. {
  147. $this->item->published = false;
  148. $this->item->drafted = false;
  149. if(file_exists($this->settings['rootPath'] . $this->settings['contentFolder'] . $this->item->pathWithoutType . '.md'))
  150. {
  151. $this->item->published = true;
  152. # add file-type in case it is a folder
  153. $this->item->fileType = "md";
  154. }
  155. if(file_exists($this->settings['rootPath'] . $this->settings['contentFolder'] . $this->item->pathWithoutType . '.txt'))
  156. {
  157. $this->item->drafted = true;
  158. # add file-type in case it is a folder
  159. $this->item->fileType = "txt";
  160. }
  161. if(!$this->item->drafted && !$this->item->published && $this->item->elementType == "folder")
  162. {
  163. # set txt as default for a folder, so that we can create an index.txt for a folder.
  164. $this->item->fileType = "txt";
  165. }
  166. }
  167. protected function deleteContentFiles($fileTypes)
  168. {
  169. $basePath = $this->settings['rootPath'] . $this->settings['contentFolder'];
  170. foreach($fileTypes as $fileType)
  171. {
  172. if(file_exists($basePath . $this->item->pathWithoutType . '.' . $fileType))
  173. {
  174. unlink($basePath . $this->item->pathWithoutType . '.' . $fileType);
  175. # if file could not be deleted
  176. # $this->errors = ['errors' => ['message' => 'Could not delete files, please check, if files are writable.']];
  177. }
  178. }
  179. return true;
  180. }
  181. protected function setContent()
  182. {
  183. # if the file exists
  184. if($this->item->published OR $this->item->drafted)
  185. {
  186. $content = $this->write->getFile($this->settings['contentFolder'], $this->path);
  187. if($this->item->fileType == 'txt')
  188. {
  189. # decode the json-draft to an array
  190. $content = json_decode($content);
  191. }
  192. }
  193. elseif($this->item->elementType == "folder")
  194. {
  195. $content = '';
  196. }
  197. else
  198. {
  199. $this->errors = ['errors' => ['message' => 'requested file not found']];
  200. return false;
  201. }
  202. $this->content = $content;
  203. return true;
  204. }
  205. }