-
Notifications
You must be signed in to change notification settings - Fork 1
/
startup.php
198 lines (161 loc) · 6.92 KB
/
startup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
// This bot doesn't properly work on a Fast-CGI environment, so toss an error on this (Check is pretty loose, might make this better later on)
if (isset($_SERVER['GATEWAY_INTERFACE']) && ((stristr($_SERVER['GATEWAY_INTERFACE'], 'fcgi')) || (stristr($_SERVER['GATEWAY_INTERFACE'], 'fastcgi'))))
{
exit('This bot can not run properly in a FastCGI environment. Please use a different PHP environment like "mod_php"');
}
// Srt our PHP environment variables
ini_set('max_execution_time', '0'); // Set this directive so the PHP CGI does no kill the script during execution
ini_set('memory_limit', '32M'); // Set this direcive in case the server lowers the memory cost of a script
ini_set('ignore_user_abort', '1'); // Stops an impatient user or script from failing to start the bot. We do validity checks anyway
ini_set('default_charset', 'UTF-8'); // Sets our default charset to UTF-8
// Set a constant to define the core dir of the bot
define('BURNBOT_CORE_PATH', realpath(__DIR__));
// Get our config files
require(BURNBOT_CORE_PATH . '/dependencies/constants.php');
require(BURNBOT_CORE_PATH . '/dependencies/config.php');
// Sift through the POST data and validate it all
$host = (isset($_POST['host'])) ? strval($_POST['host']) : null;
$port = (isset($_POST['port']) && (strval($_POST['port']) != '')) ? intval($_POST['port']) : 6667;
$chan = (isset($_POST['chan'])) ? strval($_POST['chan']) : null;
$chan = (!is_null($chan) && ($chan[0] == '#')) ? $chan : '#' . $chan;
$nick = (isset($_POST['nick'])) ? strval($_POST['nick']) : null;
$pass = (isset($_POST['pass'])) ? strval($_POST['pass']) : null;
$preJoin = (isset($_POST['prejoin'])) ? strval($_POST['prejoin']) : null;
$postJoin = (isset($_POST['postjoin'])) ? strval($_POST['postjoin']) : null;
$readOnly = (isset($_POST['read_only']) && (strval($_POST['read_only']) == '1')) ? true : false;
if (!$host || !$chan || !$nick)
{
// We didn't get crucial data, silently die
exit;
}
// Echo out our confirmation that startup is proceeding
echo "Startup checks passed. Output passed to logging handlers";
header('Connection: close');
header('Content-length: ' . ob_get_length());
// Perform both because some systems may not support ob_end_flush properly or may not allow it to flush the page
ob_end_flush();
flush();
// Start our logger and error handler here
require(BURNBOT_CORE_PATH . '/dependencies/logger.php');
$file = BURNBOT_CORE_PATH . "/logs/$host/$chan/" . date('m-d-y_H-i-s') . '.php';
// Check to see if our logging dir exists. Create it recursively if not.
if (!is_dir(BURNBOT_CORE_PATH . "/logs/$host/$chan/"))
{
mkdir(BURNBOT_CORE_PATH . "/logs/$host/$chan/", 0755, true);
}
$logger = new logger($file);
// And now overwrite all of the error handlers
function errorHandler($errNo, $errStr, $file, $line)
{
global $logger;
// If the error was not suppressed, process it
if ($errNo !== 0)
{
$logger->logErrorHandler($errNo, $errStr, $file, $line);
}
}
function exceptionHandler($exception)
{
global $logger;
$logger->logException($exception);
}
function fatalCrashHandler()
{
global $logger;
// Always log the last erre on shutdown. Core will overwrite the shutdown function in normal cases
$err = error_get_last();
if (isset($err) && !empty($err))
{
$logger->logErrorHandler($err['type'], $err['message'], $err['file'], $err['line']);
}
}
set_error_handler("errorHandler");
set_exception_handler('exceptionHandler');
register_shutdown_function('fatalCrashHandler');
error_reporting(E_ALL);
$logger->logLine('Current bot path set as: ' . BURNBOT_CORE_PATH, 'startup');
$logger->logLine('Logging and error handling initialized', 'startup');
// Init the IRC library
require(BURNBOT_CORE_PATH . '/dependencies/irc.php');
$irc = new irc($host, $port);
$logger->logLine('IRC library initialized', 'startup');
// Init the DB connection
require(BURNBOT_CORE_PATH . '/dependencies/db.php');
$db = new db($dbCreds['host'], $dbCreds['port'], $dbCreds['user'], $dbCreds['pass'], $dbCreds['db_name']);
unset($dbCreds['pass']);
if (!$db->connected())
{
// Try again here
if (!$db->connect())
{
exit('Error: Could not make a database link using ' . $dbCreds['user'] . '@' . $dbCreds['host'] . ':' . $dbCreds['port']);
}
}
$logger->logLine('Database connection initialized', 'startup');
// Load any module dependencies included
$h = opendir(BURNBOT_CORE_PATH . '/dependencies/module/');
while ($fn = readdir($h))
{
if (!is_dir($fn))
{
include_once(BURNBOT_CORE_PATH . "/dependencies/module/$fn");
$logger->logLine("Loaded dependeny: ./dependencies/module/$fn", 'startup');
}
}
unset($h, $fn);
$logger->logLine('Loaded all module dependencies', 'startup');
require(BURNBOT_CORE_PATH . '/core.php');
$burnBot = new burnbot($host, $port, $chan, $nick, $pass, $preJoin, $postJoin, $readOnly, $db, $irc, $logger);
$logger->logLine('Core module loaded', 'startup');
// Load modules
$logger->logLine('Looking for global modules', 'startup');
$h = opendir(BURNBOT_CORE_PATH . '/modules/');
while (($fn = readdir($h)) !== false)
{
if (!is_dir( BURNBOT_CORE_PATH . "/modules/$fn"))
{
include_once(BURNBOT_CORE_PATH . "/modules/$fn");
$fn = rtrim($fn, '.php');
$obj = new $fn(true);
unset($obj);
$logger->logLine("loaded module: ./modules/$fn.php", 'startup');
}
}
unset($h, $fn);
// Now look to load network and channel modules
$logger->logLine("Looking for network and channel modules", 'startup');
if (file_exists(BURNBOT_CORE_PATH . "/modules/$host") && is_dir(BURNBOT_CORE_PATH . "/modules/$host"))
{
$h = opendir(BURNBOT_CORE_PATH . "/modules/$host");
while ($fn = readdir($h))
{
if (!is_dir(BURNBOT_CORE_PATH . "/modules/$host/$fn"))
{
include_once(BURNBOT_CORE_PATH . "/modules/$host/$fn");
$fn = rtrim($fn, '.php');
$obj = new $fn(true);
unset($obj);
$logger->logLine("loaded module: ./modules/$host/$fn.php", 'startup');
} elseif (is_dir(BURNBOT_CORE_PATH . "/modules/$host/$fn") && ($fn == $chan)) {
$ch = opendir(BURNBOT_CORE_PATH . "/modules/$host/$chan");
while ($cfn = readdir($ch))
{
if (!is_dir(BURNBOT_CORE_PATH . "/modules/$host/$chan/$cfn"))
{
include_once(BURNBOT_CORE_PATH . "/modules/$host/$chan/$cfn");
$cfn = rtrim($cfn, '.php');
$obj = new $cfn(true);
unset($obj);
$logger->logLine("loaded module: ./modules/$host/$chan/$cfn.php", 'startup');
}
}
}
}
}
$logger->logLine('Loaded all modules', 'startup');
$logger->logLine("Startup completed. Starting bot initialization and tick", 'startup');
// Run the init phase and then start the bot
$burnBot->init();
$burnBot->tick(true);
?>