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

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

Allow to add videos from Wat.tv.

File size: 8.3 KB
Line 
1<?php
2
3if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
4
5$video_types = array('google', 'youtube', 'dailymotion', 'wideo', 'vimeo', 'wat');
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'] = @urldecode($thumb_url[1]);
20      $video['thumb_url'] = @str_replace(array('\x3d', '\x26'), array('=', '&'), $video['thumb_url']);
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://.*?)"#', $source, $matches);
66      $video['thumb_url'] = @str_replace('160.jpg', '200.jpg', $matches[1]);
67    }
68    return $video;
69
70  case "wat":
71    if (fetchRemote($url, $source))
72    {
73      @preg_match('#link rel="video_src" href="http://www.wat.tv/swf2/(.*?)"#i', $source, $id);
74      if (empty($id[1])) return false;
75      $video['id'] = $id[1];
76      $video['ext'] = 'wat';
77      if ($_POST['thumbnail'] == 'thumb_from_server')
78      {
79        @preg_match('#link rel="image_src" href="(.*?)"#', $source, $matches);
80        $video['thumb_url'] = @str_replace('120x90', '320x240', $matches[1]);
81      }
82      return $video;
83    }
84
85  default:
86    return false;
87  }
88}
89
90// Creation du nouveau fichier
91if (isset($_POST['submit_add']) and !is_adviser())
92{
93  if (empty($_POST['pywaie_add_name']) or empty($_POST['pywaie_add_url']) or ($_POST['parent'] == 0))
94  {
95    array_push($page['errors'], l10n('py_error2'), l10n('py_error3'));
96  }
97  else
98  {
99    $py_url = $_POST['pywaie_add_url'];
100    if (!substr_count($py_url, "http://")) $py_url = "http://" . $py_url;
101
102    // Extraction de l'id et du type
103    foreach ($video_types as $type)
104    {
105      if (substr_count($py_url, $type))
106      {
107        $video = get_video_infos($py_url, $type);
108        break;
109      }
110    }
111
112    if (!isset($video) or !$video)
113    {
114      array_unshift($page['errors'], l10n('py_error5'));
115    }
116    else
117    {
118      $cat = $_POST['parent'];
119      $video['name'] = str_replace(" ", "_", $_POST['pywaie_add_name']);
120      $catpath = get_fulldirs(array($cat));
121      $path_file = $catpath[$cat] . '/' . $video['name'] . '.' . $video['ext'];
122      $thefile = substr($path_file, 2);
123      if (file_exists($path_file))
124      {
125        array_push($page['errors'], sprintf(l10n('py_error6'), $thefile));
126      }
127      else
128      {
129        $file = @fopen($thefile , 'w');
130
131        // Ecriture du fichier et attribution des messages
132        if (@!fwrite ($file, stripslashes($video['id']) . '/' . $_POST['pywaie_add_h'] . '/' . $_POST['pywaie_add_w'] . '/' . $_POST['pywaie_add_start']))
133        {
134          array_push($page['errors'], sprintf(l10n('py_error7'), $thefile), sprintf(l10n('py_error8'), $catpath[$cat]));
135        }
136        else
137        {
138          // Miniatures
139          $thumb_extension = 'NULL';
140          if ($_POST['thumbnail'] == 'thumb_from_server' or $_POST['thumbnail'] == 'thumb_from_user')
141          {
142                                          include_once ('thumbnails.php');
143          }
144         
145          // Synchronisation avec la base de donnees
146          $infos['name'] = (!empty($_POST['name']) ? '"' . $_POST['name'] . '"' : 'NULL');
147          $infos['description'] = (!empty($_POST['description']) ? '"' . $_POST['description'] . '"' : 'NULL');
148          $infos['author'] = (!empty($_POST['author']) ? '"' . $_POST['author'] . '"' : 'NULL');
149
150          $query = 'SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id  FROM ' . IMAGES_TABLE . ' ;';
151          list($next_element_id) = mysql_fetch_array(pwg_query($query));
152
153          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 )
154                                          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);');
155          pwg_query('INSERT INTO ' . IMAGE_CATEGORY_TABLE . ' ( image_id , category_id )
156                                          VALUES ( ' . $next_element_id . ', ' . $cat . ');');
157
158          $query = 'SELECT representative_picture_id FROM ' . CATEGORIES_TABLE . ' WHERE id =' .  $cat . ';';
159          list($result) = mysql_fetch_array(pwg_query($query));
160          if ($result === NULL)
161          {
162            pwg_query('UPDATE ' . CATEGORIES_TABLE . ' SET representative_picture_id=' . $next_element_id . ' WHERE id = ' . $cat . ' LIMIT 1');
163          }
164
165          invalidate_user_cache();
166          array_unshift($page['infos'], sprintf(l10n('py_info3'), $thefile));
167          array_push($page['infos'], sprintf(l10n('py_show_file'), PHPWG_ROOT_PATH . 'picture.php?/' . $next_element_id . '/category/' . $cat));
168          @fclose($file);
169        }
170      }
171    }
172  }
173}
174
175
176// Affichage de la liste des categories
177$site_locaux = array();
178$query = '
179SELECT id , galleries_url
180FROM ' . SITES_TABLE . '
181ORDER by id';
182$result = pwg_query($query);
183
184if (mysql_num_rows($result) > 0)
185{
186  while (list($id , $galleries_url) = mysql_fetch_row($result))
187  {
188    if (!url_is_remote($galleries_url)) array_push($site_locaux , $id);
189  }
190}
191if (empty($site_locaux))
192{
193  array_push($page['errors'], l10n('py_error1'));
194  $site_locaux = array(0);
195}
196
197$query = '
198SELECT id,name,uppercats,global_rank
199  FROM ' . CATEGORIES_TABLE . '
200  WHERE site_id IN (' . implode("," , $site_locaux) . ');';
201
202if (isset($_POST['parent'])) $selected = array($_POST['parent']);
203else $selected = array();
204
205display_select_cat_wrapper($query, $selected , 'category_option_parent', false);
206
207
208// Parametrage du template
209if (isset($_POST['submit_add']))
210{
211  $template->assign(array(
212    'PYWAIE_ADD_NAME' => $_POST['pywaie_add_name'],
213    'PYWAIE_ADD_URL' => $_POST['pywaie_add_url'],
214                $_POST['thumbnail'] . '_CHECKED' => 'checked="checked"',
215    'ADD_BAND' => isset($_POST['add_band']) ? 'checked="checked"' : '',
216    'THUMB_RESIZE' => isset($_POST['thumb_resize']) ? 'checked="checked"' : '',
217    'PYWAIE_ADD_START' => $_POST['pywaie_add_start'],
218    'PYWAIE_ADD_W' => $_POST['pywaie_add_w'],
219    'PYWAIE_ADD_H' => $_POST['pywaie_add_h'],
220    'NAME' => $_POST['name'],
221    'AUTHOR' => $_POST['author'],
222    'DESCRIPTION' => $_POST['description']));
223}
224else
225{
226  $template->assign(array('no_thumb_CHECKED' => 'checked="checked"'));
227}
228
229$template->assign(array(
230  'INFOBULLES_JS' => GVIDEO_PATH . 'admin/infobulles.js',
231  'ICON_INFOBULLE' => GVIDEO_PATH . 'admin/infobulle.png',
232  'DEFAULT_THUMB_W' => $conf['tn_width'],
233  'DEFAULT_THUMB_H' => $conf['tn_height']));
234
235$template->set_filenames(array('plugin_admin_content' => dirname(__FILE__) . '/add_page.tpl'));
236$template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content');
237
238?>
Note: See TracBrowser for help on using the repository browser.