Annotation of /trunk/lib/getid3/write.real.php
Parent Directory
|
Revision Log
Revision 2 - (view) (download)
| 1 : | andphe | 2 | <?php |
| 2 : | ///////////////////////////////////////////////////////////////// | ||
| 3 : | /// getID3() by James Heinrich <info@getid3.org> // | ||
| 4 : | // available at http://getid3.sourceforge.net // | ||
| 5 : | // or http://www.getid3.org // | ||
| 6 : | ///////////////////////////////////////////////////////////////// | ||
| 7 : | // See readme.txt for more details // | ||
| 8 : | ///////////////////////////////////////////////////////////////// | ||
| 9 : | // // | ||
| 10 : | // write.real.php // | ||
| 11 : | // module for writing RealAudio/RealVideo tags // | ||
| 12 : | // dependencies: module.tag.real.php // | ||
| 13 : | // /// | ||
| 14 : | ///////////////////////////////////////////////////////////////// | ||
| 15 : | // MOS Intruder Alerts | ||
| 16 : | defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' ); | ||
| 17 : | |||
| 18 : | class getid3_write_real | ||
| 19 : | { | ||
| 20 : | var $filename; | ||
| 21 : | var $tag_data = array(); | ||
| 22 : | var $warnings = array(); // any non-critical errors will be stored here | ||
| 23 : | var $errors = array(); // any critical errors will be stored here | ||
| 24 : | var $paddedlength = 512; // minimum length of CONT tag in bytes | ||
| 25 : | |||
| 26 : | function getid3_write_real() { | ||
| 27 : | return true; | ||
| 28 : | } | ||
| 29 : | |||
| 30 : | function WriteReal() { | ||
| 31 : | // File MUST be writeable - CHMOD(646) at least | ||
| 32 : | if (is_writeable($this->filename)) { | ||
| 33 : | if ($fp_source = @fopen($this->filename, 'r+b')) { | ||
| 34 : | |||
| 35 : | // Initialize getID3 engine | ||
| 36 : | $getID3 = new getID3; | ||
| 37 : | $OldThisFileInfo = $getID3->analyze($this->filename); | ||
| 38 : | if (empty($OldThisFileInfo['real']['chunks']) && !empty($OldThisFileInfo['real']['old_ra_header'])) { | ||
| 39 : | $this->errors[] = 'Cannot write Real tags on old-style file format'; | ||
| 40 : | fclose($fp_source); | ||
| 41 : | return false; | ||
| 42 : | } | ||
| 43 : | |||
| 44 : | if (empty($OldThisFileInfo['real']['chunks'])) { | ||
| 45 : | $this->errors[] = 'Cannot write Real tags because cannot find DATA chunk in file'; | ||
| 46 : | fclose($fp_source); | ||
| 47 : | return false; | ||
| 48 : | } | ||
| 49 : | foreach ($OldThisFileInfo['real']['chunks'] as $chunknumber => $chunkarray) { | ||
| 50 : | $oldChunkInfo[$chunkarray['name']] = $chunkarray; | ||
| 51 : | } | ||
| 52 : | if (!empty($oldChunkInfo['CONT']['length'])) { | ||
| 53 : | $this->paddedlength = max($oldChunkInfo['CONT']['length'], $this->paddedlength); | ||
| 54 : | } | ||
| 55 : | |||
| 56 : | $new_CONT_tag_data = $this->GenerateCONTchunk(); | ||
| 57 : | $new_PROP_tag_data = $this->GeneratePROPchunk($OldThisFileInfo['real']['chunks'], $new_CONT_tag_data); | ||
| 58 : | $new__RMF_tag_data = $this->GenerateRMFchunk($OldThisFileInfo['real']['chunks']); | ||
| 59 : | |||
| 60 : | if (@$oldChunkInfo['.RMF']['length'] == strlen($new__RMF_tag_data)) { | ||
| 61 : | fseek($fp_source, $oldChunkInfo['.RMF']['offset'], SEEK_SET); | ||
| 62 : | fwrite($fp_source, $new__RMF_tag_data); | ||
| 63 : | } else { | ||
| 64 : | $this->errors[] = 'new .RMF tag ('.strlen($new__RMF_tag_data).' bytes) different length than old .RMF tag ('.$oldChunkInfo['.RMF']['length'].' bytes)'; | ||
| 65 : | fclose($fp_source); | ||
| 66 : | return false; | ||
| 67 : | } | ||
| 68 : | |||
| 69 : | if (@$oldChunkInfo['PROP']['length'] == strlen($new_PROP_tag_data)) { | ||
| 70 : | fseek($fp_source, $oldChunkInfo['PROP']['offset'], SEEK_SET); | ||
| 71 : | fwrite($fp_source, $new_PROP_tag_data); | ||
| 72 : | } else { | ||
| 73 : | $this->errors[] = 'new PROP tag ('.strlen($new_PROP_tag_data).' bytes) different length than old PROP tag ('.$oldChunkInfo['PROP']['length'].' bytes)'; | ||
| 74 : | fclose($fp_source); | ||
| 75 : | return false; | ||
| 76 : | } | ||
| 77 : | |||
| 78 : | if (@$oldChunkInfo['CONT']['length'] == strlen($new_CONT_tag_data)) { | ||
| 79 : | |||
| 80 : | // new data length is same as old data length - just overwrite | ||
| 81 : | fseek($fp_source, $oldChunkInfo['CONT']['offset'], SEEK_SET); | ||
| 82 : | fwrite($fp_source, $new_CONT_tag_data); | ||
| 83 : | fclose($fp_source); | ||
| 84 : | return true; | ||
| 85 : | |||
| 86 : | } else { | ||
| 87 : | |||
| 88 : | if (empty($oldChunkInfo['CONT'])) { | ||
| 89 : | // no existing CONT chunk | ||
| 90 : | $BeforeOffset = $oldChunkInfo['DATA']['offset']; | ||
| 91 : | $AfterOffset = $oldChunkInfo['DATA']['offset']; | ||
| 92 : | } else { | ||
| 93 : | // new data is longer than old data | ||
| 94 : | $BeforeOffset = $oldChunkInfo['CONT']['offset']; | ||
| 95 : | $AfterOffset = $oldChunkInfo['CONT']['offset'] + $oldChunkInfo['CONT']['length']; | ||
| 96 : | } | ||
| 97 : | if ($tempfilename = tempnam('*', 'getID3')) { | ||
| 98 : | ob_start(); | ||
| 99 : | if ($fp_temp = fopen($tempfilename, 'wb')) { | ||
| 100 : | |||
| 101 : | rewind($fp_source); | ||
| 102 : | fwrite($fp_temp, fread($fp_source, $BeforeOffset)); | ||
| 103 : | fwrite($fp_temp, $new_CONT_tag_data); | ||
| 104 : | fseek($fp_source, $AfterOffset, SEEK_SET); | ||
| 105 : | while ($buffer = fread($fp_source, GETID3_FREAD_BUFFER_SIZE)) { | ||
| 106 : | fwrite($fp_temp, $buffer, strlen($buffer)); | ||
| 107 : | } | ||
| 108 : | fclose($fp_temp); | ||
| 109 : | |||
| 110 : | if (copy($tempfilename, $this->filename)) { | ||
| 111 : | unlink($tempfilename); | ||
| 112 : | fclose($fp_source); | ||
| 113 : | return true; | ||
| 114 : | } | ||
| 115 : | unlink($tempfilename); | ||
| 116 : | $this->errors[] = 'FAILED: copy('.$tempfilename.', '.$this->filename.') - '.strip_tags(ob_get_contents()); | ||
| 117 : | |||
| 118 : | } else { | ||
| 119 : | |||
| 120 : | $this->errors[] = 'Could not open '.$tempfilename.' mode "wb" - '.strip_tags(ob_get_contents()); | ||
| 121 : | |||
| 122 : | } | ||
| 123 : | ob_end_clean(); | ||
| 124 : | } | ||
| 125 : | fclose($fp_source); | ||
| 126 : | return false; | ||
| 127 : | |||
| 128 : | } | ||
| 129 : | |||
| 130 : | |||
| 131 : | } else { | ||
| 132 : | $this->errors[] = 'Could not open '.$this->filename.' mode "r+b"'; | ||
| 133 : | return false; | ||
| 134 : | } | ||
| 135 : | } | ||
| 136 : | $this->errors[] = 'File is not writeable: '.$this->filename; | ||
| 137 : | return false; | ||
| 138 : | } | ||
| 139 : | |||
| 140 : | function GenerateRMFchunk(&$chunks) { | ||
| 141 : | $oldCONTexists = false; | ||
| 142 : | foreach ($chunks as $key => $chunk) { | ||
| 143 : | $chunkNameKeys[$chunk['name']] = $key; | ||
| 144 : | if ($chunk['name'] == 'CONT') { | ||
| 145 : | $oldCONTexists = true; | ||
| 146 : | } | ||
| 147 : | } | ||
| 148 : | $newHeadersCount = $chunks[$chunkNameKeys['.RMF']]['headers_count'] + ($oldCONTexists ? 0 : 1); | ||
| 149 : | |||
| 150 : | $RMFchunk = "\x00\x00"; // object version | ||
| 151 : | $RMFchunk .= getid3_lib::BigEndian2String($chunks[$chunkNameKeys['.RMF']]['file_version'], 4); | ||
| 152 : | $RMFchunk .= getid3_lib::BigEndian2String($newHeadersCount, 4); | ||
| 153 : | |||
| 154 : | $RMFchunk = '.RMF'.getid3_lib::BigEndian2String(strlen($RMFchunk) + 8, 4).$RMFchunk; // .RMF chunk identifier + chunk length | ||
| 155 : | return $RMFchunk; | ||
| 156 : | } | ||
| 157 : | |||
| 158 : | function GeneratePROPchunk(&$chunks, &$new_CONT_tag_data) { | ||
| 159 : | $old_CONT_length = 0; | ||
| 160 : | $old_DATA_offset = 0; | ||
| 161 : | $old_INDX_offset = 0; | ||
| 162 : | foreach ($chunks as $key => $chunk) { | ||
| 163 : | $chunkNameKeys[$chunk['name']] = $key; | ||
| 164 : | if ($chunk['name'] == 'CONT') { | ||
| 165 : | $old_CONT_length = $chunk['length']; | ||
| 166 : | } elseif ($chunk['name'] == 'DATA') { | ||
| 167 : | if (!$old_DATA_offset) { | ||
| 168 : | $old_DATA_offset = $chunk['offset']; | ||
| 169 : | } | ||
| 170 : | } elseif ($chunk['name'] == 'INDX') { | ||
| 171 : | if (!$old_INDX_offset) { | ||
| 172 : | $old_INDX_offset = $chunk['offset']; | ||
| 173 : | } | ||
| 174 : | } | ||
| 175 : | } | ||
| 176 : | $CONTdelta = strlen($new_CONT_tag_data) - $old_CONT_length; | ||
| 177 : | |||
| 178 : | $PROPchunk = "\x00\x00"; // object version | ||
| 179 : | $PROPchunk .= getid3_lib::BigEndian2String($chunks[$chunkNameKeys['PROP']]['max_bit_rate'], 4); | ||
| 180 : | $PROPchunk .= getid3_lib::BigEndian2String($chunks[$chunkNameKeys['PROP']]['avg_bit_rate'], 4); | ||
| 181 : | $PROPchunk .= getid3_lib::BigEndian2String($chunks[$chunkNameKeys['PROP']]['max_packet_size'], 4); | ||
| 182 : | $PROPchunk .= getid3_lib::BigEndian2String($chunks[$chunkNameKeys['PROP']]['avg_packet_size'], 4); | ||
| 183 : | $PROPchunk .= getid3_lib::BigEndian2String($chunks[$chunkNameKeys['PROP']]['num_packets'], 4); | ||
| 184 : | $PROPchunk .= getid3_lib::BigEndian2String($chunks[$chunkNameKeys['PROP']]['duration'], 4); | ||
| 185 : | $PROPchunk .= getid3_lib::BigEndian2String($chunks[$chunkNameKeys['PROP']]['preroll'], 4); | ||
| 186 : | $PROPchunk .= getid3_lib::BigEndian2String(max(0, $old_INDX_offset + $CONTdelta), 4); | ||
| 187 : | $PROPchunk .= getid3_lib::BigEndian2String(max(0, $old_DATA_offset + $CONTdelta), 4); | ||
| 188 : | $PROPchunk .= getid3_lib::BigEndian2String($chunks[$chunkNameKeys['PROP']]['num_streams'], 2); | ||
| 189 : | $PROPchunk .= getid3_lib::BigEndian2String($chunks[$chunkNameKeys['PROP']]['flags_raw'], 2); | ||
| 190 : | |||
| 191 : | $PROPchunk = 'PROP'.getid3_lib::BigEndian2String(strlen($PROPchunk) + 8, 4).$PROPchunk; // PROP chunk identifier + chunk length | ||
| 192 : | return $PROPchunk; | ||
| 193 : | } | ||
| 194 : | |||
| 195 : | function GenerateCONTchunk() { | ||
| 196 : | foreach ($this->tag_data as $key => $value) { | ||
| 197 : | // limit each value to 0xFFFF bytes | ||
| 198 : | $this->tag_data[$key] = substr($value, 0, 65535); | ||
| 199 : | } | ||
| 200 : | |||
| 201 : | $CONTchunk = "\x00\x00"; // object version | ||
| 202 : | |||
| 203 : | $CONTchunk .= getid3_lib::BigEndian2String(strlen(@$this->tag_data['title']), 2); | ||
| 204 : | $CONTchunk .= @$this->tag_data['title']; | ||
| 205 : | |||
| 206 : | $CONTchunk .= getid3_lib::BigEndian2String(strlen(@$this->tag_data['artist']), 2); | ||
| 207 : | $CONTchunk .= @$this->tag_data['artist']; | ||
| 208 : | |||
| 209 : | $CONTchunk .= getid3_lib::BigEndian2String(strlen(@$this->tag_data['copyright']), 2); | ||
| 210 : | $CONTchunk .= @$this->tag_data['copyright']; | ||
| 211 : | |||
| 212 : | $CONTchunk .= getid3_lib::BigEndian2String(strlen(@$this->tag_data['comment']), 2); | ||
| 213 : | $CONTchunk .= @$this->tag_data['comment']; | ||
| 214 : | |||
| 215 : | if ($this->paddedlength > (strlen($CONTchunk) + 8)) { | ||
| 216 : | $CONTchunk .= str_repeat("\x00", $this->paddedlength - strlen($CONTchunk) - 8); | ||
| 217 : | } | ||
| 218 : | |||
| 219 : | $CONTchunk = 'CONT'.getid3_lib::BigEndian2String(strlen($CONTchunk) + 8, 4).$CONTchunk; // CONT chunk identifier + chunk length | ||
| 220 : | |||
| 221 : | return $CONTchunk; | ||
| 222 : | } | ||
| 223 : | |||
| 224 : | function RemoveReal() { | ||
| 225 : | // File MUST be writeable - CHMOD(646) at least | ||
| 226 : | if (is_writeable($this->filename)) { | ||
| 227 : | if ($fp_source = @fopen($this->filename, 'r+b')) { | ||
| 228 : | |||
| 229 : | // Initialize getID3 engine | ||
| 230 : | $getID3 = new getID3; | ||
| 231 : | $OldThisFileInfo = $getID3->analyze($this->filename); | ||
| 232 : | if (empty($OldThisFileInfo['real']['chunks']) && !empty($OldThisFileInfo['real']['old_ra_header'])) { | ||
| 233 : | $this->errors[] = 'Cannot remove Real tags from old-style file format'; | ||
| 234 : | fclose($fp_source); | ||
| 235 : | return false; | ||
| 236 : | } | ||
| 237 : | |||
| 238 : | if (empty($OldThisFileInfo['real']['chunks'])) { | ||
| 239 : | $this->errors[] = 'Cannot remove Real tags because cannot find DATA chunk in file'; | ||
| 240 : | fclose($fp_source); | ||
| 241 : | return false; | ||
| 242 : | } | ||
| 243 : | foreach ($OldThisFileInfo['real']['chunks'] as $chunknumber => $chunkarray) { | ||
| 244 : | $oldChunkInfo[$chunkarray['name']] = $chunkarray; | ||
| 245 : | } | ||
| 246 : | |||
| 247 : | if (empty($oldChunkInfo['CONT'])) { | ||
| 248 : | // no existing CONT chunk | ||
| 249 : | fclose($fp_source); | ||
| 250 : | return true; | ||
| 251 : | } | ||
| 252 : | |||
| 253 : | $BeforeOffset = $oldChunkInfo['CONT']['offset']; | ||
| 254 : | $AfterOffset = $oldChunkInfo['CONT']['offset'] + $oldChunkInfo['CONT']['length']; | ||
| 255 : | if ($tempfilename = tempnam('*', 'getID3')) { | ||
| 256 : | ob_start(); | ||
| 257 : | if ($fp_temp = fopen($tempfilename, 'wb')) { | ||
| 258 : | |||
| 259 : | rewind($fp_source); | ||
| 260 : | fwrite($fp_temp, fread($fp_source, $BeforeOffset)); | ||
| 261 : | fseek($fp_source, $AfterOffset, SEEK_SET); | ||
| 262 : | while ($buffer = fread($fp_source, GETID3_FREAD_BUFFER_SIZE)) { | ||
| 263 : | fwrite($fp_temp, $buffer, strlen($buffer)); | ||
| 264 : | } | ||
| 265 : | fclose($fp_temp); | ||
| 266 : | |||
| 267 : | if (copy($tempfilename, $this->filename)) { | ||
| 268 : | unlink($tempfilename); | ||
| 269 : | fclose($fp_source); | ||
| 270 : | return true; | ||
| 271 : | } | ||
| 272 : | unlink($tempfilename); | ||
| 273 : | $this->errors[] = 'FAILED: copy('.$tempfilename.', '.$this->filename.') - '.strip_tags(ob_get_contents()); | ||
| 274 : | |||
| 275 : | } else { | ||
| 276 : | |||
| 277 : | $this->errors[] = 'Could not open '.$tempfilename.' mode "wb" - '.strip_tags(ob_get_contents()); | ||
| 278 : | |||
| 279 : | } | ||
| 280 : | ob_end_clean(); | ||
| 281 : | } | ||
| 282 : | fclose($fp_source); | ||
| 283 : | return false; | ||
| 284 : | |||
| 285 : | |||
| 286 : | } else { | ||
| 287 : | $this->errors[] = 'Could not open '.$this->filename.' mode "r+b"'; | ||
| 288 : | return false; | ||
| 289 : | } | ||
| 290 : | } | ||
| 291 : | $this->errors[] = 'File is not writeable: '.$this->filename; | ||
| 292 : | return false; | ||
| 293 : | } | ||
| 294 : | |||
| 295 : | } | ||
| 296 : | |||
| 297 : | ?> |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |
Web Hosting provided by Network Redux.

