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.
 
 
 
 
 
 

583 lines
19 KiB

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