PSR-2. I'm not a huge fan, but ugly consistency beats no consistency...

This commit is contained in:
Shish
2019-05-28 17:59:38 +01:00
parent 5ec3e89884
commit 34b05cca7c
295 changed files with 27094 additions and 24632 deletions

View File

@ -12,193 +12,204 @@
* extensions and read their documentation
*/
function __extman_extcmp(ExtensionInfo $a, ExtensionInfo $b): int {
return strcmp($a->name, $b->name);
function __extman_extcmp(ExtensionInfo $a, ExtensionInfo $b): int
{
return strcmp($a->name, $b->name);
}
class ExtensionInfo {
public $ext_name, $name, $link, $author, $email;
public $description, $documentation, $version, $visibility;
public $enabled;
class ExtensionInfo
{
public $ext_name;
public $name;
public $link;
public $author;
public $email;
public $description;
public $documentation;
public $version;
public $visibility;
public $enabled;
public 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);
public function __construct($main)
{
$matches = [];
$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];
}
else if(preg_match("/Visibility: (.*)/", $line, $matches)) {
$this->visibility = $matches[1];
}
else if(preg_match("/Link: (.*)/", $line, $matches)) {
$this->link = $matches[1];
if($this->link[0] == "/") {
$this->link = make_link(substr($this->link, 1));
}
}
else if(preg_match("/Version: (.*)/", $line, $matches)) {
$this->version = $matches[1];
}
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];
}
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) {
$this->description .= " ".substr($lines[$i+1], $start_len);
$i++;
}
}
else if(preg_match("/(.*)Documentation: ?(.*)/", $line, $matches)) {
$this->documentation = $matches[2];
$start = $matches[1]." ";
$start_len = strlen($start);
while(substr($lines[$i+1], 0, $start_len) == $start) {
$this->documentation .= " ".substr($lines[$i+1], $start_len);
$i++;
}
$this->documentation = str_replace('$site', make_http(get_base_href()), $this->documentation);
}
else if(preg_match("/\*\//", $line, $matches)) {
break;
}
}
}
for ($i=0; $i<$number_of_lines; $i++) {
$line = $lines[$i];
if (preg_match("/Name: (.*)/", $line, $matches)) {
$this->name = $matches[1];
} elseif (preg_match("/Visibility: (.*)/", $line, $matches)) {
$this->visibility = $matches[1];
} elseif (preg_match("/Link: (.*)/", $line, $matches)) {
$this->link = $matches[1];
if ($this->link[0] == "/") {
$this->link = make_link(substr($this->link, 1));
}
} elseif (preg_match("/Version: (.*)/", $line, $matches)) {
$this->version = $matches[1];
} elseif (preg_match("/Author: (.*) [<\(](.*@.*)[>\)]/", $line, $matches)) {
$this->author = $matches[1];
$this->email = $matches[2];
} elseif (preg_match("/Author: (.*)/", $line, $matches)) {
$this->author = $matches[1];
} elseif (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) {
$this->description .= " ".substr($lines[$i+1], $start_len);
$i++;
}
} elseif (preg_match("/(.*)Documentation: ?(.*)/", $line, $matches)) {
$this->documentation = $matches[2];
$start = $matches[1]." ";
$start_len = strlen($start);
while (substr($lines[$i+1], 0, $start_len) == $start) {
$this->documentation .= " ".substr($lines[$i+1], $start_len);
$i++;
}
$this->documentation = str_replace('$site', make_http(get_base_href()), $this->documentation);
} elseif (preg_match("/\*\//", $line, $matches)) {
break;
}
}
}
private function is_enabled(string $fname): ?bool {
$core = explode(",", CORE_EXTS);
$extra = explode(",", EXTRA_EXTS);
private function is_enabled(string $fname): ?bool
{
$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
}
if (in_array($fname, $extra)) {
return true;
} // enabled
if (in_array($fname, $core)) {
return null;
} // core
return false; // not enabled
}
}
class ExtManager extends Extension {
public function onPageRequest(PageRequestEvent $event) {
global $page, $user;
if($event->page_matches("ext_manager")) {
if($user->can("manage_extension_list")) {
if($event->get_arg(0) == "set" && $user->check_auth_token()) {
if(is_writable("data/config")) {
$this->set_things($_POST);
log_warning("ext_manager", "Active extensions changed", "Active extensions changed");
$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 {
$this->theme->display_table($page, $this->get_extensions(true), true);
}
}
else {
$this->theme->display_table($page, $this->get_extensions(false), false);
}
}
class ExtManager extends Extension
{
public function onPageRequest(PageRequestEvent $event)
{
global $page, $user;
if ($event->page_matches("ext_manager")) {
if ($user->can("manage_extension_list")) {
if ($event->get_arg(0) == "set" && $user->check_auth_token()) {
if (is_writable("data/config")) {
$this->set_things($_POST);
log_warning("ext_manager", "Active extensions changed", "Active extensions changed");
$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 {
$this->theme->display_table($page, $this->get_extensions(true), true);
}
} else {
$this->theme->display_table($page, $this->get_extensions(false), false);
}
}
if($event->page_matches("ext_doc")) {
$ext = $event->get_arg(0);
if(file_exists("ext/$ext/main.php")) {
$info = new ExtensionInfo("ext/$ext/main.php");
$this->theme->display_doc($page, $info);
}
else {
$this->theme->display_table($page, $this->get_extensions(false), false);
}
}
}
if ($event->page_matches("ext_doc")) {
$ext = $event->get_arg(0);
if (file_exists("ext/$ext/main.php")) {
$info = new ExtensionInfo("ext/$ext/main.php");
$this->theme->display_doc($page, $info);
} else {
$this->theme->display_table($page, $this->get_extensions(false), false);
}
}
}
public function onCommand(CommandEvent $event) {
if($event->cmd == "help") {
print "\tdisable-all-ext\n";
print "\t\tdisable all extensions\n\n";
}
if($event->cmd == "disable-all-ext") {
$this->write_config(array());
}
}
public function onCommand(CommandEvent $event)
{
if ($event->cmd == "help") {
print "\tdisable-all-ext\n";
print "\t\tdisable all extensions\n\n";
}
if ($event->cmd == "disable-all-ext") {
$this->write_config([]);
}
}
public function onUserBlockBuilding(UserBlockBuildingEvent $event) {
global $user;
if($user->can("manage_extension_list")) {
$event->add_link("Extension Manager", make_link("ext_manager"));
}
else {
$event->add_link("Help", make_link("ext_doc"));
}
}
public function onUserBlockBuilding(UserBlockBuildingEvent $event)
{
global $user;
if ($user->can("manage_extension_list")) {
$event->add_link("Extension Manager", make_link("ext_manager"));
} else {
$event->add_link("Help", make_link("ext_doc"));
}
}
/**
* #return ExtensionInfo[]
*/
private function get_extensions(bool $all): array {
$extensions = array();
if($all) {
$exts = zglob("ext/*/main.php");
}
else {
$exts = zglob("ext/{".ENABLED_EXTS."}/main.php");
}
foreach($exts as $main) {
$extensions[] = new ExtensionInfo($main);
}
usort($extensions, "__extman_extcmp");
return $extensions;
}
/**
* #return ExtensionInfo[]
*/
private function get_extensions(bool $all): array
{
$extensions = [];
if ($all) {
$exts = zglob("ext/*/main.php");
} else {
$exts = zglob("ext/{".ENABLED_EXTS."}/main.php");
}
foreach ($exts as $main) {
$extensions[] = new ExtensionInfo($main);
}
usort($extensions, "__extman_extcmp");
return $extensions;
}
private function set_things($settings) {
$core = explode(",", CORE_EXTS);
$extras = array();
private function set_things($settings)
{
$core = explode(",", CORE_EXTS);
$extras = [];
foreach(glob("ext/*/main.php") as $main) {
$matches = array();
preg_match("#ext/(.*)/main.php#", $main, $matches);
$fname = $matches[1];
foreach (glob("ext/*/main.php") as $main) {
$matches = [];
preg_match("#ext/(.*)/main.php#", $main, $matches);
$fname = $matches[1];
if(!in_array($fname, $core) && isset($settings["ext_$fname"])) {
$extras[] = $fname;
}
}
if (!in_array($fname, $core) && isset($settings["ext_$fname"])) {
$extras[] = $fname;
}
}
$this->write_config($extras);
}
$this->write_config($extras);
}
/**
* #param string[] $extras
*/
private function write_config(array $extras) {
file_put_contents(
"data/config/extensions.conf.php",
'<'.'?php'."\n".
'define("EXTRA_EXTS", "'.implode(",", $extras).'");'."\n".
'?'.">"
);
private function write_config(array $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");
}
}
// 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");
}
}
}

View File

@ -1,25 +1,27 @@
<?php
class ExtManagerTest extends ShimmiePHPUnitTestCase {
public function testAuth() {
$this->get_page('ext_manager');
$this->assert_title("Extensions");
class ExtManagerTest extends ShimmiePHPUnitTestCase
{
public function testAuth()
{
$this->get_page('ext_manager');
$this->assert_title("Extensions");
$this->get_page('ext_doc');
$this->assert_title("Extensions");
$this->get_page('ext_doc');
$this->assert_title("Extensions");
$this->get_page('ext_doc/ext_manager');
$this->assert_title("Documentation for Extension Manager");
$this->assert_text("view a list of all extensions");
$this->get_page('ext_doc/ext_manager');
$this->assert_title("Documentation for Extension Manager");
$this->assert_text("view a list of all extensions");
# test author without email
$this->get_page('ext_doc/user');
# test author without email
$this->get_page('ext_doc/user');
$this->log_in_as_admin();
$this->get_page('ext_manager');
$this->assert_title("Extensions");
//$this->assert_text("SimpleTest integration"); // FIXME: something which still exists
$this->log_out();
$this->log_in_as_admin();
$this->get_page('ext_manager');
$this->assert_title("Extensions");
//$this->assert_text("SimpleTest integration"); // FIXME: something which still exists
$this->log_out();
# FIXME: test that some extensions can be added and removed? :S
}
# FIXME: test that some extensions can be added and removed? :S
}
}

View File

@ -1,12 +1,14 @@
<?php
class ExtManagerTheme extends Themelet {
/**
* #param ExtensionInfo[] $extensions
*/
public function display_table(Page $page, array $extensions, bool $editable) {
$h_en = $editable ? "<th>Enabled</th>" : "";
$html = "
class ExtManagerTheme extends Themelet
{
/**
* #param ExtensionInfo[] $extensions
*/
public function display_table(Page $page, array $extensions, bool $editable)
{
$h_en = $editable ? "<th>Enabled</th>" : "";
$html = "
".make_form(make_link("ext_manager/set"))."
<table id='extensions' class='zebra sortable'>
<thead>
@ -19,110 +21,112 @@ class ExtManagerTheme extends Themelet {
</thead>
<tbody>
";
foreach($extensions as $extension) {
if(!$editable && $extension->visibility == "admin") continue;
foreach ($extensions as $extension) {
if (!$editable && $extension->visibility == "admin") {
continue;
}
$h_name = html_escape(empty($extension->name) ? $extension->ext_name : $extension->name);
$h_description = html_escape($extension->description);
$h_link = make_link("ext_doc/".url_escape($extension->ext_name));
$h_enabled = ($extension->enabled === TRUE ? " checked='checked'" : ($extension->enabled === FALSE ? "" : " disabled checked='checked'"));
$h_enabled_box = $editable ? "<td><input type='checkbox' name='ext_".html_escape($extension->ext_name)."'$h_enabled></td>" : "";
$h_docs = ($extension->documentation ? "<a href='$h_link'>■</a>" : ""); //TODO: A proper "docs" symbol would be preferred here.
$h_name = html_escape(empty($extension->name) ? $extension->ext_name : $extension->name);
$h_description = html_escape($extension->description);
$h_link = make_link("ext_doc/".url_escape($extension->ext_name));
$h_enabled = ($extension->enabled === true ? " checked='checked'" : ($extension->enabled === false ? "" : " disabled checked='checked'"));
$h_enabled_box = $editable ? "<td><input type='checkbox' name='ext_".html_escape($extension->ext_name)."'$h_enabled></td>" : "";
$h_docs = ($extension->documentation ? "<a href='$h_link'>■</a>" : ""); //TODO: A proper "docs" symbol would be preferred here.
$html .= "
$html .= "
<tr data-ext='{$extension->ext_name}'>
{$h_enabled_box}
<td>{$h_name}</td>
<td>{$h_docs}</td>
<td style='text-align: left;'>{$h_description}</td>
</tr>";
}
$h_set = $editable ? "<tfoot><tr><td colspan='5'><input type='submit' value='Set Extensions'></td></tr></tfoot>" : "";
$html .= "
}
$h_set = $editable ? "<tfoot><tr><td colspan='5'><input type='submit' value='Set Extensions'></td></tr></tfoot>" : "";
$html .= "
</tbody>
$h_set
</table>
</form>
";
$page->set_title("Extensions");
$page->set_heading("Extensions");
$page->add_block(new NavBlock());
$page->add_block(new Block("Extension Manager", $html));
}
$page->set_title("Extensions");
$page->set_heading("Extensions");
$page->add_block(new NavBlock());
$page->add_block(new Block("Extension Manager", $html));
}
/*
public function display_blocks(Page $page, $extensions) {
global $user;
$col_1 = "";
$col_2 = "";
foreach($extensions as $extension) {
$ext_name = $extension->ext_name;
$h_name = empty($extension->name) ? $ext_name : html_escape($extension->name);
$h_email = html_escape($extension->email);
$h_link = isset($extension->link) ?
"<a href=\"".html_escape($extension->link)."\">Original Site</a>" : "";
$h_doc = isset($extension->documentation) ?
"<a href=\"".make_link("ext_doc/".html_escape($extension->ext_name))."\">Documentation</a>" : "";
$h_author = html_escape($extension->author);
$h_description = html_escape($extension->description);
$h_enabled = $extension->enabled ? " checked='checked'" : "";
$h_author_link = empty($h_email) ?
"$h_author" :
"<a href='mailto:$h_email'>$h_author</a>";
/*
public function display_blocks(Page $page, $extensions) {
global $user;
$col_1 = "";
$col_2 = "";
foreach($extensions as $extension) {
$ext_name = $extension->ext_name;
$h_name = empty($extension->name) ? $ext_name : html_escape($extension->name);
$h_email = html_escape($extension->email);
$h_link = isset($extension->link) ?
"<a href=\"".html_escape($extension->link)."\">Original Site</a>" : "";
$h_doc = isset($extension->documentation) ?
"<a href=\"".make_link("ext_doc/".html_escape($extension->ext_name))."\">Documentation</a>" : "";
$h_author = html_escape($extension->author);
$h_description = html_escape($extension->description);
$h_enabled = $extension->enabled ? " checked='checked'" : "";
$h_author_link = empty($h_email) ?
"$h_author" :
"<a href='mailto:$h_email'>$h_author</a>";
$html = "
<p><table border='1'>
<tr>
<th colspan='2'>$h_name</th>
</tr>
<tr>
<td>By $h_author_link</td>
<td width='25%'>Enabled:&nbsp;<input type='checkbox' name='ext_$ext_name'$h_enabled></td>
</tr>
<tr>
<td style='text-align: left' colspan='2'>$h_description<p>$h_link $h_doc</td>
</tr>
</table>
";
if($n++ % 2 == 0) {
$col_1 .= $html;
}
else {
$col_2 .= $html;
}
}
$html = "
".make_form(make_link("ext_manager/set"))."
".$user->get_auth_html()."
<table border='0'>
<tr><td width='50%'>$col_1</td><td>$col_2</td></tr>
<tr><td colspan='2'><input type='submit' value='Set Extensions'></td></tr>
</table>
</form>
";
$html = "
<p><table border='1'>
<tr>
<th colspan='2'>$h_name</th>
</tr>
<tr>
<td>By $h_author_link</td>
<td width='25%'>Enabled:&nbsp;<input type='checkbox' name='ext_$ext_name'$h_enabled></td>
</tr>
<tr>
<td style='text-align: left' colspan='2'>$h_description<p>$h_link $h_doc</td>
</tr>
</table>
";
if($n++ % 2 == 0) {
$col_1 .= $html;
}
else {
$col_2 .= $html;
}
}
$html = "
".make_form(make_link("ext_manager/set"))."
".$user->get_auth_html()."
<table border='0'>
<tr><td width='50%'>$col_1</td><td>$col_2</td></tr>
<tr><td colspan='2'><input type='submit' value='Set Extensions'></td></tr>
</table>
</form>
";
$page->set_title("Extensions");
$page->set_heading("Extensions");
$page->add_block(new NavBlock());
$page->add_block(new Block("Extension Manager", $html));
}
*/
$page->set_title("Extensions");
$page->set_heading("Extensions");
$page->add_block(new NavBlock());
$page->add_block(new Block("Extension Manager", $html));
}
*/
public function display_doc(Page $page, ExtensionInfo $info) {
$author = "";
if($info->author) {
if($info->email) {
$author = "<br><b>Author:</b> <a href=\"mailto:".html_escape($info->email)."\">".html_escape($info->author)."</a>";
}
else {
$author = "<br><b>Author:</b> ".html_escape($info->author);
}
}
$version = ($info->version) ? "<br><b>Version:</b> ".html_escape($info->version) : "";
$link = ($info->link) ? "<br><b>Home Page:</b> <a href=\"".html_escape($info->link)."\">Link</a>" : "";
$doc = $info->documentation;
$html = "
public function display_doc(Page $page, ExtensionInfo $info)
{
$author = "";
if ($info->author) {
if ($info->email) {
$author = "<br><b>Author:</b> <a href=\"mailto:".html_escape($info->email)."\">".html_escape($info->author)."</a>";
} else {
$author = "<br><b>Author:</b> ".html_escape($info->author);
}
}
$version = ($info->version) ? "<br><b>Version:</b> ".html_escape($info->version) : "";
$link = ($info->link) ? "<br><b>Home Page:</b> <a href=\"".html_escape($info->link)."\">Link</a>" : "";
$doc = $info->documentation;
$html = "
<div style='margin: auto; text-align: left; width: 512px;'>
$author
$version
@ -132,10 +136,9 @@ class ExtManagerTheme extends Themelet {
<p><a href='".make_link("ext_manager")."'>Back to the list</a>
</div>";
$page->set_title("Documentation for ".html_escape($info->name));
$page->set_heading(html_escape($info->name));
$page->add_block(new NavBlock());
$page->add_block(new Block("Documentation", $html));
}
$page->set_title("Documentation for ".html_escape($info->name));
$page->set_heading(html_escape($info->name));
$page->add_block(new NavBlock());
$page->add_block(new Block("Documentation", $html));
}
}