File indexing completed on 2024-12-22 05:33:14

0001 <?php
0002 /////////////////////////////////////////////////////////////////
0003 /// getID3() by James Heinrich <info@getid3.org>               //
0004 //  available at http://getid3.sourceforge.net                 //
0005 //            or http://www.getid3.org                         //
0006 //          also https://github.com/JamesHeinrich/getID3       //
0007 /////////////////////////////////////////////////////////////////
0008 // See readme.txt for more details                             //
0009 /////////////////////////////////////////////////////////////////
0010 //                                                             //
0011 // write.id3v1.php                                             //
0012 // module for writing ID3v1 tags                               //
0013 // dependencies: module.tag.id3v1.php                          //
0014 //                                                            ///
0015 /////////////////////////////////////////////////////////////////
0016 
0017 getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.tag.id3v1.php', __FILE__, true);
0018 
0019 class getid3_write_id3v1
0020 {
0021   public $filename;
0022   public $filesize;
0023   public $tag_data;
0024   public $warnings = array(); // any non-critical errors will be stored here
0025   public $errors   = array(); // any critical errors will be stored here
0026 
0027   public function getid3_write_id3v1() {
0028     return true;
0029   }
0030 
0031   public function WriteID3v1() {
0032     // File MUST be writeable - CHMOD(646) at least
0033     if (!empty($this->filename) && is_readable($this->filename) && is_writable($this->filename) && is_file($this->filename)) {
0034       $this->setRealFileSize();
0035       if (($this->filesize <= 0) || !getid3_lib::intValueSupported($this->filesize)) {
0036         $this->errors[] = 'Unable to WriteID3v1('.$this->filename.') because filesize ('.$this->filesize.') is larger than '.round(PHP_INT_MAX / 1073741824).'GB';
0037         return false;
0038       }
0039       if ($fp_source = fopen($this->filename, 'r+b')) {
0040         fseek($fp_source, -128, SEEK_END);
0041         if (fread($fp_source, 3) == 'TAG') {
0042           fseek($fp_source, -128, SEEK_END); // overwrite existing ID3v1 tag
0043         } else {
0044           fseek($fp_source, 0, SEEK_END);    // append new ID3v1 tag
0045         }
0046         $this->tag_data['track'] = (isset($this->tag_data['track']) ? $this->tag_data['track'] : (isset($this->tag_data['track_number']) ? $this->tag_data['track_number'] : (isset($this->tag_data['tracknumber']) ? $this->tag_data['tracknumber'] : '')));
0047 
0048         $new_id3v1_tag_data = getid3_id3v1::GenerateID3v1Tag(
0049                             (isset($this->tag_data['title']  ) ? $this->tag_data['title']   : ''),
0050                             (isset($this->tag_data['artist'] ) ? $this->tag_data['artist']  : ''),
0051                             (isset($this->tag_data['album']  ) ? $this->tag_data['album']   : ''),
0052                             (isset($this->tag_data['year']   ) ? $this->tag_data['year']    : ''),
0053                             (isset($this->tag_data['genreid']) ? $this->tag_data['genreid'] : ''),
0054                             (isset($this->tag_data['comment']) ? $this->tag_data['comment'] : ''),
0055                             (isset($this->tag_data['track']  ) ? $this->tag_data['track']   : ''));
0056         fwrite($fp_source, $new_id3v1_tag_data, 128);
0057         fclose($fp_source);
0058         return true;
0059 
0060       } else {
0061         $this->errors[] = 'Could not fopen('.$this->filename.', "r+b")';
0062         return false;
0063       }
0064     }
0065     $this->errors[] = 'File is not writeable: '.$this->filename;
0066     return false;
0067   }
0068 
0069   public function FixID3v1Padding() {
0070     // ID3v1 data is supposed to be padded with NULL characters, but some taggers incorrectly use spaces
0071     // This function rewrites the ID3v1 tag with correct padding
0072 
0073     // Initialize getID3 engine
0074     $getID3 = new getID3;
0075     $getID3->option_tag_id3v2  = false;
0076     $getID3->option_tag_apetag = false;
0077     $getID3->option_tags_html  = false;
0078     $getID3->option_extra_info = false;
0079     $getID3->option_tag_id3v1  = true;
0080     $ThisFileInfo = $getID3->analyze($this->filename);
0081     if (isset($ThisFileInfo['tags']['id3v1'])) {
0082       foreach ($ThisFileInfo['tags']['id3v1'] as $key => $value) {
0083         $id3v1data[$key] = implode(',', $value);
0084       }
0085       $this->tag_data = $id3v1data;
0086       return $this->WriteID3v1();
0087     }
0088     return false;
0089   }
0090 
0091   public function RemoveID3v1() {
0092     // File MUST be writeable - CHMOD(646) at least
0093     if (!empty($this->filename) && is_readable($this->filename) && is_writable($this->filename) && is_file($this->filename)) {
0094       $this->setRealFileSize();
0095       if (($this->filesize <= 0) || !getid3_lib::intValueSupported($this->filesize)) {
0096         $this->errors[] = 'Unable to RemoveID3v1('.$this->filename.') because filesize ('.$this->filesize.') is larger than '.round(PHP_INT_MAX / 1073741824).'GB';
0097         return false;
0098       }
0099       if ($fp_source = fopen($this->filename, 'r+b')) {
0100 
0101         fseek($fp_source, -128, SEEK_END);
0102         if (fread($fp_source, 3) == 'TAG') {
0103           ftruncate($fp_source, $this->filesize - 128);
0104         } else {
0105           // no ID3v1 tag to begin with - do nothing
0106         }
0107         fclose($fp_source);
0108         return true;
0109 
0110       } else {
0111         $this->errors[] = 'Could not fopen('.$this->filename.', "r+b")';
0112       }
0113     } else {
0114       $this->errors[] = $this->filename.' is not writeable';
0115     }
0116     return false;
0117   }
0118 
0119   public function setRealFileSize() {
0120     if (PHP_INT_MAX > 2147483647) {
0121       $this->filesize = filesize($this->filename);
0122       return true;
0123     }
0124     // 32-bit PHP will not return correct values for filesize() if file is >=2GB
0125     // but getID3->analyze() has workarounds to get actual filesize
0126     $getID3 = new getID3;
0127     $getID3->option_tag_id3v1  = false;
0128     $getID3->option_tag_id3v2  = false;
0129     $getID3->option_tag_apetag = false;
0130     $getID3->option_tags_html  = false;
0131     $getID3->option_extra_info = false;
0132     $ThisFileInfo = $getID3->analyze($this->filename);
0133     $this->filesize = $ThisFileInfo['filesize'];
0134     return true;
0135   }
0136 
0137 }