ContentController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. # name of structure-file for draft or live
  75. $filename = $draft ? $this->structureDraftName : $this->structureLiveName;
  76. # set variables and objects
  77. $this->write = new writeCache();
  78. # check, if cached structure is still valid
  79. if($cache && $this->write->validate('cache', 'lastCache.txt', 600))
  80. {
  81. # get the cached structure
  82. $structure = $this->write->getCache('cache', $filename);
  83. }
  84. else
  85. {
  86. # scan the content of the folder
  87. $structure = Folder::scanFolder($this->settings['rootPath'] . $this->settings['contentFolder'], $draft);
  88. # if there is no content, render an empty page
  89. if(count($structure) == 0)
  90. {
  91. $this->errors = ['errors' => ['message' => 'content folder is empty']];
  92. return false;
  93. }
  94. # create an array of object with the whole content of the folder
  95. $structure = Folder::getFolderContentDetails($structure, $this->uri->getBaseUrl(), $this->uri->getBasePath());
  96. # cache navigation
  97. $this->write->updateCache('cache', $filename, 'lastCache.txt', $structure);
  98. }
  99. $this->structure = $structure;
  100. return true;
  101. }
  102. protected function setItem()
  103. {
  104. # if it is the homepage
  105. if($this->params['url'] == $this->uri->getBasePath() OR $this->params['url'] == '/')
  106. {
  107. $item = new \stdClass;
  108. $item->elementType = 'folder';
  109. $item->path = '';
  110. $item->urlRel = '/';
  111. }
  112. else
  113. {
  114. # search for the url in the structure
  115. $item = Folder::getItemForUrl($this->structure, $this->params['url']);
  116. }
  117. if($item)
  118. {
  119. if($item->elementType == 'file')
  120. {
  121. $pathParts = explode('.', $item->path);
  122. $fileType = array_pop($pathParts);
  123. $pathWithoutType = implode('.', $pathParts);
  124. $item->pathWithoutType = $pathWithoutType;
  125. }
  126. elseif($item->elementType == 'folder')
  127. {
  128. $item->path = $item->path . DIRECTORY_SEPARATOR . 'index';
  129. $item->pathWithoutType = $item->path;
  130. }
  131. $this->item = $item;
  132. return true;
  133. }
  134. $this->errors = ['errors' => ['message' => 'requested page-url not found']];
  135. return false;
  136. }
  137. # determine if you want to write to published file (md) or to draft (txt)
  138. protected function setItemPath($fileType)
  139. {
  140. $this->path = $this->item->pathWithoutType . '.' . $fileType;
  141. }
  142. protected function setPublishStatus()
  143. {
  144. $this->item->published = false;
  145. $this->item->drafted = false;
  146. if(file_exists($this->settings['rootPath'] . $this->settings['contentFolder'] . $this->item->pathWithoutType . '.md'))
  147. {
  148. $this->item->published = true;
  149. # add file-type in case it is a folder
  150. $this->item->fileType = "md";
  151. }
  152. elseif(file_exists($this->settings['rootPath'] . $this->settings['contentFolder'] . $this->item->pathWithoutType . '.txt'))
  153. {
  154. $this->item->drafted = true;
  155. # add file-type in case it is a folder
  156. $this->item->fileType = "txt";
  157. }
  158. elseif($this->item->elementType == "folder")
  159. {
  160. # set txt as default for a folder, so that we can create an index.txt for a folder.
  161. $this->item->fileType = "txt";
  162. }
  163. }
  164. protected function deleteContentFiles($fileTypes)
  165. {
  166. $basePath = $this->settings['rootPath'] . $this->settings['contentFolder'];
  167. foreach($fileTypes as $fileType)
  168. {
  169. if(file_exists($basePath . $this->item->pathWithoutType . '.' . $fileType))
  170. {
  171. unlink($basePath . $this->item->pathWithoutType . '.' . $fileType);
  172. # if file could not be deleted
  173. # $this->errors = ['errors' => ['message' => 'Could not delete files, please check, if files are writable.']];
  174. }
  175. }
  176. return true;
  177. }
  178. protected function setContent()
  179. {
  180. # if the file exists
  181. if($this->item->published OR $this->item->drafted)
  182. {
  183. $content = $this->write->getFile($this->settings['contentFolder'], $this->path);
  184. if($this->item->fileType == 'txt')
  185. {
  186. # decode the json-draft to an array
  187. $content = json_decode($content);
  188. }
  189. }
  190. elseif($this->item->elementType == "folder")
  191. {
  192. $content = '';
  193. }
  194. else
  195. {
  196. $this->errors = ['errors' => ['message' => 'requested file not found']];
  197. return false;
  198. }
  199. $this->content = $content;
  200. return true;
  201. }
  202. }