00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 function array_to_js($container)
00019 {
00020 $ret = '<script type="text/javascript">'.nl();
00021
00022 ksort($container);
00023 $exists = array();
00024 foreach ($container as $key=>$val) {
00025
00026 $objs = expl('.', $key);
00027 for ($i=0; $i < count($objs)-1; $i++) {
00028 $obj = implode('.', array_slice($objs, 0, $i+1));
00029 if (!in_array($obj, $exists)) {
00030 if ($i == 0) {
00031 $ret .= tab().'var '.$obj.' = '.$obj.' || {};'.nl();
00032 } else {
00033 $ret .= tab().$obj.' = '.$obj.' || {};'.nl();
00034 }
00035 $exists[] = $obj;
00036 }
00037 }
00038 $ret .= tab().''.$key.' = '.json_encode($val).';'.nl();
00039 }
00040 $ret .= '</script>'.nl();
00041 return $ret;
00042 }
00043
00044
00045
00046
00047
00048
00049
00050
00051 function array_unique_element(&$a, $key)
00052 {
00053
00054 for ($cur=0; $cur < count($a); $cur++) {
00055
00056 for ($i=$cur+1; $i < count($a); $i++) {
00057
00058
00059 if ($a[$i][$key] == $a[$cur][$key]) {
00060
00061 array_splice($a, $i, 1);
00062 $i--;
00063 }
00064 }
00065 }
00066 }
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 function dir_has_same_file($dir, $fn, $orig_fn = '')
00079 {
00080
00081 if (substr($dir, -1) == '/') {
00082 $dir = substr($dir, 0, -1);
00083 }
00084 if (empty($orig_fn)) {
00085 $orig_fn = basename($fn);
00086 } else {
00087 $orig_fn = basename($orig_fn);
00088 }
00089
00090 if (($dir_fns = @scandir($dir)) === false) {
00091 return false;
00092 }
00093
00094 if (($i = array_search($orig_fn, $dir_fns)) !== false) {
00095 $a = array_splice($dir_fns, $i, 1);
00096 array_unshift($dir_fns, $a[0]);
00097 }
00098 foreach ($dir_fns as $f) {
00099 if ($f == '.' || $f == '..') {
00100 continue;
00101 }
00102 if (!file_is_different($fn, $dir.'/'.$f)) {
00103 return $f;
00104 }
00105 }
00106 return false;
00107 }
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 function dir_is_different($a, $b)
00118 {
00119 if (substr($a, -1) == '/') {
00120 $a = substr($a, 0, -1);
00121 }
00122 if (substr($b, -1) == '/') {
00123 $b = substr($b, 0, -1);
00124 }
00125
00126 $a_fns = @scandir($a);
00127 $b_fns = @scandir($b);
00128 if ($a_fns !== $b_fns) {
00129 return true;
00130 }
00131
00132 foreach ($a_fns as $fn) {
00133 if ($fn == '.' || $fn == '..') {
00134 continue;
00135 }
00136 if (is_dir($a.'/'.$fn) || is_dir($b.'/'.$fn)) {
00137 if (dir_is_different($a.'/'.$fn, $b.'/'.$fn)) {
00138 return true;
00139 }
00140 } else {
00141 if (file_is_different($a.'/'.$fn, $b.'/'.$fn)) {
00142 return true;
00143 }
00144 }
00145 }
00146
00147 return false;
00148 }
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159 function expl($delimiter, $string)
00160 {
00161 $ret = explode($delimiter, $string);
00162 if (count($ret) == 1 && empty($ret[0])) {
00163 return array();
00164 } else {
00165 return $ret;
00166 }
00167 }
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 function expl_whitesp($s, $honor_quot = false)
00178 {
00179
00180 $whitesp = array(' ', "\t", "\n", "\r", "\0", "\x0B");
00181 $quot = array('"', "'");
00182 $ret = array();
00183
00184 $prev = -1;
00185 $cur_quot = false;
00186
00187 for ($i=0; $i < strlen($s); $i++) {
00188 if ($honor_quot && in_array($s[$i], $quot)) {
00189 if ($cur_quot === false) {
00190
00191 $cur_quot = $s[$i];
00192 } elseif ($cur_quot == $s[$i] && ($i < 1 || $s[$i-1] != "\\")) {
00193
00194 $cur_quot = false;
00195 }
00196 }
00197 if ($cur_quot !== false) {
00198
00199 continue;
00200 }
00201 if (in_array($s[$i], $whitesp)) {
00202 if ($prev+1 == $i) {
00203 $prev++;
00204 } else {
00205 $ret[] = substr($s, $prev+1, $i-$prev-1);
00206 $prev = $i;
00207 }
00208 }
00209 }
00210 if ($prev+2 < $i) {
00211 $ret[] = substr($s, $prev+1);
00212 }
00213
00214 return $ret;
00215 }
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225 function file_is_different($a, $b)
00226 {
00227 if (@filesize($a) !== @filesize($b)) {
00228 return true;
00229 }
00230 if (@md5_file($a) !== @md5_file($b)) {
00231 return true;
00232 } else {
00233 return false;
00234 }
00235 }
00236
00237
00238
00239
00240
00241
00242
00243
00244 function filext($s)
00245 {
00246 $a = expl('.', $s);
00247 if (1 < count($a)) {
00248 return(array_pop($a));
00249 } else {
00250 return '';
00251 }
00252 }
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265 function http_error($code, $header_only = false)
00266 {
00267 switch ($code) {
00268 case 400:
00269 $error = 'Bad Request';
00270 break;
00271 case 404:
00272 $error = 'Not Found';
00273 break;
00274 case 500:
00275 $error = 'Internal Server Error';
00276 break;
00277 default:
00278
00279 return false;
00280 }
00281 header($_SERVER['SERVER_PROTOCOL'].' '.$code.' '.$error);
00282 if (!$header_only) {
00283 echo $error;
00284 die();
00285 } else {
00286 return true;
00287 }
00288 }
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303 function http_digest_check($users, $realm = '')
00304 {
00305
00306 if (empty($_SERVER['PHP_AUTH_DIGEST'])) {
00307 return -1;
00308 } else {
00309 $auth = $_SERVER['PHP_AUTH_DIGEST'];
00310 }
00311
00312
00313 $data = array();
00314 preg_match("/username=\"([^\"]+)\"/i", $auth, $match);
00315 if (isset($match[1])) {
00316 $data['username'] = $match[1];
00317 } else {
00318 return -2;
00319 }
00320 preg_match('/nonce=\"([^\"]+)\"/i', $auth, $match);
00321 if (isset($match[1])) {
00322 $data['nonce'] = $match[1];
00323 } else {
00324 return -2;
00325 }
00326 preg_match('/nc=([0-9a-f]+)/i', $auth, $match);
00327 if (isset($match[1])) {
00328 $data['nc'] = $match[1];
00329 } else {
00330 return -2;
00331 }
00332 preg_match('/cnonce=\"([^\"]+)\"/i', $auth, $match);
00333 if (isset($match[1])) {
00334 $data['cnonce'] = $match[1];
00335 } else {
00336 return -2;
00337 }
00338
00339
00340 preg_match('/qop=\"?([^,\"]+)/i', $auth, $match);
00341 if (isset($match[1])) {
00342 $data['qop'] = $match[1];
00343 } else {
00344 return -2;
00345 }
00346 preg_match('/uri=\"([^\"]+)\"/i', $auth, $match);
00347 if (isset($match[1])) {
00348 $data['uri'] = $match[1];
00349 } else {
00350 return -2;
00351 }
00352 preg_match('/response=\"([^\"]+)\"/i', $auth, $match);
00353 if (isset($match[1])) {
00354 $data['response'] = $match[1];
00355 } else {
00356 return -2;
00357 }
00358
00359
00360 if (!array_key_exists($data['username'], $users)) {
00361 return -3;
00362 }
00363
00364
00365 $a1 = md5($data['username'].':'.str_replace("\"", '', $realm).':'.$users[$data['username']]);
00366 $a2 = md5($_SERVER['REQUEST_METHOD'].':'.$data['uri']);
00367 $valid_response = md5($a1.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.$a2);
00368
00369 if ($data['response'] != $valid_response) {
00370 return -4;
00371 } else {
00372 return 0;
00373 }
00374 }
00375
00376
00377
00378
00379
00380
00381
00382
00383 function http_digest_prompt($realm = '')
00384 {
00385
00386 header($_SERVER['SERVER_PROTOCOL'].' 401 Unauthorized');
00387 header('WWW-Authenticate: Digest realm="'.str_replace("\"", '', $realm).'",qop="auth",nonce="'.uniqid().'",opaque="'.md5(str_replace("\"", '', $realm)).'"');
00388 }
00389
00390
00391
00392
00393
00394
00395
00396
00397 function is_url($s)
00398 {
00399 if (strpos($s, '://') || strtolower(substr($s, 0, 7)) == 'mailto:') {
00400 return true;
00401 } else {
00402 return false;
00403 }
00404 }
00405
00406
00407
00408
00409
00410
00411
00412
00413 function nl($count = 1)
00414 {
00415 $s = '';
00416 while (0 < $count--) {
00417 $s .= "\n";
00418 }
00419 return $s;
00420 }
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431 function pad($s, $num, $chr = ' ')
00432 {
00433 for ($i=strlen($s); $i < $num; $i++) {
00434 $s .= $chr;
00435 }
00436 return $s;
00437 }
00438
00439
00440
00441
00442
00443
00444
00445
00446 function quot($s)
00447 {
00448 return '"'.$s.'"';
00449 }
00450
00451
00452
00453
00454
00455
00456
00457
00458 function rm_recursive($f)
00459 {
00460 if (@is_file($f) || @is_link($f)) {
00461
00462 return @unlink($f);
00463 } else {
00464 if (($childs = @scandir($f)) === false) {
00465 return false;
00466 }
00467
00468 if (substr($f, -1) == '/') {
00469 $f = substr($f, 0, -1);
00470 }
00471 foreach ($childs as $child) {
00472 if ($child == '.' || $child == '..') {
00473 continue;
00474 } else {
00475 rm_recursive($f.'/'.$child);
00476 }
00477 }
00478 return @rmdir($f);
00479 }
00480 }
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491 function serve_file($fn, $dl, $mime = '')
00492 {
00493 if (($size = @filesize($fn)) === false) {
00494 return false;
00495 }
00496 if (empty($mime)) {
00497
00498 $mime = 'application/octet-stream';
00499 }
00500
00501
00502
00503
00504
00505
00506
00507 if ($dl) {
00508
00509 header('Content-Description: File Transfer');
00510 header('Content-Type: application/octet-stream');
00511 header('Content-Disposition: attachment; filename='.basename($fn));
00512 header('Content-Transfer-Encoding: binary');
00513 } else {
00514 header('Content-Type: '.$mime);
00515 }
00516 header('Content-Length: '.$size);
00517 flush();
00518 @readfile($fn);
00519 die();
00520 }
00521
00522
00523
00524
00525
00526
00527
00528
00529 function tab($count = 1)
00530 {
00531 $s = '';
00532 while (0 < $count--) {
00533 $s .= "\t";
00534 }
00535 return $s;
00536 }
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546 function unique_filename($dir, $orig_fn)
00547 {
00548
00549 if (substr($dir, -1) == '/') {
00550 $dir = substr($dir, 0, -1);
00551 }
00552
00553 $fn = basename($orig_fn);
00554 $num = 1;
00555
00556 while (is_file($dir.'/'.$fn) || is_dir($dir.'/'.$fn) || is_link($dir.'/'.$fn)) {
00557
00558
00559
00560 $fn = basename($orig_fn);
00561 if (($p = strpos($fn, '.')) !== false) {
00562 $fn = substr($fn, 0, $p).'_'.(++$num).substr($fn, $p);
00563 } else {
00564 $fn .= '_'.(++$num);
00565 }
00566 }
00567
00568 return $fn;
00569 }
00570
00571
00572
00573
00574
00575
00576
00577
00578 function var_dump_inl($var)
00579 {
00580
00581
00582 if (is_bool($var) && $var) {
00583 return 'true';
00584 } elseif (is_bool($var)) {
00585 return 'false';
00586 }
00587
00588 $ret = print_r($var, true);
00589
00590 $ret = str_replace("\n", ' ', $ret);
00591 $ret = str_replace("\r", ' ', $ret);
00592 $ret = str_replace("\t", ' ', $ret);
00593
00594 do {
00595 $ret = str_replace(' ', ' ', $ret, $cnt);
00596 } while (0 < $cnt);
00597 return trim($ret);
00598 }