| @@ -1588,7 +1588,7 @@ discard block | ||
| 1588 | 1588 | * | 
| 1589 | 1589 | * @param array ref $root | 
| 1590 | 1590 | * @param string $path | 
| 1591 | - * @param object $asn1 | |
| 1591 | + * @param ASN1 $asn1 | |
| 1592 | 1592 | * @access private | 
| 1593 | 1593 | */ | 
| 1594 | 1594 | function _mapInExtensions(&$root, $path, $asn1) | 
| @@ -1638,7 +1638,7 @@ discard block | ||
| 1638 | 1638 | * | 
| 1639 | 1639 | * @param array ref $root | 
| 1640 | 1640 | * @param string $path | 
| 1641 | - * @param object $asn1 | |
| 1641 | + * @param ASN1 $asn1 | |
| 1642 | 1642 | * @access private | 
| 1643 | 1643 | */ | 
| 1644 | 1644 | function _mapOutExtensions(&$root, $path, $asn1) | 
| @@ -168,6 +168,7 @@ | ||
| 168 | 168 | |
| 169 | 169 | /** | 
| 170 | 170 | * Builds a string to be encoded in a QR code | 
| 171 | + * @param string $label | |
| 171 | 172 | */ | 
| 172 | 173 | public function getQRText($label, $secret) | 
| 173 | 174 |      { | 
| @@ -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 | |
| @@ -80,11 +85,14 @@ discard block | ||
| 80 | 85 |      { | 
| 81 | 86 | $secret = ''; | 
| 82 | 87 | $bytes = ceil($bits / 5); //We use 5 bits of each byte (since we have a 32-character 'alphabet' / BASE32) | 
| 83 | - if ($requirecryptosecure && !$this->rngprovider->isCryptographicallySecure()) | |
| 84 | -            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 | + } | |
| 85 | 91 | $rnd = $this->rngprovider->getRandomBytes($bytes); | 
| 86 | - for ($i = 0; $i < $bytes; $i++) | |
| 87 | - $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 | |
| 88 | 96 | return $secret; | 
| 89 | 97 | } | 
| 90 | 98 | |
| @@ -181,23 +189,28 @@ discard block | ||
| 181 | 189 | |
| 182 | 190 | private function base32Decode($value) | 
| 183 | 191 |      { | 
| 184 | - if (strlen($value) == 0) return ''; | |
| 192 | +        if (strlen($value) == 0) { | |
| 193 | + return ''; | |
| 194 | + } | |
| 185 | 195 | |
| 186 | -        if (preg_match('/[^'.preg_quote(self::$_base32dict).']/', $value) !== 0) | |
| 187 | -            throw new TwoFactorAuthException('Invalid base32 string'); | |
| 196 | +        if (preg_match('/[^'.preg_quote(self::$_base32dict).']/', $value) !== 0) { | |
| 197 | +                    throw new TwoFactorAuthException('Invalid base32 string'); | |
| 198 | + } | |
| 188 | 199 | |
| 189 | 200 | $buffer = ''; | 
| 190 | 201 | foreach (str_split($value) as $char) | 
| 191 | 202 |          { | 
| 192 | - if ($char !== '=') | |
| 193 | - $buffer .= str_pad(decbin(self::$_base32lookup[$char]), 5, 0, STR_PAD_LEFT); | |
| 203 | +            if ($char !== '=') { | |
| 204 | + $buffer .= str_pad(decbin(self::$_base32lookup[$char]), 5, 0, STR_PAD_LEFT); | |
| 205 | + } | |
| 194 | 206 | } | 
| 195 | 207 | $length = strlen($buffer); | 
| 196 | 208 | $blocks = trim(chunk_split(substr($buffer, 0, $length - ($length % 8)), 8, ' ')); | 
| 197 | 209 | |
| 198 | 210 | $output = ''; | 
| 199 | -        foreach (explode(' ', $blocks) as $block) | |
| 200 | - $output .= chr(bindec(str_pad($block, 8, 0, STR_PAD_RIGHT))); | |
| 211 | +        foreach (explode(' ', $blocks) as $block) { | |
| 212 | + $output .= chr(bindec(str_pad($block, 8, 0, STR_PAD_RIGHT))); | |
| 213 | + } | |
| 201 | 214 | |
| 202 | 215 | return $output; | 
| 203 | 216 | } | 
| @@ -76,6 +76,7 @@ | ||
| 76 | 76 | * @throws Ex\EnvironmentIsBrokenException | 
| 77 | 77 | * @throws Ex\WrongKeyOrModifiedCiphertextException | 
| 78 | 78 | * | 
| 79 | + * @param string $password | |
| 79 | 80 | * @return Key | 
| 80 | 81 | */ | 
| 81 | 82 | public function unlockKey($password) | 
| @@ -150,6 +150,9 @@ | ||
| 150 | 150 | return array($hi, $lo); | 
| 151 | 151 | } | 
| 152 | 152 | |
| 153 | + /** | |
| 154 | + * @param string $tag | |
| 155 | + */ | |
| 153 | 156 |  	function get_table_pos($tag) { | 
| 154 | 157 | $offset = $this->tables[$tag]['offset']; | 
| 155 | 158 | $length = $this->tables[$tag]['length']; | 
| @@ -69,41 +69,41 @@ discard block | ||
| 69 | 69 | var $defaultWidth; | 
| 70 | 70 | var $maxStrLenRead; | 
| 71 | 71 | |
| 72 | -	function __construct() { | |
| 73 | - $this->maxStrLenRead = 200000; // Maximum size of glyf table to read in as string (otherwise reads each glyph from file) | |
| 74 | - } | |
| 75 | - | |
| 76 | - | |
| 77 | - /** | |
| 78 | - * @param string $file | |
| 79 | - */ | |
| 80 | -	function getMetrics($file) { | |
| 81 | - $this->filename = $file; | |
| 82 | -		$this->fh = fopen($file,'rb') or die('Can\'t open file ' . $file); | |
| 83 | - $this->_pos = 0; | |
| 84 | - $this->charWidths = ''; | |
| 85 | - $this->glyphPos = array(); | |
| 86 | - $this->charToGlyph = array(); | |
| 87 | - $this->tables = array(); | |
| 88 | - $this->otables = array(); | |
| 89 | - $this->ascent = 0; | |
| 90 | - $this->descent = 0; | |
| 91 | - $this->TTCFonts = array(); | |
| 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); | |
| 99 | - $this->readTableDirectory(); | |
| 100 | - $this->extractInfo(); | |
| 101 | - fclose($this->fh); | |
| 102 | - } | |
| 103 | - | |
| 104 | - | |
| 105 | -	function readTableDirectory() { | |
| 106 | - $this->numTables = $this->read_ushort(); | |
| 72 | +    function __construct() { | |
| 73 | + $this->maxStrLenRead = 200000; // Maximum size of glyf table to read in as string (otherwise reads each glyph from file) | |
| 74 | + } | |
| 75 | + | |
| 76 | + | |
| 77 | + /** | |
| 78 | + * @param string $file | |
| 79 | + */ | |
| 80 | +    function getMetrics($file) { | |
| 81 | + $this->filename = $file; | |
| 82 | +        $this->fh = fopen($file,'rb') or die('Can\'t open file ' . $file); | |
| 83 | + $this->_pos = 0; | |
| 84 | + $this->charWidths = ''; | |
| 85 | + $this->glyphPos = array(); | |
| 86 | + $this->charToGlyph = array(); | |
| 87 | + $this->tables = array(); | |
| 88 | + $this->otables = array(); | |
| 89 | + $this->ascent = 0; | |
| 90 | + $this->descent = 0; | |
| 91 | + $this->TTCFonts = array(); | |
| 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); | |
| 99 | + $this->readTableDirectory(); | |
| 100 | + $this->extractInfo(); | |
| 101 | + fclose($this->fh); | |
| 102 | + } | |
| 103 | + | |
| 104 | + | |
| 105 | +    function readTableDirectory() { | |
| 106 | + $this->numTables = $this->read_ushort(); | |
| 107 | 107 | $this->searchRange = $this->read_ushort(); | 
| 108 | 108 | $this->entrySelector = $this->read_ushort(); | 
| 109 | 109 | $this->rangeShift = $this->read_ushort(); | 
| @@ -115,178 +115,178 @@ discard block | ||
| 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 | -	function get_table_pos($tag) { | |
| 154 | - $offset = $this->tables[$tag]['offset']; | |
| 155 | - $length = $this->tables[$tag]['length']; | |
| 156 | - return array($offset, $length); | |
| 157 | - } | |
| 158 | - | |
| 159 | -	function seek($pos) { | |
| 160 | - $this->_pos = $pos; | |
| 161 | - fseek($this->fh,$this->_pos); | |
| 162 | - } | |
| 163 | - | |
| 164 | - /** | |
| 165 | - * @param integer $delta | |
| 166 | - */ | |
| 167 | -	function skip($delta) { | |
| 168 | - $this->_pos = $this->_pos + $delta; | |
| 169 | - fseek($this->fh,$this->_pos); | |
| 170 | - } | |
| 171 | - | |
| 172 | - /** | |
| 173 | - * @param string $tag | |
| 174 | - */ | |
| 175 | -	function seek_table($tag, $offset_in_table = 0) { | |
| 176 | - $tpos = $this->get_table_pos($tag); | |
| 177 | - $this->_pos = $tpos[0] + $offset_in_table; | |
| 178 | - fseek($this->fh, $this->_pos); | |
| 179 | - return $this->_pos; | |
| 180 | - } | |
| 181 | - | |
| 182 | -	function read_tag() { | |
| 183 | - $this->_pos += 4; | |
| 184 | - return fread($this->fh, 4); | |
| 185 | - } | |
| 186 | - | |
| 187 | -	function read_short() { | |
| 188 | - $this->_pos += 2; | |
| 189 | - $s = fread($this->fh, 2); | |
| 190 | - $a = (ord($s[0]) << 8) + ord($s[1]); | |
| 191 | -		if ($a & (1 << 15)) { $a = ($a - (1 << 16)); } | |
| 192 | - return $a; | |
| 193 | - } | |
| 194 | - | |
| 195 | -	function unpack_short($s) { | |
| 196 | - $a = (ord($s[0]) << 8) + ord($s[1]); | |
| 197 | -		if ($a & (1 << 15)) {  | |
| 198 | - $a = ($a - (1 << 16)); | |
| 199 | - } | |
| 200 | - return $a; | |
| 201 | - } | |
| 202 | - | |
| 203 | -	function read_ushort() { | |
| 204 | - $this->_pos += 2; | |
| 205 | - $s = fread($this->fh,2); | |
| 206 | - return (ord($s[0])<<8) + ord($s[1]); | |
| 207 | - } | |
| 208 | - | |
| 209 | -	function read_ulong() { | |
| 210 | - $this->_pos += 4; | |
| 211 | - $s = fread($this->fh,4); | |
| 212 | - // if large uInt32 as an integer, PHP converts it to -ve | |
| 213 | - return (ord($s[0])*16777216) + (ord($s[1])<<16) + (ord($s[2])<<8) + ord($s[3]); // 16777216 = 1<<24 | |
| 214 | - } | |
| 215 | - | |
| 216 | -	function get_ushort($pos) { | |
| 217 | - fseek($this->fh,$pos); | |
| 218 | - $s = fread($this->fh,2); | |
| 219 | - return (ord($s[0])<<8) + ord($s[1]); | |
| 220 | - } | |
| 221 | - | |
| 222 | -	function get_ulong($pos) { | |
| 223 | - fseek($this->fh,$pos); | |
| 224 | - $s = fread($this->fh,4); | |
| 225 | - // iF large uInt32 as an integer, PHP converts it to -ve | |
| 226 | - return (ord($s[0])*16777216) + (ord($s[1])<<16) + (ord($s[2])<<8) + ord($s[3]); // 16777216 = 1<<24 | |
| 227 | - } | |
| 228 | - | |
| 229 | -	function pack_short($val) { | |
| 230 | -		if ($val<0) {  | |
| 231 | - $val = abs($val); | |
| 232 | - $val = ~$val; | |
| 233 | - $val += 1; | |
| 234 | - } | |
| 235 | -		return pack("n",$val);  | |
| 236 | - } | |
| 237 | - | |
| 238 | - /** | |
| 239 | - * @param string $value | |
| 240 | - */ | |
| 241 | -	function splice($stream, $offset, $value) { | |
| 242 | - return substr($stream,0,$offset) . $value . substr($stream,$offset+strlen($value)); | |
| 243 | - } | |
| 244 | - | |
| 245 | - /** | |
| 246 | - * @param string|null $stream | |
| 247 | - * @param integer $offset | |
| 248 | - */ | |
| 249 | -	function _set_ushort($stream, $offset, $value) { | |
| 250 | -		$up = pack("n", $value); | |
| 251 | - return $this->splice($stream, $offset, $up); | |
| 252 | - } | |
| 253 | - | |
| 254 | -	function _set_short($stream, $offset, $val) { | |
| 255 | -		if ($val<0) {  | |
| 256 | - $val = abs($val); | |
| 257 | - $val = ~$val; | |
| 258 | - $val += 1; | |
| 259 | - } | |
| 260 | -		$up = pack("n",$val);  | |
| 261 | - return $this->splice($stream, $offset, $up); | |
| 262 | - } | |
| 263 | - | |
| 264 | -	function get_chunk($pos, $length) { | |
| 265 | - fseek($this->fh,$pos); | |
| 266 | -		if ($length <1) { return ''; } | |
| 267 | - return (fread($this->fh,$length)); | |
| 268 | - } | |
| 269 | - | |
| 270 | - /** | |
| 271 | - * @param string $tag | |
| 272 | - */ | |
| 273 | -	function get_table($tag) { | |
| 274 | - list($pos, $length) = $this->get_table_pos($tag); | |
| 275 | -		if ($length == 0) { die('Truetype font ('.$this->filename.'): error reading table: '.$tag); } | |
| 276 | - fseek($this->fh,$pos); | |
| 277 | - return (fread($this->fh,$length)); | |
| 278 | - } | |
| 279 | - | |
| 280 | - /** | |
| 281 | - * @param string $tag | |
| 282 | - * @param null|string $data | |
| 283 | - */ | |
| 284 | -	function add($tag, $data) { | |
| 285 | -		if ($tag == 'head') { | |
| 286 | - $data = $this->splice($data, 8, "\0\0\0\0"); | |
| 287 | - } | |
| 288 | - $this->otables[$tag] = $data; | |
| 289 | - } | |
| 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 | +    function get_table_pos($tag) { | |
| 154 | + $offset = $this->tables[$tag]['offset']; | |
| 155 | + $length = $this->tables[$tag]['length']; | |
| 156 | + return array($offset, $length); | |
| 157 | + } | |
| 158 | + | |
| 159 | +    function seek($pos) { | |
| 160 | + $this->_pos = $pos; | |
| 161 | + fseek($this->fh,$this->_pos); | |
| 162 | + } | |
| 163 | + | |
| 164 | + /** | |
| 165 | + * @param integer $delta | |
| 166 | + */ | |
| 167 | +    function skip($delta) { | |
| 168 | + $this->_pos = $this->_pos + $delta; | |
| 169 | + fseek($this->fh,$this->_pos); | |
| 170 | + } | |
| 171 | + | |
| 172 | + /** | |
| 173 | + * @param string $tag | |
| 174 | + */ | |
| 175 | +    function seek_table($tag, $offset_in_table = 0) { | |
| 176 | + $tpos = $this->get_table_pos($tag); | |
| 177 | + $this->_pos = $tpos[0] + $offset_in_table; | |
| 178 | + fseek($this->fh, $this->_pos); | |
| 179 | + return $this->_pos; | |
| 180 | + } | |
| 181 | + | |
| 182 | +    function read_tag() { | |
| 183 | + $this->_pos += 4; | |
| 184 | + return fread($this->fh, 4); | |
| 185 | + } | |
| 186 | + | |
| 187 | +    function read_short() { | |
| 188 | + $this->_pos += 2; | |
| 189 | + $s = fread($this->fh, 2); | |
| 190 | + $a = (ord($s[0]) << 8) + ord($s[1]); | |
| 191 | +        if ($a & (1 << 15)) { $a = ($a - (1 << 16)); } | |
| 192 | + return $a; | |
| 193 | + } | |
| 194 | + | |
| 195 | +    function unpack_short($s) { | |
| 196 | + $a = (ord($s[0]) << 8) + ord($s[1]); | |
| 197 | +        if ($a & (1 << 15)) {  | |
| 198 | + $a = ($a - (1 << 16)); | |
| 199 | + } | |
| 200 | + return $a; | |
| 201 | + } | |
| 202 | + | |
| 203 | +    function read_ushort() { | |
| 204 | + $this->_pos += 2; | |
| 205 | + $s = fread($this->fh,2); | |
| 206 | + return (ord($s[0])<<8) + ord($s[1]); | |
| 207 | + } | |
| 208 | + | |
| 209 | +    function read_ulong() { | |
| 210 | + $this->_pos += 4; | |
| 211 | + $s = fread($this->fh,4); | |
| 212 | + // if large uInt32 as an integer, PHP converts it to -ve | |
| 213 | + return (ord($s[0])*16777216) + (ord($s[1])<<16) + (ord($s[2])<<8) + ord($s[3]); // 16777216 = 1<<24 | |
| 214 | + } | |
| 215 | + | |
| 216 | +    function get_ushort($pos) { | |
| 217 | + fseek($this->fh,$pos); | |
| 218 | + $s = fread($this->fh,2); | |
| 219 | + return (ord($s[0])<<8) + ord($s[1]); | |
| 220 | + } | |
| 221 | + | |
| 222 | +    function get_ulong($pos) { | |
| 223 | + fseek($this->fh,$pos); | |
| 224 | + $s = fread($this->fh,4); | |
| 225 | + // iF large uInt32 as an integer, PHP converts it to -ve | |
| 226 | + return (ord($s[0])*16777216) + (ord($s[1])<<16) + (ord($s[2])<<8) + ord($s[3]); // 16777216 = 1<<24 | |
| 227 | + } | |
| 228 | + | |
| 229 | +    function pack_short($val) { | |
| 230 | +        if ($val<0) {  | |
| 231 | + $val = abs($val); | |
| 232 | + $val = ~$val; | |
| 233 | + $val += 1; | |
| 234 | + } | |
| 235 | +        return pack("n",$val);  | |
| 236 | + } | |
| 237 | + | |
| 238 | + /** | |
| 239 | + * @param string $value | |
| 240 | + */ | |
| 241 | +    function splice($stream, $offset, $value) { | |
| 242 | + return substr($stream,0,$offset) . $value . substr($stream,$offset+strlen($value)); | |
| 243 | + } | |
| 244 | + | |
| 245 | + /** | |
| 246 | + * @param string|null $stream | |
| 247 | + * @param integer $offset | |
| 248 | + */ | |
| 249 | +    function _set_ushort($stream, $offset, $value) { | |
| 250 | +        $up = pack("n", $value); | |
| 251 | + return $this->splice($stream, $offset, $up); | |
| 252 | + } | |
| 253 | + | |
| 254 | +    function _set_short($stream, $offset, $val) { | |
| 255 | +        if ($val<0) {  | |
| 256 | + $val = abs($val); | |
| 257 | + $val = ~$val; | |
| 258 | + $val += 1; | |
| 259 | + } | |
| 260 | +        $up = pack("n",$val);  | |
| 261 | + return $this->splice($stream, $offset, $up); | |
| 262 | + } | |
| 263 | + | |
| 264 | +    function get_chunk($pos, $length) { | |
| 265 | + fseek($this->fh,$pos); | |
| 266 | +        if ($length <1) { return ''; } | |
| 267 | + return (fread($this->fh,$length)); | |
| 268 | + } | |
| 269 | + | |
| 270 | + /** | |
| 271 | + * @param string $tag | |
| 272 | + */ | |
| 273 | +    function get_table($tag) { | |
| 274 | + list($pos, $length) = $this->get_table_pos($tag); | |
| 275 | +        if ($length == 0) { die('Truetype font ('.$this->filename.'): error reading table: '.$tag); } | |
| 276 | + fseek($this->fh,$pos); | |
| 277 | + return (fread($this->fh,$length)); | |
| 278 | + } | |
| 279 | + | |
| 280 | + /** | |
| 281 | + * @param string $tag | |
| 282 | + * @param null|string $data | |
| 283 | + */ | |
| 284 | +    function add($tag, $data) { | |
| 285 | +        if ($tag == 'head') { | |
| 286 | + $data = $this->splice($data, 8, "\0\0\0\0"); | |
| 287 | + } | |
| 288 | + $this->otables[$tag] = $data; | |
| 289 | + } | |
| 290 | 290 | |
| 291 | 291 | |
| 292 | 292 | |
| @@ -295,841 +295,841 @@ discard block | ||
| 295 | 295 | |
| 296 | 296 | ///////////////////////////////////////////////////////////////////////////////////////// | 
| 297 | 297 | |
| 298 | -	function extractInfo() { | |
| 299 | - /////////////////////////////////// | |
| 300 | - // name - Naming table | |
| 301 | - /////////////////////////////////// | |
| 302 | - $this->sFamilyClass = 0; | |
| 303 | - $this->sFamilySubClass = 0; | |
| 304 | - | |
| 305 | -			$name_offset = $this->seek_table("name"); | |
| 306 | - $format = $this->read_ushort(); | |
| 307 | -			if ($format != 0) { | |
| 308 | -							die("Unknown name table format ".$format); | |
| 309 | - } | |
| 310 | - $numRecords = $this->read_ushort(); | |
| 311 | - $string_data_offset = $name_offset + $this->read_ushort(); | |
| 312 | - $names = array(1=>'', 2=>'', 3=>'', 4=>'', 6=>''); | |
| 313 | - $K = array_keys($names); | |
| 314 | - $nameCount = count($names); | |
| 315 | -			for ($i = 0; $i < $numRecords; $i++) { | |
| 316 | - $platformId = $this->read_ushort(); | |
| 317 | - $encodingId = $this->read_ushort(); | |
| 318 | - $languageId = $this->read_ushort(); | |
| 319 | - $nameId = $this->read_ushort(); | |
| 320 | - $length = $this->read_ushort(); | |
| 321 | - $offset = $this->read_ushort(); | |
| 322 | - if (!in_array($nameId, $K)) continue; | |
| 323 | - $N = ''; | |
| 324 | -				if ($platformId == 3 && $encodingId == 1 && $languageId == 0x409) { // Microsoft, Unicode, US English, PS Name | |
| 325 | - $opos = $this->_pos; | |
| 326 | - $this->seek($string_data_offset + $offset); | |
| 327 | - if ($length % 2 != 0) | |
| 328 | -						die("PostScript name is UTF-16BE string of odd length"); | |
| 329 | - $length /= 2; | |
| 330 | - $N = ''; | |
| 331 | -					while ($length > 0) { | |
| 332 | - $char = $this->read_ushort(); | |
| 333 | - $N .= (chr($char)); | |
| 334 | - $length -= 1; | |
| 335 | - } | |
| 336 | - $this->_pos = $opos; | |
| 337 | - $this->seek($opos); | |
| 338 | -				} else if ($platformId == 1 && $encodingId == 0 && $languageId == 0) { // Macintosh, Roman, English, PS Name | |
| 339 | - $opos = $this->_pos; | |
| 340 | - $N = $this->get_chunk($string_data_offset + $offset, $length); | |
| 341 | - $this->_pos = $opos; | |
| 342 | - $this->seek($opos); | |
| 343 | - } | |
| 344 | -				if ($N && $names[$nameId] == '') { | |
| 345 | - $names[$nameId] = $N; | |
| 346 | - $nameCount -= 1; | |
| 347 | - if ($nameCount == 0) break; | |
| 348 | - } | |
| 349 | - } | |
| 350 | - if ($names[6]) | |
| 351 | - $psName = $names[6]; | |
| 352 | - else if ($names[4]) | |
| 353 | -				$psName = preg_replace('/ /', '-', $names[4]); | |
| 354 | - else if ($names[1]) | |
| 355 | -				$psName = preg_replace('/ /', '-', $names[1]); | |
| 356 | - else | |
| 357 | - $psName = ''; | |
| 358 | - if (!$psName) | |
| 359 | -				die("Could not find PostScript font name"); | |
| 360 | - $this->name = $psName; | |
| 361 | -			if ($names[1]) { $this->familyName = $names[1]; } else { $this->familyName = $psName; } | |
| 362 | -			if ($names[2]) { $this->styleName = $names[2]; } else { $this->styleName = 'Regular'; } | |
| 363 | -			if ($names[4]) { $this->fullName = $names[4]; } else { $this->fullName = $psName; } | |
| 364 | -			if ($names[3]) { $this->uniqueFontID = $names[3]; } else { $this->uniqueFontID = $psName; } | |
| 365 | -			if ($names[6]) { $this->fullName = $names[6]; } | |
| 366 | - | |
| 367 | - /////////////////////////////////// | |
| 368 | - // head - Font header table | |
| 369 | - /////////////////////////////////// | |
| 370 | -		$this->seek_table("head"); | |
| 371 | - $this->skip(18); | |
| 372 | - $this->unitsPerEm = $unitsPerEm = $this->read_ushort(); | |
| 373 | - $scale = 1000 / $unitsPerEm; | |
| 374 | - $this->skip(16); | |
| 375 | - $xMin = $this->read_short(); | |
| 376 | - $yMin = $this->read_short(); | |
| 377 | - $xMax = $this->read_short(); | |
| 378 | - $yMax = $this->read_short(); | |
| 379 | - $this->bbox = array(($xMin * $scale), ($yMin * $scale), ($xMax * $scale), ($yMax * $scale)); | |
| 380 | - $this->skip(3 * 2); | |
| 381 | - $indexToLocFormat = $this->read_ushort(); | |
| 382 | - $glyphDataFormat = $this->read_ushort(); | |
| 383 | -		if ($glyphDataFormat != 0) { | |
| 384 | -					die('Unknown glyph data format '.$glyphDataFormat); | |
| 385 | - } | |
| 386 | - | |
| 387 | - /////////////////////////////////// | |
| 388 | - // hhea metrics table | |
| 389 | - /////////////////////////////////// | |
| 390 | - // ttf2t1 seems to use this value rather than the one in OS/2 - so put in for compatibility | |
| 391 | -		if (isset($this->tables["hhea"])) { | |
| 392 | -			$this->seek_table("hhea"); | |
| 393 | - $this->skip(4); | |
| 394 | - $hheaAscender = $this->read_short(); | |
| 395 | - $hheaDescender = $this->read_short(); | |
| 396 | - $this->ascent = ($hheaAscender * $scale); | |
| 397 | - $this->descent = ($hheaDescender * $scale); | |
| 398 | - } | |
| 399 | - | |
| 400 | - /////////////////////////////////// | |
| 401 | - // OS/2 - OS/2 and Windows metrics table | |
| 402 | - /////////////////////////////////// | |
| 403 | -		if (isset($this->tables["OS/2"])) { | |
| 404 | -			$this->seek_table("OS/2"); | |
| 405 | - $version = $this->read_ushort(); | |
| 406 | - $this->skip(2); | |
| 407 | - $usWeightClass = $this->read_ushort(); | |
| 408 | - $this->skip(2); | |
| 409 | - $fsType = $this->read_ushort(); | |
| 410 | -			if ($fsType == 0x0002 || ($fsType & 0x0300) != 0) { | |
| 411 | -				die('ERROR - Font file '.$this->filename.' cannot be embedded due to copyright restrictions.'); | |
| 412 | - $this->restrictedUse = true; | |
| 413 | - } | |
| 414 | - $this->skip(20); | |
| 415 | - $sF = $this->read_short(); | |
| 416 | - $this->sFamilyClass = ($sF >> 8); | |
| 417 | - $this->sFamilySubClass = ($sF & 0xFF); | |
| 418 | - $this->_pos += 10; //PANOSE = 10 byte length | |
| 419 | - $panose = fread($this->fh, 10); | |
| 420 | - $this->skip(26); | |
| 421 | - $sTypoAscender = $this->read_short(); | |
| 422 | - $sTypoDescender = $this->read_short(); | |
| 423 | - if (!$this->ascent) $this->ascent = ($sTypoAscender * $scale); | |
| 424 | - if (!$this->descent) $this->descent = ($sTypoDescender * $scale); | |
| 425 | -			if ($version > 1) { | |
| 426 | - $this->skip(16); | |
| 427 | - $sCapHeight = $this->read_short(); | |
| 428 | - $this->capHeight = ($sCapHeight * $scale); | |
| 429 | - } | |
| 430 | -			else { | |
| 431 | - $this->capHeight = $this->ascent; | |
| 432 | - } | |
| 433 | - } | |
| 434 | -		else { | |
| 435 | - $usWeightClass = 500; | |
| 436 | - if (!$this->ascent) $this->ascent = ($yMax * $scale); | |
| 437 | - if (!$this->descent) $this->descent = ($yMin * $scale); | |
| 438 | - $this->capHeight = $this->ascent; | |
| 439 | - } | |
| 440 | - $this->stemV = 50 + intval(pow(($usWeightClass / 65.0), 2)); | |
| 441 | - | |
| 442 | - /////////////////////////////////// | |
| 443 | - // post - PostScript table | |
| 444 | - /////////////////////////////////// | |
| 445 | -		$this->seek_table("post"); | |
| 446 | - $this->skip(4); | |
| 447 | - $this->italicAngle = $this->read_short() + $this->read_ushort() / 65536.0; | |
| 448 | - $this->underlinePosition = $this->read_short() * $scale; | |
| 449 | - $this->underlineThickness = $this->read_short() * $scale; | |
| 450 | - $isFixedPitch = $this->read_ulong(); | |
| 451 | - | |
| 452 | - $this->flags = 4; | |
| 453 | - | |
| 454 | - if ($this->italicAngle != 0) | |
| 455 | - $this->flags = $this->flags | 64; | |
| 456 | - if ($usWeightClass >= 600) | |
| 457 | - $this->flags = $this->flags | 262144; | |
| 458 | - if ($isFixedPitch) | |
| 459 | - $this->flags = $this->flags | 1; | |
| 460 | - | |
| 461 | - /////////////////////////////////// | |
| 462 | - // hhea - Horizontal header table | |
| 463 | - /////////////////////////////////// | |
| 464 | -		$this->seek_table("hhea"); | |
| 465 | - $this->skip(32); | |
| 466 | - $metricDataFormat = $this->read_ushort(); | |
| 467 | -		if ($metricDataFormat != 0) { | |
| 468 | -					die('Unknown horizontal metric data format '.$metricDataFormat); | |
| 469 | - } | |
| 470 | - $numberOfHMetrics = $this->read_ushort(); | |
| 471 | -		if ($numberOfHMetrics == 0) { | |
| 472 | -					die('Number of horizontal metrics is 0'); | |
| 473 | - } | |
| 474 | - | |
| 475 | - /////////////////////////////////// | |
| 476 | - // maxp - Maximum profile table | |
| 477 | - /////////////////////////////////// | |
| 478 | -		$this->seek_table("maxp"); | |
| 479 | - $this->skip(4); | |
| 480 | - $numGlyphs = $this->read_ushort(); | |
| 481 | - | |
| 482 | - | |
| 483 | - /////////////////////////////////// | |
| 484 | - // cmap - Character to glyph index mapping table | |
| 485 | - /////////////////////////////////// | |
| 486 | -		$cmap_offset = $this->seek_table("cmap"); | |
| 487 | - $this->skip(2); | |
| 488 | - $cmapTableCount = $this->read_ushort(); | |
| 489 | - $unicode_cmap_offset = 0; | |
| 490 | -		for ($i = 0; $i < $cmapTableCount; $i++) { | |
| 491 | - $platformID = $this->read_ushort(); | |
| 492 | - $encodingID = $this->read_ushort(); | |
| 493 | - $offset = $this->read_ulong(); | |
| 494 | - $save_pos = $this->_pos; | |
| 495 | -			if (($platformID == 3 && $encodingID == 1) || $platformID == 0) { // Microsoft, Unicode | |
| 496 | - $format = $this->get_ushort($cmap_offset + $offset); | |
| 497 | -				if ($format == 4) { | |
| 498 | - if (!$unicode_cmap_offset) $unicode_cmap_offset = $cmap_offset + $offset; | |
| 499 | - break; | |
| 500 | - } | |
| 501 | - } | |
| 502 | - $this->seek($save_pos); | |
| 503 | - } | |
| 504 | - if (!$unicode_cmap_offset) | |
| 505 | -			die('Font ('.$this->filename.') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); | |
| 506 | - | |
| 507 | - | |
| 508 | - $glyphToChar = array(); | |
| 509 | - $charToGlyph = array(); | |
| 510 | - $this->getCMAP4($unicode_cmap_offset, $glyphToChar, $charToGlyph); | |
| 511 | - | |
| 512 | - /////////////////////////////////// | |
| 513 | - // hmtx - Horizontal metrics table | |
| 514 | - /////////////////////////////////// | |
| 515 | - $this->getHMTX($numberOfHMetrics, $numGlyphs, $glyphToChar, $scale); | |
| 516 | - | |
| 517 | - } | |
| 298 | +    function extractInfo() { | |
| 299 | + /////////////////////////////////// | |
| 300 | + // name - Naming table | |
| 301 | + /////////////////////////////////// | |
| 302 | + $this->sFamilyClass = 0; | |
| 303 | + $this->sFamilySubClass = 0; | |
| 304 | + | |
| 305 | +            $name_offset = $this->seek_table("name"); | |
| 306 | + $format = $this->read_ushort(); | |
| 307 | +            if ($format != 0) { | |
| 308 | +                            die("Unknown name table format ".$format); | |
| 309 | + } | |
| 310 | + $numRecords = $this->read_ushort(); | |
| 311 | + $string_data_offset = $name_offset + $this->read_ushort(); | |
| 312 | + $names = array(1=>'', 2=>'', 3=>'', 4=>'', 6=>''); | |
| 313 | + $K = array_keys($names); | |
| 314 | + $nameCount = count($names); | |
| 315 | +            for ($i = 0; $i < $numRecords; $i++) { | |
| 316 | + $platformId = $this->read_ushort(); | |
| 317 | + $encodingId = $this->read_ushort(); | |
| 318 | + $languageId = $this->read_ushort(); | |
| 319 | + $nameId = $this->read_ushort(); | |
| 320 | + $length = $this->read_ushort(); | |
| 321 | + $offset = $this->read_ushort(); | |
| 322 | + if (!in_array($nameId, $K)) continue; | |
| 323 | + $N = ''; | |
| 324 | +                if ($platformId == 3 && $encodingId == 1 && $languageId == 0x409) { // Microsoft, Unicode, US English, PS Name | |
| 325 | + $opos = $this->_pos; | |
| 326 | + $this->seek($string_data_offset + $offset); | |
| 327 | + if ($length % 2 != 0) | |
| 328 | +                        die("PostScript name is UTF-16BE string of odd length"); | |
| 329 | + $length /= 2; | |
| 330 | + $N = ''; | |
| 331 | +                    while ($length > 0) { | |
| 332 | + $char = $this->read_ushort(); | |
| 333 | + $N .= (chr($char)); | |
| 334 | + $length -= 1; | |
| 335 | + } | |
| 336 | + $this->_pos = $opos; | |
| 337 | + $this->seek($opos); | |
| 338 | +                } else if ($platformId == 1 && $encodingId == 0 && $languageId == 0) { // Macintosh, Roman, English, PS Name | |
| 339 | + $opos = $this->_pos; | |
| 340 | + $N = $this->get_chunk($string_data_offset + $offset, $length); | |
| 341 | + $this->_pos = $opos; | |
| 342 | + $this->seek($opos); | |
| 343 | + } | |
| 344 | +                if ($N && $names[$nameId] == '') { | |
| 345 | + $names[$nameId] = $N; | |
| 346 | + $nameCount -= 1; | |
| 347 | + if ($nameCount == 0) break; | |
| 348 | + } | |
| 349 | + } | |
| 350 | + if ($names[6]) | |
| 351 | + $psName = $names[6]; | |
| 352 | + else if ($names[4]) | |
| 353 | +                $psName = preg_replace('/ /', '-', $names[4]); | |
| 354 | + else if ($names[1]) | |
| 355 | +                $psName = preg_replace('/ /', '-', $names[1]); | |
| 356 | + else | |
| 357 | + $psName = ''; | |
| 358 | + if (!$psName) | |
| 359 | +                die("Could not find PostScript font name"); | |
| 360 | + $this->name = $psName; | |
| 361 | +            if ($names[1]) { $this->familyName = $names[1]; } else { $this->familyName = $psName; } | |
| 362 | +            if ($names[2]) { $this->styleName = $names[2]; } else { $this->styleName = 'Regular'; } | |
| 363 | +            if ($names[4]) { $this->fullName = $names[4]; } else { $this->fullName = $psName; } | |
| 364 | +            if ($names[3]) { $this->uniqueFontID = $names[3]; } else { $this->uniqueFontID = $psName; } | |
| 365 | +            if ($names[6]) { $this->fullName = $names[6]; } | |
| 366 | + | |
| 367 | + /////////////////////////////////// | |
| 368 | + // head - Font header table | |
| 369 | + /////////////////////////////////// | |
| 370 | +        $this->seek_table("head"); | |
| 371 | + $this->skip(18); | |
| 372 | + $this->unitsPerEm = $unitsPerEm = $this->read_ushort(); | |
| 373 | + $scale = 1000 / $unitsPerEm; | |
| 374 | + $this->skip(16); | |
| 375 | + $xMin = $this->read_short(); | |
| 376 | + $yMin = $this->read_short(); | |
| 377 | + $xMax = $this->read_short(); | |
| 378 | + $yMax = $this->read_short(); | |
| 379 | + $this->bbox = array(($xMin * $scale), ($yMin * $scale), ($xMax * $scale), ($yMax * $scale)); | |
| 380 | + $this->skip(3 * 2); | |
| 381 | + $indexToLocFormat = $this->read_ushort(); | |
| 382 | + $glyphDataFormat = $this->read_ushort(); | |
| 383 | +        if ($glyphDataFormat != 0) { | |
| 384 | +                    die('Unknown glyph data format '.$glyphDataFormat); | |
| 385 | + } | |
| 386 | + | |
| 387 | + /////////////////////////////////// | |
| 388 | + // hhea metrics table | |
| 389 | + /////////////////////////////////// | |
| 390 | + // ttf2t1 seems to use this value rather than the one in OS/2 - so put in for compatibility | |
| 391 | +        if (isset($this->tables["hhea"])) { | |
| 392 | +            $this->seek_table("hhea"); | |
| 393 | + $this->skip(4); | |
| 394 | + $hheaAscender = $this->read_short(); | |
| 395 | + $hheaDescender = $this->read_short(); | |
| 396 | + $this->ascent = ($hheaAscender * $scale); | |
| 397 | + $this->descent = ($hheaDescender * $scale); | |
| 398 | + } | |
| 399 | + | |
| 400 | + /////////////////////////////////// | |
| 401 | + // OS/2 - OS/2 and Windows metrics table | |
| 402 | + /////////////////////////////////// | |
| 403 | +        if (isset($this->tables["OS/2"])) { | |
| 404 | +            $this->seek_table("OS/2"); | |
| 405 | + $version = $this->read_ushort(); | |
| 406 | + $this->skip(2); | |
| 407 | + $usWeightClass = $this->read_ushort(); | |
| 408 | + $this->skip(2); | |
| 409 | + $fsType = $this->read_ushort(); | |
| 410 | +            if ($fsType == 0x0002 || ($fsType & 0x0300) != 0) { | |
| 411 | +                die('ERROR - Font file '.$this->filename.' cannot be embedded due to copyright restrictions.'); | |
| 412 | + $this->restrictedUse = true; | |
| 413 | + } | |
| 414 | + $this->skip(20); | |
| 415 | + $sF = $this->read_short(); | |
| 416 | + $this->sFamilyClass = ($sF >> 8); | |
| 417 | + $this->sFamilySubClass = ($sF & 0xFF); | |
| 418 | + $this->_pos += 10; //PANOSE = 10 byte length | |
| 419 | + $panose = fread($this->fh, 10); | |
| 420 | + $this->skip(26); | |
| 421 | + $sTypoAscender = $this->read_short(); | |
| 422 | + $sTypoDescender = $this->read_short(); | |
| 423 | + if (!$this->ascent) $this->ascent = ($sTypoAscender * $scale); | |
| 424 | + if (!$this->descent) $this->descent = ($sTypoDescender * $scale); | |
| 425 | +            if ($version > 1) { | |
| 426 | + $this->skip(16); | |
| 427 | + $sCapHeight = $this->read_short(); | |
| 428 | + $this->capHeight = ($sCapHeight * $scale); | |
| 429 | + } | |
| 430 | +            else { | |
| 431 | + $this->capHeight = $this->ascent; | |
| 432 | + } | |
| 433 | + } | |
| 434 | +        else { | |
| 435 | + $usWeightClass = 500; | |
| 436 | + if (!$this->ascent) $this->ascent = ($yMax * $scale); | |
| 437 | + if (!$this->descent) $this->descent = ($yMin * $scale); | |
| 438 | + $this->capHeight = $this->ascent; | |
| 439 | + } | |
| 440 | + $this->stemV = 50 + intval(pow(($usWeightClass / 65.0), 2)); | |
| 441 | + | |
| 442 | + /////////////////////////////////// | |
| 443 | + // post - PostScript table | |
| 444 | + /////////////////////////////////// | |
| 445 | +        $this->seek_table("post"); | |
| 446 | + $this->skip(4); | |
| 447 | + $this->italicAngle = $this->read_short() + $this->read_ushort() / 65536.0; | |
| 448 | + $this->underlinePosition = $this->read_short() * $scale; | |
| 449 | + $this->underlineThickness = $this->read_short() * $scale; | |
| 450 | + $isFixedPitch = $this->read_ulong(); | |
| 451 | + | |
| 452 | + $this->flags = 4; | |
| 453 | + | |
| 454 | + if ($this->italicAngle != 0) | |
| 455 | + $this->flags = $this->flags | 64; | |
| 456 | + if ($usWeightClass >= 600) | |
| 457 | + $this->flags = $this->flags | 262144; | |
| 458 | + if ($isFixedPitch) | |
| 459 | + $this->flags = $this->flags | 1; | |
| 460 | + | |
| 461 | + /////////////////////////////////// | |
| 462 | + // hhea - Horizontal header table | |
| 463 | + /////////////////////////////////// | |
| 464 | +        $this->seek_table("hhea"); | |
| 465 | + $this->skip(32); | |
| 466 | + $metricDataFormat = $this->read_ushort(); | |
| 467 | +        if ($metricDataFormat != 0) { | |
| 468 | +                    die('Unknown horizontal metric data format '.$metricDataFormat); | |
| 469 | + } | |
| 470 | + $numberOfHMetrics = $this->read_ushort(); | |
| 471 | +        if ($numberOfHMetrics == 0) { | |
| 472 | +                    die('Number of horizontal metrics is 0'); | |
| 473 | + } | |
| 474 | + | |
| 475 | + /////////////////////////////////// | |
| 476 | + // maxp - Maximum profile table | |
| 477 | + /////////////////////////////////// | |
| 478 | +        $this->seek_table("maxp"); | |
| 479 | + $this->skip(4); | |
| 480 | + $numGlyphs = $this->read_ushort(); | |
| 481 | + | |
| 482 | + | |
| 483 | + /////////////////////////////////// | |
| 484 | + // cmap - Character to glyph index mapping table | |
| 485 | + /////////////////////////////////// | |
| 486 | +        $cmap_offset = $this->seek_table("cmap"); | |
| 487 | + $this->skip(2); | |
| 488 | + $cmapTableCount = $this->read_ushort(); | |
| 489 | + $unicode_cmap_offset = 0; | |
| 490 | +        for ($i = 0; $i < $cmapTableCount; $i++) { | |
| 491 | + $platformID = $this->read_ushort(); | |
| 492 | + $encodingID = $this->read_ushort(); | |
| 493 | + $offset = $this->read_ulong(); | |
| 494 | + $save_pos = $this->_pos; | |
| 495 | +            if (($platformID == 3 && $encodingID == 1) || $platformID == 0) { // Microsoft, Unicode | |
| 496 | + $format = $this->get_ushort($cmap_offset + $offset); | |
| 497 | +                if ($format == 4) { | |
| 498 | + if (!$unicode_cmap_offset) $unicode_cmap_offset = $cmap_offset + $offset; | |
| 499 | + break; | |
| 500 | + } | |
| 501 | + } | |
| 502 | + $this->seek($save_pos); | |
| 503 | + } | |
| 504 | + if (!$unicode_cmap_offset) | |
| 505 | +            die('Font ('.$this->filename.') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); | |
| 506 | + | |
| 507 | + | |
| 508 | + $glyphToChar = array(); | |
| 509 | + $charToGlyph = array(); | |
| 510 | + $this->getCMAP4($unicode_cmap_offset, $glyphToChar, $charToGlyph); | |
| 511 | + | |
| 512 | + /////////////////////////////////// | |
| 513 | + // hmtx - Horizontal metrics table | |
| 514 | + /////////////////////////////////// | |
| 515 | + $this->getHMTX($numberOfHMetrics, $numGlyphs, $glyphToChar, $scale); | |
| 516 | + | |
| 517 | + } | |
| 518 | 518 | |
| 519 | 519 | |
| 520 | 520 | ///////////////////////////////////////////////////////////////////////////////////////// | 
| 521 | 521 | ///////////////////////////////////////////////////////////////////////////////////////// | 
| 522 | 522 | |
| 523 | 523 | |
| 524 | - /** | |
| 525 | - * @return string | |
| 526 | - */ | |
| 527 | -	function makeSubset($file, &$subset) { | |
| 528 | - $this->filename = $file; | |
| 529 | -		$this->fh = fopen($file, 'rb') or die('Can\'t open file '.$file); | |
| 530 | - $this->_pos = 0; | |
| 531 | - $this->charWidths = ''; | |
| 532 | - $this->glyphPos = array(); | |
| 533 | - $this->charToGlyph = array(); | |
| 534 | - $this->tables = array(); | |
| 535 | - $this->otables = array(); | |
| 536 | - $this->ascent = 0; | |
| 537 | - $this->descent = 0; | |
| 538 | - $this->skip(4); | |
| 539 | - $this->maxUni = 0; | |
| 540 | - $this->readTableDirectory(); | |
| 541 | - | |
| 542 | - | |
| 543 | - /////////////////////////////////// | |
| 544 | - // head - Font header table | |
| 545 | - /////////////////////////////////// | |
| 546 | -		$this->seek_table("head"); | |
| 547 | - $this->skip(50); | |
| 548 | - $indexToLocFormat = $this->read_ushort(); | |
| 549 | - $glyphDataFormat = $this->read_ushort(); | |
| 550 | - | |
| 551 | - /////////////////////////////////// | |
| 552 | - // hhea - Horizontal header table | |
| 553 | - /////////////////////////////////// | |
| 554 | -		$this->seek_table("hhea"); | |
| 555 | - $this->skip(32); | |
| 556 | - $metricDataFormat = $this->read_ushort(); | |
| 557 | - $orignHmetrics = $numberOfHMetrics = $this->read_ushort(); | |
| 558 | - | |
| 559 | - /////////////////////////////////// | |
| 560 | - // maxp - Maximum profile table | |
| 561 | - /////////////////////////////////// | |
| 562 | -		$this->seek_table("maxp"); | |
| 563 | - $this->skip(4); | |
| 564 | - $numGlyphs = $this->read_ushort(); | |
| 565 | - | |
| 566 | - | |
| 567 | - /////////////////////////////////// | |
| 568 | - // cmap - Character to glyph index mapping table | |
| 569 | - /////////////////////////////////// | |
| 570 | -		$cmap_offset = $this->seek_table("cmap"); | |
| 571 | - $this->skip(2); | |
| 572 | - $cmapTableCount = $this->read_ushort(); | |
| 573 | - $unicode_cmap_offset = 0; | |
| 574 | -		for ($i = 0; $i < $cmapTableCount; $i++) { | |
| 575 | - $platformID = $this->read_ushort(); | |
| 576 | - $encodingID = $this->read_ushort(); | |
| 577 | - $offset = $this->read_ulong(); | |
| 578 | - $save_pos = $this->_pos; | |
| 579 | -			if (($platformID == 3 && $encodingID == 1) || $platformID == 0) { // Microsoft, Unicode | |
| 580 | - $format = $this->get_ushort($cmap_offset + $offset); | |
| 581 | -				if ($format == 4) { | |
| 582 | - $unicode_cmap_offset = $cmap_offset + $offset; | |
| 583 | - break; | |
| 584 | - } | |
| 585 | - } | |
| 586 | - $this->seek($save_pos); | |
| 587 | - } | |
| 588 | - | |
| 589 | - if (!$unicode_cmap_offset) | |
| 590 | -			die('Font ('.$this->filename.') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); | |
| 591 | - | |
| 592 | - | |
| 593 | - $glyphToChar = array(); | |
| 594 | - $charToGlyph = array(); | |
| 595 | - $this->getCMAP4($unicode_cmap_offset, $glyphToChar, $charToGlyph); | |
| 596 | - | |
| 597 | - $this->charToGlyph = $charToGlyph; | |
| 598 | - | |
| 599 | - /////////////////////////////////// | |
| 600 | - // hmtx - Horizontal metrics table | |
| 601 | - /////////////////////////////////// | |
| 602 | - $scale = 1; // not used | |
| 603 | - $this->getHMTX($numberOfHMetrics, $numGlyphs, $glyphToChar, $scale); | |
| 604 | - | |
| 605 | - /////////////////////////////////// | |
| 606 | - // loca - Index to location | |
| 607 | - /////////////////////////////////// | |
| 608 | - $this->getLOCA($indexToLocFormat, $numGlyphs); | |
| 609 | - | |
| 610 | - $subsetglyphs = array(0=>0); | |
| 611 | - $subsetCharToGlyph = array(); | |
| 612 | -		foreach ($subset AS $code) { | |
| 613 | -			if (isset($this->charToGlyph[$code])) { | |
| 614 | - $subsetglyphs[$this->charToGlyph[$code]] = $code; // Old Glyph ID => Unicode | |
| 615 | - $subsetCharToGlyph[$code] = $this->charToGlyph[$code]; // Unicode to old GlyphID | |
| 616 | - | |
| 617 | - } | |
| 618 | - $this->maxUni = max($this->maxUni, $code); | |
| 619 | - } | |
| 620 | - | |
| 621 | -		list($start, $dummy) = $this->get_table_pos('glyf'); | |
| 622 | - | |
| 623 | - $glyphSet = array(); | |
| 624 | - ksort($subsetglyphs); | |
| 625 | - $n = 0; | |
| 626 | - $fsLastCharIndex = 0; // maximum Unicode index (character code) in this font, according to the cmap subtable for platform ID 3 and platform- specific encoding ID 0 or 1. | |
| 627 | -		foreach ($subsetglyphs AS $originalGlyphIdx => $uni) { | |
| 628 | - $fsLastCharIndex = max($fsLastCharIndex, $uni); | |
| 629 | - $glyphSet[$originalGlyphIdx] = $n; // old glyphID to new glyphID | |
| 630 | - $n++; | |
| 631 | - } | |
| 632 | - | |
| 633 | - ksort($subsetCharToGlyph); | |
| 634 | -		foreach ($subsetCharToGlyph AS $uni => $originalGlyphIdx) { | |
| 635 | - $codeToGlyph[$uni] = $glyphSet[$originalGlyphIdx]; | |
| 636 | - } | |
| 637 | - $this->codeToGlyph = $codeToGlyph; | |
| 638 | - | |
| 639 | - ksort($subsetglyphs); | |
| 640 | -		foreach ($subsetglyphs AS $originalGlyphIdx => $uni) { | |
| 641 | - $this->getGlyphs($originalGlyphIdx, $start, $glyphSet, $subsetglyphs); | |
| 642 | - } | |
| 643 | - | |
| 644 | - $numGlyphs = $numberOfHMetrics = count($subsetglyphs); | |
| 645 | - | |
| 646 | - //tables copied from the original | |
| 647 | -		$tags = array('name'); | |
| 648 | -		foreach ($tags AS $tag) { $this->add($tag, $this->get_table($tag)); } | |
| 649 | -		$tags = array('cvt ', 'fpgm', 'prep', 'gasp'); | |
| 650 | -		foreach ($tags AS $tag) { | |
| 651 | -			if (isset($this->tables[$tag])) { $this->add($tag, $this->get_table($tag)); } | |
| 652 | - } | |
| 653 | - | |
| 654 | - // post - PostScript | |
| 655 | -		$opost = $this->get_table('post'); | |
| 656 | - $post = "\x00\x03\x00\x00".substr($opost, 4, 12)."\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; | |
| 657 | -		$this->add('post', $post); | |
| 658 | - | |
| 659 | - // Sort CID2GID map into segments of contiguous codes | |
| 660 | - ksort($codeToGlyph); | |
| 661 | - unset($codeToGlyph[0]); | |
| 662 | - //unset($codeToGlyph[65535]); | |
| 663 | - $rangeid = 0; | |
| 664 | - $range = array(); | |
| 665 | - $prevcid = -2; | |
| 666 | - $prevglidx = -1; | |
| 667 | - // for each character | |
| 668 | -		foreach ($codeToGlyph as $cid => $glidx) { | |
| 669 | -			if ($cid == ($prevcid + 1) && $glidx == ($prevglidx + 1)) { | |
| 670 | - $range[$rangeid][] = $glidx; | |
| 671 | -			} else { | |
| 672 | - // new range | |
| 673 | - $rangeid = $cid; | |
| 674 | - $range[$rangeid] = array(); | |
| 675 | - $range[$rangeid][] = $glidx; | |
| 676 | - } | |
| 677 | - $prevcid = $cid; | |
| 678 | - $prevglidx = $glidx; | |
| 679 | - } | |
| 680 | - | |
| 681 | - // cmap - Character to glyph mapping - Format 4 (MS / ) | |
| 682 | - $segCount = count($range) + 1; // + 1 Last segment has missing character 0xFFFF | |
| 683 | - $searchRange = 1; | |
| 684 | - $entrySelector = 0; | |
| 685 | -		while ($searchRange * 2 <= $segCount) { | |
| 686 | - $searchRange = $searchRange * 2; | |
| 687 | - $entrySelector = $entrySelector + 1; | |
| 688 | - } | |
| 689 | - $searchRange = $searchRange * 2; | |
| 690 | - $rangeShift = $segCount * 2 - $searchRange; | |
| 691 | - $length = 16 + (8 * $segCount) + ($numGlyphs + 1); | |
| 692 | - $cmap = array(0, 1, // Index : version, number of encoding subtables | |
| 693 | - 3, 1, // Encoding Subtable : platform (MS=3), encoding (Unicode) | |
| 694 | - 0, 12, // Encoding Subtable : offset (hi,lo) | |
| 695 | - 4, $length, 0, // Format 4 Mapping subtable: format, length, language | |
| 696 | - $segCount * 2, | |
| 697 | - $searchRange, | |
| 698 | - $entrySelector, | |
| 699 | - $rangeShift); | |
| 700 | - | |
| 701 | - // endCode(s) | |
| 702 | -		foreach ($range AS $start=>$subrange) { | |
| 703 | - $endCode = $start + (count($subrange) - 1); | |
| 704 | - $cmap[] = $endCode; // endCode(s) | |
| 705 | - } | |
| 706 | - $cmap[] = 0xFFFF; // endCode of last Segment | |
| 707 | - $cmap[] = 0; // reservedPad | |
| 708 | - | |
| 709 | - // startCode(s) | |
| 710 | -		foreach ($range AS $start=>$subrange) { | |
| 711 | - $cmap[] = $start; // startCode(s) | |
| 712 | - } | |
| 713 | - $cmap[] = 0xFFFF; // startCode of last Segment | |
| 714 | - // idDelta(s) | |
| 715 | -		foreach ($range AS $start=>$subrange) { | |
| 716 | - $idDelta = -($start - $subrange[0]); | |
| 717 | - $n += count($subrange); | |
| 718 | - $cmap[] = $idDelta; // idDelta(s) | |
| 719 | - } | |
| 720 | - $cmap[] = 1; // idDelta of last Segment | |
| 721 | - // idRangeOffset(s) | |
| 722 | -		foreach ($range AS $subrange) { | |
| 723 | - $cmap[] = 0; // idRangeOffset[segCount] Offset in bytes to glyph indexArray, or 0 | |
| 724 | - | |
| 725 | - } | |
| 726 | - $cmap[] = 0; // idRangeOffset of last Segment | |
| 727 | -		foreach ($range AS $subrange) { | |
| 728 | -			foreach ($subrange AS $glidx) { | |
| 729 | - $cmap[] = $glidx; | |
| 730 | - } | |
| 731 | - } | |
| 732 | - $cmap[] = 0; // Mapping for last character | |
| 733 | - $cmapstr = ''; | |
| 734 | -		foreach ($cmap AS $cm) { $cmapstr .= pack("n", $cm); } | |
| 735 | -		$this->add('cmap', $cmapstr); | |
| 736 | - | |
| 737 | - | |
| 738 | - // glyf - Glyph data | |
| 739 | -		list($glyfOffset, $glyfLength) = $this->get_table_pos('glyf'); | |
| 740 | -		if ($glyfLength < $this->maxStrLenRead) { | |
| 741 | -			$glyphData = $this->get_table('glyf'); | |
| 742 | - } | |
| 743 | - | |
| 744 | - $offsets = array(); | |
| 745 | - $glyf = ''; | |
| 746 | - $pos = 0; | |
| 747 | - | |
| 748 | - $hmtxstr = ''; | |
| 749 | - $xMinT = 0; | |
| 750 | - $yMinT = 0; | |
| 751 | - $xMaxT = 0; | |
| 752 | - $yMaxT = 0; | |
| 753 | - $advanceWidthMax = 0; | |
| 754 | - $minLeftSideBearing = 0; | |
| 755 | - $minRightSideBearing = 0; | |
| 756 | - $xMaxExtent = 0; | |
| 757 | - $maxPoints = 0; // points in non-compound glyph | |
| 758 | - $maxContours = 0; // contours in non-compound glyph | |
| 759 | - $maxComponentPoints = 0; // points in compound glyph | |
| 760 | - $maxComponentContours = 0; // contours in compound glyph | |
| 761 | - $maxComponentElements = 0; // number of glyphs referenced at top level | |
| 762 | - $maxComponentDepth = 0; // levels of recursion, set to 0 if font has only simple glyphs | |
| 763 | - $this->glyphdata = array(); | |
| 764 | - | |
| 765 | -		foreach ($subsetglyphs AS $originalGlyphIdx => $uni) { | |
| 766 | - // hmtx - Horizontal Metrics | |
| 767 | - $hm = $this->getHMetric($orignHmetrics, $originalGlyphIdx); | |
| 768 | - $hmtxstr .= $hm; | |
| 769 | - | |
| 770 | - $offsets[] = $pos; | |
| 771 | - $glyphPos = $this->glyphPos[$originalGlyphIdx]; | |
| 772 | - $glyphLen = $this->glyphPos[$originalGlyphIdx + 1] - $glyphPos; | |
| 773 | -			if ($glyfLength < $this->maxStrLenRead) { | |
| 774 | - $data = substr($glyphData, $glyphPos, $glyphLen); | |
| 775 | - } | |
| 776 | -			else { | |
| 777 | - if ($glyphLen > 0) $data = $this->get_chunk($glyfOffset + $glyphPos, $glyphLen); | |
| 778 | - else $data = ''; | |
| 779 | - } | |
| 780 | - | |
| 781 | -			if ($glyphLen > 0) { | |
| 782 | -				$up = unpack("n", substr($data, 0, 2)); | |
| 783 | - } | |
| 784 | - | |
| 785 | -			if ($glyphLen > 2 && ($up[1] & (1 << 15))) {	// If number of contours <= -1 i.e. composiste glyph | |
| 786 | - $pos_in_glyph = 10; | |
| 787 | - $flags = GF_MORE; | |
| 788 | - $nComponentElements = 0; | |
| 789 | -				while ($flags & GF_MORE) { | |
| 790 | - $nComponentElements += 1; // number of glyphs referenced at top level | |
| 791 | -					$up = unpack("n", substr($data, $pos_in_glyph, 2)); | |
| 792 | - $flags = $up[1]; | |
| 793 | -					$up = unpack("n", substr($data, $pos_in_glyph + 2, 2)); | |
| 794 | - $glyphIdx = $up[1]; | |
| 795 | - $this->glyphdata[$originalGlyphIdx]['compGlyphs'][] = $glyphIdx; | |
| 796 | - $data = $this->_set_ushort($data, $pos_in_glyph + 2, $glyphSet[$glyphIdx]); | |
| 797 | - $pos_in_glyph += 4; | |
| 798 | -					if ($flags & GF_WORDS) { $pos_in_glyph += 4; } else { $pos_in_glyph += 2; } | |
| 799 | -					if ($flags & GF_SCALE) { $pos_in_glyph += 2; } else if ($flags & GF_XYSCALE) { $pos_in_glyph += 4; } else if ($flags & GF_TWOBYTWO) { $pos_in_glyph += 8; } | |
| 800 | - } | |
| 801 | - $maxComponentElements = max($maxComponentElements, $nComponentElements); | |
| 802 | - } | |
| 803 | - | |
| 804 | - $glyf .= $data; | |
| 805 | - $pos += $glyphLen; | |
| 806 | -			if ($pos % 4 != 0) { | |
| 807 | - $padding = 4 - ($pos % 4); | |
| 808 | -				$glyf .= str_repeat("\0", $padding); | |
| 809 | - $pos += $padding; | |
| 810 | - } | |
| 811 | - } | |
| 812 | - | |
| 813 | - $offsets[] = $pos; | |
| 814 | -		$this->add('glyf', $glyf); | |
| 815 | - | |
| 816 | - // hmtx - Horizontal Metrics | |
| 817 | -		$this->add('hmtx', $hmtxstr); | |
| 818 | - | |
| 819 | - // loca - Index to location | |
| 820 | - $locastr = ''; | |
| 821 | -		if ((($pos + 1) >> 1) > 0xFFFF) { | |
| 822 | - $indexToLocFormat = 1; // long format | |
| 823 | -			foreach ($offsets AS $offset) { $locastr .= pack("N", $offset); } | |
| 824 | - } | |
| 825 | -		else { | |
| 826 | - $indexToLocFormat = 0; // short format | |
| 827 | -			foreach ($offsets AS $offset) { $locastr .= pack("n", ($offset / 2)); } | |
| 828 | - } | |
| 829 | -		$this->add('loca', $locastr); | |
| 830 | - | |
| 831 | - // head - Font header | |
| 832 | -		$head = $this->get_table('head'); | |
| 833 | - $head = $this->_set_ushort($head, 50, $indexToLocFormat); | |
| 834 | -		$this->add('head', $head); | |
| 835 | - | |
| 836 | - | |
| 837 | - // hhea - Horizontal Header | |
| 838 | -		$hhea = $this->get_table('hhea'); | |
| 839 | - $hhea = $this->_set_ushort($hhea, 34, $numberOfHMetrics); | |
| 840 | -		$this->add('hhea', $hhea); | |
| 841 | - | |
| 842 | - // maxp - Maximum Profile | |
| 843 | -		$maxp = $this->get_table('maxp'); | |
| 844 | - $maxp = $this->_set_ushort($maxp, 4, $numGlyphs); | |
| 845 | -		$this->add('maxp', $maxp); | |
| 846 | - | |
| 847 | - | |
| 848 | - // OS/2 - OS/2 | |
| 849 | -		$os2 = $this->get_table('OS/2'); | |
| 850 | -		$this->add('OS/2', $os2); | |
| 851 | - | |
| 852 | - fclose($this->fh); | |
| 853 | - | |
| 854 | - // Put the TTF file together | |
| 855 | - $stm = ''; | |
| 856 | - $this->endTTFile($stm); | |
| 857 | - return $stm; | |
| 858 | - } | |
| 859 | - | |
| 860 | - ////////////////////////////////////////////////////////////////////////////////// | |
| 861 | - // Recursively get composite glyph data | |
| 862 | -	function getGlyphData($originalGlyphIdx, &$maxdepth, &$depth, &$points, &$contours) { | |
| 863 | - $depth++; | |
| 864 | - $maxdepth = max($maxdepth, $depth); | |
| 865 | -		if (count($this->glyphdata[$originalGlyphIdx]['compGlyphs'])) { | |
| 866 | -			foreach ($this->glyphdata[$originalGlyphIdx]['compGlyphs'] AS $glyphIdx) { | |
| 867 | - $this->getGlyphData($glyphIdx, $maxdepth, $depth, $points, $contours); | |
| 868 | - } | |
| 869 | -		} else if (($this->glyphdata[$originalGlyphIdx]['nContours'] > 0) && $depth > 0) {	// simple | |
| 870 | - $contours += $this->glyphdata[$originalGlyphIdx]['nContours']; | |
| 871 | - $points += $this->glyphdata[$originalGlyphIdx]['nPoints']; | |
| 872 | - } | |
| 873 | - $depth--; | |
| 874 | - } | |
| 875 | - | |
| 876 | - | |
| 877 | - ////////////////////////////////////////////////////////////////////////////////// | |
| 878 | - // Recursively get composite glyphs | |
| 879 | -	function getGlyphs($originalGlyphIdx, &$start, &$glyphSet, &$subsetglyphs) { | |
| 880 | - $glyphPos = $this->glyphPos[$originalGlyphIdx]; | |
| 881 | - $glyphLen = $this->glyphPos[$originalGlyphIdx + 1] - $glyphPos; | |
| 882 | -		if (!$glyphLen) {  | |
| 883 | - return; | |
| 884 | - } | |
| 885 | - $this->seek($start + $glyphPos); | |
| 886 | - $numberOfContours = $this->read_short(); | |
| 887 | -		if ($numberOfContours < 0) { | |
| 888 | - $this->skip(8); | |
| 889 | - $flags = GF_MORE; | |
| 890 | -			while ($flags & GF_MORE) { | |
| 891 | - $flags = $this->read_ushort(); | |
| 892 | - $glyphIdx = $this->read_ushort(); | |
| 893 | -				if (!isset($glyphSet[$glyphIdx])) { | |
| 894 | - $glyphSet[$glyphIdx] = count($subsetglyphs); // old glyphID to new glyphID | |
| 895 | - $subsetglyphs[$glyphIdx] = true; | |
| 896 | - } | |
| 897 | - $savepos = ftell($this->fh); | |
| 898 | - $this->getGlyphs($glyphIdx, $start, $glyphSet, $subsetglyphs); | |
| 899 | - $this->seek($savepos); | |
| 900 | -				if ($flags & GF_WORDS) { | |
| 901 | - $this->skip(4); | |
| 902 | -				} else { | |
| 903 | - $this->skip(2); | |
| 904 | - } | |
| 905 | -				if ($flags & GF_SCALE) { | |
| 906 | - $this->skip(2); | |
| 907 | -				} else if ($flags & GF_XYSCALE) { | |
| 908 | - $this->skip(4); | |
| 909 | -				} else if ($flags & GF_TWOBYTWO) { | |
| 910 | - $this->skip(8); | |
| 911 | - } | |
| 912 | - } | |
| 913 | - } | |
| 914 | - } | |
| 915 | - | |
| 916 | - ////////////////////////////////////////////////////////////////////////////////// | |
| 917 | - | |
| 918 | - /** | |
| 919 | - * @param integer $numberOfHMetrics | |
| 920 | - * @param integer $numGlyphs | |
| 921 | - * @param integer $scale | |
| 922 | - */ | |
| 923 | -	function getHMTX($numberOfHMetrics, $numGlyphs, &$glyphToChar, $scale) { | |
| 924 | -		$start = $this->seek_table("hmtx"); | |
| 925 | - $aw = 0; | |
| 926 | -		$this->charWidths = str_pad('', 256 * 256 * 2, "\x00"); | |
| 927 | - $nCharWidths = 0; | |
| 928 | -		if (($numberOfHMetrics * 4) < $this->maxStrLenRead) { | |
| 929 | - $data = $this->get_chunk($start, ($numberOfHMetrics * 4)); | |
| 930 | -			$arr = unpack("n*", $data); | |
| 931 | - } | |
| 932 | -		else { $this->seek($start); } | |
| 933 | -		for ($glyph = 0; $glyph < $numberOfHMetrics; $glyph++) { | |
| 934 | - | |
| 935 | -			if (($numberOfHMetrics * 4) < $this->maxStrLenRead) { | |
| 936 | - $aw = $arr[($glyph * 2) + 1]; | |
| 937 | - } | |
| 938 | -			else { | |
| 939 | - $aw = $this->read_ushort(); | |
| 940 | - $lsb = $this->read_ushort(); | |
| 941 | - } | |
| 942 | -			if (isset($glyphToChar[$glyph]) || $glyph == 0) { | |
| 943 | - | |
| 944 | -				if ($aw >= (1 << 15)) { $aw = 0; }	// 1.03 Some (arabic) fonts have -ve values for width | |
| 945 | - // although should be unsigned value - comes out as e.g. 65108 (intended -50) | |
| 946 | -				if ($glyph == 0) { | |
| 947 | - $this->defaultWidth = $scale * $aw; | |
| 948 | - continue; | |
| 949 | - } | |
| 950 | -				foreach ($glyphToChar[$glyph] AS $char) { | |
| 951 | -					if ($char != 0 && $char != 65535) { | |
| 952 | - $w = intval(round($scale * $aw)); | |
| 953 | -						if ($w == 0) { $w = 65535; } | |
| 954 | -						if ($char < 196608) { | |
| 955 | - $this->charWidths[$char * 2] = chr($w >> 8); | |
| 956 | - $this->charWidths[$char * 2 + 1] = chr($w & 0xFF); | |
| 957 | - $nCharWidths++; | |
| 958 | - } | |
| 959 | - } | |
| 960 | - } | |
| 961 | - } | |
| 962 | - } | |
| 963 | - $data = $this->get_chunk(($start + $numberOfHMetrics * 4), ($numGlyphs * 2)); | |
| 964 | -		$arr = unpack("n*", $data); | |
| 965 | - $diff = $numGlyphs - $numberOfHMetrics; | |
| 966 | -		for ($pos = 0; $pos < $diff; $pos++) { | |
| 967 | - $glyph = $pos + $numberOfHMetrics; | |
| 968 | -			if (isset($glyphToChar[$glyph])) { | |
| 969 | -				foreach ($glyphToChar[$glyph] AS $char) { | |
| 970 | -					if ($char != 0 && $char != 65535) { | |
| 971 | - $w = intval(round($scale * $aw)); | |
| 972 | -						if ($w == 0) { $w = 65535; } | |
| 973 | -						if ($char < 196608) { | |
| 974 | - $this->charWidths[$char * 2] = chr($w >> 8); | |
| 975 | - $this->charWidths[$char * 2 + 1] = chr($w & 0xFF); | |
| 976 | - $nCharWidths++; | |
| 977 | - } | |
| 978 | - } | |
| 979 | - } | |
| 980 | - } | |
| 981 | - } | |
| 982 | - // NB 65535 is a set width of 0 | |
| 983 | - // First bytes define number of chars in font | |
| 984 | - $this->charWidths[0] = chr($nCharWidths >> 8); | |
| 985 | - $this->charWidths[1] = chr($nCharWidths & 0xFF); | |
| 986 | - } | |
| 987 | - | |
| 988 | - /** | |
| 989 | - * @param integer $numberOfHMetrics | |
| 990 | - */ | |
| 991 | -	function getHMetric($numberOfHMetrics, $gid) { | |
| 992 | -		$start = $this->seek_table("hmtx"); | |
| 993 | -		if ($gid < $numberOfHMetrics) { | |
| 994 | - $this->seek($start + ($gid * 4)); | |
| 995 | - $hm = fread($this->fh, 4); | |
| 996 | - } | |
| 997 | -		else { | |
| 998 | - $this->seek($start + (($numberOfHMetrics - 1) * 4)); | |
| 999 | - $hm = fread($this->fh, 2); | |
| 1000 | - $this->seek($start + ($numberOfHMetrics * 2) + ($gid * 2)); | |
| 1001 | - $hm .= fread($this->fh, 2); | |
| 1002 | - } | |
| 1003 | - return $hm; | |
| 1004 | - } | |
| 1005 | - | |
| 1006 | - /** | |
| 1007 | - * @param integer $indexToLocFormat | |
| 1008 | - * @param integer $numGlyphs | |
| 1009 | - */ | |
| 1010 | -	function getLOCA($indexToLocFormat, $numGlyphs) { | |
| 1011 | -		$start = $this->seek_table('loca'); | |
| 1012 | - $this->glyphPos = array(); | |
| 1013 | -		if ($indexToLocFormat == 0) { | |
| 1014 | - $data = $this->get_chunk($start, ($numGlyphs * 2) + 2); | |
| 1015 | -			$arr = unpack("n*", $data); | |
| 1016 | -			for ($n = 0; $n <= $numGlyphs; $n++) { | |
| 1017 | - $this->glyphPos[] = ($arr[$n + 1] * 2); | |
| 1018 | - } | |
| 1019 | - } | |
| 1020 | -		else if ($indexToLocFormat == 1) { | |
| 1021 | - $data = $this->get_chunk($start, ($numGlyphs * 4) + 4); | |
| 1022 | -			$arr = unpack("N*", $data); | |
| 1023 | -			for ($n = 0; $n <= $numGlyphs; $n++) { | |
| 1024 | - $this->glyphPos[] = ($arr[$n + 1]); | |
| 1025 | - } | |
| 1026 | - } | |
| 1027 | - else | |
| 1028 | -			die('Unknown location table format '.$indexToLocFormat); | |
| 1029 | - } | |
| 1030 | - | |
| 1031 | - | |
| 1032 | - // CMAP Format 4 | |
| 1033 | - | |
| 1034 | - /** | |
| 1035 | - * @param integer $unicode_cmap_offset | |
| 1036 | - */ | |
| 1037 | -	function getCMAP4($unicode_cmap_offset, &$glyphToChar, &$charToGlyph ) { | |
| 1038 | - $this->maxUniChar = 0; | |
| 1039 | - $this->seek($unicode_cmap_offset + 2); | |
| 1040 | - $length = $this->read_ushort(); | |
| 1041 | - $limit = $unicode_cmap_offset + $length; | |
| 1042 | - $this->skip(2); | |
| 1043 | - | |
| 1044 | - $segCount = $this->read_ushort() / 2; | |
| 1045 | - $this->skip(6); | |
| 1046 | - $endCount = array(); | |
| 1047 | -		for ($i = 0; $i < $segCount; $i++) { $endCount[] = $this->read_ushort(); } | |
| 1048 | - $this->skip(2); | |
| 1049 | - $startCount = array(); | |
| 1050 | -		for ($i = 0; $i < $segCount; $i++) { $startCount[] = $this->read_ushort(); } | |
| 1051 | - $idDelta = array(); | |
| 1052 | -		for ($i = 0; $i < $segCount; $i++) { $idDelta[] = $this->read_short(); }		// ???? was unsigned short | |
| 1053 | - $idRangeOffset_start = $this->_pos; | |
| 1054 | - $idRangeOffset = array(); | |
| 1055 | -		for ($i = 0; $i < $segCount; $i++) { $idRangeOffset[] = $this->read_ushort(); } | |
| 1056 | - | |
| 1057 | -		for ($n = 0; $n < $segCount; $n++) { | |
| 1058 | - $endpoint = ($endCount[$n] + 1); | |
| 1059 | -			for ($unichar = $startCount[$n]; $unichar < $endpoint; $unichar++) { | |
| 1060 | - if ($idRangeOffset[$n] == 0) | |
| 1061 | - $glyph = ($unichar + $idDelta[$n]) & 0xFFFF; | |
| 1062 | -				else { | |
| 1063 | - $offset = ($unichar - $startCount[$n]) * 2 + $idRangeOffset[$n]; | |
| 1064 | - $offset = $idRangeOffset_start + 2 * $n + $offset; | |
| 1065 | - if ($offset >= $limit) | |
| 1066 | - $glyph = 0; | |
| 1067 | -					else { | |
| 1068 | - $glyph = $this->get_ushort($offset); | |
| 1069 | - if ($glyph != 0) | |
| 1070 | - $glyph = ($glyph + $idDelta[$n]) & 0xFFFF; | |
| 1071 | - } | |
| 1072 | - } | |
| 1073 | - $charToGlyph[$unichar] = $glyph; | |
| 1074 | -				if ($unichar < 196608) { $this->maxUniChar = max($unichar, $this->maxUniChar); } | |
| 1075 | - $glyphToChar[$glyph][] = $unichar; | |
| 1076 | - } | |
| 1077 | - } | |
| 1078 | - } | |
| 1079 | - | |
| 1080 | - | |
| 1081 | - // Put the TTF file together | |
| 1082 | - | |
| 1083 | - /** | |
| 1084 | - * @param string $stm | |
| 1085 | - */ | |
| 1086 | -	function endTTFile(&$stm) { | |
| 1087 | - $stm = ''; | |
| 1088 | - $numTables = count($this->otables); | |
| 1089 | - $searchRange = 1; | |
| 1090 | - $entrySelector = 0; | |
| 1091 | -		while ($searchRange * 2 <= $numTables) { | |
| 1092 | - $searchRange = $searchRange * 2; | |
| 1093 | - $entrySelector = $entrySelector + 1; | |
| 1094 | - } | |
| 1095 | - $searchRange = $searchRange * 16; | |
| 1096 | - $rangeShift = $numTables * 16 - $searchRange; | |
| 1097 | - | |
| 1098 | - // Header | |
| 1099 | -		if (_TTF_MAC_HEADER) { | |
| 1100 | -			$stm .= (pack("Nnnnn", 0x74727565, $numTables, $searchRange, $entrySelector, $rangeShift)); // Mac | |
| 1101 | - } | |
| 1102 | -		else { | |
| 1103 | -			$stm .= (pack("Nnnnn", 0x00010000, $numTables, $searchRange, $entrySelector, $rangeShift)); // Windows | |
| 1104 | - } | |
| 1105 | - | |
| 1106 | - // Table directory | |
| 1107 | - $tables = $this->otables; | |
| 1108 | - | |
| 1109 | - ksort($tables); | |
| 1110 | - $offset = 12 + $numTables * 16; | |
| 1111 | -		foreach ($tables AS $tag=>$data) { | |
| 1112 | -			if ($tag == 'head') { $head_start = $offset; } | |
| 1113 | - $stm .= $tag; | |
| 1114 | - $checksum = $this->calcChecksum($data); | |
| 1115 | -			$stm .= pack("nn", $checksum[0], $checksum[1]); | |
| 1116 | -			$stm .= pack("NN", $offset, strlen($data)); | |
| 1117 | - $paddedLength = (strlen($data) + 3) & ~3; | |
| 1118 | - $offset = $offset + $paddedLength; | |
| 1119 | - } | |
| 1120 | - | |
| 1121 | - // Table data | |
| 1122 | -		foreach ($tables AS $tag=>$data) { | |
| 1123 | - $data .= "\0\0\0"; | |
| 1124 | - $stm .= substr($data, 0, (strlen($data) & ~3)); | |
| 1125 | - } | |
| 1126 | - | |
| 1127 | - $checksum = $this->calcChecksum($stm); | |
| 1128 | - $checksum = $this->sub32(array(0xB1B0, 0xAFBA), $checksum); | |
| 1129 | -		$chk = pack("nn", $checksum[0], $checksum[1]); | |
| 1130 | - $stm = $this->splice($stm, ($head_start + 8), $chk); | |
| 1131 | - return $stm; | |
| 1132 | - } | |
| 524 | + /** | |
| 525 | + * @return string | |
| 526 | + */ | |
| 527 | +    function makeSubset($file, &$subset) { | |
| 528 | + $this->filename = $file; | |
| 529 | +        $this->fh = fopen($file, 'rb') or die('Can\'t open file '.$file); | |
| 530 | + $this->_pos = 0; | |
| 531 | + $this->charWidths = ''; | |
| 532 | + $this->glyphPos = array(); | |
| 533 | + $this->charToGlyph = array(); | |
| 534 | + $this->tables = array(); | |
| 535 | + $this->otables = array(); | |
| 536 | + $this->ascent = 0; | |
| 537 | + $this->descent = 0; | |
| 538 | + $this->skip(4); | |
| 539 | + $this->maxUni = 0; | |
| 540 | + $this->readTableDirectory(); | |
| 541 | + | |
| 542 | + | |
| 543 | + /////////////////////////////////// | |
| 544 | + // head - Font header table | |
| 545 | + /////////////////////////////////// | |
| 546 | +        $this->seek_table("head"); | |
| 547 | + $this->skip(50); | |
| 548 | + $indexToLocFormat = $this->read_ushort(); | |
| 549 | + $glyphDataFormat = $this->read_ushort(); | |
| 550 | + | |
| 551 | + /////////////////////////////////// | |
| 552 | + // hhea - Horizontal header table | |
| 553 | + /////////////////////////////////// | |
| 554 | +        $this->seek_table("hhea"); | |
| 555 | + $this->skip(32); | |
| 556 | + $metricDataFormat = $this->read_ushort(); | |
| 557 | + $orignHmetrics = $numberOfHMetrics = $this->read_ushort(); | |
| 558 | + | |
| 559 | + /////////////////////////////////// | |
| 560 | + // maxp - Maximum profile table | |
| 561 | + /////////////////////////////////// | |
| 562 | +        $this->seek_table("maxp"); | |
| 563 | + $this->skip(4); | |
| 564 | + $numGlyphs = $this->read_ushort(); | |
| 565 | + | |
| 566 | + | |
| 567 | + /////////////////////////////////// | |
| 568 | + // cmap - Character to glyph index mapping table | |
| 569 | + /////////////////////////////////// | |
| 570 | +        $cmap_offset = $this->seek_table("cmap"); | |
| 571 | + $this->skip(2); | |
| 572 | + $cmapTableCount = $this->read_ushort(); | |
| 573 | + $unicode_cmap_offset = 0; | |
| 574 | +        for ($i = 0; $i < $cmapTableCount; $i++) { | |
| 575 | + $platformID = $this->read_ushort(); | |
| 576 | + $encodingID = $this->read_ushort(); | |
| 577 | + $offset = $this->read_ulong(); | |
| 578 | + $save_pos = $this->_pos; | |
| 579 | +            if (($platformID == 3 && $encodingID == 1) || $platformID == 0) { // Microsoft, Unicode | |
| 580 | + $format = $this->get_ushort($cmap_offset + $offset); | |
| 581 | +                if ($format == 4) { | |
| 582 | + $unicode_cmap_offset = $cmap_offset + $offset; | |
| 583 | + break; | |
| 584 | + } | |
| 585 | + } | |
| 586 | + $this->seek($save_pos); | |
| 587 | + } | |
| 588 | + | |
| 589 | + if (!$unicode_cmap_offset) | |
| 590 | +            die('Font ('.$this->filename.') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); | |
| 591 | + | |
| 592 | + | |
| 593 | + $glyphToChar = array(); | |
| 594 | + $charToGlyph = array(); | |
| 595 | + $this->getCMAP4($unicode_cmap_offset, $glyphToChar, $charToGlyph); | |
| 596 | + | |
| 597 | + $this->charToGlyph = $charToGlyph; | |
| 598 | + | |
| 599 | + /////////////////////////////////// | |
| 600 | + // hmtx - Horizontal metrics table | |
| 601 | + /////////////////////////////////// | |
| 602 | + $scale = 1; // not used | |
| 603 | + $this->getHMTX($numberOfHMetrics, $numGlyphs, $glyphToChar, $scale); | |
| 604 | + | |
| 605 | + /////////////////////////////////// | |
| 606 | + // loca - Index to location | |
| 607 | + /////////////////////////////////// | |
| 608 | + $this->getLOCA($indexToLocFormat, $numGlyphs); | |
| 609 | + | |
| 610 | + $subsetglyphs = array(0=>0); | |
| 611 | + $subsetCharToGlyph = array(); | |
| 612 | +        foreach ($subset AS $code) { | |
| 613 | +            if (isset($this->charToGlyph[$code])) { | |
| 614 | + $subsetglyphs[$this->charToGlyph[$code]] = $code; // Old Glyph ID => Unicode | |
| 615 | + $subsetCharToGlyph[$code] = $this->charToGlyph[$code]; // Unicode to old GlyphID | |
| 616 | + | |
| 617 | + } | |
| 618 | + $this->maxUni = max($this->maxUni, $code); | |
| 619 | + } | |
| 620 | + | |
| 621 | +        list($start, $dummy) = $this->get_table_pos('glyf'); | |
| 622 | + | |
| 623 | + $glyphSet = array(); | |
| 624 | + ksort($subsetglyphs); | |
| 625 | + $n = 0; | |
| 626 | + $fsLastCharIndex = 0; // maximum Unicode index (character code) in this font, according to the cmap subtable for platform ID 3 and platform- specific encoding ID 0 or 1. | |
| 627 | +        foreach ($subsetglyphs AS $originalGlyphIdx => $uni) { | |
| 628 | + $fsLastCharIndex = max($fsLastCharIndex, $uni); | |
| 629 | + $glyphSet[$originalGlyphIdx] = $n; // old glyphID to new glyphID | |
| 630 | + $n++; | |
| 631 | + } | |
| 632 | + | |
| 633 | + ksort($subsetCharToGlyph); | |
| 634 | +        foreach ($subsetCharToGlyph AS $uni => $originalGlyphIdx) { | |
| 635 | + $codeToGlyph[$uni] = $glyphSet[$originalGlyphIdx]; | |
| 636 | + } | |
| 637 | + $this->codeToGlyph = $codeToGlyph; | |
| 638 | + | |
| 639 | + ksort($subsetglyphs); | |
| 640 | +        foreach ($subsetglyphs AS $originalGlyphIdx => $uni) { | |
| 641 | + $this->getGlyphs($originalGlyphIdx, $start, $glyphSet, $subsetglyphs); | |
| 642 | + } | |
| 643 | + | |
| 644 | + $numGlyphs = $numberOfHMetrics = count($subsetglyphs); | |
| 645 | + | |
| 646 | + //tables copied from the original | |
| 647 | +        $tags = array('name'); | |
| 648 | +        foreach ($tags AS $tag) { $this->add($tag, $this->get_table($tag)); } | |
| 649 | +        $tags = array('cvt ', 'fpgm', 'prep', 'gasp'); | |
| 650 | +        foreach ($tags AS $tag) { | |
| 651 | +            if (isset($this->tables[$tag])) { $this->add($tag, $this->get_table($tag)); } | |
| 652 | + } | |
| 653 | + | |
| 654 | + // post - PostScript | |
| 655 | +        $opost = $this->get_table('post'); | |
| 656 | + $post = "\x00\x03\x00\x00".substr($opost, 4, 12)."\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; | |
| 657 | +        $this->add('post', $post); | |
| 658 | + | |
| 659 | + // Sort CID2GID map into segments of contiguous codes | |
| 660 | + ksort($codeToGlyph); | |
| 661 | + unset($codeToGlyph[0]); | |
| 662 | + //unset($codeToGlyph[65535]); | |
| 663 | + $rangeid = 0; | |
| 664 | + $range = array(); | |
| 665 | + $prevcid = -2; | |
| 666 | + $prevglidx = -1; | |
| 667 | + // for each character | |
| 668 | +        foreach ($codeToGlyph as $cid => $glidx) { | |
| 669 | +            if ($cid == ($prevcid + 1) && $glidx == ($prevglidx + 1)) { | |
| 670 | + $range[$rangeid][] = $glidx; | |
| 671 | +            } else { | |
| 672 | + // new range | |
| 673 | + $rangeid = $cid; | |
| 674 | + $range[$rangeid] = array(); | |
| 675 | + $range[$rangeid][] = $glidx; | |
| 676 | + } | |
| 677 | + $prevcid = $cid; | |
| 678 | + $prevglidx = $glidx; | |
| 679 | + } | |
| 680 | + | |
| 681 | + // cmap - Character to glyph mapping - Format 4 (MS / ) | |
| 682 | + $segCount = count($range) + 1; // + 1 Last segment has missing character 0xFFFF | |
| 683 | + $searchRange = 1; | |
| 684 | + $entrySelector = 0; | |
| 685 | +        while ($searchRange * 2 <= $segCount) { | |
| 686 | + $searchRange = $searchRange * 2; | |
| 687 | + $entrySelector = $entrySelector + 1; | |
| 688 | + } | |
| 689 | + $searchRange = $searchRange * 2; | |
| 690 | + $rangeShift = $segCount * 2 - $searchRange; | |
| 691 | + $length = 16 + (8 * $segCount) + ($numGlyphs + 1); | |
| 692 | + $cmap = array(0, 1, // Index : version, number of encoding subtables | |
| 693 | + 3, 1, // Encoding Subtable : platform (MS=3), encoding (Unicode) | |
| 694 | + 0, 12, // Encoding Subtable : offset (hi,lo) | |
| 695 | + 4, $length, 0, // Format 4 Mapping subtable: format, length, language | |
| 696 | + $segCount * 2, | |
| 697 | + $searchRange, | |
| 698 | + $entrySelector, | |
| 699 | + $rangeShift); | |
| 700 | + | |
| 701 | + // endCode(s) | |
| 702 | +        foreach ($range AS $start=>$subrange) { | |
| 703 | + $endCode = $start + (count($subrange) - 1); | |
| 704 | + $cmap[] = $endCode; // endCode(s) | |
| 705 | + } | |
| 706 | + $cmap[] = 0xFFFF; // endCode of last Segment | |
| 707 | + $cmap[] = 0; // reservedPad | |
| 708 | + | |
| 709 | + // startCode(s) | |
| 710 | +        foreach ($range AS $start=>$subrange) { | |
| 711 | + $cmap[] = $start; // startCode(s) | |
| 712 | + } | |
| 713 | + $cmap[] = 0xFFFF; // startCode of last Segment | |
| 714 | + // idDelta(s) | |
| 715 | +        foreach ($range AS $start=>$subrange) { | |
| 716 | + $idDelta = -($start - $subrange[0]); | |
| 717 | + $n += count($subrange); | |
| 718 | + $cmap[] = $idDelta; // idDelta(s) | |
| 719 | + } | |
| 720 | + $cmap[] = 1; // idDelta of last Segment | |
| 721 | + // idRangeOffset(s) | |
| 722 | +        foreach ($range AS $subrange) { | |
| 723 | + $cmap[] = 0; // idRangeOffset[segCount] Offset in bytes to glyph indexArray, or 0 | |
| 724 | + | |
| 725 | + } | |
| 726 | + $cmap[] = 0; // idRangeOffset of last Segment | |
| 727 | +        foreach ($range AS $subrange) { | |
| 728 | +            foreach ($subrange AS $glidx) { | |
| 729 | + $cmap[] = $glidx; | |
| 730 | + } | |
| 731 | + } | |
| 732 | + $cmap[] = 0; // Mapping for last character | |
| 733 | + $cmapstr = ''; | |
| 734 | +        foreach ($cmap AS $cm) { $cmapstr .= pack("n", $cm); } | |
| 735 | +        $this->add('cmap', $cmapstr); | |
| 736 | + | |
| 737 | + | |
| 738 | + // glyf - Glyph data | |
| 739 | +        list($glyfOffset, $glyfLength) = $this->get_table_pos('glyf'); | |
| 740 | +        if ($glyfLength < $this->maxStrLenRead) { | |
| 741 | +            $glyphData = $this->get_table('glyf'); | |
| 742 | + } | |
| 743 | + | |
| 744 | + $offsets = array(); | |
| 745 | + $glyf = ''; | |
| 746 | + $pos = 0; | |
| 747 | + | |
| 748 | + $hmtxstr = ''; | |
| 749 | + $xMinT = 0; | |
| 750 | + $yMinT = 0; | |
| 751 | + $xMaxT = 0; | |
| 752 | + $yMaxT = 0; | |
| 753 | + $advanceWidthMax = 0; | |
| 754 | + $minLeftSideBearing = 0; | |
| 755 | + $minRightSideBearing = 0; | |
| 756 | + $xMaxExtent = 0; | |
| 757 | + $maxPoints = 0; // points in non-compound glyph | |
| 758 | + $maxContours = 0; // contours in non-compound glyph | |
| 759 | + $maxComponentPoints = 0; // points in compound glyph | |
| 760 | + $maxComponentContours = 0; // contours in compound glyph | |
| 761 | + $maxComponentElements = 0; // number of glyphs referenced at top level | |
| 762 | + $maxComponentDepth = 0; // levels of recursion, set to 0 if font has only simple glyphs | |
| 763 | + $this->glyphdata = array(); | |
| 764 | + | |
| 765 | +        foreach ($subsetglyphs AS $originalGlyphIdx => $uni) { | |
| 766 | + // hmtx - Horizontal Metrics | |
| 767 | + $hm = $this->getHMetric($orignHmetrics, $originalGlyphIdx); | |
| 768 | + $hmtxstr .= $hm; | |
| 769 | + | |
| 770 | + $offsets[] = $pos; | |
| 771 | + $glyphPos = $this->glyphPos[$originalGlyphIdx]; | |
| 772 | + $glyphLen = $this->glyphPos[$originalGlyphIdx + 1] - $glyphPos; | |
| 773 | +            if ($glyfLength < $this->maxStrLenRead) { | |
| 774 | + $data = substr($glyphData, $glyphPos, $glyphLen); | |
| 775 | + } | |
| 776 | +            else { | |
| 777 | + if ($glyphLen > 0) $data = $this->get_chunk($glyfOffset + $glyphPos, $glyphLen); | |
| 778 | + else $data = ''; | |
| 779 | + } | |
| 780 | + | |
| 781 | +            if ($glyphLen > 0) { | |
| 782 | +                $up = unpack("n", substr($data, 0, 2)); | |
| 783 | + } | |
| 784 | + | |
| 785 | +            if ($glyphLen > 2 && ($up[1] & (1 << 15))) {	// If number of contours <= -1 i.e. composiste glyph | |
| 786 | + $pos_in_glyph = 10; | |
| 787 | + $flags = GF_MORE; | |
| 788 | + $nComponentElements = 0; | |
| 789 | +                while ($flags & GF_MORE) { | |
| 790 | + $nComponentElements += 1; // number of glyphs referenced at top level | |
| 791 | +                    $up = unpack("n", substr($data, $pos_in_glyph, 2)); | |
| 792 | + $flags = $up[1]; | |
| 793 | +                    $up = unpack("n", substr($data, $pos_in_glyph + 2, 2)); | |
| 794 | + $glyphIdx = $up[1]; | |
| 795 | + $this->glyphdata[$originalGlyphIdx]['compGlyphs'][] = $glyphIdx; | |
| 796 | + $data = $this->_set_ushort($data, $pos_in_glyph + 2, $glyphSet[$glyphIdx]); | |
| 797 | + $pos_in_glyph += 4; | |
| 798 | +                    if ($flags & GF_WORDS) { $pos_in_glyph += 4; } else { $pos_in_glyph += 2; } | |
| 799 | +                    if ($flags & GF_SCALE) { $pos_in_glyph += 2; } else if ($flags & GF_XYSCALE) { $pos_in_glyph += 4; } else if ($flags & GF_TWOBYTWO) { $pos_in_glyph += 8; } | |
| 800 | + } | |
| 801 | + $maxComponentElements = max($maxComponentElements, $nComponentElements); | |
| 802 | + } | |
| 803 | + | |
| 804 | + $glyf .= $data; | |
| 805 | + $pos += $glyphLen; | |
| 806 | +            if ($pos % 4 != 0) { | |
| 807 | + $padding = 4 - ($pos % 4); | |
| 808 | +                $glyf .= str_repeat("\0", $padding); | |
| 809 | + $pos += $padding; | |
| 810 | + } | |
| 811 | + } | |
| 812 | + | |
| 813 | + $offsets[] = $pos; | |
| 814 | +        $this->add('glyf', $glyf); | |
| 815 | + | |
| 816 | + // hmtx - Horizontal Metrics | |
| 817 | +        $this->add('hmtx', $hmtxstr); | |
| 818 | + | |
| 819 | + // loca - Index to location | |
| 820 | + $locastr = ''; | |
| 821 | +        if ((($pos + 1) >> 1) > 0xFFFF) { | |
| 822 | + $indexToLocFormat = 1; // long format | |
| 823 | +            foreach ($offsets AS $offset) { $locastr .= pack("N", $offset); } | |
| 824 | + } | |
| 825 | +        else { | |
| 826 | + $indexToLocFormat = 0; // short format | |
| 827 | +            foreach ($offsets AS $offset) { $locastr .= pack("n", ($offset / 2)); } | |
| 828 | + } | |
| 829 | +        $this->add('loca', $locastr); | |
| 830 | + | |
| 831 | + // head - Font header | |
| 832 | +        $head = $this->get_table('head'); | |
| 833 | + $head = $this->_set_ushort($head, 50, $indexToLocFormat); | |
| 834 | +        $this->add('head', $head); | |
| 835 | + | |
| 836 | + | |
| 837 | + // hhea - Horizontal Header | |
| 838 | +        $hhea = $this->get_table('hhea'); | |
| 839 | + $hhea = $this->_set_ushort($hhea, 34, $numberOfHMetrics); | |
| 840 | +        $this->add('hhea', $hhea); | |
| 841 | + | |
| 842 | + // maxp - Maximum Profile | |
| 843 | +        $maxp = $this->get_table('maxp'); | |
| 844 | + $maxp = $this->_set_ushort($maxp, 4, $numGlyphs); | |
| 845 | +        $this->add('maxp', $maxp); | |
| 846 | + | |
| 847 | + | |
| 848 | + // OS/2 - OS/2 | |
| 849 | +        $os2 = $this->get_table('OS/2'); | |
| 850 | +        $this->add('OS/2', $os2); | |
| 851 | + | |
| 852 | + fclose($this->fh); | |
| 853 | + | |
| 854 | + // Put the TTF file together | |
| 855 | + $stm = ''; | |
| 856 | + $this->endTTFile($stm); | |
| 857 | + return $stm; | |
| 858 | + } | |
| 859 | + | |
| 860 | + ////////////////////////////////////////////////////////////////////////////////// | |
| 861 | + // Recursively get composite glyph data | |
| 862 | +    function getGlyphData($originalGlyphIdx, &$maxdepth, &$depth, &$points, &$contours) { | |
| 863 | + $depth++; | |
| 864 | + $maxdepth = max($maxdepth, $depth); | |
| 865 | +        if (count($this->glyphdata[$originalGlyphIdx]['compGlyphs'])) { | |
| 866 | +            foreach ($this->glyphdata[$originalGlyphIdx]['compGlyphs'] AS $glyphIdx) { | |
| 867 | + $this->getGlyphData($glyphIdx, $maxdepth, $depth, $points, $contours); | |
| 868 | + } | |
| 869 | +        } else if (($this->glyphdata[$originalGlyphIdx]['nContours'] > 0) && $depth > 0) {	// simple | |
| 870 | + $contours += $this->glyphdata[$originalGlyphIdx]['nContours']; | |
| 871 | + $points += $this->glyphdata[$originalGlyphIdx]['nPoints']; | |
| 872 | + } | |
| 873 | + $depth--; | |
| 874 | + } | |
| 875 | + | |
| 876 | + | |
| 877 | + ////////////////////////////////////////////////////////////////////////////////// | |
| 878 | + // Recursively get composite glyphs | |
| 879 | +    function getGlyphs($originalGlyphIdx, &$start, &$glyphSet, &$subsetglyphs) { | |
| 880 | + $glyphPos = $this->glyphPos[$originalGlyphIdx]; | |
| 881 | + $glyphLen = $this->glyphPos[$originalGlyphIdx + 1] - $glyphPos; | |
| 882 | +        if (!$glyphLen) {  | |
| 883 | + return; | |
| 884 | + } | |
| 885 | + $this->seek($start + $glyphPos); | |
| 886 | + $numberOfContours = $this->read_short(); | |
| 887 | +        if ($numberOfContours < 0) { | |
| 888 | + $this->skip(8); | |
| 889 | + $flags = GF_MORE; | |
| 890 | +            while ($flags & GF_MORE) { | |
| 891 | + $flags = $this->read_ushort(); | |
| 892 | + $glyphIdx = $this->read_ushort(); | |
| 893 | +                if (!isset($glyphSet[$glyphIdx])) { | |
| 894 | + $glyphSet[$glyphIdx] = count($subsetglyphs); // old glyphID to new glyphID | |
| 895 | + $subsetglyphs[$glyphIdx] = true; | |
| 896 | + } | |
| 897 | + $savepos = ftell($this->fh); | |
| 898 | + $this->getGlyphs($glyphIdx, $start, $glyphSet, $subsetglyphs); | |
| 899 | + $this->seek($savepos); | |
| 900 | +                if ($flags & GF_WORDS) { | |
| 901 | + $this->skip(4); | |
| 902 | +                } else { | |
| 903 | + $this->skip(2); | |
| 904 | + } | |
| 905 | +                if ($flags & GF_SCALE) { | |
| 906 | + $this->skip(2); | |
| 907 | +                } else if ($flags & GF_XYSCALE) { | |
| 908 | + $this->skip(4); | |
| 909 | +                } else if ($flags & GF_TWOBYTWO) { | |
| 910 | + $this->skip(8); | |
| 911 | + } | |
| 912 | + } | |
| 913 | + } | |
| 914 | + } | |
| 915 | + | |
| 916 | + ////////////////////////////////////////////////////////////////////////////////// | |
| 917 | + | |
| 918 | + /** | |
| 919 | + * @param integer $numberOfHMetrics | |
| 920 | + * @param integer $numGlyphs | |
| 921 | + * @param integer $scale | |
| 922 | + */ | |
| 923 | +    function getHMTX($numberOfHMetrics, $numGlyphs, &$glyphToChar, $scale) { | |
| 924 | +        $start = $this->seek_table("hmtx"); | |
| 925 | + $aw = 0; | |
| 926 | +        $this->charWidths = str_pad('', 256 * 256 * 2, "\x00"); | |
| 927 | + $nCharWidths = 0; | |
| 928 | +        if (($numberOfHMetrics * 4) < $this->maxStrLenRead) { | |
| 929 | + $data = $this->get_chunk($start, ($numberOfHMetrics * 4)); | |
| 930 | +            $arr = unpack("n*", $data); | |
| 931 | + } | |
| 932 | +        else { $this->seek($start); } | |
| 933 | +        for ($glyph = 0; $glyph < $numberOfHMetrics; $glyph++) { | |
| 934 | + | |
| 935 | +            if (($numberOfHMetrics * 4) < $this->maxStrLenRead) { | |
| 936 | + $aw = $arr[($glyph * 2) + 1]; | |
| 937 | + } | |
| 938 | +            else { | |
| 939 | + $aw = $this->read_ushort(); | |
| 940 | + $lsb = $this->read_ushort(); | |
| 941 | + } | |
| 942 | +            if (isset($glyphToChar[$glyph]) || $glyph == 0) { | |
| 943 | + | |
| 944 | +                if ($aw >= (1 << 15)) { $aw = 0; }	// 1.03 Some (arabic) fonts have -ve values for width | |
| 945 | + // although should be unsigned value - comes out as e.g. 65108 (intended -50) | |
| 946 | +                if ($glyph == 0) { | |
| 947 | + $this->defaultWidth = $scale * $aw; | |
| 948 | + continue; | |
| 949 | + } | |
| 950 | +                foreach ($glyphToChar[$glyph] AS $char) { | |
| 951 | +                    if ($char != 0 && $char != 65535) { | |
| 952 | + $w = intval(round($scale * $aw)); | |
| 953 | +                        if ($w == 0) { $w = 65535; } | |
| 954 | +                        if ($char < 196608) { | |
| 955 | + $this->charWidths[$char * 2] = chr($w >> 8); | |
| 956 | + $this->charWidths[$char * 2 + 1] = chr($w & 0xFF); | |
| 957 | + $nCharWidths++; | |
| 958 | + } | |
| 959 | + } | |
| 960 | + } | |
| 961 | + } | |
| 962 | + } | |
| 963 | + $data = $this->get_chunk(($start + $numberOfHMetrics * 4), ($numGlyphs * 2)); | |
| 964 | +        $arr = unpack("n*", $data); | |
| 965 | + $diff = $numGlyphs - $numberOfHMetrics; | |
| 966 | +        for ($pos = 0; $pos < $diff; $pos++) { | |
| 967 | + $glyph = $pos + $numberOfHMetrics; | |
| 968 | +            if (isset($glyphToChar[$glyph])) { | |
| 969 | +                foreach ($glyphToChar[$glyph] AS $char) { | |
| 970 | +                    if ($char != 0 && $char != 65535) { | |
| 971 | + $w = intval(round($scale * $aw)); | |
| 972 | +                        if ($w == 0) { $w = 65535; } | |
| 973 | +                        if ($char < 196608) { | |
| 974 | + $this->charWidths[$char * 2] = chr($w >> 8); | |
| 975 | + $this->charWidths[$char * 2 + 1] = chr($w & 0xFF); | |
| 976 | + $nCharWidths++; | |
| 977 | + } | |
| 978 | + } | |
| 979 | + } | |
| 980 | + } | |
| 981 | + } | |
| 982 | + // NB 65535 is a set width of 0 | |
| 983 | + // First bytes define number of chars in font | |
| 984 | + $this->charWidths[0] = chr($nCharWidths >> 8); | |
| 985 | + $this->charWidths[1] = chr($nCharWidths & 0xFF); | |
| 986 | + } | |
| 987 | + | |
| 988 | + /** | |
| 989 | + * @param integer $numberOfHMetrics | |
| 990 | + */ | |
| 991 | +    function getHMetric($numberOfHMetrics, $gid) { | |
| 992 | +        $start = $this->seek_table("hmtx"); | |
| 993 | +        if ($gid < $numberOfHMetrics) { | |
| 994 | + $this->seek($start + ($gid * 4)); | |
| 995 | + $hm = fread($this->fh, 4); | |
| 996 | + } | |
| 997 | +        else { | |
| 998 | + $this->seek($start + (($numberOfHMetrics - 1) * 4)); | |
| 999 | + $hm = fread($this->fh, 2); | |
| 1000 | + $this->seek($start + ($numberOfHMetrics * 2) + ($gid * 2)); | |
| 1001 | + $hm .= fread($this->fh, 2); | |
| 1002 | + } | |
| 1003 | + return $hm; | |
| 1004 | + } | |
| 1005 | + | |
| 1006 | + /** | |
| 1007 | + * @param integer $indexToLocFormat | |
| 1008 | + * @param integer $numGlyphs | |
| 1009 | + */ | |
| 1010 | +    function getLOCA($indexToLocFormat, $numGlyphs) { | |
| 1011 | +        $start = $this->seek_table('loca'); | |
| 1012 | + $this->glyphPos = array(); | |
| 1013 | +        if ($indexToLocFormat == 0) { | |
| 1014 | + $data = $this->get_chunk($start, ($numGlyphs * 2) + 2); | |
| 1015 | +            $arr = unpack("n*", $data); | |
| 1016 | +            for ($n = 0; $n <= $numGlyphs; $n++) { | |
| 1017 | + $this->glyphPos[] = ($arr[$n + 1] * 2); | |
| 1018 | + } | |
| 1019 | + } | |
| 1020 | +        else if ($indexToLocFormat == 1) { | |
| 1021 | + $data = $this->get_chunk($start, ($numGlyphs * 4) + 4); | |
| 1022 | +            $arr = unpack("N*", $data); | |
| 1023 | +            for ($n = 0; $n <= $numGlyphs; $n++) { | |
| 1024 | + $this->glyphPos[] = ($arr[$n + 1]); | |
| 1025 | + } | |
| 1026 | + } | |
| 1027 | + else | |
| 1028 | +            die('Unknown location table format '.$indexToLocFormat); | |
| 1029 | + } | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + // CMAP Format 4 | |
| 1033 | + | |
| 1034 | + /** | |
| 1035 | + * @param integer $unicode_cmap_offset | |
| 1036 | + */ | |
| 1037 | +    function getCMAP4($unicode_cmap_offset, &$glyphToChar, &$charToGlyph ) { | |
| 1038 | + $this->maxUniChar = 0; | |
| 1039 | + $this->seek($unicode_cmap_offset + 2); | |
| 1040 | + $length = $this->read_ushort(); | |
| 1041 | + $limit = $unicode_cmap_offset + $length; | |
| 1042 | + $this->skip(2); | |
| 1043 | + | |
| 1044 | + $segCount = $this->read_ushort() / 2; | |
| 1045 | + $this->skip(6); | |
| 1046 | + $endCount = array(); | |
| 1047 | +        for ($i = 0; $i < $segCount; $i++) { $endCount[] = $this->read_ushort(); } | |
| 1048 | + $this->skip(2); | |
| 1049 | + $startCount = array(); | |
| 1050 | +        for ($i = 0; $i < $segCount; $i++) { $startCount[] = $this->read_ushort(); } | |
| 1051 | + $idDelta = array(); | |
| 1052 | +        for ($i = 0; $i < $segCount; $i++) { $idDelta[] = $this->read_short(); }		// ???? was unsigned short | |
| 1053 | + $idRangeOffset_start = $this->_pos; | |
| 1054 | + $idRangeOffset = array(); | |
| 1055 | +        for ($i = 0; $i < $segCount; $i++) { $idRangeOffset[] = $this->read_ushort(); } | |
| 1056 | + | |
| 1057 | +        for ($n = 0; $n < $segCount; $n++) { | |
| 1058 | + $endpoint = ($endCount[$n] + 1); | |
| 1059 | +            for ($unichar = $startCount[$n]; $unichar < $endpoint; $unichar++) { | |
| 1060 | + if ($idRangeOffset[$n] == 0) | |
| 1061 | + $glyph = ($unichar + $idDelta[$n]) & 0xFFFF; | |
| 1062 | +                else { | |
| 1063 | + $offset = ($unichar - $startCount[$n]) * 2 + $idRangeOffset[$n]; | |
| 1064 | + $offset = $idRangeOffset_start + 2 * $n + $offset; | |
| 1065 | + if ($offset >= $limit) | |
| 1066 | + $glyph = 0; | |
| 1067 | +                    else { | |
| 1068 | + $glyph = $this->get_ushort($offset); | |
| 1069 | + if ($glyph != 0) | |
| 1070 | + $glyph = ($glyph + $idDelta[$n]) & 0xFFFF; | |
| 1071 | + } | |
| 1072 | + } | |
| 1073 | + $charToGlyph[$unichar] = $glyph; | |
| 1074 | +                if ($unichar < 196608) { $this->maxUniChar = max($unichar, $this->maxUniChar); } | |
| 1075 | + $glyphToChar[$glyph][] = $unichar; | |
| 1076 | + } | |
| 1077 | + } | |
| 1078 | + } | |
| 1079 | + | |
| 1080 | + | |
| 1081 | + // Put the TTF file together | |
| 1082 | + | |
| 1083 | + /** | |
| 1084 | + * @param string $stm | |
| 1085 | + */ | |
| 1086 | +    function endTTFile(&$stm) { | |
| 1087 | + $stm = ''; | |
| 1088 | + $numTables = count($this->otables); | |
| 1089 | + $searchRange = 1; | |
| 1090 | + $entrySelector = 0; | |
| 1091 | +        while ($searchRange * 2 <= $numTables) { | |
| 1092 | + $searchRange = $searchRange * 2; | |
| 1093 | + $entrySelector = $entrySelector + 1; | |
| 1094 | + } | |
| 1095 | + $searchRange = $searchRange * 16; | |
| 1096 | + $rangeShift = $numTables * 16 - $searchRange; | |
| 1097 | + | |
| 1098 | + // Header | |
| 1099 | +        if (_TTF_MAC_HEADER) { | |
| 1100 | +            $stm .= (pack("Nnnnn", 0x74727565, $numTables, $searchRange, $entrySelector, $rangeShift)); // Mac | |
| 1101 | + } | |
| 1102 | +        else { | |
| 1103 | +            $stm .= (pack("Nnnnn", 0x00010000, $numTables, $searchRange, $entrySelector, $rangeShift)); // Windows | |
| 1104 | + } | |
| 1105 | + | |
| 1106 | + // Table directory | |
| 1107 | + $tables = $this->otables; | |
| 1108 | + | |
| 1109 | + ksort($tables); | |
| 1110 | + $offset = 12 + $numTables * 16; | |
| 1111 | +        foreach ($tables AS $tag=>$data) { | |
| 1112 | +            if ($tag == 'head') { $head_start = $offset; } | |
| 1113 | + $stm .= $tag; | |
| 1114 | + $checksum = $this->calcChecksum($data); | |
| 1115 | +            $stm .= pack("nn", $checksum[0], $checksum[1]); | |
| 1116 | +            $stm .= pack("NN", $offset, strlen($data)); | |
| 1117 | + $paddedLength = (strlen($data) + 3) & ~3; | |
| 1118 | + $offset = $offset + $paddedLength; | |
| 1119 | + } | |
| 1120 | + | |
| 1121 | + // Table data | |
| 1122 | +        foreach ($tables AS $tag=>$data) { | |
| 1123 | + $data .= "\0\0\0"; | |
| 1124 | + $stm .= substr($data, 0, (strlen($data) & ~3)); | |
| 1125 | + } | |
| 1126 | + | |
| 1127 | + $checksum = $this->calcChecksum($stm); | |
| 1128 | + $checksum = $this->sub32(array(0xB1B0, 0xAFBA), $checksum); | |
| 1129 | +        $chk = pack("nn", $checksum[0], $checksum[1]); | |
| 1130 | + $stm = $this->splice($stm, ($head_start + 8), $chk); | |
| 1131 | + return $stm; | |
| 1132 | + } | |
| 1133 | 1133 | |
| 1134 | 1134 | |
| 1135 | 1135 | |
| @@ -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(); | 
| @@ -158,7 +158,7 @@ discard block | ||
| 158 | 158 | |
| 159 | 159 |  	function seek($pos) { | 
| 160 | 160 | $this->_pos = $pos; | 
| 161 | - fseek($this->fh,$this->_pos); | |
| 161 | + fseek($this->fh, $this->_pos); | |
| 162 | 162 | } | 
| 163 | 163 | |
| 164 | 164 | /** | 
| @@ -166,7 +166,7 @@ discard block | ||
| 166 | 166 | */ | 
| 167 | 167 |  	function skip($delta) { | 
| 168 | 168 | $this->_pos = $this->_pos + $delta; | 
| 169 | - fseek($this->fh,$this->_pos); | |
| 169 | + fseek($this->fh, $this->_pos); | |
| 170 | 170 | } | 
| 171 | 171 | |
| 172 | 172 | /** | 
| @@ -202,44 +202,44 @@ discard block | ||
| 202 | 202 | |
| 203 | 203 |  	function read_ushort() { | 
| 204 | 204 | $this->_pos += 2; | 
| 205 | - $s = fread($this->fh,2); | |
| 206 | - return (ord($s[0])<<8) + ord($s[1]); | |
| 205 | + $s = fread($this->fh, 2); | |
| 206 | + return (ord($s[0]) << 8) + ord($s[1]); | |
| 207 | 207 | } | 
| 208 | 208 | |
| 209 | 209 |  	function read_ulong() { | 
| 210 | 210 | $this->_pos += 4; | 
| 211 | - $s = fread($this->fh,4); | |
| 211 | + $s = fread($this->fh, 4); | |
| 212 | 212 | // if large uInt32 as an integer, PHP converts it to -ve | 
| 213 | - return (ord($s[0])*16777216) + (ord($s[1])<<16) + (ord($s[2])<<8) + ord($s[3]); // 16777216 = 1<<24 | |
| 213 | + return (ord($s[0]) * 16777216) + (ord($s[1]) << 16) + (ord($s[2]) << 8) + ord($s[3]); // 16777216 = 1<<24 | |
| 214 | 214 | } | 
| 215 | 215 | |
| 216 | 216 |  	function get_ushort($pos) { | 
| 217 | - fseek($this->fh,$pos); | |
| 218 | - $s = fread($this->fh,2); | |
| 219 | - return (ord($s[0])<<8) + ord($s[1]); | |
| 217 | + fseek($this->fh, $pos); | |
| 218 | + $s = fread($this->fh, 2); | |
| 219 | + return (ord($s[0]) << 8) + ord($s[1]); | |
| 220 | 220 | } | 
| 221 | 221 | |
| 222 | 222 |  	function get_ulong($pos) { | 
| 223 | - fseek($this->fh,$pos); | |
| 224 | - $s = fread($this->fh,4); | |
| 223 | + fseek($this->fh, $pos); | |
| 224 | + $s = fread($this->fh, 4); | |
| 225 | 225 | // iF large uInt32 as an integer, PHP converts it to -ve | 
| 226 | - return (ord($s[0])*16777216) + (ord($s[1])<<16) + (ord($s[2])<<8) + ord($s[3]); // 16777216 = 1<<24 | |
| 226 | + return (ord($s[0]) * 16777216) + (ord($s[1]) << 16) + (ord($s[2]) << 8) + ord($s[3]); // 16777216 = 1<<24 | |
| 227 | 227 | } | 
| 228 | 228 | |
| 229 | 229 |  	function pack_short($val) { | 
| 230 | -		if ($val<0) {  | |
| 230 | +		if ($val < 0) {  | |
| 231 | 231 | $val = abs($val); | 
| 232 | 232 | $val = ~$val; | 
| 233 | 233 | $val += 1; | 
| 234 | 234 | } | 
| 235 | -		return pack("n",$val);  | |
| 235 | +		return pack("n", $val);  | |
| 236 | 236 | } | 
| 237 | 237 | |
| 238 | 238 | /** | 
| 239 | 239 | * @param string $value | 
| 240 | 240 | */ | 
| 241 | 241 |  	function splice($stream, $offset, $value) { | 
| 242 | - return substr($stream,0,$offset) . $value . substr($stream,$offset+strlen($value)); | |
| 242 | + return substr($stream, 0, $offset).$value.substr($stream, $offset + strlen($value)); | |
| 243 | 243 | } | 
| 244 | 244 | |
| 245 | 245 | /** | 
| @@ -252,19 +252,19 @@ discard block | ||
| 252 | 252 | } | 
| 253 | 253 | |
| 254 | 254 |  	function _set_short($stream, $offset, $val) { | 
| 255 | -		if ($val<0) {  | |
| 255 | +		if ($val < 0) {  | |
| 256 | 256 | $val = abs($val); | 
| 257 | 257 | $val = ~$val; | 
| 258 | 258 | $val += 1; | 
| 259 | 259 | } | 
| 260 | -		$up = pack("n",$val);  | |
| 260 | +		$up = pack("n", $val);  | |
| 261 | 261 | return $this->splice($stream, $offset, $up); | 
| 262 | 262 | } | 
| 263 | 263 | |
| 264 | 264 |  	function get_chunk($pos, $length) { | 
| 265 | - fseek($this->fh,$pos); | |
| 266 | -		if ($length <1) { return ''; } | |
| 267 | - return (fread($this->fh,$length)); | |
| 265 | + fseek($this->fh, $pos); | |
| 266 | +		if ($length < 1) { return ''; } | |
| 267 | + return (fread($this->fh, $length)); | |
| 268 | 268 | } | 
| 269 | 269 | |
| 270 | 270 | /** | 
| @@ -273,8 +273,8 @@ discard block | ||
| 273 | 273 |  	function get_table($tag) { | 
| 274 | 274 | list($pos, $length) = $this->get_table_pos($tag); | 
| 275 | 275 |  		if ($length == 0) { die('Truetype font ('.$this->filename.'): error reading table: '.$tag); } | 
| 276 | - fseek($this->fh,$pos); | |
| 277 | - return (fread($this->fh,$length)); | |
| 276 | + fseek($this->fh, $pos); | |
| 277 | + return (fread($this->fh, $length)); | |
| 278 | 278 | } | 
| 279 | 279 | |
| 280 | 280 | /** | 
| @@ -1034,7 +1034,7 @@ discard block | ||
| 1034 | 1034 | /** | 
| 1035 | 1035 | * @param integer $unicode_cmap_offset | 
| 1036 | 1036 | */ | 
| 1037 | -	function getCMAP4($unicode_cmap_offset, &$glyphToChar, &$charToGlyph ) { | |
| 1037 | +	function getCMAP4($unicode_cmap_offset, &$glyphToChar, &$charToGlyph) { | |
| 1038 | 1038 | $this->maxUniChar = 0; | 
| 1039 | 1039 | $this->seek($unicode_cmap_offset + 2); | 
| 1040 | 1040 | $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); | 
| @@ -319,13 +322,16 @@ discard block | ||
| 319 | 322 | $nameId = $this->read_ushort(); | 
| 320 | 323 | $length = $this->read_ushort(); | 
| 321 | 324 | $offset = $this->read_ushort(); | 
| 322 | - if (!in_array($nameId, $K)) continue; | |
| 325 | +				if (!in_array($nameId, $K)) { | |
| 326 | + continue; | |
| 327 | + } | |
| 323 | 328 | $N = ''; | 
| 324 | 329 |  				if ($platformId == 3 && $encodingId == 1 && $languageId == 0x409) { // Microsoft, Unicode, US English, PS Name | 
| 325 | 330 | $opos = $this->_pos; | 
| 326 | 331 | $this->seek($string_data_offset + $offset); | 
| 327 | - if ($length % 2 != 0) | |
| 328 | -						die("PostScript name is UTF-16BE string of odd length"); | |
| 332 | +					if ($length % 2 != 0) { | |
| 333 | +											die("PostScript name is UTF-16BE string of odd length"); | |
| 334 | + } | |
| 329 | 335 | $length /= 2; | 
| 330 | 336 | $N = ''; | 
| 331 | 337 |  					while ($length > 0) { | 
| @@ -344,19 +350,23 @@ discard block | ||
| 344 | 350 |  				if ($N && $names[$nameId] == '') { | 
| 345 | 351 | $names[$nameId] = $N; | 
| 346 | 352 | $nameCount -= 1; | 
| 347 | - if ($nameCount == 0) break; | |
| 353 | +					if ($nameCount == 0) { | |
| 354 | + break; | |
| 355 | + } | |
| 348 | 356 | } | 
| 349 | 357 | } | 
| 350 | - if ($names[6]) | |
| 351 | - $psName = $names[6]; | |
| 352 | - else if ($names[4]) | |
| 353 | -				$psName = preg_replace('/ /', '-', $names[4]); | |
| 354 | - else if ($names[1]) | |
| 355 | -				$psName = preg_replace('/ /', '-', $names[1]); | |
| 356 | - else | |
| 357 | - $psName = ''; | |
| 358 | - if (!$psName) | |
| 359 | -				die("Could not find PostScript font name"); | |
| 358 | +			if ($names[6]) { | |
| 359 | + $psName = $names[6]; | |
| 360 | +			} else if ($names[4]) { | |
| 361 | +							$psName = preg_replace('/ /', '-', $names[4]); | |
| 362 | +			} else if ($names[1]) { | |
| 363 | +							$psName = preg_replace('/ /', '-', $names[1]); | |
| 364 | +			} else { | |
| 365 | + $psName = ''; | |
| 366 | + } | |
| 367 | +			if (!$psName) { | |
| 368 | +							die("Could not find PostScript font name"); | |
| 369 | + } | |
| 360 | 370 | $this->name = $psName; | 
| 361 | 371 |  			if ($names[1]) { $this->familyName = $names[1]; } else { $this->familyName = $psName; } | 
| 362 | 372 |  			if ($names[2]) { $this->styleName = $names[2]; } else { $this->styleName = 'Regular'; } | 
| @@ -420,21 +430,27 @@ discard block | ||
| 420 | 430 | $this->skip(26); | 
| 421 | 431 | $sTypoAscender = $this->read_short(); | 
| 422 | 432 | $sTypoDescender = $this->read_short(); | 
| 423 | - if (!$this->ascent) $this->ascent = ($sTypoAscender * $scale); | |
| 424 | - if (!$this->descent) $this->descent = ($sTypoDescender * $scale); | |
| 433 | +			if (!$this->ascent) { | |
| 434 | + $this->ascent = ($sTypoAscender * $scale); | |
| 435 | + } | |
| 436 | +			if (!$this->descent) { | |
| 437 | + $this->descent = ($sTypoDescender * $scale); | |
| 438 | + } | |
| 425 | 439 |  			if ($version > 1) { | 
| 426 | 440 | $this->skip(16); | 
| 427 | 441 | $sCapHeight = $this->read_short(); | 
| 428 | 442 | $this->capHeight = ($sCapHeight * $scale); | 
| 429 | - } | |
| 430 | -			else { | |
| 443 | +			} else { | |
| 431 | 444 | $this->capHeight = $this->ascent; | 
| 432 | 445 | } | 
| 433 | - } | |
| 434 | -		else { | |
| 446 | +		} else { | |
| 435 | 447 | $usWeightClass = 500; | 
| 436 | - if (!$this->ascent) $this->ascent = ($yMax * $scale); | |
| 437 | - if (!$this->descent) $this->descent = ($yMin * $scale); | |
| 448 | +			if (!$this->ascent) { | |
| 449 | + $this->ascent = ($yMax * $scale); | |
| 450 | + } | |
| 451 | +			if (!$this->descent) { | |
| 452 | + $this->descent = ($yMin * $scale); | |
| 453 | + } | |
| 438 | 454 | $this->capHeight = $this->ascent; | 
| 439 | 455 | } | 
| 440 | 456 | $this->stemV = 50 + intval(pow(($usWeightClass / 65.0), 2)); | 
| @@ -451,12 +467,15 @@ discard block | ||
| 451 | 467 | |
| 452 | 468 | $this->flags = 4; | 
| 453 | 469 | |
| 454 | - if ($this->italicAngle != 0) | |
| 455 | - $this->flags = $this->flags | 64; | |
| 456 | - if ($usWeightClass >= 600) | |
| 457 | - $this->flags = $this->flags | 262144; | |
| 458 | - if ($isFixedPitch) | |
| 459 | - $this->flags = $this->flags | 1; | |
| 470 | +		if ($this->italicAngle != 0) { | |
| 471 | + $this->flags = $this->flags | 64; | |
| 472 | + } | |
| 473 | +		if ($usWeightClass >= 600) { | |
| 474 | + $this->flags = $this->flags | 262144; | |
| 475 | + } | |
| 476 | +		if ($isFixedPitch) { | |
| 477 | + $this->flags = $this->flags | 1; | |
| 478 | + } | |
| 460 | 479 | |
| 461 | 480 | /////////////////////////////////// | 
| 462 | 481 | // hhea - Horizontal header table | 
| @@ -495,14 +514,17 @@ discard block | ||
| 495 | 514 |  			if (($platformID == 3 && $encodingID == 1) || $platformID == 0) { // Microsoft, Unicode | 
| 496 | 515 | $format = $this->get_ushort($cmap_offset + $offset); | 
| 497 | 516 |  				if ($format == 4) { | 
| 498 | - if (!$unicode_cmap_offset) $unicode_cmap_offset = $cmap_offset + $offset; | |
| 517 | +					if (!$unicode_cmap_offset) { | |
| 518 | + $unicode_cmap_offset = $cmap_offset + $offset; | |
| 519 | + } | |
| 499 | 520 | break; | 
| 500 | 521 | } | 
| 501 | 522 | } | 
| 502 | 523 | $this->seek($save_pos); | 
| 503 | 524 | } | 
| 504 | - if (!$unicode_cmap_offset) | |
| 505 | -			die('Font ('.$this->filename.') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); | |
| 525 | +		if (!$unicode_cmap_offset) { | |
| 526 | +					die('Font ('.$this->filename.') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); | |
| 527 | + } | |
| 506 | 528 | |
| 507 | 529 | |
| 508 | 530 | $glyphToChar = array(); | 
| @@ -586,8 +608,9 @@ discard block | ||
| 586 | 608 | $this->seek($save_pos); | 
| 587 | 609 | } | 
| 588 | 610 | |
| 589 | - if (!$unicode_cmap_offset) | |
| 590 | -			die('Font ('.$this->filename.') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); | |
| 611 | +		if (!$unicode_cmap_offset) { | |
| 612 | +					die('Font ('.$this->filename.') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); | |
| 613 | + } | |
| 591 | 614 | |
| 592 | 615 | |
| 593 | 616 | $glyphToChar = array(); | 
| @@ -772,10 +795,12 @@ discard block | ||
| 772 | 795 | $glyphLen = $this->glyphPos[$originalGlyphIdx + 1] - $glyphPos; | 
| 773 | 796 |  			if ($glyfLength < $this->maxStrLenRead) { | 
| 774 | 797 | $data = substr($glyphData, $glyphPos, $glyphLen); | 
| 775 | - } | |
| 776 | -			else { | |
| 777 | - if ($glyphLen > 0) $data = $this->get_chunk($glyfOffset + $glyphPos, $glyphLen); | |
| 778 | - else $data = ''; | |
| 798 | +			} else { | |
| 799 | +				if ($glyphLen > 0) { | |
| 800 | + $data = $this->get_chunk($glyfOffset + $glyphPos, $glyphLen); | |
| 801 | +				} else { | |
| 802 | + $data = ''; | |
| 803 | + } | |
| 779 | 804 | } | 
| 780 | 805 | |
| 781 | 806 |  			if ($glyphLen > 0) { | 
| @@ -821,8 +846,7 @@ discard block | ||
| 821 | 846 |  		if ((($pos + 1) >> 1) > 0xFFFF) { | 
| 822 | 847 | $indexToLocFormat = 1; // long format | 
| 823 | 848 |  			foreach ($offsets AS $offset) { $locastr .= pack("N", $offset); } | 
| 824 | - } | |
| 825 | -		else { | |
| 849 | +		} else { | |
| 826 | 850 | $indexToLocFormat = 0; // short format | 
| 827 | 851 |  			foreach ($offsets AS $offset) { $locastr .= pack("n", ($offset / 2)); } | 
| 828 | 852 | } | 
| @@ -928,14 +952,12 @@ discard block | ||
| 928 | 952 |  		if (($numberOfHMetrics * 4) < $this->maxStrLenRead) { | 
| 929 | 953 | $data = $this->get_chunk($start, ($numberOfHMetrics * 4)); | 
| 930 | 954 |  			$arr = unpack("n*", $data); | 
| 931 | - } | |
| 932 | -		else { $this->seek($start); } | |
| 955 | +		} else { $this->seek($start); } | |
| 933 | 956 |  		for ($glyph = 0; $glyph < $numberOfHMetrics; $glyph++) { | 
| 934 | 957 | |
| 935 | 958 |  			if (($numberOfHMetrics * 4) < $this->maxStrLenRead) { | 
| 936 | 959 | $aw = $arr[($glyph * 2) + 1]; | 
| 937 | - } | |
| 938 | -			else { | |
| 960 | +			} else { | |
| 939 | 961 | $aw = $this->read_ushort(); | 
| 940 | 962 | $lsb = $this->read_ushort(); | 
| 941 | 963 | } | 
| @@ -993,8 +1015,7 @@ discard block | ||
| 993 | 1015 |  		if ($gid < $numberOfHMetrics) { | 
| 994 | 1016 | $this->seek($start + ($gid * 4)); | 
| 995 | 1017 | $hm = fread($this->fh, 4); | 
| 996 | - } | |
| 997 | -		else { | |
| 1018 | +		} else { | |
| 998 | 1019 | $this->seek($start + (($numberOfHMetrics - 1) * 4)); | 
| 999 | 1020 | $hm = fread($this->fh, 2); | 
| 1000 | 1021 | $this->seek($start + ($numberOfHMetrics * 2) + ($gid * 2)); | 
| @@ -1016,16 +1037,15 @@ discard block | ||
| 1016 | 1037 |  			for ($n = 0; $n <= $numGlyphs; $n++) { | 
| 1017 | 1038 | $this->glyphPos[] = ($arr[$n + 1] * 2); | 
| 1018 | 1039 | } | 
| 1019 | - } | |
| 1020 | -		else if ($indexToLocFormat == 1) { | |
| 1040 | +		} else if ($indexToLocFormat == 1) { | |
| 1021 | 1041 | $data = $this->get_chunk($start, ($numGlyphs * 4) + 4); | 
| 1022 | 1042 |  			$arr = unpack("N*", $data); | 
| 1023 | 1043 |  			for ($n = 0; $n <= $numGlyphs; $n++) { | 
| 1024 | 1044 | $this->glyphPos[] = ($arr[$n + 1]); | 
| 1025 | 1045 | } | 
| 1046 | +		} else { | |
| 1047 | +					die('Unknown location table format '.$indexToLocFormat); | |
| 1026 | 1048 | } | 
| 1027 | - else | |
| 1028 | -			die('Unknown location table format '.$indexToLocFormat); | |
| 1029 | 1049 | } | 
| 1030 | 1050 | |
| 1031 | 1051 | |
| @@ -1057,17 +1077,18 @@ discard block | ||
| 1057 | 1077 |  		for ($n = 0; $n < $segCount; $n++) { | 
| 1058 | 1078 | $endpoint = ($endCount[$n] + 1); | 
| 1059 | 1079 |  			for ($unichar = $startCount[$n]; $unichar < $endpoint; $unichar++) { | 
| 1060 | - if ($idRangeOffset[$n] == 0) | |
| 1061 | - $glyph = ($unichar + $idDelta[$n]) & 0xFFFF; | |
| 1062 | -				else { | |
| 1080 | +				if ($idRangeOffset[$n] == 0) { | |
| 1081 | + $glyph = ($unichar + $idDelta[$n]) & 0xFFFF; | |
| 1082 | +				} else { | |
| 1063 | 1083 | $offset = ($unichar - $startCount[$n]) * 2 + $idRangeOffset[$n]; | 
| 1064 | 1084 | $offset = $idRangeOffset_start + 2 * $n + $offset; | 
| 1065 | - if ($offset >= $limit) | |
| 1066 | - $glyph = 0; | |
| 1067 | -					else { | |
| 1085 | +					if ($offset >= $limit) { | |
| 1086 | + $glyph = 0; | |
| 1087 | +					} else { | |
| 1068 | 1088 | $glyph = $this->get_ushort($offset); | 
| 1069 | - if ($glyph != 0) | |
| 1070 | - $glyph = ($glyph + $idDelta[$n]) & 0xFFFF; | |
| 1089 | +						if ($glyph != 0) { | |
| 1090 | + $glyph = ($glyph + $idDelta[$n]) & 0xFFFF; | |
| 1091 | + } | |
| 1071 | 1092 | } | 
| 1072 | 1093 | } | 
| 1073 | 1094 | $charToGlyph[$unichar] = $glyph; | 
| @@ -1098,8 +1119,7 @@ discard block | ||
| 1098 | 1119 | // Header | 
| 1099 | 1120 |  		if (_TTF_MAC_HEADER) { | 
| 1100 | 1121 |  			$stm .= (pack("Nnnnn", 0x74727565, $numTables, $searchRange, $entrySelector, $rangeShift)); // Mac | 
| 1101 | - } | |
| 1102 | -		else { | |
| 1122 | +		} else { | |
| 1103 | 1123 |  			$stm .= (pack("Nnnnn", 0x00010000, $numTables, $searchRange, $entrySelector, $rangeShift)); // Windows | 
| 1104 | 1124 | } | 
| 1105 | 1125 | |
| @@ -164,6 +164,7 @@ | ||
| 164 | 164 | |
| 165 | 165 | /** | 
| 166 | 166 | * Get MD5 as binary string | 
| 167 | + * @param string $string | |
| 167 | 168 | */ | 
| 168 | 169 | function _md5_16($string) | 
| 169 | 170 |      { | 
| @@ -122,9 +122,9 @@ discard block | ||
| 122 | 122 | } | 
| 123 | 123 | |
| 124 | 124 | /** | 
| 125 | - * Compute key depending on object number where the encrypted data is stored | |
| 126 | - * @param integer $n | |
| 127 | - */ | |
| 125 | + * Compute key depending on object number where the encrypted data is stored | |
| 126 | + * @param integer $n | |
| 127 | + */ | |
| 128 | 128 | function _objectkey($n) | 
| 129 | 129 |      { | 
| 130 | 130 |          return substr($this->_md5_16($this->encryption_key.pack('VXxx', $n)), 0, 10); | 
| @@ -163,18 +163,18 @@ discard block | ||
| 163 | 163 | } | 
| 164 | 164 | |
| 165 | 165 | /** | 
| 166 | - * Get MD5 as binary string | |
| 167 | - */ | |
| 166 | + * Get MD5 as binary string | |
| 167 | + */ | |
| 168 | 168 | function _md5_16($string) | 
| 169 | 169 |      { | 
| 170 | 170 |          return pack('H*', md5($string)); | 
| 171 | 171 | } | 
| 172 | 172 | |
| 173 | 173 | /** | 
| 174 | - * Compute O value | |
| 175 | - * @param string $user_pass | |
| 176 | - * @param string $owner_pass | |
| 177 | - */ | |
| 174 | + * Compute O value | |
| 175 | + * @param string $user_pass | |
| 176 | + * @param string $owner_pass | |
| 177 | + */ | |
| 178 | 178 | function _Ovalue($user_pass, $owner_pass) | 
| 179 | 179 |      { | 
| 180 | 180 | $tmp = $this->_md5_16($owner_pass); | 
| @@ -183,18 +183,18 @@ discard block | ||
| 183 | 183 | } | 
| 184 | 184 | |
| 185 | 185 | /** | 
| 186 | - * Compute U value | |
| 187 | - */ | |
| 186 | + * Compute U value | |
| 187 | + */ | |
| 188 | 188 | function _Uvalue() | 
| 189 | 189 |      { | 
| 190 | 190 | return RC4($this->encryption_key, $this->padding); | 
| 191 | 191 | } | 
| 192 | 192 | |
| 193 | 193 | /** | 
| 194 | - * Compute encryption key | |
| 195 | - * @param string $user_pass | |
| 196 | - * @param integer $protection | |
| 197 | - */ | |
| 194 | + * Compute encryption key | |
| 195 | + * @param string $user_pass | |
| 196 | + * @param integer $protection | |
| 197 | + */ | |
| 198 | 198 | function _generateencryptionkey($user_pass, $owner_pass, $protection) | 
| 199 | 199 |      { | 
| 200 | 200 | // Pad passwords | 
| @@ -77,11 +77,11 @@ | ||
| 77 | 77 | * - If an owner password is set, document can be opened in privilege mode with no | 
| 78 | 78 | * restriction if that password is entered | 
| 79 | 79 | */ | 
| 80 | - function SetProtection($permissions=array(), $user_pass='', $owner_pass=null) | |
| 80 | + function SetProtection($permissions = array(), $user_pass = '', $owner_pass = null) | |
| 81 | 81 |      { | 
| 82 | -        $options = array('print' => 4, 'modify' => 8, 'copy' => 16, 'annot-forms' => 32 ); | |
| 82 | +        $options = array('print' => 4, 'modify' => 8, 'copy' => 16, 'annot-forms' => 32); | |
| 83 | 83 | $protection = 192; | 
| 84 | - foreach($permissions as $permission) | |
| 84 | + foreach ($permissions as $permission) | |
| 85 | 85 |          { | 
| 86 | 86 |              if (!isset($options[$permission])) { | 
| 87 | 87 |                              $this->Error('Incorrect permission: '.$permission); | 
| @@ -1331,6 +1331,9 @@ | ||
| 1331 | 1331 | } | 
| 1332 | 1332 | } | 
| 1333 | 1333 | |
| 1334 | +/** | |
| 1335 | + * @param string $size | |
| 1336 | + */ | |
| 1334 | 1337 | function _getpagesize($size) | 
| 1335 | 1338 |  { | 
| 1336 | 1339 | if (is_string($size)) | 
| @@ -174,12 +174,12 @@ discard block | ||
| 174 | 174 | * @param double $left | 
| 175 | 175 | * @param double $top | 
| 176 | 176 | */ | 
| 177 | -function SetMargins($left, $top, $right=null) | |
| 177 | +function SetMargins($left, $top, $right = null) | |
| 178 | 178 |  { | 
| 179 | 179 | // Set left, top and right margins | 
| 180 | 180 | $this->lMargin = $left; | 
| 181 | 181 | $this->tMargin = $top; | 
| 182 | -    if($right===null) { | |
| 182 | +    if ($right === null) { | |
| 183 | 183 | $right = $left; | 
| 184 | 184 | } | 
| 185 | 185 | $this->rMargin = $right; | 
| @@ -208,25 +208,25 @@ discard block | ||
| 208 | 208 | /** | 
| 209 | 209 | * @param boolean $auto | 
| 210 | 210 | */ | 
| 211 | -function SetAutoPageBreak($auto, $margin=0) | |
| 211 | +function SetAutoPageBreak($auto, $margin = 0) | |
| 212 | 212 |  { | 
| 213 | 213 | // Set auto page break mode and triggering margin | 
| 214 | 214 | $this->AutoPageBreak = $auto; | 
| 215 | 215 | $this->bMargin = $margin; | 
| 216 | - $this->PageBreakTrigger = $this->h-$margin; | |
| 216 | + $this->PageBreakTrigger = $this->h - $margin; | |
| 217 | 217 | } | 
| 218 | 218 | |
| 219 | 219 | /** | 
| 220 | 220 | * @param string $zoom | 
| 221 | 221 | */ | 
| 222 | -function SetDisplayMode($zoom, $layout='default') | |
| 222 | +function SetDisplayMode($zoom, $layout = 'default') | |
| 223 | 223 |  { | 
| 224 | 224 | // Set display mode in viewer | 
| 225 | - if($zoom=='fullpage' || $zoom=='fullwidth' || $zoom=='real' || $zoom=='default' || !is_string($zoom)) | |
| 225 | + if ($zoom == 'fullpage' || $zoom == 'fullwidth' || $zoom == 'real' || $zoom == 'default' || !is_string($zoom)) | |
| 226 | 226 | $this->ZoomMode = $zoom; | 
| 227 | 227 | else | 
| 228 | 228 |          $this->Error('Incorrect zoom display mode: '.$zoom); | 
| 229 | - if($layout=='single' || $layout=='continuous' || $layout=='two' || $layout=='default') | |
| 229 | + if ($layout == 'single' || $layout == 'continuous' || $layout == 'two' || $layout == 'default') | |
| 230 | 230 | $this->LayoutMode = $layout; | 
| 231 | 231 | else | 
| 232 | 232 |          $this->Error('Incorrect layout display mode: '.$layout); | 
| @@ -404,133 +404,133 @@ discard block | ||
| 404 | 404 | return $this->page; | 
| 405 | 405 | } | 
| 406 | 406 | |
| 407 | -function SetDrawColor($r, $g=null, $b=null) | |
| 407 | +function SetDrawColor($r, $g = null, $b = null) | |
| 408 | 408 |  { | 
| 409 | 409 | // Set color for all stroking operations | 
| 410 | -    if(($r==0 && $g==0 && $b==0) || $g===null) { | |
| 411 | -            $this->DrawColor = sprintf('%.3F G',$r/255); | |
| 410 | +    if (($r == 0 && $g == 0 && $b == 0) || $g === null) { | |
| 411 | +            $this->DrawColor = sprintf('%.3F G', $r / 255); | |
| 412 | 412 |      } else { | 
| 413 | -            $this->DrawColor = sprintf('%.3F %.3F %.3F RG',$r/255,$g/255,$b/255); | |
| 413 | +            $this->DrawColor = sprintf('%.3F %.3F %.3F RG', $r / 255, $g / 255, $b / 255); | |
| 414 | 414 | } | 
| 415 | -    if($this->page>0) { | |
| 415 | +    if ($this->page > 0) { | |
| 416 | 416 | $this->_out($this->DrawColor); | 
| 417 | 417 | } | 
| 418 | 418 | } | 
| 419 | 419 | |
| 420 | -function SetFillColor($r, $g=null, $b=null) | |
| 420 | +function SetFillColor($r, $g = null, $b = null) | |
| 421 | 421 |  { | 
| 422 | 422 | // Set color for all filling operations | 
| 423 | -    if(($r==0 && $g==0 && $b==0) || $g===null) { | |
| 424 | -            $this->FillColor = sprintf('%.3F g',$r/255); | |
| 423 | +    if (($r == 0 && $g == 0 && $b == 0) || $g === null) { | |
| 424 | +            $this->FillColor = sprintf('%.3F g', $r / 255); | |
| 425 | 425 |      } else { | 
| 426 | -            $this->FillColor = sprintf('%.3F %.3F %.3F rg',$r/255,$g/255,$b/255); | |
| 426 | +            $this->FillColor = sprintf('%.3F %.3F %.3F rg', $r / 255, $g / 255, $b / 255); | |
| 427 | 427 | } | 
| 428 | - $this->ColorFlag = ($this->FillColor!=$this->TextColor); | |
| 429 | -    if($this->page>0) { | |
| 428 | + $this->ColorFlag = ($this->FillColor != $this->TextColor); | |
| 429 | +    if ($this->page > 0) { | |
| 430 | 430 | $this->_out($this->FillColor); | 
| 431 | 431 | } | 
| 432 | 432 | } | 
| 433 | 433 | |
| 434 | -function SetTextColor($r, $g=null, $b=null) | |
| 434 | +function SetTextColor($r, $g = null, $b = null) | |
| 435 | 435 |  { | 
| 436 | 436 | // Set color for text | 
| 437 | -    if(($r==0 && $g==0 && $b==0) || $g===null) { | |
| 438 | -            $this->TextColor = sprintf('%.3F g',$r/255); | |
| 437 | +    if (($r == 0 && $g == 0 && $b == 0) || $g === null) { | |
| 438 | +            $this->TextColor = sprintf('%.3F g', $r / 255); | |
| 439 | 439 |      } else { | 
| 440 | -            $this->TextColor = sprintf('%.3F %.3F %.3F rg',$r/255,$g/255,$b/255); | |
| 440 | +            $this->TextColor = sprintf('%.3F %.3F %.3F rg', $r / 255, $g / 255, $b / 255); | |
| 441 | 441 | } | 
| 442 | - $this->ColorFlag = ($this->FillColor!=$this->TextColor); | |
| 442 | + $this->ColorFlag = ($this->FillColor != $this->TextColor); | |
| 443 | 443 | } | 
| 444 | 444 | |
| 445 | 445 | function GetStringWidth($s) | 
| 446 | 446 |  { | 
| 447 | 447 | // Get width of a string in the current font | 
| 448 | - $s = (string)$s; | |
| 448 | + $s = (string) $s; | |
| 449 | 449 | $cw = &$this->CurrentFont['cw']; | 
| 450 | - $w=0; | |
| 450 | + $w = 0; | |
| 451 | 451 |      if ($this->unifontSubset) { | 
| 452 | 452 | $unicode = $this->UTF8StringToArray($s); | 
| 453 | -        foreach($unicode as $char) { | |
| 454 | -            if (isset($cw[$char])) { $w += (ord($cw[2*$char])<<8) + ord($cw[2*$char+1]); } else if($char>0 && $char<128 && isset($cw[chr($char)])) { $w += $cw[chr($char)]; } else if(isset($this->CurrentFont['desc']['MissingWidth'])) { $w += $this->CurrentFont['desc']['MissingWidth']; } else if(isset($this->CurrentFont['MissingWidth'])) { $w += $this->CurrentFont['MissingWidth']; } else { $w += 500; } | |
| 453 | +        foreach ($unicode as $char) { | |
| 454 | +            if (isset($cw[$char])) { $w += (ord($cw[2 * $char]) << 8) + ord($cw[2 * $char + 1]); } else if ($char > 0 && $char < 128 && isset($cw[chr($char)])) { $w += $cw[chr($char)]; } else if (isset($this->CurrentFont['desc']['MissingWidth'])) { $w += $this->CurrentFont['desc']['MissingWidth']; } else if (isset($this->CurrentFont['MissingWidth'])) { $w += $this->CurrentFont['MissingWidth']; } else { $w += 500; } | |
| 455 | 455 | } | 
| 456 | 456 |      } else { | 
| 457 | 457 | $l = strlen($s); | 
| 458 | -        for($i=0;$i<$l;$i++) { | |
| 458 | +        for ($i = 0; $i < $l; $i++) { | |
| 459 | 459 | $w += $cw[$s[$i]]; | 
| 460 | 460 | } | 
| 461 | 461 | } | 
| 462 | - return $w*$this->FontSize/1000; | |
| 462 | + return $w * $this->FontSize / 1000; | |
| 463 | 463 | } | 
| 464 | 464 | |
| 465 | 465 | function SetLineWidth($width) | 
| 466 | 466 |  { | 
| 467 | 467 | // Set line width | 
| 468 | 468 | $this->LineWidth = $width; | 
| 469 | -    if($this->page>0) { | |
| 470 | -            $this->_out(sprintf('%.2F w',$width*$this->k)); | |
| 469 | +    if ($this->page > 0) { | |
| 470 | +            $this->_out(sprintf('%.2F w', $width * $this->k)); | |
| 471 | 471 | } | 
| 472 | 472 | } | 
| 473 | 473 | |
| 474 | 474 | function Line($x1, $y1, $x2, $y2) | 
| 475 | 475 |  { | 
| 476 | 476 | // Draw a line | 
| 477 | -    $this->_out(sprintf('%.2F %.2F m %.2F %.2F l S',$x1*$this->k,($this->h-$y1)*$this->k,$x2*$this->k,($this->h-$y2)*$this->k)); | |
| 477 | +    $this->_out(sprintf('%.2F %.2F m %.2F %.2F l S', $x1 * $this->k, ($this->h - $y1) * $this->k, $x2 * $this->k, ($this->h - $y2) * $this->k)); | |
| 478 | 478 | } | 
| 479 | 479 | |
| 480 | -function Rect($x, $y, $w, $h, $style='') | |
| 480 | +function Rect($x, $y, $w, $h, $style = '') | |
| 481 | 481 |  { | 
| 482 | 482 | // Draw a rectangle | 
| 483 | -    if($style=='F') { | |
| 483 | +    if ($style == 'F') { | |
| 484 | 484 | $op = 'f'; | 
| 485 | -    } elseif($style=='FD' || $style=='DF') { | |
| 485 | +    } elseif ($style == 'FD' || $style == 'DF') { | |
| 486 | 486 | $op = 'B'; | 
| 487 | 487 |      } else { | 
| 488 | 488 | $op = 'S'; | 
| 489 | 489 | } | 
| 490 | -    $this->_out(sprintf('%.2F %.2F %.2F %.2F re %s',$x*$this->k,($this->h-$y)*$this->k,$w*$this->k,-$h*$this->k,$op)); | |
| 490 | +    $this->_out(sprintf('%.2F %.2F %.2F %.2F re %s', $x * $this->k, ($this->h - $y) * $this->k, $w * $this->k, -$h * $this->k, $op)); | |
| 491 | 491 | } | 
| 492 | 492 | |
| 493 | 493 | /** | 
| 494 | 494 | * @param string $family | 
| 495 | 495 | */ | 
| 496 | -function AddFont($family, $style='', $file='', $uni=false) | |
| 496 | +function AddFont($family, $style = '', $file = '', $uni = false) | |
| 497 | 497 |  { | 
| 498 | 498 | // Add a TrueType, OpenType or Type1 font | 
| 499 | 499 | $family = strtolower($family); | 
| 500 | 500 | $style = strtoupper($style); | 
| 501 | - if($style=='IB') | |
| 502 | - $style='BI'; | |
| 503 | -    if($file=='') { | |
| 501 | + if ($style == 'IB') | |
| 502 | + $style = 'BI'; | |
| 503 | +    if ($file == '') { | |
| 504 | 504 |          if ($uni) { | 
| 505 | -        $file = str_replace(' ','',$family).strtolower($style).'.ttf'; | |
| 505 | +        $file = str_replace(' ', '', $family).strtolower($style).'.ttf'; | |
| 506 | 506 | } | 
| 507 | 507 |          else { | 
| 508 | -        $file = str_replace(' ','',$family).strtolower($style).'.php'; | |
| 508 | +        $file = str_replace(' ', '', $family).strtolower($style).'.php'; | |
| 509 | 509 | } | 
| 510 | 510 | } | 
| 511 | 511 | $fontkey = $family.$style; | 
| 512 | - if(isset($this->fonts[$fontkey])) | |
| 512 | + if (isset($this->fonts[$fontkey])) | |
| 513 | 513 | return; | 
| 514 | 514 | |
| 515 | 515 |      if ($uni) { | 
| 516 | -        if (defined("_SYSTEM_TTFONTS") && file_exists(_SYSTEM_TTFONTS.$file )) { $ttffilename = _SYSTEM_TTFONTS.$file ; } | |
| 517 | -        else { $ttffilename = $this->_getfontpath().'unifont/'.$file ; } | |
| 518 | - $unifilename = $this->_getfontpath().'unifont/'.strtolower(substr($file ,0,(strpos($file ,'.')))); | |
| 516 | +        if (defined("_SYSTEM_TTFONTS") && file_exists(_SYSTEM_TTFONTS.$file)) { $ttffilename = _SYSTEM_TTFONTS.$file; } | |
| 517 | +        else { $ttffilename = $this->_getfontpath().'unifont/'.$file; } | |
| 518 | + $unifilename = $this->_getfontpath().'unifont/'.strtolower(substr($file, 0, (strpos($file, '.')))); | |
| 519 | 519 | $name = ''; | 
| 520 | 520 | $originalsize = 0; | 
| 521 | 521 | $ttfstat = stat($ttffilename); | 
| 522 | 522 |          if (file_exists($unifilename.'.mtx.php')) { | 
| 523 | 523 | include($unifilename.'.mtx.php'); | 
| 524 | 524 | } | 
| 525 | -        if (!isset($type) ||  !isset($name) || $originalsize != $ttfstat['size']) { | |
| 525 | +        if (!isset($type) || !isset($name) || $originalsize != $ttfstat['size']) { | |
| 526 | 526 | $ttffile = $ttffilename; | 
| 527 | 527 | require_once($this->_getfontpath().'unifont/ttfonts.php'); | 
| 528 | 528 | $ttf = new TTFontFile(); | 
| 529 | 529 | $ttf->getMetrics($ttffile); | 
| 530 | 530 | $cw = $ttf->charWidths; | 
| 531 | -            $name = preg_replace('/[ ()]/','',$ttf->fullName); | |
| 531 | +            $name = preg_replace('/[ ()]/', '', $ttf->fullName); | |
| 532 | 532 | |
| 533 | -            $desc= array('Ascent'=>round($ttf->ascent), | |
| 533 | +            $desc = array('Ascent'=>round($ttf->ascent), | |
| 534 | 534 | 'Descent'=>round($ttf->descent), | 
| 535 | 535 | 'CapHeight'=>round($ttf->capHeight), | 
| 536 | 536 | 'Flags'=>$ttf->flags, | 
| @@ -607,48 +607,48 @@ discard block | ||
| 607 | 607 | /** | 
| 608 | 608 | * @param string $family | 
| 609 | 609 | */ | 
| 610 | -function SetFont($family, $style='', $size=0) | |
| 610 | +function SetFont($family, $style = '', $size = 0) | |
| 611 | 611 |  { | 
| 612 | 612 | // Select a font; size given in points | 
| 613 | -    if($family=='') { | |
| 613 | +    if ($family == '') { | |
| 614 | 614 | $family = $this->FontFamily; | 
| 615 | 615 |      } else { | 
| 616 | 616 | $family = strtolower($family); | 
| 617 | 617 | } | 
| 618 | 618 | $style = strtoupper($style); | 
| 619 | - if(strpos($style,'U')!==false) | |
| 619 | + if (strpos($style, 'U') !== false) | |
| 620 | 620 |      { | 
| 621 | 621 | $this->underline = true; | 
| 622 | -        $style = str_replace('U','',$style); | |
| 622 | +        $style = str_replace('U', '', $style); | |
| 623 | 623 |      } else { | 
| 624 | 624 | $this->underline = false; | 
| 625 | 625 | } | 
| 626 | -    if($style=='IB') { | |
| 626 | +    if ($style == 'IB') { | |
| 627 | 627 | $style = 'BI'; | 
| 628 | 628 | } | 
| 629 | -    if($size==0) { | |
| 629 | +    if ($size == 0) { | |
| 630 | 630 | $size = $this->FontSizePt; | 
| 631 | 631 | } | 
| 632 | 632 | // Test if font is already selected | 
| 633 | -    if($this->FontFamily==$family && $this->FontStyle==$style && $this->FontSizePt==$size) { | |
| 633 | +    if ($this->FontFamily == $family && $this->FontStyle == $style && $this->FontSizePt == $size) { | |
| 634 | 634 | return; | 
| 635 | 635 | } | 
| 636 | 636 | // Test if font is already loaded | 
| 637 | 637 | $fontkey = $family.$style; | 
| 638 | - if(!isset($this->fonts[$fontkey])) | |
| 638 | + if (!isset($this->fonts[$fontkey])) | |
| 639 | 639 |      { | 
| 640 | 640 | // Test if one of the core fonts | 
| 641 | -        if($family=='arial') { | |
| 641 | +        if ($family == 'arial') { | |
| 642 | 642 | $family = 'helvetica'; | 
| 643 | 643 | } | 
| 644 | - if(in_array($family,$this->CoreFonts)) | |
| 644 | + if (in_array($family, $this->CoreFonts)) | |
| 645 | 645 |          { | 
| 646 | -            if($family=='symbol' || $family=='zapfdingbats') { | |
| 646 | +            if ($family == 'symbol' || $family == 'zapfdingbats') { | |
| 647 | 647 | $style = ''; | 
| 648 | 648 | } | 
| 649 | 649 | $fontkey = $family.$style; | 
| 650 | -            if(!isset($this->fonts[$fontkey])) { | |
| 651 | - $this->AddFont($family,$style); | |
| 650 | +            if (!isset($this->fonts[$fontkey])) { | |
| 651 | + $this->AddFont($family, $style); | |
| 652 | 652 | } | 
| 653 | 653 |          } else { | 
| 654 | 654 |                      $this->Error('Undefined font: '.$family.' '.$style); | 
| @@ -1095,11 +1095,11 @@ discard block | ||
| 1095 | 1095 | } | 
| 1096 | 1096 | } | 
| 1097 | 1097 | // Last chunk | 
| 1098 | -    if($i!=$j) { | |
| 1098 | +    if ($i != $j) { | |
| 1099 | 1099 |          if ($this->unifontSubset) { | 
| 1100 | - $this->Cell($l,$h,mb_substr($s,$j,$i-$j,'UTF-8'),0,0,'',0,$link); | |
| 1100 | + $this->Cell($l, $h, mb_substr($s, $j, $i - $j, 'UTF-8'), 0, 0, '', 0, $link); | |
| 1101 | 1101 |          } else { | 
| 1102 | - $this->Cell($l,$h,substr($s,$j),0,0,'',0,$link); | |
| 1102 | + $this->Cell($l, $h, substr($s, $j), 0, 0, '', 0, $link); | |
| 1103 | 1103 | } | 
| 1104 | 1104 | } | 
| 1105 | 1105 | } | 
| @@ -1107,86 +1107,86 @@ discard block | ||
| 1107 | 1107 | /** | 
| 1108 | 1108 | * @param integer $h | 
| 1109 | 1109 | */ | 
| 1110 | -function Ln($h=null) | |
| 1110 | +function Ln($h = null) | |
| 1111 | 1111 |  { | 
| 1112 | 1112 | // Line feed; default value is last cell height | 
| 1113 | 1113 | $this->x = $this->lMargin; | 
| 1114 | -    if($h===null) { | |
| 1114 | +    if ($h === null) { | |
| 1115 | 1115 | $this->y += $this->lasth; | 
| 1116 | 1116 |      } else { | 
| 1117 | 1117 | $this->y += $h; | 
| 1118 | 1118 | } | 
| 1119 | 1119 | } | 
| 1120 | 1120 | |
| 1121 | -function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='') | |
| 1121 | +function Image($file, $x = null, $y = null, $w = 0, $h = 0, $type = '', $link = '') | |
| 1122 | 1122 |  { | 
| 1123 | 1123 | // Put an image on the page | 
| 1124 | - if(!isset($this->images[$file])) | |
| 1124 | + if (!isset($this->images[$file])) | |
| 1125 | 1125 |      { | 
| 1126 | 1126 | // First use of this image, get info | 
| 1127 | - if($type=='') | |
| 1127 | + if ($type == '') | |
| 1128 | 1128 |          { | 
| 1129 | - $pos = strrpos($file,'.'); | |
| 1130 | -            if(!$pos) { | |
| 1129 | + $pos = strrpos($file, '.'); | |
| 1130 | +            if (!$pos) { | |
| 1131 | 1131 |                              $this->Error('Image file has no extension and no type was specified: '.$file); | 
| 1132 | 1132 | } | 
| 1133 | - $type = substr($file,$pos+1); | |
| 1133 | + $type = substr($file, $pos + 1); | |
| 1134 | 1134 | } | 
| 1135 | 1135 | $type = strtolower($type); | 
| 1136 | -        if($type=='jpeg') { | |
| 1136 | +        if ($type == 'jpeg') { | |
| 1137 | 1137 | $type = 'jpg'; | 
| 1138 | 1138 | } | 
| 1139 | 1139 | $mtd = '_parse'.$type; | 
| 1140 | -        if(!method_exists($this,$mtd)) { | |
| 1140 | +        if (!method_exists($this, $mtd)) { | |
| 1141 | 1141 |                      $this->Error('Unsupported image type: '.$type); | 
| 1142 | 1142 | } | 
| 1143 | 1143 | $info = $this->$mtd($file); | 
| 1144 | - $info['i'] = count($this->images)+1; | |
| 1144 | + $info['i'] = count($this->images) + 1; | |
| 1145 | 1145 | $this->images[$file] = $info; | 
| 1146 | 1146 |      } else { | 
| 1147 | 1147 | $info = $this->images[$file]; | 
| 1148 | 1148 | } | 
| 1149 | 1149 | |
| 1150 | 1150 | // Automatic width and height calculation if needed | 
| 1151 | - if($w==0 && $h==0) | |
| 1151 | + if ($w == 0 && $h == 0) | |
| 1152 | 1152 |      { | 
| 1153 | 1153 | // Put image at 96 dpi | 
| 1154 | 1154 | $w = -96; | 
| 1155 | 1155 | $h = -96; | 
| 1156 | 1156 | } | 
| 1157 | -    if($w<0) { | |
| 1158 | - $w = -$info['w']*72/$w/$this->k; | |
| 1157 | +    if ($w < 0) { | |
| 1158 | + $w = -$info['w'] * 72 / $w / $this->k; | |
| 1159 | 1159 | } | 
| 1160 | -    if($h<0) { | |
| 1161 | - $h = -$info['h']*72/$h/$this->k; | |
| 1160 | +    if ($h < 0) { | |
| 1161 | + $h = -$info['h'] * 72 / $h / $this->k; | |
| 1162 | 1162 | } | 
| 1163 | -    if($w==0) { | |
| 1164 | - $w = $h*$info['w']/$info['h']; | |
| 1163 | +    if ($w == 0) { | |
| 1164 | + $w = $h * $info['w'] / $info['h']; | |
| 1165 | 1165 | } | 
| 1166 | -    if($h==0) { | |
| 1167 | - $h = $w*$info['h']/$info['w']; | |
| 1166 | +    if ($h == 0) { | |
| 1167 | + $h = $w * $info['h'] / $info['w']; | |
| 1168 | 1168 | } | 
| 1169 | 1169 | |
| 1170 | 1170 | // Flowing mode | 
| 1171 | - if($y===null) | |
| 1171 | + if ($y === null) | |
| 1172 | 1172 |      { | 
| 1173 | - if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak()) | |
| 1173 | + if ($this->y + $h > $this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak()) | |
| 1174 | 1174 |          { | 
| 1175 | 1175 | // Automatic page break | 
| 1176 | 1176 | $x2 = $this->x; | 
| 1177 | - $this->AddPage($this->CurOrientation,$this->CurPageSize); | |
| 1177 | + $this->AddPage($this->CurOrientation, $this->CurPageSize); | |
| 1178 | 1178 | $this->x = $x2; | 
| 1179 | 1179 | } | 
| 1180 | 1180 | $y = $this->y; | 
| 1181 | 1181 | $this->y += $h; | 
| 1182 | 1182 | } | 
| 1183 | 1183 | |
| 1184 | -    if($x===null) { | |
| 1184 | +    if ($x === null) { | |
| 1185 | 1185 | $x = $this->x; | 
| 1186 | 1186 | } | 
| 1187 | -    $this->_out(sprintf('q %.2F 0 0 %.2F %.2F %.2F cm /I%d Do Q',$w*$this->k,$h*$this->k,$x*$this->k,($this->h-($y+$h))*$this->k,$info['i'])); | |
| 1188 | -    if($link) { | |
| 1189 | - $this->Link($x,$y,$w,$h,$link); | |
| 1187 | +    $this->_out(sprintf('q %.2F 0 0 %.2F %.2F %.2F cm /I%d Do Q', $w * $this->k, $h * $this->k, $x * $this->k, ($this->h - ($y + $h)) * $this->k, $info['i'])); | |
| 1188 | +    if ($link) { | |
| 1189 | + $this->Link($x, $y, $w, $h, $link); | |
| 1190 | 1190 | } | 
| 1191 | 1191 | } | 
| 1192 | 1192 | |
| @@ -1782,62 +1782,62 @@ discard block | ||
| 1782 | 1782 |      $this->_out('1 0 obj'); | 
| 1783 | 1783 |      $this->_out('<</Type /Pages'); | 
| 1784 | 1784 | $kids = '/Kids ['; | 
| 1785 | -    for($i=0;$i<$nb;$i++) { | |
| 1786 | - $kids .= (3+2*$i).' 0 R '; | |
| 1785 | +    for ($i = 0; $i < $nb; $i++) { | |
| 1786 | + $kids .= (3 + 2 * $i).' 0 R '; | |
| 1787 | 1787 | } | 
| 1788 | 1788 | $this->_out($kids.']'); | 
| 1789 | 1789 |      $this->_out('/Count '.$nb); | 
| 1790 | -    $this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]',$wPt,$hPt)); | |
| 1790 | +    $this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]', $wPt, $hPt)); | |
| 1791 | 1791 |      $this->_out('>>'); | 
| 1792 | 1792 |      $this->_out('endobj'); | 
| 1793 | 1793 | } | 
| 1794 | 1794 | |
| 1795 | 1795 | function _putfonts() | 
| 1796 | 1796 |  { | 
| 1797 | - $nf=$this->n; | |
| 1798 | - foreach($this->diffs as $diff) | |
| 1797 | + $nf = $this->n; | |
| 1798 | + foreach ($this->diffs as $diff) | |
| 1799 | 1799 |      { | 
| 1800 | 1800 | // Encodings | 
| 1801 | 1801 | $this->_newobj(); | 
| 1802 | 1802 |          $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>'); | 
| 1803 | 1803 |          $this->_out('endobj'); | 
| 1804 | 1804 | } | 
| 1805 | - foreach($this->FontFiles as $file=>$info) | |
| 1805 | + foreach ($this->FontFiles as $file=>$info) | |
| 1806 | 1806 |      { | 
| 1807 | -        if (!isset($info['type']) || $info['type']!='TTF') { | |
| 1807 | +        if (!isset($info['type']) || $info['type'] != 'TTF') { | |
| 1808 | 1808 | // Font file embedding | 
| 1809 | 1809 | $this->_newobj(); | 
| 1810 | - $this->FontFiles[$file]['n']=$this->n; | |
| 1811 | - $font=''; | |
| 1812 | - $f=fopen($this->_getfontpath().$file,'rb',1); | |
| 1813 | -        if(!$f) { | |
| 1810 | + $this->FontFiles[$file]['n'] = $this->n; | |
| 1811 | + $font = ''; | |
| 1812 | + $f = fopen($this->_getfontpath().$file, 'rb', 1); | |
| 1813 | +        if (!$f) { | |
| 1814 | 1814 |                      $this->Error('Font file not found'); | 
| 1815 | 1815 | } | 
| 1816 | -        while(!feof($f)) { | |
| 1817 | - $font.=fread($f,8192); | |
| 1816 | +        while (!feof($f)) { | |
| 1817 | + $font .= fread($f, 8192); | |
| 1818 | 1818 | } | 
| 1819 | 1819 | fclose($f); | 
| 1820 | - $compressed=(substr($file,-2)=='.z'); | |
| 1821 | - if(!$compressed && isset($info['length2'])) | |
| 1820 | + $compressed = (substr($file, -2) == '.z'); | |
| 1821 | + if (!$compressed && isset($info['length2'])) | |
| 1822 | 1822 |          { | 
| 1823 | - $header=(ord($font[0])==128); | |
| 1824 | - if($header) | |
| 1823 | + $header = (ord($font[0]) == 128); | |
| 1824 | + if ($header) | |
| 1825 | 1825 |              { | 
| 1826 | 1826 | // Strip first binary header | 
| 1827 | - $font=substr($font,6); | |
| 1827 | + $font = substr($font, 6); | |
| 1828 | 1828 | } | 
| 1829 | - if($header && ord($font[$info['length1']])==128) | |
| 1829 | + if ($header && ord($font[$info['length1']]) == 128) | |
| 1830 | 1830 |              { | 
| 1831 | 1831 | // Strip second binary header | 
| 1832 | - $font=substr($font,0,$info['length1']).substr($font,$info['length1']+6); | |
| 1832 | + $font = substr($font, 0, $info['length1']).substr($font, $info['length1'] + 6); | |
| 1833 | 1833 | } | 
| 1834 | 1834 | } | 
| 1835 | 1835 |          $this->_out('<</Length '.strlen($font)); | 
| 1836 | -        if($compressed) { | |
| 1836 | +        if ($compressed) { | |
| 1837 | 1837 |                      $this->_out('/Filter /FlateDecode'); | 
| 1838 | 1838 | } | 
| 1839 | 1839 |          $this->_out('/Length1 '.$info['length1']); | 
| 1840 | -        if(isset($info['length2'])) { | |
| 1840 | +        if (isset($info['length2'])) { | |
| 1841 | 1841 |                      $this->_out('/Length2 '.$info['length2'].' /Length3 0'); | 
| 1842 | 1842 | } | 
| 1843 | 1843 |          $this->_out('>>'); | 
| @@ -1845,40 +1845,40 @@ discard block | ||
| 1845 | 1845 |          $this->_out('endobj'); | 
| 1846 | 1846 | } | 
| 1847 | 1847 | } | 
| 1848 | - foreach($this->fonts as $k=>$font) | |
| 1848 | + foreach ($this->fonts as $k=>$font) | |
| 1849 | 1849 |      { | 
| 1850 | 1850 | // Font objects | 
| 1851 | 1851 | //$this->fonts[$k]['n']=$this->n+1; | 
| 1852 | 1852 | $type = $font['type']; | 
| 1853 | 1853 | $name = $font['name']; | 
| 1854 | - if($type=='Core') | |
| 1854 | + if ($type == 'Core') | |
| 1855 | 1855 |          { | 
| 1856 | 1856 | // Standard font | 
| 1857 | - $this->fonts[$k]['n']=$this->n+1; | |
| 1857 | + $this->fonts[$k]['n'] = $this->n + 1; | |
| 1858 | 1858 | $this->_newobj(); | 
| 1859 | 1859 |              $this->_out('<</Type /Font'); | 
| 1860 | 1860 |              $this->_out('/BaseFont /'.$name); | 
| 1861 | 1861 |              $this->_out('/Subtype /Type1'); | 
| 1862 | -            if($name!='Symbol' && $name!='ZapfDingbats') { | |
| 1862 | +            if ($name != 'Symbol' && $name != 'ZapfDingbats') { | |
| 1863 | 1863 |                              $this->_out('/Encoding /WinAnsiEncoding'); | 
| 1864 | 1864 | } | 
| 1865 | 1865 |              $this->_out('>>'); | 
| 1866 | 1866 |              $this->_out('endobj'); | 
| 1867 | - } elseif($type=='Type1' || $type=='TrueType') | |
| 1867 | + } elseif ($type == 'Type1' || $type == 'TrueType') | |
| 1868 | 1868 |          { | 
| 1869 | 1869 | // Additional Type1 or TrueType font | 
| 1870 | - $this->fonts[$k]['n']=$this->n+1; | |
| 1870 | + $this->fonts[$k]['n'] = $this->n + 1; | |
| 1871 | 1871 | $this->_newobj(); | 
| 1872 | 1872 |              $this->_out('<</Type /Font'); | 
| 1873 | 1873 |              $this->_out('/BaseFont /'.$name); | 
| 1874 | 1874 |              $this->_out('/Subtype /'.$type); | 
| 1875 | 1875 |              $this->_out('/FirstChar 32 /LastChar 255'); | 
| 1876 | -            $this->_out('/Widths '.($this->n+1).' 0 R'); | |
| 1877 | -            $this->_out('/FontDescriptor '.($this->n+2).' 0 R'); | |
| 1878 | - if($font['enc']) | |
| 1876 | +            $this->_out('/Widths '.($this->n + 1).' 0 R'); | |
| 1877 | +            $this->_out('/FontDescriptor '.($this->n + 2).' 0 R'); | |
| 1878 | + if ($font['enc']) | |
| 1879 | 1879 |              { | 
| 1880 | -                if(isset($font['diff'])) { | |
| 1881 | -                                    $this->_out('/Encoding '.($nf+$font['diff']).' 0 R'); | |
| 1880 | +                if (isset($font['diff'])) { | |
| 1881 | +                                    $this->_out('/Encoding '.($nf + $font['diff']).' 0 R'); | |
| 1882 | 1882 |                  } else { | 
| 1883 | 1883 |                                      $this->_out('/Encoding /WinAnsiEncoding'); | 
| 1884 | 1884 | } | 
| @@ -2327,15 +2327,15 @@ discard block | ||
| 2327 | 2327 |      for ($i = 0; $i < $len; $i++) { | 
| 2328 | 2328 | $uni = -1; | 
| 2329 | 2329 | $h = ord($str[$i]); | 
| 2330 | - if ( $h <= 0x7F ) | |
| 2330 | + if ($h <= 0x7F) | |
| 2331 | 2331 | $uni = $h; | 
| 2332 | -        elseif ( $h >= 0xC2 ) { | |
| 2333 | - if ( ($h <= 0xDF) && ($i < $len -1) ) | |
| 2332 | +        elseif ($h >= 0xC2) { | |
| 2333 | + if (($h <= 0xDF) && ($i < $len - 1)) | |
| 2334 | 2334 | $uni = ($h & 0x1F) << 6 | (ord($str[++$i]) & 0x3F); | 
| 2335 | - elseif ( ($h <= 0xEF) && ($i < $len -2) ) | |
| 2335 | + elseif (($h <= 0xEF) && ($i < $len - 2)) | |
| 2336 | 2336 | $uni = ($h & 0x0F) << 12 | (ord($str[++$i]) & 0x3F) << 6 | 
| 2337 | 2337 | | (ord($str[++$i]) & 0x3F); | 
| 2338 | - elseif ( ($h <= 0xF4) && ($i < $len -3) ) | |
| 2338 | + elseif (($h <= 0xF4) && ($i < $len - 3)) | |
| 2339 | 2339 | $uni = ($h & 0x0F) << 18 | (ord($str[++$i]) & 0x3F) << 12 | 
| 2340 | 2340 | | (ord($str[++$i]) & 0x3F) << 6 | 
| 2341 | 2341 | | (ord($str[++$i]) & 0x3F); | 
| @@ -108,26 +108,28 @@ discard block | ||
| 108 | 108 |      if (defined('FPDF_FONTPATH')) | 
| 109 | 109 |      { | 
| 110 | 110 | $this->fontpath = FPDF_FONTPATH; | 
| 111 | - if (substr($this->fontpath, -1) != '/' && substr($this->fontpath, -1) != '\\') | |
| 112 | - $this->fontpath .= '/'; | |
| 111 | +        if (substr($this->fontpath, -1) != '/' && substr($this->fontpath, -1) != '\\') { | |
| 112 | + $this->fontpath .= '/'; | |
| 113 | + } | |
| 114 | +    } elseif (is_dir(dirname(__FILE__).'/font')) { | |
| 115 | + $this->fontpath = dirname(__FILE__).'/font/'; | |
| 116 | +    } else { | |
| 117 | + $this->fontpath = ''; | |
| 113 | 118 | } | 
| 114 | - elseif (is_dir(dirname(__FILE__).'/font')) | |
| 115 | - $this->fontpath = dirname(__FILE__).'/font/'; | |
| 116 | - else | |
| 117 | - $this->fontpath = ''; | |
| 118 | 119 | // Core fonts | 
| 119 | 120 |      $this->CoreFonts = array('courier', 'helvetica', 'times', 'symbol', 'zapfdingbats'); | 
| 120 | 121 | // Scale factor | 
| 121 | - if ($unit == 'pt') | |
| 122 | - $this->k = 1; | |
| 123 | - elseif ($unit == 'mm') | |
| 124 | - $this->k = 72 / 25.4; | |
| 125 | - elseif ($unit == 'cm') | |
| 126 | - $this->k = 72 / 2.54; | |
| 127 | - elseif ($unit == 'in') | |
| 128 | - $this->k = 72; | |
| 129 | - else | |
| 130 | -        $this->Error('Incorrect unit: '.$unit); | |
| 122 | +    if ($unit == 'pt') { | |
| 123 | + $this->k = 1; | |
| 124 | +    } elseif ($unit == 'mm') { | |
| 125 | + $this->k = 72 / 25.4; | |
| 126 | +    } elseif ($unit == 'cm') { | |
| 127 | + $this->k = 72 / 2.54; | |
| 128 | +    } elseif ($unit == 'in') { | |
| 129 | + $this->k = 72; | |
| 130 | +    } else { | |
| 131 | +            $this->Error('Incorrect unit: '.$unit); | |
| 132 | + } | |
| 131 | 133 | // Page sizes | 
| 132 | 134 |      $this->StdPageSizes = array('a3'=>array(841.89, 1190.55), 'a4'=>array(595.28, 841.89), 'a5'=>array(420.94, 595.28), | 
| 133 | 135 | 'letter'=>array(612, 792), 'legal'=>array(612, 1008)); | 
| @@ -141,15 +143,14 @@ discard block | ||
| 141 | 143 | $this->DefOrientation = 'P'; | 
| 142 | 144 | $this->w = $size[0]; | 
| 143 | 145 | $this->h = $size[1]; | 
| 144 | - } | |
| 145 | - elseif ($orientation == 'l' || $orientation == 'landscape') | |
| 146 | + } elseif ($orientation == 'l' || $orientation == 'landscape') | |
| 146 | 147 |      { | 
| 147 | 148 | $this->DefOrientation = 'L'; | 
| 148 | 149 | $this->w = $size[1]; | 
| 149 | 150 | $this->h = $size[0]; | 
| 151 | +    } else { | |
| 152 | +            $this->Error('Incorrect orientation: '.$orientation); | |
| 150 | 153 | } | 
| 151 | - else | |
| 152 | -        $this->Error('Incorrect orientation: '.$orientation); | |
| 153 | 154 | $this->CurOrientation = $this->DefOrientation; | 
| 154 | 155 | $this->wPt = $this->w * $this->k; | 
| 155 | 156 | $this->hPt = $this->h * $this->k; | 
| @@ -189,9 +190,10 @@ discard block | ||
| 189 | 190 |  { | 
| 190 | 191 | // Set left margin | 
| 191 | 192 | $this->lMargin = $margin; | 
| 192 | - if ($this->page > 0 && $this->x < $margin) | |
| 193 | - $this->x = $margin; | |
| 194 | -} | |
| 193 | +    if ($this->page > 0 && $this->x < $margin) { | |
| 194 | + $this->x = $margin; | |
| 195 | + } | |
| 196 | + } | |
| 195 | 197 | |
| 196 | 198 | function SetTopMargin($margin) | 
| 197 | 199 |  { | 
| @@ -222,15 +224,17 @@ discard block | ||
| 222 | 224 | function SetDisplayMode($zoom, $layout='default') | 
| 223 | 225 |  { | 
| 224 | 226 | // Set display mode in viewer | 
| 225 | - if($zoom=='fullpage' || $zoom=='fullwidth' || $zoom=='real' || $zoom=='default' || !is_string($zoom)) | |
| 226 | - $this->ZoomMode = $zoom; | |
| 227 | - else | |
| 228 | -        $this->Error('Incorrect zoom display mode: '.$zoom); | |
| 229 | - if($layout=='single' || $layout=='continuous' || $layout=='two' || $layout=='default') | |
| 230 | - $this->LayoutMode = $layout; | |
| 231 | - else | |
| 232 | -        $this->Error('Incorrect layout display mode: '.$layout); | |
| 233 | -} | |
| 227 | +    if($zoom=='fullpage' || $zoom=='fullwidth' || $zoom=='real' || $zoom=='default' || !is_string($zoom)) { | |
| 228 | + $this->ZoomMode = $zoom; | |
| 229 | +    } else { | |
| 230 | +            $this->Error('Incorrect zoom display mode: '.$zoom); | |
| 231 | + } | |
| 232 | +    if($layout=='single' || $layout=='continuous' || $layout=='two' || $layout=='default') { | |
| 233 | + $this->LayoutMode = $layout; | |
| 234 | +    } else { | |
| 235 | +            $this->Error('Incorrect layout display mode: '.$layout); | |
| 236 | + } | |
| 237 | + } | |
| 234 | 238 | |
| 235 | 239 | /** | 
| 236 | 240 | * @param boolean $compress | 
| @@ -238,49 +242,55 @@ discard block | ||
| 238 | 242 | function SetCompression($compress) | 
| 239 | 243 |  { | 
| 240 | 244 | // Set page compression | 
| 241 | -    if (function_exists('gzcompress')) | |
| 242 | - $this->compress = $compress; | |
| 243 | - else | |
| 244 | - $this->compress = false; | |
| 245 | -} | |
| 245 | +    if (function_exists('gzcompress')) { | |
| 246 | + $this->compress = $compress; | |
| 247 | +    } else { | |
| 248 | + $this->compress = false; | |
| 249 | + } | |
| 250 | + } | |
| 246 | 251 | |
| 247 | 252 | function SetTitle($title, $isUTF8 = false) | 
| 248 | 253 |  { | 
| 249 | 254 | // Title of document | 
| 250 | - if ($isUTF8) | |
| 251 | - $title = $this->_UTF8toUTF16($title); | |
| 255 | +    if ($isUTF8) { | |
| 256 | + $title = $this->_UTF8toUTF16($title); | |
| 257 | + } | |
| 252 | 258 | $this->title = $title; | 
| 253 | 259 | } | 
| 254 | 260 | |
| 255 | 261 | function SetSubject($subject, $isUTF8 = false) | 
| 256 | 262 |  { | 
| 257 | 263 | // Subject of document | 
| 258 | - if ($isUTF8) | |
| 259 | - $subject = $this->_UTF8toUTF16($subject); | |
| 264 | +    if ($isUTF8) { | |
| 265 | + $subject = $this->_UTF8toUTF16($subject); | |
| 266 | + } | |
| 260 | 267 | $this->subject = $subject; | 
| 261 | 268 | } | 
| 262 | 269 | |
| 263 | 270 | function SetAuthor($author, $isUTF8 = false) | 
| 264 | 271 |  { | 
| 265 | 272 | // Author of document | 
| 266 | - if ($isUTF8) | |
| 267 | - $author = $this->_UTF8toUTF16($author); | |
| 273 | +    if ($isUTF8) { | |
| 274 | + $author = $this->_UTF8toUTF16($author); | |
| 275 | + } | |
| 268 | 276 | $this->author = $author; | 
| 269 | 277 | } | 
| 270 | 278 | |
| 271 | 279 | function SetKeywords($keywords, $isUTF8 = false) | 
| 272 | 280 |  { | 
| 273 | 281 | // Keywords of document | 
| 274 | - if ($isUTF8) | |
| 275 | - $keywords = $this->_UTF8toUTF16($keywords); | |
| 282 | +    if ($isUTF8) { | |
| 283 | + $keywords = $this->_UTF8toUTF16($keywords); | |
| 284 | + } | |
| 276 | 285 | $this->keywords = $keywords; | 
| 277 | 286 | } | 
| 278 | 287 | |
| 279 | 288 | function SetCreator($creator, $isUTF8 = false) | 
| 280 | 289 |  { | 
| 281 | 290 | // Creator of document | 
| 282 | - if ($isUTF8) | |
| 283 | - $creator = $this->_UTF8toUTF16($creator); | |
| 291 | +    if ($isUTF8) { | |
| 292 | + $creator = $this->_UTF8toUTF16($creator); | |
| 293 | + } | |
| 284 | 294 | $this->creator = $creator; | 
| 285 | 295 | } | 
| 286 | 296 | |
| @@ -305,10 +315,12 @@ discard block | ||
| 305 | 315 | function Close() | 
| 306 | 316 |  { | 
| 307 | 317 | // Terminate document | 
| 308 | - if ($this->state == 3) | |
| 309 | - return; | |
| 310 | - if ($this->page == 0) | |
| 311 | - $this->AddPage(); | |
| 318 | +    if ($this->state == 3) { | |
| 319 | + return; | |
| 320 | + } | |
| 321 | +    if ($this->page == 0) { | |
| 322 | + $this->AddPage(); | |
| 323 | + } | |
| 312 | 324 | // Page footer | 
| 313 | 325 | $this->InFooter = true; | 
| 314 | 326 | $this->Footer(); | 
| @@ -322,8 +334,9 @@ discard block | ||
| 322 | 334 | function AddPage($orientation = '', $size = '') | 
| 323 | 335 |  { | 
| 324 | 336 | // Start a new page | 
| 325 | - if ($this->state == 0) | |
| 326 | - $this->Open(); | |
| 337 | +    if ($this->state == 0) { | |
| 338 | + $this->Open(); | |
| 339 | + } | |
| 327 | 340 | $family = $this->FontFamily; | 
| 328 | 341 | $style = $this->FontStyle.($this->underline ? 'U' : ''); | 
| 329 | 342 | $fontsize = $this->FontSizePt; | 
| @@ -349,15 +362,18 @@ discard block | ||
| 349 | 362 | $this->LineWidth = $lw; | 
| 350 | 363 |      $this->_out(sprintf('%.2F w', $lw * $this->k)); | 
| 351 | 364 | // Set font | 
| 352 | - if ($family) | |
| 353 | - $this->SetFont($family, $style, $fontsize); | |
| 365 | +    if ($family) { | |
| 366 | + $this->SetFont($family, $style, $fontsize); | |
| 367 | + } | |
| 354 | 368 | // Set colors | 
| 355 | 369 | $this->DrawColor = $dc; | 
| 356 | - if ($dc != '0 G') | |
| 357 | - $this->_out($dc); | |
| 370 | +    if ($dc != '0 G') { | |
| 371 | + $this->_out($dc); | |
| 372 | + } | |
| 358 | 373 | $this->FillColor = $fc; | 
| 359 | - if ($fc != '0 g') | |
| 360 | - $this->_out($fc); | |
| 374 | +    if ($fc != '0 g') { | |
| 375 | + $this->_out($fc); | |
| 376 | + } | |
| 361 | 377 | $this->TextColor = $tc; | 
| 362 | 378 | $this->ColorFlag = $cf; | 
| 363 | 379 | // Page header | 
| @@ -371,8 +387,9 @@ discard block | ||
| 371 | 387 |          $this->_out(sprintf('%.2F w', $lw * $this->k)); | 
| 372 | 388 | } | 
| 373 | 389 | // Restore font | 
| 374 | - if ($family) | |
| 375 | - $this->SetFont($family, $style, $fontsize); | |
| 390 | +    if ($family) { | |
| 391 | + $this->SetFont($family, $style, $fontsize); | |
| 392 | + } | |
| 376 | 393 | // Restore colors | 
| 377 | 394 | if ($this->DrawColor != $dc) | 
| 378 | 395 |      { | 
| @@ -498,23 +515,23 @@ discard block | ||
| 498 | 515 | // Add a TrueType, OpenType or Type1 font | 
| 499 | 516 | $family = strtolower($family); | 
| 500 | 517 | $style = strtoupper($style); | 
| 501 | - if($style=='IB') | |
| 502 | - $style='BI'; | |
| 518 | +    if($style=='IB') { | |
| 519 | + $style='BI'; | |
| 520 | + } | |
| 503 | 521 |      if($file=='') { | 
| 504 | 522 |          if ($uni) { | 
| 505 | 523 |          $file = str_replace(' ','',$family).strtolower($style).'.ttf'; | 
| 506 | - } | |
| 507 | -        else { | |
| 524 | +        } else { | |
| 508 | 525 |          $file = str_replace(' ','',$family).strtolower($style).'.php'; | 
| 509 | 526 | } | 
| 510 | 527 | } | 
| 511 | 528 | $fontkey = $family.$style; | 
| 512 | - if(isset($this->fonts[$fontkey])) | |
| 513 | - return; | |
| 529 | +    if(isset($this->fonts[$fontkey])) { | |
| 530 | + return; | |
| 531 | + } | |
| 514 | 532 | |
| 515 | 533 |      if ($uni) { | 
| 516 | -        if (defined("_SYSTEM_TTFONTS") && file_exists(_SYSTEM_TTFONTS.$file )) { $ttffilename = _SYSTEM_TTFONTS.$file ; } | |
| 517 | -        else { $ttffilename = $this->_getfontpath().'unifont/'.$file ; } | |
| 534 | +        if (defined("_SYSTEM_TTFONTS") && file_exists(_SYSTEM_TTFONTS.$file )) { $ttffilename = _SYSTEM_TTFONTS.$file ; } else { $ttffilename = $this->_getfontpath().'unifont/'.$file ; } | |
| 518 | 535 | $unifilename = $this->_getfontpath().'unifont/'.strtolower(substr($file ,0,(strpos($file ,'.')))); | 
| 519 | 536 | $name = ''; | 
| 520 | 537 | $originalsize = 0; | 
| @@ -563,22 +580,21 @@ discard block | ||
| 563 | 580 | @unlink($unifilename.'.cw127.php'); | 
| 564 | 581 | } | 
| 565 | 582 | unset($ttf); | 
| 566 | - } | |
| 567 | -        else { | |
| 583 | +        } else { | |
| 568 | 584 | $cw = @file_get_contents($unifilename.'.cw.dat'); | 
| 569 | 585 | } | 
| 570 | 586 | $i = count($this->fonts) + 1; | 
| 571 | - if (!empty($this->AliasNbPages)) | |
| 572 | - $sbarr = range(0, 57); | |
| 573 | - else | |
| 574 | - $sbarr = range(0, 32); | |
| 587 | +        if (!empty($this->AliasNbPages)) { | |
| 588 | + $sbarr = range(0, 57); | |
| 589 | +        } else { | |
| 590 | + $sbarr = range(0, 32); | |
| 591 | + } | |
| 575 | 592 |          $this->fonts[$fontkey] = array('i'=>$i, 'type'=>$type, 'name'=>$name, 'desc'=>$desc, 'up'=>$up, 'ut'=>$ut, 'cw'=>$cw, 'ttffile'=>$ttffile, 'fontkey'=>$fontkey, 'subset'=>$sbarr, 'unifilename'=>$unifilename); | 
| 576 | 593 | |
| 577 | 594 |          $this->FontFiles[$fontkey] = array('length1'=>$originalsize, 'type'=>"TTF", 'ttffile'=>$ttffile); | 
| 578 | 595 |          $this->FontFiles[$file] = array('type'=>"TTF"); | 
| 579 | 596 | unset($cw); | 
| 580 | - } | |
| 581 | -    else { | |
| 597 | +    } else { | |
| 582 | 598 | $info = $this->_loadfont($file); | 
| 583 | 599 | $info['i'] = count($this->fonts) + 1; | 
| 584 | 600 | if (!empty($info['diff'])) | 
| @@ -595,10 +611,11 @@ discard block | ||
| 595 | 611 | if (!empty($info['file'])) | 
| 596 | 612 |          { | 
| 597 | 613 | // Embedded font | 
| 598 | - if ($info['type'] == 'TrueType') | |
| 599 | -                $this->FontFiles[$info['file']] = array('length1'=>$info['originalsize']); | |
| 600 | - else | |
| 601 | -                $this->FontFiles[$info['file']] = array('length1'=>$info['size1'], 'length2'=>$info['size2']); | |
| 614 | +            if ($info['type'] == 'TrueType') { | |
| 615 | +                            $this->FontFiles[$info['file']] = array('length1'=>$info['originalsize']); | |
| 616 | +            } else { | |
| 617 | +                            $this->FontFiles[$info['file']] = array('length1'=>$info['size1'], 'length2'=>$info['size2']); | |
| 618 | + } | |
| 602 | 619 | } | 
| 603 | 620 | $this->fonts[$fontkey] = $info; | 
| 604 | 621 | } | 
| @@ -660,22 +677,24 @@ discard block | ||
| 660 | 677 | $this->FontSizePt = $size; | 
| 661 | 678 | $this->FontSize = $size / $this->k; | 
| 662 | 679 | $this->CurrentFont = &$this->fonts[$fontkey]; | 
| 663 | -    if ($this->fonts[$fontkey]['type'] == 'TTF') { $this->unifontSubset = true; } | |
| 664 | -    else { $this->unifontSubset = false; } | |
| 665 | - if ($this->page > 0) | |
| 666 | -        $this->_out(sprintf('BT /F%d %.2F Tf ET', $this->CurrentFont['i'], $this->FontSizePt)); | |
| 667 | -} | |
| 680 | +    if ($this->fonts[$fontkey]['type'] == 'TTF') { $this->unifontSubset = true; } else { $this->unifontSubset = false; } | |
| 681 | +    if ($this->page > 0) { | |
| 682 | +            $this->_out(sprintf('BT /F%d %.2F Tf ET', $this->CurrentFont['i'], $this->FontSizePt)); | |
| 683 | + } | |
| 684 | + } | |
| 668 | 685 | |
| 669 | 686 | function SetFontSize($size) | 
| 670 | 687 |  { | 
| 671 | 688 | // Set font size in points | 
| 672 | - if ($this->FontSizePt == $size) | |
| 673 | - return; | |
| 689 | +    if ($this->FontSizePt == $size) { | |
| 690 | + return; | |
| 691 | + } | |
| 674 | 692 | $this->FontSizePt = $size; | 
| 675 | 693 | $this->FontSize = $size / $this->k; | 
| 676 | - if ($this->page > 0) | |
| 677 | -        $this->_out(sprintf('BT /F%d %.2F Tf ET', $this->CurrentFont['i'], $this->FontSizePt)); | |
| 678 | -} | |
| 694 | +    if ($this->page > 0) { | |
| 695 | +            $this->_out(sprintf('BT /F%d %.2F Tf ET', $this->CurrentFont['i'], $this->FontSizePt)); | |
| 696 | + } | |
| 697 | + } | |
| 679 | 698 | |
| 680 | 699 | function AddLink() | 
| 681 | 700 |  { | 
| @@ -688,10 +707,12 @@ discard block | ||
| 688 | 707 | function SetLink($link, $y = 0, $page = -1) | 
| 689 | 708 |  { | 
| 690 | 709 | // Set destination of internal link | 
| 691 | - if ($y == -1) | |
| 692 | - $y = $this->y; | |
| 693 | - if ($page == -1) | |
| 694 | - $page = $this->page; | |
| 710 | +    if ($y == -1) { | |
| 711 | + $y = $this->y; | |
| 712 | + } | |
| 713 | +    if ($page == -1) { | |
| 714 | + $page = $this->page; | |
| 715 | + } | |
| 695 | 716 | $this->links[$link] = array($page, $y); | 
| 696 | 717 | } | 
| 697 | 718 | |
| @@ -711,16 +732,19 @@ discard block | ||
| 711 | 732 | if ($this->unifontSubset) | 
| 712 | 733 |      { | 
| 713 | 734 |          $txt2 = '('.$this->_escape($this->UTF8ToUTF16BE($txt, false)).')'; | 
| 714 | - foreach ($this->UTF8StringToArray($txt) as $uni) | |
| 715 | - $this->CurrentFont['subset'][$uni] = $uni; | |
| 735 | +        foreach ($this->UTF8StringToArray($txt) as $uni) { | |
| 736 | + $this->CurrentFont['subset'][$uni] = $uni; | |
| 737 | + } | |
| 738 | +    } else { | |
| 739 | +            $txt2 = '('.$this->_escape($txt).')'; | |
| 716 | 740 | } | 
| 717 | - else | |
| 718 | -        $txt2 = '('.$this->_escape($txt).')'; | |
| 719 | 741 |      $s = sprintf('BT %.2F %.2F Td %s Tj ET', $x * $this->k, ($this->h - $y) * $this->k, $txt2); | 
| 720 | - if ($this->underline && $txt != '') | |
| 721 | - $s .= ' '.$this->_dounderline($x, $y, $txt); | |
| 722 | - if ($this->ColorFlag) | |
| 723 | - $s = 'q '.$this->TextColor.' '.$s.' Q'; | |
| 742 | +    if ($this->underline && $txt != '') { | |
| 743 | + $s .= ' '.$this->_dounderline($x, $y, $txt); | |
| 744 | + } | |
| 745 | +    if ($this->ColorFlag) { | |
| 746 | + $s = 'q '.$this->TextColor.' '.$s.' Q'; | |
| 747 | + } | |
| 724 | 748 | $this->_out($s); | 
| 725 | 749 | } | 
| 726 | 750 | |
| @@ -752,45 +776,54 @@ discard block | ||
| 752 | 776 |              $this->_out(sprintf('%.3F Tw', $ws * $k)); | 
| 753 | 777 | } | 
| 754 | 778 | } | 
| 755 | - if ($w == 0) | |
| 756 | - $w = $this->w - $this->rMargin - $this->x; | |
| 779 | +    if ($w == 0) { | |
| 780 | + $w = $this->w - $this->rMargin - $this->x; | |
| 781 | + } | |
| 757 | 782 | $s = ''; | 
| 758 | 783 | if ($fill || $border == 1) | 
| 759 | 784 |      { | 
| 760 | - if ($fill) | |
| 761 | - $op = ($border == 1) ? 'B' : 'f'; | |
| 762 | - else | |
| 763 | - $op = 'S'; | |
| 785 | +        if ($fill) { | |
| 786 | + $op = ($border == 1) ? 'B' : 'f'; | |
| 787 | +        } else { | |
| 788 | + $op = 'S'; | |
| 789 | + } | |
| 764 | 790 |          $s = sprintf('%.2F %.2F %.2F %.2F re %s ', $this->x * $k, ($this->h - $this->y) * $k, $w * $k, -$h * $k, $op); | 
| 765 | 791 | } | 
| 766 | 792 | if (is_string($border)) | 
| 767 | 793 |      { | 
| 768 | 794 | $x = $this->x; | 
| 769 | 795 | $y = $this->y; | 
| 770 | - if (strpos($border, 'L') !== false) | |
| 771 | -            $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', $x * $k, ($this->h - $y) * $k, $x * $k, ($this->h - ($y + $h)) * $k); | |
| 772 | - if (strpos($border, 'T') !== false) | |
| 773 | -            $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', $x * $k, ($this->h - $y) * $k, ($x + $w) * $k, ($this->h - $y) * $k); | |
| 774 | - if (strpos($border, 'R') !== false) | |
| 775 | -            $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', ($x + $w) * $k, ($this->h - $y) * $k, ($x + $w) * $k, ($this->h - ($y + $h)) * $k); | |
| 776 | - if (strpos($border, 'B') !== false) | |
| 777 | -            $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', $x * $k, ($this->h - ($y + $h)) * $k, ($x + $w) * $k, ($this->h - ($y + $h)) * $k); | |
| 796 | +        if (strpos($border, 'L') !== false) { | |
| 797 | +                    $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', $x * $k, ($this->h - $y) * $k, $x * $k, ($this->h - ($y + $h)) * $k); | |
| 798 | + } | |
| 799 | +        if (strpos($border, 'T') !== false) { | |
| 800 | +                    $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', $x * $k, ($this->h - $y) * $k, ($x + $w) * $k, ($this->h - $y) * $k); | |
| 801 | + } | |
| 802 | +        if (strpos($border, 'R') !== false) { | |
| 803 | +                    $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', ($x + $w) * $k, ($this->h - $y) * $k, ($x + $w) * $k, ($this->h - ($y + $h)) * $k); | |
| 804 | + } | |
| 805 | +        if (strpos($border, 'B') !== false) { | |
| 806 | +                    $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', $x * $k, ($this->h - ($y + $h)) * $k, ($x + $w) * $k, ($this->h - ($y + $h)) * $k); | |
| 807 | + } | |
| 778 | 808 | } | 
| 779 | 809 | if ($txt !== '') | 
| 780 | 810 |      { | 
| 781 | - if ($align == 'R') | |
| 782 | - $dx = $w - $this->cMargin - $this->GetStringWidth($txt); | |
| 783 | - elseif ($align == 'C') | |
| 784 | - $dx = ($w - $this->GetStringWidth($txt)) / 2; | |
| 785 | - else | |
| 786 | - $dx = $this->cMargin; | |
| 787 | - if ($this->ColorFlag) | |
| 788 | - $s .= 'q '.$this->TextColor.' '; | |
| 811 | +        if ($align == 'R') { | |
| 812 | + $dx = $w - $this->cMargin - $this->GetStringWidth($txt); | |
| 813 | +        } elseif ($align == 'C') { | |
| 814 | + $dx = ($w - $this->GetStringWidth($txt)) / 2; | |
| 815 | +        } else { | |
| 816 | + $dx = $this->cMargin; | |
| 817 | + } | |
| 818 | +        if ($this->ColorFlag) { | |
| 819 | + $s .= 'q '.$this->TextColor.' '; | |
| 820 | + } | |
| 789 | 821 | |
| 790 | 822 | // If multibyte, Tw has no effect - do word spacing using an adjustment before each space | 
| 791 | 823 |          if ($this->ws && $this->unifontSubset) { | 
| 792 | - foreach ($this->UTF8StringToArray($txt) as $uni) | |
| 793 | - $this->CurrentFont['subset'][$uni] = $uni; | |
| 824 | +            foreach ($this->UTF8StringToArray($txt) as $uni) { | |
| 825 | + $this->CurrentFont['subset'][$uni] = $uni; | |
| 826 | + } | |
| 794 | 827 |              $space = $this->_escape($this->UTF8ToUTF16BE(' ', false)); | 
| 795 | 828 |              $s .= sprintf('BT 0 Tw %.2F %.2F Td [', ($this->x + $dx) * $k, ($this->h - ($this->y + .5 * $h + .3 * $this->FontSize)) * $k); | 
| 796 | 829 |              $t = explode(' ', $txt); | 
| @@ -806,55 +839,63 @@ discard block | ||
| 806 | 839 | } | 
| 807 | 840 | $s .= '] TJ'; | 
| 808 | 841 | $s .= ' ET'; | 
| 809 | - } | |
| 810 | -        else { | |
| 842 | +        } else { | |
| 811 | 843 | if ($this->unifontSubset) | 
| 812 | 844 |              { | 
| 813 | 845 |                  $txt2 = '('.$this->_escape($this->UTF8ToUTF16BE($txt, false)).')'; | 
| 814 | - foreach ($this->UTF8StringToArray($txt) as $uni) | |
| 815 | - $this->CurrentFont['subset'][$uni] = $uni; | |
| 846 | +                foreach ($this->UTF8StringToArray($txt) as $uni) { | |
| 847 | + $this->CurrentFont['subset'][$uni] = $uni; | |
| 848 | + } | |
| 849 | +            } else { | |
| 850 | +                            $txt2 = '('.str_replace(')', '\\)', str_replace('(', '\\(', str_replace('\\', '\\\\', $txt))).')'; | |
| 816 | 851 | } | 
| 817 | - else | |
| 818 | -                $txt2 = '('.str_replace(')', '\\)', str_replace('(', '\\(', str_replace('\\', '\\\\', $txt))).')'; | |
| 819 | 852 |              $s .= sprintf('BT %.2F %.2F Td %s Tj ET', ($this->x + $dx) * $k, ($this->h - ($this->y + .5 * $h + .3 * $this->FontSize)) * $k, $txt2); | 
| 820 | 853 | } | 
| 821 | - if ($this->underline) | |
| 822 | - $s .= ' '.$this->_dounderline($this->x + $dx, $this->y + .5 * $h + .3 * $this->FontSize, $txt); | |
| 823 | - if ($this->ColorFlag) | |
| 824 | - $s .= ' Q'; | |
| 825 | - if ($link) | |
| 826 | - $this->Link($this->x + $dx, $this->y + .5 * $h - .5 * $this->FontSize, $this->GetStringWidth($txt), $this->FontSize, $link); | |
| 854 | +        if ($this->underline) { | |
| 855 | + $s .= ' '.$this->_dounderline($this->x + $dx, $this->y + .5 * $h + .3 * $this->FontSize, $txt); | |
| 856 | + } | |
| 857 | +        if ($this->ColorFlag) { | |
| 858 | + $s .= ' Q'; | |
| 859 | + } | |
| 860 | +        if ($link) { | |
| 861 | + $this->Link($this->x + $dx, $this->y + .5 * $h - .5 * $this->FontSize, $this->GetStringWidth($txt), $this->FontSize, $link); | |
| 862 | + } | |
| 863 | + } | |
| 864 | +    if ($s) { | |
| 865 | + $this->_out($s); | |
| 827 | 866 | } | 
| 828 | - if ($s) | |
| 829 | - $this->_out($s); | |
| 830 | 867 | $this->lasth = $h; | 
| 831 | 868 | if ($ln > 0) | 
| 832 | 869 |      { | 
| 833 | 870 | // Go to next line | 
| 834 | 871 | $this->y += $h; | 
| 835 | - if ($ln == 1) | |
| 836 | - $this->x = $this->lMargin; | |
| 872 | +        if ($ln == 1) { | |
| 873 | + $this->x = $this->lMargin; | |
| 874 | + } | |
| 875 | +    } else { | |
| 876 | + $this->x += $w; | |
| 877 | + } | |
| 837 | 878 | } | 
| 838 | - else | |
| 839 | - $this->x += $w; | |
| 840 | -} | |
| 841 | 879 | |
| 842 | 880 | function MultiCell($w, $h, $txt, $border = 0, $align = 'J', $fill = false) | 
| 843 | 881 |  { | 
| 844 | 882 | // Output text with automatic or explicit line breaks | 
| 845 | 883 | $cw = &$this->CurrentFont['cw']; | 
| 846 | - if ($w == 0) | |
| 847 | - $w = $this->w - $this->rMargin - $this->x; | |
| 884 | +    if ($w == 0) { | |
| 885 | + $w = $this->w - $this->rMargin - $this->x; | |
| 886 | + } | |
| 848 | 887 | $wmax = ($w - 2 * $this->cMargin); | 
| 849 | 888 |      $s = str_replace("\r", '', $txt); | 
| 850 | 889 |      if ($this->unifontSubset) { | 
| 851 | 890 | $nb = mb_strlen($s, 'utf-8'); | 
| 852 | - while ($nb > 0 && mb_substr($s, $nb - 1, 1, 'utf-8') == "\n") $nb--; | |
| 853 | - } | |
| 854 | -    else { | |
| 855 | - $nb = strlen($s); | |
| 856 | - if ($nb > 0 && $s[$nb - 1] == "\n") | |
| 891 | +        while ($nb > 0 && mb_substr($s, $nb - 1, 1, 'utf-8') == "\n") { | |
| 857 | 892 | $nb--; | 
| 893 | + } | |
| 894 | +    } else { | |
| 895 | + $nb = strlen($s); | |
| 896 | +        if ($nb > 0 && $s[$nb - 1] == "\n") { | |
| 897 | + $nb--; | |
| 898 | + } | |
| 858 | 899 | } | 
| 859 | 900 | $b = 0; | 
| 860 | 901 | if ($border) | 
| @@ -864,14 +905,15 @@ discard block | ||
| 864 | 905 | $border = 'LTRB'; | 
| 865 | 906 | $b = 'LRT'; | 
| 866 | 907 | $b2 = 'LR'; | 
| 867 | - } | |
| 868 | - else | |
| 908 | + } else | |
| 869 | 909 |          { | 
| 870 | 910 | $b2 = ''; | 
| 871 | - if (strpos($border, 'L') !== false) | |
| 872 | - $b2 .= 'L'; | |
| 873 | - if (strpos($border, 'R') !== false) | |
| 874 | - $b2 .= 'R'; | |
| 911 | +            if (strpos($border, 'L') !== false) { | |
| 912 | + $b2 .= 'L'; | |
| 913 | + } | |
| 914 | +            if (strpos($border, 'R') !== false) { | |
| 915 | + $b2 .= 'R'; | |
| 916 | + } | |
| 875 | 917 | $b = (strpos($border, 'T') !== false) ? $b2.'T' : $b2; | 
| 876 | 918 | } | 
| 877 | 919 | } | 
| @@ -886,8 +928,7 @@ discard block | ||
| 886 | 928 | // Get next character | 
| 887 | 929 |          if ($this->unifontSubset) { | 
| 888 | 930 | $c = mb_substr($s, $i, 1, 'UTF-8'); | 
| 889 | - } | |
| 890 | -        else { | |
| 931 | +        } else { | |
| 891 | 932 | $c = $s[$i]; | 
| 892 | 933 | } | 
| 893 | 934 | if ($c == "\n") | 
| @@ -900,8 +941,7 @@ discard block | ||
| 900 | 941 | } | 
| 901 | 942 |              if ($this->unifontSubset) { | 
| 902 | 943 | $this->Cell($w, $h, mb_substr($s, $j, $i - $j, 'UTF-8'), $b, 2, $align, $fill); | 
| 903 | - } | |
| 904 | -            else { | |
| 944 | +            } else { | |
| 905 | 945 | $this->Cell($w, $h, substr($s, $j, $i - $j), $b, 2, $align, $fill); | 
| 906 | 946 | } | 
| 907 | 947 | $i++; | 
| @@ -910,8 +950,9 @@ discard block | ||
| 910 | 950 | $l = 0; | 
| 911 | 951 | $ns = 0; | 
| 912 | 952 | $nl++; | 
| 913 | - if ($border && $nl == 2) | |
| 914 | - $b = $b2; | |
| 953 | +            if ($border && $nl == 2) { | |
| 954 | + $b = $b2; | |
| 955 | + } | |
| 915 | 956 | continue; | 
| 916 | 957 | } | 
| 917 | 958 | if ($c == ' ') | 
| @@ -921,16 +962,16 @@ discard block | ||
| 921 | 962 | $ns++; | 
| 922 | 963 | } | 
| 923 | 964 | |
| 924 | -        if ($this->unifontSubset) { $l += $this->GetStringWidth($c); } | |
| 925 | -        else { $l += $cw[$c] * $this->FontSize / 1000; } | |
| 965 | +        if ($this->unifontSubset) { $l += $this->GetStringWidth($c); } else { $l += $cw[$c] * $this->FontSize / 1000; } | |
| 926 | 966 | |
| 927 | 967 | if ($l > $wmax) | 
| 928 | 968 |          { | 
| 929 | 969 | // Automatic line break | 
| 930 | 970 | if ($sep == -1) | 
| 931 | 971 |              { | 
| 932 | - if ($i == $j) | |
| 933 | - $i++; | |
| 972 | +                if ($i == $j) { | |
| 973 | + $i++; | |
| 974 | + } | |
| 934 | 975 | if ($this->ws > 0) | 
| 935 | 976 |                  { | 
| 936 | 977 | $this->ws = 0; | 
| @@ -938,12 +979,10 @@ discard block | ||
| 938 | 979 | } | 
| 939 | 980 |                  if ($this->unifontSubset) { | 
| 940 | 981 | $this->Cell($w, $h, mb_substr($s, $j, $i - $j, 'UTF-8'), $b, 2, $align, $fill); | 
| 941 | - } | |
| 942 | -                else { | |
| 982 | +                } else { | |
| 943 | 983 | $this->Cell($w, $h, substr($s, $j, $i - $j), $b, 2, $align, $fill); | 
| 944 | 984 | } | 
| 945 | - } | |
| 946 | - else | |
| 985 | + } else | |
| 947 | 986 |              { | 
| 948 | 987 | if ($align == 'J') | 
| 949 | 988 |                  { | 
| @@ -952,8 +991,7 @@ discard block | ||
| 952 | 991 | } | 
| 953 | 992 |                  if ($this->unifontSubset) { | 
| 954 | 993 | $this->Cell($w, $h, mb_substr($s, $j, $sep - $j, 'UTF-8'), $b, 2, $align, $fill); | 
| 955 | - } | |
| 956 | -                else { | |
| 994 | +                } else { | |
| 957 | 995 | $this->Cell($w, $h, substr($s, $j, $sep - $j), $b, 2, $align, $fill); | 
| 958 | 996 | } | 
| 959 | 997 | $i = $sep + 1; | 
| @@ -963,11 +1001,12 @@ discard block | ||
| 963 | 1001 | $l = 0; | 
| 964 | 1002 | $ns = 0; | 
| 965 | 1003 | $nl++; | 
| 966 | - if ($border && $nl == 2) | |
| 967 | - $b = $b2; | |
| 1004 | +            if ($border && $nl == 2) { | |
| 1005 | + $b = $b2; | |
| 1006 | + } | |
| 1007 | +        } else { | |
| 1008 | + $i++; | |
| 968 | 1009 | } | 
| 969 | - else | |
| 970 | - $i++; | |
| 971 | 1010 | } | 
| 972 | 1011 | // Last chunk | 
| 973 | 1012 | if ($this->ws > 0) | 
| @@ -975,12 +1014,12 @@ discard block | ||
| 975 | 1014 | $this->ws = 0; | 
| 976 | 1015 |          $this->_out('0 Tw'); | 
| 977 | 1016 | } | 
| 978 | - if ($border && strpos($border, 'B') !== false) | |
| 979 | - $b .= 'B'; | |
| 1017 | +    if ($border && strpos($border, 'B') !== false) { | |
| 1018 | + $b .= 'B'; | |
| 1019 | + } | |
| 980 | 1020 |      if ($this->unifontSubset) { | 
| 981 | 1021 | $this->Cell($w, $h, mb_substr($s, $j, $i - $j, 'UTF-8'), $b, 2, $align, $fill); | 
| 982 | - } | |
| 983 | -    else { | |
| 1022 | +    } else { | |
| 984 | 1023 | $this->Cell($w, $h, substr($s, $j, $i - $j), $b, 2, $align, $fill); | 
| 985 | 1024 | } | 
| 986 | 1025 | $this->x = $this->lMargin; | 
| @@ -1013,8 +1052,7 @@ discard block | ||
| 1013 | 1052 | // Get next character | 
| 1014 | 1053 |          if ($this->unifontSubset) { | 
| 1015 | 1054 | $c = mb_substr($s, $i, 1, 'UTF-8'); | 
| 1016 | - } | |
| 1017 | -        else { | |
| 1055 | +        } else { | |
| 1018 | 1056 | $c = $s[$i]; | 
| 1019 | 1057 | } | 
| 1020 | 1058 | if ($c == "\n") | 
| @@ -1022,8 +1060,7 @@ discard block | ||
| 1022 | 1060 | // Explicit line break | 
| 1023 | 1061 |              if ($this->unifontSubset) { | 
| 1024 | 1062 | $this->Cell($w, $h, mb_substr($s, $j, $i - $j, 'UTF-8'), 0, 2, '', 0, $link); | 
| 1025 | - } | |
| 1026 | -            else { | |
| 1063 | +            } else { | |
| 1027 | 1064 | $this->Cell($w, $h, substr($s, $j, $i - $j), 0, 2, '', 0, $link); | 
| 1028 | 1065 | } | 
| 1029 | 1066 | $i++; | 
| @@ -1039,11 +1076,11 @@ discard block | ||
| 1039 | 1076 | $nl++; | 
| 1040 | 1077 | continue; | 
| 1041 | 1078 | } | 
| 1042 | - if ($c == ' ') | |
| 1043 | - $sep = $i; | |
| 1079 | +        if ($c == ' ') { | |
| 1080 | + $sep = $i; | |
| 1081 | + } | |
| 1044 | 1082 | |
| 1045 | -        if ($this->unifontSubset) { $l += $this->GetStringWidth($c); } | |
| 1046 | -        else { $l += $cw[$c] * $this->FontSize / 1000; } | |
| 1083 | +        if ($this->unifontSubset) { $l += $this->GetStringWidth($c); } else { $l += $cw[$c] * $this->FontSize / 1000; } | |
| 1047 | 1084 | |
| 1048 | 1085 | if ($l > $wmax) | 
| 1049 | 1086 |          { | 
| @@ -1061,21 +1098,19 @@ discard block | ||
| 1061 | 1098 | $nl++; | 
| 1062 | 1099 | continue; | 
| 1063 | 1100 | } | 
| 1064 | - if ($i == $j) | |
| 1065 | - $i++; | |
| 1101 | +                if ($i == $j) { | |
| 1102 | + $i++; | |
| 1103 | + } | |
| 1066 | 1104 |                  if ($this->unifontSubset) { | 
| 1067 | 1105 | $this->Cell($w, $h, mb_substr($s, $j, $i - $j, 'UTF-8'), 0, 2, '', 0, $link); | 
| 1068 | - } | |
| 1069 | -                else { | |
| 1106 | +                } else { | |
| 1070 | 1107 | $this->Cell($w, $h, substr($s, $j, $i - $j), 0, 2, '', 0, $link); | 
| 1071 | 1108 | } | 
| 1072 | - } | |
| 1073 | - else | |
| 1109 | + } else | |
| 1074 | 1110 |              { | 
| 1075 | 1111 |                  if ($this->unifontSubset) { | 
| 1076 | 1112 | $this->Cell($w, $h, mb_substr($s, $j, $sep - $j, 'UTF-8'), 0, 2, '', 0, $link); | 
| 1077 | - } | |
| 1078 | -                else { | |
| 1113 | +                } else { | |
| 1079 | 1114 | $this->Cell($w, $h, substr($s, $j, $sep - $j), 0, 2, '', 0, $link); | 
| 1080 | 1115 | } | 
| 1081 | 1116 | $i = $sep + 1; | 
| @@ -1199,11 +1234,12 @@ discard block | ||
| 1199 | 1234 | function SetX($x) | 
| 1200 | 1235 |  { | 
| 1201 | 1236 | // Set x position | 
| 1202 | - if ($x >= 0) | |
| 1203 | - $this->x = $x; | |
| 1204 | - else | |
| 1205 | - $this->x = $this->w + $x; | |
| 1206 | -} | |
| 1237 | +    if ($x >= 0) { | |
| 1238 | + $this->x = $x; | |
| 1239 | +    } else { | |
| 1240 | + $this->x = $this->w + $x; | |
| 1241 | + } | |
| 1242 | + } | |
| 1207 | 1243 | |
| 1208 | 1244 | function GetY() | 
| 1209 | 1245 |  { | 
| @@ -1215,11 +1251,12 @@ discard block | ||
| 1215 | 1251 |  { | 
| 1216 | 1252 | // Set y position and reset x | 
| 1217 | 1253 | $this->x = $this->lMargin; | 
| 1218 | - if ($y >= 0) | |
| 1219 | - $this->y = $y; | |
| 1220 | - else | |
| 1221 | - $this->y = $this->h + $y; | |
| 1222 | -} | |
| 1254 | +    if ($y >= 0) { | |
| 1255 | + $this->y = $y; | |
| 1256 | +    } else { | |
| 1257 | + $this->y = $this->h + $y; | |
| 1258 | + } | |
| 1259 | + } | |
| 1223 | 1260 | |
| 1224 | 1261 | function SetXY($x, $y) | 
| 1225 | 1262 |  { | 
| @@ -1231,8 +1268,9 @@ discard block | ||
| 1231 | 1268 | function Output($name = '', $dest = '') | 
| 1232 | 1269 |  { | 
| 1233 | 1270 | // Output PDF to some destination | 
| 1234 | - if ($this->state < 3) | |
| 1235 | - $this->Close(); | |
| 1271 | +    if ($this->state < 3) { | |
| 1272 | + $this->Close(); | |
| 1273 | + } | |
| 1236 | 1274 | $dest = strtoupper($dest); | 
| 1237 | 1275 | if ($dest == '') | 
| 1238 | 1276 |      { | 
| @@ -1271,8 +1309,9 @@ discard block | ||
| 1271 | 1309 | case 'F': | 
| 1272 | 1310 | // Save to local file | 
| 1273 | 1311 | $f = fopen($name, 'wb'); | 
| 1274 | - if (!$f) | |
| 1275 | -                $this->Error('Unable to create output file: '.$name); | |
| 1312 | +            if (!$f) { | |
| 1313 | +                            $this->Error('Unable to create output file: '.$name); | |
| 1314 | + } | |
| 1276 | 1315 | fwrite($f, $this->buffer, strlen($this->buffer)); | 
| 1277 | 1316 | fclose($f); | 
| 1278 | 1317 | break; | 
| @@ -1293,18 +1332,22 @@ discard block | ||
| 1293 | 1332 | function _dochecks() | 
| 1294 | 1333 |  { | 
| 1295 | 1334 | // Check availability of %F | 
| 1296 | -    if (sprintf('%.1F', 1.0) != '1.0') | |
| 1297 | -        $this->Error('This version of PHP is not supported'); | |
| 1335 | +    if (sprintf('%.1F', 1.0) != '1.0') { | |
| 1336 | +            $this->Error('This version of PHP is not supported'); | |
| 1337 | + } | |
| 1298 | 1338 | // Check availability of mbstring | 
| 1299 | -    if (!function_exists('mb_strlen')) | |
| 1300 | -        $this->Error('mbstring extension is not available'); | |
| 1339 | +    if (!function_exists('mb_strlen')) { | |
| 1340 | +            $this->Error('mbstring extension is not available'); | |
| 1341 | + } | |
| 1301 | 1342 | // Check mbstring overloading | 
| 1302 | -    if (ini_get('mbstring.func_overload') & 2) | |
| 1303 | -        $this->Error('mbstring overloading must be disabled'); | |
| 1343 | +    if (ini_get('mbstring.func_overload') & 2) { | |
| 1344 | +            $this->Error('mbstring overloading must be disabled'); | |
| 1345 | + } | |
| 1304 | 1346 | // Ensure runtime magic quotes are disabled | 
| 1305 | - if (get_magic_quotes_runtime()) | |
| 1306 | - @set_magic_quotes_runtime(0); | |
| 1307 | -} | |
| 1347 | +    if (get_magic_quotes_runtime()) { | |
| 1348 | + @set_magic_quotes_runtime(0); | |
| 1349 | + } | |
| 1350 | + } | |
| 1308 | 1351 | |
| 1309 | 1352 | function _getfontpath() | 
| 1310 | 1353 |  { | 
| @@ -1315,8 +1358,9 @@ discard block | ||
| 1315 | 1358 |  { | 
| 1316 | 1359 | if (PHP_SAPI != 'cli') | 
| 1317 | 1360 |      { | 
| 1318 | - if (headers_sent($file, $line)) | |
| 1319 | -            $this->Error("Some data has already been output, can't send PDF file (output started at $file:$line)"); | |
| 1361 | +        if (headers_sent($file, $line)) { | |
| 1362 | +                    $this->Error("Some data has already been output, can't send PDF file (output started at $file:$line)"); | |
| 1363 | + } | |
| 1320 | 1364 | } | 
| 1321 | 1365 | if (ob_get_length()) | 
| 1322 | 1366 |      { | 
| @@ -1336,17 +1380,18 @@ discard block | ||
| 1336 | 1380 | if (is_string($size)) | 
| 1337 | 1381 |      { | 
| 1338 | 1382 | $size = strtolower($size); | 
| 1339 | - if (!isset($this->StdPageSizes[$size])) | |
| 1340 | -            $this->Error('Unknown page size: '.$size); | |
| 1383 | +        if (!isset($this->StdPageSizes[$size])) { | |
| 1384 | +                    $this->Error('Unknown page size: '.$size); | |
| 1385 | + } | |
| 1341 | 1386 | $a = $this->StdPageSizes[$size]; | 
| 1342 | 1387 | return array($a[0] / $this->k, $a[1] / $this->k); | 
| 1343 | - } | |
| 1344 | - else | |
| 1388 | + } else | |
| 1345 | 1389 |      { | 
| 1346 | - if ($size[0] > $size[1]) | |
| 1347 | - return array($size[1], $size[0]); | |
| 1348 | - else | |
| 1349 | - return $size; | |
| 1390 | +        if ($size[0] > $size[1]) { | |
| 1391 | + return array($size[1], $size[0]); | |
| 1392 | +        } else { | |
| 1393 | + return $size; | |
| 1394 | + } | |
| 1350 | 1395 | } | 
| 1351 | 1396 | } | 
| 1352 | 1397 | |
| @@ -1363,14 +1408,16 @@ discard block | ||
| 1363 | 1408 | $this->y = $this->tMargin; | 
| 1364 | 1409 | $this->FontFamily = ''; | 
| 1365 | 1410 | // Check page size and orientation | 
| 1366 | - if ($orientation == '') | |
| 1367 | - $orientation = $this->DefOrientation; | |
| 1368 | - else | |
| 1369 | - $orientation = strtoupper($orientation[0]); | |
| 1370 | - if ($size == '') | |
| 1371 | - $size = $this->DefPageSize; | |
| 1372 | - else | |
| 1373 | - $size = $this->_getpagesize($size); | |
| 1411 | +    if ($orientation == '') { | |
| 1412 | + $orientation = $this->DefOrientation; | |
| 1413 | +    } else { | |
| 1414 | + $orientation = strtoupper($orientation[0]); | |
| 1415 | + } | |
| 1416 | +    if ($size == '') { | |
| 1417 | + $size = $this->DefPageSize; | |
| 1418 | +    } else { | |
| 1419 | + $size = $this->_getpagesize($size); | |
| 1420 | + } | |
| 1374 | 1421 | if ($orientation != $this->CurOrientation || $size[0] != $this->CurPageSize[0] || $size[1] != $this->CurPageSize[1]) | 
| 1375 | 1422 |      { | 
| 1376 | 1423 | // New size or orientation | 
| @@ -1389,9 +1436,10 @@ discard block | ||
| 1389 | 1436 | $this->CurOrientation = $orientation; | 
| 1390 | 1437 | $this->CurPageSize = $size; | 
| 1391 | 1438 | } | 
| 1392 | - if ($orientation != $this->DefOrientation || $size[0] != $this->DefPageSize[0] || $size[1] != $this->DefPageSize[1]) | |
| 1393 | - $this->PageSizes[$this->page] = array($this->wPt, $this->hPt); | |
| 1394 | -} | |
| 1439 | +    if ($orientation != $this->DefOrientation || $size[0] != $this->DefPageSize[0] || $size[1] != $this->DefPageSize[1]) { | |
| 1440 | + $this->PageSizes[$this->page] = array($this->wPt, $this->hPt); | |
| 1441 | + } | |
| 1442 | + } | |
| 1395 | 1443 | |
| 1396 | 1444 | function _endpage() | 
| 1397 | 1445 |  { | 
| @@ -1406,8 +1454,9 @@ discard block | ||
| 1406 | 1454 | // Load a font definition file from the font directory | 
| 1407 | 1455 | include($this->fontpath.$font); | 
| 1408 | 1456 | $a = get_defined_vars(); | 
| 1409 | - if (!isset($a['name'])) | |
| 1410 | -        $this->Error('Could not include font definition file'); | |
| 1457 | +    if (!isset($a['name'])) { | |
| 1458 | +            $this->Error('Could not include font definition file'); | |
| 1459 | + } | |
| 1411 | 1460 | return $a; | 
| 1412 | 1461 | } | 
| 1413 | 1462 | |
| @@ -1443,15 +1492,13 @@ discard block | ||
| 1443 | 1492 | $c3 = ord($s[$i++]); | 
| 1444 | 1493 | $res .= chr((($c1 & 0x0F) << 4) + (($c2 & 0x3C) >> 2)); | 
| 1445 | 1494 | $res .= chr((($c2 & 0x03) << 6) + ($c3 & 0x3F)); | 
| 1446 | - } | |
| 1447 | - elseif ($c1 >= 192) | |
| 1495 | + } elseif ($c1 >= 192) | |
| 1448 | 1496 |          { | 
| 1449 | 1497 | // 2-byte character | 
| 1450 | 1498 | $c2 = ord($s[$i++]); | 
| 1451 | 1499 | $res .= chr(($c1 & 0x1C) >> 2); | 
| 1452 | 1500 | $res .= chr((($c1 & 0x03) << 6) + ($c2 & 0x3F)); | 
| 1453 | - } | |
| 1454 | - else | |
| 1501 | + } else | |
| 1455 | 1502 |          { | 
| 1456 | 1503 | // Single-byte character | 
| 1457 | 1504 | $res .= "\0".chr($c1); | 
| @@ -1473,16 +1520,19 @@ discard block | ||
| 1473 | 1520 |  { | 
| 1474 | 1521 | // Extract info from a JPEG file | 
| 1475 | 1522 | $a = getimagesize($file); | 
| 1476 | - if (!$a) | |
| 1477 | -        $this->Error('Missing or incorrect image file: '.$file); | |
| 1478 | - if ($a[2] != 2) | |
| 1479 | -        $this->Error('Not a JPEG file: '.$file); | |
| 1480 | - if (!isset($a['channels']) || $a['channels'] == 3) | |
| 1481 | - $colspace = 'DeviceRGB'; | |
| 1482 | - elseif ($a['channels'] == 4) | |
| 1483 | - $colspace = 'DeviceCMYK'; | |
| 1484 | - else | |
| 1485 | - $colspace = 'DeviceGray'; | |
| 1523 | +    if (!$a) { | |
| 1524 | +            $this->Error('Missing or incorrect image file: '.$file); | |
| 1525 | + } | |
| 1526 | +    if ($a[2] != 2) { | |
| 1527 | +            $this->Error('Not a JPEG file: '.$file); | |
| 1528 | + } | |
| 1529 | +    if (!isset($a['channels']) || $a['channels'] == 3) { | |
| 1530 | + $colspace = 'DeviceRGB'; | |
| 1531 | +    } elseif ($a['channels'] == 4) { | |
| 1532 | + $colspace = 'DeviceCMYK'; | |
| 1533 | +    } else { | |
| 1534 | + $colspace = 'DeviceGray'; | |
| 1535 | + } | |
| 1486 | 1536 | $bpc = isset($a['bits']) ? $a['bits'] : 8; | 
| 1487 | 1537 | $data = file_get_contents($file); | 
| 1488 | 1538 |      return array('w'=>$a[0], 'h'=>$a[1], 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'DCTDecode', 'data'=>$data); | 
| @@ -1495,8 +1545,9 @@ discard block | ||
| 1495 | 1545 |  { | 
| 1496 | 1546 | // Extract info from a PNG file | 
| 1497 | 1547 | $f = fopen($file, 'rb'); | 
| 1498 | - if (!$f) | |
| 1499 | -        $this->Error('Can\'t open image file: '.$file); | |
| 1548 | +    if (!$f) { | |
| 1549 | +            $this->Error('Can\'t open image file: '.$file); | |
| 1550 | + } | |
| 1500 | 1551 | $info = $this->_parsepngstream($f, $file); | 
| 1501 | 1552 | fclose($f); | 
| 1502 | 1553 | return $info; | 
| @@ -1505,33 +1556,40 @@ discard block | ||
| 1505 | 1556 | function _parsepngstream($f, $file) | 
| 1506 | 1557 |  { | 
| 1507 | 1558 | // Check signature | 
| 1508 | - if ($this->_readstream($f, 8) != chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) | |
| 1509 | -        $this->Error('Not a PNG file: '.$file); | |
| 1559 | +    if ($this->_readstream($f, 8) != chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) { | |
| 1560 | +            $this->Error('Not a PNG file: '.$file); | |
| 1561 | + } | |
| 1510 | 1562 | |
| 1511 | 1563 | // Read header chunk | 
| 1512 | 1564 | $this->_readstream($f, 4); | 
| 1513 | - if ($this->_readstream($f, 4) != 'IHDR') | |
| 1514 | -        $this->Error('Incorrect PNG file: '.$file); | |
| 1565 | +    if ($this->_readstream($f, 4) != 'IHDR') { | |
| 1566 | +            $this->Error('Incorrect PNG file: '.$file); | |
| 1567 | + } | |
| 1515 | 1568 | $w = $this->_readint($f); | 
| 1516 | 1569 | $h = $this->_readint($f); | 
| 1517 | 1570 | $bpc = ord($this->_readstream($f, 1)); | 
| 1518 | - if ($bpc > 8) | |
| 1519 | -        $this->Error('16-bit depth not supported: '.$file); | |
| 1571 | +    if ($bpc > 8) { | |
| 1572 | +            $this->Error('16-bit depth not supported: '.$file); | |
| 1573 | + } | |
| 1520 | 1574 | $ct = ord($this->_readstream($f, 1)); | 
| 1521 | - if ($ct == 0 || $ct == 4) | |
| 1522 | - $colspace = 'DeviceGray'; | |
| 1523 | - elseif ($ct == 2 || $ct == 6) | |
| 1524 | - $colspace = 'DeviceRGB'; | |
| 1525 | - elseif ($ct == 3) | |
| 1526 | - $colspace = 'Indexed'; | |
| 1527 | - else | |
| 1528 | -        $this->Error('Unknown color type: '.$file); | |
| 1529 | - if (ord($this->_readstream($f, 1)) != 0) | |
| 1530 | -        $this->Error('Unknown compression method: '.$file); | |
| 1531 | - if (ord($this->_readstream($f, 1)) != 0) | |
| 1532 | -        $this->Error('Unknown filter method: '.$file); | |
| 1533 | - if (ord($this->_readstream($f, 1)) != 0) | |
| 1534 | -        $this->Error('Interlacing not supported: '.$file); | |
| 1575 | +    if ($ct == 0 || $ct == 4) { | |
| 1576 | + $colspace = 'DeviceGray'; | |
| 1577 | +    } elseif ($ct == 2 || $ct == 6) { | |
| 1578 | + $colspace = 'DeviceRGB'; | |
| 1579 | +    } elseif ($ct == 3) { | |
| 1580 | + $colspace = 'Indexed'; | |
| 1581 | +    } else { | |
| 1582 | +            $this->Error('Unknown color type: '.$file); | |
| 1583 | + } | |
| 1584 | +    if (ord($this->_readstream($f, 1)) != 0) { | |
| 1585 | +            $this->Error('Unknown compression method: '.$file); | |
| 1586 | + } | |
| 1587 | +    if (ord($this->_readstream($f, 1)) != 0) { | |
| 1588 | +            $this->Error('Unknown filter method: '.$file); | |
| 1589 | + } | |
| 1590 | +    if (ord($this->_readstream($f, 1)) != 0) { | |
| 1591 | +            $this->Error('Interlacing not supported: '.$file); | |
| 1592 | + } | |
| 1535 | 1593 | $this->_readstream($f, 4); | 
| 1536 | 1594 | $dp = '/Predictor 15 /Colors '.($colspace == 'DeviceRGB' ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w; | 
| 1537 | 1595 | |
| @@ -1548,44 +1606,45 @@ discard block | ||
| 1548 | 1606 | // Read palette | 
| 1549 | 1607 | $pal = $this->_readstream($f, $n); | 
| 1550 | 1608 | $this->_readstream($f, 4); | 
| 1551 | - } | |
| 1552 | - elseif ($type == 'tRNS') | |
| 1609 | + } elseif ($type == 'tRNS') | |
| 1553 | 1610 |          { | 
| 1554 | 1611 | // Read transparency info | 
| 1555 | 1612 | $t = $this->_readstream($f, $n); | 
| 1556 | - if ($ct == 0) | |
| 1557 | - $trns = array(ord(substr($t, 1, 1))); | |
| 1558 | - elseif ($ct == 2) | |
| 1559 | - $trns = array(ord(substr($t, 1, 1)), ord(substr($t, 3, 1)), ord(substr($t, 5, 1))); | |
| 1560 | - else | |
| 1613 | +            if ($ct == 0) { | |
| 1614 | + $trns = array(ord(substr($t, 1, 1))); | |
| 1615 | +            } elseif ($ct == 2) { | |
| 1616 | + $trns = array(ord(substr($t, 1, 1)), ord(substr($t, 3, 1)), ord(substr($t, 5, 1))); | |
| 1617 | + } else | |
| 1561 | 1618 |              { | 
| 1562 | 1619 | $pos = strpos($t, chr(0)); | 
| 1563 | - if ($pos !== false) | |
| 1564 | - $trns = array($pos); | |
| 1620 | +                if ($pos !== false) { | |
| 1621 | + $trns = array($pos); | |
| 1622 | + } | |
| 1565 | 1623 | } | 
| 1566 | 1624 | $this->_readstream($f, 4); | 
| 1567 | - } | |
| 1568 | - elseif ($type == 'IDAT') | |
| 1625 | + } elseif ($type == 'IDAT') | |
| 1569 | 1626 |          { | 
| 1570 | 1627 | // Read image data block | 
| 1571 | 1628 | $data .= $this->_readstream($f, $n); | 
| 1572 | 1629 | $this->_readstream($f, 4); | 
| 1630 | +        } elseif ($type == 'IEND') { | |
| 1631 | + break; | |
| 1632 | +        } else { | |
| 1633 | + $this->_readstream($f, $n + 4); | |
| 1573 | 1634 | } | 
| 1574 | - elseif ($type == 'IEND') | |
| 1575 | - break; | |
| 1576 | - else | |
| 1577 | - $this->_readstream($f, $n + 4); | |
| 1578 | 1635 | } | 
| 1579 | 1636 | while ($n); | 
| 1580 | 1637 | |
| 1581 | - if ($colspace == 'Indexed' && empty($pal)) | |
| 1582 | -        $this->Error('Missing palette in '.$file); | |
| 1638 | +    if ($colspace == 'Indexed' && empty($pal)) { | |
| 1639 | +            $this->Error('Missing palette in '.$file); | |
| 1640 | + } | |
| 1583 | 1641 |      $info = array('w'=>$w, 'h'=>$h, 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'FlateDecode', 'dp'=>$dp, 'pal'=>$pal, 'trns'=>$trns); | 
| 1584 | 1642 | if ($ct >= 4) | 
| 1585 | 1643 |      { | 
| 1586 | 1644 | // Extract alpha channel | 
| 1587 | -        if (!function_exists('gzuncompress')) | |
| 1588 | -            $this->Error('Zlib not available, can\'t handle alpha channel: '.$file); | |
| 1645 | +        if (!function_exists('gzuncompress')) { | |
| 1646 | +                    $this->Error('Zlib not available, can\'t handle alpha channel: '.$file); | |
| 1647 | + } | |
| 1589 | 1648 | $data = gzuncompress($data); | 
| 1590 | 1649 | $color = ''; | 
| 1591 | 1650 | $alpha = ''; | 
| @@ -1619,8 +1678,9 @@ discard block | ||
| 1619 | 1678 | unset($data); | 
| 1620 | 1679 | $data = gzcompress($color); | 
| 1621 | 1680 | $info['smask'] = gzcompress($alpha); | 
| 1622 | - if ($this->PDFVersion < '1.4') | |
| 1623 | - $this->PDFVersion = '1.4'; | |
| 1681 | +        if ($this->PDFVersion < '1.4') { | |
| 1682 | + $this->PDFVersion = '1.4'; | |
| 1683 | + } | |
| 1624 | 1684 | } | 
| 1625 | 1685 | $info['data'] = $data; | 
| 1626 | 1686 | return $info; | 
| @@ -1633,13 +1693,15 @@ discard block | ||
| 1633 | 1693 | while ($n > 0 && !feof($f)) | 
| 1634 | 1694 |      { | 
| 1635 | 1695 | $s = fread($f, $n); | 
| 1636 | - if ($s === false) | |
| 1637 | -            $this->Error('Error while reading stream'); | |
| 1696 | +        if ($s === false) { | |
| 1697 | +                    $this->Error('Error while reading stream'); | |
| 1698 | + } | |
| 1638 | 1699 | $n -= strlen($s); | 
| 1639 | 1700 | $res .= $s; | 
| 1640 | 1701 | } | 
| 1641 | - if ($n > 0) | |
| 1642 | -        $this->Error('Unexpected end of stream'); | |
| 1702 | +    if ($n > 0) { | |
| 1703 | +            $this->Error('Unexpected end of stream'); | |
| 1704 | + } | |
| 1643 | 1705 | return $res; | 
| 1644 | 1706 | } | 
| 1645 | 1707 | |
| @@ -1653,13 +1715,16 @@ discard block | ||
| 1653 | 1715 | function _parsegif($file) | 
| 1654 | 1716 |  { | 
| 1655 | 1717 | // Extract info from a GIF file (via PNG conversion) | 
| 1656 | -    if (!function_exists('imagepng')) | |
| 1657 | -        $this->Error('GD extension is required for GIF support'); | |
| 1658 | -    if (!function_exists('imagecreatefromgif')) | |
| 1659 | -        $this->Error('GD has no GIF read support'); | |
| 1718 | +    if (!function_exists('imagepng')) { | |
| 1719 | +            $this->Error('GD extension is required for GIF support'); | |
| 1720 | + } | |
| 1721 | +    if (!function_exists('imagecreatefromgif')) { | |
| 1722 | +            $this->Error('GD has no GIF read support'); | |
| 1723 | + } | |
| 1660 | 1724 | $im = imagecreatefromgif($file); | 
| 1661 | - if (!$im) | |
| 1662 | -        $this->Error('Missing or incorrect image file: '.$file); | |
| 1725 | +    if (!$im) { | |
| 1726 | +            $this->Error('Missing or incorrect image file: '.$file); | |
| 1727 | + } | |
| 1663 | 1728 | imageinterlace($im, 0); | 
| 1664 | 1729 |      $f = @fopen('php://temp', 'rb+'); | 
| 1665 | 1730 | if ($f) | 
| @@ -1673,15 +1738,16 @@ discard block | ||
| 1673 | 1738 | rewind($f); | 
| 1674 | 1739 | $info = $this->_parsepngstream($f, $file); | 
| 1675 | 1740 | fclose($f); | 
| 1676 | - } | |
| 1677 | - else | |
| 1741 | + } else | |
| 1678 | 1742 |      { | 
| 1679 | 1743 | // Use temporary file | 
| 1680 | 1744 |          $tmp = tempnam('.', 'gif'); | 
| 1681 | - if (!$tmp) | |
| 1682 | -            $this->Error('Unable to create a temporary file'); | |
| 1683 | - if (!imagepng($im, $tmp)) | |
| 1684 | -            $this->Error('Error while saving to temporary file'); | |
| 1745 | +        if (!$tmp) { | |
| 1746 | +                    $this->Error('Unable to create a temporary file'); | |
| 1747 | + } | |
| 1748 | +        if (!imagepng($im, $tmp)) { | |
| 1749 | +                    $this->Error('Error while saving to temporary file'); | |
| 1750 | + } | |
| 1685 | 1751 | imagedestroy($im); | 
| 1686 | 1752 | $info = $this->_parsepng($tmp); | 
| 1687 | 1753 | unlink($tmp); | 
| @@ -1707,11 +1773,12 @@ discard block | ||
| 1707 | 1773 | function _out($s) | 
| 1708 | 1774 |  { | 
| 1709 | 1775 | // Add a line to the document | 
| 1710 | - if ($this->state == 2) | |
| 1711 | - $this->pages[$this->page] .= $s."\n"; | |
| 1712 | - else | |
| 1713 | - $this->buffer .= $s."\n"; | |
| 1714 | -} | |
| 1776 | +    if ($this->state == 2) { | |
| 1777 | + $this->pages[$this->page] .= $s."\n"; | |
| 1778 | +    } else { | |
| 1779 | + $this->buffer .= $s."\n"; | |
| 1780 | + } | |
| 1781 | + } | |
| 1715 | 1782 | |
| 1716 | 1783 | function _putpages() | 
| 1717 | 1784 |  { | 
| @@ -1721,18 +1788,19 @@ discard block | ||
| 1721 | 1788 | // Replace number of pages in fonts using subsets | 
| 1722 | 1789 | $alias = $this->UTF8ToUTF16BE($this->AliasNbPages, false); | 
| 1723 | 1790 |          $r = $this->UTF8ToUTF16BE("$nb", false); | 
| 1724 | - for ($n = 1; $n <= $nb; $n++) | |
| 1725 | - $this->pages[$n] = str_replace($alias, $r, $this->pages[$n]); | |
| 1791 | +        for ($n = 1; $n <= $nb; $n++) { | |
| 1792 | + $this->pages[$n] = str_replace($alias, $r, $this->pages[$n]); | |
| 1793 | + } | |
| 1726 | 1794 | // Now repeat for no pages in non-subset fonts | 
| 1727 | - for ($n = 1; $n <= $nb; $n++) | |
| 1728 | - $this->pages[$n] = str_replace($this->AliasNbPages, $nb, $this->pages[$n]); | |
| 1795 | +        for ($n = 1; $n <= $nb; $n++) { | |
| 1796 | + $this->pages[$n] = str_replace($this->AliasNbPages, $nb, $this->pages[$n]); | |
| 1797 | + } | |
| 1729 | 1798 | } | 
| 1730 | 1799 | if ($this->DefOrientation == 'P') | 
| 1731 | 1800 |      { | 
| 1732 | 1801 | $wPt = $this->DefPageSize[0] * $this->k; | 
| 1733 | 1802 | $hPt = $this->DefPageSize[1] * $this->k; | 
| 1734 | - } | |
| 1735 | - else | |
| 1803 | + } else | |
| 1736 | 1804 |      { | 
| 1737 | 1805 | $wPt = $this->DefPageSize[1] * $this->k; | 
| 1738 | 1806 | $hPt = $this->DefPageSize[0] * $this->k; | 
| @@ -1744,8 +1812,9 @@ discard block | ||
| 1744 | 1812 | $this->_newobj(); | 
| 1745 | 1813 |          $this->_out('<</Type /Page'); | 
| 1746 | 1814 |          $this->_out('/Parent 1 0 R'); | 
| 1747 | - if (isset($this->PageSizes[$n])) | |
| 1748 | -            $this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]', $this->PageSizes[$n][0], $this->PageSizes[$n][1])); | |
| 1815 | +        if (isset($this->PageSizes[$n])) { | |
| 1816 | +                    $this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]', $this->PageSizes[$n][0], $this->PageSizes[$n][1])); | |
| 1817 | + } | |
| 1749 | 1818 |          $this->_out('/Resources 2 0 R'); | 
| 1750 | 1819 | if (isset($this->PageLinks[$n])) | 
| 1751 | 1820 |          { | 
| @@ -1755,9 +1824,9 @@ discard block | ||
| 1755 | 1824 |              { | 
| 1756 | 1825 |                  $rect = sprintf('%.2F %.2F %.2F %.2F', $pl[0], $pl[1], $pl[0] + $pl[2], $pl[1] - $pl[3]); | 
| 1757 | 1826 | $annots .= '<</Type /Annot /Subtype /Link /Rect ['.$rect.'] /Border [0 0 0] '; | 
| 1758 | - if (is_string($pl[4])) | |
| 1759 | - $annots .= '/A <</S /URI /URI '.$this->_textstring($pl[4]).'>>>>'; | |
| 1760 | - else | |
| 1827 | +                if (is_string($pl[4])) { | |
| 1828 | + $annots .= '/A <</S /URI /URI '.$this->_textstring($pl[4]).'>>>>'; | |
| 1829 | + } else | |
| 1761 | 1830 |                  { | 
| 1762 | 1831 | $l = $this->links[$pl[4]]; | 
| 1763 | 1832 | $h = isset($this->PageSizes[$l[0]]) ? $this->PageSizes[$l[0]][1] : $hPt; | 
| @@ -1766,8 +1835,9 @@ discard block | ||
| 1766 | 1835 | } | 
| 1767 | 1836 | $this->_out($annots.']'); | 
| 1768 | 1837 | } | 
| 1769 | - if ($this->PDFVersion > '1.3') | |
| 1770 | -            $this->_out('/Group <</Type /Group /S /Transparency /CS /DeviceRGB>>'); | |
| 1838 | +        if ($this->PDFVersion > '1.3') { | |
| 1839 | +                    $this->_out('/Group <</Type /Group /S /Transparency /CS /DeviceRGB>>'); | |
| 1840 | + } | |
| 1771 | 1841 |          $this->_out('/Contents '.($this->n + 1).' 0 R>>'); | 
| 1772 | 1842 |          $this->_out('endobj'); | 
| 1773 | 1843 | // Page content | 
| @@ -1889,18 +1959,21 @@ discard block | ||
| 1889 | 1959 | $this->_newobj(); | 
| 1890 | 1960 | $cw = &$font['cw']; | 
| 1891 | 1961 | $s = '['; | 
| 1892 | - for ($i = 32; $i <= 255; $i++) | |
| 1893 | - $s .= $cw[chr($i)].' '; | |
| 1962 | +            for ($i = 32; $i <= 255; $i++) { | |
| 1963 | + $s .= $cw[chr($i)].' '; | |
| 1964 | + } | |
| 1894 | 1965 | $this->_out($s.']'); | 
| 1895 | 1966 |              $this->_out('endobj'); | 
| 1896 | 1967 | // Descriptor | 
| 1897 | 1968 | $this->_newobj(); | 
| 1898 | 1969 | $s = '<</Type /FontDescriptor /FontName /'.$name; | 
| 1899 | - foreach ($font['desc'] as $k=>$v) | |
| 1900 | - $s .= ' /'.$k.' '.$v; | |
| 1970 | +            foreach ($font['desc'] as $k=>$v) { | |
| 1971 | + $s .= ' /'.$k.' '.$v; | |
| 1972 | + } | |
| 1901 | 1973 | $file = $font['file']; | 
| 1902 | - if ($file) | |
| 1903 | - $s .= ' /FontFile'.($type == 'Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R'; | |
| 1974 | +            if ($file) { | |
| 1975 | + $s .= ' /FontFile'.($type == 'Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R'; | |
| 1976 | + } | |
| 1904 | 1977 | $this->_out($s.'>>'); | 
| 1905 | 1978 |              $this->_out('endobj'); | 
| 1906 | 1979 | } | 
| @@ -2019,14 +2092,14 @@ discard block | ||
| 2019 | 2092 | $this->_putstream($fontstream); | 
| 2020 | 2093 |              $this->_out('endobj'); | 
| 2021 | 2094 | unset($ttf); | 
| 2022 | - } | |
| 2023 | - else | |
| 2095 | + } else | |
| 2024 | 2096 |          { | 
| 2025 | 2097 | // Allow for additional types | 
| 2026 | 2098 | $this->fonts[$k]['n'] = $this->n + 1; | 
| 2027 | 2099 | $mtd = '_put'.strtolower($type); | 
| 2028 | - if (!method_exists($this, $mtd)) | |
| 2029 | -                $this->Error('Unsupported font type: '.$type); | |
| 2100 | +            if (!method_exists($this, $mtd)) { | |
| 2101 | +                            $this->Error('Unsupported font type: '.$type); | |
| 2102 | + } | |
| 2030 | 2103 | $this->$mtd($font); | 
| 2031 | 2104 | } | 
| 2032 | 2105 | } | 
| @@ -2058,8 +2131,7 @@ discard block | ||
| 2058 | 2131 | $cw127 .= '$rangeid='.$rangeid.";\n"; | 
| 2059 | 2132 | $cw127 .= '$prevcid='.$prevcid.";\n"; | 
| 2060 | 2133 | $cw127 .= '$prevwidth='.$prevwidth.";\n"; | 
| 2061 | -                if ($interval) { $cw127 .= '$interval=true'.";\n"; } | |
| 2062 | -                else { $cw127 .= '$interval=false'.";\n"; } | |
| 2134 | +                if ($interval) { $cw127 .= '$interval=true'.";\n"; } else { $cw127 .= '$interval=false'.";\n"; } | |
| 2063 | 2135 | $cw127 .= '$range='.var_export($range, true).";\n"; | 
| 2064 | 2136 | $cw127 .= "?>"; | 
| 2065 | 2137 | fwrite($fh, $cw127, strlen($cw127)); | 
| @@ -2113,21 +2185,17 @@ discard block | ||
| 2113 | 2185 |              if (isset($range[$k]['interval'])) { unset($range[$k]['interval']); } | 
| 2114 | 2186 | $range[$prevk] = array_merge($range[$prevk], $range[$k]); | 
| 2115 | 2187 | unset($range[$k]); | 
| 2116 | - } | |
| 2117 | -        else { $prevk = $k; } | |
| 2188 | +        } else { $prevk = $k; } | |
| 2118 | 2189 | $nextk = $k + $cws; | 
| 2119 | 2190 |          if (isset($ws['interval'])) { | 
| 2120 | -            if ($cws > 3) { $prevint = true; } | |
| 2121 | -            else { $prevint = false; } | |
| 2191 | +            if ($cws > 3) { $prevint = true; } else { $prevint = false; } | |
| 2122 | 2192 | unset($range[$k]['interval']); | 
| 2123 | 2193 | --$nextk; | 
| 2124 | - } | |
| 2125 | -        else { $prevint = false; } | |
| 2194 | +        } else { $prevint = false; } | |
| 2126 | 2195 | } | 
| 2127 | 2196 | $w = ''; | 
| 2128 | 2197 |      foreach ($range as $k => $ws) { | 
| 2129 | -        if (count(array_count_values($ws)) == 1) { $w .= ' '.$k.' '.($k + count($ws) - 1).' '.$ws[0]; } | |
| 2130 | -        else { $w .= ' '.$k.' [ '.implode(' ', $ws).' ]'."\n"; } | |
| 2198 | +        if (count(array_count_values($ws)) == 1) { $w .= ' '.$k.' '.($k + count($ws) - 1).' '.$ws[0]; } else { $w .= ' '.$k.' [ '.implode(' ', $ws).' ]'."\n"; } | |
| 2131 | 2199 | } | 
| 2132 | 2200 |      $this->_out('/W ['.$w.' ]'); | 
| 2133 | 2201 | } | 
| @@ -2150,28 +2218,33 @@ discard block | ||
| 2150 | 2218 |      $this->_out('/Subtype /Image'); | 
| 2151 | 2219 |      $this->_out('/Width '.$info['w']); | 
| 2152 | 2220 |      $this->_out('/Height '.$info['h']); | 
| 2153 | - if ($info['cs'] == 'Indexed') | |
| 2154 | -        $this->_out('/ColorSpace [/Indexed /DeviceRGB '.(strlen($info['pal']) / 3 - 1).' '.($this->n + 1).' 0 R]'); | |
| 2155 | - else | |
| 2221 | +    if ($info['cs'] == 'Indexed') { | |
| 2222 | +            $this->_out('/ColorSpace [/Indexed /DeviceRGB '.(strlen($info['pal']) / 3 - 1).' '.($this->n + 1).' 0 R]'); | |
| 2223 | + } else | |
| 2156 | 2224 |      { | 
| 2157 | 2225 |          $this->_out('/ColorSpace /'.$info['cs']); | 
| 2158 | - if ($info['cs'] == 'DeviceCMYK') | |
| 2159 | -            $this->_out('/Decode [1 0 1 0 1 0 1 0]'); | |
| 2226 | +        if ($info['cs'] == 'DeviceCMYK') { | |
| 2227 | +                    $this->_out('/Decode [1 0 1 0 1 0 1 0]'); | |
| 2228 | + } | |
| 2160 | 2229 | } | 
| 2161 | 2230 |      $this->_out('/BitsPerComponent '.$info['bpc']); | 
| 2162 | - if (isset($info['f'])) | |
| 2163 | -        $this->_out('/Filter /'.$info['f']); | |
| 2164 | - if (isset($info['dp'])) | |
| 2165 | -        $this->_out('/DecodeParms <<'.$info['dp'].'>>'); | |
| 2231 | +    if (isset($info['f'])) { | |
| 2232 | +            $this->_out('/Filter /'.$info['f']); | |
| 2233 | + } | |
| 2234 | +    if (isset($info['dp'])) { | |
| 2235 | +            $this->_out('/DecodeParms <<'.$info['dp'].'>>'); | |
| 2236 | + } | |
| 2166 | 2237 | if (isset($info['trns']) && is_array($info['trns'])) | 
| 2167 | 2238 |      { | 
| 2168 | 2239 | $trns = ''; | 
| 2169 | - for ($i = 0; $i < count($info['trns']); $i++) | |
| 2170 | - $trns .= $info['trns'][$i].' '.$info['trns'][$i].' '; | |
| 2240 | +        for ($i = 0; $i < count($info['trns']); $i++) { | |
| 2241 | + $trns .= $info['trns'][$i].' '.$info['trns'][$i].' '; | |
| 2242 | + } | |
| 2171 | 2243 |          $this->_out('/Mask ['.$trns.']'); | 
| 2172 | 2244 | } | 
| 2173 | - if (isset($info['smask'])) | |
| 2174 | -        $this->_out('/SMask '.($this->n + 1).' 0 R'); | |
| 2245 | +    if (isset($info['smask'])) { | |
| 2246 | +            $this->_out('/SMask '.($this->n + 1).' 0 R'); | |
| 2247 | + } | |
| 2175 | 2248 |      $this->_out('/Length '.strlen($info['data']).'>>'); | 
| 2176 | 2249 | $this->_putstream($info['data']); | 
| 2177 | 2250 |      $this->_out('endobj'); | 
| @@ -2196,9 +2269,10 @@ discard block | ||
| 2196 | 2269 | |
| 2197 | 2270 | function _putxobjectdict() | 
| 2198 | 2271 |  { | 
| 2199 | - foreach ($this->images as $image) | |
| 2200 | -        $this->_out('/I'.$image['i'].' '.$image['n'].' 0 R'); | |
| 2201 | -} | |
| 2272 | +    foreach ($this->images as $image) { | |
| 2273 | +            $this->_out('/I'.$image['i'].' '.$image['n'].' 0 R'); | |
| 2274 | + } | |
| 2275 | + } | |
| 2202 | 2276 | |
| 2203 | 2277 | function _putresourcedict() | 
| 2204 | 2278 |  { | 
| @@ -2229,16 +2303,21 @@ discard block | ||
| 2229 | 2303 | function _putinfo() | 
| 2230 | 2304 |  { | 
| 2231 | 2305 |      $this->_out('/Producer '.$this->_textstring('tFPDF '.tFPDF_VERSION)); | 
| 2232 | - if (!empty($this->title)) | |
| 2233 | -        $this->_out('/Title '.$this->_textstring($this->title)); | |
| 2234 | - if (!empty($this->subject)) | |
| 2235 | -        $this->_out('/Subject '.$this->_textstring($this->subject)); | |
| 2236 | - if (!empty($this->author)) | |
| 2237 | -        $this->_out('/Author '.$this->_textstring($this->author)); | |
| 2238 | - if (!empty($this->keywords)) | |
| 2239 | -        $this->_out('/Keywords '.$this->_textstring($this->keywords)); | |
| 2240 | - if (!empty($this->creator)) | |
| 2241 | -        $this->_out('/Creator '.$this->_textstring($this->creator)); | |
| 2306 | +    if (!empty($this->title)) { | |
| 2307 | +            $this->_out('/Title '.$this->_textstring($this->title)); | |
| 2308 | + } | |
| 2309 | +    if (!empty($this->subject)) { | |
| 2310 | +            $this->_out('/Subject '.$this->_textstring($this->subject)); | |
| 2311 | + } | |
| 2312 | +    if (!empty($this->author)) { | |
| 2313 | +            $this->_out('/Author '.$this->_textstring($this->author)); | |
| 2314 | + } | |
| 2315 | +    if (!empty($this->keywords)) { | |
| 2316 | +            $this->_out('/Keywords '.$this->_textstring($this->keywords)); | |
| 2317 | + } | |
| 2318 | +    if (!empty($this->creator)) { | |
| 2319 | +            $this->_out('/Creator '.$this->_textstring($this->creator)); | |
| 2320 | + } | |
| 2242 | 2321 |      $this->_out('/CreationDate '.$this->_textstring('D:'.@date('YmdHis'))); | 
| 2243 | 2322 | } | 
| 2244 | 2323 | |
| @@ -2246,21 +2325,23 @@ discard block | ||
| 2246 | 2325 |  { | 
| 2247 | 2326 |      $this->_out('/Type /Catalog'); | 
| 2248 | 2327 |      $this->_out('/Pages 1 0 R'); | 
| 2249 | - if ($this->ZoomMode == 'fullpage') | |
| 2250 | -        $this->_out('/OpenAction [3 0 R /Fit]'); | |
| 2251 | - elseif ($this->ZoomMode == 'fullwidth') | |
| 2252 | -        $this->_out('/OpenAction [3 0 R /FitH null]'); | |
| 2253 | - elseif ($this->ZoomMode == 'real') | |
| 2254 | -        $this->_out('/OpenAction [3 0 R /XYZ null null 1]'); | |
| 2255 | - elseif (!is_string($this->ZoomMode)) | |
| 2256 | -        $this->_out('/OpenAction [3 0 R /XYZ null null '.sprintf('%.2F', $this->ZoomMode / 100).']'); | |
| 2257 | - if ($this->LayoutMode == 'single') | |
| 2258 | -        $this->_out('/PageLayout /SinglePage'); | |
| 2259 | - elseif ($this->LayoutMode == 'continuous') | |
| 2260 | -        $this->_out('/PageLayout /OneColumn'); | |
| 2261 | - elseif ($this->LayoutMode == 'two') | |
| 2262 | -        $this->_out('/PageLayout /TwoColumnLeft'); | |
| 2263 | -} | |
| 2328 | +    if ($this->ZoomMode == 'fullpage') { | |
| 2329 | +            $this->_out('/OpenAction [3 0 R /Fit]'); | |
| 2330 | +    } elseif ($this->ZoomMode == 'fullwidth') { | |
| 2331 | +            $this->_out('/OpenAction [3 0 R /FitH null]'); | |
| 2332 | +    } elseif ($this->ZoomMode == 'real') { | |
| 2333 | +            $this->_out('/OpenAction [3 0 R /XYZ null null 1]'); | |
| 2334 | +    } elseif (!is_string($this->ZoomMode)) { | |
| 2335 | +            $this->_out('/OpenAction [3 0 R /XYZ null null '.sprintf('%.2F', $this->ZoomMode / 100).']'); | |
| 2336 | + } | |
| 2337 | +    if ($this->LayoutMode == 'single') { | |
| 2338 | +            $this->_out('/PageLayout /SinglePage'); | |
| 2339 | +    } elseif ($this->LayoutMode == 'continuous') { | |
| 2340 | +            $this->_out('/PageLayout /OneColumn'); | |
| 2341 | +    } elseif ($this->LayoutMode == 'two') { | |
| 2342 | +            $this->_out('/PageLayout /TwoColumnLeft'); | |
| 2343 | + } | |
| 2344 | + } | |
| 2264 | 2345 | |
| 2265 | 2346 | function _putheader() | 
| 2266 | 2347 |  { | 
| @@ -2296,8 +2377,9 @@ discard block | ||
| 2296 | 2377 |      $this->_out('xref'); | 
| 2297 | 2378 |      $this->_out('0 '.($this->n + 1)); | 
| 2298 | 2379 |      $this->_out('0000000000 65535 f '); | 
| 2299 | - for ($i = 1; $i <= $this->n; $i++) | |
| 2300 | -        $this->_out(sprintf('%010d 00000 n ', $this->offsets[$i])); | |
| 2380 | +    for ($i = 1; $i <= $this->n; $i++) { | |
| 2381 | +            $this->_out(sprintf('%010d 00000 n ', $this->offsets[$i])); | |
| 2382 | + } | |
| 2301 | 2383 | // Trailer | 
| 2302 | 2384 |      $this->_out('trailer'); | 
| 2303 | 2385 |      $this->_out('<<'); | 
| @@ -2327,18 +2409,19 @@ discard block | ||
| 2327 | 2409 |      for ($i = 0; $i < $len; $i++) { | 
| 2328 | 2410 | $uni = -1; | 
| 2329 | 2411 | $h = ord($str[$i]); | 
| 2330 | - if ( $h <= 0x7F ) | |
| 2331 | - $uni = $h; | |
| 2332 | -        elseif ( $h >= 0xC2 ) { | |
| 2333 | - if ( ($h <= 0xDF) && ($i < $len -1) ) | |
| 2334 | - $uni = ($h & 0x1F) << 6 | (ord($str[++$i]) & 0x3F); | |
| 2335 | - elseif ( ($h <= 0xEF) && ($i < $len -2) ) | |
| 2336 | - $uni = ($h & 0x0F) << 12 | (ord($str[++$i]) & 0x3F) << 6 | |
| 2412 | +        if ( $h <= 0x7F ) { | |
| 2413 | + $uni = $h; | |
| 2414 | +        } elseif ( $h >= 0xC2 ) { | |
| 2415 | +            if ( ($h <= 0xDF) && ($i < $len -1) ) { | |
| 2416 | + $uni = ($h & 0x1F) << 6 | (ord($str[++$i]) & 0x3F); | |
| 2417 | +            } elseif ( ($h <= 0xEF) && ($i < $len -2) ) { | |
| 2418 | + $uni = ($h & 0x0F) << 12 | (ord($str[++$i]) & 0x3F) << 6 | |
| 2337 | 2419 | | (ord($str[++$i]) & 0x3F); | 
| 2338 | - elseif ( ($h <= 0xF4) && ($i < $len -3) ) | |
| 2339 | - $uni = ($h & 0x0F) << 18 | (ord($str[++$i]) & 0x3F) << 12 | |
| 2420 | +            } elseif ( ($h <= 0xF4) && ($i < $len -3) ) { | |
| 2421 | + $uni = ($h & 0x0F) << 18 | (ord($str[++$i]) & 0x3F) << 12 | |
| 2340 | 2422 | | (ord($str[++$i]) & 0x3F) << 6 | 
| 2341 | 2423 | | (ord($str[++$i]) & 0x3F); | 
| 2424 | + } | |
| 2342 | 2425 | } | 
| 2343 | 2426 |      if ($uni >= 0) { | 
| 2344 | 2427 | $out[] = $uni; | 
| @@ -340,7 +340,7 @@ | ||
| 340 | 340 | * to the self::$_e[] table. | 
| 341 | 341 | * This is only used in the F() function | 
| 342 | 342 | * | 
| 343 | - * @param array $r 32 bit binary, each bit in an array element | |
| 343 | + * @param string $r 32 bit binary, each bit in an array element | |
| 344 | 344 | * @return string 48 bit binary string | 
| 345 | 345 | */ | 
| 346 | 346 | private function e($r) | 
| @@ -38,556 +38,556 @@ | ||
| 38 | 38 | */ | 
| 39 | 39 | class Cipher_DES extends Cipher | 
| 40 | 40 |  { | 
| 41 | - /** @type integer BYTES_BLOCK The block size, in bytes */ | |
| 42 | - const BYTES_BLOCK = 8; // 64 bits | |
| 41 | + /** @type integer BYTES_BLOCK The block size, in bytes */ | |
| 42 | + const BYTES_BLOCK = 8; // 64 bits | |
| 43 | 43 | |
| 44 | - /** @type integer BYTES_KEY The key size, in bytes */ | |
| 45 | - const BYTES_KEY = 8; // 64 bits | |
| 44 | + /** @type integer BYTES_KEY The key size, in bytes */ | |
| 45 | + const BYTES_KEY = 8; // 64 bits | |
| 46 | 46 | |
| 47 | - /** @type array $sub_keys The permutated subkeys */ | |
| 48 | - protected $sub_keys = array(); | |
| 47 | + /** @type array $sub_keys The permutated subkeys */ | |
| 48 | + protected $sub_keys = array(); | |
| 49 | 49 | |
| 50 | - /* | |
| 50 | + /* | |
| 51 | 51 | * Tables initialized in the initTables() | 
| 52 | 52 | */ | 
| 53 | 53 | |
| 54 | - /** | |
| 55 | - * @type array $_pc1 Permutated choice 1 (PC1), | |
| 56 | - * This should be considered a constant | |
| 57 | - */ | |
| 58 | - protected static $_pc1 = array(); | |
| 59 | - | |
| 60 | - /** | |
| 61 | - * @type array $_pc2 Permutated choice 2 (PC2), | |
| 62 | - * This should be considered a constant | |
| 63 | - */ | |
| 64 | - protected static $_pc2 = array(); | |
| 65 | - | |
| 66 | - /** | |
| 67 | - * @type array $_key_sched The key schedule, | |
| 68 | - * This should be considered a constant | |
| 69 | - */ | |
| 70 | - protected static $_key_sched = array(); | |
| 71 | - | |
| 72 | - /** | |
| 73 | - * @type array $_ip The Initial Permutation (IP), | |
| 74 | - * This should be considered a constant | |
| 75 | - */ | |
| 76 | - private static $_ip = array(); | |
| 77 | - | |
| 78 | - /** | |
| 79 | - * @type array $_e The Expansion table (E), | |
| 80 | - * This should be considered a constant | |
| 81 | - */ | |
| 82 | - private static $_e = array(); | |
| 83 | - | |
| 84 | - /** | |
| 85 | - * @type array $_s The Substitution Box (S), | |
| 86 | - * This should be considered a constant | |
| 87 | - */ | |
| 88 | - private static $_s = array(); | |
| 89 | - | |
| 90 | - /** | |
| 91 | - * @type array $_p The Permutation table (P), | |
| 92 | - * This should be considered a constant | |
| 93 | - */ | |
| 94 | - private static $_p = array(); | |
| 95 | - | |
| 96 | - /** | |
| 97 | - * @type array $_ip The The Final Permutation table (FP), | |
| 98 | - * This should be considered a constant | |
| 99 | - */ | |
| 100 | - private static $_fp = array(); | |
| 101 | - | |
| 102 | - | |
| 103 | - /** | |
| 104 | - * Constructor, used only when calling this class directly | |
| 105 | - * for classes that extend this class, call __construct1() | |
| 106 | - * | |
| 107 | - * @param string $key The key used for Encryption/Decryption | |
| 108 | - * @return void | |
| 109 | - */ | |
| 110 | - public function __construct($key) | |
| 111 | -	{ | |
| 112 | - // set the DES key | |
| 113 | - parent::__construct(PHP_Crypt::CIPHER_DES, $key, self::BYTES_KEY); | |
| 114 | - | |
| 115 | - // initialize variables | |
| 116 | - $this->initTables(); | |
| 117 | - | |
| 118 | - // DES requires that data is 64 bits | |
| 119 | - $this->blockSize(self::BYTES_BLOCK); | |
| 120 | - | |
| 121 | - // create the 16 rounds of 56 bit keys | |
| 122 | - $this->keyPermutation(); | |
| 123 | - } | |
| 124 | - | |
| 125 | - | |
| 126 | - /** | |
| 127 | - * Second Constructor, used only by child classes that extend this class | |
| 128 | - * | |
| 129 | - * @param string $cipher The name of the cipher extending this class | |
| 130 | - * @param string $key The key used for Encryption/Decryption | |
| 131 | - * @param integer $key_byte_sz The required byte size of the extending cipher | |
| 132 | - * @return void | |
| 133 | - */ | |
| 134 | - protected function __construct1($cipher, $key, $key_byte_sz) | |
| 135 | -	{ | |
| 136 | - // set the key and key size | |
| 137 | - parent::__construct($cipher, $key, $key_byte_sz); | |
| 138 | - | |
| 139 | - // initialize variables | |
| 140 | - $this->initTables(); | |
| 141 | - } | |
| 142 | - | |
| 143 | - | |
| 144 | - /** | |
| 145 | - * Destructor | |
| 146 | - * | |
| 147 | - * @return void | |
| 148 | - */ | |
| 149 | - public function __destruct() | |
| 150 | -	{ | |
| 151 | - parent::__destruct(); | |
| 152 | - } | |
| 153 | - | |
| 154 | - | |
| 155 | - /** | |
| 156 | - * Encrypt plain text data using DES | |
| 157 | - * | |
| 158 | - * @return boolean Returns true | |
| 159 | - */ | |
| 160 | - public function encrypt(&$text) | |
| 161 | -	{ | |
| 162 | - $this->operation(parent::ENCRYPT); | |
| 163 | - return $this->des($text); | |
| 164 | - } | |
| 165 | - | |
| 166 | - | |
| 167 | - /** | |
| 168 | - * Decrypt a DES encrypted string | |
| 169 | - * | |
| 170 | - * @return boolean Returns true | |
| 171 | - */ | |
| 172 | - public function decrypt(&$text) | |
| 173 | -	{ | |
| 174 | - $this->operation(parent::DECRYPT); | |
| 175 | - return $this->des($text); | |
| 176 | - } | |
| 177 | - | |
| 178 | - | |
| 179 | - /** | |
| 180 | - * This is where the actual encrypt/decryption takes place. Since | |
| 181 | - * encryption and decryption are the same algorithm in DES, we only | |
| 182 | - * need one function to do both. | |
| 183 | - * | |
| 184 | - * @param string $data The string to be encrypted or decrypted | |
| 185 | - * @return boolean Returns true | |
| 186 | - */ | |
| 187 | - protected function des(&$data) | |
| 188 | -	{ | |
| 189 | - $l = array(); | |
| 190 | - $r = array(); | |
| 191 | - | |
| 192 | - // step two: Initial Permutation (IP) of plaintext | |
| 193 | - $data = $this->ip($data); | |
| 194 | - | |
| 195 | - // divide the permuted block IP into a left half L0 of 32 bits, | |
| 196 | - // and a right half R0 of 32 bits | |
| 197 | - $l[0] = substr($data, 0, 32); | |
| 198 | - $r[0] = substr($data, 32, 32); | |
| 199 | - | |
| 200 | - for ($n = 1; $n <= 16; ++$n) | |
| 201 | -		{ | |
| 202 | - $l[$n] = $r[$n - 1]; | |
| 203 | - | |
| 204 | - if ($this->operation() == parent::DECRYPT) | |
| 205 | - $f = $this->F($r[$n - 1], $this->sub_keys[16 - $n]); | |
| 206 | - else | |
| 207 | - $f = $this->F($r[$n - 1], $this->sub_keys[$n - 1]); | |
| 208 | - | |
| 209 | - // XOR F with Ln | |
| 210 | - $r[$n] = $this->xorBin($l[$n - 1], $f); | |
| 211 | - } | |
| 212 | - | |
| 213 | - // now we combine L[16] and R[16] back into a 64-bit string, but we reverse | |
| 214 | - // L[16] and R[16] so that it becomes R[16]L[16] | |
| 215 | - $data = $r[16].$l[16]; | |
| 216 | - | |
| 217 | - // now do the final permutation | |
| 218 | - $data = $this->fp($data); | |
| 219 | - $data = parent::bin2Str($data); | |
| 220 | - | |
| 221 | - return true; | |
| 222 | - } | |
| 223 | - | |
| 224 | - | |
| 225 | - /** | |
| 226 | - * The Key permutation, based on tables $_pc1 and $_pc2 | |
| 227 | - * Create 16 subkeys, each of which is 48-bits long. | |
| 228 | - * | |
| 229 | - * @return void | |
| 230 | - */ | |
| 231 | - private function keyPermutation() | |
| 232 | -	{ | |
| 233 | - $this->sub_keys = array(); | |
| 234 | - $pc1m = array(); | |
| 235 | - $pcr = array(); | |
| 236 | - $c = array(); | |
| 237 | - $d = array(); | |
| 238 | - | |
| 239 | - // convert the key to binary | |
| 240 | - $binkey = parent::str2Bin($this->key()); | |
| 241 | - | |
| 242 | - // reduce the key down to 56bits based on table $_pc1 | |
| 243 | - for ($i = 0; $i < 56; ++$i) | |
| 54 | + /** | |
| 55 | + * @type array $_pc1 Permutated choice 1 (PC1), | |
| 56 | + * This should be considered a constant | |
| 57 | + */ | |
| 58 | + protected static $_pc1 = array(); | |
| 59 | + | |
| 60 | + /** | |
| 61 | + * @type array $_pc2 Permutated choice 2 (PC2), | |
| 62 | + * This should be considered a constant | |
| 63 | + */ | |
| 64 | + protected static $_pc2 = array(); | |
| 65 | + | |
| 66 | + /** | |
| 67 | + * @type array $_key_sched The key schedule, | |
| 68 | + * This should be considered a constant | |
| 69 | + */ | |
| 70 | + protected static $_key_sched = array(); | |
| 71 | + | |
| 72 | + /** | |
| 73 | + * @type array $_ip The Initial Permutation (IP), | |
| 74 | + * This should be considered a constant | |
| 75 | + */ | |
| 76 | + private static $_ip = array(); | |
| 77 | + | |
| 78 | + /** | |
| 79 | + * @type array $_e The Expansion table (E), | |
| 80 | + * This should be considered a constant | |
| 81 | + */ | |
| 82 | + private static $_e = array(); | |
| 83 | + | |
| 84 | + /** | |
| 85 | + * @type array $_s The Substitution Box (S), | |
| 86 | + * This should be considered a constant | |
| 87 | + */ | |
| 88 | + private static $_s = array(); | |
| 89 | + | |
| 90 | + /** | |
| 91 | + * @type array $_p The Permutation table (P), | |
| 92 | + * This should be considered a constant | |
| 93 | + */ | |
| 94 | + private static $_p = array(); | |
| 95 | + | |
| 96 | + /** | |
| 97 | + * @type array $_ip The The Final Permutation table (FP), | |
| 98 | + * This should be considered a constant | |
| 99 | + */ | |
| 100 | + private static $_fp = array(); | |
| 101 | + | |
| 102 | + | |
| 103 | + /** | |
| 104 | + * Constructor, used only when calling this class directly | |
| 105 | + * for classes that extend this class, call __construct1() | |
| 106 | + * | |
| 107 | + * @param string $key The key used for Encryption/Decryption | |
| 108 | + * @return void | |
| 109 | + */ | |
| 110 | + public function __construct($key) | |
| 111 | +    { | |
| 112 | + // set the DES key | |
| 113 | + parent::__construct(PHP_Crypt::CIPHER_DES, $key, self::BYTES_KEY); | |
| 114 | + | |
| 115 | + // initialize variables | |
| 116 | + $this->initTables(); | |
| 117 | + | |
| 118 | + // DES requires that data is 64 bits | |
| 119 | + $this->blockSize(self::BYTES_BLOCK); | |
| 120 | + | |
| 121 | + // create the 16 rounds of 56 bit keys | |
| 122 | + $this->keyPermutation(); | |
| 123 | + } | |
| 124 | + | |
| 125 | + | |
| 126 | + /** | |
| 127 | + * Second Constructor, used only by child classes that extend this class | |
| 128 | + * | |
| 129 | + * @param string $cipher The name of the cipher extending this class | |
| 130 | + * @param string $key The key used for Encryption/Decryption | |
| 131 | + * @param integer $key_byte_sz The required byte size of the extending cipher | |
| 132 | + * @return void | |
| 133 | + */ | |
| 134 | + protected function __construct1($cipher, $key, $key_byte_sz) | |
| 135 | +    { | |
| 136 | + // set the key and key size | |
| 137 | + parent::__construct($cipher, $key, $key_byte_sz); | |
| 138 | + | |
| 139 | + // initialize variables | |
| 140 | + $this->initTables(); | |
| 141 | + } | |
| 142 | + | |
| 143 | + | |
| 144 | + /** | |
| 145 | + * Destructor | |
| 146 | + * | |
| 147 | + * @return void | |
| 148 | + */ | |
| 149 | + public function __destruct() | |
| 150 | +    { | |
| 151 | + parent::__destruct(); | |
| 152 | + } | |
| 153 | + | |
| 154 | + | |
| 155 | + /** | |
| 156 | + * Encrypt plain text data using DES | |
| 157 | + * | |
| 158 | + * @return boolean Returns true | |
| 159 | + */ | |
| 160 | + public function encrypt(&$text) | |
| 161 | +    { | |
| 162 | + $this->operation(parent::ENCRYPT); | |
| 163 | + return $this->des($text); | |
| 164 | + } | |
| 165 | + | |
| 166 | + | |
| 167 | + /** | |
| 168 | + * Decrypt a DES encrypted string | |
| 169 | + * | |
| 170 | + * @return boolean Returns true | |
| 171 | + */ | |
| 172 | + public function decrypt(&$text) | |
| 173 | +    { | |
| 174 | + $this->operation(parent::DECRYPT); | |
| 175 | + return $this->des($text); | |
| 176 | + } | |
| 177 | + | |
| 178 | + | |
| 179 | + /** | |
| 180 | + * This is where the actual encrypt/decryption takes place. Since | |
| 181 | + * encryption and decryption are the same algorithm in DES, we only | |
| 182 | + * need one function to do both. | |
| 183 | + * | |
| 184 | + * @param string $data The string to be encrypted or decrypted | |
| 185 | + * @return boolean Returns true | |
| 186 | + */ | |
| 187 | + protected function des(&$data) | |
| 188 | +    { | |
| 189 | + $l = array(); | |
| 190 | + $r = array(); | |
| 191 | + | |
| 192 | + // step two: Initial Permutation (IP) of plaintext | |
| 193 | + $data = $this->ip($data); | |
| 194 | + | |
| 195 | + // divide the permuted block IP into a left half L0 of 32 bits, | |
| 196 | + // and a right half R0 of 32 bits | |
| 197 | + $l[0] = substr($data, 0, 32); | |
| 198 | + $r[0] = substr($data, 32, 32); | |
| 199 | + | |
| 200 | + for ($n = 1; $n <= 16; ++$n) | |
| 201 | +        { | |
| 202 | + $l[$n] = $r[$n - 1]; | |
| 203 | + | |
| 204 | + if ($this->operation() == parent::DECRYPT) | |
| 205 | + $f = $this->F($r[$n - 1], $this->sub_keys[16 - $n]); | |
| 206 | + else | |
| 207 | + $f = $this->F($r[$n - 1], $this->sub_keys[$n - 1]); | |
| 208 | + | |
| 209 | + // XOR F with Ln | |
| 210 | + $r[$n] = $this->xorBin($l[$n - 1], $f); | |
| 211 | + } | |
| 212 | + | |
| 213 | + // now we combine L[16] and R[16] back into a 64-bit string, but we reverse | |
| 214 | + // L[16] and R[16] so that it becomes R[16]L[16] | |
| 215 | + $data = $r[16].$l[16]; | |
| 216 | + | |
| 217 | + // now do the final permutation | |
| 218 | + $data = $this->fp($data); | |
| 219 | + $data = parent::bin2Str($data); | |
| 220 | + | |
| 221 | + return true; | |
| 222 | + } | |
| 223 | + | |
| 224 | + | |
| 225 | + /** | |
| 226 | + * The Key permutation, based on tables $_pc1 and $_pc2 | |
| 227 | + * Create 16 subkeys, each of which is 48-bits long. | |
| 228 | + * | |
| 229 | + * @return void | |
| 230 | + */ | |
| 231 | + private function keyPermutation() | |
| 232 | +    { | |
| 233 | + $this->sub_keys = array(); | |
| 234 | + $pc1m = array(); | |
| 235 | + $pcr = array(); | |
| 236 | + $c = array(); | |
| 237 | + $d = array(); | |
| 238 | + | |
| 239 | + // convert the key to binary | |
| 240 | + $binkey = parent::str2Bin($this->key()); | |
| 241 | + | |
| 242 | + // reduce the key down to 56bits based on table $_pc1 | |
| 243 | + for ($i = 0; $i < 56; ++$i) | |
| 244 | 244 | $pc1m[$i] = $binkey[self::$_pc1[$i] - 1]; | 
| 245 | 245 | |
| 246 | - // split $pc1m in half (C0 and D0) | |
| 247 | - $c[0] = array_slice($pc1m, 0, 28); | |
| 248 | - $d[0] = array_slice($pc1m, 28, 28); | |
| 249 | - | |
| 250 | - // now that $c[0] and $d[0] are defined, create 16 blocks for Cn and Dn | |
| 251 | - // where 1 <= n <= 16 | |
| 252 | - for ($i = 1; $i <= 16; ++$i) | |
| 253 | -		{ | |
| 254 | - // now set the next Cn and Dn as the previous Cn and Dn | |
| 255 | - $c[$i] = $c[$i - 1]; | |
| 256 | - $d[$i] = $d[$i - 1]; | |
| 257 | - | |
| 258 | - for ($j = 0; $j < self::$_key_sched[$i - 1]; ++$j) | |
| 259 | -			{ | |
| 260 | - // do a left shift, move each bit one place to the left, | |
| 261 | - // except for the first bit, which is cycled to the end | |
| 262 | - // of the block. | |
| 263 | - $c[$i][] = array_shift($c[$i]); | |
| 264 | - $d[$i][] = array_shift($d[$i]); | |
| 265 | - } | |
| 266 | - | |
| 267 | - // We now form the sub_keys (Kn), for 1<=n<=16, by applying the | |
| 268 | - // following permutation table to each of the concatenated | |
| 269 | - // pairs CnDn. Each pair has 56 bits, but PC-2 only uses 48 | |
| 270 | - // of these. | |
| 271 | - $CnDn = array_merge($c[$i], $d[$i]); | |
| 272 | - $this->sub_keys[$i - 1] = ""; | |
| 273 | - for ($j = 0; $j < 48; ++$j) | |
| 274 | - $this->sub_keys[$i - 1] .= $CnDn[self::$_pc2[$j] - 1]; | |
| 275 | - } | |
| 276 | - | |
| 277 | - // the sub_keys are created, we are done with the key permutation | |
| 278 | - } | |
| 279 | - | |
| 280 | - | |
| 281 | - /** | |
| 282 | - * Initial Permutation (IP) | |
| 283 | - * Now we encode each 64-bit block of data. There is an initial permutation IP of | |
| 284 | - * the 64 bits of the message data M. This rearranges the bits according to the | |
| 285 | - * following table, where the entries in the table show the new arrangement of the | |
| 286 | - * bits from their initial order. The 58th bit of M becomes the first bit of IP. | |
| 287 | - * The 50th bit of M becomes the second bit of IP. The 7th bit of M is the last | |
| 288 | - * bit of IP. | |
| 289 | - * | |
| 290 | - * According to the book Applied Cryptography (Bruce Schneier, 2nd edition, pg. 271): | |
| 291 | - * The initial permution was used to make it easier to load plain text and cipher text | |
| 292 | - * data into a DES chip in byte-sized pieces when doing DES in hardware. The IP and FP | |
| 293 | - * are not necessary in software implementations and do not affect the security. However, | |
| 294 | - * the IP and FP are part of the DES standard and not implementing it would deviate from | |
| 295 | - * the standard, so we will do it here in phpCrypt. | |
| 296 | - * | |
| 297 | - * @param string $text | |
| 298 | - * @return string the Initial Permutation (IP) | |
| 299 | - */ | |
| 300 | - private function ip($text) | |
| 301 | -	{ | |
| 302 | - $text = parent::str2Bin($text); | |
| 303 | - $ip = ""; | |
| 304 | - | |
| 305 | - // loop through the 64 bit block, ordering it occording to $_ip | |
| 306 | - for ($i = 0; $i < 64; ++$i) | |
| 307 | - $ip .= $text[self::$_ip[$i] - 1]; | |
| 308 | - | |
| 309 | - return $ip; | |
| 310 | - } | |
| 311 | - | |
| 312 | - | |
| 313 | - /** | |
| 314 | - * Function F - To calculate f, we first expand each block Rn-1 from 32 bits to 48 bits. | |
| 315 | - * This is done by using a selection table that repeats some of the bits in Rn-1. We'll | |
| 316 | - * call the use of this selection table the function E. Thus E(Rn-1) has a 32 bit input | |
| 317 | - * block, and a 48 bit output block. | |
| 318 | - * | |
| 319 | - * @param string $r 32 bit binary, each bit in an array element | |
| 320 | - * @param string $k 48 bit binary string | |
| 321 | - * @return string 48 bit binary string | |
| 322 | - */ | |
| 323 | - private function f($r, $k) | |
| 324 | -	{ | |
| 325 | - $bin = parent::xorBin($k, $this->E($r)); | |
| 326 | - | |
| 327 | - // create a 32-bit string from $bits by passing it through the S-Boxes | |
| 328 | - $bin = $this->s($bin); | |
| 329 | - | |
| 330 | - // now send permute $bin as defined by table self::$_p | |
| 331 | - $bin = $this->p($bin); | |
| 332 | - | |
| 333 | - return $bin; | |
| 334 | - } | |
| 335 | - | |
| 336 | - | |
| 337 | - /** | |
| 338 | - * Function E - Let E be such that the 48 bits of its output, written as 8 blocks of | |
| 339 | - * 6 bits each, are obtained by selecting the bits in its inputs in order according | |
| 340 | - * to the self::$_e[] table. | |
| 341 | - * This is only used in the F() function | |
| 342 | - * | |
| 343 | - * @param array $r 32 bit binary, each bit in an array element | |
| 344 | - * @return string 48 bit binary string | |
| 345 | - */ | |
| 346 | - private function e($r) | |
| 347 | -	{ | |
| 348 | - $e = ""; | |
| 349 | - for ($i = 0; $i < 48; ++$i) | |
| 350 | - $e .= $r[self::$_e[$i] - 1]; | |
| 351 | - | |
| 352 | - return $e; | |
| 353 | - } | |
| 354 | - | |
| 355 | - | |
| 356 | - /** | |
| 357 | - * S-Box | |
| 358 | - * Take a 48-bit string from F() and run it through the S-Boxes, this requires | |
| 359 | - * us to break up the 48-bit string into 8 groups of 6 bits before sending it | |
| 360 | - * through the S-Boxes | |
| 361 | - * | |
| 362 | - * @param string $bits The 48-bit string from F() to be processed | |
| 363 | - * @return string A 32-bit string from created from the 48-bit string after passing through S-Boxes | |
| 364 | - */ | |
| 365 | - private function s($bits) | |
| 366 | -	{ | |
| 367 | - $s = ""; | |
| 368 | - | |
| 369 | - for ($i = 0; $i <= 42; $i += 6) | |
| 370 | -		{ | |
| 371 | - $sbits = substr($bits, $i, 6); | |
| 372 | - | |
| 373 | - // we need to determine the S-Box column number and row number | |
| 374 | - // from the 6 bit string passed in, this is done using the following method: | |
| 375 | - // The First & Last bits represent a number between 0-3, used to determine which row | |
| 376 | - // The middle 4 bits represent a number between 0-15, used to determine the column | |
| 377 | -			$row = bindec("{$sbits[0]}{$sbits[5]}"); | |
| 378 | -			$col = bindec("{$sbits[1]}{$sbits[2]}{$sbits[3]}{$sbits[4]}"); | |
| 379 | - | |
| 380 | - // determine the position in the S-BOX, S-Box table is in self::$_s[] | |
| 381 | - $pos = ($row * 16) + $col; | |
| 382 | - | |
| 383 | - // get the integer from the S-Box and convert it to binary | |
| 384 | - $bin = decbin(self::$_s[($i / 6)][$pos]); | |
| 385 | - $s .= str_pad($bin, 4, "0", STR_PAD_LEFT); | |
| 386 | - } | |
| 387 | - | |
| 388 | - return $s; | |
| 389 | - } | |
| 390 | - | |
| 391 | - | |
| 392 | - /** | |
| 393 | - * Permutation P | |
| 394 | - * The permutation P is defined in self::$_p. P() returns a 32-bit output | |
| 395 | - * from a 32-bit input from a binary string from the S-BOX by permuting | |
| 396 | - * the bits of the input block. | |
| 397 | - * This is only used inside of F() function | |
| 398 | - * | |
| 399 | - * @param string $s A 32-bit string originating from being passed through S-Box | |
| 400 | - * @return string A 32-bit string, which is $s permuted through table self::$_p | |
| 401 | - */ | |
| 402 | - private function p($s) | |
| 403 | -	{ | |
| 404 | - $p = ""; | |
| 405 | - for ($i = 0; $i < 32; ++$i) | |
| 406 | - $p .= $s[self::$_p[$i] - 1]; | |
| 407 | - | |
| 408 | - return $p; | |
| 409 | - } | |
| 410 | - | |
| 411 | - | |
| 412 | - /** | |
| 413 | - * Final Permutation (FP) | |
| 414 | - * Read the comment about IP and FP being unecessary in software implmented DES (though | |
| 415 | - * we will do it to follow the DES standard). | |
| 416 | - * | |
| 417 | - * @param string $bin A 64-bit binary string | |
| 418 | - * @return string A 64-bit binary string that has been run through self::$_fp[] table | |
| 419 | - */ | |
| 420 | - private function fp($bin) | |
| 421 | -	{ | |
| 422 | - $fp = ""; | |
| 423 | - for ($i = 0; $i < 64; ++$i) | |
| 424 | - $fp .= $bin[self::$_fp[$i] - 1]; | |
| 425 | - | |
| 426 | - return $fp; | |
| 427 | - } | |
| 428 | - | |
| 429 | - | |
| 430 | - /** | |
| 431 | - * Initialize all the tables, this function is called inside the constructor | |
| 432 | - * | |
| 433 | - * @return void | |
| 434 | - */ | |
| 435 | - private function initTables() | |
| 436 | -	{ | |
| 437 | - // permuted choice 1 (PC1) | |
| 438 | - // these values are chars and should be run through chr() when used | |
| 439 | - self::$_pc1 = array( | |
| 440 | - 57, 49, 41, 33, 25, 17, 9, | |
| 441 | - 1, 58, 50, 42, 34, 26, 18, | |
| 442 | - 10, 2, 59, 51, 43, 35, 27, | |
| 443 | - 19, 11, 3, 60, 52, 44, 36, | |
| 444 | - 63, 55, 47, 39, 31, 23, 15, | |
| 445 | - 7, 62, 54, 46, 38, 30, 22, | |
| 446 | - 14, 6, 61, 53, 45, 37, 29, | |
| 447 | - 21, 13, 5, 28, 20, 12, 4 | |
| 448 | - ); | |
| 449 | - | |
| 450 | - // permuted choice 2 (PC2) | |
| 451 | - // these values are chars and should be run through chr() when used | |
| 452 | - self::$_pc2 = array( | |
| 453 | - 14, 17, 11, 24, 1, 5, | |
| 454 | - 3, 28, 15, 6, 21, 10, | |
| 455 | - 23, 19, 12, 4, 26, 8, | |
| 456 | - 16, 7, 27, 20, 13, 2, | |
| 457 | - 41, 52, 31, 37, 47, 55, | |
| 458 | - 30, 40, 51, 45, 33, 48, | |
| 459 | - 44, 49, 39, 56, 34, 53, | |
| 460 | - 46, 42, 50, 36, 29, 32 | |
| 461 | - ); | |
| 462 | - | |
| 463 | - // initial permutation (IP) | |
| 464 | - self::$_ip = array( | |
| 465 | - 58, 50, 42, 34, 26, 18, 10, 2, | |
| 466 | - 60, 52, 44, 36, 28, 20, 12, 4, | |
| 467 | - 62, 54, 46, 38, 30, 22, 14, 6, | |
| 468 | - 64, 56, 48, 40, 32, 24, 16, 8, | |
| 469 | - 57, 49, 41, 33, 25, 17, 9, 1, | |
| 470 | - 59, 51, 43, 35, 27, 19, 11, 3, | |
| 471 | - 61, 53, 45, 37, 29, 21, 13, 5, | |
| 472 | - 63, 55, 47, 39, 31, 23, 15, 7 | |
| 473 | - ); | |
| 474 | - | |
| 475 | - // expansion (E) | |
| 476 | - self::$_e = array( | |
| 477 | - 32, 1, 2, 3, 4, 5, | |
| 478 | - 4, 5, 6, 7, 8, 9, | |
| 479 | - 8, 9, 10, 11, 12, 13, | |
| 480 | - 12, 13, 14, 15, 16, 17, | |
| 481 | - 16, 17, 18, 19, 20, 21, | |
| 482 | - 20, 21, 22, 23, 24, 25, | |
| 483 | - 24, 25, 26, 27, 28, 29, | |
| 484 | - 28, 29, 30, 31, 32, 1 | |
| 485 | - ); | |
| 486 | - | |
| 487 | - // substition box (S) | |
| 488 | - self::$_s = array( | |
| 489 | - /* S1 */ | |
| 490 | - array( | |
| 491 | - 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, | |
| 492 | - 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, | |
| 493 | - 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, | |
| 494 | - 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 | |
| 495 | - ), | |
| 496 | - | |
| 497 | - /* S2 */ | |
| 498 | - array( | |
| 499 | - 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, | |
| 500 | - 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, | |
| 501 | - 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, | |
| 502 | - 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 | |
| 503 | - ), | |
| 504 | - | |
| 505 | - /* S3 */ | |
| 506 | - array( | |
| 507 | - 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, | |
| 508 | - 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, | |
| 509 | - 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, | |
| 510 | - 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 | |
| 511 | - ), | |
| 512 | - | |
| 513 | - /* S4 */ | |
| 514 | - array( | |
| 515 | - 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, | |
| 516 | - 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, | |
| 517 | - 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, | |
| 518 | - 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 | |
| 519 | - ), | |
| 520 | - | |
| 521 | - /* S5 */ | |
| 522 | - array( | |
| 523 | - 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, | |
| 524 | - 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, | |
| 525 | - 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, | |
| 526 | - 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 | |
| 527 | - ), | |
| 528 | - | |
| 529 | - /* S6 */ | |
| 530 | - array( | |
| 531 | - 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, | |
| 532 | - 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, | |
| 533 | - 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, | |
| 534 | - 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 | |
| 535 | - ), | |
| 536 | - | |
| 537 | - /* S7 */ | |
| 538 | - array( | |
| 539 | - 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, | |
| 540 | - 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, | |
| 541 | - 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, | |
| 542 | - 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 | |
| 543 | - ), | |
| 544 | - | |
| 545 | - /* S8 */ | |
| 546 | - array( | |
| 547 | - 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, | |
| 548 | - 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, | |
| 549 | - 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, | |
| 550 | - 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 | |
| 551 | - ) | |
| 552 | - ); | |
| 553 | - | |
| 554 | - // permutation (P) | |
| 555 | - self::$_p = array( | |
| 556 | - 16, 7, 20, 21, | |
| 557 | - 29, 12, 28, 17, | |
| 558 | - 1, 15, 23, 26, | |
| 559 | - 5, 18, 31, 10, | |
| 560 | - 2, 8, 24, 14, | |
| 561 | - 32, 27, 3, 9, | |
| 562 | - 19, 13, 30, 6, | |
| 563 | - 22, 11, 4, 25 | |
| 564 | - ); | |
| 565 | - | |
| 566 | - // final permutation (FP) | |
| 567 | - self::$_fp = array( | |
| 568 | - 40, 8, 48, 16, 56, 24, 64, 32, | |
| 569 | - 39, 7, 47, 15, 55, 23, 63, 31, | |
| 570 | - 38, 6, 46, 14, 54, 22, 62, 30, | |
| 571 | - 37, 5, 45, 13, 53, 21, 61, 29, | |
| 572 | - 36, 4, 44, 12, 52, 20, 60, 28, | |
| 573 | - 35, 3, 43, 11, 51, 19, 59, 27, | |
| 574 | - 34, 2, 42, 10, 50, 18, 58, 26, | |
| 575 | - 33, 1, 41, 9, 49, 17, 57, 25 | |
| 576 | - ); | |
| 577 | - | |
| 578 | - // key schedule used in KeyPermutation() | |
| 579 | - self::$_key_sched = array(1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1); | |
| 580 | - } | |
| 581 | - | |
| 582 | - | |
| 583 | - /** | |
| 584 | - * Indicates this is a block cipher | |
| 585 | - * | |
| 586 | - * @return integer Returns Cipher::BLOCK | |
| 587 | - */ | |
| 588 | - public function type() | |
| 589 | -	{ | |
| 590 | - return parent::BLOCK; | |
| 591 | - } | |
| 246 | + // split $pc1m in half (C0 and D0) | |
| 247 | + $c[0] = array_slice($pc1m, 0, 28); | |
| 248 | + $d[0] = array_slice($pc1m, 28, 28); | |
| 249 | + | |
| 250 | + // now that $c[0] and $d[0] are defined, create 16 blocks for Cn and Dn | |
| 251 | + // where 1 <= n <= 16 | |
| 252 | + for ($i = 1; $i <= 16; ++$i) | |
| 253 | +        { | |
| 254 | + // now set the next Cn and Dn as the previous Cn and Dn | |
| 255 | + $c[$i] = $c[$i - 1]; | |
| 256 | + $d[$i] = $d[$i - 1]; | |
| 257 | + | |
| 258 | + for ($j = 0; $j < self::$_key_sched[$i - 1]; ++$j) | |
| 259 | +            { | |
| 260 | + // do a left shift, move each bit one place to the left, | |
| 261 | + // except for the first bit, which is cycled to the end | |
| 262 | + // of the block. | |
| 263 | + $c[$i][] = array_shift($c[$i]); | |
| 264 | + $d[$i][] = array_shift($d[$i]); | |
| 265 | + } | |
| 266 | + | |
| 267 | + // We now form the sub_keys (Kn), for 1<=n<=16, by applying the | |
| 268 | + // following permutation table to each of the concatenated | |
| 269 | + // pairs CnDn. Each pair has 56 bits, but PC-2 only uses 48 | |
| 270 | + // of these. | |
| 271 | + $CnDn = array_merge($c[$i], $d[$i]); | |
| 272 | + $this->sub_keys[$i - 1] = ""; | |
| 273 | + for ($j = 0; $j < 48; ++$j) | |
| 274 | + $this->sub_keys[$i - 1] .= $CnDn[self::$_pc2[$j] - 1]; | |
| 275 | + } | |
| 276 | + | |
| 277 | + // the sub_keys are created, we are done with the key permutation | |
| 278 | + } | |
| 279 | + | |
| 280 | + | |
| 281 | + /** | |
| 282 | + * Initial Permutation (IP) | |
| 283 | + * Now we encode each 64-bit block of data. There is an initial permutation IP of | |
| 284 | + * the 64 bits of the message data M. This rearranges the bits according to the | |
| 285 | + * following table, where the entries in the table show the new arrangement of the | |
| 286 | + * bits from their initial order. The 58th bit of M becomes the first bit of IP. | |
| 287 | + * The 50th bit of M becomes the second bit of IP. The 7th bit of M is the last | |
| 288 | + * bit of IP. | |
| 289 | + * | |
| 290 | + * According to the book Applied Cryptography (Bruce Schneier, 2nd edition, pg. 271): | |
| 291 | + * The initial permution was used to make it easier to load plain text and cipher text | |
| 292 | + * data into a DES chip in byte-sized pieces when doing DES in hardware. The IP and FP | |
| 293 | + * are not necessary in software implementations and do not affect the security. However, | |
| 294 | + * the IP and FP are part of the DES standard and not implementing it would deviate from | |
| 295 | + * the standard, so we will do it here in phpCrypt. | |
| 296 | + * | |
| 297 | + * @param string $text | |
| 298 | + * @return string the Initial Permutation (IP) | |
| 299 | + */ | |
| 300 | + private function ip($text) | |
| 301 | +    { | |
| 302 | + $text = parent::str2Bin($text); | |
| 303 | + $ip = ""; | |
| 304 | + | |
| 305 | + // loop through the 64 bit block, ordering it occording to $_ip | |
| 306 | + for ($i = 0; $i < 64; ++$i) | |
| 307 | + $ip .= $text[self::$_ip[$i] - 1]; | |
| 308 | + | |
| 309 | + return $ip; | |
| 310 | + } | |
| 311 | + | |
| 312 | + | |
| 313 | + /** | |
| 314 | + * Function F - To calculate f, we first expand each block Rn-1 from 32 bits to 48 bits. | |
| 315 | + * This is done by using a selection table that repeats some of the bits in Rn-1. We'll | |
| 316 | + * call the use of this selection table the function E. Thus E(Rn-1) has a 32 bit input | |
| 317 | + * block, and a 48 bit output block. | |
| 318 | + * | |
| 319 | + * @param string $r 32 bit binary, each bit in an array element | |
| 320 | + * @param string $k 48 bit binary string | |
| 321 | + * @return string 48 bit binary string | |
| 322 | + */ | |
| 323 | + private function f($r, $k) | |
| 324 | +    { | |
| 325 | + $bin = parent::xorBin($k, $this->E($r)); | |
| 326 | + | |
| 327 | + // create a 32-bit string from $bits by passing it through the S-Boxes | |
| 328 | + $bin = $this->s($bin); | |
| 329 | + | |
| 330 | + // now send permute $bin as defined by table self::$_p | |
| 331 | + $bin = $this->p($bin); | |
| 332 | + | |
| 333 | + return $bin; | |
| 334 | + } | |
| 335 | + | |
| 336 | + | |
| 337 | + /** | |
| 338 | + * Function E - Let E be such that the 48 bits of its output, written as 8 blocks of | |
| 339 | + * 6 bits each, are obtained by selecting the bits in its inputs in order according | |
| 340 | + * to the self::$_e[] table. | |
| 341 | + * This is only used in the F() function | |
| 342 | + * | |
| 343 | + * @param array $r 32 bit binary, each bit in an array element | |
| 344 | + * @return string 48 bit binary string | |
| 345 | + */ | |
| 346 | + private function e($r) | |
| 347 | +    { | |
| 348 | + $e = ""; | |
| 349 | + for ($i = 0; $i < 48; ++$i) | |
| 350 | + $e .= $r[self::$_e[$i] - 1]; | |
| 351 | + | |
| 352 | + return $e; | |
| 353 | + } | |
| 354 | + | |
| 355 | + | |
| 356 | + /** | |
| 357 | + * S-Box | |
| 358 | + * Take a 48-bit string from F() and run it through the S-Boxes, this requires | |
| 359 | + * us to break up the 48-bit string into 8 groups of 6 bits before sending it | |
| 360 | + * through the S-Boxes | |
| 361 | + * | |
| 362 | + * @param string $bits The 48-bit string from F() to be processed | |
| 363 | + * @return string A 32-bit string from created from the 48-bit string after passing through S-Boxes | |
| 364 | + */ | |
| 365 | + private function s($bits) | |
| 366 | +    { | |
| 367 | + $s = ""; | |
| 368 | + | |
| 369 | + for ($i = 0; $i <= 42; $i += 6) | |
| 370 | +        { | |
| 371 | + $sbits = substr($bits, $i, 6); | |
| 372 | + | |
| 373 | + // we need to determine the S-Box column number and row number | |
| 374 | + // from the 6 bit string passed in, this is done using the following method: | |
| 375 | + // The First & Last bits represent a number between 0-3, used to determine which row | |
| 376 | + // The middle 4 bits represent a number between 0-15, used to determine the column | |
| 377 | +            $row = bindec("{$sbits[0]}{$sbits[5]}"); | |
| 378 | +            $col = bindec("{$sbits[1]}{$sbits[2]}{$sbits[3]}{$sbits[4]}"); | |
| 379 | + | |
| 380 | + // determine the position in the S-BOX, S-Box table is in self::$_s[] | |
| 381 | + $pos = ($row * 16) + $col; | |
| 382 | + | |
| 383 | + // get the integer from the S-Box and convert it to binary | |
| 384 | + $bin = decbin(self::$_s[($i / 6)][$pos]); | |
| 385 | + $s .= str_pad($bin, 4, "0", STR_PAD_LEFT); | |
| 386 | + } | |
| 387 | + | |
| 388 | + return $s; | |
| 389 | + } | |
| 390 | + | |
| 391 | + | |
| 392 | + /** | |
| 393 | + * Permutation P | |
| 394 | + * The permutation P is defined in self::$_p. P() returns a 32-bit output | |
| 395 | + * from a 32-bit input from a binary string from the S-BOX by permuting | |
| 396 | + * the bits of the input block. | |
| 397 | + * This is only used inside of F() function | |
| 398 | + * | |
| 399 | + * @param string $s A 32-bit string originating from being passed through S-Box | |
| 400 | + * @return string A 32-bit string, which is $s permuted through table self::$_p | |
| 401 | + */ | |
| 402 | + private function p($s) | |
| 403 | +    { | |
| 404 | + $p = ""; | |
| 405 | + for ($i = 0; $i < 32; ++$i) | |
| 406 | + $p .= $s[self::$_p[$i] - 1]; | |
| 407 | + | |
| 408 | + return $p; | |
| 409 | + } | |
| 410 | + | |
| 411 | + | |
| 412 | + /** | |
| 413 | + * Final Permutation (FP) | |
| 414 | + * Read the comment about IP and FP being unecessary in software implmented DES (though | |
| 415 | + * we will do it to follow the DES standard). | |
| 416 | + * | |
| 417 | + * @param string $bin A 64-bit binary string | |
| 418 | + * @return string A 64-bit binary string that has been run through self::$_fp[] table | |
| 419 | + */ | |
| 420 | + private function fp($bin) | |
| 421 | +    { | |
| 422 | + $fp = ""; | |
| 423 | + for ($i = 0; $i < 64; ++$i) | |
| 424 | + $fp .= $bin[self::$_fp[$i] - 1]; | |
| 425 | + | |
| 426 | + return $fp; | |
| 427 | + } | |
| 428 | + | |
| 429 | + | |
| 430 | + /** | |
| 431 | + * Initialize all the tables, this function is called inside the constructor | |
| 432 | + * | |
| 433 | + * @return void | |
| 434 | + */ | |
| 435 | + private function initTables() | |
| 436 | +    { | |
| 437 | + // permuted choice 1 (PC1) | |
| 438 | + // these values are chars and should be run through chr() when used | |
| 439 | + self::$_pc1 = array( | |
| 440 | + 57, 49, 41, 33, 25, 17, 9, | |
| 441 | + 1, 58, 50, 42, 34, 26, 18, | |
| 442 | + 10, 2, 59, 51, 43, 35, 27, | |
| 443 | + 19, 11, 3, 60, 52, 44, 36, | |
| 444 | + 63, 55, 47, 39, 31, 23, 15, | |
| 445 | + 7, 62, 54, 46, 38, 30, 22, | |
| 446 | + 14, 6, 61, 53, 45, 37, 29, | |
| 447 | + 21, 13, 5, 28, 20, 12, 4 | |
| 448 | + ); | |
| 449 | + | |
| 450 | + // permuted choice 2 (PC2) | |
| 451 | + // these values are chars and should be run through chr() when used | |
| 452 | + self::$_pc2 = array( | |
| 453 | + 14, 17, 11, 24, 1, 5, | |
| 454 | + 3, 28, 15, 6, 21, 10, | |
| 455 | + 23, 19, 12, 4, 26, 8, | |
| 456 | + 16, 7, 27, 20, 13, 2, | |
| 457 | + 41, 52, 31, 37, 47, 55, | |
| 458 | + 30, 40, 51, 45, 33, 48, | |
| 459 | + 44, 49, 39, 56, 34, 53, | |
| 460 | + 46, 42, 50, 36, 29, 32 | |
| 461 | + ); | |
| 462 | + | |
| 463 | + // initial permutation (IP) | |
| 464 | + self::$_ip = array( | |
| 465 | + 58, 50, 42, 34, 26, 18, 10, 2, | |
| 466 | + 60, 52, 44, 36, 28, 20, 12, 4, | |
| 467 | + 62, 54, 46, 38, 30, 22, 14, 6, | |
| 468 | + 64, 56, 48, 40, 32, 24, 16, 8, | |
| 469 | + 57, 49, 41, 33, 25, 17, 9, 1, | |
| 470 | + 59, 51, 43, 35, 27, 19, 11, 3, | |
| 471 | + 61, 53, 45, 37, 29, 21, 13, 5, | |
| 472 | + 63, 55, 47, 39, 31, 23, 15, 7 | |
| 473 | + ); | |
| 474 | + | |
| 475 | + // expansion (E) | |
| 476 | + self::$_e = array( | |
| 477 | + 32, 1, 2, 3, 4, 5, | |
| 478 | + 4, 5, 6, 7, 8, 9, | |
| 479 | + 8, 9, 10, 11, 12, 13, | |
| 480 | + 12, 13, 14, 15, 16, 17, | |
| 481 | + 16, 17, 18, 19, 20, 21, | |
| 482 | + 20, 21, 22, 23, 24, 25, | |
| 483 | + 24, 25, 26, 27, 28, 29, | |
| 484 | + 28, 29, 30, 31, 32, 1 | |
| 485 | + ); | |
| 486 | + | |
| 487 | + // substition box (S) | |
| 488 | + self::$_s = array( | |
| 489 | + /* S1 */ | |
| 490 | + array( | |
| 491 | + 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, | |
| 492 | + 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, | |
| 493 | + 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, | |
| 494 | + 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 | |
| 495 | + ), | |
| 496 | + | |
| 497 | + /* S2 */ | |
| 498 | + array( | |
| 499 | + 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, | |
| 500 | + 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, | |
| 501 | + 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, | |
| 502 | + 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 | |
| 503 | + ), | |
| 504 | + | |
| 505 | + /* S3 */ | |
| 506 | + array( | |
| 507 | + 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, | |
| 508 | + 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, | |
| 509 | + 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, | |
| 510 | + 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 | |
| 511 | + ), | |
| 512 | + | |
| 513 | + /* S4 */ | |
| 514 | + array( | |
| 515 | + 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, | |
| 516 | + 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, | |
| 517 | + 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, | |
| 518 | + 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 | |
| 519 | + ), | |
| 520 | + | |
| 521 | + /* S5 */ | |
| 522 | + array( | |
| 523 | + 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, | |
| 524 | + 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, | |
| 525 | + 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, | |
| 526 | + 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 | |
| 527 | + ), | |
| 528 | + | |
| 529 | + /* S6 */ | |
| 530 | + array( | |
| 531 | + 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, | |
| 532 | + 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, | |
| 533 | + 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, | |
| 534 | + 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 | |
| 535 | + ), | |
| 536 | + | |
| 537 | + /* S7 */ | |
| 538 | + array( | |
| 539 | + 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, | |
| 540 | + 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, | |
| 541 | + 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, | |
| 542 | + 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 | |
| 543 | + ), | |
| 544 | + | |
| 545 | + /* S8 */ | |
| 546 | + array( | |
| 547 | + 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, | |
| 548 | + 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, | |
| 549 | + 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, | |
| 550 | + 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 | |
| 551 | + ) | |
| 552 | + ); | |
| 553 | + | |
| 554 | + // permutation (P) | |
| 555 | + self::$_p = array( | |
| 556 | + 16, 7, 20, 21, | |
| 557 | + 29, 12, 28, 17, | |
| 558 | + 1, 15, 23, 26, | |
| 559 | + 5, 18, 31, 10, | |
| 560 | + 2, 8, 24, 14, | |
| 561 | + 32, 27, 3, 9, | |
| 562 | + 19, 13, 30, 6, | |
| 563 | + 22, 11, 4, 25 | |
| 564 | + ); | |
| 565 | + | |
| 566 | + // final permutation (FP) | |
| 567 | + self::$_fp = array( | |
| 568 | + 40, 8, 48, 16, 56, 24, 64, 32, | |
| 569 | + 39, 7, 47, 15, 55, 23, 63, 31, | |
| 570 | + 38, 6, 46, 14, 54, 22, 62, 30, | |
| 571 | + 37, 5, 45, 13, 53, 21, 61, 29, | |
| 572 | + 36, 4, 44, 12, 52, 20, 60, 28, | |
| 573 | + 35, 3, 43, 11, 51, 19, 59, 27, | |
| 574 | + 34, 2, 42, 10, 50, 18, 58, 26, | |
| 575 | + 33, 1, 41, 9, 49, 17, 57, 25 | |
| 576 | + ); | |
| 577 | + | |
| 578 | + // key schedule used in KeyPermutation() | |
| 579 | + self::$_key_sched = array(1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1); | |
| 580 | + } | |
| 581 | + | |
| 582 | + | |
| 583 | + /** | |
| 584 | + * Indicates this is a block cipher | |
| 585 | + * | |
| 586 | + * @return integer Returns Cipher::BLOCK | |
| 587 | + */ | |
| 588 | + public function type() | |
| 589 | +    { | |
| 590 | + return parent::BLOCK; | |
| 591 | + } | |
| 592 | 592 | } | 
| 593 | 593 | ?> | 
| @@ -201,10 +201,11 @@ discard block | ||
| 201 | 201 |  		{ | 
| 202 | 202 | $l[$n] = $r[$n - 1]; | 
| 203 | 203 | |
| 204 | - if ($this->operation() == parent::DECRYPT) | |
| 205 | - $f = $this->F($r[$n - 1], $this->sub_keys[16 - $n]); | |
| 206 | - else | |
| 207 | - $f = $this->F($r[$n - 1], $this->sub_keys[$n - 1]); | |
| 204 | +			if ($this->operation() == parent::DECRYPT) { | |
| 205 | + $f = $this->F($r[$n - 1], $this->sub_keys[16 - $n]); | |
| 206 | +			} else { | |
| 207 | + $f = $this->F($r[$n - 1], $this->sub_keys[$n - 1]); | |
| 208 | + } | |
| 208 | 209 | |
| 209 | 210 | // XOR F with Ln | 
| 210 | 211 | $r[$n] = $this->xorBin($l[$n - 1], $f); | 
| @@ -240,8 +241,9 @@ discard block | ||
| 240 | 241 | $binkey = parent::str2Bin($this->key()); | 
| 241 | 242 | |
| 242 | 243 | // reduce the key down to 56bits based on table $_pc1 | 
| 243 | - for ($i = 0; $i < 56; ++$i) | |
| 244 | - $pc1m[$i] = $binkey[self::$_pc1[$i] - 1]; | |
| 244 | +		for ($i = 0; $i < 56; ++$i) { | |
| 245 | + $pc1m[$i] = $binkey[self::$_pc1[$i] - 1]; | |
| 246 | + } | |
| 245 | 247 | |
| 246 | 248 | // split $pc1m in half (C0 and D0) | 
| 247 | 249 | $c[0] = array_slice($pc1m, 0, 28); | 
| @@ -270,8 +272,9 @@ discard block | ||
| 270 | 272 | // of these. | 
| 271 | 273 | $CnDn = array_merge($c[$i], $d[$i]); | 
| 272 | 274 | $this->sub_keys[$i - 1] = ""; | 
| 273 | - for ($j = 0; $j < 48; ++$j) | |
| 274 | - $this->sub_keys[$i - 1] .= $CnDn[self::$_pc2[$j] - 1]; | |
| 275 | +			for ($j = 0; $j < 48; ++$j) { | |
| 276 | + $this->sub_keys[$i - 1] .= $CnDn[self::$_pc2[$j] - 1]; | |
| 277 | + } | |
| 275 | 278 | } | 
| 276 | 279 | |
| 277 | 280 | // the sub_keys are created, we are done with the key permutation | 
| @@ -303,8 +306,9 @@ discard block | ||
| 303 | 306 | $ip = ""; | 
| 304 | 307 | |
| 305 | 308 | // loop through the 64 bit block, ordering it occording to $_ip | 
| 306 | - for ($i = 0; $i < 64; ++$i) | |
| 307 | - $ip .= $text[self::$_ip[$i] - 1]; | |
| 309 | +		for ($i = 0; $i < 64; ++$i) { | |
| 310 | + $ip .= $text[self::$_ip[$i] - 1]; | |
| 311 | + } | |
| 308 | 312 | |
| 309 | 313 | return $ip; | 
| 310 | 314 | } | 
| @@ -346,8 +350,9 @@ discard block | ||
| 346 | 350 | private function e($r) | 
| 347 | 351 |  	{ | 
| 348 | 352 | $e = ""; | 
| 349 | - for ($i = 0; $i < 48; ++$i) | |
| 350 | - $e .= $r[self::$_e[$i] - 1]; | |
| 353 | +		for ($i = 0; $i < 48; ++$i) { | |
| 354 | + $e .= $r[self::$_e[$i] - 1]; | |
| 355 | + } | |
| 351 | 356 | |
| 352 | 357 | return $e; | 
| 353 | 358 | } | 
| @@ -402,8 +407,9 @@ discard block | ||
| 402 | 407 | private function p($s) | 
| 403 | 408 |  	{ | 
| 404 | 409 | $p = ""; | 
| 405 | - for ($i = 0; $i < 32; ++$i) | |
| 406 | - $p .= $s[self::$_p[$i] - 1]; | |
| 410 | +		for ($i = 0; $i < 32; ++$i) { | |
| 411 | + $p .= $s[self::$_p[$i] - 1]; | |
| 412 | + } | |
| 407 | 413 | |
| 408 | 414 | return $p; | 
| 409 | 415 | } | 
| @@ -420,8 +426,9 @@ discard block | ||
| 420 | 426 | private function fp($bin) | 
| 421 | 427 |  	{ | 
| 422 | 428 | $fp = ""; | 
| 423 | - for ($i = 0; $i < 64; ++$i) | |
| 424 | - $fp .= $bin[self::$_fp[$i] - 1]; | |
| 429 | +		for ($i = 0; $i < 64; ++$i) { | |
| 430 | + $fp .= $bin[self::$_fp[$i] - 1]; | |
| 431 | + } | |
| 425 | 432 | |
| 426 | 433 | return $fp; | 
| 427 | 434 | } | 
| @@ -232,10 +232,10 @@ discard block | ||
| 232 | 232 | * | 
| 233 | 233 | * Unicode multi-byte character safe | 
| 234 | 234 | * | 
| 235 | - * @param plaintext source text to be encrypted | |
| 235 | + * @param plaintext string text to be encrypted | |
| 236 | 236 | * @param password the password to use to generate a key | 
| 237 | - * @param nBits number of bits to be used in the key (128, 192, or 256) | |
| 238 | - * @return encrypted text | |
| 237 | + * @param nBits integer of bits to be used in the key (128, 192, or 256) | |
| 238 | + * @return string text | |
| 239 | 239 | */ | 
| 240 | 240 | public static function encrypt($plaintext, $password, $nBits) | 
| 241 | 241 |      { | 
| @@ -315,8 +315,8 @@ discard block | ||
| 315 | 315 | * | 
| 316 | 316 | * @param ciphertext source text to be decrypted | 
| 317 | 317 | * @param password the password to use to generate a key | 
| 318 | - * @param nBits number of bits to be used in the key (128, 192, or 256) | |
| 319 | - * @return decrypted text | |
| 318 | + * @param nBits integer of bits to be used in the key (128, 192, or 256) | |
| 319 | + * @return string text | |
| 320 | 320 | */ | 
| 321 | 321 | public static function decrypt($ciphertext, $password, $nBits) | 
| 322 | 322 |      { | 
| @@ -1798,7 +1798,7 @@ discard block | ||
| 1798 | 1798 | 'type' => 'folder_deleted', | 
| 1799 | 1799 | 'intitule' => "f".$array_category[$i], | 
| 1800 | 1800 | 'valeur' => $folder->id.', '.$folder->parent_id.', '. | 
| 1801 | - $folder->title.', '.$folder->nleft.', '.$folder->nright.', '. $folder->nlevel.', 0, 0, 0, 0' | |
| 1801 | + $folder->title.', '.$folder->nleft.', '.$folder->nright.', '.$folder->nlevel.', 0, 0, 0, 0' | |
| 1802 | 1802 | ) | 
| 1803 | 1803 | ); | 
| 1804 | 1804 | //delete folder | 
| @@ -1993,29 +1993,29 @@ discard block | ||
| 1993 | 1993 | |
| 1994 | 1994 | echo json_encode($json); | 
| 1995 | 1995 |                  } else { | 
| 1996 | -                    rest_error ('NO_PARAMETERS'); | |
| 1996 | +                    rest_error('NO_PARAMETERS'); | |
| 1997 | 1997 | } | 
| 1998 | 1998 |              } else if ($GLOBALS['request'][1] === "version") { | 
| 1999 | 1999 |                  echo '{"api-version":"'.$api_version.'"}'; | 
| 2000 | 2000 |              } else { | 
| 2001 | -                rest_error ('NO_PARAMETERS'); | |
| 2001 | +                rest_error('NO_PARAMETERS'); | |
| 2002 | 2002 | } | 
| 2003 | 2003 |          } else { | 
| 2004 | -            rest_error ('METHOD'); | |
| 2004 | +            rest_error('METHOD'); | |
| 2005 | 2005 | } | 
| 2006 | 2006 | } | 
| 2007 | 2007 | } | 
| 2008 | 2008 | |
| 2009 | 2009 |  function rest_put() { | 
| 2010 | -    if(!@count($GLOBALS['request'])==0){ | |
| 2010 | +    if (!@count($GLOBALS['request']) == 0) { | |
| 2011 | 2011 | $request_uri = $GLOBALS['_SERVER']['REQUEST_URI']; | 
| 2012 | -        preg_match('/\/api(\/index.php|)\/(.*)\?apikey=(.*)/',$request_uri,$matches); | |
| 2012 | +        preg_match('/\/api(\/index.php|)\/(.*)\?apikey=(.*)/', $request_uri, $matches); | |
| 2013 | 2013 |          if (count($matches) == 0) { | 
| 2014 | -            rest_error ('REQUEST_SENT_NOT_UNDERSTANDABLE'); | |
| 2014 | +            rest_error('REQUEST_SENT_NOT_UNDERSTANDABLE'); | |
| 2015 | 2015 | } | 
| 2016 | -        $GLOBALS['request'] =  explode('/',$matches[2]); | |
| 2016 | +        $GLOBALS['request'] = explode('/', $matches[2]); | |
| 2017 | 2017 | } | 
| 2018 | -    if(apikey_checker($GLOBALS['apikey'])) { | |
| 2018 | +    if (apikey_checker($GLOBALS['apikey'])) { | |
| 2019 | 2019 | global $server, $user, $pass, $database, $pre, $link; | 
| 2020 | 2020 | teampass_connect(); | 
| 2021 | 2021 | |
| @@ -2025,7 +2025,7 @@ discard block | ||
| 2025 | 2025 | /** | 
| 2026 | 2026 | * @param string $type | 
| 2027 | 2027 | */ | 
| 2028 | -function rest_error ($type,$detail = 'N/A') { | |
| 2028 | +function rest_error($type, $detail = 'N/A') { | |
| 2029 | 2029 |      switch ($type) { | 
| 2030 | 2030 | case 'APIKEY': | 
| 2031 | 2031 |              $message = Array('err' => 'This api_key '.$GLOBALS['apikey'].' doesn\'t exist'); | 
| @@ -352,8 +352,7 @@ discard block | ||
| 352 | 352 | |
| 353 | 353 | $x++; | 
| 354 | 354 | } | 
| 355 | - } | |
| 356 | -            else if ($GLOBALS['request'][1] == "userpw") { | |
| 355 | +            } else if ($GLOBALS['request'][1] == "userpw") { | |
| 357 | 356 | /* | 
| 358 | 357 | * READ USER ITEMS | 
| 359 | 358 | */ | 
| @@ -437,8 +436,7 @@ discard block | ||
| 437 | 436 | |
| 438 | 437 | $x++; | 
| 439 | 438 | } | 
| 440 | - } | |
| 441 | -            else if ($GLOBALS['request'][1] == "userfolders") { | |
| 439 | +            } else if ($GLOBALS['request'][1] == "userfolders") { | |
| 442 | 440 | /* | 
| 443 | 441 | * READ USER FOLDERS | 
| 444 | 442 | * Sends back a list of folders | 
| @@ -481,8 +479,7 @@ discard block | ||
| 481 | 479 | } | 
| 482 | 480 | } | 
| 483 | 481 | } | 
| 484 | - } | |
| 485 | -            elseif ($GLOBALS['request'][1] == "items") { | |
| 482 | +            } elseif ($GLOBALS['request'][1] == "items") { | |
| 486 | 483 | /* | 
| 487 | 484 | * READ ITEMS asked | 
| 488 | 485 | */ | 
| @@ -829,8 +826,11 @@ discard block | ||
| 829 | 826 |                                  "SELECT `id` FROM ".prefix_table("roles_title")." WHERE title = %s", | 
| 830 | 827 | $role | 
| 831 | 828 | ); | 
| 832 | - if (empty($rolesList)) $rolesList = $tmp['id']; | |
| 833 | - else $rolesList .= ";".$tmp['id']; | |
| 829 | +                            if (empty($rolesList)) { | |
| 830 | + $rolesList = $tmp['id']; | |
| 831 | +                            } else { | |
| 832 | + $rolesList .= ";".$tmp['id']; | |
| 833 | + } | |
| 834 | 834 | } | 
| 835 | 835 | |
| 836 | 836 | // Add user in DB | 
| @@ -1925,11 +1925,21 @@ discard block | ||
| 1925 | 1925 | |
| 1926 | 1926 | // init | 
| 1927 | 1927 | $pwgen->setLength($params[0]); | 
| 1928 | - if ($params[1] === "1") $pwgen->setSecure(true); | |
| 1929 | - if ($params[2] === "1") $pwgen->setNumerals(true); | |
| 1930 | - if ($params[3] === "1") $pwgen->setCapitalize(true); | |
| 1931 | - if ($params[4] === "1") $pwgen->setAmbiguous(true); | |
| 1932 | - if ($params[5] === "1" && $params[6] === "1") $pwgen->setSymbols(true); | |
| 1928 | +                if ($params[1] === "1") { | |
| 1929 | + $pwgen->setSecure(true); | |
| 1930 | + } | |
| 1931 | +                if ($params[2] === "1") { | |
| 1932 | + $pwgen->setNumerals(true); | |
| 1933 | + } | |
| 1934 | +                if ($params[3] === "1") { | |
| 1935 | + $pwgen->setCapitalize(true); | |
| 1936 | + } | |
| 1937 | +                if ($params[4] === "1") { | |
| 1938 | + $pwgen->setAmbiguous(true); | |
| 1939 | + } | |
| 1940 | +                if ($params[5] === "1" && $params[6] === "1") { | |
| 1941 | + $pwgen->setSymbols(true); | |
| 1942 | + } | |
| 1933 | 1943 | |
| 1934 | 1944 | // generate and send back (generate in base64 if symbols are asked) | 
| 1935 | 1945 |                  if ($params[6] === "1") { |