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.

302 lines
8.9 KiB

9 years ago
  1. <?php
  2. use Friendica\App;
  3. require_once("include/Scrape.php");
  4. require_once("include/socgraph.php");
  5. require_once('include/group.php');
  6. require_once('include/salmon.php');
  7. require_once('include/ostatus.php');
  8. require_once("include/Photo.php");
  9. require_once('include/diaspora.php');
  10. function update_contact($id) {
  11. /*
  12. Warning: Never ever fetch the public key via probe_url and write it into the contacts.
  13. This will reliably kill your communication with Friendica contacts.
  14. */
  15. $r = q("SELECT `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `network` FROM `contact` WHERE `id` = %d", intval($id));
  16. if (!$r)
  17. return false;
  18. $ret = probe_url($r[0]["url"]);
  19. // If probe_url fails the network code will be different
  20. if ($ret["network"] != $r[0]["network"])
  21. return false;
  22. $update = false;
  23. // make sure to not overwrite existing values with blank entries
  24. foreach ($ret AS $key => $val) {
  25. if (isset($r[0][$key]) AND ($r[0][$key] != "") AND ($val == ""))
  26. $ret[$key] = $r[0][$key];
  27. if (isset($r[0][$key]) AND ($ret[$key] != $r[0][$key]))
  28. $update = true;
  29. }
  30. if (!$update)
  31. return true;
  32. q("UPDATE `contact` SET `url` = '%s', `nurl` = '%s', `addr` = '%s', `alias` = '%s', `batch` = '%s', `notify` = '%s', `poll` = '%s', `poco` = '%s' WHERE `id` = %d",
  33. dbesc($ret['url']),
  34. dbesc(normalise_link($ret['url'])),
  35. dbesc($ret['addr']),
  36. dbesc($ret['alias']),
  37. dbesc($ret['batch']),
  38. dbesc($ret['notify']),
  39. dbesc($ret['poll']),
  40. dbesc($ret['poco']),
  41. intval($id)
  42. );
  43. // Update the corresponding gcontact entry
  44. poco_last_updated($ret["url"]);
  45. return true;
  46. }
  47. //
  48. // Takes a $uid and a url/handle and adds a new contact
  49. // Currently if the contact is DFRN, interactive needs to be true, to redirect to the
  50. // dfrn_request page.
  51. // Otherwise this can be used to bulk add statusnet contacts, twitter contacts, etc.
  52. // Returns an array
  53. // $return['success'] boolean true if successful
  54. // $return['message'] error text if success is false.
  55. function new_contact($uid,$url,$interactive = false) {
  56. $result = array('cid' => -1, 'success' => false,'message' => '');
  57. $a = get_app();
  58. // remove ajax junk, e.g. Twitter
  59. $url = str_replace('/#!/','/',$url);
  60. if (! allowed_url($url)) {
  61. $result['message'] = t('Disallowed profile URL.');
  62. return $result;
  63. }
  64. if (blocked_url($url)) {
  65. $result['message'] = t('Blocked domain');
  66. return $result;
  67. }
  68. if (! $url) {
  69. $result['message'] = t('Connect URL missing.');
  70. return $result;
  71. }
  72. $arr = array('url' => $url, 'contact' => array());
  73. call_hooks('follow', $arr);
  74. if (x($arr['contact'],'name')) {
  75. $ret = $arr['contact'];
  76. }
  77. else {
  78. $ret = probe_url($url);
  79. }
  80. if ($ret['network'] === NETWORK_DFRN) {
  81. if ($interactive) {
  82. if (strlen($a->path)) {
  83. $myaddr = bin2hex(App::get_baseurl() . '/profile/' . $a->user['nickname']);
  84. } else {
  85. $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
  86. }
  87. goaway($ret['request'] . "&addr=$myaddr");
  88. // NOTREACHED
  89. }
  90. } elseif (get_config('system','dfrn_only')) {
  91. $result['message'] = t('This site is not configured to allow communications with other networks.') . EOL;
  92. $result['message'] != t('No compatible communication protocols or feeds were discovered.') . EOL;
  93. return $result;
  94. }
  95. // This extra param just confuses things, remove it
  96. if ($ret['network'] === NETWORK_DIASPORA) {
  97. $ret['url'] = str_replace('?absolute=true','',$ret['url']);
  98. }
  99. // do we have enough information?
  100. if (! ((x($ret,'name')) && (x($ret,'poll')) && ((x($ret,'url')) || (x($ret,'addr'))))) {
  101. $result['message'] .= t('The profile address specified does not provide adequate information.') . EOL;
  102. if (! x($ret,'poll')) {
  103. $result['message'] .= t('No compatible communication protocols or feeds were discovered.') . EOL;
  104. }
  105. if (! x($ret,'name')) {
  106. $result['message'] .= t('An author or name was not found.') . EOL;
  107. }
  108. if (! x($ret,'url')) {
  109. $result['message'] .= t('No browser URL could be matched to this address.') . EOL;
  110. }
  111. if (strpos($url,'@') !== false) {
  112. $result['message'] .= t('Unable to match @-style Identity Address with a known protocol or email contact.') . EOL;
  113. $result['message'] .= t('Use mailto: in front of address to force email check.') . EOL;
  114. }
  115. return $result;
  116. }
  117. if ($ret['network'] === NETWORK_OSTATUS && get_config('system','ostatus_disabled')) {
  118. $result['message'] .= t('The profile address specified belongs to a network which has been disabled on this site.') . EOL;
  119. $ret['notify'] = '';
  120. }
  121. if (! $ret['notify']) {
  122. $result['message'] .= t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL;
  123. }
  124. $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
  125. $subhub = (($ret['network'] === NETWORK_OSTATUS) ? true : false);
  126. $hidden = (($ret['network'] === NETWORK_MAIL) ? 1 : 0);
  127. if (in_array($ret['network'], array(NETWORK_MAIL, NETWORK_DIASPORA))) {
  128. $writeable = 1;
  129. }
  130. // check if we already have a contact
  131. // the poll url is more reliable than the profile url, as we may have
  132. // indirect links or webfinger links
  133. $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` IN ('%s', '%s') AND `network` = '%s' LIMIT 1",
  134. intval($uid),
  135. dbesc($ret['poll']),
  136. dbesc(normalise_link($ret['poll'])),
  137. dbesc($ret['network'])
  138. );
  139. if (!dbm::is_result($r))
  140. $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `network` = '%s' LIMIT 1",
  141. intval($uid), dbesc(normalise_link($url)), dbesc($ret['network'])
  142. );
  143. if (dbm::is_result($r)) {
  144. // update contact
  145. if ($r[0]['rel'] == CONTACT_IS_FOLLOWER || ($network === NETWORK_DIASPORA && $r[0]['rel'] == CONTACT_IS_SHARING)) {
  146. q("UPDATE `contact` SET `rel` = %d , `subhub` = %d, `readonly` = 0 WHERE `id` = %d AND `uid` = %d",
  147. intval(CONTACT_IS_FRIEND),
  148. intval($subhub),
  149. intval($r[0]['id']),
  150. intval($uid)
  151. );
  152. }
  153. } else {
  154. // check service class limits
  155. $r = q("SELECT COUNT(*) AS `total` FROM `contact` WHERE `uid` = %d AND `pending` = 0 AND `self` = 0",
  156. intval($uid)
  157. );
  158. if (dbm::is_result($r))
  159. $total_contacts = $r[0]['total'];
  160. if (! service_class_allows($uid,'total_contacts',$total_contacts)) {
  161. $result['message'] .= upgrade_message();
  162. return $result;
  163. }
  164. $r = q("SELECT COUNT(`network`) AS `total` FROM `contact` WHERE `uid` = %d AND `network` = '%s' AND `pending` = 0 AND `self` = 0",
  165. intval($uid),
  166. dbesc($network)
  167. );
  168. if (dbm::is_result($r)) {
  169. $total_network = $r[0]['total'];
  170. }
  171. if (! service_class_allows($uid,'total_contacts_' . $network,$total_network)) {
  172. $result['message'] .= upgrade_message();
  173. return $result;
  174. }
  175. $new_relation = ((in_array($ret['network'], array(NETWORK_MAIL, NETWORK_DIASPORA))) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
  176. // create contact record
  177. $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `network`, `pubkey`, `rel`, `priority`,
  178. `writable`, `hidden`, `blocked`, `readonly`, `pending`, `subhub` )
  179. VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, 0, 0, 0, %d ) ",
  180. intval($uid),
  181. dbesc(datetime_convert()),
  182. dbesc($ret['url']),
  183. dbesc(normalise_link($ret['url'])),
  184. dbesc($ret['addr']),
  185. dbesc($ret['alias']),
  186. dbesc($ret['batch']),
  187. dbesc($ret['notify']),
  188. dbesc($ret['poll']),
  189. dbesc($ret['poco']),
  190. dbesc($ret['name']),
  191. dbesc($ret['nick']),
  192. dbesc($ret['network']),
  193. dbesc($ret['pubkey']),
  194. intval($new_relation),
  195. intval($ret['priority']),
  196. intval($writeable),
  197. intval($hidden),
  198. intval($subhub)
  199. );
  200. }
  201. $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `network` = '%s' AND `uid` = %d LIMIT 1",
  202. dbesc($ret['url']),
  203. dbesc($ret['network']),
  204. intval($uid)
  205. );
  206. if (! dbm::is_result($r)) {
  207. $result['message'] .= t('Unable to retrieve contact information.') . EOL;
  208. return $result;
  209. }
  210. $contact = $r[0];
  211. $contact_id = $r[0]['id'];
  212. $result['cid'] = $contact_id;
  213. $def_gid = get_default_group($uid, $contact["network"]);
  214. if (intval($def_gid)) {
  215. group_add_member($uid, '', $contact_id, $def_gid);
  216. }
  217. // Update the avatar
  218. update_contact_avatar($ret['photo'],$uid,$contact_id);
  219. // pull feed and consume it, which should subscribe to the hub.
  220. proc_run(PRIORITY_HIGH, "include/onepoll.php", $contact_id, "force");
  221. $r = q("SELECT `contact`.*, `user`.* FROM `contact` INNER JOIN `user` ON `contact`.`uid` = `user`.`uid`
  222. WHERE `user`.`uid` = %d AND `contact`.`self` LIMIT 1",
  223. intval($uid)
  224. );
  225. if (dbm::is_result($r)) {
  226. if (($contact['network'] == NETWORK_OSTATUS) && (strlen($contact['notify']))) {
  227. // create a follow slap
  228. $item = array();
  229. $item['verb'] = ACTIVITY_FOLLOW;
  230. $item['follow'] = $contact["url"];
  231. $slap = ostatus::salmon($item, $r[0]);
  232. slapper($r[0], $contact['notify'], $slap);
  233. }
  234. if ($contact['network'] == NETWORK_DIASPORA) {
  235. $ret = Diaspora::send_share($a->user,$contact);
  236. logger('share returns: '.$ret);
  237. }
  238. }
  239. $result['success'] = true;
  240. return $result;
  241. }