summaryrefslogtreecommitdiffstats
path: root/upload.php
blob: f6cbf3ceebeb41151ae9aad8cc6df5a642f11f20 (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
<?php
/*

 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.

*/
/*

 Copyright (c) 2003 Lamar
 All rights reserved.

 **Revised by Vollmerk**

 upload.php script.
 saves all uploaded files to the temp/ directory
 then processes the files and moves them to the
 proper directory.

 There are two basic modes of operation.  HTML
 mode and GUI mode.  If GUI mode is enabled a response
 with header 200 will be sent to the GUI. 

*/

require_once ("modules/init.php");



// Set page header
show_template('header');
show_menu_items('Upload');

// Access Control
if(!$user->prefs['upload'] || conf('demo_mode'))  {
	access_denied();
}

/* Action Settings */
$action = scrub_in($_REQUEST['action']);


/* 
	FILE UPLOAD SECTION 
	This section handles file uploads.  File types should
	be declared in  the $types hash.  This will provide
	an easy lookup mechanism.
*/
$types = array( 
		'mp3'=>'music',
		'MP3'=>'music',
		'ogg'=>'music',
		'OGG'=>'music',
		'WMA'=>'music',
		'FLAC'=>'music',
		'flac'=>'music',
		'm4a' =>'music',
		'aac' =>'music',
		'.gz'=>'compressed',
		'tar'=>'compressed',
		'zip'=>'compressed',
		'ZIP'=>'compressed',
	);

/*  Upload Section Which Processes All Files Sent As Post  */
$audio_info = new Audioinfo();

switch ($action) { 
	case 'upload_now':
		// Verify the needed settings are in place
		if (!@chdir($user->prefs['upload_dir']) || strlen($user->prefs['upload_dir']) < 1) { 
			break;
		}

		//FIXME: Set which catalog it goes into somewhere....
		$sql = "SELECT * FROM catalog LIMIT 1";
		$db_results = mysql_query($sql, dbh()); 

		$results = mysql_fetch_object($db_results);

		$catalog = new Catalog($results->id);

		// Create arrays
		$filelist = array();
		
		foreach($_FILES as $tagname=>$file){
			/* Skip blank file names  */
	
			if( strlen($file['name'] ) ){
		
			// Determine tempfile name
			$tempfile = $file['tmp_name'];

			// Determine real file name
			$realname = $user->prefs['upload_dir'] . "/" .  $file['name'];
		
			/* Determine Extension */
			$ext = substr( $file['name'], -3 );

			/* Prevent Unauthorized file types   */
			if(  $types[$ext] == 'compressed' ){
	                        // This section is currently disabled
			}
			elseif( $types[$ext] == 'music' ){
				$error = @move_uploaded_file($tempfile, $realname );
				if( $error )
				{
					$filelist = array( $realname => $file['name'] );
				}
				else{
					switch ($file['error']) {
						case '1':
							$error_text = _("The uploaded file exceeds the upload_max_filesize directive in php.ini");
							break;
						case '2':
							$error_text = _("The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.");
							break;
						case '3':
							$error_text = _("The uploaded file was only partially uploaded.");
							break;
						case '4':
							$error_text = _("No file was uploaded.");
							break;
						default:
							$error_text = _("An Unknown Error has occured.");
							break;
					} // end switch 
					if (conf('debug')) { 
						log_event($_SESSION['userdata']['username'],'upload',$error_text);
					}
					$message[$file['name']] .= "Error:  $error_text";	
					$errorenum[$file['name']]=true;
				}
			} // end if known
			// If unknown filetype
			else{
				$message[$file['name']] .= "Error:  Unsupported File Type- $ext<br />";
				$errorenum[$file['name']]=true;
			}
			// foreach through files uploaded
			foreach( $filelist as $fullpath => $music ) {
				
				// If we are quarantining the file
				if ($user->prefs['quarantine']) { 
					// Log the upload but don't do anything
					$message[$music] .= _("Successfully-Quarantined");
					/* Log the upload */
					$sql = "INSERT INTO upload (`user`,`file`,`addition_time`)" .
						" VALUES ('$user->username','" . sql_escape($fullpath) . "','" . time() . "')";
					$db_results = mysql_query($sql, dbh());
				} // if quarantine

				// Go ahead and insert the file
				else {
					$catalog->insert_local_song($fullpath,filesize($fullpath));
					$message[$music] .= _("Successfully-Cataloged");
				  } // end foreach
			flush();
			}
		}
		
		} // end foreach
		
		flush();
		/* Display Upload results  */
		if( $message ){
			print( "<table align='center'>\n" );
			print( "<th>Filename</th><th>Result</th>\n" );
		
			foreach ( $message as $key => $value ){
				if( $errorenum[$key] ){
					$color="color='red'";
				}
				else{
					$color="color='green'";
				}
				print( "<tr>\n");
				print(	"<td><font $color>$key</font></td><td><font $color>$value</font></td>\n");
				print( "</tr>\n");
			}
			print( "</table>\n" );
		}
		require_once(conf('prefix') . "/templates/show_upload.inc");
		break;
	case 'add':
	case 'delete':
	default:
		require_once(conf('prefix') . "/templates/show_upload.inc");
		break;
} // end of switch on action

echo "\n<br /><br />\n";

/* 
	SHOW QUARANTINE SONGS
	This Section Displays Quarantined Songs
	Always process all files in quarantine directory.
	Make a list (and check it twice)
*/
$songs = array();

if ( $handle = @opendir($user->prefs['upload_dir'] ) ){
	/* Loop Through the directory */
	while( false !== ($file = readdir( $handle ))){

		/* Find extension */
		$ext =  substr( $file, -4 );

		if(( $ext == '.mp3' )||( $ext == '.ogg' )){
			$songs[$file]=$user->prefs['upload_dir'] . "/" . "$file";
		}
	}	
} // end if upload_dir
?>

<table class="tabledata" cellspacing="0" cellpadding="0" align="center">
	<tr class="table-header">
		<td><?php echo _("Action"); ?></td>
		<td><?php echo _("Song"); ?></td>
		<td><?php echo _("Artist"); ?></td>
		<td><?php echo _("Album"); ?></td>
		<td><?php echo _("Genre"); ?></td>
		<td><?php echo _("Time"); ?></td>
		<td><?php echo _("Bitrate"); ?></td>
		<td><?php echo _("Size"); ?></td>
		<td><?php echo _("Filename"); ?></td>
		<td><?php echo _("User"); ?></td>
		<td><?php echo _("Date"); ?></td>
	</tr>

<?
	/* Only populate table if valid songs exist */
	if( sizeof($songs) ) {

		/* Get file info */
		$audio_info = new Audioinfo();
		$order = conf('id3tag_order');

		foreach( $songs as $file=>$song ){

			if( $class == "odd" ){
				$class = "even";
			}
			else{
				$class = "odd";
			}
			
			$sql = "SELECT user,addition_time FROM upload WHERE file = '$song'";				
			$db_result = mysql_query($sql, dbh());
			
			if( $r = mysql_fetch_object($db_result) ){
				$temp_user = new User($r->user);
				$uname = $temp_user->fullname;
			}
			else{
				$uname = _("Unknown");
			}

			/* Get filesize */
			$filesize = @filesize( $song );
			$add_time = date( "r",filemtime( $song ) );

			/* get audio information */
			$results = $audio_info->Info($song);

                	$key = get_tag_type($results);
	
			// Crappy Math time boys and girls!
			//FIXME: Do this right
			$min = floor($results['playing_time']/60);
			$sec = floor($results['playing_time'] - ($min*60));
			$time = $min . ":" . $sec;	

			echo  "		<tr class=\"".$class."\">\n";

			if( $user->access === 'admin' ){
				echo "			<td>\n" .
					"		<a href=\"" . $web_path . "upload.php/?action=add&song=$file\">" . _("Add") . "</a><br />\n" .
					"		<a href=\"" . $web_path . "upload.php/?action=delete&song=$file\">" . _("Delete") . "</a><br />\n" .
					"			</td>\n"; 
			}
			else{
				echo "			<td>" . _("Quarantined") . "</td>\n";
			}
		
			  
			echo  "			<td><a href='" . $web_path . 
				"/play/pupload.php?action=m3u&song=$file&uid=$user->username'>" . 
				$results[$key][title] . "</a></td>\n";


			echo  "			<td>" . $results[$key]['artist'] . "&nbsp</td>\n";
			echo  "			<td>" . $results[$key]['album'] . "&nbsp</td>\n";
			echo  "			<td>" . $results[$key]['genre'] . "</td>\n";
			echo  "			<td>" . $time . " </td>\n";
			echo  "			<td>" . intval($results['avg_bit_rate']/1000) . "-" . $results['bitrate_mode'] . "</td>\n";
			echo  "			<td>" . sprintf("%.2f",($filesize/1048576)) . "</td>\n";
			echo  "			<td>$file </td>\n";
			echo  "			<td>$uname</td>\n";
			echo  "			<td>$add_time </td>\n";
			echo  "		</tr>\n";		
		}
	}


?>
	</table>
<br />
<br />
<br />
<?php> show_page_footer ('Upload', '',$user->prefs['display_menu']); ?>