PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/theme-my-login/modules/custom-passwords/custom-passwords.php

https://gitlab.com/Gashler/sg
PHP | 310 lines | 122 code | 31 blank | 157 comment | 23 complexity | 8615d33ce84b6f6ee7652cae0b9e46b1 MD5 | raw file
  1. <?php
  2. /**
  3. * Plugin Name: Custom Passwords
  4. * Description: Enabling this module will initialize and enable custom passwords. There are no other settings for this module.
  5. *
  6. * Holds the Theme My Login Custom Passwords class
  7. *
  8. * @package Theme_My_Login
  9. * @subpackage Theme_My_Login_Custom_Passwords
  10. * @since 6.0
  11. */
  12. if ( ! class_exists( 'Theme_My_Login_Custom_Passwords' ) ) :
  13. /**
  14. * Theme My Login Custom Passwords module class
  15. *
  16. * @since 6.0
  17. */
  18. class Theme_My_Login_Custom_Passwords extends Theme_My_Login_Abstract {
  19. /**
  20. * Returns singleton instance
  21. *
  22. * @since 6.3
  23. * @access public
  24. * @return object
  25. */
  26. public static function get_object( $class = null ) {
  27. return parent::get_object( __CLASS__ );
  28. }
  29. /**
  30. * Loads the module
  31. *
  32. * @since 6.0
  33. * @access protected
  34. */
  35. protected function load() {
  36. add_action( 'register_form', array( &$this, 'password_fields' ) );
  37. add_filter( 'registration_errors', array( &$this, 'password_errors' ) );
  38. add_filter( 'random_password', array( &$this, 'set_password' ) );
  39. add_action( 'signup_extra_fields', array( &$this, 'ms_password_fields' ) );
  40. add_action( 'signup_hidden_fields', array( &$this, 'ms_hidden_password_field' ) );
  41. add_filter( 'wpmu_validate_user_signup', array( &$this, 'ms_password_errors' ) );
  42. add_filter( 'add_signup_meta', array( &$this, 'ms_save_password' ) );
  43. add_action( 'tml_new_user_registered', array( &$this, 'remove_default_password_nag' ) );
  44. add_action( 'approve_user', array( &$this, 'remove_default_password_nag' ) );
  45. add_filter( 'tml_register_passmail_template_message', array( &$this, 'register_passmail_template_message' ) );
  46. add_action( 'tml_request', array( &$this, 'action_messages' ) );
  47. add_filter( 'registration_redirect', array( &$this, 'registration_redirect' ) );
  48. }
  49. /**
  50. * Outputs password fields to registration form
  51. *
  52. * Callback for "register_form" hook in file "register-form.php", included by Theme_My_Login_Template::display()
  53. *
  54. * @see Theme_My_Login::display()
  55. * @since 6.0
  56. * @access public
  57. */
  58. public function password_fields() {
  59. $template =& Theme_My_Login::get_object()->get_active_instance();
  60. ?>
  61. <p><label for="pass1<?php $template->the_instance(); ?>"><?php _e( 'Password' ); ?></label>
  62. <input autocomplete="off" name="pass1" id="pass1<?php $template->the_instance(); ?>" class="input" size="20" value="" type="password" /></p>
  63. <p><label for="pass2<?php $template->the_instance(); ?>"><?php _e( 'Confirm Password', 'theme-my-login' ); ?></label>
  64. <input autocomplete="off" name="pass2" id="pass2<?php $template->the_instance(); ?>" class="input" size="20" value="" type="password" /></p>
  65. <?php
  66. }
  67. /**
  68. * Outputs password fields to multisite signup user form
  69. *
  70. * Callback for "signup_extra_fields" hook in file "ms-signup-user-form.php", included by Theme_My_Login_Template::display()
  71. *
  72. * @see Theme_My_Login::display()
  73. * @since 6.1
  74. * @access public
  75. */
  76. public function ms_password_fields() {
  77. $theme_my_login = Theme_My_Login::get_object();
  78. $template =& $theme_my_login->get_active_instance();
  79. $errors = array();
  80. foreach ( $theme_my_login->errors->get_error_codes() as $code ) {
  81. if ( in_array( $code, array( 'empty_password', 'password_mismatch', 'password_length' ) ) )
  82. $errors[] = $theme_my_login->errors->get_error_message( $code );
  83. }
  84. ?>
  85. <label for="pass1<?php $template->the_instance(); ?>"><?php _e( 'Password:', 'theme-my-login' ); ?></label>
  86. <?php if ( ! empty( $errors ) ) { ?>
  87. <p class="error"><?php echo implode( '<br />', $errors ); ?></p>
  88. <?php } ?>
  89. <input autocomplete="off" name="pass1" id="pass1<?php $template->the_instance(); ?>" class="input" size="20" value="" type="password" /><br />
  90. <span class="hint"><?php echo apply_filters( 'tml_password_hint', __( '(Must be at least 6 characters.)', 'theme-my-login' ) ); ?></span>
  91. <label for="pass2<?php $template->the_instance(); ?>"><?php _e( 'Confirm Password:', 'theme-my-login' ); ?></label>
  92. <input autocomplete="off" name="pass2" id="pass2<?php $template->the_instance(); ?>" class="input" size="20" value="" type="password" /><br />
  93. <span class="hint"><?php echo apply_filters( 'tml_password_confirm_hint', __( 'Confirm that you\'ve typed your password correctly.', 'theme-my-login' ) ); ?></span>
  94. <?php
  95. }
  96. /**
  97. * Outputs password field to multisite signup blog form
  98. *
  99. * Callback for "signup_hidden_fields" hook in file "ms-signup-blog-form.php", included by Theme_My_Login_Template::display()
  100. *
  101. * @see Theme_My_Login::display()
  102. * @since 6.1
  103. * @access public
  104. */
  105. public function ms_hidden_password_field() {
  106. if ( isset( $_POST['user_pass'] ) )
  107. echo '<input type="hidden" name="user_pass" value="' . $_POST['user_pass'] . '" />' . "\n";
  108. }
  109. /**
  110. * Handles password errors for registration form
  111. *
  112. * Callback for "registration_errors" hook in Theme_My_Login::register_new_user()
  113. *
  114. * @see Theme_My_Login::register_new_user()
  115. * @since 6.0
  116. * @access public
  117. *
  118. * @param WP_Error $errors WP_Error object
  119. * @return WP_Error WP_Error object
  120. */
  121. public function password_errors( $errors = '' ) {
  122. // Make sure $errors is a WP_Error object
  123. if ( empty( $errors ) )
  124. $errors = new WP_Error();
  125. // Make sure passwords aren't empty
  126. if ( empty( $_POST['pass1'] ) || empty( $_POST['pass2'] ) ) {
  127. $errors->add( 'empty_password', __( '<strong>ERROR</strong>: Please enter your password twice.' ) );
  128. // Make sure there's no "\" in the password
  129. } elseif ( false !== strpos( stripslashes( $_POST['pass1'] ), "\\" ) ) {
  130. $errors->add( 'password_backslash', __( '<strong>ERROR</strong>: Passwords may not contain the character "\\".' ) );
  131. // Make sure passwords match
  132. } elseif ( $_POST['pass1'] != $_POST['pass2'] ) {
  133. $errors->add( 'password_mismatch', __( '<strong>ERROR</strong>: Please enter the same password in the two password fields.' ) );
  134. // Make sure password is long enough
  135. } elseif ( strlen( $_POST['pass1'] ) < 6 ) {
  136. $errors->add( 'password_length', __( '<strong>ERROR</strong>: Your password must be at least 6 characters in length.', 'theme-my-login' ) );
  137. // All is good, assign password to a friendlier key
  138. } else {
  139. $_POST['user_pass'] = $_POST['pass1'];
  140. }
  141. return $errors;
  142. }
  143. /**
  144. * Handles password errors for multisite signup form
  145. *
  146. * Callback for "registration_errors" hook in Theme_My_Login::register_new_user()
  147. *
  148. * @see Theme_My_Login::register_new_user()
  149. * @since 6.1
  150. * @access public
  151. *
  152. * @param WP_Error $errors WP_Error object
  153. * @return WP_Error WP_Error object
  154. */
  155. public function ms_password_errors( $result ) {
  156. if ( isset( $_POST['stage'] ) && 'validate-user-signup' == $_POST['stage'] ) {
  157. $errors = $this->password_errors();
  158. foreach ( $errors->get_error_codes() as $code ) {
  159. foreach ( $errors->get_error_messages( $code ) as $error ) {
  160. $result['errors']->add( $code, preg_replace( '/<strong>([^<]+)<\/strong>: /', '', $error ) );
  161. }
  162. }
  163. }
  164. return $result;
  165. }
  166. /**
  167. * Adds password to signup meta array
  168. *
  169. * Callback for "add_signup_meta" hook
  170. *
  171. * @since 6.1
  172. * @access public
  173. *
  174. * @param array $meta Signup meta
  175. * @return array $meta Signup meta
  176. */
  177. public function ms_save_password( $meta ) {
  178. if ( isset( $_POST['user_pass'] ) )
  179. $meta['user_pass'] = $_POST['user_pass'];
  180. return $meta;
  181. }
  182. /**
  183. * Sets the user password
  184. *
  185. * Callback for "random_password" hook in wp_generate_password()
  186. *
  187. * @see wp_generate_password()
  188. * @since 6.0
  189. * @access public
  190. *
  191. * @param string $password Auto-generated password passed in from filter
  192. * @return string Password chosen by user
  193. */
  194. public function set_password( $password ) {
  195. global $wpdb;
  196. // Remove filter as not to filter User Moderation activation key
  197. remove_filter( 'random_password', array( &$this, 'set_password' ) );
  198. if ( is_multisite() && isset( $_REQUEST['key'] ) ) {
  199. if ( $meta = $wpdb->get_var( $wpdb->prepare( "SELECT meta FROM $wpdb->signups WHERE activation_key = %s", $_REQUEST['key'] ) ) ) {
  200. $meta = unserialize( $meta );
  201. if ( isset( $meta['user_pass'] ) ) {
  202. $password = $meta['user_pass'];
  203. unset( $meta['user_pass'] );
  204. $wpdb->update( $wpdb->signups, array( 'meta' => serialize( $meta ) ), array( 'activation_key' => $_REQUEST['key'] ) );
  205. }
  206. }
  207. } else {
  208. // Make sure password isn't empty
  209. if ( ! empty( $_POST['user_pass'] ) )
  210. $password = $_POST['user_pass'];
  211. }
  212. return $password;
  213. }
  214. /**
  215. * Removes the default password nag
  216. *
  217. * Callback for "tml_new_user_registered" hook in Theme_My_Login::register_new_user()
  218. *
  219. * @see Theme_My_Login::register_new_user()
  220. * @since 6.0
  221. * @access public
  222. *
  223. * @param int $user_id The user's ID
  224. */
  225. public function remove_default_password_nag( $user_id ) {
  226. update_user_meta( $user_id, 'default_password_nag', false );
  227. }
  228. /**
  229. * Changes the register template message
  230. *
  231. * Callback for "tml_register_passmail_template_message" hook
  232. *
  233. * @since 6.0
  234. * @access public
  235. *
  236. * @return string The new register message
  237. */
  238. public function register_passmail_template_message() {
  239. // Removes "A password will be e-mailed to you." from register form
  240. return;
  241. }
  242. /**
  243. * Handles display of various action/status messages
  244. *
  245. * Callback for "tml_request" hook in Theme_My_Login::the_request()
  246. *
  247. * @since 6.0
  248. * @access public
  249. *
  250. * @param object $theme_my_login Reference to global $theme_my_login object
  251. */
  252. public function action_messages( &$theme_my_login ) {
  253. // Change "Registration complete. Please check your e-mail." to reflect the fact that they already set a password
  254. if ( isset( $_GET['registration'] ) && 'complete' == $_GET['registration'] )
  255. $theme_my_login->errors->add( 'registration_complete', __( 'Registration complete. You may now log in.', 'theme-my-login' ), 'message' );
  256. }
  257. /**
  258. * Changes where the user is redirected upon successful registration
  259. *
  260. * Callback for "registration_redirect" hook in Theme_My_Login_Template::get_redirect_url()
  261. *
  262. * @see Theme_My_Login_Template::get_redirect_url()
  263. * @since 6.0
  264. * @access public
  265. *
  266. * @return string $redirect_to Default redirect
  267. * @return string URL to redirect to
  268. */
  269. public function registration_redirect( $redirect_to ) {
  270. // Redirect to login page with "registration=complete" added to the query
  271. $redirect_to = site_url( 'wp-login.php?registration=complete' );
  272. // Add instance to the query if specified
  273. if ( ! empty( $_REQUEST['instance'] ) )
  274. $redirect_to = add_query_arg( 'instance', $_REQUEST['instance'], $redirect_to );
  275. return $redirect_to;
  276. }
  277. }
  278. Theme_My_Login_Custom_Passwords::get_object();
  279. endif;