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.

679 lines
22KB

  1. <?php
  2. use Friendica\Core\Config;
  3. require_once('include/follow.php');
  4. function RemoveReply($subject) {
  5. while (in_array(strtolower(substr($subject, 0, 3)), array("re:", "aw:")))
  6. $subject = trim(substr($subject, 4));
  7. return($subject);
  8. }
  9. function onepoll_run(&$argv, &$argc){
  10. global $a;
  11. require_once('include/datetime.php');
  12. require_once('include/items.php');
  13. require_once('include/Contact.php');
  14. require_once('include/email.php');
  15. require_once('include/socgraph.php');
  16. require_once('include/queue_fn.php');
  17. logger('onepoll: start');
  18. $manual_id = 0;
  19. $generation = 0;
  20. $hub_update = false;
  21. $force = false;
  22. $restart = false;
  23. if (($argc > 1) && (intval($argv[1]))) {
  24. $contact_id = intval($argv[1]);
  25. }
  26. if (($argc > 2) && ($argv[2] == "force")) {
  27. $force = true;
  28. }
  29. if (! $contact_id) {
  30. logger('onepoll: no contact');
  31. return;
  32. }
  33. $d = datetime_convert();
  34. // Only poll from those with suitable relationships,
  35. // and which have a polling address and ignore Diaspora since
  36. // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
  37. $contacts = q("SELECT `contact`.* FROM `contact`
  38. WHERE ( `rel` = %d OR `rel` = %d ) AND `poll` != ''
  39. AND NOT `network` IN ( '%s', '%s' )
  40. AND `contact`.`id` = %d
  41. AND `self` = 0 AND `contact`.`blocked` = 0 AND `contact`.`readonly` = 0
  42. AND `contact`.`archive` = 0 LIMIT 1",
  43. intval(CONTACT_IS_SHARING),
  44. intval(CONTACT_IS_FRIEND),
  45. dbesc(NETWORK_FACEBOOK),
  46. dbesc(NETWORK_PUMPIO),
  47. intval($contact_id)
  48. );
  49. if (! count($contacts)) {
  50. return;
  51. }
  52. $contact = $contacts[0];
  53. // load current friends if possible.
  54. if (($contact['poco'] != "") && ($contact['success_update'] > $contact['failure_update'])) {
  55. $r = q("SELECT count(*) AS total FROM glink
  56. WHERE `cid` = %d AND updated > UTC_TIMESTAMP() - INTERVAL 1 DAY",
  57. intval($contact['id'])
  58. );
  59. if (dbm::is_result($r)) {
  60. if (!$r[0]['total']) {
  61. poco_load($contact['id'],$importer_uid,0,$contact['poco']);
  62. }
  63. }
  64. }
  65. /// @TODO Check why we don't poll the Diaspora feed at the moment (some guid problem in the items?)
  66. /// @TODO Check whether this is possible with Redmatrix
  67. if ($contact["network"] == NETWORK_DIASPORA) {
  68. if (poco_do_update($contact["created"], $contact["last-item"], $contact["failure_update"], $contact["success_update"])) {
  69. $last_updated = poco_last_updated($contact["url"]);
  70. $updated = datetime_convert();
  71. if ($last_updated) {
  72. q("UPDATE `contact` SET `last-item` = '%s', `last-update` = '%s', `success_update` = '%s' WHERE `id` = %d",
  73. dbesc($last_updated),
  74. dbesc($updated),
  75. dbesc($updated),
  76. intval($contact['id'])
  77. );
  78. } else {
  79. q("UPDATE `contact` SET `last-update` = '%s', `failure_update` = '%s' WHERE `id` = %d",
  80. dbesc($updated),
  81. dbesc($updated),
  82. intval($contact['id'])
  83. );
  84. }
  85. }
  86. return;
  87. }
  88. $xml = false;
  89. $t = $contact['last-update'];
  90. if ($contact['subhub']) {
  91. $poll_interval = get_config('system','pushpoll_frequency');
  92. $contact['priority'] = (($poll_interval !== false) ? intval($poll_interval) : 3);
  93. $hub_update = false;
  94. if (datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day")) {
  95. $hub_update = true;
  96. }
  97. } else {
  98. $hub_update = false;
  99. }
  100. $importer_uid = $contact['uid'];
  101. $r = q("SELECT `contact`.*, `user`.`page-flags` FROM `contact` INNER JOIN `user` on `contact`.`uid` = `user`.`uid` WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
  102. intval($importer_uid)
  103. );
  104. if (!dbm::is_result($r)) {
  105. return;
  106. }
  107. $importer = $r[0];
  108. logger("onepoll: poll: ({$contact['id']}) IMPORTER: {$importer['name']}, CONTACT: {$contact['name']}");
  109. $last_update = (($contact['last-update'] <= NULL_DATE)
  110. ? datetime_convert('UTC','UTC','now - 7 days', ATOM_TIME)
  111. : datetime_convert('UTC','UTC',$contact['last-update'], ATOM_TIME)
  112. );
  113. // Update the contact entry
  114. if (($contact['network'] === NETWORK_OSTATUS) || ($contact['network'] === NETWORK_DIASPORA) || ($contact['network'] === NETWORK_DFRN)) {
  115. if (!poco_reachable($contact['url'])) {
  116. logger("Skipping probably dead contact ".$contact['url']);
  117. return;
  118. }
  119. if (!update_contact($contact["id"])) {
  120. mark_for_death($contact);
  121. return;
  122. } else {
  123. unmark_for_death($contact);
  124. }
  125. }
  126. if ($contact['network'] === NETWORK_DFRN) {
  127. $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
  128. if (intval($contact['duplex']) && $contact['dfrn-id']) {
  129. $idtosend = '0:' . $orig_id;
  130. }
  131. if (intval($contact['duplex']) && $contact['issued-id']) {
  132. $idtosend = '1:' . $orig_id;
  133. }
  134. // they have permission to write to us. We already filtered this in the contact query.
  135. $perm = 'rw';
  136. // But this may be our first communication, so set the writable flag if it isn't set already.
  137. if (! intval($contact['writable'])) {
  138. q("UPDATE `contact` SET `writable` = 1 WHERE `id` = %d", intval($contact['id']));
  139. }
  140. $url = $contact['poll'] . '?dfrn_id=' . $idtosend
  141. . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
  142. . '&type=data&last_update=' . $last_update
  143. . '&perm=' . $perm ;
  144. $ret = z_fetch_url($url);
  145. if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
  146. return;
  147. }
  148. $handshake_xml = $ret['body'];
  149. $html_code = $a->get_curl_code();
  150. logger('onepoll: handshake with url ' . $url . ' returns xml: ' . $handshake_xml, LOGGER_DATA);
  151. if ((! strlen($handshake_xml)) || ($html_code >= 400) || (! $html_code)) {
  152. logger("poller: $url appears to be dead - marking for death ");
  153. // dead connection - might be a transient event, or this might
  154. // mean the software was uninstalled or the domain expired.
  155. // Will keep trying for one month.
  156. mark_for_death($contact);
  157. // set the last-update so we don't keep polling
  158. $r = q("UPDATE `contact` SET `last-update` = '%s', `failure_update` = '%s' WHERE `id` = %d",
  159. dbesc(datetime_convert()),
  160. dbesc(datetime_convert()),
  161. intval($contact['id'])
  162. );
  163. return;
  164. }
  165. if (! strstr($handshake_xml,'<')) {
  166. logger('poller: response from ' . $url . ' did not contain XML.');
  167. mark_for_death($contact);
  168. $r = q("UPDATE `contact` SET `last-update` = '%s', `failure_update` = '%s' WHERE `id` = %d",
  169. dbesc(datetime_convert()),
  170. dbesc(datetime_convert()),
  171. intval($contact['id'])
  172. );
  173. return;
  174. }
  175. $res = parse_xml_string($handshake_xml);
  176. if (intval($res->status) == 1) {
  177. logger("poller: $url replied status 1 - marking for death ");
  178. // we may not be friends anymore. Will keep trying for one month.
  179. // set the last-update so we don't keep polling
  180. $r = q("UPDATE `contact` SET `last-update` = '%s', `failure_update` = '%s' WHERE `id` = %d",
  181. dbesc(datetime_convert()),
  182. dbesc(datetime_convert()),
  183. intval($contact['id'])
  184. );
  185. mark_for_death($contact);
  186. } elseif ($contact['term-date'] > NULL_DATE) {
  187. logger("poller: $url back from the dead - removing mark for death");
  188. unmark_for_death($contact);
  189. }
  190. if ((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id))) {
  191. return;
  192. }
  193. if (((float) $res->dfrn_version > 2.21) && ($contact['poco'] == '')) {
  194. q("update contact set poco = '%s' where id = %d",
  195. dbesc(str_replace('/profile/','/poco/', $contact['url'])),
  196. intval($contact['id'])
  197. );
  198. }
  199. $postvars = array();
  200. $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
  201. $challenge = hex2bin((string) $res->challenge);
  202. $final_dfrn_id = '';
  203. if (($contact['duplex']) && strlen($contact['prvkey'])) {
  204. openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
  205. openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
  206. } else {
  207. openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
  208. openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
  209. }
  210. $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
  211. if (strpos($final_dfrn_id,':') == 1) {
  212. $final_dfrn_id = substr($final_dfrn_id,2);
  213. }
  214. if ($final_dfrn_id != $orig_id) {
  215. logger('poller: ID did not decode: ' . $contact['id'] . ' orig: ' . $orig_id . ' final: ' . $final_dfrn_id);
  216. // did not decode properly - cannot trust this site
  217. return;
  218. }
  219. $postvars['dfrn_id'] = $idtosend;
  220. $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
  221. $postvars['perm'] = 'rw';
  222. $xml = post_url($contact['poll'],$postvars);
  223. } elseif (($contact['network'] === NETWORK_OSTATUS)
  224. || ($contact['network'] === NETWORK_DIASPORA)
  225. || ($contact['network'] === NETWORK_FEED)) {
  226. // Upgrading DB fields from an older Friendica version
  227. // Will only do this once per notify-enabled OStatus contact
  228. // or if relationship changes
  229. $stat_writeable = ((($contact['notify']) && ($contact['rel'] == CONTACT_IS_FOLLOWER || $contact['rel'] == CONTACT_IS_FRIEND)) ? 1 : 0);
  230. // Contacts from OStatus are always writable
  231. if ($contact['network'] === NETWORK_OSTATUS) {
  232. $stat_writeable = 1;
  233. }
  234. if ($stat_writeable != $contact['writable']) {
  235. q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d",
  236. intval($stat_writeable),
  237. intval($contact['id'])
  238. );
  239. }
  240. // Are we allowed to import from this person?
  241. if ($contact['rel'] == CONTACT_IS_FOLLOWER || $contact['blocked'] || $contact['readonly']) {
  242. return;
  243. }
  244. $cookiejar = tempnam(get_temppath(), 'cookiejar-onepoll-');
  245. $ret = z_fetch_url($contact['poll'], false, $redirects, array('cookiejar' => $cookiejar));
  246. if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
  247. return;
  248. }
  249. $xml = $ret['body'];
  250. unlink($cookiejar);
  251. } elseif ($contact['network'] === NETWORK_MAIL || $contact['network'] === NETWORK_MAIL2) {
  252. logger("Mail: Fetching", LOGGER_DEBUG);
  253. $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
  254. if ($mail_disabled)
  255. return;
  256. logger("Mail: Enabled", LOGGER_DEBUG);
  257. $mbox = null;
  258. $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1",
  259. intval($importer_uid)
  260. );
  261. $mailconf = q("SELECT * FROM `mailacct` WHERE `server` != '' AND `uid` = %d LIMIT 1",
  262. intval($importer_uid)
  263. );
  264. if (count($x) && count($mailconf)) {
  265. $mailbox = construct_mailbox_name($mailconf[0]);
  266. $password = '';
  267. openssl_private_decrypt(hex2bin($mailconf[0]['pass']),$password,$x[0]['prvkey']);
  268. $mbox = email_connect($mailbox,$mailconf[0]['user'],$password);
  269. unset($password);
  270. logger("Mail: Connect to " . $mailconf[0]['user']);
  271. if ($mbox) {
  272. q("UPDATE `mailacct` SET `last_check` = '%s' WHERE `id` = %d AND `uid` = %d",
  273. dbesc(datetime_convert()),
  274. intval($mailconf[0]['id']),
  275. intval($importer_uid)
  276. );
  277. logger("Mail: Connected to " . $mailconf[0]['user']);
  278. } else {
  279. logger("Mail: Connection error ".$mailconf[0]['user']." ".print_r(imap_errors(), true));
  280. }
  281. }
  282. if ($mbox) {
  283. $msgs = email_poll($mbox,$contact['addr']);
  284. if (count($msgs)) {
  285. logger("Mail: Parsing ".count($msgs)." mails for ".$mailconf[0]['user'], LOGGER_DEBUG);
  286. $metas = email_msg_meta($mbox,implode(',',$msgs));
  287. if (count($metas) != count($msgs)) {
  288. logger("onepoll: for " . $mailconf[0]['user'] . " there are ". count($msgs) . " messages but received " . count($metas) . " metas", LOGGER_DEBUG);
  289. } else {
  290. $msgs = array_combine($msgs, $metas);
  291. foreach ($msgs as $msg_uid => $meta) {
  292. logger("Mail: Parsing mail ".$msg_uid, LOGGER_DATA);
  293. $datarray = array();
  294. $datarray['verb'] = ACTIVITY_POST;
  295. $datarray['object-type'] = ACTIVITY_OBJ_NOTE;
  296. // $meta = email_msg_meta($mbox,$msg_uid);
  297. // $headers = email_msg_headers($mbox,$msg_uid);
  298. $datarray['uri'] = msgid2iri(trim($meta->message_id,'<>'));
  299. // Have we seen it before?
  300. $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
  301. intval($importer_uid),
  302. dbesc($datarray['uri'])
  303. );
  304. if (dbm::is_result($r)) {
  305. logger("Mail: Seen before ".$msg_uid." for ".$mailconf[0]['user']." UID: ".$importer_uid." URI: ".$datarray['uri'],LOGGER_DEBUG);
  306. // Only delete when mails aren't automatically moved or deleted
  307. if (($mailconf[0]['action'] != 1) && ($mailconf[0]['action'] != 3))
  308. if ($meta->deleted && ! $r[0]['deleted']) {
  309. q("UPDATE `item` SET `deleted` = 1, `changed` = '%s' WHERE `id` = %d",
  310. dbesc(datetime_convert()),
  311. intval($r[0]['id'])
  312. );
  313. }
  314. switch ($mailconf[0]['action']) {
  315. case 0:
  316. logger("Mail: Seen before ".$msg_uid." for ".$mailconf[0]['user'].". Doing nothing.", LOGGER_DEBUG);
  317. break;
  318. case 1:
  319. logger("Mail: Deleting ".$msg_uid." for ".$mailconf[0]['user']);
  320. imap_delete($mbox, $msg_uid, FT_UID);
  321. break;
  322. case 2:
  323. logger("Mail: Mark as seen ".$msg_uid." for ".$mailconf[0]['user']);
  324. imap_setflag_full($mbox, $msg_uid, "\\Seen", ST_UID);
  325. break;
  326. case 3:
  327. logger("Mail: Moving ".$msg_uid." to ".$mailconf[0]['movetofolder']." for ".$mailconf[0]['user']);
  328. imap_setflag_full($mbox, $msg_uid, "\\Seen", ST_UID);
  329. if ($mailconf[0]['movetofolder'] != "")
  330. imap_mail_move($mbox, $msg_uid, $mailconf[0]['movetofolder'], FT_UID);
  331. break;
  332. }
  333. continue;
  334. }
  335. // look for a 'references' or an 'in-reply-to' header and try to match with a parent item we have locally.
  336. // $raw_refs = ((x($headers,'references')) ? str_replace("\t",'',$headers['references']) : '');
  337. $raw_refs = ((property_exists($meta,'references')) ? str_replace("\t",'',$meta->references) : '');
  338. if (! trim($raw_refs))
  339. $raw_refs = ((property_exists($meta,'in_reply_to')) ? str_replace("\t",'',$meta->in_reply_to) : '');
  340. $raw_refs = trim($raw_refs); // Don't allow a blank reference in $refs_arr
  341. if ($raw_refs) {
  342. $refs_arr = explode(' ', $raw_refs);
  343. if (count($refs_arr)) {
  344. for ($x = 0; $x < count($refs_arr); $x ++) {
  345. $refs_arr[$x] = "'" . msgid2iri(str_replace(array('<','>',' '),array('','',''),dbesc($refs_arr[$x]))) . "'";
  346. }
  347. }
  348. $qstr = implode(',',$refs_arr);
  349. $r = q("SELECT `uri` , `parent-uri` FROM `item` USE INDEX (`uid_uri`) WHERE `uri` IN ($qstr) AND `uid` = %d LIMIT 1",
  350. intval($importer_uid)
  351. );
  352. if (dbm::is_result($r)) {
  353. $datarray['parent-uri'] = $r[0]['parent-uri']; // Set the parent as the top-level item
  354. //$datarray['parent-uri'] = $r[0]['uri'];
  355. }
  356. }
  357. // Decoding the header
  358. $subject = imap_mime_header_decode($meta->subject);
  359. $datarray['title'] = "";
  360. foreach ($subject as $subpart) {
  361. if ($subpart->charset != "default") {
  362. $datarray['title'] .= iconv($subpart->charset, 'UTF-8//IGNORE', $subpart->text);
  363. } else {
  364. $datarray['title'] .= $subpart->text;
  365. }
  366. }
  367. $datarray['title'] = notags(trim($datarray['title']));
  368. //$datarray['title'] = notags(trim($meta->subject));
  369. $datarray['created'] = datetime_convert('UTC','UTC',$meta->date);
  370. // Is it a reply?
  371. $reply = ((substr(strtolower($datarray['title']), 0, 3) == "re:") ||
  372. (substr(strtolower($datarray['title']), 0, 3) == "re-") ||
  373. ($raw_refs != ""));
  374. // Remove Reply-signs in the subject
  375. $datarray['title'] = RemoveReply($datarray['title']);
  376. // If it seems to be a reply but a header couldn't be found take the last message with matching subject
  377. if (!x($datarray,'parent-uri') && $reply) {
  378. $r = q("SELECT `uri` , `parent-uri` FROM `item` WHERE `title` = \"%s\" AND `uid` = %d AND `network` = '%s' ORDER BY `created` DESC LIMIT 1",
  379. dbesc(protect_sprintf($datarray['title'])),
  380. intval($importer_uid),
  381. dbesc(NETWORK_MAIL));
  382. if (dbm::is_result($r))
  383. $datarray['parent-uri'] = $r[0]['parent-uri'];
  384. }
  385. if (! x($datarray,'parent-uri'))
  386. $datarray['parent-uri'] = $datarray['uri'];
  387. $r = email_get_msg($mbox,$msg_uid, $reply);
  388. if (! $r) {
  389. logger("Mail: can't fetch msg ".$msg_uid." for ".$mailconf[0]['user']);
  390. continue;
  391. }
  392. $datarray['body'] = escape_tags($r['body']);
  393. $datarray['body'] = limit_body_size($datarray['body']);
  394. logger("Mail: Importing ".$msg_uid." for ".$mailconf[0]['user']);
  395. // some mailing lists have the original author as 'from' - add this sender info to msg body.
  396. /// @TODO Adding a gravatar for the original author would be cool
  397. if (! stristr($meta->from,$contact['addr'])) {
  398. $from = imap_mime_header_decode($meta->from);
  399. $fromdecoded = "";
  400. foreach ($from as $frompart) {
  401. if ($frompart->charset != "default") {
  402. $fromdecoded .= iconv($frompart->charset, 'UTF-8//IGNORE', $frompart->text);
  403. } else {
  404. $fromdecoded .= $frompart->text;
  405. }
  406. }
  407. $fromarr = imap_rfc822_parse_adrlist($fromdecoded, $a->get_hostname());
  408. $frommail = $fromarr[0]->mailbox."@".$fromarr[0]->host;
  409. if (isset($fromarr[0]->personal)) {
  410. $fromname = $fromarr[0]->personal;
  411. } else {
  412. $fromname = $frommail;
  413. }
  414. //$datarray['body'] = "[b]".t('From: ') . escape_tags($fromdecoded) . "[/b]\n\n" . $datarray['body'];
  415. $datarray['author-name'] = $fromname;
  416. $datarray['author-link'] = "mailto:".$frommail;
  417. $datarray['author-avatar'] = $contact['photo'];
  418. $datarray['owner-name'] = $contact['name'];
  419. $datarray['owner-link'] = "mailto:".$contact['addr'];
  420. $datarray['owner-avatar'] = $contact['photo'];
  421. } else {
  422. $datarray['author-name'] = $contact['name'];
  423. $datarray['author-link'] = 'mailbox';
  424. $datarray['author-avatar'] = $contact['photo'];
  425. }
  426. $datarray['uid'] = $importer_uid;
  427. $datarray['contact-id'] = $contact['id'];
  428. if ($datarray['parent-uri'] === $datarray['uri'])
  429. $datarray['private'] = 1;
  430. if (($contact['network'] === NETWORK_MAIL) && (! get_pconfig($importer_uid,'system','allow_public_email_replies'))) {
  431. $datarray['private'] = 1;
  432. $datarray['allow_cid'] = '<' . $contact['id'] . '>';
  433. }
  434. $stored_item = item_store($datarray);
  435. q("UPDATE `item` SET `last-child` = 0 WHERE `parent-uri` = '%s' AND `uid` = %d",
  436. dbesc($datarray['parent-uri']),
  437. intval($importer_uid)
  438. );
  439. q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d",
  440. intval($stored_item)
  441. );
  442. switch ($mailconf[0]['action']) {
  443. case 0:
  444. logger("Mail: Seen before ".$msg_uid." for ".$mailconf[0]['user'].". Doing nothing.", LOGGER_DEBUG);
  445. break;
  446. case 1:
  447. logger("Mail: Deleting ".$msg_uid." for ".$mailconf[0]['user']);
  448. imap_delete($mbox, $msg_uid, FT_UID);
  449. break;
  450. case 2:
  451. logger("Mail: Mark as seen ".$msg_uid." for ".$mailconf[0]['user']);
  452. imap_setflag_full($mbox, $msg_uid, "\\Seen", ST_UID);
  453. break;
  454. case 3:
  455. logger("Mail: Moving ".$msg_uid." to ".$mailconf[0]['movetofolder']." for ".$mailconf[0]['user']);
  456. imap_setflag_full($mbox, $msg_uid, "\\Seen", ST_UID);
  457. if ($mailconf[0]['movetofolder'] != "")
  458. imap_mail_move($mbox, $msg_uid, $mailconf[0]['movetofolder'], FT_UID);
  459. break;
  460. }
  461. }
  462. }
  463. } else {
  464. logger("Mail: no mails for ".$mailconf[0]['user']);
  465. }
  466. logger("Mail: closing connection for ".$mailconf[0]['user']);
  467. imap_close($mbox);
  468. }
  469. } elseif ($contact['network'] === NETWORK_FACEBOOK) {
  470. // This is picked up by the Facebook plugin on a cron hook.
  471. // Ignored here.
  472. } elseif ($contact['network'] === NETWORK_PUMPIO) {
  473. // This is picked up by the pump.io plugin on a cron hook.
  474. // Ignored here.
  475. }
  476. if ($xml) {
  477. logger('poller: received xml : ' . $xml, LOGGER_DATA);
  478. if (! strstr($xml,'<')) {
  479. logger('poller: post_handshake: response from ' . $url . ' did not contain XML.');
  480. $r = q("UPDATE `contact` SET `last-update` = '%s', `failure_update` = '%s' WHERE `id` = %d",
  481. dbesc(datetime_convert()),
  482. dbesc(datetime_convert()),
  483. intval($contact['id'])
  484. );
  485. return;
  486. }
  487. logger("Consume feed of contact ".$contact['id']);
  488. consume_feed($xml,$importer,$contact,$hub,1,1);
  489. // do it twice. Ensures that children of parents which may be later in the stream aren't tossed
  490. consume_feed($xml,$importer,$contact,$hub,1,2);
  491. $hubmode = 'subscribe';
  492. if ($contact['network'] === NETWORK_DFRN || $contact['blocked'] || $contact['readonly']) {
  493. $hubmode = 'unsubscribe';
  494. }
  495. if (($contact['network'] === NETWORK_OSTATUS || $contact['network'] == NETWORK_FEED) && (! $contact['hub-verify'])) {
  496. $hub_update = true;
  497. }
  498. if ($force) {
  499. $hub_update = true;
  500. }
  501. logger("Contact ".$contact['id']." returned hub: ".$hub." Network: ".$contact['network']." Relation: ".$contact['rel']." Update: ".$hub_update);
  502. if ((strlen($hub)) && ($hub_update) && (($contact['rel'] != CONTACT_IS_FOLLOWER) || $contact['network'] == NETWORK_FEED) ) {
  503. logger('poller: hub ' . $hubmode . ' : ' . $hub . ' contact name : ' . $contact['name'] . ' local user : ' . $importer['name']);
  504. $hubs = explode(',', $hub);
  505. if (count($hubs)) {
  506. foreach ($hubs as $h) {
  507. $h = trim($h);
  508. if (! strlen($h)) {
  509. continue;
  510. }
  511. subscribe_to_hub($h,$importer,$contact,$hubmode);
  512. }
  513. }
  514. }
  515. $updated = datetime_convert();
  516. $r = q("UPDATE `contact` SET `last-update` = '%s', `success_update` = '%s' WHERE `id` = %d",
  517. dbesc($updated),
  518. dbesc($updated),
  519. intval($contact['id'])
  520. );
  521. q("UPDATE `gcontact` SET `last_contact` = '%s' WHERE `nurl` = '%s'",
  522. dbesc($updated),
  523. dbesc($contact['nurl'])
  524. );
  525. } elseif (in_array($contact["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, NETWORK_FEED))) {
  526. $updated = datetime_convert();
  527. $r = q("UPDATE `contact` SET `last-update` = '%s', `failure_update` = '%s' WHERE `id` = %d",
  528. dbesc($updated),
  529. dbesc($updated),
  530. intval($contact['id'])
  531. );
  532. q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
  533. dbesc($updated),
  534. dbesc($contact['nurl'])
  535. );
  536. } else {
  537. $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d",
  538. dbesc($updated),
  539. intval($contact['id'])
  540. );
  541. }
  542. return;
  543. }