PHP OpenID consumer
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.

index.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?PHP
  2. /* Poidsy 0.4 - http://chris.smith.name/projects/poidsy
  3. * Copyright (c) 2008 Chris Smith
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. // Poidsy returns its results in a session variable, so we need to start the
  24. // session here in order to get access to the results
  25. session_start();
  26. // Request some information from the identity provider. Note that providers
  27. // don't have to implement the extension that provides this, so you can't
  28. // rely on getting results back. You can use OPENID_SREG_REQUEST to more
  29. // strongly request the information (it implies that the user will have to
  30. // manually enter the data if the provider doesn't supply it).
  31. //
  32. // The fields listed here are all the valid SREG fields. Anything else
  33. // almost certainly won't work (but you can of course omit ones you don't
  34. // need).
  35. define('OPENID_SREG_OPTIONAL',
  36. 'nickname,email,fullname,dob,gender,postcode,country,language,timezone');
  37. if (isset($_POST['openid_url']) || isset($_REQUEST['openid_mode'])) {
  38. // There are two cases when poidsy's processor needs to be invoked - firstly,
  39. // when the user has just submitted an OpenID identifier to be verified, in
  40. // which case $_POST['openid_url'] will be present (poidsy has special
  41. // handling for inputs named openid_url. If you want to use a URL from
  42. // another source, you can define the OPENID_URL constant instead.).
  43. // Secondly, if the user is being redirected back from their provider, the
  44. // openid.mode parameter will be present (which PHP translates to openid_mode)
  45. require('../../processor.php');
  46. } else {
  47. // If we don't have any processing to be doing, show them the form and
  48. // results.
  49. ?>
  50. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  51. "http://www.w3.org/TR/html4/strict.dtd">
  52. <html>
  53. <head>
  54. <title>OpenID consumer demonstration</title>
  55. <style type="text/css">
  56. input#openid_url {
  57. background: url('../../openid.gif') no-repeat; padding-left: 20px;
  58. }
  59. p { padding-left: 10px; }
  60. p.error { border-left: 10px solid #f00; }
  61. p.succ { border-left: 10px solid #0f0; }
  62. caption { text-align: left; }
  63. table { margin: 10px; }
  64. </style>
  65. </head>
  66. <body>
  67. <h1>OpenID consumer demo</h1>
  68. <?PHP
  69. if (isset($_SESSION['openid']['error'])) {
  70. // If the error variable is set, it means that poidsy has encountered an
  71. // error while trying to validate the identifier. We just tell the user
  72. // what went wrong, and unset the session vars so the messages don't persist
  73. echo '<p class="error">An error occured: ', htmlentities($_SESSION['openid']['error']), '</p>';
  74. unset($_SESSION['openid']['error']);
  75. } else if (isset($_SESSION['openid']['validated']) && $_SESSION['openid']['validated']) {
  76. // Upon a successful validation, the validated field will have been set to
  77. // true. It's important to check the validated field, as the identity
  78. // will be specified in the array throughout the process, so it would be
  79. // possible for the user to request the page with an identity specified
  80. // but before Poidsy had validated it. As above, we unset the session
  81. // vars so that the details don't persist.
  82. echo '<p class="succ">Success: your OpenID identifier is <em>', htmlentities($_SESSION['openid']['identity']), '</em></p>';
  83. unset($_SESSION['openid']['validated']);
  84. // Show the SREG data returned, if any. SREG data is only present if you
  85. // defined one of the OPENID_SREG constants before the request was sent,
  86. // if the user's identity provider supports SREG, and if (depending on the
  87. // provider) the user gives permission for you to have the data.
  88. if (isset($_SESSION['openid']['sreg'])) {
  89. echo '<table>';
  90. echo '<caption>Simple Registration Extension data</caption>';
  91. foreach ($_SESSION['openid']['sreg'] as $type => $data) {
  92. echo '<tr><th>', htmlentities($type), '</th>';
  93. echo '<td>', htmlentities($data), '</td></tr>';
  94. }
  95. echo '</table>';
  96. unset($_SESSION['openid']['sreg']);
  97. }
  98. }
  99. ?>
  100. <form action="<?PHP echo htmlentities($_SERVER['REQUEST_URI']); ?>"
  101. method="post">
  102. <input type="text" name="openid_url" id="openid_url">
  103. <input type="submit" value="Login">
  104. </form>
  105. </body>
  106. </html>
  107. <?PHP
  108. }
  109. ?>