summaryrefslogtreecommitdiffstats
path: root/lib/preferences.php
blob: c33980f3ba96ddd04bdc792c87d72db602d24588 (plain)
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
<?php
/*

 Copyright (c) 2001 - 2006 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.

*/
/*!
	@header Preferences Library
	@discussion This contains all of the functions needed for the preferences
*/

/*!
	@function get_site_preferences
	@discussion gets all of the preferences for this Ampache site
*/
function get_site_preferences() { 

	$results = array();

	$sql = "SELECT preferences.name, preferences.type, user_preference.value, preferences.description FROM preferences,user_preference " .
		" WHERE preferences.id=user_preference.preference AND user_preference.user = '-1' ORDER BY `type`,`name`";
	$db_results = mysql_query($sql, dbh());

	while ($r = mysql_fetch_object($db_results)) { 
		$results[] = $r;
	}

	return $results;

} // get_site_preferences

/*!
	@function set_site_preferences
	@discussion sets the conf() function with the current site preferences from the db
*/
function set_site_preferences() { 

	$results = array();

	$sql = "SELECT preferences.name,user_preference.value FROM preferences,user_preference WHERE user='-1' AND user_preference.preference=preferences.id";
	$db_results = @mysql_query($sql, dbh());

	while ($r = @mysql_fetch_object($db_results)) { 
		$results[$r->name] = $r->value;
	} // db results

	if (strlen($results['theme_name']) > 0) { 
		$results['theme_path'] = "/themes/" . $results['theme_name'];
	}

	conf($results,1);

} // set_site_preferences

/*!
	@function clean_preference_name
	@discussion s/_/ /g & upper case first
*/
function clean_preference_name($name) { 

	$name = str_replace("_"," ",$name);
	$name = ucwords($name);

	return $name;

} // clean_preference_name

/*!
	@function update_preferences
	@discussion grabs the current keys that should be added
		and then runs throught $_REQUEST looking for those
		values and updates them for this user
*/
function update_preferences($pref_id=0) { 
	
	$pref_user = new User($pref_id);
	
	/* Get current keys */
	$sql = "SELECT id,name,type FROM preferences";

	/* If it isn't the System Account's preferences */
	if ($pref_id != '-1') { $sql .= " WHERE type!='system'"; }
	
	$db_results = mysql_query($sql, dbh());

	// Collect the current possible keys
	while ($r = mysql_fetch_assoc($db_results)) { 
		$results[] = array('id' => $r['id'], 'name' => $r['name'],'type' => $r['type']);
	} // end collecting keys

	/* Foreach through possible keys and assign them */
	foreach ($results as $data) { 
		/* Get the Value from POST/GET var called $data */
		$type 		= $data['type'];
		$name 		= $data['name'];
		$apply_to_all	= "check_" . $data['name'];
		$id		= $data['id'];
		$value 		= sql_escape(scrub_in($_REQUEST[$name]));

		/* Some preferences require some extra checks to be performed */
		switch ($name) { 
			case 'theme_name':
				// If the theme exists and it's different then our current one reset the colors
				if (theme_exists($value) AND $pref_user->prefs['theme_name'] != $value) { 
					set_theme_colors($value,$pref_id);
				}
			break;
			case 'sample_rate':
				$value = validate_bitrate($value);
			break;
			default: 
			break;
		}

		/* Run the update for this preference only if it's set */
		if (isset($_REQUEST[$name])) { 
			update_preference($pref_id,$name,$id,$value);
		}

	} // end foreach preferences


} // update_preferences

/**
 * update_preference
 * This function updates a single preference and is called by the update_preferences function
 * @package Preferences
 * @catagory Update
 */
