Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 265 lines (224 sloc) 6.59 kb
a783d14 @madeinnordeste php server monitor files
authored
1 <?php
2
3 /*
4 * PHP Server Monitor v2.0.1
5 * Monitor your servers with error notification
6 * http://phpservermon.sourceforge.net/
7 *
8 * Copyright (c) 2008-2011 Pepijn Over (ipdope@users.sourceforge.net)
9 *
10 * This file is part of PHP Server Monitor.
11 * PHP Server Monitor is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * PHP Server Monitor is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with PHP Server Monitor. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 /**
26 *
27 * Autoload
28 *
29 */
30 function __autoload($class) {
31 // first check if a subdir exists for the class
32 // it splits using uppercase chars
33 preg_match_all("/(\P{Lu}+)|(\p{Lu}+\P{Lu}*)/", $class, $subdir_matches);
34
35 if(!empty($subdir_matches) && count($subdir_matches[0]) > 1) {
36 // okay we have some upper case, lets see if a dir exists
37 $dir = dirname(__FILE__) . '/classes/' . trim($subdir_matches[0][0]);
38 $file = $dir . '/' . trim($class) . '.class.php';
39
40 if(is_dir($dir) && file_exists($file)) {
41 require $file;
42 return $file;
43 }
44 } else {
45 $file = dirname(__FILE__).'/classes/'.trim(strtolower($class)).'.class.php';
46
47 if(file_exists($file)){
48 require $file;
49 return $file;
50 }
51 }
52
53 trigger_error("KERNEL_ERR : Unable to find file:\n\t\t[$file]\n\t associated with class:\n\t\t$class", E_USER_ERROR);
54 return false;
55 }
56
57 ###############################################
58 #
59 # Language functions
60 #
61 ###############################################
62
63 /**
64 * Retrieve language settings from the selected language file
65 *
66 * @return string
67 * @see sm_load_lang()
68 */
69 function sm_get_lang() {
70 $args = func_get_args();
71
72 if(empty($args)) return $GLOBALS['sm_lang'];
73
74 $result = null;
75 $node = null;
76
77 if($args) {
78 $node = '$GLOBALS[\'sm_lang\'][\'' . implode('\'][\'', $args) . '\']';
79 }
80 eval('if (isset('.$node.')) $result = '.$node.';');
81
82 return $result;
83 }
84
85 /**
86 * Load language from the language file to the $GLOBALS['sm_lang'] variable
87 *
88 * @param string $lang language
89 * @see sm_get_lang()
90 */
91 function sm_load_lang($lang) {
92 $lang_file = dirname(__FILE__) . '/lang/' . $lang . '.lang.php';
93
94 if(!file_exists($lang_file)) {
95 die('unable to load language file: ' . $lang_file);
96 }
97
98 require $lang_file;
99
100 $GLOBALS['sm_lang'] = $sm_lang;
101 }
102
103 /**
104 * Get a setting from the config.
105 * The config must have been loaded first using sm_load_conf()
106 *
107 * @param string $key
108 * @return string
109 * @see sm_load_conf()
110 */
111 function sm_get_conf($key) {
112 $result = (isset($GLOBALS['sm_config'][$key])) ? $GLOBALS['sm_config'][$key] : null;
113
114 return $result;
115 }
116
117 /**
118 * Load config from the database to the $GLOBALS['sm_config'] variable
119 *
120 * @global object $db
121 * @see sm_get_conf()
122 */
123 function sm_load_conf() {
124 global $db;
125
126 // load config from database into global scope
127 $GLOBALS['sm_config'] = array();
128 $config_db = $db->select(SM_DB_PREFIX . 'config', null, array('key', 'value'));
129 foreach($config_db as $setting) {
130 $GLOBALS['sm_config'][$setting['key']] = $setting['value'];
131 }
132
133 if(empty($GLOBALS['sm_config']) && basename($_SERVER['SCRIPT_NAME']) != 'install.php') {
134 // no config found, go to install page
135 die('Failed to load config table. Please run the install.php file');
136 }
137 }
138
139
140 ###############################################
141 #
142 # Miscellaneous functions
143 #
144 ###############################################
145
146 /**
147 * This function merely adds the message to the log table. It does not perform any checks,
148 * everything should have been handled when calling this function
149 *
150 * @param string $server_id
151 * @param string $message
152 */
153 function sm_add_log($server_id, $type, $message, $user_id = null) {
154 global $db;
155
156 $db->save(
157 SM_DB_PREFIX.'log',
158 array(
159 'server_id' => $server_id,
160 'type' => $type,
161 'message' => $message,
162 'user_id' => ($user_id === null) ? '' : $user_id,
163 )
164 );
165 }
166
167 /**
168 * Parses a string from the language file with the correct variables replaced in the message
169 *
170 * @param string $status is either 'on' or 'off'
171 * @param string $type is either 'sms' or 'email'
172 * @param array $server information about the server which may be placed in a message: %KEY% will be replaced by your value
173 * @return string parsed message
174 */
175 function sm_parse_msg($status, $type, $vars) {
176 $message = '';
177
178 $message = sm_get_lang('notifications', $status . '_' . $type);
179
180 if(!$message) {
181 return $message;
182 }
183 $vars['date'] = date('Y-m-d H:i:s');
184
185 foreach($vars as $k => $v) {
186 $message = str_replace('%' . strtoupper($k) . '%', $v, $message);
187 }
188
189 return $message;
190 }
191
192 /**
193 * Shortcut to curl_init(), curl_exec and curl_close()
194 *
195 * @param string $href
196 * @return string cURL result
197 */
198 function sm_curl_get($href) {
199 $ch = curl_init();
200 curl_setopt($ch, CURLOPT_HEADER, 0);
201 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
202 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
203
204 curl_setopt($ch, CURLOPT_URL, $href);
205 $result = curl_exec($ch);
206 curl_close($ch);
207
208 return $result;
209 }
210
211 /**
212 * Check if an update is available for PHP Server Monitor
213 *
214 * @global object $db
215 * @return boolean
216 */
217 function sm_check_updates() {
218 global $db;
219
220 $last_update = sm_get_conf('last_update_check');
221
222 if((time() - (7 * 24 * 60 * 60)) > $last_update) {
223 // been more than a week since update, lets go
224 // update "update-date"
225 $db->save(SM_DB_PREFIX . 'config', array('value' => time()), array('key' => 'last_update_check'));
226 $latest = sm_curl_get('http://phpservermon.neanderthal-technology.com/version');
227 $current = sm_get_conf('version');
228
229 if((int) $current < (int) $latest) {
230 // new update available
231 return true;
232 }
233 }
234 return false;
235 }
236
237 ###############################################
238 #
239 # Debug functions
240 #
241 ###############################################
242
243 /**
244 * Only used for debugging and testing
245 *
246 * @param mixed $arr
247 */
248 function pre($arr = null) {
249 echo "<pre>";
250 if ($arr === null) debug_print_backtrace();
251 print_r($arr);
252 echo "</pre>";
253 }
254
255 /**
256 * Send headers to the browser to avoid caching
257 */
258 function sm_no_cache() {
259 header("Expires: Mon, 20 Dec 1998 01:00:00 GMT");
260 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
261 header("Cache-Control: no-cache, must-revalidate");
262 header("Pragma: no-cache");
263 }
264
265 ?>
Something went wrong with that request. Please try again.