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.
 
 
 
 
 
 

294 lines
8.0 KiB

  1. <?php
  2. require_once('boot.php');
  3. $a = new App;
  4. @include('.htconfig.php');
  5. require_once('dba.php');
  6. $db = new dba($db_host, $db_user, $db_pass, $db_data, $install);
  7. unset($db_host, $db_user, $db_pass, $db_data);
  8. require_once('session.php');
  9. require_once('datetime.php');
  10. require_once('simplepie/simplepie.inc');
  11. require_once('include/items.php');
  12. $a->set_baseurl(get_config('system','url'));
  13. $contacts = q("SELECT * FROM `contact` WHERE `dfrn-id` != '' AND `self` = 0 AND `blocked` = 0 AND `readonly` = 0 ORDER BY RAND()");
  14. if(! count($contacts))
  15. killme();
  16. foreach($contacts as $contact) {
  17. if($contact['priority']) {
  18. $update = false;
  19. $t = $contact['last-update'];
  20. switch ($contact['priority']) {
  21. case 5:
  22. if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 month"))
  23. $update = true;
  24. break;
  25. case 4:
  26. if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 week"))
  27. $update = true;
  28. break;
  29. case 3:
  30. if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 day"))
  31. $update = true;
  32. break;
  33. case 2:
  34. if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 12 hour"))
  35. $update = true;
  36. break;
  37. case 1:
  38. default:
  39. if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 hour"))
  40. $update = true;
  41. break;
  42. }
  43. if(! $update)
  44. continue;
  45. }
  46. $importer_uid = $contact['uid'];
  47. $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
  48. intval($importer_uid)
  49. );
  50. if(! count($r))
  51. continue;
  52. $importer = $r[0];
  53. $last_update = (($contact['last-update'] == '0000-00-00 00:00:00')
  54. ? datetime_convert('UTC','UTC','now - 30 days','Y-m-d\TH:i:s\Z')
  55. : datetime_convert('UTC','UTC',$contact['last-update'],'Y-m-d\TH:i:s\Z'));
  56. $url = $contact['poll'] . '?dfrn_id=' . $contact['dfrn-id'] . '&type=data&last_update=' . $last_update ;
  57. $xml = fetch_url($url);
  58. echo "URL: " . $url;
  59. echo "XML: " . $xml;
  60. if(! $xml)
  61. continue;
  62. $res = simplexml_load_string($xml);
  63. if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
  64. continue;
  65. $postvars = array();
  66. $sent_dfrn_id = hex2bin($res->dfrn_id);
  67. $final_dfrn_id = '';
  68. openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
  69. $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
  70. if($final_dfrn_id != $contact['dfrn-id']) {
  71. // did not decode properly - cannot trust this site
  72. continue;
  73. }
  74. $postvars['dfrn_id'] = $contact['dfrn-id'];
  75. $challenge = hex2bin($res->challenge);
  76. openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
  77. $xml = post_url($contact['poll'],$postvars);
  78. echo "XML response:" . $xml . "\r\n";
  79. echo "Length:" . strlen($xml) . "\r\n";
  80. if(! strlen($xml)) {
  81. // an empty response may mean there's nothing new - record the fact that we checked
  82. $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
  83. dbesc(datetime_convert()),
  84. intval($contact['id'])
  85. );
  86. continue;
  87. }
  88. $feed = new SimplePie();
  89. $feed->set_raw_data($xml);
  90. $feed->enable_order_by_date(false);
  91. $feed->init();
  92. $photo_rawupdate = $feed->get_feed_tags(NAMESPACE_DFRN,'icon-updated');
  93. if($photo_rawupdate) {
  94. $photo_timestamp = datetime_convert('UTC','UTC',$photo_rawupdate[0]['data']);
  95. $photo_url = $feed->get_image_url();
  96. if(strlen($photo_url) && $photo_timestamp > $contact['avatar-date']) {
  97. require_once("Photo.php");
  98. $photo_failure = false;
  99. $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
  100. intval($contact['id']),
  101. intval($contact['uid'])
  102. );
  103. if(count($r)) {
  104. $resource_id = $r[0]['resource-id'];
  105. $img_str = fetch_url($photo_url,true);
  106. $img = new Photo($img_str);
  107. if($img) {
  108. q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND contact-id` = %d AND `uid` = %d",
  109. dbesc($resource_id),
  110. intval($contact['id']),
  111. intval($contact['uid'])
  112. );
  113. $img->scaleImageSquare(175);
  114. $hash = $resource_id;
  115. $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 4);
  116. $img->scaleImage(80);
  117. $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 5);
  118. if($r)
  119. q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
  120. dbesc(datetime_convert()),
  121. intval($contact['uid']),
  122. intval($contact['id'])
  123. );
  124. }
  125. }
  126. }
  127. }
  128. foreach($feed->get_items() as $item) {
  129. $deleted = false;
  130. $rawdelete = $item->get_item_tags("http://purl.org/atompub/tombstones/1.0", 'deleted-entry');
  131. if(isset($rawdelete[0]['attribs']['']['ref'])) {
  132. $uri = $rawthread[0]['attribs']['']['ref'];
  133. $deleted = true;
  134. if(isset($rawdelete[0]['attribs']['']['when'])) {
  135. $when = $rawthread[0]['attribs']['']['when'];
  136. $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
  137. }
  138. else
  139. $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
  140. }
  141. if($deleted) {
  142. $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
  143. dbesc($uri),
  144. intval($importer['uid'])
  145. );
  146. if(count($r)) {
  147. if($r[0]['uri'] == $r[0]['parent-uri']) {
  148. $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s',
  149. `body` = '', `title` = ''
  150. WHERE `parent-uri` = '%s'",
  151. dbesc($when),
  152. dbesc($r[0]['uri'])
  153. );
  154. }
  155. else {
  156. $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s',
  157. `body` = '', `title` = ''
  158. WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
  159. dbesc($when),
  160. dbesc($uri),
  161. intval($importer['uid'])
  162. );
  163. }
  164. }
  165. continue;
  166. }
  167. $is_reply = false;
  168. $item_id = $item->get_id();
  169. $rawthread = $item->get_item_tags("http://purl.org/syndication/thread/1.0",'in-reply-to');
  170. if(isset($rawthread[0]['attribs']['']['ref'])) {
  171. $is_reply = true;
  172. $parent_uri = $rawthread[0]['attribs']['']['ref'];
  173. }
  174. if($is_reply) {
  175. // Have we seen it? If not, import it.
  176. $item_id = $item->get_id();
  177. $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
  178. dbesc($item_id),
  179. intval($importer['uid'])
  180. );
  181. // FIXME update content if 'updated' changes
  182. if(count($r)) {
  183. $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
  184. if($allow && $allow[0]['data'] != $r[0]['last-child']) {
  185. $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
  186. intval($allow[0]['data']),
  187. dbesc($item_id),
  188. intval($importer['uid'])
  189. );
  190. }
  191. continue;
  192. }
  193. $datarray = get_atom_elements($item);
  194. $datarray['parent-uri'] = $parent_uri;
  195. $datarray['uid'] = $importer['uid'];
  196. $datarray['contact-id'] = $contact['id'];
  197. $r = post_remote($a,$datarray);
  198. continue;
  199. }
  200. else {
  201. // Head post of a conversation. Have we seen it? If not, import it.
  202. $item_id = $item->get_id();
  203. $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
  204. dbesc($item_id),
  205. intval($importer['uid'])
  206. );
  207. if(count($r)) {
  208. $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
  209. if($allow && $allow[0]['data'] != $r[0]['last-child']) {
  210. $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
  211. intval($allow[0]['data']),
  212. dbesc($item_id),
  213. intval($importer['uid'])
  214. );
  215. }
  216. continue;
  217. }
  218. $datarray = get_atom_elements($item);
  219. $datarray['parent-uri'] = $item_id;
  220. $datarray['uid'] = $importer['uid'];
  221. $datarray['contact-id'] = $contact['id'];
  222. $r = post_remote($a,$datarray);
  223. continue;
  224. }
  225. }
  226. $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
  227. dbesc(datetime_convert()),
  228. intval($contact['id'])
  229. );
  230. }
  231. killme();