| 1 | <?php |
|---|
| 2 | // +-----------------------------------------------------------------------+ |
|---|
| 3 | // | Piwigo - a PHP based picture gallery | |
|---|
| 4 | // +-----------------------------------------------------------------------+ |
|---|
| 5 | // | Copyright(C) 2010 Pierrick LE GALL http://piwigo.org | |
|---|
| 6 | // +-----------------------------------------------------------------------+ |
|---|
| 7 | // | This program is free software; you can redistribute it and/or modify | |
|---|
| 8 | // | it under the terms of the GNU General Public License as published by | |
|---|
| 9 | // | the Free Software Foundation | |
|---|
| 10 | // | | |
|---|
| 11 | // | This program is distributed in the hope that it will be useful, but | |
|---|
| 12 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
|---|
| 13 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
|---|
| 14 | // | General Public License for more details. | |
|---|
| 15 | // | | |
|---|
| 16 | // | You should have received a copy of the GNU General Public License | |
|---|
| 17 | // | along with this program; if not, write to the Free Software | |
|---|
| 18 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
|---|
| 19 | // | USA. | |
|---|
| 20 | // +-----------------------------------------------------------------------+ |
|---|
| 21 | |
|---|
| 22 | if( !defined("PHPWG_ROOT_PATH") ) |
|---|
| 23 | { |
|---|
| 24 | die ("Hacking attempt!"); |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
|---|
| 28 | include_once(UPLOAD_FORM_PATH.'include/functions_upload.inc.php'); |
|---|
| 29 | load_language('plugin.lang', UPLOAD_FORM_PATH); |
|---|
| 30 | |
|---|
| 31 | $admin_base_url = get_root_url().'admin.php?page=plugin§ion=upload_form%2Fupload.php'; |
|---|
| 32 | $thumbnails = array(); |
|---|
| 33 | |
|---|
| 34 | // +-----------------------------------------------------------------------+ |
|---|
| 35 | // | Check Access and exit when user status is not ok | |
|---|
| 36 | // +-----------------------------------------------------------------------+ |
|---|
| 37 | check_status(ACCESS_ADMINISTRATOR); |
|---|
| 38 | |
|---|
| 39 | // +-----------------------------------------------------------------------+ |
|---|
| 40 | // | process form | |
|---|
| 41 | // +-----------------------------------------------------------------------+ |
|---|
| 42 | |
|---|
| 43 | if (isset($_POST['submit_upload'])) |
|---|
| 44 | { |
|---|
| 45 | $category_id = null; |
|---|
| 46 | if ('existing' == $_POST['category_type']) |
|---|
| 47 | { |
|---|
| 48 | $category_id = $_POST['category']; |
|---|
| 49 | } |
|---|
| 50 | elseif ('new' == $_POST['category_type']) |
|---|
| 51 | { |
|---|
| 52 | $output_create = create_virtual_category( |
|---|
| 53 | $_POST['category_name'], |
|---|
| 54 | (0 == $_POST['category_parent'] ? null : $_POST['category_parent']) |
|---|
| 55 | ); |
|---|
| 56 | |
|---|
| 57 | $category_id = $output_create['id']; |
|---|
| 58 | |
|---|
| 59 | if (isset($output_create['error'])) |
|---|
| 60 | { |
|---|
| 61 | array_push($page['errors'], $output_create['error']); |
|---|
| 62 | } |
|---|
| 63 | else |
|---|
| 64 | { |
|---|
| 65 | $category_name = get_cat_display_name_from_id($category_id, 'admin.php?page=cat_modify&cat_id='); |
|---|
| 66 | // information |
|---|
| 67 | array_push( |
|---|
| 68 | $page['infos'], |
|---|
| 69 | sprintf( |
|---|
| 70 | l10n('Category "%s" has been added'), |
|---|
| 71 | '<em>'.$category_name.'</em>' |
|---|
| 72 | ) |
|---|
| 73 | ); |
|---|
| 74 | // TODO: add the onclick="window.open(this.href); return false;" |
|---|
| 75 | // attribute with jQuery on upload.tpl side for href containing |
|---|
| 76 | // "cat_modify" |
|---|
| 77 | } |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | $starttime = get_moment(); |
|---|
| 81 | |
|---|
| 82 | foreach ($_FILES['image_upload']['error'] as $idx => $error) |
|---|
| 83 | { |
|---|
| 84 | if (UPLOAD_ERR_OK == $error) |
|---|
| 85 | { |
|---|
| 86 | $source_filepath = $_FILES['image_upload']['tmp_name'][$idx]; |
|---|
| 87 | $original_filename = $_FILES['image_upload']['name'][$idx]; |
|---|
| 88 | |
|---|
| 89 | $image_id = add_uploaded_file( |
|---|
| 90 | $source_filepath, |
|---|
| 91 | $original_filename, |
|---|
| 92 | array($category_id), |
|---|
| 93 | $_POST['level'] |
|---|
| 94 | ); |
|---|
| 95 | |
|---|
| 96 | // TODO: if $image_id is not an integer, something went wrong |
|---|
| 97 | |
|---|
| 98 | // we could return the list of properties from the add_uploaded_file |
|---|
| 99 | // function, but I like the "double check". And it costs nothing |
|---|
| 100 | // compared to the upload process. |
|---|
| 101 | $thumbnail = array(); |
|---|
| 102 | |
|---|
| 103 | $query = ' |
|---|
| 104 | SELECT |
|---|
| 105 | file, |
|---|
| 106 | path, |
|---|
| 107 | tn_ext |
|---|
| 108 | FROM '.IMAGES_TABLE.' |
|---|
| 109 | WHERE id = '.$image_id.' |
|---|
| 110 | ;'; |
|---|
| 111 | $image_infos = mysql_fetch_assoc(pwg_query($query)); |
|---|
| 112 | |
|---|
| 113 | $thumbnail['file'] = $image_infos['file']; |
|---|
| 114 | |
|---|
| 115 | $thumbnail['src'] = get_thumbnail_location( |
|---|
| 116 | array( |
|---|
| 117 | 'path' => $image_infos['path'], |
|---|
| 118 | 'tn_ext' => $image_infos['tn_ext'], |
|---|
| 119 | ) |
|---|
| 120 | ); |
|---|
| 121 | |
|---|
| 122 | // TODO: when implementing this plugin in Piwigo core, we should have |
|---|
| 123 | // a function get_image_name($name, $file) (if name is null, then |
|---|
| 124 | // compute a temporary name from filename) that would be also used in |
|---|
| 125 | // picture.php. UPDATE: in fact, "get_name_from_file($file)" already |
|---|
| 126 | // exists and is used twice (element_set_unit + comments, but not in |
|---|
| 127 | // picture.php I don't know why) with the same pattern if |
|---|
| 128 | // (empty($name)) {$name = get_name_from_file($file)}, a clean |
|---|
| 129 | // function get_image_name($name, $file) would be better |
|---|
| 130 | $thumbnail['title'] = get_name_from_file($image_infos['file']); |
|---|
| 131 | |
|---|
| 132 | $thumbnail['link'] = PHPWG_ROOT_PATH.'admin.php?page=picture_modify' |
|---|
| 133 | .'&image_id='.$image_id |
|---|
| 134 | .'&cat_id='.$category_id |
|---|
| 135 | ; |
|---|
| 136 | |
|---|
| 137 | array_push($thumbnails, $thumbnail); |
|---|
| 138 | } |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | $endtime = get_moment(); |
|---|
| 142 | $elapsed = ($endtime - $starttime) * 1000; |
|---|
| 143 | // printf('%.2f ms', $elapsed); |
|---|
| 144 | |
|---|
| 145 | if (!empty($thumbnails)) |
|---|
| 146 | { |
|---|
| 147 | array_push( |
|---|
| 148 | $page['infos'], |
|---|
| 149 | sprintf( |
|---|
| 150 | l10n('%d photos uploaded'), |
|---|
| 151 | count($thumbnails) |
|---|
| 152 | ) |
|---|
| 153 | ); |
|---|
| 154 | |
|---|
| 155 | if (0 != $_POST['level']) |
|---|
| 156 | { |
|---|
| 157 | array_push( |
|---|
| 158 | $page['infos'], |
|---|
| 159 | sprintf( |
|---|
| 160 | l10n('Privacy level set to "%s"'), |
|---|
| 161 | l10n( |
|---|
| 162 | sprintf('Level %d', $_POST['level']) |
|---|
| 163 | ) |
|---|
| 164 | ) |
|---|
| 165 | ); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | if ('existing' == $_POST['category_type']) |
|---|
| 169 | { |
|---|
| 170 | $query = ' |
|---|
| 171 | SELECT |
|---|
| 172 | COUNT(*) |
|---|
| 173 | FROM '.IMAGE_CATEGORY_TABLE.' |
|---|
| 174 | WHERE category_id = '.$category_id.' |
|---|
| 175 | ;'; |
|---|
| 176 | list($count) = mysql_fetch_row(pwg_query($query)); |
|---|
| 177 | $category_name = get_cat_display_name_from_id($category_id, 'admin.php?page=cat_modify&cat_id='); |
|---|
| 178 | |
|---|
| 179 | // information |
|---|
| 180 | array_push( |
|---|
| 181 | $page['infos'], |
|---|
| 182 | sprintf( |
|---|
| 183 | l10n('Category "%s" now contains %d photos'), |
|---|
| 184 | '<em>'.$category_name.'</em>', |
|---|
| 185 | $count |
|---|
| 186 | ) |
|---|
| 187 | ); |
|---|
| 188 | } |
|---|
| 189 | } |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | // +-----------------------------------------------------------------------+ |
|---|
| 193 | // | template init | |
|---|
| 194 | // +-----------------------------------------------------------------------+ |
|---|
| 195 | |
|---|
| 196 | $template->set_filenames( |
|---|
| 197 | array( |
|---|
| 198 | 'plugin_admin_content' => dirname(__FILE__).'/upload.tpl' |
|---|
| 199 | ) |
|---|
| 200 | ); |
|---|
| 201 | |
|---|
| 202 | $template->assign( |
|---|
| 203 | array( |
|---|
| 204 | 'F_ADD_ACTION'=> $admin_base_url, |
|---|
| 205 | 'plugin_path' => UPLOAD_FORM_PATH, |
|---|
| 206 | 'thumbnails' => $thumbnails, |
|---|
| 207 | ) |
|---|
| 208 | ); |
|---|
| 209 | |
|---|
| 210 | $query = ' |
|---|
| 211 | SELECT id,name,uppercats,global_rank |
|---|
| 212 | FROM '.CATEGORIES_TABLE.' |
|---|
| 213 | ;'; |
|---|
| 214 | |
|---|
| 215 | display_select_cat_wrapper( |
|---|
| 216 | $query, |
|---|
| 217 | array(), |
|---|
| 218 | 'category_options' |
|---|
| 219 | ); |
|---|
| 220 | |
|---|
| 221 | // image level options |
|---|
| 222 | $tpl_options = array(); |
|---|
| 223 | foreach ($conf['available_permission_levels'] as $level) |
|---|
| 224 | { |
|---|
| 225 | $tpl_options[$level] = l10n( sprintf('Level %d', $level) ); |
|---|
| 226 | } |
|---|
| 227 | $selected_level = isset($_POST['level']) ? $_POST['level'] : 0; |
|---|
| 228 | $template->assign( |
|---|
| 229 | array( |
|---|
| 230 | 'level_options'=> $tpl_options, |
|---|
| 231 | 'level_options_selected' => array($selected_level) |
|---|
| 232 | ) |
|---|
| 233 | ); |
|---|
| 234 | |
|---|
| 235 | // +-----------------------------------------------------------------------+ |
|---|
| 236 | // | setup errors | |
|---|
| 237 | // +-----------------------------------------------------------------------+ |
|---|
| 238 | |
|---|
| 239 | $setup_errors = array(); |
|---|
| 240 | |
|---|
| 241 | $upload_base_dir = 'upload'; |
|---|
| 242 | $upload_dir = PHPWG_ROOT_PATH.$upload_base_dir; |
|---|
| 243 | |
|---|
| 244 | if (!is_dir($upload_dir)) |
|---|
| 245 | { |
|---|
| 246 | if (!is_writable(PHPWG_ROOT_PATH)) |
|---|
| 247 | { |
|---|
| 248 | array_push( |
|---|
| 249 | $setup_errors, |
|---|
| 250 | sprintf( |
|---|
| 251 | l10n('Create the "%s" directory at the root of your Piwigo installation'), |
|---|
| 252 | $upload_base_dir |
|---|
| 253 | ) |
|---|
| 254 | ); |
|---|
| 255 | } |
|---|
| 256 | } |
|---|
| 257 | else |
|---|
| 258 | { |
|---|
| 259 | if (!is_writable($upload_dir)) |
|---|
| 260 | { |
|---|
| 261 | @chmod($upload_dir, 0777); |
|---|
| 262 | |
|---|
| 263 | if (!is_writable($upload_dir)) |
|---|
| 264 | { |
|---|
| 265 | array_push( |
|---|
| 266 | $setup_errors, |
|---|
| 267 | sprintf( |
|---|
| 268 | l10n('Give write access (chmod 777) to "%s" directory at the root of your Piwigo installation'), |
|---|
| 269 | $upload_base_dir |
|---|
| 270 | ) |
|---|
| 271 | ); |
|---|
| 272 | } |
|---|
| 273 | } |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | $template->assign( |
|---|
| 277 | array( |
|---|
| 278 | 'setup_errors'=> $setup_errors, |
|---|
| 279 | ) |
|---|
| 280 | ); |
|---|
| 281 | |
|---|
| 282 | // +-----------------------------------------------------------------------+ |
|---|
| 283 | // | sending html code | |
|---|
| 284 | // +-----------------------------------------------------------------------+ |
|---|
| 285 | |
|---|
| 286 | $template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content'); |
|---|
| 287 | ?> |
|---|