Passed
Push — main ( 4a5f0c...02f3c9 )
by MyFatoorah
06:23 queued 02:44
created

MyFatoorahHelper::getMFConfigFileContent()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 17
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 26
rs 9.3888
1
<?php
2
3
namespace MyFatoorah\Library;
4
5
use Exception;
6
7
/**
8
 * Trait MyFatoorah is responsible for helping calling MyFatoorah API endpoints.
9
 */
10
class MyFatoorahHelper
11
{
12
    /**
13
     * The file name or the logger object
14
     * It is used in logging the payment/shipping events to help in debugging and monitor the process and connections.
15
     *
16
     * @var string|object
17
     */
18
    public static $loggerObj;
19
20
    /**
21
     * The function name that will be used in the debugging if $loggerObj is set as a logger object.
22
     *
23
     * @var string
24
     */
25
    public static $loggerFunc;
26
27
    //-----------------------------------------------------------------------------------------------------------------------------------------
28
29
    /**
30
     * Returns the country code and the phone after applying MyFatoorah restriction
31
     *
32
     * Matching regular expression pattern: ^(?:(\+)|(00)|(\\*)|())[0-9]{3,14}((\\#)|())$
33
     * if (!preg_match('/^(?:(\+)|(00)|(\\*)|())[0-9]{3,14}((\\#)|())$/iD', $inputString))
34
     * String length: inclusive between 0 and 11
35
     *
36
     * @param string $inputString It is the input phone number provide by the end user.
37
     *
38
     * @return array        That contains the phone code in the 1st element the the phone number the the 2nd element.
39
     *
40
     * @throws Exception    Throw exception if the input length is less than 3 chars or long than 14 chars.
41
     */
42
    public static function getPhone($inputString)
43
    {
44
45
        //remove any arabic digit
46
        $string3 = self::convertArabicDigitstoEnglish($inputString);
47
48
        //Keep Only digits
49
        $string4 = preg_replace('/[^0-9]/', '', $string3);
50
51
        //remove 00 at start
52
        if (strpos($string4, '00') === 0) {
53
            $string4 = substr($string4, 2);
54
        }
55
56
        if (!$string4) {
57
            return ['', ''];
58
        }
59
60
        //check for the allowed length
61
        $len = strlen($string4);
62
        if ($len < 3 || $len > 14) {
63
            throw new Exception('Phone Number lenght must be between 3 to 14 digits');
64
        }
65
66
        //get the phone arr
67
        if (strlen(substr($string4, 3)) > 3) {
68
            return [
69
                substr($string4, 0, 3),
70
                substr($string4, 3)
71
            ];
72
        }
73
74
        return [
75
            '',
76
            $string4
77
        ];
78
    }
79
80
    //-----------------------------------------------------------------------------------------------------------------------------------------
81
82
    /**
83
     * Converts any Arabic or Persian numbers to English digits
84
     *
85
     * @param string $inputString It is the input phone number provide by the end user.
86
     *
87
     * @return string
88
     */
89
    protected static function convertArabicDigitstoEnglish($inputString)
90
    {
91
92
        $newNumbers = range(0, 9);
93
94
        $persianDecimal = ['&#1776;', '&#1777;', '&#1778;', '&#1779;', '&#1780;', '&#1781;', '&#1782;', '&#1783;', '&#1784;', '&#1785;']; // 1. Persian HTML decimal
95
        $arabicDecimal  = ['&#1632;', '&#1633;', '&#1634;', '&#1635;', '&#1636;', '&#1637;', '&#1638;', '&#1639;', '&#1640;', '&#1641;']; // 2. Arabic HTML decimal
96
        $arabic         = ['٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩']; // 3. Arabic Numeric
97
        $persian        = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']; // 4. Persian Numeric
98
99
        $string0 = str_replace($persianDecimal, $newNumbers, $inputString);
100
        $string1 = str_replace($arabicDecimal, $newNumbers, $string0);
101
        $string2 = str_replace($arabic, $newNumbers, $string1);
102
103
        return str_replace($persian, $newNumbers, $string2);
104
    }
105
106
    //-----------------------------------------------------------------------------------------------------------------------------------------
107
108
    /**
109
     * Get the rate that will convert the given weight unit to MyFatoorah default weight unit.
110
     * Weight must be in kg, g, lbs, or oz. Default is kg.
111
     *
112
     * @param string $unit It is the weight unit used.
113
     *
114
     * @return double|int The conversion rate that will convert the given unit into the kg.
115
     *
116
     * @throws Exception Throw exception if the input unit is not support.
117
     */
118
    public static function getWeightRate($unit)
119
    {
120
121
        $lUnit = strtolower($unit);
122
123
        //kg is the default
124
        $rateUnits = [
125
            '1'         => ['kg', 'kgs', 'كج', 'كلغ', 'كيلو جرام', 'كيلو غرام'],
126
            '0.001'     => ['g', 'جرام', 'غرام', 'جم'],
127
            '0.453592'  => ['lbs', 'lb', 'رطل', 'باوند'],
128
            '0.0283495' => ['oz', 'اوقية', 'أوقية'],
129
        ];
130
131
        foreach ($rateUnits as $rate => $unitArr) {
132
            if (array_search($lUnit, $unitArr) !== false) {
133
                return (float) $rate;
134
            }
135
        }
136
        throw new Exception('Weight units must be in kg, g, lbs, or oz. Default is kg');
137
    }
138
139
    //-----------------------------------------------------------------------------------------------------------------------------------------
140
141
    /**
142
     * Get the rate that will convert the given dimension unit to MyFatoorah default dimension unit.
143
     * Dimension must be in cm, m, mm, in, or yd. Default is cm.
144
     *
145
     * @param string $unit It is the dimension unit used in width, hight, or depth.
146
     *
147
     * @return double|int   The conversion rate that will convert the given unit into the cm.
148
     *
149
     * @throws Exception        Throw exception if the input unit is not support.
150
     */
151
    public static function getDimensionRate($unit)
152
    {
153
154
        $lUnit = strtolower($unit);
155
156
        //cm is the default
157
        $rateUnits = [
158
            '1'     => ['cm', 'سم'],
159
            '100'   => ['m', 'متر', 'م'],
160
            '0.1'   => ['mm', 'مم'],
161
            '2.54'  => ['in', 'انش', 'إنش', 'بوصه', 'بوصة'],
162
            '91.44' => ['yd', 'يارده', 'ياردة'],
163
        ];
164
165
        foreach ($rateUnits as $rate => $unitArr) {
166
            if (array_search($lUnit, $unitArr) !== false) {
167
                return (float) $rate;
168
            }
169
        }
170
        throw new Exception('Dimension units must be in cm, m, mm, in, or yd. Default is cm');
171
    }
172
173
    //-----------------------------------------------------------------------------------------------------------------------------------------
174
175
    /**
176
     * Validate webhook signature function
177
     *
178
     * @param array  $dataArray webhook request array
179
     * @param string $secret    webhook secret key
180
     * @param string $signature MyFatoorah signature
181
     * @param int    $eventType MyFatoorah Event type Number (1, 2, 3 , 4)
182
     *
183
     * @return boolean
184
     */
185
    public static function isSignatureValid($dataArray, $secret, $signature, $eventType = 0)
186
    {
187
188
        if ($eventType == 2) {
189
            unset($dataArray['GatewayReference']);
190
        }
191
192
        uksort($dataArray, 'strcasecmp');
193
194
        $mapFun = function ($v, $k) {
195
            return sprintf("%s=%s", $k, $v);
196
        };
197
        $outputArr = array_map($mapFun, $dataArray, array_keys($dataArray));
198
        $output    = implode(',', $outputArr);
199
200
        // generate hash of $field string
201
        $hash = base64_encode(hash_hmac('sha256', $output, $secret, true));
202
203
        return $signature === $hash;
204
    }
205
206
    //-----------------------------------------------------------------------------------------------------------------------------------------
207
208
    /**
209
     * Get a list of MyFatoorah countries, their API URLs, and names.
210
     *
211
     * @return array of MyFatoorah data
212
     */
213
    public static function getMFCountries()
214
    {
215
216
        $cachedFile = dirname(__FILE__) . '/mf-config.json';
217
218
        if (file_exists($cachedFile)) {
219
            if ((time() - filemtime($cachedFile) > 3600)) {
220
                $countries = self::getMFConfigFileContent($cachedFile);
221
            }
222
223
            if (!empty($countries)) {
224
                return $countries;
225
            }
226
227
            $cache = file_get_contents($cachedFile);
228
            return ($cache) ? json_decode($cache, true) : [];
229
        } else {
230
            return self::getMFConfigFileContent($cachedFile);
231
        }
232
    }
233
234
    //-----------------------------------------------------------------------------------------------------------------------------------------
235
236
    /**
237
     * Cache a list of MyFatoorah countries, their API URLs, and names.
238
     *
239
     * @param string $cachedFile The file name used in caching data.
240
     *
241
     * @return array of MyFatoorah data
242
     */
243
    protected static function getMFConfigFileContent($cachedFile)
244
    {
245
246
        $curl = curl_init('https://portal.myfatoorah.com/Files/API/mf-config.json');
247
248
        $option = [
249
            CURLOPT_HTTPHEADER     => ['Content-Type: application/json'],
250
            CURLOPT_RETURNTRANSFER => true
251
        ];
252
        curl_setopt_array($curl, $option);
253
254
        $response  = curl_exec($curl);
255
        $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
256
        curl_close($curl);
257
258
        if ($http_code == 200 && is_string($response)) {
259
            file_put_contents($cachedFile, $response);
260
            return json_decode($response, true);
261
        } elseif ($http_code == 403) {
262
            touch($cachedFile);
263
            $fileContent = file_get_contents($cachedFile);
264
            if (!empty($fileContent)) {
265
                return json_decode($fileContent, true);
266
            }
267
        }
268
        return [];
269
    }
270
271
    //-----------------------------------------------------------------------------------------------------------------------------------------
272
273
    /**
274
     * Filter an input from global variables like $_GET, $_POST, $_REQUEST, $_COOKIE, $_SERVER
275
     *
276
     * @param string $name The field name the need to be filter.
277
     * @param string $type The input type to be filter (GET, POST, REQUEST, COOKIE, SERVER).
278
     *
279
     * @return string|null
280
     */
281
    public static function filterInputField($name, $type = 'GET')
282
    {
283
        if (isset($GLOBALS["_$type"][$name])) {
284
            return htmlspecialchars($GLOBALS["_$type"][$name]);
285
        }
286
        return null;
287
    }
288
289
    //-----------------------------------------------------------------------------------------------------------------------------------------
290
}
291