Advertisement
Guest User

Untitled

a guest
Jun 24th, 2015
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.69 KB | None | 0 0
  1. <?php
  2.  
  3. // +----------------------------------------------------------------------+
  4. // | Copyright Incsub (http://incsub.com/) |
  5. // +----------------------------------------------------------------------+
  6. // | This program is free software; you can redistribute it and/or modify |
  7. // | it under the terms of the GNU General Public License, version 2, as |
  8. // | published by the Free Software Foundation. |
  9. // | |
  10. // | This program is distributed in the hope that it will be useful, |
  11. // | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  12. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  13. // | GNU General Public License for more details. |
  14. // | |
  15. // | You should have received a copy of the GNU General Public License |
  16. // | along with this program; if not, write to the Free Software |
  17. // | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
  18. // | MA 02110-1301 USA |
  19. // +----------------------------------------------------------------------+
  20.  
  21. /**
  22. * The module responsible for cross domain single sign on.
  23. *
  24. * @category Domainmap
  25. * @package Module
  26. *
  27. * @since 4.0.2
  28. */
  29.  
  30. class Domainmap_Module_Cdsso extends Domainmap_Module {
  31.  
  32. const NAME = __CLASS__;
  33.  
  34. const ACTION_KEY = '__domainmap_action';
  35.  
  36. const ACTION_SETUP_CDSSO = 'domainmap-setup-cdsso';
  37. const ACTION_CHECK_LOGIN_STATUS = 'domainmap-check-login-status';
  38. const ACTION_AUTHORIZE_USER = 'domainmap-authorize-user';
  39. const ACTION_AUTHORIZE_USER_ASYNC = 'domainmap-authorize-user-async';
  40. const ACTION_PROPAGATE_USER = 'domainmap-propagate-user';
  41. const ACTION_LOGOUT_USER = 'domainmap-logout-user';
  42. const SSO_ENDPOINT = 'dm-sso-endpoint';
  43.  
  44. /**
  45. * Determines whether we need to propagate user to the original blog or not.
  46. *
  47. * @since 4.1.2
  48. *
  49. * @access private
  50. * @var boolean
  51. */
  52. private $_do_propagation = false;
  53.  
  54. /**
  55. * Determines whether we do logout process or not.
  56. *
  57. * @since 4.1.2
  58. *
  59. * @access private
  60. * @var boolean
  61. */
  62. private $_do_logout = false;
  63.  
  64. /**
  65. * Whether to load the sso scripts asynchronously
  66. *
  67. * @since 4.2.1
  68. *
  69. * @access private
  70. * @var bool
  71. */
  72. private $_async = false;
  73.  
  74. /**
  75. * Constructor.
  76. *
  77. * @since 4.0.2
  78. *
  79. * @access public
  80. * @param Domainmap_Plugin $plugin The instance of the plugin class.
  81. */
  82. public function __construct( Domainmap_Plugin $plugin ) {
  83. parent::__construct( $plugin );
  84.  
  85. $this->_async = $plugin->get_option("map_crossautologin_async");
  86.  
  87. $this->_add_filter( 'wp_redirect', 'add_logout_marker' );
  88. $this->_add_filter( 'login_redirect', 'set_interim_login', 10, 3 );
  89. $this->_add_filter( 'login_message', 'get_login_message' );
  90. $this->_add_filter( 'login_url', 'update_login_url', 10, 2 );
  91. $this->_add_action( 'login_init', 'reauthenticate_user', 10 );
  92. $this->_add_action('wp_head', 'add_auth_script', 0 );
  93.  
  94. $this->_add_action( 'login_form_login', 'set_auth_script_for_login' );
  95. $this->_add_action( 'wp_head', 'add_logout_propagation_script', 0 );
  96. $this->_add_action( 'login_head', 'add_logout_propagation_script', 0 );
  97. $this->_add_action( 'login_footer', 'add_propagation_script' );
  98. $this->_add_action( 'wp_logout', 'set_logout_var' );
  99.  
  100. if( !$this->_async ){
  101. $this->_add_action( 'plugins_loaded', 'authorize_user' );
  102. }
  103.  
  104. add_filter('init', array( $this, "add_query_var_for_endpoint" ));
  105. add_action('template_redirect', array( $this, 'dispatch_ajax_request' ));
  106.  
  107. $this->_add_ajax_action( self::ACTION_LOGOUT_USER, 'logout_user', true, true );
  108. $this->_add_ajax_action( self::ACTION_PROPAGATE_USER, 'propagate_user', true, true );
  109. }
  110.  
  111. /**
  112. * Adds hook for login_head action if user tries to login.
  113. *
  114. * @since 4.1.2
  115. * @action login_form_login
  116. *
  117. * @access public
  118. */
  119. public function set_auth_script_for_login() {
  120. $this->_add_action( 'login_head', 'add_auth_script', 0 );
  121. }
  122.  
  123. /**
  124. * Equalizes redirect_to domain name with login URL domain.
  125. *
  126. * @since 4.1.2.1
  127. * @filter login_url 10 2
  128. *
  129. * @param string $login_url The login URL.
  130. * @param string $redirect_to The redirect URL.
  131. * @return string Updated login URL.
  132. */
  133. public function update_login_url( $login_url, $redirect_to ) {
  134. if( empty( $redirect_to ) )
  135. return $login_url;
  136.  
  137. $login_domain = parse_url( $login_url, PHP_URL_HOST );
  138. $redirect_domain = parse_url( $redirect_to, PHP_URL_HOST );
  139. if ( $login_domain != $redirect_domain ) {
  140. $redirect_to = str_replace( "://{$redirect_domain}", "://{$login_domain}", $redirect_to );
  141. $login_url = esc_url_raw( add_query_arg( 'redirect_to', urlencode( $redirect_to ), $login_url ) );
  142. }
  143.  
  144. return $login_url;
  145. }
  146.  
  147. /**
  148. * Sets logout var to determine logout process.
  149. *
  150. * @since 4.1.2
  151. * @access wp_logout
  152. *
  153. * @access public
  154. */
  155. public function set_logout_var() {
  156. $this->_do_logout = true;
  157. }
  158.  
  159. /**
  160. * Adds logout marker if need be.
  161. *
  162. * @since 4.1.2
  163. * @filter wp_redirect
  164. *
  165. * @access public
  166. * @param string $redirect_to The initial redirect URL.
  167. * @return string Updated redirect URL.
  168. */
  169. public function add_logout_marker( $redirect_to ) {
  170. if ( $this->_do_logout ) {
  171. $redirect_to = esc_url_raw( add_query_arg( self::ACTION_KEY, self::ACTION_LOGOUT_USER, $redirect_to ) );
  172. }
  173.  
  174. return $redirect_to;
  175. }
  176.  
  177. /**
  178. * Adds logout propagation script if need be.
  179. *
  180. * @since 4.1.2
  181. * @action wp_head 0
  182. * @action login_head 0
  183. *
  184. * @access public
  185. */
  186. public function add_logout_propagation_script() {
  187. if ( is_user_logged_in() || get_current_blog_id() == 1 || filter_input( INPUT_GET, self::ACTION_KEY ) != self::ACTION_LOGOUT_USER ) {
  188. return;
  189. }
  190.  
  191. $url = add_query_arg( 'action', self::ACTION_LOGOUT_USER, $this->get_main_ajax_url() );
  192. $this->_add_script( esc_url_raw( $url ) );
  193. }
  194.  
  195. /**
  196. * Do logout from the main blog.
  197. *
  198. * @since 4.1.2
  199. * @action wp_ajax_domainmap-logout-user
  200. * @action wp_ajax_no_priv_domainmap-logout-user
  201. *
  202. * @access public
  203. */
  204. public function logout_user() {
  205. header( "Content-Type: text/javascript; charset=" . get_bloginfo( 'charset' ) );
  206.  
  207. if ( !is_user_logged_in() || empty( $_SERVER['HTTP_REFERER'] ) ) {
  208. header( "Vary: Accept-Encoding" ); // Handle proxies
  209. header( "Expires: " . gmdate( "D, d M Y H:i:s", time() + MINUTE_IN_SECONDS ) . " GMT" );
  210. exit;
  211. }
  212.  
  213.  
  214. wp_destroy_all_sessions();
  215. wp_clear_auth_cookie();
  216.  
  217. $url = add_query_arg( self::ACTION_KEY, false, $_SERVER['HTTP_REFERER'] );
  218.  
  219. echo 'window.location = "', esc_url_raw( $url ), '";';
  220. exit;
  221. }
  222.  
  223. /**
  224. * Sets interim login mode.
  225. *
  226. * @since 4.1.2
  227. * @filter login_redirect 10 3
  228. *
  229. * @access public
  230. * @global boolean $interim_login Determines whether to show interim login page or not.
  231. * @param string $redirect_to The redirection URL.
  232. * @param string $requested_redirect_to The initial redirection URL.
  233. * @param WP_User|WP_Error $user The user or error object.
  234. * @return string The income redirection URL.
  235. */
  236. public function set_interim_login( $redirect_to, $requested_redirect_to, $user ) {
  237.  
  238. global $interim_login;
  239. if ( is_a( $user, 'WP_User' ) && get_current_blog_id() != 1 ) {
  240. if ( $this->is_mapped_domain() || $this->is_subdomain() ) {
  241. $interim_login = $this->_do_propagation = true;
  242. }
  243. }
  244.  
  245. return $redirect_to;
  246. }
  247.  
  248. /**
  249. * Updates login message for interim login page.
  250. *
  251. * @since 4.1.2
  252. * @filter login_message
  253. *
  254. * @access public
  255. * @param string $message The original message.
  256. * @return string The new extended login message.
  257. */
  258. public function get_login_message( $message ) {
  259. return $this->_do_propagation
  260. ? '<p style="border-color: #6F1B11;" class="message">' . esc_html__( 'You are currently being logged into the Strategic Armory Corps network.', 'domainmap' ) . '</p>'
  261. : $message;
  262. }
  263.  
  264. /**
  265. * Adds propagation scripts at interim login page after successfull login.
  266. *
  267. * @since 4.1.2
  268. * @access login_footer
  269. *
  270. * @access public
  271. * @global string $redirect_to The redirection URL.
  272. * @global WP_User $user Current user.
  273. */
  274. public function add_propagation_script() {
  275. global $redirect_to, $user;
  276.  
  277.  
  278. if ( !$this->_do_propagation ) {
  279. return;
  280. }
  281.  
  282. if ( ( empty( $redirect_to ) || $redirect_to == 'wp-admin/' || $redirect_to == admin_url() ) ) {
  283. // If the user doesn't belong to a blog, send them to user admin. If the user can't edit posts, send them to their profile.
  284. if ( is_multisite() && !get_active_blog_for_user( $user->ID ) && !is_super_admin( $user->ID ) ) {
  285. $redirect_to = user_admin_url();
  286. } elseif ( is_multisite() && !$user->has_cap( 'read' ) ) {
  287. $redirect_to = get_dashboard_url( $user->ID );
  288. } elseif ( !$user->has_cap( 'edit_posts' ) ) {
  289. $redirect_to = admin_url( 'profile.php' );
  290. }
  291. }
  292. ?>
  293. <script <?php echo $this->_async ? "async='true'" : ""; ?> type="text/javascript">
  294. function domainmap_do_redirect() { window.location = "<?php echo $redirect_to ?>"; }
  295. setTimeout(domainmap_do_redirect, 5000);
  296. </script>
  297.  
  298. <?php
  299.  
  300. $url = add_query_arg( array(
  301. 'action' => self::ACTION_PROPAGATE_USER,
  302. 'auth' => wp_generate_auth_cookie( $user->ID, time() + MINUTE_IN_SECONDS ),
  303. ), $this->get_main_ajax_url() );
  304.  
  305. $this->_add_script( $url );
  306. }
  307.  
  308. /**
  309. * Adds authorization script to the current page header. (Subsite)
  310. *
  311. * @since 4.1.2
  312. * @action wp_head 0
  313. * @action login_head 0
  314. *
  315. * @uses _add_auth_script_sync
  316. * @uses _add_auth_script_async
  317. *
  318. * @access public
  319. */
  320. public function add_auth_script() {
  321.  
  322. if ( is_user_logged_in()
  323. || 1 === get_current_blog_id()
  324. || filter_input( INPUT_GET, self::ACTION_KEY ) == self::ACTION_AUTHORIZE_USER
  325. || isset( $_POST["pwd"] )
  326. || filter_input( INPUT_GET, self::ACTION_KEY ) == self::ACTION_LOGOUT_USER
  327. ) return;
  328.  
  329. if($this->_async)
  330. $this->_add_auth_script_async();
  331. else
  332. $this->_add_auth_script_sync();
  333.  
  334. }
  335.  
  336. private function _add_auth_script_sync(){
  337.  
  338. $url = add_query_arg( 'dm_action', self::ACTION_SETUP_CDSSO, $this->_get_sso_endpoint_url() );
  339. $this->_add_script( esc_url_raw( $url ) );
  340. }
  341.  
  342. private function _add_auth_script_async(){
  343.  
  344. $url = add_query_arg( array(
  345. 'dm_action' => self::ACTION_CHECK_LOGIN_STATUS,
  346. 'domain' => $_SERVER['HTTP_HOST'] ,
  347. ), $this->_get_sso_endpoint_url()
  348. );
  349.  
  350. $this->_add_iframe( esc_url_raw( $url ) );
  351. }
  352.  
  353. /**
  354. * Setups CDSSO for logged in user. (sync)
  355. *
  356. * @since 4.1.2
  357. * @action wp_ajax_domainmap-setup-cdsso
  358. * @action wp_ajax_nopriv_domainmap-setup-cdsso
  359. *
  360. * @access public
  361. */
  362. public function setup_cdsso() {
  363. header( "Content-Type: text/javascript; charset=" . get_bloginfo( 'charset' ) );
  364. if ( !is_user_logged_in() || empty( $_SERVER['HTTP_REFERER'] ) ) {
  365. header( "Vary: Accept-Encoding" ); // Handle proxies
  366. header( "Expires: " . gmdate( "D, d M Y H:i:s", time() + ( 2 * MINUTE_IN_SECONDS ) ) . " GMT" );
  367. exit;
  368. }
  369.  
  370. $url = add_query_arg( array(
  371. self::ACTION_KEY => self::ACTION_AUTHORIZE_USER,
  372. 'auth' => wp_generate_auth_cookie( get_current_user_id(), time() + ( 2 * MINUTE_IN_SECONDS ) ),
  373. ), $_SERVER['HTTP_REFERER'] );
  374. ?>
  375. window.location.replace("<?php echo esc_url_raw( $url ) ?>");
  376. <?php
  377. exit;
  378. }
  379.  
  380. /**
  381. * Prints sync javascript script
  382. *
  383. * @since 4.2.1
  384. *
  385. * @access private
  386. * @param $url
  387. */
  388. private function _add_script( $url )
  389. {
  390. ?>
  391. <script type="text/javascript" src="<?php echo $url; ?>"></script>
  392. <?php
  393. }
  394.  
  395.  
  396. /**
  397. * Creates the endpoint to respond to the sso requests
  398. *
  399. * @since 4.3.1
  400. * @param $vars
  401. *
  402. * @return array
  403. */
  404. function add_query_var_for_endpoint($vars) {
  405. add_rewrite_endpoint( self::SSO_ENDPOINT, EP_ALL );
  406. $vars[] = self::SSO_ENDPOINT;
  407. $this->_flush_rewrite_rules();
  408. return $vars;
  409. }
  410.  
  411. /**
  412. * Flushes rewrite rules if needed
  413. *
  414. * @since 4.3.1
  415. */
  416. function _flush_rewrite_rules(){
  417. $key = domain_map::FLUSHED_REWRITE_RULES . get_current_blog_id();
  418. if( !get_site_option( $key ) ){
  419. flush_rewrite_rules();
  420. update_site_option( $key , true);
  421. }
  422. }
  423.  
  424. /**
  425. * Returns relevant endpoint url
  426. *
  427. * @since 4.3.1
  428. * @param bool $subsite
  429. * @param null $domain
  430. *
  431. * @return string
  432. */
  433. private function _get_sso_endpoint_url( $subsite = false, $domain = null){
  434. global $wp_rewrite, $current_blog, $current_site;;
  435.  
  436. $admin_scheme = is_ssl() ? "https://" : "http://";
  437.  
  438. if( $subsite ){
  439. $domain = is_null( $domain ) ? $current_blog->domain : $domain;
  440. $url = $admin_scheme . $domain . "/";
  441. }else{
  442. $url = trailingslashit( network_home_url("/", $admin_scheme) );
  443. }
  444.  
  445. return $wp_rewrite->using_permalinks() ? $url . self::SSO_ENDPOINT . "/" . time() . "/" : $url . "?" . self::SSO_ENDPOINT . "=" . time() ;
  446. }
  447.  
  448.  
  449. /**
  450. * Dispatches ajax request to the relevant methods
  451. *
  452. * @since 4.3.1
  453. */
  454. function dispatch_ajax_request(){
  455.  
  456. global $wp_query;
  457.  
  458. if( !isset( $wp_query->query_vars[ self::SSO_ENDPOINT ] ) ) return;
  459.  
  460. define('DOING_AJAX', true);
  461. header('Content-Type: text/html');
  462. send_nosniff_header();
  463. header('Cache-Control: no-cache');
  464. header('Pragma: no-cache');
  465.  
  466. if( isset( $_REQUEST["dm_action"] ) ){
  467. $action = $_REQUEST["dm_action"];
  468. }elseif( isset( $_REQUEST["q"] ) ){
  469. $q = $_REQUEST["q"];
  470. $parsed = parse_url($q );
  471.  
  472. if( isset( $parsed['query'] ) && strpos($parsed['query'], "dm_action") !== false ){ // if query is set
  473. $action = str_replace("dm_action=", "", $parsed['query']);
  474. }else{
  475. $prefix = "?dm_action=";
  476. $pos = strpos( $q, $prefix );
  477. $action = substr($q, $pos + strlen($prefix));
  478. }
  479.  
  480. }
  481.  
  482. if( !empty( $action ) ){
  483. $method = str_replace(array("domainmap-", "-"), array("", "_"), $action);
  484.  
  485. if( method_exists("Domainmap_Module_Cdsso", $method) )
  486. call_user_func(array($this, $method));
  487. else
  488. wp_send_json_error( "Method " . $method . " not found" );
  489. }
  490. exit;
  491. }
  492.  
  493. /**
  494. * Checks login status of the user on the main site
  495. *
  496. * @uses authorize_user_async
  497. * @since 4.3.1
  498. */
  499. function check_login_status(){
  500.  
  501. header( "Content-Type: text/javascript; charset=" . get_bloginfo( 'charset' ) );
  502. if ( !is_user_logged_in() ) {
  503. header( "Vary: Accept-Encoding" ); // Handle proxies
  504. header( "Expires: " . gmdate( "D, d M Y H:i:s", time() + MINUTE_IN_SECONDS ) . " GMT" );
  505. exit;
  506. }
  507.  
  508. $domain_name = filter_input( INPUT_GET, 'domain' );
  509. $admin_mapping = $this->_plugin->get_option("map_force_admin_ssl");
  510. ?>
  511. // Starting Domain Mapping SSO
  512. <?php
  513. $args = array(
  514. "dm_action" => self::ACTION_AUTHORIZE_USER_ASYNC,
  515. 'auth' => wp_generate_auth_cookie( get_current_user_id(), time() + MINUTE_IN_SECONDS ),
  516. 'refresh' => 1
  517. );
  518. if( $admin_mapping ){
  519. $args["refresh"] = 0;
  520. }
  521. $url = add_query_arg( $args, $this->_get_sso_endpoint_url( true, $domain_name ) );
  522.  
  523. $url = set_url_scheme( $url, "http" );
  524. $this->_add_inner_iframe( esc_url_raw( $url ) );
  525.  
  526. if( $admin_mapping ){ // set user cookie for https as well and refresh
  527. $args["refresh"] = 1;
  528. $url = add_query_arg( $args, $this->_get_sso_endpoint_url( true, $domain_name ) );
  529. $this->_add_inner_iframe( esc_url_raw( $url ) );
  530. }
  531. }
  532.  
  533. /**
  534. * Adds iframe
  535. *
  536. *
  537. * @since 4.4.0
  538. * @param $url
  539. */
  540. private function _add_iframe( $url ){
  541. ?>
  542. <script type="text/javascript">
  543. (function(window) {
  544. var document = window.document;
  545. var url = '<?php echo $url; ?>';
  546. var iframe = document.createElement('iframe');
  547. (iframe.frameElement || iframe).style.cssText =
  548. "width: 0; height: 0; border: 0";
  549. iframe.src = "javascript:false";
  550. var where = document.getElementsByTagName('script')[0];
  551. where.parentNode.insertBefore(iframe, where);
  552. var doc = iframe.contentWindow.document;
  553. doc.open().write('<body onload="'+
  554. 'var js = document.createElement(\'script\');'+
  555. 'js.src = \''+ url +'\';'+
  556. 'document.body.appendChild(js);">');
  557. doc.close();
  558.  
  559. }(parent.window));
  560. </script>
  561. <?php
  562. }
  563. /**
  564. * Adds iframe inside the parent iframe
  565. *
  566. *
  567. * @since 4.4.0
  568. * @param $url
  569. */
  570. private function _add_inner_iframe( $url ){
  571. ?>
  572. (function(window) {
  573. var document = window.top.document;
  574. var url = '<?php echo $url; ?>';
  575. var iframe = document.createElement('iframe');
  576. (iframe.frameElement || iframe).style.cssText =
  577. "width: 0; height: 0; border: 0";
  578. iframe.src = "javascript:false";
  579. var where = document.getElementsByTagName('script')[0];
  580. where.parentNode.insertBefore(iframe, where);
  581. var doc = iframe.contentWindow.document;
  582. doc.open().write('<body onLoad="'+
  583. 'var js = document.createElement(\'script\');'+
  584. 'js.src = \''+ url +'\';'+
  585. 'document.body.appendChild(js);">');
  586. doc.close();
  587.  
  588. }(parent.top.window));
  589. <?php
  590. }
  591. /**
  592. * Sets auth cookie for the user on the subsite
  593. * Used by plugins_loaded action hook
  594. *
  595. * @since 4.2.1
  596. */
  597. function authorize_user() {
  598.  
  599. if ( filter_input( INPUT_GET, self::ACTION_KEY ) == self::ACTION_AUTHORIZE_USER ) {
  600. $user_id = wp_validate_auth_cookie( filter_input( INPUT_GET, 'auth' ), 'auth' );
  601. if ( $user_id ) {
  602. wp_set_auth_cookie( $user_id );
  603.  
  604. $redirect_to = in_array( $GLOBALS['pagenow'], array( 'wp-login.php' ) ) && filter_input( INPUT_GET, 'redirect_to', FILTER_VALIDATE_URL )
  605. ? $_GET['redirect_to']
  606. : add_query_arg( array( self::ACTION_KEY => false, 'auth' => false ) );
  607.  
  608. wp_redirect( esc_url_raw( $redirect_to ) );
  609. exit;
  610. } else {
  611. wp_die( __( "Incorrect or out of date login key", 'domainmap' ) );
  612. }
  613. }
  614. }
  615.  
  616. /**
  617. * Sets auth cookie for the user on the subsite ( async )
  618. *
  619. * @since 4.3.1
  620. */
  621. private function authorize_user_async(){
  622. header( "Content-Type: text/javascript; charset=" . get_bloginfo( 'charset' ) );
  623.  
  624. $user_id = wp_validate_auth_cookie( filter_input( INPUT_GET, 'auth' ), 'auth' );
  625. $refresh = filter_input( INPUT_GET, 'refresh' );
  626.  
  627. if ( $user_id ) {
  628. wp_set_auth_cookie( $user_id );
  629. if( $refresh ){
  630. ?>
  631. window.top.location.reload();
  632. <?php
  633. }
  634. }
  635. }
  636.  
  637. /**
  638. * Propagates user
  639. *
  640. * Logs in the user on the main site
  641. *
  642. * @since 4.3.1
  643. *
  644. */
  645. function propagate_user(){
  646. header( "Content-Type: text/javascript; charset=" . get_bloginfo( 'charset' ) );
  647.  
  648. if ( get_current_blog_id() == 1 ) {
  649. $user_id = wp_validate_auth_cookie( filter_input( INPUT_GET, 'auth' ), 'auth' );
  650. if ( $user_id ) {
  651. wp_set_auth_cookie( $user_id );
  652. echo 'if (typeof domainmap_do_redirect === "function") domainmap_do_redirect();';
  653. exit;
  654. }
  655. }
  656.  
  657. exit;
  658. }
  659.  
  660. /**
  661. * Reeuthenticates user
  662. *
  663. * It tries to reauth user if it is logged on the mapped domain and then lands in the
  664. * login page of the sub-site with the original domain
  665. *
  666. * @hook login_init
  667. *
  668. * @since 4.4.0.3
  669. */
  670. function reauthenticate_user(){
  671. global $current_user, $redirect_to;
  672.  
  673. if( !empty( $current_user->ID ) && !isset( $_REQUEST['loggedout'] ) && !isset( $_REQUEST['action'] ) ){
  674.  
  675.  
  676. if( !isset( $redirect_to ) )
  677. $redirect_to = $this->_get_reauthenticate_redirect_to();
  678.  
  679. wp_set_auth_cookie( $current_user->ID );
  680. wp_redirect( $redirect_to );
  681. exit();
  682. }
  683. }
  684.  
  685. /**
  686. * Returns $redirect_to variable on reauthentication
  687. *
  688. *
  689. * @since 4.4.0.7
  690. * @return mixed|string|void
  691. */
  692. private function _get_reauthenticate_redirect_to(){
  693. $secure_cookie = false;
  694. // If the user wants ssl but the session is not ssl, force a secure cookie.
  695. if ( !empty($_POST['log']) && !force_ssl_admin() ) {
  696. $user_name = sanitize_user($_POST['log']);
  697. if ( $user = get_user_by('login', $user_name) ) {
  698. if ( get_user_option('use_ssl', $user->ID) ) {
  699. $secure_cookie = true;
  700. force_ssl_admin(true);
  701. }
  702. }
  703. }
  704.  
  705. if ( isset( $_REQUEST['redirect_to'] ) ) {
  706. $redirect_to = $_REQUEST['redirect_to'];
  707. // Redirect to https if user wants ssl
  708. if ( $secure_cookie && false !== strpos($redirect_to, 'wp-admin') )
  709. $redirect_to = preg_replace('|^http://|', 'https://', $redirect_to);
  710. } else {
  711. $redirect_to = admin_url();
  712. }
  713.  
  714. return $redirect_to;
  715. }
  716.  
  717. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement