mirror of
https://gitlab.linphone.org/BC/public/flexisip-account-manager.git
synced 2026-01-17 18:08:06 +00:00
453 lines
13 KiB
PHP
453 lines
13 KiB
PHP
<?php
|
||
/*
|
||
Flexisip Account Manager is a set of tools to manage SIP accounts.
|
||
Copyright (C) 2020 Belledonne Communications SARL, All rights reserved.
|
||
|
||
This program is free software: you can redistribute it and/or modify
|
||
it under the terms of the GNU Affero General Public License as
|
||
published by the Free Software Foundation, either version 3 of the
|
||
License, or (at your option) any later version.
|
||
|
||
This program is distributed in the hope that it will be useful,
|
||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
GNU Affero General Public License for more details.
|
||
|
||
You should have received a copy of the GNU Affero General Public License
|
||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
*/
|
||
|
||
use Illuminate\Support\Str;
|
||
|
||
use App\Account;
|
||
use App\Space;
|
||
use App\DigestNonce;
|
||
use Illuminate\Http\Request;
|
||
use League\CommonMark\CommonMarkConverter;
|
||
use League\CommonMark\Extension\HeadingPermalink\HeadingPermalinkExtension;
|
||
use League\CommonMark\Extension\TableOfContents\TableOfContentsExtension;
|
||
use Illuminate\Support\Facades\DB;
|
||
|
||
function space(): ?Space
|
||
{
|
||
return is_object(request()->space) ? request()->space :null;
|
||
}
|
||
|
||
function passwordAlgorithms(): array
|
||
{
|
||
return [
|
||
'MD5' => 'md5',
|
||
'SHA-256' => 'sha256',
|
||
];
|
||
}
|
||
|
||
function generateNonce(): string
|
||
{
|
||
return Str::random(32);
|
||
}
|
||
|
||
function getRequestBoolean(Request $request, string $key, bool $reversed = false): bool
|
||
{
|
||
$bool = $request->has($key) ? $request->get($key) == "on" : false;
|
||
|
||
return $reversed ? !$bool : $bool;
|
||
}
|
||
|
||
function generateValidNonce(Account $account): string
|
||
{
|
||
$nonce = new DigestNonce();
|
||
$nonce->account_id = $account->id;
|
||
$nonce->nonce = generateNonce();
|
||
$nonce->save();
|
||
|
||
return $nonce->nonce;
|
||
}
|
||
|
||
function bchash(string $username, string $domain, string $password, string $algorithm = 'MD5'): string
|
||
{
|
||
return hash(passwordAlgorithms()[$algorithm], $username . ':' . $domain . ':' . $password);
|
||
}
|
||
|
||
function generatePin(): int
|
||
{
|
||
return mt_rand(1000, 9999);
|
||
}
|
||
|
||
function percent($value, $max): float
|
||
{
|
||
if ($max == 0) {
|
||
$max = 1;
|
||
}
|
||
return round(($value * 100) / $max, 2);
|
||
}
|
||
|
||
function markdownDocumentationView(string $view): string
|
||
{
|
||
$converter = new CommonMarkConverter([
|
||
'heading_permalink' => [
|
||
'html_class' => 'permalink',
|
||
'insert' => 'after',
|
||
'title' => 'Permalink',
|
||
'id_prefix' => '',
|
||
'fragment_prefix' => '',
|
||
],
|
||
'table_of_contents' => [
|
||
'html_class' => 'table-of-contents float-right',
|
||
],
|
||
]);
|
||
|
||
$converter->getEnvironment()->addExtension(new HeadingPermalinkExtension());
|
||
$converter->getEnvironment()->addExtension(new TableOfContentsExtension());
|
||
|
||
return (string) $converter->convert(
|
||
(string)view($view, [
|
||
'app_name' => space()->name
|
||
])->render()
|
||
);
|
||
}
|
||
|
||
function hasCoTURNConfigured(): bool
|
||
{
|
||
return config('app.coturn_session_ttl_minutes') > 0
|
||
&& !empty(config('app.coturn_server_host'))
|
||
&& !empty(config('app.coturn_static_auth_secret'));
|
||
}
|
||
|
||
function getCoTURNCredentials(): array
|
||
{
|
||
$user = Str::random(8);
|
||
$secret = config('app.coturn_static_auth_secret');
|
||
|
||
$ttl = config('app.coturn_session_ttl_minutes') * 60;
|
||
$time = time() + $ttl;
|
||
|
||
$username = $time . ':' . Str::random(16);
|
||
$password = base64_encode(hash_hmac('sha1', $username, $secret, true));
|
||
|
||
return [
|
||
'username' => $username,
|
||
'password' => $password,
|
||
];
|
||
}
|
||
|
||
function parseSIP(string $sipAdress): array
|
||
{
|
||
return explode('@', \substr($sipAdress, 4));
|
||
}
|
||
|
||
function isRegularExpression(string $string): bool
|
||
{
|
||
set_error_handler(function () {
|
||
}, E_WARNING);
|
||
|
||
$isRegularExpression = preg_match($string, '') !== false;
|
||
restore_error_handler();
|
||
|
||
return $isRegularExpression;
|
||
}
|
||
|
||
function replaceHost(string $url, string $host): string
|
||
{
|
||
$components = parse_url($url);
|
||
return str_replace($components['host'], $host, $url);
|
||
}
|
||
|
||
function resolveDomain(Request $request): string
|
||
{
|
||
return $request->has('domain')
|
||
&& $request->user()
|
||
&& $request->user()->superAdmin
|
||
? $request->get('domain')
|
||
: $request->space->domain;
|
||
}
|
||
|
||
function captchaConfigured(): bool
|
||
{
|
||
return env('HCAPTCHA_SECRET', false) != false || env('HCAPTCHA_SITEKEY', false) != false;
|
||
}
|
||
|
||
function resolveUserContacts(Request $request)
|
||
{
|
||
$selected = ['id', 'username', 'domain', 'activated', 'dtmf_protocol', 'display_name'];
|
||
|
||
return Account::withoutGlobalScopes()->whereIn('id', function ($query) use ($request) {
|
||
$query->select('contact_id')
|
||
->from('contacts')
|
||
->where('account_id', $request->user()->id)
|
||
->union(
|
||
DB::table('contacts_list_contact')
|
||
->select('contact_id')
|
||
->whereIn('contacts_list_id', function ($query) use ($request) {
|
||
$query->select('contacts_list_id')
|
||
->from('account_contacts_list')
|
||
->where('account_id', $request->user()->id);
|
||
})
|
||
);
|
||
})->select($selected);
|
||
}
|
||
|
||
/**
|
||
* Validate date string to ISO8601
|
||
* From: https://github.com/penance316/laravel-iso8601-validator/blob/master/src/IsoDateValidator.php
|
||
*
|
||
* @param $attribute
|
||
* @param $value
|
||
* @param $parameters
|
||
* @param $validator
|
||
*
|
||
* @return bool
|
||
*/
|
||
function validateIsoDate($attribute, $value, $parameters, $validator): bool
|
||
{
|
||
$regex = (is_array($parameters) && in_array('utc', $parameters))
|
||
? '/^(\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)))?$/'
|
||
// 2012-04-23T18:25:43.511Z
|
||
// Regex from https://www.myintervals.com/blog/2009/05/20/iso-8601-date-validation-that-doesnt-suck/
|
||
: '/^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/';
|
||
|
||
return (bool)preg_match($regex, $value);
|
||
}
|
||
|
||
/**
|
||
* This list was got from the Internet
|
||
*
|
||
* @see https://gist.github.com/vxnick/380904
|
||
* @return array
|
||
*/
|
||
function getCountryCodes()
|
||
{
|
||
return [
|
||
'AF' => 'Afghanistan',
|
||
'AX' => 'Åland Islands',
|
||
'AL' => 'Albania',
|
||
'DZ' => 'Algeria',
|
||
'AS' => 'American Samoa',
|
||
'AD' => 'Andorra',
|
||
'AO' => 'Angola',
|
||
'AI' => 'Anguilla',
|
||
'AG' => 'Antigua & Barbuda',
|
||
'AR' => 'Argentina',
|
||
'AU' => 'Australia',
|
||
'AT' => 'Austria',
|
||
'AZ' => 'Azerbaijan',
|
||
'BS' => 'Bahamas',
|
||
'BH' => 'Bahrain',
|
||
'BD' => 'Bangladesh',
|
||
'BB' => 'Barbados',
|
||
'BY' => 'Belarus',
|
||
'BE' => 'Belgium',
|
||
'BZ' => 'Belize',
|
||
'BJ' => 'Benin',
|
||
'BM' => 'Bermuda',
|
||
'BT' => 'Bhutan',
|
||
'BO' => 'Bolivia',
|
||
'BA' => 'Bosnia & Herzegovina',
|
||
'BW' => 'Botswana',
|
||
'BR' => 'Brazil',
|
||
'IO' => 'British Indian Ocean Territory',
|
||
'BN' => 'Brunei',
|
||
'BG' => 'Bulgaria',
|
||
'BF' => 'Burkina Faso',
|
||
'BI' => 'Burundi',
|
||
'KH' => 'Cambodia',
|
||
'CM' => 'Cameroon',
|
||
'CA' => 'Canada',
|
||
'CV' => 'Cape Verde',
|
||
'KY' => 'Cayman Islands',
|
||
'CF' => 'Central African Republic',
|
||
'TD' => 'Chad',
|
||
'CL' => 'Chile',
|
||
'CN' => 'China',
|
||
'CX' => 'Christmas Island',
|
||
'CC' => 'Cocos (Keeling) Islands',
|
||
'CO' => 'Colombia',
|
||
'KM' => 'Comoros',
|
||
'CG' => 'Congo - Brazzaville',
|
||
'CD' => 'Congo - Kinshasa',
|
||
'CK' => 'Cook Islands',
|
||
'CR' => 'Costa Rica',
|
||
'CI' => 'Côte d’Ivoire',
|
||
'HR' => 'Croatia',
|
||
'CU' => 'Cuba',
|
||
'CY' => 'Cyprus',
|
||
'CZ' => 'Czechia',
|
||
'DK' => 'Denmark',
|
||
'DJ' => 'Djibouti',
|
||
'DM' => 'Dominica',
|
||
'DO' => 'Dominican Republic',
|
||
'EC' => 'Ecuador',
|
||
'EG' => 'Egypt',
|
||
'SV' => 'El Salvador',
|
||
'GQ' => 'Equatorial Guinea',
|
||
'ER' => 'Eritrea',
|
||
'EE' => 'Estonia',
|
||
'ET' => 'Ethiopia',
|
||
'FK' => 'Falkland Islands',
|
||
'FO' => 'Faroe Islands',
|
||
'FJ' => 'Fiji',
|
||
'FI' => 'Finland',
|
||
'FR' => 'France',
|
||
'GF' => 'French Guiana',
|
||
'PF' => 'French Polynesia',
|
||
'GA' => 'Gabon',
|
||
'GM' => 'Gambia',
|
||
'GE' => 'Georgia',
|
||
'DE' => 'Germany',
|
||
'GH' => 'Ghana',
|
||
'GI' => 'Gibraltar',
|
||
'GR' => 'Greece',
|
||
'GL' => 'Greenland',
|
||
'GD' => 'Grenada',
|
||
'GP' => 'Guadeloupe',
|
||
'GU' => 'Guam',
|
||
'GT' => 'Guatemala',
|
||
'GG' => 'Guernsey',
|
||
'GN' => 'Guinea',
|
||
'GW' => 'Guinea-Bissau',
|
||
'GY' => 'Guyana',
|
||
'HT' => 'Haiti',
|
||
'HN' => 'Honduras',
|
||
'HK' => 'Hong Kong SAR China',
|
||
'HU' => 'Hungary',
|
||
'IS' => 'Iceland',
|
||
'IN' => 'India',
|
||
'ID' => 'Indonesia',
|
||
'IR' => 'Iran',
|
||
'IQ' => 'Iraq',
|
||
'IE' => 'Ireland',
|
||
'IM' => 'Isle of Man',
|
||
'IL' => 'Israel',
|
||
'IT' => 'Italy',
|
||
'JM' => 'Jamaica',
|
||
'JP' => 'Japan',
|
||
'JE' => 'Jersey',
|
||
'JO' => 'Jordan',
|
||
'KZ' => 'Kazakhstan',
|
||
'KE' => 'Kenya',
|
||
'KI' => 'Kiribati',
|
||
'KP' => 'North Korea',
|
||
'KR' => 'South Korea',
|
||
'KW' => 'Kuwait',
|
||
'KG' => 'Kyrgyzstan',
|
||
'LA' => 'Laos',
|
||
'LV' => 'Latvia',
|
||
'LB' => 'Lebanon',
|
||
'LS' => 'Lesotho',
|
||
'LR' => 'Liberia',
|
||
'LY' => 'Libya',
|
||
'LI' => 'Liechtenstein',
|
||
'LT' => 'Lithuania',
|
||
'LU' => 'Luxembourg',
|
||
'MO' => 'Macao SAR China',
|
||
'MK' => 'North Macedonia',
|
||
'MG' => 'Madagascar',
|
||
'MW' => 'Malawi',
|
||
'MY' => 'Malaysia',
|
||
'MV' => 'Maldives',
|
||
'ML' => 'Mali',
|
||
'MT' => 'Malta',
|
||
'MH' => 'Marshall Islands',
|
||
'MQ' => 'Martinique',
|
||
'MR' => 'Mauritania',
|
||
'MU' => 'Mauritius',
|
||
'YT' => 'Mayotte',
|
||
'MX' => 'Mexico',
|
||
'FM' => 'Micronesia',
|
||
'MD' => 'Moldova',
|
||
'MC' => 'Monaco',
|
||
'MN' => 'Mongolia',
|
||
'ME' => 'Montenegro',
|
||
'MS' => 'Montserrat',
|
||
'MA' => 'Morocco',
|
||
'MZ' => 'Mozambique',
|
||
'MM' => 'Myanmar (Burma)',
|
||
'NA' => 'Namibia',
|
||
'NR' => 'Nauru',
|
||
'NP' => 'Nepal',
|
||
'NL' => 'Netherlands',
|
||
'NC' => 'New Caledonia',
|
||
'NZ' => 'New Zealand',
|
||
'NI' => 'Nicaragua',
|
||
'NE' => 'Niger',
|
||
'NG' => 'Nigeria',
|
||
'NU' => 'Niue',
|
||
'NF' => 'Norfolk Island',
|
||
'MP' => 'Northern Mariana Islands',
|
||
'NO' => 'Norway',
|
||
'OM' => 'Oman',
|
||
'PK' => 'Pakistan',
|
||
'PW' => 'Palau',
|
||
'PS' => 'Palestinian Territories',
|
||
'PA' => 'Panama',
|
||
'PG' => 'Papua New Guinea',
|
||
'PY' => 'Paraguay',
|
||
'PE' => 'Peru',
|
||
'PH' => 'Philippines',
|
||
'PL' => 'Poland',
|
||
'PT' => 'Portugal',
|
||
'PR' => 'Puerto Rico',
|
||
'QA' => 'Qatar',
|
||
'RE' => 'Réunion',
|
||
'RO' => 'Romania',
|
||
'RU' => 'Russia',
|
||
'RW' => 'Rwanda',
|
||
'SH' => 'St. Helena',
|
||
'KN' => 'St. Kitts & Nevis',
|
||
'LC' => 'St. Lucia',
|
||
'PM' => 'St. Pierre & Miquelon',
|
||
'VC' => 'St. Vincent & Grenadines',
|
||
'WS' => 'Samoa',
|
||
'SM' => 'San Marino',
|
||
'ST' => 'São Tomé & Príncipe',
|
||
'SA' => 'Saudi Arabia',
|
||
'SN' => 'Senegal',
|
||
'RS' => 'Serbia',
|
||
'SC' => 'Seychelles',
|
||
'SL' => 'Sierra Leone',
|
||
'SG' => 'Singapore',
|
||
'SK' => 'Slovakia',
|
||
'SI' => 'Slovenia',
|
||
'SB' => 'Solomon Islands',
|
||
'SO' => 'Somalia',
|
||
'ZA' => 'South Africa',
|
||
'ES' => 'Spain',
|
||
'LK' => 'Sri Lanka',
|
||
'SD' => 'Sudan',
|
||
'SR' => 'Suriname',
|
||
'SJ' => 'Svalbard & Jan Mayen',
|
||
'SZ' => 'Eswatini',
|
||
'SE' => 'Sweden',
|
||
'CH' => 'Switzerland',
|
||
'SY' => 'Syria',
|
||
'TW' => 'Taiwan',
|
||
'TJ' => 'Tajikistan',
|
||
'TZ' => 'Tanzania',
|
||
'TH' => 'Thailand',
|
||
'TL' => 'Timor-Leste',
|
||
'TG' => 'Togo',
|
||
'TK' => 'Tokelau',
|
||
'TO' => 'Tonga',
|
||
'TT' => 'Trinidad & Tobago',
|
||
'TN' => 'Tunisia',
|
||
'TM' => 'Turkmenistan',
|
||
'TC' => 'Turks & Caicos Islands',
|
||
'TV' => 'Tuvalu',
|
||
'UG' => 'Uganda',
|
||
'UA' => 'Ukraine',
|
||
'AE' => 'United Arab Emirates',
|
||
'GB' => 'United Kingdom',
|
||
'US' => 'United States',
|
||
'UY' => 'Uruguay',
|
||
'UZ' => 'Uzbekistan',
|
||
'VU' => 'Vanuatu',
|
||
'VE' => 'Venezuela',
|
||
'VN' => 'Vietnam',
|
||
'VG' => 'British Virgin Islands',
|
||
'VI' => 'U.S. Virgin Islands',
|
||
'WF' => 'Wallis & Futuna',
|
||
'EH' => 'Western Sahara',
|
||
'YE' => 'Yemen',
|
||
'ZM' => 'Zambia',
|
||
'ZW' => 'Zimbabwe',
|
||
];
|
||
}
|