authorize.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * LiveEdu.tv API authorization.
  4. *
  5. * @package LctvApi\Authorize
  6. * @version 0.0.9
  7. * @since 0.0.3
  8. */
  9. /** Initialize. */
  10. require( 'LctvApi.inc' );
  11. if ( ( ! isset( $_GET['state'] ) || empty( $_GET['state'] ) ) &&
  12. ( ! isset( $_GET['code'] ) || empty( $_GET['code'] ) ) ) {
  13. if ( ! isset( $_GET['user'] ) || empty( $_GET['user'] ) ) {
  14. ?>
  15. <script type="text/javascript">
  16. var loc = window.location;
  17. var this_location = loc.protocol + "//" + loc.host + loc.pathname;
  18. do var user = prompt("Please enter your LCTV nick:", ""); while (user == null);
  19. window.location = this_location + "?user=" + user;
  20. </script>
  21. <?php
  22. exit ;
  23. }
  24. else {
  25. $auth_user = htmlspecialchars( $_GET['user'] );
  26. }
  27. }
  28. else {
  29. $auth_user = '';
  30. }
  31. /** Load the API. */
  32. try {
  33. $lctv_api = new LctvApi( $auth_user );
  34. }
  35. catch(Exception $ex) {
  36. die($ex->getMessage());
  37. }
  38. ?>
  39. <!DOCTYPE html>
  40. <html lang="en-US">
  41. <head>
  42. <title>LCTV Badges Authorization</title>
  43. </head>
  44. <body style="font-family:Helvetica,Arial,sans-serif;font-size:15px;font-weight:400;color:#111;">
  45. <?php define(TRACE , false) ;
  46. if ( TRACE ) { ?>
  47. // begin DEBUG
  48. <pre>constants
  49. * <?php echo "LCTV_CLIENT_ID = '" . LCTV_CLIENT_ID . "'\n" ?>
  50. * <?php echo "LCTV_CLIENT_SECRET = '" . LCTV_CLIENT_SECRET . "'\n" ?>
  51. * <?php echo "LCTV_REDIRECT_URL = '" . LCTV_REDIRECT_URL . "'\n" ?>
  52. * <?php echo "LCTVAPI_STORAGE_CLASS = '" . LCTVAPI_STORAGE_CLASS . "'\n" ?>
  53. * <?php echo "LCTV_MASTER_USER = '" . LCTV_MASTER_USER . "'\n" ?>
  54. </pre>
  55. <pre>params
  56. <?php $is_state_set = isset( $_GET['state'] ) ? "true" : "false"; ?>
  57. <?php $is_state_empty = empty( $_GET['state'] ) ? "true" : "false" ; ?>
  58. <?php $is_code_set = isset( $_GET['code'] ) ? "true" : "false"; ?>
  59. <?php $is_code_empty = empty( $_GET['code'] ) ? "true" : "false" ; ?>
  60. <?php $is_user_set = isset( $_GET['user'] ) ? "true" : "false"; ?>
  61. <?php $is_user_empty = empty( $_GET['user'] ) ? "true" : "false" ; ?>
  62. * <?php echo "isset(_GET['state']) = $is_state_set\n"; ?>
  63. * <?php echo "empty(_GET['state']) = $is_state_empty\n"; ?>
  64. * <?php echo "isset(_GET['code']) = $is_code_set\n"; ?>
  65. * <?php echo "empty(_GET['code']) = $is_code_empty\n"; ?>
  66. * <?php echo "isset(_GET['user']) = $is_user_set\n"; ?>
  67. * <?php echo "empty(_GET['user']) = $is_user_empty\n"; ?>
  68. * <?php echo "user=$auth_user\n"; ?>
  69. </pre>
  70. <?php $client_id = (isset($lctv_api->client_id )) ? $lctv_api->client_id : "unset" ; ?>
  71. <?php $client_secret = (isset($lctv_api->client_secret)) ? $lctv_api->client_secret : "unset" ; ?>
  72. <?php $data_store = (isset($lctv_api->data_store )) ? $lctv_api->data_store : "unset" ; ?>
  73. <?php $redirect_url = (isset($lctv_api->redirect_url )) ? $lctv_api->redirect_url : "unset" ; ?>
  74. <?php $scope = (isset($lctv_api->scope )) ? $lctv_api->scope : "unset" ; ?>
  75. <?php $auth_user = (isset($lctv_api->auth_user )) ? $lctv_api->auth_user : "unset" ; ?>
  76. <pre>library
  77. * <?php echo "lctv_api->client_id = '$client_id'\n" ?>
  78. * <?php echo "lctv_api->client_secret = '$client_secret'\n" ?>
  79. * <?php echo "lctv_api->redirect_url = '$redirect_url'\n" ?>
  80. * <?php echo "lctv_api->data_store = '" . LCTVAPI_STORAGE_CLASS . "'\n" ?>
  81. * <?php echo "lctv_api->scope = '$scope'\n" ?>
  82. * <?php echo "lctv_api->auth_user = '$auth_user'\n" ?>
  83. </pre>
  84. <?php
  85. $is_set= isset($lctv_api->token) ;
  86. $is_empty = empty($lctv_api->token) ;
  87. $is_authorized = $is_set && isset($lctv_api->token->access_token);
  88. $token_dump = var_export($lctv_api->token , true) ;
  89. $token = (($is_set) ? (($is_empty) ? "empty" : $token_dump): "unset") ;
  90. $access_token = (($is_authorized) ? $lctv_api->token->access_token : "unset") ;
  91. ?>
  92. <pre>tokens
  93. * <?php echo "token=$token"; ?>\n
  94. * <?php echo "access_token=$access_token"; ?>\n
  95. * <?php echo "LCTVAPI_STORAGE_PATH=" . LCTVAPI_STORAGE_PATH; ?>\n
  96. </pre>
  97. // end DEBUG
  98. <?php } ?>
  99. <div style="padding:20px;width:600px;margin:0 auto;">
  100. <?php if ( ! $lctv_api->is_authorized() ) : ?>
  101. <h1 style="font-size:24px;font-weight:400;">Authorize</h1>
  102. <p>LCTV Badges use the LiveEdu.tv API to get their relevant stats.
  103. Badges that display data from public API endpoints require only the site admin to authorize this site.
  104. Badges that display data from private API endpoints require each target user to authorize this site.
  105. Vistors to the site will not need to authorize.
  106. Authorize account access with the link below.</p>
  107. <p><a href="<?php echo $lctv_api->get_authorization_url(); ?>">Connect your account</a></p>
  108. <br><br>
  109. <h1 style="font-size:24px;font-weight:400;">Remove Authorization</h1>
  110. <?php if ( isset( $_GET['user'] ) && isset( $_GET['delete'] ) ) : ?>
  111. <p>The account &ldquo;<?php echo htmlspecialchars( $_GET['user'] ); ?>&rdquo; has been disconnected.</p>
  112. <?php else : ?>
  113. <p>This site saves an authorization token to read certain status information from your LiveEdu.tv account. This site can not modify your LiveEdu.tv account in any way. You may delete this token at any time so as to stop this site from further accessing your account information.</p>
  114. <p>To disconnect your account, first connect your account with the link above, even if you have previously done so. After authorization a link will appear for this purpose.</p>
  115. <?php endif; ?>
  116. <?php else : ?>
  117. <h1 style="font-size:24px;font-weight:400;">Authorize</h1>
  118. <p>The account &ldquo;<?php echo htmlspecialchars( $lctv_api->auth_user ); ?>&rdquo; is now connected.</p>
  119. <br><br>
  120. <h1 style="font-size:24px;font-weight:400;">Remove Authorization</h1>
  121. <p>This site saves an authorization token to read certain status information from your LiveEdu.tv account. This site can not modify your LiveEdu.tv account in any way. You may delete this token so as to stop this site from further accessing your account information with the link below.</p>
  122. <p><a href="<?php echo $lctv_api->redirect_url; ?>?user=<?php echo urlencode( $lctv_api->auth_user ); ?>&delete=<?php echo urlencode( $lctv_api->token->delete_id ); ?>">Disconnect your account</a></p>
  123. <?php endif; ?>
  124. <br><br>
  125. <p><a href="./">Return Home</a></p>
  126. </div>
  127. </body>
  128. </html>
  129. <?php