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
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
|
<?php
/*
Copyright (c) 2001 - 2007 Ampache.org
All rights reserved.
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 2
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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/**
* UI Function Library
* This contains functions that are generic, and display information
* things like a confirmation box, etc and so forth
* @package Web Interface
* @catagory Library
*/
/**
* show_confirmation
* shows a confirmation of an action
* $next_url Where to go next
* $title The Title of the message
* $text The details of the message
* $cancel T/F show a cancel button that uses return_referrer()
*/
function show_confirmation($title,$text,$next_url,$cancel=0) {
if (substr_count($next_url,conf('web_path'))) {
$path = $next_url;
}
else {
$path = conf('web_path') . "/$next_url";
}
require (conf('prefix') . "/templates/show_confirmation.inc.php");
} // show_confirmation
/**
* flip_class
* takes an array of 2 class names
* and flips them back and forth and
* then echo's out [0]
*/
function flip_class($array=0) {
static $classes = array();
if ($array) {
$classes = $array;
}
else {
$classes = array_reverse($classes);
return $classes[0];
}
} // flip_class
/**
* clear_now_playing
* Clears the now playing information incase something has
* gotten stuck in there
*/
function clear_now_playing() {
$sql = "TRUNCATE TABLE `now_playing`";
$db_results = Dba::query($sql);
} // clear_now_playing
/**
* _
* checks to see if the alias _ is defined
* if it isn't it defines it as a simple return
*/
if (!function_exists('_')) {
function _($string) {
return $string;
} // _
} // if _ isn't defined
/**
* show_admin_menu
* shows the admin menu
*/
function show_admin_menu ($admin_highlight) {
include(conf('prefix') . "/templates/admin_menu.inc");
} // show_admin_menu
/**
* access_denied
* throws an error if they try to do something
* that they aren't allowed to
*/
function access_denied() {
echo "<br /><br /><br />";
echo "<div class=\"fatalerror\">" . _("Error Access Denied") . "</div>\n";
show_footer();
exit();
} // access_denied
/**
* return_referer
* returns the script part of the referer address passed by the web browser
* this is not %100 accurate. Also because this is not passed by us we need
* to clean it up, take the filename then check for a /admin/ and dump the rest
*/
function return_referer() {
$referer = $_SERVER['HTTP_REFERER'];
$file = basename($referer);
/* Strip off the filename */
$referer = substr($referer,0,strlen($referer)-strlen($file));
if (substr($referer,strlen($referer)-6,6) == 'admin/') {
$file = 'admin/' . $file;
}
return $file;
} // return_referer
/**
* show_alphabet_list
* shows the A-Z,0-9 lists for
* albums and artist pages
*/
function show_alphabet_list ($type,$script="artist.php",$selected="false",$action='match') {
$list = array(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,1,2,3,4,5,6,7,8,9,"0");
$style_name = "style_" . strtolower($selected);
${$style_name} = "style=\"font-weight:bold;\"";
unset($title);
echo "<div class=\"alphabet\">";
foreach ($list as $l) {
$style_name = "style_" . strtolower($l);
echo "<a href=\"". conf('web_path') ."/$script?action=$action&match=$l\" " . ${$style_name} . ">$l</a> | \n";
}
echo " <a href=\"". conf('web_path') ."/$script?action=$action&match=Browse\" $style_browse>" . _("Browse") . "</a> | \n";
if ($script == "albums.php") {
echo " <a href=\"". conf('web_path') ."/$script?action=$action&match=Show_missing_art\" $style_show_missing_art>" . _("Show w/o art") . "</a> | \n";
} // if we are on the albums page
echo " <a href=\"". conf('web_path') ."/$script?action=$action&match=Show_all\" $style_show_all>" . _("Show all") . "</a>";
echo "</div>\n";
} // show_alphabet_list
/**
* show_alphabet_form
* this shows the spiffy little form that acts as a "quick search" when browsing
* @package General
* @catagory Display
*/
function show_alphabet_form($match, $text, $action) {
require (conf('prefix') . '/templates/show_alphabet_form.inc.php');
} // show_alphabet_form
/**
* show_local_control
* shows the controls
* for localplay
*/
function show_local_control () {
require_once(conf('prefix') . "/templates/show_localplay.inc");
} // show_local_control
/**
* truncate_with_ellipse
* truncates a text file to specified length by adding
* thre dots (ellipse) to the end
* (Thx Nedko Arnaudov)
* @todo Fix Spelling!
* @depreciated
*/
function truncate_with_ellipse($text, $max=27) {
/* Run the function with the correct spelling */
return truncate_with_ellipsis($text,$max);
} // truncate_with_ellipse
/**
* truncate_with_ellipsis
* Correct Spelling function that truncates text to a specific lenght
* and appends three dots, or an ellipsis to the end
* @package Web Interface
* @catagory General
* @author Nedko Arnaudov
*/
function truncate_with_ellipsis($text, $max=27) {
/* If we want it to be shorter than three, just throw it back */
if ($max > 3) {
/* Make sure the functions exist before doing the iconv mojo */
if (function_exists('iconv') && function_exists('iconv_substr') && function_exists('iconv_strlen')) {
if (iconv_strlen($text, Config::get('site_charset')) > $max) {
$text = iconv_substr($text, 0, $max-3, Config::get('site_charset'));
$text .= iconv("ISO-8859-1", Config::get('site_charset'), "...");
}
}
/* Do normal substr if we don't have iconv */
else {
if (strlen($text) > $max) {
$text = substr($text,0,$max-3)."...";
}
} // else no iconv
} // else greater than 3
return $text;
} // truncate_with_ellipsis
/**
* show_footer
* shows the footer of the page
*/
function show_footer() {
require_once Config::get('prefix') . '/templates/footer.inc';
} // show_footer
/**
* show_now_playing
* shows the now playing template
*/
function show_now_playing() {
$web_path = Config::get('web_path');
$results = get_now_playing();
require Config::get('prefix') . '/templates/show_now_playing.inc.php';
} // show_now_playing
/**
* show_user_registration
* this function is called for a new user
* registration
* @author Terry
* @todo Fix so that it recieves an array of values for the user reg rather than seperate
*/
function show_user_registration ($values=array()) {
require (conf('prefix') . "/templates/show_user_registration.inc.php");
} // show_user_registration
/**
* show_play_selected
* this shows the playselected/add to playlist
* box, which includes a little javascript
*/
function show_play_selected() {
require (conf('prefix') . "/templates/show_play_selected.inc.php");
} // show_play_selected
/**
* get_now_playing
* gets the now playing information
* @package Web Interface
* @catagory Get
*/
function get_now_playing($filter='') {
$sql = "SELECT `song_id`,`user` FROM `now_playing` ORDER BY `id` DESC";
$db_results = Dba::query($sql);
$results = array();
/* While we've got stuff playing */
while ($r = Dba::fetch_assoc($db_results)) {
$song = new Song($r['song_id']);
$song->format();
$np_user = new User($r['user']);
$results[] = array('song'=>$song,'user'=>$np_user);
} // end while
return $results;
} // get_now_playing
/*
* Artist Ratings - Implemented by SoundOfEmotion
*
* set_artist_rating()
*
* check to see if the ratings exist
* if they do: update them
* if they don't: insert them
*
*/
function set_artist_rating($artist_id, $rate_user, $rating) {
$artist_id = sql_escape($artist_id);
$sql = "SELECT * FROM ratings WHERE user='$rate_user' AND object_type='artist' AND object_id='$artist_id'";
$db_result = mysql_query( $sql, dbh() );
$r = mysql_fetch_row( $db_result );
if($r[0]) {
$sql2 = "UPDATE ratings SET user_rating='$rating' WHERE object_id='$artist_id' AND user='$rate_user' AND object_type='artist'";
$db_result2 = mysql_query( $sql2, dbh() );
$r = mysql_fetch_row( $db_result2 );
return mysql_insert_id( dbh() );
}
else if(!$r[0]) {
$sql2 = "INSERT INTO ratings (id,user,object_type,object_id,user_rating) ".
"VALUES ('','$rate_user','artist','$artist_id','$rating')";
$db_result2 = mysql_query( $sql2, dbh() );
return mysql_insert_id(dbh() );
}
else{
return "NA";
}
} // set_artist_rating()
/*
* Album Ratings - Implemented by SoundOfEmotion
*
* set_album_rating()
*
* check to see if the ratings exist
* if they do: update them
* if they don't: insert them
*
*/
function set_album_rating($album_id, $rate_user, $rating) {
$album_id = sql_escape($album_id);
$sql = "SELECT * FROM ratings WHERE user='$rate_user' AND object_type='album' AND object_id='$album_id'";
$db_result = mysql_query( $sql, dbh() );
$r = mysql_fetch_row( $db_result );
if($r[0]) {
$sql2 = "UPDATE ratings SET user_rating='$rating' WHERE object_id='$album_id' AND user='$rate_user' AND object_type='album'";
$db_result2 = mysql_query( $sql2, dbh() );
return mysql_insert_id( dbh() );
}
else if(!$r[0]) {
$sql2 = "INSERT INTO ratings (id,user,object_type,object_id,user_rating) ".
"VALUES ('','$rate_user','album','$album_id','$rating')";
$db_result2 = mysql_query( $sql2, dbh() );
return mysql_insert_id( dbh() );
}
else{
return "NA";
}
} // set_album_rating()
/*
* Song Ratings - Implemented by SoundOfEmotion
*
* set_song_rating()
*
* check to see if the ratings exist
* if they do: update them
* if they don't: insert them
*
*/
function set_song_rating($song_id, $rate_user, $rating) {
$song_id = sql_escape($song_id);
$sql = "SELECT * FROM ratings WHERE user='$rate_user' AND object_type='song' AND object_id='$song_id'";
$db_result = mysql_query( $sql, dbh() );
$r = mysql_fetch_row( $db_result );
if($r[0]){
$sql2 = "UPDATE ratings SET user_rating='$rating' WHERE object_id='$song_id' AND user='$rate_user' AND object_type='song'";
$db_result2 = mysql_query( $sql2, dbh() );
return mysql_insert_id( dbh() );
}
else if(!$r[0]){
$sql2 = "INSERT INTO ratings (id,user,object_type,object_id,user_rating) ".
"VALUES ('','$rate_user','song','$song_id','$rating')";
$db_result2 = mysql_query( $sql2, dbh() );
return mysql_insert_id( dbh() );
}
else{
return "NA";
}
} // set_song_rating()
/**
* show_clear
* this is a hack because of the float mojo it clears the floats
* @package Web Interface
* @catagory Hack-o-Rama
* @author Karl Vollmer
*/
function show_clear() {
echo "\n<br style=\"clear:both;\" />\n";
} // show_clear
/**
* show_page_footer
* adds page footer including html and body end tags
* @param $menu menu item to highlight
* @param $admin_menu admin menu item to highlight
* @param $display_menu display menu or not (1 on 0 off)
* @package Web Interface
* @catagory Display
*/
function show_page_footer($menu="Home", $admin_menu='', $display_menu=0) {
if ($display_menu){
if($menu == 'Admin'){
show_admin_menu($admin_menu);
} // end if admin
show_menu_items($menu);
} // end if
show_template('footer');
} // show_page_footer
/**
* Show All Popular
* This functions shows all of the possible global popular tables, this is basicly a top X where X is
* set on a per user basis
* @package Web Interface
* @catagory Display
* @author Karl Vollmer
*/
function show_all_popular() {
$artists = get_global_popular('artist');
$albums = get_global_popular('album');
$songs = get_global_popular('song');
$genres = get_global_popular('genre');
require_once Config::get('prefix') . '/templates/show_all_popular.inc.php';
} // show_all_popular
/**
* Show All Recent
* This function shows all of the possible "Newest" tables. The number of newest is pulled from the users
* popular threshold
* @package Web Interface
* @catagory Display
* @author Karl Vollmer
*/
function show_all_recent($limit='') {
$artists = Stats::get_newest('artist',$limit);
$albums = Stats::get_newest('album',$limit);
require_once Config::get('prefix') . '/templates/show_all_recent.inc.php';
} // show_all_recent
/**
* show_local_catalog_info
* Shows the catalog stats
* @package Web INterface
* @catagory Display
*/
function show_local_catalog_info() {
/* Before we display anything make sure that they have a catalog */
$query = "SELECT * FROM catalog";
$db_results = Dba::query($query);
// Make sure we have something to display
if (!Dba::num_rows($db_results)) {
show_box_top();
$items[] = "<span align=\"center\" class=\"error\">" . _('No Catalogs Found!') . "</span><br />";
$items[] = "<a href=\"" . Config::get('web_path') . "/admin/catalog.php?action=show_add_catalog\">" ._('Add a Catalog') . "</a>";
show_info_box('','catalog',$items);
show_box_bottom();
return false;
}
$results = Catalog::get_stats();
$hours = floor($results['time']/3600);
$size = $results['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";
}
require Config::get('prefix') . '/templates/show_local_catalog_info.inc.php';
} // show_local_catalog_info
/**
* img_resize
* this automaticly resizes the image for thumbnail viewing
* only works on gif/jpg/png this function also checks to make
* sure php-gd is enabled
*/
function img_resize($image,$size,$type,$album_id) {
/* Make sure they even want us to resize it */
if (!Config::get('resize_images')) {
return $image['art'];
}
// Already resized
if ($image['resized']) {
debug_event('using_resized','using resized image for Album:' . $album_id,'2');
return $image['art'];
}
$image = $image['art'];
if (!function_exists('gd_info')) { return false; }
/* First check for php-gd */
$info = gd_info();
if ( ($type == 'jpg' OR $type == 'jpeg') AND !$info['JPG Support']) {
return false;
}
elseif ($type == 'png' AND !$info['PNG Support']) {
return false;
}
elseif ($type == 'gif' AND !$info['GIF Create Support']) {
return false;
}
$src = imagecreatefromstring($image);
if (!$src) { return false; }
$width = imagesx($src);
$height = imagesy($src);
$new_w = $size['width'];
$new_h = $size['height'];
$img = imagecreatetruecolor($new_w,$new_h);
if (!imagecopyresampled($img,$src,0,0,0,0,$new_w,$new_h,$width,$height)) {
return false;
}
ob_start();
// determine image type and send it to the client
switch ($type) {
case 'jpg':
case 'jpeg':
imagejpeg($img,null,100);
break;
case 'gif':
imagegif($img,null,100);
break;
case 'png':
imagepng($img,null,100);
break;
}
// Grab this image data and save it into the thumbnail
$data = ob_get_contents();
ob_end_clean();
// If our image create failed don't save it, just return
if (!$data) {
debug_event('IMG_RESIZE','Failed to resize Art from Album:' . $album_id,'3');
return $image;
}
// Save what we've got
Album::save_resized_art($data,'image/' . $type,$album_id);
return $data;
} // img_resize
/**
* show_genres
* this shows the 'many' genre form, it takes an array of genre objects and the view object
* @package Genre
* @catagory Display
*/
function show_genres($genres,$view) {
require (conf('prefix') . '/templates/show_genres.inc.php');
} // show_genres
/**
* show_genre
* this shows a single genre item which is basicly just a link to the albums/artists/songs of said genre
* @package Genre
* @catagory Display
*/
function show_genre($genre_id) {
$genre = new Genre($genre_id);
require (conf('prefix') . '/templates/show_genre.inc.php');
} // show_genre
function show_random_play_bar() {
require (conf('prefix') . '/templates/show_random_play_bar.inc.php');
} // show_random_play_bar()
/*
* show_artist_pulldown()
*
* Helper functions for album and artist functions
*
*/
function show_artist_pulldown ($artist_id,$select_name='artist') {
$query = "SELECT id FROM artist ORDER BY name";
$db_result = mysql_query($query, dbh());
echo "\n<select name=\"$select_name\">\n";
while ($r = mysql_fetch_assoc($db_result)) {
$artist = new Artist($r['id']);
$artist->get_count();
if ( $artist_id == $r['id'] ) {
echo "\t<option value=\"" . $artist->id . "\" selected=\"selected\">". scrub_out($artist->name) . "</option>\n";
}
else {
echo "\t<option value=\"" . $artist->id . "\">". scrub_out($artist->name) ."</option>\n";
}
} // end while fetching artists
echo "</select>\n";
} // show_artist_pulldown
/**
* show_catalog_pulldown
* This has been changed, first is the name of the
* dropdown select, the second is the style to be applied
*
*/
function show_catalog_pulldown ($name='catalog',$style) {
$sql = "SELECT `id`,`name` FROM `catalog` ORDER BY `name`";
$db_result = Dba::query($sql);
echo "\n<select name=\"" . $name . "\" style=\"" . $style . "\">\n";
echo "<option value=\"-1\">" . _('All') . "</option>\n";
while ($r = Dba::fetch_assoc($db_result)) {
$catalog_name = scrub_out($r['name']);
if ( $catalog == $r['id'] ) {
echo " <option value=\"" .$r['id'] . "\" selected=\"selected\">$catalog_name</option>\n";
}
else {
echo " <option value=\"" . $r['id'] . "\">$catalog_name</option>\n";
}
}
echo "\n</select>\n";
} // show_catalog_pulldown
/**
* show_submenu
* This shows the submenu mojo for the sidebar, and I guess honestly anything
* else you would want it to... takes an array of items which have ['url'] ['title']
* and ['active']
*/
function show_submenu($items) {
require Config::get('prefix') . '/templates/subnavbar.inc.php';
} // show_submenu
/**
* get_location
* This function gets the information about said persons currently location
* this is used for A) Sidebar highlighting & submenu showing and B) Titlebar information
* it returns an array of information about what they are currently doing
* Possible array elements
* ['title'] Text name for the page
* ['page'] actual page name
* ['section'] name of the section we are in, admin, browse etc (submenu control)
* @package General
*/
function get_location() {
$location = array();
if (strlen($_SERVER['PHP_SELF'])) {
$source = $_SERVER['PHP_SELF'];
}
else {
$source = $_SERVER['REQUEST_URI'];
}
/* Sanatize the $_SERVER['PHP_SELF'] variable */
$source = str_replace(Config::get('raw_web_path'),"",$source);
$location['page'] = preg_replace("/^\/(.+\.php)\/?.*/","$1",$source);
switch ($location['page']) {
case 'index.php':
$location['title'] = _('Home');
break;
case 'upload.php':
$location['title'] = _('Upload');
break;
case 'localplay.php':
$location['title'] = _('Local Play');
break;
case 'randomplay.php':
$location['title'] = _('Random Play');
break;
case 'playlist.php':
$location['title'] = _('Playlist');
break;
case 'search.php':
$location['title'] = _('Search');
break;
case 'preferences.php':
$location['title'] = _('Preferences');
break;
case 'admin/index.php':
$location['title'] = _('Admin-Catalog');
$location['section'] = 'admin';
break;
case 'admin/catalog.php':
$location['title'] = _('Admin-Catalog');
$location['section'] = 'admin';
break;
case 'admin/users.php':
$location['title'] = _('Admin-User Management');
$location['section'] = 'admin';
break;
case 'admin/mail.php':
$location['title'] = _('Admin-Mail Users');
$location['section'] = 'admin';
break;
case 'admin/access.php':
$location['title'] = _('Admin-Manage Access Lists');
$location['section'] = 'admin';
break;
case 'admin/preferences.php':
$location['title'] = _('Admin-Site Preferences');
$location['section'] = 'admin';
break;
case 'admin/modules.php':
$location['title'] = _('Admin-Manage Modules');
$location['section'] = 'admin';
break;
case 'browse.php':
$location['title'] = _('Browse Music');
$location['section'] = 'browse';
break;
case 'albums.php':
$location['title'] = _('Albums');
$location['section'] = 'browse';
break;
case 'artists.php':
$location['title'] = _('Artists');
$location['section'] = 'browse';
break;
case 'genre.php':
$location['title'] = _('Genre');
$location['section'] = 'browse';
break;
case 'stats.php':
$location['title'] = _('Statistics');
break;
default:
$location['title'] = '';
break;
} // switch on raw page location
return $location;
} // get_location
/**
* show_preference_box
* This shows the preference box for the preferences pages
* it takes a chunck of the crazy preference array and then displays it out
* it does not contain the <form> </form> tags
*/
function show_preference_box($preferences) {
require Config::get('prefix') . '/templates/show_preference_box.inc.php';
} // show_preference_box
/**
* show_genre_pulldown
* This shows a select of all of the genres, it takes the name of the select
* the currently selected and then the size
*
*/
function show_genre_pulldown ($name,$selected='',$size=1,$width=0,$style='') {
/* Get them genre hippies */
$sql = "SELECT genre.id,genre.name FROM genre ORDER BY genre.name";
$db_result = Dba::query($sql);
if ($size > 0) {
$multiple_txt = "multiple=\"multiple\" size=\"$size\"";
}
if ($style) {
$style_txt = "style=\"$style\"";
}
echo "<select name=\"" . $name . "[]\" $multiple_txt $style_txt>\n";
echo "\t<option value=\"-1\">" . _("All") . "</option>\n";
while ($r = Dba::fetch_assoc($db_result)) {
if ($width > 0) {
$r['name'] = truncate_with_ellipsis($r['name'],$width);
}
$r['name'] = scrub_out($r['name']);
if ( $selected == $r['id'] ) {
echo "\t<option value=\"" . $r['id'] . "\" selected=\"selected\">" . $r['name'] . "</option>\n";
}
else {
echo " <option value=\"" . $r['id'] . "\">" . $r['name'] . "</option>\n";
}
} // end while
echo "</select>\n";
} // show_genre_pulldown
/**
* good_email
* Don't get me started... I'm sure the indenting is still wrong on this
* it shouldn't be named this, it should be documented, yea this needs
* some serious MOJO work
*/
function good_email($email) {
// First check that there's one @ symbol, and that the lengths are good
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
return false;
}
}
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
return false;
}
}
}
return true;
} //good_email
/**
* str_rand
*
*
*/
function str_rand($length = 8, $seeds = 'abcdefghijklmnopqrstuvwxyz0123456789'){
$str = '';
$seeds_count = strlen($seeds);
// Seed
list($usec, $sec) = explode(' ', microtime());
$seed = (float) $sec + ((float) $usec * 100000);
mt_srand($seed);
// Generate
for ($i = 0; $length > $i; $i++) {
$str .= $seeds{mt_rand(0, $seeds_count - 1)};
}
return $str;
} //str_rand
/**
* send_confirmation
*
*
*/
function send_confirmation($username, $fullname, $email, $password, $validation) {
$title = conf('site_title');
$from = "From: Ampache <".conf('mail_from').">";
$body = "Welcome to $title
Please keep this email for your records. Your account information is as follows:
----------------------------
Username: $username
Password: $password
----------------------------
Your account is currently inactive. You cannot use it until you visit the following link:
"
. conf('web_path'). "/activate.php?mode=activate&u=$username&act_key=$validation
Please do not forget your password as it has been encrypted in our database and we cannot retrieve it for you. However, should you forget your password you can request a new one which will be activated in the same way as this account.
Thank you for registering.";
mail($email, "Welcome to $title" , $body, $from);
if (conf('admin_notify_reg')){
$admin_body = "A new user has registered at $title
The following values where entered;
Username: $username
Fullname: $fullname
E-Mail: $email
Click here to view user:
"
. conf('web_path') . "/admin/users.php?action=edit&user=$username";
mail (conf('mail_from'), "New user registration at $title", $admin_body, $from);
}
} //send_confirmation
/**
* show_registration_agreement
* This function reads in /config/registration_agreement.php
* Plaintext Only
*/
function show_registration_agreement() {
$filename = conf('prefix') . '/config/registration_agreement.php';
/* Check for existance */
$fp = fopen($filename,'r');
if (!$fp) { return false; }
$data = fread($fp,filesize($filename));
/* Scrub and show */
echo $data;
} // show_registration_agreement
/**
* show_playlist_import
* This shows the playlist import templates
*/
function show_playlist_import() {
require (conf('prefix') . '/templates/show_import_playlist.inc.php');
} // show_playlist_import
/**
* show_songs
* Still not happy with this function, but at least it's in the right
* place now
*/
function show_songs ($song_ids, $playlist, $album=0) {
$dbh = dbh();
$totaltime = 0;
$totalsize = 0;
require (conf('prefix') . "/templates/show_songs.inc");
return true;
} // show_songs
/**
* show_album_select
* This displays a select of every album that we've got in Ampache, (it can be hella long) it's used
* by the Edit page, it takes a $name and a $album_id
*/
function show_album_select($name='album',$album_id=0) {
echo "<select name=\"$name\">\n";
$sql = "SELECT id, name, prefix FROM album ORDER BY name";
$db_results = mysql_query($sql, dbh());
while ($r = mysql_fetch_assoc($db_results)) {
$selected = '';
$album_name = trim($r['prefix'] . " " . $r['name']);
if ($r['id'] == $album_id) {
$selected = "selected=\"selected\"";
}
echo "\t<option value=\"" . $r['id'] . "\" $selected>" . scrub_out($album_name) . "</option>\n";
} // end while
echo "</select>\n";
} // show_album_select
/**
* show_artist_select
* This is the same as the album select except it's *gasp* for artists how inventive!
*/
function show_artist_select($name='artist', $artist_id=0) {
echo "<select name=\"$name\">\n";
$sql = "SELECT id, name, prefix FROM artist ORDER BY name";
$db_results = mysql_query($sql, dbh());
while ($r = mysql_fetch_assoc($db_results)) {
$selected = '';
$artist_name = trim($r['prefix'] . " " . $r['name']);
if ($r['id'] == $artist_id) {
$selected = "selected=\"selected\"";
}
echo "\t<option value=\"" . $r['id'] . "\" $selected>" . scrub_out($artist_name) . "</option>\n";
} // end while
echo "</select>\n";
} // show_artist_select
/**
* show_genre_select
* It's amazing we have three of these funtions now, this one shows a select of genres and take s name
* and a selected genre... Woot!
*/
function show_genre_select($name='genre',$genre_id=0) {
echo "<select name=\"$name\">\n";
$sql = "SELECT id, name FROM genre ORDER BY name";
$db_results = mysql_query($sql, dbh());
while ($r = mysql_fetch_assoc($db_results)) {
$selected = '';
$genre_name = $r['name'];
if ($r['id'] == $genre_id) {
$selected = "selected=\"selected\"";
}
echo "\t<option value=\"" . $r['id'] . "\" $selected>" . scrub_out($genre_name) . "</option>\n";
} // end while
echo "</select>\n";
} // show_genre_select
/**
* show_catalog_select
* Yet another one of these buggers. this shows a drop down of all of your catalogs
*/
function show_catalog_select($name='catalog',$catalog_id=0,$style='') {
echo "<select name=\"$name\" style=\"$style\">\n";
$sql = "SELECT id, name FROM catalog ORDER BY name";
$db_results = mysql_query($sql, dbh());
while ($r = mysql_fetch_assoc($db_results)) {
$selected = '';
if ($r['id'] == $catalog_id) {
$selected = "selected=\"selected\"";
}
echo "\t<option value=\"" . $r['id'] . "\" $selected>" . scrub_out($r['name']) . "</option>\n";
} // end while
echo "</select>\n";
} // show_catalog_select
/**
* show_user_select
* This one is for users! shows a select/option statement so you can pick a user
* to blame
*/
function show_user_select($name,$selected='',$style='') {
echo "<select name=\"$name\" style=\"$style\">\n";
echo "\t<option value=\"\">" . _('None') . "</option>\n";
$sql = "SELECT username as id,fullname FROM user ORDER BY fullname";
$db_results = mysql_query($sql, dbh());
while ($r = mysql_fetch_assoc($db_results)) {
$select_txt = '';
if ($r['id'] == $selected) {
$select_txt = 'selected="selected"';
}
echo "\t<option value=\"" . $r['id'] . "\" $select_txt>" . scrub_out($r['fullname']) . "</option>\n";
} // end while users
} // show_user_select
/**
* show_box_top
* This function requires the top part of the box
* it takes title as an optional argument
*/
function show_box_top($title='') {
require Config::get('prefix') . '/templates/show_box_top.inc.php';
} // show_box_top
/**
* show_box_bottom
* This function requires the bottom part of the box
* it does not take any arguments
*/
function show_box_bottom() {
require Config::get('prefix') . '/templates/show_box_bottom.inc.php';
} // show_box_bottom
/**
* get_user_icon
* this function takes a name and a returns either a text representation
* or an <img /> tag
*/
function get_user_icon($name,$hover_name='') {
/* Because we do a lot of calls cache the URLs */
static $url_cache = array();
if (isset($url_cache[$name])) {
$img_url = $url_cache[$name];
$cache_url = true;
}
if (isset($url_cache[$hover_name])) {
$hover_url = $url_cache[$hover_name];
$cache_hover = true;
}
if (empty($hover_name)) { $cache_hover = true; }
if (!isset($cache_url) OR !isset($cache_hover)) {
$icon_name = 'icon_' . $name . '.png';
/* Build the image url */
if (file_exists(Config::get('prefix') . '/themes/' . Config::get('theme_path') . '/images/' . $icon_name)) {
$img_url = Config::get('web_path') . Config::get('theme_path') . '/images/' . $icon_name;
}
else {
$img_url = Config::get('web_path') . '/images/' . $icon_name;
}
/* If Hover, then build its url */
if (!empty($hover_name)) {
$hover_icon = 'icon_' . $hover_name . '.png';
if (file_exists(Config::get('prefix') . '/themes/' . Config::get('theme_path') . '/images/' . $icon_name)) {
$hov_url = Config::get('web_path') . Config::get('theme_path') . '/images/' . $hover_icon;
}
else {
$hov_url = Config::get('web_path') . '/images/' . $hover_icon;
}
$hov_txt = "onMouseOver=\"this.src='$hov_url'; return true;\" onMouseOut=\"this.src='$img_url'; return true;\"";
} // end hover
} // end if not cached
$string = "<img style=\"cursor: pointer;\" src=\"$img_url\" border=\"0\" alt=\"" . ucfirst($name) . "\" title=\"" . ucfirst($name) . "\" $hov_txt/>";
return $string;
} // show_icon
/**
* xml_from_array
* This takes a one dimensional array and
* creates a XML document form it for use
* primarly by the ajax mojo
*/
function xml_from_array($array,$callback=0,$type='') {
switch ($type) {
case 'itunes':
foreach ($array as $key=>$value) {
if (is_array($value)) {
$value = xml_from_array($value,1,$type);
$string .= "\t\t<$key>\n$value\t\t</$key>\n";
}
else {
if ($key == "key"){
$string .= "\t\t<$key>$value</$key>\n";
} elseif (is_numeric($value)) {
$string .= "\t\t\t<key>$key</key><integer>$value</integer>\n";
} elseif ($key == "Date Added") {
$string .= "\t\t\t<key>$key</key><date>$value</date>\n";
} elseif (is_string($value)) {
/* We need to escape the value */
$string .= "\t\t\t<key>$key</key><string><![CDATA[$value]]></string>\n";
}
}
}
return $string;
break;
case 'xspf':
foreach ($array as $key=>$value) {
if (is_array($value)) {
$value = xml_from_array($value,1,$type);
$string .= "\t\t<$key>\n$value\t\t</$key>\n";
}
else {
if ($key == "key"){
$string .= "\t\t<$key>$value</$key>\n";
} elseif (is_numeric($value)) {
$string .= "\t\t\t<$key>$value</$key>\n";
} elseif (is_string($value)) {
/* We need to escape the value */
$string .= "\t\t\t<$key><![CDATA[$value]]></$key>\n";
}
}
}
return $string;
break;
default:
foreach ($array as $key=>$value) {
if (is_numeric($key)) { $key = 'item'; }
if (is_array($value)) {
$value = xml_from_array($value,1);
$string .= "\t<content div=\"$key\">$value</content>\n";
}
else {
/* We need to escape the value */
$string .= "\t<content div=\"$key\"><![CDATA[$value]]></content>\n";
}
// end foreach elements
}
if (!$callback) {
$string = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<root>\n" . $string . "</root>\n";
}
return $string;
break;
}
} // xml_from_array
/**
* xml_get_header
* This takes the type and returns the correct xml header
*/
function xml_get_header($type){
switch ($type){
case 'itunes':
$header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" .
"<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\"\n" .
"\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" .
"<plist version=\"1.0\">\n" .
"<dict>\n" .
" <key>Major Version</key><integer>1</integer>\n" .
" <key>Minor Version</key><integer>1</integer>\n" .
" <key>Application Version</key><string>7.0.2</string>\n" .
" <key>Features</key><integer>1</integer>\n" .
" <key>Show Content Ratings</key><true/>\n" .
" <key>Tracks</key>\n" .
" <dict>\n";
return $header;
break;
case 'xspf':
$header = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" .
"<playlist version = \"1\" xmlns=\"http://xspf.org/ns/0/\">\n ".
"<title>Ampache XSPF Playlist</title>\n" .
"<creator>" . conf('site_title') . "</creator>\n" .
"<annotation>" . conf('site_title') . "</annotation>\n" .
"<info>". conf('web_path') ."</info>\n" .
"<trackList>\n\n\n\n";
return $header;
break;
default:
$header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
return $header;
break;
}
} //xml_get_header
/**
* xml_get_footer
* This takes the type and returns the correct xml footer
*/
function xml_get_footer($type){
switch ($type){
case 'itunes':
$footer = " </dict>\n" .
"</dict>\n" .
"</plist>\n";
return $footer;
break;
case 'xspf':
$footer = " </trackList>\n" .
"</playlist>\n";
return $footer;
break;
default:
break;
}
} //xml_get_footer
/**
* get_users
* This returns an array of user objects and takes an sql statement
*/
function get_users($sql) {
$db_results = mysql_query($sql,dbh());
$results = array();
while ($u = mysql_fetch_assoc($db_results)) {
$results[] = new User($u['id']);
}
return $results;
} // get_users
?>
|