[19472] | 1 | <?php |
---|
| 2 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
---|
| 3 | |
---|
| 4 | include_once(PHPWG_ROOT_PATH . 'include/functions.inc.php'); |
---|
[19875] | 5 | include_once(PHPWG_ROOT_PATH . 'admin/include/functions_upload.inc.php'); |
---|
[19472] | 6 | |
---|
| 7 | global $template, $conf, $user, $page; |
---|
| 8 | |
---|
| 9 | if (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 { |
---|
[19875] | 18 | $file_uploader_errors['file']['no_file'] = l10n('Specify a file to upload'); |
---|
[19472] | 19 | } |
---|
| 20 | |
---|
| 21 | if($_FILES['file_uploader_thumbnail']['size'] != 0 && $_FILES['file_uploader_file']['size'] != 0) { |
---|
[19681] | 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']); |
---|
[19472] | 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 | |
---|
| 39 | function file_uploader_upload_file($file_uploader_file) { |
---|
[19875] | 40 | require(FILE_UPLOADER_PATH.'install/config_default.inc.php'); |
---|
| 41 | require_once(FILE_UPLOADER_PATH.'install/functions.inc.php'); |
---|
[19472] | 42 | |
---|
| 43 | global $conf; |
---|
| 44 | $conf_file_uploader = unserialize($conf['file_uploader']); |
---|
| 45 | |
---|
| 46 | $file_uploader_file_tmp = $file_uploader_file['tmp_name']; |
---|
[19875] | 47 | $file_uploader_file_title = substr($file_uploader_file['name'], 0, strrpos($file_uploader_file['name'], '.')); |
---|
[19472] | 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']); |
---|
[19681] | 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; |
---|
[19472] | 60 | $file_uploader_errors = array(); |
---|
| 61 | $return = array(); |
---|
| 62 | |
---|
[19875] | 63 | $return['title'] = $file_uploader_file_title; |
---|
[19472] | 64 | $return['name_wo_extension'] = $file_uploader_file_name_wo_extension; |
---|
| 65 | $return['extension'] = $file_uploader_file_extension; |
---|
[19681] | 66 | $return['folder'] = $file_uploader_file_category; |
---|
[19472] | 67 | $return['destination'] = $file_uploader_file_destination; |
---|
| 68 | $return['size'] = $file_uploader_file_size; |
---|
| 69 | $return['overwrite'] = false; |
---|
| 70 | |
---|
[19875] | 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 | |
---|
[19472] | 74 | //If the directory does not exist, we create it |
---|
[19681] | 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; |
---|
[19775] | 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; |
---|
[19875] | 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); |
---|
[19775] | 98 | } |
---|
[19472] | 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'); |
---|
[19681] | 122 | } |
---|
[19472] | 123 | } else if (file_exists($file_uploader_file_destination)) { |
---|
| 124 | if ($conf_file_uploader['overwrite'] == 1) |
---|
| 125 | $return['overwrite'] = true; |
---|
| 126 | else |
---|
[19835] | 127 | $file_uploader_errors['already_exist'] = l10n('file_uploader_error_already_exist'); |
---|
[19472] | 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 | |
---|
[19681] | 138 | function file_uploader_upload_thumbnail($file_uploader_thumbnail, $file_uploader_file_name_wo_extension, $file_uploader_file_extension, $file_uploader_file_folder) { |
---|
[19875] | 139 | require(FILE_UPLOADER_PATH.'install/config_default.inc.php'); |
---|
[19472] | 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']); |
---|
[19681] | 144 | $file_uploader_thumbnail_directory = $file_uploader_galleries_dir.$file_uploader_file_folder.'/pwg_representative'; |
---|
[19472] | 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 | |
---|
| 190 | function 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']); |
---|
[19775] | 198 | $physical_category_id = pwg_db_fetch_assoc(pwg_query('SELECT id FROM '.CATEGORIES_TABLE.' WHERE name = "'.$file_uploader_file['folder'].'";')); |
---|
[19472] | 199 | $insert = array( |
---|
| 200 | 'file' => substr(strrchr($file_path, '/'), 1), |
---|
[19875] | 201 | 'name' => ($file_uploader_file_properties['title'] != '') ? pwg_db_real_escape_string($file_uploader_file_properties['title']) : $file_uploader_file['title'], |
---|
[19472] | 202 | 'comment' => pwg_db_real_escape_string($file_uploader_file_properties['description']), |
---|
| 203 | 'date_available' => $dbnow, |
---|
| 204 | 'path' => $file_path, |
---|
| 205 | 'representative_ext' => $file_uploader_thumbnail['extension'], |
---|
| 206 | 'filesize' => $file_uploader_file['size'], |
---|
[19775] | 207 | 'storage_category_id' => $physical_category_id, |
---|
[19472] | 208 | 'md5sum' => md5_file($file_path), |
---|
| 209 | 'added_by' => $user['id'], |
---|
| 210 | ); |
---|
| 211 | |
---|
| 212 | if ($file_uploader_file['overwrite']) { |
---|
| 213 | $query = 'SELECT id FROM '.IMAGES_TABLE.' WHERE path = "'.$file_path.'";'; |
---|
| 214 | $image_id = pwg_db_fetch_assoc(pwg_query($query)); |
---|
| 215 | single_update(IMAGES_TABLE, $insert, array('id' => $image_id['id'])); |
---|
| 216 | } else { |
---|
| 217 | single_insert(IMAGES_TABLE, $insert); |
---|
| 218 | $image_id = pwg_db_insert_id(IMAGES_TABLE); |
---|
| 219 | } |
---|
| 220 | |
---|
[19875] | 221 | if (!is_array($image_id)) |
---|
| 222 | $image_id = array($image_id); |
---|
| 223 | |
---|
[19472] | 224 | if(isset($file_uploader_file_properties['category']) and count($file_uploader_file_properties['category']) > 0) { |
---|
| 225 | associate_images_to_categories( |
---|
[19875] | 226 | $image_id, |
---|
[19472] | 227 | array($file_uploader_file_properties['category'], $physical_category_id['id']) |
---|
| 228 | ); |
---|
| 229 | } |
---|
| 230 | |
---|
[19875] | 231 | @fill_caddie($image_id); |
---|
[19472] | 232 | } |
---|
| 233 | |
---|
| 234 | |
---|
| 235 | //Categories |
---|
| 236 | $query = 'SELECT id,name,uppercats,global_rank FROM '.CATEGORIES_TABLE.';'; |
---|
| 237 | display_select_cat_wrapper($query, array(), 'category_parent_options'); |
---|
| 238 | |
---|
[19875] | 239 | //Add parameters to template |
---|
| 240 | $upload_max_filesize = min(get_ini_size('upload_max_filesize'), get_ini_size('post_max_size')); |
---|
| 241 | if ($upload_max_filesize == get_ini_size('upload_max_filesize')) { |
---|
| 242 | $upload_max_filesize = get_ini_size('upload_max_filesize', true); |
---|
| 243 | } else { |
---|
| 244 | $upload_max_filesize = get_ini_size('post_max_filesize', true); |
---|
| 245 | } |
---|
[21393] | 246 | $upload_max_filesize_display = round($upload_max_filesize/1024, 0); |
---|
[19875] | 247 | |
---|
| 248 | $template->assign( |
---|
| 249 | array( |
---|
| 250 | 'upload_max_filesize' => $upload_max_filesize, |
---|
| 251 | 'upload_max_filesize_display' => $upload_max_filesize_display, |
---|
| 252 | ) |
---|
| 253 | ); |
---|
| 254 | |
---|
[19472] | 255 | $template->set_filenames(array('plugin_admin_content' => dirname(__FILE__) . '/admin_upload.tpl')); |
---|
| 256 | $template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content'); |
---|
| 257 | ?> |
---|