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.

922 lines
27 KiB

9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
9 years ago
  1. <?php
  2. require_once("boot.php");
  3. require_once('include/queue_fn.php');
  4. require_once('include/html2plain.php');
  5. /*
  6. * This file was at one time responsible for doing all deliveries, but this caused
  7. * big problems on shared hosting systems, where the process might get killed by the
  8. * hosting provider and nothing would get delivered.
  9. * It now only delivers one message under certain cases, and invokes a queued
  10. * delivery mechanism (include/deliver.php) to deliver individual contacts at
  11. * controlled intervals.
  12. * This has a much better chance of surviving random processes getting killed
  13. * by the hosting provider.
  14. * A lot of this code is duplicated in include/deliver.php until we have time to go back
  15. * and re-structure the delivery procedure based on the obstacles that have been thrown at
  16. * us by hosting providers.
  17. */
  18. /*
  19. * The notifier is typically called with:
  20. *
  21. * proc_run('php', "include/notifier.php", COMMAND, ITEM_ID);
  22. *
  23. * where COMMAND is one of the following:
  24. *
  25. * activity (in diaspora.php, dfrn_confirm.php, profiles.php)
  26. * comment-import (in diaspora.php, items.php)
  27. * comment-new (in item.php)
  28. * drop (in diaspora.php, items.php, photos.php)
  29. * edit_post (in item.php)
  30. * event (in events.php)
  31. * expire (in items.php)
  32. * like (in like.php, poke.php)
  33. * mail (in message.php)
  34. * suggest (in fsuggest.php)
  35. * tag (in photos.php, poke.php, tagger.php)
  36. * tgroup (in items.php)
  37. * wall-new (in photos.php, item.php)
  38. *
  39. * and ITEM_ID is the id of the item in the database that needs to be sent to others.
  40. */
  41. function notifier_run($argv, $argc){
  42. global $a, $db;
  43. if(is_null($a)){
  44. $a = new App;
  45. }
  46. if(is_null($db)) {
  47. @include(".htconfig.php");
  48. require_once("dba.php");
  49. $db = new dba($db_host, $db_user, $db_pass, $db_data);
  50. unset($db_host, $db_user, $db_pass, $db_data);
  51. }
  52. require_once("session.php");
  53. require_once("datetime.php");
  54. require_once('include/items.php');
  55. require_once('include/bbcode.php');
  56. require_once('include/email.php');
  57. load_config('config');
  58. load_config('system');
  59. load_hooks();
  60. if($argc < 3)
  61. return;
  62. $a->set_baseurl(get_config('system','url'));
  63. logger('notifier: invoked: ' . print_r($argv,true), LOGGER_DEBUG);
  64. $cmd = $argv[1];
  65. switch($cmd) {
  66. case 'mail':
  67. default:
  68. $item_id = intval($argv[2]);
  69. if(! $item_id){
  70. return;
  71. }
  72. break;
  73. }
  74. $expire = false;
  75. $mail = false;
  76. $fsuggest = false;
  77. $top_level = false;
  78. $recipients = array();
  79. $url_recipients = array();
  80. $normal_mode = true;
  81. if($cmd === 'mail') {
  82. $normal_mode = false;
  83. $mail = true;
  84. $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
  85. intval($item_id)
  86. );
  87. if(! count($message)){
  88. return;
  89. }
  90. $uid = $message[0]['uid'];
  91. $recipients[] = $message[0]['contact-id'];
  92. $item = $message[0];
  93. }
  94. elseif($cmd === 'expire') {
  95. $normal_mode = false;
  96. $expire = true;
  97. $items = q("SELECT * FROM `item` WHERE `uid` = %d AND `wall` = 1
  98. AND `deleted` = 1 AND `changed` > UTC_TIMESTAMP() - INTERVAL 10 MINUTE",
  99. intval($item_id)
  100. );
  101. $uid = $item_id;
  102. $item_id = 0;
  103. if(! count($items))
  104. return;
  105. }
  106. elseif($cmd === 'suggest') {
  107. $normal_mode = false;
  108. $fsuggest = true;
  109. $suggest = q("SELECT * FROM `fsuggest` WHERE `id` = %d LIMIT 1",
  110. intval($item_id)
  111. );
  112. if(! count($suggest))
  113. return;
  114. $uid = $suggest[0]['uid'];
  115. $recipients[] = $suggest[0]['cid'];
  116. $item = $suggest[0];
  117. }
  118. elseif($cmd === 'removeme') {
  119. $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval($item_id));
  120. $user = $r[0];
  121. $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1", intval($item_id));
  122. $self = $r[0];
  123. $r = q("SELECT * FROM `contact` WHERE `self` = 0 AND `uid` = %d", intval($item_id));
  124. if(! count($r))
  125. return;
  126. require_once('include/Contact.php');
  127. foreach($r as $contact) {
  128. terminate_friendship($user, $self, $contact);
  129. }
  130. return;
  131. }
  132. else {
  133. // find ancestors
  134. $r = q("SELECT * FROM `item` WHERE `id` = %d and visible = 1 and moderated = 0 LIMIT 1",
  135. intval($item_id)
  136. );
  137. if((! count($r)) || (! intval($r[0]['parent']))) {
  138. return;
  139. }
  140. $target_item = $r[0];
  141. $parent_id = intval($r[0]['parent']);
  142. $uid = $r[0]['uid'];
  143. $updated = $r[0]['edited'];
  144. // POSSIBLE CLEANUP --> The following seems superfluous. We've already checked for "if (! intval($r[0]['parent']))" a few lines up
  145. if(! $parent_id)
  146. return;
  147. $items = q("SELECT `item`.*, `sign`.`signed_text`,`sign`.`signature`,`sign`.`signer`
  148. FROM `item` LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id` WHERE `parent` = %d and visible = 1 and moderated = 0 ORDER BY `id` ASC",
  149. intval($parent_id)
  150. );
  151. if(! count($items)) {
  152. return;
  153. }
  154. // avoid race condition with deleting entries
  155. if($items[0]['deleted']) {
  156. foreach($items as $item)
  157. $item['deleted'] = 1;
  158. }
  159. if((count($items) == 1) && ($items[0]['id'] === $target_item['id']) && ($items[0]['uri'] === $items[0]['parent-uri'])) {
  160. logger('notifier: top level post');
  161. $top_level = true;
  162. }
  163. }
  164. $r = q("SELECT `contact`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`,
  165. `user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`,
  166. `user`.`page-flags`, `user`.`prvnets`
  167. FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
  168. WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
  169. intval($uid)
  170. );
  171. if(! count($r))
  172. return;
  173. $owner = $r[0];
  174. $walltowall = ((($top_level) && ($owner['id'] != $items[0]['contact-id'])) ? true : false);
  175. $hub = get_config('system','huburl');
  176. // If this is a public conversation, notify the feed hub
  177. $public_message = true;
  178. // fill this in with a single salmon slap if applicable
  179. $slap = '';
  180. if(! ($mail || $fsuggest)) {
  181. require_once('include/group.php');
  182. $parent = $items[0];
  183. // This is IMPORTANT!!!!
  184. // We will only send a "notify owner to relay" or followup message if the referenced post
  185. // originated on our system by virtue of having our hostname somewhere
  186. // in the URI, AND it was a comment (not top_level) AND the parent originated elsewhere.
  187. // if $parent['wall'] == 1 we will already have the parent message in our array
  188. // and we will relay the whole lot.
  189. // expire sends an entire group of expire messages and cannot be forwarded.
  190. // However the conversation owner will be a part of the conversation and will
  191. // be notified during this run.
  192. // Other DFRN conversation members will be alerted during polled updates.
  193. // Diaspora members currently are not notified of expirations, and other networks have
  194. // either limited or no ability to process deletions. We should at least fix Diaspora
  195. // by stringing togther an array of retractions and sending them onward.
  196. $localhost = str_replace('www.','',$a->get_hostname());
  197. if(strpos($localhost,':'))
  198. $localhost = substr($localhost,0,strpos($localhost,':'));
  199. /**
  200. *
  201. * Be VERY CAREFUL if you make any changes to the following several lines. Seemingly innocuous changes
  202. * have been known to cause runaway conditions which affected several servers, along with
  203. * permissions issues.
  204. *
  205. */
  206. $relay_to_owner = false;
  207. if((! $top_level) && ($parent['wall'] == 0) && (! $expire) && (stristr($target_item['uri'],$localhost))) {
  208. $relay_to_owner = true;
  209. }
  210. if(($cmd === 'uplink') && (intval($parent['forum_mode']) == 1) && (! $top_level)) {
  211. $relay_to_owner = true;
  212. }
  213. // until the 'origin' flag has been in use for several months
  214. // we will just use it as a fallback test
  215. // later we will be able to use it as the primary test of whether or not to relay.
  216. if(! $target_item['origin'])
  217. $relay_to_owner = false;
  218. if($parent['origin'])
  219. $relay_to_owner = false;
  220. if($relay_to_owner) {
  221. logger('notifier: followup', LOGGER_DEBUG);
  222. // local followup to remote post
  223. $followup = true;
  224. $public_message = false; // not public
  225. $conversant_str = dbesc($parent['contact-id']);
  226. }
  227. else {
  228. $followup = false;
  229. // don't send deletions onward for other people's stuff
  230. if($target_item['deleted'] && (! intval($target_item['wall']))) {
  231. logger('notifier: ignoring delete notification for non-wall item');
  232. return;
  233. }
  234. if((strlen($parent['allow_cid']))
  235. || (strlen($parent['allow_gid']))
  236. || (strlen($parent['deny_cid']))
  237. || (strlen($parent['deny_gid']))) {
  238. $public_message = false; // private recipients, not public
  239. }
  240. $allow_people = expand_acl($parent['allow_cid']);
  241. $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
  242. $deny_people = expand_acl($parent['deny_cid']);
  243. $deny_groups = expand_groups(expand_acl($parent['deny_gid']));
  244. // if our parent is a public forum (forum_mode == 1), uplink to the origional author causing
  245. // a delivery fork. private groups (forum_mode == 2) do not uplink
  246. if((intval($parent['forum_mode']) == 1) && (! $top_level) && ($cmd !== 'uplink')) {
  247. proc_run('php','include/notifier.php','uplink',$item_id);
  248. }
  249. $conversants = array();
  250. foreach($items as $item) {
  251. $recipients[] = $item['contact-id'];
  252. $conversants[] = $item['contact-id'];
  253. // pull out additional tagged people to notify (if public message)
  254. if($public_message && strlen($item['inform'])) {
  255. $people = explode(',',$item['inform']);
  256. foreach($people as $person) {
  257. if(substr($person,0,4) === 'cid:') {
  258. $recipients[] = intval(substr($person,4));
  259. $conversants[] = intval(substr($person,4));
  260. }
  261. else {
  262. $url_recipients[] = substr($person,4);
  263. }
  264. }
  265. }
  266. }
  267. logger('notifier: url_recipients' . print_r($url_recipients,true));
  268. $conversants = array_unique($conversants);
  269. $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups));
  270. $deny = array_unique(array_merge($deny_people,$deny_groups));
  271. $recipients = array_diff($recipients,$deny);
  272. $conversant_str = dbesc(implode(', ',$conversants));
  273. }
  274. $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0");
  275. if(count($r))
  276. $contacts = $r;
  277. }
  278. $feed_template = get_markup_template('atom_feed.tpl');
  279. $mail_template = get_markup_template('atom_mail.tpl');
  280. $atom = '';
  281. $slaps = array();
  282. $hubxml = feed_hublinks();
  283. $birthday = feed_birthday($owner['uid'],$owner['timezone']);
  284. if(strlen($birthday))
  285. $birthday = '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>';
  286. $atom .= replace_macros($feed_template, array(
  287. '$version' => xmlify(FRIENDICA_VERSION),
  288. '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
  289. '$feed_title' => xmlify($owner['name']),
  290. '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
  291. '$hub' => $hubxml,
  292. '$salmon' => '', // private feed, we don't use salmon here
  293. '$name' => xmlify($owner['name']),
  294. '$profile_page' => xmlify($owner['url']),
  295. '$photo' => xmlify($owner['photo']),
  296. '$thumb' => xmlify($owner['thumb']),
  297. '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
  298. '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
  299. '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME)) ,
  300. '$birthday' => $birthday,
  301. '$community' => (($owner['page-flags'] == PAGE_COMMUNITY) ? '<dfrn:community>1</dfrn:community>' : '')
  302. ));
  303. if($mail) {
  304. $public_message = false; // mail is not public
  305. $body = fix_private_photos($item['body'],$owner['uid'],null,$message[0]['contact-id']);
  306. $atom .= replace_macros($mail_template, array(
  307. '$name' => xmlify($owner['name']),
  308. '$profile_page' => xmlify($owner['url']),
  309. '$thumb' => xmlify($owner['thumb']),
  310. '$item_id' => xmlify($item['uri']),
  311. '$subject' => xmlify($item['title']),
  312. '$created' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
  313. '$content' => xmlify($body),
  314. '$parent_id' => xmlify($item['parent-uri'])
  315. ));
  316. }
  317. elseif($fsuggest) {
  318. $public_message = false; // suggestions are not public
  319. $sugg_template = get_markup_template('atom_suggest.tpl');
  320. $atom .= replace_macros($sugg_template, array(
  321. '$name' => xmlify($item['name']),
  322. '$url' => xmlify($item['url']),
  323. '$photo' => xmlify($item['photo']),
  324. '$request' => xmlify($item['request']),
  325. '$note' => xmlify($item['note'])
  326. ));
  327. // We don't need this any more
  328. q("DELETE FROM `fsuggest` WHERE `id` = %d LIMIT 1",
  329. intval($item['id'])
  330. );
  331. }
  332. else {
  333. if($followup) {
  334. foreach($items as $item) { // there is only one item
  335. if(! $item['parent'])
  336. continue;
  337. if($item['id'] == $item_id) {
  338. logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
  339. $slap = atom_entry($item,'html',null,$owner,false);
  340. $atom .= atom_entry($item,'text',null,$owner,false);
  341. }
  342. }
  343. }
  344. else {
  345. foreach($items as $item) {
  346. if(! $item['parent'])
  347. continue;
  348. // private emails may be in included in public conversations. Filter them.
  349. if(($public_message) && $item['private'] == 1)
  350. continue;
  351. $contact = get_item_contact($item,$contacts);
  352. if(! $contact)
  353. continue;
  354. if($normal_mode) {
  355. // we only need the current item, but include the parent because without it
  356. // older sites without a corresponding dfrn_notify change may do the wrong thing.
  357. if($item_id == $item['id'] || $item['id'] == $item['parent'])
  358. $atom .= atom_entry($item,'text',null,$owner,true);
  359. }
  360. else
  361. $atom .= atom_entry($item,'text',null,$owner,true);
  362. if(($top_level) && ($public_message) && ($item['author-link'] === $item['owner-link']) && (! $expire))
  363. $slaps[] = atom_entry($item,'html',null,$owner,true);
  364. }
  365. }
  366. }
  367. $atom .= '</feed>' . "\r\n";
  368. logger('notifier: ' . $atom, LOGGER_DATA);
  369. logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
  370. // If this is a public message and pubmail is set on the parent, include all your email contacts
  371. $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
  372. if(! $mail_disabled) {
  373. if((! strlen($target_item['allow_cid'])) && (! strlen($target_item['allow_gid']))
  374. && (! strlen($target_item['deny_cid'])) && (! strlen($target_item['deny_gid']))
  375. && (intval($target_item['pubmail']))) {
  376. $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `network` = '%s'",
  377. intval($uid),
  378. dbesc(NETWORK_MAIL)
  379. );
  380. if(count($r)) {
  381. foreach($r as $rr)
  382. $recipients[] = $rr['id'];
  383. }
  384. }
  385. }
  386. if($followup)
  387. $recip_str = $parent['contact-id'];
  388. else
  389. $recip_str = implode(', ', $recipients);
  390. $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ",
  391. dbesc($recip_str)
  392. );
  393. require_once('include/salmon.php');
  394. $interval = ((get_config('system','delivery_interval') === false) ? 2 : intval(get_config('system','delivery_interval')));
  395. // delivery loop
  396. if(count($r)) {
  397. foreach($r as $contact) {
  398. if((! $mail) && (! $fsuggest) && (! $followup) && (! $contact['self'])) {
  399. if(($contact['network'] === NETWORK_DIASPORA) && ($public_message))
  400. continue;
  401. q("insert into deliverq ( `cmd`,`item`,`contact` ) values ('%s', %d, %d )",
  402. dbesc($cmd),
  403. intval($item_id),
  404. intval($contact['id'])
  405. );
  406. }
  407. }
  408. // This controls the number of deliveries to execute with each separate delivery process.
  409. // By default we'll perform one delivery per process. Assuming a hostile shared hosting
  410. // provider, this provides the greatest chance of deliveries if processes start getting
  411. // killed. We can also space them out with the delivery_interval to also help avoid them
  412. // getting whacked.
  413. // If $deliveries_per_process > 1, we will chain this number of multiple deliveries
  414. // together into a single process. This will reduce the overall number of processes
  415. // spawned for each delivery, but they will run longer.
  416. $deliveries_per_process = intval(get_config('system','delivery_batch_count'));
  417. if($deliveries_per_process <= 0)
  418. $deliveries_per_process = 1;
  419. $this_batch = array();
  420. for($x = 0; $x < count($r); $x ++) {
  421. $contact = $r[$x];
  422. if($contact['self'])
  423. continue;
  424. // potentially more than one recipient. Start a new process and space them out a bit.
  425. // we will deliver single recipient types of message and email recipients here.
  426. if((! $mail) && (! $fsuggest) && (! $followup)) {
  427. $this_batch[] = $contact['id'];
  428. if(count($this_batch) == $deliveries_per_process) {
  429. proc_run('php','include/delivery.php',$cmd,$item_id,$this_batch);
  430. $this_batch = array();
  431. if($interval)
  432. @time_sleep_until(microtime(true) + (float) $interval);
  433. }
  434. continue;
  435. }
  436. $deliver_status = 0;
  437. logger("main delivery by notifier: followup=$followup mail=$mail fsuggest=$fsuggest");
  438. switch($contact['network']) {
  439. case NETWORK_DFRN:
  440. // perform local delivery if we are on the same site
  441. $basepath = implode('/', array_slice(explode('/',$contact['url']),0,3));
  442. if(link_compare($basepath,$a->get_baseurl())) {
  443. $nickname = basename($contact['url']);
  444. if($contact['issued-id'])
  445. $sql_extra = sprintf(" AND `dfrn-id` = '%s' ", dbesc($contact['issued-id']));
  446. else
  447. $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($contact['dfrn-id']));
  448. $x = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`,
  449. `contact`.`pubkey` AS `cpubkey`,
  450. `contact`.`prvkey` AS `cprvkey`,
  451. `contact`.`thumb` AS `thumb`,
  452. `contact`.`url` as `url`,
  453. `contact`.`name` as `senderName`,
  454. `user`.*
  455. FROM `contact`
  456. LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
  457. WHERE `contact`.`blocked` = 0 AND `contact`.`archive` = 0
  458. AND `contact`.`pending` = 0
  459. AND `contact`.`network` = '%s' AND `user`.`nickname` = '%s'
  460. $sql_extra
  461. AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 LIMIT 1",
  462. dbesc(NETWORK_DFRN),
  463. dbesc($nickname)
  464. );
  465. if($x && count($x)) {
  466. $write_flag = ((($x[0]['rel']) && ($x[0]['rel'] != CONTACT_IS_SHARING)) ? true : false);
  467. if((($owner['page-flags'] == PAGE_COMMUNITY) || ($write_flag)) && (! $x[0]['writable'])) {
  468. q("update contact set writable = 1 where id = %d limit 1",
  469. intval($x[0]['id'])
  470. );
  471. $x[0]['writable'] = 1;
  472. }
  473. // if contact's ssl policy changed, which we just determined
  474. // is on our own server, update our contact links
  475. $ssl_policy = get_config('system','ssl_policy');
  476. fix_contact_ssl_policy($x[0],$ssl_policy);
  477. // If we are setup as a soapbox we aren't accepting input from this person
  478. if($x[0]['page-flags'] == PAGE_SOAPBOX)
  479. break;
  480. require_once('library/simplepie/simplepie.inc');
  481. logger('mod-delivery: local delivery');
  482. local_delivery($x[0],$atom);
  483. break;
  484. }
  485. }
  486. logger('notifier: dfrndelivery: ' . $contact['name']);
  487. $deliver_status = dfrn_deliver($owner,$contact,$atom);
  488. logger('notifier: dfrn_delivery returns ' . $deliver_status);
  489. if($deliver_status == (-1)) {
  490. logger('notifier: delivery failed: queuing message');
  491. // queue message for redelivery
  492. add_to_queue($contact['id'],NETWORK_DFRN,$atom);
  493. }
  494. break;
  495. case NETWORK_OSTATUS:
  496. // Do not send to ostatus if we are not configured to send to public networks
  497. if($owner['prvnets'])
  498. break;
  499. if(get_config('system','ostatus_disabled') || get_config('system','dfrn_only'))
  500. break;
  501. if($followup && $contact['notify']) {
  502. logger('notifier: slapdelivery: ' . $contact['name']);
  503. $deliver_status = slapper($owner,$contact['notify'],$slap);
  504. if($deliver_status == (-1)) {
  505. // queue message for redelivery
  506. add_to_queue($contact['id'],NETWORK_OSTATUS,$slap);
  507. }
  508. }
  509. else {
  510. // only send salmon if public - e.g. if it's ok to notify
  511. // a public hub, it's ok to send a salmon
  512. if((count($slaps)) && ($public_message) && (! $expire)) {
  513. logger('notifier: slapdelivery: ' . $contact['name']);
  514. foreach($slaps as $slappy) {
  515. if($contact['notify']) {
  516. $deliver_status = slapper($owner,$contact['notify'],$slappy);
  517. if($deliver_status == (-1)) {
  518. // queue message for redelivery
  519. add_to_queue($contact['id'],NETWORK_OSTATUS,$slappy);
  520. }
  521. }
  522. }
  523. }
  524. }
  525. break;
  526. case NETWORK_MAIL:
  527. case NETWORK_MAIL2:
  528. if(get_config('system','dfrn_only'))
  529. break;
  530. // WARNING: does not currently convert to RFC2047 header encodings, etc.
  531. $addr = $contact['addr'];
  532. if(! strlen($addr))
  533. break;
  534. if($cmd === 'wall-new' || $cmd === 'comment-new') {
  535. $it = null;
  536. if($cmd === 'wall-new')
  537. $it = $items[0];
  538. else {
  539. $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
  540. intval($argv[2]),
  541. intval($uid)
  542. );
  543. if(count($r))
  544. $it = $r[0];
  545. }
  546. if(! $it)
  547. break;
  548. $local_user = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
  549. intval($uid)
  550. );
  551. if(! count($local_user))
  552. break;
  553. $reply_to = '';
  554. $r1 = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
  555. intval($uid)
  556. );
  557. if($r1 && $r1[0]['reply_to'])
  558. $reply_to = $r1[0]['reply_to'];
  559. $subject = (($it['title']) ? email_header_encode($it['title'],'UTF-8') : t("\x28no subject\x29")) ;
  560. // only expose our real email address to true friends
  561. if(($contact['rel'] == CONTACT_IS_FRIEND) && (! $contact['blocked']))
  562. $headers = 'From: ' . email_header_encode($local_user[0]['username'],'UTF-8') . ' <' . $local_user[0]['email'] . '>' . "\n";
  563. else
  564. $headers = 'From: ' . email_header_encode($local_user[0]['username'],'UTF-8') . ' <' . t('noreply') . '@' . $a->get_hostname() . '>' . "\n";
  565. if($reply_to)
  566. $headers .= 'Reply-to: ' . $reply_to . "\n";
  567. // for testing purposes: Collect exported mails
  568. //$file = tempnam("/tmp/friendica/", "mail-out2-");
  569. //file_put_contents($file, json_encode($it));
  570. $headers .= 'Message-Id: <' . iri2msgid($it['uri']) . '>' . "\n";
  571. if($it['uri'] !== $it['parent-uri']) {
  572. $headers .= 'References: <' . iri2msgid($it['parent-uri']) . '>' . "\n";
  573. if(!strlen($it['title'])) {
  574. $r = q("SELECT `title` FROM `item` WHERE `parent-uri` = '%s' LIMIT 1",
  575. dbesc($it['parent-uri']));
  576. if(count($r) AND ($r[0]['title'] != ''))
  577. $subject = $r[0]['title'];
  578. }
  579. if(strncasecmp($subject,'RE:',3))
  580. $subject = 'Re: '.$subject;
  581. }
  582. email_send($addr, $subject, $headers, $it);
  583. }
  584. break;
  585. case NETWORK_DIASPORA:
  586. require_once('include/diaspora.php');
  587. if(get_config('system','dfrn_only') || (! get_config('system','diaspora_enabled')))
  588. break;
  589. if($mail) {
  590. diaspora_send_mail($item,$owner,$contact);
  591. break;
  592. }
  593. if(! $normal_mode)
  594. break;
  595. // special handling for followup to public post
  596. // all other public posts processed as public batches further below
  597. if($public_message) {
  598. if($followup)
  599. diaspora_send_followup($target_item,$owner,$contact, true);
  600. break;
  601. }
  602. if(! $contact['pubkey'])
  603. break;
  604. if($target_item['verb'] === ACTIVITY_DISLIKE) {
  605. // unsupported
  606. break;
  607. }
  608. elseif(($target_item['deleted']) && (($target_item['uri'] === $target_item['parent-uri']) || $followup)) {
  609. // send both top-level retractions and relayable retractions for owner to relay
  610. diaspora_send_retraction($target_item,$owner,$contact);
  611. break;
  612. }
  613. elseif($followup) {
  614. // send comments and likes to owner to relay
  615. diaspora_send_followup($target_item,$owner,$contact);
  616. break;
  617. }
  618. elseif($target_item['uri'] !== $target_item['parent-uri']) {
  619. // we are the relay - send comments, likes and relayable_retractions
  620. // (of comments and likes) to our conversants
  621. diaspora_send_relay($target_item,$owner,$contact);
  622. break;
  623. }
  624. elseif(($top_level) && (! $walltowall)) {
  625. // currently no workable solution for sending walltowall
  626. diaspora_send_status($target_item,$owner,$contact);
  627. break;
  628. }
  629. break;
  630. case NETWORK_FEED:
  631. case NETWORK_FACEBOOK:
  632. if(get_config('system','dfrn_only'))
  633. break;
  634. default:
  635. break;
  636. }
  637. }
  638. }
  639. // send additional slaps to mentioned remote tags (@foo@example.com)
  640. if($slap && count($url_recipients) && ($followup || $top_level) && $public_message && (! $expire)) {
  641. if(! get_config('system','dfrn_only')) {
  642. foreach($url_recipients as $url) {
  643. if($url) {
  644. logger('notifier: urldelivery: ' . $url);
  645. $deliver_status = slapper($owner,$url,$slap);
  646. // TODO: redeliver/queue these items on failure, though there is no contact record
  647. }
  648. }
  649. }
  650. }
  651. if($public_message) {
  652. $r1 = q("SELECT DISTINCT(`batch`), `id`, `name`,`network` FROM `contact` WHERE `network` = '%s'
  653. AND `uid` = %d AND `rel` != %d group by `batch` ORDER BY rand() ",
  654. dbesc(NETWORK_DIASPORA),
  655. intval($owner['uid']),
  656. intval(CONTACT_IS_SHARING)
  657. );
  658. $r2 = q("SELECT `id`, `name`,`network` FROM `contact`
  659. WHERE `network` in ( '%s', '%s') AND `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
  660. AND `rel` != %d order by rand() ",
  661. dbesc(NETWORK_DFRN),
  662. dbesc(NETWORK_MAIL2),
  663. intval($owner['uid']),
  664. intval(CONTACT_IS_SHARING)
  665. );
  666. $r = array_merge($r2,$r1);
  667. if(count($r)) {
  668. logger('pubdeliver: ' . print_r($r,true), LOGGER_DEBUG);
  669. // throw everything into the queue in case we get killed
  670. foreach($r as $rr) {
  671. if((! $mail) && (! $fsuggest) && (! $followup)) {
  672. q("insert into deliverq ( `cmd`,`item`,`contact` ) values ('%s', %d, %d )",
  673. dbesc($cmd),
  674. intval($item_id),
  675. intval($rr['id'])
  676. );
  677. }
  678. }
  679. foreach($r as $rr) {
  680. // except for Diaspora batch jobs
  681. // Don't deliver to folks who have already been delivered to
  682. if(($rr['network'] !== NETWORK_DIASPORA) && (in_array($rr['id'],$conversants))) {
  683. logger('notifier: already delivered id=' . $rr['id']);
  684. continue;
  685. }
  686. if((! $mail) && (! $fsuggest) && (! $followup)) {
  687. logger('notifier: delivery agent: ' . $rr['name'] . ' ' . $rr['id']);
  688. proc_run('php','include/delivery.php',$cmd,$item_id,$rr['id']);
  689. if($interval)
  690. @time_sleep_until(microtime(true) + (float) $interval);
  691. }
  692. }
  693. }
  694. if(strlen($hub)) {
  695. $hubs = explode(',', $hub);
  696. if(count($hubs)) {
  697. foreach($hubs as $h) {
  698. $h = trim($h);
  699. if(! strlen($h))
  700. continue;
  701. $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
  702. post_url($h,$params);
  703. logger('pubsub: publish: ' . $h . ' ' . $params . ' returned ' . $a->get_curl_code());
  704. if(count($hubs) > 1)
  705. sleep(7); // try and avoid multiple hubs responding at precisely the same time
  706. }
  707. }
  708. }
  709. }
  710. // If the item was deleted, clean up the `sign` table
  711. if($target_item['deleted']) {
  712. $r = q("DELETE FROM sign where `retract_iid` = %d",
  713. intval($target_item['id'])
  714. );
  715. }
  716. logger('notifier: calling hooks', LOGGER_DEBUG);
  717. if($normal_mode)
  718. call_hooks('notifier_normal',$target_item);
  719. call_hooks('notifier_end',$target_item);
  720. return;
  721. }
  722. if (array_search(__file__,get_included_files())===0){
  723. notifier_run($argv,$argc);
  724. killme();
  725. }