oauth.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. if(!defined('ABSPATH')) define('ABSPATH', $_SERVER['DOCUMENT_ROOT'] . '/');
  3. require ABSPATH.'functions/tools.php';
  4. $opts = load_opts();
  5. $auth = (isset($_GET['a'])) ? sanitize($_GET['a']) : $opts->user_auth;
  6. /* ------------------------------------------------------------------------------------
  7. * Goosle - The fast, privacy oriented search tool that just works.
  8. *
  9. * COPYRIGHT NOTICE
  10. * Copyright 2023-2024 Arnan de Gans. All Rights Reserved.
  11. *
  12. * COPYRIGHT NOTICES AND ALL THE COMMENTS SHOULD REMAIN INTACT.
  13. * By using this code you agree to indemnify Arnan de Gans from any
  14. * liability that might arise from its use.
  15. ------------------------------------------------------------------------------------ */
  16. ?>
  17. <!DOCTYPE html>
  18. <html lang="en">
  19. <head>
  20. <title>Goosle Search oAUTH</title>
  21. <meta charset="utf-8" />
  22. <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
  23. <meta name="robots" content="noodp,noydir" />
  24. <meta name="referrer" content="no-referrer"/>
  25. <meta name="description" content="Get your Goosle on! - The best meta search engine for private and fast internet fun!" />
  26. <link rel="icon" href="../favicon.ico" />
  27. <link rel="apple-touch-icon" href="../apple-touch-icon.png" />
  28. <link rel="canonical" href="<?php echo get_base_url($opts->siteurl); ?>/functions/oauth.php" />
  29. <link rel="stylesheet" type="text/css" href="<?php echo get_base_url($opts->siteurl); ?>/assets/css/styles.css"/>
  30. <link rel="stylesheet" type="text/css" href="<?php echo get_base_url($opts->siteurl); ?>/assets/css/<?php echo $opts->colorscheme; ?>.css"/>
  31. </head>
  32. <body class="oauthpage">
  33. <?php
  34. if(verify_hash($opts->hash_auth, $opts->hash, $auth)) {
  35. ?>
  36. <div class="content">
  37. <?php
  38. $connect = (isset($_REQUEST['oa'])) ? sanitize($_REQUEST['oa']) : '';
  39. // Openverse
  40. $email = (isset($_REQUEST['oae'])) ? sanitize($_REQUEST['oae']) : '';
  41. $client_id = (isset($_REQUEST['oaid'])) ? sanitize($_REQUEST['oaid']) : '';
  42. $client_secret = (isset($_REQUEST['oacs'])) ? sanitize($_REQUEST['oacs']) : '';
  43. if(empty($connect)) {
  44. ?>
  45. <div class="oauth-form">
  46. <h1><span class="goosle-g">G</span>oosle</h1>
  47. <p>Use this page to set up an authorization token for Openverse.<br />
  48. Fill in the relevant fields and click the button at the bottom to continue.</p>
  49. <form action="oauth.php" method="get" autocomplete="off">
  50. <h2>Registration</h2>
  51. <p>Email address:<br /><input tabindex="10" type="text" class="field" name="oae" /><br /><small>(Always required for verification)</small></p>
  52. <h3>Recovering a previous registration?</h3>
  53. <p>Client ID:<br /><input tabindex="20" type="text" class="field" name="oaid" /></p>
  54. <p>Client Secret:<br /><input tabindex="30" type="text" class="field" name="oacs" /></p>
  55. <input type="hidden" name="a" value="<?php echo $opts->hash; ?>"/>
  56. <div class="oauth-buttons">
  57. <button tabindex="100" name="oa" value="openverse" type="submit">Connect to Openverse</button>
  58. </div>
  59. <a href="/">Back to Goosle</a>
  60. </form>
  61. </div>
  62. <?php
  63. } else {
  64. $token_file = ABSPATH.'cache/token.data';
  65. if(empty($client_id) AND empty($client_secret) AND !empty($email)) {
  66. $registration = do_curl_request(
  67. 'https://api.openverse.org/v1/auth_tokens/register/', // (string) Where?
  68. array('Accept: application/json, */*;q=0.8', 'User-Agent: '.$opts->user_agents[0].';'), // (array) Headers
  69. 'post', // (string) post/get
  70. array('name' => 'Goosle Meta Search '.md5(get_base_url($opts->siteurl)), 'description' => 'Goosle Meta Search for '.get_base_url($opts->siteurl), 'email' => $email) // (assoc array) Post body
  71. );
  72. $registration = json_decode($registration, true);
  73. // Site already exists, get new token
  74. if(stristr($registration['name'][0], 'this name already exists')) {
  75. if(is_file($token_file)) {
  76. $tokens = unserialize(file_get_contents($token_file));
  77. $registration = $tokens['openverse'];
  78. } else {
  79. echo "<div class=\"auth-error\">Error - Token file is missing. Please recover your registration with the Client ID and Client Secret.<br /><a href=\"/functions/oauth.php?a=".$opts->hash."\">Try again</a></div>";
  80. exit;
  81. }
  82. }
  83. } else {
  84. $registration = array('client_id' => $client_id, 'client_secret' => $client_secret);
  85. }
  86. $new_token = do_curl_request(
  87. 'https://api.openverse.org/v1/auth_tokens/token/', // (string) Where?
  88. array('Accept: application/json, */*;q=0.8', 'User-Agent: '.$opts->user_agents[0].';', 'Authorization: Bearer'.$registration['client_id']), // (array) Headers
  89. 'post', // (string) post/get
  90. array('grant_type' => 'client_credentials', 'client_id' => $registration['client_id'], 'client_secret' => $registration['client_secret']) // (assoc array) Post body
  91. );
  92. $new_token = json_decode($new_token, true);
  93. $new_token['expires_in'] = time() + ($new_token['expires_in'] - 3600);
  94. oauth_store_token($token_file, $connect, array('client_id' => $registration['client_id'], 'client_secret' => $registration['client_secret'], 'access_token' => $new_token['access_token'], 'expires' => $new_token['expires_in']));
  95. echo "<div class=\"auth-success\"><p>SUCCESS!</p>";
  96. echo "<p>Goosle is now authorized and you can enable Openverse in your config.php!<br />If this is your first time authorizing with this email address you will receive an email from Openverse in a few minutes with a verification link that you need to click.</p>";
  97. echo "<p>To be able to recover your registration save these values:</p>";
  98. echo "<p>Used Email Address: ".$email."<br />Client ID: ".$registration['client_id']."<br />Client Secret: ".$registration['client_secret']."<br /><br /><a href=\"/results.php?a=".$opts->hash."&q=goose&t=1\">Continue to Goosle</div>";
  99. unset($registration, $new_token);
  100. }
  101. ?>
  102. </div>
  103. <?php
  104. } else {
  105. echo "<div class=\"auth-error\">Goosle</div>";
  106. }
  107. ?>
  108. </body>
  109. </html>