$identity ); return wp_json_encode( array_merge( $identity, array( 'domain' => videm_app_wordpress_domain() ) ) ); } /** Get the owner details needed to verify the panel credentials. */ function videm_app_get_panel_owner( $settings ) { $response = videm_app_panel_request( 'GET', $settings['panel_url'] . '/api/__panel/me', array( 'timeout' => 10, 'redirection' => 0, 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( $settings['owner_mobile'] . ':' . $settings['api_token'] ), 'Accept' => 'application/json', ), ) ); if ( is_wp_error( $response ) ) { return new WP_Error( 'videm_app_connection_failed', __( 'اتصال به پنل ویدم برقرار نشد.', 'videm-app' ) ); } if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { return new WP_Error( 'videm_app_auth_failed', __( 'پنل ویدم مشخصات مالک و API token را تأیید نکرد.', 'videm-app' ) ); } $data = json_decode( wp_remote_retrieve_body( $response ), true ); if ( ! is_array( $data ) || true !== ( $data['status'] ?? null ) || ( $data['data']['mobile'] ?? null ) !== $settings['owner_mobile'] ) { return new WP_Error( 'videm_app_owner_mismatch', __( 'پاسخ پنل با شماره موبایل مالک مطابقت ندارد.', 'videm-app' ) ); } return $data['data']; } /** Verify the credentials and the customer-token endpoint before saving settings. */ function videm_app_verify_panel_connection( $settings ) { $owner = videm_app_get_panel_owner( $settings ); if ( is_wp_error( $owner ) ) { return $owner; } $endpoint = videm_app_panel_request( 'POST', $settings['panel_url'] . '/api/__panel/customers/login-token', array( 'timeout' => 10, 'redirection' => 0, 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( $settings['owner_mobile'] . ':' . $settings['api_token'] ), 'Content-Type' => 'application/json', 'Accept' => 'application/json', ), 'body' => videm_app_token_request_body( 'invalid' ), ) ); if ( is_wp_error( $endpoint ) || 422 !== wp_remote_retrieve_response_code( $endpoint ) ) { return new WP_Error( 'videm_app_token_endpoint_unavailable', __( 'مسیر صدور توکن مشتری در پنل ویدم در دسترس نیست.', 'videm-app' ) ); } return true; } /** * Only a phone number explicitly verified by the authentication provider is eligible. * Providers can supply that number through the videm_app_verified_mobile filter. * * @return string|WP_Error */ function videm_app_current_user_mobile() { if ( ! is_user_logged_in() ) { return new WP_Error( 'videm_app_login_required', __( 'برای ورود به ویدم ابتدا وارد حساب وردپرس خود شوید.', 'videm-app' ) ); } $user = wp_get_current_user(); $woo = videm_app_woo_settings(); $verified = videm_app_normalize_mobile( apply_filters( 'videm_app_verified_mobile', '', $user ) ); if ( ! $verified ) { foreach ( array_unique( array( $woo['identity_meta_key'], 'verified_mobile', 'verified_phone', 'mobile_verified_number' ) ) as $key ) { $verified = videm_app_normalize_mobile( get_user_meta( $user->ID, $key, true ) ); if ( $verified ) { break; } } } if ( ! $verified ) { return new WP_Error( 'videm_app_mobile_unverified', __( 'شماره موبایل تأییدشده‌ای برای حساب وردپرس پیدا نشد.', 'videm-app' ) ); } foreach ( array( 'mobile', 'phone', 'phone_number', 'digits_phone', 'billing_phone' ) as $key ) { if ( $verified === videm_app_normalize_mobile( get_user_meta( $user->ID, $key, true ) ) ) { return $verified; } } return $verified; } /** Email login is enabled only by a paid order bound to this WordPress account. */ function videm_app_current_user_identity() { if ( ! is_user_logged_in() ) { return new WP_Error( 'videm_app_login_required', __( 'برای ورود به ویدم ابتدا وارد حساب وردپرس خود شوید.', 'videm-app' ) ); } if ( 'email' !== videm_app_woo_settings()['identity_type'] ) { $mobile = videm_app_current_user_mobile(); return is_wp_error( $mobile ) ? $mobile : array( 'mobile' => $mobile ); } $user = wp_get_current_user(); // Primary: the verified email stored under the admin-configured identity meta key. $email = sanitize_email( get_user_meta( $user->ID, videm_app_woo_settings()['identity_meta_key'], true ) ); if ( is_email( $email ) ) { return array( 'email' => $email ); } // Backward-compatible fallback: email bound by a paid WooCommerce order. $legacy = sanitize_email( get_user_meta( $user->ID, '_videm_app_login_email', true ) ); if ( is_email( $legacy ) && strtolower( $legacy ) === strtolower( $user->user_email ) ) { return array( 'email' => $legacy ); } return new WP_Error( 'videm_app_email_not_linked', __( 'ایمیل تأییدشده‌ای برای این حساب پیدا نشد. کلید متای ایمیل تأییدشده را در تنظیمات بررسی کنید یا خرید دوره با همین ایمیل را ثبت کنید.', 'videm-app' ) ); } /** The longest a per-user session token is cached before the panel is asked again. */ if ( ! defined( 'VIDEM_APP_SESSION_TTL' ) ) { define( 'VIDEM_APP_SESSION_TTL', WEEK_IN_SECONDS ); } /** Transient key holding the cached panel token for a single WordPress user. */ function videm_app_session_cache_key( $user_id, $identity, $settings ) { return 'videm_app_session_' . (int) $user_id . '_' . hash( 'sha256', wp_json_encode( array( $identity, $settings['panel_url'], $settings['owner_mobile'], $settings['api_token'] ) ) ); } /** * Return a cached session token for the user, or request a fresh one and cache it. * * Repeated calls within the cache window reuse the stored token instead of hitting * the panel again, so a logged-in user cannot drive one panel request per click. * The cache never outlives the token itself: it expires at the sooner of one week * or the token's own expire_at. * * @return array{token:string,expire_at:string}|WP_Error */ function videm_app_get_customer_session( $identity, $user_id ) { $settings = videm_app_panel_settings(); if ( ! $settings['owner_mobile'] || ! $settings['api_token'] ) { return new WP_Error( 'videm_app_not_configured', __( 'اتصال پنل ویدم هنوز تنظیم نشده است.', 'videm-app' ) ); } $cache_key = videm_app_session_cache_key( $user_id, $identity, $settings ); $cached = get_transient( $cache_key ); if ( is_array( $cached ) && isset( $cached['token'], $cached['expire_at'] ) ) { return array( 'token' => $cached['token'], 'expire_at' => $cached['expire_at'] ); } $session = videm_app_request_customer_token( $identity, $settings ); if ( is_wp_error( $session ) ) { return $session; } // Cache up to one week, but never past the moment the token stops being valid. $ttl = min( VIDEM_APP_SESSION_TTL, strtotime( $session['expire_at'] ) - time() - MINUTE_IN_SECONDS ); if ( $ttl > 0 ) { set_transient( $cache_key, $session, $ttl ); } return $session; } /** * @return array{token:string,expire_at:string}|WP_Error */ function videm_app_request_customer_token( $identity, $settings ) { $identity = is_array( $identity ) ? $identity : array( 'mobile' => $identity ); if ( isset( $identity['email'] ) && ! isset( $identity['mobile'] ) ) { $email = sanitize_email( $identity['email'] ); if ( ! is_email( $email ) ) { return new WP_Error( 'videm_app_invalid_email', __( 'ایمیل حساب کاربری معتبر نیست.', 'videm-app' ) ); } $identity = array( 'email' => $email ); } elseif ( isset( $identity['mobile'] ) && ! isset( $identity['email'] ) ) { $mobile = videm_app_normalize_mobile( $identity['mobile'] ); if ( ! $mobile ) { return new WP_Error( 'videm_app_invalid_mobile', __( 'شماره موبایل حساب کاربری معتبر نیست.', 'videm-app' ) ); } $identity = array( 'mobile' => $mobile ); } else { return new WP_Error( 'videm_app_invalid_identity', __( 'شناسهٔ ورود حساب کاربری معتبر نیست.', 'videm-app' ) ); } $response = videm_app_panel_request( 'POST', $settings['panel_url'] . '/api/__panel/customers/login-token', array( 'timeout' => 10, 'redirection' => 0, 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( $settings['owner_mobile'] . ':' . $settings['api_token'] ), 'Content-Type' => 'application/json', 'Accept' => 'application/json', ), 'body' => videm_app_token_request_body( $identity ), ) ); if ( is_wp_error( $response ) ) { return new WP_Error( 'videm_app_connection_failed', __( 'اتصال به پنل ویدم برقرار نشد.', 'videm-app' ) ); } $status = wp_remote_retrieve_response_code( $response ); if ( 404 === $status ) { return new WP_Error( 'videm_app_not_found', __( 'برای این شناسهٔ ورود، حساب ویدم در پنل مالک پیدا نشد.', 'videm-app' ) ); } if ( 409 === $status ) { return new WP_Error( 'videm_app_no_device', __( 'برای این حساب ویدم دستگاهی ثبت نشده است.', 'videm-app' ) ); } if ( 401 === $status || 403 === $status ) { return new WP_Error( 'videm_app_panel_auth_failed', __( 'مشخصات اتصال به پنل ویدم معتبر نیست.', 'videm-app' ) ); } if ( 200 !== $status ) { return new WP_Error( 'videm_app_panel_failed', __( 'پنل ویدم توکن ورود را برنگرداند.', 'videm-app' ) ); } $data = json_decode( wp_remote_retrieve_body( $response ), true ); if ( ! is_array( $data ) || ! isset( $data['token'], $data['expire_at'] ) || ! is_string( $data['token'] ) || ! is_string( $data['expire_at'] ) || ! preg_match( '/^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/', $data['token'] ) || false === strtotime( $data['expire_at'] ) ) { return new WP_Error( 'videm_app_invalid_response', __( 'پاسخ پنل ویدم معتبر نیست.', 'videm-app' ) ); } return array( 'token' => $data['token'], 'expire_at' => $data['expire_at'] ); }