Plugin Directory

Changeset 1711810


Ignore:
Timestamp:
08/10/2017 10:27:16 PM (9 years ago)
Author:
MVR
Message:

improve sso login

Location:
userecho
Files:
2 edited
1 copied

Legend:

Unmodified
Added
Removed
  • userecho/tags/1.0.6/userecho.php

    r1542874 r1711810  
    22/*
    33Plugin Name: UserEcho for Wordpress - collect feedback for your blog
    4 Version: 1.0.5
    5 Plugin URI: http://userecho.com
     4Version: 1.0.6
     5Plugin URI: https://userecho.com
    66Author: Jonathan Champ, Sergey Stukov, Vladimir Mullagaliyev
    7 Author URI: http://userecho.com
     7Author URI: https://userecho.com
    88Description: UserEcho - feedback widget to collect and manage user feedback for your blog.
    99License: GPLv2
     
    2929class UserEcho {
    3030    public function __construct() {
     31        $this->check_if_sso_login();
    3132        load_plugin_textdomain( 'UserEcho', false, basename( dirname( __FILE__ ) ) . '/lang' );
    3233
    3334        add_action( 'admin_menu', array( $this, 'add_menu' ), 20 );
    34         add_action( 'template_redirect', array( $this, 'login' ), 1 );
    3535        add_action( 'wp_footer', array( $this, 'show_tab_widget' ) );
    3636
     
    6565            $options['tab_icon_show'] = !empty( $_POST['tab_icon_show'] );
    6666            $options['verified_email'] = !empty( $_POST['verified_email'] );
     67           
    6768            if ( !empty( $_POST['forum'] ) ) { $options['forum'] = $_POST['forum']; }
    6869            if ( !empty( $_POST['tab_corner_radius'] ) ) { $options['tab_corner_radius'] = $_POST['tab_corner_radius']; }           
     
    123124       
    124125        if ( !empty( $options['language'] ) ) {
    125             $language_options += array( $options['language'] => 'Custom (' + $options['language'] + ')' );
     126            $language_options += array( $options['language'] => 'Custom ('.$options['language'].')' );
    126127        }
    127128
     
    130131<div id="icon-options-general" class="icon32"><br></div>
    131132<h2>UserEcho integration Settings</h2>
    132 <h3>Tab widget:</h3>
    133 
    134133        <form method="post" action="">
    135134            <?php wp_nonce_field( 'userecho' ); ?>
    136135            <input type="hidden" name="ue_action" value="edit" />
    137136            <div class="table">
     137                <h3>Common:</h3>
    138138                <table class="form-table">
    139139                    <tbody>
    140140                        <tr valign="top">
    141                             <th><?php _e( 'Show UserEcho tab widget', 'UserEcho' ); ?></th>
     141                            <th><?php _e( 'Domain', 'UserEcho' ); ?></th>
     142                            <td>
     143                                <input id="domain" name="domain" type="text" class="regular-text" value="<?php echo esc_attr( $options['domain'] ); ?>" />
     144                                <span class="description"><?php _e( 'Your UserEcho community url (UE subdomain or custom domain, depends on project settings).', 'UserEcho' ); ?></span>
     145                            </td>
     146                        </tr>
     147                    </tbody>
     148                </table>
     149                <h3>Tab widget:</h3>
     150                <table class="form-table">
     151                    <tbody>
     152                        <tr valign="top">
     153                            <th><?php _e( 'Show tab', 'UserEcho' ); ?></th>
    142154                            <td>
    143155                                <input id="show_tab" name="show_tab" type="checkbox" value="1"<?php checked( $options['show_tab'], '1' ); ?> />
     
    145157                        </tr>
    146158                        <tr valign="top">
    147                             <th><?php _e( 'Domain', 'UserEcho' ); ?></th>
    148                             <td>
    149                                 <input id="domain" name="domain" type="text" class="regular-text" value="<?php echo esc_attr( $options['domain'] ); ?>" />
    150                                 <span class="description"><?php _e( 'Your UserEcho community url.', 'UserEcho' ); ?></span>
    151                             </td>
    152                         </tr>
    153                         <tr valign="top">
    154159                            <th><?php _e( 'Forum id', 'UserEcho' ); ?></th>
    155160                            <td>
    156161                                <input id="forum" name="forum" type="text" class="regular-text" value="<?php echo esc_attr( $options['forum'] ); ?>" />
    157                                 <span class="description"><?php _e( 'Move mouse over forum name at the your UserEcho community right panel. And find out forum id in the browser status bar.', 'UserEcho' ); ?></span>
     162                                <span class="description"><?php _e( 'Add a forum ID you would like to see in the widget.', 'UserEcho' ); ?></span>
    158163                            </td>
    159164                        </tr>
     
    169174                        </tr>
    170175
    171                         </tbody>
    172 
    173                         </table>
    174                         <h3>Tab visual style:</h3>
    175                         <table class="form-table"><tbody>
    176                        
    177                        
    178                         <tr valign="top">
     176                    </tbody>
     177                </table>
     178                <h3>Tab visual style:</h3>
     179                <table class="form-table"><tbody>
     180                    <tr valign="top">
    179181                            <th><?php _e( 'Font size', 'UserEcho' ); ?></th>
    180182                            <td>
     
    230232                    </tbody>
    231233                </table>
    232 <h3>Single Sign On (SSO):</h3>
     234                <h3>Single Sign On (SSO):</h3>
    233235                <table class="form-table">
    234236                    <tbody>
    235237                        <tr valign="top">
    236                             <th><?php _e( 'API Key', 'UserEcho' ); ?></th>
     238                            <th><?php _e( 'SSO Key', 'UserEcho' ); ?></th>
    237239                            <td>
    238240                                <input id="api_key" name="api_key" type="text" class="regular-text" value="<?php echo esc_attr( $options['api_key'] ); ?>" />
     241                                <span class="description"><?php _e( 'Available in your SSO settings.', 'UserEcho' ); ?></span>
    239242                            </td>
    240243                        </tr>
     
    243246                            <td>
    244247                                <input id="verified_email" name="verified_email" type="checkbox" value="1"<?php checked( $options['verified_email'], '1' ); ?> />
     248                                <span class="description"><?php _e( 'If checked user email will be marked as verified.', 'UserEcho' ); ?></span>
    245249                            </td>
    246250                        </tr>
     
    263267            $default = array(
    264268                'api_key' => '',
    265                 'domain' => 'YOUR_ALIAS.userecho.com',
     269                'domain' => '[YOUR_ALIAS].userecho.com',
    266270                'show_tab' => 1,
    267271                'language' => 'en',
     
    276280                'tab_icon_show' => 1,
    277281                'verified_email' => 1,
    278 
    279282            );
    280283        }
     
    382385
    383386
    384 
    385     public function login() {
    386         // Perform login on ?userecho_sso_login=1
    387         if ( empty( $_GET['userecho_sso_login'] ) ) {
    388             return;
    389         }
    390 
    391         $options = $this->get_options();
    392 
    393         $base_url = 'http://' . $options['domain'] . '/';
    394 
    395         if ( empty( $options['api_key'] ) ) {
    396             //if api_key not provided just go to the userecho forum without authorization, like simple link was clciked
    397             header( 'Location: ' . $base_url );
    398             die();
    399             return;
    400         }
    401 
     387    public function check_if_sso_login()
     388        {
     389        global $current_user, $pagenow;
     390        #http://127.0.0.1/wordpress/wp-login.php
     391        # http://127.0.0.1/wordpress/wp-login.php?host_url=https://userecho.com&action=ue-login
    402392       
    403         if ( isset( $_GET['return'] ) && strpos( $_GET['return'], $base_url ) === 0 ) {
    404             $base_url = $_GET['return'];
    405         }
    406 
    407         $redirect = $base_url;
    408 
    409         $sso_token = $this->get_sso_token();
    410         if ($sso_token){
    411             $redirect .= '?sso_token=' . $this->get_sso_token();
    412         }
    413        
    414         header( 'Location: ' . $redirect );
    415         die();
     393        if ( 'wp-login.php' == $pagenow ){
     394            $options = $this->get_options();
     395            if ( ! isset( $_REQUEST['return'] ) || ! isset( $_GET['action'] )) {
     396                return;
     397            }
     398            $return_url = $_REQUEST['return'];
     399            if( ! $return_url ) {
     400                return;
     401            }
     402            if ( $_GET['action'] == 'ue-login' ) {
     403                if ( !empty( $current_user->ID) ) {
     404                    // If user is logged into WP, generate SSO and redirect him back to UE
     405                    header( 'Location: '.$this->get_sso_redirect_url($return_url) ) ;
     406                    die();
     407                }
     408                else{
     409                    if (isset($return_url)){
     410                        header( "Location: " .wp_login_url()."?redirect_to=ue_sso_redirect_to=".$return_url );
     411                        die();
     412                    }
     413                }
     414            }
     415        }
     416    }
     417    public function get_sso_redirect_url($return_url){
     418        return $return_url . '?sso_token=' . $this->get_sso_token();
    416419    }
    417420}
     
    531534
    532535$ue = new UserEcho();
     536
     537# Redirect back to UE after login if it's login via SSO
     538add_filter( 'login_redirect', 'ue_sso_login_redirect', 10, 3 );
     539function ue_sso_login_redirect($url, $request, $user) {
     540    global $ue;
     541    parse_str( $request, $params );
     542    if ( ! isset($params['ue_sso_redirect_to']) ) {
     543        return $url;
     544    }
     545    $return_url = $params['ue_sso_redirect_to'];
     546    if ( ! $return_url ) {
     547        return $url;
     548    }
     549
     550    if (isset( $_REQUEST['wp-submit'] ) && $_REQUEST['wp-submit'] == "Log In" && is_a( $user, 'WP_User' )) {
     551        header("Location: ".$ue->get_sso_redirect_url($return_url));
     552        die();
     553    }
     554    return  $request;
     555}
  • userecho/trunk/userecho.php

    r1542874 r1711810  
    22/*
    33Plugin Name: UserEcho for Wordpress - collect feedback for your blog
    4 Version: 1.0.5
    5 Plugin URI: http://userecho.com
     4Version: 1.0.6
     5Plugin URI: https://userecho.com
    66Author: Jonathan Champ, Sergey Stukov, Vladimir Mullagaliyev
    7 Author URI: http://userecho.com
     7Author URI: https://userecho.com
    88Description: UserEcho - feedback widget to collect and manage user feedback for your blog.
    99License: GPLv2
     
    2929class UserEcho {
    3030    public function __construct() {
     31        $this->check_if_sso_login();
    3132        load_plugin_textdomain( 'UserEcho', false, basename( dirname( __FILE__ ) ) . '/lang' );
    3233
    3334        add_action( 'admin_menu', array( $this, 'add_menu' ), 20 );
    34         add_action( 'template_redirect', array( $this, 'login' ), 1 );
    3535        add_action( 'wp_footer', array( $this, 'show_tab_widget' ) );
    3636
     
    6565            $options['tab_icon_show'] = !empty( $_POST['tab_icon_show'] );
    6666            $options['verified_email'] = !empty( $_POST['verified_email'] );
     67           
    6768            if ( !empty( $_POST['forum'] ) ) { $options['forum'] = $_POST['forum']; }
    6869            if ( !empty( $_POST['tab_corner_radius'] ) ) { $options['tab_corner_radius'] = $_POST['tab_corner_radius']; }           
     
    123124       
    124125        if ( !empty( $options['language'] ) ) {
    125             $language_options += array( $options['language'] => 'Custom (' + $options['language'] + ')' );
     126            $language_options += array( $options['language'] => 'Custom ('.$options['language'].')' );
    126127        }
    127128
     
    130131<div id="icon-options-general" class="icon32"><br></div>
    131132<h2>UserEcho integration Settings</h2>
    132 <h3>Tab widget:</h3>
    133 
    134133        <form method="post" action="">
    135134            <?php wp_nonce_field( 'userecho' ); ?>
    136135            <input type="hidden" name="ue_action" value="edit" />
    137136            <div class="table">
     137                <h3>Common:</h3>
    138138                <table class="form-table">
    139139                    <tbody>
    140140                        <tr valign="top">
    141                             <th><?php _e( 'Show UserEcho tab widget', 'UserEcho' ); ?></th>
     141                            <th><?php _e( 'Domain', 'UserEcho' ); ?></th>
     142                            <td>
     143                                <input id="domain" name="domain" type="text" class="regular-text" value="<?php echo esc_attr( $options['domain'] ); ?>" />
     144                                <span class="description"><?php _e( 'Your UserEcho community url (UE subdomain or custom domain, depends on project settings).', 'UserEcho' ); ?></span>
     145                            </td>
     146                        </tr>
     147                    </tbody>
     148                </table>
     149                <h3>Tab widget:</h3>
     150                <table class="form-table">
     151                    <tbody>
     152                        <tr valign="top">
     153                            <th><?php _e( 'Show tab', 'UserEcho' ); ?></th>
    142154                            <td>
    143155                                <input id="show_tab" name="show_tab" type="checkbox" value="1"<?php checked( $options['show_tab'], '1' ); ?> />
     
    145157                        </tr>
    146158                        <tr valign="top">
    147                             <th><?php _e( 'Domain', 'UserEcho' ); ?></th>
    148                             <td>
    149                                 <input id="domain" name="domain" type="text" class="regular-text" value="<?php echo esc_attr( $options['domain'] ); ?>" />
    150                                 <span class="description"><?php _e( 'Your UserEcho community url.', 'UserEcho' ); ?></span>
    151                             </td>
    152                         </tr>
    153                         <tr valign="top">
    154159                            <th><?php _e( 'Forum id', 'UserEcho' ); ?></th>
    155160                            <td>
    156161                                <input id="forum" name="forum" type="text" class="regular-text" value="<?php echo esc_attr( $options['forum'] ); ?>" />
    157                                 <span class="description"><?php _e( 'Move mouse over forum name at the your UserEcho community right panel. And find out forum id in the browser status bar.', 'UserEcho' ); ?></span>
     162                                <span class="description"><?php _e( 'Add a forum ID you would like to see in the widget.', 'UserEcho' ); ?></span>
    158163                            </td>
    159164                        </tr>
     
    169174                        </tr>
    170175
    171                         </tbody>
    172 
    173                         </table>
    174                         <h3>Tab visual style:</h3>
    175                         <table class="form-table"><tbody>
    176                        
    177                        
    178                         <tr valign="top">
     176                    </tbody>
     177                </table>
     178                <h3>Tab visual style:</h3>
     179                <table class="form-table"><tbody>
     180                    <tr valign="top">
    179181                            <th><?php _e( 'Font size', 'UserEcho' ); ?></th>
    180182                            <td>
     
    230232                    </tbody>
    231233                </table>
    232 <h3>Single Sign On (SSO):</h3>
     234                <h3>Single Sign On (SSO):</h3>
    233235                <table class="form-table">
    234236                    <tbody>
    235237                        <tr valign="top">
    236                             <th><?php _e( 'API Key', 'UserEcho' ); ?></th>
     238                            <th><?php _e( 'SSO Key', 'UserEcho' ); ?></th>
    237239                            <td>
    238240                                <input id="api_key" name="api_key" type="text" class="regular-text" value="<?php echo esc_attr( $options['api_key'] ); ?>" />
     241                                <span class="description"><?php _e( 'Available in your SSO settings.', 'UserEcho' ); ?></span>
    239242                            </td>
    240243                        </tr>
     
    243246                            <td>
    244247                                <input id="verified_email" name="verified_email" type="checkbox" value="1"<?php checked( $options['verified_email'], '1' ); ?> />
     248                                <span class="description"><?php _e( 'If checked user email will be marked as verified.', 'UserEcho' ); ?></span>
    245249                            </td>
    246250                        </tr>
     
    263267            $default = array(
    264268                'api_key' => '',
    265                 'domain' => 'YOUR_ALIAS.userecho.com',
     269                'domain' => '[YOUR_ALIAS].userecho.com',
    266270                'show_tab' => 1,
    267271                'language' => 'en',
     
    276280                'tab_icon_show' => 1,
    277281                'verified_email' => 1,
    278 
    279282            );
    280283        }
     
    382385
    383386
    384 
    385     public function login() {
    386         // Perform login on ?userecho_sso_login=1
    387         if ( empty( $_GET['userecho_sso_login'] ) ) {
    388             return;
    389         }
    390 
    391         $options = $this->get_options();
    392 
    393         $base_url = 'http://' . $options['domain'] . '/';
    394 
    395         if ( empty( $options['api_key'] ) ) {
    396             //if api_key not provided just go to the userecho forum without authorization, like simple link was clciked
    397             header( 'Location: ' . $base_url );
    398             die();
    399             return;
    400         }
    401 
     387    public function check_if_sso_login()
     388        {
     389        global $current_user, $pagenow;
     390        #http://127.0.0.1/wordpress/wp-login.php
     391        # http://127.0.0.1/wordpress/wp-login.php?host_url=https://userecho.com&action=ue-login
    402392       
    403         if ( isset( $_GET['return'] ) && strpos( $_GET['return'], $base_url ) === 0 ) {
    404             $base_url = $_GET['return'];
    405         }
    406 
    407         $redirect = $base_url;
    408 
    409         $sso_token = $this->get_sso_token();
    410         if ($sso_token){
    411             $redirect .= '?sso_token=' . $this->get_sso_token();
    412         }
    413        
    414         header( 'Location: ' . $redirect );
    415         die();
     393        if ( 'wp-login.php' == $pagenow ){
     394            $options = $this->get_options();
     395            if ( ! isset( $_REQUEST['return'] ) || ! isset( $_GET['action'] )) {
     396                return;
     397            }
     398            $return_url = $_REQUEST['return'];
     399            if( ! $return_url ) {
     400                return;
     401            }
     402            if ( $_GET['action'] == 'ue-login' ) {
     403                if ( !empty( $current_user->ID) ) {
     404                    // If user is logged into WP, generate SSO and redirect him back to UE
     405                    header( 'Location: '.$this->get_sso_redirect_url($return_url) ) ;
     406                    die();
     407                }
     408                else{
     409                    if (isset($return_url)){
     410                        header( "Location: " .wp_login_url()."?redirect_to=ue_sso_redirect_to=".$return_url );
     411                        die();
     412                    }
     413                }
     414            }
     415        }
     416    }
     417    public function get_sso_redirect_url($return_url){
     418        return $return_url . '?sso_token=' . $this->get_sso_token();
    416419    }
    417420}
     
    531534
    532535$ue = new UserEcho();
     536
     537# Redirect back to UE after login if it's login via SSO
     538add_filter( 'login_redirect', 'ue_sso_login_redirect', 10, 3 );
     539function ue_sso_login_redirect($url, $request, $user) {
     540    global $ue;
     541    parse_str( $request, $params );
     542    if ( ! isset($params['ue_sso_redirect_to']) ) {
     543        return $url;
     544    }
     545    $return_url = $params['ue_sso_redirect_to'];
     546    if ( ! $return_url ) {
     547        return $url;
     548    }
     549
     550    if (isset( $_REQUEST['wp-submit'] ) && $_REQUEST['wp-submit'] == "Log In" && is_a( $user, 'WP_User' )) {
     551        header("Location: ".$ue->get_sso_redirect_url($return_url));
     552        die();
     553    }
     554    return  $request;
     555}
Note: See TracChangeset for help on using the changeset viewer.