TemplateController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php namespace Kanboard\Plugin\OMITemplateModder\Controller;
  2. use Kanboard\Controller\BaseController;
  3. /**
  4. * TemplateController Controller
  5. *
  6. * @package Kanboard\Plugin\OMITemplateModder
  7. * @author Dwayne @ OMI NZ
  8. */
  9. class TemplateController extends BaseController {
  10. /**
  11. * Show all the available templates.
  12. */
  13. public function show() {
  14. $templates = $this->getEditableTemplates();
  15. $missingTemplates = array_diff($this->getCoreTemplates(), $this->getPluginTemplates());
  16. $extraTemplates = array_diff($this->getPluginTemplates(), $this->getCoreTemplates());
  17. $modifiedStatus = $this->getModifiedStatus();
  18. $this->response->html($this->helper->layout->config("OMITemplateModder:config/index", [
  19. "title" => t("OMI - Notification Template Modder"),
  20. "templates" => $templates,
  21. "missing_templates" => $missingTemplates,
  22. "extra_templates" => $extraTemplates,
  23. "modified_status" => $modifiedStatus,
  24. ]));
  25. }
  26. /**
  27. * Get a list of editable templates from the plugin's directory.
  28. *
  29. * @return array
  30. */
  31. private function getEditableTemplates() {
  32. $templates = [];
  33. $templateDir = \ETM_PLUGIN_ROOT_DIR . "/Template/notification";
  34. foreach (glob($templateDir . "/*.php") as $filename) {
  35. $templateName = basename($filename, ".php");
  36. $templates[] = "notification/" . $templateName;
  37. }
  38. return $templates;
  39. }
  40. /**
  41. * Get a list of core notification templates.
  42. *
  43. * @return array
  44. */
  45. private function getCoreTemplates() {
  46. $templates = [];
  47. $templateDir = \KB_APP_DIR . "/Template/notification";
  48. foreach (glob($templateDir . "/*.php") as $filename) {
  49. $templates[] = basename($filename, ".php");
  50. }
  51. return $templates;
  52. }
  53. /**
  54. * Get a list of plugin notification templates.
  55. *
  56. * @return array
  57. */
  58. private function getPluginTemplates() {
  59. $templates = [];
  60. $templateDir = \ETM_PLUGIN_ROOT_DIR . "/Template/notification";
  61. foreach (glob($templateDir . "/*.php") as $filename) {
  62. $templates[] = basename($filename, ".php");
  63. }
  64. return $templates;
  65. }
  66. /**
  67. * Compares core and plugin templates by file content to check for modifications.
  68. *
  69. * @return array
  70. */
  71. private function getModifiedStatus() {
  72. $modified = [];
  73. $pluginTemplateDir = \ETM_PLUGIN_ROOT_DIR . "/Template/notification";
  74. $coreTemplateDir = \KB_APP_DIR . "/Template/notification";
  75. foreach (glob($pluginTemplateDir . "/*.php") as $filename) {
  76. $templateName = basename($filename);
  77. $pluginFile = $pluginTemplateDir . "/" . $templateName;
  78. $coreFile = $coreTemplateDir . "/" . $templateName;
  79. // Only compare if the core file exists
  80. if (file_exists($coreFile)) {
  81. $pluginContent = file_get_contents($pluginFile);
  82. $coreContent = file_get_contents($coreFile);
  83. // Compare the file contents
  84. if ($pluginContent !== $coreContent) {
  85. $modified[$templateName] = true;
  86. }
  87. }
  88. }
  89. return $modified;
  90. }
  91. /**
  92. * Create a missing template from the core version.
  93. */
  94. public function create() {
  95. $templateName = $this->request->getStringParam("template_name");
  96. $coreTemplateFile = \KB_APP_DIR . "/Template/notification/" . $templateName . ".php";
  97. $pluginTemplateFile = \ETM_PLUGIN_ROOT_DIR . "/Template/notification/" . $templateName . ".php";
  98. if (file_exists($coreTemplateFile)) {
  99. if (!file_exists(dirname($pluginTemplateFile))) {
  100. mkdir(dirname($pluginTemplateFile), 0755, true);
  101. }
  102. if (copy($coreTemplateFile, $pluginTemplateFile)) {
  103. $this->flash->success(t("Template created successfully!"));
  104. } else {
  105. $this->flash->failure(t("Unable to create template. Check folder permissions."));
  106. }
  107. } else {
  108. $this->flash->failure(t("Core template not found."));
  109. }
  110. return $this->response->redirect($this->helper->url->to("TemplateController", "show", ["plugin" => "OMITemplateModder"]));
  111. }
  112. /**
  113. * Show the form to edit a template
  114. */
  115. public function edit() {
  116. $templateIndex = $this->request->getIntegerParam("template_index");
  117. $templates = $this->getEditableTemplates();
  118. if (!isset($templates[$templateIndex])) {
  119. $this->flash->failure(t("Template not found."));
  120. return $this->response->redirect($this->helper->url->to("TemplateController", "show", ["plugin" => "OMITemplateModder"]));
  121. }
  122. $templateName = $templates[$templateIndex];
  123. $templatePath = $this->getTemplatePath($templateName);
  124. if (!file_exists($templatePath)) {
  125. $this->flash->failure(t("Template file not found."));
  126. return $this->response->redirect($this->helper->url->to("TemplateController", "show", ["plugin" => "OMITemplateModder"]));
  127. }
  128. $templateContent = file_get_contents($templatePath);
  129. // Use the layout helper to render the page with the full Kanboard layout.
  130. $this->response->html($this->helper->layout->config("OMITemplateModder:config/edit", [
  131. "title" => t("OMI - Notification Template Modder"),
  132. "subtitle" => t("Edit Template: %s", $templateName),
  133. "template_name" => $templateName,
  134. "template_content" => htmlspecialchars($templateContent),
  135. "template_index" => $templateIndex // Pass the index for the form action
  136. ]));
  137. }
  138. /**
  139. * Save the edited template
  140. */
  141. public function save() {
  142. $templateIndex = $this->request->getIntegerParam("template_index");
  143. $templates = $this->getEditableTemplates();
  144. if (!isset($templates[$templateIndex])) {
  145. $this->flash->failure(t("Template not found. Check your Kanboard version is compatible with this plugin."));
  146. return $this->response->redirect($this->helper->url->to("TemplateController", "show", ["plugin" => "OMITemplateModder"]));
  147. }
  148. $templateName = $templates[$templateIndex];
  149. $templateContent = $this->request->getValue("content");
  150. $templatePath = $this->getTemplatePath($templateName);
  151. //check if the realpath of the file to be saved begins with the realpath of the allowed directory.
  152. $allowedDir = realpath(\ETM_PLUGIN_ROOT_DIR . "/Template/notification");
  153. $realTemplatePath = realpath($templatePath);
  154. if ($realTemplatePath === false || strpos($realTemplatePath, $allowedDir) !== 0) {
  155. $this->flash->failure(t("Invalid template path."));
  156. return $this->response->redirect($this->helper->url->to("TemplateController", "show", ["plugin" => "OMITemplateModder"]));
  157. }
  158. // Try to save the file
  159. if (file_put_contents($templatePath, $templateContent) !== false) {
  160. $this->flash->success(t("Template saved successfully."));
  161. } else {
  162. $this->flash->failure(t("Unable to save template. Please check file permissions and directory '_DIR_' set up."));
  163. }
  164. return $this->response->redirect($this->helper->url->to("TemplateController", "edit", ["plugin" => "OMITemplateModder", "template_index" => $templateIndex]));
  165. }
  166. /**
  167. * Get the full path to a template file
  168. *
  169. * @param string $templateName
  170. * @return string
  171. */
  172. private function getTemplatePath($templateName) {
  173. return \ETM_PLUGIN_ROOT_DIR . "/Template/" . $templateName . ".php";
  174. }
  175. }
  176. #-
  177. #plugins/OMITemplateModder/Controller/TemplateController.php