source: extensions/charlies_content/getid3/getid3/write.id3v1.php @ 3544

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

Change: getid3 upgraded to -> 1.7.9

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 4.0 KB
Line 
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.id3v1.php                                             //
11// module for writing ID3v1 tags                               //
12// dependencies: module.tag.id3v1.php                          //
13//                                                            ///
14/////////////////////////////////////////////////////////////////
15
16getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.tag.id3v1.php', __FILE__, true);
17
18class getid3_write_id3v1
19{
20        var $filename;
21        var $tag_data;
22        var $warnings = array(); // any non-critical errors will be stored here
23        var $errors   = array(); // any critical errors will be stored here
24
25        function getid3_write_id3v1() {
26                return true;
27        }
28
29        function WriteID3v1() {
30                if ((filesize($this->filename) >= (pow(2, 31) - 128)) || (filesize($this->filename) < 0)) {
31                        $this->errors[] = 'Unable to write ID3v1 because file is larger than 2GB';
32                        return false;
33                }
34
35                // File MUST be writeable - CHMOD(646) at least
36                if (is_writeable($this->filename)) {
37                        if ($fp_source = @fopen($this->filename, 'r+b')) {
38
39                                fseek($fp_source, -128, SEEK_END);
40                                if (fread($fp_source, 3) == 'TAG') {
41                                        fseek($fp_source, -128, SEEK_END); // overwrite existing ID3v1 tag
42                                } else {
43                                        fseek($fp_source, 0, SEEK_END);    // append new ID3v1 tag
44                                }
45                                $this->tag_data['track'] = (isset($this->tag_data['track']) ? $this->tag_data['track'] : (isset($this->tag_data['track_number']) ? $this->tag_data['track_number'] : (isset($this->tag_data['tracknumber']) ? $this->tag_data['tracknumber'] : '')));
46
47                                $new_id3v1_tag_data = getid3_id3v1::GenerateID3v1Tag(
48                                                                                                                @$this->tag_data['title'],
49                                                                                                                @$this->tag_data['artist'],
50                                                                                                                @$this->tag_data['album'],
51                                                                                                                @$this->tag_data['year'],
52                                                                                                                @$this->tag_data['genreid'],
53                                                                                                                @$this->tag_data['comment'],
54                                                                                                                @$this->tag_data['track']);
55                                fwrite($fp_source, $new_id3v1_tag_data, 128);
56                                fclose($fp_source);
57                                return true;
58
59                        } else {
60                                $this->errors[] = 'Could not open '.$this->filename.' mode "r+b"';
61                                return false;
62                        }
63                }
64                $this->errors[] = 'File is not writeable: '.$this->filename;
65                return false;
66        }
67
68        function FixID3v1Padding() {
69                // ID3v1 data is supposed to be padded with NULL characters, but some taggers incorrectly use spaces
70                // This function rewrites the ID3v1 tag with correct padding
71
72                // Initialize getID3 engine
73                $getID3 = new getID3;
74                $ThisFileInfo = $getID3->analyze($this->filename);
75                if ($ThisFileInfo['filesize'] >= (pow(2, 31) - 128)) {
76                        // cannot write tags on files > 2GB
77                        return false;
78                }
79                if (isset($ThisFileInfo['tags']['id3v1'])) {
80                        foreach ($ThisFileInfo['tags']['id3v1'] as $key => $value) {
81                                $id3v1data[$key] = implode(',', $value);
82                        }
83                        $this->tag_data = $id3v1data;
84                        return $this->WriteID3v1();
85                }
86                return false;
87        }
88
89        function RemoveID3v1() {
90                if ($ThisFileInfo['filesize'] >= pow(2, 31)) {
91                        $this->errors[] = 'Unable to write ID3v1 because file is larger than 2GB';
92                        return false;
93                }
94
95                // File MUST be writeable - CHMOD(646) at least
96                if (is_writeable($this->filename)) {
97                        if ($fp_source = @fopen($this->filename, 'r+b')) {
98
99                                fseek($fp_source, -128, SEEK_END);
100                                if (fread($fp_source, 3) == 'TAG') {
101                                        ftruncate($fp_source, filesize($this->filename) - 128);
102                                } else {
103                                        // no ID3v1 tag to begin with - do nothing
104                                }
105                                fclose($fp_source);
106                                return true;
107
108                        } else {
109                                $this->errors[] = 'Could not open '.$this->filename.' mode "r+b"';
110                        }
111                } else {
112                        $this->errors[] = $this->filename.' is not writeable';
113                }
114                return false;
115        }
116
117}
118
119?>
Note: See TracBrowser for help on using the repository browser.