Advertisement
dedotombo

WP Cassify - Modified wp_cassify_plugin.php

Oct 3rd, 2017
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 59.86 KB | None | 0 0
  1. <?php
  2. namespace wp_cassify;
  3.  
  4. class WP_Cassify_Plugin {
  5.  
  6.     public $wp_cassify_network_activated;
  7.  
  8.     public $wp_cassify_default_xpath_query_to_extact_cas_user;
  9.     public $wp_cassify_default_xpath_query_to_extact_cas_attributes;
  10.     public $wp_cassify_default_redirect_parameter_name;
  11.     public $wp_cassify_default_service_ticket_parameter_name;
  12.     public $wp_cassify_default_service_service_parameter_name;
  13.     public $wp_cassify_default_gateway_parameter_name;
  14.     public $wp_cassify_default_bypass_parameter_name;
  15.     public $wp_cassify_default_cachetimes_for_authrecheck;
  16.  
  17.     public $wp_cassify_default_wordpress_blog_http_port;
  18.     public $wp_cassify_default_wordpress_blog_https_port;
  19.     public $wp_cassify_default_ssl_check_certificate;
  20.  
  21.     public $wp_cassify_default_login_servlet;
  22.     public $wp_cassify_default_logout_servlet;
  23.     public $wp_cassify_default_service_validate_servlet;
  24.  
  25.     public $wp_cassify_default_allow_deny_order;
  26.  
  27.     public $wp_cassify_match_first_level_parenthesis_group_pattern;
  28.     public $wp_cassify_match_second_level_parenthesis_group_pattern;
  29.     public $wp_cassify_match_cas_variable_pattern;
  30.     public $wp_cassify_allowed_operators;
  31.     public $wp_cassify_operator_prefix;
  32.     public $wp_cassify_allowed_parenthesis;
  33.  
  34.     public $wp_cassify_allowed_get_parameters;
  35.     public $wp_cassify_error_messages;
  36.     public $wp_cassify_user_error_codes;
  37.  
  38.     private $wp_cassify_allow_rules = array();
  39.     private $wp_cassify_deny_rules = array();
  40.  
  41.     private $wp_cassify_current_blog_id;
  42.  
  43.     /**
  44.      * Constructor
  45.      */
  46.     public function __construct() {
  47.     }
  48.  
  49.     /**
  50.      * Initialize the plugin with parameters
  51.      *
  52.      * param string $wp_cassify_network_activated
  53.      * param string $wp_cassify_default_xpath_query_to_extact_cas_user
  54.      * param string $wp_cassify_default_xpath_query_to_extact_cas_attributes
  55.      * param string $wp_cassify_default_redirect_parameter_name
  56.      * param string $wp_cassify_default_service_ticket_parameter_name
  57.      * param string $wp_cassify_default_service_service_parameter_name
  58.      * param string $wp_cassify_default_gateway_parameter_name
  59.      * param string $wp_cassify_default_bypass_parameter_name
  60.      * param int    $wp_cassify_default_cachetimes_for_authrecheck
  61.      * param string $wp_cassify_default_wordpress_blog_http_port
  62.      * param string $wp_cassify_default_wordpress_blog_https_port
  63.      * param string $wp_cassify_default_ssl_check_certificate
  64.      * param string $wp_cassify_default_login_servlet
  65.      * param string $wp_cassify_default_logout_servlet
  66.      * param string $wp_cassify_default_service_validate_servlet
  67.      * param string $wp_cassify_default_allow_deny_order
  68.      * param string $wp_cassify_match_first_level_parenthesis_group_pattern
  69.      * param string $wp_cassify_match_second_level_parenthesis_group_pattern
  70.      * param string $wp_cassify_match_cas_variable_pattern
  71.      * param string $wp_cassify_allowed_operators
  72.      * param string $wp_cassify_operator_prefix
  73.      * param string $wp_cassify_allowed_parenthesis
  74.      * param array  $wp_cassify_allowed_get_parameters
  75.      * param array  $wp_cassify_error_messages
  76.      * param array  $wp_cassify_user_error_codes
  77.      */
  78.     public function init_parameters(
  79.         $wp_cassify_network_activated,
  80.         $wp_cassify_default_xpath_query_to_extact_cas_user,
  81.         $wp_cassify_default_xpath_query_to_extact_cas_attributes,
  82.         $wp_cassify_default_redirect_parameter_name,
  83.         $wp_cassify_default_service_ticket_parameter_name,
  84.         $wp_cassify_default_service_service_parameter_name,
  85.         $wp_cassify_default_gateway_parameter_name,
  86.         $wp_cassify_default_bypass_parameter_name,
  87.         $wp_cassify_default_cachetimes_for_authrecheck,
  88.         $wp_cassify_default_wordpress_blog_http_port,
  89.         $wp_cassify_default_wordpress_blog_https_port,
  90.         $wp_cassify_default_ssl_check_certificate,
  91.         $wp_cassify_default_login_servlet,
  92.         $wp_cassify_default_logout_servlet,
  93.         $wp_cassify_default_service_validate_servlet,
  94.         $wp_cassify_default_allow_deny_order,
  95.         $wp_cassify_match_first_level_parenthesis_group_pattern,
  96.         $wp_cassify_match_second_level_parenthesis_group_pattern,
  97.         $wp_cassify_match_cas_variable_pattern,
  98.         $wp_cassify_allowed_operators,
  99.         $wp_cassify_operator_prefix,
  100.         $wp_cassify_allowed_parenthesis,
  101.         $wp_cassify_allowed_get_parameters,
  102.         $wp_cassify_error_messages,
  103.         $wp_cassify_user_error_codes
  104.     ) {
  105.         $this->wp_cassify_network_activated = $wp_cassify_network_activated;
  106.         $this->wp_cassify_default_xpath_query_to_extact_cas_user = $wp_cassify_default_xpath_query_to_extact_cas_user;
  107.         $this->wp_cassify_default_xpath_query_to_extact_cas_attributes = $wp_cassify_default_xpath_query_to_extact_cas_attributes;
  108.         $this->wp_cassify_default_redirect_parameter_name = $wp_cassify_default_redirect_parameter_name;
  109.         $this->wp_cassify_default_service_ticket_parameter_name = $wp_cassify_default_service_ticket_parameter_name;
  110.         $this->wp_cassify_default_service_service_parameter_name = $wp_cassify_default_service_service_parameter_name;
  111.         $this->wp_cassify_default_gateway_parameter_name = $wp_cassify_default_gateway_parameter_name;
  112.         $this->wp_cassify_default_bypass_parameter_name = $wp_cassify_default_bypass_parameter_name;
  113.         $this->wp_cassify_default_cachetimes_for_authrecheck = $wp_cassify_default_cachetimes_for_authrecheck;
  114.         $this->wp_cassify_default_wordpress_blog_http_port = $wp_cassify_default_wordpress_blog_http_port;
  115.         $this->wp_cassify_default_wordpress_blog_https_port = $wp_cassify_default_wordpress_blog_https_port;
  116.         $this->wp_cassify_default_ssl_check_certificate = $wp_cassify_default_ssl_check_certificate;
  117.         $this->wp_cassify_default_login_servlet = $wp_cassify_default_login_servlet;
  118.         $this->wp_cassify_default_logout_servlet = $wp_cassify_default_logout_servlet;
  119.         $this->wp_cassify_default_service_validate_servlet = $wp_cassify_default_service_validate_servlet;
  120.         $this->wp_cassify_default_allow_deny_order = $wp_cassify_default_allow_deny_order;
  121.         $this->wp_cassify_match_first_level_parenthesis_group_pattern = $wp_cassify_match_first_level_parenthesis_group_pattern;
  122.         $this->wp_cassify_match_second_level_parenthesis_group_pattern = $wp_cassify_match_second_level_parenthesis_group_pattern;
  123.         $this->wp_cassify_match_cas_variable_pattern = $wp_cassify_match_cas_variable_pattern;
  124.         $this->wp_cassify_allowed_operators = $wp_cassify_allowed_operators;
  125.         $this->wp_cassify_operator_prefix = $wp_cassify_operator_prefix;
  126.         $this->wp_cassify_allowed_parenthesis = $wp_cassify_allowed_parenthesis;
  127.         $this->wp_cassify_allowed_get_parameters = $wp_cassify_allowed_get_parameters;
  128.         $this->wp_cassify_error_messages    = $wp_cassify_error_messages;
  129.         $this->wp_cassify_user_error_codes = $wp_cassify_user_error_codes;
  130.  
  131.         // Check if CAS Authentication must be bypassed.
  132.         if (! $this->wp_cassify_bypass() ) {
  133.  
  134.             // Add the filters
  135.             add_filter( 'query_vars', array( $this , 'add_custom_query_var' ) );
  136.             add_filter( 'login_url', array( $this, 'wp_cassify_clear_reauth' ) );
  137.             add_filter( 'the_content', array( $this, 'wp_cassify_display_message' ) );
  138.  
  139.             // Add the actions
  140.             add_action( 'init', array( $this , 'wp_cassify_session_start' ), 1 );
  141.             add_action( 'init', array( $this , 'wp_cassify_grab_service_ticket' ) , 2 );
  142.  
  143.             add_action( 'wp_authenticate', array( $this , 'wp_cassify_redirect' ) , 1 );
  144.             add_action( 'wp_logout', array( $this , 'wp_cassify_logout' ) , 10 );
  145.             add_action( 'wp_cassify_send_notification', array( $this, 'wp_cassify_send_notification_message' ), 1, 1 );
  146.  
  147.             add_action( 'template_redirect', array ( $this, 'wp_cassify_gateway_mode') , 1 );
  148.             add_action( 'init', array ( $this, 'wp_cassify_sso' ) , 1 );
  149.         }
  150.  
  151.         // Get current blog id to store information session in $_SESSION['wp_cassify'][$blogid]
  152.         $this->wp_cassify_current_blog_id = get_current_blog_id();
  153.     }
  154.  
  155.     /**
  156.      * Allow custom get parameters in url
  157.      * @param   array   $vars An array of GET allowed parameters
  158.      * @return  array   $vars An array of GET allowed parameters filled with extra parameters
  159.      */
  160.     public function add_custom_query_var( $vars ){
  161.  
  162.       $vars[] = $this->wp_cassify_default_service_ticket_parameter_name;
  163.       $vars[] = $this->wp_cassify_default_service_service_parameter_name;
  164.       $vars[] = $this->wp_cassify_default_bypass_parameter_name;
  165.       $vars[] = $this->wp_cassify_default_gateway_parameter_name;
  166.  
  167.       foreach ( $this->wp_cassify_allowed_get_parameters as $allowed_get_parameter ) {
  168.             $vars[] = $allowed_get_parameter;
  169.       }
  170.  
  171.       return $vars;
  172.     }
  173.  
  174.     /**
  175.      * Display information messages from plugin on front-ofice
  176.      * @param string $content   Page content to replace by message to display
  177.      */
  178.     public function wp_cassify_display_message( $content ) {
  179.  
  180.         $wp_cassify_message_parameter = get_query_var( 'wp-cassify-message' );
  181.  
  182.         if (! empty( $wp_cassify_message_parameter ) ) {
  183.             $content = '<h1>'. $this->wp_cassify_user_error_codes[ $wp_cassify_message_parameter ] . '</h1>';
  184.         }
  185.  
  186.         return $content;
  187.     }
  188.  
  189.     /**
  190.      * Clear reauth parameter from login url to login directly from CAS server.
  191.      */
  192.     public function wp_cassify_clear_reauth( $login_url ) {
  193.  
  194.         $login_url = remove_query_arg( 'reauth', $login_url );
  195.         return $login_url;
  196.     }
  197.  
  198.     /**
  199.      * Start the php session inside the plugin because session is needed to store callback url.
  200.      */
  201.     public function wp_cassify_session_start() {
  202.  
  203.         if(! session_id() ) {
  204.             session_start();
  205.         }
  206.     }
  207.  
  208.     /**
  209.      * This function store CAS User authentication state into session.
  210.      * @param bool $is_authenticated    If true, CAS User authentication state is set to true.
  211.      */
  212.     public function wp_cassify_set_authenticated( $is_authenticated ) {
  213.  
  214.         $this->wp_cassify_session_start();
  215.  
  216.         if ( $is_authenticated ) {
  217.             $_SESSION['wp_cassify'][ $this->wp_cassify_current_blog_id ]['user_auth'] = true;
  218.         }
  219.         else {
  220.             $_SESSION['wp_cassify'][ $this->wp_cassify_current_blog_id ]['user_auth'] = false;
  221.         }
  222.     }
  223.  
  224.     /**
  225.      * This function informed if user is already authenticated by CAS.
  226.      */
  227.     public function wp_cassify_is_authenticated() {
  228.  
  229.         $is_authenticated = false;
  230.  
  231.         $this->wp_cassify_session_start();
  232.  
  233.         if ( isset( $_SESSION['wp_cassify'] ) ) {
  234.             if ( $_SESSION['wp_cassify'][ $this->wp_cassify_current_blog_id ]['user_auth'] == true ) {
  235.                 $is_authenticated = true;
  236.             }
  237.         }
  238.  
  239.         return $is_authenticated;
  240.     }
  241.  
  242.     /**
  243.      * This function return cas user attributes populated into php session by plugin.
  244.      * @return array    $cas_user_datas Associative array containing user attributes and id. Use print_r to expect variable.
  245.      */
  246.     public function wp_cassify_get_cas_user_datas() {
  247.  
  248.         $this->wp_cassify_session_start();
  249.  
  250.         $cas_user_datas = false;
  251.  
  252.         if ( isset( $_SESSION['wp_cassify'] ) ) {
  253.             $cas_user_datas = $_SESSION['wp_cassify'][ $this->wp_cassify_current_blog_id ]['wp_cassify_cas_user_datas'];
  254.         }
  255.  
  256.         return $cas_user_datas;
  257.     }
  258.  
  259.     /**
  260.      * Get the times authentication will be cached before really accessing
  261.      * the CAS server in gateway mode.
  262.      *
  263.      * @return int $cache_times_for_auth_recheck
  264.      */
  265.     public function wp_cassify_get_cache_times_for_auth_recheck() {
  266.  
  267.         $cachetimes_for_authrecheck = WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_cachetimes_for_authrecheck' );
  268.  
  269.         if ( empty( $cachetimes_for_authrecheck ) ) {
  270.             $cachetimes_for_authrecheck = $this->wp_cassify_default_cachetimes_for_authrecheck;
  271.         }
  272.  
  273.         return $cachetimes_for_authrecheck;
  274.     }
  275.  
  276.     /**
  277.      * Perform a redirection to cas server to obtain service ticket.
  278.      */
  279.     public function wp_cassify_grab_service_ticket() {
  280.  
  281.         $service_url = null;
  282.         $service_ticket = null;
  283.         $wordpress_user_account_created = false;
  284.         $current_blog_id = get_current_blog_id();
  285.  
  286.         $wp_cassify_base_url = WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_base_url' );
  287.         $wp_cassify_create_user_if_not_exist = WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_create_user_if_not_exist' );
  288.         $wp_cassify_ssl_cipher =  WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_ssl_cipher' );
  289.         $wp_cassify_ssl_check_certificate =  WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_ssl_check_certificate' );
  290.         $wp_cassify_attributes_list = WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_attributes_list' );
  291.         $wp_cassify_login_servlet = WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_login_servlet' );
  292.         $wp_cassify_logout_servlet = WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_logout_servlet' );
  293.         $wp_cassify_service_validate_servlet =  WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_service_validate_servlet' );
  294.         $wp_cassify_allow_deny_order = WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_allow_deny_order' );
  295.         $wp_cassify_autorization_rules = unserialize( WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_autorization_rules' ) );
  296.         $wp_cassify_user_role_rules = unserialize( WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_user_role_rules' ) );
  297.         $wp_cassify_user_attributes_mapping_list = unserialize( WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_user_attributes_mapping_list' ) );
  298.         $wp_cassify_notification_rules = unserialize( WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_notification_rules' ) );
  299.         $wp_cassify_expiration_rules = unserialize( WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_expiration_rules' ) );
  300.  
  301.         if ( empty( $wp_cassify_login_servlet ) ) {
  302.             $wp_cassify_login_servlet = $this->wp_cassify_default_login_servlet;
  303.         }
  304.  
  305.         if ( empty( $wp_cassify_logout_servlet ) ) {
  306.             $wp_cassify_logout_servlet = $this->wp_cassify_default_logout_servlet;
  307.         }
  308.  
  309.         if ( empty( $wp_cassify_service_validate_servlet ) ) {
  310.             $wp_cassify_service_validate_servlet = $this->wp_cassify_default_service_validate_servlet;
  311.         }
  312.  
  313.         if (! empty( $wp_cassify_ssl_cipher ) ) {
  314.             $wp_cassify_ssl_cipher_selected = $wp_cassify_ssl_cipher;
  315.         }
  316.         else {
  317.             $wp_cassify_ssl_cipher_selected = '0';
  318.         }
  319.  
  320.         if ( empty( $wp_cassify_ssl_check_certificate ) ) {
  321.             $wp_cassify_ssl_check_certificate = $this->wp_cassify_default_ssl_check_certificate;
  322.         }
  323.  
  324.         if ( empty( $wp_cassify_allow_deny_order ) ) {
  325.             $wp_cassify_allow_deny_order = $this->wp_cassify_default_allow_deny_order;
  326.         }
  327.  
  328.         if ( ( is_array( $wp_cassify_autorization_rules ) ) && ( count( $wp_cassify_autorization_rules ) > 0 ) ) {
  329.             foreach ( $wp_cassify_autorization_rules as $rule_key => $rule_value ) {
  330.                 $wp_cassify_autorization_rules[ $rule_key ] = stripslashes( $rule_value );
  331.             }
  332.         }
  333.         else {
  334.             $wp_cassify_autorization_rules = array();
  335.         }
  336.  
  337.         $service_url = $this->wp_cassify_get_service_callback_url();
  338.         $service_ticket = $this->wp_cassify_get_service_ticket();
  339.  
  340.         $gateway_mode = $this->wp_cassify_is_gateway_request( null );
  341.  
  342.         if ( (! is_user_logged_in() ) || (! is_user_member_of_blog() ) ) {
  343.             if (! empty( $service_ticket ) ) {
  344.                 $service_validate_url = $wp_cassify_base_url .
  345.                     $wp_cassify_service_validate_servlet . '?' .
  346.                     $this->wp_cassify_default_service_ticket_parameter_name . '=' . $service_ticket . '&' .
  347.                     $this->wp_cassify_default_service_service_parameter_name .'=' . $service_url;
  348.  
  349.                 $cas_server_xml_response = WP_Cassify_Utils::wp_cassify_do_ssl_web_request(
  350.                     $service_validate_url,
  351.                     $wp_cassify_ssl_cipher_selected,
  352.                     $wp_cassify_ssl_check_certificate
  353.                 );
  354.  
  355.                 // Dump xml cas server response if debug option is enabled.
  356.                 if ( WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_xml_response_dump' ) == 'enabled' ) {
  357.                     if ( $this->wp_cassify_network_activated ) {
  358.                         update_site_option( 'wp_cassify_xml_response_value' , $cas_server_xml_response );
  359.                     }
  360.                     else {
  361.                         update_option( 'wp_cassify_xml_response_value' , $cas_server_xml_response );
  362.                     }
  363.                 }
  364.  
  365.                 // Parse CAS Server response and store into associative array.
  366.                 $cas_user_datas = $this->wp_cassify_parse_xml_response( $cas_server_xml_response );
  367.  
  368.                 if ( empty( $cas_user_datas['cas_user_id'] ) ) {
  369.                     die( 'CAS Authentication failed ! ');
  370.                 }
  371.                 else {
  372.                     $this->wp_cassify_set_authenticated( true );
  373.                 }
  374.  
  375.                 // Define custom plugin filter to build your custom parsing function
  376.                 if( has_filter( 'wp_cassify_custom_parsing_cas_xml_response' ) ) {
  377.                     $cas_user_datas = apply_filters( 'wp_cassify_custom_parsing_cas_xml_response', $cas_server_xml_response, $cas_user_datas );
  378.                 }
  379.  
  380.                 // Evaluate authorization rules
  381.                 if ( ( is_array( $wp_cassify_autorization_rules ) ) &&  ( count( $wp_cassify_autorization_rules ) > 0 ) ) {
  382.                     $this->wp_cassify_separate_rules( $wp_cassify_autorization_rules );
  383.  
  384.                     // Force logout if user is not allowed.
  385.                     if (! $this->wp_cassify_is_user_allowed( $cas_user_datas, $wp_cassify_allow_deny_order ) ) {
  386.                         $this->wp_cassify_logout_if_not_allowed( 'user_is_not_allowed' );
  387.                     }
  388.                 }
  389.  
  390.                 // Evaluate expiration rules
  391.                 if ( ( is_array( $wp_cassify_expiration_rules ) ) &&  ( count( $wp_cassify_expiration_rules ) > 0 ) ) {
  392.                     if ( $this->wp_cassify_is_user_account_expired( $cas_user_datas, $wp_cassify_expiration_rules ) ) {
  393.  
  394.                         $notification_rule_matched = $this->wp_cassify_notification_rule_matched(
  395.                             $cas_user_datas,
  396.                             $wp_cassify_notification_rules,
  397.                             'when_user_account_expire'
  398.                         );
  399.  
  400.                         if ( $notification_rule_matched ) {
  401.                             // Define custom plugin hook to send notification after user account has expired.
  402.                             do_action( 'wp_cassify_send_notification', 'User ' . $cas_user_datas[ 'cas_user_id' ] . ' : user account has expired.' );
  403.                         }
  404.  
  405.                         // Force logout if user account has expired.
  406.                         $this->wp_cassify_logout_if_not_allowed( 'user_account_expired' );
  407.                     }
  408.                 }
  409.  
  410.                 // Define custom plugin hook after cas authentication.
  411.                 // For example, for two factor authentication, you can plug another authentication plugin to fired custom action here.
  412.                 do_action( 'wp_cassify_after_cas_authentication', $cas_user_datas );
  413.  
  414.                 // Populate selected attributes into session
  415.                 $this->wp_cassify_populate_attributes_into_session( $cas_user_datas, $wp_cassify_attributes_list );
  416.  
  417.                 // Create wordpress user account if not exist
  418.                 if ( $wp_cassify_create_user_if_not_exist == 'create_user_if_not_exist' ) {
  419.                     if ( WP_Cassify_Utils::wp_cassify_is_wordpress_user_exist( $cas_user_datas[ 'cas_user_id' ] ) == false ) {
  420.                         $wordpress_user_id = WP_Cassify_Utils::wp_cassify_create_wordpress_user( $cas_user_datas[ 'cas_user_id' ], null );
  421.  
  422.                         if ( $wordpress_user_id > 0 ) {
  423.                             $wordpress_user_account_created = true;
  424.  
  425.                             $notification_rule_matched = $this->wp_cassify_notification_rule_matched(
  426.                                 $cas_user_datas,
  427.                                 $wp_cassify_notification_rules,
  428.                                 'after_user_account_created'
  429.                             );
  430.  
  431.                             if ( $notification_rule_matched ) {
  432.                                 // Define custom plugin hook to send notification after user account has been created.
  433.                                 do_action( 'wp_cassify_send_notification', 'User account has been created :' . $cas_user_datas[ 'cas_user_id' ] );
  434.                             }
  435.                         }
  436.                     }
  437.                 }
  438.  
  439.                 // Set wordpress user roles if defined in plugin admin settings
  440.                 $roles_to_push = $this->wp_cassify_get_roles_to_push( $cas_user_datas, $wp_cassify_user_role_rules, $this->wp_cassify_network_activated, $current_blog_id  );
  441.  
  442.                 // Define custom plugin filter to override list roles to push.
  443.                 if( has_filter( 'wp_cassify_grab_service_ticket_roles_to_push' ) ) {
  444.                     $roles_to_push = apply_filters( 'wp_cassify_grab_service_ticket_roles_to_push', $roles_to_push );
  445.                 }
  446.  
  447.                 foreach ( $roles_to_push as $role ) {
  448.                     WP_Cassify_Utils::wp_cassify_add_role_to_wordpress_user( $cas_user_datas[ 'cas_user_id' ], $role );
  449.                 }
  450.  
  451.                 // Sync CAS User attributes with Wordpress User meta
  452.                 $this->wp_cassify_sync_user_metadata(
  453.                     $cas_user_datas[ 'cas_user_id' ],
  454.                     $cas_user_datas,
  455.                     $wp_cassify_user_attributes_mapping_list
  456.                 );
  457.  
  458.                 $cas_service_ticket = get_user_meta( get_user_by( 'login', $cas_user_datas[ 'cas_user_id' ] ), 'cas_service_ticket', true );
  459.                 update_user_meta(
  460.                     get_user_by( 'login', $cas_user_datas[ 'cas_user_id' ] )->ID,
  461.                     'cas_service_ticket',
  462.                     $service_ticket
  463.                 );
  464.  
  465.                 // Custom hook to perform action before wordpress authentication.
  466.                 do_action( 'wp_cassify_before_auth_user_wordpress', $cas_user_datas );
  467.  
  468.                 // Auth user into wordpress
  469.                 WP_Cassify_Utils::wp_cassify_auth_user_wordpress( $cas_user_datas[ 'cas_user_id' ] );
  470.  
  471.                 $notification_rule_matched = $this->wp_cassify_notification_rule_matched(
  472.                     $cas_user_datas,
  473.                     $wp_cassify_notification_rules,
  474.                     'after_user_login'
  475.                 );
  476.  
  477.                 if ( $notification_rule_matched ) {
  478.                     // Define custom plugin hook to send notification after user has been logged in
  479.                     do_action( 'wp_cassify_send_notification', 'User ' . $cas_user_datas[ 'cas_user_id' ] . ' is logged in' );
  480.                 }
  481.  
  482.                 // Redirect to the service url.
  483.                 WP_Cassify_Utils::wp_cassify_redirect_url( $service_url );
  484.             }
  485.         }
  486.     }
  487.  
  488.     /**
  489.      * Perform a redirection to cas server to obtain service ticket.
  490.      * @param   bool    $gateway_mode   Make gateway request. See CAS documentation here : https://wiki.jasig.org/display/CAS/gateway.
  491.      */
  492.     public function wp_cassify_redirect( $gateway_mode = false ) {
  493.  
  494.         do_action( 'wp_cassify_before_redirect' );
  495.  
  496.         $service_url = null;
  497.         $service_ticket = null;
  498.  
  499.         $wp_cassify_base_url = WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_base_url' );
  500.         $wp_cassify_login_servlet = WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_login_servlet' );
  501.         $wp_cassify_logout_servlet = WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_logout_servlet' );
  502.  
  503.         // Define default values if options values empty.
  504.         if ( empty( $wp_cassify_login_servlet ) ) {
  505.             $wp_cassify_login_servlet = $this->wp_cassify_default_login_servlet;
  506.         }
  507.  
  508.         if ( empty( $wp_cassify_logout_servlet ) ) {
  509.             $wp_cassify_logout_servlet = $this->wp_cassify_default_logout_servlet;
  510.         }
  511.  
  512.         if ( empty( $wp_cassify_service_validate_servlet ) ) {
  513.             $wp_cassify_service_validate_servlet = $this->wp_cassify_default_service_validate_servlet;
  514.         }
  515.  
  516.         $service_url = $this->wp_cassify_get_service_callback_url();
  517.         $service_ticket = $this->wp_cassify_get_service_ticket();
  518.  
  519.         $current_user = null;
  520.  
  521.         if ( ( (! is_user_logged_in() ) && (! empty( $wp_cassify_base_url ) ) ) || ( $gateway_mode == TRUE ) )  {
  522.             if (! $this->wp_cassify_is_in_while_list( $service_url ) ) {
  523.                 if ( empty( $service_url ) ) {
  524.                     die( 'CAS Service URL not set !' );
  525.                 }
  526.                 elseif ( empty( $service_ticket ) ) {
  527.  
  528.                     if ( parse_url( $service_url, PHP_URL_QUERY) ) {
  529.                         $service_url = WP_Cassify_Utils::wp_cassify_encode_query_in_url( $service_url );
  530.                     }
  531.  
  532.                     // Test if service url must be overriden by plugin options.
  533.                     $wp_cassify_override_service_url = WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_override_service_url' );
  534.  
  535.                     if (! empty( $wp_cassify_override_service_url ) ) {
  536.                         if ( strpos( $wp_cassify_override_service_url, "{WP_CASSIFY_CURRENT_SERVICE_URL}" ) !== FALSE ) {
  537.                             $wp_cassify_override_service_url = str_replace( "{WP_CASSIFY_CURRENT_SERVICE_URL}" , $service_url, $wp_cassify_override_service_url );
  538.                         }
  539.  
  540.                         $service_url = $wp_cassify_override_service_url;
  541.                     }
  542.  
  543.                     // Define custom plugin filter to build service url with your own value.
  544.                     if( has_filter( 'wp_cassify_redirect_service_url_filter' ) ) {
  545.                         $service_url = apply_filters( 'wp_cassify_redirect_service_url_filter', $service_url );
  546.                     }
  547.  
  548.                     $redirect_url = $wp_cassify_base_url .
  549.                         $wp_cassify_login_servlet . '?' .
  550.                         $this->wp_cassify_default_service_service_parameter_name . '=' . $service_url;
  551.  
  552.                     if ( $gateway_mode ) {
  553.                         $redirect_url .= '&gateway=true';
  554.                     }
  555.  
  556.                     WP_Cassify_Utils::wp_cassify_redirect_url( $redirect_url );
  557.                 }
  558.             }
  559.         }
  560.         else {
  561.             $current_user = wp_get_current_user();
  562.         }
  563.  
  564.         do_action( 'wp_cassify_after_redirect', $current_user->user_login );
  565.     }
  566.  
  567.     /**
  568.      * Enable support for auto-login (Gateway Mode).
  569.      */
  570.     public function wp_cassify_gateway_mode() {
  571.         $wp_cassify_enable_gateway_mode = WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_enable_gateway_mode' );
  572.         if ( $wp_cassify_enable_gateway_mode ) {
  573.             if ( ((! is_user_logged_in() ) || (! is_user_member_of_blog() ) ) && ( $_SESSION['wp_cassify'][get_current_blog_id()]['user_auth'] ) && (! get_query_var( 'wp_cassify_bypass' ) ) ) {
  574.                 if ( isset($GLOBALS['wp-cassify']) ) {
  575.                     $_SESSION['wp_cassify'][get_current_blog_id()]['user_auth'] = false;
  576.                     $GLOBALS['wp-cassify']->wp_cassify_check_authentication();
  577.                 }
  578.             }
  579.             else if ( (! is_user_logged_in() ) && (! get_query_var( 'wp_cassify_bypass' ) ) ){  
  580.                 if ( isset($GLOBALS['wp-cassify']) ) {
  581.                     $GLOBALS['wp-cassify']->wp_cassify_check_authentication();
  582.                 }
  583.             }
  584.             else if ( ! is_user_member_of_blog() ) {
  585.                 if ( isset($GLOBALS['wp-cassify']) ) {
  586.                     $GLOBALS['wp-cassify']->wp_cassify_check_authentication();
  587.                 }  
  588.             }
  589.         }
  590.     }
  591.  
  592.     /**
  593.      * Enable support for central logout (Single Sign Out).
  594.      */
  595.     public function wp_cassify_sso() {
  596.         $wp_cassify_enable_sso = WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_enable_sso' );
  597.         $wp_cassify_base_url = WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_base_url' );
  598.         if ( $wp_cassify_enable_sso ) {
  599.             if ( !empty($_POST['logoutRequest']) ) {
  600.                 $decoded_logout_rq = urldecode($_POST['logoutRequest']);
  601.                 $client_ip = $_SERVER['REMOTE_ADDR'];
  602.                 if ( $client_ip == gethostbyname(parse_url($wp_cassify_base_url, PHP_URL_HOST)) ) {
  603.                     preg_match(
  604.                     "|<samlp:SessionIndex>(.*)</samlp:SessionIndex>|",
  605.                     $decoded_logout_rq, $tick, PREG_OFFSET_CAPTURE, 3
  606.                     );
  607.                     $wrappedSamlSessionIndex = preg_replace(
  608.                     '|<samlp:SessionIndex>|', '', $tick[0][0]
  609.                     );
  610.                     $ticket2logout = preg_replace(
  611.                     '|</samlp:SessionIndex>|', '', $wrappedSamlSessionIndex
  612.                     );
  613.                     $ticket2logout = preg_replace('/[^a-zA-Z0-9\-]/', '', $ticket2logout);
  614.                     $cas_users = get_users( array( 'meta_key' => 'cas_service_ticket', 'meta_value' => $ticket2logout) );
  615.                     if ( !empty( $cas_users ) ) {
  616.                         foreach ( $cas_users as $cas_user ) {
  617.                             delete_user_meta( $cas_user->ID, 'cas_service_ticket' );
  618.                             wp_set_current_user ( $cas_user->ID );
  619.                             wp_destroy_all_sessions();
  620.                             exit();
  621.                         }
  622.                     }
  623.                 }
  624.             }
  625.         }
  626.     }
  627.  
  628.     /**
  629.      * Logout from CAS and Wordpress
  630.      */
  631.     function wp_cassify_logout() {
  632.  
  633.         $wp_cassify_logout_servlet = WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_logout_servlet' );
  634.         $wp_cassify_base_url = WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_base_url' );
  635.         $wp_cassify_redirect_url_after_logout = WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_redirect_url_after_logout' );
  636.  
  637.         // Define default values if options values empty.
  638.         if ( empty( $wp_cassify_logout_servlet ) ) {
  639.             $wp_cassify_logout_servlet = $this->wp_cassify_default_logout_servlet;
  640.         }
  641.  
  642.         if ( empty ( $wp_cassify_redirect_url_after_logout ) ) {
  643.             $wp_cassify_redirect_url_after_logout = get_home_url();
  644.         }
  645.  
  646.         // Send logout notification if rule is matched.
  647.         if ( isset( $_SESSION['wp_cassify'][ $this->wp_cassify_current_blog_id ]['wp_cassify_cas_user_datas'] ) ) {
  648.  
  649.             $cas_user_datas = $_SESSION['wp_cassify'][ $this->wp_cassify_current_blog_id ]['wp_cassify_cas_user_datas'];
  650.  
  651.             $wp_cassify_notification_rules = unserialize( WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_notification_rules' ) );
  652.  
  653.             $notification_rule_matched = $this->wp_cassify_notification_rule_matched(
  654.                 $cas_user_datas,
  655.                 $wp_cassify_notification_rules,
  656.                 'after_user_logout'
  657.             );
  658.  
  659.             if ( $notification_rule_matched ) {
  660.                 do_action( 'wp_cassify_send_notification', 'User account has been logged out :' . $cas_user_datas[ 'cas_user_id' ] );
  661.             }
  662.         }
  663.  
  664.         // Detect if user has been authenticated using CAS.
  665.         $authenticated_by_cas = $this->wp_cassify_is_authenticated();
  666.         if ( $authenticated_by_cas ) {
  667.             $cas_service_ticket = get_user_meta( wp_get_current_user()->ID, 'cas_service_ticket', true );
  668.             if ( !empty( $cas_service_ticket) ) {
  669.                 delete_user_meta( wp_get_current_user()->ID, 'cas_service_ticket' );
  670.             }
  671.         }
  672.  
  673.         // Destroy wordpress session;
  674.         session_destroy();
  675.  
  676.         if ( $authenticated_by_cas ) {
  677.             // Redirect to the logout CAS end point.
  678.             $redirect_url = $wp_cassify_base_url .
  679.                 $wp_cassify_logout_servlet . '?' .
  680.                 $this->wp_cassify_default_service_service_parameter_name . '=' . $wp_cassify_redirect_url_after_logout;
  681.         }
  682.         else {
  683.             // If user not authenticated by CAS redirect to home_url.
  684.             $redirect_url = home_url();
  685.         }
  686.  
  687.         WP_Cassify_Utils::wp_cassify_redirect_url( $redirect_url );
  688.     }
  689.  
  690.     /**
  691.      *  Get the service ticket from cas server request.
  692.      */
  693.     public function wp_cassify_get_service_ticket() {
  694.  
  695.         $wp_cassify_service_ticket = get_query_var( $this->wp_cassify_default_service_ticket_parameter_name );
  696.  
  697.         if ( empty( $wp_cassify_service_ticket ) ) {
  698.  
  699.             $current_url = WP_Cassify_Utils::wp_cassify_get_current_url(
  700.                     $this->wp_cassify_default_wordpress_blog_http_port,
  701.                     $this->wp_cassify_default_wordpress_blog_https_port
  702.                 );
  703.             $wp_cassify_service_ticket = WP_Cassify_Utils::wp_cassify_extract_get_parameter(
  704.                 rawurldecode( $current_url ),
  705.                 $this->wp_cassify_default_service_ticket_parameter_name );
  706.         }
  707.  
  708.         return $wp_cassify_service_ticket;
  709.     }
  710.  
  711.     /**
  712.      * Function used to detect if user has previously been authenticated by CAS.
  713.      * Performs redirection to cas server programmactically.
  714.      * This function is used throught hook wp_cassify_check_authentication.
  715.      *
  716.      * @result  bool    $auth           Return true if user has already authenticated by CAS Server.
  717.      */
  718.     public function wp_cassify_check_authentication() {
  719.  
  720.         $this->wp_cassify_session_start();
  721.  
  722.         $auth = false;
  723.  
  724.         if ( $this->wp_cassify_is_authenticated() ) {
  725.             $auth = true;
  726.         }
  727.         else if ( isset( $_SESSION['wp_cassify'][ $this->wp_cassify_current_blog_id ]['auth_checked'] ) ) {
  728.             // the previous request has redirected the client to the CAS server with gateway=true
  729.             unset( $_SESSION['wp_cassify'][ $this->wp_cassify_current_blog_id ]['auth_checked'] );
  730.  
  731.             $auth = false;
  732.         }
  733.         else {
  734.             // avoid a check against CAS on every request
  735.             if ( !isset( $_SESSION['wp_cassify'][ $this->wp_cassify_current_blog_id ]['unauth_count'] ) ) {
  736.                 $_SESSION['wp_cassify'][ $this->wp_cassify_current_blog_id ]['unauth_count'] = -2;
  737.             }
  738.  
  739.             if ( ( ( $_SESSION['wp_cassify'][ $this->wp_cassify_current_blog_id ]['unauth_count'] != -2 ) && ( $this->wp_cassify_get_cache_times_for_auth_recheck() == -1 ) ) ||
  740.                  ( $_SESSION['wp_cassify'][ $this->wp_cassify_current_blog_id ]['unauth_count'] >= 0  && $_SESSION['wp_cassify'][ $this->wp_cassify_current_blog_id ]['unauth_count'] < $this->wp_cassify_get_cache_times_for_auth_recheck() )
  741.             ) {
  742.                 $auth = false;
  743.  
  744.                 if ( $this->wp_cassify_get_cache_times_for_auth_recheck() != -1 ) {
  745.                     $_SESSION['wp_cassify'][ $this->wp_cassify_current_blog_id ]['unauth_count']++;;
  746.                 }
  747.             }
  748.             else {
  749.                 $_SESSION['wp_cassify'][ $this->wp_cassify_current_blog_id ]['unauth_count'] = 0;
  750.                 $_SESSION['wp_cassify'][ $this->wp_cassify_current_blog_id ]['auth_checked'] = true;
  751.  
  752.                 $this->wp_cassify_redirect( true );
  753.  
  754.                 // never reached
  755.                 $auth = false;
  756.             }
  757.         }
  758.  
  759.         return $auth;
  760.     }
  761.  
  762.     /**
  763.      * Populate cas user_id and selected attributes from CAS into session.
  764.      * @param array $cas_user_datas
  765.      * @param array $wp_cassify_attributes_list
  766.      */
  767.     public function wp_cassify_populate_attributes_into_session( $cas_user_datas, $wp_cassify_attributes_list ) {
  768.  
  769.         $cas_user_datas_filtered = array();
  770.  
  771.         // cas_user_id is populated by default.
  772.         $cas_user_datas_filtered[ 'cas_user_id' ] = $cas_user_datas[ 'cas_user_id' ];
  773.  
  774.         if (! empty( $wp_cassify_attributes_list ) ) {
  775.             $cas_user_attributes_names = explode( ',', $wp_cassify_attributes_list );
  776.  
  777.             if ( is_array( $cas_user_attributes_names ) ) {
  778.                 foreach( $cas_user_attributes_names as $cas_user_attributes_name ) {
  779.                     $cas_user_datas_filtered[ $cas_user_attributes_name ] = $cas_user_datas[ $cas_user_attributes_name ];
  780.                 }
  781.             }
  782.         }
  783.  
  784.         $this->wp_cassify_session_start();
  785.  
  786.         $_SESSION['wp_cassify'][ $this->wp_cassify_current_blog_id ]['wp_cassify_cas_user_datas'] = $cas_user_datas_filtered;
  787.     }
  788.  
  789.     /**
  790.      *  Get the service callback url from php session.
  791.      *  @return string
  792.      */
  793.     public function wp_cassify_get_service_callback_url() {
  794.  
  795.         $wp_cassify_callback_service_url = WP_Cassify_Utils::wp_cassify_get_current_url(
  796.                 $this->wp_cassify_default_wordpress_blog_http_port,
  797.                 $this->wp_cassify_default_wordpress_blog_https_port );
  798.  
  799.         // Check if request use gateway mode.
  800.         $gateway_mode = false;
  801.  
  802.         if ( $this->wp_cassify_is_gateway_request( $wp_cassify_callback_service_url ) ) {
  803.             $gateway_mode = true;
  804.         }
  805.  
  806.         $wp_cassify_redirect_parameter = WP_Cassify_Utils::wp_cassify_extract_get_parameter( $wp_cassify_callback_service_url , $this->wp_cassify_default_redirect_parameter_name );
  807.         $wp_cassify_service_ticket_parameter = WP_Cassify_Utils::wp_cassify_extract_get_parameter( $wp_cassify_callback_service_url , $this->wp_cassify_default_service_ticket_parameter_name );
  808.  
  809.         if ( !empty( $wp_cassify_redirect_parameter ) ) {
  810.             $wp_cassify_callback_service_url = WP_Cassify_Utils::wp_cassify_extract_get_parameter(
  811.                 $wp_cassify_callback_service_url,
  812.                 $this->wp_cassify_default_redirect_parameter_name
  813.             );
  814.  
  815.             // Append home_url if url contains only /my-slug-page/. callback service url must be fully qualified.
  816.             if ( strrpos( $wp_cassify_callback_service_url, '/', -strlen( $wp_cassify_callback_service_url ) ) !== false ) {
  817.                 $wp_cassify_callback_service_url = WP_Cassify_Utils::wp_cassify_get_host_uri( home_url() ) .
  818.                     $wp_cassify_callback_service_url;
  819.             }
  820.         }
  821.         else if ( !empty( $wp_cassify_service_ticket_parameter ) ) {
  822.             $wp_cassify_callback_service_url = WP_Cassify_Utils::wp_cassify_strip_get_parameter( $wp_cassify_callback_service_url , $this->wp_cassify_default_service_ticket_parameter_name );
  823.  
  824.             // Append home_url if url contains only /my-slug-page/. callback service url must be fully qualified.
  825.             if ( strrpos( $wp_cassify_callback_service_url, '/', -strlen( $wp_cassify_callback_service_url ) ) !== false ) {
  826.                 $wp_cassify_callback_service_url = WP_Cassify_Utils::wp_cassify_get_host_uri( home_url() ) .
  827.                     $wp_cassify_callback_service_url;
  828.             }
  829.         }
  830.         else {
  831.         $wp_cassify_callback_service_url = home_url() . '/';
  832.         }
  833.  
  834.         if ( $gateway_mode ) {
  835.             $wp_cassify_callback_service_url .= '%3Fgateway=true';
  836.         }
  837.  
  838.         // Fix bug : slug is duplicate in return url
  839.         if( is_multisite() ) {
  840.             $blog_details = get_blog_details();
  841.             $blog_path = ltrim( $blog_details->path , '/');
  842.             $duplicate_path_to_remove = rtrim( $blog_path . $blog_path, '/' );
  843.             $duplicate_path_to_replace = rtrim( $blog_path, '/' );
  844.  
  845.             if ( ! empty( $duplicate_path_to_remove ) && strpos( $wp_cassify_callback_service_url, $duplicate_path_to_remove ) !== false ) {
  846.                 $wp_cassify_callback_service_url = str_replace( $duplicate_path_to_remove, $duplicate_path_to_replace , $wp_cassify_callback_service_url );
  847.             }
  848.         }
  849.         // End Fix bug
  850.  
  851.         return $wp_cassify_callback_service_url;
  852.     }
  853.  
  854.     /**
  855.      * Logout from CAS and Wordpress
  856.      * @param string $wp_cassify_error_code Error code passing by GET to display custom error messages after logout.
  857.      */
  858.     private function wp_cassify_logout_if_not_allowed( $wp_cassify_error_code = '' ) {
  859.  
  860.         $wp_cassify_logout_servlet = WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_logout_servlet' );
  861.         $wp_cassify_base_url = WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_base_url' );
  862.         $wp_cassify_redirect_url_if_not_allowed = WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_redirect_url_if_not_allowed' );
  863.  
  864.         // Define default values if options values empty.
  865.         if ( empty( $wp_cassify_logout_servlet ) ) {
  866.             $wp_cassify_logout_servlet = $this->wp_cassify_default_logout_servlet;
  867.         }
  868.  
  869.         if ( empty ( $wp_cassify_redirect_url_if_not_allowed ) ) {
  870.             $wp_cassify_redirect_url_if_not_allowed = get_home_url();
  871.  
  872.             if (! empty( $wp_cassify_error_code ) ) {
  873.                 $wp_cassify_redirect_url_if_not_allowed .= '%3F' . 'wp-cassify-message=' . $wp_cassify_error_code;
  874.             }
  875.         }
  876.  
  877.         // Destroy wordpress session;
  878.         session_destroy();
  879.  
  880.         $redirect_url = $wp_cassify_base_url .
  881.             $wp_cassify_logout_servlet . '?' .
  882.             $this->wp_cassify_default_service_service_parameter_name . '=' . $wp_cassify_redirect_url_if_not_allowed;
  883.  
  884.         // Redirect to the logout CAS end point.
  885.         WP_Cassify_Utils::wp_cassify_redirect_url( $redirect_url );
  886.     }
  887.  
  888.     /**
  889.      * Parse the CAS Server response
  890.      * @param string $cas_server_xml_response   Xml response stream sent by CAS Server.
  891.      * @return array $cas_user_datas
  892.      */
  893.      private function wp_cassify_parse_xml_response( $cas_server_xml_response ) {
  894.  
  895.         $wp_cassify_xpath_query_to_extact_cas_user = WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_xpath_query_to_extact_cas_user' );
  896.         $wp_cassify_xpath_query_to_extact_cas_attributes = WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_xpath_query_to_extact_cas_attributes' );
  897.  
  898.         // Define default values if options values empty.
  899.         if ( empty( $wp_cassify_xpath_query_to_extact_cas_user ) ) {
  900.             $wp_cassify_xpath_query_to_extact_cas_user = $this->wp_cassify_default_xpath_query_to_extact_cas_user;
  901.         }
  902.  
  903.         if ( empty( $wp_cassify_xpath_query_to_extact_cas_attributes ) ) {
  904.             $wp_cassify_xpath_query_to_extact_cas_attributes = $this->wp_cassify_default_xpath_query_to_extact_cas_attributes;
  905.         }
  906.  
  907.         $cas_user_datas = array();
  908.  
  909.         $cas_user_datas_xml = new \DomDocument();
  910.         $cas_user_datas_xml->loadXML( $cas_server_xml_response );
  911.  
  912.         $xpath = new \DOMXPath( $cas_user_datas_xml );
  913.  
  914.         // Extract cas user_id
  915.         $query_cas_user = $wp_cassify_xpath_query_to_extact_cas_user;
  916.         $cas_user_entries = $xpath->query( $query_cas_user );
  917.  
  918.         foreach ( $cas_user_entries as $cas_user_entry ) {
  919.             $cas_user_datas[ 'cas_user_id' ] = $cas_user_entry->nodeValue;
  920.         }
  921.  
  922.         // Extract attributes
  923.         $query_cas_attributes = $wp_cassify_xpath_query_to_extact_cas_attributes;
  924.         $cas_user_attributes = $xpath->query( $query_cas_attributes );
  925.  
  926.         if ( $cas_user_attributes->length > 0 ) {
  927.             $cas_user_attributes_items = $cas_user_attributes->item( 0 );
  928.             foreach ( $cas_user_attributes_items->childNodes as $cas_user_attributes_item ) {
  929.                 $cas_attribute_name = preg_replace( '#^cas:#', '', $cas_user_attributes_item->nodeName );
  930.  
  931.                 // Process multivaluate fields. Test if array_key already exist.
  932.                 if ( array_key_exists( $cas_attribute_name, $cas_user_datas ) ) {
  933.                     if ( isset( $cas_user_datas[ $cas_attribute_name ] ) ) {
  934.                         if ( is_array( $cas_user_datas[ $cas_attribute_name ] ) ) {
  935.                             array_push( $cas_user_datas[ $cas_attribute_name ], $cas_user_attributes_item->nodeValue );
  936.                         }
  937.                         else {
  938.                             $cas_attribute_value = $cas_user_datas[ $cas_attribute_name ];
  939.                             $cas_user_datas[ $cas_attribute_name ] = array();
  940.  
  941.                             array_push( $cas_user_datas[ $cas_attribute_name ], $cas_attribute_value );
  942.                             array_push( $cas_user_datas[ $cas_attribute_name ], $cas_user_attributes_item->nodeValue );
  943.                         }
  944.                     }
  945.                 }
  946.                 else {
  947.                     // Process single valuate field.
  948.                     $cas_user_datas[ $cas_attribute_name ] = $cas_user_attributes_item->nodeValue;
  949.                 }
  950.             }
  951.         }
  952.  
  953.         return $cas_user_datas;
  954.     }
  955.  
  956.     /**
  957.      * Check if request use gateway mode.
  958.      * @param   string  $callback_service_url   Url used by CAS server to return to service.
  959.      * @return  bool    $is_gateway_request     Return true if request to CAS Server is made in gateway mode (eg : ?gateway=true)
  960.      */
  961.     private function wp_cassify_is_gateway_request( $callback_service_url ) {
  962.  
  963.         $is_gateway_request = false;
  964.  
  965.         // Test current url if callback_service_url is not set.
  966.         if ( empty( $callback_service_url ) ) {
  967.             $callback_service_url = WP_Cassify_Utils::wp_cassify_get_current_url(
  968.                 $this->wp_cassify_default_wordpress_blog_http_port,
  969.                 $this->wp_cassify_default_wordpress_blog_https_port
  970.             );
  971.         }
  972.  
  973.         $gateway = WP_Cassify_Utils::wp_cassify_extract_get_parameter(
  974.                 rawurldecode( $callback_service_url ),
  975.                 $this->wp_cassify_default_gateway_parameter_name );
  976.  
  977.         if (! empty( $gateway ) ) {
  978.             $is_gateway_request = true;
  979.         }
  980.  
  981.         return $is_gateway_request;
  982.     }
  983.  
  984.     /**
  985.      * Store rules in two array according to her type (ALLOW or DENY).
  986.      * @param array $wp_cassify_autorization_rules
  987.      */
  988.     private function wp_cassify_separate_rules( $wp_cassify_autorization_rules ) {
  989.  
  990.         foreach ( $wp_cassify_autorization_rules as $wp_cassify_autorization_rule ) {
  991.             $wp_cassify_rule_parts = explode( '|', $wp_cassify_autorization_rule );
  992.             $wp_cassify_rule_type = $wp_cassify_rule_parts[ 0 ];
  993.             $wp_cassify_rule_expression = $wp_cassify_rule_parts[ 1 ];
  994.  
  995.             if ( $wp_cassify_rule_type == 'ALLOW' ) {
  996.                 array_push( $this->wp_cassify_allow_rules, $wp_cassify_rule_expression );
  997.             }
  998.  
  999.             if ( $wp_cassify_rule_type == 'DENY' ) {
  1000.                 array_push( $this->wp_cassify_deny_rules, $wp_cassify_rule_expression );
  1001.             }
  1002.         }
  1003.     }
  1004.  
  1005.     /**
  1006.      * Test if this URL must be bypassed by CAS Authentication
  1007.      * @return bool $wp_cassify_bypass
  1008.      */
  1009.      private function wp_cassify_bypass() {
  1010.  
  1011.         $wp_cassify_bypass = false;
  1012.  
  1013.         $wp_cassify_bypass_by_referrer = '';
  1014.         $wp_cassify_bypass_by_post = '';
  1015.         $wp_cassify_bypass_by_get = '';
  1016.  
  1017.         $wp_cassify_disable_authentication = '';
  1018.  
  1019.         // 1- Check if bypass GET URL parameter is set from the Referrer.
  1020.         if (! empty( $_SERVER['HTTP_REFERER'] ) ) {
  1021.             $wp_cassify_bypass_by_referrer = WP_Cassify_Utils::wp_cassify_extract_get_parameter( $_SERVER['HTTP_REFERER'], $this->wp_cassify_default_bypass_parameter_name );
  1022.         }
  1023.  
  1024.         // 2- Check if bypass parameter is send by POST
  1025.         if (! empty( $_POST['redirect_to'] ) ) {
  1026.             $wp_cassify_bypass_by_post = WP_Cassify_Utils::wp_cassify_extract_get_parameter( $_POST['redirect_to'], $this->wp_cassify_default_bypass_parameter_name );
  1027.         }
  1028.  
  1029.         // 3- Or check if bypass has been defined in admin panel.
  1030.         $wp_cassify_disable_authentication = WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_disable_authentication' );
  1031.  
  1032.         // 4- Check $_GET['wp_cassify_bypass'] value
  1033.         if ( isset( $_GET[ $this->wp_cassify_default_bypass_parameter_name ] ) ) {
  1034.             // Can't use get_query_var function because 'query_vars' filter has not yet fired. I use $_GET instead.
  1035.             $wp_cassify_bypass_by_get = $_GET[ $this->wp_cassify_default_bypass_parameter_name ];
  1036.         }
  1037.  
  1038.         if ( ( $wp_cassify_bypass_by_referrer == 'bypass' ) || ( $wp_cassify_bypass_by_post == 'bypass' ) || ( $wp_cassify_bypass_by_get == 'bypass' ) || ( $wp_cassify_disable_authentication == 'disabled' ) ) {
  1039.             $wp_cassify_bypass = true;
  1040.         }
  1041.  
  1042.         return $wp_cassify_bypass;
  1043.      }
  1044.  
  1045.     /**
  1046.      * Check if url is in white list and don't be authenticated by CAS.
  1047.      * @param   string  $url                Url of to test
  1048.      * @return  bool    $is_in_while_list   Return true if user can acess this url without to be authenticated by CAS.
  1049.      */
  1050.      private function wp_cassify_is_in_while_list( $url ) {
  1051.  
  1052.          $is_in_while_list = false;
  1053.  
  1054.          $wp_cassify_redirect_url_white_list = WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_redirect_url_white_list' );
  1055.          $white_list_urls = explode( ';', $wp_cassify_redirect_url_white_list );
  1056.  
  1057.          if ( ( is_array( $white_list_urls ) ) && ( count( $white_list_urls ) > 0 ) ){
  1058.             foreach( $white_list_urls as $white_url ) {
  1059.                 if ( strrpos( $url, $white_url, -strlen( $url ) ) !== false ) {
  1060.                     $is_in_while_list = true;
  1061.                 }
  1062.             }
  1063.          }
  1064.  
  1065.          return $is_in_while_list;
  1066.      }
  1067.  
  1068.     /**
  1069.      * Check if user is allow to connect according to autorization rules.
  1070.      * @param array $cas_user_datas                 Associative array containing CAS userID and attributes
  1071.      * @param string $wp_cassify_allow_deny_order   Order to process authorization rules.
  1072.      * @return bool $is_user_allowed                Return true if user is allowed to connect. Return false on the other hand.
  1073.      */
  1074.     private function wp_cassify_is_user_allowed( $cas_user_datas = array(), $wp_cassify_allow_deny_order ) {
  1075.  
  1076.         $is_user_allowed = false;
  1077.         $rule_check = false;
  1078.  
  1079.         $solver = new \wp_cassify\wp_cassify_rule_solver();
  1080.  
  1081.         $solver->match_first_level_parenthesis_group_pattern = $this->wp_cassify_match_first_level_parenthesis_group_pattern;
  1082.         $solver->match_second_level_parenthesis_group_pattern = $this->wp_cassify_match_second_level_parenthesis_group_pattern;
  1083.         $solver->match_cas_variable_pattern = $this->wp_cassify_match_cas_variable_pattern;
  1084.         $solver->allowed_operators = $this->wp_cassify_allowed_operators;
  1085.         $solver->operator_prefix = $this->wp_cassify_operator_prefix;
  1086.         $solver->allowed_parenthesis = $this->wp_cassify_allowed_parenthesis;
  1087.         $solver->error_messages = $this->wp_cassify_error_messages;
  1088.         $solver->cas_user_datas = $cas_user_datas;
  1089.  
  1090.         // Check Allow rules first
  1091.         if ( $wp_cassify_allow_deny_order == 'allow, deny' ) {
  1092.             if ( ( is_array( $this->wp_cassify_allow_rules ) ) && ( count( $this->wp_cassify_allow_rules ) > 0 ) ) {
  1093.                 foreach ( $this->wp_cassify_allow_rules as $rule ) {
  1094.                     if (! $rule_check ) {
  1095.                         $rule_check = $solver->solve( $rule );
  1096.  
  1097.                         if ( $rule_check ) {
  1098.                             $is_user_allowed = true;
  1099.                         }
  1100.                     }
  1101.                 }
  1102.             }
  1103.  
  1104.             if ( ( is_array( $this->wp_cassify_deny_rules ) ) && ( count( $this->wp_cassify_deny_rules ) > 0 ) ) {
  1105.                 foreach ( $this->wp_cassify_deny_rules as $rule ) {
  1106.                     if (! $rule_check ) {
  1107.                         $rule_check = $solver->solve( $rule );
  1108.  
  1109.                         if ( $rule_check ) {
  1110.                             $is_user_allowed = false;
  1111.                         }
  1112.                     }
  1113.                 }
  1114.             }
  1115.         }
  1116.         else { // Check Deny Rules first
  1117.             if ( ( is_array( $this->wp_cassify_deny_rules ) ) && ( count( $this->wp_cassify_deny_rules ) > 0 ) ) {
  1118.                 foreach ( $this->wp_cassify_deny_rules as $rule ) {
  1119.                     if (! $rule_check ) {
  1120.                         $rule_check = $solver->solve( $rule );
  1121.  
  1122.                         if ( $rule_check ) {
  1123.                             $is_user_allowed = false;
  1124.                         }
  1125.                     }
  1126.                 }
  1127.             }
  1128.  
  1129.             if ( ( is_array( $this->wp_cassify_allow_rules ) ) && ( count( $this->wp_cassify_allow_rules ) > 0 ) ) {
  1130.                 foreach ( $this->wp_cassify_allow_rules as $rule ) {
  1131.                     if (! $rule_check ) {
  1132.                         $rule_check = $solver->solve( $rule );
  1133.  
  1134.                         if ( $rule_check ) {
  1135.                             $is_user_allowed = true;
  1136.                         }
  1137.                     }
  1138.                 }
  1139.             }
  1140.         }
  1141.  
  1142.         return $is_user_allowed;
  1143.     }
  1144.  
  1145.     /**
  1146.      * Check if user is matched by Conditionnal Rule
  1147.      * @param   array   $cas_user_datas     Associative array containing CAS userID and attributes
  1148.      * @param   string  $wp_cassify_rule    WP Cassify rule
  1149.      * @return  bool    $rule_matched       Return true if WP Cassify rule assertion is verified. Return false on the other hand.
  1150.      */
  1151.     private function wp_cassify_rule_matched( $cas_user_datas = array(), $wp_cassify_rule ) {
  1152.  
  1153.         $rule_matched = false;
  1154.  
  1155.         $solver = new \wp_cassify\wp_cassify_rule_solver();
  1156.  
  1157.         $solver->match_first_level_parenthesis_group_pattern = $this->wp_cassify_match_first_level_parenthesis_group_pattern;
  1158.         $solver->match_second_level_parenthesis_group_pattern = $this->wp_cassify_match_second_level_parenthesis_group_pattern;
  1159.         $solver->match_cas_variable_pattern = $this->wp_cassify_match_cas_variable_pattern;
  1160.         $solver->allowed_operators = $this->wp_cassify_allowed_operators;
  1161.         $solver->operator_prefix = $this->wp_cassify_operator_prefix;
  1162.         $solver->allowed_parenthesis = $this->wp_cassify_allowed_parenthesis;
  1163.         $solver->error_messages = $this->wp_cassify_error_messages;
  1164.         $solver->cas_user_datas = $cas_user_datas;
  1165.  
  1166.         $rule_matched = $solver->solve( $wp_cassify_rule );
  1167.  
  1168.         return $rule_matched;
  1169.     }
  1170.  
  1171.     /**
  1172.      * Test if user account has expired.
  1173.      * @param   array   $cas_user_datas                         Associative array containing CAS userID and attributes
  1174.      * @param   array   $expiration_rules           Array of WP Cassify user account expiration rules
  1175.      * @result  bool    $is_user_account_expired        Return true if user account has expired. Return false on the other hand.
  1176.      */
  1177.     private function wp_cassify_is_user_account_expired( $cas_user_datas, $expiration_rules = array() ) {
  1178.  
  1179.         $is_user_account_expired = false;
  1180.  
  1181.         foreach ( $expiration_rules as $expiration_rule ) {
  1182.  
  1183.             $expiration_rule_parts = explode( '|', $expiration_rule );
  1184.  
  1185.             if ( ( is_array( $expiration_rule_parts ) ) && ( count( $expiration_rule_parts ) == 3 ) ) {
  1186.                 $expiration_rule_type = $expiration_rule_parts[0];
  1187.                 $expiration_rule_type_value = $expiration_rule_parts[1];
  1188.                 $expiration_rule_value = stripslashes( $expiration_rule_parts[2] );
  1189.  
  1190.                 if ( $this->wp_cassify_rule_matched( $cas_user_datas, $expiration_rule_value ) ) {
  1191.  
  1192.                     switch( $expiration_rule_type ) {
  1193.                         case 'after_user_account_created_time_limit' :
  1194.                             $current_user = get_user_by( 'login', $cas_user_datas[ 'cas_user_id' ] );
  1195.                             $expiration_date = new \DateTime( $current_user->user_registered );
  1196.  
  1197.                             // Add expiration delay (in days) from user account registered date
  1198.                             $expiration_date->add( new \DateInterval( 'P'. $expiration_rule_type_value . 'D' ) );
  1199.                             break;
  1200.  
  1201.                         case 'fixed_datetime_limit' :
  1202.                             $expiration_date = new \DateTime( $expiration_rule_type_value );
  1203.                             break;
  1204.                     }
  1205.  
  1206.                     $now = new \DateTime( 'now' );
  1207.  
  1208.                     if ( $expiration_date < $now ) {
  1209.                         $is_user_account_expired = true;
  1210.                     }
  1211.                 }
  1212.             }
  1213.         }
  1214.  
  1215.         return $is_user_account_expired;
  1216.     }
  1217.  
  1218.     /**
  1219.      * Check if user is matched by Notification Rule
  1220.      * @param array     $cas_user_datas     Associative array containing CAS userID and attributes
  1221.      * @param array     $role_rules         Array containing all role rules
  1222.      * @param bool      $network_activated  True if plugin is activated over the network
  1223.      * @param int       $current_blog_id    The id of the current blog
  1224.      * @return array    $roles_to_push      Array containing roles to push to user
  1225.      */
  1226.     private function wp_cassify_get_roles_to_push( $cas_user_datas = array(), $role_rules = array(), $network_activated = false, $current_blog_id ) {
  1227.  
  1228.         $roles_to_push = array();
  1229.  
  1230.         if ( ( is_array( $role_rules ) ) && ( count( $role_rules ) > 0 ) ) {
  1231.             foreach ( $role_rules as $role_rule ) {
  1232.                 $role_rule_parts = explode( '|', $role_rule );
  1233.  
  1234.                 if ( $network_activated ) {
  1235.                     if ( ( is_array( $role_rule_parts ) ) && ( count( $role_rule_parts ) == 3 ) ) {
  1236.                         $role_rule_key = $role_rule_parts[0];
  1237.                         // Determine scope of the rule if network activated
  1238.                         $role_rule_blog_id = $role_rule_parts[1];
  1239.                         $role_rule_expression = stripslashes( $role_rule_parts[2] );
  1240.  
  1241.                         // role_rule_blog_id == 0 match "ALL BLOGS"
  1242.                         if ( ( $role_rule_blog_id == $current_blog_id ) || ( $role_rule_blog_id == 0 ) ) {
  1243.                             if ( $this->wp_cassify_rule_matched( $cas_user_datas, $role_rule_expression ) ) {
  1244.                                 array_push( $roles_to_push, $role_rule_key );
  1245.                             }
  1246.                         }
  1247.                     }
  1248.                 }
  1249.                 else {
  1250.                     if ( ( is_array( $role_rule_parts ) ) && ( count( $role_rule_parts ) == 2 ) ) {
  1251.                         $role_rule_key = $role_rule_parts[0];
  1252.                         $role_rule_expression = stripslashes( $role_rule_parts[1] );
  1253.  
  1254.                         if ( $this->wp_cassify_rule_matched( $cas_user_datas, $role_rule_expression ) ) {
  1255.                             array_push( $roles_to_push, $role_rule_key );
  1256.                         }
  1257.                     }
  1258.                 }
  1259.             }
  1260.         }
  1261.  
  1262.         return $roles_to_push;
  1263.     }
  1264.  
  1265.     /**
  1266.      * Check if user is matched by Notification Rule
  1267.      * @param   array   $cas_user_datas             Associative array containing CAS userID and attributes
  1268.      * @param   array   $notification_rules         Array containing all notification rules
  1269.      * @param   array   $trigger_name               The name of the action wich fire the notification
  1270.      * @return  boolean $notification_rule_matched  Return true if notification rule assertion is verified. Return false on the other hand.
  1271.      */
  1272.     private function wp_cassify_notification_rule_matched( $cas_user_datas = array(), $notification_rules = array(), $trigger_name ) {
  1273.  
  1274.         $notification_rule_matched = false;
  1275.  
  1276.         if ( ( is_array( $notification_rules ) ) && ( count( $notification_rules ) > 0 ) ) {
  1277.             foreach ( $notification_rules as $notification_rule ) {
  1278.                 $notification_rule_parts = explode( '|', $notification_rule );
  1279.  
  1280.                 if ( ( is_array( $notification_rule_parts ) ) && ( count( $notification_rule_parts ) == 2 ) ) {
  1281.                     $notification_rule_key = $notification_rule_parts[0];
  1282.                     $notification_rule_expression = stripslashes( $notification_rule_parts[1] );
  1283.  
  1284.                     if ( $notification_rule_key == $trigger_name ) {
  1285.                         if ( $this->wp_cassify_rule_matched( $cas_user_datas, $notification_rule_expression ) ) {
  1286.                             $notification_rule_matched = true;
  1287.                         }
  1288.                     }
  1289.                 }
  1290.             }
  1291.         }
  1292.  
  1293.         return $notification_rule_matched;
  1294.     }
  1295.  
  1296.     /**
  1297.      * Synchronize CAS User attributes values with Wordpress User metadatas. Create custom user_meta if not exist.
  1298.      * @param   string  $cas_user_id                                CAS userID
  1299.      * @param   array   $cas_user_datas                             Associative array containing CAS userID and attributes
  1300.      * @param   array   $wp_cassify_user_attributes_mapping_list    Array containing mapping between CAS user attributes and Wordpress user attributes
  1301.      */
  1302.     private function wp_cassify_sync_user_metadata( $cas_user_id, $cas_user_datas = array(), $wp_cassify_user_attributes_mapping_list = array() ) {
  1303.  
  1304.         if ( ( is_array( $wp_cassify_user_attributes_mapping_list ) ) && ( count( $wp_cassify_user_attributes_mapping_list ) > 0 ) ) {
  1305.             $wp_user = get_user_by( 'login', $cas_user_id );
  1306.  
  1307.             if ( $wp_user != false  ) {
  1308.                 foreach( $wp_cassify_user_attributes_mapping_list as $wp_cassify_user_attributes_mapping ) {
  1309.                     $wp_cassify_user_attributes_mapping_parts = explode( '|', $wp_cassify_user_attributes_mapping );
  1310.  
  1311.                     $wp_cassify_wordpress_user_meta = $wp_cassify_user_attributes_mapping_parts[ '0' ];
  1312.                     $wp_cassify_cas_user_attribute = $wp_cassify_user_attributes_mapping_parts[ '1' ];
  1313.  
  1314.                     $cas_user_data_formatted = null;
  1315.  
  1316.                     if ( is_array( $cas_user_datas[ $wp_cassify_cas_user_attribute ] ) ) {
  1317.                         $cas_user_data_formatted = maybe_serialize( $cas_user_datas[ $wp_cassify_cas_user_attribute ] );
  1318.                     }
  1319.                     else {
  1320.                         $cas_user_data_formatted = $cas_user_datas[ $wp_cassify_cas_user_attribute ];
  1321.                     }
  1322.  
  1323.                     $mapping_set = false;
  1324.  
  1325.                     if ( property_exists( $wp_user->data, $wp_cassify_wordpress_user_meta ) ) {
  1326.  
  1327.                         $user_id = wp_update_user(
  1328.                             array(
  1329.                                 'ID' => $wp_user->data->ID,
  1330.                                 $wp_cassify_wordpress_user_meta => $cas_user_data_formatted
  1331.                             )
  1332.                         );
  1333.  
  1334.                         $mapping_set = true;
  1335.                     }
  1336.  
  1337.                     if (! $mapping_set ) {
  1338.  
  1339.                         $wp_cassify_wordpress_user_meta_value = get_user_meta( $wp_user->ID, $wp_cassify_wordpress_user_meta );
  1340.  
  1341.                         if ( empty( $wp_cassify_wordpress_user_meta_value ) ) {
  1342.                             add_user_meta(
  1343.                                 $wp_user->ID,
  1344.                                 $wp_cassify_wordpress_user_meta,
  1345.                                 $cas_user_data_formatted,
  1346.                                 true
  1347.                             );
  1348.                         }
  1349.                         else {
  1350.                             update_user_meta(
  1351.                                 $wp_user->ID,
  1352.                                 $wp_cassify_wordpress_user_meta,
  1353.                                 $cas_user_data_formatted
  1354.                             );
  1355.                         }
  1356.                     }
  1357.                 }
  1358.             }
  1359.         }
  1360.     }
  1361.  
  1362.     /**
  1363.      * Send email notification
  1364.      * @param string $message       Body of email notification message.
  1365.      * @result boole $send_result   Return true if message is sent successfully. Return false on the other hand.
  1366.      */
  1367.     public function wp_cassify_send_notification_message( $message ) {
  1368.  
  1369.         $wp_cassify_notifications_smtp_to = esc_attr( WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_notifications_smtp_to' ) );
  1370.         $wp_cassify_notifications_subject_prefix = esc_attr( WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_notifications_subject_prefix' ) );
  1371.  
  1372.         if ( empty( $message ) ) {
  1373.             $wp_cassify_send_notification_subject = $this->wp_cassify_default_notifications_options[ 'wp_cassify_default_notifications_subject' ];
  1374.             $wp_cassify_send_notification_message = $this->wp_cassify_default_notifications_options[ 'wp_cassify_default_notifications_message' ];
  1375.         }
  1376.         else {
  1377.             $wp_cassify_send_notification_subject = $message;
  1378.             $wp_cassify_send_notification_message = $message;
  1379.         }
  1380.  
  1381.         if ( empty( $wp_cassify_notifications_subject_prefix ) ) {
  1382.             $wp_cassify_send_notification_subject = $this->wp_cassify_default_notifications_options[ 'wp_cassify_default_notifications_subject_prefix' ] . $wp_cassify_send_notification_subject;
  1383.         }
  1384.         else {
  1385.             $wp_cassify_send_notification_subject = $wp_cassify_notifications_subject_prefix  . $wp_cassify_send_notification_subject;
  1386.         }
  1387.  
  1388.         $wp_cassify_notifications_smtp_auth = esc_attr( WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_notifications_smtp_auth' ) );
  1389.  
  1390.         $wp_cassify_notifications_smtp_auth_enabled = false;
  1391.         $wp_cassify_notifications_encryption_type = null;
  1392.  
  1393.         if ( $wp_cassify_notifications_smtp_auth == 'enabled' ) {
  1394.             $wp_cassify_notifications_smtp_auth_enabled = true;
  1395.             $wp_cassify_notifications_encryption_type = esc_attr( WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_notifications_encryption_type' ) );
  1396.         }
  1397.  
  1398.         $wp_cassify_notifications_priority = esc_attr( WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_notifications_priority' ) );
  1399.  
  1400.         $wp_cassify_notifications_smtp_user = esc_attr( WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_notifications_smtp_user' ) );
  1401.         $wp_cassify_notifications_smtp_password = esc_attr( WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_notifications_smtp_password' ) );
  1402.  
  1403.         $wp_cassify_notifications_salt = esc_attr( WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_notifications_salt' ) );
  1404.  
  1405.         if (! empty( $wp_cassify_notifications_salt ) ) {
  1406.             $wp_cassify_notifications_smtp_password = WP_Cassify_Utils::wp_cassify_simple_decrypt(
  1407.                 $wp_cassify_notifications_smtp_password,
  1408.                 $wp_cassify_notifications_salt
  1409.             );
  1410.         }
  1411.         else {
  1412.             $wp_cassify_notifications_smtp_password = WP_Cassify_Utils::wp_cassify_simple_decrypt(
  1413.                 $wp_cassify_notifications_smtp_password
  1414.             );
  1415.         }
  1416.  
  1417.         $send_result = WP_Cassify_Utils::wp_cassify_sendmail(
  1418.             esc_attr( WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_notifications_smtp_from' ) ),
  1419.             $wp_cassify_notifications_smtp_to,
  1420.             $wp_cassify_send_notification_subject,
  1421.             $wp_cassify_send_notification_message,
  1422.             $wp_cassify_notifications_priority,
  1423.             esc_attr( WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_notifications_smtp_host' ) ),
  1424.             esc_attr( WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_notifications_smtp_port' ) ),
  1425.             $wp_cassify_notifications_smtp_auth_enabled,
  1426.             $wp_cassify_notifications_encryption_type,
  1427.             $wp_cassify_notifications_smtp_user,
  1428.             $wp_cassify_notifications_smtp_password
  1429.         );
  1430.  
  1431.         return $send_result;
  1432.     }
  1433. }
  1434. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement