-
Notifications
You must be signed in to change notification settings - Fork 44
/
index.php
executable file
·103 lines (85 loc) · 3.87 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
require __DIR__ . '/vendor/autoload.php';
/* Load Twig */
$twig = new \Twig\Environment(new \Twig\Loader\FilesystemLoader(__DIR__ . '/templates/'));
$twig->addGlobal('baseurl', $_SERVER['SCRIPT_NAME']);
$twig->addGlobal('phpversion', phpversion());
$twig->addGlobal('version', \Composer\InstalledVersions::getVersion('giggsey/libphonenumber-for-php'));
$twig->addFunction(new \Twig\TwigFunction('get_class', 'get_class'));
/* Check if we have loaded variables */
if (isset($_GET['phonenumber']) && $_GET['phonenumber'] != '') {
$input = array();
$input['phonenumber'] = $_GET['phonenumber'];
$input['country'] = (isset($_GET['country'])) ? $_GET['country'] : null;
$input['language'] = (isset($_GET['language']) && $_GET['language'] != '') ? $_GET['language'] : 'en';
$input['region'] = (isset($_GET['region']) && $_GET['region'] != '') ? $_GET['region'] : null;
$phoneNumberUtil = \libphonenumber\PhoneNumberUtil::getInstance();
$shortNumberInfo = \libphonenumber\ShortNumberInfo::getInstance();
$phoneNumberGeocoder = \libphonenumber\geocoding\PhoneNumberOfflineGeocoder::getInstance();
$validNumber = false;
$validNumberForRegion = false;
$possibleNumber = false;
$isPossibleNumberWithReason = null;
$geolocation = null;
$phoneNumberToCarrierInfo = null;
$timezone = null;
try {
$phoneNumber = $phoneNumberUtil->parse($input['phonenumber'], $input['country'], null, true);
$possibleNumber = $phoneNumberUtil->isPossibleNumber($phoneNumber);
$isPossibleNumberWithReason = $phoneNumberUtil->isPossibleNumberWithReason($phoneNumber);
$validNumber = $phoneNumberUtil->isValidNumber($phoneNumber);
$validNumberForRegion = $phoneNumberUtil->isValidNumberForRegion($phoneNumber, $input['country']);
$phoneNumberRegion = $phoneNumberUtil->getRegionCodeForNumber($phoneNumber);
$phoneNumberType = $phoneNumberUtil->getNumberType($phoneNumber);
$geolocation = $phoneNumberGeocoder->getDescriptionForNumber(
$phoneNumber,
$input['language'],
$input['region']
);
$phoneNumberToCarrierInfo = \libphonenumber\PhoneNumberToCarrierMapper::getInstance()->getNameForNumber(
$phoneNumber,
$input['language']
);
$timezone = \libphonenumber\PhoneNumberToTimeZonesMapper::getInstance()->getTimeZonesForNumber($phoneNumber);
} catch (\Exception $e) {
echo $twig->render(
'error.twig',
array(
'input' => $input,
'exception' => $e,
)
);
exit;
}
$regions = $phoneNumberUtil->getSupportedRegions();
asort($regions);
$baseLanguagePath = __DIR__ . '/vendor/umpirsky/country-list/data/';
$resolvedPath = realpath($baseLanguagePath . $input['language'] . '/country.php');
if (strpos($resolvedPath, $baseLanguagePath) === 0 && file_exists($resolvedPath)) {
$countries = require $resolvedPath;
} else {
$countries = require $baseLanguagePath . 'en/country.php';
}
echo $twig->render(
'data.twig',
array(
'phoneNumberObj' => $phoneNumber,
'phoneUtil' => $phoneNumberUtil,
'possibleNumber' => $possibleNumber,
'isPossibleNumberWithReason' => $isPossibleNumberWithReason,
'validNumber' => $validNumber,
'validNumberForRegion' => $validNumberForRegion,
'phoneNumberRegion' => $phoneNumberRegion,
'phoneNumberType' => $phoneNumberType,
'geolocation' => $geolocation,
'timezone' => $timezone,
'carrierinfo' => $phoneNumberToCarrierInfo,
'shortNumber' => $shortNumberInfo,
'input' => $input,
'regions' => $regions,
'countries' => $countries,
)
);
} else {
echo $twig->render('home.twig');
}