isNotEmpty(); } /** * Check if given user has access to given node. * * @param $node * @param null $account * @return bool */ function mkbh_paywall_is_user_has_access($node, $account = null): bool { global $user; if ($account === null) { $account = $user; } if (!mkbh_paywall_is_node_enabled($node) || user_access('administer nodes')) { if ($_GET['paywall_debug'] ?? false) { watchdog('mkbh_paywall_is_user_has_access', json_encode(['result' => true, 'reason' => 'Node is not paywalled or user has admin access.']), [], WATCHDOG_DEBUG); } return true; } $_SESSION['paywall_last_checked_node_nid'] = $node->nid; $clientIp = ip_address(); $matcher = new \CIDRmatch\CIDRmatch(); $matchedIPs = collect(variable_get('paywall_ip_whitelist', []))->filter(function($ip) use($clientIp, $matcher, &$debug) { return $matcher->match($clientIp, $ip); }); if($matchedIPs->isNotEmpty()) { if ($_GET['paywall_debug'] ?? false) { watchdog('mkbh_paywall_is_user_has_access', json_encode(['result' => true, 'reason' => 'User IP is whitelisted.']), [], WATCHDOG_DEBUG); } return true; } $nodePlans = mkbh_paywall_node_get_plans($node); $userPlans = mkbh_paywall_user_get_plans($account); if ($userPlans->isEmpty()) { if ($_GET['paywall_debug'] ?? false) { watchdog('mkbh_paywall_is_user_has_access', json_encode(['result' => false, 'reason' => 'User has no active plans.']), [], WATCHDOG_DEBUG); } return false; } $result = $nodePlans->intersect($userPlans)->isNotEmpty(); if ($_GET['paywall_debug'] ?? false) { watchdog('mkbh_paywall_is_user_has_access', json_encode(['result' => $result, 'reason' => 'User has access to node.', '$nodePlans' => $nodePlans, '$userPlans' => $userPlans]), [], WATCHDOG_DEBUG); } return $result; } /** * Get plans associated with given node. * * @param $node * @return \Illuminate\Support\Collection */ function mkbh_paywall_node_get_plans($node) { $nid = (int)data_get($node, 'nid'); if ($nid) { $plans = database()->table('mkbh_paywall_node_options')->where('nid', '=', $nid)->pluck('plan_id'); return $plans ->flatMap(function($plan) { if ($plan === MKBH_DONATE_PERSONAL_BASIS_PLAN_ID) { return [MKBH_DONATE_PERSONAL_BASIS_PLAN_ID, MKBH_DONATE_PERSONAL_BASIS_YEAR_PLAN_ID]; } if ($plan === MKBH_DONATE_PERSONAL_PLUS_PLAN_ID) { return [MKBH_DONATE_PERSONAL_PLUS_PLAN_ID, MKBH_DONATE_PERSONAL_PLUS_YEAR_PLAN_ID]; } return [$plan]; }) ->mapWithKeys(function ($plan) { return [$plan => $plan]; }); } return collect(); } /** * Get plans associated with given user. * * @param $account * @return \Illuminate\Support\Collection|\Tightenco\Collect\Support\Collection */ function mkbh_paywall_user_get_plans($account = null) { global $user; if ($account === null) { $account = $user; } $userPlans = collect(); if((int)$account->uid === 0) { return $userPlans; } $businessInfo = database()->table('mkbh_business_info') ->where('master_uid', '=', $account->uid) ->first(); if ($businessInfo !== null && ((bool)$businessInfo->master_takes_seat) === false) { return $userPlans; } $subscription = database()->table('mkbh_stripe_subscriptions')->where('user_id', '=', (int)$account->uid)->first(); if ($subscription !== null) { $userPlans->push($subscription->plan_id); } if ($_GET['paywall_debug'] ?? false) { watchdog('mkbh_paywall_user_get_plans', json_encode(['step' => 'fetch_stripe_plans', '$userPlans' => $userPlans]), [], WATCHDOG_DEBUG); } $manualPlans = mkbh_general_stripe_manual_plan_ids($account); if ($_GET['paywall_debug'] ?? false) { watchdog('mkbh_paywall_user_get_plans', json_encode(['step' => 'fetch_manual_plans', '$manualPlans' => $manualPlans, '$userPlans' => $userPlans]), [], WATCHDOG_DEBUG); } /** @var MobilePayRepository $mobilePayRepository */ $mobilePayRepository = container()->make(MobilePayRepository::class); $agreements = $mobilePayRepository->findAllForAccount($account)->filter(function($agreement) { if (!$agreement->last_payment_at) { if ($_GET['paywall_debug'] ?? false) { watchdog('mkbh_paywall_user_get_plans', json_encode(['step' => 'process_mobile_pay_agreements', '$agreement' => $agreement]), [], WATCHDOG_DEBUG); } return false; } try { $gracePeriod = Carbon\Carbon::createFromTimestamp( $agreement->last_payment_at, 'UTC' ); switch ($agreement->frequency_unit) { case MobilePayIntervalEnum::DAY: $gracePeriod->addDays($agreement->frequency); break; case MobilePayIntervalEnum::WEEK: $gracePeriod->addWeeks($agreement->frequency); break; case MobilePayIntervalEnum::MONTH: $gracePeriod->addMonths($agreement->frequency); break; case MobilePayIntervalEnum::YEAR: $gracePeriod->addYears($agreement->frequency); break; default: return false; } $gracePeriodActive = Carbon\Carbon::now('UTC')->lte($gracePeriod); if ($_GET['paywall_debug'] ?? false) { watchdog('mkbh_paywall_user_get_plans', json_encode(['step' => 'process_mobile_pay_agreements', '$gracePeriod' => $gracePeriod->toIso8601String(), '$gracePeriodActive' => $gracePeriodActive]), [], WATCHDOG_DEBUG); } return $gracePeriodActive; } catch(\Exception $e) { return false; } }); if ($agreements->isNotEmpty()) { $manualPlans = $manualPlans->merge( $agreements->pluck('plan_id') ); if ($_GET['paywall_debug'] ?? false) { watchdog('mkbh_paywall_user_get_plans', json_encode(['step' => 'merged_agreements', '$manualPlans' => $manualPlans]), [], WATCHDOG_DEBUG); } } if ($businessSubscriptionPlan = mkbh_general_stripe_business_get_business_plan_id_by_email($account->mail)) { $manualPlans = $manualPlans->merge([ $businessSubscriptionPlan, ]); if ($_GET['paywall_debug'] ?? false) { watchdog('mkbh_paywall_user_get_plans', json_encode(['step' => 'merged_business_subscription', '$manualPlans' => $manualPlans]), [], WATCHDOG_DEBUG); } } $userPlans = $userPlans->merge($manualPlans); if ($_GET['paywall_debug'] ?? false) { watchdog('mkbh_paywall_user_get_plans', json_encode(['step' => 'final_user_plans', '$userPlans' => $userPlans]), [], WATCHDOG_DEBUG); } return $userPlans; } /** * Get last checked paywall node. * * @return false|mixed|null */ function mkbh_paywall_get_last_checked_node() { if (isset($_SESSION['paywall_last_checked_node_nid']) && is_numeric($_SESSION['paywall_last_checked_node_nid'])) { return node_load($_SESSION['paywall_last_checked_node_nid']); } return null; }