changed git call from https to git readonly
[atutor.git] / mods / atutor_opencaps / opencaps / include / classes / system_class.php
1 <?php
2 /*
3  * OpenCaps
4  * http://opencaps.atrc.utoronto.ca
5  * 
6  * Copyright 2009 Heidi Hazelton
7  * Adaptive Technology Resource Centre, University of Toronto
8  * 
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
12  * 
13  */
14
15 class system {
16         
17         public $system_id;
18         public $name;
19         private $type; 
20
21         public $base_url;       
22         private $get_url;
23         private $put_url;
24         
25         private $username; //optional
26         private $password; //optional
27         
28         
29         /* new system connection */
30         function __construct($system_id) {
31                 global $systems;
32                 
33                 $_SESSION['rid'] = $system_id;
34                 
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
40
41                 $this->username = $systems[$system_id]['username'];
42                 $this->password = $systems[$system_id]['password'];
43                                                 
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';
47         }
48         
49         function openProject($id) {
50                 global $this_proj, $supported_ext;
51                 
52                 $media_info = $this->getMedia();
53                 
54                 if (!empty($media_info)) {
55
56                         //check if correct format
57                         $ext = explode(".", $media_info->mediaFile);
58                         $ext = end($ext);
59         
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");
64                                 exit;
65                         }       
66                                         
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();     
71                                 
72                         //import existing captions, if any                      
73                         if (!empty($media_info->captionFile))                   
74                                 $this->importSystemCaptions($media_info->captionFile);  
75                                                                                 
76                         //save working file
77                         $json = json_encode(get_object_vars($this_proj));
78                         $this_proj->saveJson($json, $this_proj->id);
79                         
80                 } else {                        
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");
84                         exit;
85                 }       
86
87                 // set user as guest
88                 $_SESSION['valid_user'] = true;
89                 $_SESSION['mid'] = '99999';
90                                                         
91                 header('Location:editor.php');
92                 exit;
93                 
94                 return;         
95         }
96         
97         
98         function getMedia() {
99                 global $this_proj;
100                                                 
101                 //get json              
102                 switch ($this->type) {
103                         case 'matterhorn':
104                                 $media_info = json_decode($this->matterhornAuth('/opencaps/rest/'.$this_proj->id));
105                                 
106                                 //convert naming
107                                 $media_info->mediaFile = $media_info->mediaURL;
108                                 $media_info->captionFile = $media_info->DFXPURL;
109                                 unset($media_info->mediaURL);
110                                 unset($media_info->DFXPURL);
111                                 break;
112                                 
113                         case 'atutor':                          
114                                 $media_info = json_decode(@file_get_contents($this->get_url.'&id='.$this_proj->id));
115                                 break;
116                                 
117                         default:
118                                 $media_info = json_decode(@file_get_contents($this->get_url.'&id='.$this_proj->id));                            
119                                 break;
120                 }       
121                 return $media_info;     
122         }               
123         
124         function putCaps($id, $format="SubRipSrt") {
125                 global $this_proj, $base_url, $systems;
126                                 
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
130                 \r
131                 if (OC_DEBUG_MODE_ON)\r
132                 {\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
147                 }\r
148                 
149                 //send captions
150                 if (!empty($formatted_captions)) {
151                         
152                         switch ($this->type) {
153                                 case 'matterhorn':
154                                         $uri = '/opencaps/rest/'.$this_proj->id.'/TimedText/';                  
155                                         $response = matterhornAuth($this->id, $uri, $formatted_captions);                                       
156                                         break;
157                                         
158                                 default:\r
159                                         /*\r
160                                          * ANTO APPROACH\r
161                                          */\r
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
165                                         break;
166                         }       \r
167                         /*\r
168                          * ANTO: this stuff is too accurate, really tells nothing about the success to the action (e.g. was the file updated?):\r
169                          * \r
170                         */                       \r
171                         if (empty($response)) {
172                                 $_SESSION['feedback'][] = "Successfully updated server.";                       
173                         } else {                                
174                                 $_SESSION['errors'][] = "Could not update remote server.";
175                                 
176                         }\r
177                 
178                         
179                         
180                 } else {
181                         $_SESSION['errors'][] = "Could not convert captions to required format.";
182                 }
183                 
184                 return true;    
185                 
186         }
187         
188         function importSystemCaptions($capfile) {               
189                 global $base_url, $this_proj;           
190                 
191                 $ccollection = new clipCollection() ;
192                 
193                 //get captions          
194                 switch ($this->type) {
195                         case 'matterhorn':
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);       
200                                 break;
201                                 
202                         case 'atutor':
203                                 $caption_file = $capfile;
204                                 break;
205                                 
206                         default:
207                                 break;
208                 }       
209                                         
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
212 \r
213                 if (!empty($json_captions) && $json_captions != "The format of source Caption was not recognized.") {                   
214                         
215                         foreach ($json_captions->captionCollection as $clip) {                          
216                                 $this_clip = new clip($clip->inTime, $clip->outTime, trim($clip->caption));
217                                 $ccollection->addClip($this_clip);
218                         }       
219
220                         $_SESSION['feedback'][] = "Captions imported successfully.";
221                                                                                 
222                 } else  {
223                         $_SESSION['errors'][] = "Problem with caption file - the format is incorrect, or unsupported.";
224                 }               
225                 
226                 $this_proj->clip_collection = $ccollection;
227                         
228                 //save reference file
229                 $json = json_encode(get_object_vars($this_proj));               
230                 $this_proj->saveJson($json, $this->id);         
231                 return;
232
233         }                       
234         
235         
236         /* digest auth for matterhorn systems */
237         function matterhornAuth($uri, $content='') {
238                 global $this_proj;
239                         
240             $username = $this->username;
241             $password = $this->password;
242                         
243                 $ch = curl_init();
244             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
245             
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")); 
250                 
251             if ($content == "media") {
252                 curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
253                 
254                 $temp_file = 'projects/'.$this_proj->id.'/media'; //use tempname();
255                 $fh = fopen($temp_file, 'w');
256                         curl_setopt($ch, CURLOPT_FILE, $fh);
257                         curl_exec($ch);
258                         curl_close($ch);
259                         fclose($fh); 
260                         return $temp_file;
261                         
262             } else if (!empty($content)) {
263                     curl_setopt($ch, CURLOPT_POST, 1);
264                 curl_setopt($ch, CURLOPT_POSTFIELDS, $content);   
265             }
266                 
267             $response = curl_exec($ch);
268             $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
269         
270             curl_close($ch);
271         
272                 return $response;       
273         }
274
275         
276         /* list a system's available projects */
277         function printProjects($pageNum=1) {
278                                 
279                 //check if connected
280                 if (!@file_get_contents($this->url)) {
281                         echo "Can't connect to remote server.";
282                         return;
283                 }
284                 
285                 $projsPerPage = 20;
286
287                 $uri = "/opencaps/rest/list/processed.json";    
288                 $remote_media = json_decode(matterhornAuth($rid, $uri));        
289                                 
290                 $numrows = $remote_media->total;                
291                 $maxPage = ceil($numrows/$projsPerPage);
292                                 
293                 $nav  = 'Page: ';               
294                 for($page = 1; $page <= $maxPage; $page++) {
295                    if ($page == $pageNum) {
296                       $nav .= " $page "; 
297                    } else {
298                       $nav .= ' <a href="start_remote.php?r='.$rid.'&page='.$page.'">'.$page.'</a> ';
299                    }
300                 }               
301                 
302                 if ($maxPage > 1)
303                         echo $nav."<br /><br />";
304                 
305                 $offset = ($pageNum - 1) * $projsPerPage;
306                 
307                 $uri = "/opencaps/rest/list/processed.json?count=90&startPage=".($pageNum-1);           
308                                 
309                 //$remote_media = $remote_systems[$rid]['url']."/opencaps/rest/list/processed.json?count=$projsPerPage&startPage=".($pageNum-1);
310                 
311                 $_SESSION['rid'] = $rid;                
312                 $remote_media = json_decode(matterhornAuth($rid, $uri));                
313                                 
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>';
318                         }
319                         echo '</ul>';
320                         echo "<div style='text-align:right;'><input type='submit' class='button' style='width:6em;margin-top:5px;' name='startopen' value='Submit' /></div>";
321                 } else {
322                         echo "No projects yet.";
323                 }
324         }       
325         
326 }       
327
328 ?>