Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 286 lines (259 sloc) 8.28 KB
#!/usr/bin/php
<?php
/* Copyright (C) by Hugo Leisink <hugo@leisink.net>
* This file is part of the Banshee PHP framework
* https://www.banshee-php.org/
*
* Licensed under The MIT License
*/
chdir(__DIR__);
require("libraries/core/banshee.php");
require("libraries/helpers/console.php");
print "Remove Banshee Module\n\n";
$db = new Banshee\Database\MySQLi_connection(DB_HOSTNAME, DB_DATABASE, DB_USERNAME, DB_PASSWORD);
$banshee_modules = array(
"agenda" => array(
"pages" => array("agenda", "cms/agenda"),
"tables" => array("agenda")),
"analytics" => array(
"pages" => array("cms/analytics"),
"libraries" => array("analytics"),
"tables" => array("log_clients", "log_page_views", "log_referers", "log_search_queries", "log_visits")),
"apitest" => array(
"pages" => array("cms/apitest")),
"captcha" => array(
"pages" => array("captcha"),
"libraries" => array("captcha"),
"files" => array("extra/captcha_font.ttf")),
"contact" => array(
"pages" => array("contact"),
"settings" => array("contact\_%")),
"dictionary" => array(
"pages" => array("dictionary", "cms/dictionary"),
"tables" => array("dictionary")),
"faq" => array(
"pages" => array("faq", "cms/faq"),
"tables" => array("faqs", "faq_sections")),
"flag" => array(
"pages" => array("cms/flag"),
"libraries" => array("flags"),
"tables" => array("flags")),
"forum" => array(
"pages" => array("forum", "cms/forum", "cms/forum/section"),
"tables" => array("forum_last_view", "forum_messages", "forum_topics", "forums"),
"settings" => array("forum\_%"),
"directories" => array("cms/forum")),
"guestbook" => array(
"pages" => array("guestbook", "cms/guestbook"),
"tables" => array("guestbook"),
"settings" => array("guestbook\_%")),
"links" => array(
"pages" => array("links", "cms/link", "cms/link/category"),
"tables" => array("links", "link_categories"),
"directories" => array("cms/link")),
"mailbox" => array(
"pages" => array("mailbox"),
"tables" => array("mailbox")),
"news" => array(
"pages" => array("news", "news.xml", "cms/news"),
"tables" => array("news"),
"settings" => array("news\_%")),
"newsletter" => array(
"pages" => array("newsletter", "cms/newsletter", "cms/subscriptions"),
"libraries" => array("newsletter"),
"tables" => array("subscriptions"),
"settings" => array("newsletter\_%"),
"files" => array("extra/newsletter.txt")),
"password" => array(
"pages" => array("password"),
"files" => array("extra/password.txt")),
"photo" => array(
"pages" => array("photo", "collection", "cms/photos/photo", "cms/photos/album", "cms/photos/collection"),
"libraries" => array("image"),
"tables" => array("collection_album", "collections", "photos", "photo_albums"),
"settings" => array("photo\_%"),
"directories" => array("cms/photos", "public/photos")),
"poll" => array(
"pages" => array("poll", "cms/poll"),
"libraries" => array("poll"),
"tables" => array("poll_answers", "polls"),
"settings" => array("poll\_%"),
"files" => array("logfiles/poll.log", "templates/poll_controller.php", "templates/poll_view.xslt")),
"register" => array(
"pages" => array("register"),
"files" => array("extra/register.txt")),
"search" => array(
"pages" => array("search"),
"files" => array("logfiles/search.log")),
"setup" => array(
"pages" => array("setup")),
"weblog" => array(
"pages" => array("weblog", "weblog.xml", "cms/weblog", "cms/weblog/comment"),
"tables" => array("weblog_tagged", "weblog_tags", "weblog_comments", "weblogs"),
"settings" => array("weblog\_%"),
"directories" => array("cms/weblog")),
"webshop" => array(
"pages" => array("webshop", "webshop/cart", "webshop/checkout", "webshop/orders",
"cms/webshop/article", "cms/webshop/category", "cms/webshop/order"),
"tables" => array("shop_order_article", "shop_orders", "shop_articles", "shop_categories"),
"settings" => array("webshop\_%"),
"files" => array("public/images/cart.png"),
"directories" => array("webshop", "cms/webshop")));
if (count($argv) == 1) {
/* Show modules
*/
$modules = array();
foreach ($banshee_modules as $module => $info) {
$file = $info["pages"][0];
if (file_exists("controllers/".$file.".php") || file_exists("../views/".$file.".xslt")) {
array_push($modules, $module);
}
}
sort($modules);
$width = get_terminal_width();
$max = 0;
foreach ($modules as $module) {
$len = strlen($module);
if ($len > $max) {
$max = $len;
}
}
$cols = floor($width / ($max + 2));
$col = 0;
print "Usage: ".$argv[0]." <module> [<module> ...]\n\n";
print "Available modules:\n";
foreach ($modules as $module) {
print " ".str_pad($module, $max);
if (++$col >= $cols) {
print "\n";
$col = 0;
}
}
print "\n";
return;
}
/* Remove module
*/
$params = $argv;
array_shift($params);
$modules = array();
foreach ($params as $param) {
if (isset($banshee_modules[$param])) {
array_push($modules, $banshee_modules[$param]);
}
}
$locations = array(
"css" => array("public/css"),
"js" => array("public/js"),
"php" => array("controllers", "models"),
"png" => array("public/images/icons"),
"xslt" => array("views"));
foreach ($modules as $module) {
/* Pages
*/
foreach ($locations as $extension => $paths) {
foreach ($paths as $path) {
foreach ($module["pages"] as $page) {
$file = $path."/".$page.".".$extension;
if (file_exists($file)) {
printf("Deleting file %s.\n", $file);
if (unlink($file) == false) {
printf("Error while deleting file %s\n", $file);
}
}
}
}
}
/* Libraries
*/
if (isset($module["libraries"])) {
foreach ($module["libraries"] as $library) {
$file = "libraries/".$library.".php";
if (file_exists($file)) {
printf("Deleting library %s.\n", $file);
if (unlink($file) == false) {
printf("Error while deleting library %s\n", $file);
}
}
}
}
/* Tables
*/
if (isset($module["tables"]) && $db->connected) {
foreach ($module["tables"] as $table) {
printf("Dropping table %s.\n", $table);
$db->query("drop table if exists %S", $table);
}
}
/* Files
*/
if (isset($module["files"])) {
foreach ($module["files"] as $file) {
if (file_exists($file)) {
printf("Deleting file %s.\n", $file, 3);
if (unlink($file) == false) {
printf("Error while deleting file %s\n", $file, 3);
}
}
}
}
/* Settings
*/
if (isset($module["settings"]) && $db->connected) {
foreach ($module["settings"] as $setting) {
$key = str_replace("\\", "", $setting);
$key = str_replace("%", "*", $key);
printf("Deleting setting %s.\n", $key);
$db->query("delete from settings where %S like %s", "key", $setting);
}
}
/* Directories
*/
if (isset($module["directories"])) {
foreach ($module["directories"] as $directory) {
if (file_exists($directory)) {
printf("Deleting directory %s.\n", $directory);
if (rmdir($directory) == false) {
printf("Error while deleting directory %s\n", $directory);
}
} else foreach ($locations as $paths) {
foreach ($paths as $path) {
$dir = $path."/".$directory;
if (file_exists($dir)) {
printf("Deleting directory %s.\n", $dir);
if (rmdir($dir) == false) {
printf("Error while deleting directory %s\n", $dir);
}
}
}
}
}
}
}
/* Page configuration
*/
printf("Removing module from page configuration files.\n");
foreach (array("public_modules.conf", "private_modules.conf") as $file) {
$file = "settings/".$file;
if (($config = file($file)) == false) {
continue;
}
if (($fp = fopen($file, "w")) == false) {
continue;
}
foreach ($config as $line) {
$item = chop($line);
list($item) = explode(".", $item, 2);
foreach ($modules as $module) {
if (in_array($item, $module["pages"])) {
continue 2;
}
}
fputs($fp, $line);
}
fclose($fp);
}
if ($db->connected) {
system("database/private_modules");
}
?>