| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
|---|
| 4 | |
|---|
| 5 | include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php'); |
|---|
| 6 | |
|---|
| 7 | $video_types = array('google', 'youtube', 'dailymotion', 'wideo', 'vimeo', 'wat'); |
|---|
| 8 | |
|---|
| 9 | function get_video_infos($url, $type) |
|---|
| 10 | { |
|---|
| 11 | switch ($type) |
|---|
| 12 | { |
|---|
| 13 | case "google": |
|---|
| 14 | @preg_match('#\=([\-\+0-9]*)#', $url, $id); |
|---|
| 15 | if (empty($id[1])) return false; |
|---|
| 16 | $video['id'] = $id[1]; |
|---|
| 17 | $video['ext'] = 'gvideo'; |
|---|
| 18 | if ($_POST['thumbnail'] == 'thumb_from_server' and fetchRemote($url, $source)) |
|---|
| 19 | { |
|---|
| 20 | @preg_match("#thumbnailUrl\\\\x3d(http://.*/ThumbnailServer.*)\\\\x26#", $source, $thumb_url); |
|---|
| 21 | $video['thumb_url'] = @urldecode($thumb_url[1]); |
|---|
| 22 | $video['thumb_url'] = @str_replace(array('\x3d', '\x26'), array('=', '&'), $video['thumb_url']); |
|---|
| 23 | } |
|---|
| 24 | return $video; |
|---|
| 25 | |
|---|
| 26 | case "youtube": |
|---|
| 27 | @preg_match('#\=([\-_a-z0-9]*)#i', $url, $id); |
|---|
| 28 | if (empty($id[1])) return false; |
|---|
| 29 | $video['id'] = $id[1]; |
|---|
| 30 | $video['ext'] = 'ytube'; |
|---|
| 31 | if ($_POST['thumbnail'] == 'thumb_from_server') |
|---|
| 32 | { |
|---|
| 33 | $video['thumb_url'] = 'http://img.youtube.com/vi/' . $video['id'] . '/default.jpg'; |
|---|
| 34 | } |
|---|
| 35 | return $video; |
|---|
| 36 | |
|---|
| 37 | case "dailymotion": |
|---|
| 38 | @preg_match('#video/([_a-z0-9]*)#i', $url, $id); |
|---|
| 39 | if (empty($id[1])) return false; |
|---|
| 40 | $video['id'] = $id[1]; |
|---|
| 41 | $video['ext'] = 'dm'; |
|---|
| 42 | if ($_POST['thumbnail'] == 'thumb_from_server') |
|---|
| 43 | { |
|---|
| 44 | $video['thumb_url'] = 'http://www.dailymotion.com/thumbnail/160x120/video/' . $video['id']; |
|---|
| 45 | } |
|---|
| 46 | return $video; |
|---|
| 47 | |
|---|
| 48 | case "wideo": |
|---|
| 49 | @preg_match('#video/([_a-z0-9]*)#i', $url, $id); |
|---|
| 50 | if (empty($id[1])) return false; |
|---|
| 51 | $video['id'] = $id[1]; |
|---|
| 52 | $video['ext'] = 'wideo'; |
|---|
| 53 | if ($_POST['thumbnail'] == 'thumb_from_server' and fetchRemote($url, $source)) |
|---|
| 54 | { |
|---|
| 55 | @preg_match('#link rel\="thumbnail" href\="(.*)"#', $source, $matches); |
|---|
| 56 | $video['thumb_url'] = @str_replace('74x54', '154x114', $matches[1]); |
|---|
| 57 | } |
|---|
| 58 | return $video; |
|---|
| 59 | |
|---|
| 60 | case "vimeo": |
|---|
| 61 | @preg_match('#vimeo.com/([0-9]*)#i', $url, $id); |
|---|
| 62 | if (empty($id[1])) return false; |
|---|
| 63 | $video['id'] = $id[1]; |
|---|
| 64 | $video['ext'] = 'vimeo'; |
|---|
| 65 | if ($_POST['thumbnail'] == 'thumb_from_server' and fetchRemote($url, $source)) |
|---|
| 66 | { |
|---|
| 67 | if (preg_match('#meta property="og:image" content="(http://.*?)"#', $source, $matches)) |
|---|
| 68 | { |
|---|
| 69 | $video['thumb_url'] = str_replace('160.jpg', '200.jpg', $matches[1]); |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | return $video; |
|---|
| 73 | |
|---|
| 74 | case "wat": |
|---|
| 75 | if (fetchRemote($url, $source)) |
|---|
| 76 | { |
|---|
| 77 | @preg_match('#link rel="video_src" href="http://www.wat.tv/swf2/(.*?)"#i', $source, $id); |
|---|
| 78 | if (empty($id[1])) return false; |
|---|
| 79 | $video['id'] = $id[1]; |
|---|
| 80 | $video['ext'] = 'wat'; |
|---|
| 81 | if ($_POST['thumbnail'] == 'thumb_from_server') |
|---|
| 82 | { |
|---|
| 83 | @preg_match('#link rel="image_src" href="(.*?)"#', $source, $matches); |
|---|
| 84 | $video['thumb_url'] = @str_replace('120x90', '320x240', $matches[1]); |
|---|
| 85 | } |
|---|
| 86 | return $video; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | default: |
|---|
| 90 | return false; |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | // Creation du nouveau fichier |
|---|
| 95 | if (isset($_POST['submit_add']) and !is_adviser()) |
|---|
| 96 | { |
|---|
| 97 | if (empty($_POST['pywaie_add_name']) or empty($_POST['pywaie_add_url']) or ($_POST['parent'] == 0)) |
|---|
| 98 | { |
|---|
| 99 | array_push($page['errors'], l10n('py_error2'), l10n('py_error3')); |
|---|
| 100 | } |
|---|
| 101 | else |
|---|
| 102 | { |
|---|
| 103 | $py_url = $_POST['pywaie_add_url']; |
|---|
| 104 | if (!substr_count($py_url, "http://")) $py_url = "http://" . $py_url; |
|---|
| 105 | |
|---|
| 106 | // Extraction de l'id et du type |
|---|
| 107 | foreach ($video_types as $type) |
|---|
| 108 | { |
|---|
| 109 | if (substr_count($py_url, $type)) |
|---|
| 110 | { |
|---|
| 111 | $video = get_video_infos($py_url, $type); |
|---|
| 112 | break; |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | if (!isset($video) or !$video) |
|---|
| 117 | { |
|---|
| 118 | array_unshift($page['errors'], l10n('py_error5')); |
|---|
| 119 | } |
|---|
| 120 | else |
|---|
| 121 | { |
|---|
| 122 | // current date |
|---|
| 123 | list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();')); |
|---|
| 124 | |
|---|
| 125 | $file_name = str2url($_POST['pywaie_add_name']).'.'.$video['ext']; |
|---|
| 126 | |
|---|
| 127 | // prepare database registration |
|---|
| 128 | $insert = array( |
|---|
| 129 | 'name' => $_POST['pywaie_add_name'], |
|---|
| 130 | 'file' => $file_name, |
|---|
| 131 | 'date_available' => $dbnow, |
|---|
| 132 | ); |
|---|
| 133 | |
|---|
| 134 | $optional_fields = array('author', 'comment'); |
|---|
| 135 | foreach ($optional_fields as $field) |
|---|
| 136 | { |
|---|
| 137 | if (isset($_POST[$field]) and !empty($_POST[$field])) |
|---|
| 138 | { |
|---|
| 139 | $insert[$field] = $_POST[$field]; |
|---|
| 140 | } |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | check_input_parameter('parent', $_POST, false, PATTERN_ID); |
|---|
| 144 | $category_id = $_POST['parent']; |
|---|
| 145 | |
|---|
| 146 | $query = ' |
|---|
| 147 | SELECT |
|---|
| 148 | c.id, |
|---|
| 149 | c.name, |
|---|
| 150 | c.permalink, |
|---|
| 151 | c.dir, |
|---|
| 152 | c.site_id, |
|---|
| 153 | s.galleries_url |
|---|
| 154 | FROM '.CATEGORIES_TABLE.' AS c |
|---|
| 155 | LEFT JOIN '.SITES_TABLE.' AS s ON s.id = c.site_id |
|---|
| 156 | WHERE c.id = '.$category_id.' |
|---|
| 157 | ;'; |
|---|
| 158 | $result = pwg_query($query); |
|---|
| 159 | $category = pwg_db_fetch_assoc($result); |
|---|
| 160 | |
|---|
| 161 | // is the category virtual or is the category on a remote site? |
|---|
| 162 | $use_galleries_directory = true; |
|---|
| 163 | if (empty($category['dir'])) |
|---|
| 164 | { |
|---|
| 165 | $use_galleries_directory = false; |
|---|
| 166 | } |
|---|
| 167 | if (!empty($category['galleries_url']) and url_is_remote($category['galleries_url'])) |
|---|
| 168 | { |
|---|
| 169 | $use_galleries_directory = false; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | if (!$use_galleries_directory) |
|---|
| 173 | { |
|---|
| 174 | list($year, $month, $day) = preg_split('/[^\d]/', $dbnow, 4); |
|---|
| 175 | |
|---|
| 176 | // upload directory hierarchy |
|---|
| 177 | $upload_dir = sprintf( |
|---|
| 178 | PHPWG_ROOT_PATH.$conf['upload_dir'].'/%s/%s/%s', |
|---|
| 179 | $year, |
|---|
| 180 | $month, |
|---|
| 181 | $day |
|---|
| 182 | ); |
|---|
| 183 | prepare_directory($upload_dir); |
|---|
| 184 | |
|---|
| 185 | $file_path = $upload_dir.'/'.$file_name; |
|---|
| 186 | $thumb_path = file_path_for_type($file_path, 'thumb'); |
|---|
| 187 | $thumb_dir = dirname($thumb_path); |
|---|
| 188 | prepare_directory($thumb_dir); |
|---|
| 189 | } |
|---|
| 190 | // or physical and local |
|---|
| 191 | else |
|---|
| 192 | { |
|---|
| 193 | $catpath = get_fulldirs(array($category_id)); |
|---|
| 194 | $file_path = $catpath[$category_id].'/'.$file_name; |
|---|
| 195 | |
|---|
| 196 | $insert['storage_category_id'] = $category_id; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | $insert['path'] = $file_path; |
|---|
| 200 | |
|---|
| 201 | if (file_exists($file_path)) |
|---|
| 202 | { |
|---|
| 203 | array_push($page['errors'], sprintf(l10n('py_error6'), $file_path)); |
|---|
| 204 | } |
|---|
| 205 | else |
|---|
| 206 | { |
|---|
| 207 | // Write fake file with video settings inside |
|---|
| 208 | // |
|---|
| 209 | // TODO: store these information in a specific database table instead |
|---|
| 210 | $file_content = stripslashes($video['id']); |
|---|
| 211 | $file_content.= '/'.$_POST['pywaie_add_h']; |
|---|
| 212 | $file_content.= '/'.$_POST['pywaie_add_w']; |
|---|
| 213 | $file_content.= '/'.$_POST['pywaie_add_start']; |
|---|
| 214 | |
|---|
| 215 | $bytes_written = file_put_contents($file_path, $file_content); |
|---|
| 216 | if (false === $bytes_written) |
|---|
| 217 | { |
|---|
| 218 | array_push( |
|---|
| 219 | $page['errors'], |
|---|
| 220 | |
|---|
| 221 | sprintf( |
|---|
| 222 | l10n('py_error7'), |
|---|
| 223 | $file_path |
|---|
| 224 | ), |
|---|
| 225 | |
|---|
| 226 | sprintf( |
|---|
| 227 | l10n('py_error8'), |
|---|
| 228 | dirname($file_path) |
|---|
| 229 | ) |
|---|
| 230 | ); |
|---|
| 231 | |
|---|
| 232 | } |
|---|
| 233 | else |
|---|
| 234 | { |
|---|
| 235 | // thumbnail |
|---|
| 236 | $thumb_extension = null; |
|---|
| 237 | if ($_POST['thumbnail'] == 'thumb_from_server' or $_POST['thumbnail'] == 'thumb_from_user') |
|---|
| 238 | { |
|---|
| 239 | include_once ('thumbnails.php'); |
|---|
| 240 | $insert['tn_ext'] = $thumb_extension; |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | // database registration |
|---|
| 244 | mass_inserts( |
|---|
| 245 | IMAGES_TABLE, |
|---|
| 246 | array_keys($insert), |
|---|
| 247 | array($insert) |
|---|
| 248 | ); |
|---|
| 249 | |
|---|
| 250 | $image_id = pwg_db_insert_id(IMAGES_TABLE); |
|---|
| 251 | associate_images_to_categories(array($image_id), array($category_id)); |
|---|
| 252 | invalidate_user_cache(); |
|---|
| 253 | |
|---|
| 254 | // success information to display |
|---|
| 255 | array_unshift($page['infos'], sprintf(l10n('py_info3'), $file_path)); |
|---|
| 256 | |
|---|
| 257 | array_push( |
|---|
| 258 | $page['infos'], |
|---|
| 259 | sprintf( |
|---|
| 260 | l10n('py_show_file'), |
|---|
| 261 | make_picture_url( |
|---|
| 262 | array( |
|---|
| 263 | 'image_id' => $image_id, |
|---|
| 264 | 'section' => 'categories', |
|---|
| 265 | 'category' => $category, |
|---|
| 266 | ) |
|---|
| 267 | ) |
|---|
| 268 | ) |
|---|
| 269 | ); |
|---|
| 270 | } |
|---|
| 271 | } |
|---|
| 272 | } |
|---|
| 273 | } |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | // display list of all categories |
|---|
| 278 | $query = ' |
|---|
| 279 | SELECT |
|---|
| 280 | id, |
|---|
| 281 | name, |
|---|
| 282 | uppercats, |
|---|
| 283 | global_rank |
|---|
| 284 | FROM '.CATEGORIES_TABLE.' |
|---|
| 285 | ;'; |
|---|
| 286 | |
|---|
| 287 | if (isset($_POST['parent'])) |
|---|
| 288 | { |
|---|
| 289 | $selected = array($_POST['parent']); |
|---|
| 290 | } |
|---|
| 291 | else |
|---|
| 292 | { |
|---|
| 293 | $selected = array(); |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | display_select_cat_wrapper($query, $selected , 'category_option_parent', false); |
|---|
| 297 | |
|---|
| 298 | |
|---|
| 299 | // Parametrage du template |
|---|
| 300 | if (isset($_POST['submit_add'])) |
|---|
| 301 | { |
|---|
| 302 | $template->assign(array( |
|---|
| 303 | 'PYWAIE_ADD_NAME' => $_POST['pywaie_add_name'], |
|---|
| 304 | 'PYWAIE_ADD_URL' => $_POST['pywaie_add_url'], |
|---|
| 305 | $_POST['thumbnail'] . '_CHECKED' => 'checked="checked"', |
|---|
| 306 | 'ADD_BAND' => isset($_POST['add_band']) ? 'checked="checked"' : '', |
|---|
| 307 | 'THUMB_RESIZE' => isset($_POST['thumb_resize']) ? 'checked="checked"' : '', |
|---|
| 308 | 'PYWAIE_ADD_START' => $_POST['pywaie_add_start'], |
|---|
| 309 | 'PYWAIE_ADD_W' => $_POST['pywaie_add_w'], |
|---|
| 310 | 'PYWAIE_ADD_H' => $_POST['pywaie_add_h'], |
|---|
| 311 | 'AUTHOR' => $_POST['author'], |
|---|
| 312 | 'COMMENT' => $_POST['comment'])); |
|---|
| 313 | } |
|---|
| 314 | else |
|---|
| 315 | { |
|---|
| 316 | $template->assign(array('no_thumb_CHECKED' => 'checked="checked"')); |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | $template->assign(array( |
|---|
| 320 | 'INFOBULLES_JS' => GVIDEO_PATH . 'admin/infobulles.js', |
|---|
| 321 | 'ICON_INFOBULLE' => GVIDEO_PATH . 'admin/infobulle.png', |
|---|
| 322 | 'DEFAULT_THUMB_W' => $conf['tn_width'], |
|---|
| 323 | 'DEFAULT_THUMB_H' => $conf['tn_height'])); |
|---|
| 324 | |
|---|
| 325 | $template->set_filenames(array('plugin_admin_content' => dirname(__FILE__) . '/add_page.tpl')); |
|---|
| 326 | $template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content'); |
|---|
| 327 | |
|---|
| 328 | ?> |
|---|