|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Handles data for the current customers session. |
| 4 | + * |
| 5 | + * @package WPGraphQL\Extensions\WooCommerce\Utils |
| 6 | + * @since 0.1.2 |
| 7 | + */ |
| 8 | + |
| 9 | +namespace WPGraphQL\Extensions\WooCommerce\Utils; |
| 10 | + |
| 11 | +/** |
| 12 | + * Class - QL_Session_Handler |
| 13 | + */ |
| 14 | +class QL_Session_Handler extends \WC_Session_Handler { |
| 15 | + /** |
| 16 | + * Encrypt and decrypt |
| 17 | + * |
| 18 | + * @author Nazmul Ahsan <n.mukto@gmail.com> |
| 19 | + * @author Geoff Taylor <kidunot89@gmail.com> |
| 20 | + * @link http://nazmulahsan.me/simple-two-way-function-encrypt-decrypt-string/ |
| 21 | + * |
| 22 | + * @param string $string string to be encrypted/decrypted. |
| 23 | + * @param string $action what to do with this? e for encrypt, d for decrypt. |
| 24 | + * |
| 25 | + * @return string |
| 26 | + */ |
| 27 | + private function crypt( $string, $action = 'e' ) { |
| 28 | + // you may change these values to your own. |
| 29 | + $secret_key = apply_filters( 'woographql_session_header_secret_key', 'my_simple_secret_key' ); |
| 30 | + $secret_iv = apply_filters( 'woographql_session_header_secret_iv', 'my_simple_secret_iv' ); |
| 31 | + |
| 32 | + $output = false; |
| 33 | + $encrypt_method = 'AES-256-CBC'; |
| 34 | + $key = hash( 'sha256', $secret_key ); |
| 35 | + $iv = substr( hash( 'sha256', $secret_iv ), 0, 16 ); |
| 36 | + |
| 37 | + if ( 'e' === $action ) { |
| 38 | + $output = base64_encode( openssl_encrypt( $string, $encrypt_method, $key, 0, $iv ) ); |
| 39 | + } elseif ( 'd' === $action ) { |
| 40 | + $output = openssl_decrypt( base64_decode( $string ), $encrypt_method, $key, 0, $iv ); |
| 41 | + } |
| 42 | + |
| 43 | + return $output; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Returns formatted $_SERVER index from provided string. |
| 48 | + * |
| 49 | + * @param string $string String to be formatted. |
| 50 | + * |
| 51 | + * @return string |
| 52 | + */ |
| 53 | + private function get_server_key( $string ) { |
| 54 | + return 'HTTP_' . strtoupper( preg_replace( '#[^A-z0-9]#', '_', $string ) ); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Encrypts and sets the session header on-demand (usually after adding an item to the cart). |
| 59 | + * |
| 60 | + * Warning: Headers will only be set if this is called before the headers are sent. |
| 61 | + * |
| 62 | + * @param bool $set Should the session cookie be set. |
| 63 | + */ |
| 64 | + public function set_customer_session_cookie( $set ) { |
| 65 | + if ( $set ) { |
| 66 | + $to_hash = $this->_customer_id . '|' . $this->_session_expiration; |
| 67 | + $cookie_hash = hash_hmac( 'md5', $to_hash, wp_hash( $to_hash ) ); |
| 68 | + $cookie_value = $this->_customer_id . '||' . $this->_session_expiration . '||' . $this->_session_expiring . '||' . $cookie_hash; |
| 69 | + $this->_has_cookie = true; |
| 70 | + if ( ! isset( $_SERVER[ $this->_cookie ] ) || $_SERVER[ $this->_cookie ] !== $cookie_value ) { |
| 71 | + add_filter( |
| 72 | + 'graphql_response_headers_to_send', |
| 73 | + function( $headers ) use ( $cookie_value ) { |
| 74 | + $headers[ $this->_cookie ] = $this->crypt( $cookie_value, 'e' ); |
| 75 | + return $headers; |
| 76 | + } |
| 77 | + ); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Return true if the current user has an active session, i.e. a cookie to retrieve values. |
| 84 | + * |
| 85 | + * @return bool |
| 86 | + */ |
| 87 | + public function has_session() { |
| 88 | + // @codingStandardsIgnoreLine. |
| 89 | + return isset( $_SERVER[ $this->get_server_key( $this->_cookie ) ] ) || $this->_has_cookie || is_user_logged_in(); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Retrieve and decrypt the session data from session, if set. Otherwise return false. |
| 94 | + * |
| 95 | + * Session cookies without a customer ID are invalid. |
| 96 | + * |
| 97 | + * @return bool|array |
| 98 | + */ |
| 99 | + public function get_session_cookie() { |
| 100 | + // @codingStandardsIgnoreStart. |
| 101 | + $cookie_value = isset( $_SERVER[ $this->get_server_key( $this->_cookie ) ] ) |
| 102 | + ? $this->crypt( $_SERVER[ $this->get_server_key( $this->_cookie ) ], 'd' ) |
| 103 | + : false; |
| 104 | + // @codingStandardsIgnoreEnd. |
| 105 | + if ( empty( $cookie_value ) || ! is_string( $cookie_value ) ) { |
| 106 | + return false; |
| 107 | + } |
| 108 | + list( $customer_id, $session_expiration, $session_expiring, $cookie_hash ) = explode( '||', $cookie_value ); |
| 109 | + if ( empty( $customer_id ) ) { |
| 110 | + return false; |
| 111 | + } |
| 112 | + // Validate hash. |
| 113 | + $to_hash = $customer_id . '|' . $session_expiration; |
| 114 | + $hash = hash_hmac( 'md5', $to_hash, wp_hash( $to_hash ) ); |
| 115 | + if ( empty( $cookie_hash ) || ! hash_equals( $hash, $cookie_hash ) ) { |
| 116 | + return false; |
| 117 | + } |
| 118 | + return array( $customer_id, $session_expiration, $session_expiring, $cookie_hash ); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Forget all session data without destroying it. |
| 123 | + */ |
| 124 | + public function forget_session() { |
| 125 | + add_filter( |
| 126 | + 'graphql_response_headers_to_send', |
| 127 | + function( $headers ) { |
| 128 | + $headers[ $this->_cookie ] = 'false'; |
| 129 | + return $headers; |
| 130 | + } |
| 131 | + ); |
| 132 | + wc_empty_cart(); |
| 133 | + $this->_data = array(); |
| 134 | + $this->_dirty = false; |
| 135 | + $this->_customer_id = $this->generate_customer_id(); |
| 136 | + } |
| 137 | +} |
0 commit comments