forked from BitweaverCMS/kernel
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkernel_lib.php
More file actions
925 lines (832 loc) · 23.3 KB
/
kernel_lib.php
File metadata and controls
925 lines (832 loc) · 23.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
<?php
/**
* @version $Header$
* @package kernel
* @subpackage functions
*/
/**
* some PHP compatability issues need to be dealt with
* array_fill
*/
if( !function_exists( 'array_fill' )) {
require_once( KERNEL_PKG_PATH.'array_fill.func.php' );
}
// str_split
include_once( UTIL_PKG_PATH.'PHP_Compat/Compat/Function/str_split.php' );
/** \brief substr with a utf8 string - works only with $start and $length positive or nuls
* This function is the same as substr but works with multibyte
* In a multybyte sequence, the first byte of a multibyte sequence that represents a non-ASCII character is always in the range 0xC0 to 0xFD
* and it indicates how many bytes follow for this character.
* All further bytes in a multibyte sequence are in the range 0x80 to 0xBF.
*/
/**
* Check mb_substr availability
*/
if( function_exists( 'mb_substr' )) {
mb_internal_encoding( "UTF-8" );
} else {
function mb_substr( $str, $start, $len = '', $encoding = "UTF-8" ) {
$limit = strlen( $str );
for( $s = 0; $start > 0;--$start ) {
if( $s >= $limit ) {
break;
}
if( $str[$s] <= "\x7F" ) {
++$s;
} else {
++$s; // skip length
while( $str[$s] >= "\x80" && $str[$s] <= "\xBF" ) {
++$s;
}
}
}
if( $len == '' ) {
return substr( $str, $s );
}
else {
// found the real end
for( $e = $s; $len > 0; --$len ) {
if( $e >= $limit ) {
break;
}
if( $str[$e] <= "\x7F" ) {
++$e;
} else {
++$e; //skip length
while( $str[$e] >= "\x80" && $str[$e] <= "\xBF" && $e < $limit ) {
++$e;
}
}
}
}
return substr( $str, $s, $e - $s );
}
}
if( !function_exists( 'file_get_contents' )) {
function file_get_contents( $pFile ) {
ob_start();
$retval = @readfile( $pFile );
if( false !== $retval ) { // no readfile error
$retval = ob_get_contents();
}
ob_end_clean();
return $retval;
}
}
/**
* Return system defined temporary directory.
* In Unix, this is usually /tmp
* In Windows, this is usually c:\windows\temp or c:\winnt\temp
*
* @access public
* @return system defined temporary directory.
*/
function get_temp_dir() {
static $tempdir;
if( !$tempdir ) {
global $gTempDir;
if( !empty( $gTempDir ) ) {
$tempdir = $gTempDir;
} else {
if( !is_windows() ) {
$tempfile = tempnam((( @ini_get( 'safe_mode' ))
?( $_SERVER['DOCUMENT_ROOT'] . '/temp/' )
:( FALSE )), 'foo' );
$tempdir = dirname( $tempfile );
@unlink( $tempfile );
} else {
$tempdir = getenv( "TMP" );
}
}
}
return $tempdir;
}
/**
* is_windows
*
* @access public
* @return TRUE if we are on windows, FALSE otherwise
*/
function is_windows() {
static $sWindows;
if( !isset( $sWindows )) {
$sWindows = substr( PHP_OS, 0, 3 ) == 'WIN';
}
return $sWindows;
}
/**
* Recursively create directories
*
* @param array $pTarget target directory
* @param float $pPerms octal permissions
* @access public
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
*/
function mkdir_p( $pTarget, $pPerms = 0755 ) {
// clean up input
$pTarget = str_replace( "//", "/", trim( $pTarget ));
if( empty( $pTarget ) || $pTarget == ';' || $pTarget == "/" ) {
return FALSE;
}
if( ini_get( 'safe_mode' )) {
$pTarget = preg_replace( '/^\/tmp/', $_SERVER['DOCUMENT_ROOT'].'/temp', $pTarget );
}
if( file_exists( $pTarget ) || is_dir( $pTarget )) {
bitdebug( "mkdir_p() - file already exists $pTarget" );
return FALSE;
}
if( !is_windows() ) {
if( substr( $pTarget, 0, 1 ) != '/' ) {
bitdebug( "mkdir_p() - prepending with a /" );
$pTarget = "/$pTarget";
}
if( preg_match( '#\.\.#', $pTarget )) {
bitdebug( "mkdir_p() - We don't allow '..' in path for security reasons: $pTarget" );
return FALSE;
}
}
$oldu = umask( 0 );
// make use of PHP5 recursive mkdir feature
if( version_compare( phpversion(), "5.0.0", ">=" )) {
@mkdir( $pTarget, $pPerms, TRUE );
umask( $oldu );
return TRUE;
} else {
if( @mkdir( $pTarget, $pPerms )) {
bitdebug( "mkdir_p() - creating $pTarget" );
umask( $oldu );
return TRUE;
} else {
umask( $oldu );
$parent = substr( $pTarget, 0, ( strrpos( $pTarget, '/' )));
bitdebug( "mkdir_p() - trying to create parent $parent" );
// recursively create parents
if( mkdir_p( $parent, $pPerms )) {
// make the actual target!
if( @mkdir( $pTarget, $pPerms )) {
return TRUE;
} elseif( !is_dir( $pTarget ) ) {
error_log( "mkdir() - could not create $pTarget" );
}
}
}
}
}
/**
* check to see if particular directories are wroteable by bitweaver
* added check for Windows - wolff_borg - see http://bugs.php.net/bug.php?id=27609
*
* @param array $pPath path to file or dir
* @access public
* @return TRUE on success, FALSE on failure
*/
function bw_is_writeable( $pPath ) {
if( !is_windows() ) {
return is_writeable( $pPath );
} else {
$writeable = FALSE;
if( is_dir( $pPath )) {
$rnd = rand();
$writeable = @fopen( $pPath."/".$rnd, "a" );
if( $writeable ) {
fclose( $writeable );
unlink( $pPath."/".$rnd );
$writeable = TRUE;
}
} else {
$writeable = @fopen( $pPath,"a" );
if( $writeable ) {
fclose( $writeable );
$writeable = TRUE;
}
}
return $writeable;
}
}
/**
* clean up an array of values and remove any dangerous html - particularly useful for cleaning up $_GET and $_REQUEST.
* Turn off urldecode when detoxifying $_GET or $_REQUEST - these two are always urldecoded done by PHP itself (check docs).
* If you urldecode $_GET twice there might be unexpected consequences (like page One%2BTwo --(PHP)--> One+Two --(you)--> One Two).
*
* @param array $pParamHash array to be cleaned
* @param boolean $pHtml set true to escape HTML code as well
* @param boolean $pUrldecode set true to urldecode as well
* @access public
* @return void
*/
function detoxify( &$pParamHash, $pHtml = FALSE, $pUrldecode = TRUE) {
if( !empty( $pParamHash ) && is_array( $pParamHash ) ) {
foreach( $pParamHash as $key => $value ) {
if( isset( $value ) && is_array( $value ) ) {
detoxify( $value, $pHtml, $pUrldecode );
} else {
if( $pHtml ) {
$newValue = $pUrldecode ? urldecode( $value ) : $value;
$pParamHash[$key] = htmlspecialchars( ( $newValue ), ENT_NOQUOTES );
} elseif( preg_match( "/<script[^>]*>/i", urldecode( $value ) ) ) {
unset( $pParamHash[$key] );
}
}
}
}
}
/**
* file_name_to_title
*
* @param string $pFileName
* @access public
* @return clean file name that can be used as title
*/
function file_name_to_title( $pFileName ) {
if( preg_match( '/^[A-Z]:\\\/', $pFileName ) ) {
// MSIE shit file names if passthrough via gigaupload, etc.
// basename will not work - see http://us3.php.net/manual/en/function.basename.php
$tmp = preg_split("[\\\]",$pFileName);
$defaultName = $tmp[count($tmp) - 1];
} elseif( strpos( '.', $pFileName ) ) {
list( $defaultName, $ext ) = explode( '.', $pFileName );
} else {
$defaultName = $pFileName;
}
return( str_replace( '_', ' ', substr( $defaultName, 0, strrpos( $defaultName, '.' ) ) ) );
}
/**
* path_to_url
*
* @param string $pPath Relative path starting in the bitweaver root directory
* @access public
* @return a valid url based on the given path
*/
function storage_path_to_url( $pPath ) {
global $gBitSystem;
$root = !empty( $_REQUEST['uri_mode'] ) ? BIT_BASE_URI.'/' : BIT_ROOT_URL;
return( $gBitSystem->getConfig( 'storage_host', $root ).str_replace( '//', '/', str_replace( '+', '%20', str_replace( '%2F', '/', urlencode( $pPath )))));
}
/**
* simple function to include in deprecated function calls. makes the developer replace with newer code
*
* @param array $pReplace code that needs replacing
* @access public
* @return void
*/
function deprecated( $pReplace = NULL ) {
$trace = debug_backtrace();
if( !empty( $trace[1]['class'] )) {
$function = $trace[1]['class']."::".$trace[1]['function'];
} else {
$function = $trace[1]['function'];
}
$out = "Deprecated function call:\n\tfunction: $function()\n\tfile: ".$trace[1]['file']."\n\tline: ".$trace[1]['line'];
if( !empty( $pReplace ) ) {
$out .= "\n\t".str_replace( "\n", "\n\t", $pReplace );
}
if( !defined( 'IS_LIVE' ) || IS_LIVE == FALSE ) {
vd( $out, FALSE, TRUE );
} else {
error_log( $out );
}
}
/**
* Check that function is enabled on server.
* @access public
* @return TRUE if function is enabled on server, FALSE otherwise
*/
function function_enabled ( $pName ) {
static $disabled = NULL;
if( $disabled == NULL ) {
$functions = @ini_get( 'disable_functions' );
$functions = str_replace( ' ', '', $functions );
$disabled = explode( ',', $functions );
}
return !( in_array( $pName, $disabled ));
}
/**
* html encode all characters
* taken from: http://www.bbsinc.com/iso8859.html
*
* @param sting $pData string that might contain an email address
* @access public
* @return encoded email address
* $note email regex taken from: http://www.regular-expressions.info/regexbuddy/email.html
*/
define( 'EMAIL_ADDRESS_REGEX', '[-a-zA-Z0-9._%+]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}' );
function encode_email_addresses( $pData ) {
$trans = array(
// Upper case
'A' => 'A',
'B' => 'B',
'C' => 'C',
'D' => 'D',
'E' => 'E',
'F' => 'F',
'G' => 'G',
'H' => 'H',
'I' => 'I',
'J' => 'J',
'K' => 'K',
'L' => 'L',
'M' => 'M',
'N' => 'N',
'O' => 'O',
'P' => 'P',
'Q' => 'Q',
'R' => 'R',
'S' => 'S',
'T' => 'T',
'U' => 'U',
'V' => 'V',
'W' => 'W',
'X' => 'X',
'Y' => 'Y',
'Z' => 'Z',
// lower case
'a' => 'a',
'b' => 'b',
'c' => 'c',
'd' => 'd',
'e' => 'e',
'f' => 'f',
'g' => 'g',
'h' => 'h',
'i' => 'i',
'j' => 'j',
'k' => 'k',
'l' => 'l',
'm' => 'm',
'n' => 'n',
'o' => 'o',
'p' => 'p',
'q' => 'q',
'r' => 'r',
's' => 's',
't' => 't',
'u' => 'u',
'v' => 'v',
'w' => 'w',
'x' => 'x',
'y' => 'y',
'z' => 'z',
// digits
'0' => '0',
'1' => '1',
'2' => '2',
'3' => '3',
'4' => '4',
'5' => '5',
'6' => '6',
'7' => '7',
'8' => '8',
'9' => '9',
// special chars
'_' => '_',
'-' => '-',
'.' => '.',
'@' => '@',
//'[' => '[',
//']' => ']',
//'|' => '|',
//'{' => '{',
//'}' => '}',
//'~' => '~',
);
preg_match_all( "!\b".EMAIL_ADDRESS_REGEX."\b!", $pData, $addresses );
foreach( $addresses[0] as $address ) {
$encoded = strtr( $address, $trans );
$pData = preg_replace( "/\b".preg_quote( $address )."\b/", $encoded, $pData );
}
return $pData;
}
/**
* validate email sysntax
* php include as a string
*
* @param $pEmail the file to include
* @return a string with the results of the file
**/
function validate_email_syntax( $pEmail ) {
return( preg_match( "!^".EMAIL_ADDRESS_REGEX."$!", trim( $pEmail )));
}
/**
* get_include_contents -- handy function for getting the contents of a
* php include as a string
*
* @param $pFile the file to include
* @return a string with the results of the file
**/
function get_include_contents($pFile) {
if( is_file( $pFile )) {
ob_start();
global $gContent, $gBitSystem, $gBitSmarty, $gLibertySystem, $gBitUser;
include $pFile;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
return FALSE;
}
/**
* Translate a string
*
* @param string $pString String that needs to be translated
* @access public
* @return void
*/
function tra( $pString ) {
global $gBitLanguage;
return( $gBitLanguage->translate( $pString ) );
}
/**
* recursively remove files and directories
*
* @param string $pPath directory we want to remove
* @param boolean $pFollowLinks follow symlinks or not
* @access public
* @return TRUE on success, FALSE on failure
*/
function unlink_r( $pPath, $pFollowLinks = FALSE ) {
if( is_dir( $pPath ) ) {
if( $dir = opendir( $pPath ) ) {
while( FALSE !== ( $entry = readdir( $dir ) ) ) {
if( is_file( "$pPath/$entry" ) || ( !$pFollowLinks && is_link( "$pPath/$entry" ) ) ) {
unlink( "$pPath/$entry" );
} elseif( is_dir( "$pPath/$entry" ) && $entry != '.' && $entry != '..' ) {
unlink_r( "$pPath/$entry" ) ;
}
}
closedir( $dir ) ;
}
return @rmdir( $pPath );
}
}
/**
* recursively copy the contents of a directory to a new location akin to copy -r
*
* @param array $pSource source directory
* @param array $pTarget target directory
* @access public
* @return void
*/
function copy_r( $pSource, $pTarget ) {
if( is_dir( $pSource )) {
@mkdir_p( $pTarget );
$d = dir( $pSource );
while( FALSE !== ( $entry = $d->read() )) {
if( $entry == '.' || $entry == '..' ) {
continue;
}
$source = $pSource.'/'.$entry;
if( is_dir( $source )) {
copy_r( $source, $pTarget.'/'.$entry );
continue;
}
copy( $source, $pTarget.'/'.$entry );
}
$d->close();
} else {
copy( $pSource, $pTarget );
}
}
/**
* Fetch the contents of a file on a remote host
*
* @param string $pUrl url to file to fetch
* @param boolean $pNoCurl skip the use of curl if this causes problems
* @access public
* @return FALSE on failure, contents of file on success
*/
function bit_http_request( $pUrl, $pNoCurl = FALSE ) {
global $gBitSystem;
$ret = FALSE;
if( !empty( $pUrl )) {
$pUrl = trim( $pUrl );
// rewrite url if sloppy # added a case for https urls
if( !preg_match( "!^https?://!", $pUrl )) {
$pUrl = "http://".$pUrl;
}
if( !preg_match("/^[-_a-zA-Z0-9:\/\.\?&;%=\+]*$/", $pUrl )) {
return FALSE;
}
// try using curl first as it allows the use of a proxy
if( !$pNoCurl && function_exists( 'curl_init' )) {
$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL, $pUrl );
curl_setopt( $curl, CURLOPT_HEADER, 0 );
curl_setopt( $curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT'] );
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $curl, CURLOPT_TIMEOUT, 5 );
// Proxy settings
if( $gBitSystem->isFeatureActive( 'site_use_proxy' )) {
curl_setopt( $curl, CURLOPT_PROXY, $gBitSystem->getConfig( 'site_proxy_host' ));
curl_setopt( $curl, CURLOPT_PROXYPORT, $gBitSystem->getConfig( 'site_proxy_port' ));
curl_setopt( $curl, CURLOPT_HTTPPROXYTUNNEL, 1);
}
$ret = curl_exec( $curl );
curl_close( $curl );
} else {
// try using fsock now
$parsed = parse_url( $pUrl );
if( $fsock = @fsockopen( $parsed['host'], 80, $error['number'], $error['string'], 5 )) {
@fwrite( $fsock, "GET ".$parsed['path'].( !empty( $parsed['query'] ) ? '?'.$parsed['query'] : '' )." HTTP/1.1\r\n" );
@fwrite( $fsock, "HOST: {$parsed['host']}\r\n" );
@fwrite( $fsock, "Connection: close\r\n\r\n" );
$get_info = FALSE;
while( !@feof( $fsock )) {
if( $get_info ) {
$ret .= @fread( $fsock, 1024 );
} else {
if( @fgets( $fsock, 1024 ) == "\r\n" ) {
$get_info = TRUE;
}
}
}
@fclose( $fsock );
if( !empty( $error['string'] )) {
return FALSE;
}
}
}
}
return $ret;
}
/**
* Parse XML Attributes and return an array
*
* this function has a whopper of a RegEx.
* I nabbed it from http://www.phpbuilder.com/annotate/message.php3?id=1000234 - XOXO spiderr
*
* @param array $pString XML type string of parameters
* @access public
* @return TRUE on success, FALSE on failure
*/
function parse_xml_attributes( $pString ) {
//$parameters = array( '', '' );
$parameters = array();
$regexp_str = "/([A-Za-z0-9_-]+)(?:\\s*=\\s*(?:(\"|\')((?:[\\\\].|[^\\\\]?)*?)(?:\\2)|([^=\\s]*)))?/";
preg_match_all( $regexp_str, $pString, $matches, PREG_SET_ORDER );
while( list( $key, $match ) = each( $matches ) ) {
$attrib = $match[1];
$value = $match[sizeof( $match )-1]; // The value can be at different indexes because of optional quotes, but we know it's always at the end.
$value = preg_replace( "/\\\\(.)/","\\1",$value );
$parameters[$attrib] = trim( $value, '\"' );
}
return $parameters;
}
/**
* XML Entity Mandatory Escape Characters
*
* @param array $string
* @param array $quote_style
* @access public
* @return TRUE on success, FALSE on failure
*/
function xmlentities( $string, $quote_style=ENT_QUOTES ) {
static $trans;
if( !isset( $trans )) {
$trans = get_html_translation_table( HTML_ENTITIES, $quote_style );
foreach( $trans as $key => $value ) {
$trans[$key] = '&#'.ord( $key ).';';
}
// dont translate the '&' in case it is part of &xxx;
$trans[chr(38)] = '&';
}
// after the initial translation, _do_ map standalone '&' into '&'
return preg_replace( "/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,5};)/","&" , strtr( $string, $trans ));
}
/**
* Redirect to another page or site
* @param string The url to redirect to
*/
function bit_redirect( $pUrl, $pStatusCode=NULL ) {
$errors = array(
400 => "HTTP/1.1 400 Bad Request",
401 => "HTTP/1.1 401 Unauthorized",
402 => "HTTP/1.1 402 Payment Required",
403 => "HTTP/1.1 403 Forbidden",
404 => "HTTP/1.1 404 Not Found",
405 => "HTTP/1.1 405 Method Not Allowed",
406 => "HTTP/1.1 406 Not Acceptable",
407 => "HTTP/1.1 407 Proxy Authentication Required",
408 => "HTTP/1.1 408 Request Time-out",
409 => "HTTP/1.1 409 Conflict",
410 => "HTTP/1.1 410 Gone",
411 => "HTTP/1.1 411 Length Required",
412 => "HTTP/1.1 412 Precondition Failed",
413 => "HTTP/1.1 413 Request Entity Too Large",
414 => "HTTP/1.1 414 Request-URI Too Large",
415 => "HTTP/1.1 415 Unsupported Media Type",
416 => "HTTP/1.1 416 Requested range not satisfiable",
417 => "HTTP/1.1 417 Expectation Failed",
500 => "HTTP/1.1 500 Internal Server Error",
501 => "HTTP/1.1 501 Not Implemented",
502 => "HTTP/1.1 502 Bad Gateway",
503 => "HTTP/1.1 503 Service Unavailable",
504 => "HTTP/1.1 504 Gateway Time-out"
);
// Handle non-3xx codes separately
if( $pStatusCode && isset( $errors[$pStatusCode] ) ) {
header( $errors[$pStatusCode] );
$pStatusCode = NULL;
}
// clean up URL before executing it
while( strstr( $pUrl, '&&' ) ) {
$pUrl = str_replace( '&&', '&', $pUrl );
}
while( strstr( $pUrl, '&&' ) ) {
$pUrl = str_replace( '&&', '&', $pUrl );
}
// header locates should not have the & in the address it breaks things
while( strstr( $pUrl, '&' ) ) {
$pUrl = str_replace( '&', '&', $pUrl );
}
if( $pStatusCode ) {
header( 'Location: ' . $pUrl, TRUE, $pStatusCode );
} else {
header( 'Location: ' . $pUrl );
}
session_write_close();
exit();
}
/**
* array_diff_keys
*
* @access public
*/
function array_diff_keys() {
$args = func_get_args();
$res = $args[0];
if( !is_array( $res )) {
return array();
}
for( $i = 1; $i < count( $args ); $i++ ) {
if( !is_array( $args[$i] )) {
continue;
}
foreach( $args[$i] as $key => $data ) {
unset( $res[$key] );
}
}
return $res;
}
/**
* trim_array
*
* @param array $pArray
* @access public
*/
function trim_array( &$pArray ) {
if( is_array( $pArray ) ) {
foreach( array_keys( $pArray ) as $key ) {
if( is_string( $pArray[$key] ) ) {
$pArray[$key] = trim( $pArray[$key] );
}
}
}
}
/**
* ordinalize
*
* @param numeric $num Number to append th, st, nd, rd to - only makes sense when languages is english
* @access public
*/
function ordinalize( $num ) {
$ord = '';
if( is_numeric( $num ) ) {
if( $num >= 11 and $num <= 19 ) {
$ord = tra( "th" );
} elseif( $num % 10 == 1 ) {
$ord = tra( "st" );
} elseif( $num % 10 == 2 ) {
$ord = tra( "nd" );
} elseif( $num % 10 == 3 ) {
$ord = tra( "rd" );
} else {
$ord = tra( "th" );
}
}
return $num.$ord;
}
/**
* Cleans file path according to system we're on
*
* @param array $pPath
* @access public
* @return TRUE on success, FALSE on failure
*/
function clean_file_path( $pPath ) {
$pPath = (!empty($_SERVER["SERVER_SOFTWARE"]) && strpos($_SERVER["SERVER_SOFTWARE"],"IIS") ? str_replace( '\/', '\\', $pPath) : $pPath);
return $pPath;
}
function httpScheme() {
return 'http'.( ( isset($_SERVER['HTTPS'] ) && ( $_SERVER['HTTPS'] == 'on' ) ) ? 's' : '' );
}
function httpPrefix() {
return httpScheme().'://'.$_SERVER['HTTP_HOST'];
}
/**
* If an unrecoverable error has occurred, this method should be invoked. script exist occurs
*
* @param string $ pMsg error message to be displayed
* @return none this function will DIE DIE DIE!!!
* @access public
*/
function install_error( $pMsg = null ) {
global $gBitDbType;
// here we decide where to go. if there are no db settings yet, we go the welcome page.
if( isset( $gBitDbType )) {
$step = 1;
} else {
$step = 0;
}
header( "Location: ".httpPrefix().BIT_ROOT_URL."install/install.php?step=".$step );
die;
}
/**
* bitdebug display an debug output when $gDebug is set to TRUE
*
* @param array $pMessage Message to display
* @access public
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
*/
function bitdebug( $pMessage ) {
global $gDebug;
if( !empty( $gDebug )) {
echo "<pre>$pMessage</pre>";
}
}
/**
* pear_check will check to see if a given PEAR module is installed
*
* @param string $pPearModule The name of the module in the format: Image/GraphViz.php
* @access public
* @return string with error message on failure, NULL on success
*/
function pear_check( $pPearModule = NULL ) {
if( !@include_once( "PEAR.php" )) {
return tra( "PEAR is not installed." );
} elseif( !empty( $pPearModule ) && !@include_once( $pPearModule )) {
$module = str_replace( ".php", "", str_replace( "/", "_", $pPearModule ));
return tra( "The PEAR plugin <strong>$module</strong> is not installed. Install it with '<strong>pear install $module</strong>' or use your distribution's package manager.");
} else {
return NULL;
}
}
/**
* A set of compare functions that can be used in conjunction with usort() type functions
*
* @param array $ar1
* @param array $ar2
* @access public
* @return TRUE on success, FALSE on failure
*/
function usort_by_title( $ar1, $ar2 ) {
if( !empty( $ar1['title'] ) && !empty( $ar2['title'] ) ) {
return strcasecmp( $ar1['title'], $ar2['title'] );
} else {
return 0;
}
}
function compare_links( $ar1, $ar2 ) {
return $ar1["links"] - $ar2["links"];
}
function compare_backlinks( $ar1, $ar2 ) {
return $ar1["backlinks"] - $ar2["backlinks"];
}
function r_compare_links( $ar1, $ar2 ) {
return $ar2["links"] - $ar1["links"];
}
function r_compare_backlinks( $ar1, $ar2 ) {
return $ar2["backlinks"] - $ar1["backlinks"];
}
function compare_images( $ar1, $ar2 ) {
return $ar1["images"] - $ar2["images"];
}
function r_compare_images( $ar1, $ar2 ) {
return $ar2["images"] - $ar1["images"];
}
function compare_files( $ar1, $ar2 ) {
return $ar1["files"] - $ar2["files"];
}
function r_compare_files( $ar1, $ar2 ) {
return $ar2["files"] - $ar1["files"];
}
function compare_versions( $ar1, $ar2 ) {
return $ar1["versions"] - $ar2["versions"];
}
function r_compare_versions( $ar1, $ar2 ) {
return $ar2["versions"] - $ar1["versions"];
}
function compare_changed( $ar1, $ar2 ) {
return $ar1["lastChanged"] - $ar2["lastChanged"];
}
function r_compare_changed( $ar1, $ar2 ) {
return $ar2["lastChanged"] - $ar1["lastChanged"];
}
// ======================= deprecated functions =======================
/**
* @deprecated deprecated since version 2.1.0-beta
*/
function chkgd2() {
deprecated( 'Please use get_gd_version() instead' );
}
?>