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->writeDB("php_p10.db");
70 public function autosave() {
71 foreach($this->loadedBots as $name => $bot) {
72 if(array_key_exists(strtolower($name), $this->botDatabases)) {
73 $db = $bot->writeDB();
74 $this->saxdb->setSection($this->botDatabases[strtolower($name)],$db);
78 timer(60*10, array($this, "autosave"), array());
81 public function loop() {
82 foreach($this->loadedBots as $name => $bot) {
87 private function loadBot($name, $botfile) {
88 if(array_key_exists(strtolower($name), $this->loadedBots)) return false;
90 $bot = $this->loadClass($botfile, $name);
91 if(!$bot) return false;
92 $bot->load($this->uplink);
93 $this->loadedBots[strtolower($name)] = $bot;
94 if(!array_key_exists($name, $this->botdb["bots"])) {
95 $this->botdb["bots"][$name] = $botfile;
99 private function unloadBot($name, $delete = true) {
100 if(!(array_key_exists(strtolower($name), $this->loadedBots))) return false;
102 $bot = $this->loadedBots[strtolower($name)];
103 if(array_key_exists(strtolower($name), $this->botDatabases)) {
104 $db = $bot->writeDB();
105 $this->saxdb->setSection($this->botDatabases[strtolower($name)],$db);
106 unset($this->botDatabases[strtolower($name)]);
108 ModCMD::unbindBot($bot);
110 unset($this->loadedBots[strtolower($name)]);
111 if(array_key_exists($name, $this->botdb["bots"]) && $delete) {
112 unset($this->botdb["bots"][$name]);
116 private function rehashBot($name) {
117 if(!(array_key_exists(strtolower($name), $this->loadedBots))) return false;
119 $bot = $this->loadedBots[strtolower($name)];
120 if(array_key_exists(strtolower($name), $this->botDatabases)) {
121 $db = $bot->writeDB();
122 $this->saxdb->setSection($this->botDatabases[strtolower($name)],$db);
123 unset($this->botDatabases[strtolower($name)]);
125 ModCMD::unbindBot($bot);
126 $data = $bot->unload(true);
127 unset($this->loadedBots[strtolower($name)]);
128 //ok bot is unloaded... load it again...
129 $bot = $this->loadClass($botfile, $name);
130 if(!$bot) return false;
131 $bot->load($this->uplink, $data);
132 $this->loadedBots[strtolower($name)] = $bot;
135 private function listLoadedBots() {
136 return $this->loadedBots;
139 private function addDBsection($bot, $section) {
140 if(!is_a($bot, "Bot")) return false;
142 foreach($this->loadedBots as $botname => $cbot) {
148 if($name == null) return false;
149 if(array_key_exists(strtolower($name), $this->botDatabases)) {
152 $this->botDatabases[strtolower($name)] = $section;
153 $bot->readDB($this->saxdb->getSection($section));
156 private function loadClass($file, $classprefix) {
157 $dir = self::BOT_DIR;
158 $tmp = self::TMP_DIR;
159 $maincode=file_get_contents($dir."/".$file);
160 if(!$maincode) return;
161 $class = rand(10000,99999);
162 while(class_exists("bot_".$classprefix."_".$class)) {
163 $class = rand(10000,99999);
165 $maincode = str_replace('{$_NAME}', "module_".$classprefix."_".$class, $maincode);
166 $fp = fopen($tmp."/modules_".$classprefix."_".$class.".tmp.php", 'w');
167 fwrite($fp, $maincode);
169 include($tmp."/modules_".$classprefix."_".$class.".tmp.php");
170 $classname = "module_".$classprefix."_".$class;
171 $newclass = new $classname();
172 unlink($tmp."/modules_".$classprefix."_".$class.".tmp.php");
176 public static function load($name, $botfile) {
177 return self::$botloader->loadBot($name, $botfile);
180 public static function unload($name) {
181 return self::$botloader->unloadBot($name);
184 public static function rehash($name) {
185 return self::$botloader->rehashBot($name);
188 public static function listBots() {
189 return self::$botloader->listLoadedBots();
192 public static function registerDB($bot, $name) {
193 return self::$botloader->addDBsection($bot, $name);