source: extensions/gvideo/admin/add_page.php @ 7284

Last change on this file since 7284 was 7284, checked in by plg, 13 years ago

feature 1941: make PY Gvideo plugin compatible with virtual categories (and remote sites as well)

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