5182b62a5167ff274ed966355f254b65e6faa77e
[living-lab-site.git] / application / controllers / video.php
1 <?php
2
3 /**
4  * Class Video controls video items handling: watching, commenting, rating,
5  * adding etc.
6  *
7  * @category    Controller
8  * @author              Călin-Andrei Burloiu
9  */
10 class Video extends CI_Controller {
11
12         protected $uploaded_file;
13         protected $av_info;
14         
15         public function __construct()
16         {
17                 parent::__construct();
18                 
19                 $this->lang->load('video');
20         }
21         
22         public function index()
23         {
24                 //phpinfo();
25         }
26         
27         public function test()
28         {
29                 $this->load->helper('video');
30                 
31                 var_dump(get_av_info('data/upload/test.ogv'));
32         }
33         
34         /**
35          * The page used for watching a video
36          *
37          * @param       string $id      DB id of the video
38          * @param       string $name    `name` of the video from DB
39          * @param       string $plugin  video plugin ('ns-vlc', 'ns-html5'). If it's set 
40          * to NULL or 'auto', the plugin is automatically selected.
41          */
42         public function watch($id, $name = NULL, $plugin = NULL)
43         {
44                 // **
45                 // ** LOADING MODEL
46                 // **
47                 $this->load->model('videos_model');
48                 
49                 $data['user_id'] = $this->session->userdata('user_id');
50                 if ($data['user_id'] === FALSE)
51                         $data['user_id'] = '';
52                 else
53                         $data['user_id'] = intval($data['user_id']);
54                 $user_roles = intval($this->session->userdata('roles'));
55 //              echo USER_ROLE_ADMIN . ' / ';
56 //              var_dump($user_roles);
57 //              var_dump($user_roles | USER_ROLE_ADMIN);
58 //              die();
59                 
60                 // Retrieve video information.
61                 $data['video'] = $this->videos_model->get_video($id, $name);
62                 if ($data['video'] === FALSE)
63                 {       
64                         $this->load->helper('message');
65                         show_error_msg_page($this, 
66                                 $this->lang->line('video_msg_no_video'));
67                         return;
68                 }
69                 
70                 // Video is being processed by CIS.
71                 if ($data['video']['activation_code']
72                                 && !$data['video']['content_ingested'])
73                 {
74                         $this->load->helper('message');
75                         show_error_msg_page($this, 
76                                 $this->lang->line('video_msg_video_not_ready'));
77                         return;
78                 }
79                 
80                 // Unlogged in user can't see unactivated videos.
81                 if (empty($data['user_id']))
82                         $allow_unactivated = FALSE;
83                 else
84                 {
85                         if (($user_roles & USER_ROLE_ADMIN) == 0
86                                         && $data['user_id'] != $data['video']['user_id'])
87                                 $allow_unactivated = FALSE;
88                         else
89                                 $allow_unactivated = TRUE;
90                 }
91                 
92                 // Video is not activated; can be seen by owner and admin.
93                 if ($data['video']['activation_code'] && !$allow_unactivated)
94                 {
95                         $this->load->helper('message');
96                         show_error_msg_page($this, 
97                                 $this->lang->line('video_msg_video_unactivated'));
98                         return;
99                 }                       
100                 
101                 $categories = $this->config->item('categories');
102                 $data['video']['category_name'] = 
103                         $categories[ $data['video']['category_id'] ];
104                 $data['plugin_type'] = ($plugin === NULL ? 'auto' : $plugin);
105                 
106                 // Increment the number of views for the video.
107                 $this->videos_model->inc_views($id);
108                 
109                 // Display page.
110                 $params = array(        'title' => $data['video']['title'] . ' &ndash; '
111                                                                 . $this->config->item('site_name'),
112                                                         'css' => array(
113                                                                 'jquery.ui.nsvideo.css',
114                                                                 'video.css'
115                                                         ),
116                                                         'js' => array(
117                                                                 'jquery.ui.nsvideo.js',
118                                                                 'jquery.ui.ajax_links_maker.js'
119                                                         ),
120                                                         //'metas' => array('description'=>'','keywords'=>'')
121                                                         );
122                 $this->load->library('html_head_params', $params);
123                 
124                 // Preloading video plugin.
125                 // TODO plugin auto: type and format
126                 if ($data['plugin_type'] == 'auto')
127                         $data['plugin_type'] = 'ns-html5';
128                 $data['asset_index'] = 0;
129                 
130                 // TODO remove old AJAX plugin content
131 //              $data['plugin_content'] = $this->_plugin('ns-html5', 
132 //                      $data['video']['url'][0], TRUE);
133
134                 // Comments
135                 $data['comments'] = $this->_ajax_comment(TRUE, $id);
136                 
137                 // **
138                 // ** LOADING VIEWS
139                 // **
140                 $this->load->view('html_begin', $this->html_head_params);
141                 $this->load->view('header');
142                 
143                 //$main_params['content'] = $this->load->view('video/watch_view', $data, TRUE);
144                 $this->load->view('video/watch_view', $data);
145                 
146                 $this->load->view('footer');
147                 $this->load->view('html_end');
148         }
149                 
150         public function upload()
151         {
152                 $user_id = $this->session->userdata('user_id');
153                 
154                 // Action not possible if an user is not logged in.
155                 if (!$user_id)
156                 {
157                         $this->load->helper('message');
158                         show_error_msg_page($this, 
159                                 $this->lang->line('ui_msg_login_restriction'));
160                         return;
161                 }
162                 
163                 $this->load->library('form_validation');
164
165                 $this->form_validation->set_error_delimiters('<span class="error">',
166                                 '</span>');
167                 
168                 if ($this->form_validation->run('upload') === FALSE)
169                 {
170                         $params = array('title' =>
171                                                                 $this->lang->line('ui_nav_menu_upload')
172                                                                         .' &ndash; '
173                                                                         . $this->config->item('site_name'),
174                                                         //'metas' => array('description'=>'')
175                         );
176                         $this->load->library('html_head_params', $params);
177
178                         // **
179                         // ** LOADING VIEWS
180                         // **
181                         $this->load->view('html_begin', $this->html_head_params);
182                         $this->load->view('header',
183                                         array('selected_menu' => 'upload'));
184
185                         $main_params['content'] = $this->load->view(
186                                         'video/upload_view', array(), TRUE);
187                         $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
188                         $this->load->view('main', $main_params);
189
190                         $this->load->view('footer');
191                         $this->load->view('html_end');
192                 }
193                 else
194                 {
195                         $this->load->model('videos_model');
196                         $this->load->helper('video');
197                         $this->config->load('content_ingestion');
198                         
199                         $name = urlencode(str_replace(' ', '-',
200                                         $this->input->post('video-title')));
201                         $category_id = $this->input->post('video-category');
202                         
203                         // Prepare formats
204                         $formats = $this->config->item('formats');
205                         $prepared_formats = prepare_formats($formats, $this->av_info,
206                                         $this->config->item('elim_dupl_res'));
207                         
208                         // Add video to DB.
209                         $activation_code = $this->videos_model->add_video($name,
210                                         $this->input->post('video-title'),
211                                         $this->input->post('video-description'),
212                                         $this->input->post('video-tags'),
213                                         $this->av_info['duration'],
214                                         $prepared_formats['db_formats'], $category_id, $user_id,
215                                         $this->uploaded_file);
216                         
217                         // Send a content ingestion request to
218                         // CIS (Content Ingestion Server).
219                         $this->_send_content_ingestion($activation_code,
220                                         $this->uploaded_file,
221                                         $name, $this->av_info['size'],
222                                         $prepared_formats['transcode_configs']);
223                         
224                         $this->load->helper('message');
225                         show_info_msg_page($this, 
226                                 $this->lang->line('video_msg_video_uploaded'));
227                 }
228         }
229         
230         public function cis_completion($activation_code)
231         {
232                 $this->load->model('videos_model');
233                 
234                 if ($this->config->item('require_moderation'))
235                         $this->videos_model->set_content_ingested($activation_code);
236                 else
237                         $this->videos_model->activate_video($activation_code);
238                 
239 //              log_message('info', "cis_completion $activation_code");
240         }
241         
242         /**
243         * Increments (dis)likes count for video with the specified id and returns to
244         * the client as plain text the number if likes.
245         *
246         * @param string $action 'like' or 'dislike'
247         * @param string $video_id
248         * @param string $user_id
249         */
250         public function ajax_vote($action, $video_id)
251         {
252                 $video_id = intval($video_id);
253                 $user_id = $this->session->userdata('user_id');
254                 $this->load->model('videos_model');
255         
256                 $res = $this->videos_model->vote($video_id, $user_id,
257                         (strcmp($action, 'like') == 0 ? TRUE : FALSE));
258         
259                 if ($res !== -1)
260                         echo $res;
261         }
262         /**
263          * Increments (dis)likes count for a comment with a specified id and returns
264          * to the client as plain text the number if likes. 
265          * 
266          * @param string $action        'like' or 'dislike'
267          * @param string $comment_id
268          */
269         public function ajax_vote_comment($action, $comment_id)
270         {
271                 $comment_id = intval($comment_id);
272                 $user_id = $this->session->userdata('user_id');
273                 $this->load->model('videos_model');
274                 
275                 $res = $this->videos_model->vote_comment($comment_id, $user_id,
276                         (strcmp($action, 'like') == 0 ? TRUE : FALSE));
277                 
278                 if ($res !== -1)
279                         echo $res;
280         }
281         
282         public function ajax_comment($video_id,
283                         $ordering = 'newest', $offset = '0')
284         {
285                 $this->_ajax_comment(FALSE, $video_id, $ordering, $offset);
286         }
287         
288         public function _ajax_comment($return_output, $video_id,
289                         $ordering = 'newest', $offset = '0')
290         {
291                 $video_id = intval($video_id);
292                 
293                 $this->load->library('form_validation');
294                 $this->form_validation->set_error_delimiters('<span class="error">',
295                                         '</span>');
296                 
297                 if ($this->form_validation->run('comment_video'))
298                 {
299                         $this->load->model('videos_model');
300                         $user_id = intval($this->session->userdata('user_id'));
301                         $comment = $this->input->post('comment');
302                         
303                         $this->videos_model->comment_video($video_id, $user_id, $comment);
304                 }
305                 
306                 // **
307                 // ** MODEL **
308                 // **           
309                 $this->load->model('videos_model');
310                 $data['comments'] = $this->videos_model->get_video_comments($video_id,
311                         $offset, $this->config->item('video_comments_per_page'), $ordering);
312                 $data['comments_count'] =
313                         $this->videos_model->get_video_comments_count($video_id);
314                 $data['hottest_comments'] = $this->videos_model->get_video_comments(
315                         $video_id, 0, 2, 'hottest');
316                 $data['video_id'] = $video_id;
317                 $data['user_id'] = $this->session->userdata('user_id');
318                 
319                 // Pagination
320                 $this->load->library('pagination');
321                 $pg_config['base_url'] = site_url("video/ajax_comment/$video_id/$ordering/");
322                 $pg_config['uri_segment'] = 5;
323                 $pg_config['total_rows'] = $data['comments_count'];
324                 $pg_config['per_page'] = $this->config->item('video_comments_per_page');
325                 $this->pagination->initialize($pg_config);
326                 $data['comments_pagination'] = $this->pagination->create_links();
327                 
328                 // **
329                 // ** VIEWS **
330                 // **
331                 $output = $this->load->view('video/comments_view',
332                         $data, $return_output);
333                 
334                 if ($return_output)
335                         return $output;
336         }
337         
338         /**
339          * Request content_ingest to the CIS in order to start the content
340          * ingestion process.
341          * 
342          * @param string $activation_code
343          * @param string $raw_video_fn uploaded video file name
344          * @param string $name
345          * @param int $raw_video_size uploaded video file size in bytes
346          * @param array $transcode_configs dictionary which must be included in
347          * the JSON data that needs to be sent to CIS
348          * @return mixed return the HTTP content (body) on success and FALSE
349          * otherwise
350          */
351         protected function _send_content_ingestion($activation_code, $raw_video_fn,
352                         $name, $raw_video_size, $transcode_configs)
353         {
354                 $this->config->load('content_ingestion');
355                 
356                 $url = $this->config->item('cis_url') . 'ingest_content';
357                 $data = array(
358                         'code'=>$activation_code,
359                         'raw_video'=>$raw_video_fn,
360                         'name'=>$name,
361                         'weight'=>$raw_video_size,
362                         'transcode_configs'=>$transcode_configs,
363                         'thumbs'=>$this->config->item('thumbs_count')
364                 );
365                 $json_data = json_encode($data);
366                 
367                 // Send request to CIS.
368                 $r = new HttpRequest($url, HttpRequest::METH_POST);
369                 $r->setBody($json_data);
370                 try
371                 {
372                         $response = $r->send()->getBody();
373                 }
374                 catch (HttpException $ex) 
375                 {
376                         return FALSE;
377                 }
378                 
379                 return $response;
380         }
381         
382         public function _is_user_loggedin($param)
383         {
384                 if (! $this->session->userdata('user_id'))
385                         return FALSE;
386                 
387                 return TRUE;
388         }
389         
390         public function _do_comment($comment)
391         {
392                 // Note: Videos_model must be already loaded.
393                 $this->load->model('videos_model');
394                 
395                 $video_id = intval($this->input->post('video-id'));
396                 $user_id = intval($this->session->userdata('user_id'));
397                 
398                 $this->videos_model->comment_video($video_id, $user_id, $comment);
399         }
400         
401         public function _valid_tags($tags)
402         {
403                 $tok = strtok($tags, ',');
404                 while ($tok != FALSE)
405                 {
406                         $tok = trim($tok);
407                         if (!ctype_alnum($tok))
408                                 return FALSE;
409                         
410                         $tok = strtok(',');
411                 }
412                 
413                 return TRUE;
414         }
415         
416         public function _valid_upload($file)
417         {
418                 if ($_FILES['video-upload-file']['tmp_name'])
419                 {
420                         // Upload library
421                         $config_upload = array();
422                         $config_upload['upload_path'] = './data/upload';
423                         $config_upload['allowed_types'] = '*';
424                         $this->load->library('upload', $config_upload);
425
426                         if ($this->upload->do_upload('video-upload-file'))
427                         {
428                                 $upload_data = $this->upload->data();
429                                 $this->uploaded_file = $upload_data['file_name'];
430                                 
431                                 $this->load->helper('video');
432                                 $this->av_info = get_av_info($upload_data['full_path']);
433                                 if (!$this->av_info)
434                                         return FALSE;
435                                 
436                                 return TRUE;
437                         }
438                         else
439                         {
440                                 $this->form_validation->set_message('_valid_upload',
441                                                 $this->upload->display_errors('<span class="error">',
442                                                                 '</span>'));
443                                 return FALSE;
444                         }
445                 }
446                 
447                 $this->form_validation->set_message('_valid_upload',
448                                 $this->lang->line('_required_upload'));
449                 return FALSE;
450         }
451         
452         /**
453          * OBSOLETE: AJAX page which retrieves a video plugin.
454          *
455          * The view associated with this controller should be parameter type
456          * concatenated with '_plugin_view' and must be located in
457          * 'application/views/video'.
458          *
459          * @param       string $type    'ns-vlc', 'ns-html5'
460          */
461         public function plugin($type)
462         {
463                 $url = $this->input->post('url', TRUE);
464                 
465                 $this->_plugin($type, $url);
466         }
467         
468         /**
469          * OBSOLETE: Video plugin controller
470          *
471          * See plugin function for details. If the second parameter is TRUE
472          * the output is return instead of being displayed (used in preloading).
473          */
474         public function _plugin($type, $url, $return_output=FALSE)
475         {       
476                 if ($type == 'ns-html5')
477                         $data['url'] = 'tribe://' . $url;
478                 else if ($type == 'ns-vlc')
479                         $data['url'] = $url;
480                 
481                 $output = $this->load->view('video/'. $type . '_plugin_view', $data, 
482                         $return_output);
483                 
484                 if ($return_output)
485                         return $output;
486         }
487         
488 }
489
490 /* End of file video.php */
491 /* Location: ./application/controllers/video.php */