1: <?php
2:
3: namespace Budabot\Core;
4:
5: use LoggerConfiguratorDefault;
6: use Logger;
7:
8:
9:
10:
11: function isWindows() {
12: if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
13: return true;
14: } else {
15: return false;
16: }
17: }
18:
19: class BotRunner {
20:
21:
22: public $version = "4";
23:
24: private $argv = array();
25:
26: public function __construct($argv) {
27: $this->argv = $argv;
28:
29: global $version;
30: $version = $this->version;
31: }
32:
33: public function run() {
34:
35: date_default_timezone_set("UTC");
36:
37: echo $this->getInitialInfoMessage();
38: $this->loadPhpExtensions();
39:
40:
41: $this->loadPhpLibraries();
42: $this->loadEssentialCoreClasses();
43:
44:
45: global $vars;
46: $vars = $this->getConfigVars();
47: $logFolderName = $vars['name'] . '.' . $vars['dimension'];
48:
49: $this->setErrorHandling($logFolderName);
50:
51: $this->showSetupDialog();
52: $this->canonicalizeBotCharacterName();
53:
54: $this->configureLogger($logFolderName);
55:
56: $this->setWindowTitle();
57:
58: LegacyLogger::log('INFO', 'StartUp', "Starting {$vars['name']} ($this->version) on RK{$vars['dimension']}...");
59:
60: $classLoader = new ClassLoader($vars['module_load_paths']);
61: Registry::injectDependencies($classLoader);
62: $classLoader->loadInstances();
63:
64: $this->connectToDatabase();
65: $this->clearDatabaseInformation();
66:
67: $this->runUpgradeScripts();
68:
69: list($server, $port) = $this->getServerAndPort($vars);
70:
71: $chatBot = Registry::getInstance('chatBot');
72:
73:
74: $chatBot->init($vars);
75:
76:
77: $chatBot->connectAO($vars['login'], $vars['password'], $server, $port);
78:
79:
80: unset($vars['login']);
81: unset($vars['password']);
82:
83:
84: $chatBot->run();
85: }
86:
87: private function getInitialInfoMessage() {
88: return "\n\n\n\n\n
89: **************************************************
90: Budabot {$this->version}
91:
92: Project Site: https://github.com/Budabot/Budabot
93: Support Forum: http://www.budabot.com/forum
94: In-Game Contact: Tyrence27
95: **************************************************
96: \n";
97: }
98:
99: private function loadPhpExtensions() {
100: if (isWindows()) {
101:
102: dl("php_sockets.dll");
103: dl("php_pdo_sqlite.dll");
104: dl("php_pdo_mysql.dll");
105: } else {
106:
107:
108: if (!extension_loaded('pdo_sqlite')) {
109: @dl('pdo_sqlite.so');
110: }
111: if (!extension_loaded('pdo_mysql')) {
112: @dl('pdo_mysql.so');
113: }
114: }
115: }
116:
117: protected function getConfigVars() {
118: require_once 'ConfigFile.class.php';
119:
120:
121: $configFilePath = $this->argv[1];
122: global $configFile;
123: $configFile = new ConfigFile($configFilePath);
124: $configFile->load();
125: $vars = $configFile->getVars();
126: return $vars;
127: }
128:
129: private function setErrorHandling($logFolderName) {
130: error_reporting(E_ALL & ~E_STRICT & ~E_WARNING & ~E_NOTICE);
131: ini_set("log_errors", 1);
132: ini_set('display_errors', 1);
133: ini_set("error_log", "./logs/" . $logFolderName . "/php_errors.log");
134: }
135:
136: private function loadPhpLibraries() {
137: require_once './lib/addendum-0.4.1/annotations.php';
138: require_once './lib/vendor/autoload.php';
139: }
140:
141: private function loadEssentialCoreClasses() {
142: require_once './core/Registry.class.php';
143: require_once './core/ClassLoader.class.php';
144: require_once './core/LegacyLogger.class.php';
145: require_once './core/LoggerWrapper.class.php';
146: require_once './core/annotations.php';
147: }
148:
149: private function showSetupDialog() {
150: if ($this->shouldShowSetup()) {
151: global $vars;
152: include "./core/SETUP/setup.php";
153: }
154: }
155:
156: private function shouldShowSetup() {
157: global $vars;
158: return $vars['login'] == "" || $vars['password'] == "" || $vars['name'] == "";
159: }
160:
161: private function canonicalizeBotCharacterName() {
162: global $vars;
163: $vars["name"] = ucfirst(strtolower($vars["name"]));
164: }
165:
166:
167: private function configureLogger($logFolderName) {
168: $configurator = new LoggerConfiguratorDefault();
169: $config = $configurator->parse('conf/log4php.xml');
170: $file = $config['appenders']['defaultFileAppender']['params']['file'];
171: $file = str_replace("./logs/", "./logs/" . $logFolderName . "/", $file);
172: $config['appenders']['defaultFileAppender']['params']['file'] = $file;
173: Logger::configure($config);
174: }
175:
176: private function setWindowTitle() {
177:
178: if (isWindows()) {
179: global $vars;
180: system("title {$vars['name']} - Budabot");
181: }
182: }
183:
184: private function connectToDatabase() {
185: global $vars;
186: $db = Registry::getInstance('db');
187: $db->connect($vars["DB Type"], $vars["DB Name"], $vars["DB Host"], $vars["DB username"], $vars["DB password"]);
188: }
189:
190: private function clearDatabaseInformation() {
191: global $vars;
192: unset($vars["DB Type"]);
193: unset($vars["DB Name"]);
194: unset($vars["DB Host"]);
195: unset($vars["DB username"]);
196: unset($vars["DB password"]);
197: }
198:
199: private function runUpgradeScripts() {
200: if (file_exists('upgrade.php')) {
201: include 'upgrade.php';
202: }
203: }
204:
205: protected function getServerAndPort($vars) {
206:
207: if ($vars['use_proxy'] == 1) {
208:
209: $server = $vars['proxy_server'];
210: $port = $vars['proxy_port'];
211: } else if ($vars["dimension"] == 4) {
212: $server = "chat.dt.funcom.com";
213: $port = 7109;
214: } else if ($vars["dimension"] == 5) {
215: $server = "chat.d1.funcom.com";
216: $port = 7105;
217: } else {
218: LegacyLogger::log('ERROR', 'StartUp', "No valid server to connect with! Available dimensions are 4 and 5.");
219: sleep(10);
220: die();
221: }
222: return array($server, $port);
223: }
224: }
225: