@@ -47,8 +47,8 @@ discard block |
||
47 | 47 | * @param integer $Nb |
48 | 48 | */ |
49 | 49 | private static function addRoundKey($state, $w, $rnd, $Nb) { // xor Round Key into state S [é5.1.4] |
50 | - for ($r=0; $r<4; $r++) { |
|
51 | - for ($c=0; $c<$Nb; $c++) $state[$r][$c] ^= $w[$rnd*4+$c][$r]; |
|
50 | + for ($r = 0; $r < 4; $r++) { |
|
51 | + for ($c = 0; $c < $Nb; $c++) $state[$r][$c] ^= $w[$rnd * 4 + $c][$r]; |
|
52 | 52 | } |
53 | 53 | |
54 | 54 | return $state; |
@@ -58,8 +58,8 @@ discard block |
||
58 | 58 | * @param integer $Nb |
59 | 59 | */ |
60 | 60 | private static function subBytes($s, $Nb) { // apply SBox to state S [é5.1.1] |
61 | - for ($r=0; $r<4; $r++) { |
|
62 | - for ($c=0; $c<$Nb; $c++) { |
|
61 | + for ($r = 0; $r < 4; $r++) { |
|
62 | + for ($c = 0; $c < $Nb; $c++) { |
|
63 | 63 | $s[$r][$c] = self::$sBox[$s[$r][$c]]; |
64 | 64 | } |
65 | 65 | } |
@@ -72,29 +72,29 @@ discard block |
||
72 | 72 | */ |
73 | 73 | private static function shiftRows($s, $Nb) { // shift row r of state S left by r bytes [é5.1.2] |
74 | 74 | $t = array(4); |
75 | - for ($r=1; $r<4; $r++) { |
|
76 | - for ($c=0; $c<4; $c++) { |
|
77 | - $t[$c] = $s[$r][($c+$r)%$Nb]; |
|
75 | + for ($r = 1; $r < 4; $r++) { |
|
76 | + for ($c = 0; $c < 4; $c++) { |
|
77 | + $t[$c] = $s[$r][($c + $r) % $Nb]; |
|
78 | 78 | } |
79 | 79 | // shift into temp copy |
80 | - for ($c=0; $c<4; $c++) { |
|
80 | + for ($c = 0; $c < 4; $c++) { |
|
81 | 81 | $s[$r][$c] = $t[$c]; |
82 | 82 | } |
83 | 83 | // and copy back |
84 | 84 | } // note that this will work for Nb=4,5,6, but not 7,8 (always 4 for AES): |
85 | - return $s; // see fp.gladman.plus.com/cryptography_technology/rijndael/aes.spec.311.pdf |
|
85 | + return $s; // see fp.gladman.plus.com/cryptography_technology/rijndael/aes.spec.311.pdf |
|
86 | 86 | } |
87 | 87 | |
88 | 88 | /** |
89 | 89 | * @param integer $Nb |
90 | 90 | */ |
91 | 91 | private static function mixColumns($s, $Nb) { // combine bytes of each col of state S [é5.1.3] |
92 | - for ($c=0; $c<4; $c++) { |
|
93 | - $a = array(4); // 'a' is a copy of the current column from 's' |
|
94 | - $b = array(4); // 'b' is aé{02} in GF(2^8) |
|
95 | - for ($i=0; $i<4; $i++) { |
|
92 | + for ($c = 0; $c < 4; $c++) { |
|
93 | + $a = array(4); // 'a' is a copy of the current column from 's' |
|
94 | + $b = array(4); // 'b' is aé{02} in GF(2^8) |
|
95 | + for ($i = 0; $i < 4; $i++) { |
|
96 | 96 | $a[$i] = $s[$i][$c]; |
97 | - $b[$i] = $s[$i][$c]&0x80 ? $s[$i][$c]<<1 ^ 0x011b : $s[$i][$c]<<1; |
|
97 | + $b[$i] = $s[$i][$c] & 0x80 ? $s[$i][$c] << 1 ^ 0x011b : $s[$i][$c] << 1; |
|
98 | 98 | } |
99 | 99 | // a[n] ^ b[n] is aé{03} in GF(2^8) |
100 | 100 | $s[0][$c] = $b[0] ^ $a[1] ^ $b[1] ^ $a[2] ^ $a[3]; // 2*a0 + 3*a1 + a2 + a3 |
@@ -114,33 +114,33 @@ discard block |
||
114 | 114 | * @return key schedule as 2D byte-array (Nr+1 x Nb bytes) |
115 | 115 | */ |
116 | 116 | public static function keyExpansion($key) { // generate Key Schedule from Cipher Key [é5.2] |
117 | - $Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES) |
|
118 | - $Nk = count($key)/4; // key length (in words): 4/6/8 for 128/192/256-bit keys |
|
119 | - $Nr = $Nk + 6; // no of rounds: 10/12/14 for 128/192/256-bit keys |
|
117 | + $Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES) |
|
118 | + $Nk = count($key) / 4; // key length (in words): 4/6/8 for 128/192/256-bit keys |
|
119 | + $Nr = $Nk + 6; // no of rounds: 10/12/14 for 128/192/256-bit keys |
|
120 | 120 | |
121 | 121 | $w = array(); |
122 | 122 | $temp = array(); |
123 | 123 | |
124 | - for ($i=0; $i<$Nk; $i++) { |
|
125 | - $r = array($key[4*$i], $key[4*$i+1], $key[4*$i+2], $key[4*$i+3]); |
|
124 | + for ($i = 0; $i < $Nk; $i++) { |
|
125 | + $r = array($key[4 * $i], $key[4 * $i + 1], $key[4 * $i + 2], $key[4 * $i + 3]); |
|
126 | 126 | $w[$i] = $r; |
127 | 127 | } |
128 | 128 | |
129 | - for ($i=$Nk; $i<($Nb*($Nr+1)); $i++) { |
|
129 | + for ($i = $Nk; $i < ($Nb * ($Nr + 1)); $i++) { |
|
130 | 130 | $w[$i] = array(); |
131 | - for ($t=0; $t<4; $t++) { |
|
132 | - $temp[$t] = $w[$i-1][$t]; |
|
131 | + for ($t = 0; $t < 4; $t++) { |
|
132 | + $temp[$t] = $w[$i - 1][$t]; |
|
133 | 133 | } |
134 | 134 | if ($i % $Nk == 0) { |
135 | 135 | $temp = self::subWord(self::rotWord($temp)); |
136 | - for ($t=0; $t<4; $t++) { |
|
137 | - $temp[$t] ^= self::$rCon[$i/$Nk][$t]; |
|
136 | + for ($t = 0; $t < 4; $t++) { |
|
137 | + $temp[$t] ^= self::$rCon[$i / $Nk][$t]; |
|
138 | 138 | } |
139 | - } elseif ($Nk > 6 && $i%$Nk == 4) { |
|
139 | + } elseif ($Nk > 6 && $i % $Nk == 4) { |
|
140 | 140 | $temp = self::subWord($temp); |
141 | 141 | } |
142 | - for ($t=0; $t<4; $t++) { |
|
143 | - $w[$i][$t] = $w[$i-$Nk][$t] ^ $temp[$t]; |
|
142 | + for ($t = 0; $t < 4; $t++) { |
|
143 | + $w[$i][$t] = $w[$i - $Nk][$t] ^ $temp[$t]; |
|
144 | 144 | } |
145 | 145 | } |
146 | 146 | |
@@ -148,7 +148,7 @@ discard block |
||
148 | 148 | } |
149 | 149 | |
150 | 150 | private static function subWord($w) { // apply SBox to 4-byte word w |
151 | - for ($i=0; $i<4; $i++) { |
|
151 | + for ($i = 0; $i < 4; $i++) { |
|
152 | 152 | $w[$i] = self::$sBox[$w[$i]]; |
153 | 153 | } |
154 | 154 | |
@@ -157,8 +157,8 @@ discard block |
||
157 | 157 | |
158 | 158 | private static function rotWord($w) { // rotate 4-byte word w left by one byte |
159 | 159 | $tmp = $w[0]; |
160 | - for ($i=0; $i<3; $i++) { |
|
161 | - $w[$i] = $w[$i+1]; |
|
160 | + for ($i = 0; $i < 3; $i++) { |
|
161 | + $w[$i] = $w[$i + 1]; |
|
162 | 162 | } |
163 | 163 | $w[3] = $tmp; |
164 | 164 |
@@ -37,7 +37,9 @@ discard block |
||
37 | 37 | $state = self::addRoundKey($state, $w, $Nr, $Nb); |
38 | 38 | |
39 | 39 | $output = array(4 * $Nb); // convert state to 1-d array before returning [é3.4] |
40 | - for ($i = 0; $i < 4 * $Nb; $i++) $output[$i] = $state[$i % 4][floor($i / 4)]; |
|
40 | + for ($i = 0; $i < 4 * $Nb; $i++) { |
|
41 | + $output[$i] = $state[$i % 4][floor($i / 4)]; |
|
42 | + } |
|
41 | 43 | |
42 | 44 | return $output; |
43 | 45 | } |
@@ -48,7 +50,9 @@ discard block |
||
48 | 50 | */ |
49 | 51 | private static function addRoundKey($state, $w, $rnd, $Nb) { // xor Round Key into state S [é5.1.4] |
50 | 52 | for ($r=0; $r<4; $r++) { |
51 | - for ($c=0; $c<$Nb; $c++) $state[$r][$c] ^= $w[$rnd*4+$c][$r]; |
|
53 | + for ($c=0; $c<$Nb; $c++) { |
|
54 | + $state[$r][$c] ^= $w[$rnd*4+$c][$r]; |
|
55 | + } |
|
52 | 56 | } |
53 | 57 | |
54 | 58 | return $state; |
@@ -20,8 +20,8 @@ discard block |
||
20 | 20 | */ |
21 | 21 | public static function encrypt($plaintext, $password, $nBits) |
22 | 22 | { |
23 | - $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
|
24 | - if (!($nBits==128 || $nBits==192 || $nBits==256)) { |
|
23 | + $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
|
24 | + if (!($nBits == 128 || $nBits == 192 || $nBits == 256)) { |
|
25 | 25 | return ''; |
26 | 26 | } |
27 | 27 | // standard allows 128/192/256 bit keys |
@@ -99,58 +99,58 @@ discard block |
||
99 | 99 | */ |
100 | 100 | public static function decrypt($ciphertext, $password, $nBits) |
101 | 101 | { |
102 | - $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
|
103 | - if (!($nBits==128 || $nBits==192 || $nBits==256)) { |
|
102 | + $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
|
103 | + if (!($nBits == 128 || $nBits == 192 || $nBits == 256)) { |
|
104 | 104 | return ''; |
105 | 105 | } |
106 | 106 | // standard allows 128/192/256 bit keys |
107 | 107 | $ciphertext = base64_decode($ciphertext); |
108 | 108 | |
109 | 109 | // use AES to encrypt password (mirroring encrypt routine) |
110 | - $nBytes = $nBits/8; // no bytes in key |
|
110 | + $nBytes = $nBits / 8; // no bytes in key |
|
111 | 111 | $pwBytes = array(); |
112 | - for ($i=0; $i<$nBytes; $i++) { |
|
113 | - $pwBytes[$i] = ord(substr($password,$i,1)) & 0xff; |
|
112 | + for ($i = 0; $i < $nBytes; $i++) { |
|
113 | + $pwBytes[$i] = ord(substr($password, $i, 1)) & 0xff; |
|
114 | 114 | } |
115 | 115 | $key = Aes::cipher($pwBytes, Aes::keyExpansion($pwBytes)); |
116 | - $key = array_merge($key, array_slice($key, 0, $nBytes-16)); // expand key to 16/24/32 bytes long |
|
116 | + $key = array_merge($key, array_slice($key, 0, $nBytes - 16)); // expand key to 16/24/32 bytes long |
|
117 | 117 | |
118 | 118 | // recover nonce from 1st element of ciphertext |
119 | 119 | $counterBlock = array(); |
120 | 120 | $ctrTxt = substr($ciphertext, 0, 8); |
121 | - for ($i=0; $i<8; $i++) { |
|
122 | - $counterBlock[$i] = ord(substr($ctrTxt,$i,1)); |
|
121 | + for ($i = 0; $i < 8; $i++) { |
|
122 | + $counterBlock[$i] = ord(substr($ctrTxt, $i, 1)); |
|
123 | 123 | } |
124 | 124 | |
125 | 125 | // generate key schedule |
126 | 126 | $keySchedule = Aes::keyExpansion($key); |
127 | 127 | |
128 | 128 | // separate ciphertext into blocks (skipping past initial 8 bytes) |
129 | - $nBlocks = ceil((strlen($ciphertext)-8) / $blockSize); |
|
129 | + $nBlocks = ceil((strlen($ciphertext) - 8) / $blockSize); |
|
130 | 130 | $ct = array(); |
131 | - for ($b=0; $b<$nBlocks; $b++) { |
|
132 | - $ct[$b] = substr($ciphertext, 8+$b*$blockSize, 16); |
|
131 | + for ($b = 0; $b < $nBlocks; $b++) { |
|
132 | + $ct[$b] = substr($ciphertext, 8 + $b * $blockSize, 16); |
|
133 | 133 | } |
134 | - $ciphertext = $ct; // ciphertext is now array of block-length strings |
|
134 | + $ciphertext = $ct; // ciphertext is now array of block-length strings |
|
135 | 135 | |
136 | 136 | // plaintext will get generated block-by-block into array of block-length strings |
137 | 137 | $plaintxt = array(); |
138 | 138 | |
139 | - for ($b=0; $b<$nBlocks; $b++) { |
|
139 | + for ($b = 0; $b < $nBlocks; $b++) { |
|
140 | 140 | // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes) |
141 | - for ($c=0; $c<4; $c++) { |
|
142 | - $counterBlock[15-$c] = self::urs($b, $c*8) & 0xff; |
|
141 | + for ($c = 0; $c < 4; $c++) { |
|
142 | + $counterBlock[15 - $c] = self::urs($b, $c * 8) & 0xff; |
|
143 | 143 | } |
144 | - for ($c=0; $c<4; $c++) { |
|
145 | - $counterBlock[15-$c-4] = self::urs(($b+1)/0x100000000-1, $c*8) & 0xff; |
|
144 | + for ($c = 0; $c < 4; $c++) { |
|
145 | + $counterBlock[15 - $c - 4] = self::urs(($b + 1) / 0x100000000 - 1, $c * 8) & 0xff; |
|
146 | 146 | } |
147 | 147 | |
148 | - $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // encrypt counter block |
|
148 | + $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // encrypt counter block |
|
149 | 149 | |
150 | 150 | $plaintxtByte = array(); |
151 | - for ($i=0; $i<strlen($ciphertext[$b]); $i++) { |
|
151 | + for ($i = 0; $i < strlen($ciphertext[$b]); $i++) { |
|
152 | 152 | // -- xor plaintext with ciphered counter byte-by-byte -- |
153 | - $plaintxtByte[$i] = $cipherCntr[$i] ^ ord(substr($ciphertext[$b],$i,1)); |
|
153 | + $plaintxtByte[$i] = $cipherCntr[$i] ^ ord(substr($ciphertext[$b], $i, 1)); |
|
154 | 154 | $plaintxtByte[$i] = chr($plaintxtByte[$i]); |
155 | 155 | |
156 | 156 | } |
@@ -66,8 +66,12 @@ |
||
66 | 66 | for ($b = 0; $b < $blockCount; $b++) { |
67 | 67 | // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes) |
68 | 68 | // done in two stages for 32-bit ops: using two words allows us to go past 2^32 blocks (68GB) |
69 | - for ($c = 0; $c < 4; $c++) $counterBlock[15 - $c] = self::urs($b, $c * 8) & 0xff; |
|
70 | - for ($c = 0; $c < 4; $c++) $counterBlock[15 - $c - 4] = self::urs($b / 0x100000000, $c * 8); |
|
69 | + for ($c = 0; $c < 4; $c++) { |
|
70 | + $counterBlock[15 - $c] = self::urs($b, $c * 8) & 0xff; |
|
71 | + } |
|
72 | + for ($c = 0; $c < 4; $c++) { |
|
73 | + $counterBlock[15 - $c - 4] = self::urs($b / 0x100000000, $c * 8); |
|
74 | + } |
|
71 | 75 | |
72 | 76 | $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // -- encrypt counter block -- |
73 | 77 |
@@ -880,7 +880,8 @@ discard block |
||
880 | 880 | 'error_ssh_credentials_missing' => 'Poświadczenia SSH są niepoprawne.', |
881 | 881 | 'error_url_must_be_ssh' => 'URL musi zawierać protokół SSH!', |
882 | 882 | 'auto_update_server_password_info' => 'Kliknięcie start, automatycznie wykona poniższe kroki: < ul > <li > Połączenie przez SSH do { |
883 | - serwera wykorzystując dane logowania i pole `URL`, < / li > <li > Zmiana hasła użytkownika na serwerze < /il > <li > Zachowanie nowego hasła w Teampass < /il > <li > Zakończenie połączenia SSH < /li > < / ul > <br / > < b > Upewnij się, że użytkownik posiada prawa do zalogowania się i zmiany hasła na serwerze(jeśli nie, określ hasło użytkownika root) przed uruchomieniem.< / b > ', |
|
883 | + serwera wykorzystując dane logowania i pole `URL`, < / li > <li > Zmiana hasła użytkownika na serwerze < /il > <li > Zachowanie nowego hasła w Teampass < /il > <li > Zakończenie połączenia SSH < /li > < / ul > <br / > < b > Upewnij się, że użytkownik posiada prawa do { |
|
884 | + zalogowania się i zmiany hasła na serwerze(jeśli nie, określ hasło użytkownika root) przed uruchomieniem.< / b > ', |
|
884 | 885 | 'update_server_password' => 'Zaktualizuj hasło serwera', |
885 | 886 | 'error_personal_sk_expected' => 'Musisz najpierw wprowadzić swój prywatny saltkety!', |
886 | 887 | 'click_to_generate' => 'Kliknij by wygenerować', |
@@ -1048,5 +1049,6 @@ discard block |
||
1048 | 1049 | 'no_username_needed' => 'No username needed', |
1049 | 1050 | '' => '' |
1050 | 1051 | ); |
1051 | -} |
|
1052 | + } |
|
1053 | + } |
|
1052 | 1054 | } |
@@ -215,11 +215,9 @@ |
||
215 | 215 | if ($isGUID === true) { |
216 | 216 | $username = $this->adldap->utilities()->strGuidToHex($username); |
217 | 217 | $filter = "objectguid=".$username; |
218 | - } |
|
219 | - else if (strpos($username, "@")) { |
|
218 | + } else if (strpos($username, "@")) { |
|
220 | 219 | $filter = "userPrincipalName=".$username; |
221 | - } |
|
222 | - else { |
|
220 | + } else { |
|
223 | 221 | $filter = "samaccountname=".$username; |
224 | 222 | } |
225 | 223 | $filter = "(&(objectCategory=person)({$filter}))"; |
@@ -321,14 +321,14 @@ |
||
321 | 321 | /** |
322 | 322 | * @param string $which |
323 | 323 | */ |
324 | - public function insertOrReplace($which, $table, $datas, $options=array()) { |
|
324 | + public function insertOrReplace($which, $table, $datas, $options = array()) { |
|
325 | 325 | $datas = unserialize(serialize($datas)); // break references within array |
326 | 326 | $keys = $values = array(); |
327 | 327 | |
328 | 328 | if (isset($datas[0]) && is_array($datas[0])) { |
329 | 329 | foreach ($datas as $datum) { |
330 | 330 | ksort($datum); |
331 | - if (! $keys) { |
|
331 | + if (!$keys) { |
|
332 | 332 | $keys = array_keys($datum); |
333 | 333 | } |
334 | 334 | $values[] = array_values($datum); |
@@ -161,12 +161,24 @@ discard block |
||
161 | 161 | |
162 | 162 | |
163 | 163 | public function __construct($host = null, $user = null, $password = null, $dbName = null, $port = null, $encoding = null) { |
164 | - if ($host === null) $host = DB::$host; |
|
165 | - if ($user === null) $user = DB::$user; |
|
166 | - if ($password === null) $password = DB::$password; |
|
167 | - if ($dbName === null) $dbName = DB::$dbName; |
|
168 | - if ($port === null) $port = DB::$port; |
|
169 | - 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 | + } |
|
170 | 182 | |
171 | 183 | $this->host = $host; |
172 | 184 | $this->user = $user; |
@@ -180,7 +192,9 @@ discard block |
||
180 | 192 | $mysql = $this->internal_mysql; |
181 | 193 | |
182 | 194 | if (!($mysql instanceof MySQLi)) { |
183 | - if (!$this->port) $this->port = ini_get('mysqli.default_port'); |
|
195 | + if (!$this->port) { |
|
196 | + $this->port = ini_get('mysqli.default_port'); |
|
197 | + } |
|
184 | 198 | $this->current_db = $this->dbName; |
185 | 199 | |
186 | 200 | $mysql = new mysqli($this->host, $this->user, $this->password, $this->dbName, $this->port); |
@@ -300,8 +314,11 @@ discard block |
||
300 | 314 | protected function formatTableName($table) { |
301 | 315 | $table = trim($table, '`'); |
302 | 316 | |
303 | - if (strpos($table, '.')) return implode('.', array_map(array($this, 'formatTableName'), explode('.', $table))); |
|
304 | - 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 | + } |
|
305 | 322 | } |
306 | 323 | |
307 | 324 | public function update() { |
@@ -529,21 +546,31 @@ discard block |
||
529 | 546 | |
530 | 547 | protected function sanitize($value) { |
531 | 548 | if (is_object($value)) { |
532 | - if ($value instanceof MeekroDBEval) return $value->text; |
|
533 | - else if ($value instanceof DateTime) return $this->escape($value->format('Y-m-d H:i:s')); |
|
534 | - else return ''; |
|
549 | + if ($value instanceof MeekroDBEval) { |
|
550 | + return $value->text; |
|
551 | + } else if ($value instanceof DateTime) { |
|
552 | + return $this->escape($value->format('Y-m-d H:i:s')); |
|
553 | + } else { |
|
554 | + return ''; |
|
555 | + } |
|
535 | 556 | } |
536 | 557 | |
537 | - if (is_null($value)) return $this->usenull ? 'NULL' : "''"; |
|
538 | - else if (is_bool($value)) return ($value ? 1 : 0); |
|
539 | - else if (is_int($value)) return $value; |
|
540 | - else if (is_float($value)) return $value; |
|
541 | - |
|
542 | - else if (is_array($value)) { |
|
558 | + if (is_null($value)) { |
|
559 | + return $this->usenull ? 'NULL' : "''"; |
|
560 | + } else if (is_bool($value)) { |
|
561 | + return ($value ? 1 : 0); |
|
562 | + } else if (is_int($value)) { |
|
563 | + return $value; |
|
564 | + } else if (is_float($value)) { |
|
565 | + return $value; |
|
566 | + } else if (is_array($value)) { |
|
543 | 567 | // non-assoc array? |
544 | 568 | if (array_values($value) === $value) { |
545 | - if (is_array($value[0])) return implode(', ', array_map(array($this, 'sanitize'), $value)); |
|
546 | - else return '('.implode(', ', array_map(array($this, 'sanitize'), $value)).')'; |
|
569 | + if (is_array($value[0])) { |
|
570 | + return implode(', ', array_map(array($this, 'sanitize'), $value)); |
|
571 | + } else { |
|
572 | + return '('.implode(', ', array_map(array($this, 'sanitize'), $value)).')'; |
|
573 | + } |
|
547 | 574 | } |
548 | 575 | |
549 | 576 | $pairs = array(); |
@@ -591,30 +618,48 @@ discard block |
||
591 | 618 | |
592 | 619 | if ($type != '?') { |
593 | 620 | $is_array_type = in_array($type, $array_types, true); |
594 | - if ($is_array_type && !is_array($arg)) $this->nonSQLError("Badly formatted SQL query: Expected array, got scalar instead!"); |
|
595 | - else if (!$is_array_type && is_array($arg)) $this->nonSQLError("Badly formatted SQL query: Expected scalar, got array instead!"); |
|
621 | + if ($is_array_type && !is_array($arg)) { |
|
622 | + $this->nonSQLError("Badly formatted SQL query: Expected array, got scalar instead!"); |
|
623 | + } else if (!$is_array_type && is_array($arg)) { |
|
624 | + $this->nonSQLError("Badly formatted SQL query: Expected scalar, got array instead!"); |
|
625 | + } |
|
596 | 626 | } |
597 | 627 | |
598 | - if ($type == 's') $result = $this->escape($arg); |
|
599 | - else if ($type == 'i') $result = $this->intval($arg); |
|
600 | - else if ($type == 'd') $result = doubleval($arg); |
|
601 | - else if ($type == 'b') $result = $this->formatTableName($arg); |
|
602 | - else if ($type == 'l') $result = $arg; |
|
603 | - else if ($type == 'ss') $result = $this->escape("%".str_replace(array('%', '_'), array('\%', '\_'), $arg)."%"); |
|
604 | - else if ($type == 't') $result = $this->escape($this->parseTS($arg)); |
|
605 | - |
|
606 | - else if ($type == 'ls') $result = array_map(array($this, 'escape'), $arg); |
|
607 | - else if ($type == 'li') $result = array_map(array($this, 'intval'), $arg); |
|
608 | - else if ($type == 'ld') $result = array_map('doubleval', $arg); |
|
609 | - else if ($type == 'lb') $result = array_map(array($this, 'formatTableName'), $arg); |
|
610 | - else if ($type == 'll') $result = $arg; |
|
611 | - else if ($type == 'lt') $result = array_map(array($this, 'escape'), array_map(array($this, 'parseTS'), $arg)); |
|
612 | - |
|
613 | - else if ($type == '?') $result = $this->sanitize($arg); |
|
614 | - |
|
615 | - else $this->nonSQLError("Badly formatted SQL query: Invalid MeekroDB param $type"); |
|
628 | + if ($type == 's') { |
|
629 | + $result = $this->escape($arg); |
|
630 | + } else if ($type == 'i') { |
|
631 | + $result = $this->intval($arg); |
|
632 | + } else if ($type == 'd') { |
|
633 | + $result = doubleval($arg); |
|
634 | + } else if ($type == 'b') { |
|
635 | + $result = $this->formatTableName($arg); |
|
636 | + } else if ($type == 'l') { |
|
637 | + $result = $arg; |
|
638 | + } else if ($type == 'ss') { |
|
639 | + $result = $this->escape("%".str_replace(array('%', '_'), array('\%', '\_'), $arg)."%"); |
|
640 | + } else if ($type == 't') { |
|
641 | + $result = $this->escape($this->parseTS($arg)); |
|
642 | + } else if ($type == 'ls') { |
|
643 | + $result = array_map(array($this, 'escape'), $arg); |
|
644 | + } else if ($type == 'li') { |
|
645 | + $result = array_map(array($this, 'intval'), $arg); |
|
646 | + } else if ($type == 'ld') { |
|
647 | + $result = array_map('doubleval', $arg); |
|
648 | + } else if ($type == 'lb') { |
|
649 | + $result = array_map(array($this, 'formatTableName'), $arg); |
|
650 | + } else if ($type == 'll') { |
|
651 | + $result = $arg; |
|
652 | + } else if ($type == 'lt') { |
|
653 | + $result = array_map(array($this, 'escape'), array_map(array($this, 'parseTS'), $arg)); |
|
654 | + } else if ($type == '?') { |
|
655 | + $result = $this->sanitize($arg); |
|
656 | + } else { |
|
657 | + $this->nonSQLError("Badly formatted SQL query: Invalid MeekroDB param $type"); |
|
658 | + } |
|
616 | 659 | |
617 | - if (is_array($result)) $result = '('.implode(',', $result).')'; |
|
660 | + if (is_array($result)) { |
|
661 | + $result = '('.implode(',', $result).')'; |
|
662 | + } |
|
618 | 663 | |
619 | 664 | $query .= $result; |
620 | 665 | } |
@@ -720,23 +765,33 @@ discard block |
||
720 | 765 | $this->affected_rows = $db->affected_rows; |
721 | 766 | |
722 | 767 | // mysqli_result->num_rows won't initially show correct results for unbuffered data |
723 | - if ($is_buffered && ($result instanceof MySQLi_Result)) $this->num_rows = $result->num_rows; |
|
724 | - else $this->num_rows = null; |
|
768 | + if ($is_buffered && ($result instanceof MySQLi_Result)) { |
|
769 | + $this->num_rows = $result->num_rows; |
|
770 | + } else { |
|
771 | + $this->num_rows = null; |
|
772 | + } |
|
725 | 773 | |
726 | - if ($row_type == 'raw' || !($result instanceof MySQLi_Result)) return $result; |
|
774 | + if ($row_type == 'raw' || !($result instanceof MySQLi_Result)) { |
|
775 | + return $result; |
|
776 | + } |
|
727 | 777 | |
728 | 778 | $return = array(); |
729 | 779 | |
730 | 780 | if ($full_names) { |
731 | 781 | $infos = array(); |
732 | 782 | foreach ($result->fetch_fields() as $info) { |
733 | - if (strlen($info->table)) $infos[] = $info->table.'.'.$info->name; |
|
734 | - else $infos[] = $info->name; |
|
783 | + if (strlen($info->table)) { |
|
784 | + $infos[] = $info->table.'.'.$info->name; |
|
785 | + } else { |
|
786 | + $infos[] = $info->name; |
|
787 | + } |
|
735 | 788 | } |
736 | 789 | } |
737 | 790 | |
738 | 791 | while ($row = ($row_type == 'assoc' ? $result->fetch_assoc() : $result->fetch_row())) { |
739 | - if ($full_names) $row = array_combine($infos, $row); |
|
792 | + if ($full_names) { |
|
793 | + $row = array_combine($infos, $row); |
|
794 | + } |
|
740 | 795 | $return[] = $row; |
741 | 796 | } |
742 | 797 | |
@@ -929,7 +984,9 @@ discard block |
||
929 | 984 | DB::startTransaction(); |
930 | 985 | } |
931 | 986 | function __destruct() { |
932 | - if (!$this->committed) DB::rollback(); |
|
987 | + if (!$this->committed) { |
|
988 | + DB::rollback(); |
|
989 | + } |
|
933 | 990 | } |
934 | 991 | function commit() { |
935 | 992 | DB::commit(); |
@@ -963,10 +1020,14 @@ discard block |
||
963 | 1020 | |
964 | 1021 | $R = array(); |
965 | 1022 | foreach ($array as $obj) { |
966 | - if (!array_key_exists($field, $obj)) die("verticalSlice: array doesn't have requested field\n"); |
|
1023 | + if (!array_key_exists($field, $obj)) { |
|
1024 | + die("verticalSlice: array doesn't have requested field\n"); |
|
1025 | + } |
|
967 | 1026 | |
968 | 1027 | if ($keyfield) { |
969 | - if (!array_key_exists($keyfield, $obj)) die("verticalSlice: array doesn't have requested field\n"); |
|
1028 | + if (!array_key_exists($keyfield, $obj)) { |
|
1029 | + die("verticalSlice: array doesn't have requested field\n"); |
|
1030 | + } |
|
970 | 1031 | $R[$obj[$keyfield]] = $obj[$field]; |
971 | 1032 | } else { |
972 | 1033 | $R[] = $obj[$field]; |
@@ -990,7 +1051,9 @@ discard block |
||
990 | 1051 | $target = & $R; |
991 | 1052 | |
992 | 1053 | foreach ($fields as $field) { |
993 | - if (!array_key_exists($field, $obj)) die("reIndex: array doesn't have requested field\n"); |
|
1054 | + if (!array_key_exists($field, $obj)) { |
|
1055 | + die("reIndex: array doesn't have requested field\n"); |
|
1056 | + } |
|
994 | 1057 | |
995 | 1058 | $nextkey = $obj[$field]; |
996 | 1059 | $target = & $target[$nextkey]; |
@@ -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 | /** |
@@ -291,12 +297,20 @@ discard block |
||
291 | 297 | 'otp'=>$ret['otp'], |
292 | 298 | 'nonce'=>md5(uniqid(rand()))); |
293 | 299 | /* Take care of protocol version 2 parameters */ |
294 | - if ($use_timestamp) $params['timestamp'] = 1; |
|
295 | - if ($sl) $params['sl'] = $sl; |
|
296 | - if ($timeout) $params['timeout'] = $timeout; |
|
300 | + if ($use_timestamp) { |
|
301 | + $params['timestamp'] = 1; |
|
302 | + } |
|
303 | + if ($sl) { |
|
304 | + $params['sl'] = $sl; |
|
305 | + } |
|
306 | + if ($timeout) { |
|
307 | + $params['timeout'] = $timeout; |
|
308 | + } |
|
297 | 309 | ksort($params); |
298 | 310 | $parameters = ''; |
299 | - foreach ($params as $p=>$v) $parameters .= "&".$p."=".$v; |
|
311 | + foreach ($params as $p=>$v) { |
|
312 | + $parameters .= "&".$p."=".$v; |
|
313 | + } |
|
300 | 314 | $parameters = ltrim($parameters, "&"); |
301 | 315 | |
302 | 316 | /* Generate signature. */ |
@@ -401,7 +415,9 @@ discard block |
||
401 | 415 | $check = Null; |
402 | 416 | foreach ($parameters as $param) { |
403 | 417 | if (array_key_exists($param, $response)) { |
404 | - if ($check) $check = $check.'&'; |
|
418 | + if ($check) { |
|
419 | + $check = $check.'&'; |
|
420 | + } |
|
405 | 421 | $check = $check.$param.'='.$response[$param]; |
406 | 422 | } |
407 | 423 | } |
@@ -3,6 +3,7 @@ |
||
3 | 3 | $name = 'Courier-Oblique'; |
4 | 4 | $up = -100; |
5 | 5 | $ut = 50; |
6 | -for ($i = 0; $i <= 255; $i++) |
|
6 | +for ($i = 0; $i <= 255; $i++) { |
|
7 | 7 | $cw[chr($i)] = 600; |
8 | +} |
|
8 | 9 | ?> |
@@ -4,6 +4,6 @@ |
||
4 | 4 | $up = -100; |
5 | 5 | $ut = 50; |
6 | 6 | for ($i = 0; $i <= 255; $i++) { |
7 | - $cw[chr($i)] = 600; |
|
7 | + $cw[chr($i)] = 600; |
|
8 | 8 | } |
9 | 9 | ?> |
@@ -115,181 +115,181 @@ |
||
115 | 115 | $record['offset'] = $this->read_ulong(); |
116 | 116 | $record['length'] = $this->read_ulong(); |
117 | 117 | $this->tables[$record['tag']] = $record; |
118 | - } |
|
119 | - } |
|
120 | - |
|
121 | - |
|
122 | - /** |
|
123 | - * @param integer[] $x |
|
124 | - * @param integer[] $y |
|
125 | - */ |
|
126 | - function sub32($x, $y) { |
|
127 | - $xlo = $x[1]; |
|
128 | - $xhi = $x[0]; |
|
129 | - $ylo = $y[1]; |
|
130 | - $yhi = $y[0]; |
|
131 | - if ($ylo > $xlo) { $xlo += 1 << 16; $yhi += 1; } |
|
132 | - $reslo = $xlo - $ylo; |
|
133 | - if ($yhi > $xhi) { $xhi += 1 << 16; } |
|
134 | - $reshi = $xhi - $yhi; |
|
135 | - $reshi = $reshi & 0xFFFF; |
|
136 | - return array($reshi, $reslo); |
|
137 | - } |
|
138 | - |
|
139 | - function calcChecksum($data) { |
|
140 | - if (strlen($data) % 4) { $data .= str_repeat("\0", (4 - (strlen($data) % 4))); } |
|
141 | - $hi = 0x0000; |
|
142 | - $lo = 0x0000; |
|
143 | - for ($i = 0; $i < strlen($data); $i += 4) { |
|
144 | - $hi += (ord($data[$i]) << 8) + ord($data[$i + 1]); |
|
145 | - $lo += (ord($data[$i + 2]) << 8) + ord($data[$i + 3]); |
|
146 | - $hi += $lo >> 16; |
|
147 | - $lo = $lo & 0xFFFF; |
|
148 | - $hi = $hi & 0xFFFF; |
|
149 | - } |
|
150 | - return array($hi, $lo); |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * @param string $tag |
|
155 | - */ |
|
156 | - function get_table_pos($tag) { |
|
157 | - $offset = $this->tables[$tag]['offset']; |
|
158 | - $length = $this->tables[$tag]['length']; |
|
159 | - return array($offset, $length); |
|
160 | - } |
|
161 | - |
|
162 | - function seek($pos) { |
|
163 | - $this->_pos = $pos; |
|
164 | - fseek($this->fh, $this->_pos); |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * @param integer $delta |
|
169 | - */ |
|
170 | - function skip($delta) { |
|
171 | - $this->_pos = $this->_pos + $delta; |
|
172 | - fseek($this->fh, $this->_pos); |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * @param string $tag |
|
177 | - */ |
|
178 | - function seek_table($tag, $offset_in_table = 0) { |
|
179 | - $tpos = $this->get_table_pos($tag); |
|
180 | - $this->_pos = $tpos[0] + $offset_in_table; |
|
181 | - fseek($this->fh, $this->_pos); |
|
182 | - return $this->_pos; |
|
183 | - } |
|
184 | - |
|
185 | - function read_tag() { |
|
186 | - $this->_pos += 4; |
|
187 | - return fread($this->fh, 4); |
|
188 | - } |
|
189 | - |
|
190 | - function read_short() { |
|
191 | - $this->_pos += 2; |
|
192 | - $s = fread($this->fh, 2); |
|
193 | - $a = (ord($s[0]) << 8) + ord($s[1]); |
|
194 | - if ($a & (1 << 15)) { $a = ($a - (1 << 16)); } |
|
195 | - return $a; |
|
196 | - } |
|
197 | - |
|
198 | - function unpack_short($s) { |
|
199 | - $a = (ord($s[0]) << 8) + ord($s[1]); |
|
200 | - if ($a & (1 << 15)) { |
|
201 | - $a = ($a - (1 << 16)); |
|
202 | - } |
|
203 | - return $a; |
|
204 | - } |
|
205 | - |
|
206 | - function read_ushort() { |
|
207 | - $this->_pos += 2; |
|
208 | - $s = fread($this->fh, 2); |
|
209 | - return (ord($s[0]) << 8) + ord($s[1]); |
|
210 | - } |
|
211 | - |
|
212 | - function read_ulong() { |
|
213 | - $this->_pos += 4; |
|
214 | - $s = fread($this->fh, 4); |
|
215 | - // if large uInt32 as an integer, PHP converts it to -ve |
|
216 | - return (ord($s[0]) * 16777216) + (ord($s[1]) << 16) + (ord($s[2]) << 8) + ord($s[3]); // 16777216 = 1<<24 |
|
217 | - } |
|
218 | - |
|
219 | - function get_ushort($pos) { |
|
220 | - fseek($this->fh, $pos); |
|
221 | - $s = fread($this->fh, 2); |
|
222 | - return (ord($s[0]) << 8) + ord($s[1]); |
|
223 | - } |
|
224 | - |
|
225 | - function get_ulong($pos) { |
|
226 | - fseek($this->fh, $pos); |
|
227 | - $s = fread($this->fh, 4); |
|
228 | - // iF large uInt32 as an integer, PHP converts it to -ve |
|
229 | - return (ord($s[0]) * 16777216) + (ord($s[1]) << 16) + (ord($s[2]) << 8) + ord($s[3]); // 16777216 = 1<<24 |
|
230 | - } |
|
231 | - |
|
232 | - function pack_short($val) { |
|
233 | - if ($val < 0) { |
|
234 | - $val = abs($val); |
|
235 | - $val = ~$val; |
|
236 | - $val += 1; |
|
237 | - } |
|
238 | - return pack("n", $val); |
|
239 | - } |
|
240 | - |
|
241 | - /** |
|
242 | - * @param string $value |
|
243 | - */ |
|
244 | - function splice($stream, $offset, $value) { |
|
245 | - return substr($stream, 0, $offset).$value.substr($stream, $offset + strlen($value)); |
|
246 | - } |
|
247 | - |
|
248 | - /** |
|
249 | - * @param string|null $stream |
|
250 | - * @param integer $offset |
|
251 | - */ |
|
252 | - function _set_ushort($stream, $offset, $value) { |
|
253 | - $up = pack("n", $value); |
|
254 | - return $this->splice($stream, $offset, $up); |
|
255 | - } |
|
256 | - |
|
257 | - function _set_short($stream, $offset, $val) { |
|
258 | - if ($val < 0) { |
|
259 | - $val = abs($val); |
|
260 | - $val = ~$val; |
|
261 | - $val += 1; |
|
262 | - } |
|
263 | - $up = pack("n", $val); |
|
264 | - return $this->splice($stream, $offset, $up); |
|
265 | - } |
|
266 | - |
|
267 | - function get_chunk($pos, $length) { |
|
268 | - fseek($this->fh, $pos); |
|
269 | - if ($length < 1) { return ''; } |
|
270 | - return (fread($this->fh, $length)); |
|
271 | - } |
|
272 | - |
|
273 | - /** |
|
274 | - * @param string $tag |
|
275 | - */ |
|
276 | - function get_table($tag) { |
|
277 | - list($pos, $length) = $this->get_table_pos($tag); |
|
278 | - if ($length == 0) { die('Truetype font ('.$this->filename.'): error reading table: '.$tag); } |
|
279 | - fseek($this->fh, $pos); |
|
280 | - return (fread($this->fh, $length)); |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * @param string $tag |
|
285 | - * @param null|string $data |
|
286 | - */ |
|
287 | - function add($tag, $data) { |
|
288 | - if ($tag == 'head') { |
|
289 | - $data = $this->splice($data, 8, "\0\0\0\0"); |
|
290 | - } |
|
291 | - $this->otables[$tag] = $data; |
|
292 | - } |
|
118 | + } |
|
119 | + } |
|
120 | + |
|
121 | + |
|
122 | + /** |
|
123 | + * @param integer[] $x |
|
124 | + * @param integer[] $y |
|
125 | + */ |
|
126 | + function sub32($x, $y) { |
|
127 | + $xlo = $x[1]; |
|
128 | + $xhi = $x[0]; |
|
129 | + $ylo = $y[1]; |
|
130 | + $yhi = $y[0]; |
|
131 | + if ($ylo > $xlo) { $xlo += 1 << 16; $yhi += 1; } |
|
132 | + $reslo = $xlo - $ylo; |
|
133 | + if ($yhi > $xhi) { $xhi += 1 << 16; } |
|
134 | + $reshi = $xhi - $yhi; |
|
135 | + $reshi = $reshi & 0xFFFF; |
|
136 | + return array($reshi, $reslo); |
|
137 | + } |
|
138 | + |
|
139 | + function calcChecksum($data) { |
|
140 | + if (strlen($data) % 4) { $data .= str_repeat("\0", (4 - (strlen($data) % 4))); } |
|
141 | + $hi = 0x0000; |
|
142 | + $lo = 0x0000; |
|
143 | + for ($i = 0; $i < strlen($data); $i += 4) { |
|
144 | + $hi += (ord($data[$i]) << 8) + ord($data[$i + 1]); |
|
145 | + $lo += (ord($data[$i + 2]) << 8) + ord($data[$i + 3]); |
|
146 | + $hi += $lo >> 16; |
|
147 | + $lo = $lo & 0xFFFF; |
|
148 | + $hi = $hi & 0xFFFF; |
|
149 | + } |
|
150 | + return array($hi, $lo); |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * @param string $tag |
|
155 | + */ |
|
156 | + function get_table_pos($tag) { |
|
157 | + $offset = $this->tables[$tag]['offset']; |
|
158 | + $length = $this->tables[$tag]['length']; |
|
159 | + return array($offset, $length); |
|
160 | + } |
|
161 | + |
|
162 | + function seek($pos) { |
|
163 | + $this->_pos = $pos; |
|
164 | + fseek($this->fh, $this->_pos); |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * @param integer $delta |
|
169 | + */ |
|
170 | + function skip($delta) { |
|
171 | + $this->_pos = $this->_pos + $delta; |
|
172 | + fseek($this->fh, $this->_pos); |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * @param string $tag |
|
177 | + */ |
|
178 | + function seek_table($tag, $offset_in_table = 0) { |
|
179 | + $tpos = $this->get_table_pos($tag); |
|
180 | + $this->_pos = $tpos[0] + $offset_in_table; |
|
181 | + fseek($this->fh, $this->_pos); |
|
182 | + return $this->_pos; |
|
183 | + } |
|
184 | + |
|
185 | + function read_tag() { |
|
186 | + $this->_pos += 4; |
|
187 | + return fread($this->fh, 4); |
|
188 | + } |
|
189 | + |
|
190 | + function read_short() { |
|
191 | + $this->_pos += 2; |
|
192 | + $s = fread($this->fh, 2); |
|
193 | + $a = (ord($s[0]) << 8) + ord($s[1]); |
|
194 | + if ($a & (1 << 15)) { $a = ($a - (1 << 16)); } |
|
195 | + return $a; |
|
196 | + } |
|
197 | + |
|
198 | + function unpack_short($s) { |
|
199 | + $a = (ord($s[0]) << 8) + ord($s[1]); |
|
200 | + if ($a & (1 << 15)) { |
|
201 | + $a = ($a - (1 << 16)); |
|
202 | + } |
|
203 | + return $a; |
|
204 | + } |
|
205 | + |
|
206 | + function read_ushort() { |
|
207 | + $this->_pos += 2; |
|
208 | + $s = fread($this->fh, 2); |
|
209 | + return (ord($s[0]) << 8) + ord($s[1]); |
|
210 | + } |
|
211 | + |
|
212 | + function read_ulong() { |
|
213 | + $this->_pos += 4; |
|
214 | + $s = fread($this->fh, 4); |
|
215 | + // if large uInt32 as an integer, PHP converts it to -ve |
|
216 | + return (ord($s[0]) * 16777216) + (ord($s[1]) << 16) + (ord($s[2]) << 8) + ord($s[3]); // 16777216 = 1<<24 |
|
217 | + } |
|
218 | + |
|
219 | + function get_ushort($pos) { |
|
220 | + fseek($this->fh, $pos); |
|
221 | + $s = fread($this->fh, 2); |
|
222 | + return (ord($s[0]) << 8) + ord($s[1]); |
|
223 | + } |
|
224 | + |
|
225 | + function get_ulong($pos) { |
|
226 | + fseek($this->fh, $pos); |
|
227 | + $s = fread($this->fh, 4); |
|
228 | + // iF large uInt32 as an integer, PHP converts it to -ve |
|
229 | + return (ord($s[0]) * 16777216) + (ord($s[1]) << 16) + (ord($s[2]) << 8) + ord($s[3]); // 16777216 = 1<<24 |
|
230 | + } |
|
231 | + |
|
232 | + function pack_short($val) { |
|
233 | + if ($val < 0) { |
|
234 | + $val = abs($val); |
|
235 | + $val = ~$val; |
|
236 | + $val += 1; |
|
237 | + } |
|
238 | + return pack("n", $val); |
|
239 | + } |
|
240 | + |
|
241 | + /** |
|
242 | + * @param string $value |
|
243 | + */ |
|
244 | + function splice($stream, $offset, $value) { |
|
245 | + return substr($stream, 0, $offset).$value.substr($stream, $offset + strlen($value)); |
|
246 | + } |
|
247 | + |
|
248 | + /** |
|
249 | + * @param string|null $stream |
|
250 | + * @param integer $offset |
|
251 | + */ |
|
252 | + function _set_ushort($stream, $offset, $value) { |
|
253 | + $up = pack("n", $value); |
|
254 | + return $this->splice($stream, $offset, $up); |
|
255 | + } |
|
256 | + |
|
257 | + function _set_short($stream, $offset, $val) { |
|
258 | + if ($val < 0) { |
|
259 | + $val = abs($val); |
|
260 | + $val = ~$val; |
|
261 | + $val += 1; |
|
262 | + } |
|
263 | + $up = pack("n", $val); |
|
264 | + return $this->splice($stream, $offset, $up); |
|
265 | + } |
|
266 | + |
|
267 | + function get_chunk($pos, $length) { |
|
268 | + fseek($this->fh, $pos); |
|
269 | + if ($length < 1) { return ''; } |
|
270 | + return (fread($this->fh, $length)); |
|
271 | + } |
|
272 | + |
|
273 | + /** |
|
274 | + * @param string $tag |
|
275 | + */ |
|
276 | + function get_table($tag) { |
|
277 | + list($pos, $length) = $this->get_table_pos($tag); |
|
278 | + if ($length == 0) { die('Truetype font ('.$this->filename.'): error reading table: '.$tag); } |
|
279 | + fseek($this->fh, $pos); |
|
280 | + return (fread($this->fh, $length)); |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * @param string $tag |
|
285 | + * @param null|string $data |
|
286 | + */ |
|
287 | + function add($tag, $data) { |
|
288 | + if ($tag == 'head') { |
|
289 | + $data = $this->splice($data, 8, "\0\0\0\0"); |
|
290 | + } |
|
291 | + $this->otables[$tag] = $data; |
|
292 | + } |
|
293 | 293 | |
294 | 294 | |
295 | 295 |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | var $maxStrLenRead; |
71 | 71 | |
72 | 72 | function __construct() { |
73 | - $this->maxStrLenRead = 200000; // Maximum size of glyf table to read in as string (otherwise reads each glyph from file) |
|
73 | + $this->maxStrLenRead = 200000; // Maximum size of glyf table to read in as string (otherwise reads each glyph from file) |
|
74 | 74 | } |
75 | 75 | |
76 | 76 | |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | */ |
80 | 80 | function getMetrics($file) { |
81 | 81 | $this->filename = $file; |
82 | - $this->fh = fopen($file,'rb') or die('Can\'t open file ' . $file); |
|
82 | + $this->fh = fopen($file, 'rb') or die('Can\'t open file '.$file); |
|
83 | 83 | $this->_pos = 0; |
84 | 84 | $this->charWidths = ''; |
85 | 85 | $this->glyphPos = array(); |
@@ -1037,7 +1037,7 @@ discard block |
||
1037 | 1037 | /** |
1038 | 1038 | * @param integer $unicode_cmap_offset |
1039 | 1039 | */ |
1040 | - function getCMAP4($unicode_cmap_offset, &$glyphToChar, &$charToGlyph ) { |
|
1040 | + function getCMAP4($unicode_cmap_offset, &$glyphToChar, &$charToGlyph) { |
|
1041 | 1041 | $this->maxUniChar = 0; |
1042 | 1042 | $this->seek($unicode_cmap_offset + 2); |
1043 | 1043 | $length = $this->read_ushort(); |
@@ -90,12 +90,15 @@ discard block |
||
90 | 90 | $this->descent = 0; |
91 | 91 | $this->TTCFonts = array(); |
92 | 92 | $this->version = $version = $this->read_ulong(); |
93 | - if ($version == 0x4F54544F) |
|
94 | - die("Postscript outlines are not supported"); |
|
95 | - if ($version == 0x74746366) |
|
96 | - die("ERROR - TrueType Fonts Collections not supported"); |
|
97 | - if (!in_array($version, array(0x00010000, 0x74727565))) |
|
98 | - die("Not a TrueType font: version=".$version); |
|
93 | + if ($version == 0x4F54544F) { |
|
94 | + die("Postscript outlines are not supported"); |
|
95 | + } |
|
96 | + if ($version == 0x74746366) { |
|
97 | + die("ERROR - TrueType Fonts Collections not supported"); |
|
98 | + } |
|
99 | + if (!in_array($version, array(0x00010000, 0x74727565))) { |
|
100 | + die("Not a TrueType font: version=".$version); |
|
101 | + } |
|
99 | 102 | $this->readTableDirectory(); |
100 | 103 | $this->extractInfo(); |
101 | 104 | fclose($this->fh); |
@@ -322,13 +325,16 @@ discard block |
||
322 | 325 | $nameId = $this->read_ushort(); |
323 | 326 | $length = $this->read_ushort(); |
324 | 327 | $offset = $this->read_ushort(); |
325 | - if (!in_array($nameId, $K)) continue; |
|
328 | + if (!in_array($nameId, $K)) { |
|
329 | + continue; |
|
330 | + } |
|
326 | 331 | $N = ''; |
327 | 332 | if ($platformId == 3 && $encodingId == 1 && $languageId == 0x409) { // Microsoft, Unicode, US English, PS Name |
328 | 333 | $opos = $this->_pos; |
329 | 334 | $this->seek($string_data_offset + $offset); |
330 | - if ($length % 2 != 0) |
|
331 | - die("PostScript name is UTF-16BE string of odd length"); |
|
335 | + if ($length % 2 != 0) { |
|
336 | + die("PostScript name is UTF-16BE string of odd length"); |
|
337 | + } |
|
332 | 338 | $length /= 2; |
333 | 339 | $N = ''; |
334 | 340 | while ($length > 0) { |
@@ -347,19 +353,23 @@ discard block |
||
347 | 353 | if ($N && $names[$nameId] == '') { |
348 | 354 | $names[$nameId] = $N; |
349 | 355 | $nameCount -= 1; |
350 | - if ($nameCount == 0) break; |
|
356 | + if ($nameCount == 0) { |
|
357 | + break; |
|
358 | + } |
|
351 | 359 | } |
352 | 360 | } |
353 | - if ($names[6]) |
|
354 | - $psName = $names[6]; |
|
355 | - else if ($names[4]) |
|
356 | - $psName = preg_replace('/ /', '-', $names[4]); |
|
357 | - else if ($names[1]) |
|
358 | - $psName = preg_replace('/ /', '-', $names[1]); |
|
359 | - else |
|
360 | - $psName = ''; |
|
361 | - if (!$psName) |
|
362 | - die("Could not find PostScript font name"); |
|
361 | + if ($names[6]) { |
|
362 | + $psName = $names[6]; |
|
363 | + } else if ($names[4]) { |
|
364 | + $psName = preg_replace('/ /', '-', $names[4]); |
|
365 | + } else if ($names[1]) { |
|
366 | + $psName = preg_replace('/ /', '-', $names[1]); |
|
367 | + } else { |
|
368 | + $psName = ''; |
|
369 | + } |
|
370 | + if (!$psName) { |
|
371 | + die("Could not find PostScript font name"); |
|
372 | + } |
|
363 | 373 | $this->name = $psName; |
364 | 374 | if ($names[1]) { $this->familyName = $names[1]; } else { $this->familyName = $psName; } |
365 | 375 | if ($names[2]) { $this->styleName = $names[2]; } else { $this->styleName = 'Regular'; } |
@@ -423,21 +433,27 @@ discard block |
||
423 | 433 | $this->skip(26); |
424 | 434 | $sTypoAscender = $this->read_short(); |
425 | 435 | $sTypoDescender = $this->read_short(); |
426 | - if (!$this->ascent) $this->ascent = ($sTypoAscender * $scale); |
|
427 | - if (!$this->descent) $this->descent = ($sTypoDescender * $scale); |
|
436 | + if (!$this->ascent) { |
|
437 | + $this->ascent = ($sTypoAscender * $scale); |
|
438 | + } |
|
439 | + if (!$this->descent) { |
|
440 | + $this->descent = ($sTypoDescender * $scale); |
|
441 | + } |
|
428 | 442 | if ($version > 1) { |
429 | 443 | $this->skip(16); |
430 | 444 | $sCapHeight = $this->read_short(); |
431 | 445 | $this->capHeight = ($sCapHeight * $scale); |
432 | - } |
|
433 | - else { |
|
446 | + } else { |
|
434 | 447 | $this->capHeight = $this->ascent; |
435 | 448 | } |
436 | - } |
|
437 | - else { |
|
449 | + } else { |
|
438 | 450 | $usWeightClass = 500; |
439 | - if (!$this->ascent) $this->ascent = ($yMax * $scale); |
|
440 | - if (!$this->descent) $this->descent = ($yMin * $scale); |
|
451 | + if (!$this->ascent) { |
|
452 | + $this->ascent = ($yMax * $scale); |
|
453 | + } |
|
454 | + if (!$this->descent) { |
|
455 | + $this->descent = ($yMin * $scale); |
|
456 | + } |
|
441 | 457 | $this->capHeight = $this->ascent; |
442 | 458 | } |
443 | 459 | $this->stemV = 50 + intval(pow(($usWeightClass / 65.0), 2)); |
@@ -454,12 +470,15 @@ discard block |
||
454 | 470 | |
455 | 471 | $this->flags = 4; |
456 | 472 | |
457 | - if ($this->italicAngle != 0) |
|
458 | - $this->flags = $this->flags | 64; |
|
459 | - if ($usWeightClass >= 600) |
|
460 | - $this->flags = $this->flags | 262144; |
|
461 | - if ($isFixedPitch) |
|
462 | - $this->flags = $this->flags | 1; |
|
473 | + if ($this->italicAngle != 0) { |
|
474 | + $this->flags = $this->flags | 64; |
|
475 | + } |
|
476 | + if ($usWeightClass >= 600) { |
|
477 | + $this->flags = $this->flags | 262144; |
|
478 | + } |
|
479 | + if ($isFixedPitch) { |
|
480 | + $this->flags = $this->flags | 1; |
|
481 | + } |
|
463 | 482 | |
464 | 483 | /////////////////////////////////// |
465 | 484 | // hhea - Horizontal header table |
@@ -498,14 +517,17 @@ discard block |
||
498 | 517 | if (($platformID == 3 && $encodingID == 1) || $platformID == 0) { // Microsoft, Unicode |
499 | 518 | $format = $this->get_ushort($cmap_offset + $offset); |
500 | 519 | if ($format == 4) { |
501 | - if (!$unicode_cmap_offset) $unicode_cmap_offset = $cmap_offset + $offset; |
|
520 | + if (!$unicode_cmap_offset) { |
|
521 | + $unicode_cmap_offset = $cmap_offset + $offset; |
|
522 | + } |
|
502 | 523 | break; |
503 | 524 | } |
504 | 525 | } |
505 | 526 | $this->seek($save_pos); |
506 | 527 | } |
507 | - if (!$unicode_cmap_offset) |
|
508 | - die('Font ('.$this->filename.') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); |
|
528 | + if (!$unicode_cmap_offset) { |
|
529 | + die('Font ('.$this->filename.') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); |
|
530 | + } |
|
509 | 531 | |
510 | 532 | |
511 | 533 | $glyphToChar = array(); |
@@ -589,8 +611,9 @@ discard block |
||
589 | 611 | $this->seek($save_pos); |
590 | 612 | } |
591 | 613 | |
592 | - if (!$unicode_cmap_offset) |
|
593 | - die('Font ('.$this->filename.') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); |
|
614 | + if (!$unicode_cmap_offset) { |
|
615 | + die('Font ('.$this->filename.') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); |
|
616 | + } |
|
594 | 617 | |
595 | 618 | |
596 | 619 | $glyphToChar = array(); |
@@ -775,10 +798,12 @@ discard block |
||
775 | 798 | $glyphLen = $this->glyphPos[$originalGlyphIdx + 1] - $glyphPos; |
776 | 799 | if ($glyfLength < $this->maxStrLenRead) { |
777 | 800 | $data = substr($glyphData, $glyphPos, $glyphLen); |
778 | - } |
|
779 | - else { |
|
780 | - if ($glyphLen > 0) $data = $this->get_chunk($glyfOffset + $glyphPos, $glyphLen); |
|
781 | - else $data = ''; |
|
801 | + } else { |
|
802 | + if ($glyphLen > 0) { |
|
803 | + $data = $this->get_chunk($glyfOffset + $glyphPos, $glyphLen); |
|
804 | + } else { |
|
805 | + $data = ''; |
|
806 | + } |
|
782 | 807 | } |
783 | 808 | |
784 | 809 | if ($glyphLen > 0) { |
@@ -824,8 +849,7 @@ discard block |
||
824 | 849 | if ((($pos + 1) >> 1) > 0xFFFF) { |
825 | 850 | $indexToLocFormat = 1; // long format |
826 | 851 | foreach ($offsets AS $offset) { $locastr .= pack("N", $offset); } |
827 | - } |
|
828 | - else { |
|
852 | + } else { |
|
829 | 853 | $indexToLocFormat = 0; // short format |
830 | 854 | foreach ($offsets AS $offset) { $locastr .= pack("n", ($offset / 2)); } |
831 | 855 | } |
@@ -931,14 +955,12 @@ discard block |
||
931 | 955 | if (($numberOfHMetrics * 4) < $this->maxStrLenRead) { |
932 | 956 | $data = $this->get_chunk($start, ($numberOfHMetrics * 4)); |
933 | 957 | $arr = unpack("n*", $data); |
934 | - } |
|
935 | - else { $this->seek($start); } |
|
958 | + } else { $this->seek($start); } |
|
936 | 959 | for ($glyph = 0; $glyph < $numberOfHMetrics; $glyph++) { |
937 | 960 | |
938 | 961 | if (($numberOfHMetrics * 4) < $this->maxStrLenRead) { |
939 | 962 | $aw = $arr[($glyph * 2) + 1]; |
940 | - } |
|
941 | - else { |
|
963 | + } else { |
|
942 | 964 | $aw = $this->read_ushort(); |
943 | 965 | $lsb = $this->read_ushort(); |
944 | 966 | } |
@@ -996,8 +1018,7 @@ discard block |
||
996 | 1018 | if ($gid < $numberOfHMetrics) { |
997 | 1019 | $this->seek($start + ($gid * 4)); |
998 | 1020 | $hm = fread($this->fh, 4); |
999 | - } |
|
1000 | - else { |
|
1021 | + } else { |
|
1001 | 1022 | $this->seek($start + (($numberOfHMetrics - 1) * 4)); |
1002 | 1023 | $hm = fread($this->fh, 2); |
1003 | 1024 | $this->seek($start + ($numberOfHMetrics * 2) + ($gid * 2)); |
@@ -1019,16 +1040,15 @@ discard block |
||
1019 | 1040 | for ($n = 0; $n <= $numGlyphs; $n++) { |
1020 | 1041 | $this->glyphPos[] = ($arr[$n + 1] * 2); |
1021 | 1042 | } |
1022 | - } |
|
1023 | - else if ($indexToLocFormat == 1) { |
|
1043 | + } else if ($indexToLocFormat == 1) { |
|
1024 | 1044 | $data = $this->get_chunk($start, ($numGlyphs * 4) + 4); |
1025 | 1045 | $arr = unpack("N*", $data); |
1026 | 1046 | for ($n = 0; $n <= $numGlyphs; $n++) { |
1027 | 1047 | $this->glyphPos[] = ($arr[$n + 1]); |
1028 | 1048 | } |
1049 | + } else { |
|
1050 | + die('Unknown location table format '.$indexToLocFormat); |
|
1029 | 1051 | } |
1030 | - else |
|
1031 | - die('Unknown location table format '.$indexToLocFormat); |
|
1032 | 1052 | } |
1033 | 1053 | |
1034 | 1054 | |
@@ -1060,17 +1080,18 @@ discard block |
||
1060 | 1080 | for ($n = 0; $n < $segCount; $n++) { |
1061 | 1081 | $endpoint = ($endCount[$n] + 1); |
1062 | 1082 | for ($unichar = $startCount[$n]; $unichar < $endpoint; $unichar++) { |
1063 | - if ($idRangeOffset[$n] == 0) |
|
1064 | - $glyph = ($unichar + $idDelta[$n]) & 0xFFFF; |
|
1065 | - else { |
|
1083 | + if ($idRangeOffset[$n] == 0) { |
|
1084 | + $glyph = ($unichar + $idDelta[$n]) & 0xFFFF; |
|
1085 | + } else { |
|
1066 | 1086 | $offset = ($unichar - $startCount[$n]) * 2 + $idRangeOffset[$n]; |
1067 | 1087 | $offset = $idRangeOffset_start + 2 * $n + $offset; |
1068 | - if ($offset >= $limit) |
|
1069 | - $glyph = 0; |
|
1070 | - else { |
|
1088 | + if ($offset >= $limit) { |
|
1089 | + $glyph = 0; |
|
1090 | + } else { |
|
1071 | 1091 | $glyph = $this->get_ushort($offset); |
1072 | - if ($glyph != 0) |
|
1073 | - $glyph = ($glyph + $idDelta[$n]) & 0xFFFF; |
|
1092 | + if ($glyph != 0) { |
|
1093 | + $glyph = ($glyph + $idDelta[$n]) & 0xFFFF; |
|
1094 | + } |
|
1074 | 1095 | } |
1075 | 1096 | } |
1076 | 1097 | $charToGlyph[$unichar] = $glyph; |
@@ -1101,8 +1122,7 @@ discard block |
||
1101 | 1122 | // Header |
1102 | 1123 | if (_TTF_MAC_HEADER) { |
1103 | 1124 | $stm .= (pack("Nnnnn", 0x74727565, $numTables, $searchRange, $entrySelector, $rangeShift)); // Mac |
1104 | - } |
|
1105 | - else { |
|
1125 | + } else { |
|
1106 | 1126 | $stm .= (pack("Nnnnn", 0x00010000, $numTables, $searchRange, $entrySelector, $rangeShift)); // Windows |
1107 | 1127 | } |
1108 | 1128 |