source: extensions/charlies_content/getid3/getid3/write.flac.php @ 3318

Last change on this file since 3318 was 3318, checked in by vdigital, 15 years ago

+ Add Charlies' content to depository

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 5.5 KB
Line 
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 3318 2009-05-20 21:54:10Z vdigital $
23
24
25class getid3_write_flac extends getid3_handler_write
26{
27   
28    public $comments = array ();
29   
30   
31    public function __construct($filename) {
32       
33        if (ini_get('safe_mode')) {
34            throw new getid3_exception('PHP running in Safe Mode (backtick operator not available). Cannot call metaflac binary.');
35        }
36       
37        static $initialized;
38        if (!$initialized) {
39           
40            // check existance and version of metaflac
41            if (!ereg('^metaflac ([0-9]+\.[0-9]+\.[0-9]+)', `metaflac --version`, $r)) {
42                throw new getid3_exception('Fatal: metaflac binary not available.');
43            }
44            if (strnatcmp($r[1], '1.1.1') == -1) {
45                throw new getid3_exception('Fatal: metaflac version 1.1.1 or newer is required, available version: ' . $r[1] . '.');
46            }
47
48            $initialized = true;
49        }
50       
51        parent::__construct($filename);
52    }
53   
54   
55    public function read() {
56       
57        // read info with metaflac
58        if (!$info = trim(`metaflac --no-utf8-convert --export-tags-to=- "$this->filename"`)) {
59            return;
60        }
61       
62        // process info
63        foreach (explode("\n", $info) as $line) {
64           
65            $pos    = strpos($line, '=');
66           
67            $key    = strtolower(substr($line, 0, $pos));
68            $value  = substr($line, $pos+1);
69
70            $this->comments[$key][] = $value;
71        }
72       
73        // convert single element arrays to string
74        foreach ($this->comments as $key => $value) {
75            if (sizeof($value) == 1) {
76                $this->comments[$key] = $value[0];
77            }
78        }
79       
80        return true;
81    }
82   
83   
84    public function write() {
85       
86        // create temp file with new comments
87        $temp_filename = tempnam('*', 'getID3');
88        if (!$fp = @fopen($temp_filename, 'wb')) {
89            throw new getid3_exception('Could not write temporary file.');
90        }
91        fwrite($fp, $this->generate_tag());
92        fclose($fp);
93       
94        // write comments
95        $this->save_permissions();
96        if ($error = `metaflac --no-utf8-convert --remove-all-tags --import-tags-from="$temp_filename" "$this->filename" 2>&1`) {
97            throw new getid3_exception('Fatal: metaflac returned error: ' . $error);         
98        }
99        $this->restore_permissions();
100       
101        // success
102        @unlink($temp_filename);
103        return true;
104    }
105
106
107    protected function generate_tag() {
108       
109        if (!$this->comments) {
110            throw new getid3_exception('Cannot write empty tag, use remove() instead.');
111        }
112
113        $result = '';
114           
115        foreach ($this->comments as $key => $values) {
116           
117            // A case-insensitive FLAC field name that may consist of ASCII 0x20 through 0x7D, 0x3D ('=') excluded.
118            // ASCII 0x41 through 0x5A  inclusive (A-Z) is to be considered equivalent to ASCII 0x61 through 0x7A inclusive (a-z).
119            if (preg_match("/[^\x20-\x7D]|\x3D/", $key)) {
120                throw new getid3_exception('Field name "' . $key . '" contains invalid character(s).');
121            }
122           
123            $key = strtolower($key);
124           
125            if (!is_array($values)) {
126                $values = array ($values);
127            }
128               
129            foreach ($values as $value) {
130                if (strstr($value, "\n") || strstr($value, "\r")) {
131                    throw new getid3_exception('Multi-line comments not supported (value contains \n or \r)');
132                }
133                $result .= $key . '=' . $value . "\n";
134            }
135        }
136       
137        return $result;
138    }   
139       
140   
141    public function remove() {
142       
143        $this->save_permissions();
144        if ($error = `metaflac --remove-all-tags "$this->filename" 2>&1`) {
145            throw new getid3_exception('Fatal: metaflac returned error: ' . $error);         
146        }
147        $this->restore_permissions();
148       
149        // success when removing non-existant tag
150        return true;
151    }
152
153}
154
155?>
Note: See TracBrowser for help on using the repository browser.