8
8
* License: GNU/GPLv2
9
9
* @see LICENSE.txt
10
10
*
11
- * This file: The loader (last modified: 2020.11.27 ).
11
+ * This file: The loader (last modified: 2021.01.10 ).
12
12
*/
13
13
14
14
namespace phpMussel \Core ;
@@ -60,6 +60,11 @@ class Loader
60
60
*/
61
61
public $ Events ;
62
62
63
+ /**
64
+ * @var \Maikuolan\Common\Request An object for sending cURL requests.
65
+ */
66
+ public $ Request ;
67
+
63
68
/**
64
69
* @var \Maikuolan\Common\L10N An object for handling configuration-defined L10N data.
65
70
*/
@@ -83,7 +88,7 @@ class Loader
83
88
/**
84
89
* @var string phpMussel version number (SemVer).
85
90
*/
86
- public $ ScriptVersion = '3.1.0 ' ;
91
+ public $ ScriptVersion = '3.1.1 ' ;
87
92
88
93
/**
89
94
* @var string phpMussel version identifier (complete notation).
@@ -138,11 +143,6 @@ class Loader
138
143
*/
139
144
public $ HashReference = '' ;
140
145
141
- /**
142
- * @var bool Set as true at the implementation to enable debug messages.
143
- */
144
- public $ EnableDebugMessages = false ;
145
-
146
146
/**
147
147
* @var int Populated by the request method.
148
148
*/
@@ -158,11 +158,6 @@ class Loader
158
158
*/
159
159
private $ L10NPath = __DIR__ . DIRECTORY_SEPARATOR . '.. ' . DIRECTORY_SEPARATOR . 'l10n ' . DIRECTORY_SEPARATOR ;
160
160
161
- /**
162
- * @var int The default timeout for request.
163
- */
164
- private $ Timeout = 12 ;
165
-
166
161
/**
167
162
* @var array Channels information for request.
168
163
*/
@@ -331,8 +326,16 @@ public function __construct(
331
326
date_default_timezone_set ($ this ->Configuration ['core ' ]['timezone ' ]);
332
327
}
333
328
334
- /** Calculate instantiation time. */
335
- $ this ->Time = time () + ($ this ->Configuration ['core ' ]['time_offset ' ] * 60 );
329
+ /** Instantiate request class. */
330
+ $ this ->Request = new \Maikuolan \Common \Request ();
331
+ $ this ->Request ->Channels = (
332
+ $ Channels = $ this ->readFileBlocks ($ this ->AssetsPath . 'channels.yaml ' )
333
+ ) ? (new \Maikuolan \Common \YAML ($ Channels ))->Data : [];
334
+ if (!isset ($ this ->Request ->Channels ['Triggers ' ])) {
335
+ $ this ->Request ->Channels ['Triggers ' ] = [];
336
+ }
337
+ $ this ->Request ->Disabled = $ this ->Configuration ['core ' ]['disabled_channels ' ];
338
+ $ this ->Request ->UserAgent = $ this ->ScriptUA ;
336
339
337
340
/** If the language directive is empty, default to English. */
338
341
if (empty ($ this ->Configuration ['core ' ]['lang ' ])) {
@@ -342,6 +345,9 @@ public function __construct(
342
345
/** Load phpMussel core L10N data. */
343
346
$ this ->loadL10N ($ this ->L10NPath );
344
347
348
+ /** Calculate instantiation time. */
349
+ $ this ->Time = time () + ($ this ->Configuration ['core ' ]['time_offset ' ] * 60 );
350
+
345
351
/** Initialise the cache. */
346
352
$ this ->initialiseCache ();
347
353
@@ -908,148 +914,6 @@ public function readFileAsArray(string $Filename, int $Flags = 0, $Context = nul
908
914
return is_array ($ Output ) ? $ Output : [];
909
915
}
910
916
911
- /**
912
- * Used to send cURL requests.
913
- *
914
- * @param string $URI The resource to request.
915
- * @param mixed $Params If empty or omitted, CURLOPT_POST is false. Otherwise,
916
- * CURLOPT_POST is true, and the parameter is used to supply
917
- * CURLOPT_POSTFIELDS. Normally an associative array of key-value pairs,
918
- * but can be any kind of value supported by CURLOPT_POSTFIELDS. Optional.
919
- * @param int $Timeout An optional timeout limit.
920
- * @param array $Headers An optional array of headers to send with the request.
921
- * @param int $Depth Recursion depth of the current closure instance.
922
- * @return string The results of the request, or an empty string upon failure.
923
- */
924
- public function request (string $ URI , $ Params = [], int $ Timeout = -1 , array $ Headers = [], int $ Depth = 0 ): string
925
- {
926
- /** Fetch channel information. */
927
- if (empty ($ this ->Channels )) {
928
- $ this ->Channels = (
929
- $ Channels = $ this ->readFileBlocks ($ this ->AssetsPath . 'channels.yaml ' )
930
- ) ? (new \Maikuolan \Common \YAML ($ Channels ))->Data : [];
931
- if (!isset ($ this ->Channels ['Triggers ' ])) {
932
- $ this ->Channels ['Triggers ' ] = [];
933
- }
934
- }
935
-
936
- /** Test channel triggers. */
937
- foreach ($ this ->Channels ['Triggers ' ] as $ TriggerName => $ TriggerURI ) {
938
- if (
939
- !isset ($ this ->Channels [$ TriggerName ]) ||
940
- !is_array ($ this ->Channels [$ TriggerName ]) ||
941
- substr ($ URI , 0 , strlen ($ TriggerURI )) !== $ TriggerURI
942
- ) {
943
- continue ;
944
- }
945
- foreach ($ this ->Channels [$ TriggerName ] as $ Channel => $ Options ) {
946
- if (!is_array ($ Options ) || !isset ($ Options [$ TriggerName ])) {
947
- continue ;
948
- }
949
- $ Len = strlen ($ Options [$ TriggerName ]);
950
- if (substr ($ URI , 0 , $ Len ) !== $ Options [$ TriggerName ]) {
951
- continue ;
952
- }
953
- unset($ Options [$ TriggerName ]);
954
- if (empty ($ Options ) || $ this ->inCsv (key ($ Options ), $ this ->Configuration ['core ' ]['disabled_channels ' ])) {
955
- continue ;
956
- }
957
- $ AlternateURI = current ($ Options ) . substr ($ URI , $ Len );
958
- break ;
959
- }
960
- if ($ this ->inCsv ($ TriggerName , $ this ->Configuration ['core ' ]['disabled_channels ' ])) {
961
- if (isset ($ AlternateURI )) {
962
- return $ this ->request ($ AlternateURI , $ Params , $ Timeout , $ Headers , $ Depth );
963
- }
964
- return '' ;
965
- }
966
- if (isset ($ this ->Channels ['Overrides ' ], $ this ->Channels ['Overrides ' ][$ TriggerName ])) {
967
- $ Overrides = $ this ->Channels ['cURL Overrides ' ][$ TriggerName ];
968
- }
969
- break ;
970
- }
971
-
972
- /** Empty overrides in case none declared. */
973
- $ Overrides = [];
974
-
975
- /** Initialise the cURL session. */
976
- $ Request = curl_init ($ URI );
977
-
978
- $ LCURI = strtolower ($ URI );
979
- $ SSL = (substr ($ LCURI , 0 , 6 ) === 'https: ' );
980
-
981
- curl_setopt ($ Request , CURLOPT_FRESH_CONNECT , true );
982
- curl_setopt ($ Request , CURLOPT_HEADER , false );
983
- if (empty ($ Params )) {
984
- curl_setopt ($ Request , CURLOPT_POST , false );
985
- $ Post = false ;
986
- } else {
987
- curl_setopt ($ Request , CURLOPT_POST , true );
988
- curl_setopt ($ Request , CURLOPT_POSTFIELDS , $ Params );
989
- $ Post = true ;
990
- }
991
- if ($ SSL ) {
992
- curl_setopt ($ Request , CURLOPT_PROTOCOLS , CURLPROTO_HTTPS );
993
- curl_setopt ($ Request , CURLOPT_SSL_VERIFYPEER , (
994
- isset ($ Overrides ['CURLOPT_SSL_VERIFYPEER ' ]) ? !empty ($ Overrides ['CURLOPT_SSL_VERIFYPEER ' ]) : false
995
- ));
996
- }
997
- curl_setopt ($ Request , CURLOPT_FOLLOWLOCATION , true );
998
- curl_setopt ($ Request , CURLOPT_MAXREDIRS , 1 );
999
- curl_setopt ($ Request , CURLOPT_RETURNTRANSFER , true );
1000
- curl_setopt ($ Request , CURLOPT_TIMEOUT , ($ Timeout > 0 ? $ Timeout : $ this ->Timeout ));
1001
- curl_setopt ($ Request , CURLOPT_USERAGENT , $ this ->ScriptUA );
1002
- curl_setopt ($ Request , CURLOPT_HTTPHEADER , $ Headers ?: []);
1003
- $ Time = microtime (true );
1004
-
1005
- /** Execute and get the response. */
1006
- $ Response = curl_exec ($ Request );
1007
-
1008
- /** Used for debugging. */
1009
- $ Time = microtime (true ) - $ Time ;
1010
-
1011
- /** Check for problems (e.g., resource not found, server errors, etc). */
1012
- if (($ Info = curl_getinfo ($ Request )) && is_array ($ Info ) && isset ($ Info ['http_code ' ])) {
1013
-
1014
- /** Used for debugging. */
1015
- $ this ->debugMessage (sprintf (
1016
- "\r%s - %s - %s - %s \n" ,
1017
- $ Post ? 'POST ' : 'GET ' ,
1018
- $ URI ,
1019
- $ Info ['http_code ' ],
1020
- (floor ($ Time * 100 ) / 100 ) . 's '
1021
- ));
1022
-
1023
- /** Most recent HTTP code flag. */
1024
- $ this ->MostRecentHttpCode = $ Info ['http_code ' ];
1025
-
1026
- /** Request failed. Try again using an alternative address. */
1027
- if ($ Info ['http_code ' ] >= 400 && isset ($ AlternateURI ) && $ Depth < 3 ) {
1028
- curl_close ($ Request );
1029
- return $ this ->request ($ AlternateURI , $ Params , $ Timeout , $ Headers , $ Depth + 1 );
1030
- }
1031
- } else {
1032
-
1033
- /** Used for debugging. */
1034
- $ this ->debugMessage (sprintf (
1035
- "\r%s - %s - %s - %s \n" ,
1036
- $ Post ? 'POST ' : 'GET ' ,
1037
- $ URI ,
1038
- 200 ,
1039
- (floor ($ Time * 100 ) / 100 ) . 's '
1040
- ));
1041
-
1042
- /** Most recent HTTP code flag. */
1043
- $ this ->MostRecentHttpCode = 200 ;
1044
- }
1045
-
1046
- /** Close the cURL session. */
1047
- curl_close ($ Request );
1048
-
1049
- /** Return the results of the request. */
1050
- return $ Response ;
1051
- }
1052
-
1053
917
/**
1054
918
* A simple safety wrapper for unpack.
1055
919
*
@@ -1274,47 +1138,6 @@ private function initialiseCache()
1274
1138
}
1275
1139
}
1276
1140
1277
- /**
1278
- * Checks for a value within CSV.
1279
- *
1280
- * @param string $Value The value to look for.
1281
- * @param string $CSV The CSV to look in.
1282
- * @return bool True when found; False when not found.
1283
- */
1284
- public function inCsv (string $ Value , string $ CSV ): bool
1285
- {
1286
- if (!$ Value || !$ CSV ) {
1287
- return false ;
1288
- }
1289
- $ Arr = explode (', ' , $ CSV );
1290
- if (strpos ($ CSV , '" ' ) !== false ) {
1291
- foreach ($ Arr as &$ Item ) {
1292
- if (substr ($ Item , 0 , 1 ) === '" ' && substr ($ Item , -1 ) === '" ' ) {
1293
- $ Item = substr ($ Item , 1 , -1 );
1294
- }
1295
- }
1296
- }
1297
- return in_array ($ Value , $ Arr , true );
1298
- }
1299
-
1300
- /**
1301
- * Prints debug messages (used in dev; not needed for production).
1302
- *
1303
- * @param string $Message The debug message to send.
1304
- */
1305
- public function debugMessage (string $ Message )
1306
- {
1307
- if ($ this ->EnableDebugMessages !== true ) {
1308
- return ;
1309
- }
1310
- $ Handle = fopen ('php://stdout ' , 'wb ' );
1311
- if (!is_resource ($ Handle )) {
1312
- return ;
1313
- }
1314
- fwrite ($ Handle , $ Message );
1315
- fclose ($ Handle );
1316
- }
1317
-
1318
1141
/**
1319
1142
* Fetch favicon.
1320
1143
*
0 commit comments