3 # This form requires wfpl. See: http://sametwice.com/wfpl
5 define('ADMIN_FILES_DB_FIELDS', 'filename,description');
7 # Set this to the path to your uploads directory. It can be relative to the
8 # location of this script. IT MUST END WITH A SLASH
9 $GLOBALS['upload_directory'] = 'files/';
11 # emergency backup in case uploaders file name is blank somehow
12 $GLOBALS['filename_file_name'] = uniqid() . getmypid() . '.txt';
15 require_once(INC_WFPL . 'format.php');
16 require_once(INC_WFPL . 'email.php');
17 require_once(INC_WFPL . 'upload.php');
19 function admin_files_get_fields() {
22 $data['description'] = format_oneline(_REQUEST_cut('description'));
24 #header('Content-Type: text/plain');
25 #print_r(array($_REQUEST['filename'], $_FILES['filename']));
27 $filename_filename_tmp = format_filename($_FILES['filename']['name']);
28 if(!$filename_filename_tmp) {
29 $filename_filename_tmp = $GLOBALS['filename_file_name'];
31 if($_FILES['filename'] && $_FILES['filename']['error'] == 0) {
32 $data['filename'] = save_uploaded_file('filename', $GLOBALS['upload_directory'] . $filename_filename_tmp);
34 if(_REQUEST_cut('delete_filename') == 'Yes') {
35 $data['filename'] = '';
37 $data['filename'] = format_path(_REQUEST_cut('old_filename'));
40 unset($_FILES['filename']);
46 function admin_files_main() {
47 if(logged_in_as_admin()) {
48 tem_set('admin_privs');
50 $_REQUEST['url'] = this_url();
54 $id = _REQUEST_cut('edit_id');
56 return admin_files_main_form($id);
59 $id = _REQUEST_cut('admin_files_delete_id');
61 return admin_files_main_delete($id);
64 if(_REQUEST_cut('new')) {
65 return admin_files_main_form();
68 if(_REQUEST_cut('list')) {
69 return admin_files_main_listing();
72 if(isset($_POST['description'])) {
73 return admin_files_main_form();
77 return admin_files_main_listing();
80 function admin_files_main_delete($id) {
81 $fn = db_get_value('files', 'filename', 'where id=%i', $id);
84 db_delete('files', 'where id=%i', $id);
85 message('File deleted.');
87 message("Couldn't find file to delete. Maybe it's already been deleted?");
89 return './admin_files';
92 function admin_files_main_listing() {
93 $listing_rows = db_get_assocs('files', 'id,filename,description', 'order by coalesce(nullif(description, ""), substring(filename, 7)) limit 100');
94 tem_set('listings', $listing_rows);
97 function admin_files_main_form($id = false) {
102 if(isset($_POST['description'])) {
103 $data = admin_files_get_fields();
105 if("you're happy with the POSTed values") {
107 db_update_assoc('files', $data, 'where id=%i', $id);
108 message('File updated.');
110 db_insert_assoc('files', $data);
111 message('File saved.');
113 if($error !== true) {
114 return './admin_files';
117 # otherwise, we display the form again. admin_files_get_fields() has
118 # already put the posted values back into the template engine, so they will
119 # show up in the form fields. You should add some message asking people to
120 # fix their entry in whatever way you require.
122 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
123 $data = db_get_assoc('files', ADMIN_FILES_DB_FIELDS, 'where id=%i', $id);
125 # form not submitted, you can set default values like so:
126 #$data = array('description' => 'Yes');
130 tem_set('upload_max_filesize', upload_max_filesize());
132 tem_set('form', $data);