source: extensions/File_Uploader/admin/admin_upload.php @ 21394

Last change on this file since 21394 was 21394, checked in by julien1311, 8 years ago

[file_uploader] bug fixes

  • Property svn:eol-style set to LF
File size: 11.5 KB
Line 
1<?php
2if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
3
4include_once(PHPWG_ROOT_PATH . 'include/functions.inc.php');
5include_once(PHPWG_ROOT_PATH . 'admin/include/functions_upload.inc.php');
6
7global $template, $conf, $user, $page;
8
9if (isset($_POST['submit'])) {
10        $file_uploader_errors = array();
11        $file_uploader_upload_file = array();
12
13        if($_FILES['file_uploader_file']['size'] != 0) {
14                $file_uploader_upload_file = file_uploader_upload_file($_FILES['file_uploader_file']);
15                if(count($file_uploader_upload_file['errors']) != 0)
16                        $file_uploader_errors['file'] = $file_uploader_upload_file['errors'];
17        } else {
18                $file_uploader_errors['file']['no_file'] = l10n('Specify a file to upload');
19        }
20       
21        if($_FILES['file_uploader_thumbnail']['size'] != 0 && $_FILES['file_uploader_file']['size'] != 0) {
22                $file_uploader_upload_thumbnail = file_uploader_upload_thumbnail($_FILES['file_uploader_thumbnail'], $file_uploader_upload_file['name_wo_extension'], $file_uploader_upload_file['extension'], $file_uploader_upload_file['folder']);
23                if(count($file_uploader_upload_thumbnail['errors']) != 0)
24                        $file_uploader_errors['thumbnail'] = $file_uploader_upload_thumbnail['errors'];
25        } else {
26                $file_uploader_errors['thumbnail']['no_file'] = 'Specify a thumbnail for your file';
27        }
28       
29        if (count($file_uploader_errors) == 0) {
30                file_uploader_synchronize($file_uploader_upload_file, $_POST['file_uploader'], $file_uploader_upload_thumbnail);
31                array_push($page['infos'], l10n('File uploaded and synchronized'));
32        } else {
33                array_push($page['errors'], l10n('There have been errors. See below'));
34                $template->assign('file_uploader_errors', $file_uploader_errors);
35                $template->assign('file_uploader', $_POST['file_uploader']);
36        }
37}
38
39function file_uploader_upload_file($file_uploader_file) {
40        require(FILE_UPLOADER_PATH.'install/config_default.inc.php');
41        require_once(FILE_UPLOADER_PATH.'install/functions.inc.php');
42       
43        global $conf;
44        $conf_file_uploader = unserialize($conf['file_uploader']);
45       
46        $file_uploader_file_tmp = $file_uploader_file['tmp_name'];
47        $file_uploader_file_title = substr($file_uploader_file['name'], 0, strrpos($file_uploader_file['name'], '.'));
48        $file_uploader_file_name = preg_replace('/[^a-zA-Z0-9s.]/', '_', $file_uploader_file['name']);
49        $file_uploader_file_name_wo_extension = substr($file_uploader_file_name, 0, strrpos($file_uploader_file_name, '.'));
50        $file_uploader_file_extension = strtolower(substr(strrchr($file_uploader_file['name'], '.'),1));
51        $file_uploader_file_size = filesize($file_uploader_file['tmp_name']);
52        if (isset($file_uploader_destination_folder[$file_uploader_file_extension])) {
53                $file_uploader_file_category = $file_uploader_destination_folder[$file_uploader_file_extension];
54                $file_uploader_file_directory_full = $file_uploader_galleries_dir.$file_uploader_file_category;
55        } else {
56                $file_uploader_file_category = $file_uploader_destination_folder['others'];
57                $file_uploader_file_directory_full = $file_uploader_galleries_dir.$file_uploader_file_category;
58        }
59        $file_uploader_file_destination = $file_uploader_file_directory_full.'/'.$file_uploader_file_name;
60        $file_uploader_errors = array();
61        $return = array();
62       
63        $return['title'] = $file_uploader_file_title;
64        $return['name_wo_extension'] = $file_uploader_file_name_wo_extension;
65        $return['extension'] = $file_uploader_file_extension;
66        $return['folder'] = $file_uploader_file_category;
67        $return['destination'] = $file_uploader_file_destination;
68        $return['size'] = $file_uploader_file_size;
69        $return['overwrite'] = false;
70       
71        //Check if the folder file_uploader exists
72        file_uploader_folder($file_uploader_galleries_folder, $file_uploader_galleries_dir, $file_uploader_galleries_folder_name);
73       
74        //If the directory does not exist, we create it
75        if (!file_exists($file_uploader_file_directory_full))
76                if(!mkdir($file_uploader_file_directory_full))
77                        $file_uploader_errors['upload_error'] = l10n('Unable to create folder ').$file_uploader_file_directory_full;
78                //If the physical category si not in database, we add it
79                else if (pwg_db_num_rows(pwg_query('SELECT id FROM '.CATEGORIES_TABLE.' WHERE name = "'.$file_uploader_file_category.'";')) == 0){
80                        $next_id = pwg_db_nextval('id', CATEGORIES_TABLE);
81                        $category_rank = pwg_db_fetch_assoc(pwg_query('SELECT MAX(rank) FROM '.CATEGORIES_TABLE.';'));
82                        $category_rank = $category_rank['MAX(rank)'] + 1;
83                        $file_uploader_physical_category = pwg_db_fetch_assoc(pwg_query('SELECT id FROM '.CATEGORIES_TABLE.' WHERE dir = "'.$file_uploader_galleries_folder.'";'));
84                        $insert = array(
85                                'id' => $next_id,
86                                'name' => $file_uploader_file_category,
87                                'id_uppercat' => $file_uploader_physical_category['id'],
88                                'comment' => 'Created by the File Uploader plugin',
89                                'dir' => $file_uploader_file_category,
90                                'rank' => $category_rank,
91                                'status' => 'private',
92                                'visible' => boolean_to_string($conf['newcat_default_visible']),
93                                'uppercats' => $next_id.','.$file_uploader_physical_category['id'],
94                                'global_rank' => $category_rank,
95                                'site_id' => '1',
96                        );
97                        single_insert(CATEGORIES_TABLE, $insert);
98                }
99       
100        if ($file_uploader_file['error'] !== UPLOAD_ERR_OK) {
101                switch ($_FILES['file_uploader_file']['error']) {
102                        case UPLOAD_ERR_INI_SIZE:
103                                $file_uploader_errors['upload_error'] = l10n('File exceeds the upload_max_filesize directive in php.ini');
104                                break;
105                        case UPLOAD_ERR_PARTIAL:
106                                $file_uploader_errors['upload_error'] = l10n('File was only partially uploaded');
107                                break;
108                        case UPLOAD_ERR_NO_FILE:
109                                $file_uploader_errors['upload_error'] = l10n('No file to upload');
110                                break;
111                        case UPLOAD_ERR_NO_TMP_DIR:
112                                $file_uploader_errors['upload_error'] = l10n('Missing a temporary folder');
113                                break;
114                        case UPLOAD_ERR_CANT_WRITE:
115                                $file_uploader_errors['upload_error'] = l10n('Failed to write file to disk');
116                                break;
117                        case UPLOAD_ERR_EXTENSION:
118                                $file_uploader_errors['upload_error'] = l10n('File upload stopped by extension');
119                                break;
120                        default:
121                                $file_uploader_errors['upload_error'] = l10n('Upload error');
122                }
123        } else if (file_exists($file_uploader_file_destination)) {
124                if ($conf_file_uploader['overwrite'] == 1)
125                        $return['overwrite'] = true;
126                else
127                        $file_uploader_errors['already_exist'] = l10n('file_uploader_error_already_exist');
128        } else if (!move_uploaded_file($file_uploader_file_tmp, $file_uploader_file_destination)) {
129                $file_uploader_errors['move_uploaded_file'] = l10n('Can\'t upload file to galleries directory');
130        }
131       
132        $return['errors'] = $file_uploader_errors;
133       
134        return $return;
135}
136
137
138function file_uploader_upload_thumbnail($file_uploader_thumbnail, $file_uploader_file_name_wo_extension, $file_uploader_file_extension, $file_uploader_file_folder) {
139        require(FILE_UPLOADER_PATH.'install/config_default.inc.php');
140       
141        $file_uploader_thumbnail_tmp = $file_uploader_thumbnail['tmp_name'];
142        $file_uploader_thumbnail_extension = strtolower(substr(strrchr($file_uploader_thumbnail['name'], '.'),1));
143        $file_uploader_thumbnail_size = filesize($file_uploader_thumbnail['tmp_name']);
144        $file_uploader_thumbnail_directory = $file_uploader_galleries_dir.$file_uploader_file_folder.'/pwg_representative';
145        $file_uploader_thumbnail_destination = $file_uploader_thumbnail_directory.'/'.$file_uploader_file_name_wo_extension.'.'.$file_uploader_thumbnail_extension;
146        $file_uploader_errors = array();
147        $return = array();
148       
149        $return['extension'] = $file_uploader_thumbnail_extension;
150       
151        if (!file_exists($file_uploader_thumbnail_directory))
152                if(!mkdir($file_uploader_thumbnail_directory))
153                        $file_uploader_errors['upload_error'] = l10n('Unable to create folder ').$file_uploader_thumbnail_directory;
154       
155        if ($file_uploader_thumbnail['error'] !== UPLOAD_ERR_OK) {
156                switch ($_FILES['file_uploader_thumbnail']['error']) {
157                        case UPLOAD_ERR_INI_SIZE:
158                                $file_uploader_errors['upload_error'] = l10n('File exceeds the upload_max_filesize directive in php.ini');
159                                break;
160                        case UPLOAD_ERR_PARTIAL:
161                                $file_uploader_errors['upload_error'] = l10n('File was only partially uploaded');
162                                break;
163                        case UPLOAD_ERR_NO_FILE:
164                                $file_uploader_errors['upload_error'] = l10n('No file to upload');
165                                break;
166                        case UPLOAD_ERR_NO_TMP_DIR:
167                                $file_uploader_errors['upload_error'] = l10n('Missing a temporary folder');
168                                break;
169                        case UPLOAD_ERR_CANT_WRITE:
170                                $file_uploader_errors['upload_error'] = l10n('Failed to write file to disk');
171                                break;
172                        case UPLOAD_ERR_EXTENSION:
173                                $file_uploader_errors['upload_error'] = l10n('File upload stopped by extension');
174                                break;
175                        default:
176                                $file_uploader_errors['upload_error'] = l10n('Upload error');
177                } 
178        } else if(!in_array($file_uploader_thumbnail_extension, $file_uploader_allowed_thumbnail_extension)) {
179                $file_uploader_errors['filetype'] = l10n('The thumbnail must be a picture');
180        } else if (!move_uploaded_file($file_uploader_thumbnail_tmp, $file_uploader_thumbnail_destination)) {
181                $file_uploader_errors['move_uploaded_file'] = l10n('Can\'t upload file to galleries directory');
182        }
183       
184        $return['errors'] = $file_uploader_errors;
185       
186        return $return;
187}
188
189
190function file_uploader_synchronize($file_uploader_file, $file_uploader_file_properties, $file_uploader_thumbnail) {
191        global $user;
192       
193        //Current date
194        list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
195
196        //Database registration
197        $file_path = pwg_db_real_escape_string($file_uploader_file['destination']);
198        $physical_category = pwg_db_fetch_assoc(pwg_query('SELECT id FROM '.CATEGORIES_TABLE.' WHERE name = "'.$file_uploader_file['folder'].'";'));
199        $physical_category_id = $physical_category['id'];
200        $insert = array(
201                'file' => substr(strrchr($file_path, '/'), 1),
202                'name' => ($file_uploader_file_properties['title'] != '') ? pwg_db_real_escape_string($file_uploader_file_properties['title']) : $file_uploader_file['title'],
203                'comment' => pwg_db_real_escape_string($file_uploader_file_properties['description']),
204                'date_available' => $dbnow,
205                'path' => $file_path,
206                'representative_ext' => $file_uploader_thumbnail['extension'],
207                'filesize' => $file_uploader_file['size'],
208                'storage_category_id' => $physical_category_id,
209                'md5sum' => md5_file($file_path),
210                'added_by' => $user['id'],
211        );
212       
213        if ($file_uploader_file['overwrite']) {
214                $query = 'SELECT id FROM '.IMAGES_TABLE.' WHERE path = "'.$file_path.'";';
215                $image_id = pwg_db_fetch_assoc(pwg_query($query));
216                single_update(IMAGES_TABLE, $insert, array('id' => $image_id['id']));
217        } else {
218                single_insert(IMAGES_TABLE, $insert);
219                $image_id = pwg_db_insert_id(IMAGES_TABLE);
220        }
221       
222        if (!is_array($image_id))
223                $image_id = array($image_id);
224       
225        if(isset($file_uploader_file_properties['category']) and count($file_uploader_file_properties['category']) > 0) {
226                @associate_images_to_categories(
227                        $image_id,
228                        array($file_uploader_file_properties['category'], $physical_category_id['id'])
229                );
230        }
231       
232        @fill_caddie($image_id);
233}
234
235
236//Categories
237$query = 'SELECT id,name,uppercats,global_rank FROM '.CATEGORIES_TABLE.';';
238display_select_cat_wrapper($query, array(), 'category_parent_options');
239
240//Add parameters to template
241$upload_max_filesize = min(get_ini_size('upload_max_filesize'), get_ini_size('post_max_size'));
242if ($upload_max_filesize == get_ini_size('upload_max_filesize')) {
243        $upload_max_filesize = get_ini_size('upload_max_filesize', true);
244} else {
245        $upload_max_filesize = get_ini_size('post_max_filesize', true);
246}
247$upload_max_filesize_display = round($upload_max_filesize/1024, 0);
248
249$template->assign(
250    array(
251                'upload_max_filesize' => $upload_max_filesize,
252                'upload_max_filesize_display' => $upload_max_filesize_display,
253    )
254);
255
256$template->set_filenames(array('plugin_admin_content' => dirname(__FILE__) . '/admin_upload.tpl')); 
257$template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content');
258?>
Note: See TracBrowser for help on using the repository browser.