@@ -235,15 +235,11 @@ public static function hash($str) {
235
235
return hash ('sha256 ' , $ str );
236
236
}
237
237
238
- public static function create_token () {
239
- return self ::create_uid (true );
240
- }
241
-
242
238
public static function save_session_result ($ data ) {
243
- $ uid = self ::create_uid ();
244
- $ _SESSION [$ uid ] = json_encode ($ data );
239
+ $ uuid = self ::uuid ();
240
+ $ _SESSION [$ uuid ] = json_encode ($ data );
245
241
246
- return $ uid ;
242
+ return $ uuid ;
247
243
}
248
244
249
245
public static function get_session_result ($ token ) {
@@ -553,19 +549,6 @@ public static function get_browser_info() {
553
549
'pattern ' => $ pattern
554
550
];
555
551
}
556
- public static function get_random_bytes ($ length = 32 ) {
557
- if (!isset ($ length ) || intval ($ length ) <= 8 ){
558
- $ length = 32 ;
559
- }
560
-
561
- if (function_exists ('random_bytes ' )) {
562
- return random_bytes ($ length );
563
- }
564
-
565
- if (function_exists ('mcrypt_create_iv ' )) {
566
- return mcrypt_create_iv ($ length , MCRYPT_DEV_URANDOM );
567
- }
568
- }
569
552
570
553
/**
571
554
* Returns an base64 encoded encrypted string
@@ -682,102 +665,46 @@ public static function debug($var, $options = null, $return = false) {
682
665
else echo $ result ;
683
666
}
684
667
685
- public static function mysql_now ($ format = "Y-m-d H:i " ) {
686
- return "' " . date ($ format ) . "' " ;
687
- }
688
-
689
- public static function get_file_info ($ filename , $ icon_prefix = 'octicon ' ) {
690
- preg_match ('/\.[^\.]+$/i ' , $ filename , $ ext );
691
- $ return = new stdClass ;
692
- $ extetion = isset ($ ext [0 ]) ? $ ext [0 ] : '' ;
693
- $ category = "" ;
694
- switch (strtolower ($ extetion )) {
695
- case ".pdf " :
696
- case ".doc " :
697
- case ".rtf " :
698
- case ".txt " :
699
- case ".docx " :
700
- case ".xls " :
701
- case ".xlsx " :
702
- $ icon = "$ icon_prefix $ icon_prefix-file-text " ;
703
- $ category = 'document ' ;
704
- break ;
705
- case ".png " :
706
- case ".jpg " :
707
- case ".jpeg " :
708
- case ".gif " :
709
- case ".bmp " :
710
- case ".psd " :
711
- case ".tif " :
712
- case ".tiff " :
713
- $ icon = "$ icon_prefix $ icon_prefix-picture " ;
714
- $ category = "image " ;
715
- break ;
716
- case ".mp3 " :
717
- case ".wav " :
718
- case ".wma " :
719
- case ".m4a " :
720
- case ".m3u " :
721
- $ icon = "$ icon_prefix $ icon_prefix-music " ;
722
- $ category = "audio " ;
723
- break ;
724
- case ".3g2 " :
725
- case ".3gp " :
726
- case ".asf " :
727
- case ".asx " :
728
- case ".avi " :
729
- case ".flv " :
730
- case ".m4v " :
731
- case ".mov " :
732
- case ".mp4 " :
733
- case ".mpg " :
734
- case ".srt " :
735
- case ".swf " :
736
- case ".vob " :
737
- case ".wmv " :
738
- $ icon = "$ icon_prefix $ icon_prefix-film " ;
739
- $ category = "video " ;
740
- break ;
741
- default :
742
- $ icon = "$ icon_prefix $ icon_prefix-file-binary " ;
743
- $ category = "other " ;
744
- break ;
745
- }
746
- $ return ->icon_class = $ icon ;
747
- $ return ->extension = $ extetion ;
748
- $ return ->category = $ category ;
749
- return $ return ;
750
- }
668
+ public static function uuid () {
669
+ if (function_exists ('com_create_guid ' ) === true )
670
+ return trim (com_create_guid (), '{} ' );
751
671
752
- public static function doc_viewer ($ url , $ use_google = false ) {
753
- if ($ use_google ) self ::redirect ("http://docs.google.com/viewer?url= " . urlencode ($ url ));
754
- else self ::redirect ($ url );
672
+ $ data = openssl_random_pseudo_bytes (16 );
673
+ $ data [6 ] = chr (ord ($ data [6 ]) & 0x0f | 0x40 ); // set version to 0100
674
+ $ data [8 ] = chr (ord ($ data [8 ]) & 0x3f | 0x80 ); // set bits 6-7 to 10
675
+ return vsprintf ('%s%s-%s-%s-%s-%s%s%s ' , str_split (bin2hex ($ data ), 4 ));
755
676
}
756
677
757
- public static function create_uid ($ len = 16 ) {
758
- if (is_bool ($ len )) $ len = $ len === true ? 128 : 16 ;
678
+ public static function random_int ($ min , $ max ) {
679
+ if (function_exists ('random_int ' ) === true )
680
+ return random_int ($ min , $ max );
681
+
682
+ $ range = $ max - $ min ;
683
+ if ($ range < 1 ) return $ min ; // not so random...
759
684
760
- $ rand = function ($ min , $ max ) {
761
- $ range = $ max - $ min ;
762
- if ($ range < 1 ) return $ min ; // not so random...
763
- $ log = ceil (log ($ range , 2 ));
764
- $ bytes = (int ) ($ log / 8 ) + 1 ; // length in bytes
765
- $ bits = (int ) $ log + 1 ; // length in bits
766
- $ filter = (int ) (1 << $ bits ) - 1 ; // set all lower bits to 1
767
- do {
768
- $ rnd = hexdec (bin2hex (openssl_random_pseudo_bytes ($ bytes )));
769
- $ rnd = $ rnd & $ filter ; // discard irrelevant bits
770
- } while ($ rnd >= $ range );
771
- return $ min + $ rnd ;
772
- };
685
+ $ log = ceil (log ($ range , 2 ));
686
+ $ bytes = (int ) ($ log / 8 ) + 1 ; // length in bytes
687
+ $ bits = (int ) $ log + 1 ; // length in bits
688
+ $ filter = (int ) (1 << $ bits ) - 1 ; // set all lower bits to 1
773
689
690
+ do {
691
+ $ rnd = hexdec (bin2hex (openssl_random_pseudo_bytes ($ bytes )));
692
+ $ rnd = $ rnd & $ filter ; // discard irrelevant bits
693
+ } while ($ rnd > $ range );
694
+
695
+ return $ min + $ rnd ;
696
+ }
697
+
698
+ public static function token ($ length = 16 ) {
774
699
$ token = "" ;
775
700
$ codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ " ;
776
701
$ codeAlphabet .= "abcdefghijklmnopqrstuvwxyz " ;
777
702
$ codeAlphabet .= "0123456789 " ;
778
- $ max = strlen ($ codeAlphabet ) - 1 ;
779
- for ($ i =0 ; $ i < $ len ; $ i ++)
780
- $ token .= $ codeAlphabet [$ rand (0 , $ max )];
703
+ $ max = strlen ($ codeAlphabet ); // edited
704
+
705
+ for ($ i =0 ; $ i < $ length ; $ i ++) {
706
+ $ token .= $ codeAlphabet [self ::random_int (0 , $ max -1 )];
707
+ }
781
708
782
709
return $ token ;
783
710
}
0 commit comments