-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMusicTag.php
120 lines (101 loc) · 4.14 KB
/
MusicTag.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
* This file is part of mp3 Browser.
*
* This is free software: you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* You should have received a copy of the GNU General Public License (V2) along with this. If not,
* see <http://www.gnu.org/licenses/>.
*
* Previous copyright likely held by others such as Jon Hollis, Luke Collymore, as associated with
* dotcomdevelopment.com.
* Copyright 2012-'13 Totaal Software (www.totaalsoftware.com).
*/
defined("_JEXEC") or die("Restricted access");
// all the regular expression stuff is defined here, allowing inter-constant reuse
define("PARAM_PATTERN_PART", "\s+(\w+)=\"(.*?)\"");
define("PARAM_PATTERN", "#" . PARAM_PATTERN_PART . "#");
define("MUSIC_PATTERN", "#{music((" . PARAM_PATTERN_PART . ")*)\s*}(.*?){/music}#");
define("PARAM_PATTERN_GROUP_KEY", "1");
define("PARAM_PATTERN_GROUP_VALUE", "2");
define("MUSIC_PATTERN_GROUP_PARAMETERS", "1");
define("MUSIC_PATTERN_GROUP_PATHTRAIL", "5");
class MusicTag {
private $originalFullTag;
private $replacementContent;
private $configuration;
private $pathTrail;
public function __construct($originalFullTag, Configuration $configuration = null) {
$this->originalFullTag = $originalFullTag;
$this->parsePathTrail();
if ($configuration) {
$this->setConfiguration($configuration);
}
}
public function addConfiguration($configuration) {
$this->configuration = clone $configuration;
$this->parseParametersIntoConfiguration();
}
/**
* Get a string representation. Introduced to support array_unique comparisions
* @return string string representation of music tag
*/
public function __toString() {
return $this->originalFullTag;
}
private function parseParametersIntoConfiguration() {
$parametersBlock = $this->getParametersBlock();
preg_match_all(PARAM_PATTERN, $parametersBlock, $matches, PREG_PATTERN_ORDER);
$keys = $matches[PARAM_PATTERN_GROUP_KEY];
$values = $matches[PARAM_PATTERN_GROUP_VALUE];
for ($index = 0; $index < count($keys); $index++) {
if (!$this->configuration->exists($keys[$index])
|| $this->configuration->isConfigurationOverrideAllowed()) {
$this->configuration->set($keys[$index], $values[$index]);
}
}
}
private function getParametersBlock() {
preg_match_all(MUSIC_PATTERN, $this->originalFullTag, $matches, PREG_PATTERN_ORDER);
if (count($matches[MUSIC_PATTERN_GROUP_PARAMETERS]) != 1) {
$message = JText::_("PLG_MP3BROWSER_ILLEGALPATTERN_PARAMETERS");
$message .= ": ";
$message .= count($matches[MUSIC_PATTERN_GROUP_PARAMETERS]);
throw new Exception($message);
}
return $matches[MUSIC_PATTERN_GROUP_PARAMETERS][0];
}
private function parsePathTrail() {
preg_match_all(MUSIC_PATTERN, $this->originalFullTag, $matches, PREG_PATTERN_ORDER);
if (count($matches[MUSIC_PATTERN_GROUP_PATHTRAIL]) != 1) {
$message = JText::_("PLG_MP3BROWSER_ILLEGALPATTERN_MUSICTAGS");
$message .= ": ";
$message .= count($matches[MUSIC_PATTERN_GROUP_PATHTRAIL]);
throw new Exception($message);
}
$this->pathTrail = $matches[MUSIC_PATTERN_GROUP_PATHTRAIL][0];
}
public function getPathTrail() {
return $this->pathTrail;
}
public function getFullTag() {
return $this->originalFullTag;
}
public function setReplacementContent($replacementContent) {
$this->replacementContent = $replacementContent;
}
public function getReplacementContent() {
return $this->replacementContent;
}
public function getConfiguration() {
return $this->configuration;
}
public function getPageNumber() {
return $this->configuration->get("pageNumber", 0);
}
public function getOffset() {
return $this->configuration->get("offset", 0);
}
}