1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Http/Controllers/LicenseController.php

213 lines
7.1 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\Controllers;
use App\Models\Account;
use App\Utils\CurlUtils;
2023-03-18 08:24:56 +01:00
use Carbon\Carbon;
2020-10-28 11:10:49 +01:00
use Illuminate\Http\Response;
2023-03-10 03:01:32 +01:00
use Illuminate\Support\Facades\Http;
2023-03-18 08:24:56 +01:00
use stdClass;
class LicenseController extends BaseController
{
public function __construct()
{
parent::__construct();
}
/**
* Claim a white label license.
*
2020-10-28 11:10:49 +01:00
* @return Response
*
* @OA\Get(
* path="/api/v1/claim_license",
* operationId="getClaimLicense",
* tags={"claim_license"},
* summary="Attempts to claim a white label license",
* description="Attempts to claim a white label license",
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(
* name="license_key",
2020-07-30 05:27:00 +02:00
* in="query",
* description="The license hash",
* example="d87sh-s755s-s7d76-sdsd8",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Parameter(
* name="product_id",
2020-07-30 05:27:00 +02:00
* in="query",
* description="The ID of the product purchased.",
* example="1",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Success!",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
2023-03-15 21:35:44 +01:00
public function index()
{
2021-04-20 14:45:35 +02:00
$this->checkLicense();
/* Catch claim license requests */
if (config('ninja.environment') == 'selfhost' && request()->has('license_key')) {
$license_key = request()->input('license_key');
$product_id = 3;
2023-03-20 10:17:04 +01:00
if(substr($license_key, 0, 3) == 'v5_') {
return $this->v5ClaimLicense($license_key, $product_id);
2023-10-26 04:57:44 +02:00
}
2023-03-20 10:17:04 +01:00
$url = config('ninja.license_url')."/claim_license?license_key={$license_key}&product_id={$product_id}&get_date=true";
$data = trim(CurlUtils::get($url));
if ($data == Account::RESULT_FAILURE) {
$error = [
'message' => trans('texts.invalid_white_label_license'),
2024-01-14 05:05:00 +01:00
'errors' => new stdClass(),
];
return response()->json($error, 400);
} elseif ($data) {
$date = date_create($data)->modify('+1 year');
if ($date < date_create()) {
$error = [
'message' => trans('texts.invalid_white_label_license'),
2024-01-14 05:05:00 +01:00
'errors' => new stdClass(),
];
2021-11-26 21:41:13 +01:00
$account = auth()->user()->account;
2021-05-20 23:58:46 +02:00
$account->plan_term = Account::PLAN_TERM_YEARLY;
$account->plan_paid = null;
$account->plan_expires = null;
$account->plan = Account::PLAN_FREE;
$account->save();
return response()->json($error, 400);
} else {
2021-11-26 21:41:13 +01:00
$account = auth()->user()->account;
$account->plan_term = Account::PLAN_TERM_YEARLY;
$account->plan_paid = $data;
$account->plan_expires = $date->format('Y-m-d');
$account->plan = Account::PLAN_WHITE_LABEL;
$account->save();
$error = [
'message' => trans('texts.bought_white_label'),
2024-01-14 05:05:00 +01:00
'errors' => new stdClass(),
];
return response()->json($error, 200);
}
} else {
$error = [
2023-08-02 11:10:39 +02:00
'message' => 'There was an issue connecting to the license server. Please check your network.',
2024-01-14 05:05:00 +01:00
'errors' => new stdClass(),
];
return response()->json($error, 400);
}
}
$error = [
2021-01-25 00:04:50 +01:00
'message' => ctrans('texts.invoice_license_or_environment', ['environment' => config('ninja.environment')]),
2024-01-14 05:05:00 +01:00
'errors' => new stdClass(),
];
return response()->json($error, 400);
}
2021-04-20 14:45:35 +02:00
2023-03-20 10:17:04 +01:00
public function v5ClaimLicense(string $license_key)
2023-03-10 03:01:32 +01:00
{
$this->checkLicense();
/* Catch claim license requests */
2023-03-20 10:17:04 +01:00
if (config('ninja.environment') == 'selfhost') {
2023-03-10 03:21:07 +01:00
// $response = Http::get( "http://ninja.test:8000/claim_license", [
2023-03-18 08:24:56 +01:00
$response = Http::get("https://invoicing.co/claim_license", [
2023-03-20 10:17:04 +01:00
'license_key' => $license_key,
2023-03-10 03:01:32 +01:00
'product_id' => 3,
]);
2023-03-18 08:24:56 +01:00
if ($response->successful()) {
2023-03-10 03:01:32 +01:00
$payload = $response->json();
$account = auth()->user()->account;
$account->plan_term = Account::PLAN_TERM_YEARLY;
2023-03-10 03:19:43 +01:00
$account->plan_expires = Carbon::parse($payload['expires'])->addYear()->format('Y-m-d');
2023-03-10 03:01:32 +01:00
$account->plan = Account::PLAN_WHITE_LABEL;
$account->save();
$error = [
'message' => trans('texts.bought_white_label'),
2024-01-14 05:05:00 +01:00
'errors' => new \stdClass(),
2023-03-10 03:01:32 +01:00
];
return response()->json($error, 200);
2023-03-18 08:24:56 +01:00
} else {
2023-03-10 03:01:32 +01:00
$error = [
'message' => trans('texts.white_label_license_error'),
2024-01-14 05:05:00 +01:00
'errors' => new stdClass(),
2023-03-10 03:01:32 +01:00
];
return response()->json($error, 400);
}
}
$error = [
'message' => ctrans('texts.invoice_license_or_environment', ['environment' => config('ninja.environment')]),
2024-01-14 05:05:00 +01:00
'errors' => new stdClass(),
2023-03-10 03:01:32 +01:00
];
return response()->json($error, 400);
}
2021-04-20 14:45:35 +02:00
private function checkLicense()
{
2021-11-26 21:41:13 +01:00
$account = auth()->user()->account;
2021-04-20 14:45:35 +02:00
if ($account->plan == 'white_label' && Carbon::parse($account->plan_expires)->lt(now())) {
2021-04-20 14:45:35 +02:00
$account->plan = null;
$account->plan_expires = null;
$account->save();
}
}
}