function update_preference($username,$name,$pref_id,$value) { 

	$apply_check = "check_" . $name;

	/* First see if they are an administrator and we are applying this to everything */
	if ($GLOBALS['user']->has_access(100) AND make_bool($_REQUEST[$apply_check])) { 
		$sql = "UPDATE user_preference SET `value`='$value' WHERE preference='$pref_id'";
		$db_results = mysql_query($sql, dbh());
		/* Reset everyones colors! */
		if ($name =='theme_name') { 
			set_theme_colors($value,0);
		}
		return true;
	}
	
	/* Else make sure that the current users has the right to do this */
	if (has_preference_access($name)) { 
		$sql = "UPDATE user_preference SET `value`='$value' WHERE preference='$pref_id' AND user='$username'";
		$db_resutls = mysql_query($sql, dbh());
		return true;
	}

	return false;

} // update_preference

/*!
	@function has_preference_access
	@discussion makes sure that the user has sufficient
		rights to actually set this preference, handle
		as allow all, deny X
	//FIXME:
	// This is no longer needed, we just need to check against preferences.level
*/
function has_preference_access($name) { 

        if (conf('demo_mode')) {
	        return false;
        }

	switch($name) {

		case 'download':
		case 'upload':
		case 'quarantine':
		case 'upload_dir':
		case 'sample_rate':
		case 'direct_link':
			$level = 100;
		break;
		default:
			$level = 25;
		break;
	} // end switch key


	if ($GLOBALS['user']->has_access($level)) { 
		return true;
	}

	return false;

} // has_preference_access


/*!
	@function create_preference_input
	@discussion takes the key and then creates
		the correct type of input for updating it
*/
function create_preference_input($name,$value) { 

	$len = strlen($value);
	if ($len <= 1) { $len = 8; }

	if (!has_preference_access($name)) { 
		if ($value == '1') { 
			echo "Enabled";
		}
		elseif ($value == '0') { 
			echo "Disabled";
		}
		else {
			echo $value; 
		}
		return;
	} // if we don't have access to it

	switch($name) {
		case 'display_menu':
		case 'download':
		case 'quarantine':
		case 'upload':
		case 'access_list':
		case 'lock_songs':
		case 'xml_rpc':
		case 'force_http_play':
		case 'no_symlinks':
		case 'use_auth':
		case 'access_control':
		case 'demo_mode':
		case 'condPL':
		case 'direct_link':
			if ($value == '1') { $is_true = "selected=\"selected\""; } 
			else { $is_false = "selected=\"selected\""; }
			echo "<select name=\"$name\">\n";
			echo "\t<option value=\"1\" $is_true>" . _("Enable") . "</option>\n";
			echo "\t<option value=\"0\" $is_false>" . _("Disable") . "</option>\n";
			echo "</select>\n";
		break;
		case 'play_type':
			if ($value == 'local_play') { $is_local = "selected=\"selected\""; }
			elseif ($value == 'icecast2') { $is_ice = "selected=\"selected\""; }
			elseif ($value == 'downsample') { $is_down = "selected=\"selected\""; }
			elseif ($value == 'mpd') { $is_mpd = "selected=\"selected\""; }
			elseif ($value == 'slim') { $is_slim = "selected=\"selected\""; }
			else { $is_stream = "selected=\"selected\""; } 
			echo "<select name=\"$name\">\n";
			if (conf('allow_local_playback')) { 
				echo "\t<option value=\"local_play\" $is_local>" . _("Local") . "</option>\n";
			}
			if (conf('allow_stream_playback')) { 
				echo "\t<option value=\"stream\" $is_stream>" . _("Stream") . "</option>\n";
			}
			if (conf('allow_icecast_playback')) { 
				echo "\t<option value=\"icecast2\" $is_ice>" . _("IceCast") . "</option>\n";
			}
			if (conf('allow_downsample_playback')) { 
				echo "\t<option value=\"downsample\" $is_down>" . _("Downsample") . "</option>\n";
			}
			if (conf('allow_mpd_playback')) { 
				echo "\t<option value=\"mpd\" $is_mpd>" . _("Music Player Daemon") . "</option>\n";
			}
			if (conf('allow_slim_playback')) { 
				echo "\t<option value=\"slim\" $is_slim>" . _("SlimServer") . "</option>\n";
			}
			
			echo "</select>\n";
		break;
		case 'playlist_type':
			$var_name = $value . "_type";
			${$var_name} = "selected=\"selected\""; 
			echo "<select name=\"$name\">\n";
			echo "\t<option value=\"m3u\" $m3u_type>" . _("M3U") . "</option>\n";
			echo "\t<option value=\"simple_m3u\" $simple_m3u_type>" . _("Simple M3U") . "</option>\n";
			echo "\t<option value=\"pls\" $pls_type>" . _("PLS") . "</option>\n";
			echo "\t<option value=\"asx\" $asx_type>" . _("Asx") . "</option>\n";
			echo "\t<option value=\"ram\" $ram_type>" . _("RAM") . "</option>\n";
			echo "</select>\n";
		break;
		case 'lang':
			$languages = get_languages();
			$var_name = $value . "_lang";
			${$var_name} = "selected=\"selected\"";
			
			echo "<select name=\"$name\">\n";
			
			foreach ($languages as $lang=>$name) { 
				$var_name = $lang . "_lang";
				
				echo "\t<option value=\"$lang\" " . ${$var_name} . ">$name</option>\n";
			} // end foreach
			echo "</select>\n";
		break;
		case 'theme_name':
			$themes = get_themes();
			echo "<select name=\"$name\">\n";
			foreach ($themes as $theme) { 
				$is_selected = "";
				if ($value == $theme['path']) { $is_selected = "selected=\"selected\""; }
				echo "\t<option value=\"" . $theme['path'] . "\" $is_selected>" . $theme['name'] . "</option>\n";
			} // foreach themes
			echo "</select>\n";
			break;
		default:
			echo "<input type=\"text\" size=\"$len\" name=\"$name\" value=\"$value\" />";
		break;

	} 

} // create_preference_input

