This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/ext_manager/main.php

209 lines
5.7 KiB
PHP
Raw Normal View History

<?php
/**
* Name: Extension Manager
* Author: Shish <webmaster@shishnet.org>
2010-01-04 12:44:20 +00:00
* Link: http://code.shishnet.org/shimmie2/
* License: GPLv2
* Visibility: admin
* Description: A thing for point & click extension management
2010-01-05 10:11:53 +00:00
* Documentation:
* Allows the admin to view a list of all extensions and enable or
* disable them; also allows users to view the list of activated
* extensions and read their documentation
*/
/** @private */
function __extman_extcmp(ExtensionInfo $a, ExtensionInfo $b) {
return strcmp($a->name, $b->name);
}
/** @private */
class ExtensionInfo {
var $ext_name, $name, $link, $author, $email;
var $description, $documentation, $version, $visibility;
2014-04-06 19:47:01 +00:00
var $enabled;
2014-03-22 09:00:59 +00:00
function __construct($main) {
$matches = array();
$lines = file($main);
$number_of_lines = count($lines);
preg_match("#ext/(.*)/main.php#", $main, $matches);
$this->ext_name = $matches[1];
$this->name = $this->ext_name;
$this->enabled = $this->is_enabled($this->ext_name);
for($i=0; $i<$number_of_lines; $i++) {
$line = $lines[$i];
if(preg_match("/Name: (.*)/", $line, $matches)) {
$this->name = $matches[1];
}
2015-09-27 10:01:59 +00:00
else if(preg_match("/Visibility: (.*)/", $line, $matches)) {
$this->visibility = $matches[1];
}
2015-09-27 10:01:59 +00:00
else if(preg_match("/Link: (.*)/", $line, $matches)) {
$this->link = $matches[1];
if($this->link[0] == "/") {
$this->link = make_link(substr($this->link, 1));
}
}
2015-09-27 10:01:59 +00:00
else if(preg_match("/Version: (.*)/", $line, $matches)) {
$this->version = $matches[1];
}
2015-09-27 10:01:59 +00:00
else if(preg_match("/Author: (.*) [<\(](.*@.*)[>\)]/", $line, $matches)) {
$this->author = $matches[1];
$this->email = $matches[2];
}
else if(preg_match("/Author: (.*)/", $line, $matches)) {
$this->author = $matches[1];
}
2015-09-27 10:01:59 +00:00
else if(preg_match("/(.*)Description: ?(.*)/", $line, $matches)) {
$this->description = $matches[2];
$start = $matches[1]." ";
$start_len = strlen($start);
while(substr($lines[$i+1], 0, $start_len) == $start) {
2009-01-16 05:57:30 +00:00
$this->description .= " ".substr($lines[$i+1], $start_len);
$i++;
}
}
2015-09-27 10:01:59 +00:00
else if(preg_match("/(.*)Documentation: ?(.*)/", $line, $matches)) {
2009-01-16 05:41:59 +00:00
$this->documentation = $matches[2];
$start = $matches[1]." ";
$start_len = strlen($start);
while(substr($lines[$i+1], 0, $start_len) == $start) {
2009-01-16 05:57:30 +00:00
$this->documentation .= " ".substr($lines[$i+1], $start_len);
2009-01-16 05:41:59 +00:00
$i++;
}
$this->documentation = str_replace('$site', make_http(get_base_href()), $this->documentation);
2009-01-16 05:41:59 +00:00
}
2015-09-27 10:01:59 +00:00
else if(preg_match("/\*\//", $line, $matches)) {
break;
}
}
}
/**
* @param string $fname
* @return bool|null
*/
2012-02-02 14:14:33 +00:00
private function is_enabled(/*string*/ $fname) {
$core = explode(",", CORE_EXTS);
$extra = explode(",", EXTRA_EXTS);
if(in_array($fname, $extra)) return true; // enabled
if(in_array($fname, $core)) return null; // core
return false; // not enabled
}
}
class ExtManager extends Extension {
2010-05-15 11:17:32 +00:00
public function onPageRequest(PageRequestEvent $event) {
2009-05-11 21:09:24 +00:00
global $page, $user;
if($event->page_matches("ext_manager")) {
2012-02-07 15:15:18 +00:00
if($user->can("manage_extension_list")) {
2010-05-28 13:26:46 +00:00
if($event->get_arg(0) == "set" && $user->check_auth_token()) {
if(is_writable("data/config")) {
$this->set_things($_POST);
2012-06-10 03:21:03 +00:00
log_warning("ext_manager", "Active extensions changed", true);
$page->set_mode("redirect");
$page->set_redirect(make_link("ext_manager"));
}
else {
$this->theme->display_error(500, "File Operation Failed",
"The config file (data/config/extensions.conf.php) isn't writable by the web server :(");
}
}
else {
2010-01-05 09:40:26 +00:00
$this->theme->display_table($page, $this->get_extensions(true), true);
}
}
else {
2010-01-05 09:40:26 +00:00
$this->theme->display_table($page, $this->get_extensions(false), false);
}
}
2009-05-11 21:09:24 +00:00
if($event->page_matches("ext_doc")) {
2009-01-16 05:41:59 +00:00
$ext = $event->get_arg(0);
2010-01-04 12:41:04 +00:00
if(file_exists("ext/$ext/main.php")) {
$info = new ExtensionInfo("ext/$ext/main.php");
$this->theme->display_doc($page, $info);
2010-01-04 12:41:04 +00:00
}
else {
$this->theme->display_table($page, $this->get_extensions(false), false);
}
2009-01-16 05:41:59 +00:00
}
2009-05-11 21:09:24 +00:00
}
2009-01-16 05:41:59 +00:00
2012-06-17 23:27:58 +00:00
public function onCommand(CommandEvent $event) {
if($event->cmd == "help") {
2015-08-23 15:09:52 +00:00
print "\tdisable-all-ext\n";
print "\t\tdisable all extensions\n\n";
2012-06-17 23:27:58 +00:00
}
if($event->cmd == "disable-all-ext") {
$this->write_config(array());
}
}
2010-05-15 11:17:32 +00:00
public function onUserBlockBuilding(UserBlockBuildingEvent $event) {
2009-05-11 21:09:24 +00:00
global $user;
2012-02-07 15:15:18 +00:00
if($user->can("manage_extension_list")) {
2009-05-11 21:09:24 +00:00
$event->add_link("Extension Manager", make_link("ext_manager"));
}
2010-01-05 09:40:26 +00:00
else {
$event->add_link("Help", make_link("ext_doc"));
2010-01-05 09:40:26 +00:00
}
}
/**
* @param bool $all
* @return array
*/
2012-02-02 14:14:33 +00:00
private function get_extensions(/*bool*/ $all) {
$extensions = array();
if($all) {
$exts = zglob("ext/*/main.php");
}
else {
$exts = zglob("ext/{".ENABLED_EXTS."}/main.php");
}
2010-01-05 09:40:26 +00:00
foreach($exts as $main) {
$extensions[] = new ExtensionInfo($main);
}
usort($extensions, "__extman_extcmp");
return $extensions;
}
private function set_things($settings) {
$core = explode(",", CORE_EXTS);
2014-04-06 19:47:01 +00:00
$extras = array();
foreach(glob("ext/*/main.php") as $main) {
$matches = array();
preg_match("#ext/(.*)/main.php#", $main, $matches);
$fname = $matches[1];
if(!in_array($fname, $core) && isset($settings["ext_$fname"])) {
$extras[] = $fname;
}
}
2012-06-17 23:27:58 +00:00
$this->write_config($extras);
}
private function write_config($extras) {
file_put_contents(
"data/config/extensions.conf.php",
'<'.'?php'."\n".
'define("EXTRA_EXTS", "'.implode(",", $extras).'");'."\n".
'?'.">"
);
// when the list of active extensions changes, we can be
// pretty sure that the list of who reacts to what will
// change too
if(file_exists("data/cache/event_listeners.php")) {
unlink("data/cache/event_listeners.php");
}
}
}