PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/ext_manager/main.php

https://github.com/zshall/shimmie2
PHP | 193 lines | 173 code | 6 blank | 14 comment | 24 complexity | 54fdbb0ecf69f2dc73a2bd6cf1f57f08 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Name: Extension Manager
  4. * Author: Shish <webmaster@shishnet.org>
  5. * Link: http://code.shishnet.org/shimmie2/
  6. * License: GPLv2
  7. * Visibility: admin
  8. * Description: A thing for point & click extension management
  9. * Documentation:
  10. * Allows the admin to view a list of all extensions and enable or
  11. * disable them; also allows users to view the list of activated
  12. * extensions and read their documentation
  13. */
  14. /** @private */
  15. function __extman_extcmp(ExtensionInfo $a, ExtensionInfo $b) {
  16. return strcmp($a->name, $b->name);
  17. }
  18. /** @private */
  19. class ExtensionInfo {
  20. var $ext_name, $name, $link, $author, $email;
  21. var $description, $documentation, $version, $visibility;
  22. function ExtensionInfo($main) {
  23. $matches = array();
  24. $lines = file($main);
  25. preg_match("#(ext|contrib)/(.*)/main.php#", $main, $matches);
  26. $this->ext_name = $matches[2];
  27. $this->name = $this->ext_name;
  28. $this->enabled = $this->is_enabled($this->ext_name);
  29. for($i=0; $i<count($lines); $i++) {
  30. $line = $lines[$i];
  31. if(preg_match("/Name: (.*)/", $line, $matches)) {
  32. $this->name = $matches[1];
  33. }
  34. if(preg_match("/Visibility: (.*)/", $line, $matches)) {
  35. $this->visibility = $matches[1];
  36. }
  37. if(preg_match("/Link: (.*)/", $line, $matches)) {
  38. $this->link = $matches[1];
  39. if($this->link[0] == "/") {
  40. $this->link = make_link(substr($this->link, 1));
  41. }
  42. }
  43. if(preg_match("/Version: (.*)/", $line, $matches)) {
  44. $this->version = $matches[1];
  45. }
  46. if(preg_match("/Author: (.*) [<\(](.*@.*)[>\)]/", $line, $matches)) {
  47. $this->author = $matches[1];
  48. $this->email = $matches[2];
  49. }
  50. else if(preg_match("/Author: (.*)/", $line, $matches)) {
  51. $this->author = $matches[1];
  52. }
  53. if(preg_match("/(.*)Description: ?(.*)/", $line, $matches)) {
  54. $this->description = $matches[2];
  55. $start = $matches[1]." ";
  56. $start_len = strlen($start);
  57. while(substr($lines[$i+1], 0, $start_len) == $start) {
  58. $this->description .= " ".substr($lines[$i+1], $start_len);
  59. $i++;
  60. }
  61. }
  62. if(preg_match("/(.*)Documentation: ?(.*)/", $line, $matches)) {
  63. $this->documentation = $matches[2];
  64. $start = $matches[1]." ";
  65. $start_len = strlen($start);
  66. while(substr($lines[$i+1], 0, $start_len) == $start) {
  67. $this->documentation .= " ".substr($lines[$i+1], $start_len);
  68. $i++;
  69. }
  70. $this->documentation = str_replace('$site', make_http(get_base_href()), $this->documentation);
  71. }
  72. if(preg_match("/\*\//", $line, $matches)) {
  73. break;
  74. }
  75. }
  76. }
  77. private function is_enabled($fname) {
  78. if(file_exists("ext/$fname") && file_exists("contrib/$fname")) return true; // both
  79. if(file_exists("contrib/$fname")) return false; // only disabled (optional)
  80. return null; // only active (core)
  81. }
  82. }
  83. class ExtManager extends SimpleExtension {
  84. public function onPageRequest($event) {
  85. global $page, $user;
  86. if($event->page_matches("ext_manager")) {
  87. if($user->is_admin()) {
  88. if($event->get_arg(0) == "set") {
  89. if(is_writable("ext")) {
  90. $this->set_things($_POST);
  91. $page->set_mode("redirect");
  92. $page->set_redirect(make_link("ext_manager"));
  93. }
  94. else {
  95. $this->theme->display_error($page, "File Operation Failed",
  96. "The extension folder isn't writable by the web server :(");
  97. }
  98. }
  99. else {
  100. $this->theme->display_table($page, $this->get_extensions(true), true);
  101. }
  102. }
  103. else {
  104. $this->theme->display_table($page, $this->get_extensions(false), false);
  105. }
  106. }
  107. if($event->page_matches("ext_doc")) {
  108. $ext = $event->get_arg(0);
  109. if(file_exists("ext/$ext/main.php")) {
  110. $info = new ExtensionInfo("ext/$ext/main.php");
  111. }
  112. else {
  113. $info = new ExtensionInfo("contrib/$ext/main.php");
  114. }
  115. $this->theme->display_doc($page, $info);
  116. }
  117. }
  118. public function onUserBlockBuilding($event) {
  119. global $user;
  120. if($user->is_admin()) {
  121. $event->add_link("Extension Manager", make_link("ext_manager"));
  122. }
  123. else {
  124. $event->add_link("Help", make_link("ext_manager"));
  125. }
  126. }
  127. private function get_extensions($all) {
  128. $extensions = array();
  129. if($all) {
  130. $exts = glob("ext/*/main.php");
  131. foreach(glob("contrib/*/main.php") as $ae) {
  132. if(!in_array("ext".substr($ae, 7), $exts)) {
  133. $exts[] = $ae;
  134. }
  135. }
  136. }
  137. else {
  138. $exts = glob("ext/*/main.php");
  139. }
  140. foreach($exts as $main) {
  141. $extensions[] = new ExtensionInfo($main);
  142. }
  143. usort($extensions, "__extman_extcmp");
  144. return $extensions;
  145. }
  146. private function set_things($settings) {
  147. foreach(glob("contrib/*/main.php") as $main) {
  148. $matches = array();
  149. preg_match("#contrib/(.*)/main.php#", $main, $matches);
  150. $fname = $matches[1];
  151. if(!isset($settings["ext_$fname"])) $settings["ext_$fname"] = 0;
  152. $this->set_enabled($fname, $settings["ext_$fname"]);
  153. }
  154. }
  155. private function set_enabled($fname, $enabled) {
  156. if($enabled) {
  157. // enable if currently disabled
  158. if(!file_exists("ext/$fname")) {
  159. if(function_exists("symlink")) {
  160. // yes, even though we are in /, and thus the path to contrib is
  161. // ./contrib, the link needs to be ../ because it is literal data
  162. // which will be interpreted relative to ./ext/ by the OS
  163. symlink("../contrib/$fname", "ext/$fname");
  164. }
  165. else {
  166. full_copy("contrib/$fname", "ext/$fname");
  167. }
  168. log_info("ext_manager", "Enabling $fname");
  169. }
  170. }
  171. else {
  172. // disable if currently enabled
  173. if(file_exists("ext/$fname")) {
  174. deltree("ext/$fname");
  175. log_info("ext_manager", "Disabling $fname");
  176. }
  177. }
  178. }
  179. }
  180. ?>