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.

675 lines
22KB

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