3
1

TemplateController.php 6.4 KB

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