PageRenderTime 41ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/getid3/write.vorbiscomment.php

https://bitbucket.org/chamilo/chamilo/
PHP | 124 lines | 64 code | 31 blank | 29 comment | 11 complexity | 35256ffa7e532fe9408daff32a4960d0 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?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.vorbiscomment.php //
  11. // module for writing VorbisComment tags //
  12. // dependencies: /helperapps/vorbiscomment.exe //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. class getid3_write_vorbiscomment
  16. {
  17. var $filename;
  18. var $tag_data;
  19. var $warnings = array(); // any non-critical errors will be stored here
  20. var $errors = array(); // any critical errors will be stored here
  21. function __construct() {
  22. return true;
  23. }
  24. function WriteVorbisComment() {
  25. if (!ini_get('safe_mode')) {
  26. // Create file with new comments
  27. $tempcommentsfilename = tempnam('*', 'getID3');
  28. if ($fpcomments = @fopen($tempcommentsfilename, 'wb')) {
  29. foreach ($this->tag_data as $key => $value) {
  30. foreach ($value as $commentdata) {
  31. fwrite($fpcomments, $this->CleanVorbisCommentName($key).'='.$commentdata."\n");
  32. }
  33. }
  34. fclose($fpcomments);
  35. } else {
  36. $this->errors[] = 'failed to open temporary tags file "'.$tempcommentsfilename.'", tags not written';
  37. return false;
  38. }
  39. $oldignoreuserabort = ignore_user_abort(true);
  40. if (GETID3_OS_ISWINDOWS) {
  41. if (file_exists(GETID3_HELPERAPPSDIR.'vorbiscomment.exe')) {
  42. //$commandline = '"'.GETID3_HELPERAPPSDIR.'vorbiscomment.exe" -w --raw -c "'.$tempcommentsfilename.'" "'.str_replace('/', '\\', $this->filename).'"';
  43. // vorbiscomment works fine if you copy-paste the above commandline into a command prompt,
  44. // but refuses to work with `backtick` if there are "doublequotes" present around BOTH
  45. // the metaflac pathname and the target filename. For whatever reason...??
  46. // The solution is simply ensure that the metaflac pathname has no spaces,
  47. // and therefore does not need to be quoted
  48. // On top of that, if error messages are not always captured properly under Windows
  49. // To at least see if there was a problem, compare file modification timestamps before and after writing
  50. clearstatcache();
  51. $timestampbeforewriting = filemtime($this->filename);
  52. $commandline = GETID3_HELPERAPPSDIR.'vorbiscomment.exe -w --raw -c "'.$tempcommentsfilename.'" "'.$this->filename.'" 2>&1';
  53. $VorbiscommentError = `$commandline`;
  54. if (empty($VorbiscommentError)) {
  55. clearstatcache();
  56. if ($timestampbeforewriting == filemtime($this->filename)) {
  57. $VorbiscommentError = 'File modification timestamp has not changed - it looks like the tags were not written';
  58. }
  59. }
  60. } else {
  61. $VorbiscommentError = 'vorbiscomment.exe not found in '.GETID3_HELPERAPPSDIR;
  62. }
  63. } else {
  64. $commandline = 'vorbiscomment -w --raw -c "'.$tempcommentsfilename.'" "'.$this->filename.'" 2>&1';
  65. $VorbiscommentError = `$commandline`;
  66. }
  67. // Remove temporary comments file
  68. unlink($tempcommentsfilename);
  69. ignore_user_abort($oldignoreuserabort);
  70. if (!empty($VorbiscommentError)) {
  71. $this->errors[] = 'system call to vorbiscomment failed with message: '."\n\n".$VorbiscommentError;
  72. return false;
  73. }
  74. return true;
  75. }
  76. $this->errors[] = 'PHP running in Safe Mode (backtick operator not available) - cannot call vorbiscomment, tags not written';
  77. return false;
  78. }
  79. function DeleteVorbisComment() {
  80. $this->tag_data = array(array());
  81. return $this->WriteVorbisComment();
  82. }
  83. function CleanVorbisCommentName($originalcommentname) {
  84. // A case-insensitive field name that may consist of ASCII 0x20 through 0x7D, 0x3D ('=') excluded.
  85. // ASCII 0x41 through 0x5A inclusive (A-Z) is to be considered equivalent to ASCII 0x61 through
  86. // 0x7A inclusive (a-z).
  87. // replace invalid chars with a space, return uppercase text
  88. // Thanks Chris Bolt <chris-getid3?bolt*cx> for improving this function
  89. // note: ereg_replace() replaces nulls with empty string (not space)
  90. return strtoupper(ereg_replace('[^ -<>-}]', ' ', str_replace("\x00", ' ', $originalcommentname)));
  91. }
  92. }
  93. ?>