PluginProbe ʕ •ᴥ•ʔ
Firebase Authentication / trunk
Firebase Authentication vtrunk
1.7.1 1.7.0 trunk 1.0.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.8 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9
firebase-authentication / firebase-authentication.php
firebase-authentication Last commit date
admin 3 weeks ago includes 2 months ago languages 6 years ago public 2 months ago views 2 months ago README.txt 4 days ago class-mo-firebase-config.php 3 weeks ago class-mo-firebase-contact-us.php 3 years ago firebase-authentication.php 4 days ago index.php 3 years ago uninstall.php 3 years ago
firebase-authentication.php
693 lines
1 <?php //phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase -- Not changing file name because this is the main plugin file, and changing this would lead to deacivation of plugin for the active users.
2 /**
3 * Initial File for Firebase Authentication plugin.
4 *
5 * @link https://miniorange.com
6 * @since 1.0.0
7 * @package Firebase_Authentication
8 *
9 * @wordpress-plugin
10 * Plugin Name: Firebase Authentication
11 * Plugin URI: https://wordpress.org/plugins/firebase-authentication/
12 * Description: This plugin allows login into WordPress using Firebase as Identity provider.
13 * Version: 1.7.2
14 * Author: miniOrange
15 * Author URI: https://miniorange.com
16 * License: Expat
17 * License URI: https://plugins.miniorange.com/mit-license
18 */
19
20 // If this file is called directly, abort.
21 if ( ! defined( 'WPINC' ) ) {
22 die;
23 }
24
25 /**
26 * Currently plugin version.
27 * Start at version 1.0.0 and use SemVer - https://semver.org
28 * Rename this for your plugin and update it as you release new versions.
29 */
30 define( 'MO_FIREBASE_AUTHENTICATION_VERSION', '1.7.2' );
31 define( 'MO_FIREBASE_AUTHENTICATION_DIR', plugin_dir_path( __FILE__ ) );
32 define( 'MO_FIREBASE_AUTHENTICATION_URL', plugin_dir_url( __FILE__ ) );
33
34 /**
35 * The code that runs during plugin deactivation.
36 * This action is documented in includes/class-mo-firebase-authentication-deactivator.php
37 */
38 function mo_firebase_deactivate_firebase_authentication() {
39 require_once MO_FIREBASE_AUTHENTICATION_DIR . 'includes' . DIRECTORY_SEPARATOR . 'class-mo-firebase-authentication-deactivator.php';
40 MO_Firebase_Authentication_Deactivator::deactivate();
41 }
42
43 register_deactivation_hook( __FILE__, 'mo_firebase_deactivate_firebase_authentication' );
44
45 /**
46 * The core plugin class that is used to define internationalization,
47 * admin-specific hooks, and public-facing site hooks.
48 */
49 require MO_FIREBASE_AUTHENTICATION_DIR . 'includes' . DIRECTORY_SEPARATOR . 'class-mo-firebase-authentication.php';
50 require_once 'class-mo-firebase-config.php';
51 require 'views' . DIRECTORY_SEPARATOR . 'feedback-form.php';
52 require 'class-mo-firebase-contact-us.php';
53 require 'admin' . DIRECTORY_SEPARATOR . 'class-mo-firebase-customer.php';
54
55
56 /**
57 * Begins execution of the plugin.
58 *
59 * Since everything within the plugin is registered via hooks,
60 * then kicking off the plugin from this point in the file does
61 * not affect the page life cycle.
62 *
63 * @since 1.0.0
64 */
65 function mo_firebase_run_firebase_authentication() {
66
67 $plugin = new MO_Firebase_Authentication();
68 $plugin->run();
69
70 }
71 mo_firebase_run_firebase_authentication();
72
73 /**
74 * Check if the customer key exists.
75 */
76 function mo_firebase_authentication_is_customer_registered() {
77 $email = get_option( 'mo_firebase_authentication_admin_email' );
78 $customer_key = get_option( 'mo_firebase_authentication_admin_customer_key' );
79 if ( ! $email || ! $customer_key || ! is_numeric( trim( $customer_key ) ) ) {
80 return 0;
81 } else {
82 return 1;
83 }
84 }
85 /**
86 * Check is license key verified
87 */
88 function mo_firebase_authentication_is_clv() {
89 $license_key = get_option( 'mo_firebase_authentication_lk' );
90 $isverified = get_option( 'mo_firebase_authentication_lv' );
91 if ( $isverified ) {
92 $isverified = mo_firebase_authentication_decrypt( $isverified );
93 }
94
95 if ( ! empty( $license_key ) && 'true' === $isverified ) {
96 return 1;
97 }
98 return 0;
99 }
100 /**
101 * Encryption for license key
102 *
103 * @param string $str .
104 */
105 function mo_firebase_authentication_encrypt( $str ) {
106 $pass = get_option( 'mo_firebase_authentication_customer_token' );
107 $pass = str_split( str_pad( '', strlen( $str ), $pass, STR_PAD_RIGHT ) );
108 $stra = str_split( $str );
109 foreach ( $stra as $k => $v ) {
110 $tmp = ord( $v ) + ord( $pass[ $k ] );
111 $stra[ $k ] = chr( $tmp > 255 ? ( $tmp - 256 ) : $tmp );
112 }
113 return base64_encode( join( '', $stra ) ); //phpcs:ignore -- ignoring DiscouragedPHPFunctions warning as this line of code is used for a valid code consisting license key encryption.
114 }
115 /**
116 * Decryption for license key
117 *
118 * @param string $str .
119 */
120 function mo_firebase_authentication_decrypt( $str ) {
121 $str = base64_decode( $str ); //phpcs:ignore -- ignoring DiscouragedPHPFunctions warning as this line of code is used for a valid code consisting license key ncryption.
122 $pass = get_option( 'mo_firebase_authentication_customer_token' );
123 $pass = str_split( str_pad( '', strlen( $str ), $pass, STR_PAD_RIGHT ) );
124 $stra = str_split( $str );
125 foreach ( $stra as $k => $v ) {
126 $tmp = ord( $v ) - ord( $pass[ $k ] );
127 $stra[ $k ] = chr( $tmp < 0 ? ( $tmp + 256 ) : $tmp );
128 }
129 return join( '', $stra );
130 }
131
132 /**
133 * Firebase Authentication Main Class
134 */
135 class Miniorange_Firebase_Authentication {
136 /**
137 * Constructor
138 */
139 public function __construct() {
140 add_action( 'init', array( $this, 'post_register' ) );
141 add_action( 'admin_init', array( $this, 'mo_firebase_auth_admin_forms_handler' ) );
142 if ( 1 === (int) get_option( 'mo_enable_firebase_auth' ) ) {
143 if ( strpos( ( isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '' ), '/wp-json' ) === false ) {
144 remove_filter( 'authenticate', 'wp_authenticate_username_password', 20, 3 );
145 remove_filter( 'authenticate', 'wp_authenticate_email_password', 20, 3 );
146 add_filter( 'authenticate', array( $this, 'mo_firebase_auth' ), 0, 3 );
147 }
148 }
149 remove_action( 'admin_notices', array( $this, 'mo_firebase_auth_success_message' ) );
150 remove_action( 'admin_notices', array( $this, 'mo_firebase_auth_error_message' ) );
151 add_action( 'admin_footer', array( $this, 'mo_firebase_auth_feedback_request' ) );
152 add_action( 'admin_notices', array( $this, 'mo_firebase_auth_open_demo_request' ) );
153 add_action( 'admin_notices', array( $this, 'mo_firebase_unverified_email_admin_notice' ) );
154 add_action( 'wp_ajax_mo_firebase_dismiss_unverified_email_notice', array( $this, 'mo_firebase_dismiss_unverified_email_notice' ) );
155 update_option( 'mo_fb_host_name', 'https://login.xecurify.com' );
156 }
157
158 /**
159 * Shows an admin notice on the Plugins page and this plugin's settings page
160 * explaining that only Firebase-verified emails can log in by default.
161 * Reappears on every page load as long as the "allow unverified email login"
162 * setting stays unchecked - unless the admin has dismissed it, which is permanent.
163 */
164 public function mo_firebase_unverified_email_admin_notice() {
165 if ( get_option( 'mo_firebase_allow_unverified_email_login' )
166 || get_option( 'mo_firebase_unverified_login_notice_dismissed' )
167 || ! current_user_can( 'manage_options' ) ) {
168 return;
169 }
170
171 $screen = get_current_screen();
172 if ( ! $screen || ! in_array( $screen->id, array( 'plugins', 'toplevel_page_mo_firebase_authentication' ), true ) ) {
173 return;
174 }
175
176 $settings_url = admin_url( 'admin.php?page=mo_firebase_authentication&tab=loginsettings' );
177 $message = sprintf(
178 /* translators: %s: link to the Firebase Authentication login settings page. */
179 __( 'Firebase Authentication: For security reasons, only users with a Firebase-verified email address are allowed to log in. If you want to allow login for unverified email addresses, %s.', 'firebase-authentication' ),
180 '<a href="' . esc_url( $settings_url ) . '">' . esc_html__( 'click here to enable it', 'firebase-authentication' ) . '</a>'
181 );
182 $allowed_tags = array(
183 'a' => array( 'href' => array() ),
184 );
185 ?>
186 <div class="notice notice-warning is-dismissible mo-firebase-unverified-email-notice">
187 <p><?php echo wp_kses( $message, $allowed_tags ); ?></p>
188 </div>
189 <script>
190 jQuery( function ( $ ) {
191 $( document ).on( 'click', '.mo-firebase-unverified-email-notice .notice-dismiss', function () {
192 $.post( ajaxurl, {
193 action: 'mo_firebase_dismiss_unverified_email_notice',
194 nonce: '<?php echo esc_js( wp_create_nonce( 'mo_firebase_dismiss_unverified_email_notice' ) ); ?>'
195 } );
196 } );
197 } );
198 </script>
199 <?php
200 }
201
202 /**
203 * AJAX handler that permanently dismisses the unverified-email admin notice.
204 */
205 public function mo_firebase_dismiss_unverified_email_notice() {
206 if ( ! current_user_can( 'manage_options' ) || ! check_ajax_referer( 'mo_firebase_dismiss_unverified_email_notice', 'nonce', false ) ) {
207 wp_send_json_error();
208 }
209 update_option( 'mo_firebase_unverified_login_notice_dismissed', 1 );
210 wp_send_json_success();
211 }
212
213 /**
214 * Save details after customer registration
215 */
216 public function post_register() {
217 if ( isset( $_POST['verify_user'] ) && isset( $_REQUEST['page'] ) && sanitize_text_field( wp_unslash( $_REQUEST['page'] ) ) === 'mo_firebase_authentication' && wp_verify_nonce( isset( $_REQUEST['mo_firebase_auth_config_field'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['mo_firebase_auth_config_field'] ) ) : '', 'mo_firebase_auth_config_form' ) ) {
218
219 if ( current_user_can( 'administrator' ) ) {
220 update_option( 'mo_firebase_auth_disable_wordpress_login', isset( $_POST['disable_wordpress_login'] ) ? (int) filter_var( wp_unslash( $_POST['disable_wordpress_login'] ), FILTER_SANITIZE_NUMBER_INT ) : 0 );
221
222 update_option( 'mo_firebase_auth_enable_admin_wp_login', isset( $_POST['mo_firebase_auth_enable_admin_wp_login'] ) ? (int) filter_var( wp_unslash( $_POST['mo_firebase_auth_enable_admin_wp_login'] ), FILTER_SANITIZE_NUMBER_INT ) : 0 );
223
224 $project_id = isset( $_POST['projectid'] ) ? sanitize_text_field( wp_unslash( $_POST['projectid'] ) ) : '';
225 update_option( 'mo_firebase_auth_project_id', $project_id );
226
227 $api_key = isset( $_POST['apikey'] ) ? sanitize_text_field( wp_unslash( $_POST['apikey'] ) ) : '';
228 update_option( 'mo_firebase_auth_api_key', $api_key );
229
230 $this->mo_firebase_auth_store_certificates();
231 update_option( 'mo_firebase_auth_message', 'Configurations saved successfully. Please <a href="' . admin_url( 'admin.php?page=mo_firebase_authentication&tab=config#test_authentication' ) . '">Test Authentication</a> before trying to Login.' );
232 $this->mo_firebase_auth_show_success_message();
233 }
234 }
235 }
236 /**
237 * Store certificates.
238 */
239 public function mo_firebase_auth_store_certificates() {
240 $response = wp_remote_get( 'https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com' );
241 if ( is_array( $response ) ) {
242 $header = $response['headers']; // array of http header lines.
243 $body = $response['body']; // use the content.
244
245 $split_result = explode( ':', $body );
246 $count = count( $split_result );
247 $kid1 = substr( $split_result[0], 5, 40 );
248 $s = explode( ',', $split_result[1] );
249 $c1 = substr( $s[0], 2, 1158 );
250 $c1 = str_replace( '\n', '', $c1 );
251 update_option( 'mo_firebase_auth_kid1', $kid1 );
252 update_option( 'mo_firebase_auth_cert1', $c1 );
253 if ( 3 === $count ) {
254 $kid2 = substr( $s[1], 4, 40 );
255 $c2 = explode( '}', $split_result[2] );
256 $c2[0] = substr( $c2[0], 2, 1158 );
257 $c2[0] = str_replace( '\n', '', $c2[0] );
258 update_option( 'mo_firebase_auth_kid2', $kid2 );
259 update_option( 'mo_firebase_auth_cert2', $c2[0] );
260 } elseif ( $count > 3 ) {
261 $kid2 = substr( $s[1], 4, 40 );
262 $s2 = explode( ',', $split_result[2] );
263 $c2 = substr( $s2[0], 2, 1158 );
264 $kid3 = substr( $s2[1], 4, 40 );
265 $c3 = explode( '}', $split_result[3] );
266 $c3[0] = substr( $c3[0], 2, 1158 );
267 $c2 = str_replace( '\n', '', $c2 );
268 update_option( 'mo_firebase_auth_kid2', $kid2 );
269 update_option( 'mo_firebase_auth_cert2', $c2 );
270 $c3[0] = str_replace( '\n', '', $c3[0] );
271 update_option( 'mo_firebase_auth_kid3', $kid3 );
272 update_option( 'mo_firebase_auth_cert3', $c3[0] );
273 }
274 } else {
275 if ( is_wp_error( $response ) ) {
276 $error_message = $response->get_error_message();
277 echo 'Something went wrong: ' . esc_attr( $error_message );
278 exit();
279 }
280 }
281 }
282 /**
283 * Handler function
284 *
285 * @param array $errors .
286 * @param string $redirect_to .
287 */
288 public function mo_fb_clear_wp_login_errors( $errors, $redirect_to ) {
289 return new WP_Error();
290 }
291
292 /**
293 * Firebase Authentication Login Handler
294 *
295 * @param WP_User/WP_Error $default_user .
296 * @param string $username .
297 * @param string $password .
298 */
299 public function mo_firebase_auth( $default_user, $username, $password ) {
300
301 if ( 'POST' !== ( isset( $_SERVER['REQUEST_METHOD'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) : '' ) ) {
302 add_filter( 'wp_login_errors', array( $this, 'mo_fb_clear_wp_login_errors' ), 0, 2 );
303 return $default_user;
304 }
305
306 if ( empty( $username ) || empty( $password ) ) {
307
308 $error = new WP_Error();
309 // create new error object and add errors to it.
310
311 if ( empty( $username ) ) { // No email.
312 $error->add(
313 'empty_username',
314 wp_kses(
315 '<strong>' . esc_html__( 'ERROR', 'firebase-authentication' ) . '</strong>: ' . esc_html__( 'Email field is empty.', 'firebase-authentication' ),
316 array( 'strong' => array() )
317 )
318 );
319 } elseif ( empty( $password ) ) { // No password.
320 $error->add(
321 'empty_password',
322 wp_kses(
323 '<strong>' . esc_html__( 'ERROR', 'firebase-authentication' ) . '</strong>: ' . esc_html__( 'Password field is empty.', 'firebase-authentication' ),
324 array( 'strong' => array() )
325 )
326 );
327 }
328 return $error;
329 }
330
331 $mo_firebase_config_obj = new Mo_Firebase_Config();
332 $fb_user = $mo_firebase_config_obj->mo_firebase_authenticate_call( $username, $password );
333 $fb_user = json_decode( $fb_user, true );
334
335 if ( isset( $fb_user['idToken'] ) ) {
336 $response = $mo_firebase_config_obj->mo_fb_login_user( $fb_user['idToken'] );
337 } else {
338
339 $error_message = $fb_user['error']['message'];
340
341 if ( 'INVALID_EMAIL' === $error_message || 'EMAIL_NOT_FOUND' === $error_message || 'INVALID_LOGIN_CREDENTIALS' === $error_message ) {
342 if ( '0' === get_option( 'mo_firebase_auth_disable_wordpress_login' ) ) {
343 $user = get_user_by( 'login', $username );
344 if ( ! $user ) {
345 $user = get_user_by( 'email', $username );
346 }
347 if ( $user && wp_check_password( $password, $user->data->user_pass, $user->ID ) ) {
348 return $user;
349 }
350 } elseif ( get_option( 'mo_firebase_auth_enable_admin_wp_login' ) ) {
351 $user = get_user_by( 'login', $username );
352 if ( ! $user ) {
353 $user = get_user_by( 'email', $username );
354 }
355 if ( $user && $this->is_administrator_user( $user ) ) {
356
357 if ( wp_check_password( $password, $user->data->user_pass, $user->ID ) ) {
358 return $user;
359 }
360 }
361 }
362 } else {
363 $error = new WP_Error();
364 if ( 'INVALID_PASSWORD' === $error_message ) {
365 $error_message = 'The password is invalid or the user does not have a password.';
366 }
367 $error_message = wp_kses(
368 '<strong>' . esc_html__( 'ERROR', 'firebase-authentication' ) . '</strong>: ' . esc_html( $error_message ),
369 array( 'strong' => array() )
370 );
371 $error->add( 'firebase_error', $error_message );
372 return $error;
373 }
374 }
375 return $default_user;
376 }
377
378 /**
379 * Admin dashboard messages
380 */
381 public function mo_firebase_auth_success_message() {
382 $message = "<div class='error'><p>" . get_option( 'mo_firebase_auth_message' ) . '</p></div>';
383 $allowed_tags = array(
384 'div' => array(
385 'class' => array(),
386 ),
387 'a' => array(
388 'href' => array(),
389 ),
390 );
391 echo wp_kses( $message, $allowed_tags );
392 }
393
394 /**
395 * Admin dashboard messages
396 */
397 public function mo_firebase_auth_error_message() {
398 $message = "<div class='updated'><p>" . get_option( 'mo_firebase_auth_message' ) . '</p></div>';
399 $allowed_tags = array(
400 'div' => array(
401 'class' => array(),
402 ),
403 'a' => array(
404 'href' => array(),
405 ),
406 );
407 echo wp_kses( $message, $allowed_tags );
408 }
409
410 /**
411 * Check for admin user
412 *
413 * @param WP_User $user .
414 */
415 public function is_administrator_user( $user ) {
416 $user_role = ( $user->roles );
417 if ( ! is_null( $user_role ) && in_array( 'administrator', $user_role, true ) ) {
418 return true;
419 } else {
420 return false;
421 }
422 }
423
424 /**
425 * Admin dashboard messages
426 */
427 private function mo_firebase_auth_show_success_message() {
428 remove_action( 'admin_notices', array( $this, 'mo_firebase_auth_success_message' ) );
429 add_action( 'admin_notices', array( $this, 'mo_firebase_auth_error_message' ) );
430 }
431 /**
432 * Admin dashboard messages
433 */
434 private function mo_firebase_auth_show_error_message() {
435 remove_action( 'admin_notices', array( $this, 'mo_firebase_auth_error_message' ) );
436 add_action( 'admin_notices', array( $this, 'mo_firebase_auth_success_message' ) );
437 }
438 /**
439 * Admin dashboard feedback form on deactivation
440 */
441 public function mo_firebase_auth_feedback_request() {
442 mo_firebase_auth_display_feedback_form();
443 }
444
445 /**
446 * Opens the requested demo site once the admin page is rendering.
447 *
448 * The demo request is processed on admin_init, where nothing can be echoed
449 * yet, so the sandbox URL is picked up here instead. A visible link is also
450 * printed as a fallback for browsers that block the popup.
451 */
452 public function mo_firebase_auth_open_demo_request() {
453 $transient_key = 'mo_firebase_auth_demo_request_url_' . get_current_user_id();
454 $demo_url = get_transient( $transient_key );
455 if ( empty( $demo_url ) ) {
456 return;
457 }
458 delete_transient( $transient_key );
459 ?>
460 <div class="notice notice-success" id="mo_firebase_auth_demo_notice">
461 <p>
462 <?php esc_html_e( 'Your demo request has been submitted.', 'firebase-authentication' ); ?>
463 <a href="<?php echo esc_url( $demo_url ); ?>" target="_blank" rel="noopener noreferrer" id="mo_firebase_auth_demo_link">
464 <?php esc_html_e( 'Click here if your demo site does not open automatically.', 'firebase-authentication' ); ?>
465 </a>
466 </p>
467 </div>
468 <script type="text/javascript">
469 ( function () {
470 var demoWindow = window.open( <?php echo wp_json_encode( $demo_url ); ?>, '_blank', 'noopener' );
471 if ( demoWindow ) {
472 demoWindow.opener = null;
473 }
474 } )();
475 </script>
476 <?php
477 }
478
479 /**
480 * Function to check the validations
481 *
482 * @param string $value .
483 */
484 private function mo_firebase_authentication_check_empty_or_null( $value ) {
485 if ( ! isset( $value ) || empty( $value ) ) {
486 return true;
487 }
488 return false;
489 }
490 /**
491 * Function for backend of admin forms such as contact us, feedback, login, etc.
492 */
493 public function mo_firebase_auth_admin_forms_handler() {
494
495 if ( isset( $_POST['option'] ) ) {
496
497 if ( sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo_firebase_authentication_change_email' && isset( $_REQUEST['mo_firebase_authentication_change_email_form_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['mo_firebase_authentication_change_email_form_nonce'] ) ), 'mo_firebase_authentication_change_email_form' ) ) {
498 // Adding back button.
499 update_option( 'mo_firebase_authentication_verify_customer', '' );
500 update_option( 'mo_firebase_authentication_registration_status', '' );
501 update_option( 'mo_firebase_authentication_new_registration', 'true' );
502 }
503
504 if ( 'mo_firebase_authentication_sign_in_option' === sanitize_text_field( wp_unslash( $_POST['option'] ) ) && isset( $_REQUEST['mo_firebase_auth_sign_in_option_field'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['mo_firebase_auth_sign_in_option_field'] ) ), 'mo_firebase_auth_sign_in_option_form' ) ) {
505 if ( current_user_can( 'administrator' ) ) {
506 update_option( 'mo_firebase_allow_unverified_email_login', isset( $_POST['mo_firebase_allow_unverified_email_login'] ) ? 1 : 0 );
507 }
508 }
509
510 if ( sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'change_miniorange' && isset( $_REQUEST['change_miniorange_form_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['change_miniorange_form_nonce'] ) ), 'change_miniorange_form' ) ) {
511 require_once MO_FIREBASE_AUTHENTICATION_DIR . 'includes' . DIRECTORY_SEPARATOR . 'class-mo-firebase-authentication-deactivator.php';
512 MO_Firebase_Authentication_Deactivator::deactivate();
513 return;
514 }
515
516 if ( sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo_firebase_authentication_register_customer' && isset( $_REQUEST['mo_fb_register_form_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['mo_fb_register_form_nonce'] ) ), 'mo_fb_register_form' ) ) { // register the admin to miniOrange
517 // validation and sanitization.
518 $email = '';
519 $phone = '';
520 $password = isset( $_POST['password'] ) ? stripslashes( $_POST['password'] ) : ''; //phpcs:ignore -- Ignoring sanitization for password input in case of special characters.
521 $confirm_password = isset( $_POST['confirmPassword'] ) ? stripslashes( $_POST['confirmPassword'] ) : ''; //phpcs:ignore -- Ignoring sanitization for password input in case of special characters.
522 $fname = '';
523 $lname = '';
524 $company = '';
525 if ( ! ( isset( $_POST['email'] ) && isset( $_POST['password'] ) && isset( $_POST['confirmPassword'] ) ) ) {
526 update_option( 'mo_firebase_auth_message', 'All the fields are required. Please enter valid entries.' );
527 $this->mo_firebase_auth_show_error_message();
528 return;
529 } elseif ( strlen( $password ) < 8 || strlen( $confirm_password ) < 8 ) {
530 update_option( 'mo_firebase_auth_message', 'Choose a password with minimum length 8.' );
531 $this->mo_firebase_auth_show_error_message();
532 return;
533 } else {
534 $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : '';
535 $phone = isset( $_POST['phone'] ) ? sanitize_text_field( wp_unslash( $_POST['phone'] ) ) : '';
536 $fname = isset( $_POST['fname'] ) ? sanitize_text_field( wp_unslash( $_POST['fname'] ) ) : '';
537 $lname = isset( $_POST['lname'] ) ? sanitize_text_field( wp_unslash( $_POST['lname'] ) ) : '';
538 $company = isset( $_POST['company'] ) ? sanitize_text_field( wp_unslash( $_POST['company'] ) ) : '';
539 }
540
541 update_option( 'mo_firebase_authentication_admin_email', $email );
542 update_option( 'mo_firebase_authentication_admin_phone', $phone );
543 update_option( 'mo_firebase_authentication_admin_fname', $fname );
544 update_option( 'mo_firebase_authentication_admin_lname', $lname );
545 update_option( 'mo_firebase_authentication_admin_company', $company );
546
547 if ( 0 === strcmp( $password, $confirm_password ) ) {
548 update_option( 'password', $password );
549 $customer = new MO_Firebase_Customer();
550 $email = get_option( 'mo_firebase_authentication_admin_email' );
551 $content = json_decode( $customer->check_customer(), true );
552
553 if ( 0 === strcasecmp( $content['status'], 'CUSTOMER_NOT_FOUND' ) ) {
554 $response = json_decode( $customer->create_customer(), true );
555 if ( strcasecmp( $response['status'], 'SUCCESS' ) !== 0 ) {
556 update_option( 'mo_firebase_auth_message', 'Failed to create customer. Try again.' );
557 $this->mo_firebase_auth_show_error_message();
558 } else {
559 update_option( 'mo_firebase_auth_message', 'Your registration is successful. Please login.' );
560 $this->mo_firebase_auth_show_success_message();
561 }
562 } elseif ( 0 === strcasecmp( $content['status'], 'SUCCESS' ) ) {
563 update_option( 'mo_firebase_auth_message', 'Account already exist. Please Login.' );
564 $this->mo_firebase_auth_show_error_message();
565 } else {
566 update_option( 'mo_firebase_auth_message', $content['status'] );
567 $this->mo_firebase_auth_show_success_message();
568 }
569 } else {
570 update_option( 'mo_firebase_auth_message', 'Passwords do not match.' );
571 delete_option( 'mo_firebase_authentication_verify_customer' );
572 $this->mo_firebase_auth_show_error_message();
573 }
574 } if ( sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo_firebase_authentication_goto_login' && isset( $_REQUEST['mo_firebase_authentication_goto_login_form_field'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['mo_firebase_authentication_goto_login_form_field'] ) ), 'mo_firebase_authentication_goto_login_form' ) ) {
575 delete_option( 'mo_firebase_authentication_new_registration' );
576 update_option( 'mo_firebase_authentication_verify_customer', 'true' );
577
578 } if ( sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo_enable_firebase_auth' && wp_verify_nonce( ( isset( $_REQUEST['mo_firebase_auth_enable_field'] ) ? sanitize_key( wp_unslash( $_REQUEST['mo_firebase_auth_enable_field'] ) ) : '' ), 'mo_firebase_auth_enable_form' ) ) {
579 update_option( 'mo_enable_firebase_auth', isset( $_POST['mo_enable_firebase_auth'] ) ? (int) filter_var( wp_unslash( $_POST['mo_enable_firebase_auth'] ), FILTER_SANITIZE_NUMBER_INT ) : 0 );
580
581 } elseif ( sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo_firebase_auth_contact_us' && isset( $_REQUEST['mo_firebase_auth_contact_us_field'] ) && wp_verify_nonce( ( isset( $_REQUEST['mo_firebase_auth_contact_us_field'] ) ? sanitize_key( wp_unslash( $_REQUEST['mo_firebase_auth_contact_us_field'] ) ) : '' ), 'mo_firebase_auth_contact_us_form' ) ) {
582 $email = isset( $_POST['mo_firebase_auth_contact_us_email'] ) ? sanitize_email( wp_unslash( $_POST['mo_firebase_auth_contact_us_email'] ) ) : '';
583 $phone = isset( $_POST['mo_firebase_auth_contact_us_phone'] ) ? '+ ' . preg_replace( '/[^0-9]/', '', sanitize_text_field( wp_unslash( $_POST['mo_firebase_auth_contact_us_phone'] ) ) ) : '';
584 $query = isset( $_POST['mo_firebase_auth_contact_us_query'] ) ? sanitize_text_field( wp_unslash( $_POST['mo_firebase_auth_contact_us_query'] ) ) : '';
585 if ( $this->mo_firebase_authentication_check_empty_or_null( $email ) || $this->mo_firebase_authentication_check_empty_or_null( $query ) ) {
586 echo '<br><b style=color:red>Please fill up Email and Query fields to submit your query.</b>';
587 } else {
588 $contact_us = new MO_Firebase_contact_us();
589 $submited = $contact_us->mo_firebase_auth_contact_us( $email, $phone, $query );
590 if ( false === $submited ) {
591 update_option( 'mo_firebase_auth_message', 'Your query could not be submitted. Please try again.' );
592 $this->mo_firebase_auth_show_error_message();
593 } else {
594 update_option( 'mo_firebase_auth_message', 'Thanks for getting in touch! We shall get back to you shortly.' );
595 $this->mo_firebase_auth_show_success_message();
596 }
597 }
598 } elseif ( sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo_firebase_authentication_verify_customer' && isset( $_REQUEST['mo_fb_login_form_nonce'] ) && wp_verify_nonce( ( isset( $_REQUEST['mo_fb_login_form_nonce'] ) ? sanitize_key( wp_unslash( $_REQUEST['mo_fb_login_form_nonce'] ) ) : '' ), 'mo_fb_login_form' ) ) {// register the admin to miniOrange
599 // validation and sanitization.
600 $email = '';
601 $password = '';
602 if ( ! ( isset( $_POST['email'] ) && isset( $_POST['password'] ) ) ) {
603 update_option( 'mo_firebase_auth_message', 'All the fields are required. Please enter valid entries.' );
604 $this->mo_firebase_auth_show_error_message();
605 return;
606 } else {
607 $email = sanitize_email( wp_unslash( $_POST['email'] ) );
608 $password = stripslashes( wp_unslash( $_POST['password'] ) ); //phpcs:ignore -- Ignoring sanitization for password input in case of special characters.
609 }
610
611 update_option( 'mo_firebase_authentication_admin_email', $email );
612 update_option( 'password', $password );
613 $customer = new MO_Firebase_Customer();
614 $content = $customer->mo_firebase_auth_get_customer_key();
615 $customer_key = json_decode( $content, true );
616 if ( json_last_error() === JSON_ERROR_NONE ) {
617 update_option( 'mo_firebase_authentication_admin_customer_key', $customer_key['id'] );
618 update_option( 'mo_firebase_authentication_admin_api_key', $customer_key['apiKey'] );
619 update_option( 'mo_firebase_authentication_customer_token', $customer_key['token'] );
620 if ( isset( $customer_key['phone'] ) ) {
621 update_option( 'mo_firebase_authentication_admin_phone', $customer_key['phone'] );
622 }
623 delete_option( 'password' );
624 update_option( 'mo_firebase_auth_message', 'Customer retrieved successfully' );
625 delete_option( 'mo_firebase_authentication_verify_customer' );
626 $this->mo_firebase_auth_show_success_message();
627 } else {
628 update_option( 'mo_firebase_auth_message', 'Invalid username or password. Please try again.' );
629 $this->mo_firebase_auth_show_error_message();
630 }
631 } elseif ( sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo_firebase_auth_skip_feedback' && isset( $_REQUEST['mo_firebase_auth_skip_feedback_form_nonce'] ) && wp_verify_nonce( ( isset( $_REQUEST['mo_firebase_auth_skip_feedback_form_nonce'] ) ? sanitize_key( wp_unslash( $_REQUEST['mo_firebase_auth_skip_feedback_form_nonce'] ) ) : '' ), 'mo_firebase_auth_skip_feedback_form' ) ) {
632 deactivate_plugins( __FILE__ );
633 update_option( 'mo_firebase_auth_message', 'Plugin deactivated successfully' );
634 $this->mo_firebase_auth_show_success_message();
635
636 } elseif ( sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo_firebase_auth_feedback' && isset( $_REQUEST['mo_firebase_auth_feedback_field'] ) && wp_verify_nonce( ( isset( $_REQUEST['mo_firebase_auth_feedback_field'] ) ? sanitize_key( wp_unslash( $_REQUEST['mo_firebase_auth_feedback_field'] ) ) : '' ), 'mo_firebase_auth_feedback_form' ) ) {
637 $user = wp_get_current_user();
638 $message = 'Plugin Deactivated:';
639 $deactivate_reason = array_key_exists( 'deactivate_reason_radio', $_POST ) ? sanitize_text_field( wp_unslash( $_POST['deactivate_reason_radio'] ) ) : false;
640 $deactivate_reason_message = array_key_exists( 'query_feedback', $_POST ) ? sanitize_text_field( wp_unslash( $_POST['query_feedback'] ) ) : false;
641 if ( $deactivate_reason ) {
642 $message .= $deactivate_reason;
643 if ( isset( $deactivate_reason_message ) ) {
644 $message .= ':' . $deactivate_reason_message;
645 }
646
647 $email = $user->user_email;
648 $contact_us = new MO_Firebase_contact_us();
649 $response = $contact_us->mo_firebase_auth_send_email_alert( $email, $message, 'Feedback: WordPress Firebase Authentication' );
650 if ( ! is_null( $response ) ) {
651 $submited = json_decode( $response, true );
652 }
653 deactivate_plugins( __FILE__ );
654 update_option( 'mo_firebase_auth_message', 'Thank you for the feedback.' );
655 $this->mo_firebase_auth_show_success_message();
656
657 } else {
658 update_option( 'mo_firebase_auth_message', 'Please Select one of the reasons ,if your reason is not mentioned please select Other Reasons' );
659 $this->mo_firebase_auth_show_error_message();
660 }
661 } elseif ( sanitize_text_field( wp_unslash( $_POST['option'] ) ) === 'mo_fb_demo_request_form' && isset( $_REQUEST['mo_fb_demo_request_field'] ) && wp_verify_nonce( ( isset( $_REQUEST['mo_fb_demo_request_field'] ) ? sanitize_key( wp_unslash( $_REQUEST['mo_fb_demo_request_field'] ) ) : '' ), 'mo_fb_demo_request_form' ) ) {
662
663 if ( current_user_can( 'administrator' ) ) {
664 $email = isset( $_POST['mo_auto_create_demosite_email'] ) ? sanitize_email( wp_unslash( $_POST['mo_auto_create_demosite_email'] ) ) : '';
665 $firestore_check = isset( $_POST['mo_auto_create_demosite_firestore_integrator_check'] ) ? sanitize_text_field( wp_unslash( $_POST['mo_auto_create_demosite_firestore_integrator_check'] ) ) : '';
666 $query = isset( $_POST['mo_auto_create_demosite_usecase'] ) ? sanitize_text_field( wp_unslash( $_POST['mo_auto_create_demosite_usecase'] ) ) : '';
667
668 if ( $this->mo_firebase_authentication_check_empty_or_null( $email ) || $this->mo_firebase_authentication_check_empty_or_null( $query ) ) {
669 update_option( 'mo_firebase_auth_message', 'Please fill up Usecase, Email field to submit your query.' );
670 $this->mo_firebase_auth_show_error_message();
671 } else {
672 global $wp_version;
673 $mo_firebase_sandbox_usecase_with_addons = 'Usecase: ' . PHP_EOL .
674 $query .
675 PHP_EOL .
676 ' ' .
677 'Firestore Integrator: ' . PHP_EOL . ( 'true' === $firestore_check ? 'true' : 'false' );
678 $wp_version_trim = substr( $wp_version, 0, 3 );
679 $mo_oauth_sandbox_href = 'https://sandbox.miniorange.com/?email=' . rawurlencode( $email ) . '&mo_plugin=mo_firebase_authentication&wordpress_version=' . rawurlencode( $wp_version_trim ) . '&usecase=' . rawurlencode( $mo_firebase_sandbox_usecase_with_addons ) . '&referer=' . rawurlencode( get_site_url() );
680
681 // The handler runs on admin_init, before any markup is sent, so the
682 // sandbox URL is stashed and opened once the admin page is rendered.
683 set_transient( 'mo_firebase_auth_demo_request_url_' . get_current_user_id(), $mo_oauth_sandbox_href, 5 * MINUTE_IN_SECONDS );
684 }
685 }
686 }
687 }
688 }
689
690 }
691
692 $mo_firebase_authentication_obj = new Miniorange_Firebase_Authentication();
693