PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Application/Models/people/people.php

https://github.com/terasa/import_repo
PHP | 313 lines | 247 code | 19 blank | 47 comment | 26 complexity | 77e4088cfa2e82b0f3e6260e3f32e2fc 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 peopleModel extends Model {
  21. public $cachable = array('is_friend', 'get_person', 'get_person_info', 'get_friends', 'get_friends_count', 'get_friend_requests');
  22. // persons table supported fields.
  23. public $supported_fields = array('id','email','password','about_me','age','children','date_of_birth','drinker',
  24. 'ethnicity','fashion','gender','happiest_when','humor','job_interests','living_arrangement','looking_for',
  25. 'nickname','pets','political_views','profile_song','profile_url','profile_video','relationship_status',
  26. 'religion','romance','scared_of','sexual_orientation','smoker','status','thumbnail_url','time_zone',
  27. 'first_name','last_name','uploaded_size');
  28. public function load_is_friend($person_id, $friend_id) {
  29. global $db;
  30. $this->add_dependency('people', $person_id);
  31. $this->add_dependency('people', $friend_id);
  32. $person_id = $db->addslashes($person_id);
  33. $friend_id = $db->addslashes($friend_id);
  34. $res = $db->query("select * from friends where (person_id = $person_id and friend_id = $friend_id) or (person_id = $friend_id and friend_id = $person_id)");
  35. // return 0 instead of false, not to trip up the caching layer (who does a binary === false compare on data, so 0 == false but not === false)
  36. return $db->num_rows($res) != 0 ? true : 0;
  37. }
  38. public function remove_friend($person_id, $friend_id) {
  39. global $db;
  40. $this->invalidate_dependency('people', $person_id);
  41. $this->invalidate_dependency('people', $friend_id);
  42. $person_id = $db->addslashes($person_id);
  43. $friend_id = $db->addslashes($friend_id);
  44. $res = $db->query("delete from friends where (person_id = $person_id and friend_id = $friend_id) or (person_id = $friend_id and friend_id = $person_id)");
  45. return $db->affected_rows($res) != 0;
  46. }
  47. public function set_profile_photo($id, $url) {
  48. global $db;
  49. $this->invalidate_dependency('people', $id);
  50. $id = $db->addslashes($id);
  51. $url = $db->addslashes($url);
  52. $db->query("update persons set thumbnail_url = '$url' where id = $id");
  53. }
  54. public function save_person($id, $person) {
  55. global $db;
  56. $this->invalidate_dependency('people', $id);
  57. $id = $db->addslashes($id);
  58. foreach ($person as $key => $val) {
  59. if (in_array($key, $this->supported_fields)) {
  60. if ($val == '-') {
  61. $updates[] = "`" . $db->addslashes($key) . "` = null";
  62. } else {
  63. $updates[] = "`" . $db->addslashes($key) . "` = '" . $db->addslashes($val) . "'";
  64. }
  65. }
  66. }
  67. if (count($updates)) {
  68. $query = "update persons set " . implode(', ', $updates) . " where id = $id";
  69. $db->query($query);
  70. }
  71. }
  72. // if extended = true, it also queries all child tables
  73. // defaults to false since its a hell of a presure on the database.
  74. // remove once we add some proper caching
  75. public function load_get_person($id, $extended = false) {
  76. global $db;
  77. $this->add_dependency('people', $id);
  78. $id = $db->addslashes($id);
  79. $res = $db->query("select * from persons where id = $id");
  80. if (! $db->num_rows($res)) {
  81. throw new Exception("Invalid person");
  82. }
  83. $person = $db->fetch_array($res, MYSQLI_ASSOC);
  84. //TODO missing : person_languages_spoken, need to add table with ISO 639-1 codes
  85. $tables_addresses = array('person_addresses', 'person_current_location');
  86. $tables_organizations = array('person_jobs', 'person_schools');
  87. $tables = array('person_activities', 'person_body_type', 'person_books', 'person_cars',
  88. 'person_emails', 'person_food', 'person_heroes', 'person_movies',
  89. 'person_interests', 'person_music', 'person_phone_numbers', 'person_quotes',
  90. 'person_sports', 'person_tags', 'person_turn_offs', 'person_turn_ons',
  91. 'person_tv_shows', 'person_urls');
  92. foreach ($tables as $table) {
  93. $person[$table] = array();
  94. $res = $db->query("select * from $table where person_id = $id");
  95. while ($data = $db->fetch_array($res, MYSQLI_ASSOC)) {
  96. $person[$table][] = $data;
  97. }
  98. }
  99. foreach ($tables_addresses as $table) {
  100. $res = $db->query("select addresses.* from addresses, $table where $table.person_id = $id and addresses.id = $table.address_id");
  101. while ($data = $db->fetch_array($res)) {
  102. $person[$table][] = $data;
  103. }
  104. }
  105. foreach ($tables_organizations as $table) {
  106. $res = $db->query("select organizations.* from organizations, $table where $table.person_id = $id and organizations.id = $table.organization_id");
  107. while ($data = $db->fetch_array($res)) {
  108. $person[$table][] = $data;
  109. }
  110. }
  111. return $person;
  112. }
  113. /*
  114. * doing a select * on a large table is way to IO and memory expensive to do
  115. * for all friends/people on a page. So this gets just the basic fields required
  116. * to build a person expression:
  117. * id, email, first_name, last_name, thumbnail_url and profile_url
  118. */
  119. public function load_get_person_info($id) {
  120. global $db;
  121. $this->add_dependency('people', $id);
  122. $id = $db->addslashes($id);
  123. $res = $db->query("select id, email, first_name, last_name, thumbnail_url, profile_url from persons where id = $id");
  124. if (! $db->num_rows($res)) {
  125. throw new Exception("Invalid person");
  126. }
  127. return $db->fetch_array($res, MYSQLI_ASSOC);
  128. }
  129. public function load_get_friends($id, $limit = false) {
  130. global $db;
  131. $this->add_dependency('people', $id);
  132. $ret = array();
  133. $limit = $limit ? ' limit ' . $db->addslashes($limit) : '';
  134. $person_id = $db->addslashes($id);
  135. $res = $db->query("select person_id, friend_id from friends where person_id = $person_id or friend_id = $person_id $limit");
  136. while (list($p1, $p2) = $db->fetch_row($res)) {
  137. // friend requests are made both ways, so find the 'friend' in the pair
  138. $friend = $p1 != $person_id ? $p1 : $p2;
  139. $ret[$friend] = $this->get_person_info($friend);
  140. }
  141. return $ret;
  142. }
  143. public function load_get_friends_count($id) {
  144. global $db;
  145. $this->add_dependency('people', $id);
  146. $ret = array();
  147. $person_id = $db->addslashes($id);
  148. $res = $db->query("select count(person_id) from friends where person_id = $person_id or friend_id = $person_id");
  149. list($ret) = $db->fetch_row($res);
  150. return $ret;
  151. }
  152. public function add_friend_request($id, $friend_id) {
  153. global $db;
  154. try {
  155. $this->invalidate_dependency('friendrequest', $id);
  156. $this->invalidate_dependency('friendrequest', $friend_id);
  157. $person_id = $db->addslashes($id);
  158. $friend_id = $db->addslashes($friend_id);
  159. $db->query("insert into friend_requests values ($person_id, $friend_id)");
  160. } catch (DBException $e) {
  161. return false;
  162. }
  163. return true;
  164. }
  165. public function accept_friend_request($id, $friend_id) {
  166. global $db;
  167. $person_id = $db->addslashes($id);
  168. $friend_id = $db->addslashes($friend_id);
  169. try {
  170. // double check if a friend request actually exists (reversed friend/person since the request came from the other party)
  171. $db->query("delete from friend_requests where person_id = $friend_id and friend_id = $person_id");
  172. // -1 = sql error, 0 = no request was made, so can't accept it since the other party never gave permission
  173. if ($db->affected_rows() < 1) {
  174. die("couldnt delete friend request, means there was none?");
  175. return false;
  176. }
  177. // make sure there's not already a connection between the two the other way around
  178. $res = $db->query("select friend_id from friends where person_id = $friend_id and friend_id = $person_id");
  179. if ($db->num_rows($res)) {
  180. die("the relation already exists the other way around,bailing");
  181. return false;
  182. }
  183. $db->query("insert into friends values ($person_id, $friend_id)");
  184. //FIXME quick hack to put in befriending activities, move this to its own class/function soon
  185. // We want to create the friend activities on both people so we do this twice
  186. $time = $_SERVER['REQUEST_TIME'];
  187. foreach (array($friend_id => $person_id, $person_id => $friend_id) as $key => $val) {
  188. $res = $db->query("select concat(first_name, ' ', last_name) from persons where id = $key");
  189. list($name) = $db->fetch_row($res);
  190. $db->query("insert into activities (person_id, app_id, title, body, created) values ($val, 0, 'and <a href=\"/profile/$key\" rel=\"friend\">$name</a> are now friends.', '', $time)");
  191. $this->invalidate_dependency('activities', $key);
  192. }
  193. } catch (DBException $e) {
  194. die("sql error: " . $e->getMessage());
  195. return false;
  196. }
  197. $this->invalidate_dependency('friendrequest', $id);
  198. $this->invalidate_dependency('friendrequest', $friend_id);
  199. $this->invalidate_dependency('people', $id);
  200. $this->invalidate_dependency('people', $friend_id);
  201. return true;
  202. }
  203. public function reject_friend_request($id, $friend_id) {
  204. global $db;
  205. $this->invalidate_dependency('friendrequest', $id);
  206. $this->invalidate_dependency('friendrequest', $friend_id);
  207. $person_id = $db->addslashes($id);
  208. $friend_id = $db->addslashes($friend_id);
  209. try {
  210. $db->query("delete from friend_requests where person_id = $friend_id and friend_id = $person_id");
  211. } catch (DBException $e) {
  212. return false;
  213. }
  214. return true;
  215. }
  216. public function load_get_friend_requests($id) {
  217. global $db;
  218. $this->add_dependency('friendrequest', $id);
  219. $requests = array();
  220. $friend_id = $db->addslashes($id);
  221. $res = $db->query("select person_id from friend_requests where friend_id = $friend_id");
  222. while (list($friend_id) = $db->fetch_row($res)) {
  223. $requests[$friend_id] = $this->get_person($friend_id, false);
  224. }
  225. return $requests;
  226. }
  227. public function search($name) {
  228. global $db;
  229. $name = $db->addslashes($name);
  230. $ret = array();
  231. $res = $db->query("select id, email, first_name, last_name from persons where concat(first_name, ' ', last_name) like '%$name%' or email like '%$name%'");
  232. while ($row = $db->fetch_array($res, MYSQLI_ASSOC)) {
  233. $ret[] = $row;
  234. }
  235. return $ret;
  236. }
  237. /*
  238. * get person info, need set field which we need.
  239. */
  240. public function get_person_fields($id, $fields) {
  241. global $db;
  242. $id = $db->addslashes($id);
  243. foreach ($fields as $val) {
  244. if (in_array($val, $this->supported_fields)) {
  245. $fields_adds[] = "`" . $db->addslashes($val) . "`";
  246. }
  247. }
  248. $res = $db->query("select " . implode(', ', $fields_adds) . " from persons where id = $id");
  249. if (! $db->num_rows($res)) {
  250. throw new Exception("Invalid person");
  251. }
  252. return $db->fetch_array($res, MYSQLI_ASSOC);
  253. }
  254. /*
  255. * set person info, need set field which we need.
  256. */
  257. public function set_person_fields($id, $fields) {
  258. global $db;
  259. $id = $db->addslashes($id);
  260. foreach ($fields as $key => $val) {
  261. if (in_array($key, $this->supported_fields)) {
  262. if (is_null($val)) {
  263. $updates[] = "`" . $db->addslashes($key) . "` = null";
  264. } else {
  265. $updates[] = "`" . $db->addslashes($key) . "` = '" . $db->addslashes($val) . "'";
  266. }
  267. }
  268. }
  269. if (count($updates)) {
  270. $query = "update persons set " . implode(', ', $updates) . " where id = $id";
  271. $db->query($query);
  272. return $id;
  273. }
  274. }
  275. /*
  276. * if we can promise our code is safe, we can do it.
  277. * update media table use literal word, so do not escape update code.
  278. * for example update albums set uploaded_size = uploaded_size+1000; it will be easy.
  279. */
  280. public function literal_set_person_fields($id, $fields) {
  281. global $db;
  282. $id = $db->addslashes($id);
  283. foreach ($fields as $key => $val) {
  284. if (in_array($key, $this->supported_fields)) {
  285. $updates[] = "`" . $db->addslashes($key) . "` = " . $val ;
  286. }
  287. }
  288. if (count($updates)) {
  289. $query = "update persons set " . implode(', ', $updates) . " where id = $id";
  290. $db->query($query);
  291. return $id;
  292. }
  293. }
  294. }