twitter.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. set_include_path(get_include_path() . PATH_SEPARATOR .
  3. dirname(__FILE__) . "/include");
  4. require_once "functions.php";
  5. require_once "sessions.php";
  6. require_once "sanity_check.php";
  7. require_once "config.php";
  8. require_once "db.php";
  9. //require_once "lib/twitteroauth/twitteroauth.php";
  10. require_once "lib/tmhoauth/tmhOAuth.php";
  11. $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  12. if (!init_connection($link)) return;
  13. login_sequence($link);
  14. $owner_uid = $_SESSION["uid"];
  15. $op = $_REQUEST['op'];
  16. if (!SINGLE_USER_MODE && !$_SESSION['uid']) {
  17. render_login_form($link);
  18. exit;
  19. }
  20. $callback_url = get_self_url_prefix() . "/twitter.php?op=callback";
  21. $tmhOAuth = new tmhOAuth(array(
  22. 'consumer_key' => CONSUMER_KEY,
  23. 'consumer_secret' => CONSUMER_SECRET,
  24. ));
  25. if ($op == 'clear') {
  26. unset($_SESSION['oauth']);
  27. header("Location: twitter.php");
  28. return;
  29. }
  30. if (isset($_REQUEST['oauth_verifier'])) {
  31. $op = 'callback';
  32. $tmhOAuth->config['user_token'] = $_SESSION['oauth']['oauth_token'];
  33. $tmhOAuth->config['user_secret'] = $_SESSION['oauth']['oauth_token_secret'];
  34. $code = $tmhOAuth->request('POST', $tmhOAuth->url('oauth/access_token', ''), array(
  35. 'oauth_verifier' => $_REQUEST['oauth_verifier']));
  36. if ($code == 200) {
  37. $access_token = json_encode($tmhOAuth->extract_params($tmhOAuth->response['response']));
  38. unset($_SESSION['oauth']);
  39. db_query($link, "UPDATE ttrss_users SET twitter_oauth = '$access_token'
  40. WHERE id = ".$_SESSION['uid']);
  41. } else {
  42. header('Location: twitter.php?op=clear');
  43. return;
  44. }
  45. }
  46. if ($op == 'register') {
  47. $code = $tmhOAuth->request('POST',
  48. $tmhOAuth->url('oauth/request_token', ''), array(
  49. 'oauth_callback' => $callback));
  50. if ($code == 200) {
  51. $_SESSION['oauth'] = $tmhOAuth->extract_params($tmhOAuth->response['response']);
  52. $method = isset($_REQUEST['signin']) ? 'authenticate' : 'authorize';
  53. $force = isset($_REQUEST['force']) ? '&force_login=1' : '';
  54. $forcewrite = isset($_REQUEST['force_write']) ? '&oauth_access_type=write' : '';
  55. $forceread = isset($_REQUEST['force_read']) ? '&oauth_access_type=read' : '';
  56. $location = $tmhOAuth->url("oauth/{$method}", '') .
  57. "?oauth_token={$_SESSION['oauth']['oauth_token']}{$force}{$forcewrite}{$forceread}";
  58. header("Location: $location");
  59. }
  60. }
  61. ?>
  62. <html>
  63. <head>
  64. <title>Register with Twitter</title>
  65. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  66. <link rel="stylesheet" type="text/css" href="utility.css">
  67. </head>
  68. <body>
  69. <h1><?php echo __('Register with Twitter') ?></h1>
  70. <?php if ($op == 'register') { ?>
  71. <p><?php print_error(__('Could not connect to Twitter. Refresh the page or try again later.')) ?></p>
  72. <?php } else if ($op == 'callback') { ?>
  73. <p><?php print_notice(__('Congratulations! You have successfully registered with Twitter.')) ?>
  74. </p>
  75. <form method="GET" action="prefs.php">
  76. <input type="hidden" name="tab" value="feedConfig">
  77. <button type="submit"><?php echo __('Return to Tiny Tiny RSS') ?></button>
  78. </form>
  79. <?php } else { ?>
  80. <form method="GET" action="twitter.php" style='display : inline'>
  81. <input type="hidden" name="op" value="register">
  82. <button type="submit"><?php echo __('Register') ?></button>
  83. </form>
  84. <form method="GET" action="prefs.php" style='display : inline'>
  85. <input type="hidden" name="tab" value="feedConfig">
  86. <button type="submit"><?php echo __('Return to Tiny Tiny RSS') ?></button>
  87. </form>
  88. <?php } ?>
  89. </body>
  90. </html>