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.
 
 
 
 
 
 

537 line
16 KiB

  1. <?php
  2. use Friendica\Core\Config;
  3. require_once('include/queue_fn.php');
  4. require_once('include/html2plain.php');
  5. require_once('include/Scrape.php');
  6. require_once('include/diaspora.php');
  7. require_once('include/ostatus.php');
  8. require_once('include/dfrn.php');
  9. function delivery_run(&$argv, &$argc){
  10. global $a;
  11. require_once('include/datetime.php');
  12. require_once('include/items.php');
  13. require_once('include/bbcode.php');
  14. require_once('include/email.php');
  15. if ($argc < 3) {
  16. return;
  17. }
  18. logger('delivery: invoked: '. print_r($argv,true), LOGGER_DEBUG);
  19. $cmd = $argv[1];
  20. $item_id = intval($argv[2]);
  21. for ($x = 3; $x < $argc; $x ++) {
  22. $contact_id = intval($argv[$x]);
  23. if (!$item_id || !$contact_id) {
  24. continue;
  25. }
  26. $expire = false;
  27. $mail = false;
  28. $fsuggest = false;
  29. $relocate = false;
  30. $top_level = false;
  31. $recipients = array();
  32. $url_recipients = array();
  33. $followup = false;
  34. $normal_mode = true;
  35. $recipients[] = $contact_id;
  36. if ($cmd === 'mail') {
  37. $normal_mode = false;
  38. $mail = true;
  39. $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
  40. intval($item_id)
  41. );
  42. if (!count($message)) {
  43. return;
  44. }
  45. $uid = $message[0]['uid'];
  46. $recipients[] = $message[0]['contact-id'];
  47. $item = $message[0];
  48. } elseif ($cmd === 'expire') {
  49. $normal_mode = false;
  50. $expire = true;
  51. $items = q("SELECT * FROM `item` WHERE `uid` = %d AND `wall` = 1
  52. AND `deleted` = 1 AND `changed` > UTC_TIMESTAMP() - INTERVAL 30 MINUTE",
  53. intval($item_id)
  54. );
  55. $uid = $item_id;
  56. $item_id = 0;
  57. if (!count($items)) {
  58. continue;
  59. }
  60. } elseif ($cmd === 'suggest') {
  61. $normal_mode = false;
  62. $fsuggest = true;
  63. $suggest = q("SELECT * FROM `fsuggest` WHERE `id` = %d LIMIT 1",
  64. intval($item_id)
  65. );
  66. if (!count($suggest)) {
  67. return;
  68. }
  69. $uid = $suggest[0]['uid'];
  70. $recipients[] = $suggest[0]['cid'];
  71. $item = $suggest[0];
  72. } elseif ($cmd === 'relocate') {
  73. $normal_mode = false;
  74. $relocate = true;
  75. $uid = $item_id;
  76. } else {
  77. // find ancestors
  78. $r = q("SELECT * FROM `item` WHERE `id` = %d and visible = 1 and moderated = 0 LIMIT 1",
  79. intval($item_id)
  80. );
  81. if ((!dbm::is_result($r)) || (!intval($r[0]['parent']))) {
  82. continue;
  83. }
  84. $target_item = $r[0];
  85. $parent_id = intval($r[0]['parent']);
  86. $uid = $r[0]['uid'];
  87. $updated = $r[0]['edited'];
  88. $items = q("SELECT `item`.*, `sign`.`signed_text`,`sign`.`signature`,`sign`.`signer`
  89. FROM `item` LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id` WHERE `parent` = %d and visible = 1 and moderated = 0 ORDER BY `id` ASC",
  90. intval($parent_id)
  91. );
  92. if (!count($items)) {
  93. continue;
  94. }
  95. $icontacts = null;
  96. $contacts_arr = array();
  97. foreach ($items as $item) {
  98. if (!in_array($item['contact-id'],$contacts_arr)) {
  99. $contacts_arr[] = intval($item['contact-id']);
  100. }
  101. }
  102. if (count($contacts_arr)) {
  103. $str_contacts = implode(',',$contacts_arr);
  104. $icontacts = q("SELECT * FROM `contact`
  105. WHERE `id` IN ( $str_contacts ) "
  106. );
  107. }
  108. if ( !($icontacts && count($icontacts))) {
  109. continue;
  110. }
  111. // avoid race condition with deleting entries
  112. if ($items[0]['deleted']) {
  113. foreach ($items as $item) {
  114. $item['deleted'] = 1;
  115. }
  116. }
  117. // When commenting too fast after delivery, a post wasn't recognized as top level post.
  118. // The count then showed more than one entry. The additional check should help.
  119. // The check for the "count" should be superfluous, but I'm not totally sure by now, so we keep it.
  120. if ((($items[0]['id'] == $item_id) || (count($items) == 1)) && ($items[0]['uri'] === $items[0]['parent-uri'])) {
  121. logger('delivery: top level post');
  122. $top_level = true;
  123. }
  124. }
  125. $r = q("SELECT `contact`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`,
  126. `user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`,
  127. `user`.`page-flags`, `user`.`account-type`, `user`.`prvnets`
  128. FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
  129. WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
  130. intval($uid)
  131. );
  132. if (!dbm::is_result($r)) {
  133. continue;
  134. }
  135. $owner = $r[0];
  136. $walltowall = ((($top_level) && ($owner['id'] != $items[0]['contact-id'])) ? true : false);
  137. $public_message = true;
  138. if (!($mail || $fsuggest || $relocate)) {
  139. require_once('include/group.php');
  140. $parent = $items[0];
  141. // This is IMPORTANT!!!!
  142. // We will only send a "notify owner to relay" or followup message if the referenced post
  143. // originated on our system by virtue of having our hostname somewhere
  144. // in the URI, AND it was a comment (not top_level) AND the parent originated elsewhere.
  145. // if $parent['wall'] == 1 we will already have the parent message in our array
  146. // and we will relay the whole lot.
  147. // expire sends an entire group of expire messages and cannot be forwarded.
  148. // However the conversation owner will be a part of the conversation and will
  149. // be notified during this run.
  150. // Other DFRN conversation members will be alerted during polled updates.
  151. // Diaspora members currently are not notified of expirations, and other networks have
  152. // either limited or no ability to process deletions. We should at least fix Diaspora
  153. // by stringing togther an array of retractions and sending them onward.
  154. $localhost = $a->get_hostname();
  155. if (strpos($localhost,':')) {
  156. $localhost = substr($localhost,0,strpos($localhost,':'));
  157. }
  158. /**
  159. *
  160. * Be VERY CAREFUL if you make any changes to the following line. Seemingly innocuous changes
  161. * have been known to cause runaway conditions which affected several servers, along with
  162. * permissions issues.
  163. *
  164. */
  165. $relay_to_owner = false;
  166. if (!$top_level && ($parent['wall'] == 0) && !$expire && stristr($target_item['uri'],$localhost)) {
  167. $relay_to_owner = true;
  168. }
  169. if ($relay_to_owner) {
  170. logger('followup '.$target_item["guid"], LOGGER_DEBUG);
  171. // local followup to remote post
  172. $followup = true;
  173. }
  174. if ((strlen($parent['allow_cid']))
  175. || (strlen($parent['allow_gid']))
  176. || (strlen($parent['deny_cid']))
  177. || (strlen($parent['deny_gid']))
  178. || $parent["private"]) {
  179. $public_message = false; // private recipients, not public
  180. }
  181. }
  182. $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `blocked` = 0 AND `pending` = 0",
  183. intval($contact_id)
  184. );
  185. if (dbm::is_result($r)) {
  186. $contact = $r[0];
  187. }
  188. if ($contact['self']) {
  189. continue;
  190. }
  191. $deliver_status = 0;
  192. logger("main delivery by delivery: followup=$followup mail=$mail fsuggest=$fsuggest relocate=$relocate - network ".$contact['network']);
  193. switch($contact['network']) {
  194. case NETWORK_DFRN:
  195. logger('notifier: '.$target_item["guid"].' dfrndelivery: '.$contact['name']);
  196. if ($mail) {
  197. $item['body'] = fix_private_photos($item['body'],$owner['uid'],null,$message[0]['contact-id']);
  198. $atom = dfrn::mail($item, $owner);
  199. } elseif ($fsuggest) {
  200. $atom = dfrn::fsuggest($item, $owner);
  201. q("DELETE FROM `fsuggest` WHERE `id` = %d LIMIT 1", intval($item['id']));
  202. } elseif ($relocate) {
  203. $atom = dfrn::relocate($owner, $uid);
  204. } elseif ($followup) {
  205. $msgitems = array();
  206. foreach ($items as $item) { // there is only one item
  207. if (!$item['parent']) {
  208. continue;
  209. }
  210. if ($item['id'] == $item_id) {
  211. logger('followup: item: '. print_r($item,true), LOGGER_DATA);
  212. $msgitems[] = $item;
  213. }
  214. }
  215. $atom = dfrn::entries($msgitems,$owner);
  216. } else {
  217. $msgitems = array();
  218. foreach ($items as $item) {
  219. if (!$item['parent']) {
  220. continue;
  221. }
  222. // private emails may be in included in public conversations. Filter them.
  223. if ($public_message && $item['private']) {
  224. continue;
  225. }
  226. $item_contact = get_item_contact($item,$icontacts);
  227. if (!$item_contact) {
  228. continue;
  229. }
  230. if ($normal_mode) {
  231. if ($item_id == $item['id'] || $item['id'] == $item['parent']) {
  232. $item["entry:comment-allow"] = true;
  233. $item["entry:cid"] = (($top_level) ? $contact['id'] : 0);
  234. $msgitems[] = $item;
  235. }
  236. } else {
  237. $item["entry:comment-allow"] = true;
  238. $msgitems[] = $item;
  239. }
  240. }
  241. $atom = dfrn::entries($msgitems,$owner);
  242. }
  243. logger('notifier entry: '.$contact["url"].' '.$target_item["guid"].' entry: '.$atom, LOGGER_DEBUG);
  244. logger('notifier: '.$atom, LOGGER_DATA);
  245. $basepath = implode('/', array_slice(explode('/',$contact['url']),0,3));
  246. // perform local delivery if we are on the same site
  247. if (link_compare($basepath,App::get_baseurl())) {
  248. $nickname = basename($contact['url']);
  249. if ($contact['issued-id']) {
  250. $sql_extra = sprintf(" AND `dfrn-id` = '%s' ", dbesc($contact['issued-id']));
  251. } else {
  252. $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($contact['dfrn-id']));
  253. }
  254. $x = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`,
  255. `contact`.`pubkey` AS `cpubkey`,
  256. `contact`.`prvkey` AS `cprvkey`,
  257. `contact`.`thumb` AS `thumb`,
  258. `contact`.`url` as `url`,
  259. `contact`.`name` as `senderName`,
  260. `user`.*
  261. FROM `contact`
  262. INNER JOIN `user` ON `contact`.`uid` = `user`.`uid`
  263. WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
  264. AND `contact`.`network` = '%s' AND `user`.`nickname` = '%s'
  265. $sql_extra
  266. AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 LIMIT 1",
  267. dbesc(NETWORK_DFRN),
  268. dbesc($nickname)
  269. );
  270. if ($x && count($x)) {
  271. $write_flag = ((($x[0]['rel']) && ($x[0]['rel'] != CONTACT_IS_SHARING)) ? true : false);
  272. if ((($owner['page-flags'] == PAGE_COMMUNITY) || $write_flag) && !$x[0]['writable']) {
  273. q("UPDATE `contact` SET `writable` = 1 WHERE `id` = %d",
  274. intval($x[0]['id'])
  275. );
  276. $x[0]['writable'] = 1;
  277. }
  278. $ssl_policy = get_config('system','ssl_policy');
  279. fix_contact_ssl_policy($x[0],$ssl_policy);
  280. // If we are setup as a soapbox we aren't accepting top level posts from this person
  281. if (($x[0]['page-flags'] == PAGE_SOAPBOX) AND $top_level) {
  282. break;
  283. }
  284. logger('mod-delivery: local delivery');
  285. dfrn::import($atom, $x[0]);
  286. break;
  287. }
  288. }
  289. if (!was_recently_delayed($contact['id'])) {
  290. $deliver_status = dfrn::deliver($owner,$contact,$atom);
  291. } else {
  292. $deliver_status = (-1);
  293. }
  294. logger('notifier: dfrn_delivery to '.$contact["url"].' with guid '.$target_item["guid"].' returns '.$deliver_status);
  295. if ($deliver_status < 0) {
  296. logger('notifier: delivery failed: queuing message');
  297. add_to_queue($contact['id'],NETWORK_DFRN,$atom);
  298. // The message could not be delivered. We mark the contact as "dead"
  299. mark_for_death($contact);
  300. } else {
  301. // We successfully delivered a message, the contact is alive
  302. unmark_for_death($contact);
  303. }
  304. break;
  305. case NETWORK_OSTATUS:
  306. // Do not send to otatus if we are not configured to send to public networks
  307. if ($owner['prvnets']) {
  308. break;
  309. }
  310. if (get_config('system','ostatus_disabled') || get_config('system','dfrn_only')) {
  311. break;
  312. }
  313. // There is currently no code here to distribute anything to OStatus.
  314. // This is done in "notifier.php" (See "url_recipients" and "push_notify")
  315. break;
  316. case NETWORK_MAIL:
  317. case NETWORK_MAIL2:
  318. if (get_config('system','dfrn_only')) {
  319. break;
  320. }
  321. // WARNING: does not currently convert to RFC2047 header encodings, etc.
  322. $addr = $contact['addr'];
  323. if (!strlen($addr)) {
  324. break;
  325. }
  326. if ($cmd === 'wall-new' || $cmd === 'comment-new') {
  327. $it = null;
  328. if ($cmd === 'wall-new') {
  329. $it = $items[0];
  330. } else {
  331. $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
  332. intval($argv[2]),
  333. intval($uid)
  334. );
  335. if (dbm::is_result($r))
  336. $it = $r[0];
  337. }
  338. if (!$it)
  339. break;
  340. $local_user = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
  341. intval($uid)
  342. );
  343. if (!count($local_user))
  344. break;
  345. $reply_to = '';
  346. $r1 = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
  347. intval($uid)
  348. );
  349. if ($r1 && $r1[0]['reply_to'])
  350. $reply_to = $r1[0]['reply_to'];
  351. $subject = (($it['title']) ? email_header_encode($it['title'],'UTF-8') : t("\x28no subject\x29")) ;
  352. // only expose our real email address to true friends
  353. if (($contact['rel'] == CONTACT_IS_FRIEND) && !$contact['blocked']) {
  354. if ($reply_to) {
  355. $headers = 'From: '.email_header_encode($local_user[0]['username'],'UTF-8').' <'.$reply_to.'>'."\n";
  356. $headers .= 'Sender: '.$local_user[0]['email']."\n";
  357. } else {
  358. $headers = 'From: '.email_header_encode($local_user[0]['username'],'UTF-8').' <'.$local_user[0]['email'].'>'."\n";
  359. }
  360. } else {
  361. $headers = 'From: '. email_header_encode($local_user[0]['username'],'UTF-8') .' <'. t('noreply') .'@'.$a->get_hostname() .'>'. "\n";
  362. }
  363. //if ($reply_to)
  364. // $headers .= 'Reply-to: '.$reply_to . "\n";
  365. $headers .= 'Message-Id: <'. iri2msgid($it['uri']).'>'. "\n";
  366. //logger("Mail: uri: ".$it['uri']." parent-uri ".$it['parent-uri'], LOGGER_DEBUG);
  367. //logger("Mail: Data: ".print_r($it, true), LOGGER_DEBUG);
  368. //logger("Mail: Data: ".print_r($it, true), LOGGER_DATA);
  369. if ($it['uri'] !== $it['parent-uri']) {
  370. $headers .= "References: <".iri2msgid($it["parent-uri"]).">";
  371. // If Threading is enabled, write down the correct parent
  372. if (($it["thr-parent"] != "") and ($it["thr-parent"] != $it["parent-uri"]))
  373. $headers .= " <".iri2msgid($it["thr-parent"]).">";
  374. $headers .= "\n";
  375. if (!$it['title']) {
  376. $r = q("SELECT `title` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
  377. dbesc($it['parent-uri']),
  378. intval($uid));
  379. if (dbm::is_result($r) AND ($r[0]['title'] != '')) {
  380. $subject = $r[0]['title'];
  381. } else {
  382. $r = q("SELECT `title` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d LIMIT 1",
  383. dbesc($it['parent-uri']),
  384. intval($uid));
  385. if (dbm::is_result($r) AND ($r[0]['title'] != ''))
  386. $subject = $r[0]['title'];
  387. }
  388. }
  389. if (strncasecmp($subject,'RE:',3))
  390. $subject = 'Re: '.$subject;
  391. }
  392. email_send($addr, $subject, $headers, $it);
  393. }
  394. break;
  395. case NETWORK_DIASPORA:
  396. if ($public_message)
  397. $loc = 'public batch '.$contact['batch'];
  398. else
  399. $loc = $contact['name'];
  400. logger('delivery: diaspora batch deliver: '.$loc);
  401. if (get_config('system','dfrn_only') || (!get_config('system','diaspora_enabled')))
  402. break;
  403. if ($mail) {
  404. Diaspora::send_mail($item,$owner,$contact);
  405. break;
  406. }
  407. if (!$normal_mode)
  408. break;
  409. if (!$contact['pubkey'] && !$public_message)
  410. break;
  411. if (($target_item['deleted']) && (($target_item['uri'] === $target_item['parent-uri']) || $followup)) {
  412. // top-level retraction
  413. logger('diaspora retract: '.$loc);
  414. Diaspora::send_retraction($target_item,$owner,$contact,$public_message);
  415. break;
  416. } elseif ($followup) {
  417. // send comments and likes to owner to relay
  418. logger('diaspora followup: '.$loc);
  419. Diaspora::send_followup($target_item,$owner,$contact,$public_message);
  420. break;
  421. } elseif ($target_item['uri'] !== $target_item['parent-uri']) {
  422. // we are the relay - send comments, likes and relayable_retractions to our conversants
  423. logger('diaspora relay: '.$loc);
  424. Diaspora::send_relay($target_item,$owner,$contact,$public_message);
  425. break;
  426. } elseif ($top_level && !$walltowall) {
  427. // currently no workable solution for sending walltowall
  428. logger('diaspora status: '.$loc);
  429. Diaspora::send_status($target_item,$owner,$contact,$public_message);
  430. break;
  431. }
  432. logger('delivery: diaspora unknown mode: '.$contact['name']);
  433. break;
  434. default:
  435. break;
  436. }
  437. }
  438. return;
  439. }