| | 22 | |
|---|
| | 23 | class modulelist { |
|---|
| | 24 | var $_loaded = false; |
|---|
| | 25 | var $module_array = array(); |
|---|
| | 26 | var $_db; |
|---|
| | 27 | |
|---|
| | 28 | function &create(&$db) { |
|---|
| | 29 | static $obj; |
|---|
| | 30 | if (!isset($obj)) { |
|---|
| | 31 | $obj = new modulelist($db); |
|---|
| | 32 | } |
|---|
| | 33 | return $obj; |
|---|
| | 34 | } |
|---|
| | 35 | function modulelist(&$db) { |
|---|
| | 36 | $this->_db =& $db; |
|---|
| | 37 | $module_serialized = sql("SELECT `data` FROM `module_xml` WHERE `id` = 'mod_serialized'","getOne"); |
|---|
| | 38 | if (isset($module_serialized) && $module_serialized) { |
|---|
| | 39 | $this->module_array = (unserialize($module_serialized)); |
|---|
| | 40 | $this->_loaded = true; |
|---|
| | 41 | } |
|---|
| | 42 | } |
|---|
| | 43 | function is_loaded() { |
|---|
| | 44 | return $this->_loaded; |
|---|
| | 45 | } |
|---|
| | 46 | function initialize(&$module_list) { |
|---|
| | 47 | $this->module_array = $module_list; |
|---|
| | 48 | $module_serialized = $this->_db->escapeSimple(serialize($this->module_array)); |
|---|
| | 49 | sql("DELETE FROM `module_xml` WHERE `id` = 'mod_serialized'"); |
|---|
| | 50 | sql("INSERT INTO `module_xml` (`id`, `time`, `data`) VALUES ('mod_serialized', '".time()."','".$module_serialized."')"); |
|---|
| | 51 | $this->_loaded = true; |
|---|
| | 52 | } |
|---|
| | 53 | function invalidate() { |
|---|
| | 54 | unset($this->module_array); |
|---|
| | 55 | sql("DELETE FROM `module_xml` WHERE `id` = 'mod_serialized'"); |
|---|
| | 56 | $this->_loaded = false; |
|---|
| | 57 | } |
|---|
| | 58 | } |
|---|
| | 59 | |
|---|
| | 60 | define("NOTIFICATION_TYPE_CRITICAL", 100); |
|---|
| | 61 | define("NOTIFICATION_TYPE_SECURITY", 200); |
|---|
| | 62 | define("NOTIFICATION_TYPE_UPDATE", 300); |
|---|
| | 63 | define("NOTIFICATION_TYPE_ERROR", 400); |
|---|
| | 64 | define("NOTIFICATION_TYPE_WARNING" , 500); |
|---|
| | 65 | define("NOTIFICATION_TYPE_NOTICE", 600); |
|---|
| | 66 | |
|---|
| | 67 | class notifications { |
|---|
| | 68 | |
|---|
| | 69 | var $not_loaded = true; |
|---|
| | 70 | var $notification_table = array(); |
|---|
| | 71 | var $_db; |
|---|
| | 72 | |
|---|
| | 73 | function &create(&$db) { |
|---|
| | 74 | static $obj; |
|---|
| | 75 | if (!isset($obj)) { |
|---|
| | 76 | $obj = new notifications($db); |
|---|
| | 77 | } |
|---|
| | 78 | return $obj; |
|---|
| | 79 | } |
|---|
| | 80 | |
|---|
| | 81 | function notifications(&$db) { |
|---|
| | 82 | $this->_db =& $db; |
|---|
| | 83 | } |
|---|
| | 84 | |
|---|
| | 85 | |
|---|
| | 86 | function add_critical($module, $id, $display_text, $extended_text="", $link="", $reset=true, $candelete=false) { |
|---|
| | 87 | $this->_add_type(NOTIFICATION_TYPE_CRITICAL, $module, $id, $display_text, $extended_text, $link, $reset, $candelete); |
|---|
| | 88 | } |
|---|
| | 89 | function add_security($module, $id, $display_text, $extended_text="", $link="", $reset=true, $candelete=false) { |
|---|
| | 90 | $this->_add_type(NOTIFICATION_TYPE_SECURITY, $module, $id, $display_text, $extended_text, $link, $reset, $candelete); |
|---|
| | 91 | } |
|---|
| | 92 | function add_update($module, $id, $display_text, $extended_text="", $link="", $reset=false, $candelete=false) { |
|---|
| | 93 | $this->_add_type(NOTIFICATION_TYPE_UPDATE, $module, $id, $display_text, $extended_text, $link, $reset, $candelete); |
|---|
| | 94 | } |
|---|
| | 95 | function add_error($module, $id, $display_text, $extended_text="", $link="", $reset=false, $candelete=false) { |
|---|
| | 96 | $this->_add_type(NOTIFICATION_TYPE_ERROR, $module, $id, $display_text, $extended_text, $link, $reset, $candelete); |
|---|
| | 97 | } |
|---|
| | 98 | function add_warning($module, $id, $display_text, $extended_text="", $link="", $reset=false, $candelete=false) { |
|---|
| | 99 | $this->_add_type(NOTIFICATION_TYPE_WARNING, $module, $id, $display_text, $extended_text, $link, $reset, $candelete); |
|---|
| | 100 | } |
|---|
| | 101 | function add_notice($module, $id, $display_text, $extended_text="", $link="", $reset=false, $candelete=true) { |
|---|
| | 102 | $this->_add_type(NOTIFICATION_TYPE_NOTICE, $module, $id, $display_text, $extended_text, $link, $reset, $candelete); |
|---|
| | 103 | } |
|---|
| | 104 | |
|---|
| | 105 | |
|---|
| | 106 | function list_critical($show_reset=false) { |
|---|
| | 107 | return $this->_list(NOTIFICATION_TYPE_CRITICAL, $show_reset); |
|---|
| | 108 | } |
|---|
| | 109 | function list_security($show_reset=false) { |
|---|
| | 110 | return $this->_list(NOTIFICATION_TYPE_SECURITY, $show_reset); |
|---|
| | 111 | } |
|---|
| | 112 | function list_update($show_reset=false) { |
|---|
| | 113 | return $this->_list(NOTIFICATION_TYPE_UPDATE, $show_reset); |
|---|
| | 114 | } |
|---|
| | 115 | function list_error($show_reset=false) { |
|---|
| | 116 | return $this->_list(NOTIFICATION_TYPE_ERROR, $show_reset); |
|---|
| | 117 | } |
|---|
| | 118 | function list_warning($show_reset=false) { |
|---|
| | 119 | return $this->_list(NOTIFICATION_TYPE_WARNING, $show_reset); |
|---|
| | 120 | } |
|---|
| | 121 | function list_notice($show_reset=false) { |
|---|
| | 122 | return $this->_list(NOTIFICATION_TYPE_NOTICE, $show_reset); |
|---|
| | 123 | } |
|---|
| | 124 | function list_all($show_reset=false) { |
|---|
| | 125 | return $this->_list("", $show_reset); |
|---|
| | 126 | } |
|---|
| | 127 | |
|---|
| | 128 | |
|---|
| | 129 | function reset($module, $id) { |
|---|
| | 130 | $module = q($module); |
|---|
| | 131 | $id = q($id); |
|---|
| | 132 | |
|---|
| | 133 | $sql = "UPDATE notifications SET reset = 1 WHERE module = $module AND id = $id"; |
|---|
| | 134 | sql($sql); |
|---|
| | 135 | } |
|---|
| | 136 | |
|---|
| | 137 | function delete($module, $id) { |
|---|
| | 138 | $module = q($module); |
|---|
| | 139 | $id = q($id); |
|---|
| | 140 | |
|---|
| | 141 | $sql = "DELETE FROM notifications WHERE module = $module AND id = $id"; |
|---|
| | 142 | sql($sql); |
|---|
| | 143 | } |
|---|
| | 144 | |
|---|
| | 145 | function safe_delete($module, $id) { |
|---|
| | 146 | $module = q($module); |
|---|
| | 147 | $id = q($id); |
|---|
| | 148 | |
|---|
| | 149 | $sql = "DELETE FROM notifications WHERE module = $module AND id = $id AND candelete = 1"; |
|---|
| | 150 | sql($sql); |
|---|
| | 151 | } |
|---|
| | 152 | |
|---|
| | 153 | /* Internal functions |
|---|
| | 154 | */ |
|---|
| | 155 | |
|---|
| | 156 | function _add_type($level, $module, $id, $display_text, $extended_text="", $link="", $reset=false, $candelete=false) { |
|---|
| | 157 | if ($this->not_loaded) { |
|---|
| | 158 | $this->notification_table = $this->_list("",true); |
|---|
| | 159 | $this->not_loaded = false; |
|---|
| | 160 | } |
|---|
| | 161 | |
|---|
| | 162 | $existing_row = false; |
|---|
| | 163 | foreach ($this->notification_table as $row) { |
|---|
| | 164 | if ($row['module'] == $module && $row['id'] == $id ) { |
|---|
| | 165 | $existing_row = $row; |
|---|
| | 166 | break; |
|---|
| | 167 | } |
|---|
| | 168 | } |
|---|
| | 169 | // Found an existing row - check if anything changed or if we are suppose to reset it |
|---|
| | 170 | // |
|---|
| | 171 | $candelete = $candelete ? 1 : 0; |
|---|
| | 172 | if ($existing_row) { |
|---|
| | 173 | |
|---|
| | 174 | if (($reset && $existing_row['reset'] == 1) || $existing_row['level'] != $level || $existing_row['display_text'] != $display_text || $existing_row['extended_text'] != $extended_text || $existing_row['link'] != $link || $existing_row['candelete'] == $candelete) { |
|---|
| | 175 | |
|---|
| | 176 | // If $reset is set to the special case of PASSIVE then the updates will not change it's value in an update |
|---|
| | 177 | // |
|---|
| | 178 | $reset_value = ($reset == 'PASSIVE') ? $existing_row['reset'] : 0; |
|---|
| | 179 | |
|---|
| | 180 | $module = q($module); |
|---|
| | 181 | $id = q($id); |
|---|
| | 182 | $level = q($level); |
|---|
| | 183 | $display_text = q($display_text); |
|---|
| | 184 | $extended_text = q($extended_text); |
|---|
| | 185 | $link = q($link); |
|---|
| | 186 | $now = time(); |
|---|
| | 187 | $sql = "UPDATE notifications SET |
|---|
| | 188 | level = $level, |
|---|
| | 189 | display_text = $display_text, |
|---|
| | 190 | extended_text = $extended_text, |
|---|
| | 191 | link = $link, |
|---|
| | 192 | reset = $reset_value, |
|---|
| | 193 | candelete = $candelete, |
|---|
| | 194 | timestamp = $now |
|---|
| | 195 | WHERE module = $module AND id = $id |
|---|
| | 196 | "; |
|---|
| | 197 | sql($sql); |
|---|
| | 198 | |
|---|
| | 199 | // TODO: I should really just add this to the internal cache, but really |
|---|
| | 200 | // how often does this get called that if is a big deal. |
|---|
| | 201 | $this->not_loaded = true; |
|---|
| | 202 | } |
|---|
| | 203 | } else { |
|---|
| | 204 | // No existing row so insert this new one |
|---|
| | 205 | // |
|---|
| | 206 | $now = time(); |
|---|
| | 207 | $module = q($module); |
|---|
| | 208 | $id = q($id); |
|---|
| | 209 | $level = q($level); |
|---|
| | 210 | $display_text = q($display_text); |
|---|
| | 211 | $extended_text = q($extended_text); |
|---|
| | 212 | $link = q($link); |
|---|
| | 213 | $sql = "INSERT INTO notifications |
|---|
| | 214 | (module, id, level, display_text, extended_text, link, reset, candelete, timestamp) |
|---|
| | 215 | VALUES |
|---|
| | 216 | ($module, $id, $level, $display_text, $extended_text, $link, 0, $candelete, $now) |
|---|
| | 217 | "; |
|---|
| | 218 | sql($sql); |
|---|
| | 219 | |
|---|
| | 220 | // TODO: I should really just add this to the internal cache, but really |
|---|
| | 221 | // how often does this get called that if is a big deal. |
|---|
| | 222 | $this->not_loaded = true; |
|---|
| | 223 | } |
|---|
| | 224 | } |
|---|
| | 225 | |
|---|
| | 226 | function _list($level, $show_reset=false) { |
|---|
| | 227 | |
|---|
| | 228 | $level = q($level); |
|---|
| | 229 | $where = array(); |
|---|
| | 230 | |
|---|
| | 231 | if (!$show_reset) { |
|---|
| | 232 | $where[] = "reset = 0"; |
|---|
| | 233 | } |
|---|
| | 234 | |
|---|
| | 235 | switch ($level) { |
|---|
| | 236 | case NOTIFICATION_TYPE_CRITICAL: |
|---|
| | 237 | case NOTIFICATION_TYPE_SECURITY: |
|---|
| | 238 | case NOTIFICATION_TYPE_UPDATE: |
|---|
| | 239 | case NOTIFICATION_TYPE_ERROR: |
|---|
| | 240 | case NOTIFICATION_TYPE_WARNING: |
|---|
| | 241 | case NOTIFICATION_TYPE_NOTICE: |
|---|
| | 242 | $where[] = "level = $level "; |
|---|
| | 243 | break; |
|---|
| | 244 | default: |
|---|
| | 245 | } |
|---|
| | 246 | $sql = "SELECT * FROM notifications "; |
|---|
| | 247 | if (count($where)) { |
|---|
| | 248 | $sql .= " WHERE ".implode(" AND ", $where); |
|---|
| | 249 | } |
|---|
| | 250 | $sql .= " ORDER BY level, module"; |
|---|
| | 251 | |
|---|
| | 252 | $list = sql($sql,"getAll",DB_FETCHMODE_ASSOC); |
|---|
| | 253 | return $list; |
|---|
| | 254 | } |
|---|
| | 255 | /* Returns the number of active notifications |
|---|
| | 256 | */ |
|---|
| | 257 | function get_num_active() { |
|---|
| | 258 | $sql = "SELECT COUNT(id) FROM notifications WHERE reset = 0"; |
|---|
| | 259 | return sql($sql,'getOne'); |
|---|
| | 260 | } |
|---|
| | 261 | } |
|---|
| | 262 | |
|---|
| | 263 | class cronmanager { |
|---|
| | 264 | /** |
|---|
| | 265 | * note: time is the hour time of day a job should run, -1 indicates don't care |
|---|
| | 266 | */ |
|---|
| | 267 | |
|---|
| | 268 | function &create(&$db) { |
|---|
| | 269 | static $obj; |
|---|
| | 270 | if (!isset($obj)) { |
|---|
| | 271 | $obj = new cronmanager($db); |
|---|
| | 272 | } |
|---|
| | 273 | return $obj; |
|---|
| | 274 | } |
|---|
| | 275 | |
|---|
| | 276 | function cronmanager(&$db) { |
|---|
| | 277 | $this->_db =& $db; |
|---|
| | 278 | } |
|---|
| | 279 | |
|---|
| | 280 | function save_email($address) { |
|---|
| | 281 | $address = q($address); |
|---|
| | 282 | sql("DELETE FROM admin WHERE variable = 'email'"); |
|---|
| | 283 | sql("INSERT INTO admin (variable, value) VALUES ('email', $address)"); |
|---|
| | 284 | } |
|---|
| | 285 | |
|---|
| | 286 | function get_email() { |
|---|
| | 287 | $sql = "SELECT value FROM admin WHERE variable = 'email'"; |
|---|
| | 288 | return sql($sql, 'getOne'); |
|---|
| | 289 | } |
|---|
| | 290 | |
|---|
| | 291 | function save_hash($id, &$string) { |
|---|
| | 292 | $hash = md5($string); |
|---|
| | 293 | $id = q($id); |
|---|
| | 294 | sql("DELETE FROM admin WHERE variable = $id"); |
|---|
| | 295 | sql("INSERT INTO admin (variable, value) VALUE ($id, '$hash')"); |
|---|
| | 296 | } |
|---|
| | 297 | |
|---|
| | 298 | function check_hash($id, &$string) { |
|---|
| | 299 | $id = q($id); |
|---|
| | 300 | $sql = "SELECT value FROM admin WHERE variable = $id LIMIT 1"; |
|---|
| | 301 | $hash = sql($sql, "getOne"); |
|---|
| | 302 | return ($hash == md5($string)); |
|---|
| | 303 | } |
|---|
| | 304 | |
|---|
| | 305 | function enable_updates($freq=24) { |
|---|
| | 306 | global $amp_conf; |
|---|
| | 307 | |
|---|
| | 308 | $night_time = array(19,20,21,22,23,0,1,2,3,4,5); |
|---|
| | 309 | $run_time = $night_time[rand(0,10)]; |
|---|
| | 310 | $command = $amp_conf['AMPBIN']."/module_admin listonline"; |
|---|
| | 311 | $lasttime = 0; |
|---|
| | 312 | |
|---|
| | 313 | $sql = "SELECT * FROM cronmanager WHERE module = 'module_admin' AND id = 'UPDATES'"; |
|---|
| | 314 | $result = sql($sql, "getAll",DB_FETCHMODE_ASSOC); |
|---|
| | 315 | if (count($result)) { |
|---|
| | 316 | $sql = "UPDATE cronmanager SET |
|---|
| | 317 | freq = '$freq', |
|---|
| | 318 | command = '$command' |
|---|
| | 319 | WHERE |
|---|
| | 320 | module = 'module_admin' AND id = 'UPDATES' |
|---|
| | 321 | "; |
|---|
| | 322 | } else { |
|---|
| | 323 | $sql = "INSERT INTO cronmanager |
|---|
| | 324 | (module, id, time, freq, lasttime, command) |
|---|
| | 325 | VALUES |
|---|
| | 326 | ('module_admin', 'UPDATES', '$run_time', $freq, 0, '$command') |
|---|
| | 327 | "; |
|---|
| | 328 | } |
|---|
| | 329 | sql($sql); |
|---|
| | 330 | } |
|---|
| | 331 | |
|---|
| | 332 | function disable_updates() { |
|---|
| | 333 | sql("DELETE FROM cronmanager WHERE module = 'module_admin' AND id = 'UPDATES'"); |
|---|
| | 334 | } |
|---|
| | 335 | |
|---|
| | 336 | function updates_enabled() { |
|---|
| | 337 | $results = sql("SELECT module, id FROM cronmanager WHERE module = 'module_admin' AND id = 'UPDATES'",'getAll'); |
|---|
| | 338 | return count($results); |
|---|
| | 339 | } |
|---|
| | 340 | |
|---|
| | 341 | /** run_jobs() |
|---|
| | 342 | * select all entries that need to be run now and run them, then update the times. |
|---|
| | 343 | * |
|---|
| | 344 | * 1. select all entries |
|---|
| | 345 | * 2. foreach entry, if its paramters indicate it should be run, then run it and |
|---|
| | 346 | * update it was run in the time stamp. |
|---|
| | 347 | */ |
|---|
| | 348 | function run_jobs() { |
|---|
| | 349 | |
|---|
| | 350 | $errors = 0; |
|---|
| | 351 | $error_arr = array(); |
|---|
| | 352 | |
|---|
| | 353 | $now = time(); |
|---|
| | 354 | $jobs = sql("SELECT * FROM cronmanager","getAll", DB_FETCHMODE_ASSOC); |
|---|
| | 355 | foreach ($jobs as $job) { |
|---|
| | 356 | $nexttime = $job['lasttime'] + $job['freq']*3600; |
|---|
| | 357 | if ($nexttime <= $now) { |
|---|
| | 358 | if ($job['time'] >= 0 && $job['time'] < 24) { |
|---|
| | 359 | $date_arr = getdate($now); |
|---|
| | 360 | // Now if lasttime is 0, then we want this kicked off at the proper hour |
|---|
| | 361 | // after wich the frequencey will set the pace for same time each night |
|---|
| | 362 | // |
|---|
| | 363 | if (($date_arr['hours'] != $job['time']) && !$job['lasttime']) { |
|---|
| | 364 | continue; |
|---|
| | 365 | } |
|---|
| | 366 | } |
|---|
| | 367 | } else { |
|---|
| | 368 | // no need to run job, time is not up yet |
|---|
| | 369 | continue; |
|---|
| | 370 | } |
|---|
| | 371 | // run the job |
|---|
| | 372 | exec($job['command'],$job_out,$ret); |
|---|
| | 373 | if ($ret) { |
|---|
| | 374 | $errors++; |
|---|
| | 375 | $error_arr[] = array($job['command'],$ret); |
|---|
| | 376 | |
|---|
| | 377 | // If there where errors, let's print them out in case the script is being debugged or running |
|---|
| | 378 | // from cron which will then put the errors out through the cron system. |
|---|
| | 379 | // |
|---|
| | 380 | foreach ($job_out as $line) { |
|---|
| | 381 | echo $line."\n"; |
|---|
| | 382 | } |
|---|
| | 383 | } else { |
|---|
| | 384 | $module = $job['module']; |
|---|
| | 385 | $id = $job['id']; |
|---|
| | 386 | $sql = "UPDATE cronmanager SET lasttime = $now WHERE module = '$module' AND id = '$id'"; |
|---|
| | 387 | sql($sql); |
|---|
| | 388 | } |
|---|
| | 389 | } |
|---|
| | 390 | if ($errors) { |
|---|
| | 391 | $nt =& notifications::create($db); |
|---|
| | 392 | $text = sprintf(_("Cronmanager encountered %s Errors"),$errors); |
|---|
| | 393 | $extext = _("The following commands failed with the listed error"); |
|---|
| | 394 | foreach ($error_arr as $item) { |
|---|
| | 395 | $extext .= "<br>".$item[0]." (".$item[1].")"; |
|---|
| | 396 | } |
|---|
| | 397 | $nt->add_error('cron_manager', 'EXECFAIL', $text, $extext, '', true, true); |
|---|
| | 398 | } |
|---|
| | 399 | } |
|---|
| | 400 | } |
|---|
| | 401 | |
|---|
| | 402 | class ampuser { |
|---|
| | 403 | var $username; |
|---|
| | 404 | var $_password; |
|---|
| | 405 | var $_extension_high; |
|---|
| | 406 | var $_extension_low; |
|---|
| | 407 | var $_deptname; |
|---|
| | 408 | var $_sections; |
|---|
| | 409 | |
|---|
| | 410 | function ampuser($username) { |
|---|
| | 411 | $this->username = $username; |
|---|
| | 412 | if ($user = getAmpUser($username)) { |
|---|
| | 413 | $this->_password = $user["password"]; |
|---|
| | 414 | $this->_extension_high = $user["extension_high"]; |
|---|
| | 415 | $this->_extension_low = $user["extension_low"]; |
|---|
| | 416 | $this->_deptname = $user["deptname"]; |
|---|
| | 417 | $this->_sections = $user["sections"]; |
|---|
| | 418 | } else { |
|---|
| | 419 | // user doesn't exist |
|---|
| | 420 | $this->_password = false; |
|---|
| | 421 | $this->_extension_high = ""; |
|---|
| | 422 | $this->_extension_low = ""; |
|---|
| | 423 | $this->_deptname = ""; |
|---|
| | 424 | $this->_sections = array(); |
|---|
| | 425 | } |
|---|
| | 426 | } |
|---|
| | 427 | |
|---|
| | 428 | /** Give this user full admin access |
|---|
| | 429 | */ |
|---|
| | 430 | function setAdmin() { |
|---|
| | 431 | $this->_extension_high = ""; |
|---|
| | 432 | $this->_extension_low = ""; |
|---|
| | 433 | $this->_deptname = ""; |
|---|
| | 434 | $this->_sections = array("*"); |
|---|
| | 435 | } |
|---|
| | 436 | |
|---|
| | 437 | function checkPassword($password) { |
|---|
| | 438 | // strict checking so false will never match |
|---|
| | 439 | return ($this->_password === $password); |
|---|
| | 440 | } |
|---|
| | 441 | |
|---|
| | 442 | function checkSection($section) { |
|---|
| | 443 | // if they have * then it means all sections |
|---|
| | 444 | return in_array("*", $this->_sections) || in_array($section, $this->_sections); |
|---|
| | 445 | } |
|---|
| | 446 | } |
|---|
| | 447 | |
|---|
| | 448 | /* Usage |
|---|
| | 449 | Grab some XML data, either from a file, URL, etc. however you want. Assume storage in $strYourXML; |
|---|
| | 450 | |
|---|
| | 451 | $xml = new xml2Array($strYourXML); |
|---|
| | 452 | xml array is in $xml->data; |
|---|
| | 453 | This is basically an array version of the XML data (no attributes), striaght-up. If there are |
|---|
| | 454 | multiple items with the same name, they are split into a numeric sub-array, |
|---|
| | 455 | eg, <items><item test="123">foo</item><item>bar</item></items> |
|---|
| | 456 | becomes: array('item' => array(0=>array('item'=>'foo'), 1=>array('item'=>'foo')) |
|---|
| | 457 | attributes are in $xml->attributes; |
|---|
| | 458 | These are stored with xpath type paths, as $xml->attributes['/items/item/0']["test"] == "123" |
|---|
| | 459 | |
|---|
| | 460 | |
|---|
| | 461 | Other way (still works, but not as nice): |
|---|
| | 462 | |
|---|
| | 463 | $objXML = new xml2Array(); |
|---|
| | 464 | $arrOutput = $objXML->parse($strYourXML); |
|---|
| | 465 | print_r($arrOutput); //print it out, or do whatever! |
|---|
| | 466 | |
|---|
| | 467 | */ |
|---|
| | 468 | |
|---|
| | 469 | class xml2Array { |
|---|
| | 470 | var $arrOutput = array(); |
|---|
| | 471 | var $resParser; |
|---|
| | 472 | var $strXmlData; |
|---|
| | 473 | |
|---|
| | 474 | var $attributes; |
|---|
| | 475 | var $data; |
|---|
| | 476 | |
|---|
| | 477 | function xml2Array($strInputXML = false) { |
|---|
| | 478 | if (!empty($strInputXML)) { |
|---|
| | 479 | $this->data = $this->parseAdvanced($strInputXML); |
|---|
| | 480 | } |
|---|
| | 481 | } |
|---|
| | 482 | |
|---|
| | 483 | function parse($strInputXML) { |
|---|
| | 484 | |
|---|
| | 485 | $this->resParser = xml_parser_create (); |
|---|
| | 486 | xml_set_object($this->resParser,$this); |
|---|
| | 487 | xml_set_element_handler($this->resParser, "tagOpen", "tagClosed"); |
|---|
| | 488 | |
|---|
| | 489 | xml_set_character_data_handler($this->resParser, "tagData"); |
|---|
| | 490 | |
|---|
| | 491 | $this->strXmlData = xml_parse($this->resParser,$strInputXML ); |
|---|
| | 492 | if(!$this->strXmlData) { |
|---|
| | 493 | die_freepbx(sprintf("XML error: %s at line %d", |
|---|
| | 494 | xml_error_string(xml_get_error_code($this->resParser)), |
|---|
| | 495 | xml_get_current_line_number($this->resParser))); |
|---|
| | 496 | } |
|---|
| | 497 | |
|---|
| | 498 | xml_parser_free($this->resParser); |
|---|
| | 499 | |
|---|
| | 500 | return $this->arrOutput; |
|---|
| | 501 | } |
|---|
| | 502 | function tagOpen($parser, $name, $attrs) { |
|---|
| | 503 | $tag=array("name"=>$name,"attrs"=>$attrs); |
|---|
| | 504 | @array_push($this->arrOutput,$tag); |
|---|
| | 505 | } |
|---|
| | 506 | |
|---|
| | 507 | function tagData($parser, $tagData) { |
|---|
| | 508 | if(trim($tagData)) { |
|---|
| | 509 | if(isset($this->arrOutput[count($this->arrOutput)-1]['tagData'])) { |
|---|
| | 510 | $this->arrOutput[count($this->arrOutput)-1]['tagData'] .= "\n".$tagData; |
|---|
| | 511 | } |
|---|
| | 512 | else { |
|---|
| | 513 | $this->arrOutput[count($this->arrOutput)-1]['tagData'] = $tagData; |
|---|
| | 514 | } |
|---|
| | 515 | } |
|---|
| | 516 | } |
|---|
| | 517 | |
|---|
| | 518 | function tagClosed($parser, $name) { |
|---|
| | 519 | @$this->arrOutput[count($this->arrOutput)-2]['children'][] = $this->arrOutput[count($this->arrOutput)-1]; |
|---|
| | 520 | array_pop($this->arrOutput); |
|---|
| | 521 | } |
|---|
| | 522 | |
|---|
| | 523 | function recursive_parseLevel($items, &$attrs, $path = "") { |
|---|
| | 524 | $array = array(); |
|---|
| | 525 | foreach (array_keys($items) as $idx) { |
|---|
| | 526 | @$items[$idx]['name'] = strtolower($items[$idx]['name']); |
|---|
| | 527 | |
|---|
| | 528 | $multi = false; |
|---|
| | 529 | if (isset($array[ $items[$idx]['name'] ])) { |
|---|
| | 530 | // this child is already set, so we're adding multiple items to an array |
|---|
| | 531 | |
|---|
| | 532 | if (!is_array($array[ $items[$idx]['name'] ]) || !isset($array[ $items[$idx]['name'] ][0])) { |
|---|
| | 533 | // hasn't already been made into a numerically-indexed array, so do that now |
|---|
| | 534 | // we're basically moving the current contents of this item into a 1-item array (at the |
|---|
| | 535 | // original location) so that we can add a second item in the code below |
|---|
| | 536 | $array[ $items[$idx]['name'] ] = array( $array[ $items[$idx]['name'] ] ); |
|---|
| | 537 | |
|---|
| | 538 | if (isset($attrs[ $path.'/'.$items[$idx]['name'] ])) { |
|---|
| | 539 | // move the attributes to /0 |
|---|
| | 540 | $attrs[ $path.'/'.$items[$idx]['name'].'/0' ] = $attrs[ $path.'/'.$items[$idx]['name'] ]; |
|---|
| | 541 | unset($attrs[ $path.'/'.$items[$idx]['name'] ]); |
|---|
| | 542 | } |
|---|
| | 543 | } |
|---|
| | 544 | $multi = true; |
|---|
| | 545 | } |
|---|
| | 546 | |
|---|
| | 547 | if ($multi) { |
|---|
| | 548 | $newitem = &$array[ $items[$idx]['name'] ][]; |
|---|
| | 549 | } else { |
|---|
| | 550 | $newitem = &$array[ $items[$idx]['name'] ]; |
|---|
| | 551 | } |
|---|
| | 552 | |
|---|
| | 553 | |
|---|
| | 554 | if (isset($items[$idx]['children']) && is_array($items[$idx]['children'])) { |
|---|
| | 555 | $newitem = $this->recursive_parseLevel($items[$idx]['children'], $attrs, $path.'/'.$items[$idx]['name']); |
|---|
| | 556 | } else if (isset($items[$idx]['tagData'])) { |
|---|
| | 557 | $newitem = $items[$idx]['tagData']; |
|---|
| | 558 | } else { |
|---|
| | 559 | $newitem = false; |
|---|
| | 560 | } |
|---|
| | 561 | |
|---|
| | 562 | if (isset($items[$idx]['attrs']) && is_array($items[$idx]['attrs']) && count($items[$idx]['attrs'])) { |
|---|
| | 563 | $attrpath = $path.'/'.$items[$idx]['name']; |
|---|
| | 564 | if ($multi) { |
|---|
| | 565 | $attrpath .= '/'.(count($array[ $items[$idx]['name'] ])-1); |
|---|
| | 566 | } |
|---|
| | 567 | foreach ($items[$idx]['attrs'] as $name=>$value) { |
|---|
| | 568 | $attrs[ $attrpath ][ strtolower($name) ] = $value; |
|---|
| | 569 | } |
|---|
| | 570 | } |
|---|
| | 571 | } |
|---|
| | 572 | return $array; |
|---|
| | 573 | } |
|---|
| | 574 | |
|---|
| | 575 | function parseAdvanced($strInputXML) { |
|---|
| | 576 | $array = $this->parse($strInputXML); |
|---|
| | 577 | $this->attributes = array(); |
|---|
| | 578 | return $this->data = $this->recursive_parseLevel($array, $this->attributes); |
|---|
| | 579 | } |
|---|
| | 580 | } |
|---|
| | 581 | |
|---|
| | 582 | /* |
|---|
| | 583 | Return a much more manageable assoc array with module data. |
|---|
| | 584 | */ |
|---|
| | 585 | class xml2ModuleArray extends xml2Array { |
|---|
| | 586 | function parseModulesXML($strInputXML) { |
|---|
| | 587 | $array = $this->parseAdvanced($strInputXML); |
|---|
| | 588 | if (isset($array['xml'])) { |
|---|
| | 589 | foreach ($array['xml'] as $key=>$module) { |
|---|
| | 590 | if ($key == 'module') { |
|---|
| | 591 | // copy the structure verbatim |
|---|
| | 592 | $modules[ $module['name'] ] = $module; |
|---|
| | 593 | } |
|---|
| | 594 | } |
|---|
| | 595 | } |
|---|
| | 596 | |
|---|
| | 597 | // if you are confused about what's happening below, uncomment this why we do it |
|---|
| | 598 | // echo "<pre>"; print_r($arrOutput); echo "</pre>"; |
|---|
| | 599 | |
|---|
| | 600 | // ignore the regular xml garbage ([0]['children']) & loop through each module |
|---|
| | 601 | if(!is_array($arrOutput[0]['children'])) return false; |
|---|
| | 602 | foreach($arrOutput[0]['children'] as $module) { |
|---|
| | 603 | if(!is_array($module['children'])) return false; |
|---|
| | 604 | // loop through each modules's tags |
|---|
| | 605 | foreach($module['children'] as $modTags) { |
|---|
| | 606 | if(isset($modTags['children']) && is_array($modTags['children'])) { |
|---|
| | 607 | $$modTags['name'] = $modTags['children']; |
|---|
| | 608 | // loop if there are children (menuitems and requirements) |
|---|
| | 609 | foreach($modTags['children'] as $subTag) { |
|---|
| | 610 | $subTags[strtolower($subTag['name'])] = $subTag['tagData']; |
|---|
| | 611 | } |
|---|
| | 612 | $$modTags['name'] = $subTags; |
|---|
| | 613 | unset($subTags); |
|---|
| | 614 | } else { |
|---|
| | 615 | // create a variable for each tag we find |
|---|
| | 616 | $$modTags['name'] = $modTags['tagData']; |
|---|
| | 617 | } |
|---|
| | 618 | |
|---|
| | 619 | } |
|---|
| | 620 | // now build our return array |
|---|
| | 621 | $arrModules[$RAWNAME]['rawname'] = $RAWNAME; // This has to be set |
|---|
| | 622 | $arrModules[$RAWNAME]['displayName'] = $NAME; // This has to be set |
|---|
| | 623 | $arrModules[$RAWNAME]['version'] = $VERSION; // This has to be set |
|---|
| | 624 | $arrModules[$RAWNAME]['type'] = isset($TYPE)?$TYPE:'setup'; |
|---|
| | 625 | $arrModules[$RAWNAME]['category'] = isset($CATEGORY)?$CATEGORY:'Unknown'; |
|---|
| | 626 | $arrModules[$RAWNAME]['info'] = isset($INFO)?$INFO:'http://www.freepbx.org/wiki/'.$RAWNAME; |
|---|
| | 627 | $arrModules[$RAWNAME]['location'] = isset($LOCATION)?$LOCATION:'local'; |
|---|
| | 628 | $arrModules[$RAWNAME]['items'] = isset($MENUITEMS)?$MENUITEMS:null; |
|---|
| | 629 | $arrModules[$RAWNAME]['requirements'] = isset($REQUIREMENTS)?$REQUIREMENTS:null; |
|---|
| | 630 | $arrModules[$RAWNAME]['md5sum'] = isset($MD5SUM)?$MD5SUM:null; |
|---|
| | 631 | //print_r($arrModules); |
|---|
| | 632 | //unset our variables |
|---|
| | 633 | unset($NAME); |
|---|
| | 634 | unset($VERSION); |
|---|
| | 635 | unset($TYPE); |
|---|
| | 636 | unset($CATEGORY); |
|---|
| | 637 | unset($AUTHOR); |
|---|
| | 638 | unset($EMAIL); |
|---|
| | 639 | unset($LOCATION); |
|---|
| | 640 | unset($MENUITEMS); |
|---|
| | 641 | unset($REQUIREMENTS); |
|---|
| | 642 | unset($MD5SUM); |
|---|
| | 643 | } |
|---|
| | 644 | //echo "<pre>"; print_r($arrModules); echo "</pre>"; |
|---|
| | 645 | |
|---|
| | 646 | return $arrModules; |
|---|
| | 647 | } |
|---|
| | 648 | } |
|---|
| | 649 | |
|---|
| | 650 | class moduleHook { |
|---|
| | 651 | var $hookHtml = ''; |
|---|
| | 652 | var $arrHooks = array(); |
|---|
| | 653 | |
|---|
| | 654 | function install_hooks($viewing_itemid,$target_module,$target_menuid = '') { |
|---|
| | 655 | global $active_modules; |
|---|
| | 656 | // loop through all active modules |
|---|
| | 657 | foreach($active_modules as $this_module) { |
|---|
| | 658 | // look for requested hooks for $module |
|---|
| | 659 | // ie: findme_hook_extensions() |
|---|
| | 660 | $funct = $this_module['rawname'] . '_hook_' . $target_module; |
|---|
| | 661 | if( function_exists( $funct ) ) { |
|---|
| | 662 | // execute the function, appending the |
|---|
| | 663 | // html output to that of other hooking modules |
|---|
| | 664 | if ($hookReturn = $funct($target_menuid, $viewing_itemid)) { |
|---|
| | 665 | $this->hookHtml .= $hookReturn; |
|---|
| | 666 | } |
|---|
| | 667 | // remember who installed hooks |
|---|
| | 668 | // we need to know this for processing form vars |
|---|
| | 669 | $this->arrHooks[] = $this_module['rawname']; |
|---|
| | 670 | } |
|---|
| | 671 | } |
|---|
| | 672 | } |
|---|
| | 673 | |
|---|
| | 674 | // process the request from the module we hooked |
|---|
| | 675 | function process_hooks($viewing_itemid, $target_module, $target_menuid, $request) { |
|---|
| | 676 | if(is_array($this->arrHooks)) { |
|---|
| | 677 | foreach($this->arrHooks as $hookingMod) { |
|---|
| | 678 | // check if there is a processing function |
|---|
| | 679 | $funct = $hookingMod . '_hookProcess_' . $target_module; |
|---|
| | 680 | if( function_exists( $funct ) ) { |
|---|
| | 681 | $funct($viewing_itemid, $request); |
|---|
| | 682 | } |
|---|
| | 683 | } |
|---|
| | 684 | } |
|---|
| | 685 | } |
|---|
| | 686 | } |
|---|