2 /****************************************************************/
4 /****************************************************************/
5 /* Copyright (c) 2002-2009 */
6 /* Adaptive Technology Resource Centre / University of Toronto */
9 /* This program is free software. You can redistribute it and/or*/
10 /* modify it under the terms of the GNU General Public License */
11 /* as published by the Free Software Foundation. */
12 /****************************************************************/
15 require_once(dirname(__FILE__) . '/Applications.class.php');
16 require_once(dirname(__FILE__) .'/../SecurityToken.php');
17 require_once(dirname(__FILE__) .'/../BlobCrypter.php');
18 require_once(dirname(__FILE__) .'/../Crypto.php');
19 require_once(dirname(__FILE__) .'/../BasicSecurityToken.php');
20 require_once(dirname(__FILE__) .'/../BasicBlobCrypter.php');
23 * Object for Application, (aka Gadgets)
25 class Application extends Applications{
26 var $id; //application id
27 var $url, $title, $height, $screenshot, $thumbnail, $author, $author_email, $description, $settings, $views;
31 function Application ($id=0){
34 $this->getApplicationPrefs();
39 * Add application by URL
40 * @param object gadget object retrieved from JSON + cURL
42 function addApplication($gadget_obj){
43 global $db, $addslashes;
45 //TODO: Many more fields to add
46 // $id = $gadget_obj['moduleId']; //after i change the URL to the key.
47 $author = $addslashes($gadget_obj->author);
48 $author_email = $addslashes($gadget_obj->authorEmail);
49 $description = $addslashes($gadget_obj->description);
50 $screenshot = $addslashes($gadget_obj->screenshot);
51 $thumbnail = $addslashes($gadget_obj->thumbnail);
52 $title = $addslashes($gadget_obj->title);
53 $height = intval($gadget_obj->height);
54 $module_id = intval($gadget_obj->module_id);
55 $url = $addslashes($gadget_obj->url);
56 $userPrefs = $addslashes(serialize($gadget_obj->userPrefs));
57 $views = $addslashes(serialize($gadget_obj->views));
60 $sql = 'SELECT MAX(id) AS max_id FROM '.TABLE_PREFIX.'social_applications';
61 $result = mysql_query($sql, $db);
63 $row = mysql_fetch_assoc($result);
64 $id = $row['max_id'] + 1;
68 $member_id = $_SESSION['member_id'];
70 $sql = 'INSERT INTO '.TABLE_PREFIX."social_applications (id, url, title, height, screenshot, thumbnail, author, author_email, description, settings, views, last_updated) VALUES ($id, '$url', '$title', $height, '$screenshot', '$thumbnail', '$author', '$author_email', '$description', '$userPrefs', '$views', NOW())";
71 $result = mysql_query($sql, $db);
73 //This application is already in the database, get its ID out
75 $sql = 'SELECT id, UNIX_TIMESTAMP(last_updated) AS last_updated FROM '.TABLE_PREFIX."social_applications WHERE url='$url'";
76 $result = mysql_query($sql, $db);
77 $row = mysql_fetch_assoc($result);
81 //TODO: Needs some sort of comparing instead of blinding updating everytime. Version(can't find any)? Date(need another field in db?)
82 //Use TIMESTAMP for now, but i perfer version #.
83 //If the gadget is SOCIAL_APPLICATION_UPDATE_SCHEDULE days old, update it
84 if (abs($row['last_updated']) < strtotime('-'.SOCIAL_APPLICATION_UPDATE_SCHEDULE.' day')){
85 $sql = 'UPDATE '.TABLE_PREFIX."social_applications SET title='$title', height=$height, screenshot='$screenshot', thumbnail='$thumbnail', author='$author', author_email='$author_email', description='$description', settings='$userPrefs', views='$views', last_updated=NOW() WHERE url='$url'";
86 $result = mysql_query($sql, $db);
91 //Add a record into application_member table for mapping
92 $this->addMemberApplication($member_id, $id);
97 * Add this application to the member's application list
98 * @param int member_id
99 * @param int application_id
101 function addMemberApplication($member_id, $app_id){
104 $member_id = intval($member_id);
105 $app_id = intval($app_id);
107 $sql = 'INSERT INTO '.TABLE_PREFIX."social_members_applications (member_id, application_id) VALUES ($member_id, $app_id)";
108 $result = mysql_query($sql, $db);
111 //Add this to the home page
112 $home_settings = $this->getHomeDisplaySettings();
113 $home_settings[$app_id] = 1; //manually set this application
114 $this->setHomeDisplaySettings($home_settings);
116 $act = new Activity();
117 $act->addActivity($_SESSION['member_id'], '', $app_id);
124 * Parse application details
125 * @return array of the attributes
128 function parseModulePrefs($app_url){
129 //parse all the attributes of the <ModulePrefs> tag
130 //and save everything in the object.
131 $gadget = $this->fetch_gadget_metadata($app_url);
132 return $gadget->gadgets;
136 // Restful - JSON CURL data transfer
137 private function fetch_gadget_metadata($app_url) {
138 $request = json_encode(array(
139 'context' => array('country' => 'US', 'language' => 'en', 'view' => 'default',
140 'container' => 'atutor'),
141 'gadgets' => array(array('url' => $app_url, 'moduleId' => '1'))));
143 curl_setopt($ch, CURLOPT_URL, AT_SHINDIG_URL.'/gadgets/metadata');
144 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
145 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
146 curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
147 curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
148 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);
149 curl_setopt($ch, CURLOPT_TIMEOUT, 20);
150 curl_setopt($ch, CURLOPT_POST, 1);
151 curl_setopt($ch, CURLOPT_POSTFIELDS, 'request=' . urlencode($request));
152 $content = @curl_exec($ch);
153 return json_decode($content);
158 * Add application perferences into the table.
159 * @param int member id
160 * @param string hash's key, usually the name of the application
161 * @param string hash's value, contains key, value, and st.
162 * @return true(1) if the perference has been updated, false(0) otherwise.
164 function setApplicationSettings($member_id, $key, $value){
165 global $addslashes, $db;
168 $member_id = intval($member_id);
169 $key = $addslashes($key);
170 $value = $addslashes($value);
172 $sql = 'INSERT INTO '.TABLE_PREFIX."social_application_settings (application_id, member_id, name, value) VALUES ($app_id, $member_id, '$key', '$value') ON DUPLICATE KEY UPDATE value='$value'";
173 $result = mysql_query($sql, $db);
175 //TODO: Might want to add something here to throw appropriate exceptions
181 * Get member's applications
182 * @param int the member id
184 function getMemberApplications($member_id){
188 $member_id = intval($member_id);
189 $sql = 'SELECT * FROM '.TABLE_PREFIX.'social_members_applications WHERE member_id='.$member_id;
190 $rs = mysql_query($sql, $db);
192 while($row = mysql_fetch_assoc($rs)){
193 $result[] = $row['app_id'];
201 * Get user perferences for this application
204 function getApplicationSettings($member_id){
207 $member_id = intval($member_id);
209 $sql = 'SELECT * FROM '.TABLE_PREFIX."social_application_settings WHERE member_id=$member_id AND application_id=".$this->id;
210 $rs = mysql_query($sql);
212 //loop cause an application can have multiple pairs of key=>value
213 while ($row = mysql_fetch_assoc($rs)){
214 $result[$row['name']] = $row['value'];
232 function getModuleId(){
233 return intval($this->module_id);
236 function getHeight(){
237 if ($this->height==0){
240 return $this->height;
243 function getScreenshot(){
244 return $this->screenshot;
247 function getThumbnail(){
248 //check if this thumbnail link is a relative link
249 $url = parse_url($this->thumbnail);
250 if (!isset($url['scheme']) && !isset($url['host'])){
251 $orig_url = parse_url($this->getUrl());
252 return $orig_url['scheme'].'://'.$orig_url['host'].$this->thumbnail;
254 return $this->thumbnail;
257 function getAuthor(){
258 return $this->author;
261 function getAuthorEmail(){
262 return $this->author_email;
265 function getDescription(){
266 return $this->description;
269 function getSettings(){
270 return unserialize($this->settings);
274 return unserialize($this->views);
278 * Return iframe URL based on the given parameters
279 * @param int owner id
280 * @param string avaiable options are 'profile', 'canvas'
281 * http://code.google.com/apis/orkut/docs/orkutdevguide/orkutdevguide-0.8.html#ops_mode
282 * @param string extra application parameters
285 function getIframeUrl($oid, $view='profile', $appParams=''){
286 //let view=profile as default option
287 if ($view!='profile' && $view!='canvas'){
291 $app_settings = $this->getSettings();
292 $user_settings = $this->getApplicationSettings($_SESSION['member_id']);
294 //retrieve user preferences
295 foreach ($app_settings as $key => $setting) {
297 $value = isset($user_settings[$key]) ? $user_settings[$key] : (isset($setting->default) ? $setting->default : null);
298 if (isset($user_settings[$key])) {
299 unset($user_settings[$key]);
301 //shindig doesn't like ';', it only takes '&' as of Apr 6th, 2009
302 //$prefs .= SEP.'up_' . urlencode($key) . '=' . urlencode($value);
303 $prefs .= '&up_' . urlencode($key) . '=' . urlencode($value);
306 foreach ($user_settings as $name => $value) {
307 // if some keys _are_ set in the db, but not in the gadget metadata, we still parse them on the url
308 // (the above loop unsets the entries that matched
309 if (! empty($value) && ! isset($appParams[$name])) {
310 //shindig doesn't like ';', it only takes '&' as of Apr 6th, 2009
311 //$prefs .= SEP.'up_' . urlencode($name) . '=' . urlencode($value);
312 $prefs .= '&up_' . urlencode($name) . '=' . urlencode($value);
316 //generate security token
317 $securityToken = BasicSecurityToken::createFromValues(($oid > 0?$oid:$_SESSION['member_id']), // owner
318 $_SESSION['member_id'], // viewer
319 $this->getId(), // app id
320 'default', // domain key, shindig will check for php/config/<domain>.php for container specific configuration
321 urlencode($this->getUrl()), // app url
322 $this->getModuleId());// mod id
323 //debug($securityToken);
324 $url = AT_SHINDIG_URL.'/gadgets/ifr?'
326 . "&container=default"
327 . "&viewer=". $_SESSION['member_id']
329 . "&aid=" . $this->getId() //application id
330 . "&mid=" . $this->getModuleId() //not sure what mod_id is
333 . "&view=" . $view //canvas for this big thing, should be a variable
334 . "&parent=" . urlencode("http://" . $_SERVER['HTTP_HOST']) . $prefs . (isset($appParams) ? '&view-params=' . urlencode($appParams) : '')
335 . "&st=" . urlencode(base64_encode($securityToken->toSerialForm()))
336 . "&v=" . $this->getVersion()
337 . "&url=" . urlencode($this->getUrl()) . "#rpctoken=" . rand(0, getrandmax());
342 function getVersion(){
348 * Retrieve all information about this gadget and save it in the object
351 function getApplicationPrefs(){
354 $sql = 'SELECT * FROM '.TABLE_PREFIX.'social_applications WHERE id='.$this->id;
355 $rs = mysql_query($sql);
358 $row = mysql_fetch_assoc($rs);
360 $this->url = $row['url'];
361 $this->title = $row['title'];
362 $this->height = $row['height'];
363 $this->screenshot = $row['screenshot'];
364 $this->thumbnail = $row['thumbnail'];
365 $this->author = $row['author'];
366 $this->author_email = $row['author_email'];
367 $this->description = $row['description'];
368 $this->settings = $row['settings'];
369 $this->views = $row['views'];
370 $this->last_updated = $row['last_updated'];
375 * Delete an application
377 function deleteApplication(){
380 //delete application mapping
381 $sql = 'DELETE FROM '.TABLE_PREFIX.'social_members_applications WHERE application_id='.$this->id.' AND member_id='.$_SESSION['member_id'];
382 $rs = mysql_query($sql);
384 //delete application data?
385 $sql = 'DELETE FROM '.TABLE_PREFIX.'social_application_settings WHERE application_id='.$this->id.' AND member_id='.$_SESSION['member_id'];
386 $rs = mysql_query($sql);
388 //delete application settings
389 $home_settings = $this->getHomeDisplaySettings();
390 if (isset($home_settings[$this->id])){
391 unset($home_settings[$this->id]);
392 $this->setHomeDisplaySettings($home_settings);