| 1 |
<?php |
|---|
| 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 |
function required_param($varname, $options=PARAM_CLEAN) { |
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
global $CFG; |
|---|
| 41 |
if (!empty($CFG->detect_unchecked_vars)) { |
|---|
| 42 |
global $UNCHECKED_VARS; |
|---|
| 43 |
unset ($UNCHECKED_VARS->vars[$varname]); |
|---|
| 44 |
} |
|---|
| 45 |
|
|---|
| 46 |
if (isset($_POST[$varname])) { |
|---|
| 47 |
$param = $_POST[$varname]; |
|---|
| 48 |
} else if (isset($_GET[$varname])) { |
|---|
| 49 |
$param = $_GET[$varname]; |
|---|
| 50 |
} else { |
|---|
| 51 |
error('A required parameter ('.$varname.') was missing'); |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
return clean_param($param, $options); |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
function optional_param($varname, $default=NULL, $options=PARAM_CLEAN) { |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
global $CFG; |
|---|
| 75 |
if (!empty($CFG->detect_unchecked_vars)) { |
|---|
| 76 |
global $UNCHECKED_VARS; |
|---|
| 77 |
unset ($UNCHECKED_VARS->vars[$varname]); |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
if (isset($_POST[$varname])) { |
|---|
| 81 |
$param = $_POST[$varname]; |
|---|
| 82 |
} else if (isset($_GET[$varname])) { |
|---|
| 83 |
$param = $_GET[$varname]; |
|---|
| 84 |
} else { |
|---|
| 85 |
return $default; |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
return clean_param($param, $options); |
|---|
| 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 |
function clean_param($param, $options) { |
|---|
| 123 |
|
|---|
| 124 |
global $CFG; |
|---|
| 125 |
|
|---|
| 126 |
if (is_array($param)) { |
|---|
| 127 |
$newparam = array(); |
|---|
| 128 |
foreach ($param as $key => $value) { |
|---|
| 129 |
$newparam[$key] = clean_param($value, $options); |
|---|
| 130 |
} |
|---|
| 131 |
return $newparam; |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
if (!$options) { |
|---|
| 135 |
return $param; |
|---|
| 136 |
} |
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
//if ((string)$param == (string)(int)$param) { // It's just an integer |
|---|
| 140 |
// return (int)$param; |
|---|
| 141 |
//} |
|---|
| 142 |
|
|---|
| 143 |
if ($options & PARAM_CLEAN) { |
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
$param = clean_text($param); |
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
} |
|---|
| 150 |
|
|---|
| 151 |
$options & PARAM_INT) { |
|---|
| 152 |
$param = (int)$param; |
|---|
| 153 |
} |
|---|
| 154 |
|
|---|
| 155 |
$options & PARAM_ALPHA) { |
|---|
| 156 |
$param = eregi_replace('[^a-zA-Z]', '', $param); |
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
$options & PARAM_ALPHANUM) { |
|---|
| 160 |
$param = eregi_replace('[^A-Za-z0-9]', '', $param); |
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
$options & PARAM_ALPHAEXT) { |
|---|
| 164 |
$param = eregi_replace('[^a-zA-Z/_-]', '', $param); |
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
$options & PARAM_BOOL) { |
|---|
| 168 |
$tempstr = strtolower($param); |
|---|
| 169 |
$tempstr == 'on') { |
|---|
| 170 |
$param = 1; |
|---|
| 171 |
$tempstr == 'off') { |
|---|
| 172 |
$param = 0; |
|---|
| 173 |
|
|---|
| 174 |
$param = empty($param) ? 0 : 1; |
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
$options & PARAM_NOTAGS) { |
|---|
| 179 |
$param = strip_tags($param); |
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
$options & PARAM_SAFEDIR) { |
|---|
| 183 |
$param = eregi_replace('[^a-zA-Z0-9_-]', '', $param); |
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
$options & PARAM_CLEANFILE) { |
|---|
| 187 |
$param = clean_filename($param); |
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
$options & PARAM_FILE) { |
|---|
| 191 |
$param = ereg_replace('[[:cntrl:]]|[<>"`\|\':\\/]', '', $param); |
|---|
| 192 |
$param = ereg_replace('\.\.+', '', $param); |
|---|
| 193 |
$param == '.') { |
|---|
| 194 |
$param = ''; |
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
$options & PARAM_PATH) { |
|---|
| 199 |
$param = str_replace('\\\'', '\'', $param); |
|---|
| 200 |
$param = str_replace('\\"', '"', $param); |
|---|
| 201 |
$param = str_replace('\\', '/', $param); |
|---|
| 202 |
$param = ereg_replace('[[:cntrl:]]|[<>"`\|\':]', '', $param); |
|---|
| 203 |
$param = ereg_replace('\.\.+', '', $param); |
|---|
| 204 |
$param = ereg_replace('//+', '/', $param); |
|---|
| 205 |
$param = ereg_replace('/(\./)+', '/', $param); |
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
$options & PARAM_HOST) { |
|---|
| 209 |
preg_replace('/[^\.\d\w-]/','', $param ); |
|---|
| 210 |
|
|---|
| 211 |
if (preg_match('/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/',$param, $match)){ |
|---|
| 212 |
|
|---|
| 213 |
if ( $match[0] > 255 |
|---|
| 214 |
|| $match[1] > 255 |
|---|
| 215 |
|| $match[3] > 255 |
|---|
| 216 |
|| $match[4] > 255 ) { |
|---|
| 217 |
|
|---|
| 218 |
$param = ''; |
|---|
| 219 |
|
|---|
| 220 |
preg_match('/^[\w\d\.-]+$/', $param) |
|---|
| 221 |
&& !preg_match('/^[\.-]/', $param) |
|---|
| 222 |
&& !preg_match('/[\.-]$/', $param) |
|---|
| 223 |
) { |
|---|
| 224 |
|
|---|
| 225 |
} else { |
|---|
| 226 |
|
|---|
| 227 |
$param=''; |
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
$options & PARAM_URL) { |
|---|
| 232 |
|
|---|
| 233 |
include_once($CFG->dirroot . 'lib/validateurlsyntax.php'); |
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
if (!empty($param) && validateUrlSyntax($param, 's?H?S?F?E?u-P-a?I?p-f?q?r?')) { |
|---|
| 253 |
|
|---|
| 254 |
} else { |
|---|
| 255 |
$param =''; |
|---|
| 256 |
} |
|---|
| 257 |
$options ^= PARAM_URL; |
|---|
| 258 |
} |
|---|
| 259 |
|
|---|
| 260 |
$options & PARAM_LOCALURL) { |
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
if (!empty($param)) { |
|---|
| 264 |
preg_match(':^/:', $param)) { |
|---|
| 265 |
|
|---|
| 266 |
} elseif (preg_match('/^'.preg_quote($CFG->wwwroot, '/').'/i',$param)) { |
|---|
| 267 |
|
|---|
| 268 |
} else { |
|---|
| 269 |
|
|---|
| 270 |
if (validateUrlSyntax($param, 's-u-P-a-p-f+q?r?')) { |
|---|
| 271 |
|
|---|
| 272 |
} else { |
|---|
| 273 |
$param = ''; |
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
$options & PARAM_CLEANHTML) { |
|---|
| 280 |
|
|---|
| 281 |
$param = clean_text($param); |
|---|
| 282 |
|
|---|
| 283 |
} |
|---|
| 284 |
|
|---|
| 285 |
$param; |
|---|
| 286 |
|
|---|
| 287 |
|
|---|
| 288 |
|
|---|
| 289 |
|
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 |
|
|---|
| 293 |
|
|---|
| 294 |
|
|---|
| 295 |
|
|---|
| 296 |
|
|---|
| 297 |
function get_list_of_plugins($plugin='mod', $exclude='') { |
|---|
| 298 |
|
|---|
| 299 |
global $CFG; |
|---|
| 300 |
if ($plugin == 'mod') { |
|---|
| 301 |
$plugin = $CFG->dirroot . $plugin; |
|---|
| 302 |
} |
|---|
| 303 |
static $plugincache = array(); |
|---|
| 304 |
$plugincachename = $plugin . "_" . $exclude; |
|---|
| 305 |
|
|---|
| 306 |
if (isset($plugincache[$plugincachename])) { |
|---|
| 307 |
$plugins = $plugincache[$plugincachename]; |
|---|
| 308 |
} else { |
|---|
| 309 |
$plugins = array(); |
|---|
| 310 |
if ($basedir = opendir($plugin)) { |
|---|
| 311 |
while (false !== ($dir = readdir($basedir))) { |
|---|
| 312 |
$firstchar = substr($dir, 0, 1); |
|---|
| 313 |
if ($firstchar == '.' or $dir == 'CVS' or $dir == '_vti_cnf' or $dir == $exclude) { |
|---|
| 314 |
continue; |
|---|
| 315 |
} |
|---|
| 316 |
if ((filetype($plugin .'/'. $dir) != 'dir') && ((filetype($plugin .'/'. $dir) != 'link'))) { |
|---|
| 317 |
continue; |
|---|
| 318 |
} |
|---|
| 319 |
$plugins[] = $dir; |
|---|
| 320 |
} |
|---|
| 321 |
} |
|---|
| 322 |
if ($plugins) { |
|---|
| 323 |
asort($plugins); |
|---|
| 324 |
} |
|---|
| 325 |
$plugincache[$plugincachename] = $plugins; |
|---|
| 326 |
} |
|---|
| 327 |
return $plugins; |
|---|
| 328 |
} |
|---|
| 329 |
|
|---|
| 330 |
|
|---|
| 331 |
|
|---|
| 332 |
function listen_for_event($object_type, $event, $function) { |
|---|
| 333 |
|
|---|
| 334 |
global $CFG; |
|---|
| 335 |
$CFG->event_hooks[$object_type][$event][] = $function; |
|---|
| 336 |
|
|---|
| 337 |
} |
|---|
| 338 |
|
|---|
| 339 |
function plugin_hook($object_type,$event,$object = null) { |
|---|
| 340 |
|
|---|
| 341 |
global $CFG; |
|---|
| 342 |
|
|---|
| 343 |
if (!empty($CFG->event_hooks['all']['all']) && is_array($CFG->event_hooks['all']['all'])) { |
|---|
| 344 |
foreach($CFG->event_hooks['all']['all'] as $hook) { |
|---|
| 345 |
$object = $hook($object_type,$event,$object); |
|---|
| 346 |
} |
|---|
| 347 |
} |
|---|
| 348 |
if (!empty($CFG->event_hooks[$object_type]['all']) && is_array($CFG->event_hooks[$object_type]['all'])) { |
|---|
| 349 |
foreach($CFG->event_hooks[$object_type]['all'] as $hook) { |
|---|
| 350 |
$object = $hook($object_type,$event,$object); |
|---|
| 351 |
} |
|---|
| 352 |
} |
|---|
| 353 |
if (!empty($CFG->event_hooks['all'][$event]) && is_array($CFG->event_hooks['all'][$event])) { |
|---|
| 354 |
foreach($CFG->event_hooks['all'][$event] as $hook) { |
|---|
| 355 |
$object = $hook($object_type,$event,$object); |
|---|
| 356 |
} |
|---|
| 357 |
} |
|---|
| 358 |
if (!empty($CFG->event_hooks[$object_type][$event]) && is_array($CFG->event_hooks[$object_type][$event])) { |
|---|
| 359 |
foreach($CFG->event_hooks[$object_type][$event] as $hook) { |
|---|
| 360 |
$object = $hook($object_type,$event,$object); |
|---|
| 361 |
} |
|---|
| 362 |
} |
|---|
| 363 |
|
|---|
| 364 |
return $object; |
|---|
| 365 |
|
|---|
| 366 |
} |
|---|
| 367 |
|
|---|
| 368 |
function report_session_error() { |
|---|
| 369 |
global $CFG, $FULLME; |
|---|
| 370 |
|
|---|
| 371 |
|
|---|
| 372 |
setcookie('ElggSession'.$CFG->sessioncookie, '', time() - 3600, $CFG->cookiepath); |
|---|
| 373 |
setcookie('ElggSessionTest'.$CFG->sessioncookie, '', time() - 3600, $CFG->cookiepath); |
|---|
| 374 |
|
|---|
| 375 |
//if (isset($CFG->session_error_counter)) { |
|---|
| 376 |
// set_config('session_error_counter', 1 + $CFG->session_error_counter); |
|---|
| 377 |
//} else { |
|---|
| 378 |
// set_config('session_error_counter', 1); |
|---|
| 379 |
//} |
|---|
| 380 |
//called from setup.php, so gettext module hasn't been loaded yet |
|---|
| 381 |
redirect($FULLME, '', 1); |
|---|
| 382 |
} |
|---|
| 383 |
|
|---|
| 384 |
|
|---|
| 385 |
|
|---|
| 386 |
|
|---|
| 387 |
|
|---|
| 388 |
|
|---|
| 389 |
|
|---|
| 390 |
|
|---|
| 391 |
|
|---|
| 392 |
|
|---|
| 393 |
|
|---|
| 394 |
|
|---|
| 395 |
|
|---|
| 396 |
|
|---|
| 397 |
|
|---|
| 398 |
|
|---|
| 399 |
|
|---|
| 400 |
|
|---|
| 401 |
|
|---|
| 402 |
|
|---|
| 403 |
|
|---|
| 404 |
|
|---|
| 405 |
|
|---|
| 406 |
|
|---|
| 407 |
|
|---|
| 408 |
|
|---|
| 409 |
|
|---|
| 410 |
|
|---|
| 411 |
|
|---|
| 412 |
|
|---|
| 413 |
|
|---|
| 414 |
|
|---|
| 415 |
|
|---|
| 416 |
|
|---|
| 417 |
|
|---|
| 418 |
|
|---|
| 419 |
|
|---|
| 420 |
function sesskey() { |
|---|
| 421 |
global $USER; |
|---|
| 422 |
|
|---|
| 423 |
if(!isset($USER)) { |
|---|
| 424 |
return false; |
|---|
| 425 |
} |
|---|
| 426 |
|
|---|
| 427 |
if (empty($USER->sesskey)) { |
|---|
| 428 |
$USER->sesskey = random_string(10); |
|---|
| 429 |
} |
|---|
| 430 |
|
|---|
| 431 |
return $USER->sesskey; |
|---|
| 432 |
} |
|---|
| 433 |
|
|---|
| 434 |
|
|---|
| 435 |
|
|---|
| 436 |
|
|---|
| 437 |
|
|---|
| 438 |
|
|---|
| 439 |
|
|---|
| 440 |
|
|---|
| 441 |
|
|---|
| 442 |
|
|---|
| 443 |
|
|---|
| 444 |
|
|---|
| 445 |
|
|---|
| 446 |
|
|---|
| 447 |
|
|---|
| 448 |
|
|---|
| 449 |
|
|---|
| 450 |
|
|---|
| 451 |
function email_to_user($user, $from, $subject, $messagetext, $messagehtml='', $attachment='', $attachname='', $usetrueaddress=true, $replyto='', $replytoname='') { |
|---|
| 452 |
|
|---|
| 453 |
global $CFG; |
|---|
| 454 |
$textlib = textlib_get_instance(); |
|---|
| 455 |
|
|---|
| 456 |
include_once($CFG->libdir .'/phpmailer/class.phpmailer.php'); |
|---|
| 457 |
|
|---|
| 458 |
if (empty($user) || empty($user->email)) { |
|---|
| 459 |
return false; |
|---|
| 460 |
} |
|---|
| 461 |
|
|---|
| 462 |
|
|---|
| 463 |
if (over_bounce_threshold($user)) { |
|---|
| 464 |
error_log("User $user->id (".fullname($user).") is over bounce threshold! Not sending."); |
|---|
| 465 |
return false; |
|---|
| 466 |
} |
|---|
| 467 |
*/ // this doesn't exist right now, we may bring it in later though. |
|---|
| 468 |
|
|---|
| 469 |
$mail = new phpmailer; |
|---|
| 470 |
|
|---|
| 471 |
$mail->Version = 'Elgg '; |
|---|
| 472 |
$mail->PluginDir = $CFG->libdir .'/phpmailer/'; |
|---|
| 473 |
|
|---|
| 474 |
|
|---|
| 475 |
$mail->CharSet = 'UTF-8'; |
|---|
| 476 |
|
|---|
| 477 |
if (empty($CFG->smtphosts)) { |
|---|
| 478 |
$mail->IsMail(); |
|---|
| 479 |
} else if ($CFG->smtphosts == 'qmail') { |
|---|
| 480 |
$mail->IsQmail(); |
|---|
| 481 |
} else { |
|---|
| 482 |
$mail->IsSMTP(); |
|---|
| 483 |
if ($CFG->debug > 7) { |
|---|
| 484 |
echo '<pre>' . "\n"; |
|---|
| 485 |
$mail->SMTPDebug = true; |
|---|
| 486 |
} |
|---|
| 487 |
$mail->Host = $CFG->smtphosts; |
|---|
| 488 |
|
|---|
| 489 |
if ($CFG->smtpuser) { |
|---|
| 490 |
$mail->SMTPAuth = true; |
|---|
| 491 |
$mail->Username = $CFG->smtpuser; |
|---|
| 492 |
$mail->Password = $CFG->smtppass; |
|---|
| 493 |
} |
|---|
| 494 |
} |
|---|
| 495 |
|
|---|
| 496 |
|
|---|
| 497 |
// make up an email address for handling bounces |
|---|
| 498 |
if (!empty($CFG->handlebounces)) { |
|---|
| 499 |
$modargs = 'B'.base64_encode(pack('V',$user->ident)).substr(md5($user->email),0,16); |
|---|
| 500 |
$mail->Sender = generate_email_processing_address(0,$modargs); |
|---|
| 501 |
} |
|---|
| 502 |
else { |
|---|
| 503 |
$mail->Sender = $CFG->sysadminemail; |
|---|
| 504 |
} |
|---|
| 505 |
*/ |
|---|
| 506 |
$mail->Sender = $CFG->sysadminemail; |
|---|
| 507 |
|
|---|
| 508 |
// TODO add a preference for maildisplay |
|---|
| 509 |
if (is_string($from)) { |
|---|
| 510 |
$mail->From = $CFG->noreplyaddress; |
|---|
| 511 |
$mail->FromName = $from; |
|---|
| 512 |
} else if (empty($from)) { |
|---|
| 513 |
$mail->From = $CFG->sysadminemail; |
|---|
| 514 |
$mail->FromName = $CFG->sitename.' '.__gettext('Administrator'); |
|---|
| 515 |
} else if ($usetrueaddress and !empty($from->maildisplay)) { |
|---|
| 516 |
$mail->From = $from->email; |
|---|
| 517 |
$mail->FromName = $from->name; |
|---|
| 518 |
} else { |
|---|
| 519 |
$mail->From = $CFG->noreplyaddress; |
|---|
| 520 |
$mail->FromName = $from->name; |
|---|
| 521 |
if (empty($replyto)) { |
|---|
| 522 |
$mail->AddReplyTo($CFG->noreplyaddress,__gettext('Do not reply')); |
|---|
| 523 |
} |
|---|
| 524 |
} |
|---|
| 525 |
|
|---|
| 526 |
if (!empty($replyto)) { |
|---|
| 527 |
$mail->AddReplyTo($replyto,$replytoname); |
|---|
| 528 |
} |
|---|
| 529 |
|
|---|
| 530 |
$mail->Subject = $textlib->substr(stripslashes($subject), 0, 900); |
|---|
| 531 |
|
|---|
| 532 |
$mail->AddAddress($user->email, $user->name); |
|---|
| 533 |
|
|---|
| 534 |
$mail->WordWrap = 79; |
|---|
| 535 |
|
|---|
| 536 |
if (!empty($from->customheaders)) { |
|---|
| 537 |
if (is_array($from->customheaders)) { |
|---|
| 538 |
foreach ($from->customheaders as $customheader) { |
|---|
| 539 |
$mail->AddCustomHeader($customheader); |
|---|
| 540 |
} |
|---|
| 541 |
} else { |
|---|
| 542 |
$mail->AddCustomHeader($from->customheaders); |
|---|
| 543 |
} |
|---|
| 544 |
} |
|---|
| 545 |
|
|---|
| 546 |
if (!empty($from->priority)) { |
|---|
| 547 |
$mail->Priority = $from->priority; |
|---|
| 548 |
} |
|---|
| 549 |
|
|---|
| 550 |
|
|---|
| 551 |
$user->mailformat = 0; |
|---|
| 552 |
if ($messagehtml && $user->mailformat == 1) { |
|---|
| 553 |
$mail->IsHTML(true); |
|---|
| 554 |
$mail->Encoding = 'quoted-printable'; |
|---|
| 555 |
$mail->Body = $messagehtml; |
|---|
| 556 |
$mail->AltBody = "\n$messagetext\n"; |
|---|
| 557 |
} else { |
|---|
| 558 |
$mail->IsHTML(false); |
|---|
| 559 |
$mail->Body = "\n$messagetext\n"; |
|---|
| 560 |
} |
|---|
| 561 |
|
|---|
| 562 |
if ($attachment && $attachname) { |
|---|
| 563 |
if (ereg( "\\.\\." ,$attachment )) { |
|---|
| 564 |
$mail->AddAddress($CFG->sysadminemail,$CFG->sitename.' '.__gettext('Administrator')); |
|---|
| 565 |
$mail->AddStringAttachment('Error in attachment. User attempted to attach a filename with a unsafe name.', 'error.txt', '8bit', 'text/plain'); |
|---|
| 566 |
} else { |
|---|
| 567 |
require_once($CFG->libdir.'/filelib.php'); |
|---|
| 568 |
$mimetype = mimeinfo('type', $attachname); |
|---|
| 569 |
$mail->AddAttachment($attachment, $attachname, 'base64', $mimetype); |
|---|
| 570 |
} |
|---|
| 571 |
} |
|---|
| 572 |
|
|---|
| 573 |
$mail = plugin_hook("mail","create",$mail); |
|---|
| 574 |
|
|---|
| 575 |
if ($mail->Send()) { |
|---|
| 576 |
|
|---|
| 577 |
return true; |
|---|
| 578 |
} else { |
|---|
| 579 |
mtrace('ERROR: '. $mail->ErrorInfo); |
|---|
| 580 |
return false; |
|---|
| 581 |
} |
|---|
| 582 |
} |
|---|
| 583 |
|
|---|
| 584 |
|
|---|
| 585 |
|
|---|
| 586 |
|
|---|
| 587 |
|
|---|
| 588 |
|
|---|
| 589 |
|
|---|
| 590 |
|
|---|
| 591 |
|
|---|
| 592 |
|
|---|
| 593 |
|
|---|
| 594 |
|
|---|
| 595 |
|
|---|
| 596 |
|
|---|
| 597 |
|
|---|
| 598 |
|
|---|
| 599 |
|
|---|
| 600 |
|
|---|
| 601 |
function get_directory_list($rootdir, $excludefile='', $descend=true, $getdirs=false, $getfiles=true) { |
|---|
| 602 |
|
|---|
| 603 |
$dirs = array(); |
|---|
| 604 |
|
|---|
| 605 |
if (!$getdirs and !$getfiles) { |
|---|
| 606 |
return $dirs; |
|---|
| 607 |
} |
|---|
| 608 |
|
|---|
| 609 |
if (!is_dir($rootdir)) { |
|---|
| 610 |
return $dirs; |
|---|
| 611 |
} |
|---|
| 612 |
|
|---|
| 613 |
if (!$dir = opendir($rootdir)) { |
|---|
| 614 |
return $dirs; |
|---|
| 615 |
} |
|---|
| 616 |
|
|---|
| 617 |
while (false !== ($file = readdir($dir))) { |
|---|
| 618 |
$firstchar = substr($file, 0, 1); |
|---|
| 619 |
if ($firstchar == '.' or $file == 'CVS' or $file == $excludefile) { |
|---|
| 620 |
continue; |
|---|
| 621 |
} |
|---|
| 622 |
$fullfile = $rootdir .'/'. $file; |
|---|
| 623 |
if (filetype($fullfile) == 'dir') { |
|---|
| 624 |
if ($getdirs) { |
|---|
| 625 |
$dirs[] = $file; |
|---|
| 626 |
} |
|---|
| 627 |
if ($descend) { |
|---|
| 628 |
$subdirs = get_directory_list($fullfile, $excludefile, $descend, $getdirs, $getfiles); |
|---|
| 629 |
foreach ($subdirs as $subdir) { |
|---|
| 630 |
$dirs[] = $file .'/'. $subdir; |
|---|
| 631 |
} |
|---|
| 632 |
} |
|---|
| 633 |
} else if ($getfiles) { |
|---|
| 634 |
$dirs[] = $file; |
|---|
| 635 |
} |
|---|
| 636 |
} |
|---|
| 637 |
closedir($dir); |
|---|
| 638 |
|
|---|
| 639 |
asort($dirs); |
|---|
| 640 |
|
|---|
| 641 |
return $dirs; |
|---|
| 642 |
} |
|---|
| 643 |
|
|---|
| 644 |
|
|---|
| 645 |
|
|---|
| 646 |
|
|---|
| 647 |
|
|---|
| 648 |
|
|---|
| 649 |
function resolve_filename_collisions($destination,$files,$format='%s_%d.%s') { |
|---|
| 650 |
foreach ($files as $k => $f) { |
|---|
| 651 |
if (check_potential_filename($destination,$f,$files)) { |
|---|
| 652 |
$bits = explode('.', $f); |
|---|
| 653 |
for ($i = 1; true; $i++) { |
|---|
| 654 |
$try = sprintf($format, $bits[0], $i, $bits[1]); |
|---|
| 655 |
if (!check_potential_filename($destination,$try,$files)) { |
|---|
| 656 |
$files[$k] = $try; |
|---|
| 657 |
break; |
|---|
| 658 |
} |
|---|
| 659 |
} |
|---|
| 660 |
} |
|---|
| 661 |
} |
|---|
| 662 |
return $files; |
|---|
| 663 |
} |
|---|
| 664 |
|
|---|
| 665 |
|
|---|
| 666 |
|
|---|
| 667 |
|
|---|
| 668 |
function check_potential_filename($destination,$filename,$files) { |
|---|
| 669 |
if (file_exists($destination.'/'.$filename)) { |
|---|
| 670 |
return true; |
|---|
| 671 |
} |
|---|
| 672 |
if (count(array_keys($files,$filename)) > 1) { |
|---|
| 673 |
return true; |
|---|
| 674 |
} |
|---|
| 675 |
return false; |
|---|
| 676 |
} |
|---|
| 677 |
|
|---|
| 678 |
|
|---|
| 679 |
|
|---|
| 680 |
|
|---|
| 681 |
|
|---|
| 682 |
|
|---|
| 683 |
|
|---|
| 684 |
|
|---|
| 685 |
|
|---|
| 686 |
function get_directory_size($rootdir, $excludefile='') { |
|---|
| 687 |
|
|---|
| 688 |
global $CFG; |
|---|
| 689 |
$textlib = textlib_get_instance(); |
|---|
| 690 |
|
|---|
| 691 |
|
|---|
| 692 |
if (!empty($CFG->pathtodu) && is_executable(trim($CFG->pathtodu))) { |
|---|
| 693 |
$command = trim($CFG->pathtodu).' -sk --apparent-size '.escapeshellarg($rootdir); |
|---|
| 694 |
exec($command,$output,$return); |
|---|
| 695 |
if (is_array($output)) { |
|---|
| 696 |
return get_real_size(intval($output[0]).'k'); |
|---|
| 697 |
} |
|---|
| 698 |
} |
|---|
| 699 |
|
|---|
| 700 |
$size = 0; |
|---|
| 701 |
|
|---|
| 702 |
if (!is_dir($rootdir)) { |
|---|
| 703 |
return $dirs; |
|---|
| 704 |
} |
|---|
| 705 |
|
|---|
| 706 |
if (!$dir = @opendir($rootdir)) { |
|---|
| 707 |
return $dirs; |
|---|
| 708 |
} |
|---|
| 709 |
|
|---|
| 710 |
while (false !== ($file = readdir($dir))) { |
|---|
| 711 |
$firstchar = $textlib->substr($file, 0, 1); |
|---|
| 712 |
if ($firstchar == '.' or $file == 'CVS' or $file == $excludefile) { |
|---|
| 713 |
continue; |
|---|
| 714 |
} |
|---|
| 715 |
$fullfile = $rootdir .'/'. $file; |
|---|
| 716 |
if (filetype($fullfile) == 'dir') { |
|---|
| 717 |
$size += get_directory_size($fullfile, $excludefile); |
|---|
| 718 |
} else { |
|---|
| 719 |
$size += filesize($fullfile); |
|---|
| 720 |
} |
|---|
| 721 |
} |
|---|
| 722 |
closedir($dir); |
|---|
| 723 |
|
|---|
| 724 |
return $size; |
|---|
| 725 |
} |
|---|
| 726 |
|
|---|
| 727 |
|
|---|
| 728 |
|
|---|
| 729 |
|
|---|
| 730 |
|
|---|
| 731 |
|
|---|
| 732 |
|
|---|
| 733 |
function get_real_size($size=0) { |
|---|
| 734 |
if (!$size) { |
|---|
| 735 |
return 0; |
|---|
| 736 |
} |
|---|
| 737 |
$scan['GB'] = 1073741824; |
|---|
| 738 |
$scan['Gb'] = 1073741824; |
|---|
| 739 |
$scan['G'] = 1073741824; |
|---|
| 740 |
$scan['g'] = 1073741824; |
|---|
| 741 |
$scan['MB'] = 1048576; |
|---|
| 742 |
$scan['Mb'] = 1048576; |
|---|
| 743 |
$scan['M'] = 1048576; |
|---|
| 744 |
$scan['m'] = 1048576; |
|---|
| 745 |
$scan['KB'] = 1024; |
|---|
| 746 |
$scan['Kb'] = 1024; |
|---|
| 747 |
$scan['K'] = 1024; |
|---|
| 748 |
$scan['k'] = 1024; |
|---|
| 749 |
|
|---|
| 750 |
while (list($key) = each($scan)) { |
|---|
| 751 |
if ((strlen($size)>strlen($key))&&(substr($size, strlen($size) - strlen($key))==$key)) { |
|---|
| 752 |
$size = substr($size, 0, strlen($size) - strlen($key)) * $scan[$key]; |
|---|
| 753 |
break; |
|---|
| 754 |
} |
|---|
| 755 |
} |
|---|
| 756 |
return $size; |
|---|
| 757 |
} |
|---|
| 758 |
|
|---|
| 759 |
|
|---|
| 760 |
|
|---|
| 761 |
|
|---|
| 762 |
|
|---|
| 763 |
|
|---|
| 764 |
|
|---|
| 765 |
|
|---|
| 766 |
|
|---|
| 767 |
|
|---|
| 768 |
|
|---|
| 769 |
|
|---|
| 770 |
function display_size($size) { |
|---|
| 771 |
|
|---|
| 772 |
static $gb, $mb, $kb, $b; |
|---|
| 773 |
|
|---|
| 774 |
if (empty($gb)) { |
|---|
| 775 |
$gb = __gettext('GB'); |
|---|
| 776 |
$mb = __gettext('MB'); |
|---|
| 777 |
$kb = __gettext('KB'); |
|---|
| 778 |
$b = __gettext('bytes'); |
|---|
| 779 |
} |
|---|
| 780 |
|
|---|
| 781 |
if ($size >= 1073741824) { |
|---|
| 782 |
$size = round($size / 1073741824 * 10) / 10 . $gb; |
|---|
| 783 |
} else if ($size >= 1048576) { |
|---|
| 784 |
$size = round($size / 1048576 * 10) / 10 . $mb; |
|---|
| 785 |
} else if ($size >= 1024) { |
|---|
| 786 |
$size = round($size / 1024 * 10) / 10 . $kb; |
|---|
| 787 |
} else { |
|---|
| 788 |
$size = $size .' '. $b; |
|---|
| 789 |
} |
|---|
| 790 |
return $size; |
|---|
| 791 |
} |
|---|
| 792 |
|
|---|
| 793 |
|
|---|
| 794 |
|
|---|
| 795 |
|
|---|
| 796 |
|
|---|
| 797 |
|
|---|
| 798 |
function convert_high_ascii($s) { |
|---|
| 799 |
$HighASCII = array( |
|---|
| 800 |
"!\xc0!" => 'A', |
|---|
| 801 |
"!\xe0!" => 'a', |
|---|
| 802 |
"!\xc1!" => 'A', |
|---|
| 803 |
"!\xe1!" => 'a', |
|---|
| 804 |
"!\xc2!" => 'A', |
|---|
| 805 |
"!\xe2!" => 'a', |
|---|
| 806 |
"!\xc4!" => 'Ae', |
|---|
| 807 |
"!\xe4!" => 'ae', |
|---|
| 808 |
"!\xc3!" => 'A', |
|---|
| 809 |
"!\xe3!" => 'a', |
|---|
| 810 |
"!\xc8!" => 'E', |
|---|
| 811 |
"!\xe8!" => 'e', |
|---|
| 812 |
"!\xc9!" => 'E', |
|---|
| 813 |
"!\xe9!" => 'e', |
|---|
| 814 |
"!\xca!" => 'E', |
|---|
| 815 |
"!\xea!" => 'e', |
|---|
| 816 |
"!\xcb!" => 'Ee', |
|---|
| 817 |
"!\xeb!" => 'ee', |
|---|
| 818 |
"!\xcc!" => 'I', |
|---|
| 819 |
"!\xec!" => 'i', |
|---|
| 820 |
"!\xcd!" => 'I', |
|---|
| 821 |
"!\xed!" => 'i', |
|---|
| 822 |
"!\xce!" => 'I', |
|---|
| 823 |
"!\xee!" => 'i', |
|---|
| 824 |
"!\xcf!" => 'Ie', |
|---|
| 825 |
"!\xef!" => 'ie', |
|---|
| 826 |
"!\xd2!" => 'O', |
|---|
| 827 |
"!\xf2!" => 'o', |
|---|
| 828 |
"!\xd3!" => 'O', |
|---|
| 829 |
"!\xf3!" => 'o', |
|---|
| 830 |
"!\xd4!" => 'O', |
|---|
| 831 |
"!\xf4!" => 'o', |
|---|
| 832 |
"!\xd6!" => 'Oe', |
|---|
| 833 |
"!\xf6!" => 'oe', |
|---|
| 834 |
"!\xd5!" => 'O', |
|---|
| 835 |
"!\xf5!" => 'o', |
|---|
| 836 |
"!\xd8!" => 'Oe', |
|---|
| 837 |
"!\xf8!" => 'oe', |
|---|
| 838 |
"!\xd9!" => 'U', |
|---|
| 839 |
"!\xf9!" => 'u', |
|---|
| 840 |
"!\xda!" => 'U', |
|---|
| 841 |
"!\xfa!" => 'u', |
|---|
| 842 |
"!\xdb!" => 'U', |
|---|
| 843 |
"!\xfb!" => 'u', |
|---|
| 844 |
"!\xdc!" => 'Ue', |
|---|
| 845 |
"!\xfc!" => 'ue', |
|---|
| 846 |
"!\xc7!" => 'C', |
|---|
| 847 |
"!\xe7!" => 'c', |
|---|
| 848 |
"!\xd1!" => 'N', |
|---|
| 849 |
"!\xf1!" => 'n', |
|---|
| 850 |
"!\xdf!" => 'ss' |
|---|
| 851 |
); |
|---|
| 852 |
$find = array_keys($HighASCII); |
|---|
| 853 |
$replace = array_values($HighASCII); |
|---|
| 854 |
$s = preg_replace($find,$replace,$s); |
|---|
| 855 |
return $s; |
|---|
| 856 |
} |
|---|
| 857 |
|
|---|
| 858 |
|
|---|
| 859 |
|
|---|
| 860 |
|
|---|
| 861 |
|
|---|
| 862 |
|
|---|
| 863 |
|
|---|
| 864 |
|
|---|
| 865 |
|
|---|
| 866 |
function clean_filename($string) { |
|---|
| 867 |
$string = convert_high_ascii($string); |
|---|
| 868 |
$string = eregi_replace("\.\.+", '', $string); |
|---|
| 869 |
$string = preg_replace('/[^\.a-zA-Z\d\_-]/','_', $string ); |
|---|
| 870 |
$string = eregi_replace("_+", '_', $string); |
|---|
| 871 |
return $string; |
|---|
| 872 |
} |
|---|
| 873 |
|
|---|
| 874 |
|
|---|
| 875 |
|
|---|
| 876 |
|
|---|
| 877 |
|
|---|
| 878 |
|
|---|
| 879 |
|
|---|
| 880 |
|
|---|
| 881 |
|
|---|
| 882 |
|
|---|
| 883 |
|
|---|
| 884 |
|
|---|
| 885 |
|
|---|
| 886 |
|
|---|
| 887 |
function raise_memory_limit ($newlimit) { |
|---|
| 888 |
|
|---|
| 889 |
if (empty($newlimit)) { |
|---|
| 890 |
return false; |
|---|
| 891 |
} |
|---|
| 892 |
|
|---|
| 893 |
$cur = @ini_get('memory_limit'); |
|---|
| 894 |
if (empty($cur)) { |
|---|
| 895 |
|
|---|
| 896 |
// apparently memory_limit is set to '' |
|---|
| 897 |
$cur=0; |
|---|
| 898 |
} else { |
|---|
| 899 |
if ($cur == -1){ |
|---|
| 900 |
return true; |
|---|
| 901 |
} |
|---|
| 902 |
$cur = get_real_size($cur); |
|---|
| 903 |
} |
|---|
| 904 |
|
|---|
| 905 |
$new = get_real_size($newlimit); |
|---|
| 906 |
if ($new > $cur) { |
|---|
| 907 |
ini_set('memory_limit', $newlimit); |
|---|
| 908 |
return true; |
|---|
| 909 |
} |
|---|
| 910 |
return false; |
|---|
| 911 |
} |
|---|
| 912 |
|
|---|
| 913 |
|
|---|
| 914 |
|
|---|
| 915 |
|
|---|
| 916 |
|
|---|
| 917 |
|
|---|
| 918 |
|
|---|
| 919 |
|
|---|
| 920 |
|
|---|
| 921 |
function elgg_strtolower ($string, $encoding='') { |
|---|
| 922 |
$textlib = textlib_get_instance(); |
|---|
| 923 |
return $textlib->strtolower($string, $encoding?$encoding:'utf-8'); |
|---|
| 924 |
} |
|---|
| 925 |
|
|---|
| 926 |
|
|---|
| 927 |
|
|---|
| 928 |
|
|---|
| 929 |
|
|---|
| 930 |
|
|---|
| 931 |
|
|---|
| 932 |
|
|---|
| 933 |
|
|---|
| 934 |
|
|---|
| 935 |
function swapshuffle($array) { |
|---|
| 936 |
|
|---|
| 937 |
srand ((double) microtime() * 10000000); |
|---|
| 938 |
$last = count($array) - 1; |
|---|
| 939 |
for ($i=0;$i<=$last;$i++) { |
|---|
| 940 |
$from = rand(0,$last); |
|---|
| 941 |
$curr = $array[$i]; |
|---|
| 942 |
$array[$i] = $array[$from]; |
|---|
| 943 |
$array[$from] = $curr; |
|---|
| 944 |
} |
|---|
| 945 |
return $array; |
|---|
| 946 |
} |
|---|
| 947 |
|
|---|
| 948 |
|
|---|
| 949 |
|
|---|
| 950 |
|
|---|
| 951 |
|
|---|
| 952 |
|
|---|
| 953 |
|
|---|
| 954 |
function swapshuffle_assoc($array) { |
|---|
| 955 |
|
|---|
| 956 |
|
|---|
| 957 |
$newkeys = swapshuffle(array_keys($array)); |
|---|
| 958 |
$newkeys as $newkey) { |
|---|
| 959 |
$newarray[$newkey] = $array[$newkey]; |
|---|
| 960 |
|
|---|
| 961 |
$newarray; |
|---|
| 962 |
|
|---|
| 963 |
|
|---|
| 964 |
|
|---|
| 965 |
|
|---|
| 966 |
|
|---|
| 967 |
|
|---|
| 968 |
|
|---|
| 969 |
|
|---|
| 970 |
|
|---|
| 971 |
|
|---|
| 972 |
|
|---|
| 973 |
|
|---|
| 974 |
function draw_rand_array($array, $draws) { |
|---|
| 975 |
srand ((double) microtime() * 10000000); |
|---|
| 976 |
|
|---|
| 977 |
$return = array(); |
|---|
| 978 |
|
|---|
| 979 |
$last = count($array); |
|---|
| 980 |
|
|---|
| 981 |
if ($draws > $last) { |
|---|
| 982 |
$draws = $last; |
|---|
| 983 |
} |
|---|
| 984 |
|
|---|
| 985 |
while ($draws > 0) { |
|---|
| 986 |
$last--; |
|---|
| 987 |
|
|---|
| 988 |
$keys = array_keys($array); |
|---|
| 989 |
$rand = rand(0, $last); |
|---|
| 990 |
|
|---|
| 991 |
$return[$keys[$rand]] = $array[$keys[$rand]]; |
|---|
| 992 |
unset($array[$keys[$rand]]); |
|---|
| 993 |
|
|---|
| 994 |
$draws--; |
|---|
| 995 |
} |
|---|
| 996 |
|
|---|
| 997 |
return $return; |
|---|
| 998 |
} |
|---|
| 999 |
|
|---|
| 1000 |
|
|---|
| 1001 |
|
|---|
| 1002 |
|
|---|
| 1003 |
|
|---|
| 1004 |
|
|---|
| 1005 |
|
|---|
| 1006 |
|
|---|
| 1007 |
|
|---|
| 1008 |
|
|---|
| 1009 |
|
|---|
| 1010 |
|
|---|
| 1011 |
|
|---|
| 1012 |
|
|---|
| 1013 |
|
|---|
| 1014 |
|
|---|
| 1015 |
function address_in_subnet($addr, $subnetstr) { |
|---|
| 1016 |
|
|---|
| 1017 |
$subnets = explode(',', $subnetstr); |
|---|
| 1018 |
$found = false; |
|---|
| 1019 |
$addr = trim($addr); |
|---|
| 1020 |
|
|---|
| 1021 |
foreach ($subnets as $subnet) { |
|---|
| 1022 |
$subnet = trim($subnet); |
|---|
| 1023 |
if (strpos($subnet, '/') !== false) { |
|---|
| 1024 |
|
|---|
| 1025 |
list($ip, $mask) = explode('/', $subnet); |
|---|
| 1026 |
$mask = 0xffffffff << (32 - $mask); |
|---|
| 1027 |
$found = ((ip2long($addr) & $mask) == (ip2long($ip) & $mask)); |
|---|
| 1028 |
|
|---|
| 1029 |
} else { |
|---|
| 1030 |
$found = (strpos($addr, $subnet) === 0); |
|---|
| 1031 |
} |
|---|
| 1032 |
|
|---|
| 1033 |
if ($found) { |
|---|
| 1034 |
break; |
|---|
| 1035 |
} |
|---|
| 1036 |
} |
|---|
| 1037 |
|
|---|
| 1038 |
return $found; |
|---|
| 1039 |
} |
|---|
| 1040 |
|
|---|
| 1041 |
|
|---|
| 1042 |
|
|---|
| 1043 |
|
|---|
| 1044 |
|
|---|
| 1045 |
|
|---|
| 1046 |
|
|---|
| 1047 |
|
|---|
| 1048 |
|
|---|
| 1049 |
function mtrace($string, $eol="\n", $sleep=0) { |
|---|
| 1050 |
|
|---|
| 1051 |
if (defined('STDOUT')) { |
|---|
| 1052 |
fwrite(STDOUT, $string.$eol); |
|---|
| 1053 |
} else { |
|---|
| 1054 |
echo $string . $eol; |
|---|
| 1055 |
} |
|---|
| 1056 |
|
|---|
| 1057 |
flush(); |
|---|
| 1058 |
|
|---|
| 1059 |
|
|---|
| 1060 |
if ($sleep) { |
|---|
| 1061 |
sleep($sleep); |
|---|
| 1062 |
} |
|---|
| 1063 |
} |
|---|
| 1064 |
|
|---|
| 1065 |
|
|---|
| 1066 |
function cleardoubleslashes ($path) { |
|---|
| 1067 |
return preg_replace('/(\/|\\\){1,}/','/',$path); |
|---|
| 1068 |
} |
|---|
| 1069 |
|
|---|
| 1070 |
|
|---|
| 1071 |
|
|---|
| 1072 |
|
|---|
| 1073 |
|
|---|
| 1074 |
|
|---|
| 1075 |
|
|---|
| 1076 |
|
|---|
| 1077 |
function getremoteaddr() { |
|---|
| 1078 |
if (!empty($_SERVER['HTTP_CLIENT_IP'])) { |
|---|
| 1079 |
return cleanremoteaddr($_SERVER['HTTP_CLIENT_IP']); |
|---|
| 1080 |
} |
|---|
| 1081 |
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
|---|
| 1082 |
return cleanremoteaddr($_SERVER['HTTP_X_FORWARDED_FOR']); |
|---|
| 1083 |
} |
|---|
| 1084 |
if (!empty($_SERVER['REMOTE_ADDR'])) { |
|---|
| 1085 |
return cleanremoteaddr($_SERVER['REMOTE_ADDR']); |
|---|
| 1086 |
} |
|---|
| 1087 |
return ''; |
|---|
| 1088 |
} |
|---|
| 1089 |
|
|---|
| 1090 |
|
|---|
| 1091 |
|
|---|
| 1092 |
|
|---|
| 1093 |
function cleanremoteaddr($addr) { |
|---|
| 1094 |
$originaladdr = $addr; |
|---|
| 1095 |
$matches = array(); |
|---|
| 1096 |
|
|---|
| 1097 |
if (!preg_match_all('/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/',$addr,$matches,PREG_SET_ORDER)) { |
|---|
| 1098 |
return ''; |
|---|
| 1099 |
} |
|---|
| 1100 |
$goodmatches = array(); |
|---|
| 1101 |
$lanmatches = array(); |
|---|
| 1102 |
foreach ($matches as $match) { |
|---|
| 1103 |
|
|---|
| 1104 |
// check to make sure it's not an internal address. |
|---|
| 1105 |
// the following are reserved for private lans... |
|---|
| 1106 |
// 10.0.0.0 - 10.255.255.255 |
|---|
| 1107 |
// 172.16.0.0 - 172.31.255.255 |
|---|
| 1108 |
// 192.168.0.0 - 192.168.255.255 |
|---|
| 1109 |
// 169.254.0.0 -169.254.255.255 |
|---|
| 1110 |
$bits = explode('.',$match[0]); |
|---|
| 1111 |
if (count($bits) != 4) { |
|---|
| 1112 |
|
|---|
| 1113 |
continue; |
|---|
| 1114 |
} |
|---|
| 1115 |
if (($bits[0] == 10) |
|---|
| 1116 |
|| ($bits[0] == 172 && $bits[1] >= 16 && $bits[1] <= 31) |
|---|
| 1117 |
|| ($bits[0] == 192 && $bits[1] == 168) |
|---|
| 1118 |
|| ($bits[0] == 169 && $bits[1] == 254)) { |
|---|
| 1119 |
$lanmatches[] = $match[0]; |
|---|
| 1120 |
continue; |
|---|
| 1121 |
} |
|---|
| 1122 |
|
|---|
| 1123 |
$goodmatches[] = $match[0]; |
|---|
| 1124 |
} |
|---|
| 1125 |
if (!count($goodmatches)) { |
|---|
| 1126 |
|
|---|
| 1127 |
if (!count($lanmatches)) { |
|---|
| 1128 |
return ''; |
|---|
| 1129 |
} else { |
|---|
| 1130 |
return array_pop($lanmatches); |
|---|
| 1131 |
} |
|---|
| 1132 |
} |
|---|
| 1133 |
if (count($goodmatches) == 1) { |
|---|
| 1134 |
return $goodmatches[0]; |
|---|
| 1135 |
} |
|---|
| 1136 |
error_log("NOTICE: cleanremoteaddr gives us something funny: $originaladdr had ".count($goodmatches)." matches"); |
|---|
| 1137 |
|
|---|
| 1138 |
return array_pop($goodmatches); |
|---|
| 1139 |
} |
|---|
| 1140 |
|
|---|
| 1141 |
|
|---|
| 1142 |
|
|---|
| 1143 |
|
|---|
| 1144 |
|
|---|
| 1145 |
|
|---|
| 1146 |
|
|---|
| 1147 |
|
|---|
| 1148 |
|
|---|
| 1149 |
if(!function_exists('html_entity_decode')) { |
|---|
| 1150 |
function html_entity_decode($string, $quote_style = ENT_COMPAT, $charset = 'ISO-8859-1') { |
|---|
| 1151 |
$trans_tbl = get_html_translation_table(HTML_ENTITIES, $quote_style); |
|---|
| 1152 |
$trans_tbl = array_flip($trans_tbl); |
|---|
| 1153 |
return strtr($string, $trans_tbl); |
|---|
| 1154 |
} |
|---|
| 1155 |
} |
|---|
| 1156 |
|
|---|
| 1157 |
|
|---|
| 1158 |
|
|---|
| 1159 |
|
|---|
| 1160 |
|
|---|
| 1161 |
|
|---|
| 1162 |
|
|---|
| 1163 |
|
|---|
| 1164 |
|
|---|
| 1165 |
|
|---|
| 1166 |
|
|---|
| 1167 |
|
|---|
| 1168 |
|
|---|
| 1169 |
|
|---|
| 1170 |
|
|---|
| 1171 |
|
|---|
| 1172 |
|
|---|
| 1173 |
if(!check_php_version('5.0.0')) { |
|---|
| 1174 |
|
|---|
| 1175 |
eval(' |
|---|
| 1176 |
function clone($obj) { |
|---|
| 1177 |
/// Sanity check |
|---|
| 1178 |
if (!is_object($obj)) { |
|---|
| 1179 |
user_error(\'clone() __clone method called on non-object\', E_USER_WARNING); |
|---|
| 1180 |
return; |
|---|
| 1181 |
} |
|---|
| 1182 |
|
|---|
| 1183 |
/// Use serialize/unserialize trick to deep copy the object |
|---|
| 1184 |
$obj = unserialize(serialize($obj)); |
|---|
| 1185 |
|
|---|
| 1186 |
/// If there is a __clone method call it on the "new" class |
|---|
| 1187 |
if (method_exists($obj, \'__clone\')) { |
|---|
| 1188 |
$obj->__clone(); |
|---|
| 1189 |
} |
|---|
| 1190 |
|
|---|
| 1191 |
return $obj; |
|---|
| 1192 |
} |
|---|
| 1193 |
'); |
|---|
| 1194 |
} |
|---|
| 1195 |
|
|---|
| 1196 |
|
|---|
| 1197 |
|
|---|
| 1198 |
|
|---|
| 1199 |
|
|---|
| 1200 |
|
|---|
| 1201 |
|
|---|
| 1202 |
|
|---|
| 1203 |
|
|---|
| 1204 |
|
|---|
| 1205 |
function microtime_diff($a, $b) { |
|---|
| 1206 |
list($a_dec, $a_sec) = explode(' ', $a); |
|---|
| 1207 |
list($b_dec, $b_sec) = explode(' ', $b); |
|---|
| 1208 |
return $b_sec - $a_sec + $b_dec - $a_dec; |
|---|
| 1209 |
} |
|---|
| 1210 |
|
|---|
| 1211 |
|
|---|
| 1212 |
|
|---|
| 1213 |
|
|---|
| 1214 |
|
|---|
| 1215 |
|
|---|
| 1216 |
|
|---|
| 1217 |
|
|---|
| 1218 |
|
|---|
| 1219 |
function get_performance_info() { |
|---|
| 1220 |
global $CFG, $PERF; |
|---|
| 1221 |
|
|---|
| 1222 |
$info = array(); |
|---|
| 1223 |
$info['html'] = ''; |
|---|
| 1224 |
$info['txt'] = me() . ' '; |
|---|
| 1225 |
|
|---|
| 1226 |
$info['realtime'] = microtime_diff($PERF->starttime, microtime()); |
|---|
| 1227 |
|
|---|
| 1228 |
$info['html'] .= '<span class="timeused">'.$info['realtime'].' secs</span> '; |
|---|
| 1229 |
$info['txt'] .= 'time: '.$info['realtime'].'s '; |
|---|
| 1230 |
|
|---|
| 1231 |
if (function_exists('memory_get_usage')) { |
|---|
| 1232 |
$info['memory_total'] = memory_get_usage(); |
|---|
| 1233 |
$info['memory_growth'] = memory_get_usage() - $PERF->startmemory; |
|---|
| 1234 |
$info['html'] .= '<span class="memoryused">RAM: '.display_size($info['memory_total']).'</span> '; |
|---|
| 1235 |
$info['txt'] .= 'memory_total: '.$info['memory_total'].'B (' . display_size($info['memory_total']).') memory_growth: '.$info['memory_growth'].'B ('.display_size($info['memory_growth']).') '; |
|---|
| 1236 |
} |
|---|
| 1237 |
|
|---|
| 1238 |
$inc = get_included_files(); |
|---|
| 1239 |
|
|---|
| 1240 |
$info['includecount'] = count($inc); |
|---|
| 1241 |
$info['html'] .= '<span class="included">Included '.$info['includecount'].' files</span> '; |
|---|
| 1242 |
$info['txt'] .= 'includecount: '.$info['includecount'].' '; |
|---|
| 1243 |
|
|---|
| 1244 |
if (!empty($PERF->dbqueries)) { |
|---|
| 1245 |
$info['dbqueries'] = $PERF->dbqueries; |
|---|
| 1246 |
$info['html'] .= '<span class="dbqueries">DB queries '.$info['dbqueries'].'</span> '; |
|---|
| 1247 |
$info['txt'] .= 'dbqueries: '.$info['dbqueries'].' '; |
|---|
| 1248 |
} |
|---|
| 1249 |
|
|---|
| 1250 |
if (!empty($PERF->logwrites)) { |
|---|
| 1251 |
$info['logwrites'] = $PERF->logwrites; |
|---|
| 1252 |
$info['html'] .= '<span class="logwrites">Log writes '.$info['logwrites'].'</span> '; |
|---|
| 1253 |
$info['txt'] .= 'logwrites: '.$info['logwrites'].' '; |
|---|
| 1254 |
} |
|---|
| 1255 |
|
|---|
| 1256 |
if (function_exists('posix_times')) { |
|---|
| 1257 |
$ptimes = posix_times(); |
|---|
| 1258 |
if ($ptimes) { |
|---|
| 1259 |
foreach ($ptimes as $key => $val) { |
|---|
| 1260 |
$info[$key] = $ptimes[$key] - $PERF->startposixtimes[$key]; |
|---|
| 1261 |
} |
|---|
| 1262 |
$info['html'] .= "<span class=\"posixtimes\">ticks: $info[ticks] user: $info[utime] sys: $info[stime] cuser: $info[cutime] csys: $info[cstime]</span> "; |
|---|
| 1263 |
$info['txt'] .= "ticks: $info[ticks] user: $info[utime] sys: $info[stime] cuser: $info[cutime] csys: $info[cstime] "; |
|---|
| 1264 |
} |
|---|
| 1265 |
} |
|---|
| 1266 |
|
|---|
| 1267 |
|
|---|
| 1268 |
// /proc will only work under some linux configurations |
|---|
| 1269 |
// while uptime is there under MacOSX/Darwin and other unices |
|---|
| 1270 |
if (!@ini_get('open_basedir') && is_readable('/proc/loadavg') && $loadavg = @file('/proc/loadavg')) { |
|---|
| 1271 |
list($server_load) = explode(' ', $loadavg[0]); |
|---|
| 1272 |
unset($loadavg); |
|---|
| 1273 |
} else if (!@ini_get('open_basedir') && function_exists('is_executable') && is_executable('/usr/bin/uptime') && $loadavg = `/usr/bin/uptime` ) { |
|---|
| 1274 |
if (preg_match('/load averages?: (\d+[\.:]\d+)/', $loadavg, $matches)) { |
|---|
| 1275 |
$server_load = $matches[1]; |
|---|
| 1276 |
} else { |
|---|
| 1277 |
trigger_error('Could not parse uptime output!'); |
|---|
| 1278 |
} |
|---|
| 1279 |
} |
|---|
| 1280 |
if (!empty($server_load)) { |
|---|
| 1281 |
$info['serverload'] = $server_load; |
|---|
| 1282 |
$info['html'] .= '<span class="serverload">Load average: '.$info['serverload'].'</span> '; |
|---|
| 1283 |
$info['txt'] .= 'serverload: '.$info['serverload']; |
|---|
| 1284 |
} |
|---|
| 1285 |
|
|---|
| 1286 |
|
|---|
| 1287 |
$info['html'] = '<div class="performanceinfo">'.$info['html'].'</div>'; |
|---|
| 1288 |
return $info; |
|---|
| 1289 |
} |
|---|
| 1290 |
|
|---|
| 1291 |
if (!function_exists('file_get_contents')) { |
|---|
| 1292 |
function file_get_contents($file) { |
|---|
| 1293 |
$file = file($file); |
|---|
| 1294 |
return $file ? implode('', $file) : false; |
|---|
| 1295 |
} |
|---|
| 1296 |
} |
|---|
| 1297 |
|
|---|
| 1298 |
|
|---|
| 1299 |
|
|---|
| 1300 |
|
|---|
| 1301 |
|
|---|
| 1302 |
|
|---|
| 1303 |
|
|---|
| 1304 |
|
|---|
| 1305 |
|
|---|
| 1306 |
|
|---|
| 1307 |
|
|---|
| 1308 |
function object_property_exists( $obj, $property ) { |
|---|
| 1309 |
if (is_string( $obj )) { |
|---|
| 1310 |
$properties = get_class_vars( $obj ); |
|---|
| 1311 |
} |
|---|
| 1312 |
else { |
|---|
| 1313 |
$properties = get_object_vars( $obj ); |
|---|
| 1314 |
} |
|---|
| 1315 |
return array_key_exists( $property, $properties ); |
|---|
| 1316 |
} |
|---|
| 1317 |
|
|---|
| 1318 |
|
|---|
| 1319 |
|
|---|
| 1320 |
|
|---|
| 1321 |
|
|---|
| 1322 |
|
|---|
| 1323 |
|
|---|
| 1324 |
|
|---|
| 1325 |
|
|---|
| 1326 |
|
|---|
| 1327 |
|
|---|
| 1328 |
function s($var) { |
|---|
| 1329 |
if ($var == '0') { |
|---|
| 1330 |
return '0'; |
|---|
| 1331 |
} |
|---|
| 1332 |
return preg_replace("/&(#\d+);/iu", '&$1;', htmlspecialchars(stripslashes_safe($var), ENT_COMPAT, 'utf-8')); |
|---|
| 1333 |
} |
|---|
| 1334 |
|
|---|
| 1335 |
|
|---|
| 1336 |
|
|---|
| 1337 |
|
|---|
| 1338 |
|
|---|
| 1339 |
|
|---|
| 1340 |
|
|---|
| 1341 |
|
|---|
| 1342 |
|
|---|
| 1343 |
|
|---|
| 1344 |
function p($var) { |
|---|
| 1345 |
echo s($var); |
|---|
| 1346 |
} |
|---|
| 1347 |
|
|---|
| 1348 |
|
|---|
| 1349 |
|
|---|
| 1350 |
|
|---|
| 1351 |
|
|---|
| 1352 |
|
|---|
| 1353 |
|
|---|
| 1354 |
|
|---|
| 1355 |
|
|---|
| 1356 |
|
|---|
| 1357 |
|
|---|
| 1358 |
|
|---|
| 1359 |
function nvl(&$var, $default='') { |
|---|
| 1360 |
|
|---|
| 1361 |
return isset($var) ? $var : $default; |
|---|
| 1362 |
} |
|---|
| 1363 |
|
|---|
| 1364 |
|
|---|
| 1365 |
|
|---|
| 1366 |
|
|---|
| 1367 |
|
|---|
| 1368 |
|
|---|
| 1369 |
|
|---|
| 1370 |
|
|---|
| 1371 |
|
|---|
| 1372 |
function strip_querystring($url) { |
|---|
| 1373 |
$textlib = textlib_get_instance(); |
|---|
| 1374 |
|
|---|
| 1375 |
$commapos = $textlib->strpos($url, '?')) { |
|---|
| 1376 |
$textlib->substr($url, 0, $commapos); |
|---|
| 1377 |
|
|---|
| 1378 |
$url; |
|---|
| 1379 |
|
|---|
| 1380 |
|
|---|
| 1381 |
|
|---|
| 1382 |
|
|---|
| 1383 |
|
|---|
| 1384 |
|
|---|
| 1385 |
|
|---|
| 1386 |
function get_referer() { |
|---|
| 1387 |
|
|---|
| 1388 |
return strip_querystring(nvl($_SERVER['HTTP_REFERER'])); |
|---|
| 1389 |
} |
|---|
| 1390 |
|
|---|
| 1391 |
|
|---|
| 1392 |
|
|---|
| 1393 |
|
|---|
| 1394 |
|
|---|
| 1395 |
|
|---|
| 1396 |
|
|---|
| 1397 |
|
|---|
| 1398 |
|
|---|
| 1399 |
|
|---|
| 1400 |
|
|---|
| 1401 |
function me() { |
|---|
| 1402 |
|
|---|
| 1403 |
$_SERVER['REQUEST_URI'])) { |
|---|
| 1404 |
$_SERVER['REQUEST_URI']; |
|---|
| 1405 |
|
|---|
| 1406 |
$_SERVER['PHP_SELF'])) { |
|---|
| 1407 |
$_SERVER['QUERY_STRING'])) { |
|---|
| 1408 |
$_SERVER['PHP_SELF'] .'?'. $_SERVER['QUERY_STRING']; |
|---|
| 1409 |
|
|---|
| 1410 |
$_SERVER['PHP_SELF']; |
|---|
| 1411 |
|
|---|
| 1412 |
$_SERVER['SCRIPT_NAME'])) { |
|---|
| 1413 |
$_SERVER['QUERY_STRING'])) { |
|---|
| 1414 |
$_SERVER['SCRIPT_NAME'] .'?'. $_SERVER['QUERY_STRING']; |
|---|
| 1415 |
|
|---|
| 1416 |
$_SERVER['SCRIPT_NAME']; |
|---|
| 1417 |
|
|---|
| 1418 |
$_SERVER['URL'])) { |
|---|
| 1419 |
if (!empty($_SERVER['QUERY_STRING'])) { |
|---|
| 1420 |
$_SERVER['URL'] .'?'. $_SERVER['QUERY_STRING']; |
|---|
| 1421 |
|
|---|
| 1422 |
$_SERVER['URL']; |
|---|
| 1423 |
|
|---|
| 1424 |
|
|---|
| 1425 |
notify('Warning: Could not find any of these web server variables: $REQUEST_URI, $PHP_SELF, $SCRIPT_NAME or $URL'); |
|---|
| 1426 |
false; |
|---|
| 1427 |
|
|---|
| 1428 |
|
|---|
| 1429 |
|
|---|
| 1430 |
|
|---|
| 1431 |
|
|---|
| 1432 |
|
|---|
| 1433 |
|
|---|
| 1434 |
|
|---|
| 1435 |
function qualified_me() { |
|---|
| 1436 |
|
|---|
| 1437 |
global $CFG; |
|---|
| 1438 |
|
|---|
| 1439 |
if (!empty($CFG->wwwroot)) { |
|---|
| 1440 |
$url = parse_url($CFG->wwwroot); |
|---|
| 1441 |
} |
|---|
| 1442 |
|
|---|
| 1443 |
if (!empty($url['host'])) { |
|---|
| 1444 |
$hostname = $url['host']; |
|---|
| 1445 |
} else if (!empty($_SERVER['SERVER_NAME'])) { |
|---|
| 1446 |
$hostname = $_SERVER['SERVER_NAME']; |
|---|
| 1447 |
} else if (!empty($_ENV['SERVER_NAME'])) { |
|---|
| 1448 |
$hostname = $_ENV['SERVER_NAME']; |
|---|
| 1449 |
} else if (!empty($_SERVER['HTTP_HOST'])) { |
|---|
| 1450 |
$hostname = $_SERVER['HTTP_HOST']; |
|---|
| 1451 |
} else if (!empty($_ENV['HTTP_HOST'])) { |
|---|
| 1452 |
$hostname = $_ENV['HTTP_HOST']; |
|---|
| 1453 |
} else { |
|---|
| 1454 |
notify('Warning: could not find the name of this server!'); |
|---|
| 1455 |
return false; |
|---|
| 1456 |
} |
|---|
| 1457 |
|
|---|
| 1458 |
if (!empty($url['port'])) { |
|---|
| 1459 |
$hostname .= ':'.$url['port']; |
|---|
| 1460 |
} else if (!empty($_SERVER['SERVER_PORT'])) { |
|---|
| 1461 |
if ($_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) { |
|---|
| 1462 |
$hostname .= ':'.$_SERVER['SERVER_PORT']; |
|---|
| 1463 |
} |
|---|
| 1464 |
} |
|---|
| 1465 |
|
|---|
| 1466 |
if (isset($_SERVER['HTTPS'])) { |
|---|
| 1467 |
$protocol = ($_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://'; |
|---|
| 1468 |
} else if (isset($_SERVER['SERVER_PORT'])) { |
|---|
| 1469 |
$protocol = ($_SERVER['SERVER_PORT'] == '443') ? 'https://' : 'http://'; |
|---|
| 1470 |
} else { |
|---|
| 1471 |
$protocol = 'http://'; |
|---|
| 1472 |
} |
|---|
| 1473 |
|
|---|
| 1474 |
$url_prefix = $protocol.$hostname; |
|---|
| 1475 |
return $url_prefix . me(); |
|---|
| 1476 |
} |
|---|
| 1477 |
|
|---|
| 1478 |
|
|---|
| 1479 |
|
|---|
| 1480 |
|
|---|
| 1481 |
|
|---|
| 1482 |
|
|---|
| 1483 |
|
|---|
| 1484 |
|
|---|
| 1485 |
|
|---|
| 1486 |
|
|---|
| 1487 |
|
|---|
| 1488 |
|
|---|
| 1489 |
|
|---|
| 1490 |
function match_referer($goodreferer = '') { |
|---|
| 1491 |
global $CFG; |
|---|
| 1492 |
|
|---|
| 1493 |
if (empty($CFG->secureforms)) { |
|---|
| 1494 |
return true; |
|---|
| 1495 |
} |
|---|
| 1496 |
|
|---|
| 1497 |
if ($goodreferer == 'nomatch') { |
|---|
| 1498 |
return true; |
|---|
| 1499 |
} |
|---|
| 1500 |
|
|---|
| 1501 |
if (empty($goodreferer)) { |
|---|
| 1502 |
$goodreferer = qualified_me(); |
|---|
| 1503 |
} |
|---|
| 1504 |
|
|---|
| 1505 |
$referer = get_referer(); |
|---|
| 1506 |
|
|---|
| 1507 |
return (($referer == $goodreferer) or ($referer == $CFG->wwwroot) or ($referer == $CFG->wwwroot .'index.php')); |
|---|
| 1508 |
} |
|---|
| 1509 |
|
|---|
| 1510 |
|
|---|
| 1511 |
|
|---|
| 1512 |
|
|---|
| 1513 |
|
|---|
| 1514 |
|
|---|
| 1515 |
|
|---|
| 1516 |
|
|---|
| 1517 |
|
|---|
| 1518 |
|
|---|
| 1519 |
|
|---|
| 1520 |
|
|---|
| 1521 |
|
|---|
| 1522 |
|
|---|
| 1523 |
|
|---|
| 1524 |
|
|---|
| 1525 |
|
|---|
| 1526 |
function data_submitted($url='') { |
|---|
| 1527 |
|
|---|
| 1528 |
|
|---|
| 1529 |
global $CFG; |
|---|
| 1530 |
|
|---|
| 1531 |
if (empty($_POST)) { |
|---|
| 1532 |
return false; |
|---|
| 1533 |
|
|---|
| 1534 |
} else { |
|---|
| 1535 |
if (match_referer($url)) { |
|---|
| 1536 |
return (object)$_POST; |
|---|
| 1537 |
} else { |
|---|
| 1538 |
if ($CFG->debug > 10) { |
|---|
| 1539 |
notice('The form did not come from this page! (referer = '. get_referer() .')'); |
|---|
| 1540 |
} |
|---|
| 1541 |
return false; |
|---|
| 1542 |
} |
|---|
| 1543 |
} |
|---|
| 1544 |
} |
|---|
| 1545 |
|
|---|
| 1546 |
|
|---|
| 1547 |
|
|---|
| 1548 |
|
|---|
| 1549 |
|
|---|
| 1550 |
|
|---|
| 1551 |
|
|---|
| 1552 |
|
|---|
| 1553 |
|
|---|
| 1554 |
|
|---|
| 1555 |
|
|---|
| 1556 |
|
|---|
| 1557 |
function stripslashes_safe($string) { |
|---|
| 1558 |
|
|---|
| 1559 |
$string = str_replace("\\'", "'", $string); |
|---|
| 1560 |
$string = str_replace('\\"', '"', $string); |
|---|
| 1561 |
$string = str_replace('\\\\', '\\', $string); |
|---|
| 1562 |
return $string; |
|---|
| 1563 |
} |
|---|
| 1564 |
|
|---|
| 1565 |
|
|---|
| 1566 |
|
|---|
| 1567 |
|
|---|
| 1568 |
|
|---|
| 1569 |
|
|---|
| 1570 |
|
|---|
| 1571 |
|
|---|
| 1572 |
|
|---|
| 1573 |
|
|---|
| 1574 |
|
|---|
| 1575 |
|
|---|
| 1576 |
function stripslashes_recursive($var) { |
|---|
| 1577 |
if(is_object($var)) { |
|---|
| 1578 |
$properties = get_object_vars($var); |
|---|
| 1579 |
foreach($properties as $property => $value) { |
|---|
| 1580 |
$var->$property = stripslashes_recursive($value); |
|---|
| 1581 |
} |
|---|
| 1582 |
} |
|---|
| 1583 |
else if(is_array($var)) { |
|---|
| 1584 |
foreach($var as $property => $value) { |
|---|
| 1585 |
$var[$property] = stripslashes_recursive($value); |
|---|
| 1586 |
} |
|---|
| 1587 |
} |
|---|
| 1588 |
else if(is_string($var)) { |
|---|
| 1589 |
$var = stripslashes($var); |
|---|
| 1590 |
} |
|---|
| 1591 |
return $var; |
|---|
| 1592 |
} |
|---|
| 1593 |
|
|---|
| 1594 |
|
|---|
| 1595 |
|
|---|
| 1596 |
|
|---|
| 1597 |
|
|---|
| 1598 |
|
|---|
| 1599 |
|
|---|
| 1600 |
|
|---|
| 1601 |
|
|---|
| 1602 |
|
|---|
| 1603 |
|
|---|
| 1604 |
|
|---|
| 1605 |
if (!function_exists('str_ireplace')) { |
|---|
| 1606 |
function str_ireplace($find, $replace, $string) { |
|---|
| 1607 |
$textlib = textlib_get_instance(); |
|---|
| 1608 |
|
|---|
| 1609 |
if (!is_array($find)) { |
|---|
| 1610 |
$find = array($find); |
|---|
| 1611 |
} |
|---|
| 1612 |
|
|---|
| 1613 |
if(!is_array($replace)) { |
|---|
| 1614 |
if (!is_array($find)) { |
|---|
| 1615 |
$replace = array($replace); |
|---|
| 1616 |
} else { |
|---|
| 1617 |
|
|---|
| 1618 |
$c = count($find); |
|---|
| 1619 |
$rString = $replace; |
|---|
| 1620 |
unset($replace); |
|---|
| 1621 |
for ($i = 0; $i < $c; $i++) { |
|---|
| 1622 |
$replace[$i] = $rString; |
|---|
| 1623 |
} |
|---|
| 1624 |
} |
|---|
| 1625 |
} |
|---|
| 1626 |
|
|---|
| 1627 |
foreach ($find as $fKey => $fItem) { |
|---|
| 1628 |
$between = explode($textlib->strtolower($fItem),$textlib->strtolower($string)); |
|---|
| 1629 |
$pos = 0; |
|---|
| 1630 |
foreach($between as $bKey => $bItem) { |
|---|
| 1631 |
$between[$bKey] = $textlib->substr($string,$pos,$textlib->strlen($bItem)); |
|---|
| 1632 |
$pos += $textlib->strlen($bItem) + $textlib->strlen($fItem); |
|---|
| 1633 |
} |
|---|
| 1634 |
$string = implode($replace[$fKey],$between); |
|---|
| 1635 |
} |
|---|
| 1636 |
return ($string); |
|---|
| 1637 |
} |
|---|
| 1638 |
} |
|---|
| 1639 |
|
|---|
| 1640 |
|
|---|
| 1641 |
|
|---|
| 1642 |
|
|---|
| 1643 |
|
|---|
| 1644 |
|
|---|
| 1645 |
|
|---|
| 1646 |
|
|---|
| 1647 |
|
|---|
| 1648 |
|
|---|
| 1649 |
|
|---|
| 1650 |
|
|---|
| 1651 |
if (!function_exists('stripos')) { |
|---|
| 1652 |
function stripos($haystack, $needle, $offset=0) { |
|---|
| 1653 |
$textlib = textlib_get_instance(); |
|---|
| 1654 |
|
|---|
| 1655 |
return $textlib->strpos($textlib->strtoupper($haystack), $textlib->strtoupper($needle), $offset); |
|---|
| 1656 |
} |
|---|
| 1657 |
} |
|---|
| 1658 |
|
|---|
| 1659 |
|
|---|
| 1660 |
|
|---|
| 1661 |
|
|---|
| 1662 |
|
|---|
| 1663 |
|
|---|
| 1664 |
|
|---|
| 1665 |
|
|---|
| 1666 |
|
|---|
| 1667 |
function check_php_version($version='4.1.0') { |
|---|
| 1668 |
return (version_compare(phpversion(), $version) >= 0); |
|---|
| 1669 |
} |
|---|
| 1670 |
|
|---|
| 1671 |
|
|---|
| 1672 |
|
|---|
| 1673 |
|
|---|
| 1674 |
|
|---|
| 1675 |
|
|---|
| 1676 |
|
|---|
| 1677 |
|
|---|
| 1678 |
|
|---|
| 1679 |
|
|---|
| 1680 |
|
|---|
| 1681 |
|
|---|
| 1682 |
function check_browser_version($brand='MSIE', $version=5.5) { |
|---|
| 1683 |
$agent = $_SERVER['HTTP_USER_AGENT']; |
|---|
| 1684 |
|
|---|
| 1685 |
$agent)) { |
|---|
| 1686 |
false; |
|---|
| 1687 |
|
|---|
| 1688 |
|
|---|
| 1689 |
$brand) { |
|---|
| 1690 |
|
|---|
| 1691 |
'Gecko': |
|---|
| 1692 |
|
|---|
| 1693 |
if (substr_count($agent, 'Camino')) { |
|---|
| 1694 |
|
|---|
| 1695 |
$version = 20041110; |
|---|
| 1696 |
|
|---|
| 1697 |
|
|---|
| 1698 |
|
|---|
| 1699 |
|
|---|
| 1700 |
if (preg_match("/Gecko\/([0-9]+)/i", $agent, $match)) { |
|---|
| 1701 |
$match[1] > $version) { |
|---|
| 1702 |
true; |
|---|
| 1703 |
|
|---|
| 1704 |
|
|---|
| 1705 |
|
|---|
| 1706 |
|
|---|
| 1707 |
|
|---|
| 1708 |
'MSIE': |
|---|
| 1709 |
|
|---|
| 1710 |
if (strpos($agent, 'Opera')) { |
|---|
| 1711 |
return false; |
|---|
| 1712 |
|
|---|
| 1713 |
$string = explode(';', $agent); |
|---|
| 1714 |
$string[1])) { |
|---|
| 1715 |
false; |
|---|
| 1716 |
|
|---|
| 1717 |
$string = explode(' ', trim($string[1])); |
|---|
| 1718 |
$string[0]) and !isset($string[1])) { |
|---|
| 1719 |
false; |
|---|
| 1720 |
|
|---|
| 1721 |
$string[0] == $brand and (float)$string[1] >= $version ) { |
|---|
| 1722 |
true; |
|---|
| 1723 |
|
|---|
| 1724 |
|
|---|
| 1725 |
|
|---|
| 1726 |
|
|---|
| 1727 |
|
|---|
| 1728 |
false; |
|---|
| 1729 |
|
|---|
| 1730 |
|
|---|
| 1731 |
|
|---|
| 1732 |
|
|---|
| 1733 |
|
|---|
| 1734 |
|
|---|
| 1735 |
|
|---|
| 1736 |
|
|---|
| 1737 |
|
|---|
| 1738 |
|
|---|
| 1739 |
|
|---|
| 1740 |
|
|---|
| 1741 |
|
|---|
| 1742 |
|
|---|
| 1743 |
function checked(&$var, $set_value = 1, $unset_value = 0) { |
|---|
| 1744 |
|
|---|
| 1745 |
if (empty($var)) { |
|---|
| 1746 |
$var = $unset_value; |
|---|
| 1747 |
} else { |
|---|
| 1748 |
$var = $set_value; |
|---|
| 1749 |
} |
|---|
| 1750 |
} |
|---|
| 1751 |
|
|---|
| 1752 |
|
|---|
| 1753 |
|
|---|
| 1754 |
|
|---|
| 1755 |
|
|---|
| 1756 |
|
|---|
| 1757 |
|
|---|
| 1758 |
|
|---|
| 1759 |
|
|---|
| 1760 |
function frmchecked(&$var, $true_value = 'checked', $false_value = '') { |
|---|
| 1761 |
|
|---|
| 1762 |
if ($var) { |
|---|
| 1763 |
echo $true_value; |
|---|
| 1764 |
} else { |
|---|
| 1765 |
echo $false_value; |
|---|
| 1766 |
} |
|---|
| 1767 |
} |
|---|
| 1768 |
|
|---|
| 1769 |
|
|---|
| 1770 |
|
|---|
| 1771 |
|
|---|
| 1772 |
function close_window_button($name='closewindow') { |
|---|
| 1773 |
|
|---|
| 1774 |
echo '<div style="text-align: center;">' . "\n"; |
|---|
| 1775 |
echo '<script type="text/javascript">' . "\n"; |
|---|
| 1776 |
echo '<!--' . "\n"; |
|---|
| 1777 |
echo "document.write('<form>');\n"; |
|---|
| 1778 |
echo "document.write('<input type=\"button\" onclick=\"self.close();\" value=\"".__gettext("Close this window")."\" />');\n"; |
|---|
| 1779 |
echo "document.write('<\/form>');\n"; |
|---|
| 1780 |
echo '-->' . "\n"; |
|---|
| 1781 |
echo '</script>' . "\n"; |
|---|
| 1782 |
echo '<noscript>' . "\n"; |
|---|
| 1783 |
print_string($name); |
|---|
| 1784 |
echo '</noscript>' . "\n"; |
|---|
| 1785 |
echo '</div>' . "\n"; |
|---|
| 1786 |
} |
|---|
| 1787 |
|
|---|
| 1788 |
|
|---|
| 1789 |
|
|---|
| 1790 |
|
|---|
| 1791 |
function close_window($delay=0) { |
|---|
| 1792 |
echo '<script language="JavaScript" type="text/javascript">'."\n"; |
|---|
| 1793 |
echo '<!--'."\n"; |
|---|
| 1794 |
if ($delay) { |
|---|
| 1795 |
sleep($delay); |
|---|
| 1796 |
} |
|---|
| 1797 |
echo 'self.close();'."\n"; |
|---|
| 1798 |
echo '-->'."\n"; |
|---|
| 1799 |
echo '</script>'."\n"; |
|---|
| 1800 |
exit; |
|---|
| 1801 |
} |
|---|
| 1802 |
|
|---|
| 1803 |
|
|---|
| 1804 |
|
|---|
| 1805 |
|
|---|
| 1806 |
|
|---|
| 1807 |
|
|---|
| 1808 |
|
|---|
| 1809 |
|
|---|
| 1810 |
|
|---|
| 1811 |
function choose_from_menu ($options, $name, $selected='', $nothing='choose', $script='', |
|---|
| 1812 |
$nothingvalue='0', $return=false, $disabled=false, $tabindex=0) { |
|---|
| 1813 |
|
|---|
| 1814 |
if ($nothing == 'choose') { |
|---|
| 1815 |
$nothing = __gettext('Choose') .'...'; |
|---|
| 1816 |
} |
|---|
| 1817 |
|
|---|
| 1818 |
$attributes = ($script) ? 'onchange="'. $script .'"' : ''; |
|---|
| 1819 |
if ($disabled) { |
|---|
| 1820 |
$attributes .= ' disabled="disabled"'; |
|---|
| 1821 |
} |
|---|
| 1822 |
|
|---|
| 1823 |
if ($tabindex) { |
|---|
| 1824 |
$attributes .= ' tabindex="'.$tabindex.'"'; |
|---|
| 1825 |
} |
|---|
| 1826 |
|
|---|
| 1827 |
$output = '<select id="menu'.$name.'" name="'. $name .'" '. $attributes .'>' . "\n"; |
|---|
| 1828 |
if ($nothing) { |
|---|
| 1829 |
$output .= ' <option value="'. $nothingvalue .'"'. "\n"; |
|---|
| 1830 |
if ($nothingvalue === $selected) { |
|---|
| 1831 |
$output .= ' selected="selected"'; |
|---|
| 1832 |
} |
|---|
| 1833 |
$output .= '>'. $nothing .'</option>' . "\n"; |
|---|
| 1834 |
} |
|---|
| 1835 |
if (!empty($options)) { |
|---|
| 1836 |
foreach ($options as $value => $label) { |
|---|
| 1837 |
$output .= ' <option value="'. $value .'"'; |
|---|
| 1838 |
if ($value === $selected) { |
|---|
| 1839 |
$output .= ' selected="selected"'; |
|---|
| 1840 |
} |
|---|
| 1841 |
if ($label === '') { |
|---|
| 1842 |
$output .= '>'. $value .'</option>' . "\n"; |
|---|
| 1843 |
} else { |
|---|
| 1844 |
$output .= '>'. $label .'</option>' . "\n"; |
|---|
| 1845 |
} |
|---|
| 1846 |
} |
|---|
| 1847 |
} |
|---|
| 1848 |
$output .= '</select>' . "\n"; |
|---|
| 1849 |
|
|---|
| 1850 |
if ($return) { |
|---|
| 1851 |
return $output; |
|---|
| 1852 |
} else { |
|---|
| 1853 |
echo $output; |
|---|
| 1854 |
} |
|---|
| 1855 |
} |
|---|
| 1856 |
|
|---|
| 1857 |
|
|---|
| 1858 |
|
|---|
| 1859 |
|
|---|
| 1860 |
|
|---|
| 1861 |
function choose_from_menu_nested($options,$name,$selected='',$nothing='choose',$script = '', |
|---|
| 1862 |
$nothingvalue=0,$return=false,$disabled=false,$tabindex=0) { |
|---|
| 1863 |
|
|---|
| 1864 |
if ($nothing == 'choose') { |
|---|
| 1865 |
$nothing = __gettext('Choose') .'...'; |
|---|
| 1866 |
} |
|---|
| 1867 |
|
|---|
| 1868 |
$attributes = ($script) ? 'onchange="'. $script .'"' : ''; |
|---|
| 1869 |
if ($disabled) { |
|---|
| 1870 |
$attributes .= ' disabled="disabled"'; |
|---|
| 1871 |
} |
|---|
| 1872 |
|
|---|
| 1873 |
if ($tabindex) { |
|---|
| 1874 |
$attributes .= ' tabindex="'.$tabindex.'"'; |
|---|
| 1875 |
} |
|---|
| 1876 |
|
|---|
| 1877 |
$output = '<select id="menu'.$name.'" name="'. $name .'" '. $attributes .'>' . "\n"; |
|---|
| 1878 |
if ($nothing) { |
|---|
| 1879 |
$output .= ' <option value="'. $nothingvalue .'"'. "\n"; |
|---|
| 1880 |
if ($nothingvalue === $selected) { |
|---|
| 1881 |
$output .= ' selected="selected"'; |
|---|
| 1882 |
} |
|---|
| 1883 |
$output .= '>'. $nothing .'</option>' . "\n"; |
|---|
| 1884 |
} |
|---|
| 1885 |
if (!empty($options)) { |
|---|
| 1886 |
foreach ($options as $section => $values) { |
|---|
| 1887 |
$output .= ' <optgroup label="'.$section.'">'."\n"; |
|---|
| 1888 |
foreach ($values as $value => $label) { |
|---|
| 1889 |
$output .= ' <option value="'. $value .'"'; |
|---|
| 1890 |
if ($value === $selected) { |
|---|
| 1891 |
$output .= ' selected="selected"'; |
|---|
| 1892 |
} |
|---|
| 1893 |
if ($label === '') { |
|---|
| 1894 |
$output .= '>'. $value .'</option>' . "\n"; |
|---|
| 1895 |
} else { |
|---|
| 1896 |
$output .= '>'. $label .'</option>' . "\n"; |
|---|
| 1897 |
} |
|---|
| 1898 |
} |
|---|
| 1899 |
$output .= ' </optgroup>'."\n"; |
|---|
| 1900 |
} |
|---|
| 1901 |
} |
|---|
| 1902 |
$output .= '</select>' . "\n"; |
|---|
| 1903 |
|
|---|
| 1904 |
if ($return) { |
|---|
| 1905 |
return $output; |
|---|
| 1906 |
} else { |
|---|
| 1907 |
echo $output; |
|---|
| 1908 |
} |
|---|
| 1909 |
} |
|---|
| 1910 |
|
|---|
| 1911 |
|
|---|
| 1912 |
|
|---|
| 1913 |
|
|---|
| 1914 |
|
|---|
| 1915 |
|
|---|
| 1916 |
|
|---|
| 1917 |
|
|---|
| 1918 |
|
|---|
| 1919 |
function choose_from_radio ($options, $name, $checked='') { |
|---|
| 1920 |
|
|---|
| 1921 |
static $idcounter = 0; |
|---|
| 1922 |
|
|---|
| 1923 |
if (!$name) { |
|---|
| 1924 |
$name = 'unnamed'; |
|---|
| 1925 |
} |
|---|
| 1926 |
|
|---|
| 1927 |
$output = '<span class="radiogroup '.$name."\">\n"; |
|---|
| 1928 |
|
|---|
| 1929 |
if (!empty($options)) { |
|---|
| 1930 |
$currentradio = 0; |
|---|
| 1931 |
foreach ($options as $value => $label) { |
|---|
| 1932 |
$htmlid = 'auto-rb'.sprintf('%04d', ++$idcounter); |
|---|
| 1933 |
$output .= ' <span class="radioelement '.$name.' rb'.$currentradio."\">"; |
|---|
| 1934 |
$output .= '<input name="'.$name.'" id="'.$htmlid.'" type="radio" value="'.$value.'"'; |
|---|
| 1935 |
if ($value == $checked) { |
|---|
| 1936 |
$output .= ' checked="checked"'; |
|---|
| 1937 |
} |
|---|
| 1938 |
if ($label === '') { |
|---|
| 1939 |
$output .= ' /> <label for="'.$htmlid.'">'. $value .'</label></span>' . "\n"; |
|---|
| 1940 |
} else { |
|---|
| 1941 |
$output .= ' /> <label for="'.$htmlid.'">'. $label .'</label></span>' . "\n"; |
|---|
| 1942 |
} |
|---|
| 1943 |
$currentradio = ($currentradio + 1) % 2; |
|---|
| 1944 |
} |
|---|
| 1945 |
} |
|---|
| 1946 |
|
|---|
| 1947 |
$output .= '</span>' . "\n"; |
|---|
| 1948 |
|
|---|
| 1949 |
echo $output; |
|---|
| 1950 |
} |
|---|
| 1951 |
|
|---|
| 1952 |
|
|---|
| 1953 |
|
|---|
| 1954 |
|
|---|
| 1955 |
|
|---|
| 1956 |
|
|---|
| 1957 |
|
|---|
| 1958 |
|
|---|
| 1959 |
|
|---|
| 1960 |
function print_checkbox ($name, $value, $checked = true, $label = '', $alt = '', $script='',$return=false) { |
|---|
| 1961 |
|
|---|
| 1962 |
static $idcounter = 0; |
|---|
| 1963 |
|
|---|
| 1964 |
if (!$name) { |
|---|
| 1965 |
$name = 'unnamed'; |
|---|
| 1966 |
} |
|---|
| 1967 |
|
|---|
| 1968 |
if (!$alt) { |
|---|
| 1969 |
$alt = 'checkbox'; |
|---|
| 1970 |
} |
|---|
| 1971 |
|
|---|
| 1972 |
if ($checked) { |
|---|
| 1973 |
$strchecked = ' checked="checked"'; |
|---|
| 1974 |
} |
|---|
| 1975 |
|
|---|
| 1976 |
$htmlid = 'auto-cb'.sprintf('%04d', ++$idcounter); |
|---|
| 1977 |
$output = '<span class="checkbox '.$name."\">"; |
|---|
| 1978 |
$output .= '<input name="'.$name.'" id="'.$htmlid.'" type="checkbox" value="'.$value.'" alt="'.$alt.'"'.$strchecked.' '.((!empty($script)) ? ' onclick="'.$script.'" ' : '').' />'; |
|---|
| 1979 |
if(!empty($label)) { |
|---|
| 1980 |
$output .= ' <label for="'.$htmlid.'">'.$label.'</label>'; |
|---|
| 1981 |
} |
|---|
| 1982 |
$output .= '</span>'."\n"; |
|---|
| 1983 |
|
|---|
| 1984 |
if (empty($return)) { |
|---|
| 1985 |
echo $output; |
|---|
| 1986 |
} else { |
|---|
| 1987 |
return $output; |
|---|
| 1988 |
} |
|---|
| 1989 |
|
|---|
| 1990 |
} |
|---|
| 1991 |
|
|---|
| 1992 |
|
|---|
| 1993 |
|
|---|
| 1994 |
|
|---|
| 1995 |
|
|---|
| 1996 |
|
|---|
| 1997 |
|
|---|
| 1998 |
|
|---|
| 1999 |
function print_textfield ($name, $value, $alt = '',$size=50,$maxlength= 0,$return=false) { |
|---|
| 2000 |
|
|---|
| 2001 |
static $idcounter = 0; |
|---|
| 2002 |
|
|---|
| 2003 |
if (empty($name)) { |
|---|
| 2004 |
$name = 'unnamed'; |
|---|
| 2005 |
} |
|---|
| 2006 |
|
|---|
| 2007 |
if (empty($alt)) { |
|---|
| 2008 |
$alt = 'textfield'; |
|---|
| 2009 |
} |
|---|
| 2010 |
|
|---|
| 2011 |
if (!empty($maxlength)) { |
|---|
| 2012 |
$maxlength = ' maxlength="'.$maxlength.'" '; |
|---|
| 2013 |
} |
|---|
| 2014 |
|
|---|
| 2015 |
$htmlid = 'auto-cb'.sprintf('%04d', ++$idcounter); |
|---|
| 2016 |
$output = '<span class="textfield '.$name."\">"; |
|---|
| 2017 |
$output .= '<input name="'.$name.'" id="'.$htmlid.'" type="text" value="'.$value.'" size="'.$size.'" '.$maxlength.' alt="'.$alt.'" />'; |
|---|
| 2018 |
|
|---|
| 2019 |
$output .= '</span>'."\n"; |
|---|
| 2020 |
|
|---|
| 2021 |
if (empty($return)) { |
|---|
| 2022 |
echo $output; |
|---|
| 2023 |
} else { |
|---|
| 2024 |
return $output; |
|---|
| 2025 |
} |
|---|
| 2026 |
|
|---|
| 2027 |
} |
|---|
| 2028 |
|
|---|
| 2029 |
|
|---|
| 2030 |
|
|---|
| 2031 |
|
|---|
| 2032 |
|
|---|
| 2033 |
function maxusers_limit() { |
|---|
| 2034 |
global $CFG; |
|---|
| 2035 |
|
|---|
| 2036 |
$limit = isset($CFG->maxusers) ? intval($CFG->maxusers) : 0; |
|---|
| 2037 |
|
|---|
| 2038 |
if ($limit > 0 && count_users('person') >= $limit) { |
|---|
| 2039 |
return true; |
|---|
| 2040 |
} |
|---|
| 2041 |
|
|---|
| 2042 |
return false; |
|---|
| 2043 |
} |
|---|
| 2044 |
|
|---|
| 2045 |
|
|---|
| 2046 |
|
|---|
| 2047 |
|
|---|
| 2048 |
|
|---|
| 2049 |
|
|---|
| 2050 |
function validate_username($username) { |
|---|
| 2051 |
global $CFG; |
|---|
| 2052 |
|
|---|
| 2053 |
$minchars = isset($CFG->username_minchars) ? intval($CFG->username_minchars) : 3; |
|---|
| 2054 |
$maxchars = isset($CFG->username_maxchars) ? intval($CFG->username_maxchars) : 12; |
|---|
| 2055 |
|
|---|
| 2056 |
|
|---|
| 2057 |
$regex = sprintf('/^[A-Za-z0-9]{%d,%d}$/i', $minchars, $maxchars); |
|---|
| 2058 |
|
|---|
| 2059 |
// TODO: should allow plugins to extend validation? |
|---|
| 2060 |
|
|---|
| 2061 |
if (!preg_match($regex, $username)) { |
|---|
| 2062 |
return false; |
|---|
| 2063 |
} |
|---|
| 2064 |
|
|---|
| 2065 |
return true; |
|---|
| 2066 |
} |
|---|
| 2067 |
|
|---|
| 2068 |
|
|---|
| 2069 |
|
|---|
| 2070 |
|
|---|
| 2071 |
|
|---|
| 2072 |
|
|---|
| 2073 |
function username_is_available($username) { |
|---|
| 2074 |
|
|---|
| 2075 |
if (!validate_username($username)) { |
|---|
| 2076 |
return false; |
|---|
| 2077 |
} |
|---|
| 2078 |
|
|---|
| 2079 |
if (record_exists('users', 'username', $username)) { |
|---|
| 2080 |
return false; |
|---|
| 2081 |
} |
|---|
| 2082 |
|
|---|
| 2083 |
return true; |
|---|
| 2084 |
} |
|---|
| 2085 |
|
|---|
| 2086 |
|
|---|
| 2087 |
|
|---|
| 2088 |
|
|---|
| 2089 |
|
|---|
| 2090 |
|
|---|
| 2091 |
|
|---|
| 2092 |
function validate_password($password1, $password2=null) { |
|---|
| 2093 |
global $CFG; |
|---|
| 2094 |
|
|---|
| 2095 |
$minchars = isset($CFG->password_minchars) ? intval($CFG->password_minchars) : 6; |
|---|
| 2096 |
$maxchars = isset($CFG->password_maxchars) ? intval($CFG->password_maxchars) : 32; |
|---|
| 2097 |
|
|---|
| 2098 |
|
|---|
| 2099 |
// could help to enforce passwords |
|---|
| 2100 |
|
|---|
| 2101 |
if (empty($password1) || (empty($password2) || $password1 != $password2)) { |
|---|
| 2102 |
return false; |
|---|
| 2103 |
} |
|---|
| 2104 |
|
|---|
| 2105 |
|
|---|
| 2106 |
/* |
|---|
| 2107 |
if (!preg_match('/^[a-z0-9]*$/i', $password1)) { // only allow letters and numbers |
|---|
| 2108 |
return false; |
|---|
| 2109 |
} |
|---|
| 2110 |
*/ |
|---|
| 2111 |
|
|---|
| 2112 |
$len = strlen($password1); |
|---|
| 2113 |
|
|---|
| 2114 |
if ($len < $minchars || $len > $maxchars) { |
|---|
| 2115 |
return false; |
|---|
| 2116 |
} |
|---|
| 2117 |
|
|---|
| 2118 |
return true; |
|---|
| 2119 |
} |
|---|
| 2120 |
|
|---|
| 2121 |
|
|---|
| 2122 |
|
|---|
| 2123 |
|
|---|
| 2124 |
|
|---|
| 2125 |
|
|---|
| 2126 |
|
|---|
| 2127 |
|
|---|
| 2128 |
function validate_email($address) { |
|---|
| 2129 |
|
|---|
| 2130 |
global $CFG; |
|---|
| 2131 |
|
|---|
| 2132 |
if (ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'. |
|---|
| 2133 |
'@'. |
|---|
| 2134 |
'[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'. |
|---|
| 2135 |
'[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$', |
|---|
| 2136 |
$address)) { |
|---|
| 2137 |
|
|---|
| 2138 |
if ($CFG->emailfilter != "") { |
|---|
| 2139 |
$domain = substr($address,strpos($address,"@")+1); |
|---|
| 2140 |
if (substr_count($CFG->emailfilter, $domain) == 0) { |
|---|
| 2141 |
return false; |
|---|
| 2142 |
} |
|---|
| 2143 |
} |
|---|
| 2144 |
|
|---|
| 2145 |
return true; |
|---|
| 2146 |
|
|---|
| 2147 |
} else { |
|---|
| 2148 |
return false; |
|---|
| 2149 |
} |
|---|
| 2150 |
} |
|---|
| 2151 |
|
|---|
| 2152 |
|
|---|
| 2153 |
|
|---|
| 2154 |
|
|---|
| 2155 |
|
|---|
| 2156 |
|
|---|
| 2157 |
|
|---|
| 2158 |
|
|---|
| 2159 |
function detect_munged_arguments($string, $allowdots=1) { |
|---|
| 2160 |
if (substr_count($string, '..') > $allowdots) { |
|---|
| 2161 |
return true; |
|---|
| 2162 |
} |
|---|
| 2163 |
if (ereg('[\|\`]', $string)) { |
|---|
| 2164 |
return true; |
|---|
| 2165 |
} |
|---|
| 2166 |
if (empty($string) or $string == '/') { |
|---|
| 2167 |
return true; |
|---|
| 2168 |
} |
|---|
| 2169 |
|
|---|
| 2170 |
return false; |
|---|
| 2171 |
} |
|---|
| 2172 |
|
|---|
| 2173 |
|
|---|
| 2174 |
|
|---|
| 2175 |
|
|---|
| 2176 |
|
|---|
| 2177 |
|
|---|
| 2178 |
|
|---|
| 2179 |
|
|---|
| 2180 |
|
|---|
| 2181 |
|
|---|
| 2182 |
|
|---|
| 2183 |
|
|---|
| 2184 |
function format_text_menu() { |
|---|
| 2185 |
|
|---|
| 2186 |
return array (FORMAT_MOODLE => __gettext('Elgg auto-format'), |
|---|
| 2187 |
FORMAT_HTML => __gettext('HTML format'), |
|---|
| 2188 |
FORMAT_PLAIN => __gettext('Plain text format'), |
|---|
| 2189 |
FORMAT_MARKDOWN => __gettext('Markdown format')); |
|---|
| 2190 |
} |
|---|
| 2191 |
|
|---|
| 2192 |
|
|---|
| 2193 |
|
|---|
| 2194 |
|
|---|
| 2195 |
|
|---|
| 2196 |
|
|---|
| 2197 |
|
|---|
| 2198 |
|
|---|
| 2199 |
|
|---|
| 2200 |
|
|---|
| 2201 |
|
|---|
| 2202 |
|
|---|
| 2203 |
|
|---|
| 2204 |
|
|---|
| 2205 |
|
|---|
| 2206 |
|
|---|
| 2207 |
|
|---|
| 2208 |
|
|---|
| 2209 |
|
|---|
| 2210 |
function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL ) { |
|---|
| 2211 |
|
|---|
| 2212 |
global $CFG, $course; |
|---|
| 2213 |
|
|---|
| 2214 |
if (!isset($options->noclean)) { |
|---|
| 2215 |
$options->noclean=false; |
|---|
| 2216 |
} |
|---|
| 2217 |
if (!isset($options->smiley)) { |
|---|
| 2218 |
$options->smiley=true; |
|---|
| 2219 |
} |
|---|
| 2220 |
if (!isset($options->filter)) { |
|---|
| 2221 |
$options->filter=true; |
|---|
| 2222 |
} |
|---|
| 2223 |
if (!isset($options->para)) { |
|---|
| 2224 |
$options->para=true; |
|---|
| 2225 |
} |
|---|
| 2226 |
if (!isset($options->newlines)) { |
|---|
| 2227 |
$options->newlines=true; |
|---|
| 2228 |
} |
|---|
| 2229 |
|
|---|
| 2230 |
if (empty($courseid)) { |
|---|
| 2231 |
if (!empty($course->id)) { |
|---|
| 2232 |
$courseid = $course->id; |
|---|
| 2233 |
} |
|---|
| 2234 |
} |
|---|
| 2235 |
|
|---|
| 2236 |
|
|---|
| 2237 |
if (!empty($CFG->cachetext)) { |
|---|
| 2238 |
$time = time() - $CFG->cachetext; |
|---|
| 2239 |
$md5key = md5($text.'-'.$courseid.$options->noclean.$options->smiley.$options->filter.$options->para.$options->newlines); |
|---|
| 2240 |
if ($cacheitem = get_record_select('cache_text', "md5key = '$md5key' AND timemodified > '$time'")) { |
|---|
| 2241 |
return $cacheitem->formattedtext; |
|---|
| 2242 |
} |
|---|
| 2243 |
} |
|---|
| 2244 |
*/ // DISABLED - there is no cache_text - Penny |
|---|
| 2245 |
|
|---|
| 2246 |
$CFG->currenttextiscacheable = true; |
|---|
| 2247 |
|
|---|
| 2248 |
switch ($format) { |
|---|
| 2249 |
case FORMAT_HTML: |
|---|
| 2250 |
|
|---|
| 2251 |
if (!empty($options->smiley)) { |
|---|
| 2252 |
replace_smilies($text); |
|---|
| 2253 |
} |
|---|
| 2254 |
|
|---|
| 2255 |
if (!isset($options->noclean)) { |
|---|
| 2256 |
$text = clean_text($text, $format, !empty($options->cleanuserfile)); |
|---|
| 2257 |
} |
|---|
| 2258 |
|
|---|
| 2259 |
if (!empty($options->filter)) { |
|---|
| 2260 |
$text = filter_text($text, $courseid); |
|---|
| 2261 |
} |
|---|
| 2262 |
break; |
|---|
| 2263 |
|
|---|
| 2264 |
case FORMAT_PLAIN: |
|---|
| 2265 |
$text = s($text); |
|---|
| 2266 |
$text = rebuildnolinktag($text); |
|---|
| 2267 |
$text = str_replace(' ', ' ', $text); |
|---|
| 2268 |
$text = nl2br($text); |
|---|
| 2269 |
break; |
|---|
| 2270 |
|
|---|
| 2271 |
case FORMAT_WIKI: |
|---|
| 2272 |
|
|---|
| 2273 |
$text = '<p>NOTICE: Wiki-like formatting has been removed from Moodle. You should not be seeing |
|---|
| 2274 |
this message as all texts should have been converted to Markdown format instead. |
|---|
| 2275 |
Please post a bug report to http://moodle.org/bugs with information about where you |
|---|
| 2276 |
saw this message.</p>'.s($text); |
|---|
| 2277 |
break; |
|---|
| 2278 |
|
|---|
| 2279 |
case FORMAT_MARKDOWN: |
|---|
| 2280 |
$text = markdown_to_html($text); |
|---|
| 2281 |
if (!empty($options->smiley)) { |
|---|
| 2282 |
replace_smilies($text); |
|---|
| 2283 |
} |
|---|
| 2284 |
if (empty($options->noclean)) { |
|---|
| 2285 |
$text = clean_text($text, $format); |
|---|
| 2286 |
} |
|---|
| 2287 |
if (!empty($options->filter)) { |
|---|
| 2288 |
$text = filter_text($text, $courseid); |
|---|
| 2289 |
} |
|---|
| 2290 |
break; |
|---|
| 2291 |
|
|---|
| 2292 |
default: |
|---|
| 2293 |
$text = text_to_html($text, $options->smiley, $options->para, $options->newlines); |
|---|
| 2294 |
if (empty($options->noclean)) { |
|---|
| 2295 |
$text = clean_text($text, $format); |
|---|
| 2296 |
} |
|---|
| 2297 |
if (!empty($options->filter)) { |
|---|
| 2298 |
$text = filter_text($text, $courseid); |
|---|
| 2299 |
} |
|---|
| 2300 |
break; |
|---|
| 2301 |
} |
|---|
| 2302 |
|
|---|
| 2303 |
if (!empty($CFG->cachetext) and $CFG->currenttextiscacheable) { |
|---|
| 2304 |
$newrecord->md5key = $md5key; |
|---|
| 2305 |
$newrecord->formattedtext = $text; |
|---|
| 2306 |
$newrecord->timemodified = time(); |
|---|
| 2307 |
@insert_record('cache_text', $newrecord); |
|---|
| 2308 |
} |
|---|
| 2309 |
|
|---|
| 2310 |
return $text; |
|---|
| 2311 |
} |
|---|
| 2312 |
|
|---|
| 2313 |
|---|