/** 
 * get_preference_id
 * This takes the name of a preference and returns it's id this is usefull for calling
 * the user classes update_preference function
 * @package Preferences
 * @catagory Get
 */
function get_preference_id($name) { 

	$sql = "SELECT id FROM preferences WHERE name='" . sql_escape($name) . "'";
	$db_results = mysql_query($sql, dbh());

	$results = mysql_fetch_assoc($db_results);

	return $results['id'];

} // get_preference_id

?>
n class="c1">// show_play_selected /* * 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_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 /** * 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['raw']; } // Already resized if ($image['db_resized']) { debug_event('using_resized','using resized image for Album:' . $album_id,'2'); return $image['raw']; } $image = $image['raw']; 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) { debug_event('IMG_RESIZE','Failed to create from string','3'); 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)) { debug_event('IMG_RESIZE','Failed to copy resample image','3'); 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); break; case 'png': imagepng($img); 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 */ function show_genres($genres,$view) { require Config::get('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 */ function show_genre($genre_id) { $genre = new Genre($genre_id); require Config::get('prefix') . '/templates/show_genre.inc.php'; } // show_genre /** * 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 = ltrim($source, Config::get('raw_web_path')); $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 /** * 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 /** * 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_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,$allow_add=0,$song_id=0) { // Generate key to use for HTML element ID static $id_cnt; if ($song_id) { $key = "album_select_$song_id"; } else { $key = "album_select_c" . ++$id_cnt; } // Added ID field so we can easily observe this element echo "<select name=\"$name\" id=\"$key\">\n"; $sql = "SELECT `id`, `name`, `prefix` FROM `album` ORDER BY `name`"; $db_results = Dba::query($sql); while ($r = Dba::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 if ($allow_add) { // Append additional option to the end with value=-1 echo "\t<option value=\"-1\">" . _('Add New') . "...</option>\n"; } 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, $allow_add=0, $song_id=0) { // Generate key to use for HTML element ID static $id_cnt; if ($song_id) { $key = "artist_select_$song_id"; } else { $key = "artist_select_c" . ++$id_cnt; } echo "<select name=\"$name\" id=\"$key\">\n"; $sql = "SELECT `id`, `name`, `prefix` FROM `artist` ORDER BY `name`"; $db_results = Dba::query($sql); while ($r = Dba::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 if ($allow_add) { // Append additional option to the end with value=-1 echo "\t<option value=\"-1\">Add New...</option>\n"; } 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=1,$size='', $allow_add=0, $song_id=0) { // Generate key to use for HTML element ID static $id_cnt; if ($song_id) { $key = "genre_select_$song_id"; } else { $key = "genre_select_c" . ++$id_cnt; } if ($size > 0) { $multiple_txt = " multiple=\"multiple\" size=\"$size\""; } echo "<select name=\"$name\"$multiple_txt id=\"$key\">\n"; $sql = "SELECT `id`, `name` FROM `genre` ORDER BY `name`"; $db_results = Dba::query($sql); while ($r = Dba::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 if ($allow_add) { // Append additional option to the end with value=-1 echo "\t<option value=\"-1\">Add New...</option>\n"; } 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 = Dba::query($sql); while ($r = Dba::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=\"\">" . _('All') . "</option>\n"; $sql = "SELECT `id`,`username`,`fullname` FROM `user` ORDER BY `fullname`"; $db_results = Dba::query($sql); while ($row = Dba::fetch_assoc($db_results)) { $select_txt = ''; if ($row['id'] == $selected) { $select_txt = 'selected="selected"'; } // If they don't have a full name, revert to the username $row['fullname'] = $row['fullname'] ? $row['fullname'] : $row['username']; echo "\t<option value=\"" . $row['id'] . "\" $select_txt>" . scrub_out($row['fullname']) . "</option>\n"; } // end while users echo "</select>\n"; } // show_user_select /** * show_playlist_select * This one is for users! shows a select/option statement so you can pick a user * to blame */ function show_playlist_select($name,$selected='',$style='') { echo "<select name=\"$name\" style=\"$style\">\n"; echo "\t<option value=\"\">" . _('None') . "</option>\n"; $sql = "SELECT `id`,`name` FROM `playlist` ORDER BY `name`"; $db_results = Dba::query($sql); while ($row = Dba::fetch_assoc($db_results)) { $select_txt = ''; if ($row['id'] == $selected) { $select_txt = 'selected="selected"'; } // If they don't have a full name, revert to the username echo "\t<option value=\"" . $row['id'] . "\" $select_txt>" . scrub_out($row['name']) . "</option>\n"; } // end while users echo "</select>\n"; } // show_playlist_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='',$class='') { 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,$title='',$id='') { /* Because we do a lot of calls cache the URLs */ static $url_cache = array(); // If our name is an array if (is_array($name)) { $hover_name = $name['1']; $name = $name['0']; } if (!$title) { $title = $name; } if ($id) { $id_element = 'id="' . $id . '"'; } 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') . Config::get('theme_path') . '/images/icons/' . $icon_name)) { $img_url = Config::get('web_path') . Config::get('theme_path') . '/images/icons/' . $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') . Config::get('theme_path') . '/images/icons/' . $icon_name)) { $hov_url = Config::get('web_path') . Config::get('theme_path') . '/images/icons/' . $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 src=\"$img_url\" $id_element alt=\"" . ucfirst($title) . "\" title=\"" . ucfirst($title) . "\" $hov_txt/>"; return $string; } // get_user_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='') { // If we weren't passed an array then return a blank string if (!is_array($array)) { return ''; } // The type is used for the different XML docs we pass 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_int($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"; } } } // end foreach 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"; } } } // end foreach 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>" . Config::get('site_title') . "</creator>\n" . "<annotation>" . Config::get('site_title') . "</annotation>\n" . "<info>". Config::get('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 /** * ajax_include * This does an ob_start, getcontents, clean * on the specified require, only works if you * don't need to pass data in */ function ajax_include($include) { ob_start(); require_once Config::get('prefix') . '/templates/' . $include; $results = ob_get_contents(); ob_end_clean(); return $results; } // ajax_include ?>