| Conditions | 56 |
| Paths | 1041 |
| Total Lines | 147 |
| Code Lines | 96 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 58 | function __construct(array $rawMetadata, array $idArr) |
||
| 59 | { |
||
| 60 | /** @var |
||
| 61 | * Associative array indexed by $this->metaData key, mapping to arrays: |
||
| 62 | * [ <index:string>, <numeric:bool> ] |
||
| 63 | */ |
||
| 64 | $metadataTranslation = [ |
||
| 65 | 'version' => [ 'V', true ], |
||
| 66 | 'revision' => [ 'R', true ], |
||
| 67 | 'length' => [ 'Length', true ], |
||
| 68 | 'ownerKey' => [ 'O', false ], |
||
| 69 | 'userKey' => [ 'U', false ], |
||
| 70 | 'ownerEnc' => [ 'OE', false ], |
||
| 71 | 'userEnc' => [ 'UE', false ], |
||
| 72 | 'perms' => [ 'P', true ] |
||
| 73 | ]; |
||
| 74 | |||
| 75 | $this->metadata = ['encryptMetadata' => true]; |
||
|
|
|||
| 76 | // $rawMetadata is an array of one value being an array representing a PDF list |
||
| 77 | $headerArr = $rawMetadata[0]; |
||
| 78 | if (\count($headerArr) == 3 && $headerArr[0] == '<<') { |
||
| 79 | $headerDic = $headerArr[1]; |
||
| 80 | } else { |
||
| 81 | throw new SyntaxError("Missing encryption header"); |
||
| 82 | } |
||
| 83 | foreach ($metadataTranslation as $key => $info) { |
||
| 84 | if ($info[1]) { |
||
| 85 | $this->metadata[$key] = (int)\Smalot\PdfParser\RawData\RawDataParser::getHeaderValue($headerDic, $info[0], 'numeric'); |
||
| 86 | } else { |
||
| 87 | // First look for a raw string |
||
| 88 | $val = \Smalot\PdfParser\RawData\RawDataParser::getHeaderValue($headerDic, $info[0], '(', false); |
||
| 89 | if (false !== $val) { |
||
| 90 | $this->metadata[$key] = $val; |
||
| 91 | } else { |
||
| 92 | // Then look for a hex string |
||
| 93 | $val = \Smalot\PdfParser\RawData\RawDataParser::getHeaderValue($headerDic, $info[0], '<'); |
||
| 94 | $this->metadata[$key] = \hex2bin($val); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | // This should be an array |
||
| 100 | try { |
||
| 101 | $this->docID = $this->decodeDocID($idArr); |
||
| 102 | } |
||
| 103 | catch (\TypeError $e) { |
||
| 104 | // or a scalar value, which is a spec breach |
||
| 105 | $this->docID = ""; |
||
| 106 | } |
||
| 107 | |||
| 108 | if ($this->metadata['version'] != 0 && $this->metadata['revision'] != 0 |
||
| 109 | && $this->metadata['perms'] != 0 |
||
| 110 | && is_string($this->metadata['ownerKey']) && is_string($this->metadata['userKey'])) { |
||
| 111 | if (($this->metadata['revision'] <= 4 && strlen($this->metadata['ownerKey']) == 32 && strlen($this->metadata['userKey']) == 32) |
||
| 112 | || (($this->metadata['revision'] == 5 || $this->metadata['revision'] == 6) |
||
| 113 | // the spec says 48 bytes, but Acrobat pads them out longer |
||
| 114 | && strlen($this->metadata['ownerKey']) >= 48 && strlen($this->metadata['userKey']) >= 48 |
||
| 115 | && is_string($this->metadata['ownerEnc']) && strlen($this->metadata['ownerEnc']) == 32 && is_string($this->metadata['userEnc']) |
||
| 116 | && strlen($this->metadata['userEnc']) == 32)) { |
||
| 117 | $this->encAlgorithm = 'RC4'; |
||
| 118 | // revision 2 forces a 40-bit key - some buggy PDF generators |
||
| 119 | // set the Length value incorrectly |
||
| 120 | if ($this->metadata['revision'] == 2 || $this->metadata['length'] == 0) { |
||
| 121 | $this->fileKeyLength = 5; |
||
| 122 | } else { |
||
| 123 | $this->fileKeyLength = $this->metadata['length'] / 8; |
||
| 124 | } |
||
| 125 | $this->metadata['encryptMetadata'] = true; |
||
| 126 | //~ this currently only handles a subset of crypt filter functionality |
||
| 127 | //~ (in particular, it ignores the EFF entry in $headerDic, and |
||
| 128 | //~ doesn't handle the case where StmF, StrF, and EFF are not all the |
||
| 129 | //~ same) |
||
| 130 | if (($this->metadata['version'] == 4 || $this->metadata['version'] == 5) && ($this->metadata['revision'] == 4 || $this->metadata['revision'] == 5 || $this->metadata['revision'] == 6)) { |
||
| 131 | $cryptFiltersDic = \Smalot\PdfParser\RawData\RawDataParser::getHeaderValue($headerDic, 'CF', '<<'); |
||
| 132 | $this->metadata['streamFilter'] = \Smalot\PdfParser\RawData\RawDataParser::getHeaderValue($headerDic, 'StmF', '/'); |
||
| 133 | $this->metadata['stringFilter'] = \Smalot\PdfParser\RawData\RawDataParser::getHeaderValue($headerDic, 'StrF', '/'); |
||
| 134 | if (!empty($cryptFiltersDic) && is_string($this->metadata['streamFilter']) && is_string($this->metadata['stringFilter']) && $this->metadata['streamFilter'] == $this->metadata['stringFilter']) { |
||
| 135 | if ($this->metadata['streamFilter'] == "Identity") { |
||
| 136 | // no encryption on streams or strings |
||
| 137 | $this->metadata['version'] = $this->metadata['revision'] = -1; |
||
| 138 | } else { |
||
| 139 | // Find required crypt filter and its crypt filter method |
||
| 140 | // and update metadata accordingly |
||
| 141 | $cryptFilterInfoDic = \Smalot\PdfParser\RawData\RawDataParser::getHeaderValue($cryptFiltersDic, $this->metadata['streamFilter'], '<<'); |
||
| 142 | $method = \Smalot\PdfParser\RawData\RawDataParser::getHeaderValue($cryptFilterInfoDic, 'CFM', '/'); |
||
| 143 | switch ($method) { |
||
| 144 | case 'V2': |
||
| 145 | $this->metadata['version'] = 2; |
||
| 146 | $this->metadata['revision'] = 3; |
||
| 147 | $this->metadata['cfLength'] = (int)\Smalot\PdfParser\RawData\RawDataParser::getHeaderValue($cryptFilterInfoDic, 'Length', 'numeric', -1); |
||
| 148 | if ($this->metadata['cfLength'] != 0) { |
||
| 149 | //~ according to the spec, this should be cfLength / 8 |
||
| 150 | $this->fileKeyLength = $this->metadata['cfLength']; |
||
| 151 | } |
||
| 152 | break; |
||
| 153 | |||
| 154 | case 'AESV2': |
||
| 155 | $this->metadata['version'] = 2; |
||
| 156 | $this->metadata['revision'] = 3; |
||
| 157 | $this->encAlgorithm = 'AES'; |
||
| 158 | $this->metadata['cfLength'] = (int)\Smalot\PdfParser\RawData\RawDataParser::getHeaderValue($cryptFilterInfoDic, 'Length', 'numeric', -1); |
||
| 159 | if ($this->metadata['cfLength'] != 0) { |
||
| 160 | //~ according to the spec, this should be cfLength / 8 |
||
| 161 | $this->fileKeyLength = $this->metadata['cfLength']; |
||
| 162 | } |
||
| 163 | break; |
||
| 164 | |||
| 165 | case 'AESV3': |
||
| 166 | $this->metadata['version'] = 5; |
||
| 167 | // let $this->metadata['revision'] be 5 or 6 |
||
| 168 | $this->encAlgorithm = 'AES256'; |
||
| 169 | $this->metadata['cfLength'] = (int)\Smalot\PdfParser\RawData\RawDataParser::getHeaderValue($cryptFilterInfoDic, 'Length', 'numeric', -1); |
||
| 170 | if ($this->metadata['cfLength'] != 0) { |
||
| 171 | //~ according to the spec, this should be cfLengthArr / 8 |
||
| 172 | $this->fileKeyLength = $this->metadata['cfLength']; |
||
| 173 | } |
||
| 174 | break; |
||
| 175 | |||
| 176 | default: |
||
| 177 | throw new SyntaxError("Unknown CFM '$method'"); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | } |
||
| 181 | $this->metadata['encryptMetadata'] = (\Smalot\PdfParser\RawData\RawDataParser::getHeaderValue($headerDic, 'EncryptMetadata', 'boolean') === "true"); |
||
| 182 | } |
||
| 183 | if ($this->metadata['version'] >= 1 && $this->metadata['version'] <= 2 && $this->metadata['revision'] >= 2 && $this->metadata['revision'] <= 3) { |
||
| 184 | if ($this->fileKeyLength > 16 || $this->fileKeyLength < 0) { |
||
| 185 | $this->fileKeyLength = 16; |
||
| 186 | } |
||
| 187 | $this->ok = true; |
||
| 188 | } elseif ($this->metadata['version'] == 5 && ($this->metadata['revision'] == 5 || $this->metadata['revision'] == 6)) { |
||
| 189 | if (is_string($this->metadata['ownerEnc']) && is_string($this->metadata['userEnc'])) { |
||
| 190 | if ($this->fileKeyLength > 32 || $this->fileKeyLength < 0) { |
||
| 191 | $this->fileKeyLength = 32; |
||
| 192 | } |
||
| 193 | $this->ok = true; |
||
| 194 | } else { |
||
| 195 | throw new SyntaxError("Weird encryption owner/user info"); |
||
| 196 | } |
||
| 197 | } elseif (!($this->version == -1 && $this->revision == -1)) { |
||
| 198 | throw new Unimplemented("Unsupported version/revision (%d/%d) of Standard security handler", $this->version, $this->revision); |
||
| 199 | } |
||
| 200 | } else { |
||
| 201 | throw new SyntaxError("Invalid encryption key length"); |
||
| 202 | } |
||
| 203 | } else { |
||
| 204 | throw new SyntaxError("Weird encryption info"); |
||
| 205 | } |
||
| 297 |