PageRenderTime 39ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/Application/Models/applications/applications.php

https://github.com/terasa/import_repo
PHP | 276 lines | 229 code | 12 blank | 35 comment | 22 complexity | 7387c862d3858f2a6d9c07096300ef5d MD5 | raw file
  1. <?php
  2. /**
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. class applicationsModel extends Model {
  21. public $cachable = array('get_person_applications', 'get_all_applications', 'get_application_prefs',
  22. 'get_person_application', 'get_application_by_id', 'get_application');
  23. public function load_get_person_applications($id) {
  24. global $db;
  25. $this->add_dependency('person_applications', $id);
  26. $this->add_dependency('person_application_prefs', $id);
  27. $ret = array();
  28. include_once PartuzaConfig::get('models_root') . "/oauth/oauth.php";
  29. $oauth = new oauthModel();
  30. $id = $db->addslashes($id);
  31. $res = $db->query("select applications.*, person_applications.id as mod_id from person_applications, applications where person_applications.person_id = $id and applications.id = person_applications.application_id");
  32. while ($row = $db->fetch_array($res, MYSQLI_ASSOC)) {
  33. $this->add_dependency('applications', $row['id']);
  34. $row['user_prefs'] = $this->get_application_prefs($id, $row['id']);
  35. $row['oauth'] = $oauth->get_gadget_consumer($row['id']);
  36. $ret[] = $row;
  37. }
  38. return $ret;
  39. }
  40. public function load_get_all_applications() {
  41. global $db;
  42. include_once PartuzaConfig::get('models_root') . "/oauth/oauth.php";
  43. $oauth = new oauthModel();
  44. $ret = array();
  45. $res = $db->query("select * from applications where approved = 'Y' order by directory_title, title");
  46. while ($row = $db->fetch_array($res, MYSQLI_ASSOC)) {
  47. $this->add_dependency('applications', $row['id']);
  48. $row['user_prefs'] = array();
  49. $row['oauth'] = $oauth->get_gadget_consumer($row['id']);
  50. $ret[] = $row;
  51. }
  52. return $ret;
  53. }
  54. public function set_application_pref($person_id, $app_id, $key, $value) {
  55. global $db;
  56. $this->invalidate_dependency('person_application_prefs', $person_id);
  57. $person_id = $db->addslashes($person_id);
  58. $app_id = $db->addslashes($app_id);
  59. $key = $db->addslashes($key);
  60. $value = $db->addslashes($value);
  61. $db->query("insert into application_settings (application_id, person_id, name, value) values ($app_id, $person_id, '$key', '$value')
  62. on duplicate key update value = '$value'");
  63. }
  64. public function load_get_application_prefs($person_id, $app_id) {
  65. global $db;
  66. $this->add_dependency('person_application_prefs', $person_id);
  67. $person_id = $db->addslashes($person_id);
  68. $app_id = $db->addslashes($app_id);
  69. $prefs = array();
  70. $res = $db->query("select name, value from application_settings where application_id = $app_id and person_id = $person_id");
  71. while (list($name, $value) = $db->fetch_row($res)) {
  72. $prefs[$name] = $value;
  73. }
  74. return $prefs;
  75. }
  76. public function load_get_person_application($person_id, $app_id, $mod_id) {
  77. global $db;
  78. $this->add_dependency('person_application_prefs', $person_id);
  79. $this->add_dependency('person_applications', $person_id);
  80. $this->add_dependency('applications', $app_id);
  81. $ret = array();
  82. $person_id = $db->addslashes($person_id);
  83. $app_id = $db->addslashes($app_id);
  84. $mod_id = $db->addslashes($mod_id);
  85. $res = $db->query("select url from applications where id = $app_id");
  86. if ($db->num_rows($res)) {
  87. list($app_url) = $db->fetch_row($res);
  88. $ret = $this->get_application($app_url);
  89. $ret['mod_id'] = $mod_id;
  90. $ret['user_prefs'] = $this->get_application_prefs($person_id, $app_id);
  91. }
  92. return $ret;
  93. }
  94. private function fetch_gadget_metadata($app_url, $securityToken) {
  95. foreach (array('profile', 'home', 'canvas', 'default') as $view) {
  96. $request = json_encode(array(
  97. 'context' => array('country' => 'US', 'language' => 'en', 'view' => $view, 'container' => 'partuza'),
  98. 'gadgets' => array(array('url' => $app_url, 'moduleId' => '1'))));
  99. $ch = curl_init();
  100. curl_setopt($ch, CURLOPT_URL, PartuzaConfig::get('gadget_server') . '/gadgets/metadata?st=' . urlencode(base64_encode($securityToken->toSerialForm())));
  101. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  102. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  103. curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  104. curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
  105. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);
  106. curl_setopt($ch, CURLOPT_TIMEOUT, 20);
  107. curl_setopt($ch, CURLOPT_POST, 1);
  108. curl_setopt($ch, CURLOPT_POSTFIELDS, 'request=' . urlencode($request));
  109. $content = @curl_exec($ch);
  110. curl_close($ch);
  111. $ret = json_decode($content);
  112. if (!isset($ret->gadgets[0]->errors[0])) {
  113. break;
  114. }
  115. }
  116. return $ret;
  117. }
  118. public function load_get_application_by_id($id) {
  119. global $db;
  120. $this->add_dependency('person_applications', $id);
  121. $this->add_dependency('applications', $id);
  122. $id = $db->addslashes($id);
  123. $res = $db->query("select url from applications where id = $id");
  124. if ($db->num_rows($res)) {
  125. list($url) = $db->fetch_row($res);
  126. return $this->get_application($url);
  127. }
  128. return false;
  129. }
  130. // This function either returns a valid applications record or
  131. // the error (string) that occured in ['error'].
  132. // After this function you can assume there is a valid, and up to date gadget metadata
  133. // record in the database.
  134. public function load_get_application($app_url) {
  135. global $db;
  136. $error = false;
  137. $info = array();
  138. // see if we have up-to-date info in our db. Cut-off time is 1 day (aka refresh module info once a day)
  139. $time = $_SERVER['REQUEST_TIME'] - (24 * 60 * 60);
  140. $url = $db->addslashes($app_url);
  141. $res = $db->query("select * from applications where url = '$url' and modified > $time");
  142. if ($db->num_rows($res)) {
  143. // we have an entry with up-to-date info
  144. $info = $db->fetch_array($res, MYSQLI_ASSOC);
  145. } else {
  146. // Either we dont have a record of this module or its out of date, so we retrieve the app meta data.
  147. // Create a fake security token so that gadgets with signed preloading don't fail to load
  148. $securityToken = BasicSecurityToken::createFromValues(1, 1, 0, PartuzaConfig::get('container'), urlencode($app_url), 0);
  149. $response = $this->fetch_gadget_metadata($app_url, $securityToken);
  150. if (! is_object($response) && ! is_array($response)) {
  151. // invalid json object, something bad happened on the shindig metadata side.
  152. $error = 'An error occured while retrieving the gadget information';
  153. } else {
  154. // valid response, process it
  155. $gadget = $response->gadgets[0];
  156. if (isset($gadget->errors) && ! empty($gadget->errors[0])) {
  157. // failed to retrieve gadget, or failed parsing it
  158. $error = $gadget->errors[0];
  159. } else {
  160. // retrieved and parsed gadget ok, store it in db
  161. $info['url'] = $db->addslashes($gadget->url);
  162. $info['title'] = isset($gadget->title) ? $gadget->title : '';
  163. $info['directory_title'] = isset($gadget->directoryTitle) ? $gadget->directoryTitle : '';
  164. $info['height'] = isset($gadget->height) ? $gadget->height : '';
  165. $info['screenshot'] = isset($gadget->screenshot) ? $gadget->screenshot : '';
  166. $info['thumbnail'] = isset($gadget->thumbnail) ? $gadget->thumbnail : '';
  167. $info['author'] = isset($gadget->author) ? $gadget->author : '';
  168. $info['author_email'] = isset($gadget->authorEmail) ? $gadget->authorEmail : '';
  169. $info['description'] = isset($gadget->description) ? $gadget->description : '';
  170. $info['settings'] = isset($gadget->userPrefs) ? serialize($gadget->userPrefs) : '';
  171. $info['views'] = isset($gadget->views) ? serialize($gadget->views) : '';
  172. if ($gadget->scrolling == 'true') {
  173. $gadget->scrolling = 1;
  174. }
  175. $info['scrolling'] = ! empty($gadget->scrolling) ? $gadget->scrolling : '0';
  176. $info['height'] = ! empty($gadget->height) ? $gadget->height : '0';
  177. // extract the version from the iframe url
  178. $iframe_url = $gadget->iframeUrl;
  179. $iframe_params = array();
  180. parse_str($iframe_url, $iframe_params);
  181. $info['version'] = isset($iframe_params['v']) ? $iframe_params['v'] : '';
  182. $info['modified'] = $_SERVER['REQUEST_TIME'];
  183. // Insert new application into our db, or if it exists (but had expired info) update the meta data
  184. $db->query("insert into applications
  185. (id, url, title, directory_title, screenshot, thumbnail, author, author_email, description, settings, views, version, height, scrolling, modified)
  186. values
  187. (
  188. 0,
  189. '" . $db->addslashes($info['url']) . "',
  190. '" . $db->addslashes($info['title']) . "',
  191. '" . $db->addslashes($info['directory_title']) . "',
  192. '" . $db->addslashes($info['screenshot']) . "',
  193. '" . $db->addslashes($info['thumbnail']) . "',
  194. '" . $db->addslashes($info['author']) . "',
  195. '" . $db->addslashes($info['author_email']) . "',
  196. '" . $db->addslashes($info['description']) . "',
  197. '" . $db->addslashes($info['settings']) . "',
  198. '" . $db->addslashes($info['views']) . "',
  199. '" . $db->addslashes($info['version']) . "',
  200. '" . $db->addslashes($info['height']) . "',
  201. '" . $db->addslashes($info['scrolling']) . "',
  202. '" . $db->addslashes($info['modified']) . "'
  203. ) on duplicate key update
  204. url = '" . $db->addslashes($info['url']) . "',
  205. title = '" . $db->addslashes($info['title']) . "',
  206. directory_title = '" . $db->addslashes($info['directory_title']) . "',
  207. screenshot = '" . $db->addslashes($info['screenshot']) . "',
  208. thumbnail = '" . $db->addslashes($info['thumbnail']) . "',
  209. author = '" . $db->addslashes($info['author']) . "',
  210. author_email = '" . $db->addslashes($info['author_email']) . "',
  211. description = '" . $db->addslashes($info['description']) . "',
  212. settings = '" . $db->addslashes($info['settings']) . "',
  213. views = '" . $db->addslashes($info['views']) . "',
  214. version = '" . $db->addslashes($info['version']) . "',
  215. height = '" . $db->addslashes($info['height']) . "',
  216. scrolling = '" . $db->addslashes($info['scrolling']) . "',
  217. modified = '" . $db->addslashes($info['modified']) . "'
  218. ");
  219. $res = $db->query("select id from applications where url = '" . $db->addslashes($info['url']) . "'");
  220. if (! $db->num_rows($res)) {
  221. $error = "Could not store application in registry";
  222. } else {
  223. list($id) = $db->fetch_row($res);
  224. $info['id'] = $id;
  225. $this->invalidate_dependency('applications', $id);
  226. }
  227. }
  228. }
  229. }
  230. if (! $error) {
  231. $this->add_dependency('applications', $info['id']);
  232. }
  233. $info['error'] = $error;
  234. return $info;
  235. }
  236. public function add_application($person_id, $app_url) {
  237. global $db;
  238. $mod_id = false;
  239. $app = $this->get_application($app_url);
  240. $app_id = isset($app['id']) ? $app['id'] : false;
  241. $error = $app['error'];
  242. if ($app_id && ! $error) {
  243. // we now have a valid gadget record in $info, with no errors occured, proceed to add it to the person
  244. // keep in mind a person -could- have two the same apps on his page (though with different module_id's) so no
  245. // unique check is done.
  246. $person_id = $db->addslashes($person_id);
  247. $app_id = $db->addslashes($app_id);
  248. $db->query("insert into person_applications (id, person_id, application_id) values (0, $person_id, $app_id)");
  249. $mod_id = $db->insert_id();
  250. $this->invalidate_dependency('person_applications', $person_id);
  251. $this->invalidate_dependency('person_application_prefs', $person_id);
  252. }
  253. return array('app_id' => $app_id, 'mod_id' => $mod_id, 'error' => $app['error']);
  254. }
  255. public function remove_application($person_id, $app_id, $mod_id) {
  256. global $db;
  257. $person_id = $db->addslashes($person_id);
  258. $app_id = $db->addslashes($app_id);
  259. $mod_id = $db->addslashes($mod_id);
  260. $db->query("delete from person_applications where id = $mod_id and person_id = $person_id and application_id = $app_id");
  261. $this->invalidate_dependency('person_applications', $person_id);
  262. return ($db->affected_rows() != 0);
  263. }
  264. }