Overview

Packages

  • ClipIt
    • clipit
      • api
    • urjc
      • backend
  • Elgg
    • Core
      • Access
      • Authentication
      • Cache
      • Caches
      • Core
      • DataMode
        • Site
      • DataModel
        • Annotations
        • Entities
        • Extender
        • File
        • Importable
        • Loggable
        • Notable
        • Object
        • User
      • DataStorage
      • Exception
      • Exceptions
        • Stub
      • FileStore
        • Disk
      • Groups
      • Helpers
      • HMAC
      • Memcache
      • Metadata
      • Navigation
      • ODD
      • Output
      • Plugins
        • Settings
      • Sessions
      • SocialModel
        • Friendable
        • Locatable
      • WebServicesAPI
      • Widgets
      • XML
      • XMLRPC
    • Exceptions
      • Stub
  • None
  • PHP

Classes

  • ClipitActivity
  • ClipitChat
  • ClipitComment
  • ClipitEvent
  • ClipitExample
  • ClipitFile
  • ClipitGroup
  • ClipitLA
  • ClipitLabel
  • ClipitPerformanceItem
  • ClipitPerformanceRating
  • ClipitPost
  • ClipitQuiz
  • ClipitQuizQuestion
  • ClipitQuizResult
  • ClipitRating
  • ClipitRemoteTrickyTopic
  • ClipitRemoteVideo
  • ClipitResource
  • ClipitSite
  • ClipitStoryboard
  • ClipitTag
  • ClipitTagRating
  • ClipitTask
  • ClipitTrickyTopic
  • ClipitUser
  • ClipitVideo
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * ClipIt - JuxtaLearn Web Space
  4:  * PHP version:     >= 5.2
  5:  * Creation date:   2013-10-10
  6:  * Last update:     $Date$
  7:  * @author          Pablo LlinĂ¡s Arnaiz <pebs74@gmail.com>, URJC JuxtaLearn Team
  8:  * @version         $Version$
  9:  * @link            http://www.juxtalearn.eu
 10:  * @license         GNU Affero General Public License v3
 11:  * @package         ClipIt
 12:  * @subpackage      clipit_api
 13:  */
 14: 
 15: /**
 16:  * Contains the final product (video) of a group during an activity.
 17:  */
 18: class ClipitVideo extends ClipitResource{
 19:     /**
 20:      * @const string Elgg entity SUBTYPE for this class
 21:      */
 22:     const SUBTYPE = "ClipitVideo";
 23:     const REL_RESOURCE_TAG = "ClipitVideo-ClipitTag";
 24:     const REL_RESOURCE_PERFORMANCE = "ClipitVideo-ClipitPerformance";
 25:     const REL_RESOURCE_LABEL = "ClipitVideo-ClipitLabel";
 26:     const REL_RESOURCE_USER = "ClipitVideo-ClipitUser";
 27:     const REL_EXAMPLE_RESOURCE = ClipitExample::REL_EXAMPLE_VIDEO;
 28:     const REL_GROUP_RESOURCE = ClipitGroup::REL_GROUP_VIDEO;
 29:     const REL_TASK_RESOURCE = ClipitTask::REL_TASK_VIDEO;
 30:     const REL_ACTIVITY_RESOURCE = ClipitActivity::REL_ACTIVITY_VIDEO;
 31:     const REL_TRICKYTOPIC_RESOURCE = ClipitTrickyTopic::REL_TRICKYTOPIC_VIDEO;
 32:     const REL_SITE_RESOURCE = ClipitSite::REL_SITE_VIDEO;
 33:     public $preview = "";
 34:     public $duration = 0;
 35:     public $overlay_metadata = "";
 36: 
 37:     /**
 38:      * Uploads to YouTube a video file from a local path in the server.
 39:      *
 40:      * @param string $local_video_path Local server path of the video
 41:      * @param string $title Video title
 42:      *
 43:      * @return string YouTube video URL
 44:      */
 45:     static function upload_to_youtube($local_video_path, $title)
 46:     {
 47:         if (!get_config("google_refresh_token")) {
 48:             return false;
 49:         }
 50:         set_include_path(
 51:             get_include_path() . PATH_SEPARATOR . elgg_get_plugins_path() . "z02_clipit_api/libraries/google_api/src/"
 52:         );
 53:         $lib_path = elgg_get_plugins_path() . "z02_clipit_api/libraries/";
 54:         require_once($lib_path . "google_api/src/Google/Client.php");
 55:         require_once($lib_path . "google_api/src/Google/Service/YouTube.php");
 56: 
 57:         $client = new Google_Client();
 58:         $client->setClientId(get_config("google_id"));
 59:         $client->setClientSecret(get_config("google_secret"));
 60:         try {
 61:             $client->setAccessToken(get_config("google_token"));
 62:         } catch (Exception $e) {
 63:             error_log($e);
 64:         }
 65:         if ($client->isAccessTokenExpired()) {
 66:             $refresh_token = get_config("google_refresh_token");
 67:             $client->refreshToken($refresh_token);
 68:         }
 69:         if (!$client->getAccessToken()) {
 70:             return null;
 71:         }
 72:         // Define an object that will be used to make all API requests.
 73:         $youtube = new Google_Service_YouTube($client);
 74:         // Create a snippet with title, description, tags and category ID
 75:         // Create an asset resource and set its snippet metadata and type.
 76:         // This example sets the video's title, description, keyword tags, and
 77:         // video category.
 78:         $snippet = new Google_Service_YouTube_VideoSnippet();
 79:         $snippet->setTitle($title);
 80:         // Numeric video category. See
 81:         // https://developers.google.com/youtube/v3/docs/videoCategories/list
 82:         $snippet->setCategoryId("28");
 83:         // Set the video's status to "public". Valid statuses are "public",
 84:         // "private" and "unlisted".
 85:         $status = new Google_Service_YouTube_VideoStatus();
 86:         $status->privacyStatus = "unlisted";
 87:         // Associate the snippet and status objects with a new video resource.
 88:         $video = new Google_Service_YouTube_Video();
 89:         $video->setSnippet($snippet);
 90:         $video->setStatus($status);
 91:         // Specify the size of each chunk of data, in bytes. Set a higher value for
 92:         // reliable connection as fewer chunks lead to faster uploads. Set a lower
 93:         // value for better recovery on less reliable connections.
 94:         $chunkSizeBytes = 1 * 1024 * 1024;
 95:         // Setting the defer flag to true tells the client to return a request which can be called
 96:         // with ->execute(); instead of making the API call immediately.
 97:         $client->setDefer(true);
 98:         // Create a request for the API's videos.insert method to create and upload the video.
 99:         $insertRequest = $youtube->videos->insert("status,snippet", $video);
100:         // Create a MediaFileUpload object for resumable uploads.
101:         $media = new Google_Http_MediaFileUpload($client, $insertRequest, 'video/*', null, true, $chunkSizeBytes);
102:         $media->setFileSize(filesize($local_video_path));
103:         // Read the media file and upload it chunk by chunk.
104:         $status = false;
105:         $handle = fopen($local_video_path, "rb");
106:         while (!$status && !feof($handle)) {
107:             $chunk = fread($handle, $chunkSizeBytes);
108:             $status = $media->nextChunk($chunk);
109:         }
110:         fclose($handle);
111:         // If you want to make other calls after the file upload, set setDefer back to false
112:         $client->setDefer(false);
113:         $_SESSION['token'] = $client->getAccessToken();
114:         set_config("google_token", $_SESSION['token']);
115:         return (string)"http://www.youtube.com/watch?v=" . $status['id'];
116:     }
117: 
118:     /**
119:      * Loads object parameters stored in Elgg
120:      *
121:      * @param ElggEntity $elgg_entity Elgg Object to load parameters from.
122:      */
123:     protected function copy_from_elgg($elgg_entity)
124:     {
125:         parent::copy_from_elgg($elgg_entity);
126:         $this->preview = (string)$elgg_entity->get("preview");
127:         $this->duration = (int)$elgg_entity->get("duration");
128:         $this->overlay_metadata = (string)$elgg_entity->get("overlay_metadata");
129:     }
130: 
131:     /**
132:      * Copy $this object parameters into an Elgg entity.
133:      *
134:      * @param ElggEntity $elgg_entity Elgg object instance to save $this to
135:      */
136:     protected function copy_to_elgg($elgg_entity)
137:     {
138:         parent::copy_to_elgg($elgg_entity);
139:         $video_metadata = static::video_url_parser($this->url);
140:         $elgg_entity->set("url", (string)$video_metadata["url"]);
141:         if (empty($this->preview)) {
142:             $this->preview = (string)$video_metadata["preview"];
143:         }
144:         $elgg_entity->set("preview", (string)$this->preview);
145:         $elgg_entity->set("duration", (int)$this->duration);
146:         $elgg_entity->set("overlay_metadata", (string)$this->overlay_metadata);
147:     }
148: 
149:     /**
150:      * @param $url
151:      * @return array|bool
152:      */
153:     static function video_url_parser($url)
154:     {
155:         if ($parse_url = parse_url($url)) {
156:             if (!isset($parts["scheme"])) {
157:                 $url = "http://$url";
158:             }
159:         }
160:         if (!isset($url) || !filter_var($url, FILTER_VALIDATE_URL)) {
161:             return false;
162:         }
163:         $video_patterns = array('#(((http://)?)|(^./))(((www.)?)|(^./))youtube\.com/watch[?]v=([^\[\]()<.,\s\n\t\r]+)#i'
164:         , '#(((http://)?)|(^./))(((www.)?)|(^./))youtu\.be/([^\[\]()<.,\s\n\t\r]+)#i'
165:         , '/(http:\/\/)(www\.)?(vimeo\.com\/groups)(.*)(\/videos\/)([0-9]*)/'
166:         , '/(http:\/\/)(www\.)?(vimeo.com\/)([0-9]*)/'
167:         , '/(https:\/\/)(www\.)?(vimeo.com\/)([0-9]*)/');
168:         $favicon_url_base = "http://www.google.com/s2/favicons?domain=";
169: 
170:         $output = array();
171:         foreach ($video_patterns as $video_pattern) {
172:             if (preg_match($video_pattern, $url) > 0) {
173:                 // Youtube
174:                 if (strpos($url, 'youtube.com') != false || strpos($url, 'youtu.be') != false) {
175:                     preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $url, $matches);
176:                     $output = array(
177:                         'id' => $matches[0],
178:                         'url' => 'http://www.youtube.com/watch?v=' . $matches[0],
179:                         'preview' => "http://i1.ytimg.com/vi/{$matches[0]}/mqdefault.jpg",
180:                         'favicon' => $favicon_url_base . $parse_url['host']
181:                     );
182:                 // Vimeo
183:                 } else if (strpos($url, 'vimeo.com') != false) {
184:                     preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=v=)[^&\n]+|(?<=vimeo.com/)[^&\n]+#", $url, $matches);
185:                     $data = file_get_contents("http://vimeo.com/api/v2/video/$matches[0].json");
186:                     $data = array_pop(json_decode($data));
187:                     $output = array(
188:                         'id' => $matches[0],
189:                         'url' => "http://vimeo.com/{$matches[0]}",
190:                         'preview' => $data->thumbnail_large,
191:                         'favicon' => $favicon_url_base . $parse_url['host']
192:                     );
193:                 }
194:             }
195:         }
196:         if (!$output['id']) {
197:             return false;
198:         }
199:         // Video Data output
200:         return $output;
201:     }
202: }
API documentation generated by ApiGen 2.8.0