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.

581 lines
17KB

  1. <?php
  2. /**
  3. * @file mod/dfrn_poll.php
  4. */
  5. use Friendica\App;
  6. use Friendica\Core\Config;
  7. use Friendica\Core\L10n;
  8. use Friendica\Core\Logger;
  9. use Friendica\Core\System;
  10. use Friendica\Database\DBA;
  11. use Friendica\Module\Login;
  12. use Friendica\Protocol\DFRN;
  13. use Friendica\Protocol\OStatus;
  14. use Friendica\Util\Network;
  15. use Friendica\Util\Strings;
  16. use Friendica\Util\XML;
  17. function dfrn_poll_init(App $a)
  18. {
  19. Login::sessionAuth();
  20. $dfrn_id = defaults($_GET, 'dfrn_id' , '');
  21. $type = defaults($_GET, 'type' , 'data');
  22. $last_update = defaults($_GET, 'last_update' , '');
  23. $destination_url = defaults($_GET, 'destination_url', '');
  24. $challenge = defaults($_GET, 'challenge' , '');
  25. $sec = defaults($_GET, 'sec' , '');
  26. $dfrn_version = (float) defaults($_GET, 'dfrn_version' , 2.0);
  27. $perm = defaults($_GET, 'perm' , 'r');
  28. $quiet = !empty($_GET['quiet']);
  29. // Possibly it is an OStatus compatible server that requests a user feed
  30. $user_agent = defaults($_SERVER, 'HTTP_USER_AGENT', '');
  31. if (($a->argc > 1) && ($dfrn_id == '') && !strstr($user_agent, 'Friendica')) {
  32. $nickname = $a->argv[1];
  33. header("Content-type: application/atom+xml");
  34. echo OStatus::feed($nickname, $last_update, 10);
  35. killme();
  36. }
  37. $direction = -1;
  38. if (strpos($dfrn_id, ':') == 1) {
  39. $direction = intval(substr($dfrn_id, 0, 1));
  40. $dfrn_id = substr($dfrn_id, 2);
  41. }
  42. $hidewall = false;
  43. if (($dfrn_id === '') && empty($_POST['dfrn_id'])) {
  44. if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
  45. System::httpExit(403);
  46. }
  47. $user = '';
  48. if ($a->argc > 1) {
  49. $r = q("SELECT `hidewall`,`nickname` FROM `user` WHERE `user`.`nickname` = '%s' LIMIT 1",
  50. DBA::escape($a->argv[1])
  51. );
  52. if (!$r) {
  53. System::httpExit(404);
  54. }
  55. $hidewall = ($r[0]['hidewall'] && !local_user());
  56. $user = $r[0]['nickname'];
  57. }
  58. Logger::log('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $user);
  59. header("Content-type: application/atom+xml");
  60. echo DFRN::feed('', $user, $last_update, 0, $hidewall);
  61. killme();
  62. }
  63. if (($type === 'profile') && (!strlen($sec))) {
  64. $sql_extra = '';
  65. switch ($direction) {
  66. case -1:
  67. $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", DBA::escape($dfrn_id), DBA::escape($dfrn_id));
  68. $my_id = $dfrn_id;
  69. break;
  70. case 0:
  71. $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
  72. $my_id = '1:' . $dfrn_id;
  73. break;
  74. case 1:
  75. $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
  76. $my_id = '0:' . $dfrn_id;
  77. break;
  78. default:
  79. $a->internalRedirect();
  80. break; // NOTREACHED
  81. }
  82. $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
  83. FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
  84. WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
  85. AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
  86. DBA::escape($a->argv[1])
  87. );
  88. if (DBA::isResult($r)) {
  89. $s = Network::fetchUrl($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
  90. Logger::log("dfrn_poll: old profile returns " . $s, Logger::DATA);
  91. if (strlen($s)) {
  92. $xml = XML::parseString($s);
  93. if ((int)$xml->status === 1) {
  94. $_SESSION['authenticated'] = 1;
  95. if (empty($_SESSION['remote'])) {
  96. $_SESSION['remote'] = [];
  97. }
  98. $_SESSION['remote'][] = ['cid' => $r[0]['id'], 'uid' => $r[0]['uid'], 'url' => $r[0]['url']];
  99. $_SESSION['visitor_id'] = $r[0]['id'];
  100. $_SESSION['visitor_home'] = $r[0]['url'];
  101. $_SESSION['visitor_handle'] = $r[0]['addr'];
  102. $_SESSION['visitor_visiting'] = $r[0]['uid'];
  103. $_SESSION['my_url'] = $r[0]['url'];
  104. if (!$quiet) {
  105. info(L10n::t('%1$s welcomes %2$s', $r[0]['username'], $r[0]['name']) . EOL);
  106. }
  107. // Visitors get 1 day session.
  108. $session_id = session_id();
  109. $expire = time() + 86400;
  110. q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
  111. DBA::escape($expire),
  112. DBA::escape($session_id)
  113. );
  114. }
  115. }
  116. $profile = (count($r) > 0 && isset($r[0]['nickname']) ? $r[0]['nickname'] : '');
  117. if (!empty($destination_url)) {
  118. System::externalRedirect($destination_url);
  119. } else {
  120. $a->internalRedirect('profile/' . $profile);
  121. }
  122. }
  123. $a->internalRedirect();
  124. }
  125. if ($type === 'profile-check' && $dfrn_version < 2.2) {
  126. if ((strlen($challenge)) && (strlen($sec))) {
  127. DBA::delete('profile_check', ["`expire` < ?", time()]);
  128. $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
  129. DBA::escape($sec)
  130. );
  131. if (!DBA::isResult($r)) {
  132. System::xmlExit(3, 'No ticket');
  133. // NOTREACHED
  134. }
  135. $orig_id = $r[0]['dfrn_id'];
  136. if (strpos($orig_id, ':')) {
  137. $orig_id = substr($orig_id, 2);
  138. }
  139. $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
  140. intval($r[0]['cid'])
  141. );
  142. if (!DBA::isResult($c)) {
  143. System::xmlExit(3, 'No profile');
  144. }
  145. $contact = $c[0];
  146. $sent_dfrn_id = hex2bin($dfrn_id);
  147. $challenge = hex2bin($challenge);
  148. $final_dfrn_id = '';
  149. if (($contact['duplex']) && strlen($contact['prvkey'])) {
  150. openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
  151. openssl_private_decrypt($challenge, $decoded_challenge, $contact['prvkey']);
  152. } else {
  153. openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
  154. openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
  155. }
  156. $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
  157. if (strpos($final_dfrn_id, ':') == 1) {
  158. $final_dfrn_id = substr($final_dfrn_id, 2);
  159. }
  160. if ($final_dfrn_id != $orig_id) {
  161. Logger::log('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, Logger::DEBUG);
  162. // did not decode properly - cannot trust this site
  163. System::xmlExit(3, 'Bad decryption');
  164. }
  165. header("Content-type: text/xml");
  166. echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
  167. killme();
  168. // NOTREACHED
  169. } else {
  170. // old protocol
  171. switch ($direction) {
  172. case 1:
  173. $dfrn_id = '0:' . $dfrn_id;
  174. break;
  175. case 0:
  176. $dfrn_id = '1:' . $dfrn_id;
  177. break;
  178. default:
  179. break;
  180. }
  181. DBA::delete('profile_check', ["`expire` < ?", time()]);
  182. $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
  183. DBA::escape($dfrn_id));
  184. if (DBA::isResult($r)) {
  185. System::xmlExit(1);
  186. return; // NOTREACHED
  187. }
  188. System::xmlExit(0);
  189. return; // NOTREACHED
  190. }
  191. }
  192. }
  193. function dfrn_poll_post(App $a)
  194. {
  195. $dfrn_id = defaults($_POST, 'dfrn_id' , '');
  196. $challenge = defaults($_POST, 'challenge', '');
  197. $url = defaults($_POST, 'url' , '');
  198. $sec = defaults($_POST, 'sec' , '');
  199. $ptype = defaults($_POST, 'type' , '');
  200. $perm = defaults($_POST, 'perm' , 'r');
  201. $dfrn_version = !empty($_POST['dfrn_version']) ? (float) $_POST['dfrn_version'] : 2.0;
  202. if ($ptype === 'profile-check') {
  203. if (strlen($challenge) && strlen($sec)) {
  204. Logger::log('dfrn_poll: POST: profile-check');
  205. DBA::delete('profile_check', ["`expire` < ?", time()]);
  206. $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
  207. DBA::escape($sec)
  208. );
  209. if (!DBA::isResult($r)) {
  210. System::xmlExit(3, 'No ticket');
  211. // NOTREACHED
  212. }
  213. $orig_id = $r[0]['dfrn_id'];
  214. if (strpos($orig_id, ':')) {
  215. $orig_id = substr($orig_id, 2);
  216. }
  217. $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
  218. intval($r[0]['cid'])
  219. );
  220. if (!DBA::isResult($c)) {
  221. System::xmlExit(3, 'No profile');
  222. }
  223. $contact = $c[0];
  224. $sent_dfrn_id = hex2bin($dfrn_id);
  225. $challenge = hex2bin($challenge);
  226. $final_dfrn_id = '';
  227. if ($contact['duplex'] && strlen($contact['prvkey'])) {
  228. openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
  229. openssl_private_decrypt($challenge, $decoded_challenge, $contact['prvkey']);
  230. } else {
  231. openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
  232. openssl_public_decrypt($challenge, $decoded_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. }
  238. if ($final_dfrn_id != $orig_id) {
  239. Logger::log('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, Logger::DEBUG);
  240. // did not decode properly - cannot trust this site
  241. System::xmlExit(3, 'Bad decryption');
  242. }
  243. header("Content-type: text/xml");
  244. echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
  245. killme();
  246. // NOTREACHED
  247. }
  248. }
  249. $direction = -1;
  250. if (strpos($dfrn_id, ':') == 1) {
  251. $direction = intval(substr($dfrn_id, 0, 1));
  252. $dfrn_id = substr($dfrn_id, 2);
  253. }
  254. $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
  255. DBA::escape($dfrn_id),
  256. DBA::escape($challenge)
  257. );
  258. if (!DBA::isResult($r)) {
  259. killme();
  260. }
  261. $type = $r[0]['type'];
  262. $last_update = $r[0]['last_update'];
  263. DBA::delete('challenge', ['dfrn-id' => $dfrn_id, 'challenge' => $challenge]);
  264. $sql_extra = '';
  265. switch ($direction) {
  266. case -1:
  267. $sql_extra = sprintf(" AND `issued-id` = '%s' ", DBA::escape($dfrn_id));
  268. $my_id = $dfrn_id;
  269. break;
  270. case 0:
  271. $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
  272. $my_id = '1:' . $dfrn_id;
  273. break;
  274. case 1:
  275. $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
  276. $my_id = '0:' . $dfrn_id;
  277. break;
  278. default:
  279. $a->internalRedirect();
  280. break; // NOTREACHED
  281. }
  282. $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
  283. if (!DBA::isResult($r)) {
  284. killme();
  285. }
  286. $contact = $r[0];
  287. $owner_uid = $r[0]['uid'];
  288. $contact_id = $r[0]['id'];
  289. if ($type === 'reputation' && strlen($url)) {
  290. $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
  291. DBA::escape($url),
  292. intval($owner_uid)
  293. );
  294. $reputation = 0;
  295. $text = '';
  296. if (DBA::isResult($r)) {
  297. $reputation = $r[0]['rating'];
  298. $text = $r[0]['reason'];
  299. if ($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
  300. $reputation = 0;
  301. $text = '';
  302. }
  303. }
  304. echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  305. <reputation>
  306. <url>$url</url>
  307. <rating>$reputation</rating>
  308. <description>$text</description>
  309. </reputation>
  310. ";
  311. killme();
  312. // NOTREACHED
  313. } else {
  314. // Update the writable flag if it changed
  315. Logger::log('dfrn_poll: post request feed: ' . print_r($_POST, true), Logger::DATA);
  316. if ($dfrn_version >= 2.21) {
  317. if ($perm === 'rw') {
  318. $writable = 1;
  319. } else {
  320. $writable = 0;
  321. }
  322. if ($writable != $contact['writable']) {
  323. q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d",
  324. intval($writable),
  325. intval($contact_id)
  326. );
  327. }
  328. }
  329. header("Content-type: application/atom+xml");
  330. $o = DFRN::feed($dfrn_id, $a->argv[1], $last_update, $direction);
  331. echo $o;
  332. killme();
  333. }
  334. }
  335. function dfrn_poll_content(App $a)
  336. {
  337. $dfrn_id = defaults($_GET, 'dfrn_id' , '');
  338. $type = defaults($_GET, 'type' , 'data');
  339. $last_update = defaults($_GET, 'last_update' , '');
  340. $destination_url = defaults($_GET, 'destination_url', '');
  341. $sec = defaults($_GET, 'sec' , '');
  342. $dfrn_version = !empty($_GET['dfrn_version']) ? (float) $_GET['dfrn_version'] : 2.0;
  343. $quiet = !empty($_GET['quiet']);
  344. $direction = -1;
  345. if (strpos($dfrn_id, ':') == 1) {
  346. $direction = intval(substr($dfrn_id, 0, 1));
  347. $dfrn_id = substr($dfrn_id, 2);
  348. }
  349. if ($dfrn_id != '') {
  350. // initial communication from external contact
  351. $hash = Strings::getRandomHex();
  352. $status = 0;
  353. DBA::delete('challenge', ["`expire` < ?", time()]);
  354. if ($type !== 'profile') {
  355. $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
  356. VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
  357. DBA::escape($hash),
  358. DBA::escape($dfrn_id),
  359. intval(time() + 60 ),
  360. DBA::escape($type),
  361. DBA::escape($last_update)
  362. );
  363. }
  364. $sql_extra = '';
  365. switch ($direction) {
  366. case -1:
  367. if ($type === 'profile') {
  368. $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", DBA::escape($dfrn_id), DBA::escape($dfrn_id));
  369. } else {
  370. $sql_extra = sprintf(" AND `issued-id` = '%s' ", DBA::escape($dfrn_id));
  371. }
  372. $my_id = $dfrn_id;
  373. break;
  374. case 0:
  375. $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
  376. $my_id = '1:' . $dfrn_id;
  377. break;
  378. case 1:
  379. $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
  380. $my_id = '0:' . $dfrn_id;
  381. break;
  382. default:
  383. $a->internalRedirect();
  384. break; // NOTREACHED
  385. }
  386. $nickname = $a->argv[1];
  387. $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
  388. FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
  389. WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
  390. AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
  391. DBA::escape($nickname)
  392. );
  393. if (DBA::isResult($r)) {
  394. $challenge = '';
  395. $encrypted_id = '';
  396. $id_str = $my_id . '.' . mt_rand(1000, 9999);
  397. if (($r[0]['duplex'] && strlen($r[0]['pubkey'])) || !strlen($r[0]['prvkey'])) {
  398. openssl_public_encrypt($hash, $challenge, $r[0]['pubkey']);
  399. openssl_public_encrypt($id_str, $encrypted_id, $r[0]['pubkey']);
  400. } else {
  401. openssl_private_encrypt($hash, $challenge, $r[0]['prvkey']);
  402. openssl_private_encrypt($id_str, $encrypted_id, $r[0]['prvkey']);
  403. }
  404. $challenge = bin2hex($challenge);
  405. $encrypted_id = bin2hex($encrypted_id);
  406. } else {
  407. $status = 1;
  408. $challenge = '';
  409. $encrypted_id = '';
  410. }
  411. if (($type === 'profile') && (strlen($sec))) {
  412. // heluecht: I don't know why we don't fail immediately when the user or contact hadn't been found.
  413. // Since it doesn't make sense to continue from this point on, we now fail here. This should be safe.
  414. if (!DBA::isResult($r)) {
  415. System::httpExit(404, ["title" => L10n::t('Page not found.')]);
  416. }
  417. // URL reply
  418. if ($dfrn_version < 2.2) {
  419. $s = Network::fetchUrl($r[0]['poll']
  420. . '?dfrn_id=' . $encrypted_id
  421. . '&type=profile-check'
  422. . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
  423. . '&challenge=' . $challenge
  424. . '&sec=' . $sec
  425. );
  426. } else {
  427. $s = Network::post($r[0]['poll'], [
  428. 'dfrn_id' => $encrypted_id,
  429. 'type' => 'profile-check',
  430. 'dfrn_version' => DFRN_PROTOCOL_VERSION,
  431. 'challenge' => $challenge,
  432. 'sec' => $sec
  433. ])->getBody();
  434. }
  435. Logger::log("dfrn_poll: sec profile: " . $s, Logger::DATA);
  436. if (strlen($s) && strstr($s, '<?xml')) {
  437. $xml = XML::parseString($s);
  438. Logger::log('dfrn_poll: profile: parsed xml: ' . print_r($xml, true), Logger::DATA);
  439. Logger::log('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
  440. Logger::log('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
  441. if (((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
  442. $_SESSION['authenticated'] = 1;
  443. if (empty($_SESSION['remote'])) {
  444. $_SESSION['remote'] = [];
  445. }
  446. $_SESSION['remote'][] = ['cid' => $r[0]['id'], 'uid' => $r[0]['uid'], 'url' => $r[0]['url']];
  447. $_SESSION['visitor_id'] = $r[0]['id'];
  448. $_SESSION['visitor_home'] = $r[0]['url'];
  449. $_SESSION['visitor_visiting'] = $r[0]['uid'];
  450. $_SESSION['my_url'] = $r[0]['url'];
  451. if (!$quiet) {
  452. info(L10n::t('%1$s welcomes %2$s', $r[0]['username'], $r[0]['name']) . EOL);
  453. }
  454. // Visitors get 1 day session.
  455. $session_id = session_id();
  456. $expire = time() + 86400;
  457. q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
  458. DBA::escape($expire),
  459. DBA::escape($session_id)
  460. );
  461. }
  462. }
  463. $profile = ((DBA::isResult($r) && $r[0]['nickname']) ? $r[0]['nickname'] : $nickname);
  464. switch ($destination_url) {
  465. case 'profile':
  466. $a->internalRedirect('profile/' . $profile . '?f=&tab=profile');
  467. break;
  468. case 'photos':
  469. $a->internalRedirect('photos/' . $profile);
  470. break;
  471. case 'status':
  472. case '':
  473. $a->internalRedirect('profile/' . $profile);
  474. break;
  475. default:
  476. $appendix = (strstr($destination_url, '?') ? '&f=&redir=1' : '?f=&redir=1');
  477. $a->redirect($destination_url . $appendix);
  478. break;
  479. }
  480. // NOTREACHED
  481. } else {
  482. // XML reply
  483. header("Content-type: text/xml");
  484. echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
  485. . '<dfrn_poll>' . "\r\n"
  486. . "\t" . '<status>' . $status . '</status>' . "\r\n"
  487. . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
  488. . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
  489. . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
  490. . '</dfrn_poll>' . "\r\n";
  491. killme();
  492. // NOTREACHED
  493. }
  494. }
  495. }