/core/controllers/admin/ETAppearanceAdminController.class.php

https://github.com/iugo/esoTalk · PHP · 222 lines · 67 code · 36 blank · 119 comment · 21 complexity · e9d45306014b4e81ea7a2f8ef758ff6e MD5 · raw file

  1. <?php
  2. // Copyright 2011 Toby Zerner, Simon Zerner
  3. // This file is part of esoTalk. Please see the included license file for usage information.
  4. if (!defined("IN_ESOTALK")) exit;
  5. /**
  6. * This controller handles changing of the forum appearance, including the management of skins and skin
  7. * settings.
  8. *
  9. * @package esoTalk
  10. */
  11. class ETAppearanceAdminController extends ETAdminController {
  12. /**
  13. * Show the appearearance page, including a list of skins.
  14. *
  15. * @return void
  16. */
  17. public function action_index()
  18. {
  19. $skins = $this->getSkins();
  20. $this->title = T("Appearance");
  21. $this->data("skins", $skins);
  22. $this->data("skin", C("esoTalk.skin") ? $skins[C("esoTalk.skin")] : array());
  23. $this->render("admin/appearance");
  24. }
  25. /**
  26. * Return a list of skins and their information.
  27. *
  28. * @return array
  29. */
  30. protected function getSkins()
  31. {
  32. $skins = array();
  33. // Get the installed skins and their details by reading the skins/ directory.
  34. if ($handle = opendir(PATH_SKINS)) {
  35. while (false !== ($file = readdir($handle))) {
  36. // Make sure the skin is valid, and include its skin.php file.
  37. if ($file[0] != "." and file_exists($skinFile = PATH_SKINS."/$file/skin.php") and (include_once $skinFile)) {
  38. // Add the skin's information and status to the array.
  39. $skins[$file] = array(
  40. "info" => ET::$skinInfo[$file],
  41. "selected" => $file == C("esoTalk.skin"),
  42. "selectedMobile" => $file == C("esoTalk.mobileSkin"),
  43. "settingsView" => false
  44. );
  45. // If this skin's settings function returns a view path, then store it.
  46. if ($skins[$file]["selected"]) $skins[$file]["settingsView"] = ET::$skin->settings($this);
  47. }
  48. }
  49. closedir($handle);
  50. }
  51. ksort($skins);
  52. return $skins;
  53. }
  54. /**
  55. * Activate a skin so it is used as the default skin.
  56. *
  57. * @param string $skin The name of the skin.
  58. * @return void
  59. */
  60. public function action_activate($skin = "")
  61. {
  62. if (!$this->validateToken()) return;
  63. // Get the skins and make sure this one exists.
  64. $skins = $this->getSkins();
  65. if (!$skin or !array_key_exists($skin, $skins)) return false;
  66. // Write the new setting to the config file.
  67. ET::writeConfig(array("esoTalk.skin" => $skin));
  68. // Clear skin cache.
  69. $files = glob(PATH_CACHE.'/css/*.*');
  70. foreach ($files as $file) unlink(realpath($file));
  71. $this->redirect(URL("admin/appearance"));
  72. }
  73. /**
  74. * Activate a skin so it is used as the mobile skin.
  75. *
  76. * @param string $skin The name of the skin.
  77. * @return void
  78. */
  79. public function action_activateMobile($skin = "")
  80. {
  81. if (!$this->validateToken()) return;
  82. // Get the skins and make sure this one exists.
  83. $skins = $this->getSkins();
  84. if (!$skin or !array_key_exists($skin, $skins)) return false;
  85. // Write the new setting to the config file.
  86. ET::writeConfig(array("esoTalk.mobileSkin" => $skin));
  87. // Clear skin cache.
  88. $files = glob(PATH_CACHE.'/css/*.*');
  89. foreach ($files as $file) unlink(realpath($file));
  90. $this->redirect(URL("admin/appearance"));
  91. }
  92. /**
  93. * Uninstall a skin by removing its directory.
  94. *
  95. * @param string $skin The name of the skin.
  96. * @return void
  97. */
  98. public function action_uninstall($skin = "")
  99. {
  100. if (!$this->validateToken()) return;
  101. // Get the skins and make sure this one exists.
  102. $skins = $this->getSkins();
  103. if (!$skin or !array_key_exists($skin, $skins)) return false;
  104. unset($skins[$skin]);
  105. // Attempt to remove the directory. If we couldn't, show a "not writable" message.
  106. if (!is_writable($file = PATH_SKINS) or !is_writable($file = PATH_SKINS."/$skin") or !rrmdir($file))
  107. $this->message(sprintf(T("message.notWritable"), $file), "warning");
  108. // Otherwise, show a success message.
  109. else $this->message(T("message.skinUninstalled"), "success");
  110. // If one of the skin config options is set to this skin, change it.
  111. $config = array();
  112. if (C("esoTalk.skin") == $skin) $config["esoTalk.skin"] = reset(array_keys($skins));
  113. if (C("esoTalk.mobileSkin") == $skin) $config["esoTalk.mobileSkin"] = reset(array_keys($skins));
  114. if (count($config)) ET::writeConfig($config);
  115. $this->redirect(URL("admin/appearance"));
  116. }
  117. // Install an uploaded skin.
  118. /*
  119. function installSkin()
  120. {
  121. // If the uploaded file has any errors, don't proceed.
  122. if ($_FILES["installSkin"]["error"]) {
  123. $this->esoTalk->message("invalidSkin");
  124. return false;
  125. }
  126. // Temorarily move the uploaded skin into the skins directory so that we can read it.
  127. if (!move_uploaded_file($_FILES["installSkin"]["tmp_name"], "skins/{$_FILES["installSkin"]["name"]}")) {
  128. $this->esoTalk->message("notWritable", false, "skins/");
  129. return false;
  130. }
  131. // Unzip the skin. If we can't, show an error.
  132. if (!($files = unzip("skins/{$_FILES["installSkin"]["name"]}", "skins/"))) $this->esoTalk->message("invalidSkin");
  133. else {
  134. // Loop through the files in the zip and make sure it's a valid skin.
  135. $directories = 0; $skinFound = false;
  136. foreach ($files as $k => $file) {
  137. // Strip out annoying Mac OS X files!
  138. if (substr($file["name"], 0, 9) == "__MACOSX/" or substr($file["name"], -9) == ".DS_Store") {
  139. unset($files[$k]);
  140. continue;
  141. }
  142. // If the zip has more than one base directory, it's not a valid skin.
  143. if ($file["directory"] and substr_count($file["name"], "/") < 2) $directories++;
  144. // Make sure there's an actual skin file in there.
  145. if (substr($file["name"], -8) == "skin.php") $skinFound = true;
  146. }
  147. // OK, this skin in valid!
  148. if ($skinFound and $directories == 1) {
  149. // Loop through skin files and write them to the skins directory.
  150. $error = false;
  151. foreach ($files as $k => $file) {
  152. // Make a directory if it doesn't exist!
  153. if ($file["directory"] and !is_dir("skins/{$file["name"]}")) mkdir("skins/{$file["name"]}");
  154. // Write a file.
  155. elseif (!$file["directory"]) {
  156. if (!writeFile("skins/{$file["name"]}", $file["content"])) {
  157. $this->esoTalk->message("notWritable", false, "skins/{$file["name"]}");
  158. $error = true;
  159. break;
  160. }
  161. }
  162. }
  163. // Everything copied over correctly - success!
  164. if (!$error) $this->esoTalk->message("skinAdded");
  165. }
  166. // Hmm, something went wrong. Show an error.
  167. else $this->esoTalk->message("invalidSkin");
  168. }
  169. // Delete the temporarily uploaded skin file.
  170. unlink("skins/{$_FILES["installSkin"]["name"]}");
  171. }
  172. */
  173. }