+ * Adds a new uploaded video to the DB.
+ *
+ * @param string $name
+ * @param string $title
+ * @param string $description
+ * @param string $tags comma separated tags
+ * @param string $duration video duration formatted [HH:]mm:ss
+ * @param array $formats a dictionary corresponding to `formats` JSON
+ * column from `videos` table
+ * @param int $category_id
+ * @param int $user_id
+ * @param string $uploaded_file the raw video file uploaded by the user
+ * @return mixed returns an activation code on success or FALSE otherwise
+ */
+ public function add_video($name, $title, $description, $tags, $duration,
+ $formats, $category_id, $user_id, $uploaded_file)
+ {
+ $this->load->config('content_ingestion');
+
+ // Tags.
+ $json_tags = array();
+ $tok = strtok($tags, ',');
+ while ($tok != FALSE)
+ {
+ $json_tags[trim($tok)] = 0;
+
+ $tok = strtok(',');
+ }
+ $json_tags = json_encode($json_tags);
+
+ $json_formats = json_encode($formats);
+
+ // Thumbnail images
+ $thumbs_count = $this->config->item('thumbs_count');
+ $default_thumb = rand(0, $thumbs_count - 1);
+
+ $query = $this->db->query("INSERT INTO `videos`
+ (name, title, description, duration, formats, category_id,
+ user_id, tags, date, thumbs_count, default_thumb)
+ VALUES ('$name', '$title', '$description', '$duration',
+ '$json_formats', $category_id,
+ $user_id, '$json_tags', utc_timestamp(),
+ $thumbs_count, $default_thumb)");
+ if ($query === FALSE)
+ return FALSE;
+
+ // Find out the id of the new video added.
+ $query = $this->db->query("SELECT id from `videos`
+ WHERE name = '$name'");
+ if ($query->num_rows() === 0)
+ return FALSE;
+ $video_id = $query->row()->id;
+
+ // Activation code.
+ $activation_code = Videos_model::gen_activation_code();
+
+ $query = $this->db->query("INSERT INTO `videos_unactivated`
+ (video_id, activation_code, uploaded_file)
+ VALUES ($video_id, '$activation_code', '$uploaded_file')");
+
+ return $activation_code;
+ }
+
+ /**
+ * Request content_ingest to the CIS in order to start the content
+ * ingestion process.
+ *
+ * @param string $activation_code
+ * @param string $raw_video_fn uploaded video file name
+ * @param string $name
+ * @param int $raw_video_size uploaded video file size in bytes
+ * @param array $transcode_configs dictionary which must be included in
+ * the JSON data that needs to be sent to CIS
+ * @return mixed return the HTTP content (body) on success and FALSE
+ * otherwise
+ */
+ public function send_content_ingestion($activation_code, $raw_video_fn,
+ $name, $raw_video_size, $transcode_configs)
+ {
+ $this->config->load('content_ingestion');
+
+ $url = $this->config->item('cis_url') . 'ingest_content';
+ $data = array(
+ 'code'=>$activation_code,
+ 'raw_video'=>$raw_video_fn,
+ 'name'=>$name,
+ 'weight'=>$raw_video_size,
+ 'transcode_configs'=>$transcode_configs,
+ 'thumbs'=>$this->config->item('thumbs_count')
+ );
+ $json_data = json_encode($data);
+
+ // Send request to CIS.
+ $r = new HttpRequest($url, HttpRequest::METH_POST);
+ $r->setBody($json_data);
+ try
+ {
+ $response = $r->send()->getBody();
+ }
+ catch (HttpException $ex)
+ {
+ return FALSE;
+ }
+
+ return $response;
+ }
+
+ public function set_cis_response($activation_code,
+ $response = CIS_RESP_COMPLETION)
+ {
+ return $this->db->query("UPDATE `videos_unactivated`
+ SET cis_response = $response
+ WHERE activation_code = '$activation_code'");
+ }
+
+ public function send_upload_error_email($activation_code,
+ $cis_response = CIS_RESP_INTERNAL_ERROR)
+ {
+ $query = $this->db->query("SELECT v.title, u.email
+ FROM `videos_unactivated` a, `videos` v, `users` u
+ WHERE a.activation_code = '$activation_code'
+ AND a.video_id = v.id AND v.user_id = u.id");
+
+ if ($query->num_rows() > 0)
+ {
+ $title = $query->row()->title;
+ $email = $query->row()->email;
+ }
+ else
+ return FALSE;
+
+ $subject = '['. $this->config->item('site_name')
+ . '] Upload Error';
+ if ($cis_response == CIS_RESP_INTERNAL_ERROR)
+ {
+ $msg = sprintf($this->lang->line(
+ 'video_internal_cis_error_email_content'), $title);
+ }
+ else if ($cis_response == CIS_RESP_UNREACHABLE)
+ {
+ $msg = sprintf($this->lang->line(
+ 'video_unreachable_cis_error_email_content'), $title);
+ }
+ $headers = "From: ". $this->config->item('noreply_email');
+
+ return mail($email, $subject, $msg, $headers);
+ }
+
+ /**
+ * Activates a video by deleting its entry from `videos_unactivated`.
+ *
+ * @param mixed $code_or_id use type string for activation_code or type
+ * int for video_id
+ * @return boolean TRUE on success, FALSE otherwise
+ */
+ public function activate_video($code_or_id)
+ {
+ if (is_string($code_or_id))
+ $query = $this->db->query("SELECT uploaded_file from `videos_unactivated`
+ WHERE activation_code = '$code_or_id'");
+ else if (is_int($code_or_id))
+ $query = $this->db->query("SELECT uploaded_file from `videos_unactivated`
+ WHERE video_id = '$code_or_id'");
+ else
+ return FALSE;
+
+ if ($query->num_rows() > 0)
+ $uploaded_file = $query->row()->uploaded_file;
+ else
+ return FALSE;
+
+ if (is_string($code_or_id))
+ $query = $this->db->query("DELETE FROM `videos_unactivated`
+ WHERE activation_code = '$code_or_id'");
+ else if (is_int($code_or_id))
+ $query = $this->db->query("DELETE FROM `videos_unactivated`
+ WHERE video_id = '$code_or_id'");
+ else
+ return FALSE;
+
+ if (!$query)
+ return $query;
+
+ return unlink("data/upload/$uploaded_file");
+ }
+
+ public function get_unactivated_videos()
+ {
+ $query = $this->db->query("SELECT a.video_id, v.name, a.cis_response
+ FROM `videos_unactivated` a, `videos` v
+ WHERE a.video_id = v.id AND a.cis_response = "
+ . CIS_RESP_COMPLETION);
+
+ if ($query->num_rows() > 0)
+ return $query->result_array();
+ else
+ return FALSE;
+ }
+
+ /**
+ * Retrieves comments for a video.
+ *
+ * @param int $video_id
+ * @param int $offset
+ * @param int $count
+ * @param string $ordering control comments ording by these possibilities:
+ * <ul>
+ * <li><strong>'hottest':</strong> newest most appreciated first. An
+ * appreciated comment is one which has a bigger
+ * score = likes - dislikes.</li>
+ * <li><strong>'newest':</strong> newest first.</li>
+ * </ul>
+ * @return array an array with comments
+ */
+ public function get_video_comments($video_id, $offset, $count,
+ $ordering = 'newest')
+ {
+ $this->load->helper('date');
+ $cond_hottest = '';
+
+ // Ordering
+ switch ($ordering)
+ {
+ case 'newest':
+ $order_statement = "ORDER BY time DESC";
+ break;
+ case 'hottest':
+ $order_statement = "ORDER BY score DESC, time DESC";
+ $cond_hottest = "AND c.likes + c.dislikes > 0";
+ break;
+
+ default:
+ $order_statement = "";
+ }
+
+ $query = $this->db->query(
+ "SELECT c.*, u.username, u.time_zone, (c.likes + c.dislikes) AS score
+ FROM `videos_comments` c, `users` u
+ WHERE c.user_id = u.id AND video_id = $video_id $cond_hottest
+ $order_statement
+ LIMIT $offset, $count");
+
+ if ($query->num_rows() == 0)
+ return array();
+
+ $comments = $query->result_array();
+
+ foreach ($comments as & $comment)
+ {
+ $comment['local_time'] = human_gmt_to_human_local($comment['time'],
+ $comment['time_zone']);
+ }
+
+ return $comments;
+ }
+
+ public function get_video_comments_count($video_id)
+ {
+ $query = $this->db->query(
+ "SELECT COUNT(*) count
+ FROM `videos_comments`
+ WHERE video_id = $video_id");
+
+ if ($query->num_rows() == 0)
+ return FALSE;
+
+ return $query->row()->count;
+ }
+
+ /**
+ * Insert in DB a comment for a video.
+ *
+ * @param int $video_id
+ * @param int $user_id
+ * @param string $content
+ */
+ public function comment_video($video_id, $user_id, $content)
+ {
+ // Prepping content.
+ $content = substr($content, 0, 512);
+ $content = htmlspecialchars($content);
+ $content = nl2br($content);
+
+ return $query = $this->db->query(
+ "INSERT INTO `videos_comments` (video_id, user_id, content, time)
+ VALUES ($video_id, $user_id, '$content', UTC_TIMESTAMP())");
+ }
+
+ /**
+ * Increments views count for a video.