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.metaflac.php                                          //
0012 // module for writing metaflac tags                            //
0013 // dependencies: /helperapps/metaflac.exe                      //
0014 //                                                            ///
0015 /////////////////////////////////////////////////////////////////
0016 
0017 
0018 class getid3_write_metaflac
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_metaflac() {
0027     return true;
0028   }
0029 
0030   public function WriteMetaFLAC() {
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 metaflac, 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       foreach ($this->tag_data as $key => $value) {
0041         foreach ($value as $commentdata) {
0042           fwrite($fpcomments, $this->CleanmetaflacName($key).'='.$commentdata."\n");
0043         }
0044       }
0045       fclose($fpcomments);
0046 
0047     } else {
0048       $this->errors[] = 'failed to open temporary tags file, tags not written - fopen("'.$tempcommentsfilename.'", "wb")';
0049       return false;
0050     }
0051 
0052     $oldignoreuserabort = ignore_user_abort(true);
0053     if (GETID3_OS_ISWINDOWS) {
0054 
0055       if (file_exists(GETID3_HELPERAPPSDIR.'metaflac.exe')) {
0056         //$commandline = '"'.GETID3_HELPERAPPSDIR.'metaflac.exe" --no-utf8-convert --remove-all-tags --import-tags-from="'.$tempcommentsfilename.'" "'.str_replace('/', '\\', $this->filename).'"';
0057         //  metaflac works fine if you copy-paste the above commandline into a command prompt,
0058         //  but refuses to work with `backtick` if there are "doublequotes" present around BOTH
0059         //  the metaflac pathname and the target filename. For whatever reason...??
0060         //  The solution is simply ensure that the metaflac pathname has no spaces,
0061         //  and therefore does not need to be quoted
0062 
0063         // On top of that, if error messages are not always captured properly under Windows
0064         // To at least see if there was a problem, compare file modification timestamps before and after writing
0065         clearstatcache();
0066         $timestampbeforewriting = filemtime($this->filename);
0067 
0068         $commandline = GETID3_HELPERAPPSDIR.'metaflac.exe --no-utf8-convert --remove-all-tags --import-tags-from='.escapeshellarg($tempcommentsfilename).' '.escapeshellarg($this->filename).' 2>&1';
0069         $metaflacError = `$commandline`;
0070 
0071         if (empty($metaflacError)) {
0072           clearstatcache();
0073           if ($timestampbeforewriting == filemtime($this->filename)) {
0074             $metaflacError = 'File modification timestamp has not changed - it looks like the tags were not written';
0075           }
0076         }
0077       } else {
0078         $metaflacError = 'metaflac.exe not found in '.GETID3_HELPERAPPSDIR;
0079       }
0080 
0081     } else {
0082 
0083       // It's simpler on *nix
0084       $commandline = 'metaflac --no-utf8-convert --remove-all-tags --import-tags-from='.escapeshellarg($tempcommentsfilename).' '.escapeshellarg($this->filename).' 2>&1';
0085       $metaflacError = `$commandline`;
0086 
0087     }
0088 
0089     // Remove temporary comments file
0090     unlink($tempcommentsfilename);
0091     ignore_user_abort($oldignoreuserabort);
0092 
0093     if (!empty($metaflacError)) {
0094 
0095       $this->errors[] = 'System call to metaflac failed with this message returned: '."\n\n".$metaflacError;
0096       return false;
0097 
0098     }
0099 
0100     return true;
0101   }
0102 
0103 
0104   public function DeleteMetaFLAC() {
0105 
0106     if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
0107       $this->errors[] = 'PHP running in Safe Mode (backtick operator not available) - cannot call metaflac, tags not deleted';
0108       return false;
0109     }
0110 
0111     $oldignoreuserabort = ignore_user_abort(true);
0112     if (GETID3_OS_ISWINDOWS) {
0113 
0114       if (file_exists(GETID3_HELPERAPPSDIR.'metaflac.exe')) {
0115         // To at least see if there was a problem, compare file modification timestamps before and after writing
0116         clearstatcache();
0117         $timestampbeforewriting = filemtime($this->filename);
0118 
0119         $commandline = GETID3_HELPERAPPSDIR.'metaflac.exe --remove-all-tags "'.$this->filename.'" 2>&1';
0120         $metaflacError = `$commandline`;
0121 
0122         if (empty($metaflacError)) {
0123           clearstatcache();
0124           if ($timestampbeforewriting == filemtime($this->filename)) {
0125             $metaflacError = 'File modification timestamp has not changed - it looks like the tags were not deleted';
0126           }
0127         }
0128       } else {
0129         $metaflacError = 'metaflac.exe not found in '.GETID3_HELPERAPPSDIR;
0130       }
0131 
0132     } else {
0133 
0134       // It's simpler on *nix
0135       $commandline = 'metaflac --remove-all-tags "'.$this->filename.'" 2>&1';
0136       $metaflacError = `$commandline`;
0137 
0138     }
0139 
0140     ignore_user_abort($oldignoreuserabort);
0141 
0142     if (!empty($metaflacError)) {
0143       $this->errors[] = 'System call to metaflac failed with this message returned: '."\n\n".$metaflacError;
0144       return false;
0145     }
0146     return true;
0147   }
0148 
0149 
0150   public function CleanmetaflacName($originalcommentname) {
0151     // A case-insensitive field name that may consist of ASCII 0x20 through 0x7D, 0x3D ('=') excluded.
0152     // ASCII 0x41 through 0x5A inclusive (A-Z) is to be considered equivalent to ASCII 0x61 through
0153     // 0x7A inclusive (a-z).
0154 
0155     // replace invalid chars with a space, return uppercase text
0156     // Thanks Chris Bolt <chris-getid3Øbolt*cx> for improving this function
0157     // note: *reg_replace() replaces nulls with empty string (not space)
0158     return strtoupper(preg_replace('#[^ -<>-}]#', ' ', str_replace("\x00", ' ', $originalcommentname)));
0159 
0160   }
0161 
0162 }