PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/openid/login.php

https://github.com/alx/alexgirard.com-blog
PHP | 225 lines | 152 code | 38 blank | 35 comment | 28 complexity | aabc997e62bde4b8e05982d08057b431 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * All the code required for handling logins via wp-login.php. These functions should not be considered public,
  4. * and may change without notice.
  5. */
  6. // add OpenID input field to wp-login.php
  7. add_action( 'login_head', 'openid_wp_login_head');
  8. add_action( 'login_form', 'openid_wp_login_form');
  9. add_action( 'register_form', 'openid_wp_register_form');
  10. add_action( 'register_post', 'openid_register_post');
  11. add_action( 'wp_authenticate', 'openid_wp_authenticate' );
  12. add_action( 'openid_finish_auth', 'openid_finish_login' );
  13. add_filter( 'registration_errors', 'openid_registration_errors');
  14. add_action( 'init', 'openid_login_errors' );
  15. add_filter( 'openid_consumer_return_urls', 'openid_wp_login_return_url' );
  16. // WordPress 2.5 has wp_authenticate in the wrong place
  17. if (strpos($wp_version, '2.5') === 0) {
  18. add_action( 'init', 'wp25_login_openid' );
  19. }
  20. /**
  21. * If we're doing openid authentication ($_POST['openid_identifier'] is set), start the consumer & redirect
  22. * Otherwise, return and let WordPress handle the login and/or draw the form.
  23. *
  24. * @param string $credentials username and password provided in login form
  25. */
  26. function openid_wp_authenticate(&$credentials) {
  27. if (array_key_exists('openid_consumer', $_REQUEST)) {
  28. finish_openid('login');
  29. } else if (!empty($_POST['openid_identifier'])) {
  30. openid_start_login( $_POST['openid_identifier'], 'login', array('redirect_to' => $_REQUEST['redirect_to']), site_url('/wp-login.php', 'login_post'));
  31. }
  32. }
  33. function openid_login_errors() {
  34. $self = basename( $GLOBALS['pagenow'] );
  35. if ($self != 'wp-login.php') return;
  36. if ($_REQUEST['openid_error']) {
  37. global $error;
  38. $error = $_REQUEST['openid_error'];
  39. }
  40. if ($_REQUEST['registration_closed']) {
  41. global $error;
  42. $error = __('OpenID authentication valid, but unable to find a WordPress account associated with this OpenID.', 'openid')
  43. . '<br /><br />'
  44. . __('Enable "Anyone can register" to allow creation of new accounts via OpenID.', 'openid');
  45. }
  46. }
  47. function openid_wp_login_head() {
  48. openid_style();
  49. wp_enqueue_script('jquery.xpath', openid_plugin_url() . '/f/jquery.xpath.min.js',
  50. array('jquery'), OPENID_PLUGIN_REVISION);
  51. wp_print_scripts(array('jquery.xpath'));
  52. }
  53. /**
  54. * Add OpenID input field to wp-login.php
  55. *
  56. * @action: login_form
  57. **/
  58. function openid_wp_login_form() {
  59. global $wp_version;
  60. echo '<hr id="openid_split" style="clear: both; margin-bottom: 1.5em; border: 0; border-top: 1px solid #999; height: 1px;" />';
  61. echo '
  62. <p>
  63. <label style="display: block; margin-bottom: 5px;">' . __('Or login using an OpenID:', 'openid') . '</label>
  64. <input type="text" name="openid_identifier" id="openid_identifier" class="input openid_identifier" value="" size="20" tabindex="25" /></label>
  65. </p>
  66. <p style="font-size: 0.8em;" id="what_is_openid">
  67. <a href="http://openid.net/what/" target="_blank">'.__('What is OpenID?', 'openid').'</a>
  68. </p>';
  69. }
  70. /**
  71. * Add information about registration to wp-login.php?action=register
  72. *
  73. * @action: register_form
  74. **/
  75. function openid_wp_register_form() {
  76. global $wp_version;
  77. if (get_option('openid_required_for_registration')) {
  78. $label = __('Register using an OpenID:', 'openid');
  79. echo '
  80. <script type="text/javascript">
  81. jQuery(function() {
  82. jQuery("#user_login/..").hide();
  83. jQuery("#user_email/..").hide();
  84. jQuery("#reg_passmail").hide();
  85. var link = jQuery("#nav a:first");
  86. jQuery("#nav").text("").append(link);
  87. });
  88. </script>';
  89. } else {
  90. $label = __('Or register using an OpenID:', 'openid');
  91. echo '<hr id="openid_split" style="clear: both; margin-bottom: 1.5em; border: 0; border-top: 1px solid #999; height: 1px;" />';
  92. echo '
  93. <script type="text/javascript">
  94. jQuery(function() {
  95. jQuery("#reg_passmail").insertBefore("#openid_split");
  96. jQuery("p.submit").clone().insertBefore("#openid_split");
  97. });
  98. </script>';
  99. }
  100. echo '
  101. <p>
  102. <label style="display: block; margin-bottom: 5px;">' . $label . '</label>
  103. <input type="text" name="openid_identifier" id="openid_identifier" class="input openid_identifier" value="" size="20" tabindex="25" /></label>
  104. </p>
  105. <p style="float: left; font-size: 0.8em;" id="what_is_openid">
  106. <a href="http://openid.net/what/" target="_blank">'.__('What is OpenID?', 'openid').'</a>
  107. </p>';
  108. }
  109. /**
  110. * Action method for completing the 'login' action. This action is used when a user is logging in from
  111. * wp-login.php.
  112. *
  113. * @param string $identity_url verified OpenID URL
  114. */
  115. function openid_finish_login($identity_url) {
  116. if ($_REQUEST['action'] != 'login') return;
  117. $redirect_to = urldecode($_REQUEST['redirect_to']);
  118. if (empty($identity_url)) {
  119. $url = get_option('siteurl') . '/wp-login.php?openid_error=' . urlencode(openid_message());
  120. wp_safe_redirect($url);
  121. exit;
  122. }
  123. openid_set_current_user($identity_url);
  124. if (!is_user_logged_in()) {
  125. if ( get_option('users_can_register') ) {
  126. $user_data =& openid_get_user_data($identity_url);
  127. $user = openid_create_new_user($identity_url, $user_data);
  128. openid_set_current_user($user->ID);
  129. } else {
  130. // TODO - Start a registration loop in WPMU.
  131. $url = get_option('siteurl') . '/wp-login.php?registration_closed=1';
  132. wp_safe_redirect($url);
  133. exit;
  134. }
  135. }
  136. if (empty($redirect_to)) {
  137. $redirect_to = 'wp-admin/';
  138. }
  139. if ($redirect_to == 'wp-admin/') {
  140. if (!current_user_can('edit_posts')) {
  141. $redirect_to .= 'profile.php';
  142. }
  143. }
  144. if (!preg_match('#^(http|\/)#', $redirect_to)) {
  145. $wpp = parse_url(get_option('siteurl'));
  146. $redirect_to = $wpp['path'] . '/' . $redirect_to;
  147. }
  148. wp_safe_redirect( $redirect_to );
  149. exit;
  150. }
  151. /**
  152. * Intercept login requests on wp-login.php if they include an 'openid_identifier'
  153. * value and start OpenID authentication. This hook is only necessary in
  154. * WordPress 2.5.x because it has the 'wp_authenticate' action call in the
  155. * wrong place.
  156. */
  157. function wp25_login_openid() {
  158. $self = basename( $GLOBALS['pagenow'] );
  159. if ($self == 'wp-login.php' && !empty($_POST['openid_identifier'])) {
  160. wp_signon(array('user_login'=>'openid', 'user_password'=>'openid'));
  161. }
  162. }
  163. function openid_registration_errors($errors) {
  164. if (get_option('openid_required_for_registration')) {
  165. $errors = new WP_Error();
  166. if (empty($_POST['openid_identifier'])) {
  167. $errors->add('openid_only', __('<strong>ERROR</strong>: ', 'openid') . __('New users must register using OpenID.', 'openid'));
  168. }
  169. }
  170. if (!empty($_POST['openid_identifier'])) {
  171. $errors->add('invalid_openid', __('<strong>ERROR</strong>: ', 'openid') . openid_message());
  172. }
  173. return $errors;
  174. }
  175. function openid_register_post($errors) {
  176. if (!empty($_POST['openid_identifier'])) {
  177. wp_signon(array('user_login'=>'openid', 'user_password'=>'openid'));
  178. }
  179. }
  180. function openid_wp_login_return_url($urls) {
  181. $url = site_url('/wp-login.php', 'login_post');
  182. $urls[] = $url;
  183. return $urls;
  184. }
  185. ?>