PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/getid3/write.flac.php

http://pumukit.googlecode.com/
PHP | 155 lines | 84 code | 40 blank | 31 comment | 16 complexity | 1409954eb94c6a598cb6fdce7b3e4570 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP version 5 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 2002-2006 James Heinrich, Allan Hansen |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2 of the GPL license, |
  8. // | that is bundled with this package in the file license.txt and is |
  9. // | available through the world-wide-web at the following url: |
  10. // | http://www.gnu.org/copyleft/gpl.html |
  11. // +----------------------------------------------------------------------+
  12. // | getID3() - http://getid3.sourceforge.net or http://www.getid3.org |
  13. // +----------------------------------------------------------------------+
  14. // | Authors: James Heinrich <info?getid3*org> |
  15. // | Allan Hansen <ah?artemis*dk> |
  16. // +----------------------------------------------------------------------+
  17. // | write.flac.php |
  18. // | writing module for flac tags |
  19. // | dependencies: metaflac binary. |
  20. // +----------------------------------------------------------------------+
  21. //
  22. // $Id: write.flac.php,v 1.9 2006/12/03 20:02:25 ah Exp $
  23. class getid3_write_flac extends getid3_handler_write
  24. {
  25. public $comments = array ();
  26. public function __construct($filename) {
  27. if (ini_get('safe_mode')) {
  28. throw new getid3_exception('PHP running in Safe Mode (backtick operator not available). Cannot call metaflac binary.');
  29. }
  30. static $initialized;
  31. if (!$initialized) {
  32. // check existance and version of metaflac
  33. if (!ereg('^metaflac ([0-9]+\.[0-9]+\.[0-9]+)', `metaflac --version`, $r)) {
  34. throw new getid3_exception('Fatal: metaflac binary not available.');
  35. }
  36. if (strnatcmp($r[1], '1.1.1') == -1) {
  37. throw new getid3_exception('Fatal: metaflac version 1.1.1 or newer is required, available version: ' . $r[1] . '.');
  38. }
  39. $initialized = true;
  40. }
  41. parent::__construct($filename);
  42. }
  43. public function read() {
  44. // read info with metaflac
  45. if (!$info = trim(`metaflac --no-utf8-convert --export-tags-to=- "$this->filename"`)) {
  46. return;
  47. }
  48. // process info
  49. foreach (explode("\n", $info) as $line) {
  50. $pos = strpos($line, '=');
  51. $key = strtolower(substr($line, 0, $pos));
  52. $value = substr($line, $pos+1);
  53. $this->comments[$key][] = $value;
  54. }
  55. // convert single element arrays to string
  56. foreach ($this->comments as $key => $value) {
  57. if (sizeof($value) == 1) {
  58. $this->comments[$key] = $value[0];
  59. }
  60. }
  61. return true;
  62. }
  63. public function write() {
  64. // create temp file with new comments
  65. $temp_filename = tempnam('*', 'getID3');
  66. if (!$fp = @fopen($temp_filename, 'wb')) {
  67. throw new getid3_exception('Could not write temporary file.');
  68. }
  69. fwrite($fp, $this->generate_tag());
  70. fclose($fp);
  71. // write comments
  72. $this->save_permissions();
  73. if ($error = `metaflac --no-utf8-convert --remove-all-tags --import-tags-from="$temp_filename" "$this->filename" 2>&1`) {
  74. throw new getid3_exception('Fatal: metaflac returned error: ' . $error);
  75. }
  76. $this->restore_permissions();
  77. // success
  78. @unlink($temp_filename);
  79. return true;
  80. }
  81. protected function generate_tag() {
  82. if (!$this->comments) {
  83. throw new getid3_exception('Cannot write empty tag, use remove() instead.');
  84. }
  85. $result = '';
  86. foreach ($this->comments as $key => $values) {
  87. // A case-insensitive FLAC field name that may consist of ASCII 0x20 through 0x7D, 0x3D ('=') excluded.
  88. // ASCII 0x41 through 0x5A inclusive (A-Z) is to be considered equivalent to ASCII 0x61 through 0x7A inclusive (a-z).
  89. if (preg_match("/[^\x20-\x7D]|\x3D/", $key)) {
  90. throw new getid3_exception('Field name "' . $key . '" contains invalid character(s).');
  91. }
  92. $key = strtolower($key);
  93. if (!is_array($values)) {
  94. $values = array ($values);
  95. }
  96. foreach ($values as $value) {
  97. if (strstr($value, "\n") || strstr($value, "\r")) {
  98. throw new getid3_exception('Multi-line comments not supported (value contains \n or \r)');
  99. }
  100. $result .= $key . '=' . $value . "\n";
  101. }
  102. }
  103. return $result;
  104. }
  105. public function remove() {
  106. $this->save_permissions();
  107. if ($error = `metaflac --remove-all-tags "$this->filename" 2>&1`) {
  108. throw new getid3_exception('Fatal: metaflac returned error: ' . $error);
  109. }
  110. $this->restore_permissions();
  111. // success when removing non-existant tag
  112. return true;
  113. }
  114. }
  115. ?>