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.

201 lines
5.9 KiB

10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. <?php
  2. require_once('Scrape.php');
  3. function follow_post(&$a) {
  4. if(! local_user()) {
  5. notice( t('Permission denied.') . EOL);
  6. goaway($_SESSION['return_url']);
  7. // NOTREACHED
  8. }
  9. $url = $orig_url = notags(trim($_POST['url']));
  10. // remove ajax junk, e.g. Twitter
  11. $url = str_replace('/#!/','/',$url);
  12. if(! allowed_url($url)) {
  13. notice( t('Disallowed profile URL.') . EOL);
  14. goaway($_SESSION['return_url']);
  15. // NOTREACHED
  16. }
  17. $ret = probe_url($url);
  18. if($ret['network'] === NETWORK_DFRN) {
  19. if(strlen($a->path))
  20. $myaddr = bin2hex($a->get_baseurl() . '/profile/' . $a->user['nickname']);
  21. else
  22. $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
  23. goaway($ret['request'] . "&addr=$myaddr");
  24. // NOTREACHED
  25. }
  26. else {
  27. if(get_config('system','dfrn_only')) {
  28. notice( t('This site is not configured to allow communications with other networks.') . EOL);
  29. notice( t('No compatible communication protocols or feeds were discovered.') . EOL);
  30. goaway($_SESSION['return_url']);
  31. }
  32. }
  33. // do we have enough information?
  34. if(! ((x($ret,'name')) && (x($ret,'poll')) && ((x($ret,'url')) || (x($ret,'addr'))))) {
  35. notice( t('The profile address specified does not provide adequate information.') . EOL);
  36. if(! x($ret,'poll'))
  37. notice( t('No compatible communication protocols or feeds were discovered.') . EOL);
  38. if(! x($ret,'name'))
  39. notice( t('An author or name was not found.') . EOL);
  40. if(! x($ret,'url'))
  41. notice( t('No browser URL could be matched to this address.') . EOL);
  42. if(strpos($url,'@') !== false)
  43. notice('Unable to match @-style Identity Address with a known protocol or email contact');
  44. goaway($_SESSION['return_url']);
  45. }
  46. if($ret['network'] === NETWORK_OSTATUS && get_config('system','ostatus_disabled')) {
  47. notice( t('The profile address specified belongs to a network which has been disabled on this site.') . EOL);
  48. $ret['notify'] = '';
  49. }
  50. if(! $ret['notify']) {
  51. notice( t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL);
  52. }
  53. $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
  54. if($ret['network'] === NETWORK_MAIL) {
  55. $writeable = 1;
  56. }
  57. if($ret['network'] === NETWORK_DIASPORA)
  58. $writeable = 1;
  59. // check if we already have a contact
  60. // the poll url is more reliable than the profile url, as we may have
  61. // indirect links or webfinger links
  62. $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
  63. intval(local_user()),
  64. dbesc($ret['poll'])
  65. );
  66. if(count($r)) {
  67. // update contact
  68. if($r[0]['rel'] == CONTACT_IS_FOLLOWER || ($network === NETWORK_DIASPORA && $r[0]['rel'] == CONTACT_IS_SHARING)) {
  69. q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
  70. intval(CONTACT_IS_FRIEND),
  71. intval($r[0]['id']),
  72. intval(local_user())
  73. );
  74. }
  75. }
  76. else {
  77. $new_relation = (($ret['network'] === NETWORK_MAIL) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
  78. if($ret['network'] === NETWORK_DIASPORA)
  79. $new_relation = CONTACT_IS_FOLLOWER;
  80. // create contact record
  81. $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `addr`, `alias`, `notify`, `poll`, `name`, `nick`, `photo`, `network`, `rel`, `priority`,
  82. `writable`, `blocked`, `readonly`, `pending` )
  83. VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0 ) ",
  84. intval(local_user()),
  85. dbesc(datetime_convert()),
  86. dbesc($ret['url']),
  87. dbesc($ret['addr']),
  88. dbesc($ret['alias']),
  89. dbesc($ret['notify']),
  90. dbesc($ret['poll']),
  91. dbesc($ret['name']),
  92. dbesc($ret['nick']),
  93. dbesc($ret['photo']),
  94. dbesc($ret['network']),
  95. intval($new_relation),
  96. intval($ret['priority']),
  97. intval($writeable)
  98. );
  99. }
  100. $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
  101. dbesc($ret['url']),
  102. intval(local_user())
  103. );
  104. if(! count($r)) {
  105. notice( t('Unable to retrieve contact information.') . EOL);
  106. goaway($_SESSION['return_url']);
  107. // NOTREACHED
  108. }
  109. $contact = $r[0];
  110. $contact_id = $r[0]['id'];
  111. require_once("Photo.php");
  112. $photos = import_profile_photo($ret['photo'],local_user(),$contact_id);
  113. $r = q("UPDATE `contact` SET `photo` = '%s',
  114. `thumb` = '%s',
  115. `micro` = '%s',
  116. `name-date` = '%s',
  117. `uri-date` = '%s',
  118. `avatar-date` = '%s'
  119. WHERE `id` = %d LIMIT 1
  120. ",
  121. dbesc($photos[0]),
  122. dbesc($photos[1]),
  123. dbesc($photos[2]),
  124. dbesc(datetime_convert()),
  125. dbesc(datetime_convert()),
  126. dbesc(datetime_convert()),
  127. intval($contact_id)
  128. );
  129. // pull feed and consume it, which should subscribe to the hub.
  130. proc_run('php',"include/poller.php","$contact_id");
  131. // create a follow slap
  132. $tpl = get_markup_template('follow_slap.tpl');
  133. $slap = replace_macros($tpl, array(
  134. '$name' => $a->user['username'],
  135. '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
  136. '$photo' => $a->contact['photo'],
  137. '$thumb' => $a->contact['thumb'],
  138. '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
  139. '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
  140. '$title' => '',
  141. '$type' => 'text',
  142. '$content' => t('following'),
  143. '$nick' => $a->user['nickname'],
  144. '$verb' => ACTIVITY_FOLLOW,
  145. '$ostat_follow' => ''
  146. ));
  147. $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
  148. WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
  149. intval(local_user())
  150. );
  151. if(count($r)) {
  152. if(($contact['network'] == NETWORK_OSTATUS) && (strlen($contact['notify']))) {
  153. require_once('include/salmon.php');
  154. slapper($r[0],$contact['notify'],$slap);
  155. }
  156. if($contact['network'] == NETWORK_DIASPORA) {
  157. require_once('include/diaspora.php');
  158. $ret = diaspora_share($a->user,$r[0]);
  159. logger('mod_follow: diaspora_share returns: ' . $ret);
  160. }
  161. }
  162. goaway($a->get_baseurl() . '/contacts/' . $contact_id);
  163. // goaway($_SESSION['return_url']);
  164. // NOTREACHED
  165. }