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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
|
<?php
/*
Copyright (c) 2004 ampache.org
All rights reserved.
All of the main functions for Ampache.
FIXME: Remove this file... shouldn't be used anymore
*/
/*
* show_local_catalog_info()
*
*/
function show_local_catalog_info() {
global $settings, $username;
$dbh = dbh();
$query = "SELECT count(*) AS songs, SUM(size) AS size, SUM(time) as time FROM song";
$db_result = mysql_query($query, $dbh);
$songs = mysql_fetch_array($db_result);
$query = "SELECT count(*) FROM album";
$db_result = mysql_query($query, $dbh);
$albums = mysql_fetch_row($db_result);
$query = "SELECT count(*) FROM artist";
$db_result = mysql_query($query, $dbh);
$artists = mysql_fetch_row($db_result);
$sql = "SELECT count(*) FROM user";
$db_result = mysql_query($sql, $dbh);
$users = mysql_fetch_row($db_result);
$time = time();
$last_seen_time = $time - 1200;
$sql = "SELECT count(DISTINCT s.username) FROM session AS s " .
"INNER JOIN user AS u ON s.username = u.username " .
"WHERE s.expire > " . $time . " " .
"AND u.last_seen > " . $last_seen_time;
$db_result = mysql_query($sql, $dbh);
$connected_users = mysql_fetch_row($db_result);
$hours = floor($songs['time']/3600);
$size = $songs['size']/1048576;
$days = floor($hours/24);
$hours = $hours%24;
$time_text = "$days ";
$time_text .= ($days == 1) ? _("day") : _("days");
$time_text .= ", $hours ";
$time_text .= ($hours == 1) ? _("hour") : _("hours");
if ( $size > 1024 ) {
$total_size = sprintf("%.2f", ($size/1024));
$size_unit = "GB";
}
else {
$total_size = sprintf("%.2f", $size);
$size_unit = "MB";
}
print '
<table class="border" cellspacing="1" cellpadding="3" width="100%" border="0">
<tr class="table-header">
<td colspan="2">' . _("Catalog Statistics") . '</td>
</tr>
<tr class="even">
<td> ' . _("Total Users") . ' </td>
<td> <b>' . $users[0] .'</b> </td>
</tr>
<tr class="even">
<td> ' . _("Connected Users") . ' </td>
<td> <b>' . $connected_users[0] .'</b> </td>
</tr>
<tr class="even">
<td> ' . _("Albums") . ' </td>
<td> <b>' . $albums[0] .'</b> </td>
</tr>
<tr class="even">
<td> ' . _("Artists") . ' </td>
<td> <b>' . $artists[0] .'</b> </td>
</tr>
<tr class="even">
<td> ' . _("Songs") . ' </td>
<td> <b>' . $songs[0] .'</b> </td>
</tr>
<tr class="even">
<td> ' . _("Catalog Size") . ' </td>
<td> <b>' . $total_size .' ' . $size_unit .'</b> </td>
</tr>
<tr class="even">
<td> ' . _("Catalog Time") . ' </td>
<td> <b>' . $time_text .'</b> </td>
</tr>
</table>
';
} // show_local_catalog_info()
/*
* show_random_play()
*
*/
function show_random_play() {
$web_path = conf('web_path');
print '
<form name="random" method="post" enctype="multipart/form-data" action="' . $web_path . '/song.php">
<input type="hidden" name="action" value="m3u" />
<table class="border" border="0" cellpadding="3" cellspacing="1" width="100%">
<tr class="table-header">
<td colspan="4">' . _("Play Random Selection") . '</td>
</tr>
<tr class="even">
<td>
<table border="0">
<tr class="even">
<td>' . _("Item count") .'</td>
<td>
<select name="random">
<option value="1">1</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="20">20</option>
<option value="30">30</option>
<option value="50">50</option>
<option value="100">100</option>
<option value="500">500</option>
<option value="1000">1000</option>
<option value="-1">' . _("All") . '</option>
</select></td>
<td rowspan="3" valign="top"> ' . _("From genre") . '</td>
<td rowspan="3">
';
show_genre_pulldown( -1, 0 );
print '
</td></tr>
<tr class="even">
<td>
' . _("Favor Unplayed") . ' <br />
' . _("Full Albums") . ' <br />
' . _("Full Artist") . '
</td>
<td>
<input type="checkbox" id="unplayed" name="unplayed" value="1" onclick="flipField(\'album\');flipField(\'artist\')" /><br />
<input type="checkbox" id="album" name="full_album" value="1" onclick="flipField(\'unplayed\');flipField(\'artist\')" /><br />
<input type="checkbox" id="artist" name="full_artist" value="1" onclick="flipField(\'unplayed\');flipField(\'album\')" />
</td>
</tr>
<tr class="even">
<td nowrap="nowrap"> ' . _("from catalog") . '</td>
<td>
';
show_catalog_pulldown( -1, 0);
print '
</td></tr>
<tr>
<td colspan="4">
<input type="hidden" name="aaction" value="Play!" />
<input class="button" type="submit" name="aaction" value="' . _("Play Random Songs") . '" />
</td>
</tr>
</table>
</td></tr>
</table>
</form>
';
} // show_random_play()
/*
* show_artist_pulldown()
*
* Helper functions for album and artist functions
*
*/
function show_artist_pulldown ($artist) {
global $settings;
$dbh = dbh();
$query = "SELECT id,name FROM artist ORDER BY name";
$db_result = mysql_query($query, $dbh);
echo "\n<select name=\"artist\">\n";
while ( $r = mysql_fetch_row($db_result) ) {
// $r[0] = id, $r[1] = name
if ( $artist == $r[0] ) {
echo "<option value=\"$r[0]\" selected=\"selected\">". htmlspecialchars($r[1]) ."</option>\n";
}
else {
echo "<option value=\"$r[0]\">". htmlspecialchars($r[1]) ."</option>\n";
}
}
echo " </select>";
} // show_artist_pulldown()
/*
* show_album_pulldown()
*
*/
function show_album_pulldown ($album) {
global $settings;
$dbh = dbh();
$sql = "SELECT id,name FROM album ORDER BY name";
$db_result = mysql_query($sql, $dbh);
echo "\n<select name=\"album\">\n";
while ( $r = mysql_fetch_row($db_result) ) {
// $r[0] = id, $r[1] = name
if ( $album == $r[0] ) {
echo "\t <option value=\"${r[0]}\" selected=\"selected\">".htmlspecialchars($r[1])."</option>\n";
}
else {
echo "\t <option value=\"${r[0]}\">".htmlspecialchars($r[1])."</option>\n";
}
}//while
echo "\n</select>\n";
} // show_album_pulldown()
/*
* show_flagged_popup($reason);
*
* Shows a listing of the flagged_types for when people want to mark
* a song as being broken in some way.
*/
function show_flagged_popup($reason,$label='value', $name='flagged_type', $other='') {
global $settings;
$dbh = dbh();
$access = $_SESSION['userdata']['access'];
$query = "SELECT type,value FROM flagged_types";
if ($access !== 'admin') {
$query .= " WHERE access = '$access'";
}
$db_result = mysql_query($query, $dbh);
echo "\n<select name=\"$name\" $other>\n";
while ( $r = mysql_fetch_array($db_result) ) {
// $r[0] = id, $r[1] = type
if ( $reason === $r['type'] ) {
echo "\t<option value=\"".$r['type']."\" selected=\"selected\">".htmlspecialchars($r[$label])."</option>\n";
}
else {
echo "\t<option value=\"".$r['type']."\">".htmlspecialchars($r[$label])."</option>\n";
}
}
echo "\n</select>\n";
} // show_flagged_popup()
/*
* show_genre_pulldown()
*
* Set complete=1 if you want the entire genre list
*
*/
function show_genre_pulldown ($genre, $complete) {
$dbh = dbh();
// find the genres we have in use
if ( $complete ) {
$sql = "SELECT id FROM genre ORDER BY name";
}
else {
$sql = "SELECT DISTINCT song.genre FROM genre, song" .
" WHERE song.genre = genre.id" .
" ORDER BY genre.name";
}
$db_result = mysql_query($sql, $dbh);
echo "\n<select name=\"genre[]\" multiple=\"multiple\" size=\"7\">\n";
if ( ! $complete ) {
$genre_info = get_genre_info( -1 );
if ( $genre == -1 ) {
echo " <option value=\"-1\" selected=\"selected\">${genre_info[0]} - (${genre_info[1]})</option>\n";
}
else {
echo " <option value=\"-1\">${genre_info[0]} - (${genre_info[1]})</option>\n";
}
}
while ( $r = mysql_fetch_row($db_result) ) {
// $r[0] = genre id
list($genre_name, $genre_count) = get_genre_info($r[0]);
$genre_name = htmlspecialchars($genre_name);
if ( $genre == $r[0] ) {
echo " <option value=\"${r[0]}\" selected=\"selected\">$genre_name - ($genre_count)</option>\n";
}
else {
echo " <option value=\"${r[0]}\">$genre_name - ($genre_count)</option>\n";
}
}
echo " </select>\n";
} // show_genre_pulldown()
/*
* show_catalog_pulldown()
*
* Set complete=1 if you want the entire catalog list (including disabled)
*
*/
function show_catalog_pulldown ($catalog, $complete) {
global $settings;
// find the genres we have in use
$sql = "SELECT id,name FROM catalog ORDER BY name";
$db_result = mysql_query($sql, dbh());
echo "\n<select name=\"catalog\">\n";
echo " <option value=\"-1\" selected=\"selected\">All</option>\n";
while ( $r = mysql_fetch_row($db_result) )
{
// $r[0] = genre id
$catalog_name = htmlspecialchars($r[1]);
if ( $catalog == $r[0] )
{
echo " <option value=\"${r[0]}\" selected=\"selected\">$catalog_name</option>\n";
}
else
{
echo " <option value=\"${r[0]}\">$catalog_name</option>\n";
}
}
echo "\n</select>\n";
} // show_catalog_pulldown()
/*
* update_counter()
*
* update what song/album/artist has just been played
*
*/
function update_counter ($type, $id, $dbh=0) {
global $settings;
if (!is_resource($dbh)) {
$dbh = dbh();
}
// from hopson: these queries will be very useful for generating overall statistics:
/*
SELECT song.title,SUM(object_count.count) FROM song,object_count WHERE object_count.object_type = 'song' AND object_count.object_id = song.id GROUP BY song.id;
SELECT album.name,SUM(object_count.count) FROM album,object_count WHERE object_count.object_type = 'album' AND object_count.object_id = album.id GROUP BY album.id;
SELECT artist.name,SUM(object_count.count) FROM artist,object_count WHERE object_count.object_type = 'artist' AND object_count.object_id = artist.id GROUP BY artist.id;
SELECT playlist.name,SUM(object_count.count) FROM playlist,object_count WHERE object_count.object_type = 'playlist' AND object_count.object_id = playlist.id GROUP BY playlist.id;
*/
if ( $type == 'song' ) {
$sql = "UPDATE $type SET times_played = times_played + 1 WHERE id = '$id'";
}
else {
$sql = "UPDATE $type SET times_played = times_played + 1 WHERE id = '$id'";
}
$db_result = mysql_query($sql, $dbh);
} // update_counter()
/*
* delete_user_stats()
*
* just delete stats for specific users or all of them
*
*/
function delete_user_stats ($user) {
$dbh = dbh();
if ( $user == 'all' ) {
$sql = "DELETE FROM object_count";
}
else {
$sql = "DELETE FROM object_count WHERE userid = '$user'";
}
$db_result = mysql_query($sql, $dbh);
} // delete_user_stats()
/*
* insert_flagged_song()
*
*/
function insert_flagged_song($song, $reason, $comment) {
$user = $_SESSION['userdata']['id'];
$time = time();
$sql = "INSERT INTO flagged (user,song,type,comment,date)" .
" VALUES ('$user','$song', '$reason', '$comment', '$time')";
$db_result = mysql_query($sql, dbh());
} // insert_flagged_song()
/*
* get_flagged();
*
* Get all of the songs from the flagged table. These are songs that
* may or may not be broken.
* Deprecated by hopson on 7/27
*/
function get_flagged() {
$dbh = dbh();
$sql = "SELECT flagged.id, user.username, type, song, date, comment" .
" FROM flagged, user" .
" WHERE flagged.user = user.username" .
" ORDER BY date";
$db_result = mysql_query($sql, $dbh);
$arr = array();
while ( $flag = mysql_fetch_object($db_result) ) {
$arr[] = $flag;
}
return $arr;
} // get_flagged()
/*
* get_flagged_type($type);
*
* Return the text associated with this type.
*/
function get_flagged_type($type) {
$dbh = dbh();
$sql = "SELECT value FROM flagged_types WHERE type = '$type'";
echo $sql;
$db_result = mysql_query($sql, $dbh);
if ($flagged_type = mysql_fetch_object($db_result)) {
return $flagged_type->value;
}
else {
return FALSE;
}
} // get_flagged_type()
/*
* delete_flagged( $flag );
*
*/
function delete_flagged($flag) {
$dbh = dbh();
$sql = "DELETE FROM flagged WHERE id = '$flag'";
$db_result = mysql_query($sql, $dbh);
} // delete_flagged()
/*********************************************************/
/* Functions for getting songs given artist, album or id */
/*********************************************************/
// TODO : albums should be always gruoped by
// id, like 'greatest hits' album is below, never by name.
// Other catalog functions should take care of assigning all
// songs with same name album to the same album id. It should
// not be done here.
// I'm commenting all this out to always sort by ID, to
// see how bad it is. -Rubin
function get_songs_from_album ($album) {
global $settings;
$dbh = dbh();
$songs = array();
$query = "SELECT track, id as song FROM song" .
" WHERE album = '$album'" .
" ORDER BY track, title";
$db_result = mysql_query($query, $dbh);
while ( $r = mysql_fetch_array($db_result) ) {
$songs[] = $r;
}
return $songs;
}
function get_song_ids_from_album ($album) {
$dbh = dbh();
$song_ids = array();
$query = "SELECT id FROM song" .
" WHERE album = '$album'" .
" ORDER BY track, title";
$db_result = mysql_query($query, $dbh);
while ( $r = mysql_fetch_object($db_result) ) {
$song_ids[] = $r->id;
}
return $song_ids;
}
function get_song_ids_from_artist ($artist) {
global $settings;
$dbh = dbh();
$song_ids = array();
$artist = sql_escape($artist);
$query = "SELECT id FROM song" .
" WHERE artist = '$artist'" .
" ORDER BY album, track";
$db_result = mysql_query($query, $dbh);
while ( $r = mysql_fetch_object($db_result) ) {
$song_ids[] = $r->id;
}
return $song_ids;
}
/*
* get_song_ids_from_artist_and_album();
*
* Get all of the songs that are from this album and artist
*
*/
function get_song_ids_from_artist_and_album ($artist, $album) {
global $settings;
$dbh = dbh();
$sql = "SELECT id FROM song" .
" WHERE artist = '$artist'" .
" AND album = '$album'" .
" ORDER BY track, title";
$db_result = mysql_query($sql, $dbh);
$song_ids = array();
while ( $r = mysql_fetch_object($db_result) ) {
$song_ids[] = $r->id;
}
return $song_ids;
}
// Used by playlist functions when you have an array of something of type
// and you want to extract the songs from it whether type is artists or albums
function get_songs_from_type ($type, $results, $artist_id = 0) {
$dbh = dbh();
$count = 0;
$song = array();
foreach ($results as $value) {
// special case from the album view where we don't want every orphan
if ($type == 'album' && ($value == 'orphans' || $artist_id != 0)) {
$sql = "SELECT id FROM song WHERE $type = '$value' AND artist = '$artist_id'";
$db_result = mysql_query($sql, $dbh);
}
else {
$sql = "SELECT id FROM song WHERE $type = '$value'";
$db_result = mysql_query($sql, $dbh);
}
while ( $r = mysql_fetch_row($db_result) ) {
$song[$count] = $r[0];
$count++;
}
}
return $song;
}
/*********************************************************/
/* This is the main song display function. I found tieing it to the playlist functions
was really handy in getting added functionality at no cost.
/* Lets tie it to album too, so we can show art ;) */
/*********************************************************/
function show_songs ($song_ids, $playlist_id=0, $album=0) {
$dbh = dbh();
// Get info about current user
$user = new User($_SESSION['userdata']['username']);
// Get info about playlist owner
if (isset($playlist_id) && $playlist_id != 0) {
$sql = "SELECT user FROM playlist WHERE id = '$playlist_id'";
$db_result = mysql_query($sql, $dbh);
if ($r = mysql_fetch_array($db_result)) {
$pluser = get_user_byid($r[0]);
}
}
$totaltime = 0;
$totalsize = 0;
require (conf('prefix') . "/templates/show_songs.inc");
return TRUE;
}// function show_songs
function show_playlist_form () {
print <<<ECHO
<table cellpadding="5" class="tabledata">
<tr align="center" class="odd">
<td>
<input type="button" name="select_all" value="Select All" onclick="this.value=check_results()" />
</td>
<td> Playlist:</td>
<td>
<input name="action" class="button" type="submit" value="Add to" />
ECHO;
show_playlist_dropdown();
print <<<ECHO
<input name="action" class="button" type="submit" value="View" />
<input name="action" class="button" type="submit" value="Edit" />
</td>
</tr>
<tr align="center" class="even">
<td colspan="6">
<input name="action" class="button" type="submit" value="Play Selected" />
</td>
</tr>
</table>
ECHO;
}
function get_artist_name ($artist, $dbh=0) {
global $settings;
if (!is_resource($dbh)) {
$dbh = dbh();
}
$query = "SELECT name FROM artist WHERE id = '$artist'";
$db_result = mysql_query($query, $dbh);
if ($r = mysql_fetch_object($db_result)) {
return $r->name;
}
else {
return FALSE;
}
}
function get_artist_info ($artist_id) {
$dbh = dbh();
$sql = "SELECT * FROM artist WHERE id = '$artist_id'";
$db_result = mysql_query($sql, $dbh);
if ($info = mysql_fetch_array($db_result)) {
$sql = "SELECT COUNT(song.album) FROM song " .
" WHERE song.artist = '$artist_id'" .
" GROUP BY song.album";
$db_result = mysql_query($sql, $dbh);
$albums = 0;
$songs = 0;
while(list($song) = mysql_fetch_row($db_result)) {
$songs += $song;
$albums++;
}
$info['songs'] = $songs;
$info['albums'] = $albums;
//FIXME: Lame place to put this
//if ($songs < conf('min_artist_songs') || $albums < conf('min_artist_albums')) {
// return FALSE;
//}
return $info;
}
else {
return FALSE;
}
}
function get_artist_from_album ($album_id) {
global $settings;
$dbh = dbh();
$query = "SELECT DISTINCT artist.id, artist.name FROM artist,song" .
" WHERE song.album = '$album_id' AND song.artist = artist.id";
$db_result = mysql_query($query, $dbh);
$r = mysql_fetch_object($db_result);
return $r;
}
function get_artist_name_from_song ($song_id) {
$dbh = dbh();
$sql = "SELECT artist.name AS name FROM artist, song" .
" WHERE artist.id = song.artist" .
" AND song.id = '$song_id'";
$db_result = mysql_query($sql, $dbh);
if ($r = mysql_fetch_object($db_result)) {
return $r->name;
}
else {
return FALSE;
}
}
function get_album_name ($album, $dbh = 0) {
$album = new Album($album);
return $album->name;
} // get_album_name
function get_genre_info($genre_id) {
global $settings;
$dbh = dbh();
$sql = "SELECT name FROM genre WHERE id = '$genre_id'";
$db_result = mysql_query($sql, $dbh);
// if its -1 then we're doing all songs
if ( $genre_id < 0 ) {
$sql = "SELECT count(*) FROM song";
}
else {
$sql = "SELECT count(*) FROM song WHERE genre = '$genre_id'";
}
$genre_result = mysql_query($sql, $dbh);
$genre_count = mysql_fetch_row($genre_result);
$r = mysql_fetch_row($db_result);
// Crude hack for non-standard genre types
if ($genre_id == -1) {
return array('All', $genre_count[0]);
}
elseif ($genre_id == 0) {
return array('N/A', $genre_count[0]);
}
else {
return array($r[0], $genre_count[0]);
}
}
function get_genre($id) {
global $settings;
$dbh = dbh();
$query = "SELECT * FROM genre WHERE id = '$id'";
$db_result = mysql_query($query, $dbh);
$r = mysql_fetch_object($db_result);
return $r;
}
// Utility function to help move things along
function get_song_info ($song, $dbh = 0) {
$song = new Song($song);
return $song;
} // get_song_info
/*!
@function show_albums
@discussion show many albums, uses view class
*/
function show_albums ($albums,$view=0) {
$dbh = libglue_param(libglue_param('dbh_name'));
if (!$view) {
$view = new View($_SESSION['view_base_sql'], $_SESSION['script'], $total_items,$_SESSION['view_offset_limit']);
}
if ($albums) {
require (conf('prefix') . "/templates/show_albums.inc");
}
else {
echo "<p><font color=\"red\">No Albums Found</font></p>";
}
} // show_albums
// Had to tweak this so it would show both public and private playlists
// Defaults to showing both although you could pass type=private|adminprivate|public
// to see only those
function show_playlists ($type = 'all') {
$dbh = dbh();
$user = $GLOBALS['user'];
$web_path = conf('web_path');
// mapping of types to pretty names
$typemap = array( "public" => _("Public"),
"private" => _("Your Private"),
"adminprivate" => _("Other Private")
);
if ($type == 'all') {
show_playlists('private');
if ( $user->access === 'admin' ) {
show_playlists('adminprivate');
}
show_playlists('public');
return true;
}
elseif ($type == 'public') {
$sql = "SELECT id,name,user,date ".
" FROM playlist ".
" WHERE type='public'".
" ORDER BY name";
}
elseif ($type == 'private') {
$sql = "SELECT id,name,user,date ".
" FROM playlist ".
" WHERE type='private'" .
" AND user = '$user->username'" .
" AND name <> 'Temporary'".
" ORDER BY name";
}
elseif ($type == 'adminprivate') {
if ( $user->access === 'admin' ) {
$sql = "SELECT id,name,user,date ".
" FROM playlist ".
" WHERE type='private'" .
" AND username != '$user->username'" .
" AND name <> 'Temporary'".
" ORDER BY name";
}
else {
// No admin access
$sql = 'SELECT 1+1';
}
}
else {
echo "** Error ** Call to show_playlists with unknown type $type ".
"in file ".$_SERVER['PHP_SELF']." ** <br />\n";
$sql = 'SELECT 1+1';
}
$db_result = mysql_query($sql, $dbh);
print <<<ECHO
<h3>$typemap[$type] Playlists</h3>
<table class="tabledata" cellspacing="0" cellpadding="0" border="0">
<tr class="table-header">
<th>Playlist Name</th>
<th># Songs</th>
<th>Owner</th>
<th colspan="6">Actions</th>
</tr>
ECHO;
flip_class(array('even','odd'));
if ( mysql_num_rows($db_result) ) {
while ( $r = mysql_fetch_array($db_result) ) {
$plname = $r['name'];
$plid = $r['id'];
$pluser = new User($r['user']);
$plfullname = $pluser->fullname;
$plowner = $pluser->username;
// find out how many songs in this playlist
$count_query = "SELECT count(*) ".
" FROM playlist_data ".
" WHERE playlist = '$plid'";
$count_result = mysql_query($count_query, $dbh);
list($count) = mysql_fetch_row($count_result);
$class = flip_class();
echo " <tr class=\"$class\">\n";
echo " <td><a href=\"$web_path/playlist.php?playlist_id=$plid&action=view_list\">$plname</a></td>\n";
echo " <td>$count</td>\n";
echo " <td>$plfullname</td>\n";
echo " <td><a href=\"$web_path/playlist.php?playlist_id=$plid&action=view_list\">" . _("View") . "</a></td>\n";
if ($user->username == $pluser->username || $user->has_access(100)) {
echo " <td><a href=\"$web_path/playlist.php?playlist_id=$plid&action=edit\">" . _("Edit") . "</a></td>\n";
echo " <td><a href=\"$web_path/playlist.php?playlist_id=$plid&action=delete_playlist\">" . _("Delete") . "</a></td>\n";
}
else {
echo " <td> </td>\n";
echo " <td> </td>\n";
}
if ( $count[0] ) {
echo " <td><a href=\"$web_path/song.php?action=m3u&playlist_id=$plid\">" . _("Play") . "</a> | " .
"<a href=\"$web_path/song.php?action=random&playlist_id=$plid\">" . _("Random") . "</a></td>\n";
}
else {
echo " <td> </td>\n";
}
if( batch_ok() ) {
echo" <td><a href=\"$web_path/batch.php?action=pl&id=$plid\">" . _("Download") . "</a></td>\n";
} else {
echo" <td> </td>\n";
}
echo " </tr>\n";
}
echo "\n";
} //if rows in result
else {
echo " <tr class=\"even\">\n";
echo " <td colspan=\"7\">" . _("There are no playlists of this type") . "</td>\n";
echo " </tr>\n";
}
echo "</table>\n";
echo "<br />\n";
}
function get_playlist_track_from_song ( $playlist_id, $song_id ) {
$dbh = dbh();
$sql = "SELECT track FROM playlist_data" .
" WHERE playlist = '$playlist_id'" .
" AND song = '$song_id'";
$db_result = mysql_query($sql, $dbh);
if ($r = mysql_fetch_array($db_result)) {
return $r[0];
}
else {
return FALSE;
}
}
//FIXME: Pull this and put it in a template
function show_playlist_create () {
$web_path = conf('web_path');
print <<<ECHO
<form name="songs" method="post" action="$web_path/playlist.php">
<table class="border"><tr class="table-header"><td colspan="2" align="center">
ECHO;
print _("Create a new playlist");
print <<<ECHO
</td>
</tr>
<tr class="even">
<td align="left"> Name: </td>
<td align="left"><input type="text" name="playlist_name" size="20" /></td>
</tr>
<tr class="odd">
<td align="left"> Type: </td>
<td align="left">
<select name="type">
<option value="private"> Private </option>
<option value="public"> Public </option>
</select>
</td>
</tr>
<tr class="even">
<td align="left"> </td>
<td align="left">
<input type="submit" name="action" value="Create" />
<input type="reset" name="Reset" />
</td>
</tr>
</table>
</form>
ECHO;
}
function show_playlist_edit ( $playlist ) {
$username = $_SESSION['userdata']['username'];
if (check_playlist_access($playlist->id,$username) == false) {
show_playlist_access_error($playlist, $username);
return;
}
$plname = $playlist->name;
$self = $_SERVER['PHP_SELF'];
print <<<ECHO
<form name="songs" method="post" action="$self">
<input type="hidden" name="playlist_id" value="$playlist->id" />
<table class="border">
<tr class="table-header">
<td colspan="2">Editing Playlist</td>
</tr>
<tr>
<td align="left"> Name: </td>
<td align="left">
<input type="text" name="new_playlist_name" value="$plname" size="20" />
</td>
</tr>
<tr>
<td align="left"> Type: </td>
<td align="left">
<select name="type">
ECHO;
if ($playlist->type == 'public') {
echo "<option value=\"public\" selected=\"selected\">Public</option>";
}
else {
echo "<option value=\"public\">Public</option>";
}
if ($playlist->type == 'private') {
echo "<option value=\"private\" selected=\"selected\">Private</option>";
}
else {
echo "<option value=\"private\">Private</option>";
}
print <<<ECHO
</select>
</td>
</tr>
<tr>
<td align="left"> </td>
<td align="left">
<input type="submit" name="action" value="Update" />
</td>
</tr>
</table>
</form>
ECHO;
}
// See if this user has access to work on this list
function check_playlist_access ($playlist_id, $username) {
$dbh = dbh();
$sql = "SELECT playlist.id FROM playlist, user" .
" WHERE playlist.id = '$playlist_id'" .
" AND playlist.user = user.username" .
" AND user.username = '$username'";
$db_result = mysql_query($sql, $dbh);
if ( mysql_num_rows($db_result) == 1) {
return TRUE;
}
else {
if (!conf('use_auth')) {
return TRUE;
}
// check to see if this user is an admin
if ($user = get_user($username)) {
if ( $user->access == 'admin' ) {
return TRUE;
}
}
}
// If we get here, access is denied
return FALSE;
}
function show_playlist_dropdown ($playlist_id=0) {
global $settings;
$dbh = dbh();
$userid = scrub_in($_SESSION['userdata']['username']);
$sql = "SELECT * FROM playlist" .
" WHERE user = '$userid'" .
" AND name <> 'Temporary'" .
" ORDER BY name";
$db_result = @mysql_query($sql, $dbh);
print <<<ECHO
<select name="playlist_id">
<option value="0"> -New Playlist- </option>
ECHO;
while ( $r = @mysql_fetch_object($db_result) ) {
if ( $playlist_id == $r->id ) {
echo "<option value=\"" . $r->id . "\" selected=\"selected\">" . $r->name . "</option>\n";
}
else {
echo "<option value=\"" . $r->id . "\">" . $r->name . "</option>\n";
}
}
echo "</select>\n";
}
// Used to show when we have an access error for a playlist
function show_playlist_access_error ($playlist, $username) {
$plname = $playlist->name;
$pluser = new User($playlist->user);
$plowner = $pluser->username;
print <<<ECHO
<p style="font: 12px bold;"> Playlist Access Error </p>
<p>$username doesn't have access to update the '$plname' playlist, it is owned by $plowner.</p>
ECHO;
}
// Used to show a form with confirm action button on it (for deleting playlists, users, etc)
/*!
@function show_confirm_action
@discussion shows a confirmation of an action, gives a YES/NO choice
*/
function show_confirm_action ($text, $script, $arg) {
$web_path = conf('web_path');
require (conf('prefix') . "/templates/show_confirm_action.inc.php");
} // show_confirm_action
// search functions
function search_by_type ($type, $search) {
$dbh = dbh();
// supported types are album, artist and song
if ( $type == 'Album' ) {
$query = "SELECT id FROM album WHERE name LIKE '%$search%'";
}
elseif ( $type == 'Artist' ) {
$query = "SELECT id FROM artist WHERE name LIKE '%$search%'";
}
elseif ( $type == 'Song title' ) {
$query = "SELECT id FROM song WHERE title LIKE '%$search%'";
}
elseif ( $type == 'Genre' ) {
$query = "SELECT song.id as id FROM song, genre" .
" WHERE song.genre = genre.id" .
" AND genre.name LIKE '%$search%'";
}
$db_result = mysql_query($query, $dbh);
$search_result = array();
while ( $r = mysql_fetch_array($db_result) ) {
$search_result[] = $r;
}
return ($search_result);
}
function scrub_out($str) {
return stripslashes($str);
}
function unhtmlentities ($string) {
$trans_tbl = get_html_translation_table (HTML_ENTITIES);
$trans_tbl = array_flip ($trans_tbl);
$ret = strtr ($string, $trans_tbl);
return preg_replace('/&#(\d+);/me', "chr('\\1')",$ret);
}
function insert_album($album) {
global $settings;
$dbh = dbh();
preg_match("/^(A |An |The ){0,1}(.*?)$/i",$album, $matches);
$album = sql_escape($matches[2]);
switch($matches[1]) {
case 'The ':
case 'the ':
$prefix = 'The';
break;
case 'A ':
case 'a ':
$prefix = 'A';
break;
case 'An ':
case 'an ':
$prefix = 'An';
break;
default:
$prefix = '';
}
$sql = "INSERT INTO album (name, prefix)" .
" VALUES ( '$album', '$prefix' )";
$db_result = mysql_query($sql, $dbh);
return (mysql_insert_id($dbh));
} // insert_album
/*
* insert_artist()
*
*/
function insert_artist($artist) {
global $settings;
$dbh = dbh();
$matches = array();
$var = preg_match('/^(A |An |The ){0,1}(.*?)$/i',$artist, $matches);
$artist = sql_escape($matches[2]);
switch($matches[1]) {
case 'The ':
case 'the ':
$prefix = 'The';
break;
case 'A ':
case 'a ':
$prefix = 'A';
break;
case 'An ':
case 'an ':
$prefix = 'An';
break;
default:
$prefix = '';
}
$sql = "INSERT INTO artist (name, prefix)" .
" VALUES ( '$artist', '$prefix' )";
$db_result = mysql_query($sql, $dbh);
return (mysql_insert_id($dbh));
} // insert_artist
?>
|