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.

687 lines
22 KiB

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