[19804] | 1 | <?php |
---|
| 2 | defined('URLUPLOADER_PATH') or die('Hacking attempt!'); |
---|
| 3 | |
---|
| 4 | global $template, $page, $conf; |
---|
| 5 | |
---|
| 6 | load_language('plugin.lang', URLUPLOADER_PATH); |
---|
| 7 | |
---|
| 8 | // +-----------------------------------------------------------------------+ |
---|
| 9 | // | URL Uploader tab | |
---|
| 10 | // +-----------------------------------------------------------------------+ |
---|
| 11 | define('PHOTOS_ADD_BASE_URL', get_root_url().'admin.php?page=photos_add'); |
---|
| 12 | |
---|
| 13 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
| 14 | include_once(PHPWG_ROOT_PATH.'admin/include/tabsheet.class.php'); |
---|
| 15 | include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php'); |
---|
| 16 | |
---|
| 17 | $allowed_extensions = array('jpg','jpeg','png','gif'); |
---|
| 18 | $allowed_mimes = array('image/jpeg', 'image/png', 'image/gif'); |
---|
| 19 | |
---|
| 20 | $tabsheet = new tabsheet(); |
---|
| 21 | $tabsheet->set_id('photos_add'); |
---|
| 22 | $tabsheet->select('url_uploader'); |
---|
| 23 | $tabsheet->assign(); |
---|
| 24 | |
---|
[26387] | 25 | $page['active_menu'] = get_active_menu('photo'); |
---|
[19804] | 26 | |
---|
[26387] | 27 | |
---|
[19804] | 28 | // +-----------------------------------------------------------------------+ |
---|
| 29 | // | process form | |
---|
| 30 | // +-----------------------------------------------------------------------+ |
---|
| 31 | if (isset($_GET['processed'])) |
---|
| 32 | { |
---|
| 33 | $category_id = $_POST['category']; |
---|
| 34 | $image_ids = array(); |
---|
| 35 | $page['thumbnails'] = array(); |
---|
| 36 | |
---|
| 37 | // SINGLE UPLOAD |
---|
| 38 | if ($_GET['upload_mode'] == 'single') |
---|
| 39 | { |
---|
| 40 | $_POST = array_map('trim', $_POST); |
---|
| 41 | |
---|
| 42 | // check empty url |
---|
| 43 | if (empty($_POST['file_url'])) |
---|
| 44 | { |
---|
| 45 | $page['errors'][] = l10n('File URL is empty'); |
---|
| 46 | } |
---|
| 47 | // check remote url |
---|
| 48 | else if (!url_is_remote($_POST['file_url'])) |
---|
| 49 | { |
---|
| 50 | $page['errors'][] = l10n('Invalid file URL'); |
---|
| 51 | } |
---|
| 52 | // check file extension |
---|
| 53 | else if (!in_array(strtolower(get_extension($_POST['file_url'])), $allowed_extensions)) |
---|
| 54 | { |
---|
| 55 | $page['errors'][] = l10n('Invalid file type'); |
---|
| 56 | } |
---|
| 57 | // continue... |
---|
| 58 | else |
---|
| 59 | { |
---|
| 60 | $temp_filename = $conf['data_location'].basename($_POST['file_url']); |
---|
| 61 | $file = fopen($temp_filename, 'w+'); |
---|
| 62 | $result = fetchRemote($_POST['file_url'], $file); |
---|
| 63 | fclose($file); |
---|
| 64 | |
---|
| 65 | // download failed ? |
---|
| 66 | if (!$result) |
---|
| 67 | { |
---|
| 68 | @unlink($temp_filename); |
---|
| 69 | $page['errors'][] = l10n('Unable to download file'); |
---|
| 70 | } |
---|
| 71 | // check mime-type |
---|
| 72 | else if (!in_array(get_mime($temp_filename, $allowed_mimes[0]), $allowed_mimes)) |
---|
| 73 | { |
---|
| 74 | @unlink($temp_filename); |
---|
| 75 | $page['errors'][] = l10n('Invalid file type'); |
---|
| 76 | } |
---|
| 77 | // continue... |
---|
| 78 | else |
---|
| 79 | { |
---|
| 80 | $image_id = add_uploaded_file( |
---|
| 81 | $temp_filename, |
---|
| 82 | basename($temp_filename), |
---|
| 83 | array($category_id), |
---|
| 84 | $_POST['level'] |
---|
| 85 | ); |
---|
| 86 | |
---|
[20407] | 87 | $updates = array(); |
---|
[19804] | 88 | if (!empty($_POST['photo_name'])) |
---|
| 89 | { |
---|
[20407] | 90 | $updates['name'] = $_POST['photo_name']; |
---|
[19804] | 91 | } |
---|
[20407] | 92 | if (isset($_POST['url_in_comment'])) |
---|
| 93 | { |
---|
| 94 | $url = parse_url($_POST['file_url']); |
---|
| 95 | $url = $url['scheme'].'://'.$url['host']; |
---|
| 96 | $updates['comment'] = '<a href="'. $url . '">'. $url .'</a>'; |
---|
| 97 | } |
---|
[19804] | 98 | |
---|
[20407] | 99 | single_update( |
---|
| 100 | IMAGES_TABLE, |
---|
| 101 | $updates, |
---|
| 102 | array('id' => $image_id) |
---|
| 103 | ); |
---|
| 104 | |
---|
[19804] | 105 | $image_ids = array($image_id); |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | // MULTIPLE UPLOAD |
---|
| 110 | else if ($_GET['upload_mode'] == 'multiple') |
---|
| 111 | { |
---|
| 112 | if (isset($_POST['onUploadError']) and is_array($_POST['onUploadError']) and count($_POST['onUploadError']) > 0) |
---|
| 113 | { |
---|
[26387] | 114 | $page['errors'][] = l10n('%d photos not imported', count($_POST['onUploadError'])); |
---|
[19804] | 115 | foreach ($_POST['onUploadError'] as $error) |
---|
| 116 | { |
---|
[26387] | 117 | $page['errors'][] = $error; |
---|
[19804] | 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | if (isset($_POST['imageIds']) and is_array($_POST['imageIds']) and count($_POST['imageIds']) > 0) |
---|
| 122 | { |
---|
| 123 | $image_ids = $_POST['imageIds']; |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | // DISPLAY RESULTS |
---|
| 128 | foreach ($image_ids as $image_id) |
---|
| 129 | { |
---|
| 130 | $query = ' |
---|
| 131 | SELECT id, file, path |
---|
| 132 | FROM '.IMAGES_TABLE.' |
---|
| 133 | WHERE id = '.$image_id.' |
---|
| 134 | ;'; |
---|
| 135 | $image_infos = pwg_db_fetch_assoc(pwg_query($query)); |
---|
| 136 | |
---|
| 137 | $thumbnail = array( |
---|
| 138 | 'file' => $image_infos['file'], |
---|
| 139 | 'src' => DerivativeImage::thumb_url($image_infos), |
---|
| 140 | 'title' => get_name_from_file($image_infos['file']), |
---|
| 141 | 'link' => get_root_url().'admin.php?page=photo-'.$image_id.'&cat_id='.$category_id, |
---|
| 142 | ); |
---|
| 143 | |
---|
[26387] | 144 | $page['thumbnails'][] = $thumbnail; |
---|
[19804] | 145 | } |
---|
| 146 | |
---|
| 147 | if (!empty($page['thumbnails'])) |
---|
| 148 | { |
---|
| 149 | // nb uploaded |
---|
[26387] | 150 | $page['infos'][] = l10n('%d photos uploaded', count($page['thumbnails'])); |
---|
[19804] | 151 | |
---|
| 152 | // level |
---|
| 153 | if (0 != $_POST['level']) |
---|
| 154 | { |
---|
[26387] | 155 | $page['infos'][] = l10n('Privacy level set to "%s"', l10n(sprintf('Level %d', $_POST['level']))); |
---|
[19804] | 156 | } |
---|
| 157 | |
---|
| 158 | // new category count |
---|
| 159 | $query = ' |
---|
| 160 | SELECT COUNT(*) |
---|
| 161 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
| 162 | WHERE category_id = '.$category_id.' |
---|
| 163 | ;'; |
---|
| 164 | list($count) = pwg_db_fetch_row(pwg_query($query)); |
---|
| 165 | $category_name = get_cat_display_name_from_id($category_id, 'admin.php?page=album-'); |
---|
| 166 | |
---|
[26387] | 167 | $page['infos'][] = l10n('Album "%s" now contains %d photos', '<em>'.$category_name.'</em>', $count); |
---|
[19804] | 168 | |
---|
| 169 | $page['batch_link'] = PHOTOS_ADD_BASE_URL.'&batch='.implode(',', $image_ids); |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | |
---|
| 174 | // +-----------------------------------------------------------------------+ |
---|
| 175 | // | prepare form | |
---|
| 176 | // +-----------------------------------------------------------------------+ |
---|
| 177 | include(PHPWG_ROOT_PATH.'admin/include/photos_add_direct_prepare.inc.php'); |
---|
| 178 | |
---|
| 179 | // upload mode |
---|
| 180 | $upload_modes = array('single', 'multiple'); |
---|
| 181 | $upload_mode = isset($conf['url_uploader_mode']) ? $conf['url_uploader_mode'] : 'single'; |
---|
| 182 | |
---|
[26387] | 183 | if (isset($_GET['upload_mode']) and $_GET['upload_mode']!=$upload_mode and in_array($_GET['upload_mode'], $upload_modes)) |
---|
[19804] | 184 | { |
---|
| 185 | $upload_mode = $_GET['upload_mode']; |
---|
| 186 | conf_update_param('url_uploader_mode', $upload_mode); |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | // what is the upload switch mode |
---|
| 190 | $index_of_upload_mode = array_flip($upload_modes); |
---|
| 191 | $upload_mode_index = $index_of_upload_mode[$upload_mode]; |
---|
| 192 | $upload_switch = $upload_modes[ ($upload_mode_index + 1) % 2 ]; |
---|
| 193 | |
---|
| 194 | $template->assign(array( |
---|
| 195 | 'upload_mode' => $upload_mode, |
---|
| 196 | 'form_action' => URLUPLOADER_ADMIN.'&upload_mode='.$upload_mode.'&processed=1', |
---|
| 197 | 'switch_url' => URLUPLOADER_ADMIN.'&upload_mode='.$upload_switch, |
---|
| 198 | 'another_upload_link' => URLUPLOADER_ADMIN.'&upload_mode='.$upload_mode, |
---|
| 199 | )); |
---|
| 200 | |
---|
| 201 | |
---|
| 202 | $template->set_filename('urluploader_content', realpath(URLUPLOADER_PATH . 'template/photos_add.tpl')); |
---|
| 203 | |
---|
| 204 | // template vars |
---|
| 205 | $template->assign(array( |
---|
| 206 | 'URLUPLOADER_PATH' => get_root_url() . URLUPLOADER_PATH, |
---|
| 207 | 'URLUPLOADER_ADMIN' => URLUPLOADER_ADMIN, |
---|
| 208 | )); |
---|
| 209 | |
---|
| 210 | // send page content |
---|
| 211 | $template->assign_var_from_handle('ADMIN_CONTENT', 'urluploader_content'); |
---|