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

Last change on this file since 3296 was 3296, checked in by patdenice, 15 years ago

New extension added:
PY GVideo (2.0.f)

File size: 11.0 KB
Line 
1<?php
2
3if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
4
5$video_types = array('google', 'youtube', 'dailymotion', 'wideo', 'vimeo');
6
7function get_video_infos($url, $type)
8{
9  switch ($type)
10  {
11  case "google":
12    @preg_match('#\=([\-\+0-9]*)#', $url, $id);
13    if (empty($id[1])) return false;
14    $video['id'] = $id[1];
15    $video['ext'] = 'gvideo';
16    if ($_POST['thumbnail'] == 'thumb_from_server' and fetchRemote($url, $source))
17    {
18      @preg_match("#thumbnailUrl\\\\x3d(http://.*/ThumbnailServer.*)\\\\x26#", $source, $thumb_url);
19      //$video['thumb_url'] = @str_replace(array('\x3d', '\x26'), array('=', '&'), $thumb_url[1]);
20      $video['thumb_url'] = @urldecode($thumb_url[1]);
21    }
22    return $video;
23
24  case "youtube":
25    @preg_match('#\=([\-_a-z0-9]*)#i', $url, $id);
26    if (empty($id[1])) return false;
27    $video['id'] = $id[1];
28    $video['ext'] = 'ytube';
29    if ($_POST['thumbnail'] == 'thumb_from_server')
30    {
31      $video['thumb_url'] = 'http://img.youtube.com/vi/' . $video['id'] . '/default.jpg';
32    }
33    return $video;
34
35  case "dailymotion":
36    @preg_match('#video/([_a-z0-9]*)#i', $url, $id);
37    if (empty($id[1])) return false;
38    $video['id'] = $id[1];
39    $video['ext'] = 'dm';
40    if ($_POST['thumbnail'] == 'thumb_from_server')
41    {
42      $video['thumb_url'] = 'http://www.dailymotion.com/thumbnail/160x120/video/' . $video['id'];
43    }
44    return $video;
45   
46  case "wideo":
47    @preg_match('#video/([_a-z0-9]*)#i', $url, $id);
48    if (empty($id[1])) return false;
49    $video['id'] = $id[1];
50    $video['ext'] = 'wideo';
51    if ($_POST['thumbnail'] == 'thumb_from_server' and fetchRemote($url, $source))
52    {
53      @preg_match('#link rel\="thumbnail" href\="(.*)"#', $source, $matches);
54      $video['thumb_url'] = @str_replace('74x54', '154x114', $matches[1]);
55    }
56    return $video;
57
58  case "vimeo":
59    @preg_match('#vimeo.com/([0-9]*)#i', $url, $id);
60    if (empty($id[1])) return false;
61    $video['id'] = $id[1];
62    $video['ext'] = 'vimeo';
63    if ($_POST['thumbnail'] == 'thumb_from_server' and fetchRemote($url, $source))
64    {
65      @preg_match('#link rel="image_src" href="(http://images.vimeo.com/.*?)"#', $source, $matches);
66      $video['thumb_url'] = @str_replace('160.jpg', '200.jpg', $matches[1]);
67    }
68    return $video;
69
70  default:
71    return false;
72  }
73}
74
75/* MUST BE REMOVED WITH PIWIGO 2.0.0 */
76if (!function_exists('fetchRemote'))
77{
78  function fetchRemote($src, &$dest, $user_agent='Piwigo', $step=0)
79  {
80    // After 3 redirections, return false
81    if ($step > 3) return false;
82
83    // Initialize $dest
84    is_resource($dest) or $dest = '';
85
86    // Try curl to read remote file
87    if (function_exists('curl_init'))
88    {
89      $ch = @curl_init();
90      @curl_setopt($ch, CURLOPT_URL, $src);
91      @curl_setopt($ch, CURLOPT_HEADER, 1);
92      @curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
93      @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
94      $content = @curl_exec($ch);
95      $header_length = @curl_getinfo($ch, CURLINFO_HEADER_SIZE);
96      $status = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
97      @curl_close($ch);
98      if ($content !== false and $status >= 200 and $status < 400)
99      {
100        if (preg_match('/Location:\s+?(.+)/', substr($content, 0, $header_length), $m))
101        {
102          return fetchRemote($m[1], $dest, $user_agent, $step+1);
103        }
104        $content = substr($content, $header_length);
105        is_resource($dest) ? @fwrite($dest, $content) : $dest = $content;
106        return true;
107      }
108    }
109
110    // Try file_get_contents to read remote file
111    if (ini_get('allow_url_fopen'))
112    {
113      $content = @file_get_contents($src);
114      if ($content !== false)
115      {
116        is_resource($dest) ? @fwrite($dest, $content) : $dest = $content;
117        return true;
118      }
119    }
120
121    // Try fsockopen to read remote file
122    $src = parse_url($src);
123    $host = $src['host'];
124    $path = isset($src['path']) ? $src['path'] : '/';
125    $path .= isset($src['query']) ? '?'.$src['query'] : '';
126   
127    if (($s = @fsockopen($host,80,$errno,$errstr,5)) === false)
128    {
129      return false;
130    }
131
132    fwrite($s,
133      "GET ".$path." HTTP/1.0\r\n"
134      ."Host: ".$host."\r\n"
135      ."User-Agent: ".$user_agent."\r\n"
136      ."Accept: */*\r\n"
137      ."\r\n"
138    );
139
140    $i = 0;
141    $in_content = false;
142    while (!feof($s))
143    {
144      $line = fgets($s);
145
146      if (rtrim($line,"\r\n") == '' && !$in_content)
147      {
148        $in_content = true;
149        $i++;
150        continue;
151      }
152      if ($i == 0)
153      {
154        if (!preg_match('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/',rtrim($line,"\r\n"), $m))
155        {
156          fclose($s);
157          return false;
158        }
159        $status = (integer) $m[2];
160        if ($status < 200 || $status >= 400)
161        {
162          fclose($s);
163          return false;
164        }
165      }
166      if (!$in_content)
167      {
168        if (preg_match('/Location:\s+?(.+)$/',rtrim($line,"\r\n"),$m))
169        {
170          fclose($s);
171          return fetchRemote(trim($m[1]),$dest,$user_agent,$step+1);
172        }
173        $i++;
174        continue;
175      }
176      is_resource($dest) ? @fwrite($dest, $line) : $dest .= $line;
177      $i++;
178    }
179    fclose($s);
180    return true;
181  }
182}
183
184// Creation du nouveau fichier
185if (isset($_POST['submit_add']) and !is_adviser())
186{
187  if (empty($_POST['pywaie_add_name']) or empty($_POST['pywaie_add_url']) or ($_POST['parent'] == 0))
188  {
189    array_push($page['errors'], l10n('py_error2'), l10n('py_error3'));
190  }
191  else
192  {
193    $py_url = $_POST['pywaie_add_url'];
194    if (!substr_count($py_url, "http://")) $py_url = "http://" . $py_url;
195
196    // Extraction de l'id et du type
197    foreach ($video_types as $type)
198    {
199      if (substr_count($py_url, $type))
200      {
201        $video = get_video_infos($py_url, $type);
202        break;
203      }
204    }
205
206    if (!isset($video) or !$video)
207    {
208      array_unshift($page['errors'], l10n('py_error5'));
209    }
210    else
211    {
212      $cat = $_POST['parent'];
213      $video['name'] = str_replace(" ", "_", $_POST['pywaie_add_name']);
214      $catpath = get_fulldirs(array($cat));
215      $path_file = $catpath[$cat] . '/' . $video['name'] . '.' . $video['ext'];
216      $thefile = substr($path_file, 2);
217      if (file_exists($path_file))
218      {
219        array_push($page['errors'], sprintf(l10n('py_error6'), $thefile));
220      }
221      else
222      {
223        $file = @fopen($thefile , 'w');
224
225        // Ecriture du fichier et attribution des messages
226        if (@!fwrite ($file, stripslashes($video['id']) . '/' . $_POST['pywaie_add_h'] . '/' . $_POST['pywaie_add_w'] . '/' . $_POST['pywaie_add_start']))
227        {
228          array_push($page['errors'], sprintf(l10n('py_error7'), $thefile), sprintf(l10n('py_error8'), $catpath[$cat]));
229        }
230        else
231        {
232          // Miniatures
233          $thumb_extension = 'NULL';
234          if ($_POST['thumbnail'] == 'thumb_from_server' or $_POST['thumbnail'] == 'thumb_from_user')
235          {
236                                          include_once ('thumbnails.php');
237          }
238         
239          // Synchronisation avec la base de donnees
240          $infos['name'] = (!empty($_POST['name']) ? '"' . $_POST['name'] . '"' : 'NULL');
241          $infos['description'] = (!empty($_POST['description']) ? '"' . $_POST['description'] . '"' : 'NULL');
242          $infos['author'] = (!empty($_POST['author']) ? '"' . $_POST['author'] . '"' : 'NULL');
243
244          $query = 'SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id  FROM ' . IMAGES_TABLE . ' ;';
245          list($next_element_id) = mysql_fetch_array(pwg_query($query));
246
247          pwg_query('INSERT INTO ' . IMAGES_TABLE . ' ( id , file , date_available , date_creation , tn_ext , name , comment , author , hit , filesize , width , height , representative_ext , date_metadata_update , average_rate , has_high , path , storage_category_id , high_filesize )
248                                          VALUES ( ' . $next_element_id . ', "' . $video['name'] . '.' . $video['ext'] . '", NOW() , NULL , ' . $thumb_extension . ' ,  ' . $infos['name'] . ' , ' . $infos['description'] . ' , ' . $infos['author'] . ' , 0 , NULL , NULL , NULL , NULL , NULL , NULL , NULL , "' . $path_file . '", ' . $cat . ', NULL);');
249          pwg_query('INSERT INTO ' . IMAGE_CATEGORY_TABLE . ' ( image_id , category_id )
250                                          VALUES ( ' . $next_element_id . ', ' . $cat . ');');
251
252          $query = 'SELECT representative_picture_id FROM ' . CATEGORIES_TABLE . ' WHERE id =' .  $cat . ';';
253          list($result) = mysql_fetch_array(pwg_query($query));
254          if ($result === NULL)
255          {
256            pwg_query('UPDATE ' . CATEGORIES_TABLE . ' SET representative_picture_id=' . $next_element_id . ' WHERE id = ' . $cat . ' LIMIT 1');
257          }
258
259          invalidate_user_cache();
260          array_unshift($page['infos'], sprintf(l10n('py_info3'), $thefile));
261          array_push($page['infos'], sprintf(l10n('py_show_file'), PHPWG_ROOT_PATH . 'picture.php?/' . $next_element_id . '/category/' . $cat));
262          @fclose($file);
263        }
264      }
265    }
266  }
267}
268
269
270// Affichage de la liste des categories
271$site_locaux = array();
272$query = '
273SELECT id , galleries_url
274FROM ' . SITES_TABLE . '
275ORDER by id';
276$result = pwg_query($query);
277
278if (mysql_num_rows($result) > 0)
279{
280  while (list($id , $galleries_url) = mysql_fetch_row($result))
281  {
282    if (!url_is_remote($galleries_url)) array_push($site_locaux , $id);
283  }
284}
285if (empty($site_locaux))
286{
287  array_push($page['errors'], l10n('py_error1'));
288  $site_locaux = array(0);
289}
290
291$query = '
292SELECT id,name,uppercats,global_rank
293  FROM ' . CATEGORIES_TABLE . '
294  WHERE site_id IN (' . implode("," , $site_locaux) . ');';
295
296if (isset($_POST['parent'])) $selected = array($_POST['parent']);
297else $selected = array();
298
299display_select_cat_wrapper($query, $selected , 'category_option_parent', false);
300
301
302// Parametrage du template
303if (isset($_POST['submit_add']))
304{
305  $template->assign(array(
306    'PYWAIE_ADD_NAME' => $_POST['pywaie_add_name'],
307    'PYWAIE_ADD_URL' => $_POST['pywaie_add_url'],
308                $_POST['thumbnail'] . '_CHECKED' => 'checked="checked"',
309    'ADD_BAND' => isset($_POST['add_band']) ? 'checked="checked"' : '',
310    'THUMB_RESIZE' => isset($_POST['thumb_resize']) ? 'checked="checked"' : '',
311    'PYWAIE_ADD_START' => $_POST['pywaie_add_start'],
312    'PYWAIE_ADD_W' => $_POST['pywaie_add_w'],
313    'PYWAIE_ADD_H' => $_POST['pywaie_add_h'],
314    'NAME' => $_POST['name'],
315    'AUTHOR' => $_POST['author'],
316    'DESCRIPTION' => $_POST['description']));
317}
318else
319{
320  $template->assign(array('no_thumb_CHECKED' => 'checked="checked"'));
321}
322
323$template->assign(array(
324  'INFOBULLES_JS' => GVIDEO_PATH . 'admin/infobulles.js',
325  'ICON_INFOBULLE' => GVIDEO_PATH . 'admin/infobulle.png',
326  'DEFAULT_THUMB_W' => $conf['tn_width'],
327  'DEFAULT_THUMB_H' => $conf['tn_height']));
328
329$template->set_filenames(array('plugin_admin_content' => dirname(__FILE__) . '/add_page.tpl'));
330$template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content');
331
332?>
Note: See TracBrowser for help on using the repository browser.