| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Http\Controllers\WebController; |
| 4 |
|
| 5 |
use FluentCart\App\App; |
| 6 |
use FluentCart\App\Helpers\Status; |
| 7 |
use FluentCart\App\Http\Controllers\Controller; |
| 8 |
use FluentCart\App\Models\Order; |
| 9 |
use FluentCart\App\Models\ProductDownload; |
| 10 |
use FluentCart\App\Services\DateTime\DateTime; |
| 11 |
use FluentCart\App\Services\FrontendView; |
| 12 |
use FluentCart\Framework\Database\Orm\Builder; |
| 13 |
use FluentCart\Framework\Http\Request\Request; |
| 14 |
use FluentCart\Framework\Http\URL; |
| 15 |
use FluentCart\Framework\Support\Arr; |
| 16 |
|
| 17 |
class FileDownloader extends Controller |
| 18 |
{ |
| 19 |
/** |
| 20 |
* @param Request $request |
| 21 |
* @return void |
| 22 |
*/ |
| 23 |
public function index(Request $request) |
| 24 |
{ |
| 25 |
$url = new URL(); |
| 26 |
$validated = $url->validate($request->getFullUrl()); |
| 27 |
$validTill = Arr::get($validated, 'valid_till'); |
| 28 |
|
| 29 |
if (empty($validTill)) { |
| 30 |
$this->returnError(); |
| 31 |
} |
| 32 |
|
| 33 |
try { |
| 34 |
$validTill = DateTime::anytimeToGmt($validTill); |
| 35 |
} catch (\Exception $e) { |
| 36 |
$this->returnError(); |
| 37 |
} |
| 38 |
|
| 39 |
if ($validTill < DateTime::now()) { |
| 40 |
$this->returnError(); |
| 41 |
} |
| 42 |
|
| 43 |
$downloadIdentifier = Arr::get($validated, 'download_identifier'); |
| 44 |
//dsd($downloadIdentifier); |
| 45 |
$download = ProductDownload::query()->where('download_identifier', $downloadIdentifier)->first(); |
| 46 |
|
| 47 |
if (empty($downloadIdentifier) || !$download) { |
| 48 |
$this->returnError(); |
| 49 |
} |
| 50 |
|
| 51 |
$orderIds = Arr::get($validated, 'order_id', ''); |
| 52 |
if ($orderIds) { |
| 53 |
$orderIds = json_decode($orderIds, true); |
| 54 |
if (!$orderIds) { |
| 55 |
$orderIds = []; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
if (!empty($orderIds) && is_array($orderIds)) { |
| 60 |
$orders = Order::query() |
| 61 |
->where(function (Builder $query) { |
| 62 |
$query->whereIn('payment_status', Status::getOrderPaymentSuccessStatuses()); |
| 63 |
}) |
| 64 |
->whereHas('order_items', function ($query) use ($download) { |
| 65 |
$query->where('post_id', $download->post_id); |
| 66 |
}) |
| 67 |
->with('subscriptions') |
| 68 |
->whereIn('id', $orderIds) |
| 69 |
->get(); |
| 70 |
|
| 71 |
if ($orders->isEmpty()) { |
| 72 |
$this->returnError(); |
| 73 |
} |
| 74 |
|
| 75 |
$canBeDownloaded = false; |
| 76 |
|
| 77 |
foreach ($orders as $order) { |
| 78 |
if ($canBeDownloaded) { |
| 79 |
break; |
| 80 |
} |
| 81 |
|
| 82 |
if ($order->subscriptions && $order->subscriptions->isNotEmpty()) { |
| 83 |
foreach ($order->subscriptions as $subscription) { |
| 84 |
if ($subscription->hasAccessValidity()) { |
| 85 |
$canBeDownloaded = true; |
| 86 |
break; |
| 87 |
} |
| 88 |
} |
| 89 |
continue; |
| 90 |
} |
| 91 |
|
| 92 |
$canBeDownloaded = true; |
| 93 |
} |
| 94 |
|
| 95 |
$canBeDownloaded = apply_filters('fluent_cart/product_download/can_be_downloaded', $canBeDownloaded, [ |
| 96 |
'orders' => $orders, |
| 97 |
'download' => $download |
| 98 |
]); |
| 99 |
|
| 100 |
if (!$canBeDownloaded) { |
| 101 |
$this->returnError(); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
if (is_array($validated) && !empty($validated['download_identifier'])) { |
| 107 |
$downloaded = \FluentCart\App\Services\FileSystem\DownloadService::downloadFileFromId($validated['download_identifier']); |
| 108 |
if(is_wp_error($downloaded)){ |
| 109 |
|
| 110 |
if(is_user_logged_in() && current_user_can('manage_options')){ |
| 111 |
|
| 112 |
$settingsUrl = admin_url('admin.php?page=fluent-cart#/settings/storage/' . $download->driver); |
| 113 |
|
| 114 |
FrontendView::renderNotFoundPage( |
| 115 |
__('Driver Error', 'fluent-cart'), |
| 116 |
__('Configuration Error', 'fluent-cart'), |
| 117 |
sprintf( |
| 118 |
/* translators: %s is the driver name */ |
| 119 |
__('The storage driver %s is not configured correctly.', 'fluent-cart'), |
| 120 |
$download->driver |
| 121 |
), |
| 122 |
|
| 123 |
__('Check Storage Settings', 'fluent-cart'), |
| 124 |
'', |
| 125 |
$settingsUrl |
| 126 |
); |
| 127 |
}else{ |
| 128 |
FrontendView::renderFileNotFoundPage(); |
| 129 |
} |
| 130 |
} |
| 131 |
} else { |
| 132 |
$this->returnError(); |
| 133 |
} |
| 134 |
|
| 135 |
exit; |
| 136 |
} |
| 137 |
|
| 138 |
public function returnError() |
| 139 |
{ |
| 140 |
if (App::request()->wantsJson()) { |
| 141 |
wp_send_json(['message' => __('Invalid download Url', 'fluent-cart')], 422); |
| 142 |
} else { |
| 143 |
FrontendView::renderFileNotFoundPage(); |
| 144 |
} |
| 145 |
|
| 146 |
die(); |
| 147 |
} |
| 148 |
|
| 149 |
} |
| 150 |
|