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.lyrics3.php                                           //
12// module for writing Lyrics3 tags                             //
13// dependencies: module.tag.lyrics3.php                        //
14//                                                            ///
15/////////////////////////////////////////////////////////////////
16
17
18class getid3_write_lyrics3
19{
20	/**
21	 * @var string
22	 */
23	public $filename;
24
25	/**
26	 * @var array
27	 */
28	public $tag_data;
29	//public $lyrics3_version = 2;       // 1 or 2
30
31	/**
32	 * Any non-critical errors will be stored here.
33	 *
34	 * @var array
35	 */
36	public $warnings        = array();
37
38	/**
39	 * Any critical errors will be stored here.
40	 *
41	 * @var array
42	 */
43	public $errors          = array();
44
45	public function __construct() {
46	}
47
48	/**
49	 * @return bool
50	 */
51	public function WriteLyrics3() {
52		$this->errors[] = 'WriteLyrics3() not yet functional - cannot write Lyrics3';
53		return false;
54	}
55
56	/**
57	 * @return bool
58	 */
59	public function DeleteLyrics3() {
60		// Initialize getID3 engine
61		$getID3 = new getID3;
62		$ThisFileInfo = $getID3->analyze($this->filename);
63		if (isset($ThisFileInfo['lyrics3']['tag_offset_start']) && isset($ThisFileInfo['lyrics3']['tag_offset_end'])) {
64			if (is_readable($this->filename) && getID3::is_writable($this->filename) && is_file($this->filename) && ($fp = fopen($this->filename, 'a+b'))) {
65
66				flock($fp, LOCK_EX);
67				$oldignoreuserabort = ignore_user_abort(true);
68
69				fseek($fp, $ThisFileInfo['lyrics3']['tag_offset_end']);
70				$DataAfterLyrics3 = '';
71				if ($ThisFileInfo['filesize'] > $ThisFileInfo['lyrics3']['tag_offset_end']) {
72					$DataAfterLyrics3 = fread($fp, $ThisFileInfo['filesize'] - $ThisFileInfo['lyrics3']['tag_offset_end']);
73				}
74
75				ftruncate($fp, $ThisFileInfo['lyrics3']['tag_offset_start']);
76
77				if (!empty($DataAfterLyrics3)) {
78					fseek($fp, $ThisFileInfo['lyrics3']['tag_offset_start']);
79					fwrite($fp, $DataAfterLyrics3, strlen($DataAfterLyrics3));
80				}
81
82				flock($fp, LOCK_UN);
83				fclose($fp);
84				ignore_user_abort($oldignoreuserabort);
85
86				return true;
87
88			} else {
89				$this->errors[] = 'Cannot fopen('.$this->filename.', "a+b")';
90				return false;
91			}
92		}
93		// no Lyrics3 present
94		return true;
95	}
96
97}
98