PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/plugins.controller.php

https://github.com/bk-amahi/esoTalk
PHP | 143 lines | 103 code | 18 blank | 22 comment | 30 complexity | 1e64a231d6bf5317e070f45b85baba67 MD5 | raw file
  1. <?php
  2. // Copyright 2009 Simon Zerner, Toby Zerner
  3. // This file is part of esoTalk. Please see the included license file for usage information.
  4. // Plugins controller: Controls everything for the plugins view: toggling plugins, installing, settings, etc.
  5. if (!defined("IN_ESOTALK")) exit;
  6. class plugins extends Controller {
  7. var $view = "plugins.view.php";
  8. var $plugins = array();
  9. // Get all the plugins into an array
  10. function init()
  11. {
  12. if (!$this->esoTalk->user["admin"]) redirect("");
  13. global $language, $config;
  14. $this->title = $language["Plugins"];
  15. if (isset($_FILES["uploadPlugin"])) $this->uploadPlugin();
  16. // Get the plugins and their details
  17. if ($handle = opendir("plugins")) {
  18. while (false !== ($file = readdir($handle))) {
  19. if ($file[0] != "." and is_dir("plugins/$file") and file_exists("plugins/$file/plugin.php") and (include_once "plugins/$file/plugin.php") and class_exists($file)) {
  20. $plugin = new $file;
  21. $plugin->esoTalk =& $this->esoTalk;
  22. $this->plugins[$plugin->id] = array(
  23. "loaded" => in_array($file, $config["loadedPlugins"]),
  24. "name" => $plugin->name,
  25. "version" => $plugin->version,
  26. "description" => $plugin->description,
  27. "author" => $plugin->author,
  28. "settings" => $plugin->settings()
  29. );
  30. }
  31. }
  32. closedir($handle);
  33. }
  34. ksort($this->plugins);
  35. // Toggle an plugin?
  36. if (!empty($_GET["toggle"])) {
  37. $plugins = array();
  38. // If the plugin is in the array, take it out
  39. $k = array_search($_GET["toggle"], $config["loadedPlugins"]);
  40. if ($k !== false) unset($config["loadedPlugins"][$k]);
  41. // If it's not in the array, add it in
  42. elseif ($k === false) $config["loadedPlugins"][] = $_GET["toggle"];
  43. if ($this->writeLoadedPlugins($config["loadedPlugins"])) redirect("plugins");
  44. }
  45. }
  46. // Ajax - toggle an plugin
  47. function ajax()
  48. {
  49. switch ($_POST["action"]) {
  50. // Toggle an plugin
  51. case "toggle":
  52. global $config;
  53. // If the plugin is in the array, take it out
  54. $k = array_search($_POST["id"], $config["loadedPlugins"]);
  55. if (!$_POST["enabled"] and $k !== false) unset($config["loadedPlugins"][$k]);
  56. // If it's not in the array, add it in
  57. elseif ($k === false) $config["loadedPlugins"][] = $_POST["id"];
  58. $this->writeLoadedPlugins($config["loadedPlugins"]);
  59. break;
  60. }
  61. }
  62. // Write the loaded plugins file
  63. function writeLoadedPlugins($loadedPlugins)
  64. {
  65. $loadedPlugins = array_unique($loadedPlugins);
  66. // Prepare the content
  67. $content = "<?php\n\$config[\"loadedPlugins\"] = array(";
  68. foreach ($loadedPlugins as $v) {
  69. if (!count($this->plugins) or array_key_exists($v, $this->plugins)) $content .= "\n\"$v\",";
  70. }
  71. $content .= "\n);\n?>";
  72. // Write the file.
  73. if (!writeFile("config/plugins.php", $content)) {
  74. $this->esoTalk->message("notWritable", false, "config/plugins.php");
  75. return false;
  76. }
  77. return true;
  78. }
  79. // Upload an plugin
  80. function uploadPlugin()
  81. {
  82. if ($_FILES["uploadPlugin"]["error"]) {
  83. $this->esoTalk->message("invalidPlugin");
  84. return false;
  85. }
  86. if (!move_uploaded_file($_FILES["uploadPlugin"]["tmp_name"], "plugins/{$_FILES["uploadPlugin"]["name"]}")) {
  87. $this->esoTalk->message("notWritable", false, "plugins/");
  88. return false;
  89. }
  90. if (!($files = unzip("plugins/{$_FILES["uploadPlugin"]["name"]}", "plugins/"))) $this->esoTalk->message("invalidPlugin");
  91. else {
  92. $directories = 0; $settingsFound = false;
  93. foreach ($files as $k => $file) {
  94. // Strip out annoying Mac OS X files
  95. if (substr($file["name"], 0, 9) == "__MACOSX/" or substr($file["name"], -9) == ".DS_Store") {
  96. unset($files[$k]);
  97. continue;
  98. }
  99. // If the zip has more than one base directory, don't let it pass!
  100. if ($file["directory"] and substr_count($file["name"], "/") < 2) $directories++;
  101. // Make sure there's a settings file in there
  102. if (substr($file["name"], -10) == "plugin.php") $pluginFound = true;
  103. }
  104. if ($pluginFound and $directories == 1) {
  105. $error = false;
  106. // Loop through files and copy them over
  107. foreach ($files as $k => $file) {
  108. // Make a directory
  109. if ($file["directory"] and !is_dir("plugins/{$file["name"]}")) mkdir("plugins/{$file["name"]}");
  110. // Write a file
  111. elseif (!$file["directory"]) {
  112. if (!writeFile("plugins/{$file["name"]}", $file["content"])) {
  113. $this->esoTalk->message("notWritable", false, "plugins/{$file["name"]}");
  114. $error = true;
  115. break;
  116. }
  117. }
  118. }
  119. if (!$error) $this->esoTalk->message("pluginAdded");
  120. } else $this->esoTalk->message("invalidPlugin");
  121. }
  122. unlink("plugins/{$_FILES["uploadPlugin"]["name"]}");
  123. }
  124. }
  125. ?>