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

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

admin link modification

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