'creation date (desc)', 'lastupdate' => 'last update (desc)' ); private $periods = array( 'all' => 'all', 'lasthour' => 'last hour', 'lastday' => 'last day', 'lastweek' => 'last week', 'lastmonth' => 'last month', 'lastyear' => 'last year' ); public function __construct() { $this->root_url = get_option(P3QVC_PLUGIN_SUFFIX."root_url", "https://sdn.3qsdn.com/api/v2/"); add_action( 'rest_api_init', array( $this , 'p3qvc_init' ), 2 ); } /** * function init * @desc initializes the API-class */ public function p3qvc_init () { $this->apitoken = get_option(P3QVC_PLUGIN_SUFFIX.'api_token', ''); if($this->apitoken) { // prepare options for http-requests to the 3Q-API $options = array( 'http'=>array( 'method'=>"GET", 'header'=>"Content-Type: application/json\r\n" ."X-AUTH-APIKEY: ".$this->apitoken."\r\n" ) ); $this->requestContext = stream_context_create($options); // load all projects related to the api-token $this->projects = $this->getProjects(); } if(isset($_GET['projectId']) && is_numeric($_GET['projectId'])){ $this->projectId = sanitize_text_field($_GET['projectId']); } else { if(!empty($this->projects['vod'])) { $this->projectId = reset($this->projects['vod'])['Id']; } else { $this->projectId = null; } } if(isset($_GET['projectSecret'])){ $this->projectSecret = sanitize_text_field($_GET['projectSecret']); } else { if(!empty($this->projects['vod'])) { $this->projectSecret = reset($this->projects['vod'])['SecurityKey']; } else { $this->projectSecret = null; } } if(get_option(P3QVC_PLUGIN_SUFFIX.'pager') != false) { $this->limit = get_option(P3QVC_PLUGIN_SUFFIX.'pager'); } if(isset($_GET['offset']) && is_numeric($_GET['offset'])) { $this->offset = sanitize_text_field($_GET['offset']); } if(isset($_GET['catId']) && is_numeric($_GET['catId'])) { $this->catId = sanitize_text_field($_GET['catId']); } if(isset($_GET['orderby']) && is_numeric($_GET['orderby'])) { $this->orderBy = sanitize_text_field($_GET['orderby']); } if(isset($_GET['period']) && in_array($_GET['period'], $this->periods)) { $this->period = sanitize_text_field($_GET['period']); } register_rest_route( '3q-video-connect/v1', 'getVideoList/json', array( 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this , 'getJsonVideoList'), ) ); register_rest_route( '3q-video-connect/v1', 'getVideoList/html', array( 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this , 'getVideoList'), ) ); register_rest_route( '3q-video-connect/v1', 'getChannelList/html', array( 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this , 'getChannelList'), ) ); register_rest_route( '3q-video-connect/v1', 'getUploadForm/html', array( 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this , 'getUploadForm'), ) ); } /** * function getUploadForm * @desc provides the generated html-code for the video-upload view * @param \WP_REST_Request $request * @return mixed */ public function getUploadForm( \WP_REST_Request $request ){ $jsonArr = array(); $htmlOutput = ""; $jsonArr = array(); $htmlOutput = ""; if ($this->apitoken == null || $this->apitoken == '' || $this->apitoken === false) { $error['error'] = "apitoken"; $error['message'] = "No API token exists or API token is invalid. Please enter an API token in the settings for the 3Q-Videos plugin."; ob_start(); include P3QVC_VIDEOS_ROOT_PATH.'/templates/error_template.php'; $htmlOutput .= ob_get_clean(); } elseif ($this->projectId== null || $this->projectId== '') { $error['error'] = "projectid"; $error['message'] = "No project id available. Please enter a Project Id in the settings for the 3Q-Videos plugin."; ob_start(); include P3QVC_VIDEOS_ROOT_PATH.'/templates/error_template.php'; $htmlOutput .= ob_get_clean(); } else { $vod_projects = $this->projects['vod']; if (!empty($vod_projects)) { // create the select field to set the project to upload the file $uploadProjectSelector = "
"; $uploadProjectSelector .= "
"; ob_start(); include P3QVC_VIDEOS_ROOT_PATH . '/templates/upload_template.php'; $htmlOutput .= ob_get_clean(); } else { $error['error'] = "projectid"; $error['message'] = "Your API token does not have a VOD project associated with it. Therefore, the upload is currently not possible."; ob_start(); include P3QVC_VIDEOS_ROOT_PATH . '/templates/error_template.php'; $htmlOutput .= ob_get_clean(); } } $jsonArr['html'] = $htmlOutput; return json_decode(json_encode($jsonArr)); } /** * function provides the html code to display all channels (livestream and audio) * @param \WP_REST_Request $request * @return string */ public function getChannelList( \WP_REST_Request $request ) { $jsonArr = array(); $htmlOutput = ""; if ($this->apitoken == null || $this->apitoken == '' || $this->apitoken === false) { $error['error'] = "apitoken"; $error['message'] = "No API token exists or API token is invalid. Please enter an API token in the settings for the 3Q-Videos plugin."; ob_start(); include P3QVC_VIDEOS_ROOT_PATH.'/templates/error_template.php'; $htmlOutput .= ob_get_clean(); } elseif ($this->projectId== null || $this->projectId== '') { $error['error'] = "projectid"; $error['message'] = "No project id available. Please enter a Project Id in the settings for the 3Q-Videos plugin."; ob_start(); include P3QVC_VIDEOS_ROOT_PATH.'/templates/error_template.php'; $htmlOutput .= ob_get_clean(); } else { $channels = $this->getChannels(); $htmlOutput = "
"; foreach ($channels['Channels'] as $channel) { ob_start(); $project_secret = $this->projects['livestream'][$channel['Project']['Id']]['SecurityKey']; include P3QVC_VIDEOS_ROOT_PATH . '/templates/livestream_template.php'; $htmlOutput .= ob_get_clean(); } $htmlOutput .= "
"; } $jsonArr['html'] = $htmlOutput; return json_decode(json_encode($jsonArr)); } /** * function provides html code for videos * @param \WP_REST_Request $request * @return mixed */ public function getVideoList( \WP_REST_Request $request ) { $jsonArr = array(); $htmlOutput = ""; if ($this->apitoken == null || $this->apitoken == '' || $this->apitoken === false) { $error['error'] = "apitoken"; $error['message'] = "No API token exists or API token is invalid. Please enter an API token in the settings for the 3Q-Videos plugin."; ob_start(); include P3QVC_VIDEOS_ROOT_PATH.'/templates/error_template.php'; $htmlOutput .= ob_get_clean(); } elseif ($this->projectId== null || $this->projectId== '') { $error['error'] = "projectid"; $error['message'] = "No project id available. Please enter a Project Id in the settings for the 3Q-Videos plugin."; ob_start(); include P3QVC_VIDEOS_ROOT_PATH.'/templates/error_template.php'; $htmlOutput .= ob_get_clean(); } else { $videos = $this->requestVideos(); if(is_array($videos) && empty($videos)){ $htmlOutput .= "
"; $htmlOutput .= $this->getFilter(); $htmlOutput .= "
"; $error['error'] = "no-items"; $error['message'] = "This query returned an empty result, please try something else."; ob_start(); include P3QVC_VIDEOS_ROOT_PATH.'/templates/error_template.php'; $htmlOutput .= ob_get_clean(); } elseif($videos == false){ $error['error'] = "request-failed"; $error['message'] = "The API token and / or the project id seem to be wrong. Please correct your settings.
"; $error['message'] .= "An HTTP error has occurred (404 Forbidden | 403 Not Found).
"; ob_start(); include P3QVC_VIDEOS_ROOT_PATH.'/templates/error_template.php'; $htmlOutput .= ob_get_clean(); } else { $htmlOutput .= "
"; $htmlOutput .= $this->getFilter(); $pager = $this->getPager($this->totalCount); $htmlOutput .= $pager; $htmlOutput .= "
"; $htmlOutput .= "
"; foreach ($videos as $video) { ob_start(); include P3QVC_VIDEOS_ROOT_PATH.'/templates/item_template.php'; $htmlOutput .= ob_get_clean(); } $htmlOutput .= "
"; $htmlOutput .= $pager; } } $jsonArr['html'] = $htmlOutput; return json_decode(json_encode($jsonArr)); } /** * test function to * @param \WP_REST_Request $request * @unused * @return mixed */ public function getJsonVideoList( \WP_REST_Request $request ) { $videos = $this->requestVideos(); // Return either a WP_REST_Response or WP_Error object return json_decode(json_encode($videos)); } /** * function filter * @desc generates the html-code for all filters in view * @return string */ private function getFilter() { $filterHtml = "
"; // project select $filterHtml .= "
"; $filterHtml .= "
"; // categorie select $filterHtml .= "
"; $filterHtml .= "
"; // order select $filterHtml .= "
"; $filterHtml .= "
"; // period select $filterHtml .= "
"; $filterHtml .= "
"; $filterHtml .= "
"; return $filterHtml; } /** * function getProjects * @desc function requests all projects related to the api-token * and saves the projects for vod and livestream in keys [vod|livestream] in the return array * @return array[] */ private function getProjects() { $projects['vod'] = array(); $projects['livestream'] = array(); $request_url = $this->root_url."projects"; $response = file_get_contents($request_url,false,$this->requestContext); if($response === false) { $this->apitoken = false; return null; } else { $projects_response = json_decode($response,true); foreach($projects_response['Projects'] AS $project) { if($project['StreamType']['Id'] == 1) { // vod project $projects['vod'][$project['Id']] = $project; } elseif ($project['StreamType']['Id'] == 2 || $project['StreamType']['Id'] == 3) { // livestream project $projects['livestream'][$project['Id']] = $project; } } } return $projects; } /** * @name getChannels * @desc requests all channels (livestreams) related to the API-Key * @return array */ private function getChannels() { $channels = ""; $request_url = $this->root_url."channels"; $channels = file_get_contents($request_url,false,$this->requestContext); $response = json_decode($channels, true); return $response; } /** * @name getClipCats * @desc provides the 3q categories from one customer * !!! the categories are not dependent to a specific project (valid for all projects) * @return array */ private function getClipCats() { $request_url = $this->root_url."categories"; $clipcats = file_get_contents($request_url,false,$this->requestContext); $cats = json_decode($clipcats, true); return $cats; } /** * request 3Q-API and prepare videos array * @return array|false */ private function requestVideos() { date_default_timezone_set('UTC'); // prepare the get url $request_url = $this->root_url."projects/".$this->projectId."/files?IncludeMetadata=true&IncludeProperties=true&Limit=".$this->limit; if($this->offset != 0) { $request_url .= "&Offset=".$this->offset; } if($this->catId != 0) { $request_url .= "&CategoryId=".$this->catId; } $request_url .= "&OrderBy=".$this->orderBy; if($this->period != 'all') { $request_url .= "&Period=".$this->period; } // request all files $filelist = @file_get_contents($request_url,false,$this->requestContext); if($http_response_header[0] == "HTTP/1.1 403 Forbidden" || $http_response_header[0] == "HTTP/1.1 404 Not Found"){ return false; } $j_filelist = json_decode($filelist, true); $this->totalCount = $j_filelist['TotalCount']; $videos = array(); foreach ($j_filelist['Files'] AS $file) { $video['id'] = $file['Id']; $video['title'] = $file['Metadata']['Title']; $video['image'] = $file['Metadata']['StandardFilePicture']['URI']; $video['thumb'] = $file['Metadata']['StandardFilePicture']['ThumbURI']; $video['length'] = gmdate("H:i:s", $file['Properties']['Length']); $video['created'] = date("d.m.Y", strtotime($file['CreatedAt'])); $video['updated'] = date("d.m.Y", strtotime($file['LastUpdateAt'])); $videos[] = $video; } return $videos; } /** * function getPager * @desc provides the pager for the videos * @param int $totalCount * @return string */ private function getPager($totalCount) { $currentPage = 1; $totalPages = ceil($totalCount/$this->limit); if($this->offset != 0) { $currentPage = ($this->offset / $this->limit) + 1; } $pagerHtml = "
"; $pagerHtml .= ""; $pagerHtml .= "
Page ".$currentPage." of ".$totalPages." (Total number of videos : ".$totalCount." )
"; $pagerHtml .= "
"; // div pagination end return $pagerHtml; } private function formatBytes($size, $precision = 2) { $base = log($size, 1024); $suffixes = array('', 'KB', 'MB', 'GB', 'TB'); return round(pow(1024, $base - floor($base)), $precision) .' '. $suffixes[floor($base)]; } }