-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunction.php
137 lines (130 loc) · 9.51 KB
/
function.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
class aplayer_class
{
function parseCallback($post, $config) {
$api = self::check_https($config->api); $js = '';
$pattern = self::get_shortcode_regex(array('aplayer'));
preg_match_all("/$pattern/",$post,$matches);
if (empty($matches[0])) return $post.'<div id="apajax" style="display:none;"></div>';
for ($i=0;$i<count($matches[0]);$i++) {
if ($matches[1][$i] == '[' and $matches[6][$i] == ']') {
$ap["#ap#$i#"] = substr($matches[0][$i], 1, -1);
$out = empty($out) ? self::str_replace_once($matches[0][$i], "#ap#$i#", $post) : self::str_replace_once($matches[0][$i], "#ap#$i#", $out);
} else {
$data = array('id' => md5(self::getUniqueId()));
$atts = self::shortcode_parse_atts(self::str_replace_nbsp($matches[3][$i]));
$data['narrow'] = isset($atts['narrow']) ? self::str2bool($atts['narrow']) : (bool)$config->narrow;
$data['autoplay'] = isset($atts['autoplay']) ? self::str2bool($atts['autoplay']) : (bool)$config->autoplay;
$data['mutex'] = isset($atts['mutex']) ? self::str2bool($atts['mutex']) : (bool)$config->mutex;
$data['theme'] = isset($atts['theme']) ? $atts['theme'] : $config->theme;
$data['mode'] = isset($atts['mode']) ? $atts['mode'] : ($config->mode==1 ? 'random' : ($config->mode==2 ? 'single' : ($config->mode==3 ? 'circulation' : 'order')));
$data['preload'] = isset($atts['preload']) ? $atts['preload'] : $config->preload==1 ? 'metadata' : ($config->preload==2 ? 'none' : 'auto');
isset($atts['listmaxheight']) ? $data['listmaxheight'] = $atts['listmaxheight'] : 0;
$data['music'] = array();
if ($matches[4][$i] != '/' && $matches[5][$i]) {
$regex = self::get_shortcode_regex(array('mp3'));
$content = self::str_replace_nbsp($matches[5][$i]);
if ((false !== strpos($content, '[')) && preg_match_all("/$regex/", $content , $all)) {
foreach ($all[0] as $k=>$v){
$atts = self::shortcode_parse_atts($all[3][$k]);
if (isset($atts['url'])) {
$tmp = array('url' => $atts['url']);
$tmp['pic'] = isset($atts['cover']) ? $atts['cover'] : (isset($atts['pic']) ? $atts['pic'] : '');
$data['showlrc'] = isset($atts['lrc']) ? (($tmp['lrc'] = $atts['lrc']) ? 3 : 3) :
(($tmp['lrc'] = (preg_match('/\[(lrc)](.*?)\[\/\\1]/si', $all[5][$k], $lrc)) && $lrc[2] ?
$lrc[2] : "[00:00.00]暂无歌词\n[99:00.00] ") ? 1 : 1);
$tmp['title'] = isset($atts['title']) ? $atts['title'] : 'Unknown';
$tmp['author'] = isset($atts['author']) ? $atts['author'] : (isset($atts['artist']) ? $atts['artist'] : 'Unknown');
$data['music'][] = $tmp;
} elseif (isset($atts['id']))
$data['showlrc'] = ($data['music'][] = ($t = json_decode(self::curl($api.'?id='.(int)$atts['id'].'.song'), 1)) ? $t[0] : $t[0]) ? 3 : 3;
} $data['music'] = count($data['music']) == 1 ? $data['music'][0] : $data['music'];
}
} else {
if (isset($atts['id'])) {
$data['showlrc'] = 3; $mix = $config->mix ? '.2' : '';
switch ($atts['type']) {
case 'collect': $data['music'] = json_decode(self::curl($api.'?id='.$atts['id'].'.collect'.$mix), 1); break;
case 'artist': $data['music'] = json_decode(self::curl($api.'?id='.$atts['id'].'.artist'.$mix), 1); break;
case 'album': $data['music'] = json_decode(self::curl($api.'?id='.$atts['id'].'.album'.$mix), 1); break;
default: $data['music'] = json_decode(self::curl($api.'?id='.$atts['id'].'.song'.$mix), 1); break;
}
} else {
isset($atts['url']) ? $data['music']['url'] = $atts['url'] : 0;
isset($atts['title']) ? $data['music']['title'] = $atts['title'] : 0;
isset($atts['artist']) ? $data['music']['author'] = $atts['artist'] :
(isset($atts['author']) ? $data['music']['author'] = $atts['author'] : 0);
$data['music']['pic'] = isset($atts['cover']) ? $atts['cover'] : (isset($atts['pic']) ? $atts['pic'] : '');
$data['showlrc'] = isset($atts['lrc']) ? 3 : 1;
isset($atts['lrc']) ? $data['music']['lrc'] = $atts['lrc'] : $data['showlrc'] = 0;
}
}
$data['showlrc'] = isset($atts['lrc']) && $atts['lrc']=='false' ? 0 : $data['showlrc'];
if ($data['music']) $js .= "\n".'APlayerOptions.push('.self::json_encode_pretty($data).');';
$out = empty($out) ?
self::str_replace_once($matches[0][$i], '<div id="ap'.$data['id'].'" class="aplayer"></div>', $post):
self::str_replace_once($matches[0][$i], '<div id="ap'.$data['id'].'" class="aplayer"></div>', $out);
}
}
$out .= "<div id=\"apajax\" style=\"display:none;\">$js\n</div>";
if (isset($ap)) foreach ($ap as $k => $v) $out = str_replace($k, $v, $out); return $out;
}
function json_encode_pretty($src) {
if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
return json_encode($src, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
} else {
$src = urldecode(json_encode(self::json_encode_unescaped($src)));
$ret = ''; $pos = 0; $newline = "\n"; $prevchar = '';
$length = strlen($src); $indent = ' '; $outofquotes = true;
for ($i=0; $i<=$length; $i++) {
$char = substr($src, $i, 1);
if ($char=='"' && $prevchar!='\\') $outofquotes = !$outofquotes;
elseif (($char=='}' || $char==']') && $outofquotes) {
$ret .= $newline; $pos --; for ($j=0; $j<$pos; $j++) $ret .= $indent; }
$ret .= $char;
if (($char==',' || $char=='{' || $char=='[') && $outofquotes) {
$ret .= $newline; if ($char=='{' || $char=='[') $pos ++; for ($j=0; $j<$pos; $j++) $ret .= $indent; }
$prevchar = $char;
}
return str_replace(array('":', '"0"', '"1"', '"2"', '"3"', '""'), array('": ', '0', '1', '2', '3', '0'), $ret);
} }
function json_encode_unescaped($src) {
if (is_array($src)) foreach ($src as $key => $val)
$out[urlencode($key)] = self::json_encode_unescaped($val);
else $out = urlencode($src); return $out; }
function shortcode_parse_atts($text) {
$atts = array();
$pattern = '/([\w-]+)\s*=\s*"([^"]*)"(?:\s|$)|([\w-]+)\s*=\s*\'([^\']*)\'(?:\s|$)|([\w-]+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|(\S+)(?:\s|$)/';
$text = preg_replace("/[\x{00a0}\x{200b}]+/u", " ", $text);
if (preg_match_all($pattern, $text, $match, PREG_SET_ORDER)) {
foreach ($match as $m) {
if (!empty($m[1])) $atts[strtolower($m[1])] = stripcslashes($m[2]);
elseif (!empty($m[3])) $atts[strtolower($m[3])] = stripcslashes($m[4]);
elseif (!empty($m[5])) $atts[strtolower($m[5])] = stripcslashes($m[6]);
elseif (isset($m[7]) && strlen($m[7])) $atts[] = stripcslashes($m[7]);
elseif (isset($m[8])) $atts[] = stripcslashes($m[8]); }
foreach ($atts as &$value) if (false !== strpos($value, '<')) if (1 !== preg_match('/^[^<]*+(?:<[^>]*+>[^<]*+)*+$/', $value)) $value = '';
} else $atts = ltrim($text);
return $atts;
}
function get_shortcode_regex($tagnames = null) {
return '\\[(\\[?)('.join('|', array_map('preg_quote', $tagnames)).
')(?![\\w-])([^\\]\\/]*(?:\\/(?!\\])[^\\]\\/]*)*?)(?:(\\/)\\]|\\](?:([^\\[]*+(?:\\[(?!\\/\\2\\])[^\\[]*+)*+)\\[\\/\\2\\])?)(\\]?)'; }
function curl($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
$result = curl_exec($curl); curl_close($curl); return $result; }
function check_https($url) { return ((isset($_SERVER['HTTPS']) and $_SERVER['HTTPS']=='on') or
(isset($_SERVER['HTTP_X_FORWARDED_PROTO']) and $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ?
str_replace('http://','https://',$url) : str_replace('https://','http://',$url); }
static $playerID = 0; function getUniqueId() { return self::$playerID++; }
function str2bool($str) { return $str == 'true' ? true : ($str == 'false' ? false : $str); }
function str_replace_nbsp($str) { return strip_tags(htmlspecialchars_decode(str_replace(' ',' ',$str))); }
function str_replace_once($n, $r, $h) { return ($p = strpos($h, $n)) === false ? $h : substr_replace($h, $r, $p, strlen($n)); }
}