2 /********************************* PHP-P10 ******************************
3 * P10 uplink class by pk910 (c)2011 pk910 *
4 ************************************************************************
7 * PHP-P10 is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
17 * You should have received a copy of the GNU General Public License *
18 * along with PHP-P10; if not, write to the Free Software Foundation, *
19 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
21 ************************************************************************
23 * BotLoader/BotLoader.class.php
25 * This class loades / rehashs or unloads all Bots.
28 require_once("Bot.class.php");
29 require_once("saxdb.class.php");
32 const BOT_DIR = "Bots";
33 const TMP_DIR = "tmp";
37 private $loadedBots = array();
38 private $botDatabases = array();
39 private static $botloader;
41 public function __construct($uplink) {
42 $this->uplink = $uplink;
43 $this->saxdb = new saxdb();
44 $this->saxdb->loadDB("php_p10.db");
45 $this->botdb = $this->saxdb->getSection("BotLoader");
46 self::$botloader = $this;
47 timer(60*10, array($this, "autosave"), array());
50 public function loadBots() {
51 if(array_key_exists("bots", $this->botdb) && is_array($this->botdb["bots"])) {
52 foreach($this->botdb["bots"] as $name => $botfile) {
53 $this->loadBot($name, $botfile);
56 $this->botdb["bots"] = array();
60 public function unloadBots() {
61 foreach($this->loadedBots as $name => $bot) {
62 $this->unloadBot($name, false);
66 public function save() {
67 $this->saxdb->setSection("BotLoader", $this->botdb);
68 $this->saxdb->writeDB("php_p10.db");
71 public function autosave() {
72 foreach($this->loadedBots as $name => $bot) {
73 if(array_key_exists(strtolower($name), $this->botDatabases)) {
74 $db = $bot->writeDB();
75 $this->saxdb->setSection($this->botDatabases[strtolower($name)],$db);
79 timer(60*10, array($this, "autosave"), array());
82 public function loop() {
83 foreach($this->loadedBots as $name => $bot) {
88 private function loadBot($name, $botfile) {
89 if(array_key_exists(strtolower($name), $this->loadedBots)) return false;
91 $bot = $this->loadClass($botfile, $name);
92 if(!$bot) return false;
93 $bot->load($this->uplink);
94 $this->loadedBots[strtolower($name)] = $bot;
95 if(!array_key_exists($name, $this->botdb["bots"])) {
96 $this->botdb["bots"][$name] = $botfile;
101 private function unloadBot($name, $delete = true) {
102 if(!(array_key_exists(strtolower($name), $this->loadedBots))) return false;
104 $bot = $this->loadedBots[strtolower($name)];
105 if(array_key_exists(strtolower($name), $this->botDatabases)) {
106 $db = $bot->writeDB();
107 $this->saxdb->setSection($this->botDatabases[strtolower($name)],$db);
108 unset($this->botDatabases[strtolower($name)]);
110 ModCMD::unbindBot($bot);
112 unset($this->loadedBots[strtolower($name)]);
113 if(array_key_exists($name, $this->botdb["bots"]) && $delete) {
114 unset($this->botdb["bots"][$name]);
119 private function rehashBot($name) {
120 if(!(array_key_exists(strtolower($name), $this->loadedBots))) return false;
122 foreach($this->botdb["bots"] as $botname => $file) {
123 if(strtolower($botname) == strtolower($name)) {
130 $bot = $this->loadedBots[strtolower($name)];
131 if(array_key_exists(strtolower($name), $this->botDatabases)) {
132 $db = $bot->writeDB();
133 $this->saxdb->setSection($this->botDatabases[strtolower($name)],$db);
134 unset($this->botDatabases[strtolower($name)]);
136 ModCMD::unbindBot($bot);
137 $data = $bot->unload(true);
138 unset($this->loadedBots[strtolower($name)]);
139 //ok bot is unloaded... load it again...
140 $bot = $this->loadClass($botfile, $name);
141 if(!$bot) return false;
142 $bot->load($this->uplink, $data);
143 $this->loadedBots[strtolower($name)] = $bot;
147 private function listLoadedBots() {
148 return $this->loadedBots;
151 private function addDBsection($bot, $section) {
152 if(!is_a($bot, "Bot")) return false;
154 foreach($this->loadedBots as $botname => $cbot) {
160 if($name == null) return false;
161 if(array_key_exists(strtolower($name), $this->botDatabases)) {
164 $this->botDatabases[strtolower($name)] = $section;
165 $bot->readDB($this->saxdb->getSection($section));
168 private function loadClass($file, $classprefix) {
169 $dir = self::BOT_DIR;
170 $tmp = self::TMP_DIR;
171 $maincode=file_get_contents($dir."/".$file);
172 if(!$maincode) return;
173 $class = rand(10000,99999);
174 while(class_exists("bot_".$classprefix."_".$class)) {
175 $class = rand(10000,99999);
177 $maincode = str_replace('{$_NAME}', "module_".$classprefix."_".$class, $maincode);
178 $fp = fopen($tmp."/modules_".$classprefix."_".$class.".tmp.php", 'w');
179 fwrite($fp, $maincode);
181 include($tmp."/modules_".$classprefix."_".$class.".tmp.php");
182 $classname = "module_".$classprefix."_".$class;
183 $newclass = new $classname();
184 unlink($tmp."/modules_".$classprefix."_".$class.".tmp.php");
188 public static function load($name, $botfile) {
189 return self::$botloader->loadBot($name, $botfile);
192 public static function unload($name) {
193 return self::$botloader->unloadBot($name);
196 public static function rehash($name) {
197 return self::$botloader->rehashBot($name);
200 public static function listBots() {
201 return self::$botloader->listLoadedBots();
204 public static function registerDB($bot, $name) {
205 return self::$botloader->addDBsection($bot, $name);