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

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.vorbiscomment.php                                     //
0012 // module for writing VorbisComment tags                       //
0013 // dependencies: /helperapps/vorbiscomment.exe                 //
0014 //                                                            ///
0015 /////////////////////////////////////////////////////////////////
0016 
0017 
0018 class getid3_write_vorbiscomment
0019 {
0020 
0021   public $filename;
0022   public $tag_data;
0023   public $warnings = array(); // any non-critical errors will be stored here
0024   public $errors   = array(); // any critical errors will be stored here
0025 
0026   public function getid3_write_vorbiscomment() {
0027     return true;
0028   }
0029 
0030   public function WriteVorbisComment() {
0031 
0032     if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
0033       $this->errors[] = 'PHP running in Safe Mode (backtick operator not available) - cannot call vorbiscomment, tags not written';
0034       return false;
0035     }
0036 
0037     // Create file with new comments
0038     $tempcommentsfilename = tempnam(GETID3_TEMP_DIR, 'getID3');
0039     if (is_writable($tempcommentsfilename) && is_file($tempcommentsfilename) && ($fpcomments = fopen($tempcommentsfilename, 'wb'))) {
0040 
0041       foreach ($this->tag_data as $key => $value) {
0042         foreach ($value as $commentdata) {
0043           fwrite($fpcomments, $this->CleanVorbisCommentName($key).'='.$commentdata."\n");
0044         }
0045       }
0046       fclose($fpcomments);
0047 
0048     } else {
0049       $this->errors[] = 'failed to open temporary tags file "'.$tempcommentsfilename.'", tags not written';
0050       return false;
0051     }
0052 
0053     $oldignoreuserabort = ignore_user_abort(true);
0054     if (GETID3_OS_ISWINDOWS) {
0055 
0056       if (file_exists(GETID3_HELPERAPPSDIR.'vorbiscomment.exe')) {
0057         //$commandline = '"'.GETID3_HELPERAPPSDIR.'vorbiscomment.exe" -w --raw -c "'.$tempcommentsfilename.'" "'.str_replace('/', '\\', $this->filename).'"';
0058         //  vorbiscomment works fine if you copy-paste the above commandline into a command prompt,
0059         //  but refuses to work with `backtick` if there are "doublequotes" present around BOTH
0060         //  the metaflac pathname and the target filename. For whatever reason...??
0061         //  The solution is simply ensure that the metaflac pathname has no spaces,
0062         //  and therefore does not need to be quoted
0063 
0064         // On top of that, if error messages are not always captured properly under Windows
0065         // To at least see if there was a problem, compare file modification timestamps before and after writing
0066         clearstatcache();
0067         $timestampbeforewriting = filemtime($this->filename);
0068 
0069         $commandline = GETID3_HELPERAPPSDIR.'vorbiscomment.exe -w --raw -c "'.$tempcommentsfilename.'" "'.$this->filename.'" 2>&1';
0070         $VorbiscommentError = `$commandline`;
0071 
0072         if (empty($VorbiscommentError)) {
0073           clearstatcache();
0074           if ($timestampbeforewriting == filemtime($this->filename)) {
0075             $VorbiscommentError = 'File modification timestamp has not changed - it looks like the tags were not written';
0076           }
0077         }
0078       } else {
0079         $VorbiscommentError = 'vorbiscomment.exe not found in '.GETID3_HELPERAPPSDIR;
0080       }
0081 
0082     } else {
0083 
0084       $commandline = 'vorbiscomment -w --raw -c "'.$tempcommentsfilename.'" "'.$this->filename.'" 2>&1';
0085       $VorbiscommentError = `$commandline`;
0086 
0087     }
0088 
0089     // Remove temporary comments file
0090     unlink($tempcommentsfilename);
0091     ignore_user_abort($oldignoreuserabort);
0092 
0093     if (!empty($VorbiscommentError)) {
0094 
0095       $this->errors[] = 'system call to vorbiscomment failed with message: '."\n\n".$VorbiscommentError;
0096       return false;
0097 
0098     }
0099 
0100     return true;
0101   }
0102 
0103   public function DeleteVorbisComment() {
0104     $this->tag_data = array(array());
0105     return $this->WriteVorbisComment();
0106   }
0107 
0108   public function CleanVorbisCommentName($originalcommentname) {
0109     // A case-insensitive field name that may consist of ASCII 0x20 through 0x7D, 0x3D ('=') excluded.
0110     // ASCII 0x41 through 0x5A inclusive (A-Z) is to be considered equivalent to ASCII 0x61 through
0111     // 0x7A inclusive (a-z).
0112 
0113     // replace invalid chars with a space, return uppercase text
0114     // Thanks Chris Bolt <chris-getid3Øbolt*cx> for improving this function
0115     // note: *reg_replace() replaces nulls with empty string (not space)
0116     return strtoupper(preg_replace('#[^ -<>-}]#', ' ', str_replace("\x00", ' ', $originalcommentname)));
0117 
0118   }
0119 
0120 }