OSDN Git Service

embrj
[embrj/master.git] / lib / utility.php
1 <?php
2         function setEncryptCookie($key, $value, $time = 0, $path = '/') {
3                 if (trim(SECURE_KEY) == '') {
4                         setcookie($key, $value, $time, $path);
5                 } else {
6                         setcookie($key, encrypt($value), $time, $path);
7                 }
8         }
9
10         function getEncryptCookie($key) {
11                 if ( isset($_COOKIE[$key]) ) {
12                         if (trim(SECURE_KEY) == '') {
13                                 return $_COOKIE[$key];
14                         } else {
15                                 return decrypt($_COOKIE[$key]);
16                         }
17                 } else { 
18                         return null;
19                 }
20         }
21
22         function getCookie($key) {
23                 if ( isset($_COOKIE[$key]) ) 
24                         return $_COOKIE[$key];
25                 else 
26                         return null;
27         }
28
29         function delCookie($key) {
30                 setcookie($key, '', $_SERVER['REQUEST_TIME']-300, '/');
31         }
32
33         function encrypt($plain_text) {
34                 if ( !function_exists('mcrypt_module_open') ) {
35                         return EDencrypt($plain_text, SECURE_KEY);
36                 }
37                 $td = mcrypt_module_open('blowfish', '', 'cfb', '');
38                 $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
39                 mcrypt_generic_init($td, SECURE_KEY, $iv);
40                 $crypt_text = mcrypt_generic($td, $plain_text);
41                 mcrypt_generic_deinit($td);
42                 return base64_encode($iv.$crypt_text);
43         }
44
45         function decrypt($crypt_text) {
46                 if ( !function_exists('mcrypt_module_open') ) {
47                         return EDdecrypt($crypt_text, SECURE_KEY);
48                 }
49                 $crypt_text = base64_decode($crypt_text);
50                 $td = mcrypt_module_open('blowfish', '', 'cfb', '');
51                 $ivsize = mcrypt_enc_get_iv_size($td);
52                 $iv = substr($crypt_text, 0, $ivsize);
53                 $crypt_text = substr($crypt_text, $ivsize);
54                 mcrypt_generic_init($td, SECURE_KEY, $iv);
55                 $plain_text = mdecrypt_generic($td, $crypt_text);
56                 mcrypt_generic_deinit($td);
57
58                 return $plain_text;
59         }
60
61         if ( !function_exists('mb_strlen') ) {
62                 function mb_strlen($text, $encode) {
63                         if (strtolower($encode) == 'utf-8') {
64                                 return preg_match_all('%(?:
65                                         [\x09\x0A\x0D\x20-\x7E]     # ASCII
66                                         | [\xC2-\xDF][\x80-\xBF]# non-overlong 2-byte
67                                         |  \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
68                                         | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
69                                         |  \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
70                                         |  \xF0[\x90-\xBF][\x80-\xBF]{2}    # planes 1-3
71                                         | [\xF1-\xF3][\x80-\xBF]{3}   # planes 4-15
72                                         |  \xF4[\x80-\x8F][\x80-\xBF]{2}    # plane 16
73                                 )%xs',$text,$out);
74                         }else{
75                                 return strlen($text);
76                         }
77                 }
78         }
79         
80         function keyED($txt,$encrypt_key) {
81
82                 $encrypt_key = md5($encrypt_key);
83                 $ctr=0;
84                 $tmp = "";
85
86                 for ($i=0;$i<strlen($txt);$i++) {
87                         if ($ctr==strlen($encrypt_key)) $ctr=0;
88                         $tmp.= substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1);
89                         $ctr++;
90                 }
91
92                 return $tmp;
93         }
94
95         function EDencrypt($txt,$key) {
96
97                 srand((double)microtime()*1000000);
98                 $encrypt_key = md5(rand(0,32000));
99                 $ctr=0;
100                 $tmp = "";
101
102                 for ($i=0;$i<strlen($txt);$i++) {
103                         if ($ctr==strlen($encrypt_key)) $ctr=0;
104                         $tmp.= substr($encrypt_key,$ctr,1) . (substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1));
105                         $ctr++;
106                 }
107
108                 return keyED($tmp,$key);
109         }
110
111         function EDdecrypt($txt,$key) {
112
113                 $txt = keyED($txt,$key);
114                 $tmp = "";
115
116                 for ($i=0;$i<strlen($txt);$i++) {
117                         $md5 = substr($txt,$i,1);
118                         $i++;
119                         $tmp.= (substr($txt,$i,1) ^ $md5);
120                 }
121
122                 return $tmp;
123
124         }
125 ?>