summaryrefslogtreecommitdiffstats
path: root/upload.php
blob: 0cb5f427509fbd091aa9c668feed854793e71bc8 (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
<?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**
 **Chopped to bits and made into PHP nuggets by RosenSama** 2005

*/

/* FIXME:  Things left to do
	--need to add debug logging
	--Add purge link in catalog admin
	--Why do I need to echo something before the message table to show it?
	--Handle when uploaded file is a compressed arvchive
	--play quar song by admin
	--TEST!
*/

require_once('lib/init.php');

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

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

$action = scrub_in( $_REQUEST['action'] );

switch( $action ) {
	case 'upload':
		/* Break if they don't have rights */
		if (!$GLOBALS['user']->prefs['upload'] OR !$GLOBALS['user']->has_access(25)) { 
			break;
		}
	
		/* IF we need to quarantine this */
		if ($GLOBALS['user']->prefs['quarantine']) { 
			/* Make sure the quarantine dir is writeable */
			if (!check_upload_directory(conf('quarantine_dir'))) { 
				$GLOBALS['error']->add_error('general',"Error: Quarantine Directory isn't writeable");
				debug_event('upload',"Error: Quarantine Directory isn't writeable",'2');
			} // if unwriteable

			$catalog_id = find_upload_catalog(conf('quarantine_dir'));

			/* Make sure that it's not in a catalog dir */
			if ($catalog_id) { 
				$GLOBALS['error']->add_error('general',"Error: Quarantine Directory inside a catalog");
				debug_event('upload',"Error: Quarantine Directory inside a catalog",'2');
			} // if in catalog dir

			foreach ($_FILES as $key => $file) { 
				
				if (strlen($_FILES[$key]['name'])) { 
					/* Check size and extension */
					if (!check_upload_extension($key)) { 
						$GLOBALS['error']->add_error($key,"Error: Invalid Extension");
					}
					if (!check_upload_size($key)) { 
						$GLOBALS['error']->add_error($key,"Error: File to large");
					}

					if (!$GLOBALS['error']->error_state) { 
						$new_filename = upload_file($key,conf('quarantine_dir')); 
						/* Record this upload then we're done */
						if ($new_filename) { insert_quarantine_record($user->username,'quarantine',$new_filename); }
					} // if we havn't had an error

				} // end if there is a file to check 

			} // end foreach files
			
			if ($GLOBALS['error']->error_state) { 
				show_upload(); 
			}
			else { 
				show_confirmation(_('Upload Quarantined'), _('Your Upload(s) have been quarantined and will be reviewed for addition'),'upload.php');
			}

		} // if quarantine
		
		/* Else direct upload time baby! */
		else { 
                        /* Make sure the quarantine dir is writeable */
                        if (!check_upload_directory($GLOBALS['user']->prefs['upload_dir'])) {
                                $GLOBALS['error']->add_error('general',"Error: Upload Directory isn't writeable");
                                debug_event('upload',"Error: Upload Directory isn't writeable",'2');
                        } // if unwriteable

			$catalog_id = find_upload_catalog($user->prefs['upload_dir']);
			$catalog = new Catalog($catalog_id);
			
                        /* Make sure that it's not in a catalog dir */
                        if (!$catalog_id) {
                                $GLOBALS['error']->add_error('general',"Error: Upload Directory not inside a catalog");
                                debug_event('upload',"Error: Upload Directory not inside a catalog",'2');
                        } // if in catalog dir

			/* Foreach through the post files */
                        foreach ($_FILES as $key => $file) {

                                if (strlen($_FILES[$key]['name']) && strlen($_FILES[$key]['tmp_name'])) {
                                        /* Check size and extension */
                                        if (!check_upload_extension($key)) {
                                                $GLOBALS['error']->add_error($key,"Error: Invalid Extension");
                                        }
                                        if (!check_upload_size($key)) {
                                                $GLOBALS['error']->add_error($key,"Error: File to large");
                                        }

                                        if (!$GLOBALS['error']->error_state) {
 						$new_filename = upload_file($key,$user->prefs['upload_dir']);
						
						/* We aren't doing the quarantine thing, so just insert it */
						if ($new_filename) { $catalog->insert_local_song($new_filename,filesize($new_filename)); }
                                        } // if we havn't had an error

				} // if there is a file to check

				elseif (strlen($_FILES[$key]['name'])) { 
					$GLOBALS['error']->add_error($key,'Error: Total Filesize to large, file not uploaded');
				}

                        } // end foreach files

                        if ($GLOBALS['error']->error_state) {
                                show_upload();
                        }
                        else {
                                show_confirmation(_('Files Uploaded'), _('Your Upload(s) have been inserted into Ampache and are now live'),"upload.php");
                        }

		} // man this is a bad idea, the catch all should be the conservative option... oooh well
	break;
	case 'add':
		/* Make sure they have access */
		if($GLOBALS['user']->has_access(100)) {
			$id = scrub_in($_REQUEST['id']);
			update_quarantine_record($id,'add');
			show_confirmation(_('Upload Added'),_('The Upload has been scheduled for a catalog add, please run command line script to add file'),"upload.php");
		} 
		else { 
			access_denied();
		} 
		break;
	case 'delete':
		/* Make sure they got them rights */
		if($GLOBALS['user']->has_access(100)) {
			$id = scrub_in($_REQUEST['id']);
			update_quarantine_record($id,'delete');
                        show_confirmation(_('Upload Deleted'),_('The Upload has been scheduled for deletion, please run command line script to permently delete this file'),"upload.php");
		} 
		else { 
			access_denied();
		}
		break;
	case 'ack':
		// everything is ready to bulk ack once we pass multiple ids and put them in $id[]
		if($GLOBALS['user']->has_access(100)) {
			$id[] = scrub_in($_REQUEST['id']);
			$status = upload_ack( $id );
		} else {
			access_denied();
		}
		break;

	case 'purge':
		if($GLOBALS['user']->has_access(100)) {
			$status = upload_purge();
		} else {
			access_denied();
		}
		break;
	 	
	default:
		show_upload();
	break;
}	// end switch on $action

// display any messages
if( $status ) {
	show_box_top(); 
	print( "<table align='center'>\n" );
	print( "<th>Filename</th><th>Result</th>\n" );
	foreach( $status as $status_row ) {
		$filename = $status_row[0];
		$result = $status_row[1];
		$color = "color='green'";
		if( $status_row[2] ) {
			$color = "color='red'";
		}
		print( "<tr>\n");
		print(	"<td><font $color>$filename</font></td><td><font $color>$result</font></td>\n");
		print( "</tr>\n");
	} // end for each status element
	print( "</table>\n" );
	show_box_bottom();
} // end if any messages
 
/* Show the Page Footer */
show_footer();
?>