@@ -51,6 +51,9 @@ |
||
51 | 51 | return $this->getContent($this->getUrl($qrtext, $size)); |
52 | 52 | } |
53 | 53 | |
54 | + /** |
|
55 | + * @param string $value |
|
56 | + */ |
|
54 | 57 | private function decodeColor($value) |
55 | 58 | { |
56 | 59 | return vsprintf('%d-%d-%d', sscanf($value, "%02x%02x%02x")); |
@@ -31,16 +31,16 @@ |
||
31 | 31 | { |
32 | 32 | switch (strtolower($this->format)) |
33 | 33 | { |
34 | - case 'png': |
|
34 | + case 'png': |
|
35 | 35 | return 'image/png'; |
36 | - case 'gif': |
|
36 | + case 'gif': |
|
37 | 37 | return 'image/gif'; |
38 | - case 'jpg': |
|
39 | - case 'jpeg': |
|
38 | + case 'jpg': |
|
39 | + case 'jpeg': |
|
40 | 40 | return 'image/jpeg'; |
41 | - case 'svg': |
|
41 | + case 'svg': |
|
42 | 42 | return 'image/svg+xml'; |
43 | - case 'eps': |
|
43 | + case 'eps': |
|
44 | 44 | return 'application/postscript'; |
45 | 45 | } |
46 | 46 | throw new QRException(sprintf('Unknown MIME-type: %s', $this->format)); |
@@ -59,13 +59,13 @@ |
||
59 | 59 | public function getUrl($qrtext, $size) |
60 | 60 | { |
61 | 61 | return 'https://api.qrserver.com/v1/create-qr-code/' |
62 | - . '?size=' . $size . 'x' . $size |
|
63 | - . '&ecc=' . strtoupper($this->errorcorrectionlevel) |
|
64 | - . '&margin=' . $this->margin |
|
65 | - . '&qzone=' . $this->qzone |
|
66 | - . '&bgcolor=' . $this->decodeColor($this->bgcolor) |
|
67 | - . '&color=' . $this->decodeColor($this->color) |
|
68 | - . '&format=' . strtolower($this->format) |
|
69 | - . '&data=' . rawurlencode($qrtext); |
|
62 | + . '?size='.$size.'x'.$size |
|
63 | + . '&ecc='.strtoupper($this->errorcorrectionlevel) |
|
64 | + . '&margin='.$this->margin |
|
65 | + . '&qzone='.$this->qzone |
|
66 | + . '&bgcolor='.$this->decodeColor($this->bgcolor) |
|
67 | + . '&color='.$this->decodeColor($this->color) |
|
68 | + . '&format='.strtolower($this->format) |
|
69 | + . '&data='.rawurlencode($qrtext); |
|
70 | 70 | } |
71 | 71 | } |
72 | 72 | \ No newline at end of file |
@@ -14,8 +14,9 @@ |
||
14 | 14 | |
15 | 15 | function __construct($verifyssl = false, $errorcorrectionlevel = 'L', $margin = 4, $qzone = 1, $bgcolor = 'ffffff', $color = '000000', $format = 'png') |
16 | 16 | { |
17 | - if (!is_bool($verifyssl)) |
|
18 | - throw new QRException('VerifySSL must be bool'); |
|
17 | + if (!is_bool($verifyssl)) { |
|
18 | + throw new QRException('VerifySSL must be bool'); |
|
19 | + } |
|
19 | 20 | |
20 | 21 | $this->verifyssl = $verifyssl; |
21 | 22 |
@@ -139,6 +139,7 @@ |
||
139 | 139 | |
140 | 140 | /** |
141 | 141 | * Get data-uri of QRCode |
142 | + * @param string $label |
|
142 | 143 | */ |
143 | 144 | public function getQRCodeImageAsDataUri($label, $secret, $size = 200) |
144 | 145 | { |
@@ -36,11 +36,11 @@ discard block |
||
36 | 36 | |
37 | 37 | $algorithm = strtolower(trim($algorithm)); |
38 | 38 | if (!in_array($algorithm, self::$_supportedalgos)) |
39 | - throw new TwoFactorAuthException('Unsupported algorithm: ' . $algorithm); |
|
39 | + throw new TwoFactorAuthException('Unsupported algorithm: '.$algorithm); |
|
40 | 40 | $this->algorithm = $algorithm; |
41 | 41 | |
42 | 42 | // Set default QR Code provider if none was specified |
43 | - if ($qrcodeprovider==null) |
|
43 | + if ($qrcodeprovider == null) |
|
44 | 44 | $qrcodeprovider = new Providers\Qr\GoogleQRCodeProvider(); |
45 | 45 | |
46 | 46 | if (!($qrcodeprovider instanceof Providers\Qr\IQRCodeProvider)) |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | $this->qrcodeprovider = $qrcodeprovider; |
50 | 50 | |
51 | 51 | // Try to find best available RNG provider if none was specified |
52 | - if ($rngprovider==null) { |
|
52 | + if ($rngprovider == null) { |
|
53 | 53 | if (function_exists('random_bytes')) { |
54 | 54 | $rngprovider = new Providers\Rng\CSRNGProvider(); |
55 | 55 | } elseif (function_exists('mcrypt_create_iv')) { |
@@ -78,12 +78,12 @@ discard block |
||
78 | 78 | public function createSecret($bits = 80, $requirecryptosecure = true) |
79 | 79 | { |
80 | 80 | $secret = ''; |
81 | - $bytes = ceil($bits / 5); //We use 5 bits of each byte (since we have a 32-character 'alphabet' / BASE32) |
|
81 | + $bytes = ceil($bits / 5); //We use 5 bits of each byte (since we have a 32-character 'alphabet' / BASE32) |
|
82 | 82 | if ($requirecryptosecure && !$this->rngprovider->isCryptographicallySecure()) |
83 | 83 | throw new TwoFactorAuthException('RNG provider is not cryptographically secure'); |
84 | 84 | $rnd = $this->rngprovider->getRandomBytes($bytes); |
85 | 85 | for ($i = 0; $i < $bytes; $i++) |
86 | - $secret .= self::$_base32[ord($rnd[$i]) & 31]; //Mask out left 3 bits for 0-31 values |
|
86 | + $secret .= self::$_base32[ord($rnd[$i]) & 31]; //Mask out left 3 bits for 0-31 values |
|
87 | 87 | return $secret; |
88 | 88 | } |
89 | 89 | |
@@ -94,11 +94,11 @@ discard block |
||
94 | 94 | { |
95 | 95 | $secretkey = $this->base32Decode($secret); |
96 | 96 | |
97 | - $timestamp = "\0\0\0\0" . pack('N*', $this->getTimeSlice($this->getTime($time))); // Pack time into binary string |
|
98 | - $hashhmac = hash_hmac($this->algorithm, $timestamp, $secretkey, true); // Hash it with users secret key |
|
99 | - $hashpart = substr($hashhmac, ord(substr($hashhmac, -1)) & 0x0F, 4); // Use last nibble of result as index/offset and grab 4 bytes of the result |
|
100 | - $value = unpack('N', $hashpart); // Unpack binary value |
|
101 | - $value = $value[1] & 0x7FFFFFFF; // Drop MSB, keep only 31 bits |
|
97 | + $timestamp = "\0\0\0\0".pack('N*', $this->getTimeSlice($this->getTime($time))); // Pack time into binary string |
|
98 | + $hashhmac = hash_hmac($this->algorithm, $timestamp, $secretkey, true); // Hash it with users secret key |
|
99 | + $hashpart = substr($hashhmac, ord(substr($hashhmac, -1)) & 0x0F, 4); // Use last nibble of result as index/offset and grab 4 bytes of the result |
|
100 | + $value = unpack('N', $hashpart); // Unpack binary value |
|
101 | + $value = $value[1] & 0x7FFFFFFF; // Drop MSB, keep only 31 bits |
|
102 | 102 | |
103 | 103 | return str_pad($value % pow(10, $this->digits), $this->digits, '0', STR_PAD_LEFT); |
104 | 104 | } |
@@ -115,7 +115,7 @@ discard block |
||
115 | 115 | for ($i = -$discrepancy; $i <= $discrepancy; $i++) |
116 | 116 | $result |= $this->codeEquals($this->getCode($secret, $timetamp + ($i * $this->period)), $code); |
117 | 117 | |
118 | - return (bool)$result; |
|
118 | + return (bool) $result; |
|
119 | 119 | } |
120 | 120 | |
121 | 121 | /** |
@@ -127,7 +127,7 @@ discard block |
||
127 | 127 | } else { |
128 | 128 | // In general, it's not possible to prevent length leaks. So it's OK to leak the length. The important part is that |
129 | 129 | // we don't leak information about the difference of the two strings. |
130 | - if (strlen($safe)===strlen($user)) { |
|
130 | + if (strlen($safe) === strlen($user)) { |
|
131 | 131 | $result = 0; |
132 | 132 | for ($i = 0; $i < strlen($safe); $i++) |
133 | 133 | $result |= (ord($safe[$i]) ^ ord($user[$i])); |
@@ -158,7 +158,7 @@ discard block |
||
158 | 158 | |
159 | 159 | private function getTimeSlice($time = null, $offset = 0) |
160 | 160 | { |
161 | - return (int)floor($time / $this->period) + ($offset * $this->period); |
|
161 | + return (int) floor($time / $this->period) + ($offset * $this->period); |
|
162 | 162 | } |
163 | 163 | |
164 | 164 | /** |
@@ -166,17 +166,17 @@ discard block |
||
166 | 166 | */ |
167 | 167 | public function getQRText($label, $secret) |
168 | 168 | { |
169 | - return 'otpauth://totp/' . rawurlencode($label) |
|
170 | - . '?secret=' . rawurlencode($secret) |
|
171 | - . '&issuer=' . rawurlencode($this->issuer) |
|
172 | - . '&period=' . intval($this->period) |
|
173 | - . '&algorithm=' . rawurlencode(strtoupper($this->algorithm)) |
|
174 | - . '&digits=' . intval($this->digits); |
|
169 | + return 'otpauth://totp/'.rawurlencode($label) |
|
170 | + . '?secret='.rawurlencode($secret) |
|
171 | + . '&issuer='.rawurlencode($this->issuer) |
|
172 | + . '&period='.intval($this->period) |
|
173 | + . '&algorithm='.rawurlencode(strtoupper($this->algorithm)) |
|
174 | + . '&digits='.intval($this->digits); |
|
175 | 175 | } |
176 | 176 | |
177 | 177 | private function base32Decode($value) |
178 | 178 | { |
179 | - if (strlen($value)==0) return ''; |
|
179 | + if (strlen($value) == 0) return ''; |
|
180 | 180 | |
181 | 181 | if (preg_match('/[^'.preg_quote(self::$_base32dict).']/', $value) !== 0) |
182 | 182 | throw new TwoFactorAuthException('Invalid base32 string'); |
@@ -26,25 +26,30 @@ discard block |
||
26 | 26 | { |
27 | 27 | $this->issuer = $issuer; |
28 | 28 | |
29 | - if (!is_int($digits) || $digits <= 0) |
|
30 | - throw new TwoFactorAuthException('Digits must be int > 0'); |
|
29 | + if (!is_int($digits) || $digits <= 0) { |
|
30 | + throw new TwoFactorAuthException('Digits must be int > 0'); |
|
31 | + } |
|
31 | 32 | $this->digits = $digits; |
32 | 33 | |
33 | - if (!is_int($period) || $period <= 0) |
|
34 | - throw new TwoFactorAuthException('Period must be int > 0'); |
|
34 | + if (!is_int($period) || $period <= 0) { |
|
35 | + throw new TwoFactorAuthException('Period must be int > 0'); |
|
36 | + } |
|
35 | 37 | $this->period = $period; |
36 | 38 | |
37 | 39 | $algorithm = strtolower(trim($algorithm)); |
38 | - if (!in_array($algorithm, self::$_supportedalgos)) |
|
39 | - throw new TwoFactorAuthException('Unsupported algorithm: ' . $algorithm); |
|
40 | + if (!in_array($algorithm, self::$_supportedalgos)) { |
|
41 | + throw new TwoFactorAuthException('Unsupported algorithm: ' . $algorithm); |
|
42 | + } |
|
40 | 43 | $this->algorithm = $algorithm; |
41 | 44 | |
42 | 45 | // Set default QR Code provider if none was specified |
43 | - if ($qrcodeprovider==null) |
|
44 | - $qrcodeprovider = new Providers\Qr\GoogleQRCodeProvider(); |
|
46 | + if ($qrcodeprovider==null) { |
|
47 | + $qrcodeprovider = new Providers\Qr\GoogleQRCodeProvider(); |
|
48 | + } |
|
45 | 49 | |
46 | - if (!($qrcodeprovider instanceof Providers\Qr\IQRCodeProvider)) |
|
47 | - throw new TwoFactorAuthException('QRCodeProvider must implement IQRCodeProvider'); |
|
50 | + if (!($qrcodeprovider instanceof Providers\Qr\IQRCodeProvider)) { |
|
51 | + throw new TwoFactorAuthException('QRCodeProvider must implement IQRCodeProvider'); |
|
52 | + } |
|
48 | 53 | |
49 | 54 | $this->qrcodeprovider = $qrcodeprovider; |
50 | 55 | |
@@ -63,8 +68,9 @@ discard block |
||
63 | 68 | } |
64 | 69 | } |
65 | 70 | |
66 | - if (!($rngprovider instanceof Providers\Rng\IRNGProvider)) |
|
67 | - throw new TwoFactorAuthException('RNGProvider must implement IRNGProvider'); |
|
71 | + if (!($rngprovider instanceof Providers\Rng\IRNGProvider)) { |
|
72 | + throw new TwoFactorAuthException('RNGProvider must implement IRNGProvider'); |
|
73 | + } |
|
68 | 74 | |
69 | 75 | $this->rngprovider = $rngprovider; |
70 | 76 | |
@@ -79,11 +85,14 @@ discard block |
||
79 | 85 | { |
80 | 86 | $secret = ''; |
81 | 87 | $bytes = ceil($bits / 5); //We use 5 bits of each byte (since we have a 32-character 'alphabet' / BASE32) |
82 | - if ($requirecryptosecure && !$this->rngprovider->isCryptographicallySecure()) |
|
83 | - throw new TwoFactorAuthException('RNG provider is not cryptographically secure'); |
|
88 | + if ($requirecryptosecure && !$this->rngprovider->isCryptographicallySecure()) { |
|
89 | + throw new TwoFactorAuthException('RNG provider is not cryptographically secure'); |
|
90 | + } |
|
84 | 91 | $rnd = $this->rngprovider->getRandomBytes($bytes); |
85 | - for ($i = 0; $i < $bytes; $i++) |
|
86 | - $secret .= self::$_base32[ord($rnd[$i]) & 31]; //Mask out left 3 bits for 0-31 values |
|
92 | + for ($i = 0; $i < $bytes; $i++) { |
|
93 | + $secret .= self::$_base32[ord($rnd[$i]) & 31]; |
|
94 | + } |
|
95 | + //Mask out left 3 bits for 0-31 values |
|
87 | 96 | return $secret; |
88 | 97 | } |
89 | 98 | |
@@ -112,8 +121,9 @@ discard block |
||
112 | 121 | $timetamp = $this->getTime($time); |
113 | 122 | |
114 | 123 | // To keep safe from timing-attachs we iterate *all* possible codes even though we already may have verified a code is correct |
115 | - for ($i = -$discrepancy; $i <= $discrepancy; $i++) |
|
116 | - $result |= $this->codeEquals($this->getCode($secret, $timetamp + ($i * $this->period)), $code); |
|
124 | + for ($i = -$discrepancy; $i <= $discrepancy; $i++) { |
|
125 | + $result |= $this->codeEquals($this->getCode($secret, $timetamp + ($i * $this->period)), $code); |
|
126 | + } |
|
117 | 127 | |
118 | 128 | return (bool)$result; |
119 | 129 | } |
@@ -129,8 +139,9 @@ discard block |
||
129 | 139 | // we don't leak information about the difference of the two strings. |
130 | 140 | if (strlen($safe)===strlen($user)) { |
131 | 141 | $result = 0; |
132 | - for ($i = 0; $i < strlen($safe); $i++) |
|
133 | - $result |= (ord($safe[$i]) ^ ord($user[$i])); |
|
142 | + for ($i = 0; $i < strlen($safe); $i++) { |
|
143 | + $result |= (ord($safe[$i]) ^ ord($user[$i])); |
|
144 | + } |
|
134 | 145 | return $result === 0; |
135 | 146 | } |
136 | 147 | } |
@@ -142,8 +153,9 @@ discard block |
||
142 | 153 | */ |
143 | 154 | public function getQRCodeImageAsDataUri($label, $secret, $size = 200) |
144 | 155 | { |
145 | - if (!is_int($size) || $size <= 0) |
|
146 | - throw new TwoFactorAuthException('Size must be int > 0'); |
|
156 | + if (!is_int($size) || $size <= 0) { |
|
157 | + throw new TwoFactorAuthException('Size must be int > 0'); |
|
158 | + } |
|
147 | 159 | |
148 | 160 | return 'data:' |
149 | 161 | . $this->qrcodeprovider->getMimeType() |
@@ -176,23 +188,28 @@ discard block |
||
176 | 188 | |
177 | 189 | private function base32Decode($value) |
178 | 190 | { |
179 | - if (strlen($value)==0) return ''; |
|
191 | + if (strlen($value)==0) { |
|
192 | + return ''; |
|
193 | + } |
|
180 | 194 | |
181 | - if (preg_match('/[^'.preg_quote(self::$_base32dict).']/', $value) !== 0) |
|
182 | - throw new TwoFactorAuthException('Invalid base32 string'); |
|
195 | + if (preg_match('/[^'.preg_quote(self::$_base32dict).']/', $value) !== 0) { |
|
196 | + throw new TwoFactorAuthException('Invalid base32 string'); |
|
197 | + } |
|
183 | 198 | |
184 | 199 | $buffer = ''; |
185 | 200 | foreach (str_split($value) as $char) |
186 | 201 | { |
187 | - if ($char !== '=') |
|
188 | - $buffer .= str_pad(decbin(self::$_base32lookup[$char]), 5, 0, STR_PAD_LEFT); |
|
202 | + if ($char !== '=') { |
|
203 | + $buffer .= str_pad(decbin(self::$_base32lookup[$char]), 5, 0, STR_PAD_LEFT); |
|
204 | + } |
|
189 | 205 | } |
190 | 206 | $length = strlen($buffer); |
191 | 207 | $blocks = trim(chunk_split(substr($buffer, 0, $length - ($length % 8)), 8, ' ')); |
192 | 208 | |
193 | 209 | $output = ''; |
194 | - foreach (explode(' ', $blocks) as $block) |
|
195 | - $output .= chr(bindec(str_pad($block, 8, 0, STR_PAD_RIGHT))); |
|
210 | + foreach (explode(' ', $blocks) as $block) { |
|
211 | + $output .= chr(bindec(str_pad($block, 8, 0, STR_PAD_RIGHT))); |
|
212 | + } |
|
196 | 213 | |
197 | 214 | return $output; |
198 | 215 | } |
@@ -98,8 +98,8 @@ discard block |
||
98 | 98 | * Sets up the object |
99 | 99 | * @param string $id The client identity |
100 | 100 | * @param string $key The client MAC key (optional) |
101 | - * @param boolean $https Flag whether to use https (optional) |
|
102 | - * @param boolean $httpsverify Flag whether to use verify HTTPS |
|
101 | + * @param integer $https Flag whether to use https (optional) |
|
102 | + * @param integer $httpsverify Flag whether to use verify HTTPS |
|
103 | 103 | * server certificates (optional, |
104 | 104 | * default true) |
105 | 105 | * @access public |
@@ -207,6 +207,7 @@ discard block |
||
207 | 207 | * |
208 | 208 | * @param string Input string to parse |
209 | 209 | * @param string Optional delimiter re-class, default is '[:]' |
210 | + * @param string $str |
|
210 | 211 | * @return array Keyed array with fields |
211 | 212 | * @access public |
212 | 213 | */ |
@@ -1,15 +1,15 @@ discard block |
||
1 | 1 | <?php |
2 | - /** |
|
3 | - * Class for verifying Yubico One-Time-Passcodes |
|
4 | - * |
|
5 | - * @category Auth |
|
6 | - * @package Auth_Yubico |
|
7 | - * @author Simon Josefsson <[email protected]>, Olov Danielson <[email protected]> |
|
8 | - * @copyright 2007-2015 Yubico AB |
|
9 | - * @license http://opensource.org/licenses/bsd-license.php New BSD License |
|
10 | - * @version 2.0 |
|
11 | - * @link http://www.yubico.com/ |
|
12 | - */ |
|
2 | + /** |
|
3 | + * Class for verifying Yubico One-Time-Passcodes |
|
4 | + * |
|
5 | + * @category Auth |
|
6 | + * @package Auth_Yubico |
|
7 | + * @author Simon Josefsson <[email protected]>, Olov Danielson <[email protected]> |
|
8 | + * @copyright 2007-2015 Yubico AB |
|
9 | + * @license http://opensource.org/licenses/bsd-license.php New BSD License |
|
10 | + * @version 2.0 |
|
11 | + * @link http://www.yubico.com/ |
|
12 | + */ |
|
13 | 13 | |
14 | 14 | require_once 'PEAR.php'; |
15 | 15 | |
@@ -34,340 +34,340 @@ discard block |
||
34 | 34 | */ |
35 | 35 | class Auth_Yubico |
36 | 36 | { |
37 | - /**#@+ |
|
37 | + /**#@+ |
|
38 | 38 | * @access private |
39 | 39 | */ |
40 | 40 | |
41 | - /** |
|
42 | - * Yubico client ID |
|
43 | - * @var string |
|
44 | - */ |
|
45 | - var $_id; |
|
46 | - |
|
47 | - /** |
|
48 | - * Yubico client key |
|
49 | - * @var string |
|
50 | - */ |
|
51 | - var $_key; |
|
52 | - |
|
53 | - /** |
|
54 | - * URL part of validation server |
|
55 | - * @var string |
|
56 | - */ |
|
57 | - var $_url; |
|
58 | - |
|
59 | - /** |
|
60 | - * List with URL part of validation servers |
|
61 | - * @var array |
|
62 | - */ |
|
63 | - var $_url_list; |
|
64 | - |
|
65 | - /** |
|
66 | - * index to _url_list |
|
67 | - * @var int |
|
68 | - */ |
|
69 | - var $_url_index; |
|
70 | - |
|
71 | - /** |
|
72 | - * Last query to server |
|
73 | - * @var string |
|
74 | - */ |
|
75 | - var $_lastquery; |
|
76 | - |
|
77 | - /** |
|
78 | - * Response from server |
|
79 | - * @var string |
|
80 | - */ |
|
81 | - var $_response; |
|
82 | - |
|
83 | - /** |
|
84 | - * Flag whether to use https or not. |
|
85 | - * @var boolean |
|
86 | - */ |
|
87 | - var $_https; |
|
88 | - |
|
89 | - /** |
|
90 | - * Flag whether to verify HTTPS server certificates or not. |
|
91 | - * @var boolean |
|
92 | - */ |
|
93 | - var $_httpsverify; |
|
94 | - |
|
95 | - /** |
|
96 | - * Constructor |
|
97 | - * |
|
98 | - * Sets up the object |
|
99 | - * @param string $id The client identity |
|
100 | - * @param string $key The client MAC key (optional) |
|
101 | - * @param boolean $https Flag whether to use https (optional) |
|
102 | - * @param boolean $httpsverify Flag whether to use verify HTTPS |
|
103 | - * server certificates (optional, |
|
104 | - * default true) |
|
105 | - * @access public |
|
106 | - */ |
|
107 | - public function __construct($id, $key = '', $https = 0, $httpsverify = 1) |
|
108 | - { |
|
109 | - $this->_id = $id; |
|
110 | - $this->_key = base64_decode($key); |
|
111 | - $this->_https = $https; |
|
112 | - $this->_httpsverify = $httpsverify; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Specify to use a different URL part for verification. |
|
117 | - * The default is "api.yubico.com/wsapi/verify". |
|
118 | - * |
|
119 | - * @param string $url New server URL part to use |
|
120 | - * @access public |
|
121 | - */ |
|
122 | - function setURLpart($url) |
|
123 | - { |
|
124 | - $this->_url = $url; |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Get URL part to use for validation. |
|
129 | - * |
|
130 | - * @return string Server URL part |
|
131 | - * @access public |
|
132 | - */ |
|
133 | - function getURLpart() |
|
134 | - { |
|
135 | - if ($this->_url) { |
|
136 | - return $this->_url; |
|
137 | - } else { |
|
138 | - return "api.yubico.com/wsapi/verify"; |
|
139 | - } |
|
140 | - } |
|
141 | - |
|
142 | - |
|
143 | - /** |
|
144 | - * Get next URL part from list to use for validation. |
|
145 | - * |
|
146 | - * @return mixed string with URL part of false if no more URLs in list |
|
147 | - * @access public |
|
148 | - */ |
|
149 | - function getNextURLpart() |
|
150 | - { |
|
151 | - if ($this->_url_list) $url_list=$this->_url_list; |
|
152 | - else $url_list=array('api.yubico.com/wsapi/2.0/verify', |
|
153 | - 'api2.yubico.com/wsapi/2.0/verify', |
|
154 | - 'api3.yubico.com/wsapi/2.0/verify', |
|
155 | - 'api4.yubico.com/wsapi/2.0/verify', |
|
156 | - 'api5.yubico.com/wsapi/2.0/verify'); |
|
41 | + /** |
|
42 | + * Yubico client ID |
|
43 | + * @var string |
|
44 | + */ |
|
45 | + var $_id; |
|
46 | + |
|
47 | + /** |
|
48 | + * Yubico client key |
|
49 | + * @var string |
|
50 | + */ |
|
51 | + var $_key; |
|
52 | + |
|
53 | + /** |
|
54 | + * URL part of validation server |
|
55 | + * @var string |
|
56 | + */ |
|
57 | + var $_url; |
|
58 | + |
|
59 | + /** |
|
60 | + * List with URL part of validation servers |
|
61 | + * @var array |
|
62 | + */ |
|
63 | + var $_url_list; |
|
64 | + |
|
65 | + /** |
|
66 | + * index to _url_list |
|
67 | + * @var int |
|
68 | + */ |
|
69 | + var $_url_index; |
|
70 | + |
|
71 | + /** |
|
72 | + * Last query to server |
|
73 | + * @var string |
|
74 | + */ |
|
75 | + var $_lastquery; |
|
76 | + |
|
77 | + /** |
|
78 | + * Response from server |
|
79 | + * @var string |
|
80 | + */ |
|
81 | + var $_response; |
|
82 | + |
|
83 | + /** |
|
84 | + * Flag whether to use https or not. |
|
85 | + * @var boolean |
|
86 | + */ |
|
87 | + var $_https; |
|
88 | + |
|
89 | + /** |
|
90 | + * Flag whether to verify HTTPS server certificates or not. |
|
91 | + * @var boolean |
|
92 | + */ |
|
93 | + var $_httpsverify; |
|
94 | + |
|
95 | + /** |
|
96 | + * Constructor |
|
97 | + * |
|
98 | + * Sets up the object |
|
99 | + * @param string $id The client identity |
|
100 | + * @param string $key The client MAC key (optional) |
|
101 | + * @param boolean $https Flag whether to use https (optional) |
|
102 | + * @param boolean $httpsverify Flag whether to use verify HTTPS |
|
103 | + * server certificates (optional, |
|
104 | + * default true) |
|
105 | + * @access public |
|
106 | + */ |
|
107 | + public function __construct($id, $key = '', $https = 0, $httpsverify = 1) |
|
108 | + { |
|
109 | + $this->_id = $id; |
|
110 | + $this->_key = base64_decode($key); |
|
111 | + $this->_https = $https; |
|
112 | + $this->_httpsverify = $httpsverify; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Specify to use a different URL part for verification. |
|
117 | + * The default is "api.yubico.com/wsapi/verify". |
|
118 | + * |
|
119 | + * @param string $url New server URL part to use |
|
120 | + * @access public |
|
121 | + */ |
|
122 | + function setURLpart($url) |
|
123 | + { |
|
124 | + $this->_url = $url; |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Get URL part to use for validation. |
|
129 | + * |
|
130 | + * @return string Server URL part |
|
131 | + * @access public |
|
132 | + */ |
|
133 | + function getURLpart() |
|
134 | + { |
|
135 | + if ($this->_url) { |
|
136 | + return $this->_url; |
|
137 | + } else { |
|
138 | + return "api.yubico.com/wsapi/verify"; |
|
139 | + } |
|
140 | + } |
|
141 | + |
|
142 | + |
|
143 | + /** |
|
144 | + * Get next URL part from list to use for validation. |
|
145 | + * |
|
146 | + * @return mixed string with URL part of false if no more URLs in list |
|
147 | + * @access public |
|
148 | + */ |
|
149 | + function getNextURLpart() |
|
150 | + { |
|
151 | + if ($this->_url_list) $url_list=$this->_url_list; |
|
152 | + else $url_list=array('api.yubico.com/wsapi/2.0/verify', |
|
153 | + 'api2.yubico.com/wsapi/2.0/verify', |
|
154 | + 'api3.yubico.com/wsapi/2.0/verify', |
|
155 | + 'api4.yubico.com/wsapi/2.0/verify', |
|
156 | + 'api5.yubico.com/wsapi/2.0/verify'); |
|
157 | 157 | |
158 | - if ($this->_url_index>=count($url_list)) return false; |
|
159 | - else return $url_list[$this->_url_index++]; |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * Resets index to URL list |
|
164 | - * |
|
165 | - * @access public |
|
166 | - */ |
|
167 | - function URLreset() |
|
168 | - { |
|
169 | - $this->_url_index=0; |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Add another URLpart. |
|
174 | - * |
|
175 | - * @access public |
|
176 | - */ |
|
177 | - function addURLpart($URLpart) |
|
178 | - { |
|
179 | - $this->_url_list[]=$URLpart; |
|
180 | - } |
|
158 | + if ($this->_url_index>=count($url_list)) return false; |
|
159 | + else return $url_list[$this->_url_index++]; |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * Resets index to URL list |
|
164 | + * |
|
165 | + * @access public |
|
166 | + */ |
|
167 | + function URLreset() |
|
168 | + { |
|
169 | + $this->_url_index=0; |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Add another URLpart. |
|
174 | + * |
|
175 | + * @access public |
|
176 | + */ |
|
177 | + function addURLpart($URLpart) |
|
178 | + { |
|
179 | + $this->_url_list[]=$URLpart; |
|
180 | + } |
|
181 | 181 | |
182 | - /** |
|
183 | - * Return the last query sent to the server, if any. |
|
184 | - * |
|
185 | - * @return string Request to server |
|
186 | - * @access public |
|
187 | - */ |
|
188 | - function getLastQuery() |
|
189 | - { |
|
190 | - return $this->_lastquery; |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * Return the last data received from the server, if any. |
|
195 | - * |
|
196 | - * @return string Output from server |
|
197 | - * @access public |
|
198 | - */ |
|
199 | - function getLastResponse() |
|
200 | - { |
|
201 | - return $this->_response; |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * Parse input string into password, yubikey prefix, |
|
206 | - * ciphertext, and OTP. |
|
207 | - * |
|
208 | - * @param string Input string to parse |
|
209 | - * @param string Optional delimiter re-class, default is '[:]' |
|
210 | - * @return array Keyed array with fields |
|
211 | - * @access public |
|
212 | - */ |
|
213 | - function parsePasswordOTP($str, $delim = '[:]') |
|
214 | - { |
|
215 | - if (!preg_match("/^((.*)" . $delim . ")?" . |
|
216 | - "(([cbdefghijklnrtuv]{0,16})" . |
|
217 | - "([cbdefghijklnrtuv]{32}))$/i", |
|
218 | - $str, $matches)) { |
|
219 | - /* Dvorak? */ |
|
220 | - if (!preg_match("/^((.*)" . $delim . ")?" . |
|
221 | - "(([jxe\.uidchtnbpygk]{0,16})" . |
|
222 | - "([jxe\.uidchtnbpygk]{32}))$/i", |
|
223 | - $str, $matches)) { |
|
224 | - return false; |
|
225 | - } else { |
|
226 | - $ret['otp'] = strtr($matches[3], "jxe.uidchtnbpygk", "cbdefghijklnrtuv"); |
|
227 | - } |
|
228 | - } else { |
|
229 | - $ret['otp'] = $matches[3]; |
|
230 | - } |
|
231 | - $ret['password'] = $matches[2]; |
|
232 | - $ret['prefix'] = $matches[4]; |
|
233 | - $ret['ciphertext'] = $matches[5]; |
|
234 | - return $ret; |
|
235 | - } |
|
236 | - |
|
237 | - /* TODO? Add functions to get parsed parts of server response? */ |
|
238 | - |
|
239 | - /** |
|
240 | - * Parse parameters from last response |
|
241 | - * |
|
242 | - * example: getParameters("timestamp", "sessioncounter", "sessionuse"); |
|
243 | - * |
|
244 | - * @param array @parameters Array with strings representing |
|
245 | - * parameters to parse |
|
246 | - * @return array parameter array from last response |
|
247 | - * @access public |
|
248 | - */ |
|
249 | - function getParameters($parameters) |
|
250 | - { |
|
251 | - if ($parameters == null) { |
|
252 | - $parameters = array('timestamp', 'sessioncounter', 'sessionuse'); |
|
253 | - } |
|
254 | - $param_array = array(); |
|
255 | - foreach ($parameters as $param) { |
|
256 | - if(!preg_match("/" . $param . "=([0-9]+)/", $this->_response, $out)) { |
|
257 | - return PEAR::raiseError('Could not parse parameter ' . $param . ' from response'); |
|
258 | - } |
|
259 | - $param_array[$param]=$out[1]; |
|
260 | - } |
|
261 | - return $param_array; |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * Verify Yubico OTP against multiple URLs |
|
266 | - * Protocol specification 2.0 is used to construct validation requests |
|
267 | - * |
|
268 | - * @param string $token Yubico OTP |
|
269 | - * @param int $use_timestamp 1=>send request with ×tamp=1 to |
|
270 | - * get timestamp and session information |
|
271 | - * in the response |
|
272 | - * @param boolean $wait_for_all If true, wait until all |
|
273 | - * servers responds (for debugging) |
|
274 | - * @param string $sl Sync level in percentage between 0 |
|
275 | - * and 100 or "fast" or "secure". |
|
276 | - * @param int $timeout Max number of seconds to wait |
|
277 | - * for responses |
|
278 | - * @return mixed PEAR error on error, true otherwise |
|
279 | - * @access public |
|
280 | - */ |
|
281 | - function verify($token, $use_timestamp=null, $wait_for_all=False, |
|
282 | - $sl=null, $timeout=null) |
|
283 | - { |
|
284 | - /* Construct parameters string */ |
|
285 | - $ret = $this->parsePasswordOTP($token); |
|
286 | - if (!$ret) { |
|
287 | - return PEAR::raiseError('Could not parse Yubikey OTP'); |
|
288 | - } |
|
289 | - $params = array('id'=>$this->_id, |
|
290 | - 'otp'=>$ret['otp'], |
|
291 | - 'nonce'=>md5(uniqid(rand()))); |
|
292 | - /* Take care of protocol version 2 parameters */ |
|
293 | - if ($use_timestamp) $params['timestamp'] = 1; |
|
294 | - if ($sl) $params['sl'] = $sl; |
|
295 | - if ($timeout) $params['timeout'] = $timeout; |
|
296 | - ksort($params); |
|
297 | - $parameters = ''; |
|
298 | - foreach($params as $p=>$v) $parameters .= "&" . $p . "=" . $v; |
|
299 | - $parameters = ltrim($parameters, "&"); |
|
182 | + /** |
|
183 | + * Return the last query sent to the server, if any. |
|
184 | + * |
|
185 | + * @return string Request to server |
|
186 | + * @access public |
|
187 | + */ |
|
188 | + function getLastQuery() |
|
189 | + { |
|
190 | + return $this->_lastquery; |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * Return the last data received from the server, if any. |
|
195 | + * |
|
196 | + * @return string Output from server |
|
197 | + * @access public |
|
198 | + */ |
|
199 | + function getLastResponse() |
|
200 | + { |
|
201 | + return $this->_response; |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * Parse input string into password, yubikey prefix, |
|
206 | + * ciphertext, and OTP. |
|
207 | + * |
|
208 | + * @param string Input string to parse |
|
209 | + * @param string Optional delimiter re-class, default is '[:]' |
|
210 | + * @return array Keyed array with fields |
|
211 | + * @access public |
|
212 | + */ |
|
213 | + function parsePasswordOTP($str, $delim = '[:]') |
|
214 | + { |
|
215 | + if (!preg_match("/^((.*)" . $delim . ")?" . |
|
216 | + "(([cbdefghijklnrtuv]{0,16})" . |
|
217 | + "([cbdefghijklnrtuv]{32}))$/i", |
|
218 | + $str, $matches)) { |
|
219 | + /* Dvorak? */ |
|
220 | + if (!preg_match("/^((.*)" . $delim . ")?" . |
|
221 | + "(([jxe\.uidchtnbpygk]{0,16})" . |
|
222 | + "([jxe\.uidchtnbpygk]{32}))$/i", |
|
223 | + $str, $matches)) { |
|
224 | + return false; |
|
225 | + } else { |
|
226 | + $ret['otp'] = strtr($matches[3], "jxe.uidchtnbpygk", "cbdefghijklnrtuv"); |
|
227 | + } |
|
228 | + } else { |
|
229 | + $ret['otp'] = $matches[3]; |
|
230 | + } |
|
231 | + $ret['password'] = $matches[2]; |
|
232 | + $ret['prefix'] = $matches[4]; |
|
233 | + $ret['ciphertext'] = $matches[5]; |
|
234 | + return $ret; |
|
235 | + } |
|
236 | + |
|
237 | + /* TODO? Add functions to get parsed parts of server response? */ |
|
238 | + |
|
239 | + /** |
|
240 | + * Parse parameters from last response |
|
241 | + * |
|
242 | + * example: getParameters("timestamp", "sessioncounter", "sessionuse"); |
|
243 | + * |
|
244 | + * @param array @parameters Array with strings representing |
|
245 | + * parameters to parse |
|
246 | + * @return array parameter array from last response |
|
247 | + * @access public |
|
248 | + */ |
|
249 | + function getParameters($parameters) |
|
250 | + { |
|
251 | + if ($parameters == null) { |
|
252 | + $parameters = array('timestamp', 'sessioncounter', 'sessionuse'); |
|
253 | + } |
|
254 | + $param_array = array(); |
|
255 | + foreach ($parameters as $param) { |
|
256 | + if(!preg_match("/" . $param . "=([0-9]+)/", $this->_response, $out)) { |
|
257 | + return PEAR::raiseError('Could not parse parameter ' . $param . ' from response'); |
|
258 | + } |
|
259 | + $param_array[$param]=$out[1]; |
|
260 | + } |
|
261 | + return $param_array; |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * Verify Yubico OTP against multiple URLs |
|
266 | + * Protocol specification 2.0 is used to construct validation requests |
|
267 | + * |
|
268 | + * @param string $token Yubico OTP |
|
269 | + * @param int $use_timestamp 1=>send request with ×tamp=1 to |
|
270 | + * get timestamp and session information |
|
271 | + * in the response |
|
272 | + * @param boolean $wait_for_all If true, wait until all |
|
273 | + * servers responds (for debugging) |
|
274 | + * @param string $sl Sync level in percentage between 0 |
|
275 | + * and 100 or "fast" or "secure". |
|
276 | + * @param int $timeout Max number of seconds to wait |
|
277 | + * for responses |
|
278 | + * @return mixed PEAR error on error, true otherwise |
|
279 | + * @access public |
|
280 | + */ |
|
281 | + function verify($token, $use_timestamp=null, $wait_for_all=False, |
|
282 | + $sl=null, $timeout=null) |
|
283 | + { |
|
284 | + /* Construct parameters string */ |
|
285 | + $ret = $this->parsePasswordOTP($token); |
|
286 | + if (!$ret) { |
|
287 | + return PEAR::raiseError('Could not parse Yubikey OTP'); |
|
288 | + } |
|
289 | + $params = array('id'=>$this->_id, |
|
290 | + 'otp'=>$ret['otp'], |
|
291 | + 'nonce'=>md5(uniqid(rand()))); |
|
292 | + /* Take care of protocol version 2 parameters */ |
|
293 | + if ($use_timestamp) $params['timestamp'] = 1; |
|
294 | + if ($sl) $params['sl'] = $sl; |
|
295 | + if ($timeout) $params['timeout'] = $timeout; |
|
296 | + ksort($params); |
|
297 | + $parameters = ''; |
|
298 | + foreach($params as $p=>$v) $parameters .= "&" . $p . "=" . $v; |
|
299 | + $parameters = ltrim($parameters, "&"); |
|
300 | 300 | |
301 | - /* Generate signature. */ |
|
302 | - if($this->_key <> "") { |
|
303 | - $signature = base64_encode(hash_hmac('sha1', $parameters, |
|
304 | - $this->_key, true)); |
|
305 | - $signature = preg_replace('/\+/', '%2B', $signature); |
|
306 | - $parameters .= '&h=' . $signature; |
|
307 | - } |
|
308 | - |
|
309 | - /* Generate and prepare request. */ |
|
310 | - $this->_lastquery=null; |
|
311 | - $this->URLreset(); |
|
312 | - $mh = curl_multi_init(); |
|
313 | - $ch = array(); |
|
314 | - while($URLpart=$this->getNextURLpart()) |
|
315 | - { |
|
316 | - /* Support https. */ |
|
317 | - if ($this->_https) { |
|
318 | - $query = "https://"; |
|
319 | - } else { |
|
320 | - $query = "http://"; |
|
321 | - } |
|
322 | - $query .= $URLpart . "?" . $parameters; |
|
323 | - |
|
324 | - if ($this->_lastquery) { $this->_lastquery .= " "; } |
|
325 | - $this->_lastquery .= $query; |
|
301 | + /* Generate signature. */ |
|
302 | + if($this->_key <> "") { |
|
303 | + $signature = base64_encode(hash_hmac('sha1', $parameters, |
|
304 | + $this->_key, true)); |
|
305 | + $signature = preg_replace('/\+/', '%2B', $signature); |
|
306 | + $parameters .= '&h=' . $signature; |
|
307 | + } |
|
308 | + |
|
309 | + /* Generate and prepare request. */ |
|
310 | + $this->_lastquery=null; |
|
311 | + $this->URLreset(); |
|
312 | + $mh = curl_multi_init(); |
|
313 | + $ch = array(); |
|
314 | + while($URLpart=$this->getNextURLpart()) |
|
315 | + { |
|
316 | + /* Support https. */ |
|
317 | + if ($this->_https) { |
|
318 | + $query = "https://"; |
|
319 | + } else { |
|
320 | + $query = "http://"; |
|
321 | + } |
|
322 | + $query .= $URLpart . "?" . $parameters; |
|
323 | + |
|
324 | + if ($this->_lastquery) { $this->_lastquery .= " "; } |
|
325 | + $this->_lastquery .= $query; |
|
326 | 326 | |
327 | - $handle = curl_init($query); |
|
328 | - curl_setopt($handle, CURLOPT_USERAGENT, "PEAR Auth_Yubico"); |
|
329 | - curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1); |
|
330 | - if (!$this->_httpsverify) { |
|
331 | - curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, 0); |
|
332 | - curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, 0); |
|
333 | - } |
|
334 | - curl_setopt($handle, CURLOPT_FAILONERROR, true); |
|
335 | - /* If timeout is set, we better apply it here as well |
|
327 | + $handle = curl_init($query); |
|
328 | + curl_setopt($handle, CURLOPT_USERAGENT, "PEAR Auth_Yubico"); |
|
329 | + curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1); |
|
330 | + if (!$this->_httpsverify) { |
|
331 | + curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, 0); |
|
332 | + curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, 0); |
|
333 | + } |
|
334 | + curl_setopt($handle, CURLOPT_FAILONERROR, true); |
|
335 | + /* If timeout is set, we better apply it here as well |
|
336 | 336 | in case the validation server fails to follow it. |
337 | 337 | */ |
338 | - if ($timeout) curl_setopt($handle, CURLOPT_TIMEOUT, $timeout); |
|
339 | - curl_multi_add_handle($mh, $handle); |
|
338 | + if ($timeout) curl_setopt($handle, CURLOPT_TIMEOUT, $timeout); |
|
339 | + curl_multi_add_handle($mh, $handle); |
|
340 | 340 | |
341 | - $ch[(int)$handle] = $handle; |
|
342 | - } |
|
343 | - |
|
344 | - /* Execute and read request. */ |
|
345 | - $this->_response=null; |
|
346 | - $replay=False; |
|
347 | - $valid=False; |
|
348 | - do { |
|
349 | - /* Let curl do its work. */ |
|
350 | - while (($mrc = curl_multi_exec($mh, $active)) |
|
351 | - == CURLM_CALL_MULTI_PERFORM) |
|
352 | - ; |
|
353 | - |
|
354 | - while ($info = curl_multi_info_read($mh)) { |
|
355 | - if ($info['result'] == CURLE_OK) { |
|
356 | - |
|
357 | - /* We have a complete response from one server. */ |
|
358 | - |
|
359 | - $str = curl_multi_getcontent($info['handle']); |
|
360 | - $cinfo = curl_getinfo ($info['handle']); |
|
341 | + $ch[(int)$handle] = $handle; |
|
342 | + } |
|
343 | + |
|
344 | + /* Execute and read request. */ |
|
345 | + $this->_response=null; |
|
346 | + $replay=False; |
|
347 | + $valid=False; |
|
348 | + do { |
|
349 | + /* Let curl do its work. */ |
|
350 | + while (($mrc = curl_multi_exec($mh, $active)) |
|
351 | + == CURLM_CALL_MULTI_PERFORM) |
|
352 | + ; |
|
353 | + |
|
354 | + while ($info = curl_multi_info_read($mh)) { |
|
355 | + if ($info['result'] == CURLE_OK) { |
|
356 | + |
|
357 | + /* We have a complete response from one server. */ |
|
358 | + |
|
359 | + $str = curl_multi_getcontent($info['handle']); |
|
360 | + $cinfo = curl_getinfo ($info['handle']); |
|
361 | 361 | |
362 | - if ($wait_for_all) { # Better debug info |
|
363 | - $this->_response .= 'URL=' . $cinfo['url'] ."\n" |
|
364 | - . $str . "\n"; |
|
365 | - } |
|
362 | + if ($wait_for_all) { # Better debug info |
|
363 | + $this->_response .= 'URL=' . $cinfo['url'] ."\n" |
|
364 | + . $str . "\n"; |
|
365 | + } |
|
366 | 366 | |
367 | - if (preg_match("/status=([a-zA-Z0-9_]+)/", $str, $out)) { |
|
368 | - $status = $out[1]; |
|
367 | + if (preg_match("/status=([a-zA-Z0-9_]+)/", $str, $out)) { |
|
368 | + $status = $out[1]; |
|
369 | 369 | |
370 | - /* |
|
370 | + /* |
|
371 | 371 | * There are 3 cases. |
372 | 372 | * |
373 | 373 | * 1. OTP or Nonce values doesn't match - ignore |
@@ -379,92 +379,92 @@ discard block |
||
379 | 379 | * |
380 | 380 | * 3. Return if status=OK or status=REPLAYED_OTP. |
381 | 381 | */ |
382 | - if (!preg_match("/otp=".$params['otp']."/", $str) || |
|
383 | - !preg_match("/nonce=".$params['nonce']."/", $str)) { |
|
384 | - /* Case 1. Ignore response. */ |
|
385 | - } |
|
386 | - elseif ($this->_key <> "") { |
|
387 | - /* Case 2. Verify signature first */ |
|
388 | - $rows = explode("\r\n", trim($str)); |
|
389 | - $response=array(); |
|
390 | - while (list($key, $val) = each($rows)) { |
|
391 | - /* = is also used in BASE64 encoding so we only replace the first = by # which is not used in BASE64 */ |
|
392 | - $val = preg_replace('/=/', '#', $val, 1); |
|
393 | - $row = explode("#", $val); |
|
394 | - $response[$row[0]] = $row[1]; |
|
395 | - } |
|
382 | + if (!preg_match("/otp=".$params['otp']."/", $str) || |
|
383 | + !preg_match("/nonce=".$params['nonce']."/", $str)) { |
|
384 | + /* Case 1. Ignore response. */ |
|
385 | + } |
|
386 | + elseif ($this->_key <> "") { |
|
387 | + /* Case 2. Verify signature first */ |
|
388 | + $rows = explode("\r\n", trim($str)); |
|
389 | + $response=array(); |
|
390 | + while (list($key, $val) = each($rows)) { |
|
391 | + /* = is also used in BASE64 encoding so we only replace the first = by # which is not used in BASE64 */ |
|
392 | + $val = preg_replace('/=/', '#', $val, 1); |
|
393 | + $row = explode("#", $val); |
|
394 | + $response[$row[0]] = $row[1]; |
|
395 | + } |
|
396 | 396 | |
397 | - $parameters=array('nonce','otp', 'sessioncounter', 'sessionuse', 'sl', 'status', 't', 'timeout', 'timestamp'); |
|
398 | - sort($parameters); |
|
399 | - $check=Null; |
|
400 | - foreach ($parameters as $param) { |
|
401 | - if (array_key_exists($param, $response)) { |
|
402 | - if ($check) $check = $check . '&'; |
|
403 | - $check = $check . $param . '=' . $response[$param]; |
|
404 | - } |
|
405 | - } |
|
406 | - |
|
407 | - $checksignature = |
|
408 | - base64_encode(hash_hmac('sha1', utf8_encode($check), |
|
409 | - $this->_key, true)); |
|
410 | - |
|
411 | - if($response['h'] == $checksignature) { |
|
412 | - if ($status == 'REPLAYED_OTP') { |
|
413 | - if (!$wait_for_all) { $this->_response = $str; } |
|
414 | - $replay=True; |
|
415 | - } |
|
416 | - if ($status == 'OK') { |
|
417 | - if (!$wait_for_all) { $this->_response = $str; } |
|
418 | - $valid=True; |
|
419 | - } |
|
420 | - } |
|
421 | - } else { |
|
422 | - /* Case 3. We check the status directly */ |
|
423 | - if ($status == 'REPLAYED_OTP') { |
|
424 | - if (!$wait_for_all) { $this->_response = $str; } |
|
425 | - $replay=True; |
|
426 | - } |
|
427 | - if ($status == 'OK') { |
|
428 | - if (!$wait_for_all) { $this->_response = $str; } |
|
429 | - $valid=True; |
|
430 | - } |
|
431 | - } |
|
432 | - } |
|
433 | - if (!$wait_for_all && ($valid || $replay)) |
|
434 | - { |
|
435 | - /* We have status=OK or status=REPLAYED_OTP, return. */ |
|
436 | - foreach ($ch as $h) { |
|
437 | - curl_multi_remove_handle($mh, $h); |
|
438 | - curl_close($h); |
|
439 | - } |
|
440 | - curl_multi_close($mh); |
|
441 | - if ($replay) return PEAR::raiseError('REPLAYED_OTP'); |
|
442 | - if ($valid) return true; |
|
443 | - return PEAR::raiseError($status); |
|
444 | - } |
|
397 | + $parameters=array('nonce','otp', 'sessioncounter', 'sessionuse', 'sl', 'status', 't', 'timeout', 'timestamp'); |
|
398 | + sort($parameters); |
|
399 | + $check=Null; |
|
400 | + foreach ($parameters as $param) { |
|
401 | + if (array_key_exists($param, $response)) { |
|
402 | + if ($check) $check = $check . '&'; |
|
403 | + $check = $check . $param . '=' . $response[$param]; |
|
404 | + } |
|
405 | + } |
|
406 | + |
|
407 | + $checksignature = |
|
408 | + base64_encode(hash_hmac('sha1', utf8_encode($check), |
|
409 | + $this->_key, true)); |
|
410 | + |
|
411 | + if($response['h'] == $checksignature) { |
|
412 | + if ($status == 'REPLAYED_OTP') { |
|
413 | + if (!$wait_for_all) { $this->_response = $str; } |
|
414 | + $replay=True; |
|
415 | + } |
|
416 | + if ($status == 'OK') { |
|
417 | + if (!$wait_for_all) { $this->_response = $str; } |
|
418 | + $valid=True; |
|
419 | + } |
|
420 | + } |
|
421 | + } else { |
|
422 | + /* Case 3. We check the status directly */ |
|
423 | + if ($status == 'REPLAYED_OTP') { |
|
424 | + if (!$wait_for_all) { $this->_response = $str; } |
|
425 | + $replay=True; |
|
426 | + } |
|
427 | + if ($status == 'OK') { |
|
428 | + if (!$wait_for_all) { $this->_response = $str; } |
|
429 | + $valid=True; |
|
430 | + } |
|
431 | + } |
|
432 | + } |
|
433 | + if (!$wait_for_all && ($valid || $replay)) |
|
434 | + { |
|
435 | + /* We have status=OK or status=REPLAYED_OTP, return. */ |
|
436 | + foreach ($ch as $h) { |
|
437 | + curl_multi_remove_handle($mh, $h); |
|
438 | + curl_close($h); |
|
439 | + } |
|
440 | + curl_multi_close($mh); |
|
441 | + if ($replay) return PEAR::raiseError('REPLAYED_OTP'); |
|
442 | + if ($valid) return true; |
|
443 | + return PEAR::raiseError($status); |
|
444 | + } |
|
445 | 445 | |
446 | - curl_multi_remove_handle($mh, $info['handle']); |
|
447 | - curl_close($info['handle']); |
|
448 | - unset ($ch[(int)$info['handle']]); |
|
449 | - } |
|
450 | - curl_multi_select($mh); |
|
451 | - } |
|
452 | - } while ($active); |
|
453 | - |
|
454 | - /* Typically this is only reached for wait_for_all=true or |
|
446 | + curl_multi_remove_handle($mh, $info['handle']); |
|
447 | + curl_close($info['handle']); |
|
448 | + unset ($ch[(int)$info['handle']]); |
|
449 | + } |
|
450 | + curl_multi_select($mh); |
|
451 | + } |
|
452 | + } while ($active); |
|
453 | + |
|
454 | + /* Typically this is only reached for wait_for_all=true or |
|
455 | 455 | * when the timeout is reached and there is no |
456 | 456 | * OK/REPLAYED_REQUEST answer (think firewall). |
457 | 457 | */ |
458 | 458 | |
459 | - foreach ($ch as $h) { |
|
460 | - curl_multi_remove_handle ($mh, $h); |
|
461 | - curl_close ($h); |
|
462 | - } |
|
463 | - curl_multi_close ($mh); |
|
459 | + foreach ($ch as $h) { |
|
460 | + curl_multi_remove_handle ($mh, $h); |
|
461 | + curl_close ($h); |
|
462 | + } |
|
463 | + curl_multi_close ($mh); |
|
464 | 464 | |
465 | - if ($replay) return PEAR::raiseError('REPLAYED_OTP'); |
|
466 | - if ($valid) return true; |
|
467 | - return PEAR::raiseError('NO_VALID_ANSWER'); |
|
468 | - } |
|
465 | + if ($replay) return PEAR::raiseError('REPLAYED_OTP'); |
|
466 | + if ($valid) return true; |
|
467 | + return PEAR::raiseError('NO_VALID_ANSWER'); |
|
468 | + } |
|
469 | 469 | } |
470 | 470 | ?> |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | */ |
107 | 107 | public function __construct($id, $key = '', $https = 0, $httpsverify = 1) |
108 | 108 | { |
109 | - $this->_id = $id; |
|
109 | + $this->_id = $id; |
|
110 | 110 | $this->_key = base64_decode($key); |
111 | 111 | $this->_https = $https; |
112 | 112 | $this->_httpsverify = $httpsverify; |
@@ -148,14 +148,14 @@ discard block |
||
148 | 148 | */ |
149 | 149 | function getNextURLpart() |
150 | 150 | { |
151 | - if ($this->_url_list) $url_list=$this->_url_list; |
|
152 | - else $url_list=array('api.yubico.com/wsapi/2.0/verify', |
|
151 | + if ($this->_url_list) $url_list = $this->_url_list; |
|
152 | + else $url_list = array('api.yubico.com/wsapi/2.0/verify', |
|
153 | 153 | 'api2.yubico.com/wsapi/2.0/verify', |
154 | 154 | 'api3.yubico.com/wsapi/2.0/verify', |
155 | 155 | 'api4.yubico.com/wsapi/2.0/verify', |
156 | 156 | 'api5.yubico.com/wsapi/2.0/verify'); |
157 | 157 | |
158 | - if ($this->_url_index>=count($url_list)) return false; |
|
158 | + if ($this->_url_index >= count($url_list)) return false; |
|
159 | 159 | else return $url_list[$this->_url_index++]; |
160 | 160 | } |
161 | 161 | |
@@ -166,7 +166,7 @@ discard block |
||
166 | 166 | */ |
167 | 167 | function URLreset() |
168 | 168 | { |
169 | - $this->_url_index=0; |
|
169 | + $this->_url_index = 0; |
|
170 | 170 | } |
171 | 171 | |
172 | 172 | /** |
@@ -176,7 +176,7 @@ discard block |
||
176 | 176 | */ |
177 | 177 | function addURLpart($URLpart) |
178 | 178 | { |
179 | - $this->_url_list[]=$URLpart; |
|
179 | + $this->_url_list[] = $URLpart; |
|
180 | 180 | } |
181 | 181 | |
182 | 182 | /** |
@@ -212,13 +212,13 @@ discard block |
||
212 | 212 | */ |
213 | 213 | function parsePasswordOTP($str, $delim = '[:]') |
214 | 214 | { |
215 | - if (!preg_match("/^((.*)" . $delim . ")?" . |
|
216 | - "(([cbdefghijklnrtuv]{0,16})" . |
|
215 | + if (!preg_match("/^((.*)".$delim.")?". |
|
216 | + "(([cbdefghijklnrtuv]{0,16})". |
|
217 | 217 | "([cbdefghijklnrtuv]{32}))$/i", |
218 | 218 | $str, $matches)) { |
219 | 219 | /* Dvorak? */ |
220 | - if (!preg_match("/^((.*)" . $delim . ")?" . |
|
221 | - "(([jxe\.uidchtnbpygk]{0,16})" . |
|
220 | + if (!preg_match("/^((.*)".$delim.")?". |
|
221 | + "(([jxe\.uidchtnbpygk]{0,16})". |
|
222 | 222 | "([jxe\.uidchtnbpygk]{32}))$/i", |
223 | 223 | $str, $matches)) { |
224 | 224 | return false; |
@@ -253,10 +253,10 @@ discard block |
||
253 | 253 | } |
254 | 254 | $param_array = array(); |
255 | 255 | foreach ($parameters as $param) { |
256 | - if(!preg_match("/" . $param . "=([0-9]+)/", $this->_response, $out)) { |
|
257 | - return PEAR::raiseError('Could not parse parameter ' . $param . ' from response'); |
|
256 | + if (!preg_match("/".$param."=([0-9]+)/", $this->_response, $out)) { |
|
257 | + return PEAR::raiseError('Could not parse parameter '.$param.' from response'); |
|
258 | 258 | } |
259 | - $param_array[$param]=$out[1]; |
|
259 | + $param_array[$param] = $out[1]; |
|
260 | 260 | } |
261 | 261 | return $param_array; |
262 | 262 | } |
@@ -278,8 +278,8 @@ discard block |
||
278 | 278 | * @return mixed PEAR error on error, true otherwise |
279 | 279 | * @access public |
280 | 280 | */ |
281 | - function verify($token, $use_timestamp=null, $wait_for_all=False, |
|
282 | - $sl=null, $timeout=null) |
|
281 | + function verify($token, $use_timestamp = null, $wait_for_all = False, |
|
282 | + $sl = null, $timeout = null) |
|
283 | 283 | { |
284 | 284 | /* Construct parameters string */ |
285 | 285 | $ret = $this->parsePasswordOTP($token); |
@@ -295,23 +295,23 @@ discard block |
||
295 | 295 | if ($timeout) $params['timeout'] = $timeout; |
296 | 296 | ksort($params); |
297 | 297 | $parameters = ''; |
298 | - foreach($params as $p=>$v) $parameters .= "&" . $p . "=" . $v; |
|
298 | + foreach ($params as $p=>$v) $parameters .= "&".$p."=".$v; |
|
299 | 299 | $parameters = ltrim($parameters, "&"); |
300 | 300 | |
301 | 301 | /* Generate signature. */ |
302 | - if($this->_key <> "") { |
|
302 | + if ($this->_key <> "") { |
|
303 | 303 | $signature = base64_encode(hash_hmac('sha1', $parameters, |
304 | 304 | $this->_key, true)); |
305 | 305 | $signature = preg_replace('/\+/', '%2B', $signature); |
306 | - $parameters .= '&h=' . $signature; |
|
306 | + $parameters .= '&h='.$signature; |
|
307 | 307 | } |
308 | 308 | |
309 | 309 | /* Generate and prepare request. */ |
310 | - $this->_lastquery=null; |
|
310 | + $this->_lastquery = null; |
|
311 | 311 | $this->URLreset(); |
312 | 312 | $mh = curl_multi_init(); |
313 | 313 | $ch = array(); |
314 | - while($URLpart=$this->getNextURLpart()) |
|
314 | + while ($URLpart = $this->getNextURLpart()) |
|
315 | 315 | { |
316 | 316 | /* Support https. */ |
317 | 317 | if ($this->_https) { |
@@ -319,7 +319,7 @@ discard block |
||
319 | 319 | } else { |
320 | 320 | $query = "http://"; |
321 | 321 | } |
322 | - $query .= $URLpart . "?" . $parameters; |
|
322 | + $query .= $URLpart."?".$parameters; |
|
323 | 323 | |
324 | 324 | if ($this->_lastquery) { $this->_lastquery .= " "; } |
325 | 325 | $this->_lastquery .= $query; |
@@ -338,13 +338,13 @@ discard block |
||
338 | 338 | if ($timeout) curl_setopt($handle, CURLOPT_TIMEOUT, $timeout); |
339 | 339 | curl_multi_add_handle($mh, $handle); |
340 | 340 | |
341 | - $ch[(int)$handle] = $handle; |
|
341 | + $ch[(int) $handle] = $handle; |
|
342 | 342 | } |
343 | 343 | |
344 | 344 | /* Execute and read request. */ |
345 | - $this->_response=null; |
|
346 | - $replay=False; |
|
347 | - $valid=False; |
|
345 | + $this->_response = null; |
|
346 | + $replay = False; |
|
347 | + $valid = False; |
|
348 | 348 | do { |
349 | 349 | /* Let curl do its work. */ |
350 | 350 | while (($mrc = curl_multi_exec($mh, $active)) |
@@ -357,11 +357,11 @@ discard block |
||
357 | 357 | /* We have a complete response from one server. */ |
358 | 358 | |
359 | 359 | $str = curl_multi_getcontent($info['handle']); |
360 | - $cinfo = curl_getinfo ($info['handle']); |
|
360 | + $cinfo = curl_getinfo($info['handle']); |
|
361 | 361 | |
362 | 362 | if ($wait_for_all) { # Better debug info |
363 | - $this->_response .= 'URL=' . $cinfo['url'] ."\n" |
|
364 | - . $str . "\n"; |
|
363 | + $this->_response .= 'URL='.$cinfo['url']."\n" |
|
364 | + . $str."\n"; |
|
365 | 365 | } |
366 | 366 | |
367 | 367 | if (preg_match("/status=([a-zA-Z0-9_]+)/", $str, $out)) { |
@@ -386,7 +386,7 @@ discard block |
||
386 | 386 | elseif ($this->_key <> "") { |
387 | 387 | /* Case 2. Verify signature first */ |
388 | 388 | $rows = explode("\r\n", trim($str)); |
389 | - $response=array(); |
|
389 | + $response = array(); |
|
390 | 390 | while (list($key, $val) = each($rows)) { |
391 | 391 | /* = is also used in BASE64 encoding so we only replace the first = by # which is not used in BASE64 */ |
392 | 392 | $val = preg_replace('/=/', '#', $val, 1); |
@@ -394,13 +394,13 @@ discard block |
||
394 | 394 | $response[$row[0]] = $row[1]; |
395 | 395 | } |
396 | 396 | |
397 | - $parameters=array('nonce','otp', 'sessioncounter', 'sessionuse', 'sl', 'status', 't', 'timeout', 'timestamp'); |
|
397 | + $parameters = array('nonce', 'otp', 'sessioncounter', 'sessionuse', 'sl', 'status', 't', 'timeout', 'timestamp'); |
|
398 | 398 | sort($parameters); |
399 | - $check=Null; |
|
399 | + $check = Null; |
|
400 | 400 | foreach ($parameters as $param) { |
401 | 401 | if (array_key_exists($param, $response)) { |
402 | - if ($check) $check = $check . '&'; |
|
403 | - $check = $check . $param . '=' . $response[$param]; |
|
402 | + if ($check) $check = $check.'&'; |
|
403 | + $check = $check.$param.'='.$response[$param]; |
|
404 | 404 | } |
405 | 405 | } |
406 | 406 | |
@@ -408,25 +408,25 @@ discard block |
||
408 | 408 | base64_encode(hash_hmac('sha1', utf8_encode($check), |
409 | 409 | $this->_key, true)); |
410 | 410 | |
411 | - if($response['h'] == $checksignature) { |
|
411 | + if ($response['h'] == $checksignature) { |
|
412 | 412 | if ($status == 'REPLAYED_OTP') { |
413 | 413 | if (!$wait_for_all) { $this->_response = $str; } |
414 | - $replay=True; |
|
414 | + $replay = True; |
|
415 | 415 | } |
416 | 416 | if ($status == 'OK') { |
417 | 417 | if (!$wait_for_all) { $this->_response = $str; } |
418 | - $valid=True; |
|
418 | + $valid = True; |
|
419 | 419 | } |
420 | 420 | } |
421 | 421 | } else { |
422 | 422 | /* Case 3. We check the status directly */ |
423 | 423 | if ($status == 'REPLAYED_OTP') { |
424 | 424 | if (!$wait_for_all) { $this->_response = $str; } |
425 | - $replay=True; |
|
425 | + $replay = True; |
|
426 | 426 | } |
427 | 427 | if ($status == 'OK') { |
428 | 428 | if (!$wait_for_all) { $this->_response = $str; } |
429 | - $valid=True; |
|
429 | + $valid = True; |
|
430 | 430 | } |
431 | 431 | } |
432 | 432 | } |
@@ -445,7 +445,7 @@ discard block |
||
445 | 445 | |
446 | 446 | curl_multi_remove_handle($mh, $info['handle']); |
447 | 447 | curl_close($info['handle']); |
448 | - unset ($ch[(int)$info['handle']]); |
|
448 | + unset ($ch[(int) $info['handle']]); |
|
449 | 449 | } |
450 | 450 | curl_multi_select($mh); |
451 | 451 | } |
@@ -457,10 +457,10 @@ discard block |
||
457 | 457 | */ |
458 | 458 | |
459 | 459 | foreach ($ch as $h) { |
460 | - curl_multi_remove_handle ($mh, $h); |
|
461 | - curl_close ($h); |
|
460 | + curl_multi_remove_handle($mh, $h); |
|
461 | + curl_close($h); |
|
462 | 462 | } |
463 | - curl_multi_close ($mh); |
|
463 | + curl_multi_close($mh); |
|
464 | 464 | |
465 | 465 | if ($replay) return PEAR::raiseError('REPLAYED_OTP'); |
466 | 466 | if ($valid) return true; |
@@ -148,15 +148,21 @@ discard block |
||
148 | 148 | */ |
149 | 149 | function getNextURLpart() |
150 | 150 | { |
151 | - if ($this->_url_list) $url_list=$this->_url_list; |
|
152 | - else $url_list=array('api.yubico.com/wsapi/2.0/verify', |
|
151 | + if ($this->_url_list) { |
|
152 | + $url_list=$this->_url_list; |
|
153 | + } else { |
|
154 | + $url_list=array('api.yubico.com/wsapi/2.0/verify', |
|
153 | 155 | 'api2.yubico.com/wsapi/2.0/verify', |
154 | 156 | 'api3.yubico.com/wsapi/2.0/verify', |
155 | 157 | 'api4.yubico.com/wsapi/2.0/verify', |
156 | 158 | 'api5.yubico.com/wsapi/2.0/verify'); |
159 | + } |
|
157 | 160 | |
158 | - if ($this->_url_index>=count($url_list)) return false; |
|
159 | - else return $url_list[$this->_url_index++]; |
|
161 | + if ($this->_url_index>=count($url_list)) { |
|
162 | + return false; |
|
163 | + } else { |
|
164 | + return $url_list[$this->_url_index++]; |
|
165 | + } |
|
160 | 166 | } |
161 | 167 | |
162 | 168 | /** |
@@ -290,12 +296,20 @@ discard block |
||
290 | 296 | 'otp'=>$ret['otp'], |
291 | 297 | 'nonce'=>md5(uniqid(rand()))); |
292 | 298 | /* Take care of protocol version 2 parameters */ |
293 | - if ($use_timestamp) $params['timestamp'] = 1; |
|
294 | - if ($sl) $params['sl'] = $sl; |
|
295 | - if ($timeout) $params['timeout'] = $timeout; |
|
299 | + if ($use_timestamp) { |
|
300 | + $params['timestamp'] = 1; |
|
301 | + } |
|
302 | + if ($sl) { |
|
303 | + $params['sl'] = $sl; |
|
304 | + } |
|
305 | + if ($timeout) { |
|
306 | + $params['timeout'] = $timeout; |
|
307 | + } |
|
296 | 308 | ksort($params); |
297 | 309 | $parameters = ''; |
298 | - foreach($params as $p=>$v) $parameters .= "&" . $p . "=" . $v; |
|
310 | + foreach($params as $p=>$v) { |
|
311 | + $parameters .= "&" . $p . "=" . $v; |
|
312 | + } |
|
299 | 313 | $parameters = ltrim($parameters, "&"); |
300 | 314 | |
301 | 315 | /* Generate signature. */ |
@@ -335,7 +349,9 @@ discard block |
||
335 | 349 | /* If timeout is set, we better apply it here as well |
336 | 350 | in case the validation server fails to follow it. |
337 | 351 | */ |
338 | - if ($timeout) curl_setopt($handle, CURLOPT_TIMEOUT, $timeout); |
|
352 | + if ($timeout) { |
|
353 | + curl_setopt($handle, CURLOPT_TIMEOUT, $timeout); |
|
354 | + } |
|
339 | 355 | curl_multi_add_handle($mh, $handle); |
340 | 356 | |
341 | 357 | $ch[(int)$handle] = $handle; |
@@ -382,8 +398,7 @@ discard block |
||
382 | 398 | if (!preg_match("/otp=".$params['otp']."/", $str) || |
383 | 399 | !preg_match("/nonce=".$params['nonce']."/", $str)) { |
384 | 400 | /* Case 1. Ignore response. */ |
385 | - } |
|
386 | - elseif ($this->_key <> "") { |
|
401 | + } elseif ($this->_key <> "") { |
|
387 | 402 | /* Case 2. Verify signature first */ |
388 | 403 | $rows = explode("\r\n", trim($str)); |
389 | 404 | $response=array(); |
@@ -399,7 +414,9 @@ discard block |
||
399 | 414 | $check=Null; |
400 | 415 | foreach ($parameters as $param) { |
401 | 416 | if (array_key_exists($param, $response)) { |
402 | - if ($check) $check = $check . '&'; |
|
417 | + if ($check) { |
|
418 | + $check = $check . '&'; |
|
419 | + } |
|
403 | 420 | $check = $check . $param . '=' . $response[$param]; |
404 | 421 | } |
405 | 422 | } |
@@ -438,8 +455,12 @@ discard block |
||
438 | 455 | curl_close($h); |
439 | 456 | } |
440 | 457 | curl_multi_close($mh); |
441 | - if ($replay) return PEAR::raiseError('REPLAYED_OTP'); |
|
442 | - if ($valid) return true; |
|
458 | + if ($replay) { |
|
459 | + return PEAR::raiseError('REPLAYED_OTP'); |
|
460 | + } |
|
461 | + if ($valid) { |
|
462 | + return true; |
|
463 | + } |
|
443 | 464 | return PEAR::raiseError($status); |
444 | 465 | } |
445 | 466 | |
@@ -462,8 +483,12 @@ discard block |
||
462 | 483 | } |
463 | 484 | curl_multi_close ($mh); |
464 | 485 | |
465 | - if ($replay) return PEAR::raiseError('REPLAYED_OTP'); |
|
466 | - if ($valid) return true; |
|
486 | + if ($replay) { |
|
487 | + return PEAR::raiseError('REPLAYED_OTP'); |
|
488 | + } |
|
489 | + if ($valid) { |
|
490 | + return true; |
|
491 | + } |
|
467 | 492 | return PEAR::raiseError('NO_VALID_ANSWER'); |
468 | 493 | } |
469 | 494 | } |
@@ -294,6 +294,9 @@ discard block |
||
294 | 294 | return call_user_func_array(array($this, 'query'), $args); |
295 | 295 | } |
296 | 296 | |
297 | + /** |
|
298 | + * @param string $which |
|
299 | + */ |
|
297 | 300 | public function insertOrReplace($which, $table, $datas, $options=array()) { |
298 | 301 | $datas = unserialize(serialize($datas)); // break references within array |
299 | 302 | $keys = $values = array(); |
@@ -566,6 +569,9 @@ discard block |
||
566 | 569 | return $query; |
567 | 570 | } |
568 | 571 | |
572 | + /** |
|
573 | + * @param string $prepend |
|
574 | + */ |
|
569 | 575 | protected function prependCall($function, $args, $prepend) { array_unshift($args, $prepend); return call_user_func_array($function, $args); } |
570 | 576 | public function query() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'assoc'); } |
571 | 577 | public function queryAllLists() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'list'); } |
@@ -18,33 +18,33 @@ discard block |
||
18 | 18 | |
19 | 19 | |
20 | 20 | class DB { |
21 | - // initial connection |
|
22 | - public static $dbName = ''; |
|
23 | - public static $user = ''; |
|
24 | - public static $password = ''; |
|
25 | - public static $host = 'localhost'; |
|
26 | - public static $port = null; |
|
27 | - public static $encoding = 'utf8'; |
|
28 | - |
|
29 | - // configure workings |
|
30 | - public static $param_char = '%'; |
|
31 | - public static $named_param_seperator = '_'; |
|
32 | - public static $success_handler = false; |
|
33 | - public static $error_handler = true; |
|
34 | - public static $throw_exception_on_error = false; |
|
35 | - public static $nonsql_error_handler = null; |
|
36 | - public static $throw_exception_on_nonsql_error = false; |
|
37 | - public static $nested_transactions = false; |
|
38 | - public static $usenull = true; |
|
39 | - |
|
40 | - // internal |
|
41 | - protected static $mdb = null; |
|
42 | - |
|
43 | - public static function getMDB() { |
|
21 | + // initial connection |
|
22 | + public static $dbName = ''; |
|
23 | + public static $user = ''; |
|
24 | + public static $password = ''; |
|
25 | + public static $host = 'localhost'; |
|
26 | + public static $port = null; |
|
27 | + public static $encoding = 'utf8'; |
|
28 | + |
|
29 | + // configure workings |
|
30 | + public static $param_char = '%'; |
|
31 | + public static $named_param_seperator = '_'; |
|
32 | + public static $success_handler = false; |
|
33 | + public static $error_handler = true; |
|
34 | + public static $throw_exception_on_error = false; |
|
35 | + public static $nonsql_error_handler = null; |
|
36 | + public static $throw_exception_on_nonsql_error = false; |
|
37 | + public static $nested_transactions = false; |
|
38 | + public static $usenull = true; |
|
39 | + |
|
40 | + // internal |
|
41 | + protected static $mdb = null; |
|
42 | + |
|
43 | + public static function getMDB() { |
|
44 | 44 | $mdb = DB::$mdb; |
45 | 45 | |
46 | 46 | if ($mdb === null) { |
47 | - $mdb = DB::$mdb = new MeekroDB(); |
|
47 | + $mdb = DB::$mdb = new MeekroDB(); |
|
48 | 48 | } |
49 | 49 | |
50 | 50 | if ($mdb->param_char !== DB::$param_char) $mdb->param_char = DB::$param_char; |
@@ -58,91 +58,91 @@ discard block |
||
58 | 58 | if ($mdb->usenull !== DB::$usenull) $mdb->usenull = DB::$usenull; |
59 | 59 | |
60 | 60 | return $mdb; |
61 | - } |
|
62 | - |
|
63 | - public static function get() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'get'), $args); } |
|
64 | - public static function disconnect() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'disconnect'), $args); } |
|
65 | - public static function query() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'query'), $args); } |
|
66 | - public static function queryFirstRow() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstRow'), $args); } |
|
67 | - public static function queryOneRow() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryOneRow'), $args); } |
|
68 | - public static function queryAllLists() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryAllLists'), $args); } |
|
69 | - public static function queryFullColumns() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFullColumns'), $args); } |
|
70 | - public static function queryFirstList() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstList'), $args); } |
|
71 | - public static function queryOneList() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryOneList'), $args); } |
|
72 | - public static function queryFirstColumn() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstColumn'), $args); } |
|
73 | - public static function queryOneColumn() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryOneColumn'), $args); } |
|
74 | - public static function queryFirstField() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstField'), $args); } |
|
75 | - public static function queryOneField() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryOneField'), $args); } |
|
76 | - public static function queryRaw() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryRaw'), $args); } |
|
77 | - public static function queryRawUnbuf() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryRawUnbuf'), $args); } |
|
78 | - |
|
79 | - public static function insert() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'insert'), $args); } |
|
80 | - public static function insertIgnore() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'insertIgnore'), $args); } |
|
81 | - public static function insertUpdate() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'insertUpdate'), $args); } |
|
82 | - public static function replace() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'replace'), $args); } |
|
83 | - public static function update() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'update'), $args); } |
|
84 | - public static function delete() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'delete'), $args); } |
|
85 | - |
|
86 | - public static function insertId() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'insertId'), $args); } |
|
87 | - public static function count() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'count'), $args); } |
|
88 | - public static function affectedRows() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'affectedRows'), $args); } |
|
89 | - |
|
90 | - public static function useDB() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'useDB'), $args); } |
|
91 | - public static function startTransaction() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'startTransaction'), $args); } |
|
92 | - public static function commit() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'commit'), $args); } |
|
93 | - public static function rollback() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'rollback'), $args); } |
|
94 | - public static function tableList() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'tableList'), $args); } |
|
95 | - public static function columnList() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'columnList'), $args); } |
|
96 | - |
|
97 | - public static function sqlEval() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'sqlEval'), $args); } |
|
98 | - public static function nonSQLError() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'nonSQLError'), $args); } |
|
99 | - |
|
100 | - public static function serverVersion() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'serverVersion'), $args); } |
|
101 | - public static function transactionDepth() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'transactionDepth'), $args); } |
|
102 | - |
|
103 | - |
|
104 | - public static function debugMode($handler = true) { |
|
61 | + } |
|
62 | + |
|
63 | + public static function get() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'get'), $args); } |
|
64 | + public static function disconnect() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'disconnect'), $args); } |
|
65 | + public static function query() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'query'), $args); } |
|
66 | + public static function queryFirstRow() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstRow'), $args); } |
|
67 | + public static function queryOneRow() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryOneRow'), $args); } |
|
68 | + public static function queryAllLists() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryAllLists'), $args); } |
|
69 | + public static function queryFullColumns() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFullColumns'), $args); } |
|
70 | + public static function queryFirstList() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstList'), $args); } |
|
71 | + public static function queryOneList() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryOneList'), $args); } |
|
72 | + public static function queryFirstColumn() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstColumn'), $args); } |
|
73 | + public static function queryOneColumn() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryOneColumn'), $args); } |
|
74 | + public static function queryFirstField() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstField'), $args); } |
|
75 | + public static function queryOneField() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryOneField'), $args); } |
|
76 | + public static function queryRaw() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryRaw'), $args); } |
|
77 | + public static function queryRawUnbuf() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryRawUnbuf'), $args); } |
|
78 | + |
|
79 | + public static function insert() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'insert'), $args); } |
|
80 | + public static function insertIgnore() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'insertIgnore'), $args); } |
|
81 | + public static function insertUpdate() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'insertUpdate'), $args); } |
|
82 | + public static function replace() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'replace'), $args); } |
|
83 | + public static function update() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'update'), $args); } |
|
84 | + public static function delete() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'delete'), $args); } |
|
85 | + |
|
86 | + public static function insertId() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'insertId'), $args); } |
|
87 | + public static function count() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'count'), $args); } |
|
88 | + public static function affectedRows() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'affectedRows'), $args); } |
|
89 | + |
|
90 | + public static function useDB() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'useDB'), $args); } |
|
91 | + public static function startTransaction() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'startTransaction'), $args); } |
|
92 | + public static function commit() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'commit'), $args); } |
|
93 | + public static function rollback() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'rollback'), $args); } |
|
94 | + public static function tableList() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'tableList'), $args); } |
|
95 | + public static function columnList() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'columnList'), $args); } |
|
96 | + |
|
97 | + public static function sqlEval() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'sqlEval'), $args); } |
|
98 | + public static function nonSQLError() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'nonSQLError'), $args); } |
|
99 | + |
|
100 | + public static function serverVersion() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'serverVersion'), $args); } |
|
101 | + public static function transactionDepth() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'transactionDepth'), $args); } |
|
102 | + |
|
103 | + |
|
104 | + public static function debugMode($handler = true) { |
|
105 | 105 | DB::$success_handler = $handler; |
106 | - } |
|
106 | + } |
|
107 | 107 | |
108 | - public function version() { |
|
108 | + public function version() { |
|
109 | 109 | return $this->version_info(); |
110 | - } |
|
110 | + } |
|
111 | 111 | |
112 | 112 | } |
113 | 113 | |
114 | 114 | |
115 | 115 | class MeekroDB { |
116 | - // initial connection |
|
117 | - public $dbName = ''; |
|
118 | - public $user = ''; |
|
119 | - public $password = ''; |
|
120 | - public $host = 'localhost'; |
|
121 | - public $port = null; |
|
122 | - public $encoding = 'latin1'; |
|
123 | - |
|
124 | - // configure workings |
|
125 | - public $param_char = '%'; |
|
126 | - public $named_param_seperator = '_'; |
|
127 | - public $success_handler = false; |
|
128 | - public $error_handler = true; |
|
129 | - public $throw_exception_on_error = false; |
|
130 | - public $nonsql_error_handler = null; |
|
131 | - public $throw_exception_on_nonsql_error = false; |
|
132 | - public $nested_transactions = false; |
|
133 | - public $usenull = true; |
|
134 | - |
|
135 | - // internal |
|
136 | - public $internal_mysql = null; |
|
137 | - public $server_info = null; |
|
138 | - public $insert_id = 0; |
|
139 | - public $num_rows = 0; |
|
140 | - public $affected_rows = 0; |
|
141 | - public $current_db = null; |
|
142 | - public $nested_transactions_count = 0; |
|
143 | - |
|
144 | - |
|
145 | - public function __construct($host=null, $user=null, $password=null, $dbName=null, $port=null, $encoding=null) { |
|
116 | + // initial connection |
|
117 | + public $dbName = ''; |
|
118 | + public $user = ''; |
|
119 | + public $password = ''; |
|
120 | + public $host = 'localhost'; |
|
121 | + public $port = null; |
|
122 | + public $encoding = 'latin1'; |
|
123 | + |
|
124 | + // configure workings |
|
125 | + public $param_char = '%'; |
|
126 | + public $named_param_seperator = '_'; |
|
127 | + public $success_handler = false; |
|
128 | + public $error_handler = true; |
|
129 | + public $throw_exception_on_error = false; |
|
130 | + public $nonsql_error_handler = null; |
|
131 | + public $throw_exception_on_nonsql_error = false; |
|
132 | + public $nested_transactions = false; |
|
133 | + public $usenull = true; |
|
134 | + |
|
135 | + // internal |
|
136 | + public $internal_mysql = null; |
|
137 | + public $server_info = null; |
|
138 | + public $insert_id = 0; |
|
139 | + public $num_rows = 0; |
|
140 | + public $affected_rows = 0; |
|
141 | + public $current_db = null; |
|
142 | + public $nested_transactions_count = 0; |
|
143 | + |
|
144 | + |
|
145 | + public function __construct($host=null, $user=null, $password=null, $dbName=null, $port=null, $encoding=null) { |
|
146 | 146 | if ($host === null) $host = DB::$host; |
147 | 147 | if ($user === null) $user = DB::$user; |
148 | 148 | if ($password === null) $password = DB::$password; |
@@ -156,131 +156,131 @@ discard block |
||
156 | 156 | $this->dbName = $dbName; |
157 | 157 | $this->port = $port; |
158 | 158 | $this->encoding = $encoding; |
159 | - } |
|
159 | + } |
|
160 | 160 | |
161 | - public function get() { |
|
161 | + public function get() { |
|
162 | 162 | $mysql = $this->internal_mysql; |
163 | 163 | |
164 | 164 | if (!($mysql instanceof MySQLi)) { |
165 | - if (! $this->port) $this->port = ini_get('mysqli.default_port'); |
|
166 | - $this->current_db = $this->dbName; |
|
165 | + if (! $this->port) $this->port = ini_get('mysqli.default_port'); |
|
166 | + $this->current_db = $this->dbName; |
|
167 | 167 | |
168 | - $mysql = new mysqli($this->host, $this->user, $this->password, $this->dbName, $this->port); |
|
168 | + $mysql = new mysqli($this->host, $this->user, $this->password, $this->dbName, $this->port); |
|
169 | 169 | |
170 | - if ($mysql->connect_error) { |
|
170 | + if ($mysql->connect_error) { |
|
171 | 171 | $this->nonSQLError('Unable to connect to MySQL server! Error: ' . $mysql->connect_error); |
172 | - } |
|
172 | + } |
|
173 | 173 | |
174 | - $mysql->set_charset($this->encoding); |
|
175 | - $this->internal_mysql = $mysql; |
|
176 | - $this->server_info = $mysql->server_info; |
|
174 | + $mysql->set_charset($this->encoding); |
|
175 | + $this->internal_mysql = $mysql; |
|
176 | + $this->server_info = $mysql->server_info; |
|
177 | 177 | } |
178 | 178 | |
179 | 179 | return $mysql; |
180 | - } |
|
180 | + } |
|
181 | 181 | |
182 | - public function disconnect() { |
|
182 | + public function disconnect() { |
|
183 | 183 | $mysqli = $this->internal_mysql; |
184 | 184 | if ($mysqli instanceof MySQLi) { |
185 | - if ($thread_id = $mysqli->thread_id) $mysqli->kill($thread_id); |
|
186 | - $mysqli->close(); |
|
185 | + if ($thread_id = $mysqli->thread_id) $mysqli->kill($thread_id); |
|
186 | + $mysqli->close(); |
|
187 | 187 | } |
188 | 188 | $this->internal_mysql = null; |
189 | - } |
|
189 | + } |
|
190 | 190 | |
191 | - public function nonSQLError($message) { |
|
191 | + public function nonSQLError($message) { |
|
192 | 192 | if ($this->throw_exception_on_nonsql_error) { |
193 | - $e = new MeekroDBException($message); |
|
194 | - throw $e; |
|
193 | + $e = new MeekroDBException($message); |
|
194 | + throw $e; |
|
195 | 195 | } |
196 | 196 | |
197 | 197 | $error_handler = is_callable($this->nonsql_error_handler) ? $this->nonsql_error_handler : 'meekrodb_error_handler'; |
198 | 198 | |
199 | 199 | call_user_func($error_handler, array( |
200 | - 'type' => 'nonsql', |
|
201 | - 'error' => $message |
|
200 | + 'type' => 'nonsql', |
|
201 | + 'error' => $message |
|
202 | 202 | )); |
203 | - } |
|
203 | + } |
|
204 | 204 | |
205 | - public function debugMode($handler = true) { |
|
205 | + public function debugMode($handler = true) { |
|
206 | 206 | $this->success_handler = $handler; |
207 | - } |
|
207 | + } |
|
208 | 208 | |
209 | - public function serverVersion() { $this->get(); return $this->server_info; } |
|
210 | - public function transactionDepth() { return $this->nested_transactions_count; } |
|
211 | - public function insertId() { return $this->insert_id; } |
|
212 | - public function affectedRows() { return $this->affected_rows; } |
|
213 | - public function count() { $args = func_get_args(); return call_user_func_array(array($this, 'numRows'), $args); } |
|
214 | - public function numRows() { return $this->num_rows; } |
|
209 | + public function serverVersion() { $this->get(); return $this->server_info; } |
|
210 | + public function transactionDepth() { return $this->nested_transactions_count; } |
|
211 | + public function insertId() { return $this->insert_id; } |
|
212 | + public function affectedRows() { return $this->affected_rows; } |
|
213 | + public function count() { $args = func_get_args(); return call_user_func_array(array($this, 'numRows'), $args); } |
|
214 | + public function numRows() { return $this->num_rows; } |
|
215 | 215 | |
216 | - public function useDB() { $args = func_get_args(); return call_user_func_array(array($this, 'setDB'), $args); } |
|
217 | - public function setDB($dbName) { |
|
216 | + public function useDB() { $args = func_get_args(); return call_user_func_array(array($this, 'setDB'), $args); } |
|
217 | + public function setDB($dbName) { |
|
218 | 218 | $db = $this->get(); |
219 | 219 | if (! $db->select_db($dbName)) $this->nonSQLError("Unable to set database to $dbName"); |
220 | 220 | $this->current_db = $dbName; |
221 | - } |
|
221 | + } |
|
222 | 222 | |
223 | 223 | |
224 | - public function startTransaction() { |
|
224 | + public function startTransaction() { |
|
225 | 225 | if ($this->nested_transactions && $this->serverVersion() < '5.5') { |
226 | - return $this->nonSQLError("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL " . $this->serverVersion()); |
|
226 | + return $this->nonSQLError("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL " . $this->serverVersion()); |
|
227 | 227 | } |
228 | 228 | |
229 | 229 | if (!$this->nested_transactions || $this->nested_transactions_count == 0) { |
230 | - $this->query('START TRANSACTION'); |
|
231 | - $this->nested_transactions_count = 1; |
|
230 | + $this->query('START TRANSACTION'); |
|
231 | + $this->nested_transactions_count = 1; |
|
232 | 232 | } else { |
233 | - $this->query("SAVEPOINT LEVEL{$this->nested_transactions_count}"); |
|
234 | - $this->nested_transactions_count++; |
|
233 | + $this->query("SAVEPOINT LEVEL{$this->nested_transactions_count}"); |
|
234 | + $this->nested_transactions_count++; |
|
235 | 235 | } |
236 | 236 | |
237 | 237 | return $this->nested_transactions_count; |
238 | - } |
|
238 | + } |
|
239 | 239 | |
240 | - public function commit($all=false) { |
|
240 | + public function commit($all=false) { |
|
241 | 241 | if ($this->nested_transactions && $this->serverVersion() < '5.5') { |
242 | - return $this->nonSQLError("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL " . $this->serverVersion()); |
|
242 | + return $this->nonSQLError("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL " . $this->serverVersion()); |
|
243 | 243 | } |
244 | 244 | |
245 | 245 | if ($this->nested_transactions && $this->nested_transactions_count > 0) |
246 | - $this->nested_transactions_count--; |
|
246 | + $this->nested_transactions_count--; |
|
247 | 247 | |
248 | 248 | if (!$this->nested_transactions || $all || $this->nested_transactions_count == 0) { |
249 | - $this->nested_transactions_count = 0; |
|
250 | - $this->query('COMMIT'); |
|
249 | + $this->nested_transactions_count = 0; |
|
250 | + $this->query('COMMIT'); |
|
251 | 251 | } else { |
252 | - $this->query("RELEASE SAVEPOINT LEVEL{$this->nested_transactions_count}"); |
|
252 | + $this->query("RELEASE SAVEPOINT LEVEL{$this->nested_transactions_count}"); |
|
253 | 253 | } |
254 | 254 | |
255 | 255 | return $this->nested_transactions_count; |
256 | - } |
|
256 | + } |
|
257 | 257 | |
258 | - public function rollback($all=false) { |
|
258 | + public function rollback($all=false) { |
|
259 | 259 | if ($this->nested_transactions && $this->serverVersion() < '5.5') { |
260 | - return $this->nonSQLError("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL " . $this->serverVersion()); |
|
260 | + return $this->nonSQLError("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL " . $this->serverVersion()); |
|
261 | 261 | } |
262 | 262 | |
263 | 263 | if ($this->nested_transactions && $this->nested_transactions_count > 0) |
264 | - $this->nested_transactions_count--; |
|
264 | + $this->nested_transactions_count--; |
|
265 | 265 | |
266 | 266 | if (!$this->nested_transactions || $all || $this->nested_transactions_count == 0) { |
267 | - $this->nested_transactions_count = 0; |
|
268 | - $this->query('ROLLBACK'); |
|
267 | + $this->nested_transactions_count = 0; |
|
268 | + $this->query('ROLLBACK'); |
|
269 | 269 | } else { |
270 | - $this->query("ROLLBACK TO SAVEPOINT LEVEL{$this->nested_transactions_count}"); |
|
270 | + $this->query("ROLLBACK TO SAVEPOINT LEVEL{$this->nested_transactions_count}"); |
|
271 | 271 | } |
272 | 272 | |
273 | 273 | return $this->nested_transactions_count; |
274 | - } |
|
274 | + } |
|
275 | 275 | |
276 | - protected function formatTableName($table) { |
|
276 | + protected function formatTableName($table) { |
|
277 | 277 | $table = trim($table, '`'); |
278 | 278 | |
279 | 279 | if (strpos($table, '.')) return implode('.', array_map(array($this, 'formatTableName'), explode('.', $table))); |
280 | 280 | else return '`' . str_replace('`', '``', $table) . '`'; |
281 | - } |
|
281 | + } |
|
282 | 282 | |
283 | - public function update() { |
|
283 | + public function update() { |
|
284 | 284 | $args = func_get_args(); |
285 | 285 | $table = array_shift($args); |
286 | 286 | $params = array_shift($args); |
@@ -292,95 +292,95 @@ discard block |
||
292 | 292 | array_unshift($args, $table); |
293 | 293 | array_unshift($args, $query); |
294 | 294 | return call_user_func_array(array($this, 'query'), $args); |
295 | - } |
|
295 | + } |
|
296 | 296 | |
297 | - public function insertOrReplace($which, $table, $datas, $options=array()) { |
|
297 | + public function insertOrReplace($which, $table, $datas, $options=array()) { |
|
298 | 298 | $datas = unserialize(serialize($datas)); // break references within array |
299 | 299 | $keys = $values = array(); |
300 | 300 | |
301 | 301 | if (isset($datas[0]) && is_array($datas[0])) { |
302 | - foreach ($datas as $datum) { |
|
302 | + foreach ($datas as $datum) { |
|
303 | 303 | ksort($datum); |
304 | 304 | if (! $keys) $keys = array_keys($datum); |
305 | 305 | $values[] = array_values($datum); |
306 | - } |
|
306 | + } |
|
307 | 307 | |
308 | 308 | } else { |
309 | - $keys = array_keys($datas); |
|
310 | - $values = array_values($datas); |
|
309 | + $keys = array_keys($datas); |
|
310 | + $values = array_values($datas); |
|
311 | 311 | } |
312 | 312 | |
313 | 313 | if (isset($options['ignore']) && $options['ignore']) $which = 'INSERT IGNORE'; |
314 | 314 | |
315 | 315 | if (isset($options['update']) && is_array($options['update']) && $options['update'] && strtolower($which) == 'insert') { |
316 | - if (array_values($options['update']) !== $options['update']) { |
|
316 | + if (array_values($options['update']) !== $options['update']) { |
|
317 | 317 | return $this->query("INSERT INTO %b %lb VALUES %? ON DUPLICATE KEY UPDATE %?", $table, $keys, $values, $options['update']); |
318 | - } else { |
|
318 | + } else { |
|
319 | 319 | $update_str = array_shift($options['update']); |
320 | 320 | $query_param = array("INSERT INTO %b %lb VALUES %? ON DUPLICATE KEY UPDATE $update_str", $table, $keys, $values); |
321 | 321 | $query_param = array_merge($query_param, $options['update']); |
322 | 322 | return call_user_func_array(array($this, 'query'), $query_param); |
323 | - } |
|
323 | + } |
|
324 | 324 | |
325 | 325 | } |
326 | 326 | |
327 | 327 | return $this->query("%l INTO %b %lb VALUES %?", $which, $table, $keys, $values); |
328 | - } |
|
328 | + } |
|
329 | 329 | |
330 | - public function insert($table, $data) { return $this->insertOrReplace('INSERT', $table, $data); } |
|
331 | - public function insertIgnore($table, $data) { return $this->insertOrReplace('INSERT', $table, $data, array('ignore' => true)); } |
|
332 | - public function replace($table, $data) { return $this->insertOrReplace('REPLACE', $table, $data); } |
|
330 | + public function insert($table, $data) { return $this->insertOrReplace('INSERT', $table, $data); } |
|
331 | + public function insertIgnore($table, $data) { return $this->insertOrReplace('INSERT', $table, $data, array('ignore' => true)); } |
|
332 | + public function replace($table, $data) { return $this->insertOrReplace('REPLACE', $table, $data); } |
|
333 | 333 | |
334 | - public function insertUpdate() { |
|
334 | + public function insertUpdate() { |
|
335 | 335 | $args = func_get_args(); |
336 | 336 | $table = array_shift($args); |
337 | 337 | $data = array_shift($args); |
338 | 338 | |
339 | 339 | if (! isset($args[0])) { // update will have all the data of the insert |
340 | - if (isset($data[0]) && is_array($data[0])) { //multiple insert rows specified -- failing! |
|
340 | + if (isset($data[0]) && is_array($data[0])) { //multiple insert rows specified -- failing! |
|
341 | 341 | $this->nonSQLError("Badly formatted insertUpdate() query -- you didn't specify the update component!"); |
342 | - } |
|
342 | + } |
|
343 | 343 | |
344 | - $args[0] = $data; |
|
344 | + $args[0] = $data; |
|
345 | 345 | } |
346 | 346 | |
347 | 347 | if (is_array($args[0])) $update = $args[0]; |
348 | 348 | else $update = $args; |
349 | 349 | |
350 | 350 | return $this->insertOrReplace('INSERT', $table, $data, array('update' => $update)); |
351 | - } |
|
351 | + } |
|
352 | 352 | |
353 | - public function delete() { |
|
353 | + public function delete() { |
|
354 | 354 | $args = func_get_args(); |
355 | 355 | $table = $this->formatTableName(array_shift($args)); |
356 | 356 | $where = array_shift($args); |
357 | 357 | $buildquery = "DELETE FROM $table WHERE $where"; |
358 | 358 | array_unshift($args, $buildquery); |
359 | 359 | return call_user_func_array(array($this, 'query'), $args); |
360 | - } |
|
360 | + } |
|
361 | 361 | |
362 | - public function sqleval() { |
|
362 | + public function sqleval() { |
|
363 | 363 | $args = func_get_args(); |
364 | 364 | $text = call_user_func_array(array($this, 'parseQueryParams'), $args); |
365 | 365 | return new MeekroDBEval($text); |
366 | - } |
|
366 | + } |
|
367 | 367 | |
368 | - public function columnList($table) { |
|
368 | + public function columnList($table) { |
|
369 | 369 | return $this->queryOneColumn('Field', "SHOW COLUMNS FROM $table"); |
370 | - } |
|
370 | + } |
|
371 | 371 | |
372 | - public function tableList($db = null) { |
|
372 | + public function tableList($db = null) { |
|
373 | 373 | if ($db) { |
374 | - $olddb = $this->current_db; |
|
375 | - $this->useDB($db); |
|
374 | + $olddb = $this->current_db; |
|
375 | + $this->useDB($db); |
|
376 | 376 | } |
377 | 377 | |
378 | 378 | $result = $this->queryFirstColumn('SHOW TABLES'); |
379 | 379 | if (isset($olddb)) $this->useDB($olddb); |
380 | 380 | return $result; |
381 | - } |
|
381 | + } |
|
382 | 382 | |
383 | - protected function preparseQueryParams() { |
|
383 | + protected function preparseQueryParams() { |
|
384 | 384 | $args = func_get_args(); |
385 | 385 | $sql = trim(strval(array_shift($args))); |
386 | 386 | $args_all = $args; |
@@ -391,32 +391,32 @@ discard block |
||
391 | 391 | $named_seperator_length = strlen($this->named_param_seperator); |
392 | 392 | |
393 | 393 | $types = array( |
394 | - $this->param_char . 'll', // list of literals |
|
395 | - $this->param_char . 'ls', // list of strings |
|
396 | - $this->param_char . 'l', // literal |
|
397 | - $this->param_char . 'li', // list of integers |
|
398 | - $this->param_char . 'ld', // list of decimals |
|
399 | - $this->param_char . 'lb', // list of backticks |
|
400 | - $this->param_char . 'lt', // list of timestamps |
|
401 | - $this->param_char . 's', // string |
|
402 | - $this->param_char . 'i', // integer |
|
403 | - $this->param_char . 'd', // double / decimal |
|
404 | - $this->param_char . 'b', // backtick |
|
405 | - $this->param_char . 't', // timestamp |
|
406 | - $this->param_char . '?', // infer type |
|
407 | - $this->param_char . 'ss' // search string (like string, surrounded with %'s) |
|
394 | + $this->param_char . 'll', // list of literals |
|
395 | + $this->param_char . 'ls', // list of strings |
|
396 | + $this->param_char . 'l', // literal |
|
397 | + $this->param_char . 'li', // list of integers |
|
398 | + $this->param_char . 'ld', // list of decimals |
|
399 | + $this->param_char . 'lb', // list of backticks |
|
400 | + $this->param_char . 'lt', // list of timestamps |
|
401 | + $this->param_char . 's', // string |
|
402 | + $this->param_char . 'i', // integer |
|
403 | + $this->param_char . 'd', // double / decimal |
|
404 | + $this->param_char . 'b', // backtick |
|
405 | + $this->param_char . 't', // timestamp |
|
406 | + $this->param_char . '?', // infer type |
|
407 | + $this->param_char . 'ss' // search string (like string, surrounded with %'s) |
|
408 | 408 | ); |
409 | 409 | |
410 | 410 | // generate list of all MeekroDB variables in our query, and their position |
411 | 411 | // in the form "offset => variable", sorted by offsets |
412 | 412 | $posList = array(); |
413 | 413 | foreach ($types as $type) { |
414 | - $lastPos = 0; |
|
415 | - while (($pos = strpos($sql, $type, $lastPos)) !== false) { |
|
414 | + $lastPos = 0; |
|
415 | + while (($pos = strpos($sql, $type, $lastPos)) !== false) { |
|
416 | 416 | $lastPos = $pos + 1; |
417 | 417 | if (isset($posList[$pos]) && strlen($posList[$pos]) > strlen($type)) continue; |
418 | 418 | $posList[$pos] = $type; |
419 | - } |
|
419 | + } |
|
420 | 420 | } |
421 | 421 | |
422 | 422 | ksort($posList); |
@@ -425,24 +425,24 @@ discard block |
||
425 | 425 | $chunkyQuery = array(); // preparsed query |
426 | 426 | $pos_adj = 0; // how much we've added or removed from the original sql string |
427 | 427 | foreach ($posList as $pos => $type) { |
428 | - $type = substr($type, $param_char_length); // variable, without % in front of it |
|
429 | - $length_type = strlen($type) + $param_char_length; // length of variable w/o % |
|
428 | + $type = substr($type, $param_char_length); // variable, without % in front of it |
|
429 | + $length_type = strlen($type) + $param_char_length; // length of variable w/o % |
|
430 | 430 | |
431 | - $new_pos = $pos + $pos_adj; // position of start of variable |
|
432 | - $new_pos_back = $new_pos + $length_type; // position of end of variable |
|
433 | - $arg_number_length = 0; // length of any named or numbered parameter addition |
|
431 | + $new_pos = $pos + $pos_adj; // position of start of variable |
|
432 | + $new_pos_back = $new_pos + $length_type; // position of end of variable |
|
433 | + $arg_number_length = 0; // length of any named or numbered parameter addition |
|
434 | 434 | |
435 | - // handle numbered parameters |
|
436 | - if ($arg_number_length = strspn($sql, '0123456789', $new_pos_back)) { |
|
435 | + // handle numbered parameters |
|
436 | + if ($arg_number_length = strspn($sql, '0123456789', $new_pos_back)) { |
|
437 | 437 | $arg_number = substr($sql, $new_pos_back, $arg_number_length); |
438 | 438 | if (! array_key_exists($arg_number, $args_all)) $this->nonSQLError("Non existent argument reference (arg $arg_number): $sql"); |
439 | 439 | |
440 | 440 | $arg = $args_all[$arg_number]; |
441 | 441 | |
442 | - // handle named parameters |
|
443 | - } else if (substr($sql, $new_pos_back, $named_seperator_length) == $this->named_param_seperator) { |
|
442 | + // handle named parameters |
|
443 | + } else if (substr($sql, $new_pos_back, $named_seperator_length) == $this->named_param_seperator) { |
|
444 | 444 | $arg_number_length = strspn($sql, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_', |
445 | - $new_pos_back + $named_seperator_length) + $named_seperator_length; |
|
445 | + $new_pos_back + $named_seperator_length) + $named_seperator_length; |
|
446 | 446 | |
447 | 447 | $arg_number = substr($sql, $new_pos_back + $named_seperator_length, $arg_number_length - $named_seperator_length); |
448 | 448 | if (count($args_all) != 1 || !is_array($args_all[0])) $this->nonSQLError("If you use named parameters, the second argument must be an array of parameters"); |
@@ -450,38 +450,38 @@ discard block |
||
450 | 450 | |
451 | 451 | $arg = $args_all[0][$arg_number]; |
452 | 452 | |
453 | - } else { |
|
453 | + } else { |
|
454 | 454 | $arg_number = 0; |
455 | 455 | $arg = array_shift($args); |
456 | - } |
|
456 | + } |
|
457 | 457 | |
458 | - if ($new_pos > 0) $chunkyQuery[] = substr($sql, 0, $new_pos); |
|
458 | + if ($new_pos > 0) $chunkyQuery[] = substr($sql, 0, $new_pos); |
|
459 | 459 | |
460 | - if (is_object($arg) && ($arg instanceof WhereClause)) { |
|
460 | + if (is_object($arg) && ($arg instanceof WhereClause)) { |
|
461 | 461 | list($clause_sql, $clause_args) = $arg->textAndArgs(); |
462 | 462 | array_unshift($clause_args, $clause_sql); |
463 | 463 | $preparsed_sql = call_user_func_array(array($this, 'preparseQueryParams'), $clause_args); |
464 | 464 | $chunkyQuery = array_merge($chunkyQuery, $preparsed_sql); |
465 | - } else { |
|
465 | + } else { |
|
466 | 466 | $chunkyQuery[] = array('type' => $type, 'value' => $arg); |
467 | - } |
|
467 | + } |
|
468 | 468 | |
469 | - $sql = substr($sql, $new_pos_back + $arg_number_length); |
|
470 | - $pos_adj -= $new_pos_back + $arg_number_length; |
|
469 | + $sql = substr($sql, $new_pos_back + $arg_number_length); |
|
470 | + $pos_adj -= $new_pos_back + $arg_number_length; |
|
471 | 471 | } |
472 | 472 | |
473 | 473 | if (strlen($sql) > 0) $chunkyQuery[] = $sql; |
474 | 474 | |
475 | 475 | return $chunkyQuery; |
476 | - } |
|
476 | + } |
|
477 | 477 | |
478 | - protected function escape($str) { return "'" . $this->get()->real_escape_string(strval($str)) . "'"; } |
|
478 | + protected function escape($str) { return "'" . $this->get()->real_escape_string(strval($str)) . "'"; } |
|
479 | 479 | |
480 | - protected function sanitize($value) { |
|
480 | + protected function sanitize($value) { |
|
481 | 481 | if (is_object($value)) { |
482 | - if ($value instanceof MeekroDBEval) return $value->text; |
|
483 | - else if ($value instanceof DateTime) return $this->escape($value->format('Y-m-d H:i:s')); |
|
484 | - else return ''; |
|
482 | + if ($value instanceof MeekroDBEval) return $value->text; |
|
483 | + else if ($value instanceof DateTime) return $this->escape($value->format('Y-m-d H:i:s')); |
|
484 | + else return ''; |
|
485 | 485 | } |
486 | 486 | |
487 | 487 | if (is_null($value)) return $this->usenull ? 'NULL' : "''"; |
@@ -490,33 +490,33 @@ discard block |
||
490 | 490 | else if (is_float($value)) return $value; |
491 | 491 | |
492 | 492 | else if (is_array($value)) { |
493 | - // non-assoc array? |
|
494 | - if (array_values($value) === $value) { |
|
493 | + // non-assoc array? |
|
494 | + if (array_values($value) === $value) { |
|
495 | 495 | if (is_array($value[0])) return implode(', ', array_map(array($this, 'sanitize'), $value)); |
496 | 496 | else return '(' . implode(', ', array_map(array($this, 'sanitize'), $value)) . ')'; |
497 | - } |
|
497 | + } |
|
498 | 498 | |
499 | - $pairs = array(); |
|
500 | - foreach ($value as $k => $v) { |
|
499 | + $pairs = array(); |
|
500 | + foreach ($value as $k => $v) { |
|
501 | 501 | $pairs[] = $this->formatTableName($k) . '=' . $this->sanitize($v); |
502 | - } |
|
502 | + } |
|
503 | 503 | |
504 | - return implode(', ', $pairs); |
|
504 | + return implode(', ', $pairs); |
|
505 | 505 | } |
506 | 506 | else return $this->escape($value); |
507 | - } |
|
507 | + } |
|
508 | 508 | |
509 | - protected function parseTS($ts) { |
|
509 | + protected function parseTS($ts) { |
|
510 | 510 | if (is_string($ts)) return date('Y-m-d H:i:s', strtotime($ts)); |
511 | 511 | else if (is_object($ts) && ($ts instanceof DateTime)) return $ts->format('Y-m-d H:i:s'); |
512 | - } |
|
512 | + } |
|
513 | 513 | |
514 | - protected function intval($var) { |
|
514 | + protected function intval($var) { |
|
515 | 515 | if (PHP_INT_SIZE == 8) return intval($var); |
516 | 516 | return floor(doubleval($var)); |
517 | - } |
|
517 | + } |
|
518 | 518 | |
519 | - protected function parseQueryParams() { |
|
519 | + protected function parseQueryParams() { |
|
520 | 520 | $args = func_get_args(); |
521 | 521 | $chunkyQuery = call_user_func_array(array($this, 'preparseQueryParams'), $args); |
522 | 522 | |
@@ -524,57 +524,57 @@ discard block |
||
524 | 524 | $array_types = array('ls', 'li', 'ld', 'lb', 'll', 'lt'); |
525 | 525 | |
526 | 526 | foreach ($chunkyQuery as $chunk) { |
527 | - if (is_string($chunk)) { |
|
527 | + if (is_string($chunk)) { |
|
528 | 528 | $query .= $chunk; |
529 | 529 | continue; |
530 | - } |
|
530 | + } |
|
531 | 531 | |
532 | - $type = $chunk['type']; |
|
533 | - $arg = $chunk['value']; |
|
534 | - $result = ''; |
|
532 | + $type = $chunk['type']; |
|
533 | + $arg = $chunk['value']; |
|
534 | + $result = ''; |
|
535 | 535 | |
536 | - if ($type != '?') { |
|
536 | + if ($type != '?') { |
|
537 | 537 | $is_array_type = in_array($type, $array_types, true); |
538 | 538 | if ($is_array_type && !is_array($arg)) $this->nonSQLError("Badly formatted SQL query: Expected array, got scalar instead!"); |
539 | 539 | else if (!$is_array_type && is_array($arg)) $this->nonSQLError("Badly formatted SQL query: Expected scalar, got array instead!"); |
540 | - } |
|
540 | + } |
|
541 | 541 | |
542 | - if ($type == 's') $result = $this->escape($arg); |
|
543 | - else if ($type == 'i') $result = $this->intval($arg); |
|
544 | - else if ($type == 'd') $result = doubleval($arg); |
|
545 | - else if ($type == 'b') $result = $this->formatTableName($arg); |
|
546 | - else if ($type == 'l') $result = $arg; |
|
547 | - else if ($type == 'ss') $result = $this->escape("%" . str_replace(array('%', '_'), array('\%', '\_'), $arg) . "%"); |
|
548 | - else if ($type == 't') $result = $this->escape($this->parseTS($arg)); |
|
542 | + if ($type == 's') $result = $this->escape($arg); |
|
543 | + else if ($type == 'i') $result = $this->intval($arg); |
|
544 | + else if ($type == 'd') $result = doubleval($arg); |
|
545 | + else if ($type == 'b') $result = $this->formatTableName($arg); |
|
546 | + else if ($type == 'l') $result = $arg; |
|
547 | + else if ($type == 'ss') $result = $this->escape("%" . str_replace(array('%', '_'), array('\%', '\_'), $arg) . "%"); |
|
548 | + else if ($type == 't') $result = $this->escape($this->parseTS($arg)); |
|
549 | 549 | |
550 | - else if ($type == 'ls') $result = array_map(array($this, 'escape'), $arg); |
|
551 | - else if ($type == 'li') $result = array_map(array($this, 'intval'), $arg); |
|
552 | - else if ($type == 'ld') $result = array_map('doubleval', $arg); |
|
553 | - else if ($type == 'lb') $result = array_map(array($this, 'formatTableName'), $arg); |
|
554 | - else if ($type == 'll') $result = $arg; |
|
555 | - else if ($type == 'lt') $result = array_map(array($this, 'escape'), array_map(array($this, 'parseTS'), $arg)); |
|
550 | + else if ($type == 'ls') $result = array_map(array($this, 'escape'), $arg); |
|
551 | + else if ($type == 'li') $result = array_map(array($this, 'intval'), $arg); |
|
552 | + else if ($type == 'ld') $result = array_map('doubleval', $arg); |
|
553 | + else if ($type == 'lb') $result = array_map(array($this, 'formatTableName'), $arg); |
|
554 | + else if ($type == 'll') $result = $arg; |
|
555 | + else if ($type == 'lt') $result = array_map(array($this, 'escape'), array_map(array($this, 'parseTS'), $arg)); |
|
556 | 556 | |
557 | - else if ($type == '?') $result = $this->sanitize($arg); |
|
557 | + else if ($type == '?') $result = $this->sanitize($arg); |
|
558 | 558 | |
559 | - else $this->nonSQLError("Badly formatted SQL query: Invalid MeekroDB param $type"); |
|
559 | + else $this->nonSQLError("Badly formatted SQL query: Invalid MeekroDB param $type"); |
|
560 | 560 | |
561 | - if (is_array($result)) $result = '(' . implode(',', $result) . ')'; |
|
561 | + if (is_array($result)) $result = '(' . implode(',', $result) . ')'; |
|
562 | 562 | |
563 | - $query .= $result; |
|
563 | + $query .= $result; |
|
564 | 564 | } |
565 | 565 | |
566 | 566 | return $query; |
567 | - } |
|
567 | + } |
|
568 | 568 | |
569 | - protected function prependCall($function, $args, $prepend) { array_unshift($args, $prepend); return call_user_func_array($function, $args); } |
|
570 | - public function query() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'assoc'); } |
|
571 | - public function queryAllLists() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'list'); } |
|
572 | - public function queryFullColumns() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'full'); } |
|
569 | + protected function prependCall($function, $args, $prepend) { array_unshift($args, $prepend); return call_user_func_array($function, $args); } |
|
570 | + public function query() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'assoc'); } |
|
571 | + public function queryAllLists() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'list'); } |
|
572 | + public function queryFullColumns() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'full'); } |
|
573 | 573 | |
574 | - public function queryRaw() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'raw_buf'); } |
|
575 | - public function queryRawUnbuf() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'raw_unbuf'); } |
|
574 | + public function queryRaw() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'raw_buf'); } |
|
575 | + public function queryRawUnbuf() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'raw_unbuf'); } |
|
576 | 576 | |
577 | - protected function queryHelper() { |
|
577 | + protected function queryHelper() { |
|
578 | 578 | $args = func_get_args(); |
579 | 579 | $type = array_shift($args); |
580 | 580 | $db = $this->get(); |
@@ -584,23 +584,23 @@ discard block |
||
584 | 584 | $full_names = false; |
585 | 585 | |
586 | 586 | switch ($type) { |
587 | - case 'assoc': |
|
587 | + case 'assoc': |
|
588 | 588 | break; |
589 | - case 'list': |
|
589 | + case 'list': |
|
590 | 590 | $row_type = 'list'; |
591 | 591 | break; |
592 | - case 'full': |
|
592 | + case 'full': |
|
593 | 593 | $row_type = 'list'; |
594 | 594 | $full_names = true; |
595 | 595 | break; |
596 | - case 'raw_buf': |
|
596 | + case 'raw_buf': |
|
597 | 597 | $row_type = 'raw'; |
598 | 598 | break; |
599 | - case 'raw_unbuf': |
|
599 | + case 'raw_unbuf': |
|
600 | 600 | $is_buffered = false; |
601 | 601 | $row_type = 'raw'; |
602 | 602 | break; |
603 | - default: |
|
603 | + default: |
|
604 | 604 | $this->nonSQLError('Error -- invalid argument to queryHelper!'); |
605 | 605 | } |
606 | 606 | |
@@ -613,41 +613,41 @@ discard block |
||
613 | 613 | |
614 | 614 | // ----- BEGIN ERROR HANDLING |
615 | 615 | if (!$sql || $db->error) { |
616 | - if ($this->error_handler) { |
|
616 | + if ($this->error_handler) { |
|
617 | 617 | $db_error = $db->error; |
618 | 618 | $db_errno = $db->errno; |
619 | 619 | $db->query( |
620 | - "INSERT INTO ".$GLOBALS['pre']."log_system SET |
|
620 | + "INSERT INTO ".$GLOBALS['pre']."log_system SET |
|
621 | 621 | date=".time().", |
622 | 622 | qui=".$_SESSION['user_id'].", |
623 | 623 | label='Query: ".addslashes($sql)."<br />Error: ".addslashes($db_error)."<br />@ ".mysqli_real_escape_string($link, filter_var($_SERVER['REQUEST_URI'], FILTER_SANITIZE_STRING))."', |
624 | 624 | type='error'", |
625 | - MYSQLI_USE_RESULT |
|
625 | + MYSQLI_USE_RESULT |
|
626 | 626 | ); |
627 | 627 | |
628 | 628 | $error_handler = is_callable($this->error_handler) ? $this->error_handler : 'meekrodb_error_handler'; |
629 | 629 | |
630 | 630 | call_user_func($error_handler, array( |
631 | - 'type' => 'sql', |
|
632 | - 'query' => $sql, |
|
633 | - 'error' => $db_error, |
|
634 | - 'code' => $db_errno |
|
631 | + 'type' => 'sql', |
|
632 | + 'query' => $sql, |
|
633 | + 'error' => $db_error, |
|
634 | + 'code' => $db_errno |
|
635 | 635 | )); |
636 | - } |
|
636 | + } |
|
637 | 637 | |
638 | - if ($this->throw_exception_on_error) { |
|
638 | + if ($this->throw_exception_on_error) { |
|
639 | 639 | $e = new MeekroDBException($db_error, $sql, $db_errno); |
640 | 640 | throw $e; |
641 | - } |
|
641 | + } |
|
642 | 642 | } else if ($this->success_handler) { |
643 | - $runtime = sprintf('%f', $runtime * 1000); |
|
644 | - $success_handler = is_callable($this->success_handler) ? $this->success_handler : 'meekrodb_debugmode_handler'; |
|
643 | + $runtime = sprintf('%f', $runtime * 1000); |
|
644 | + $success_handler = is_callable($this->success_handler) ? $this->success_handler : 'meekrodb_debugmode_handler'; |
|
645 | 645 | |
646 | - call_user_func($success_handler, array( |
|
646 | + call_user_func($success_handler, array( |
|
647 | 647 | 'query' => $sql, |
648 | 648 | 'runtime' => $runtime, |
649 | 649 | 'affected' => $db->affected_rows |
650 | - )); |
|
650 | + )); |
|
651 | 651 | } |
652 | 652 | |
653 | 653 | // ----- END ERROR HANDLING |
@@ -664,45 +664,45 @@ discard block |
||
664 | 664 | $return = array(); |
665 | 665 | |
666 | 666 | if ($full_names) { |
667 | - $infos = array(); |
|
668 | - foreach ($result->fetch_fields() as $info) { |
|
667 | + $infos = array(); |
|
668 | + foreach ($result->fetch_fields() as $info) { |
|
669 | 669 | if (strlen($info->table)) $infos[] = $info->table . '.' . $info->name; |
670 | 670 | else $infos[] = $info->name; |
671 | - } |
|
671 | + } |
|
672 | 672 | } |
673 | 673 | |
674 | 674 | while ($row = ($row_type == 'assoc' ? $result->fetch_assoc() : $result->fetch_row())) { |
675 | - if ($full_names) $row = array_combine($infos, $row); |
|
676 | - $return[] = $row; |
|
675 | + if ($full_names) $row = array_combine($infos, $row); |
|
676 | + $return[] = $row; |
|
677 | 677 | } |
678 | 678 | |
679 | 679 | // free results |
680 | 680 | $result->free(); |
681 | 681 | while ($db->more_results()) { |
682 | - $db->next_result(); |
|
683 | - if ($result = $db->use_result()) $result->free(); |
|
682 | + $db->next_result(); |
|
683 | + if ($result = $db->use_result()) $result->free(); |
|
684 | 684 | } |
685 | 685 | |
686 | 686 | return $return; |
687 | - } |
|
687 | + } |
|
688 | 688 | |
689 | - public function queryOneRow() { $args = func_get_args(); return call_user_func_array(array($this, 'queryFirstRow'), $args); } |
|
690 | - public function queryFirstRow() { |
|
689 | + public function queryOneRow() { $args = func_get_args(); return call_user_func_array(array($this, 'queryFirstRow'), $args); } |
|
690 | + public function queryFirstRow() { |
|
691 | 691 | $args = func_get_args(); |
692 | 692 | $result = call_user_func_array(array($this, 'query'), $args); |
693 | 693 | if (! $result) return null; |
694 | 694 | return reset($result); |
695 | - } |
|
695 | + } |
|
696 | 696 | |
697 | - public function queryOneList() { $args = func_get_args(); return call_user_func_array(array($this, 'queryFirstList'), $args); } |
|
698 | - public function queryFirstList() { |
|
697 | + public function queryOneList() { $args = func_get_args(); return call_user_func_array(array($this, 'queryFirstList'), $args); } |
|
698 | + public function queryFirstList() { |
|
699 | 699 | $args = func_get_args(); |
700 | 700 | $result = call_user_func_array(array($this, 'queryAllLists'), $args); |
701 | 701 | if (! $result) return null; |
702 | 702 | return reset($result); |
703 | - } |
|
703 | + } |
|
704 | 704 | |
705 | - public function queryFirstColumn() { |
|
705 | + public function queryFirstColumn() { |
|
706 | 706 | $args = func_get_args(); |
707 | 707 | $results = call_user_func_array(array($this, 'queryAllLists'), $args); |
708 | 708 | $ret = array(); |
@@ -710,13 +710,13 @@ discard block |
||
710 | 710 | if (!count($results) || !count($results[0])) return $ret; |
711 | 711 | |
712 | 712 | foreach ($results as $row) { |
713 | - $ret[] = $row[0]; |
|
713 | + $ret[] = $row[0]; |
|
714 | 714 | } |
715 | 715 | |
716 | 716 | return $ret; |
717 | - } |
|
717 | + } |
|
718 | 718 | |
719 | - public function queryOneColumn() { |
|
719 | + public function queryOneColumn() { |
|
720 | 720 | $args = func_get_args(); |
721 | 721 | $column = array_shift($args); |
722 | 722 | $results = call_user_func_array(array($this, 'query'), $args); |
@@ -724,103 +724,103 @@ discard block |
||
724 | 724 | |
725 | 725 | if (!count($results) || !count($results[0])) return $ret; |
726 | 726 | if ($column === null) { |
727 | - $keys = array_keys($results[0]); |
|
728 | - $column = $keys[0]; |
|
727 | + $keys = array_keys($results[0]); |
|
728 | + $column = $keys[0]; |
|
729 | 729 | } |
730 | 730 | |
731 | 731 | foreach ($results as $row) { |
732 | - $ret[] = $row[$column]; |
|
732 | + $ret[] = $row[$column]; |
|
733 | 733 | } |
734 | 734 | |
735 | 735 | return $ret; |
736 | - } |
|
736 | + } |
|
737 | 737 | |
738 | - public function queryFirstField() { |
|
738 | + public function queryFirstField() { |
|
739 | 739 | $args = func_get_args(); |
740 | 740 | $row = call_user_func_array(array($this, 'queryFirstList'), $args); |
741 | 741 | if ($row == null) return null; |
742 | 742 | return $row[0]; |
743 | - } |
|
743 | + } |
|
744 | 744 | |
745 | - public function queryOneField() { |
|
745 | + public function queryOneField() { |
|
746 | 746 | $args = func_get_args(); |
747 | 747 | $column = array_shift($args); |
748 | 748 | |
749 | 749 | $row = call_user_func_array(array($this, 'queryOneRow'), $args); |
750 | 750 | if ($row == null) { |
751 | - return null; |
|
751 | + return null; |
|
752 | 752 | } else if ($column === null) { |
753 | - $keys = array_keys($row); |
|
754 | - $column = $keys[0]; |
|
753 | + $keys = array_keys($row); |
|
754 | + $column = $keys[0]; |
|
755 | 755 | } |
756 | 756 | |
757 | 757 | return $row[$column]; |
758 | - } |
|
758 | + } |
|
759 | 759 | } |
760 | 760 | |
761 | 761 | class WhereClause { |
762 | - public $type = 'and'; //AND or OR |
|
763 | - public $negate = false; |
|
764 | - public $clauses = array(); |
|
762 | + public $type = 'and'; //AND or OR |
|
763 | + public $negate = false; |
|
764 | + public $clauses = array(); |
|
765 | 765 | |
766 | - function __construct($type) { |
|
766 | + function __construct($type) { |
|
767 | 767 | $type = strtolower($type); |
768 | 768 | if ($type !== 'or' && $type !== 'and') DB::nonSQLError('you must use either WhereClause(and) or WhereClause(or)'); |
769 | 769 | $this->type = $type; |
770 | - } |
|
770 | + } |
|
771 | 771 | |
772 | - function add() { |
|
772 | + function add() { |
|
773 | 773 | $args = func_get_args(); |
774 | 774 | $sql = array_shift($args); |
775 | 775 | |
776 | 776 | if ($sql instanceof WhereClause) { |
777 | - $this->clauses[] = $sql; |
|
777 | + $this->clauses[] = $sql; |
|
778 | 778 | } else { |
779 | - $this->clauses[] = array('sql' => $sql, 'args' => $args); |
|
779 | + $this->clauses[] = array('sql' => $sql, 'args' => $args); |
|
780 | + } |
|
780 | 781 | } |
781 | - } |
|
782 | 782 | |
783 | - function negateLast() { |
|
783 | + function negateLast() { |
|
784 | 784 | $i = count($this->clauses) - 1; |
785 | 785 | if (!isset($this->clauses[$i])) return; |
786 | 786 | |
787 | 787 | if ($this->clauses[$i] instanceof WhereClause) { |
788 | - $this->clauses[$i]->negate(); |
|
788 | + $this->clauses[$i]->negate(); |
|
789 | 789 | } else { |
790 | - $this->clauses[$i]['sql'] = 'NOT (' . $this->clauses[$i]['sql'] . ')'; |
|
790 | + $this->clauses[$i]['sql'] = 'NOT (' . $this->clauses[$i]['sql'] . ')'; |
|
791 | + } |
|
791 | 792 | } |
792 | - } |
|
793 | 793 | |
794 | - function negate() { |
|
794 | + function negate() { |
|
795 | 795 | $this->negate = ! $this->negate; |
796 | - } |
|
796 | + } |
|
797 | 797 | |
798 | - function addClause($type) { |
|
798 | + function addClause($type) { |
|
799 | 799 | $r = new WhereClause($type); |
800 | 800 | $this->add($r); |
801 | 801 | return $r; |
802 | - } |
|
802 | + } |
|
803 | 803 | |
804 | - function count() { |
|
804 | + function count() { |
|
805 | 805 | return count($this->clauses); |
806 | - } |
|
806 | + } |
|
807 | 807 | |
808 | - function textAndArgs() { |
|
808 | + function textAndArgs() { |
|
809 | 809 | $sql = array(); |
810 | 810 | $args = array(); |
811 | 811 | |
812 | 812 | if (count($this->clauses) == 0) return array('(1)', $args); |
813 | 813 | |
814 | 814 | foreach ($this->clauses as $clause) { |
815 | - if ($clause instanceof WhereClause) { |
|
815 | + if ($clause instanceof WhereClause) { |
|
816 | 816 | list($clause_sql, $clause_args) = $clause->textAndArgs(); |
817 | - } else { |
|
817 | + } else { |
|
818 | 818 | $clause_sql = $clause['sql']; |
819 | 819 | $clause_args = $clause['args']; |
820 | - } |
|
820 | + } |
|
821 | 821 | |
822 | - $sql[] = "($clause_sql)"; |
|
823 | - $args = array_merge($args, $clause_args); |
|
822 | + $sql[] = "($clause_sql)"; |
|
823 | + $args = array_merge($args, $clause_args); |
|
824 | 824 | } |
825 | 825 | |
826 | 826 | if ($this->type == 'and') $sql = implode(' AND ', $sql); |
@@ -828,111 +828,111 @@ discard block |
||
828 | 828 | |
829 | 829 | if ($this->negate) $sql = '(NOT ' . $sql . ')'; |
830 | 830 | return array($sql, $args); |
831 | - } |
|
831 | + } |
|
832 | 832 | |
833 | - // backwards compatability |
|
834 | - // we now return full WhereClause object here and evaluate it in preparseQueryParams |
|
835 | - function text() { return $this; } |
|
833 | + // backwards compatability |
|
834 | + // we now return full WhereClause object here and evaluate it in preparseQueryParams |
|
835 | + function text() { return $this; } |
|
836 | 836 | } |
837 | 837 | |
838 | 838 | class DBTransaction { |
839 | - private $committed = false; |
|
839 | + private $committed = false; |
|
840 | 840 | |
841 | - function __construct() { |
|
841 | + function __construct() { |
|
842 | 842 | DB::startTransaction(); |
843 | - } |
|
844 | - function __destruct() { |
|
843 | + } |
|
844 | + function __destruct() { |
|
845 | 845 | if (! $this->committed) DB::rollback(); |
846 | - } |
|
847 | - function commit() { |
|
846 | + } |
|
847 | + function commit() { |
|
848 | 848 | DB::commit(); |
849 | 849 | $this->committed = true; |
850 | - } |
|
850 | + } |
|
851 | 851 | |
852 | 852 | |
853 | 853 | } |
854 | 854 | |
855 | 855 | class MeekroDBException extends Exception { |
856 | - protected $query = ''; |
|
856 | + protected $query = ''; |
|
857 | 857 | |
858 | - function __construct($message='', $query='', $code = 0) { |
|
858 | + function __construct($message='', $query='', $code = 0) { |
|
859 | 859 | parent::__construct($message); |
860 | 860 | $this->query = $query; |
861 | - $this->code = $code; |
|
862 | - } |
|
861 | + $this->code = $code; |
|
862 | + } |
|
863 | 863 | |
864 | - public function getQuery() { return $this->query; } |
|
864 | + public function getQuery() { return $this->query; } |
|
865 | 865 | } |
866 | 866 | |
867 | 867 | class DBHelper { |
868 | - /* |
|
868 | + /* |
|
869 | 869 | verticalSlice |
870 | 870 | 1. For an array of assoc rays, return an array of values for a particular key |
871 | 871 | 2. if $keyfield is given, same as above but use that hash key as the key in new array |
872 | 872 | */ |
873 | 873 | |
874 | - public static function verticalSlice($array, $field, $keyfield = null) { |
|
874 | + public static function verticalSlice($array, $field, $keyfield = null) { |
|
875 | 875 | $array = (array) $array; |
876 | 876 | |
877 | 877 | $R = array(); |
878 | 878 | foreach ($array as $obj) { |
879 | - if (! array_key_exists($field, $obj)) die("verticalSlice: array doesn't have requested field\n"); |
|
879 | + if (! array_key_exists($field, $obj)) die("verticalSlice: array doesn't have requested field\n"); |
|
880 | 880 | |
881 | - if ($keyfield) { |
|
881 | + if ($keyfield) { |
|
882 | 882 | if (! array_key_exists($keyfield, $obj)) die("verticalSlice: array doesn't have requested field\n"); |
883 | 883 | $R[$obj[$keyfield]] = $obj[$field]; |
884 | - } else { |
|
884 | + } else { |
|
885 | 885 | $R[] = $obj[$field]; |
886 | - } |
|
886 | + } |
|
887 | 887 | } |
888 | 888 | return $R; |
889 | - } |
|
889 | + } |
|
890 | 890 | |
891 | - /* |
|
891 | + /* |
|
892 | 892 | reIndex |
893 | 893 | For an array of assoc rays, return a new array of assoc rays using a certain field for keys |
894 | 894 | */ |
895 | 895 | |
896 | - public static function reIndex() { |
|
896 | + public static function reIndex() { |
|
897 | 897 | $fields = func_get_args(); |
898 | 898 | $array = array_shift($fields); |
899 | 899 | $array = (array) $array; |
900 | 900 | |
901 | 901 | $R = array(); |
902 | 902 | foreach ($array as $obj) { |
903 | - $target =& $R; |
|
903 | + $target =& $R; |
|
904 | 904 | |
905 | - foreach ($fields as $field) { |
|
905 | + foreach ($fields as $field) { |
|
906 | 906 | if (! array_key_exists($field, $obj)) die("reIndex: array doesn't have requested field\n"); |
907 | 907 | |
908 | 908 | $nextkey = $obj[$field]; |
909 | 909 | $target =& $target[$nextkey]; |
910 | - } |
|
911 | - $target = $obj; |
|
910 | + } |
|
911 | + $target = $obj; |
|
912 | 912 | } |
913 | 913 | return $R; |
914 | - } |
|
914 | + } |
|
915 | 915 | } |
916 | 916 | |
917 | 917 | function meekrodb_error_handler($params) { |
918 | - echo prepareExchangedData('[{"error" : "'.$params['error'].'"}]', "encode"); |
|
918 | + echo prepareExchangedData('[{"error" : "'.$params['error'].'"}]', "encode"); |
|
919 | 919 | |
920 | - die; |
|
920 | + die; |
|
921 | 921 | } |
922 | 922 | |
923 | 923 | function meekrodb_debugmode_handler($params) { |
924 | - echo "QUERY: " . $params['query'] . " [" . $params['runtime'] . " ms]"; |
|
925 | - if (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) { |
|
924 | + echo "QUERY: " . $params['query'] . " [" . $params['runtime'] . " ms]"; |
|
925 | + if (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) { |
|
926 | 926 | echo "\n"; |
927 | - } else { |
|
927 | + } else { |
|
928 | 928 | echo "<br>\n"; |
929 | - } |
|
929 | + } |
|
930 | 930 | } |
931 | 931 | |
932 | 932 | class MeekroDBEval { |
933 | - public $text = ''; |
|
933 | + public $text = ''; |
|
934 | 934 | |
935 | - function __construct($text) { |
|
935 | + function __construct($text) { |
|
936 | 936 | $this->text = $text; |
937 | - } |
|
937 | + } |
|
938 | 938 | } |
939 | 939 | \ No newline at end of file |
@@ -142,7 +142,7 @@ discard block |
||
142 | 142 | public $nested_transactions_count = 0; |
143 | 143 | |
144 | 144 | |
145 | - public function __construct($host=null, $user=null, $password=null, $dbName=null, $port=null, $encoding=null) { |
|
145 | + public function __construct($host = null, $user = null, $password = null, $dbName = null, $port = null, $encoding = null) { |
|
146 | 146 | if ($host === null) $host = DB::$host; |
147 | 147 | if ($user === null) $user = DB::$user; |
148 | 148 | if ($password === null) $password = DB::$password; |
@@ -162,13 +162,13 @@ discard block |
||
162 | 162 | $mysql = $this->internal_mysql; |
163 | 163 | |
164 | 164 | if (!($mysql instanceof MySQLi)) { |
165 | - if (! $this->port) $this->port = ini_get('mysqli.default_port'); |
|
165 | + if (!$this->port) $this->port = ini_get('mysqli.default_port'); |
|
166 | 166 | $this->current_db = $this->dbName; |
167 | 167 | |
168 | 168 | $mysql = new mysqli($this->host, $this->user, $this->password, $this->dbName, $this->port); |
169 | 169 | |
170 | 170 | if ($mysql->connect_error) { |
171 | - $this->nonSQLError('Unable to connect to MySQL server! Error: ' . $mysql->connect_error); |
|
171 | + $this->nonSQLError('Unable to connect to MySQL server! Error: '.$mysql->connect_error); |
|
172 | 172 | } |
173 | 173 | |
174 | 174 | $mysql->set_charset($this->encoding); |
@@ -216,14 +216,14 @@ discard block |
||
216 | 216 | public function useDB() { $args = func_get_args(); return call_user_func_array(array($this, 'setDB'), $args); } |
217 | 217 | public function setDB($dbName) { |
218 | 218 | $db = $this->get(); |
219 | - if (! $db->select_db($dbName)) $this->nonSQLError("Unable to set database to $dbName"); |
|
219 | + if (!$db->select_db($dbName)) $this->nonSQLError("Unable to set database to $dbName"); |
|
220 | 220 | $this->current_db = $dbName; |
221 | 221 | } |
222 | 222 | |
223 | 223 | |
224 | 224 | public function startTransaction() { |
225 | 225 | if ($this->nested_transactions && $this->serverVersion() < '5.5') { |
226 | - return $this->nonSQLError("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL " . $this->serverVersion()); |
|
226 | + return $this->nonSQLError("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL ".$this->serverVersion()); |
|
227 | 227 | } |
228 | 228 | |
229 | 229 | if (!$this->nested_transactions || $this->nested_transactions_count == 0) { |
@@ -237,9 +237,9 @@ discard block |
||
237 | 237 | return $this->nested_transactions_count; |
238 | 238 | } |
239 | 239 | |
240 | - public function commit($all=false) { |
|
240 | + public function commit($all = false) { |
|
241 | 241 | if ($this->nested_transactions && $this->serverVersion() < '5.5') { |
242 | - return $this->nonSQLError("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL " . $this->serverVersion()); |
|
242 | + return $this->nonSQLError("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL ".$this->serverVersion()); |
|
243 | 243 | } |
244 | 244 | |
245 | 245 | if ($this->nested_transactions && $this->nested_transactions_count > 0) |
@@ -255,9 +255,9 @@ discard block |
||
255 | 255 | return $this->nested_transactions_count; |
256 | 256 | } |
257 | 257 | |
258 | - public function rollback($all=false) { |
|
258 | + public function rollback($all = false) { |
|
259 | 259 | if ($this->nested_transactions && $this->serverVersion() < '5.5') { |
260 | - return $this->nonSQLError("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL " . $this->serverVersion()); |
|
260 | + return $this->nonSQLError("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL ".$this->serverVersion()); |
|
261 | 261 | } |
262 | 262 | |
263 | 263 | if ($this->nested_transactions && $this->nested_transactions_count > 0) |
@@ -277,7 +277,7 @@ discard block |
||
277 | 277 | $table = trim($table, '`'); |
278 | 278 | |
279 | 279 | if (strpos($table, '.')) return implode('.', array_map(array($this, 'formatTableName'), explode('.', $table))); |
280 | - else return '`' . str_replace('`', '``', $table) . '`'; |
|
280 | + else return '`'.str_replace('`', '``', $table).'`'; |
|
281 | 281 | } |
282 | 282 | |
283 | 283 | public function update() { |
@@ -286,7 +286,7 @@ discard block |
||
286 | 286 | $params = array_shift($args); |
287 | 287 | $where = array_shift($args); |
288 | 288 | |
289 | - $query = "UPDATE %b SET %? WHERE " . $where; |
|
289 | + $query = "UPDATE %b SET %? WHERE ".$where; |
|
290 | 290 | |
291 | 291 | array_unshift($args, $params); |
292 | 292 | array_unshift($args, $table); |
@@ -294,14 +294,14 @@ discard block |
||
294 | 294 | return call_user_func_array(array($this, 'query'), $args); |
295 | 295 | } |
296 | 296 | |
297 | - public function insertOrReplace($which, $table, $datas, $options=array()) { |
|
297 | + public function insertOrReplace($which, $table, $datas, $options = array()) { |
|
298 | 298 | $datas = unserialize(serialize($datas)); // break references within array |
299 | 299 | $keys = $values = array(); |
300 | 300 | |
301 | 301 | if (isset($datas[0]) && is_array($datas[0])) { |
302 | 302 | foreach ($datas as $datum) { |
303 | 303 | ksort($datum); |
304 | - if (! $keys) $keys = array_keys($datum); |
|
304 | + if (!$keys) $keys = array_keys($datum); |
|
305 | 305 | $values[] = array_values($datum); |
306 | 306 | } |
307 | 307 | |
@@ -336,7 +336,7 @@ discard block |
||
336 | 336 | $table = array_shift($args); |
337 | 337 | $data = array_shift($args); |
338 | 338 | |
339 | - if (! isset($args[0])) { // update will have all the data of the insert |
|
339 | + if (!isset($args[0])) { // update will have all the data of the insert |
|
340 | 340 | if (isset($data[0]) && is_array($data[0])) { //multiple insert rows specified -- failing! |
341 | 341 | $this->nonSQLError("Badly formatted insertUpdate() query -- you didn't specify the update component!"); |
342 | 342 | } |
@@ -391,20 +391,20 @@ discard block |
||
391 | 391 | $named_seperator_length = strlen($this->named_param_seperator); |
392 | 392 | |
393 | 393 | $types = array( |
394 | - $this->param_char . 'll', // list of literals |
|
395 | - $this->param_char . 'ls', // list of strings |
|
396 | - $this->param_char . 'l', // literal |
|
397 | - $this->param_char . 'li', // list of integers |
|
398 | - $this->param_char . 'ld', // list of decimals |
|
399 | - $this->param_char . 'lb', // list of backticks |
|
400 | - $this->param_char . 'lt', // list of timestamps |
|
401 | - $this->param_char . 's', // string |
|
402 | - $this->param_char . 'i', // integer |
|
403 | - $this->param_char . 'd', // double / decimal |
|
404 | - $this->param_char . 'b', // backtick |
|
405 | - $this->param_char . 't', // timestamp |
|
406 | - $this->param_char . '?', // infer type |
|
407 | - $this->param_char . 'ss' // search string (like string, surrounded with %'s) |
|
394 | + $this->param_char.'ll', // list of literals |
|
395 | + $this->param_char.'ls', // list of strings |
|
396 | + $this->param_char.'l', // literal |
|
397 | + $this->param_char.'li', // list of integers |
|
398 | + $this->param_char.'ld', // list of decimals |
|
399 | + $this->param_char.'lb', // list of backticks |
|
400 | + $this->param_char.'lt', // list of timestamps |
|
401 | + $this->param_char.'s', // string |
|
402 | + $this->param_char.'i', // integer |
|
403 | + $this->param_char.'d', // double / decimal |
|
404 | + $this->param_char.'b', // backtick |
|
405 | + $this->param_char.'t', // timestamp |
|
406 | + $this->param_char.'?', // infer type |
|
407 | + $this->param_char.'ss' // search string (like string, surrounded with %'s) |
|
408 | 408 | ); |
409 | 409 | |
410 | 410 | // generate list of all MeekroDB variables in our query, and their position |
@@ -435,7 +435,7 @@ discard block |
||
435 | 435 | // handle numbered parameters |
436 | 436 | if ($arg_number_length = strspn($sql, '0123456789', $new_pos_back)) { |
437 | 437 | $arg_number = substr($sql, $new_pos_back, $arg_number_length); |
438 | - if (! array_key_exists($arg_number, $args_all)) $this->nonSQLError("Non existent argument reference (arg $arg_number): $sql"); |
|
438 | + if (!array_key_exists($arg_number, $args_all)) $this->nonSQLError("Non existent argument reference (arg $arg_number): $sql"); |
|
439 | 439 | |
440 | 440 | $arg = $args_all[$arg_number]; |
441 | 441 | |
@@ -446,7 +446,7 @@ discard block |
||
446 | 446 | |
447 | 447 | $arg_number = substr($sql, $new_pos_back + $named_seperator_length, $arg_number_length - $named_seperator_length); |
448 | 448 | if (count($args_all) != 1 || !is_array($args_all[0])) $this->nonSQLError("If you use named parameters, the second argument must be an array of parameters"); |
449 | - if (! array_key_exists($arg_number, $args_all[0])) $this->nonSQLError("Non existent argument reference (arg $arg_number): $sql"); |
|
449 | + if (!array_key_exists($arg_number, $args_all[0])) $this->nonSQLError("Non existent argument reference (arg $arg_number): $sql"); |
|
450 | 450 | |
451 | 451 | $arg = $args_all[0][$arg_number]; |
452 | 452 | |
@@ -475,7 +475,7 @@ discard block |
||
475 | 475 | return $chunkyQuery; |
476 | 476 | } |
477 | 477 | |
478 | - protected function escape($str) { return "'" . $this->get()->real_escape_string(strval($str)) . "'"; } |
|
478 | + protected function escape($str) { return "'".$this->get()->real_escape_string(strval($str))."'"; } |
|
479 | 479 | |
480 | 480 | protected function sanitize($value) { |
481 | 481 | if (is_object($value)) { |
@@ -493,12 +493,12 @@ discard block |
||
493 | 493 | // non-assoc array? |
494 | 494 | if (array_values($value) === $value) { |
495 | 495 | if (is_array($value[0])) return implode(', ', array_map(array($this, 'sanitize'), $value)); |
496 | - else return '(' . implode(', ', array_map(array($this, 'sanitize'), $value)) . ')'; |
|
496 | + else return '('.implode(', ', array_map(array($this, 'sanitize'), $value)).')'; |
|
497 | 497 | } |
498 | 498 | |
499 | 499 | $pairs = array(); |
500 | 500 | foreach ($value as $k => $v) { |
501 | - $pairs[] = $this->formatTableName($k) . '=' . $this->sanitize($v); |
|
501 | + $pairs[] = $this->formatTableName($k).'='.$this->sanitize($v); |
|
502 | 502 | } |
503 | 503 | |
504 | 504 | return implode(', ', $pairs); |
@@ -544,7 +544,7 @@ discard block |
||
544 | 544 | else if ($type == 'd') $result = doubleval($arg); |
545 | 545 | else if ($type == 'b') $result = $this->formatTableName($arg); |
546 | 546 | else if ($type == 'l') $result = $arg; |
547 | - else if ($type == 'ss') $result = $this->escape("%" . str_replace(array('%', '_'), array('\%', '\_'), $arg) . "%"); |
|
547 | + else if ($type == 'ss') $result = $this->escape("%".str_replace(array('%', '_'), array('\%', '\_'), $arg)."%"); |
|
548 | 548 | else if ($type == 't') $result = $this->escape($this->parseTS($arg)); |
549 | 549 | |
550 | 550 | else if ($type == 'ls') $result = array_map(array($this, 'escape'), $arg); |
@@ -558,7 +558,7 @@ discard block |
||
558 | 558 | |
559 | 559 | else $this->nonSQLError("Badly formatted SQL query: Invalid MeekroDB param $type"); |
560 | 560 | |
561 | - if (is_array($result)) $result = '(' . implode(',', $result) . ')'; |
|
561 | + if (is_array($result)) $result = '('.implode(',', $result).')'; |
|
562 | 562 | |
563 | 563 | $query .= $result; |
564 | 564 | } |
@@ -666,7 +666,7 @@ discard block |
||
666 | 666 | if ($full_names) { |
667 | 667 | $infos = array(); |
668 | 668 | foreach ($result->fetch_fields() as $info) { |
669 | - if (strlen($info->table)) $infos[] = $info->table . '.' . $info->name; |
|
669 | + if (strlen($info->table)) $infos[] = $info->table.'.'.$info->name; |
|
670 | 670 | else $infos[] = $info->name; |
671 | 671 | } |
672 | 672 | } |
@@ -690,7 +690,7 @@ discard block |
||
690 | 690 | public function queryFirstRow() { |
691 | 691 | $args = func_get_args(); |
692 | 692 | $result = call_user_func_array(array($this, 'query'), $args); |
693 | - if (! $result) return null; |
|
693 | + if (!$result) return null; |
|
694 | 694 | return reset($result); |
695 | 695 | } |
696 | 696 | |
@@ -698,7 +698,7 @@ discard block |
||
698 | 698 | public function queryFirstList() { |
699 | 699 | $args = func_get_args(); |
700 | 700 | $result = call_user_func_array(array($this, 'queryAllLists'), $args); |
701 | - if (! $result) return null; |
|
701 | + if (!$result) return null; |
|
702 | 702 | return reset($result); |
703 | 703 | } |
704 | 704 | |
@@ -787,12 +787,12 @@ discard block |
||
787 | 787 | if ($this->clauses[$i] instanceof WhereClause) { |
788 | 788 | $this->clauses[$i]->negate(); |
789 | 789 | } else { |
790 | - $this->clauses[$i]['sql'] = 'NOT (' . $this->clauses[$i]['sql'] . ')'; |
|
790 | + $this->clauses[$i]['sql'] = 'NOT ('.$this->clauses[$i]['sql'].')'; |
|
791 | 791 | } |
792 | 792 | } |
793 | 793 | |
794 | 794 | function negate() { |
795 | - $this->negate = ! $this->negate; |
|
795 | + $this->negate = !$this->negate; |
|
796 | 796 | } |
797 | 797 | |
798 | 798 | function addClause($type) { |
@@ -826,7 +826,7 @@ discard block |
||
826 | 826 | if ($this->type == 'and') $sql = implode(' AND ', $sql); |
827 | 827 | else $sql = implode(' OR ', $sql); |
828 | 828 | |
829 | - if ($this->negate) $sql = '(NOT ' . $sql . ')'; |
|
829 | + if ($this->negate) $sql = '(NOT '.$sql.')'; |
|
830 | 830 | return array($sql, $args); |
831 | 831 | } |
832 | 832 | |
@@ -842,7 +842,7 @@ discard block |
||
842 | 842 | DB::startTransaction(); |
843 | 843 | } |
844 | 844 | function __destruct() { |
845 | - if (! $this->committed) DB::rollback(); |
|
845 | + if (!$this->committed) DB::rollback(); |
|
846 | 846 | } |
847 | 847 | function commit() { |
848 | 848 | DB::commit(); |
@@ -855,7 +855,7 @@ discard block |
||
855 | 855 | class MeekroDBException extends Exception { |
856 | 856 | protected $query = ''; |
857 | 857 | |
858 | - function __construct($message='', $query='', $code = 0) { |
|
858 | + function __construct($message = '', $query = '', $code = 0) { |
|
859 | 859 | parent::__construct($message); |
860 | 860 | $this->query = $query; |
861 | 861 | $this->code = $code; |
@@ -876,10 +876,10 @@ discard block |
||
876 | 876 | |
877 | 877 | $R = array(); |
878 | 878 | foreach ($array as $obj) { |
879 | - if (! array_key_exists($field, $obj)) die("verticalSlice: array doesn't have requested field\n"); |
|
879 | + if (!array_key_exists($field, $obj)) die("verticalSlice: array doesn't have requested field\n"); |
|
880 | 880 | |
881 | 881 | if ($keyfield) { |
882 | - if (! array_key_exists($keyfield, $obj)) die("verticalSlice: array doesn't have requested field\n"); |
|
882 | + if (!array_key_exists($keyfield, $obj)) die("verticalSlice: array doesn't have requested field\n"); |
|
883 | 883 | $R[$obj[$keyfield]] = $obj[$field]; |
884 | 884 | } else { |
885 | 885 | $R[] = $obj[$field]; |
@@ -900,13 +900,13 @@ discard block |
||
900 | 900 | |
901 | 901 | $R = array(); |
902 | 902 | foreach ($array as $obj) { |
903 | - $target =& $R; |
|
903 | + $target = & $R; |
|
904 | 904 | |
905 | 905 | foreach ($fields as $field) { |
906 | - if (! array_key_exists($field, $obj)) die("reIndex: array doesn't have requested field\n"); |
|
906 | + if (!array_key_exists($field, $obj)) die("reIndex: array doesn't have requested field\n"); |
|
907 | 907 | |
908 | 908 | $nextkey = $obj[$field]; |
909 | - $target =& $target[$nextkey]; |
|
909 | + $target = & $target[$nextkey]; |
|
910 | 910 | } |
911 | 911 | $target = $obj; |
912 | 912 | } |
@@ -921,7 +921,7 @@ discard block |
||
921 | 921 | } |
922 | 922 | |
923 | 923 | function meekrodb_debugmode_handler($params) { |
924 | - echo "QUERY: " . $params['query'] . " [" . $params['runtime'] . " ms]"; |
|
924 | + echo "QUERY: ".$params['query']." [".$params['runtime']." ms]"; |
|
925 | 925 | if (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) { |
926 | 926 | echo "\n"; |
927 | 927 | } else { |
@@ -47,15 +47,33 @@ discard block |
||
47 | 47 | $mdb = DB::$mdb = new MeekroDB(); |
48 | 48 | } |
49 | 49 | |
50 | - if ($mdb->param_char !== DB::$param_char) $mdb->param_char = DB::$param_char; |
|
51 | - if ($mdb->named_param_seperator !== DB::$named_param_seperator) $mdb->named_param_seperator = DB::$named_param_seperator; |
|
52 | - if ($mdb->success_handler !== DB::$success_handler) $mdb->success_handler = DB::$success_handler; |
|
53 | - if ($mdb->error_handler !== DB::$error_handler) $mdb->error_handler = DB::$error_handler; |
|
54 | - if ($mdb->throw_exception_on_error !== DB::$throw_exception_on_error) $mdb->throw_exception_on_error = DB::$throw_exception_on_error; |
|
55 | - if ($mdb->nonsql_error_handler !== DB::$nonsql_error_handler) $mdb->nonsql_error_handler = DB::$nonsql_error_handler; |
|
56 | - if ($mdb->throw_exception_on_nonsql_error !== DB::$throw_exception_on_nonsql_error) $mdb->throw_exception_on_nonsql_error = DB::$throw_exception_on_nonsql_error; |
|
57 | - if ($mdb->nested_transactions !== DB::$nested_transactions) $mdb->nested_transactions = DB::$nested_transactions; |
|
58 | - if ($mdb->usenull !== DB::$usenull) $mdb->usenull = DB::$usenull; |
|
50 | + if ($mdb->param_char !== DB::$param_char) { |
|
51 | + $mdb->param_char = DB::$param_char; |
|
52 | + } |
|
53 | + if ($mdb->named_param_seperator !== DB::$named_param_seperator) { |
|
54 | + $mdb->named_param_seperator = DB::$named_param_seperator; |
|
55 | + } |
|
56 | + if ($mdb->success_handler !== DB::$success_handler) { |
|
57 | + $mdb->success_handler = DB::$success_handler; |
|
58 | + } |
|
59 | + if ($mdb->error_handler !== DB::$error_handler) { |
|
60 | + $mdb->error_handler = DB::$error_handler; |
|
61 | + } |
|
62 | + if ($mdb->throw_exception_on_error !== DB::$throw_exception_on_error) { |
|
63 | + $mdb->throw_exception_on_error = DB::$throw_exception_on_error; |
|
64 | + } |
|
65 | + if ($mdb->nonsql_error_handler !== DB::$nonsql_error_handler) { |
|
66 | + $mdb->nonsql_error_handler = DB::$nonsql_error_handler; |
|
67 | + } |
|
68 | + if ($mdb->throw_exception_on_nonsql_error !== DB::$throw_exception_on_nonsql_error) { |
|
69 | + $mdb->throw_exception_on_nonsql_error = DB::$throw_exception_on_nonsql_error; |
|
70 | + } |
|
71 | + if ($mdb->nested_transactions !== DB::$nested_transactions) { |
|
72 | + $mdb->nested_transactions = DB::$nested_transactions; |
|
73 | + } |
|
74 | + if ($mdb->usenull !== DB::$usenull) { |
|
75 | + $mdb->usenull = DB::$usenull; |
|
76 | + } |
|
59 | 77 | |
60 | 78 | return $mdb; |
61 | 79 | } |
@@ -143,12 +161,24 @@ discard block |
||
143 | 161 | |
144 | 162 | |
145 | 163 | public function __construct($host=null, $user=null, $password=null, $dbName=null, $port=null, $encoding=null) { |
146 | - if ($host === null) $host = DB::$host; |
|
147 | - if ($user === null) $user = DB::$user; |
|
148 | - if ($password === null) $password = DB::$password; |
|
149 | - if ($dbName === null) $dbName = DB::$dbName; |
|
150 | - if ($port === null) $port = DB::$port; |
|
151 | - if ($encoding === null) $encoding = DB::$encoding; |
|
164 | + if ($host === null) { |
|
165 | + $host = DB::$host; |
|
166 | + } |
|
167 | + if ($user === null) { |
|
168 | + $user = DB::$user; |
|
169 | + } |
|
170 | + if ($password === null) { |
|
171 | + $password = DB::$password; |
|
172 | + } |
|
173 | + if ($dbName === null) { |
|
174 | + $dbName = DB::$dbName; |
|
175 | + } |
|
176 | + if ($port === null) { |
|
177 | + $port = DB::$port; |
|
178 | + } |
|
179 | + if ($encoding === null) { |
|
180 | + $encoding = DB::$encoding; |
|
181 | + } |
|
152 | 182 | |
153 | 183 | $this->host = $host; |
154 | 184 | $this->user = $user; |
@@ -162,7 +192,9 @@ discard block |
||
162 | 192 | $mysql = $this->internal_mysql; |
163 | 193 | |
164 | 194 | if (!($mysql instanceof MySQLi)) { |
165 | - if (! $this->port) $this->port = ini_get('mysqli.default_port'); |
|
195 | + if (! $this->port) { |
|
196 | + $this->port = ini_get('mysqli.default_port'); |
|
197 | + } |
|
166 | 198 | $this->current_db = $this->dbName; |
167 | 199 | |
168 | 200 | $mysql = new mysqli($this->host, $this->user, $this->password, $this->dbName, $this->port); |
@@ -182,7 +214,9 @@ discard block |
||
182 | 214 | public function disconnect() { |
183 | 215 | $mysqli = $this->internal_mysql; |
184 | 216 | if ($mysqli instanceof MySQLi) { |
185 | - if ($thread_id = $mysqli->thread_id) $mysqli->kill($thread_id); |
|
217 | + if ($thread_id = $mysqli->thread_id) { |
|
218 | + $mysqli->kill($thread_id); |
|
219 | + } |
|
186 | 220 | $mysqli->close(); |
187 | 221 | } |
188 | 222 | $this->internal_mysql = null; |
@@ -216,7 +250,9 @@ discard block |
||
216 | 250 | public function useDB() { $args = func_get_args(); return call_user_func_array(array($this, 'setDB'), $args); } |
217 | 251 | public function setDB($dbName) { |
218 | 252 | $db = $this->get(); |
219 | - if (! $db->select_db($dbName)) $this->nonSQLError("Unable to set database to $dbName"); |
|
253 | + if (! $db->select_db($dbName)) { |
|
254 | + $this->nonSQLError("Unable to set database to $dbName"); |
|
255 | + } |
|
220 | 256 | $this->current_db = $dbName; |
221 | 257 | } |
222 | 258 | |
@@ -242,8 +278,9 @@ discard block |
||
242 | 278 | return $this->nonSQLError("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL " . $this->serverVersion()); |
243 | 279 | } |
244 | 280 | |
245 | - if ($this->nested_transactions && $this->nested_transactions_count > 0) |
|
246 | - $this->nested_transactions_count--; |
|
281 | + if ($this->nested_transactions && $this->nested_transactions_count > 0) { |
|
282 | + $this->nested_transactions_count--; |
|
283 | + } |
|
247 | 284 | |
248 | 285 | if (!$this->nested_transactions || $all || $this->nested_transactions_count == 0) { |
249 | 286 | $this->nested_transactions_count = 0; |
@@ -260,8 +297,9 @@ discard block |
||
260 | 297 | return $this->nonSQLError("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL " . $this->serverVersion()); |
261 | 298 | } |
262 | 299 | |
263 | - if ($this->nested_transactions && $this->nested_transactions_count > 0) |
|
264 | - $this->nested_transactions_count--; |
|
300 | + if ($this->nested_transactions && $this->nested_transactions_count > 0) { |
|
301 | + $this->nested_transactions_count--; |
|
302 | + } |
|
265 | 303 | |
266 | 304 | if (!$this->nested_transactions || $all || $this->nested_transactions_count == 0) { |
267 | 305 | $this->nested_transactions_count = 0; |
@@ -276,8 +314,11 @@ discard block |
||
276 | 314 | protected function formatTableName($table) { |
277 | 315 | $table = trim($table, '`'); |
278 | 316 | |
279 | - if (strpos($table, '.')) return implode('.', array_map(array($this, 'formatTableName'), explode('.', $table))); |
|
280 | - else return '`' . str_replace('`', '``', $table) . '`'; |
|
317 | + if (strpos($table, '.')) { |
|
318 | + return implode('.', array_map(array($this, 'formatTableName'), explode('.', $table))); |
|
319 | + } else { |
|
320 | + return '`' . str_replace('`', '``', $table) . '`'; |
|
321 | + } |
|
281 | 322 | } |
282 | 323 | |
283 | 324 | public function update() { |
@@ -301,7 +342,9 @@ discard block |
||
301 | 342 | if (isset($datas[0]) && is_array($datas[0])) { |
302 | 343 | foreach ($datas as $datum) { |
303 | 344 | ksort($datum); |
304 | - if (! $keys) $keys = array_keys($datum); |
|
345 | + if (! $keys) { |
|
346 | + $keys = array_keys($datum); |
|
347 | + } |
|
305 | 348 | $values[] = array_values($datum); |
306 | 349 | } |
307 | 350 | |
@@ -310,7 +353,9 @@ discard block |
||
310 | 353 | $values = array_values($datas); |
311 | 354 | } |
312 | 355 | |
313 | - if (isset($options['ignore']) && $options['ignore']) $which = 'INSERT IGNORE'; |
|
356 | + if (isset($options['ignore']) && $options['ignore']) { |
|
357 | + $which = 'INSERT IGNORE'; |
|
358 | + } |
|
314 | 359 | |
315 | 360 | if (isset($options['update']) && is_array($options['update']) && $options['update'] && strtolower($which) == 'insert') { |
316 | 361 | if (array_values($options['update']) !== $options['update']) { |
@@ -344,8 +389,11 @@ discard block |
||
344 | 389 | $args[0] = $data; |
345 | 390 | } |
346 | 391 | |
347 | - if (is_array($args[0])) $update = $args[0]; |
|
348 | - else $update = $args; |
|
392 | + if (is_array($args[0])) { |
|
393 | + $update = $args[0]; |
|
394 | + } else { |
|
395 | + $update = $args; |
|
396 | + } |
|
349 | 397 | |
350 | 398 | return $this->insertOrReplace('INSERT', $table, $data, array('update' => $update)); |
351 | 399 | } |
@@ -376,7 +424,9 @@ discard block |
||
376 | 424 | } |
377 | 425 | |
378 | 426 | $result = $this->queryFirstColumn('SHOW TABLES'); |
379 | - if (isset($olddb)) $this->useDB($olddb); |
|
427 | + if (isset($olddb)) { |
|
428 | + $this->useDB($olddb); |
|
429 | + } |
|
380 | 430 | return $result; |
381 | 431 | } |
382 | 432 | |
@@ -385,7 +435,9 @@ discard block |
||
385 | 435 | $sql = trim(strval(array_shift($args))); |
386 | 436 | $args_all = $args; |
387 | 437 | |
388 | - if (count($args_all) == 0) return array($sql); |
|
438 | + if (count($args_all) == 0) { |
|
439 | + return array($sql); |
|
440 | + } |
|
389 | 441 | |
390 | 442 | $param_char_length = strlen($this->param_char); |
391 | 443 | $named_seperator_length = strlen($this->named_param_seperator); |
@@ -414,7 +466,9 @@ discard block |
||
414 | 466 | $lastPos = 0; |
415 | 467 | while (($pos = strpos($sql, $type, $lastPos)) !== false) { |
416 | 468 | $lastPos = $pos + 1; |
417 | - if (isset($posList[$pos]) && strlen($posList[$pos]) > strlen($type)) continue; |
|
469 | + if (isset($posList[$pos]) && strlen($posList[$pos]) > strlen($type)) { |
|
470 | + continue; |
|
471 | + } |
|
418 | 472 | $posList[$pos] = $type; |
419 | 473 | } |
420 | 474 | } |
@@ -435,7 +489,9 @@ discard block |
||
435 | 489 | // handle numbered parameters |
436 | 490 | if ($arg_number_length = strspn($sql, '0123456789', $new_pos_back)) { |
437 | 491 | $arg_number = substr($sql, $new_pos_back, $arg_number_length); |
438 | - if (! array_key_exists($arg_number, $args_all)) $this->nonSQLError("Non existent argument reference (arg $arg_number): $sql"); |
|
492 | + if (! array_key_exists($arg_number, $args_all)) { |
|
493 | + $this->nonSQLError("Non existent argument reference (arg $arg_number): $sql"); |
|
494 | + } |
|
439 | 495 | |
440 | 496 | $arg = $args_all[$arg_number]; |
441 | 497 | |
@@ -445,8 +501,12 @@ discard block |
||
445 | 501 | $new_pos_back + $named_seperator_length) + $named_seperator_length; |
446 | 502 | |
447 | 503 | $arg_number = substr($sql, $new_pos_back + $named_seperator_length, $arg_number_length - $named_seperator_length); |
448 | - if (count($args_all) != 1 || !is_array($args_all[0])) $this->nonSQLError("If you use named parameters, the second argument must be an array of parameters"); |
|
449 | - if (! array_key_exists($arg_number, $args_all[0])) $this->nonSQLError("Non existent argument reference (arg $arg_number): $sql"); |
|
504 | + if (count($args_all) != 1 || !is_array($args_all[0])) { |
|
505 | + $this->nonSQLError("If you use named parameters, the second argument must be an array of parameters"); |
|
506 | + } |
|
507 | + if (! array_key_exists($arg_number, $args_all[0])) { |
|
508 | + $this->nonSQLError("Non existent argument reference (arg $arg_number): $sql"); |
|
509 | + } |
|
450 | 510 | |
451 | 511 | $arg = $args_all[0][$arg_number]; |
452 | 512 | |
@@ -455,7 +515,9 @@ discard block |
||
455 | 515 | $arg = array_shift($args); |
456 | 516 | } |
457 | 517 | |
458 | - if ($new_pos > 0) $chunkyQuery[] = substr($sql, 0, $new_pos); |
|
518 | + if ($new_pos > 0) { |
|
519 | + $chunkyQuery[] = substr($sql, 0, $new_pos); |
|
520 | + } |
|
459 | 521 | |
460 | 522 | if (is_object($arg) && ($arg instanceof WhereClause)) { |
461 | 523 | list($clause_sql, $clause_args) = $arg->textAndArgs(); |
@@ -470,7 +532,9 @@ discard block |
||
470 | 532 | $pos_adj -= $new_pos_back + $arg_number_length; |
471 | 533 | } |
472 | 534 | |
473 | - if (strlen($sql) > 0) $chunkyQuery[] = $sql; |
|
535 | + if (strlen($sql) > 0) { |
|
536 | + $chunkyQuery[] = $sql; |
|
537 | + } |
|
474 | 538 | |
475 | 539 | return $chunkyQuery; |
476 | 540 | } |
@@ -479,21 +543,31 @@ discard block |
||
479 | 543 | |
480 | 544 | protected function sanitize($value) { |
481 | 545 | if (is_object($value)) { |
482 | - if ($value instanceof MeekroDBEval) return $value->text; |
|
483 | - else if ($value instanceof DateTime) return $this->escape($value->format('Y-m-d H:i:s')); |
|
484 | - else return ''; |
|
546 | + if ($value instanceof MeekroDBEval) { |
|
547 | + return $value->text; |
|
548 | + } else if ($value instanceof DateTime) { |
|
549 | + return $this->escape($value->format('Y-m-d H:i:s')); |
|
550 | + } else { |
|
551 | + return ''; |
|
552 | + } |
|
485 | 553 | } |
486 | 554 | |
487 | - if (is_null($value)) return $this->usenull ? 'NULL' : "''"; |
|
488 | - else if (is_bool($value)) return ($value ? 1 : 0); |
|
489 | - else if (is_int($value)) return $value; |
|
490 | - else if (is_float($value)) return $value; |
|
491 | - |
|
492 | - else if (is_array($value)) { |
|
555 | + if (is_null($value)) { |
|
556 | + return $this->usenull ? 'NULL' : "''"; |
|
557 | + } else if (is_bool($value)) { |
|
558 | + return ($value ? 1 : 0); |
|
559 | + } else if (is_int($value)) { |
|
560 | + return $value; |
|
561 | + } else if (is_float($value)) { |
|
562 | + return $value; |
|
563 | + } else if (is_array($value)) { |
|
493 | 564 | // non-assoc array? |
494 | 565 | if (array_values($value) === $value) { |
495 | - if (is_array($value[0])) return implode(', ', array_map(array($this, 'sanitize'), $value)); |
|
496 | - else return '(' . implode(', ', array_map(array($this, 'sanitize'), $value)) . ')'; |
|
566 | + if (is_array($value[0])) { |
|
567 | + return implode(', ', array_map(array($this, 'sanitize'), $value)); |
|
568 | + } else { |
|
569 | + return '(' . implode(', ', array_map(array($this, 'sanitize'), $value)) . ')'; |
|
570 | + } |
|
497 | 571 | } |
498 | 572 | |
499 | 573 | $pairs = array(); |
@@ -502,17 +576,23 @@ discard block |
||
502 | 576 | } |
503 | 577 | |
504 | 578 | return implode(', ', $pairs); |
579 | + } else { |
|
580 | + return $this->escape($value); |
|
505 | 581 | } |
506 | - else return $this->escape($value); |
|
507 | 582 | } |
508 | 583 | |
509 | 584 | protected function parseTS($ts) { |
510 | - if (is_string($ts)) return date('Y-m-d H:i:s', strtotime($ts)); |
|
511 | - else if (is_object($ts) && ($ts instanceof DateTime)) return $ts->format('Y-m-d H:i:s'); |
|
585 | + if (is_string($ts)) { |
|
586 | + return date('Y-m-d H:i:s', strtotime($ts)); |
|
587 | + } else if (is_object($ts) && ($ts instanceof DateTime)) { |
|
588 | + return $ts->format('Y-m-d H:i:s'); |
|
589 | + } |
|
512 | 590 | } |
513 | 591 | |
514 | 592 | protected function intval($var) { |
515 | - if (PHP_INT_SIZE == 8) return intval($var); |
|
593 | + if (PHP_INT_SIZE == 8) { |
|
594 | + return intval($var); |
|
595 | + } |
|
516 | 596 | return floor(doubleval($var)); |
517 | 597 | } |
518 | 598 | |
@@ -535,30 +615,48 @@ discard block |
||
535 | 615 | |
536 | 616 | if ($type != '?') { |
537 | 617 | $is_array_type = in_array($type, $array_types, true); |
538 | - if ($is_array_type && !is_array($arg)) $this->nonSQLError("Badly formatted SQL query: Expected array, got scalar instead!"); |
|
539 | - else if (!$is_array_type && is_array($arg)) $this->nonSQLError("Badly formatted SQL query: Expected scalar, got array instead!"); |
|
618 | + if ($is_array_type && !is_array($arg)) { |
|
619 | + $this->nonSQLError("Badly formatted SQL query: Expected array, got scalar instead!"); |
|
620 | + } else if (!$is_array_type && is_array($arg)) { |
|
621 | + $this->nonSQLError("Badly formatted SQL query: Expected scalar, got array instead!"); |
|
622 | + } |
|
540 | 623 | } |
541 | 624 | |
542 | - if ($type == 's') $result = $this->escape($arg); |
|
543 | - else if ($type == 'i') $result = $this->intval($arg); |
|
544 | - else if ($type == 'd') $result = doubleval($arg); |
|
545 | - else if ($type == 'b') $result = $this->formatTableName($arg); |
|
546 | - else if ($type == 'l') $result = $arg; |
|
547 | - else if ($type == 'ss') $result = $this->escape("%" . str_replace(array('%', '_'), array('\%', '\_'), $arg) . "%"); |
|
548 | - else if ($type == 't') $result = $this->escape($this->parseTS($arg)); |
|
549 | - |
|
550 | - else if ($type == 'ls') $result = array_map(array($this, 'escape'), $arg); |
|
551 | - else if ($type == 'li') $result = array_map(array($this, 'intval'), $arg); |
|
552 | - else if ($type == 'ld') $result = array_map('doubleval', $arg); |
|
553 | - else if ($type == 'lb') $result = array_map(array($this, 'formatTableName'), $arg); |
|
554 | - else if ($type == 'll') $result = $arg; |
|
555 | - else if ($type == 'lt') $result = array_map(array($this, 'escape'), array_map(array($this, 'parseTS'), $arg)); |
|
556 | - |
|
557 | - else if ($type == '?') $result = $this->sanitize($arg); |
|
558 | - |
|
559 | - else $this->nonSQLError("Badly formatted SQL query: Invalid MeekroDB param $type"); |
|
625 | + if ($type == 's') { |
|
626 | + $result = $this->escape($arg); |
|
627 | + } else if ($type == 'i') { |
|
628 | + $result = $this->intval($arg); |
|
629 | + } else if ($type == 'd') { |
|
630 | + $result = doubleval($arg); |
|
631 | + } else if ($type == 'b') { |
|
632 | + $result = $this->formatTableName($arg); |
|
633 | + } else if ($type == 'l') { |
|
634 | + $result = $arg; |
|
635 | + } else if ($type == 'ss') { |
|
636 | + $result = $this->escape("%" . str_replace(array('%', '_'), array('\%', '\_'), $arg) . "%"); |
|
637 | + } else if ($type == 't') { |
|
638 | + $result = $this->escape($this->parseTS($arg)); |
|
639 | + } else if ($type == 'ls') { |
|
640 | + $result = array_map(array($this, 'escape'), $arg); |
|
641 | + } else if ($type == 'li') { |
|
642 | + $result = array_map(array($this, 'intval'), $arg); |
|
643 | + } else if ($type == 'ld') { |
|
644 | + $result = array_map('doubleval', $arg); |
|
645 | + } else if ($type == 'lb') { |
|
646 | + $result = array_map(array($this, 'formatTableName'), $arg); |
|
647 | + } else if ($type == 'll') { |
|
648 | + $result = $arg; |
|
649 | + } else if ($type == 'lt') { |
|
650 | + $result = array_map(array($this, 'escape'), array_map(array($this, 'parseTS'), $arg)); |
|
651 | + } else if ($type == '?') { |
|
652 | + $result = $this->sanitize($arg); |
|
653 | + } else { |
|
654 | + $this->nonSQLError("Badly formatted SQL query: Invalid MeekroDB param $type"); |
|
655 | + } |
|
560 | 656 | |
561 | - if (is_array($result)) $result = '(' . implode(',', $result) . ')'; |
|
657 | + if (is_array($result)) { |
|
658 | + $result = '(' . implode(',', $result) . ')'; |
|
659 | + } |
|
562 | 660 | |
563 | 661 | $query .= $result; |
564 | 662 | } |
@@ -606,10 +704,15 @@ discard block |
||
606 | 704 | |
607 | 705 | $sql = call_user_func_array(array($this, 'parseQueryParams'), $args); |
608 | 706 | |
609 | - if ($this->success_handler) $starttime = microtime(true); |
|
707 | + if ($this->success_handler) { |
|
708 | + $starttime = microtime(true); |
|
709 | + } |
|
610 | 710 | $result = $db->query($sql, $is_buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT); |
611 | - if ($this->success_handler) $runtime = microtime(true) - $starttime; |
|
612 | - else $runtime = 0; |
|
711 | + if ($this->success_handler) { |
|
712 | + $runtime = microtime(true) - $starttime; |
|
713 | + } else { |
|
714 | + $runtime = 0; |
|
715 | + } |
|
613 | 716 | |
614 | 717 | // ----- BEGIN ERROR HANDLING |
615 | 718 | if (!$sql || $db->error) { |
@@ -656,23 +759,33 @@ discard block |
||
656 | 759 | $this->affected_rows = $db->affected_rows; |
657 | 760 | |
658 | 761 | // mysqli_result->num_rows won't initially show correct results for unbuffered data |
659 | - if ($is_buffered && ($result instanceof MySQLi_Result)) $this->num_rows = $result->num_rows; |
|
660 | - else $this->num_rows = null; |
|
762 | + if ($is_buffered && ($result instanceof MySQLi_Result)) { |
|
763 | + $this->num_rows = $result->num_rows; |
|
764 | + } else { |
|
765 | + $this->num_rows = null; |
|
766 | + } |
|
661 | 767 | |
662 | - if ($row_type == 'raw' || !($result instanceof MySQLi_Result)) return $result; |
|
768 | + if ($row_type == 'raw' || !($result instanceof MySQLi_Result)) { |
|
769 | + return $result; |
|
770 | + } |
|
663 | 771 | |
664 | 772 | $return = array(); |
665 | 773 | |
666 | 774 | if ($full_names) { |
667 | 775 | $infos = array(); |
668 | 776 | foreach ($result->fetch_fields() as $info) { |
669 | - if (strlen($info->table)) $infos[] = $info->table . '.' . $info->name; |
|
670 | - else $infos[] = $info->name; |
|
777 | + if (strlen($info->table)) { |
|
778 | + $infos[] = $info->table . '.' . $info->name; |
|
779 | + } else { |
|
780 | + $infos[] = $info->name; |
|
781 | + } |
|
671 | 782 | } |
672 | 783 | } |
673 | 784 | |
674 | 785 | while ($row = ($row_type == 'assoc' ? $result->fetch_assoc() : $result->fetch_row())) { |
675 | - if ($full_names) $row = array_combine($infos, $row); |
|
786 | + if ($full_names) { |
|
787 | + $row = array_combine($infos, $row); |
|
788 | + } |
|
676 | 789 | $return[] = $row; |
677 | 790 | } |
678 | 791 | |
@@ -680,7 +793,9 @@ discard block |
||
680 | 793 | $result->free(); |
681 | 794 | while ($db->more_results()) { |
682 | 795 | $db->next_result(); |
683 | - if ($result = $db->use_result()) $result->free(); |
|
796 | + if ($result = $db->use_result()) { |
|
797 | + $result->free(); |
|
798 | + } |
|
684 | 799 | } |
685 | 800 | |
686 | 801 | return $return; |
@@ -690,7 +805,9 @@ discard block |
||
690 | 805 | public function queryFirstRow() { |
691 | 806 | $args = func_get_args(); |
692 | 807 | $result = call_user_func_array(array($this, 'query'), $args); |
693 | - if (! $result) return null; |
|
808 | + if (! $result) { |
|
809 | + return null; |
|
810 | + } |
|
694 | 811 | return reset($result); |
695 | 812 | } |
696 | 813 | |
@@ -698,7 +815,9 @@ discard block |
||
698 | 815 | public function queryFirstList() { |
699 | 816 | $args = func_get_args(); |
700 | 817 | $result = call_user_func_array(array($this, 'queryAllLists'), $args); |
701 | - if (! $result) return null; |
|
818 | + if (! $result) { |
|
819 | + return null; |
|
820 | + } |
|
702 | 821 | return reset($result); |
703 | 822 | } |
704 | 823 | |
@@ -707,7 +826,9 @@ discard block |
||
707 | 826 | $results = call_user_func_array(array($this, 'queryAllLists'), $args); |
708 | 827 | $ret = array(); |
709 | 828 | |
710 | - if (!count($results) || !count($results[0])) return $ret; |
|
829 | + if (!count($results) || !count($results[0])) { |
|
830 | + return $ret; |
|
831 | + } |
|
711 | 832 | |
712 | 833 | foreach ($results as $row) { |
713 | 834 | $ret[] = $row[0]; |
@@ -722,7 +843,9 @@ discard block |
||
722 | 843 | $results = call_user_func_array(array($this, 'query'), $args); |
723 | 844 | $ret = array(); |
724 | 845 | |
725 | - if (!count($results) || !count($results[0])) return $ret; |
|
846 | + if (!count($results) || !count($results[0])) { |
|
847 | + return $ret; |
|
848 | + } |
|
726 | 849 | if ($column === null) { |
727 | 850 | $keys = array_keys($results[0]); |
728 | 851 | $column = $keys[0]; |
@@ -738,7 +861,9 @@ discard block |
||
738 | 861 | public function queryFirstField() { |
739 | 862 | $args = func_get_args(); |
740 | 863 | $row = call_user_func_array(array($this, 'queryFirstList'), $args); |
741 | - if ($row == null) return null; |
|
864 | + if ($row == null) { |
|
865 | + return null; |
|
866 | + } |
|
742 | 867 | return $row[0]; |
743 | 868 | } |
744 | 869 | |
@@ -765,7 +890,9 @@ discard block |
||
765 | 890 | |
766 | 891 | function __construct($type) { |
767 | 892 | $type = strtolower($type); |
768 | - if ($type !== 'or' && $type !== 'and') DB::nonSQLError('you must use either WhereClause(and) or WhereClause(or)'); |
|
893 | + if ($type !== 'or' && $type !== 'and') { |
|
894 | + DB::nonSQLError('you must use either WhereClause(and) or WhereClause(or)'); |
|
895 | + } |
|
769 | 896 | $this->type = $type; |
770 | 897 | } |
771 | 898 | |
@@ -782,7 +909,9 @@ discard block |
||
782 | 909 | |
783 | 910 | function negateLast() { |
784 | 911 | $i = count($this->clauses) - 1; |
785 | - if (!isset($this->clauses[$i])) return; |
|
912 | + if (!isset($this->clauses[$i])) { |
|
913 | + return; |
|
914 | + } |
|
786 | 915 | |
787 | 916 | if ($this->clauses[$i] instanceof WhereClause) { |
788 | 917 | $this->clauses[$i]->negate(); |
@@ -809,7 +938,9 @@ discard block |
||
809 | 938 | $sql = array(); |
810 | 939 | $args = array(); |
811 | 940 | |
812 | - if (count($this->clauses) == 0) return array('(1)', $args); |
|
941 | + if (count($this->clauses) == 0) { |
|
942 | + return array('(1)', $args); |
|
943 | + } |
|
813 | 944 | |
814 | 945 | foreach ($this->clauses as $clause) { |
815 | 946 | if ($clause instanceof WhereClause) { |
@@ -823,10 +954,15 @@ discard block |
||
823 | 954 | $args = array_merge($args, $clause_args); |
824 | 955 | } |
825 | 956 | |
826 | - if ($this->type == 'and') $sql = implode(' AND ', $sql); |
|
827 | - else $sql = implode(' OR ', $sql); |
|
957 | + if ($this->type == 'and') { |
|
958 | + $sql = implode(' AND ', $sql); |
|
959 | + } else { |
|
960 | + $sql = implode(' OR ', $sql); |
|
961 | + } |
|
828 | 962 | |
829 | - if ($this->negate) $sql = '(NOT ' . $sql . ')'; |
|
963 | + if ($this->negate) { |
|
964 | + $sql = '(NOT ' . $sql . ')'; |
|
965 | + } |
|
830 | 966 | return array($sql, $args); |
831 | 967 | } |
832 | 968 | |
@@ -842,7 +978,9 @@ discard block |
||
842 | 978 | DB::startTransaction(); |
843 | 979 | } |
844 | 980 | function __destruct() { |
845 | - if (! $this->committed) DB::rollback(); |
|
981 | + if (! $this->committed) { |
|
982 | + DB::rollback(); |
|
983 | + } |
|
846 | 984 | } |
847 | 985 | function commit() { |
848 | 986 | DB::commit(); |
@@ -876,10 +1014,14 @@ discard block |
||
876 | 1014 | |
877 | 1015 | $R = array(); |
878 | 1016 | foreach ($array as $obj) { |
879 | - if (! array_key_exists($field, $obj)) die("verticalSlice: array doesn't have requested field\n"); |
|
1017 | + if (! array_key_exists($field, $obj)) { |
|
1018 | + die("verticalSlice: array doesn't have requested field\n"); |
|
1019 | + } |
|
880 | 1020 | |
881 | 1021 | if ($keyfield) { |
882 | - if (! array_key_exists($keyfield, $obj)) die("verticalSlice: array doesn't have requested field\n"); |
|
1022 | + if (! array_key_exists($keyfield, $obj)) { |
|
1023 | + die("verticalSlice: array doesn't have requested field\n"); |
|
1024 | + } |
|
883 | 1025 | $R[$obj[$keyfield]] = $obj[$field]; |
884 | 1026 | } else { |
885 | 1027 | $R[] = $obj[$field]; |
@@ -903,7 +1045,9 @@ discard block |
||
903 | 1045 | $target =& $R; |
904 | 1046 | |
905 | 1047 | foreach ($fields as $field) { |
906 | - if (! array_key_exists($field, $obj)) die("reIndex: array doesn't have requested field\n"); |
|
1048 | + if (! array_key_exists($field, $obj)) { |
|
1049 | + die("reIndex: array doesn't have requested field\n"); |
|
1050 | + } |
|
907 | 1051 | |
908 | 1052 | $nextkey = $obj[$field]; |
909 | 1053 | $target =& $target[$nextkey]; |
@@ -200,7 +200,7 @@ |
||
200 | 200 | * Connect to a POP3 server. |
201 | 201 | * @access public |
202 | 202 | * @param string $host |
203 | - * @param integer|boolean $port |
|
203 | + * @param integer $port |
|
204 | 204 | * @param integer $tval |
205 | 205 | * @return boolean |
206 | 206 | */ |
@@ -169,13 +169,13 @@ discard block |
||
169 | 169 | if (false === $port) { |
170 | 170 | $this->port = $this->POP3_PORT; |
171 | 171 | } else { |
172 | - $this->port = (integer)$port; |
|
172 | + $this->port = (integer) $port; |
|
173 | 173 | } |
174 | 174 | // If no timeout value provided, use default |
175 | 175 | if (false === $timeout) { |
176 | 176 | $this->tval = $this->POP3_TIMEOUT; |
177 | 177 | } else { |
178 | - $this->tval = (integer)$timeout; |
|
178 | + $this->tval = (integer) $timeout; |
|
179 | 179 | } |
180 | 180 | $this->do_debug = $debug_level; |
181 | 181 | $this->username = $username; |
@@ -276,11 +276,11 @@ discard block |
||
276 | 276 | } |
277 | 277 | |
278 | 278 | // Send the Username |
279 | - $this->sendString("USER $username" . self::CRLF); |
|
279 | + $this->sendString("USER $username".self::CRLF); |
|
280 | 280 | $pop3_response = $this->getResponse(); |
281 | 281 | if ($this->checkResponse($pop3_response)) { |
282 | 282 | // Send the Password |
283 | - $this->sendString("PASS $password" . self::CRLF); |
|
283 | + $this->sendString("PASS $password".self::CRLF); |
|
284 | 284 | $pop3_response = $this->getResponse(); |
285 | 285 | if ($this->checkResponse($pop3_response)) { |
286 | 286 | return true; |
@@ -49,6 +49,10 @@ discard block |
||
49 | 49 | return $output; |
50 | 50 | } |
51 | 51 | |
52 | + /** |
|
53 | + * @param integer $rnd |
|
54 | + * @param integer $Nb |
|
55 | + */ |
|
52 | 56 | private static function addRoundKey($state, $w, $rnd, $Nb) |
53 | 57 | { |
54 | 58 | // xor Round Key into state S [é5.1.4] |
@@ -61,6 +65,9 @@ discard block |
||
61 | 65 | return $state; |
62 | 66 | } |
63 | 67 | |
68 | + /** |
|
69 | + * @param integer $Nb |
|
70 | + */ |
|
64 | 71 | private static function subBytes($s, $Nb) |
65 | 72 | { |
66 | 73 | // apply SBox to state S [é5.1.1] |
@@ -73,6 +80,9 @@ discard block |
||
73 | 80 | return $s; |
74 | 81 | } |
75 | 82 | |
83 | + /** |
|
84 | + * @param integer $Nb |
|
85 | + */ |
|
76 | 86 | private static function shiftRows($s, $Nb) |
77 | 87 | { |
78 | 88 | // shift row r of state S left by r bytes [é5.1.2] |
@@ -88,6 +98,9 @@ discard block |
||
88 | 98 | return $s; // see fp.gladman.plus.com/cryptography_technology/rijndael/aes.spec.311.pdf |
89 | 99 | } |
90 | 100 | |
101 | + /** |
|
102 | + * @param integer $Nb |
|
103 | + */ |
|
91 | 104 | private static function mixColumns($s, $Nb) |
92 | 105 | { |
93 | 106 | // combine bytes of each col of state S [é5.1.3] |
@@ -219,10 +232,10 @@ discard block |
||
219 | 232 | * |
220 | 233 | * Unicode multi-byte character safe |
221 | 234 | * |
222 | - * @param plaintext source text to be encrypted |
|
235 | + * @param plaintext string text to be encrypted |
|
223 | 236 | * @param password the password to use to generate a key |
224 | - * @param nBits number of bits to be used in the key (128, 192, or 256) |
|
225 | - * @return encrypted text |
|
237 | + * @param nBits integer of bits to be used in the key (128, 192, or 256) |
|
238 | + * @return string text |
|
226 | 239 | */ |
227 | 240 | public static function encrypt($plaintext, $password, $nBits) |
228 | 241 | { |
@@ -302,8 +315,8 @@ discard block |
||
302 | 315 | * |
303 | 316 | * @param ciphertext source text to be decrypted |
304 | 317 | * @param password the password to use to generate a key |
305 | - * @param nBits number of bits to be used in the key (128, 192, or 256) |
|
306 | - * @return decrypted text |
|
318 | + * @param nBits integer of bits to be used in the key (128, 192, or 256) |
|
319 | + * @return string text |
|
307 | 320 | */ |
308 | 321 | public static function decrypt($ciphertext, $password, $nBits) |
309 | 322 | { |
@@ -214,16 +214,16 @@ discard block |
||
214 | 214 | { |
215 | 215 | |
216 | 216 | /** |
217 | - * Encrypt a text using AES encryption in Counter mode of operation |
|
218 | - * - see http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf |
|
219 | - * |
|
220 | - * Unicode multi-byte character safe |
|
221 | - * |
|
222 | - * @param plaintext source text to be encrypted |
|
223 | - * @param password the password to use to generate a key |
|
224 | - * @param nBits number of bits to be used in the key (128, 192, or 256) |
|
225 | - * @return encrypted text |
|
226 | - */ |
|
217 | + * Encrypt a text using AES encryption in Counter mode of operation |
|
218 | + * - see http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf |
|
219 | + * |
|
220 | + * Unicode multi-byte character safe |
|
221 | + * |
|
222 | + * @param plaintext source text to be encrypted |
|
223 | + * @param password the password to use to generate a key |
|
224 | + * @param nBits number of bits to be used in the key (128, 192, or 256) |
|
225 | + * @return encrypted text |
|
226 | + */ |
|
227 | 227 | public static function encrypt($plaintext, $password, $nBits) |
228 | 228 | { |
229 | 229 | $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
@@ -298,13 +298,13 @@ discard block |
||
298 | 298 | } |
299 | 299 | |
300 | 300 | /** |
301 | - * Decrypt a text encrypted by AES in counter mode of operation |
|
302 | - * |
|
303 | - * @param ciphertext source text to be decrypted |
|
304 | - * @param password the password to use to generate a key |
|
305 | - * @param nBits number of bits to be used in the key (128, 192, or 256) |
|
306 | - * @return decrypted text |
|
307 | - */ |
|
301 | + * Decrypt a text encrypted by AES in counter mode of operation |
|
302 | + * |
|
303 | + * @param ciphertext source text to be decrypted |
|
304 | + * @param password the password to use to generate a key |
|
305 | + * @param nBits number of bits to be used in the key (128, 192, or 256) |
|
306 | + * @return decrypted text |
|
307 | + */ |
|
308 | 308 | public static function decrypt($ciphertext, $password, $nBits) |
309 | 309 | { |
310 | 310 | $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
@@ -20,17 +20,17 @@ discard block |
||
20 | 20 | public static function cipher($input, $w) |
21 | 21 | { |
22 | 22 | // main cipher function [é5.1] |
23 | - $Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES) |
|
24 | - $Nr = count($w)/$Nb - 1; // no of rounds: 10/12/14 for 128/192/256-bit keys |
|
23 | + $Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES) |
|
24 | + $Nr = count($w) / $Nb - 1; // no of rounds: 10/12/14 for 128/192/256-bit keys |
|
25 | 25 | |
26 | - $state = array(); // initialise 4xNb byte-array 'state' with input [é3.4] |
|
27 | - for ($i=0; $i<4*$Nb; $i++) { |
|
28 | - $state[$i%4][floor($i/4)] = $input[$i]; |
|
26 | + $state = array(); // initialise 4xNb byte-array 'state' with input [é3.4] |
|
27 | + for ($i = 0; $i < 4 * $Nb; $i++) { |
|
28 | + $state[$i % 4][floor($i / 4)] = $input[$i]; |
|
29 | 29 | } |
30 | 30 | |
31 | 31 | $state = self::addRoundKey($state, $w, 0, $Nb); |
32 | 32 | |
33 | - for ($round=1; $round<$Nr; $round++) { // apply Nr rounds |
|
33 | + for ($round = 1; $round < $Nr; $round++) { // apply Nr rounds |
|
34 | 34 | $state = self::subBytes($state, $Nb); |
35 | 35 | $state = self::shiftRows($state, $Nb); |
36 | 36 | $state = self::mixColumns($state, $Nb); |
@@ -41,9 +41,9 @@ discard block |
||
41 | 41 | $state = self::shiftRows($state, $Nb); |
42 | 42 | $state = self::addRoundKey($state, $w, $Nr, $Nb); |
43 | 43 | |
44 | - $output = array(4*$Nb); // convert state to 1-d array before returning [é3.4] |
|
45 | - for ($i=0; $i<4*$Nb; $i++) { |
|
46 | - $output[$i] = $state[$i%4][floor($i/4)]; |
|
44 | + $output = array(4 * $Nb); // convert state to 1-d array before returning [é3.4] |
|
45 | + for ($i = 0; $i < 4 * $Nb; $i++) { |
|
46 | + $output[$i] = $state[$i % 4][floor($i / 4)]; |
|
47 | 47 | } |
48 | 48 | |
49 | 49 | return $output; |
@@ -52,9 +52,9 @@ discard block |
||
52 | 52 | private static function addRoundKey($state, $w, $rnd, $Nb) |
53 | 53 | { |
54 | 54 | // xor Round Key into state S [é5.1.4] |
55 | - for ($r=0; $r<4; $r++) { |
|
56 | - for ($c=0; $c<$Nb; $c++) { |
|
57 | - $state[$r][$c] ^= $w[$rnd*4+$c][$r]; |
|
55 | + for ($r = 0; $r < 4; $r++) { |
|
56 | + for ($c = 0; $c < $Nb; $c++) { |
|
57 | + $state[$r][$c] ^= $w[$rnd * 4 + $c][$r]; |
|
58 | 58 | } |
59 | 59 | } |
60 | 60 | |
@@ -64,8 +64,8 @@ discard block |
||
64 | 64 | private static function subBytes($s, $Nb) |
65 | 65 | { |
66 | 66 | // apply SBox to state S [é5.1.1] |
67 | - for ($r=0; $r<4; $r++) { |
|
68 | - for ($c=0; $c<$Nb; $c++) { |
|
67 | + for ($r = 0; $r < 4; $r++) { |
|
68 | + for ($c = 0; $c < $Nb; $c++) { |
|
69 | 69 | $s[$r][$c] = self::$sBox[$s[$r][$c]]; |
70 | 70 | } |
71 | 71 | } |
@@ -77,26 +77,26 @@ discard block |
||
77 | 77 | { |
78 | 78 | // shift row r of state S left by r bytes [é5.1.2] |
79 | 79 | $t = array(4); |
80 | - for ($r=1; $r<4; $r++) { |
|
81 | - for ($c=0; $c<4; $c++) { |
|
82 | - $t[$c] = $s[$r][($c+$r)%$Nb]; // shift into temp copy |
|
80 | + for ($r = 1; $r < 4; $r++) { |
|
81 | + for ($c = 0; $c < 4; $c++) { |
|
82 | + $t[$c] = $s[$r][($c + $r) % $Nb]; // shift into temp copy |
|
83 | 83 | } |
84 | - for ($c=0; $c<4; $c++) { |
|
85 | - $s[$r][$c] = $t[$c]; // and copy back |
|
84 | + for ($c = 0; $c < 4; $c++) { |
|
85 | + $s[$r][$c] = $t[$c]; // and copy back |
|
86 | 86 | } |
87 | 87 | } // note that this will work for Nb=4,5,6, but not 7,8 (always 4 for AES): |
88 | - return $s; // see fp.gladman.plus.com/cryptography_technology/rijndael/aes.spec.311.pdf |
|
88 | + return $s; // see fp.gladman.plus.com/cryptography_technology/rijndael/aes.spec.311.pdf |
|
89 | 89 | } |
90 | 90 | |
91 | 91 | private static function mixColumns($s, $Nb) |
92 | 92 | { |
93 | 93 | // combine bytes of each col of state S [é5.1.3] |
94 | - for ($c=0; $c<4; $c++) { |
|
95 | - $a = array(4); // 'a' is a copy of the current column from 's' |
|
96 | - $b = array(4); // 'b' is aé{02} in GF(2^8) |
|
97 | - for ($i=0; $i<4; $i++) { |
|
94 | + for ($c = 0; $c < 4; $c++) { |
|
95 | + $a = array(4); // 'a' is a copy of the current column from 's' |
|
96 | + $b = array(4); // 'b' is aé{02} in GF(2^8) |
|
97 | + for ($i = 0; $i < 4; $i++) { |
|
98 | 98 | $a[$i] = $s[$i][$c]; |
99 | - $b[$i] = $s[$i][$c]&0x80 ? $s[$i][$c]<<1 ^ 0x011b : $s[$i][$c]<<1; |
|
99 | + $b[$i] = $s[$i][$c] & 0x80 ? $s[$i][$c] << 1 ^ 0x011b : $s[$i][$c] << 1; |
|
100 | 100 | } |
101 | 101 | // a[n] ^ b[n] is aé{03} in GF(2^8) |
102 | 102 | $s[0][$c] = $b[0] ^ $a[1] ^ $b[1] ^ $a[2] ^ $a[3]; // 2*a0 + 3*a1 + a2 + a3 |
@@ -118,33 +118,33 @@ discard block |
||
118 | 118 | public static function keyExpansion($key) |
119 | 119 | { |
120 | 120 | // generate Key Schedule from Cipher Key [é5.2] |
121 | - $Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES) |
|
122 | - $Nk = count($key)/4; // key length (in words): 4/6/8 for 128/192/256-bit keys |
|
123 | - $Nr = $Nk + 6; // no of rounds: 10/12/14 for 128/192/256-bit keys |
|
121 | + $Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES) |
|
122 | + $Nk = count($key) / 4; // key length (in words): 4/6/8 for 128/192/256-bit keys |
|
123 | + $Nr = $Nk + 6; // no of rounds: 10/12/14 for 128/192/256-bit keys |
|
124 | 124 | |
125 | 125 | $w = array(); |
126 | 126 | $temp = array(); |
127 | 127 | |
128 | - for ($i=0; $i<$Nk; $i++) { |
|
129 | - $r = array($key[4*$i], $key[4*$i+1], $key[4*$i+2], $key[4*$i+3]); |
|
128 | + for ($i = 0; $i < $Nk; $i++) { |
|
129 | + $r = array($key[4 * $i], $key[4 * $i + 1], $key[4 * $i + 2], $key[4 * $i + 3]); |
|
130 | 130 | $w[$i] = $r; |
131 | 131 | } |
132 | 132 | |
133 | - for ($i=$Nk; $i<($Nb*($Nr+1)); $i++) { |
|
133 | + for ($i = $Nk; $i < ($Nb * ($Nr + 1)); $i++) { |
|
134 | 134 | $w[$i] = array(); |
135 | - for ($t=0; $t<4; $t++) { |
|
136 | - $temp[$t] = $w[$i-1][$t]; |
|
135 | + for ($t = 0; $t < 4; $t++) { |
|
136 | + $temp[$t] = $w[$i - 1][$t]; |
|
137 | 137 | } |
138 | 138 | if ($i % $Nk == 0) { |
139 | 139 | $temp = self::subWord(self::rotWord($temp)); |
140 | - for ($t=0; $t<4; $t++) { |
|
141 | - $temp[$t] ^= self::$rCon[$i/$Nk][$t]; |
|
140 | + for ($t = 0; $t < 4; $t++) { |
|
141 | + $temp[$t] ^= self::$rCon[$i / $Nk][$t]; |
|
142 | 142 | } |
143 | - } elseif ($Nk > 6 && $i%$Nk == 4) { |
|
143 | + } elseif ($Nk > 6 && $i % $Nk == 4) { |
|
144 | 144 | $temp = self::subWord($temp); |
145 | 145 | } |
146 | - for ($t=0; $t<4; $t++) { |
|
147 | - $w[$i][$t] = $w[$i-$Nk][$t] ^ $temp[$t]; |
|
146 | + for ($t = 0; $t < 4; $t++) { |
|
147 | + $w[$i][$t] = $w[$i - $Nk][$t] ^ $temp[$t]; |
|
148 | 148 | } |
149 | 149 | } |
150 | 150 | |
@@ -154,7 +154,7 @@ discard block |
||
154 | 154 | private static function subWord($w) |
155 | 155 | { |
156 | 156 | // apply SBox to 4-byte word w |
157 | - for ($i=0; $i<4; $i++) { |
|
157 | + for ($i = 0; $i < 4; $i++) { |
|
158 | 158 | $w[$i] = self::$sBox[$w[$i]]; |
159 | 159 | } |
160 | 160 | |
@@ -165,8 +165,8 @@ discard block |
||
165 | 165 | { |
166 | 166 | // rotate 4-byte word w left by one byte |
167 | 167 | $tmp = $w[0]; |
168 | - for ($i=0; $i<3; $i++) { |
|
169 | - $w[$i] = $w[$i+1]; |
|
168 | + for ($i = 0; $i < 3; $i++) { |
|
169 | + $w[$i] = $w[$i + 1]; |
|
170 | 170 | } |
171 | 171 | $w[3] = $tmp; |
172 | 172 | |
@@ -175,22 +175,22 @@ discard block |
||
175 | 175 | |
176 | 176 | // sBox is pre-computed multiplicative inverse in GF(2^8) used in subBytes and keyExpansion [é5.1.1] |
177 | 177 | private static $sBox = array( |
178 | - 0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76, |
|
179 | - 0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0, |
|
180 | - 0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15, |
|
181 | - 0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75, |
|
182 | - 0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84, |
|
183 | - 0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf, |
|
184 | - 0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8, |
|
185 | - 0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff,0xf3,0xd2, |
|
186 | - 0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73, |
|
187 | - 0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb, |
|
188 | - 0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79, |
|
189 | - 0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08, |
|
190 | - 0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a, |
|
191 | - 0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e, |
|
192 | - 0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf, |
|
193 | - 0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16 |
|
178 | + 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, |
|
179 | + 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, |
|
180 | + 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, |
|
181 | + 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, |
|
182 | + 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, |
|
183 | + 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, |
|
184 | + 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, |
|
185 | + 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, |
|
186 | + 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, |
|
187 | + 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, |
|
188 | + 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, |
|
189 | + 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, |
|
190 | + 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, |
|
191 | + 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, |
|
192 | + 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, |
|
193 | + 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 |
|
194 | 194 | ); |
195 | 195 | |
196 | 196 | // rCon is Round Constant used for the Key Expansion [1st col is 2^(r-1) in GF(2^8)] [é5.2] |
@@ -226,72 +226,72 @@ discard block |
||
226 | 226 | */ |
227 | 227 | public static function encrypt($plaintext, $password, $nBits) |
228 | 228 | { |
229 | - $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
|
230 | - if (!($nBits==128 || $nBits==192 || $nBits==256)) { |
|
231 | - return ''; // standard allows 128/192/256 bit keys |
|
229 | + $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
|
230 | + if (!($nBits == 128 || $nBits == 192 || $nBits == 256)) { |
|
231 | + return ''; // standard allows 128/192/256 bit keys |
|
232 | 232 | } |
233 | 233 | // note PHP (5) gives us plaintext and password in UTF8 encoding! |
234 | 234 | |
235 | 235 | // use AES itself to encrypt password to get cipher key (using plain password as source for |
236 | 236 | // key expansion) - gives us well encrypted key |
237 | - $nBytes = $nBits/8; // no bytes in key |
|
237 | + $nBytes = $nBits / 8; // no bytes in key |
|
238 | 238 | $pwBytes = array(); |
239 | - for ($i=0; $i<$nBytes; $i++) { |
|
239 | + for ($i = 0; $i < $nBytes; $i++) { |
|
240 | 240 | $pwBytes[$i] = ord(substr($password, $i, 1)) & 0xff; |
241 | 241 | } |
242 | 242 | $key = Aes::cipher($pwBytes, Aes::keyExpansion($pwBytes)); |
243 | - $key = array_merge($key, array_slice($key, 0, $nBytes-16)); // expand key to 16/24/32 bytes long |
|
243 | + $key = array_merge($key, array_slice($key, 0, $nBytes - 16)); // expand key to 16/24/32 bytes long |
|
244 | 244 | |
245 | 245 | // initialise counter block (NIST SP800-38A §B.2): millisecond time-stamp for nonce in |
246 | 246 | // 1st 8 bytes, block counter in 2nd 8 bytes |
247 | 247 | $counterBlock = array(); |
248 | - $nonce = floor(microtime(true)*1000); // timestamp: milliseconds since 1-Jan-1970 |
|
249 | - $nonceSec = floor($nonce/1000); |
|
250 | - $nonceMs = $nonce%1000; |
|
248 | + $nonce = floor(microtime(true) * 1000); // timestamp: milliseconds since 1-Jan-1970 |
|
249 | + $nonceSec = floor($nonce / 1000); |
|
250 | + $nonceMs = $nonce % 1000; |
|
251 | 251 | // encode nonce with seconds in 1st 4 bytes, and (repeated) ms part filling 2nd 4 bytes |
252 | - for ($i=0; $i<4; $i++) { |
|
253 | - $counterBlock[$i] = self::urs($nonceSec, $i*8) & 0xff; |
|
252 | + for ($i = 0; $i < 4; $i++) { |
|
253 | + $counterBlock[$i] = self::urs($nonceSec, $i * 8) & 0xff; |
|
254 | 254 | } |
255 | - for ($i=0; $i<4; $i++) { |
|
256 | - $counterBlock[$i+4] = $nonceMs & 0xff; |
|
255 | + for ($i = 0; $i < 4; $i++) { |
|
256 | + $counterBlock[$i + 4] = $nonceMs & 0xff; |
|
257 | 257 | } |
258 | 258 | // and convert it to a string to go on the front of the ciphertext |
259 | 259 | $ctrTxt = ''; |
260 | - for ($i=0; $i<8; $i++) { |
|
260 | + for ($i = 0; $i < 8; $i++) { |
|
261 | 261 | $ctrTxt .= chr($counterBlock[$i]); |
262 | 262 | } |
263 | 263 | |
264 | 264 | // generate key schedule - an expansion of the key into distinct Key Rounds for each round |
265 | 265 | $keySchedule = Aes::keyExpansion($key); |
266 | 266 | |
267 | - $blockCount = ceil(strlen($plaintext)/$blockSize); |
|
268 | - $ciphertxt = array(); // ciphertext as array of strings |
|
267 | + $blockCount = ceil(strlen($plaintext) / $blockSize); |
|
268 | + $ciphertxt = array(); // ciphertext as array of strings |
|
269 | 269 | |
270 | - for ($b=0; $b<$blockCount; $b++) { |
|
270 | + for ($b = 0; $b < $blockCount; $b++) { |
|
271 | 271 | // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes) |
272 | 272 | // done in two stages for 32-bit ops: using two words allows us to go past 2^32 blocks (68GB) |
273 | - for ($c=0; $c<4; $c++) { |
|
274 | - $counterBlock[15-$c] = self::urs($b, $c*8) & 0xff; |
|
273 | + for ($c = 0; $c < 4; $c++) { |
|
274 | + $counterBlock[15 - $c] = self::urs($b, $c * 8) & 0xff; |
|
275 | 275 | } |
276 | - for ($c=0; $c<4; $c++) { |
|
277 | - $counterBlock[15-$c-4] = self::urs($b/0x100000000, $c*8); |
|
276 | + for ($c = 0; $c < 4; $c++) { |
|
277 | + $counterBlock[15 - $c - 4] = self::urs($b / 0x100000000, $c * 8); |
|
278 | 278 | } |
279 | 279 | |
280 | - $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // -- encrypt counter block -- |
|
280 | + $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // -- encrypt counter block -- |
|
281 | 281 | |
282 | 282 | // block size is reduced on final block |
283 | - $blockLength = $b<$blockCount-1 ? $blockSize : (strlen($plaintext)-1)%$blockSize+1; |
|
283 | + $blockLength = $b < $blockCount - 1 ? $blockSize : (strlen($plaintext) - 1) % $blockSize + 1; |
|
284 | 284 | $cipherByte = array(); |
285 | 285 | |
286 | - for ($i=0; $i<$blockLength; $i++) { // -- xor plaintext with ciphered counter byte-by-byte -- |
|
287 | - $cipherByte[$i] = $cipherCntr[$i] ^ ord(substr($plaintext, $b*$blockSize+$i, 1)); |
|
286 | + for ($i = 0; $i < $blockLength; $i++) { // -- xor plaintext with ciphered counter byte-by-byte -- |
|
287 | + $cipherByte[$i] = $cipherCntr[$i] ^ ord(substr($plaintext, $b * $blockSize + $i, 1)); |
|
288 | 288 | $cipherByte[$i] = chr($cipherByte[$i]); |
289 | 289 | } |
290 | - $ciphertxt[$b] = implode('', $cipherByte); // escape troublesome characters in ciphertext |
|
290 | + $ciphertxt[$b] = implode('', $cipherByte); // escape troublesome characters in ciphertext |
|
291 | 291 | } |
292 | 292 | |
293 | 293 | // implode is more efficient than repeated string concatenation |
294 | - $ciphertext = $ctrTxt . implode('', $ciphertxt); |
|
294 | + $ciphertext = $ctrTxt.implode('', $ciphertxt); |
|
295 | 295 | $ciphertext = base64_encode($ciphertext); |
296 | 296 | |
297 | 297 | return $ciphertext; |
@@ -307,25 +307,25 @@ discard block |
||
307 | 307 | */ |
308 | 308 | public static function decrypt($ciphertext, $password, $nBits) |
309 | 309 | { |
310 | - $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
|
311 | - if (!($nBits==128 || $nBits==192 || $nBits==256)) { |
|
312 | - return ''; // standard allows 128/192/256 bit keys |
|
310 | + $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
|
311 | + if (!($nBits == 128 || $nBits == 192 || $nBits == 256)) { |
|
312 | + return ''; // standard allows 128/192/256 bit keys |
|
313 | 313 | } |
314 | 314 | $ciphertext = base64_decode($ciphertext); |
315 | 315 | |
316 | 316 | // use AES to encrypt password (mirroring encrypt routine) |
317 | - $nBytes = $nBits/8; // no bytes in key |
|
317 | + $nBytes = $nBits / 8; // no bytes in key |
|
318 | 318 | $pwBytes = array(); |
319 | - for ($i=0; $i<$nBytes; $i++) { |
|
319 | + for ($i = 0; $i < $nBytes; $i++) { |
|
320 | 320 | $pwBytes[$i] = ord(substr($password, $i, 1)) & 0xff; |
321 | 321 | } |
322 | 322 | $key = Aes::cipher($pwBytes, Aes::keyExpansion($pwBytes)); |
323 | - $key = array_merge($key, array_slice($key, 0, $nBytes-16)); // expand key to 16/24/32 bytes long |
|
323 | + $key = array_merge($key, array_slice($key, 0, $nBytes - 16)); // expand key to 16/24/32 bytes long |
|
324 | 324 | |
325 | 325 | // recover nonce from 1st element of ciphertext |
326 | 326 | $counterBlock = array(); |
327 | 327 | $ctrTxt = substr($ciphertext, 0, 8); |
328 | - for ($i=0; $i<8; $i++) { |
|
328 | + for ($i = 0; $i < 8; $i++) { |
|
329 | 329 | $counterBlock[$i] = ord(substr($ctrTxt, $i, 1)); |
330 | 330 | } |
331 | 331 | |
@@ -333,29 +333,29 @@ discard block |
||
333 | 333 | $keySchedule = Aes::keyExpansion($key); |
334 | 334 | |
335 | 335 | // separate ciphertext into blocks (skipping past initial 8 bytes) |
336 | - $nBlocks = ceil((strlen($ciphertext)-8) / $blockSize); |
|
336 | + $nBlocks = ceil((strlen($ciphertext) - 8) / $blockSize); |
|
337 | 337 | $ct = array(); |
338 | - for ($b=0; $b<$nBlocks; $b++) { |
|
339 | - $ct[$b] = substr($ciphertext, 8+$b*$blockSize, 16); |
|
338 | + for ($b = 0; $b < $nBlocks; $b++) { |
|
339 | + $ct[$b] = substr($ciphertext, 8 + $b * $blockSize, 16); |
|
340 | 340 | } |
341 | - $ciphertext = $ct; // ciphertext is now array of block-length strings |
|
341 | + $ciphertext = $ct; // ciphertext is now array of block-length strings |
|
342 | 342 | |
343 | 343 | // plaintext will get generated block-by-block into array of block-length strings |
344 | 344 | $plaintxt = array(); |
345 | 345 | |
346 | - for ($b=0; $b<$nBlocks; $b++) { |
|
346 | + for ($b = 0; $b < $nBlocks; $b++) { |
|
347 | 347 | // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes) |
348 | - for ($c=0; $c<4; $c++) { |
|
349 | - $counterBlock[15-$c] = self::urs($b, $c*8) & 0xff; |
|
348 | + for ($c = 0; $c < 4; $c++) { |
|
349 | + $counterBlock[15 - $c] = self::urs($b, $c * 8) & 0xff; |
|
350 | 350 | } |
351 | - for ($c=0; $c<4; $c++) { |
|
352 | - $counterBlock[15-$c-4] = self::urs(($b+1)/0x100000000-1, $c*8) & 0xff; |
|
351 | + for ($c = 0; $c < 4; $c++) { |
|
352 | + $counterBlock[15 - $c - 4] = self::urs(($b + 1) / 0x100000000 - 1, $c * 8) & 0xff; |
|
353 | 353 | } |
354 | 354 | |
355 | - $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // encrypt counter block |
|
355 | + $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // encrypt counter block |
|
356 | 356 | |
357 | 357 | $plaintxtByte = array(); |
358 | - for ($i=0; $i<strlen($ciphertext[$b]); $i++) { |
|
358 | + for ($i = 0; $i < strlen($ciphertext[$b]); $i++) { |
|
359 | 359 | // -- xor plaintext with ciphered counter byte-by-byte -- |
360 | 360 | $plaintxtByte[$i] = $cipherCntr[$i] ^ ord(substr($ciphertext[$b], $i, 1)); |
361 | 361 | $plaintxtByte[$i] = chr($plaintxtByte[$i]); |
@@ -379,12 +379,12 @@ discard block |
||
379 | 379 | private static function urs($a, $b) |
380 | 380 | { |
381 | 381 | $a &= 0xffffffff; |
382 | - $b &= 0x1f; // (bounds check) |
|
383 | - if ($a&0x80000000 && $b>0) { // if left-most bit set |
|
384 | - $a = ($a>>1) & 0x7fffffff; // right-shift one bit & clear left-most bit |
|
385 | - $a = $a >> ($b-1); // remaining right-shifts |
|
382 | + $b &= 0x1f; // (bounds check) |
|
383 | + if ($a & 0x80000000 && $b > 0) { // if left-most bit set |
|
384 | + $a = ($a >> 1) & 0x7fffffff; // right-shift one bit & clear left-most bit |
|
385 | + $a = $a >> ($b - 1); // remaining right-shifts |
|
386 | 386 | } else { // otherwise |
387 | - $a = ($a>>$b); // use normal right-shift |
|
387 | + $a = ($a >> $b); // use normal right-shift |
|
388 | 388 | } |
389 | 389 | |
390 | 390 | return $a; |
@@ -706,7 +706,7 @@ |
||
706 | 706 | * |
707 | 707 | * @throws Ex\IOException |
708 | 708 | * |
709 | - * @return string |
|
709 | + * @return integer |
|
710 | 710 | */ |
711 | 711 | public static function writeBytes($stream, $buf, $num_bytes = null) |
712 | 712 | { |
@@ -186,7 +186,7 @@ discard block |
||
186 | 186 | $if = @\fopen($inputFilename, 'rb'); |
187 | 187 | if ($if === false) { |
188 | 188 | throw new Ex\IOException( |
189 | - 'Cannot open input file for encrypting: ' . |
|
189 | + 'Cannot open input file for encrypting: '. |
|
190 | 190 | self::getLastErrorMessage() |
191 | 191 | ); |
192 | 192 | } |
@@ -200,7 +200,7 @@ discard block |
||
200 | 200 | if ($of === false) { |
201 | 201 | \fclose($if); |
202 | 202 | throw new Ex\IOException( |
203 | - 'Cannot open output file for encrypting: ' . |
|
203 | + 'Cannot open output file for encrypting: '. |
|
204 | 204 | self::getLastErrorMessage() |
205 | 205 | ); |
206 | 206 | } |
@@ -250,7 +250,7 @@ discard block |
||
250 | 250 | $if = @\fopen($inputFilename, 'rb'); |
251 | 251 | if ($if === false) { |
252 | 252 | throw new Ex\IOException( |
253 | - 'Cannot open input file for decrypting: ' . |
|
253 | + 'Cannot open input file for decrypting: '. |
|
254 | 254 | self::getLastErrorMessage() |
255 | 255 | ); |
256 | 256 | } |
@@ -265,7 +265,7 @@ discard block |
||
265 | 265 | if ($of === false) { |
266 | 266 | \fclose($if); |
267 | 267 | throw new Ex\IOException( |
268 | - 'Cannot open output file for decrypting: ' . |
|
268 | + 'Cannot open output file for decrypting: '. |
|
269 | 269 | self::getLastErrorMessage() |
270 | 270 | ); |
271 | 271 | } |
@@ -312,12 +312,12 @@ discard block |
||
312 | 312 | */ |
313 | 313 | private static function encryptResourceInternal($inputHandle, $outputHandle, KeyOrPassword $secret) |
314 | 314 | { |
315 | - if (! \is_resource($inputHandle)) { |
|
315 | + if (!\is_resource($inputHandle)) { |
|
316 | 316 | throw new Ex\IOException( |
317 | 317 | 'Input handle must be a resource!' |
318 | 318 | ); |
319 | 319 | } |
320 | - if (! \is_resource($outputHandle)) { |
|
320 | + if (!\is_resource($outputHandle)) { |
|
321 | 321 | throw new Ex\IOException( |
322 | 322 | 'Output handle must be a resource!' |
323 | 323 | ); |
@@ -345,7 +345,7 @@ discard block |
||
345 | 345 | /* Write the header, salt, and IV. */ |
346 | 346 | self::writeBytes( |
347 | 347 | $outputHandle, |
348 | - Core::CURRENT_VERSION . $file_salt . $iv, |
|
348 | + Core::CURRENT_VERSION.$file_salt.$iv, |
|
349 | 349 | Core::HEADER_VERSION_SIZE + Core::SALT_BYTE_SIZE + $ivsize |
350 | 350 | ); |
351 | 351 | |
@@ -362,7 +362,7 @@ discard block |
||
362 | 362 | |
363 | 363 | /* Loop until we reach the end of the input file. */ |
364 | 364 | $at_file_end = false; |
365 | - while (! (\feof($inputHandle) || $at_file_end)) { |
|
365 | + while (!(\feof($inputHandle) || $at_file_end)) { |
|
366 | 366 | /* Find out if we can read a full buffer, or only a partial one. */ |
367 | 367 | $pos = \ftell($inputHandle); |
368 | 368 | if ($pos === false) { |
@@ -429,12 +429,12 @@ discard block |
||
429 | 429 | */ |
430 | 430 | public static function decryptResourceInternal($inputHandle, $outputHandle, KeyOrPassword $secret) |
431 | 431 | { |
432 | - if (! \is_resource($inputHandle)) { |
|
432 | + if (!\is_resource($inputHandle)) { |
|
433 | 433 | throw new Ex\IOException( |
434 | 434 | 'Input handle must be a resource!' |
435 | 435 | ); |
436 | 436 | } |
437 | - if (! \is_resource($outputHandle)) { |
|
437 | + if (!\is_resource($outputHandle)) { |
|
438 | 438 | throw new Ex\IOException( |
439 | 439 | 'Output handle must be a resource!' |
440 | 440 | ); |
@@ -528,7 +528,7 @@ discard block |
||
528 | 528 | $hmac2 = \hash_copy($hmac); |
529 | 529 | |
530 | 530 | $break = false; |
531 | - while (! $break) { |
|
531 | + while (!$break) { |
|
532 | 532 | $pos = \ftell($inputHandle); |
533 | 533 | if ($pos === false) { |
534 | 534 | throw new Ex\IOException( |
@@ -560,14 +560,14 @@ discard block |
||
560 | 560 | 'Cannot duplicate a hash context' |
561 | 561 | ); |
562 | 562 | } |
563 | - $macs []= \hash_final($chunk_mac); |
|
563 | + $macs [] = \hash_final($chunk_mac); |
|
564 | 564 | } |
565 | 565 | |
566 | 566 | /* Get the final HMAC, which should match the stored one. */ |
567 | 567 | $final_mac = \hash_final($hmac, true); |
568 | 568 | |
569 | 569 | /* Verify the HMAC. */ |
570 | - if (! Core::hashEquals($final_mac, $stored_mac)) { |
|
570 | + if (!Core::hashEquals($final_mac, $stored_mac)) { |
|
571 | 571 | throw new Ex\WrongKeyOrModifiedCiphertextException( |
572 | 572 | 'Integrity check failed.' |
573 | 573 | ); |
@@ -583,7 +583,7 @@ discard block |
||
583 | 583 | } |
584 | 584 | |
585 | 585 | $at_file_end = false; |
586 | - while (! $at_file_end) { |
|
586 | + while (!$at_file_end) { |
|
587 | 587 | $pos = \ftell($inputHandle); |
588 | 588 | if ($pos === false) { |
589 | 589 | throw new Ex\IOException( |
@@ -594,7 +594,7 @@ discard block |
||
594 | 594 | /* Read the next buffer-sized chunk (or less). */ |
595 | 595 | if ($pos + Core::BUFFER_BYTE_SIZE >= $cipher_end) { |
596 | 596 | $at_file_end = true; |
597 | - $read = self::readBytes( |
|
597 | + $read = self::readBytes( |
|
598 | 598 | $inputHandle, |
599 | 599 | $cipher_end - $pos + 1 |
600 | 600 | ); |
@@ -621,7 +621,7 @@ discard block |
||
621 | 621 | throw new Ex\WrongKeyOrModifiedCiphertextException( |
622 | 622 | 'File was modified after MAC verification' |
623 | 623 | ); |
624 | - } elseif (! Core::hashEquals(\array_shift($macs), $calc)) { |
|
624 | + } elseif (!Core::hashEquals(\array_shift($macs), $calc)) { |
|
625 | 625 | throw new Ex\WrongKeyOrModifiedCiphertextException( |
626 | 626 | 'File was modified after MAC verification' |
627 | 627 | ); |
@@ -678,7 +678,7 @@ discard block |
||
678 | 678 | } |
679 | 679 | $buf = ''; |
680 | 680 | $remaining = $num_bytes; |
681 | - while ($remaining > 0 && ! \feof($stream)) { |
|
681 | + while ($remaining > 0 && !\feof($stream)) { |
|
682 | 682 | $read = \fread($stream, $remaining); |
683 | 683 | |
684 | 684 | if ($read === false) { |
@@ -74,6 +74,9 @@ discard block |
||
74 | 74 | } |
75 | 75 | |
76 | 76 | // Compares two strings $a and $b in length-constant time. |
77 | +/** |
|
78 | + * @param string $a |
|
79 | + */ |
|
77 | 80 | function slow_equals($a, $b) |
78 | 81 | { |
79 | 82 | $diff = strlen($a) ^ strlen($b); |
@@ -99,6 +102,9 @@ discard block |
||
99 | 102 | * This implementation of PBKDF2 was originally created by https://defuse.ca |
100 | 103 | * With improvements by http://www.variations-of-shadow.com |
101 | 104 | */ |
105 | +/** |
|
106 | + * @param integer $key_length |
|
107 | + */ |
|
102 | 108 | function pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false) |
103 | 109 | { |
104 | 110 | $algorithm = strtolower($algorithm); |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | { |
44 | 44 | // format: algorithm:iterations:salt:hash |
45 | 45 | $salt = base64_encode(mcrypt_create_iv(PBKDF2_SALT_BYTE_SIZE, MCRYPT_DEV_URANDOM)); |
46 | - return PBKDF2_HASH_ALGORITHM . ":" . PBKDF2_ITERATIONS . ":" . $salt . ":" . |
|
46 | + return PBKDF2_HASH_ALGORITHM.":".PBKDF2_ITERATIONS.":".$salt.":". |
|
47 | 47 | base64_encode(pbkdf2( |
48 | 48 | PBKDF2_HASH_ALGORITHM, |
49 | 49 | $password, |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | function validate_password($password, $correct_hash) |
58 | 58 | { |
59 | 59 | $params = explode(":", $correct_hash); |
60 | - if(count($params) < HASH_SECTIONS) |
|
60 | + if (count($params) < HASH_SECTIONS) |
|
61 | 61 | return false; |
62 | 62 | $pbkdf2 = base64_decode($params[HASH_PBKDF2_INDEX]); |
63 | 63 | return slow_equals( |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | $params[HASH_ALGORITHM_INDEX], |
67 | 67 | $password, |
68 | 68 | $params[HASH_SALT_INDEX], |
69 | - (int)$params[HASH_ITERATION_INDEX], |
|
69 | + (int) $params[HASH_ITERATION_INDEX], |
|
70 | 70 | strlen($pbkdf2), |
71 | 71 | true |
72 | 72 | ) |
@@ -77,7 +77,7 @@ discard block |
||
77 | 77 | function slow_equals($a, $b) |
78 | 78 | { |
79 | 79 | $diff = strlen($a) ^ strlen($b); |
80 | - for($i = 0; $i < strlen($a) && $i < strlen($b); $i++) |
|
80 | + for ($i = 0; $i < strlen($a) && $i < strlen($b); $i++) |
|
81 | 81 | { |
82 | 82 | $diff |= ord($a[$i]) ^ ord($b[$i]); |
83 | 83 | } |
@@ -102,9 +102,9 @@ discard block |
||
102 | 102 | function pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false) |
103 | 103 | { |
104 | 104 | $algorithm = strtolower($algorithm); |
105 | - if(!in_array($algorithm, hash_algos(), true)) |
|
105 | + if (!in_array($algorithm, hash_algos(), true)) |
|
106 | 106 | trigger_error('PBKDF2 ERROR: Invalid hash algorithm.', E_USER_ERROR); |
107 | - if($count <= 0 || $key_length <= 0) |
|
107 | + if ($count <= 0 || $key_length <= 0) |
|
108 | 108 | trigger_error('PBKDF2 ERROR: Invalid parameters.', E_USER_ERROR); |
109 | 109 | |
110 | 110 | if (function_exists("hash_pbkdf2")) { |
@@ -119,9 +119,9 @@ discard block |
||
119 | 119 | $block_count = ceil($key_length / $hash_length); |
120 | 120 | |
121 | 121 | $output = ""; |
122 | - for($i = 1; $i <= $block_count; $i++) { |
|
122 | + for ($i = 1; $i <= $block_count; $i++) { |
|
123 | 123 | // $i encoded as 4 bytes, big endian. |
124 | - $last = $salt . pack("N", $i); |
|
124 | + $last = $salt.pack("N", $i); |
|
125 | 125 | // first iteration |
126 | 126 | $last = $xorsum = hash_hmac($algorithm, $last, $password, true); |
127 | 127 | // perform the other $count - 1 iterations |
@@ -131,7 +131,7 @@ discard block |
||
131 | 131 | $output .= $xorsum; |
132 | 132 | } |
133 | 133 | |
134 | - if($raw_output) |
|
134 | + if ($raw_output) |
|
135 | 135 | return substr($output, 0, $key_length); |
136 | 136 | else |
137 | 137 | return bin2hex(substr($output, 0, $key_length)); |
@@ -57,8 +57,9 @@ discard block |
||
57 | 57 | function validate_password($password, $correct_hash) |
58 | 58 | { |
59 | 59 | $params = explode(":", $correct_hash); |
60 | - if(count($params) < HASH_SECTIONS) |
|
61 | - return false; |
|
60 | + if(count($params) < HASH_SECTIONS) { |
|
61 | + return false; |
|
62 | + } |
|
62 | 63 | $pbkdf2 = base64_decode($params[HASH_PBKDF2_INDEX]); |
63 | 64 | return slow_equals( |
64 | 65 | $pbkdf2, |
@@ -102,10 +103,12 @@ discard block |
||
102 | 103 | function pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false) |
103 | 104 | { |
104 | 105 | $algorithm = strtolower($algorithm); |
105 | - if(!in_array($algorithm, hash_algos(), true)) |
|
106 | - trigger_error('PBKDF2 ERROR: Invalid hash algorithm.', E_USER_ERROR); |
|
107 | - if($count <= 0 || $key_length <= 0) |
|
108 | - trigger_error('PBKDF2 ERROR: Invalid parameters.', E_USER_ERROR); |
|
106 | + if(!in_array($algorithm, hash_algos(), true)) { |
|
107 | + trigger_error('PBKDF2 ERROR: Invalid hash algorithm.', E_USER_ERROR); |
|
108 | + } |
|
109 | + if($count <= 0 || $key_length <= 0) { |
|
110 | + trigger_error('PBKDF2 ERROR: Invalid parameters.', E_USER_ERROR); |
|
111 | + } |
|
109 | 112 | |
110 | 113 | if (function_exists("hash_pbkdf2")) { |
111 | 114 | // The output length is in NIBBLES (4-bits) if $raw_output is false! |
@@ -131,9 +134,10 @@ discard block |
||
131 | 134 | $output .= $xorsum; |
132 | 135 | } |
133 | 136 | |
134 | - if($raw_output) |
|
135 | - return substr($output, 0, $key_length); |
|
136 | - else |
|
137 | - return bin2hex(substr($output, 0, $key_length)); |
|
138 | -} |
|
137 | + if($raw_output) { |
|
138 | + return substr($output, 0, $key_length); |
|
139 | + } else { |
|
140 | + return bin2hex(substr($output, 0, $key_length)); |
|
141 | + } |
|
142 | + } |
|
139 | 143 | ?> |
140 | 144 | \ No newline at end of file |
@@ -76,6 +76,10 @@ discard block |
||
76 | 76 | } |
77 | 77 | |
78 | 78 | // Compares two strings $a and $b in length-constant time. |
79 | + |
|
80 | + /** |
|
81 | + * @param string $a |
|
82 | + */ |
|
79 | 83 | public static function slow_equals($a, $b) |
80 | 84 | { |
81 | 85 | $diff = strlen($a) ^ strlen($b); |
@@ -101,6 +105,11 @@ discard block |
||
101 | 105 | * This implementation of PBKDF2 was originally created by https://defuse.ca |
102 | 106 | * With improvements by http://www.variations-of-shadow.com |
103 | 107 | */ |
108 | + |
|
109 | + /** |
|
110 | + * @param integer $count |
|
111 | + * @param integer $key_length |
|
112 | + */ |
|
104 | 113 | private static function pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false) |
105 | 114 | { |
106 | 115 | $algorithm = strtolower($algorithm); |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | { |
46 | 46 | // format: algorithm:iterations:salt:hash |
47 | 47 | $salt = base64_encode(mcrypt_create_iv(PBKDF2_SALT_BYTES, MCRYPT_DEV_URANDOM)); |
48 | - return PBKDF2_HASH_ALGORITHM . ":" . PBKDF2_ITERATIONS . ":" . $salt . ":" . |
|
48 | + return PBKDF2_HASH_ALGORITHM.":".PBKDF2_ITERATIONS.":".$salt.":". |
|
49 | 49 | base64_encode(self::pbkdf2( |
50 | 50 | PBKDF2_HASH_ALGORITHM, |
51 | 51 | $password, |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | public static function validate_password($password, $good_hash) |
60 | 60 | { |
61 | 61 | $params = explode(":", $good_hash); |
62 | - if(count($params) < HASH_SECTIONS) |
|
62 | + if (count($params) < HASH_SECTIONS) |
|
63 | 63 | return false; |
64 | 64 | $pbkdf2 = base64_decode($params[HASH_PBKDF2_INDEX]); |
65 | 65 | return self::slow_equals( |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | $params[HASH_ALGORITHM_INDEX], |
69 | 69 | $password, |
70 | 70 | $params[HASH_SALT_INDEX], |
71 | - (int)$params[HASH_ITERATION_INDEX], |
|
71 | + (int) $params[HASH_ITERATION_INDEX], |
|
72 | 72 | strlen($pbkdf2), |
73 | 73 | true |
74 | 74 | ) |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | public static function slow_equals($a, $b) |
80 | 80 | { |
81 | 81 | $diff = strlen($a) ^ strlen($b); |
82 | - for($i = 0; $i < strlen($a) && $i < strlen($b); $i++) |
|
82 | + for ($i = 0; $i < strlen($a) && $i < strlen($b); $i++) |
|
83 | 83 | { |
84 | 84 | $diff |= ord($a[$i]) ^ ord($b[$i]); |
85 | 85 | } |
@@ -104,9 +104,9 @@ discard block |
||
104 | 104 | private static function pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false) |
105 | 105 | { |
106 | 106 | $algorithm = strtolower($algorithm); |
107 | - if(!in_array($algorithm, hash_algos(), true)) |
|
107 | + if (!in_array($algorithm, hash_algos(), true)) |
|
108 | 108 | trigger_error('PBKDF2 ERROR: Invalid hash algorithm.', E_USER_ERROR); |
109 | - if($count <= 0 || $key_length <= 0) |
|
109 | + if ($count <= 0 || $key_length <= 0) |
|
110 | 110 | trigger_error('PBKDF2 ERROR: Invalid parameters.', E_USER_ERROR); |
111 | 111 | |
112 | 112 | if (function_exists("hash_pbkdf2")) { |
@@ -121,9 +121,9 @@ discard block |
||
121 | 121 | $block_count = ceil($key_length / $hash_length); |
122 | 122 | |
123 | 123 | $output = ""; |
124 | - for($i = 1; $i <= $block_count; $i++) { |
|
124 | + for ($i = 1; $i <= $block_count; $i++) { |
|
125 | 125 | // $i encoded as 4 bytes, big endian. |
126 | - $last = $salt . pack("N", $i); |
|
126 | + $last = $salt.pack("N", $i); |
|
127 | 127 | // first iteration |
128 | 128 | $last = $xorsum = hash_hmac($algorithm, $last, $password, true); |
129 | 129 | // perform the other $count - 1 iterations |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | $output .= $xorsum; |
134 | 134 | } |
135 | 135 | |
136 | - if($raw_output) |
|
136 | + if ($raw_output) |
|
137 | 137 | return substr($output, 0, $key_length); |
138 | 138 | else |
139 | 139 | return bin2hex(substr($output, 0, $key_length)); |
@@ -59,8 +59,9 @@ discard block |
||
59 | 59 | public static function validate_password($password, $good_hash) |
60 | 60 | { |
61 | 61 | $params = explode(":", $good_hash); |
62 | - if(count($params) < HASH_SECTIONS) |
|
63 | - return false; |
|
62 | + if(count($params) < HASH_SECTIONS) { |
|
63 | + return false; |
|
64 | + } |
|
64 | 65 | $pbkdf2 = base64_decode($params[HASH_PBKDF2_INDEX]); |
65 | 66 | return self::slow_equals( |
66 | 67 | $pbkdf2, |
@@ -104,10 +105,12 @@ discard block |
||
104 | 105 | private static function pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false) |
105 | 106 | { |
106 | 107 | $algorithm = strtolower($algorithm); |
107 | - if(!in_array($algorithm, hash_algos(), true)) |
|
108 | - trigger_error('PBKDF2 ERROR: Invalid hash algorithm.', E_USER_ERROR); |
|
109 | - if($count <= 0 || $key_length <= 0) |
|
110 | - trigger_error('PBKDF2 ERROR: Invalid parameters.', E_USER_ERROR); |
|
108 | + if(!in_array($algorithm, hash_algos(), true)) { |
|
109 | + trigger_error('PBKDF2 ERROR: Invalid hash algorithm.', E_USER_ERROR); |
|
110 | + } |
|
111 | + if($count <= 0 || $key_length <= 0) { |
|
112 | + trigger_error('PBKDF2 ERROR: Invalid parameters.', E_USER_ERROR); |
|
113 | + } |
|
111 | 114 | |
112 | 115 | if (function_exists("hash_pbkdf2")) { |
113 | 116 | // The output length is in NIBBLES (4-bits) if $raw_output is false! |
@@ -133,10 +136,11 @@ discard block |
||
133 | 136 | $output .= $xorsum; |
134 | 137 | } |
135 | 138 | |
136 | - if($raw_output) |
|
137 | - return substr($output, 0, $key_length); |
|
138 | - else |
|
139 | - return bin2hex(substr($output, 0, $key_length)); |
|
139 | + if($raw_output) { |
|
140 | + return substr($output, 0, $key_length); |
|
141 | + } else { |
|
142 | + return bin2hex(substr($output, 0, $key_length)); |
|
143 | + } |
|
140 | 144 | } |
141 | 145 | } |
142 | 146 | ?> |
143 | 147 | \ No newline at end of file |