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.

200 lines
5.1 KiB

  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 = notags(trim($_POST['url']));
  10. if($url) {
  11. $links = lrdd($url);
  12. if(count($links)) {
  13. foreach($links as $link) {
  14. if($link['@attributes']['rel'] === NAMESPACE_DFRN)
  15. $dfrn = $link['@attributes']['href'];
  16. if($link['@attributes']['rel'] === 'salmon')
  17. $notify = $link['@attributes']['href'];
  18. if($link['@attributes']['rel'] === NAMESPACE_FEED)
  19. $poll = $link['@attributes']['href'];
  20. if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard')
  21. $hcard = $link['@attributes']['href'];
  22. if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
  23. $profile = $link['@attributes']['href'];
  24. }
  25. }
  26. }
  27. // If we find a DFRN site, send our subscriber to the other person's
  28. // dfrn_request page and all the other details will get sorted.
  29. if(strlen($dfrn)) {
  30. $ret = scrape_dfrn($dfrn);
  31. if(is_array($ret) && x($ret,'dfrn-request')) {
  32. if(strlen($a->path))
  33. $myaddr = urlencode($a->get_baseurl() . '/profile/' . $a->user['nickname']);
  34. else
  35. $myaddr = urlencode($a->user['nickname'] . '@' . $a->get_hostname());
  36. goaway($ret['dfrn-request'] . "&address=$myaddr");
  37. // NOTREACHED
  38. }
  39. }
  40. if($hcard) {
  41. $vcard = scrape_vcard($hcard);
  42. }
  43. if(! $profile)
  44. $profile = $url;
  45. // do we have enough information?
  46. if(! x($vcard,'fn'))
  47. if(x($vcard,'nick'))
  48. $vcard['fn'] = $vcard['nick'];
  49. if(! ((x($vcard['fn'])) && ($poll) && ($notify) && ($profile))) {
  50. notice( t('The profile address specified does not provide adequate information.') . EOL);
  51. goaway($_SESSION['return_url']);
  52. }
  53. if(! x($vcard,'photo'))
  54. $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ;
  55. // check if we already have a contact
  56. $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
  57. intval(local_user()),
  58. dbesc($poll)
  59. );
  60. if($r) {
  61. // update contact
  62. if($r[0]['rel'] == REL_VIP) {
  63. q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
  64. intval(REL_BUD),
  65. intval($r[0]['id']),
  66. intval(local_user())
  67. );
  68. }
  69. }
  70. else {
  71. // create contact record
  72. $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `notify`, `poll`, `name`, `nick`, `photo`, `network`, `rel`,
  73. `blocked`, `readonly`, `pending` )
  74. VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 0 ) ",
  75. intval(local_user()),
  76. dbesc(datetime_convert()),
  77. dbesc($profile),
  78. dbesc($notify),
  79. dbesc($poll),
  80. dbesc($vcard['fn']),
  81. dbesc($vcard['nick']),
  82. dbesc($vcard['photo']),
  83. dbesc('stat'),
  84. intval(REL_FAN)
  85. );
  86. }
  87. $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
  88. dbesc($profile),
  89. intval(local_user())
  90. );
  91. if(! count($r)) {
  92. notice( t('Unable to retrieve contact information.') . EOL);
  93. goaway($_SESSION['return_url']);
  94. // NOTREACHED
  95. }
  96. $contact = $r[0];
  97. $contact_id = $r[0]['id'];
  98. require_once("Photo.php");
  99. $photo_failure = false;
  100. $filename = basename($vcard['photo']);
  101. $img_str = fetch_url($vcard['photo'],true);
  102. $img = new Photo($img_str);
  103. if($img->is_valid()) {
  104. $img->scaleImageSquare(175);
  105. $hash = photo_new_resource();
  106. $r = $img->store(local_user(), $contact_id, $hash, $filename, t('Contact Photos'), 4 );
  107. if($r === false)
  108. $photo_failure = true;
  109. $img->scaleImage(80);
  110. $r = $img->store(local_user(), $contact_id, $hash, $filename, t('Contact Photos'), 5 );
  111. if($r === false)
  112. $photo_failure = true;
  113. $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.jpg';
  114. $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.jpg';
  115. }
  116. else
  117. $photo_failure = true;
  118. if($photo_failure) {
  119. $photo = $a->get_baseurl() . '/images/default-profile.jpg';
  120. $thumb = $a->get_baseurl() . '/images/default-profile-sm.jpg';
  121. }
  122. $r = q("UPDATE `contact` SET `photo` = '%s',
  123. `thumb` = '%s',
  124. `name-date` = '%s',
  125. `uri-date` = '%s',
  126. `avatar-date` = '%s'
  127. WHERE `id` = %d LIMIT 1
  128. ",
  129. dbesc($photo),
  130. dbesc($thumb),
  131. dbesc(datetime_convert()),
  132. dbesc(datetime_convert()),
  133. dbesc(datetime_convert()),
  134. intval($contact_id)
  135. );
  136. // pull feed and consume it, which should subscribe to the hub.
  137. // create a follow slap
  138. $tpl = load_view_file('view/follow_slap.tpl');
  139. $slap = replace_macros($tpl, array(
  140. '$name' => $a->user['username'],
  141. '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
  142. '$photo' => $a->contact['photo'],
  143. '$thumb' => $a->contact['thumb'],
  144. '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
  145. '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
  146. '$title' => '',
  147. '$type' => 'text',
  148. '$content' => t('following'),
  149. '$nick' => $a->user['nickname'],
  150. '$verb' => ACTIVITY_FOLLOW
  151. ));
  152. $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
  153. WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
  154. intval(local_user())
  155. );
  156. require_once('include/salmon.php');
  157. slapper($r[0],$contact,$slap);
  158. goaway($_SESSION['return_url']);
  159. // NOTREACHED
  160. }