Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

561 rinda
16KB

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