class.swpm-utils.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. <?php
  2. abstract class SwpmUtils {
  3. public static function is_ajax() {
  4. return defined( 'DOING_AJAX' ) && DOING_AJAX;
  5. }
  6. /*
  7. * This function handles various initial setup tasks that need to be executed very early on (before other functions of the plugin is called).
  8. */
  9. public static function do_misc_initial_plugin_setup_tasks() {
  10. //Management role/permission setup
  11. $admin_dashboard_permission = SwpmSettings::get_instance()->get_value( 'admin-dashboard-access-permission' );
  12. if ( empty( $admin_dashboard_permission ) ) {
  13. //By default only admins can manage/see admin dashboard
  14. define( 'SWPM_MANAGEMENT_PERMISSION', 'manage_options' );
  15. } else {
  16. define( 'SWPM_MANAGEMENT_PERMISSION', $admin_dashboard_permission );
  17. }
  18. //Override the settings menu (options.php) update capability according to the role set in "Admin Dashboard Access Permission" option.
  19. add_filter( 'option_page_capability_swpm-settings-tab-1', 'SwpmUtils::swpm_settings_update_capability' );
  20. add_filter( 'option_page_capability_swpm-settings-tab-2', 'SwpmUtils::swpm_settings_update_capability' );
  21. add_filter( 'option_page_capability_swpm-settings-tab-3', 'SwpmUtils::swpm_settings_update_capability' );
  22. add_filter( 'option_page_capability_swpm-settings-tab-4', 'SwpmUtils::swpm_settings_update_capability' );
  23. add_filter( 'option_page_capability_swpm-settings-tab-5', 'SwpmUtils::swpm_settings_update_capability' );
  24. }
  25. public static function swpm_settings_update_capability($capability){
  26. if ( defined('SWPM_MANAGEMENT_PERMISSION') ){
  27. //Use SWPM defined one.
  28. $capability = SWPM_MANAGEMENT_PERMISSION;
  29. } else {
  30. //Use default.
  31. $capability = 'manage_options';
  32. }
  33. return $capability;
  34. }
  35. public static function subscription_type_dropdown( $selected ) {
  36. return '<option ' . ( ( $selected == SwpmMembershipLevel::NO_EXPIRY ) ? 'selected="selected"' : '' ) . ' value="' . SwpmMembershipLevel::NO_EXPIRY . '">No Expiry</option>' .
  37. '<option ' . ( ( $selected == SwpmMembershipLevel::DAYS ) ? 'selected="selected"' : '' ) . ' value="' . SwpmMembershipLevel::DAYS . '">Day(s)</option>' .
  38. '<option ' . ( ( $selected == SwpmMembershipLevel::WEEKS ) ? 'selected="selected"' : '' ) . ' value="' . SwpmMembershipLevel::WEEKS . '">Week(s)</option>' .
  39. '<option ' . ( ( $selected == SwpmMembershipLevel::MONTHS ) ? 'selected="selected"' : '' ) . ' value="' . SwpmMembershipLevel::MONTHS . '">Month(s)</option>' .
  40. '<option ' . ( ( $selected == SwpmMembershipLevel::YEARS ) ? 'selected="selected"' : '' ) . ' value="' . SwpmMembershipLevel::YEARS . '">Year(s)</option>' .
  41. '<option ' . ( ( $selected == SwpmMembershipLevel::FIXED_DATE ) ? 'selected="selected"' : '' ) . ' value="' . SwpmMembershipLevel::FIXED_DATE . '">Fixed Date</option>';
  42. }
  43. // $subscript_period must be integer.
  44. public static function calculate_subscription_period_days( $subcript_period, $subscription_duration_type ) {
  45. if ( $subscription_duration_type == SwpmMembershipLevel::NO_EXPIRY ) {
  46. return 'noexpire';
  47. }
  48. if ( ! is_numeric( $subcript_period ) ) {
  49. throw new Exception( ' subcript_period parameter must be integer in SwpmUtils::calculate_subscription_period_days method' );
  50. }
  51. switch ( strtolower( $subscription_duration_type ) ) {
  52. case SwpmMembershipLevel::DAYS:
  53. break;
  54. case SwpmMembershipLevel::WEEKS:
  55. $subcript_period = $subcript_period * 7;
  56. break;
  57. case SwpmMembershipLevel::MONTHS:
  58. $subcript_period = $subcript_period * 30;
  59. break;
  60. case SwpmMembershipLevel::YEARS:
  61. $subcript_period = $subcript_period * 365;
  62. break;
  63. }
  64. return $subcript_period;
  65. }
  66. public static function get_expiration_timestamp( $user ) {
  67. $permission = SwpmPermission::get_instance( $user->membership_level );
  68. if ( SwpmMembershipLevel::FIXED_DATE == $permission->get( 'subscription_duration_type' ) ) {
  69. return strtotime( $permission->get( 'subscription_period' ) );
  70. }
  71. $days = self::calculate_subscription_period_days( $permission->get( 'subscription_period' ), $permission->get( 'subscription_duration_type' ) );
  72. if ( $days == 'noexpire' ) {
  73. return PHP_INT_MAX; // which is equivalent to
  74. }
  75. return strtotime( $user->subscription_starts . ' ' . $days . ' days' );
  76. }
  77. public static function is_subscription_expired( $user ) {
  78. $expiration_timestamp = self::get_expiration_timestamp( $user );
  79. if ( $expiration_timestamp < time() ) {
  80. //Account expired.
  81. return true;
  82. }
  83. return false;
  84. }
  85. /*
  86. * Returns a formatted expiry date string (of a member). This can be useful to echo the date value.
  87. */
  88. public static function get_formatted_expiry_date( $start_date, $subscription_duration, $subscription_duration_type ) {
  89. if ( $subscription_duration_type == SwpmMembershipLevel::FIXED_DATE ) {
  90. //Membership will expire after a fixed date.
  91. return self::get_formatted_and_translated_date_according_to_wp_settings( $subscription_duration );
  92. }
  93. $expires = self::calculate_subscription_period_days( $subscription_duration, $subscription_duration_type );
  94. if ( $expires == 'noexpire' ) {
  95. //Membership is set to no expiry or until cancelled.
  96. return self::_( 'Never' );
  97. }
  98. //Membership is set to a duration expiry settings.
  99. return date_i18n( get_option( 'date_format' ), strtotime( $start_date . ' ' . $expires . ' days' ) );
  100. }
  101. public static function gender_dropdown( $selected = 'not specified' ) {
  102. return '<option ' . ( ( strtolower( $selected ) == 'male' ) ? 'selected="selected"' : '' ) . ' value="male">' . SwpmUtils::_('Male') . '</option>' .
  103. '<option ' . ( ( strtolower( $selected ) == 'female' ) ? 'selected="selected"' : '' ) . ' value="female">' . SwpmUtils::_('Female') . '</option>' .
  104. '<option ' . ( ( strtolower( $selected ) == 'not specified' ) ? 'selected="selected"' : '' ) . ' value="not specified">' . SwpmUtils::_('Not Specified') . '</option>';
  105. }
  106. public static function get_account_state_options() {
  107. return array(
  108. 'active' => self::_( 'Active' ),
  109. 'inactive' => self::_( 'Inactive' ),
  110. 'activation_required' => self::_( 'Activation Required' ),
  111. 'pending' => self::_( 'Pending' ),
  112. 'expired' => self::_( 'Expired' ),
  113. );
  114. }
  115. public static function account_state_dropdown( $selected = 'active' ) {
  116. $options = self::get_account_state_options();
  117. $html = '';
  118. foreach ( $options as $key => $value ) {
  119. $html .= '<option ' . ( ( strtolower( $selected ) == $key ) ? 'selected="selected"' : '' ) . ' value="' . $key . '"> ' . $value . '</option>';
  120. }
  121. return $html;
  122. }
  123. public static function membership_level_dropdown( $selected = 0 ) {
  124. $options = '';
  125. global $wpdb;
  126. $query = 'SELECT alias, id FROM ' . $wpdb->prefix . 'swpm_membership_tbl WHERE id != 1';
  127. $levels = $wpdb->get_results( $query );
  128. foreach ( $levels as $level ) {
  129. $options .= '<option ' . ( $selected == $level->id ? 'selected="selected"' : '' ) . ' value="' . $level->id . '" >' . $level->alias . '</option>';
  130. }
  131. return $options;
  132. }
  133. public static function get_all_membership_level_ids() {
  134. global $wpdb;
  135. $query = 'SELECT id FROM ' . $wpdb->prefix . 'swpm_membership_tbl WHERE id != 1';
  136. return $wpdb->get_col( $query );
  137. }
  138. public static function get_membership_level_row_by_id( $level_id ) {
  139. global $wpdb;
  140. $query = $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix . 'swpm_membership_tbl WHERE id=%d', $level_id );
  141. $level_resultset = $wpdb->get_row( $query );
  142. return $level_resultset;
  143. }
  144. public static function membership_level_id_exists( $level_id ) {
  145. //Returns true if the specified membership level exists in the system. Returns false if the level has been deleted (or doesn't exist).
  146. $all_level_ids = self::get_all_membership_level_ids();
  147. if ( in_array( $level_id, $all_level_ids ) ) {
  148. //Valid level ID
  149. return true;
  150. } else {
  151. return false;
  152. }
  153. }
  154. public static function get_registration_complete_prompt_link( $for = 'all', $send_email = false, $member_id = '' ) {
  155. $members = array();
  156. global $wpdb;
  157. switch ( $for ) {
  158. case 'one':
  159. if ( empty( $member_id ) ) {
  160. return array();
  161. }
  162. $query = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}swpm_members_tbl WHERE member_id = %d", $member_id );
  163. $members = $wpdb->get_results( $query );
  164. break;
  165. case 'all':
  166. $query = "SELECT * FROM {$wpdb->prefix}swpm_members_tbl WHERE reg_code != '' ";
  167. $members = $wpdb->get_results( $query );
  168. break;
  169. }
  170. $settings = SwpmSettings::get_instance();
  171. $separator = '?';
  172. $url = $settings->get_value( 'registration-page-url' );
  173. if ( strpos( $url, '?' ) !== false ) {
  174. $separator = '&';
  175. }
  176. $links = array();
  177. foreach ( $members as $member ) {
  178. $reg_url = $url . $separator . 'member_id=' . $member->member_id . '&code=' . $member->reg_code;
  179. if ( $send_email && empty( $member->user_name ) ) {
  180. $tags = array( '{first_name}', '{last_name}', '{reg_link}' );
  181. $vals = array( $member->first_name, $member->last_name, $reg_url );
  182. $subject = $settings->get_value( 'reg-prompt-complete-mail-subject' );
  183. if ( empty( $subject ) ) {
  184. $subject = 'Please complete your registration';
  185. }
  186. $body = $settings->get_value( 'reg-prompt-complete-mail-body' );
  187. if ( empty( $body ) ) {
  188. $body = "Please use the following link to complete your registration. \n {reg_link}";
  189. }
  190. $body = html_entity_decode( $body );
  191. $email_body = str_replace( $tags, $vals, $body );
  192. $from_address = $settings->get_value( 'email-from' );
  193. $headers = 'From: ' . $from_address . "\r\n";
  194. $subject = apply_filters( 'swpm_email_complete_your_registration_subject', $subject );
  195. $email_body = apply_filters( 'swpm_email_complete_your_registration_body', $email_body );
  196. SwpmMiscUtils::mail( $member->email, $subject, $email_body, $headers );
  197. SwpmLog::log_simple_debug( 'Prompt to complete registration email sent to: ' . $member->email . '. From email address value used: ' . $from_address, true );
  198. }
  199. $links[] = $reg_url;
  200. }
  201. return $links;
  202. }
  203. /* This function is deprecated and will be removed in the future. Use SwpmMemberUtils::update_wp_user_role() instead */
  204. public static function update_wp_user_Role( $wp_user_id, $role ) {
  205. // Deprecated function.
  206. SwpmMemberUtils::update_wp_user_role( $wp_user_id, $role );
  207. }
  208. public static function update_wp_user( $wp_user_name, $swpm_data ) {
  209. $wp_user_info = array();
  210. if ( isset( $swpm_data['email'] ) ) {
  211. $wp_user_info['user_email'] = $swpm_data['email'];
  212. }
  213. if ( isset( $swpm_data['first_name'] ) ) {
  214. $wp_user_info['first_name'] = $swpm_data['first_name'];
  215. }
  216. if ( isset( $swpm_data['last_name'] ) ) {
  217. $wp_user_info['last_name'] = $swpm_data['last_name'];
  218. }
  219. if ( isset( $swpm_data['plain_password'] ) ) {
  220. $wp_user_info['user_pass'] = $swpm_data['plain_password'];
  221. }
  222. $wp_user = get_user_by( 'login', $wp_user_name );
  223. if ( $wp_user ) {
  224. $wp_user_info['ID'] = $wp_user->ID;
  225. return wp_update_user( $wp_user_info );
  226. }
  227. return false;
  228. }
  229. public static function create_wp_user( $wp_user_data ) {
  230. //First, check if email or username belongs to an existing admin user.
  231. SwpmMemberUtils::check_and_die_if_email_belongs_to_admin_user($wp_user_data['user_email']);
  232. SwpmMemberUtils::check_and_die_if_username_belongs_to_admin_user($wp_user_data['user_login']);
  233. //At this point, the username or the email is not taken by any existing wp user with admin role.
  234. //Lets continue the normal registration process.
  235. //Check if the email belongs to an existing wp user account.
  236. $wp_user_id = email_exists( $wp_user_data['user_email'] );
  237. if ( $wp_user_id ) {
  238. //A wp user account exist with this email.
  239. //For signle site WP install, no new user will be created. The existing user ID will be returned.
  240. } else {
  241. //Check if the username belongs to an existing wp user account.
  242. $wp_user_id = username_exists( $wp_user_data['user_login'] );
  243. if ( $wp_user_id ) {
  244. //A wp user account exist with this username.
  245. //For signle site WP install, no new user will be created. The existing user ID will be returned.
  246. }
  247. }
  248. //At this point 1) A WP User with this email or username doesn't exist. Or 2) The associated wp user doesn't have admin role
  249. //Lets create a new wp user record or attach the SWPM profile to an existing user accordingly.
  250. if ( self::is_multisite_install() ) {
  251. //WP Multi-Site install
  252. global $blog_id;
  253. if ( $wp_user_id ) {
  254. //If user exists then just add him to current blog.
  255. add_existing_user_to_blog(
  256. array(
  257. 'user_id' => $wp_user_id,
  258. 'role' => 'subscriber',
  259. )
  260. );
  261. return $wp_user_id;
  262. }
  263. //No existing user. Create a new one.
  264. $wp_user_id = wpmu_create_user( $wp_user_data['user_login'], $wp_user_data['password'], $wp_user_data['user_email'] );
  265. $role = 'subscriber'; //TODO - add user as a subscriber first. The subsequent update user role function to update the role to the correct one
  266. add_user_to_blog( $blog_id, $wp_user_id, $role );
  267. //End of WPMS
  268. } else {
  269. //This is a WP Single Site install.
  270. //Lets see if an existing WP user exist from the email_exists() or username_exists() check earlier.
  271. if ( $wp_user_id ) {
  272. return $wp_user_id;
  273. }
  274. //No existing user. Try to create a brand new WP user entry.
  275. $wp_user_id = wp_create_user( $wp_user_data['user_login'], $wp_user_data['password'], $wp_user_data['user_email'] );
  276. //Update that newly created user's profile with additional data.
  277. $wp_user_data['ID'] = $wp_user_id;
  278. wp_update_user( $wp_user_data ); //Core WP function. Updates/Syncs the user info and role.
  279. }
  280. return $wp_user_id;
  281. }
  282. public static function is_multisite_install() {
  283. if ( function_exists( 'is_multisite' ) && is_multisite() ) {
  284. return true;
  285. } else {
  286. return false;
  287. }
  288. }
  289. public static function _( $msg ) {
  290. return __( $msg, 'simple-membership' );
  291. }
  292. public static function e( $msg ) {
  293. _e( $msg, 'simple-membership' );
  294. }
  295. /*
  296. * Deprecated. Instead use SwpmUtils::has_admin_management_permission()
  297. */
  298. public static function is_admin() {
  299. //This function returns true if the current user has WordPress admin management permission (not to be mistaken with SWPM admin permission.
  300. //This function is NOT like the WordPress's is_admin() function which determins if we are on the admin end of the site.
  301. //TODO - rename this function to something like is_admin_user()
  302. return current_user_can( 'manage_options' );
  303. }
  304. public static function has_admin_management_permission() {
  305. if ( current_user_can( SWPM_MANAGEMENT_PERMISSION ) ) {
  306. return true;
  307. } else {
  308. return false;
  309. }
  310. }
  311. /*
  312. * Returns the current date timestamp value suitable for debug log file.
  313. */
  314. public static function get_current_timestamp_for_debug_log(){
  315. $current_wp_time = current_time('mysql');
  316. $dt = new DateTime($current_wp_time);
  317. $current_date = $dt->format('Y/m/d H:i:s');
  318. return $current_date;
  319. }
  320. /*
  321. * Returns the current date value in (Y-m-d) format in the timzeone set for this WordPress install.
  322. */
  323. public static function get_current_date_in_wp_zone(){
  324. $current_wp_time = current_time('mysql');
  325. $dt = new DateTime($current_wp_time);
  326. $current_date = $dt->format('Y-m-d');
  327. return $current_date;
  328. }
  329. /*
  330. * Formats the given date value according to the WP date format settings. This function is useful for displaying a human readable date value to the user.
  331. */
  332. public static function get_formatted_date_according_to_wp_settings( $date ) {
  333. $date_format = get_option( 'date_format' );
  334. if ( empty( $date_format ) ) {
  335. //WordPress's date form settings is not set. Lets set a default format.
  336. $date_format = 'Y-m-d';
  337. }
  338. $date_obj = new DateTime( $date );
  339. $formatted_date = $date_obj->format( $date_format ); //Format the date value using date format settings
  340. return $formatted_date;
  341. }
  342. /*
  343. * Formats and Translates the given date value according to the WP date format settings. This function is useful for displaying a human readable date value to the user.
  344. * The $date argument value must be in nromal date format (2025-01-15). The function will use strtotime() function to convert it to unix time then use it.
  345. */
  346. public static function get_formatted_and_translated_date_according_to_wp_settings( $date ) {
  347. $date_format = get_option( 'date_format' );
  348. if ( empty( $date_format ) ) {
  349. //WordPress's date form settings is not set. Lets set a default format.
  350. $date_format = 'Y-m-d';
  351. }
  352. $formatted_translated_date = date_i18n( $date_format, strtotime( $date ) );
  353. return $formatted_translated_date;
  354. }
  355. public static function swpm_username_exists( $user_name ) {
  356. global $wpdb;
  357. $member_table = $wpdb->prefix . 'swpm_members_tbl';
  358. $query = $wpdb->prepare( 'SELECT member_id FROM ' . $member_table . ' WHERE user_name=%s', sanitize_user( $user_name ) );
  359. return $wpdb->get_var( $query );
  360. }
  361. public static function get_free_level() {
  362. $encrypted = filter_input( INPUT_POST, 'level_identifier' );
  363. if ( ! empty( $encrypted ) ) {
  364. return SwpmPermission::get_instance( $encrypted )->get( 'id' );
  365. }
  366. $is_free = SwpmSettings::get_instance()->get_value( 'enable-free-membership' );
  367. $free_level = absint( SwpmSettings::get_instance()->get_value( 'free-membership-id' ) );
  368. return ( $is_free ) ? $free_level : null;
  369. }
  370. public static function is_paid_registration() {
  371. $member_id = filter_input( INPUT_GET, 'member_id', FILTER_SANITIZE_NUMBER_INT );
  372. $code = filter_input( INPUT_GET, 'code', FILTER_SANITIZE_STRING );
  373. if ( ! empty( $member_id ) && ! empty( $code ) ) {
  374. return true;
  375. }
  376. return false;
  377. }
  378. public static function get_paid_member_info() {
  379. $member_id = filter_input( INPUT_GET, 'member_id', FILTER_SANITIZE_NUMBER_INT );
  380. $code = filter_input( INPUT_GET, 'code', FILTER_SANITIZE_STRING );
  381. global $wpdb;
  382. if ( ! empty( $member_id ) && ! empty( $code ) ) {
  383. $query = 'SELECT * FROM ' . $wpdb->prefix . 'swpm_members_tbl WHERE member_id= %d AND reg_code=%s';
  384. $query = $wpdb->prepare( $query, $member_id, $code );
  385. return $wpdb->get_row( $query );
  386. }
  387. return null;
  388. }
  389. public static function get_incomplete_paid_member_info_by_ip() {
  390. global $wpdb;
  391. $user_ip = self::get_user_ip_address();
  392. if ( ! empty( $user_ip ) ) {
  393. //Lets check if a payment has been confirmed from this user's IP and the profile needs to be completed (where username is empty).
  394. $username = '';
  395. $query = 'SELECT * FROM ' . $wpdb->prefix . 'swpm_members_tbl WHERE last_accessed_from_ip=%s AND user_name=%s';
  396. $query = $wpdb->prepare( $query, $user_ip, $username );
  397. $result = $wpdb->get_row( $query );
  398. return $result;
  399. }
  400. return null;
  401. }
  402. public static function account_delete_confirmation_ui( $msg = '' ) {
  403. ob_start();
  404. include SIMPLE_WP_MEMBERSHIP_PATH . 'views/account_delete_warning.php';
  405. ob_get_flush();
  406. wp_die( '', '', array( 'back_link' => true ) );
  407. }
  408. public static function delete_account_button() {
  409. $allow_account_deletion = SwpmSettings::get_instance()->get_value( 'allow-account-deletion' );
  410. if ( empty( $allow_account_deletion ) ) {
  411. return '';
  412. }
  413. $account_delete_link = '<div class="swpm-profile-account-delete-section">';
  414. $account_delete_link .= '<a href="' . SIMPLE_WP_MEMBERSHIP_SITE_HOME_URL . '/?swpm_delete_account=1"><div class="swpm-account-delete-button">' . self::_( 'Delete Account' ) . '</div></a>';
  415. $account_delete_link .= '</div>';
  416. return $account_delete_link;
  417. }
  418. public static function encrypt_password( $plain_password ) {
  419. include_once ABSPATH . WPINC . '/class-phpass.php';
  420. $wp_hasher = new PasswordHash( 8, true );
  421. $password_hash = $wp_hasher->HashPassword( trim( $plain_password ) );
  422. return $password_hash;
  423. }
  424. public static function get_restricted_image_url() {
  425. return SIMPLE_WP_MEMBERSHIP_URL . '/images/restricted-icon.png';
  426. }
  427. /*
  428. * Checks if the string exists in the array key value of the provided array. If it doesn't exist, it returns the first key element from the valid values.
  429. */
  430. public static function sanitize_value_by_array( $val_to_check, $valid_values ) {
  431. $keys = array_keys( $valid_values );
  432. $keys = array_map( 'strtolower', $keys );
  433. if ( in_array( $val_to_check, $keys ) ) {
  434. return $val_to_check;
  435. }
  436. return reset( $keys ); //Return he first element from the valid values
  437. }
  438. public static function get_user_ip_address() {
  439. $user_ip = '';
  440. if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) && ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
  441. $user_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  442. } else {
  443. $user_ip = $_SERVER['REMOTE_ADDR'];
  444. }
  445. if ( strstr( $user_ip, ',' ) ) {
  446. $ip_values = explode( ',', $user_ip );
  447. $user_ip = $ip_values['0'];
  448. }
  449. return apply_filters( 'swpm_get_user_ip_address', $user_ip );
  450. }
  451. public static function is_first_click_free( &$content ) {
  452. $is_first_click = false;
  453. $args = array( $is_first_click, $content );
  454. $filtered = apply_filters( 'swpm_first_click_free', $args );
  455. list($is_first_click, $content) = $filtered;
  456. return $is_first_click;
  457. }
  458. private static function crypt_fallback( $string, $action = 'e' ) {
  459. if ( $action === 'e' ) {
  460. return base64_encode( $string );
  461. } else {
  462. return base64_decode( $string );
  463. }
  464. }
  465. public static function crypt( $string, $action = 'e' ) {
  466. //check if openssl module is enabled
  467. if ( ! extension_loaded( 'openssl' ) ) {
  468. // no openssl extension loaded. Can't ecnrypt
  469. return self::crypt_fallback( $string, $action );
  470. }
  471. //check if encrypt method is supported
  472. $encrypt_method = 'aes-256-ctr';
  473. $available_methods = openssl_get_cipher_methods();
  474. if ( ! in_array( $encrypt_method, $available_methods ) ) {
  475. // no ecryption method supported. Can't encrypt
  476. return self::crypt_fallback( $string, $action );
  477. }
  478. $output = false;
  479. $secret_key = wp_salt( 'auth' );
  480. $secret_iv = wp_salt( 'secure_auth' );
  481. $key = hash( 'sha256', $secret_key );
  482. $iv = substr( hash( 'sha256', $secret_iv ), 0, 16 );
  483. if ( $action == 'e' ) {
  484. $output = base64_encode( openssl_encrypt( $string, $encrypt_method, $key, 0, $iv ) );
  485. } elseif ( $action == 'd' ) {
  486. $output = openssl_decrypt( base64_decode( $string ), $encrypt_method, $key, 0, $iv );
  487. }
  488. return $output;
  489. }
  490. }