Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

315 lines
9.3 KiB

  1. <?php
  2. function poco_init(&$a) {
  3. require_once("include/bbcode.php");
  4. $system_mode = false;
  5. if(intval(get_config('system','block_public')) || (get_config('system','block_local_dir')))
  6. http_status_exit(401);
  7. if($a->argc > 1) {
  8. $user = notags(trim($a->argv[1]));
  9. }
  10. if(! x($user)) {
  11. $c = q("SELECT * FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1");
  12. if(! count($c))
  13. http_status_exit(401);
  14. $system_mode = true;
  15. }
  16. $format = (($_GET['format']) ? $_GET['format'] : 'json');
  17. $justme = false;
  18. $global = false;
  19. if($a->argc > 1 && $a->argv[1] === '@global') {
  20. $global = true;
  21. $update_limit = date("Y-m-d H:i:s", time() - 30 * 86400);
  22. }
  23. if($a->argc > 2 && $a->argv[2] === '@me')
  24. $justme = true;
  25. if($a->argc > 3 && $a->argv[3] === '@all')
  26. $justme = false;
  27. if($a->argc > 3 && $a->argv[3] === '@self')
  28. $justme = true;
  29. if($a->argc > 4 && intval($a->argv[4]) && $justme == false)
  30. $cid = intval($a->argv[4]);
  31. if(!$system_mode AND !$global) {
  32. $r = q("SELECT `user`.*,`profile`.`hide-friends` from user left join profile on `user`.`uid` = `profile`.`uid`
  33. where `user`.`nickname` = '%s' and `profile`.`is-default` = 1 limit 1",
  34. dbesc($user)
  35. );
  36. if(! count($r) || $r[0]['hidewall'] || $r[0]['hide-friends'])
  37. http_status_exit(404);
  38. $user = $r[0];
  39. }
  40. if($justme)
  41. $sql_extra = " AND `contact`.`self` = 1 ";
  42. // else
  43. // $sql_extra = " AND `contact`.`self` = 0 ";
  44. if($cid)
  45. $sql_extra = sprintf(" AND `contact`.`id` = %d ",intval($cid));
  46. if(x($_GET,'updatedSince'))
  47. $update_limit = date("Y-m-d H:i:s",strtotime($_GET['updatedSince']));
  48. if ($global) {
  49. $r = q("SELECT count(*) AS `total` FROM `gcontact` WHERE `updated` >= '%s' AND `last_contact` >= `last_failure` AND `network` IN ('%s')",
  50. dbesc($update_limit),
  51. dbesc(NETWORK_DFRN)
  52. );
  53. } elseif($system_mode) {
  54. $r = q("SELECT count(*) AS `total` FROM `contact` WHERE `self` = 1 AND `network` IN ('%s', '%s', '%s', '%s', '')
  55. AND `success_update` >= `failure_update`
  56. AND `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) ",
  57. dbesc(NETWORK_DFRN),
  58. dbesc(NETWORK_DIASPORA),
  59. dbesc(NETWORK_OSTATUS),
  60. dbesc(NETWORK_STATUSNET)
  61. );
  62. } else {
  63. $r = q("SELECT count(*) AS `total` FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
  64. AND `success_update` >= `failure_update` AND `network` IN ('%s', '%s', '%s', '%s', '') $sql_extra",
  65. intval($user['uid']),
  66. dbesc(NETWORK_DFRN),
  67. dbesc(NETWORK_DIASPORA),
  68. dbesc(NETWORK_OSTATUS),
  69. dbesc(NETWORK_STATUSNET)
  70. );
  71. }
  72. if(count($r))
  73. $totalResults = intval($r[0]['total']);
  74. else
  75. $totalResults = 0;
  76. $startIndex = intval($_GET['startIndex']);
  77. if(! $startIndex)
  78. $startIndex = 0;
  79. $itemsPerPage = ((x($_GET,'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
  80. if ($global) {
  81. $r = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND `network` IN ('%s') AND `last_contact` >= `last_failure` LIMIT %d, %d",
  82. dbesc($update_limit),
  83. dbesc(NETWORK_DFRN),
  84. intval($startIndex),
  85. intval($itemsPerPage)
  86. );
  87. } elseif($system_mode) {
  88. $r = q("SELECT `contact`.*, `profile`.`about` AS `pabout`, `profile`.`locality` AS `plocation`, `profile`.`pub_keywords`, `profile`.`gender` AS `pgender`,
  89. `profile`.`address` AS `paddress`, `profile`.`region` AS `pregion`, `profile`.`postal-code` AS `ppostalcode`, `profile`.`country-name` AS `pcountry`
  90. FROM `contact` INNER JOIN `profile` ON `profile`.`uid` = `contact`.`uid`
  91. WHERE `self` = 1 AND `network` IN ('%s', '%s', '%s', '%s', '') AND `profile`.`is-default`
  92. AND `contact`.`success_update` >= `contact`.`failure_update`
  93. AND `contact`.`uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
  94. dbesc(NETWORK_DFRN),
  95. dbesc(NETWORK_DIASPORA),
  96. dbesc(NETWORK_OSTATUS),
  97. dbesc(NETWORK_STATUSNET),
  98. intval($startIndex),
  99. intval($itemsPerPage)
  100. );
  101. } else {
  102. $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
  103. AND `success_update` >= `failure_update` AND `network` IN ('%s', '%s', '%s', '%s', '') $sql_extra LIMIT %d, %d",
  104. intval($user['uid']),
  105. dbesc(NETWORK_DFRN),
  106. dbesc(NETWORK_DIASPORA),
  107. dbesc(NETWORK_OSTATUS),
  108. dbesc(NETWORK_STATUSNET),
  109. intval($startIndex),
  110. intval($itemsPerPage)
  111. );
  112. }
  113. $ret = array();
  114. if(x($_GET,'sorted'))
  115. $ret['sorted'] = false;
  116. if(x($_GET,'filtered'))
  117. $ret['filtered'] = false;
  118. if(x($_GET,'updatedSince') AND !$global)
  119. $ret['updatedSince'] = false;
  120. $ret['startIndex'] = (int) $startIndex;
  121. $ret['itemsPerPage'] = (int) $itemsPerPage;
  122. $ret['totalResults'] = (int) $totalResults;
  123. $ret['entry'] = array();
  124. $fields_ret = array(
  125. 'id' => false,
  126. 'displayName' => false,
  127. 'urls' => false,
  128. 'updated' => false,
  129. 'preferredUsername' => false,
  130. 'photos' => false,
  131. 'aboutMe' => false,
  132. 'currentLocation' => false,
  133. 'network' => false,
  134. 'gender' => false,
  135. 'tags' => false,
  136. 'address' => false,
  137. 'generation' => false
  138. );
  139. if((! x($_GET,'fields')) || ($_GET['fields'] === '@all'))
  140. foreach($fields_ret as $k => $v)
  141. $fields_ret[$k] = true;
  142. else {
  143. $fields_req = explode(',',$_GET['fields']);
  144. foreach($fields_req as $f)
  145. $fields_ret[trim($f)] = true;
  146. }
  147. if(is_array($r)) {
  148. if(count($r)) {
  149. foreach($r as $rr) {
  150. if (!isset($rr['generation'])) {
  151. if ($global)
  152. $rr['generation'] = 3;
  153. elseif ($system_mode)
  154. $rr['generation'] = 1;
  155. else
  156. $rr['generation'] = 2;
  157. }
  158. if (($rr['about'] == "") AND isset($rr['pabout']))
  159. $rr['about'] = $rr['pabout'];
  160. if ($rr['location'] == "") {
  161. if (isset($rr['plocation']))
  162. $rr['location'] = $rr['plocation'];
  163. if (isset($rr['pregion']) AND ($rr['pregion'] != "")) {
  164. if ($rr['location'] != "")
  165. $rr['location'] .= ", ";
  166. $rr['location'] .= $rr['pregion'];
  167. }
  168. if (isset($rr['pcountry']) AND ($rr['pcountry'] != "")) {
  169. if ($rr['location'] != "")
  170. $rr['location'] .= ", ";
  171. $rr['location'] .= $rr['pcountry'];
  172. }
  173. }
  174. if (($rr['gender'] == "") AND isset($rr['pgender']))
  175. $rr['gender'] = $rr['pgender'];
  176. if (($rr['keywords'] == "") AND isset($rr['pub_keywords']))
  177. $rr['keywords'] = $rr['pub_keywords'];
  178. $entry = array();
  179. if($fields_ret['id'])
  180. $entry['id'] = (int)$rr['id'];
  181. if($fields_ret['displayName'])
  182. $entry['displayName'] = $rr['name'];
  183. if($fields_ret['aboutMe'])
  184. $entry['aboutMe'] = bbcode($rr['about'], false, false);
  185. if($fields_ret['currentLocation'])
  186. $entry['currentLocation'] = $rr['location'];
  187. if($fields_ret['gender'])
  188. $entry['gender'] = $rr['gender'];
  189. if($fields_ret['generation'])
  190. $entry['generation'] = (int)$rr['generation'];
  191. if($fields_ret['urls']) {
  192. $entry['urls'] = array(array('value' => $rr['url'], 'type' => 'profile'));
  193. if($rr['addr'] && ($rr['network'] !== NETWORK_MAIL))
  194. $entry['urls'][] = array('value' => 'acct:' . $rr['addr'], 'type' => 'webfinger');
  195. }
  196. if($fields_ret['preferredUsername'])
  197. $entry['preferredUsername'] = $rr['nick'];
  198. if($fields_ret['updated']) {
  199. if (!$global) {
  200. $entry['updated'] = $rr['success_update'];
  201. if ($rr['name-date'] > $entry['updated'])
  202. $entry['updated'] = $rr['name-date'];
  203. if ($rr['uri-date'] > $entry['updated'])
  204. $entry['updated'] = $rr['uri-date'];
  205. if ($rr['avatar-date'] > $entry['updated'])
  206. $entry['updated'] = $rr['avatar-date'];
  207. } else
  208. $entry['updated'] = $rr['updated'];
  209. $entry['updated'] = date("c", strtotime($entry['updated']));
  210. }
  211. if($fields_ret['photos'])
  212. $entry['photos'] = array(array('value' => $rr['photo'], 'type' => 'profile'));
  213. if($fields_ret['network']) {
  214. $entry['network'] = $rr['network'];
  215. if ($entry['network'] == NETWORK_STATUSNET)
  216. $entry['network'] = NETWORK_OSTATUS;
  217. if (($entry['network'] == "") AND ($rr['self']))
  218. $entry['network'] = NETWORK_DFRN;
  219. }
  220. if($fields_ret['tags']) {
  221. $tags = str_replace(","," ",$rr['keywords']);
  222. $tags = explode(" ", $tags);
  223. $cleaned = array();
  224. foreach ($tags as $tag) {
  225. $tag = trim(strtolower($tag));
  226. if ($tag != "")
  227. $cleaned[] = $tag;
  228. }
  229. $entry['tags'] = array($cleaned);
  230. }
  231. if($fields_ret['address']) {
  232. $entry['address'] = array();
  233. // Deactivated. It just reveals too much data. (Although its from the default profile)
  234. //if (isset($rr['paddress']))
  235. // $entry['address']['streetAddress'] = $rr['paddress'];
  236. if (isset($rr['plocation']))
  237. $entry['address']['locality'] = $rr['plocation'];
  238. if (isset($rr['pregion']))
  239. $entry['address']['region'] = $rr['pregion'];
  240. // See above
  241. //if (isset($rr['ppostalcode']))
  242. // $entry['address']['postalCode'] = $rr['ppostalcode'];
  243. if (isset($rr['pcountry']))
  244. $entry['address']['country'] = $rr['pcountry'];
  245. }
  246. $ret['entry'][] = $entry;
  247. }
  248. }
  249. else
  250. $ret['entry'][] = array();
  251. }
  252. else
  253. http_status_exit(500);
  254. if($format === 'xml') {
  255. header('Content-type: text/xml');
  256. echo replace_macros(get_markup_template('poco_xml.tpl'),array_xmlify(array('$response' => $ret)));
  257. killme();
  258. }
  259. if($format === 'json') {
  260. header('Content-type: application/json');
  261. echo json_encode($ret);
  262. killme();
  263. }
  264. else
  265. http_status_exit(500);
  266. }