1<?php
2
3/////////////////////////////////////////////////////////////////
4/// getID3() by James Heinrich <info@getid3.org>               //
5//  available at https://github.com/JamesHeinrich/getID3       //
6//            or https://www.getid3.org                        //
7//            or http://getid3.sourceforge.net                 //
8//  see readme.txt for more details                            //
9/////////////////////////////////////////////////////////////////
10//                                                             //
11// write.id3v1.php                                             //
12// module for writing ID3v1 tags                               //
13// dependencies: module.tag.id3v1.php                          //
14//                                                            ///
15/////////////////////////////////////////////////////////////////
16
17if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
18	exit;
19}
20getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.tag.id3v1.php', __FILE__, true);
21
22class getid3_write_id3v1
23{
24	/**
25	 * @var string
26	 */
27	public $filename;
28
29	/**
30	 * @var int
31	 */
32	public $filesize;
33
34	/**
35	 * @var array
36	 */
37	public $tag_data;
38
39	/**
40	 * Any non-critical errors will be stored here.
41	 *
42	 * @var array
43	 */
44	public $warnings = array();
45
46	/**
47	 * Any critical errors will be stored here.
48	 *
49	 * @var array
50	 */
51	public $errors   = array();
52
53	public function __construct() {
54	}
55
56	/**
57	 * @return bool
58	 */
59	public function WriteID3v1() {
60		// File MUST be writeable - CHMOD(646) at least
61		if (!empty($this->filename) && is_readable($this->filename) && getID3::is_writable($this->filename) && is_file($this->filename)) {
62			$this->setRealFileSize();
63			if (($this->filesize <= 0) || !getid3_lib::intValueSupported($this->filesize)) {
64				$this->errors[] = 'Unable to WriteID3v1('.$this->filename.') because filesize ('.$this->filesize.') is larger than '.round(PHP_INT_MAX / 1073741824).'GB';
65				return false;
66			}
67			if ($fp_source = fopen($this->filename, 'r+b')) {
68				fseek($fp_source, -128, SEEK_END);
69				if (fread($fp_source, 3) == 'TAG') {
70					fseek($fp_source, -128, SEEK_END); // overwrite existing ID3v1 tag
71				} else {
72					fseek($fp_source, 0, SEEK_END);    // append new ID3v1 tag
73				}
74				$this->tag_data['track_number'] = (isset($this->tag_data['track_number']) ? $this->tag_data['track_number'] : '');
75
76				$new_id3v1_tag_data = getid3_id3v1::GenerateID3v1Tag(
77														(isset($this->tag_data['title']       ) ? $this->tag_data['title']        : ''),
78														(isset($this->tag_data['artist']      ) ? $this->tag_data['artist']       : ''),
79														(isset($this->tag_data['album']       ) ? $this->tag_data['album']        : ''),
80														(isset($this->tag_data['year']        ) ? $this->tag_data['year']         : ''),
81														(isset($this->tag_data['genreid']     ) ? $this->tag_data['genreid']      : ''),
82														(isset($this->tag_data['comment']     ) ? $this->tag_data['comment']      : ''),
83														(isset($this->tag_data['track_number']) ? $this->tag_data['track_number'] : ''));
84				fwrite($fp_source, $new_id3v1_tag_data, 128);
85				fclose($fp_source);
86				return true;
87
88			} else {
89				$this->errors[] = 'Could not fopen('.$this->filename.', "r+b")';
90				return false;
91			}
92		}
93		$this->errors[] = 'File is not writeable: '.$this->filename;
94		return false;
95	}
96
97	/**
98	 * @return bool
99	 */
100	public function FixID3v1Padding() {
101		// ID3v1 data is supposed to be padded with NULL characters, but some taggers incorrectly use spaces
102		// This function rewrites the ID3v1 tag with correct padding
103
104		// Initialize getID3 engine
105		$getID3 = new getID3;
106		$getID3->option_tag_id3v2  = false;
107		$getID3->option_tag_apetag = false;
108		$getID3->option_tags_html  = false;
109		$getID3->option_extra_info = false;
110		$getID3->option_tag_id3v1  = true;
111		$ThisFileInfo = $getID3->analyze($this->filename);
112		if (isset($ThisFileInfo['tags']['id3v1'])) {
113			$id3v1data = array();
114			foreach ($ThisFileInfo['tags']['id3v1'] as $key => $value) {
115				$id3v1data[$key] = implode(',', $value);
116			}
117			$this->tag_data = $id3v1data;
118			return $this->WriteID3v1();
119		}
120		return false;
121	}
122
123	/**
124	 * @return bool
125	 */
126	public function RemoveID3v1() {
127		// File MUST be writeable - CHMOD(646) at least
128		if (!empty($this->filename) && is_readable($this->filename) && getID3::is_writable($this->filename) && is_file($this->filename)) {
129			$this->setRealFileSize();
130			if (($this->filesize <= 0) || !getid3_lib::intValueSupported($this->filesize)) {
131				$this->errors[] = 'Unable to RemoveID3v1('.$this->filename.') because filesize ('.$this->filesize.') is larger than '.round(PHP_INT_MAX / 1073741824).'GB';
132				return false;
133			}
134			if ($fp_source = fopen($this->filename, 'r+b')) {
135
136				fseek($fp_source, -128, SEEK_END);
137				if (fread($fp_source, 3) == 'TAG') {
138					ftruncate($fp_source, $this->filesize - 128);
139				} else {
140					// no ID3v1 tag to begin with - do nothing
141				}
142				fclose($fp_source);
143				return true;
144
145			} else {
146				$this->errors[] = 'Could not fopen('.$this->filename.', "r+b")';
147			}
148		} else {
149			$this->errors[] = $this->filename.' is not writeable';
150		}
151		return false;
152	}
153
154	/**
155	 * @return bool
156	 */
157	public function setRealFileSize() {
158		if (PHP_INT_MAX > 2147483647) {
159			$this->filesize = filesize($this->filename);
160			return true;
161		}
162		// 32-bit PHP will not return correct values for filesize() if file is >=2GB
163		// but getID3->analyze() has workarounds to get actual filesize
164		$getID3 = new getID3;
165		$getID3->option_tag_id3v1  = false;
166		$getID3->option_tag_id3v2  = false;
167		$getID3->option_tag_apetag = false;
168		$getID3->option_tags_html  = false;
169		$getID3->option_extra_info = false;
170		$ThisFileInfo = $getID3->analyze($this->filename);
171		$this->filesize = $ThisFileInfo['filesize'];
172		return true;
173	}
174
175}
176