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.
 
 
 
 
 
 

534 lines
16 KiB

  1. <?php
  2. require_once("boot.php");
  3. function poller_run($argv, $argc){
  4. global $a, $db;
  5. if(is_null($a)) {
  6. $a = new App;
  7. }
  8. if(is_null($db)) {
  9. @include(".htconfig.php");
  10. require_once("dba.php");
  11. $db = new dba($db_host, $db_user, $db_pass, $db_data);
  12. unset($db_host, $db_user, $db_pass, $db_data);
  13. };
  14. require_once('include/session.php');
  15. require_once('include/datetime.php');
  16. require_once('library/simplepie/simplepie.inc');
  17. require_once('include/items.php');
  18. require_once('include/Contact.php');
  19. require_once('include/email.php');
  20. load_config('config');
  21. load_config('system');
  22. $a->set_baseurl(get_config('system','url'));
  23. load_hooks();
  24. logger('poller: start');
  25. // run queue delivery process in the background
  26. proc_run('php',"include/queue.php");
  27. // expire any expired accounts
  28. q("UPDATE user SET `account_expired` = 1 where `account_expired` = 0
  29. AND `account_expires_on` != '0000-00-00 00:00:00'
  30. AND `account_expires_on` < UTC_TIMESTAMP() ");
  31. $abandon_days = intval(get_config('system','account_abandon_days'));
  32. if($abandon_days < 1)
  33. $abandon_days = 0;
  34. // once daily run expire in background
  35. $d1 = get_config('system','last_expire_day');
  36. $d2 = intval(datetime_convert('UTC','UTC','now','d'));
  37. if($d2 != intval($d1)) {
  38. set_config('system','last_expire_day',$d2);
  39. proc_run('php','include/expire.php');
  40. }
  41. // clear old cache
  42. q("DELETE FROM `cache` WHERE `updated` < '%s'",
  43. dbesc(datetime_convert('UTC','UTC',"now - 30 days")));
  44. $manual_id = 0;
  45. $generation = 0;
  46. $hub_update = false;
  47. $force = false;
  48. $restart = false;
  49. if(($argc > 1) && ($argv[1] == 'force'))
  50. $force = true;
  51. if(($argc > 1) && ($argv[1] == 'restart')) {
  52. $restart = true;
  53. $generation = intval($argv[2]);
  54. if(! $generation)
  55. killme();
  56. }
  57. if(($argc > 1) && intval($argv[1])) {
  58. $manual_id = intval($argv[1]);
  59. $force = true;
  60. }
  61. $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
  62. reload_plugins();
  63. $d = datetime_convert();
  64. if(! $restart)
  65. proc_run('php','include/cronhooks.php');
  66. // Only poll from those with suitable relationships,
  67. // and which have a polling address and ignore Diaspora since
  68. // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
  69. $abandon_sql = (($abandon_days)
  70. ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
  71. : ''
  72. );
  73. $contacts = q("SELECT `contact`.`id` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
  74. WHERE ( `rel` = %d OR `rel` = %d ) AND `poll` != ''
  75. AND `network` != '%s'
  76. $sql_extra
  77. AND `self` = 0 AND `contact`.`blocked` = 0 AND `contact`.`readonly` = 0
  78. AND `user`.`account_expired` = 0 $abandon_sql ORDER BY RAND()",
  79. intval(CONTACT_IS_SHARING),
  80. intval(CONTACT_IS_FRIEND),
  81. dbesc(NETWORK_DIASPORA)
  82. );
  83. if(! count($contacts)) {
  84. return;
  85. }
  86. foreach($contacts as $c) {
  87. $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
  88. intval($c['id'])
  89. );
  90. if((! $res) || (! count($res)))
  91. continue;
  92. foreach($res as $contact) {
  93. $xml = false;
  94. if($manual_id)
  95. $contact['last-update'] = '0000-00-00 00:00:00';
  96. if($contact['priority'] || $contact['subhub']) {
  97. $hub_update = true;
  98. $update = false;
  99. $t = $contact['last-update'];
  100. // We should be getting everything via a hub. But just to be sure, let's check once a day.
  101. // (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
  102. // This also lets us update our subscription to the hub, and add or replace hubs in case it
  103. // changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
  104. if($contact['subhub']) {
  105. $interval = get_config('system','pushpoll_frequency');
  106. $contact['priority'] = (($interval !== false) ? intval($interval) : 3);
  107. $hub_update = false;
  108. if((datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day")) || $force)
  109. $hub_update = true;
  110. }
  111. else
  112. $hub_update = false;
  113. /**
  114. * Based on $contact['priority'], should we poll this site now? Or later?
  115. */
  116. switch ($contact['priority']) {
  117. case 5:
  118. if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
  119. $update = true;
  120. break;
  121. case 4:
  122. if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
  123. $update = true;
  124. break;
  125. case 3:
  126. if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
  127. $update = true;
  128. break;
  129. case 2:
  130. if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
  131. $update = true;
  132. break;
  133. case 1:
  134. default:
  135. if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
  136. $update = true;
  137. break;
  138. }
  139. if((! $update) && (! $force))
  140. continue;
  141. }
  142. // Check to see if we are running out of memory - if so spawn a new process and kill this one
  143. $avail_memory = return_bytes(ini_get('memory_limit'));
  144. $memused = memory_get_peak_usage(true);
  145. if(intval($avail_memory)) {
  146. if(($memused / $avail_memory) > 0.95) {
  147. if($generation + 1 > 10) {
  148. logger('poller: maximum number of spawns exceeded. Terminating.');
  149. killme();
  150. }
  151. logger('poller: memory exceeded. ' . $memused . ' bytes used. Spawning new poll.');
  152. proc_run('php', 'include/poller.php', 'restart', (string) $generation + 1);
  153. killme();
  154. }
  155. }
  156. $importer_uid = $contact['uid'];
  157. $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
  158. intval($importer_uid)
  159. );
  160. if(! count($r))
  161. continue;
  162. $importer = $r[0];
  163. logger("poller: poll: IMPORTER: {$importer['name']}, CONTACT: {$contact['name']}");
  164. $last_update = (($contact['last-update'] === '0000-00-00 00:00:00')
  165. ? datetime_convert('UTC','UTC','now - 30 days', ATOM_TIME)
  166. : datetime_convert('UTC','UTC',$contact['last-update'], ATOM_TIME)
  167. );
  168. if($contact['network'] === NETWORK_DFRN) {
  169. $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
  170. if(intval($contact['duplex']) && $contact['dfrn-id'])
  171. $idtosend = '0:' . $orig_id;
  172. if(intval($contact['duplex']) && $contact['issued-id'])
  173. $idtosend = '1:' . $orig_id;
  174. // they have permission to write to us. We already filtered this in the contact query.
  175. $perm = 'rw';
  176. $url = $contact['poll'] . '?dfrn_id=' . $idtosend
  177. . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
  178. . '&type=data&last_update=' . $last_update
  179. . '&perm=' . $perm ;
  180. $handshake_xml = fetch_url($url);
  181. logger('poller: handshake with url ' . $url . ' returns xml: ' . $handshake_xml, LOGGER_DATA);
  182. if(! $handshake_xml) {
  183. logger("poller: $url appears to be dead - marking for death ");
  184. // dead connection - might be a transient event, or this might
  185. // mean the software was uninstalled or the domain expired.
  186. // Will keep trying for one month.
  187. mark_for_death($contact);
  188. // set the last-update so we don't keep polling
  189. $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
  190. dbesc(datetime_convert()),
  191. intval($contact['id'])
  192. );
  193. continue;
  194. }
  195. if(! strstr($handshake_xml,'<?xml')) {
  196. logger('poller: response from ' . $url . ' did not contain XML.');
  197. $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
  198. dbesc(datetime_convert()),
  199. intval($contact['id'])
  200. );
  201. continue;
  202. }
  203. $res = parse_xml_string($handshake_xml);
  204. if(intval($res->status) == 1) {
  205. logger("poller: $url replied status 1 - marking for death ");
  206. // we may not be friends anymore. Will keep trying for one month.
  207. // set the last-update so we don't keep polling
  208. $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
  209. dbesc(datetime_convert()),
  210. intval($contact['id'])
  211. );
  212. mark_for_death($contact);
  213. }
  214. else {
  215. if($contact['term-date'] != '0000-00-00 00:00:00') {
  216. logger("poller: $url back from the dead - removing mark for death");
  217. unmark_for_death($contact);
  218. }
  219. }
  220. if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
  221. continue;
  222. $postvars = array();
  223. $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
  224. $challenge = hex2bin((string) $res->challenge);
  225. $final_dfrn_id = '';
  226. if(($contact['duplex']) && strlen($contact['prvkey'])) {
  227. openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
  228. openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
  229. }
  230. else {
  231. openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
  232. openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
  233. }
  234. $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
  235. if(strpos($final_dfrn_id,':') == 1)
  236. $final_dfrn_id = substr($final_dfrn_id,2);
  237. if($final_dfrn_id != $orig_id) {
  238. logger('poller: ID did not decode: ' . $contact['id'] . ' orig: ' . $orig_id . ' final: ' . $final_dfrn_id);
  239. // did not decode properly - cannot trust this site
  240. continue;
  241. }
  242. $postvars['dfrn_id'] = $idtosend;
  243. $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
  244. $postvars['perm'] = 'rw';
  245. $xml = post_url($contact['poll'],$postvars);
  246. }
  247. elseif(($contact['network'] === NETWORK_OSTATUS)
  248. || ($contact['network'] === NETWORK_DIASPORA)
  249. || ($contact['network'] === NETWORK_FEED) ) {
  250. // Upgrading DB fields from an older Friendika version
  251. // Will only do this once per notify-enabled OStatus contact
  252. // or if relationship changes
  253. $stat_writeable = ((($contact['notify']) && ($contact['rel'] == CONTACT_IS_FOLLOWER || $contact['rel'] == CONTACT_IS_FRIEND)) ? 1 : 0);
  254. if($stat_writeable != $contact['writable']) {
  255. q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d LIMIT 1",
  256. intval($stat_writeable),
  257. intval($contact['id'])
  258. );
  259. }
  260. // Are we allowed to import from this person?
  261. if($contact['rel'] == CONTACT_IS_FOLLOWER || $contact['blocked'] || $contact['readonly'])
  262. continue;
  263. $xml = fetch_url($contact['poll']);
  264. }
  265. elseif($contact['network'] === NETWORK_MAIL) {
  266. $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
  267. if($mail_disabled)
  268. continue;
  269. $mbox = null;
  270. $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1",
  271. intval($importer_uid)
  272. );
  273. $mailconf = q("SELECT * FROM `mailacct` WHERE `server` != '' AND `uid` = %d LIMIT 1",
  274. intval($importer_uid)
  275. );
  276. if(count($x) && count($mailconf)) {
  277. $mailbox = construct_mailbox_name($mailconf[0]);
  278. $password = '';
  279. openssl_private_decrypt(hex2bin($mailconf[0]['pass']),$password,$x[0]['prvkey']);
  280. $mbox = email_connect($mailbox,$mailconf[0]['user'],$password);
  281. unset($password);
  282. if($mbox) {
  283. q("UPDATE `mailacct` SET `last_check` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
  284. dbesc(datetime_convert()),
  285. intval($mailconf[0]['id']),
  286. intval($importer_uid)
  287. );
  288. }
  289. }
  290. if($mbox) {
  291. $msgs = email_poll($mbox,$contact['addr']);
  292. if(count($msgs)) {
  293. foreach($msgs as $msg_uid) {
  294. $datarray = array();
  295. $meta = email_msg_meta($mbox,$msg_uid);
  296. $headers = email_msg_headers($mbox,$msg_uid);
  297. // look for a 'references' header and try and match with a parent item we have locally.
  298. $raw_refs = ((x($headers,'references')) ? str_replace("\t",'',$headers['references']) : '');
  299. $datarray['uri'] = trim($meta->message_id,'<>');
  300. if($raw_refs) {
  301. $refs_arr = explode(' ', $raw_refs);
  302. if(count($refs_arr)) {
  303. for($x = 0; $x < count($refs_arr); $x ++)
  304. $refs_arr[$x] = "'" . str_replace(array('<','>',' '),array('','',''),dbesc($refs_arr[$x])) . "'";
  305. }
  306. $qstr = implode(',',$refs_arr);
  307. $r = q("SELECT `uri` , `parent-uri` FROM `item` WHERE `uri` IN ( $qstr ) AND `uid` = %d LIMIT 1",
  308. intval($importer_uid)
  309. );
  310. if(count($r))
  311. $datarray['parent-uri'] = $r[0]['uri'];
  312. }
  313. if(! x($datarray,'parent-uri'))
  314. $datarray['parent-uri'] = $datarray['uri'];
  315. // Have we seen it before?
  316. $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
  317. intval($importer_uid),
  318. dbesc($datarray['uri'])
  319. );
  320. if(count($r)) {
  321. if($meta->deleted && ! $r[0]['deleted']) {
  322. q("UPDATE `item` SET `deleted` = 1, `changed` = '%s' WHERE `id` = %d LIMIT 1",
  323. dbesc(datetime_convert()),
  324. intval($r[0]['id'])
  325. );
  326. }
  327. continue;
  328. }
  329. $datarray['title'] = notags(trim($meta->subject));
  330. $datarray['created'] = datetime_convert('UTC','UTC',$meta->date);
  331. $r = email_get_msg($mbox,$msg_uid);
  332. if(! $r)
  333. continue;
  334. $datarray['body'] = escape_tags($r['body']);
  335. // some mailing lists have the original author as 'from' - add this sender info to msg body.
  336. // todo: adding a gravatar for the original author would be cool
  337. if(! stristr($meta->from,$contact['addr']))
  338. $datarray['body'] = t('From: ') . escape_tags($meta->from) . "\n\n" . $datarray['body'];
  339. $datarray['uid'] = $importer_uid;
  340. $datarray['contact-id'] = $contact['id'];
  341. if($datarray['parent-uri'] === $datarray['uri'])
  342. $datarray['private'] = 1;
  343. if(! get_pconfig($importer_uid,'system','allow_public_email_replies')) {
  344. $datarray['private'] = 1;
  345. $datarray['allow_cid'] = '<' . $contact['id'] . '>';
  346. }
  347. $datarray['author-name'] = $contact['name'];
  348. $datarray['author-link'] = 'mailbox';
  349. $datarray['author-avatar'] = $contact['photo'];
  350. $stored_item = item_store($datarray);
  351. q("UPDATE `item` SET `last-child` = 0 WHERE `parent-uri` = '%s' AND `uid` = %d",
  352. dbesc($datarray['parent-uri']),
  353. intval($importer_uid)
  354. );
  355. q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
  356. intval($stored_item)
  357. );
  358. }
  359. }
  360. imap_close($mbox);
  361. }
  362. }
  363. elseif($contact['network'] === NETWORK_FACEBOOK) {
  364. // This is picked up by the Facebook plugin on a cron hook.
  365. // Ignored here.
  366. }
  367. if($xml) {
  368. logger('poller: received xml : ' . $xml, LOGGER_DATA);
  369. if(! strstr($xml,'<?xml')) {
  370. logger('poller: post_handshake: response from ' . $url . ' did not contain XML.');
  371. $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
  372. dbesc(datetime_convert()),
  373. intval($contact['id'])
  374. );
  375. continue;
  376. }
  377. consume_feed($xml,$importer,$contact,$hub,1, true);
  378. // do it twice. Ensures that children of parents which may be later in the stream aren't tossed
  379. consume_feed($xml,$importer,$contact,$hub,1);
  380. $hubmode = 'subscribe';
  381. if($contact['network'] === NETWORK_DFRN || $contact['blocked'] || $contact['readonly'])
  382. $hubmode = 'unsubscribe';
  383. if((strlen($hub)) && ($hub_update) && ($contact['rel'] != CONTACT_IS_FOLLOWER)) {
  384. logger('poller: hub ' . $hubmode . ' : ' . $hub . ' contact name : ' . $contact['name'] . ' local user : ' . $importer['name']);
  385. $hubs = explode(',', $hub);
  386. if(count($hubs)) {
  387. foreach($hubs as $h) {
  388. $h = trim($h);
  389. if(! strlen($h))
  390. continue;
  391. subscribe_to_hub($h,$importer,$contact,$hubmode);
  392. }
  393. }
  394. }
  395. }
  396. $updated = datetime_convert();
  397. $r = q("UPDATE `contact` SET `last-update` = '%s', `success_update` = '%s' WHERE `id` = %d LIMIT 1",
  398. dbesc($updated),
  399. dbesc($updated),
  400. intval($contact['id'])
  401. );
  402. // loop - next contact
  403. }
  404. }
  405. return;
  406. }
  407. if (array_search(__file__,get_included_files())===0){
  408. poller_run($argv,$argc);
  409. killme();
  410. }