test_consumer.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_OpenId
  17. * @subpackage Demos
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. $dir = realpath(dirname(__FILE__)."/../../..");
  22. set_include_path("$dir/library" . PATH_SEPARATOR . get_include_path());
  23. /**
  24. * @see Zend_OpenId_Consumer
  25. */
  26. require_once "Zend/OpenId/Consumer.php";
  27. /**
  28. * @see Zend_OpenId_Extension_Sreg
  29. */
  30. require_once "Zend/OpenId/Extension/Sreg.php";
  31. $id = "";
  32. $status = "";
  33. $data = array();
  34. if (isset($_POST['openid_action']) &&
  35. $_POST['openid_action'] == "login" &&
  36. !empty($_POST['openid_identifier'])) {
  37. $consumer = new Zend_OpenId_Consumer();
  38. $props = array();
  39. foreach (Zend_OpenId_Extension_Sreg::getSregProperties() as $prop) {
  40. if (isset($_POST[$prop])) {
  41. if ($_POST[$prop] === "required") {
  42. $props[$prop] = true;
  43. } else if ($_POST[$prop] === "optional") {
  44. $props[$prop] = false;
  45. }
  46. }
  47. }
  48. $sreg = new Zend_OpenId_Extension_Sreg($props, null, 1.1);
  49. $id = $_POST['openid_identifier'];
  50. if (!$consumer->login($id, null, null, $sreg)) {
  51. $status = "OpenID login failed (".$consumer->getError().")";
  52. }
  53. } else if (isset($_GET['openid_mode'])) {
  54. if ($_GET['openid_mode'] == "id_res") {
  55. $sreg = new Zend_OpenId_Extension_Sreg();
  56. $consumer = new Zend_OpenId_Consumer();
  57. if ($consumer->verify($_GET, $id, $sreg)) {
  58. $status = "VALID $id";
  59. $data = $sreg->getProperties();
  60. } else {
  61. $status = "INVALID $id (".$consumer->getError().")";
  62. }
  63. } else if ($_GET['openid_mode'] == "cancel") {
  64. $status = "CANCELED";
  65. }
  66. }
  67. $sreg_html = "";
  68. $sreg = new Zend_OpenId_Extension_Sreg();
  69. foreach (Zend_OpenId_Extension_Sreg::getSregProperties() as $prop) {
  70. $val = isset($data[$prop]) ? $data[$prop] : "";
  71. $sreg_html .= <<<EOF
  72. <tr><td>$prop</td>
  73. <td>
  74. <input type="radio" name="$prop" value="required">
  75. </td><td>
  76. <input type="radio" name="$prop" value="optional">
  77. </td><td>
  78. <input type="radio" name="$prop" value="none" checked="1">
  79. </td><td>
  80. $val
  81. </td></tr>
  82. EOF;
  83. }
  84. ?>
  85. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  86. <html>
  87. <head>
  88. <title>Zend OpenID Consumer Example</title>
  89. <style>
  90. input.openid_login {
  91. background: url(login-bg.gif) no-repeat;
  92. background-color: #fff;
  93. background-position: 0 50%;
  94. color: #000;
  95. padding-left: 18px;
  96. width: 220px;
  97. margin-right: 10px;
  98. }
  99. </style>
  100. </head>
  101. <body>
  102. <?php echo "$status<br>\n";?>
  103. <div>
  104. <form action="<?php echo Zend_OpenId::selfUrl(); ?>"
  105. method="post" onsubmit="this.login.disabled=true;">
  106. <fieldset id="openid">
  107. <legend>OpenID Login</legend>
  108. <input type="hidden" name="openid_action" value="login">
  109. <div>
  110. <input type="text" name="openid_identifier" class="openid_login" value="<?php echo $id;?>">
  111. <input type="submit" name="login" value="login">
  112. <table border="0" cellpadding="2" cellspacing="2">
  113. <tr><td>&nbsp;</td><td>requird</td><td>optional</td><td>none</td><td>&nbsp</td></tr>
  114. <?php echo "$sreg_html<br>\n";?>
  115. </table>
  116. <br>
  117. <a href="<?php echo dirname(Zend_OpenId::selfUrl()); ?>/test_server.php?openid.action=register">register</a>
  118. </div>
  119. </fieldset>
  120. </form>
  121. </div>
  122. </body>
  123. </html>