<?php
class Digiteka_Video_Upload {
private static $CHUNK_SIZE = 1048576;
private $TMP_DIR = "/tmp";
private $authenticated = false;
private $UPLOAD_URL = null;
private $META_URL = null;
private $PUBLISH_URL = null;
private $PLAYLIST_URL = null;
private $TOKEN = null;
private $file_sent = false;
private $meta_sent = false;
private $published = false;
private $cookie = false;
public function setTmpDir($path) {
$path = realpath($path);
if (is_writable($path)) {
$this->TMP_DIR = $path;
return json_encode(array('succeed' => true));
} else {
return json_encode(array('succeed' => false, 'errors' => array('Not writable path')));
}
}
public function authenticate($applicationId, $password, $secretKey) {
if (!isset($applicationId) || is_null($applicationId) || empty($applicationId)) {
return json_encode(array('succeed' => false, 'errors' => array('Authentication : invalid applicationId')));
}
if (!isset($password) || is_null($password) || empty($password)) {
return json_encode(array('succeed' => false, 'errors' => array('Authentication : invalid password')));
}
if (!isset($secretKey) || is_null($secretKey) || empty($secretKey)) {
return json_encode(array('succeed' => false, 'errors' => array('Authentication : invalid secretKey')));
}
// Paramètres
$uri = '/api/video/geturlupload';
// Préparation des paramètres d'appel
// gmdate() : Retourne une date sous forme d'une chaîne, selon un format donné. Le temps retourné est GMT.
// time() : Retourne l'heure courante mesurée en secondes, depuis le début de l'époque UNIX
$date = gmdate("D, d M Y H:i:s", time()).' GMT';
$string2sign = 'GET ' . $uri . ' ' . $secretKey . ' ' . $date;
// base64_encode() : Encore la chaîne donnée en paramètre en base64.
// hash_hmac() : Génère une valeur de clé de hachage en utilisant la méthode HMAC.
// utf8_encode() : Encode la chaîne donnée en paramètre en utf8.
$signature = base64_encode( hash_hmac( 'sha1', utf8_encode( $string2sign ), $password ) );
$Authorization = $applicationId . ':' . $signature;
$curlUrl = 'https://www.ultimedia.com' . $uri;
// Préparation des headers
$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'Ulti-Key: ' . $Authorization;
$headers[] = 'Content-type: multipart/form-data';
$headers[] = 'Date: ' . $date;
// Appel cURL
// curl_init() : Permet de récupérer le contenu d'une page Web
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $curlUrl);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$response = curl_exec ($ch);
$info = curl_getinfo($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
curl_close ($ch);
$json = json_decode($body);
if (!$json->bool) {
$this->authenticated = false;
return json_encode(array('succeed' => false, 'errors' => array($json->errors)));
}
$this->authenticated = true;
$this->UPLOAD_URL = $json->url_upload;
$this->META_URL = $json->url_meta;
$this->PUBLISH_URL = $json->url_publish;
$this->PLAYLIST_URL = $json->url_playlist;
$this->TOKEN = $json->token;
return json_encode(array('succeed' => true, 'token' => $this->TOKEN));
}
public function sendFile($infile) {
if ($this->file_sent) {
return json_encode(array('succeed' => false, 'errors' => array('File already uploaded')));
}
// Authentification
if (!$this->authenticated || is_null($this->UPLOAD_URL) || empty($this->UPLOAD_URL) || is_null($this->TOKEN) || empty($this->TOKEN)) {
return json_encode(array('succeed' => false, 'errors' => array('Not authenticated')));
}
// Vérification du fichier vidéo
if (!isset($infile) || is_null($infile) || empty($infile) || !file_exists($infile) || (filesize($infile) <= 0)) {
return json_encode(array('succeed' => false, 'errors' => array('Invalid video file')));
}
// Paramètres d'upload
$fh = fopen($infile, 'r');
$filesize = filesize($infile);
$pathinfo = pathinfo($infile);
$upload_name = $this->TOKEN . '.' . $pathinfo['extension'];
$cookie_file = $this->TMP_DIR . "/" . $this->TOKEN . ".cookie";
$progress_file = $this->TMP_DIR . "/" . $this->TOKEN . ".progress";
$tmp_file = $this->TMP_DIR . "/" . $this->TOKEN . ".tmp";
$chunks = ceil($filesize / self::$CHUNK_SIZE);
// Let's go
$current = 0;
$chunk_index = 0;
while ($current < $filesize) {
$file_chunk = fread($fh, self::$CHUNK_SIZE);
file_put_contents($tmp_file, $file_chunk);
$ch = curl_init($this->UPLOAD_URL);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data"));
if (function_exists('curl_file_create')) {
$file_uploaded = curl_file_create(realpath($tmp_file));
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, FALSE);
} else {
$file_uploaded = "@" . $tmp_file;
}
$post_params = array(
'chunk' => $chunk_index,
'chunks' => $chunks,
'name' => $upload_name,
'file' => $file_uploaded
);
curl_setopt($ch, CURLOPT_POST, count($post_params));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
$response = curl_exec($ch);
curl_close($ch);
$current += self::$CHUNK_SIZE;
$chunk_index ++;
file_put_contents($progress_file, floor(100 * $chunk_index / $chunks));
}
$response = json_decode($response);
if (!$response->bool) {
return json_encode(array('succeed' => false, 'errors' => get_object_vars($response->errors)));
}
$this->file_sent = true;
$this->cookie = file_get_contents($cookie_file);
unlink($tmp_file);
unlink($cookie_file);
unlink($progress_file);
return json_encode(array('succeed' => true));
}
public function getProgress() {
$progress_file = $this->TMP_DIR . "/" . $this->TOKEN . ".progress";
if (!$this->authenticated || is_null($this->TOKEN) || empty($this->TOKEN)) {
return 0;
} else if ($this->file_sent) {
return 100;
} else if (!file_exists($progress_file)) {
return 0;
} else {
return (int)file_get_contents($progress_file);
}
}
public function sendMetas($metas) {
if ($this->meta_sent) {
return json_encode(array('succeed' => false, 'errors' => array('Meta already sent')));
}
// Authentification
if (!$this->authenticated || is_null($this->META_URL) || empty($this->META_URL) || is_null($this->TOKEN) || empty($this->TOKEN)) {
return json_encode(array('succeed' => false, 'errors' => array('Not authenticated')));
}
/*** pour l'update video ***/
if(isset($metas['video_id']) && !is_null($metas['video_id']) && !empty($metas['video_id'])){
$post_params = array(
'video_id'=> (isset($metas['video_id']) && !is_null($metas['video_id']) && !empty($metas['video_id'])) ? $metas['video_id'] : '',
'category' => (isset($metas['category']) && !is_null($metas['category']) && !empty($metas['category'])) ? $metas['category'] : '',
'sub_category' => (isset($metas['sub_category']) && !is_null($metas['sub_category']) && !empty($metas['sub_category'])) ? implode(',', $metas['sub_category']) : '',
'title' => (isset($metas['title']) && !is_null($metas['title']) && !empty($metas['title'])) ? $metas['title'] : '',
'description' => (isset($metas['description']) && !is_null($metas['description']) && !empty($metas['description'])) ? $metas['description'] : '',
'catalog' => (isset($metas['catalog']) && !is_null($metas['catalog']) && !empty($metas['catalog'])) ? $metas['catalog'] : '',
'picture' => (isset($metas['picture']) && !is_null($metas['picture']) && !empty($metas['picture']) && file_exists($metas['picture'])) ? file_get_contents($metas['picture']) : '',
'tags' => (isset($metas['tags']) && !is_null($metas['tags']) && !empty($metas['tags']) && is_array($metas['tags'])) ? implode(',', $metas['tags']) : '',
'authors' => (isset($metas['authors']) && !is_null($metas['authors']) && !empty($metas['authors']) && is_array($metas['authors'])) ? implode(',', $metas['authors']) : '',
'language' => (isset($metas['language']) && !is_null($metas['language']) && !empty($metas['language'])) ? $metas['language'] : '',
'ad' => (isset($metas['ad']) && !is_null($metas['ad']) && !empty($metas['ad'])) ? $metas['ad'] : '',
'status' => (isset($metas['status']) && !is_null($metas['status']) && !empty($metas['status'])) ? $metas['status'] : '',
'edition_date'=> time(),
'countries_allowed' => (isset($metas['countries_allowed']) && !is_null($metas['countries_allowed']) && !empty($metas['countries_allowed']) && is_array($metas['countries_allowed'])) ? implode(',', $metas['countries_allowed']) : '',
'begin_time' => (isset($metas['begin_time']) && !is_null($metas['begin_time']) && !empty($metas['begin_time'])) ? (int)$metas['begin_time'] : 0,
'end_time' => (isset($metas['end_time']) && !is_null($metas['end_time']) && !empty($metas['end_time'])) ? (int)$metas['end_time'] : 0,
'release_time' => (isset($metas['release_time']) && !is_null($metas['release_time']) && !empty($metas['release_time'])) ? (int)$metas['release_time'] : 0,
'extern_hls' => (isset($metas['hls']) && !is_null($metas['hls']) && !empty($metas['hls'])) ? $metas['hls'] : ''
);
}else{
// Paramètres
$post_params = array(
'category' => (isset($metas['category']) && !is_null($metas['category']) && !empty($metas['category'])) ? $metas['category'] : '',
'title' => (isset($metas['title']) && !is_null($metas['title']) && !empty($metas['title'])) ? $metas['title'] : '',
'description' => (isset($metas['description']) && !is_null($metas['description']) && !empty($metas['description'])) ? $metas['description'] : '',
'catalog' => (isset($metas['catalog']) && !is_null($metas['catalog']) && !empty($metas['catalog'])) ? $metas['catalog'] : '',
'picture' => (isset($metas['picture']) && !is_null($metas['picture']) && !empty($metas['picture']) && file_exists($metas['picture'])) ? file_get_contents($metas['picture']) : '',
'publish_yt' => (isset($metas['publish_yt']) && !is_null($metas['publish_yt']) && !empty($metas['publish_yt']) && is_bool($metas['publish_yt'])) ? (int)$metas['publish_yt'] : 0,
'publish_dm' => (isset($metas['publish_dm']) && !is_null($metas['publish_dm']) && !empty($metas['publish_dm']) && is_bool($metas['publish_dm'])) ? (int)$metas['publish_dm'] : 0,
'tags' => (isset($metas['tags']) && !is_null($metas['tags']) && !empty($metas['tags']) && is_array($metas['tags'])) ? implode(',', $metas['tags']) : '',
'language' => (isset($metas['language']) && !is_null($metas['language']) && !empty($metas['language'])) ? $metas['language'] : '',
'countries_allowed' => (isset($metas['countries_allowed']) && !is_null($metas['countries_allowed']) && !empty($metas['countries_allowed']) && is_array($metas['countries_allowed'])) ? implode(',', $metas['countries_allowed']) : '',
'begin_time' => (isset($metas['begin_time']) && !is_null($metas['begin_time']) && !empty($metas['begin_time'])) ? (int)$metas['begin_time'] : 0,
'end_time' => (isset($metas['end_time']) && !is_null($metas['end_time']) && !empty($metas['end_time'])) ? (int)$metas['end_time'] : 0,
'release_time' => (isset($metas['release_time']) && !is_null($metas['release_time']) && !empty($metas['release_time'])) ? (int)$metas['release_time'] : 0,
'sub_category' => (isset($metas['sub_category']) && !is_null($metas['sub_category']) && !empty($metas['sub_category'])) ? implode(',', $metas['sub_category']) : '',
'authors' => (isset($metas['authors']) && !is_null($metas['authors']) && !empty($metas['authors']) && is_array($metas['authors'])) ? implode(',', $metas['authors']) : '',
'status' => (isset($metas['status']) && !is_null($metas['status']) && !empty($metas['status'])) ? $metas['status'] : '',
'extern_hls' => (isset($metas['hls']) && !is_null($metas['hls']) && !empty($metas['hls'])) ? $metas['hls'] : ''
);
}
$ch = curl_init($this->META_URL);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data"));
curl_setopt($ch, CURLOPT_POST, count($post_params));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
if (!$response->bool) {
return json_encode(array('succeed' => false, 'errors' => get_object_vars($response->errors)));
}
if (!empty($post_params['extern_hls'])) {
$this->file_sent = true;
}
$this->meta_sent = true;
return json_encode(array('succeed' => true));
}
public function managePlaylist($params)
{
$post_params = array(
'playlist_action' => isset($params['action']) ? $params['action'] : '',
'playlist_id' => isset($params['playlist_id']) ? $params['playlist_id'] : '',
'video_id' => isset($params['video_id']) ? $params['video_id'] : '',
);
$ch = curl_init($this->PLAYLIST_URL);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data"));
curl_setopt($ch, CURLOPT_POST, count($post_params));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
return json_encode($response);
}
public function publish($mdtk = '', $zone = '') {
if ($this->published) {
return json_encode(array('succeed' => false, 'errors' => array('Video already published')));
}
// Authentification
if (!$this->authenticated || is_null($this->PUBLISH_URL) || empty($this->PUBLISH_URL) || is_null($this->TOKEN) || empty($this->TOKEN)) {
return json_encode(array('succeed' => false, 'errors' => array('Not authenticated')));
}
if (!$this->file_sent) {
return json_encode(array('succeed' => false, 'errors' => array('File not uploaded yet')));
}
if (!$this->meta_sent) {
return json_encode(array('succeed' => false, 'errors' => array('Meta not sent yet')));
}
$cookie_file = $this->TMP_DIR . "/" . $this->TOKEN . ".cookie";
file_put_contents($cookie_file, $this->cookie);
// Paramètres
$post_params = array(
'mdtk' => $mdtk,
'zone' => $zone
);
$ch = curl_init($this->PUBLISH_URL);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data"));
curl_setopt($ch, CURLOPT_POST, count($post_params));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
$response = curl_exec($ch);
curl_close($ch);
unlink($cookie_file);
$response = json_decode($response);
if (!$response->bool) {
return json_encode(array('succeed' => false, 'errors' => get_object_vars($response->errors)));
}
$this->published = true;
$json = array('succeed' => true, 'id' => $response->id);
if (isset($response->iframe_url) && !empty($response->iframe_url)) {
$json['iframe_url'] = $response->iframe_url;
}
if (isset($response->image_original) && !empty($response->image_original)) {
$json['image_original'] = $response->image_original;
}
if (isset($response->embed) && !empty($response->embed)) {
$json['embed'] = $response->embed;
}
return json_encode($json);
}
public function getcatalogtopic($applicationId, $password, $secretKey)
{
$uri = '/api/video/getcatalogtopic';
$curlUrl = 'https://WWW.ultimedia.com' . $uri;
$date = gmdate("D, d M Y H:i:s", time()) . ' GMT'; // date de la demande
$string2sign = 'GET ' . $uri . ' ' . $secretKey . ' ' . $date;
$signature = base64_encode(hash_hmac('sha1', utf8_encode($string2sign), $password));
$Authorization = $applicationId . ':' . $signature;
$data = array();
$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'Ulti-Key: ' . $Authorization;
$headers[] = 'Content-type: multipart/form-data';
$headers[] = 'Date: ' . $date;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $curlUrl);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
curl_getinfo($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($response, $header_size);
curl_close($ch);
return $body;
}
}