-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathupdate.php
2163 lines (1826 loc) · 83.7 KB
/
update.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
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// +------------------------------------------------------------------------+
// | O!MPD, Copyright © 2015-2021 Artur Sierzant |
// | http://www.ompd.pl |
// | |
// | |
// | netjukebox, Copyright © 2001-2012 Willem Bartels |
// | |
// | http://www.netjukebox.nl |
// | http://forum.netjukebox.nl |
// | |
// | This program 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 3 of the License, or |
// | (at your option) any later version. |
// | |
// | This program is distributed in the hope that it will be useful, |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
// | GNU General Public License for more details. |
// | |
// | You should have received a copy of the GNU General Public License |
// | along with this program. If not, see <http://www.gnu.org/licenses/>. |
// +------------------------------------------------------------------------+
// +------------------------------------------------------------------------+
// | update.php |
// +------------------------------------------------------------------------+
//set_time_limit(0);
ini_set('max_execution_time', 0);
//ini_set('memory_limit', '-1');
//$updateStage = $_GET["updateStage"];
require_once('include/initialize.inc.php');
require_once('include/cache.inc.php');
require_once('include/library.inc.php');
ignore_user_abort(true);
register_shutdown_function('fatalErrorHandle');
//prevent stopping update process after force_restart_timeout (default 180s)
//when OPcache is enabled
//ini_set('opcache.force_restart_timeout', 180000);
//exit();
cliLog("Update started");
$cfg['menu'] = 'config';
$cfg['force_filename_update'] = false;
$action = getpost('action');
$dir_to_update = getpost('dir_to_update');
cliLog('dir_to_update from URL: ' . $dir_to_update);
if (!isset($dir_to_update)) {
$dir_to_update = '';
}
else {
$dir_to_update = myDecode($dir_to_update);
if (substr($dir_to_update,-1) != "/"){
$dir_to_update = $dir_to_update . "/";
}
//setcookie('update_dir', rtrim($dir_to_update,'/'), time() + (86400 * 30 * 365), "/");
setcookie('update_dir', $dir_to_update, time() + 31536000, "/");
$cfg['force_filename_update'] = true;
}
/* if (substr($cfg['media_dir'],-1) != "/"){
$cfg['media_dir'] = $cfg['media_dir'] . "/";
} */
$flag = (int) getpost('flag');
if (PHP_SAPI == 'cli') cliUpdate();
elseif ($action == 'update') update($dir_to_update);
elseif ($action == 'imageUpdate') imageUpdate($flag);
elseif ($action == 'saveImage') saveImage($flag);
elseif ($action == 'selectImageUpload') selectImageUpload($flag);
elseif ($action == 'imageUpload') imageUpload($flag);
else message(__FILE__, __LINE__, 'error', '[b]Unsupported input value for[/b][br]action');
exit();
// +------------------------------------------------------------------------+
// | Update |
// +------------------------------------------------------------------------+
function update($dir_to_update = '') {
global $cfg, $db, $lastGenre_id, $getID3, $dirsCounter, $filesCounter, $curFilesCounter, $curDirsCounter, $last_update, $file;
authenticate('access_admin', false, true);
require_once('getid3/getid3/getid3.php');
require_once('include/play.inc.php'); // Needed for mpdUpdate()
$cfg['cli_update'] = false;
$startTime = new DateTime();
cliLog("Update start time: " . date("Ymd H:i:s"));
$path = $cfg['media_dir'];
$curFilesCounter = 0;
$curDirsCounter = 0;
$dirsCounter = 1;
$filesCounter = 0;
$prevDirsCounter = 0;
$prevFilesCounter = 0;
$dirs = array();
$lastGenre_id = 1;
$query = mysqli_query($db,'SELECT last_update FROM update_progress');
$f = mysqli_fetch_assoc($query);
$last_update = strtotime($f['last_update']);
/* $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach($objects as $name){
if ($name->isDir()) {
++$dirsCounter;
}
else {
++$filesCounter;
}
} */
//mysqli_query($db,'DELETE FROM genre');
//$lastGenre_id = 1;
// formattedNavigator
$nav = array();
$nav['name'][] = 'Configuration';
$nav['url'][] = 'config.php';
$nav['name'][] = 'Update';
require_once('include/header.inc.php');
?>
<table width="100%" cellspacing="0" cellpadding="0" class="border">
<tr class="header">
<td class="space"></td>
<td class="update_text">Update</td>
<td>Progress</td>
<td class="space"></td>
</tr>
<tr class="line"><td colspan="4"></td></tr>
<tr class="odd">
<td></td>
<td>Structure & image:</td>
<td><span id="structure"></span></td>
<td></td>
</tr>
<tr class="even">
<td></td>
<td>File info:</td>
<td><span id="fileinfo"></span></td>
<td></td>
</tr>
<tr class="odd">
<td></td>
<td>Cleanup:</td>
<td><span id="cleanup"></span></td>
<td></td>
</tr>
<tr class="even">
<td></td>
<td>Update time:</td>
<td><span id="updateTime"></span></td>
<td></td>
</tr>
</table>
<script>
hideSpinner();
var intervalId = window.setInterval(function() {
show_update_progress();
}, 500);
function show_update_progress() {
$.ajax({
type: "POST",
url: "ajax-update-progress.php",
dataType : 'json',
success : function(json) {
var s = json['structure_image'];
if (s.indexOf("fa-spin") > -1) {
if (!$("#structure").hasClass("fa-spin"))
$("#structure").html(json['structure_image']);
}
else
$("#structure").html(json['structure_image']);
s = json['file_info'];
if (s.indexOf("fa-spin") > -1) {
if (!$("#fileinfo").hasClass("fa-spin"))
$("#fileinfo").html(json['file_info']);
}
else
$("#fileinfo").html(json['file_info']);
s = json['cleanup'];
if (s.indexOf("fa-spin") > -1) {
if (!$("#cleanup").hasClass("fa-spin"))
$("#cleanup").html(json['cleanup']);
}
else
$("#cleanup").html(json['cleanup']);
s = json['update_time'];
if (s.indexOf("Update error") > -1) {
$("#updateTime").html(json['update_time']);
clearInterval(intervalId);
}
else
$("#updateTime").html(json['update_time']);
}
});
}
</script>
<?php
@ob_flush();
flush();
$cfg['footer'] = 'dynamic';
require('include/footer.inc.php');
/* $getID3 = new getID3;
//initial settings for getID3:
include 'include/getID3init.inc.php';
*/
$result = mysqli_query($db,'SELECT * FROM update_progress');
if (mysqli_num_rows($result)==0) {
mysqli_query($db,'INSERT INTO update_progress (update_status, structure_image, file_info, cleanup, update_time, last_update)
VALUES ("0", "", "", "", "", "")');
$update_status = 0;
}
else {
$row=mysqli_fetch_assoc($result);
$update_status=$row["update_status"];
}
if ($update_status == 0) {
mysqli_query($db,"update update_progress set
update_status = 1,
structure_image = '',
file_info = '',
cleanup = '',
update_time = '',
last_update = 'Update in progress..'");
sleep(1);
//redirect back to update.php to use ajax status update
$cfg['footer'] = 'close';
echo ('<script>window.location.href="update.php?action=update&sign=' . $cfg['sign'] . '"</script>');
@ob_flush();
flush();
mysqli_query($db,"update update_progress set
structure_image = 'Requesting MPD update...'");
$rel_file = str_replace($cfg['media_dir'],'',$dir_to_update);
$rel_file_mpd = rtrim($rel_file,'/');
$getID3 = new getID3;
//initial settings for getID3:
include 'include/getID3init.inc.php';
mpdUpdate($rel_file_mpd);
//exit();
//@ob_flush();
//flush();
mysqli_query($db,"update update_progress set
structure_image = '<i class=\"fa fa-cog larger icon-selected fa-spin\"></i>'");
// Short sleep to prevent update problems with a previous update process that has not stopped yet.
//sleep(1);
$cfg['new_escape_char_hash'] = hmacmd5(print_r($cfg['escape_char'], true), file_get_contents(NJB_HOME_DIR . 'update.php'));
//$cfg['force_filename_update'] = ($cfg['new_escape_char_hash'] != $cfg['escape_char_hash']) ? true : false;
//$cfg['force_filename_update'] = false;
if ($cfg['image_size'] != NJB_IMAGE_SIZE || $cfg['image_quality'] != NJB_IMAGE_QUALITY) {
mysqli_query($db,'TRUNCATE TABLE bitmap');
mysqli_query($db,'UPDATE server SET value = "' . $db->real_escape_string(NJB_IMAGE_SIZE) . '" WHERE name = "image_size" LIMIT 1');
mysqli_query($db,'UPDATE server SET value = "' . $db->real_escape_string(NJB_IMAGE_QUALITY) . '" WHERE name = "image_quality" LIMIT 1');
}
if ($dir_to_update) {
//set all to updated due to prevent deleting all DB in case of previous update failure
mysqli_query($db,'UPDATE album SET updated = 1 WHERE updated <> 9');
mysqli_query($db,'UPDATE track SET updated = 1');
mysqli_query($db,'UPDATE bitmap SET updated = 1');
mysqli_query($db,'UPDATE album_id SET updated = 1 WHERE updated <> 9');
//mark only requested dir to be updated
mysqli_query($db,'UPDATE album_id SET updated = 0 WHERE updated <> 9 AND path LIKE "' . mysqli_real_escape_string($db,$dir_to_update) . '%"');
mysqli_query($db,'UPDATE album SET updated = 0 WHERE updated <> 9 AND album_id IN
(SELECT album_id FROM album_id WHERE path LIKE "' . mysqli_real_escape_string($db,$dir_to_update) . '%" )');
mysqli_query($db,'UPDATE bitmap SET updated = 0 WHERE album_id IN
(SELECT album_id FROM album_id WHERE path LIKE "' . mysqli_real_escape_string($db,$dir_to_update) . '%" )');
mysqli_query($db,'UPDATE track SET updated = 0 WHERE relative_file LIKE "' . mysqli_real_escape_string($db,$rel_file) . '%"');
cliLog('dir_to_update: ' . $dir_to_update);
}
else {
mysqli_query($db,'UPDATE album SET updated = 0 WHERE updated <> 9');
mysqli_query($db,'UPDATE track SET updated = 0');
mysqli_query($db,'UPDATE bitmap SET updated = 0');
mysqli_query($db,'UPDATE album_id SET updated = 0 WHERE updated <> 9');
}
$cfg['timer'] = 0; // force update
//recursiveScanCount_add2table($cfg['media_dir']);
//recursiveScanCount($cfg['media_dir']);
clearstatcache();
$dir_to_scan = $cfg['media_dir'];
if ($dir_to_update) {
$dir_to_scan = $dir_to_update;
}
$mediaDirs = array();
countDirectories($dir_to_scan);
//if ($dirsCounter == 1) $dirsCounter = 0;
/* $result = mysqli_query($db,"update update_progress set
update_status = 0,
update_time = '" . $updateTime . "',
last_update = '" . date('Y-m-d, H:i:s') . "'
");
exit(); */
recursiveScan($dir_to_scan);
//exit();
mysqli_query($db,'UPDATE update_progress SET structure_image = "<div class=\'out\'><div class=\'in\' style=\'width: 200px\'></div></div> 100%"');
sleep(1);
mysqli_query($db,'DELETE FROM album WHERE NOT updated');
mysqli_query($db,'DELETE FROM track WHERE NOT updated');
mysqli_query($db,'DELETE FROM bitmap WHERE NOT updated');
//mysqli_query($db,'DELETE FROM genre WHERE NOT updated');
mysqli_query($db,'UPDATE server SET value = "' . $db->real_escape_string($cfg['new_escape_char_hash']) . '" WHERE name = "escape_char_hash" LIMIT 1');
$no_image = mysqli_num_rows(mysqli_query($db,'SELECT album_id FROM bitmap WHERE flag = 0'));
$image_error = mysqli_num_rows(mysqli_query($db,'SELECT album_id FROM bitmap WHERE flag = 10'));
/* if ($no_image > 0) {
mysqli_query($db,'update update_progress set
structure_image = "<a href=\'update.php?action=imageUpdate&flag=0\'><img src=\'' . $cfg['img'] . 'small_image.png\' alt=\'\' class=\'small space\'>Update ' . $no_image . (($no_image == 1) ? ' image' : ' images') . ' from internet</a>"');
} */
$image_problems = '';
if ($no_image > 0) {
$image_problems .= ' <a href=\'statistics.php?action=noImageFront\'>No image for ' . $no_image . (($no_image == 1) ? ' folder' : ' folders') . '.</a>';
}
if ($image_error > 0) {
$image_problems .= ' <a href=\'statistics.php?action=imageError\'>Image error for ' . $image_error . (($image_error == 1) ? ' folder' : ' folders') . '.</a>';
}
if ($image_problems != '') {
mysqli_query($db,'update update_progress set
structure_image = "' . $image_problems .'"');
}
else {
mysqli_query($db,'update update_progress set
structure_image = "<i class=\"fa fa-check icon-ok \"></i> "');
}
// @ob_flush();
// flush();
mysqli_query($db,'update update_progress set
file_info = "<i class=\"fa fa-cog larger icon-selected fa-spin\"></i>"');
$cfg['timer'] = 0; // force update
cliLog("going into fileInfoLoop: " . $rel_file);
fileInfoLoop($rel_file);
mysqli_query($db,'UPDATE update_progress SET file_info = "<div class=\'out\'><div class=\'in\' style=\'width: 200px\'></div></div> 100%"');
sleep(1);
mysqli_query($db,'UPDATE update_progress SET file_info = "<i class=\"fa fa-cog larger icon-selected fa-spin\"></i> Updating genre list..."');
updateGenre();
sleep(1);
$error = mysqli_num_rows(mysqli_query($db,'SELECT error FROM track WHERE error != ""'));
if ($error > 0) {
mysqli_query($db,'update update_progress set
file_info = "<a href=\'statistics.php?action=fileError\'><i class=\"fa fa-minus-circle icon-nok\"></i> ' . $error . (($error == 1) ? ' error' : ' errors') . '</a>"');
}
else {
mysqli_query($db,'update update_progress set
file_info = "<i class=\"fa fa-check icon-ok\"></i> "');
}
// @ob_flush();
// flush();
mysqli_query($db,'update update_progress set
cleanup = "<i class=\"fa fa-cog larger icon-selected fa-spin\"></i> "');
databaseCleanup();
// @ob_flush();
// flush();
mysqli_query($db,'update update_progress set
cleanup = "<i class=\"fa fa-check icon-ok\"></i> "');
$stopTime = new DateTime();
$updateTime = $stopTime->diff($startTime);
$updateTime = $updateTime->h . 'h ' . $updateTime->i . 'm ' . $updateTime->s . 's';
$result = mysqli_query($db,"update update_progress set
update_status = 0,
update_time = '" . $updateTime . "',
last_update = '" . date('Y-m-d, H:i:s') . "'
");
cliLog("Update stop time: " . date("Ymd H:i:s"));
backgroundQueries();
}
else {
$structure_image=$row["structure_image"];
echo '<script type="text/javascript"> document.getElementById(\'structure\').innerHTML=" ' . $structure_image . '";</script>' . "\n";
$file_info=$row["file_info"];
echo '<script type="text/javascript"> document.getElementById(\'fileinfo\').innerHTML=" ' . $file_info . '";</script>' . "\n";
$cleanup=$row["cleanup"];
echo '<script type="text/javascript"> document.getElementById(\'cleanup\').innerHTML=" ' . $cleanup . '";</script>' . "\n";
$update_time=$row["update_time"];
echo '<script type="text/javascript"> document.getElementById(\'updateTime\').innerHTML=" ' . $update_time . '";</script>' . "\n";
}
$cfg['footer'] = 'close';
require('include/footer.inc.php');
}
// +------------------------------------------------------------------------+
// | Recursive scan |
// +------------------------------------------------------------------------+
function recursiveScan($dir) {
global $cfg, $db;
$album_id = '';
$file = array();
$filename = array();
$dir = iconv('UTF-8', NJB_DEFAULT_FILESYSTEM_CHARSET, $dir);
cliLog("recursiveScan: " . $dir);
// TODO: consider to remove. @see https://github.com/ArturSierzant/OMPD/issues/15
if ($cfg['ignore_media_dir_access_error']) {
$entries = @scandir($dir);
}
else {
$entries = @scandir($dir) or message(__FILE__, __LINE__, 'error', '[b]Update error![/b] [br][br][b]Failed to open directory:[/b][br]' . $dir . '[list][*]Check Media directory value in Settings -> Media update options[*]Check file permission[/list]');
}
//$dir = iconv(NJB_DEFAULT_FILESYSTEM_CHARSET, 'UTF-8', $dir);
$isIdFromFile = false;
foreach ($entries as $entry) {
cliLog('entry: ' . $entry);
if ($entry[0] === '.' || in_array($entry, $cfg['directory_blacklist']) === TRUE) {
continue;
}
if (is_dir($dir . $entry . '/')) {
recursiveScan (iconv(NJB_DEFAULT_FILESYSTEM_CHARSET, 'UTF-8', $dir . $entry . '/'));
continue;
}
$extension = strtolower(substr(strrchr($entry, '.'), 1));
if (in_array($extension, $cfg['media_extension'])) {
$entry = iconv(NJB_DEFAULT_FILESYSTEM_CHARSET, 'UTF-8', $entry);
$dir_d = iconv(NJB_DEFAULT_FILESYSTEM_CHARSET, 'UTF-8', $dir);
$file[] = $dir_d . $entry;
// $file[] = $dir . $entry;
$filename[] = substr($entry, 0, -strlen($extension) - 1);
continue;
}
if ($extension == 'id') {
$album_id = substr($entry, 0, -3);
$isIdFromFile = true;
}
}
if (count($file) === 0) {
unset($entries);
unset($entry);
unset($filename);
unset($file);
unset($dir);
return;
}
$dir = iconv(NJB_DEFAULT_FILESYSTEM_CHARSET, 'UTF-8', $dir);
//change full dir to relative dir to allow moving O!MPD from one
//server to another with different media dirs
if ($cfg['testing']=='on') {
$rel_dir = str_replace($cfg['media_dir'],'',$dir);
}
else {
$rel_dir = $dir;
}
if ($isIdFromFile) {
$db->query("
UPDATE album_id
SET updated = '1', path = '" . $db->real_escape_string($rel_dir) . "'
WHERE album_id = '" . $db->real_escape_string($album_id) . "'
LIMIT 1"
);
}
else {
$db->query("
UPDATE album_id
SET updated = '1'
WHERE path = '" . $db->real_escape_string($rel_dir) . "'
LIMIT 1"
);
}
cliLog('update results: ' . $db->info);
// Parse the digits from the info string that has the following format:
// Rows matched: 0 Changed: 0 Warnings: 0
preg_match_all('!\d+!', $db->info, $m);
// use 'Rows matched' because in some cases $db->affected_rows returned 0
// if record was found but didn't need to be updated
//if ($db->affected_rows == 0) {
if ((int)$m[0][0] == 0) {
$album_id = ($album_id == '') ? base_convert(uniqid(), 16, 36) : $album_id;
$album_add_time = time();
$db->query("
INSERT INTO album_id
(album_id, path, album_add_time, updated)
VALUES
('" . $album_id . "',
'" . $db->real_escape_string($rel_dir) . "',
'" . $album_add_time . "','1')"
);
} else {
$result = $db->query("
SELECT album_id, album_add_time
FROM album_id
WHERE path = '" . $db->real_escape_string($rel_dir) . "'
LIMIT 1"
);
$row = $result->fetch_assoc();
$album_id = $row["album_id"];
$album_add_time = $row["album_add_time"];
$result->free_result();
}
cliLog("Going into fileStructure for album_id: " . $album_id);
fileStructure($dir, $file, $filename, $album_id, $album_add_time);
}
// +------------------------------------------------------------------------+
// | Recursive scan - count directories |
// +------------------------------------------------------------------------+
function countDirectories_($dir) {
global $mediaDirs, $cfg, $db, $dirsCounter;
cliLog('countDirectories for ' . $base_dir);
$dir = iconv('UTF-8', NJB_DEFAULT_FILESYSTEM_CHARSET, $dir);
$dir = str_replace('[','\[',$dir);
$dir = str_replace(']','\]',$dir);
$tree = glob(rtrim($dir, '/') . '/*' );
if (is_array($tree)) {
foreach($tree as $file) {
if (is_dir($file)) {
countDirectories($file);
} elseif (is_file($file)) {
$extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if (in_array($extension, $cfg['media_extension'])) {
if (!in_array($dir,$mediaDirs)){
$mediaDirs[] = $dir;
$prevDirsCounter = $dirsCounter;
$dirsCounter++;
if ($dirsCounter - $prevDirsCounter > 50) {
mysqli_query($db,"UPDATE update_progress SET
structure_image = 'Counting media directories: " . $dirsCounter . "'");
}
}
else {
continue;
}
}
}
}
}
return $mediaDirs;
}
function countDirectories($base_dir) {
global $cfg, $db, $dirsCounter, $filesCounter, $isMediaDir, $prevUpdateTime;
$isMediaDir = 0;
if (!isset($prevUpdateTime)) $prevUpdateTime = microtime(true);
//$entries = @scandir($base_dir) or message(__FILE__, __LINE__, 'error', '[b]Failed to open directory:[/b][br]' . $base_dir . '[list][*]Check media_dir value in the config.inc.php file[*]Check file permission[/list]');
cliLog('countDirectories for ' . $base_dir);
if (NJB_DEFAULT_FILESYSTEM_CHARSET != 'UTF-8') {
$base_dir = iconv('UTF-8', NJB_DEFAULT_FILESYSTEM_CHARSET, $base_dir);
}
if ($cfg['ignore_media_dir_access_error']) {
$entries = @scandir($base_dir);
}
else {
$entries = @scandir($base_dir) or message(__FILE__, __LINE__, 'error', '[b]Update error![/b] [br][br][b]Failed to open directory:[/b][br]' . $base_dir . '[list][*]Check Media directory value in Settings -> Media update options[*]Check file permission[/list]');
}
$directories = array();
//foreach(scandir($base_dir) as $file) {
foreach($entries as $file) {
/* $extension = substr(strrchr($file, '.'), 1);
$extension = strtolower($extension); */
$extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if (in_array($extension, $cfg['media_extension'])) $isMediaDir = 1;
$dir = $base_dir. '/' .$file;
if (substr($base_dir,-1) == '/'){
$dir = $base_dir.$file;
}
if($file == '.' || $file == '..' || (is_dir($dir) === TRUE && in_array($file, $cfg['directory_blacklist']) === TRUE)) continue;
if(is_dir($dir)) {
if (NJB_DEFAULT_FILESYSTEM_CHARSET != 'UTF-8') {
$directories []= iconv(NJB_DEFAULT_FILESYSTEM_CHARSET,'UTF-8', $dir);
}
else {
$directories []= $dir;
}
if ($isMediaDir == 1) {
$dirsCounter = $dirsCounter + 1;
$currUpdateTime = microtime(true);
if ($currUpdateTime - $prevUpdateTime > 0.49) {
$prevUpdateTime = microtime(true);
mysqli_query($db,"UPDATE update_progress SET
structure_image = 'Counting media directories: " . $dirsCounter . "'");
}
}
if (NJB_DEFAULT_FILESYSTEM_CHARSET != 'UTF-8') {
$directories = array_merge($directories, countDirectories(iconv(NJB_DEFAULT_FILESYSTEM_CHARSET,'UTF-8', $dir)));
}
else {
$directories = array_merge($directories, countDirectories($dir));
}
}
}
return $directories;
}
// +------------------------------------------------------------------------+
// | File structure |
// +------------------------------------------------------------------------+
Function fileStructure($dir, $file, $filename, $album_id, $album_add_time) {
global $cfg, $db, $lastGenre_id, $getID3, $dirsCounter, $filesCounter, $curFilesCounter, $curDirsCounter, $prevDirsCounter, $last_update, $flag, $image_front;
// Also needed for track update!
$discs = 1;
$disc_digits = 0;
$track_digits = 0;
$year = NULL;
$month = NULL;
$artist = 'Unknown AlbumArtist';
$aGenre = 'Unknown Genre';
$aGenre_id = 0;
$album_dr = NULL;
$isUpdateRequired = false;
$new_array = array();
cliLog('force_filename_update: ' . var_export($cfg['force_filename_update'],true));
if ($cfg['name_source'] != 'tags') {
if (preg_match('#^(0{0,1}1)(0{1,3}1)+\.\s+.+#', $filename[0], $match) && preg_match('#^(\d{' . strlen($match[1] . $match[2]) . '})+\.\s+.+#', $filename[count($filename)-1])) {
// Multi disc
$disc_digits = strlen($match[1]);
$track_digits = strlen($match[2]);
preg_match('#^(\d{' . $disc_digits . '})\d{' . $track_digits . '}+\.\s+#', $filename[count($filename)-1], $match);
$discs = $match[1];
}
elseif (preg_match('#^(\d{2,4})+\.\s+.+#', $filename[0], $match)) {
// Single disc
$track_digits = strlen($match[1]);
}
$temp = decodeEscapeChar($dir);
$temp = explode('/', $temp);
$n = count($temp);
$artist_alphabetic = $temp[$n - 3];
$artist = $artist_alphabetic;
$album = $temp[$n - 2];
if (preg_match('#^(\d{4})\s+-\s+(.+)#', $album, $match)) {
$year = $match[1];
$album = $match[2];
}
elseif (preg_match('#^(\d{4})(0[1-9]|1[012])\s+-\s+(.+)#', $album, $match)) {
$year = $match[1];
$month = $match[2];
$album = $match[3];
}
}
if ($cfg['name_source'] == 'tags') {
++$curDirsCounter;
if ($cfg['cli_update'] == false && ((microtime(true) - $cfg['timer']) * 1000) > $cfg['update_refresh_time'] && ($curDirsCounter/$dirsCounter > ($prevDirsCounter/$dirsCounter + 0.005))) {
$prevDirsCounter = $curDirsCounter;
mysqli_query($db,'update update_progress set
structure_image = "<div class=\'out\'><div class=\'in\' style=\'width:' . html(floor($curDirsCounter/$dirsCounter * 200)) . 'px\'></div></div> ' . html(floor($curDirsCounter/$dirsCounter * 100)) . '%"');
$cfg['timer'] = microtime(true);
}
//dir modified: files or dirs added or forceUpdate
if (filemtime(dirname($file[0])) > $last_update || $cfg['force_filename_update']) {
$isUpdateRequired = true;
}
else {
$q = mysqli_query($db,'SELECT relative_file, filemtime FROM track WHERE album_id = BINARY "' . $db->real_escape_string($album_id) . '"');
while($row = mysqli_fetch_assoc($q)){
$row['relative_file'] = $cfg['media_dir'] . $row['relative_file'];
//$new_array[$row['relative_file']] = $row['filemtime'];
$new_array[] = $row['relative_file'];
//echo $row['relative_file'] . '<br>';
};
for ($i=0; $i < count($filename); $i++) {
//check if file is modified
if (filemtime($file[$i]) > $last_update) {
$isUpdateRequired = true;
break;
}
else {
if (!in_array($file[$i],$new_array)) {
$isUpdateRequired = true;
break;
}
}
}
}
if ($isUpdateRequired) {
cliLog("fileStructure file[0]: " . $file[0]);
$file_d = iconv('UTF-8', NJB_DEFAULT_FILESYSTEM_CHARSET, $file[0]);
$ThisFileInfo = $getID3->analyze($file_d);
getid3_lib::CopyTagsToComments($ThisFileInfo);
cliLog('getid3 ok');
$artist = parseAlbumArtist($ThisFileInfo);
if ($artist == 'Unknown AlbumArtist') {
$artist = parseTrackArtist($ThisFileInfo);
};
$artist_alphabetic = $artist;
$year = parseYear($ThisFileInfo);
$album_dr = parseAlbumDynamicRange($ThisFileInfo);
$aGenre = parseGenre($ThisFileInfo);
if ((strpos(strtolower($dir), strtolower($cfg['misc_tracks_folder'])) === false) && (strpos(strtolower($dir), strtolower($cfg['misc_tracks_misc_artists_folder'])) === false)) {
$album = parseAlbumTitle($ThisFileInfo);
}
elseif (strpos(strtolower($dir), strtolower($cfg['misc_tracks_folder'])) !== false) {
$year = NULL;
$album = $cfg['misc_tracks_folder'] . $artist;
/* if (strtolower(basename($dir)) == strtolower($cfg['misc_tracks_folder']))
$album = $cfg['misc_tracks_folder'] . $artist;
else
$album = basename($dir); */
}
elseif (strpos(strtolower($dir), strtolower($cfg['misc_tracks_misc_artists_folder'])) !== false) {
$artist = 'Various Artists';
$artist_alphabetic = $artist;
$aGenre = '';
$year = NULL;
if (strtolower(basename($dir)) == strtolower($cfg['misc_tracks_misc_artists_folder']))
$album = $cfg['misc_tracks_misc_artists_folder'];
else
$album = basename($dir);
}
/* $result = mysqli_query($db,'SELECT genre_id FROM genre WHERE genre="' . $db->real_escape_string($aGenre) . '"');
$row=mysqli_fetch_assoc($result);
$aGenre_id=$row["genre_id"];
if (mysqli_num_rows($result)==0) {
mysqli_query($db,'INSERT INTO genre (genre_id, genre, updated)
VALUES ("' . $db->real_escape_string($lastGenre_id) . '",
"' . $db->real_escape_string($aGenre) . '",
1)');
$aGenre_id = $lastGenre_id;
++$lastGenre_id;
} else {
$result = mysqli_query($db,"update genre set
updated = 1
WHERE genre = '". $db->real_escape_string($aGenre) ."';");
} */
//
// Update/Insert album information on the end of this function to be able to include image_id
//
// TODO: cli_update is not possible anymore, right?
// if ($cfg['cli_update'] && $cfg['cli_silent_update'] == false)
// echo $artist_alphabetic . ' - ' . $album . "\n";
// Track update
$disc = 1;
$number = NULL;
for ($i=0; $i < count($filename); $i++) {
cliLog("fileStructure TrackUpdate: " . $file[$i]);
$relative_file = substr($file[$i], strlen($cfg['media_dir']));
mysqli_query($db,'UPDATE track SET
updated = 1
WHERE album_id = "' . $db->real_escape_string($album_id) . '"
AND relative_file = BINARY "' . $db->real_escape_string($relative_file) . '"
LIMIT 1');
preg_match_all('!\d+!', $db->info, $m);
//if ($cfg['force_filename_update'] || mysqli_affected_rows($db) == 0)
if ($cfg['force_filename_update'] || (int)$m[0][0] == 0)
{
$temp = decodeEscapeChar($filename[$i]);
if ($cfg['name_source'] != 'tags') {
//if (preg_match('#^(\d{' . $disc_digits . '})(\d{' . $track_digits . '})\s+-\s+(.+)#', $temp, $match)) {
if (preg_match('#^(\d{' . $disc_digits . '})(\d{' . $track_digits . '})+\.\s+(.+)#', $temp, $match)) {
if ($disc_digits > 0) {
// Multiple disc
$disc = $match[1];
$number = $match[2];
}
else {
// Single disc
$number = $match[2];
}
$temp = $match[3]; // Strip disc and track number
}
if (preg_match('#^(.+?)\s+-\s+(.+?)(?:\s+Ft\.\s+(.+))?$#i', $temp, $match)) {
$track_artist = $match[1];
$title = $match[2];
$featuring = (isset($match[3])) ? $match[3] : '';
}
elseif (preg_match('#^(.+?)(?:\s+Ft\.\s+(.+))?$#i', $temp, $match)) {
$track_artist = $artist;
$title = $match[1];
$featuring = (isset($match[2])) ? $match[2] : '';
}
else {
$track_artist = '*** UNSUPPORTED FILENAME FORMAT ***';
$title = '(' . $filename[$i] . ')';
$featuring = '';
}
}
//if (mysqli_affected_rows($db) == 0)
if ((int)$m[0][0] == 0)
mysqli_query($db,'INSERT INTO track (artist, featuring, title, relative_file, disc, number, album_id, updated)
VALUES ("' . $db->real_escape_string($track_artist) . '",
"' . $db->real_escape_string($featuring) . '",
"' . $db->real_escape_string($title) . '",
"' . $db->real_escape_string($relative_file) . '",
' . (int) $disc . ',
' . ((is_null($number)) ? 'NULL' : (int) $number) . ',
"' . $db->real_escape_string($album_id) . '",
1)');
else
mysqli_query($db,'UPDATE track SET
artist = "' . $db->real_escape_string($track_artist) . '",
featuring = "' . $db->real_escape_string($featuring) . '",
title = "' . $db->real_escape_string($title) . '",
relative_file = "' . $db->real_escape_string($relative_file) . '",
disc = ' . (int) $disc . ',
number = ' . ((is_null($number)) ? 'NULL' : (int) $number) . ',
album_id = "' . $db->real_escape_string($album_id) . '",
updated = 1
WHERE album_id = "' . $db->real_escape_string($album_id) . '"
AND relative_file = BINARY "' . $db->real_escape_string($relative_file) . '"
LIMIT 1');
}
}
unset($ThisFileInfo);
}
else {
$q = mysqli_query($db,'SELECT relative_file FROM track
WHERE album_id = "' . $db->real_escape_string($album_id) . '"');
$rows = mysqli_num_rows($q);
if ($rows == count($filename)) {
mysqli_query($db,'UPDATE track SET
updated = 1
WHERE album_id = "' . $db->real_escape_string($album_id) . '"
');
}
else {
for ($i=0; $i < count($filename); $i++) {
$relative_file = substr($file[$i], strlen($cfg['media_dir']));
mysqli_query($db,'UPDATE track SET
updated = 1
WHERE album_id = "' . $db->real_escape_string($album_id) . '"
AND relative_file = BINARY "' . $db->real_escape_string($relative_file) . '"
LIMIT 1');
}
}
}
// Image update
$image = NJB_HOME_DIR . 'image/no_image.png';
$flag = 0; // No image
$misc_tracks = false;
cliLog("fileStructure ImageUpdate: " . $file[0]);
$dir = iconv('UTF-8', NJB_DEFAULT_FILESYSTEM_CHARSET, $dir);
$image_front_arr = explode(";", $cfg['image_front']);
$relative_dir = substr($dir, strlen($cfg['media_dir']));
$is_image_set = false;
foreach ($image_front_arr as $img) {
if (is_file($dir . $img . '.jpg')) {
$image = $dir . $img . '.jpg';
$image_front = $relative_dir . $img . '.jpg';
$is_image_set = true;
$flag = 3;