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(array_key_exists(strtolower($name), $this->botDatabases)) {
71 $db = $bot->writeDB();
72 $this->saxdb->setSection($this->botDatabases[strtolower($name)],$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(array_key_exists(strtolower($name), $this->botDatabases)) {
103 $db = $bot->writeDB();
104 $this->saxdb->setSection($this->botDatabases[strtolower($name)],$db);
105 unset($this->botDatabases[strtolower($name)]);
107 ModCMD::unbindBot($bot);
109 unset($this->loadedBots[strtolower($name)]);
110 if(array_key_exists($name, $this->botdb["bots"]) && $delete) {
111 unset($this->botdb["bots"][$name]);
116 private function rehashBot($name) {
117 if(!(array_key_exists(strtolower($name), $this->loadedBots))) return false;
119 foreach($this->botdb["bots"] as $botname => $file) {
120 if(strtolower($botname) == strtolower($name)) {
127 $bot = $this->loadedBots[strtolower($name)];
128 if(array_key_exists(strtolower($name), $this->botDatabases)) {
129 $db = $bot->writeDB();
130 $this->saxdb->setSection($this->botDatabases[strtolower($name)],$db);
131 unset($this->botDatabases[strtolower($name)]);
133 ModCMD::unbindBot($bot);
134 $data = $bot->unload(true);
135 unset($this->loadedBots[strtolower($name)]);
136 //ok bot is unloaded... load it again...
137 $bot = $this->loadClass($botfile, $name);
138 if(!$bot) return false;
139 $bot->load($this->uplink, $data);
140 $this->loadedBots[strtolower($name)] = $bot;
144 private function listLoadedBots() {
145 return $this->loadedBots;
148 private function addDBsection($bot, $section) {
149 if(!is_a($bot, "Bot")) return false;
151 foreach($this->loadedBots as $botname => $cbot) {
157 if($name == null) return false;
158 if(array_key_exists(strtolower($name), $this->botDatabases)) {
161 $this->botDatabases[strtolower($name)] = $section;
162 $bot->readDB($this->saxdb->getSection($section));
165 private function loadClass($file, $classprefix) {
166 $dir = self::BOT_DIR;
167 $tmp = self::TMP_DIR;
168 $maincode=file_get_contents($dir."/".$file);
169 if(!$maincode) return;
170 $class = rand(10000,99999);
171 while(class_exists("bot_".$classprefix."_".$class)) {
172 $class = rand(10000,99999);
174 $maincode = str_replace('{$_NAME}', "module_".$classprefix."_".$class, $maincode);
175 $fp = fopen($tmp."/modules_".$classprefix."_".$class.".tmp.php", 'w');
176 fwrite($fp, $maincode);
178 include($tmp."/modules_".$classprefix."_".$class.".tmp.php");
179 $classname = "module_".$classprefix."_".$class;
180 $newclass = new $classname();
181 unlink($tmp."/modules_".$classprefix."_".$class.".tmp.php");
185 public static function load($name, $botfile) {
186 return self::$botloader->loadBot($name, $botfile);
189 public static function unload($name) {
190 return self::$botloader->unloadBot($name);
193 public static function rehash($name) {
194 return self::$botloader->rehashBot($name);
197 public static function listBots() {
198 return self::$botloader->listLoadedBots();
201 public static function registerDB($bot, $name) {
202 return self::$botloader->addDBsection($bot, $name);