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.
 
 
 
 
 
 

678 lines
22 KiB

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