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.
 
 
 
 
 
 

334 lines
10 KiB

  1. <?php
  2. require_once('simplepie/simplepie.inc');
  3. require_once('include/items.php');
  4. function dfrn_notify_post(&$a) {
  5. $dfrn_id = notags(trim($_POST['dfrn_id']));
  6. $challenge = notags(trim($_POST['challenge']));
  7. $data = $_POST['data'];
  8. $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
  9. dbesc($dfrn_id),
  10. dbesc($challenge)
  11. );
  12. if(! count($r))
  13. xml_status(3);
  14. $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
  15. dbesc($dfrn_id),
  16. dbesc($challenge)
  17. );
  18. // find the local user who owns this relationship.
  19. $r = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` WHERE `issued-id` = '%s' LIMIT 1",
  20. dbesc($dfrn_id)
  21. );
  22. if(! count($r)) {
  23. xml_status(3);
  24. return; //NOTREACHED
  25. }
  26. // We aren't really interested in anything this person has to say. But be polite and make them
  27. // think we're listening intently by acknowledging receipt of their communications - which we quietly ignore.
  28. if($r[0]['readonly']) {
  29. xml_status(0);
  30. return; //NOTREACHED
  31. }
  32. $importer = $r[0];
  33. $feed = new SimplePie();
  34. $feed->set_raw_data($data);
  35. $feed->enable_order_by_date(false);
  36. $feed->init();
  37. $ismail = false;
  38. $rawmail = $feed->get_feed_tags( NAMESPACE_DFRN, 'mail' );
  39. if(isset($rawmail[0]['child'][NAMESPACE_DFRN])) {
  40. $ismail = true;
  41. $base = $rawmail[0]['child'][NAMESPACE_DFRN];
  42. $msg = array();
  43. $msg['uid'] = $importer['importer_uid'];
  44. $msg['from-name'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['name'][0]['data']));
  45. $msg['from-photo'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']));
  46. $msg['from-url'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['uri'][0]['data']));
  47. $msg['contact-id'] = $importer['id'];
  48. $msg['title'] = notags(unxmlify($base['subject'][0]['data']));
  49. $msg['body'] = escape_tags(unxmlify($base['content'][0]['data']));
  50. $msg['delivered'] = 1;
  51. $msg['seen'] = 0;
  52. $msg['replied'] = 0;
  53. $msg['uri'] = notags(unxmlify($base['id'][0]['data']));
  54. $msg['parent-uri'] = notags(unxmlify($base['in-reply-to'][0]['data']));
  55. $msg['created'] = datetime_convert(notags(unxmlify('UTC','UTC',$base['sentdate'][0]['data'])));
  56. dbesc_array($msg);
  57. $r = q("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg))
  58. . "`) VALUES ('" . implode("', '", array_values($msg)) . "')" );
  59. // send email notification if requested.
  60. require_once('bbcode.php');
  61. if($importer['notify-flags'] & NOTIFY_MAIL) {
  62. $tpl = file_get_contents('view/mail_received_eml.tpl');
  63. $email_tpl = replace_macros($tpl, array(
  64. '$sitename' => $a->config['sitename'],
  65. '$siteurl' => $a->get_baseurl(),
  66. '$username' => $importer['username'],
  67. '$email' => $importer['email'],
  68. '$from' => $msg['from-name'],
  69. '$title' => $msg['title'],
  70. '$body' => strip_tags(bbcode($msg['body']))
  71. ));
  72. $res = mail($importer['email'], t("New mail received at ") . $a->config['sitename'],
  73. $email_tpl,t("From: Administrator@") . $a->get_hostname() );
  74. }
  75. xml_status(0);
  76. return; // NOTREACHED
  77. }
  78. foreach($feed->get_items() as $item) {
  79. $deleted = false;
  80. $rawdelete = $item->get_item_tags("http://purl.org/atompub/tombstones/1.0", 'deleted-entry');
  81. if(isset($rawdelete[0]['attribs']['']['ref'])) {
  82. $uri = $rawthread[0]['attribs']['']['ref'];
  83. $deleted = true;
  84. if(isset($rawdelete[0]['attribs']['']['when'])) {
  85. $when = $rawthread[0]['attribs']['']['when'];
  86. $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
  87. }
  88. else
  89. $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
  90. }
  91. if($deleted) {
  92. $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
  93. dbesc($uri),
  94. intval($importer['importer_uid'])
  95. );
  96. if(count($r)) {
  97. if($r[0]['uri'] == $r[0]['parent-uri']) {
  98. $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s'
  99. WHERE `parent-uri` = '%s'",
  100. dbesc($when),
  101. dbesc($r[0]['uri'])
  102. );
  103. }
  104. else {
  105. $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s'
  106. WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
  107. dbesc($when),
  108. dbesc($uri),
  109. intval($importer['importer_uid'])
  110. );
  111. }
  112. }
  113. continue;
  114. }
  115. $is_reply = false;
  116. $item_id = $item->get_id();
  117. $rawthread = $item->get_item_tags("http://purl.org/syndication/thread/1.0",'in-reply-to');
  118. if(isset($rawthread[0]['attribs']['']['ref'])) {
  119. $is_reply = true;
  120. $parent_uri = $rawthread[0]['attribs']['']['ref'];
  121. }
  122. if($is_reply) {
  123. if($feed->get_item_quantity() == 1) {
  124. // remote reply to our post. Import and then notify everybody else.
  125. $datarray = get_atom_elements($item);
  126. $datarray['type'] = 'remote-comment';
  127. $datarray['parent-uri'] = $parent_uri;
  128. $datarray['uid'] = $importer['importer_uid'];
  129. $datarray['contact-id'] = $importer['id'];
  130. $posted_id = post_remote($a,$datarray);
  131. $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
  132. intval($posted_id),
  133. intval($importer['importer_uid'])
  134. );
  135. if(count($r)) {
  136. $r1 = q("UPDATE `item` SET `last-child` = 0 WHERE `uid` = %d AND `parent` = %d",
  137. intval($importer['importer_uid']),
  138. intval($r[0]['parent'])
  139. );
  140. }
  141. $r2 = q("UPDATE `item` SET `last-child` = 1 WHERE `uid` = %d AND `id` = %d LIMIT 1",
  142. intval($importer['importer_uid']),
  143. intval($posted_id)
  144. );
  145. $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
  146. proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"comment-import\" \"$posted_id\" &",
  147. array(),$foo));
  148. if(($importer['notify-flags'] & NOTIFY_COMMENT) && (! $importer['self'])) {
  149. require_once('bbcode.php');
  150. $from = stripslashes($datarray['author-name']);
  151. $tpl = file_get_contents('view/cmnt_received_eml.tpl');
  152. $email_tpl = replace_macros($tpl, array(
  153. '$sitename' => $a->config['sitename'],
  154. '$siteurl' => $a->get_baseurl(),
  155. '$username' => $importer['username'],
  156. '$email' => $importer['email'],
  157. '$from' => $from,
  158. '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
  159. ));
  160. $res = mail($importer['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
  161. $email_tpl,t("From: Administrator@") . $a->get_hostname() );
  162. }
  163. xml_status(0);
  164. return;
  165. }
  166. else {
  167. // regular comment that is part of this total conversation. Have we seen it? If not, import it.
  168. $item_id = $item->get_id();
  169. $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
  170. dbesc($item_id),
  171. intval($importer['importer_uid'])
  172. );
  173. // FIXME update content if 'updated' changes
  174. if(count($r)) {
  175. $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
  176. if($allow && $allow[0]['data'] != $r[0]['last-child']) {
  177. $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
  178. intval($allow[0]['data']),
  179. dbesc($item_id),
  180. intval($importer['importer_uid'])
  181. );
  182. }
  183. continue;
  184. }
  185. $datarray = get_atom_elements($item);
  186. $datarray['parent-uri'] = $parent_uri;
  187. $datarray['uid'] = $importer['importer_uid'];
  188. $datarray['contact-id'] = $importer['id'];
  189. $r = post_remote($a,$datarray);
  190. // find out if our user is involved in this conversation and wants to be notified.
  191. if($importer['notify-flags'] & NOTIFY_COMMENT) {
  192. $myconv = q("SELECT `author-link` FROM `item` WHERE `parent-uri` = '%s'",
  193. dbesc($parent_uri)
  194. );
  195. if(count($myconv)) {
  196. foreach($myconv as $conv) {
  197. if($conv['author-link'] != $importer['url'])
  198. continue;
  199. require_once('bbcode.php');
  200. $from = stripslashes($datarray['author-name']);
  201. $tpl = file_get_contents('view/cmnt_received_eml.tpl');
  202. $email_tpl = replace_macros($tpl, array(
  203. '$sitename' => $a->config['sitename'],
  204. '$siteurl' => $a->get_baseurl(),
  205. '$username' => $importer['username'],
  206. '$email' => $importer['email'],
  207. '$from' => $from,
  208. '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
  209. ));
  210. $res = mail($importer['email'], $from . t(" commented on an item at ")
  211. . $a->config['sitename'],
  212. $email_tpl,t("From: Administrator@") . $a->get_hostname() );
  213. break;
  214. }
  215. }
  216. }
  217. continue;
  218. }
  219. }
  220. else {
  221. // Head post of a conversation. Have we seen it? If not, import it.
  222. $item_id = $item->get_id();
  223. $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
  224. dbesc($item_id),
  225. intval($importer['importer_uid'])
  226. );
  227. if(count($r)) {
  228. $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
  229. if($allow && $allow[0]['data'] != $r[0]['last-child']) {
  230. $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
  231. intval($allow[0]['data']),
  232. dbesc($item_id),
  233. intval($importer['importer_uid'])
  234. );
  235. }
  236. continue;
  237. }
  238. $datarray = get_atom_elements($item);
  239. $datarray['parent-uri'] = $item_id;
  240. $datarray['uid'] = $importer['importer_uid'];
  241. $datarray['contact-id'] = $importer['id'];
  242. $r = post_remote($a,$datarray);
  243. continue;
  244. }
  245. }
  246. xml_status(0);
  247. killme();
  248. }
  249. function dfrn_notify_content(&$a) {
  250. if(x($_GET,'dfrn_id')) {
  251. // initial communication from external contact
  252. $hash = random_string();
  253. $status = 0;
  254. $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
  255. $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
  256. VALUES( '%s', '%s', '%s') ",
  257. dbesc($hash),
  258. dbesc(notags(trim($_GET['dfrn_id']))),
  259. intval(time() + 60 )
  260. );
  261. $r = q("SELECT * FROM `contact` WHERE `issued-id` = '%s' AND `blocked` = 0 AND `pending` = 0 LIMIT 1",
  262. dbesc($_GET['dfrn_id']));
  263. if((! count($r)) || (! strlen($r[0]['prvkey'])))
  264. $status = 1;
  265. $challenge = '';
  266. openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
  267. $challenge = bin2hex($challenge);
  268. $encrypted_id = '';
  269. $id_str = $_GET['dfrn_id'] . '.' . mt_rand(1000,9999);
  270. openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
  271. $encrypted_id = bin2hex($encrypted_id);
  272. echo '<?xml version="1.0" encoding="UTF-8"?><dfrn_notify><status>' .$status . '</status><dfrn_id>' . $encrypted_id . '</dfrn_id>' . '<challenge>' . $challenge . '</challenge></dfrn_notify>' . "\r\n" ;
  273. session_write_close();
  274. exit;
  275. }
  276. }