-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.php
More file actions
451 lines (354 loc) · 11 KB
/
helpers.php
File metadata and controls
451 lines (354 loc) · 11 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
<?php
/**
* Created by PhpStorm.
* User: brad
* Date: 1/30/15
* Time: 4:24 PM
*/
require_once("getid3/getid3/getid3.php");
$getID3 = new getID3();
function execute_sql($db, $script){
global $error;
$sqlArray = explode(';',$script);
foreach ($sqlArray as $stmt) {
if (strlen($stmt)>3 && substr(ltrim($stmt),0,2)!='/*') {
$result = mysqli_query($db, $stmt);
if (!$result) {
$error .= ' (error #'.mysqli_errno($db).') ';
$error .= mysqli_error($db);
$error .= ' statement: '.$stmt;
return $error;
}
}
}
}
// EXPECTS GLOBALS
function process_dir($dir){
global $count; // int value is 0 initially
global $file_list; // array to populate with filename list
global $error;
$dir_contents = scandir($dir);
foreach ($dir_contents as $key => $filename){
// IF ITS A DIR, RECURSE
if ( is_dir($dir.$filename) && substr($filename,0,1) != "." ) {
if(process_dir($dir.$filename."/"))
{
} else {
$error .= " can't process file: ".$dir.$filename." ";
return false;
}
} else {
}
// IF ITS A MP3 FILE, ADD TO ARRAY
if ( is_file($dir.$filename) && substr($filename,0,1) != "." ){
if ( file_extension($filename) == "mp3" ) {
$count++;
$file_list []= $dir.$filename;
} else {
}
} else {
}
}
return true;
}
function extractFromTags($path_and_file){
global $getID3;
global $error;
$ThisFileInfo = $getID3->analyze($path_and_file);
if (!array_key_exists('id3v2',$ThisFileInfo['tags'])){
$error .= ' <br/> (skipped) no id3v2 tags found in file:.'.$path_and_file;
return false;
}
// echo '<pre>';
// print_r($ThisFileInfo);
$tags = $ThisFileInfo['tags']['id3v2'];
$song = [];
// $song['filename'] = $path_and_file; <- different if using import script or recreate script
$song['duration'] = 200;
$song['comment'] = isset($tags['comment'][0])? $tags['comment'][0] : '';
$song['artist'] = $tags['artist'][0];
$song['title'] = $tags['title'][0];
$song['album'] = $tags['album'][0];
$song['track_number'] = $tags['track_number'][0];
$song['year'] = isset($tags['year'][0])? $tags['year'][0] : '';
$song['genre'] = isset($tags['genre'][0])? $tags['genre'][0] : '';
$song['composer'] = isset($tags['composer'][0] )? $tags['composer'][0] : $song['artist'];
$song['isrc'] = isset($tags['isrc'][0])? $tags['isrc'][0] : "";
$song['mood'] = isset($tags['mood'][0]) ? $tags['mood'][0] : "";
$song_track = explode('/', $song['track_number']);
$song['track_number'] = intval($song_track[0]);
$song_track = $song['track_number'];
$song['id'] = ( intval($song['isrc']) * 100 ) + $song_track;
$song['year'] = substr($song['year'],0,4);
$song['duration'] = 1000 * $ThisFileInfo['playtime_seconds'];
if( strripos($song['comment'],'ategory')){
foreach(str_split($song['comment']) as $k => $letter){
if ($letter == '3'){
$song['category'] = 3;
break;
}
if ($letter == '2'){
$song['category'] = 2;
break;
}
// else
}
} else {
$song['category'] ='';
}
// $song['title'] = str_replace('\x','',
return $song;
/*
Optional: copies data from all subarrays of [tags] into [comments] so
metadata is all available in one location for all tag formats
metainformation is always available under [tags] even if this is not called
*/
// getid3_lib::CopyTagsToComments($ThisFileInfo);
//echo $ThisFileInfo['comments_html']['artist'][0]; // artist from any/all available tag formats
//echo $ThisFileInfo['tags']['id3v2']['title'][0]; // title from ID3v2
//echo $ThisFileInfo['audio']['bitrate']; // audio bitrate
}
function correct_path_differences($string){
global $sam_path_prefix;
global $library_root;
return str_replace($library_root,$sam_path_prefix,$string);
}
function trim_fields($song){
$max_length = [
'id' => 11,
'filename' => 200,
'duration' => 11,
'artist' => 170,
'title' => 255,
'album' => 255,
'track_number' => 11,
'composer' => 100,
'isrc' => 50,
'year' => 4,
'genre' => 20,
'comment' => 10000, /// arbitrary
'mood' => 50,
'category' => 50
];
foreach($song as $k => $v){
$song[$k] = substr($v, 0, $max_length[$k]);
}
if( substr($song['filename'],-4,4) != '.mp3'){
$song['filename'] .= '.mp3';
}
return $song;
}
function ingest_song($db, $song, $playlist = false){
global $target_db_name;
global $database_error;
$replaced = array();
$replaced['artist'] = replace_accents($song['artist']);
$replaced['title'] = replace_accents($song['title']);
$replaced['album'] = replace_accents($song['album']);
$replaced['composer'] = replace_accents($song['composer']);
$replaced['genre'] = replace_accents($song['genre']);
foreach($replaced as $i => $v){
if (mb_detect_encoding($v) != 'ASCII'){
$database_error .= ' '.$i.' ('.$v.') is not latin ('.mb_detect_encoding($v).') SAM hates non-latin. ';
}
}
$query = "INSERT INTO `".$target_db_name."`.`songlist`
(`filename`,
`duration`,
`artist`,
`title`,
`album`,
`trackno`,
`composer`,
`ISRC`,
`albumyear`,
`genre`,
`info`,
`mood`)
VALUES
('".
mysqli_real_escape_string($db,mb_convert_encoding($song['filename'],'latin1') )."','".
$song['duration']."','".
mysqli_real_escape_string($db,$replaced['artist'])."','".
mysqli_real_escape_string($db,$replaced['title'])."','".
mysqli_real_escape_string($db,$replaced['album'])."','".
$song['track_number']."','".
mysqli_real_escape_string($db,$replaced['composer'])."','".
$song['isrc']."','".
$song['year']."','".
mysqli_real_escape_string($db,$replaced['genre'])."','".
mysqli_real_escape_string($db,$song['comment'])."','".
$song['mood']."')";
if( $database_error == '' && mysqli_query($db,$query) ){
$song['id'] = mysqli_insert_id($db);
echo 'imported into SAM: ✓';
return categories($song, $playlist);
} else {
$database_error .= ' '.mysqli_error($db).' query:'.$query.'. ';
echo ' imported into SAM: X';
return false;
}
}
function categories($song, $playlist = false){
global $db;
$content = array();
$categories = explode(' ',$song['mood']);
$cancon = false;
$femcon = false;
foreach($categories as $k => $v){
if(strtolower(substr($v,0,3)) == 'can'){
apply_category($song['id'], 'cancon');
$cancon = true;
}
if(strtolower(substr($v,0,3)) == 'fem'){
apply_category($song['id'], 'femcon');
$femcon = true;
}
if($cancon && $femcon){
apply_category($song['id'], 'cancon femcon');
}
}
if ($cancon && ($song['category'] == 2) ){
apply_category($song['id'], 'cancon 2');
} else if ($cancon && ($song['category'] == 3) ){
apply_category($song['id'], 'cancon 3');
}
if($playlist){
apply_category($song['id'], 'playlist');
}
return true;
}
function apply_category($id, $category){
global $sam_category;
global $target_db_name;
global $db;
$cat_id = $sam_category[$category];
$query = "INSERT INTO `".$target_db_name."`.`categorylist`
(`songID`,`categoryID`,`sortID`)
VALUES
('".
$id."','".
$cat_id."','".
"0')";
if ( mysqli_query($db, $query)){
return true;
} else {
echo mysqli_error($db).' '.$query;
return false;
}
}
function file_extension($filename){
$array = explode(".",$filename);
return $array[count($array)-1];
}
function sanitize_for_filename($string){
$string = substr($string, 0, 60);
$string = replace_accents($string);
$string = preg_replace("([^\w\s\d\-_~,;:\[\]\(\).])", '-', $string);
$string = preg_replace("([\.:;¡‐…])", '-', $string);
$string = preg_replace('/\t+/', ' ', $string);
$string = preg_replace("/\r\n|\r|\n/", ' ', $string);
return $string;
}
function replace_accents($string){
$unwanted_array = array( '‐'=> '-', '“'=>"'", '”' => "'", 'Š'=>'S', 'š'=>'s', 'Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'AE', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U',
'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c',
'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o',
'ö'=>'o', 'ø'=>'o', 'œ'=>'oe', 'ù'=>'u', 'ü'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y' );
$outstring = strtr( $string, $unwanted_array );
return $outstring;
}
/* RAW SONGLIST INSERT
(`ID`,
`filename`,
`diskID`,
`flags`,
`songtype`,
`status`,
`weight`,
`balance`,
`date_added`,
`date_played`,
`date_artist_played`,
`date_album_played`,
`date_title_played`,
`duration`,
`artist`,
`title`,
`album`,
`label`,
`pline`,
`trackno`,
`composer`,
`ISRC`,
`catalog`,
`UPC`,
`feeagency`,
`albumyear`,
`genre`,
`website`,
`buycd`,
`info`,
`lyrics`,
`picture`,
`count_played`,
`count_requested`,
`last_requested`,
`count_performances`,
`xfade`,
`bpm`,
`mood`,
`rating`,
`overlay`,
`playlimit_count`,
`playlimit_action`,
`songrights`,
`adz_listID`)
VALUES
(".$song['id'].",
".$song['filename'].",
<{diskID: 0}>,
<{flags: NNNNNNNNNN}>,
<{songtype: S}>,
<{status: 0}>,
<{weight: 50}>,
<{balance: 0}>,
<{date_added: }>,
<{date_played: }>,
<{date_artist_played: 2002-01-01 00:00:01}>,
<{date_album_played: 2002-01-01 00:00:01}>,
<{date_title_played: 2002-01-01 00:00:01}>,
<{duration: 0}>,
<{artist: }>,
<{title: }>,
<{album: }>,
<{label: }>,
<{pline: }>,
<{trackno: 0}>,
<{composer: }>,
<{ISRC: }>,
<{catalog: }>,
<{UPC: }>,
<{feeagency: }>,
<{albumyear: 0}>,
<{genre: }>,
<{website: }>,
<{buycd: }>,
<{info: }>,
<{lyrics: }>,
<{picture: }>,
<{count_played: 0}>,
<{count_requested: 0}>,
<{last_requested: 2002-01-01 00:00:01}>,
<{count_performances: 0}>,
<{xfade: }>,
<{bpm: 0}>,
<{mood: }>,
<{rating: 0}>,
<{overlay: no}>,
<{playlimit_count: 0}>,
<{playlimit_action: none}>,
<{songrights: broadcast}>,
<{adz_listID: 0}>);
*/