source: extensions/charlies_content/getid3/getid3/write.vorbis.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.8 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.vorbis.php                                                     |
18// | writing module for vorbis tags                                       |
19// | dependencies: vorbiscomment binary.                                  |
20// +----------------------------------------------------------------------+
21//
22// $Id: write.vorbis.php 3318 2009-05-20 21:54:10Z vdigital $
23
24
25class getid3_write_vorbis 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 vorbiscomment binary.');
35        }
36       
37        static $initialized;
38        if (!$initialized) {
39           
40            // check existance and version of vorbiscomment
41            if (!ereg('^Vorbiscomment ([0-9]+\.[0-9]+\.[0-9]+)', `vorbiscomment --version 2>&1`, $r)) {
42                throw new getid3_exception('Fatal: vorbiscomment binary not available.');
43            }
44            if (strnatcmp($r[1], '1.0.0') == -1) {
45                throw new getid3_exception('Fatal: vorbiscomment version 1.0.0 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 vorbiscomment
58        if (!$info = trim(`vorbiscomment -l "$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 = `vorbiscomment -w --raw -c "$temp_filename" "$this->filename" 2>&1`) {
97            throw new getid3_exception('Fatal: vorbiscomment 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 vobiscomment 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       
138        return $result;
139    }   
140       
141   
142    public function remove() {
143       
144        // create temp file with new comments
145        $temp_filename = tempnam('*', 'getID3');
146        if (!$fp = @fopen($temp_filename, 'wb')) {
147            throw new getid3_exception('Could not write temporary file.');
148        }
149        fwrite($fp, '');
150        fclose($fp);
151       
152        // write comments
153        $this->save_permissions();
154        if ($error = `vorbiscomment -w --raw -c "$temp_filename" "$this->filename" 2>&1`) {
155            throw new getid3_exception('Fatal: vorbiscomment returned error: ' . $error);         
156        }
157        $this->restore_permissions();
158       
159        // success when removing non-existant tag
160        @unlink($temp_filename);
161        return true;
162    }
163
164}
165
166?>
Note: See TracBrowser for help on using the repository browser.