4 * http://opencaps.atrc.utoronto.ca
6 * Copyright 2009 Heidi Hazelton
7 * Adaptive Technology Resource Centre, University of Toronto
9 * Licensed under the Educational Community License (ECL), Version 2.0.
10 * You may not use this file except in compliance with this License.
11 * http://www.opensource.org/licenses/ecl2.php
25 private $username; //optional
26 private $password; //optional
29 /* new system connection */
30 function __construct($system_id) {
33 $_SESSION['rid'] = $system_id;
35 /* load in system info from config file */
36 $this->system_id = $system_id;
37 $this->name = $systems[$system_id]['name'];
38 $this->base_url = $systems[$system_id]['url'];
39 $this->type = $systems[$system_id]['type'];
\r
41 $this->username = $systems[$system_id]['username'];
42 $this->password = $systems[$system_id]['password'];
44 $this->get_url = $this->base_url."?action=getMedia"; // url for receiving media info
45 $this->put_url = $this->base_url."?action=putCaps"; // url for sending captions back
46 $this->returnFormat = 'DFXP';
49 function openProject($id) {
50 global $this_proj, $supported_ext;
52 $media_info = $this->getMedia();
54 if (!empty($media_info)) {
56 //check if correct format
57 $ext = explode(".", $media_info->mediaFile);
60 if (!@in_array($ext, $supported_ext)) {
61 $_SESSION['errors'][] = "Media file format not supported.";
62 include(INCLUDE_PATH."basic_header.inc.php");
63 include(INCLUDE_PATH."footer.inc.php");
67 //populate project vars
68 $this_proj->name = $media_info->title;
69 $this_proj->media_loc = $media_info->mediaFile;
70 $this_proj->clip_collection = new clipCollection();
72 //import existing captions, if any
73 if (!empty($media_info->captionFile))
74 $this->importSystemCaptions($media_info->captionFile);
77 $json = json_encode(get_object_vars($this_proj));
78 $this_proj->saveJson($json, $this_proj->id);
81 $_SESSION['errors'][] = "Could not open project - no response from server.";
82 include(INCLUDE_PATH."basic_header.inc.php");
83 include(INCLUDE_PATH."footer.inc.php");
88 $_SESSION['valid_user'] = true;
89 $_SESSION['mid'] = '99999';
91 header('Location:editor.php');
102 switch ($this->type) {
104 $media_info = json_decode($this->matterhornAuth('/opencaps/rest/'.$this_proj->id));
107 $media_info->mediaFile = $media_info->mediaURL;
108 $media_info->captionFile = $media_info->DFXPURL;
109 unset($media_info->mediaURL);
110 unset($media_info->DFXPURL);
114 $media_info = json_decode(@file_get_contents($this->get_url.'&id='.$this_proj->id));
118 $media_info = json_decode(@file_get_contents($this->get_url.'&id='.$this_proj->id));
124 function putCaps($id, $format="SubRipSrt") {
125 global $this_proj, $base_url, $systems;
127 //convert captions to required format
128 $convert_url = $base_url.'conversion_service/index.php?cc_url='.urlencode('../projects/'.$_SESSION['pid'].'/opencaps.json').'&cc_result=0&cc_target='.$format.'&cc_name=noname.txt';
129 $formatted_captions = trim(@file_get_contents($convert_url));
\r
131 if (OC_DEBUG_MODE_ON)
\r
133 $ocAtDebugMsg = '*************************';
\r
134 $ocAtDebugMsg .= '<br/>';
\r
135 $ocAtDebugMsg .= '<b>putCaps() - class: system</b>';
\r
136 $ocAtDebugMsg .= '<br/>';
\r
137 $ocAtDebugMsg .= '<br/>ID: '.$_SESSION['pid'];
\r
138 $ocAtDebugMsg .= '<br/>';
\r
139 $ocAtDebugMsg .= '<br/>Conversion Service URL:<br/> '.$convert_url;
\r
140 $ocAtDebugMsg .= '<br/>';
\r
141 $ocAtDebugMsg .= '<br/>Caption Data:<br/>'.$formatted_captions;
\r
142 $ocAtDebugMsg .= '<br/>';
\r
143 $ocAtDebugMsg .= '<br/>Base Url: '.$base_url;
\r
144 $ocAtDebugMsg .= '<br/><br/>';
\r
145 $ocAtDebugMsg .= '*************************';
\r
146 $_SESSION['feedback'][] = $ocAtDebugMsg;
\r
150 if (!empty($formatted_captions)) {
152 switch ($this->type) {
154 $uri = '/opencaps/rest/'.$this_proj->id.'/TimedText/';
155 $response = matterhornAuth($this->id, $uri, $formatted_captions);
162 // load and call atutor class
\r
163 include_once('system_OcAtutor_class.php');
\r
164 OcAtutor::putCaps($systems[ACTIVE_SYSTEM]['url'],'putCaps',$_SESSION['pid'],$formatted_captions);
\r
168 * ANTO: this stuff is too accurate, really tells nothing about the success to the action (e.g. was the file updated?):
\r
171 if (empty($response)) {
172 $_SESSION['feedback'][] = "Successfully updated server.";
174 $_SESSION['errors'][] = "Could not update remote server.";
181 $_SESSION['errors'][] = "Could not convert captions to required format.";
188 function importSystemCaptions($capfile) {
189 global $base_url, $this_proj;
191 $ccollection = new clipCollection() ;
194 switch ($this->type) {
196 $uri = substr($capfile, strlen($remote_systems[$_SESSION['rid']]['url']));
197 $caps = matterhornAuth($_SESSION['rid'], $uri);
198 $caption_file = INCLUDE_PATH.'../projects/'.$this->id.'/'.'captions.xml';
199 @file_put_contents($caption_file, $caps);
203 $caption_file = $capfile;
210 $convert_url = $base_url.'/conversion_service/?cc_url='.$caption_file.'&cc_result=0&cc_target=JSONcc&cc_name=noname.___';
211 $json_captions = json_decode(@file_get_contents($convert_url));
\r
213 if (!empty($json_captions) && $json_captions != "The format of source Caption was not recognized.") {
215 foreach ($json_captions->captionCollection as $clip) {
216 $this_clip = new clip($clip->inTime, $clip->outTime, trim($clip->caption));
217 $ccollection->addClip($this_clip);
220 $_SESSION['feedback'][] = "Captions imported successfully.";
223 $_SESSION['errors'][] = "Problem with caption file - the format is incorrect, or unsupported.";
226 $this_proj->clip_collection = $ccollection;
228 //save reference file
229 $json = json_encode(get_object_vars($this_proj));
230 $this_proj->saveJson($json, $this->id);
236 /* digest auth for matterhorn systems */
237 function matterhornAuth($uri, $content='') {
240 $username = $this->username;
241 $password = $this->password;
244 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
246 curl_setopt($ch, CURLOPT_URL, $systems[$rid]['url'].$uri);
247 curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
248 curl_setopt($ch, CURLOPT_USERPWD, $username.':'.$password);
249 curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Requested-Auth: Digest"));
251 if ($content == "media") {
252 curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
254 $temp_file = 'projects/'.$this_proj->id.'/media'; //use tempname();
255 $fh = fopen($temp_file, 'w');
256 curl_setopt($ch, CURLOPT_FILE, $fh);
262 } else if (!empty($content)) {
263 curl_setopt($ch, CURLOPT_POST, 1);
264 curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
267 $response = curl_exec($ch);
268 $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
276 /* list a system's available projects */
277 function printProjects($pageNum=1) {
280 if (!@file_get_contents($this->url)) {
281 echo "Can't connect to remote server.";
287 $uri = "/opencaps/rest/list/processed.json";
288 $remote_media = json_decode(matterhornAuth($rid, $uri));
290 $numrows = $remote_media->total;
291 $maxPage = ceil($numrows/$projsPerPage);
294 for($page = 1; $page <= $maxPage; $page++) {
295 if ($page == $pageNum) {
298 $nav .= ' <a href="start_remote.php?r='.$rid.'&page='.$page.'">'.$page.'</a> ';
303 echo $nav."<br /><br />";
305 $offset = ($pageNum - 1) * $projsPerPage;
307 $uri = "/opencaps/rest/list/processed.json?count=90&startPage=".($pageNum-1);
309 //$remote_media = $remote_systems[$rid]['url']."/opencaps/rest/list/processed.json?count=$projsPerPage&startPage=".($pageNum-1);
311 $_SESSION['rid'] = $rid;
312 $remote_media = json_decode(matterhornAuth($rid, $uri));
314 if (!empty($remote_media->results)) {
315 echo '<ul class="proj-list">';
316 foreach($remote_media->results as $rm) {
317 echo '<li><label><input type="radio" name="proj" value='.$rm->id.' /> '.$rm->title.'</label></li>';
320 echo "<div style='text-align:right;'><input type='submit' class='button' style='width:6em;margin-top:5px;' name='startopen' value='Submit' /></div>";
322 echo "No projects yet.";