2 /******************************* PHP-P10 v2 *****************************
3 * Copyright (C) 2011 Philipp Kreil (pk910) *
5 * This program is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 3 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 ************************************************************************
20 * BotLoader/BotLoader.class.php
22 * This class loades / rehashs or unloads all Bots.
25 require_once("Bot.class.php");
26 require_once("saxdb.class.php");
29 const BOT_DIR = "Bots";
30 const TMP_DIR = "tmp";
34 private $loadedBots = array();
35 private $botDatabases = array();
36 private static $botloader;
38 public function __construct($uplink) {
39 $this->uplink = $uplink;
40 $this->saxdb = new saxdb();
41 $this->saxdb->loadDB("php_p10.db");
42 $this->botdb = $this->saxdb->getSection("BotLoader");
43 self::$botloader = $this;
44 timer(60*10, array($this, "autosave"), array());
47 public function loadBots() {
48 if(array_key_exists("bots", $this->botdb) && is_array($this->botdb["bots"])) {
49 foreach($this->botdb["bots"] as $name => $botfile) {
50 $this->loadBot($name, $botfile);
53 $this->botdb["bots"] = array();
57 public function unloadBots() {
58 foreach($this->loadedBots as $name => $bot) {
59 $this->unloadBot($name, false);
63 public function save() {
64 $this->saxdb->setSection("BotLoader", $this->botdb);
65 $this->saxdb->writeDB("php_p10.db");
68 public function autosave() {
69 foreach($this->loadedBots as $name => $bot) {
70 if($bot->getDBSection()) {
71 $db = $bot->writeDB();
72 $this->saxdb->setSection($bot->getDBSection(),$db);
76 timer(60*10, array($this, "autosave"), array());
79 public function loop() {
80 foreach($this->loadedBots as $name => $bot) {
85 private function loadBot($name, $botfile) {
86 if(array_key_exists(strtolower($name), $this->loadedBots)) return false;
88 $bot = $this->loadClass($botfile, $name);
89 if(!$bot) return false;
90 $bot->load($this->uplink);
91 $this->loadedBots[strtolower($name)] = $bot;
92 if(!array_key_exists($name, $this->botdb["bots"])) {
93 $this->botdb["bots"][$name] = $botfile;
98 private function unloadBot($name, $delete = true) {
99 if(!(array_key_exists(strtolower($name), $this->loadedBots))) return false;
101 $bot = $this->loadedBots[strtolower($name)];
102 if($bot->getDBSection()) {
103 $db = $bot->writeDB();
104 $this->saxdb->setSection($bot->getDBSection(),$db);
106 ModCMD::unbindBot($bot);
108 unset($this->loadedBots[strtolower($name)]);
109 if(array_key_exists($name, $this->botdb["bots"]) && $delete) {
110 unset($this->botdb["bots"][$name]);
115 private function rehashBot($name) {
116 if(!(array_key_exists(strtolower($name), $this->loadedBots))) return false;
118 foreach($this->botdb["bots"] as $botname => $file) {
119 if(strtolower($botname) == strtolower($name)) {
126 $bot = $this->loadedBots[strtolower($name)];
127 if($bot->getDBSection()) {
128 $db = $bot->writeDB();
129 $this->saxdb->setSection($bot->getDBSection(),$db);
131 ModCMD::unbindBot($bot);
132 $data = $bot->unload(true);
133 unset($this->loadedBots[strtolower($name)]);
134 //ok bot is unloaded... load it again...
135 $bot = $this->loadClass($botfile, $name);
136 if(!$bot) return false;
137 $bot->load($this->uplink, $data);
138 $this->loadedBots[strtolower($name)] = $bot;
142 private function listLoadedBots() {
143 return $this->loadedBots;
146 private function addDBsection($bot, $section) {
147 if(!is_a($bot, "Bot")) return false;
148 $bot->setDBSection($section);
149 $bot->readDB($this->saxdb->getSection($section));
152 private function loadClass($file, $classprefix) {
153 $dir = self::BOT_DIR;
154 $tmp = self::TMP_DIR;
155 $maincode=file_get_contents($dir."/".$file);
156 if(!$maincode) return;
157 $class = rand(10000,99999);
158 while(class_exists("bot_".$classprefix."_".$class)) {
159 $class = rand(10000,99999);
161 $maincode = str_replace('{$_NAME}', "module_".$classprefix."_".$class, $maincode);
162 $fp = fopen($tmp."/modules_".$classprefix."_".$class.".tmp.php", 'w');
163 fwrite($fp, $maincode);
165 include($tmp."/modules_".$classprefix."_".$class.".tmp.php");
166 $classname = "module_".$classprefix."_".$class;
167 $newclass = new $classname();
168 unlink($tmp."/modules_".$classprefix."_".$class.".tmp.php");
172 public static function load($name, $botfile) {
173 return self::$botloader->loadBot($name, $botfile);
176 public static function unload($name) {
177 return self::$botloader->unloadBot($name);
180 public static function rehash($name) {
181 return self::$botloader->rehashBot($name);
184 public static function listBots() {
185 return self::$botloader->listLoadedBots();
188 public static function registerDB($bot, $name) {
189 return self::$botloader->addDBsection($bot, $name);