PageRenderTime 29ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/ithemes-security-pro/pro/privilege/class-itsec-privilege.php

https://bitbucket.org/ilayabharathishenll/smartphonecare
PHP | 306 lines | 158 code | 88 blank | 60 comment | 31 complexity | a0baccfedf32fefb5d2d28b6d3c42b5c MD5 | raw file
Possible License(s): Apache-2.0, MIT, BSD-3-Clause
  1. <?php
  2. class ITSEC_Privilege {
  3. private
  4. $settings,
  5. $module_path;
  6. function run() {
  7. $this->settings = get_site_option( 'itsec_privilege' );
  8. $this->module_path = ITSEC_Lib::get_module_path( __FILE__ );
  9. add_action( 'admin_init', array( $this, 'admin_init' ) );
  10. add_action( 'edit_user_profile', array( $this, 'edit_user_profile' ) );
  11. add_action( 'edit_user_profile_update', array( $this, 'edit_user_profile_update' ) );
  12. add_action( 'init', array( $this, 'init' ), 1 );
  13. add_action( 'switch_blog', array( $this, 'init' ) );
  14. }
  15. /**
  16. * Process resetting form
  17. *
  18. * @since 1.11
  19. *
  20. * @return void
  21. */
  22. public function admin_init() {
  23. //if they've clicked a button hide the notice
  24. if ( isset( $_GET['itsec-clear-privilege'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'itsec_clear_privilege' ) ) {
  25. delete_user_meta( absint( $_GET['itsec-clear-privilege'] ), 'itsec_privilege_role' );
  26. delete_user_meta( absint( $_GET['itsec-clear-privilege'] ), 'itsec_privilege_expires' );
  27. delete_site_option( 'itsec_api_nag' );
  28. wp_redirect( admin_url( 'user-edit.php' ) . '?user_id=' . absint( $_GET['itsec-clear-privilege'] ), '302' );
  29. exit();
  30. }
  31. }
  32. /**
  33. * Converts saved role integer to appropriate string
  34. *
  35. * @since 1.11
  36. *
  37. * @param int $role the current role
  38. *
  39. * @return bool|string the current role string of false if invalid input
  40. */
  41. private function convert_current_role( $role ) {
  42. switch ( $role ) {
  43. case 1:
  44. return 'editor';
  45. break;
  46. case 2:
  47. return 'administrator';
  48. break;
  49. case 3:
  50. return 'super-admin';
  51. break;
  52. default:
  53. return false;
  54. }
  55. }
  56. /**
  57. * Display user options field to allow override
  58. *
  59. * @since 1.11
  60. *
  61. * @param mixed $user user
  62. *
  63. * @return void
  64. */
  65. public function edit_user_profile( $user ) {
  66. global $itsec_globals;
  67. if ( $user->ID != get_current_user_id() && ITSEC_Core::current_user_can_manage() ) {
  68. $temp_role = intval( get_user_meta( $user->ID, 'itsec_privilege_role', true ) );
  69. $temp_role_expires = trim( get_user_meta( $user->ID, 'itsec_privilege_expires', true ) );
  70. $current_role = $this->get_current_role( $user );
  71. echo '<h3>' . __( 'Temporary Privilege Escalation', 'it-l10n-ithemes-security-pro' ) . '</h3>';
  72. echo '<table class="form-table">';
  73. echo '<tbody>';
  74. echo '<tr>';
  75. echo '<th scope="row">' . __( 'Set Temporary Role', 'it-l10n-ithemes-security-pro' ) . '</th>';
  76. echo '<td>';
  77. if ( $temp_role != 0 && $temp_role_expires > $itsec_globals['current_time'] ) {
  78. switch ( $temp_role ) {
  79. case 3:
  80. $temp_role_text = __( 'Network Administrator', 'it-l10n-ithemes-security-pro' );
  81. break;
  82. case 2:
  83. $temp_role_text = __( 'Administrator', 'it-l10n-ithemes-security-pro' );
  84. break;
  85. case 1:
  86. $temp_role_text = __( 'Editor', 'it-l10n-ithemes-security-pro' );
  87. break;
  88. }
  89. printf(
  90. '%s <strong>%s</strong>. %s <strong>%s</strong>. <a class="itsec-clear-privilege" href="%s?itsec-clear-privilege=%s&_wpnonce=%s">%s</a>',
  91. __( 'The user has already been temporarily upgraded to the role of ', 'it-l10n-ithemes-security-pro' ),
  92. $temp_role_text,
  93. __( 'This upgrade expires at', 'it-l10n-ithemes-security-pro' ),
  94. date( 'l F jS, Y \a\t g:i a', $temp_role_expires ),
  95. admin_url( 'user-edit.php' ),
  96. $user->ID,
  97. wp_create_nonce( 'itsec_clear_privilege' ),
  98. __( 'Click here to clear', 'it-l10n-ithemes-security-pro' )
  99. );
  100. } elseif ( ( is_multisite() && $current_role < 3 ) || $current_role < 2 ) {
  101. echo '<table>';
  102. echo '<tr>';
  103. echo '<td>';
  104. echo '<select name="itsec_privilege_profile[role]" id="itsec_privelege_role">';
  105. echo '<option value="0" ' . selected( $temp_role, 0, false ) . '>' . __( 'Select', 'it-l10n-ithemes-security-pro' ) . '</option>';
  106. echo ( is_multisite() && $current_role < 3 ) ? '<option value="3" ' . selected( $temp_role, 3, false ) . '>' . __( 'Network Administrator', 'it-l10n-ithemes-security-pro' ) . '</option>' : '';
  107. echo $current_role < 2 ? '<option value="2" ' . selected( $temp_role, 2, false ) . '>' . __( 'Administrator', 'it-l10n-ithemes-security-pro' ) . '</option>' : '';
  108. echo $current_role < 1 ? '<option value="1" ' . selected( $temp_role, 1, false ) . '>' . __( 'Editor', 'it-l10n-ithemes-security-pro' ) . '</option>' : '';
  109. echo '</select>';
  110. echo '</td>';
  111. echo '<td>';
  112. echo '<input class="small-text" name="itsec_privilege_profile[expires]" id="itsec_privilege_expires" value="1" type="text">';
  113. echo '<label for="itsec_privilege_expires"> ' . __( 'Day(s)', 'it-l10n-ithemes-security-pro' ) . '</label>';
  114. echo '</td>';
  115. echo '</tr>';
  116. echo '</table>';
  117. echo '<p class="description"> ' . __( 'Set the role which you would like to assign to the user temporarily and for how long you would like it to last.', 'it-l10n-ithemes-security-pro' ) . '</p>';
  118. } else {
  119. echo __( 'This user has already been permanently upgraded to the maximum level. No further action can be taken.', 'it-l10n-ithemes-security-pro' );
  120. }
  121. echo '</td>';
  122. echo '</tr>';
  123. echo '</tbody>';
  124. echo '</table>';
  125. }
  126. }
  127. /**
  128. * Sanitize and update user option for override
  129. *
  130. * @since 1.11
  131. *
  132. * @param int $user_id user id
  133. *
  134. * @return void
  135. */
  136. public function edit_user_profile_update( $user_id ) {
  137. global $itsec_globals;
  138. if ( isset( $_POST['itsec_privilege_profile'] ) ) {
  139. $role = isset( $_POST['itsec_privilege_profile']['role'] ) && $_POST['itsec_privilege_profile']['role'] != 0 ? intval( $_POST['itsec_privilege_profile']['role'] ) : false;
  140. $exp = isset( $_POST['itsec_privilege_profile']['expires'] ) && intval( $_POST['itsec_privilege_profile']['expires'] ) > 0 ? $itsec_globals['current_time'] + ( intval( $_POST['itsec_privilege_profile']['expires'] ) * 60 * 60 * 24 ) : false;
  141. if ( $role !== false && $exp !== false ) {
  142. update_user_meta( $user_id, 'itsec_privilege_role', $role );
  143. update_user_meta( $user_id, 'itsec_privilege_expires', $exp );
  144. } elseif ( $exp === false ) {
  145. add_action( 'user_profile_update_errors', array( $this, 'user_profile_update_errors' ), 10, 3 );
  146. }
  147. }
  148. }
  149. /**
  150. * Returns the role of the current user
  151. *
  152. * @since 1.11
  153. *
  154. * @param wp_user $user WP_User object
  155. *
  156. * @return int current role
  157. */
  158. private function get_current_role( $user ) {
  159. if ( is_multisite() && $user->has_cap( 'manage_network_options' ) ) {
  160. return 3;
  161. } elseif ( $user->has_cap( 'manage_options' ) ) {
  162. return 2;
  163. } elseif ( $user->has_cap( 'moderate_comments' ) ) {
  164. return 1;
  165. }
  166. return 0;
  167. }
  168. /**
  169. * Process the user role upgrade
  170. *
  171. * @since 1.11
  172. *
  173. * @return void
  174. */
  175. public function init() {
  176. global $itsec_globals, $wp_roles, $super_admins;
  177. if ( ! is_callable( 'wp_get_current_user' ) ) {
  178. return;
  179. }
  180. $current_user = wp_get_current_user();
  181. if ( ! is_object( $current_user ) || ! isset( $current_user->ID ) ) {
  182. return;
  183. }
  184. $temp_role = intval( get_user_meta( $current_user->ID, 'itsec_privilege_role', true ) );
  185. $temp_role_expires = intval( get_user_meta( $current_user->ID, 'itsec_privilege_expires', true ) );
  186. if ( $temp_role > 0 && $temp_role_expires > 0 ) {
  187. if ( $itsec_globals['current_time'] > $temp_role_expires ) {
  188. delete_user_meta( $current_user->ID, 'itsec_privilege_role' );
  189. delete_user_meta( $current_user->ID, 'itsec_privilege_expires' );
  190. } else {
  191. $temp_role_converted = $this->convert_current_role( $temp_role );
  192. $current_role_converted = $this->convert_current_role( $this->get_current_role( $current_user ) );
  193. if ( $temp_role === 3 ) {
  194. $temp_role_converted = 'administrator';
  195. }
  196. if ( ! is_array( $super_admins ) ) {
  197. $super_admins = array( $current_user->user_login );
  198. }
  199. $current_user->allcaps = $wp_roles->roles[ $temp_role_converted ]['capabilities']; //Set new capabilities
  200. $current_user->roles[0] = strtolower( $temp_role_converted ); //Set new role
  201. unset( $current_user->caps[ $current_role_converted ] ); //Delete old capabilities
  202. $current_user->caps[ $temp_role_converted ] = true; //Turn on current capabilities
  203. }
  204. }
  205. }
  206. /**
  207. * Requires a unique nicename on profile update or activate.
  208. *
  209. * @since 1.11
  210. *
  211. * @param array $errors Array of profile entry errors.
  212. *
  213. * @return void
  214. */
  215. public function user_profile_update_errors( $errors ) {
  216. $errors->add( 'user_error', __( 'You must select a valid number of days (greater than 0) for temporary role expiration.', 'it-l10n-ithemes-security-pro' ) );
  217. }
  218. }