1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 07:33:04 +01:00
invoiceninja/app/Utils/Ninja.php

237 lines
6.5 KiB
PHP
Raw Normal View History

2019-06-04 00:08:13 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
2019-06-04 00:08:13 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2019-06-04 00:08:13 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2019-06-04 00:08:13 +02:00
*/
namespace App\Utils;
use Illuminate\Support\Facades\DB;
2023-06-12 13:12:33 +02:00
use Illuminate\Support\Facades\Http;
2019-06-04 00:08:13 +02:00
/**
* Class Ninja.
*/
class Ninja
{
2024-01-14 05:05:00 +01:00
public const TEST_USERNAME = 'user@example.com';
public static function isSelfHost()
{
return config('ninja.environment') === 'selfhost';
}
public static function isHosted()
{
return config('ninja.environment') === 'hosted';
}
public static function isNinja()
{
return config('ninja.production');
}
public static function isNinjaDev()
{
return config('ninja.environment') == 'development';
}
public static function getDebugInfo()
{
2023-09-04 02:27:28 +02:00
$mysql_version = DB::select('select version() as version')[0]->version;
$version = request()->input('version', 'No Version Supplied.');
$info = 'App Version: v'.config('ninja.app_version').'\\n'.
'White Label: '.'\\n'. // TODO: Implement white label with hasFeature.
'Server OS: '.php_uname('s').' '.php_uname('r').'\\n'.
'PHP Version: '.phpversion().'\\n'.
2022-04-27 01:39:52 +02:00
'MySQL Version: '.$mysql_version.'\\n'.
'Version: '.$version;
2019-06-04 00:08:13 +02:00
return $info;
}
public static function boot()
{
if (self::isNinjaDev()) {
return true;
}
$data = [
'license' => config('ninja.license'),
];
2023-03-06 03:15:11 +01:00
$data = trim(CurlUtils::post('https://license.invoicing.co/api/check', $data));
$data = json_decode($data);
if ($data && property_exists($data, 'message') && $data->message == sha1(config('ninja.license'))) {
return true;
} else {
return false;
}
}
public static function parse()
{
return 'Invalid license.';
}
public static function selfHostedMessage()
{
return 'Self hosted installation limited to one account';
}
public static function registerNinjaUser($user)
{
if (! $user || $user->email == self::TEST_USERNAME || self::isNinjaDev()) {
return false;
}
$url = config('ninja.license_url').'/signup/register';
$data = '';
$fields = [
'first_name' => urlencode($user->first_name),
'last_name' => urlencode($user->last_name),
'email' => urlencode($user->email),
];
foreach ($fields as $key => $value) {
$data .= $key.'='.$value.'&';
}
$data = rtrim($data, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
}
2020-07-08 14:02:16 +02:00
public static function eventVars($user_id = null)
2020-07-08 14:02:16 +02:00
{
$ip = '';
2021-11-11 10:35:07 +01:00
if (request()->hasHeader('Cf-Connecting-Ip')) {
$ip = request()->header('Cf-Connecting-Ip');
} elseif (request()->hasHeader('X-Forwarded-For')) {
$ip = request()->header('X-Forwarded-For');
} else {
$ip = request()->ip() ?: ' ';
}
2021-11-11 10:35:07 +01:00
2020-07-08 14:02:16 +02:00
return [
2021-11-11 10:35:07 +01:00
'ip' => $ip,
2020-07-08 14:02:16 +02:00
'token' => request()->header('X-API-TOKEN'),
'is_system' => app()->runningInConsole(),
'user_id' => (app()->runningInConsole() && $ip == '127.0.0.1') ? null : $user_id,
2020-07-08 14:02:16 +02:00
];
}
2020-11-10 04:36:16 +01:00
2024-01-14 05:05:00 +01:00
public static function transformTranslations($settings): array
2020-11-10 04:36:16 +01:00
{
$translations = [];
$trans = (array) $settings->translations;
2020-11-10 04:36:16 +01:00
2020-11-25 15:19:52 +01:00
if (count($trans) == 0) {
2020-11-10 04:36:16 +01:00
return $translations;
2020-11-25 15:19:52 +01:00
}
2020-11-10 04:36:16 +01:00
2020-11-25 15:19:52 +01:00
foreach ($trans as $key => $value) {
$translations['texts.'.$key] = $value;
2020-11-10 04:36:16 +01:00
}
return $translations;
}
2021-04-06 16:00:24 +02:00
2023-06-12 13:12:33 +02:00
public static function triggerForwarding(string $company_key, string $email)
{
2023-06-14 00:14:14 +02:00
try {
2023-06-18 05:35:51 +02:00
$x = Http::withHeaders([
2023-06-14 00:14:14 +02:00
'X-API-HOSTED-SECRET' => config('ninja.ninja_hosted_secret'),
])->post(config('ninja.license_url').'/api/v1/enable_forwarding', [
'account_key' => $company_key,
'email' => $email,
]);
2023-06-18 05:35:51 +02:00
nlog($x->body());
2023-10-26 04:57:44 +02:00
} catch (\Exception $e) {
2023-06-18 05:35:51 +02:00
nlog("Attempt forwarding for {$email} - {$company_key} Failed");
2023-06-14 00:14:14 +02:00
}
2023-06-12 13:12:33 +02:00
}
2021-04-06 16:00:24 +02:00
public function createLicense($request)
{
// $affiliate = Affiliate::where('affiliate_key', '=', SELF_HOST_AFFILIATE_KEY)->first();
// $email = trim(Input::get('email'));
// if (! $email || $email == TEST_USERNAME) {
// return RESULT_FAILURE;
// }
// $license = new License();
// $license->first_name = Input::get('first_name');
// $license->last_name = Input::get('last_name');
// $license->email = $email;
// $license->transaction_reference = Request::getClientIp();
// $license->license_key = self::generateLicense();
// $license->affiliate_id = $affiliate->id;
// $license->product_id = PRODUCT_SELF_HOST;
// $license->is_claimed = 1;
// $license->save();
// return RESULT_SUCCESS;
}
// public static function generateLicense()
// {
// $parts = [];
// for ($i = 0; $i < 5; $i++) {
// $parts[] = strtoupper(str_random(4));
// }
// return implode('-', $parts);
// }
//
/*
* Available - but not recommended for use
*
* This will guarantee a given string IS the correct format for a
* base64 encoded string ,
* but can't guarantee that it is a base64 encoded string
*
*/
2024-01-14 05:05:00 +01:00
public static function isBase64Encoded(string $s): bool
{
2023-02-16 02:36:09 +01:00
// Check if there are valid base64 characters
if (! preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $s)) {
return false;
}
// Decode the string in strict mode and check the results
$decoded = base64_decode($s, true);
if (false === $decoded) {
return false;
}
// if string returned contains not printable chars
if (0 < preg_match('/((?![[:graph:]])(?!\s)(?!\p{L}))./', $decoded, $matched)) {
return false;
}
// Encode the string again
if (base64_encode($decoded) != $s) {
return false;
}
return true;
}
2019-06-04 00:08:13 +02:00
}