diff --git a/C.php b/C.php index ea061cc303abb1ddedcafd0c3aae70297de05e39..ff2a1a79d1faa5d62dd03eb4cb37f703a4a215b9 100644 --- a/C.php +++ b/C.php @@ -15,6 +15,9 @@ * * 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 Lesser General Public License for more details. + * + * @created Thu, Dec 1, 2016 11:53:09 by Radek Suski + * @modified 30 April 2020 by Sigrid Suski */ namespace Sobi; @@ -22,9 +25,8 @@ namespace Sobi; defined( 'SOBI' ) || exit( 'Restricted access' ); /** - * @author Radek Suski - * @created Thu, Dec 1, 2016 11:53:09 - * @modified 21 January 2020 by Sigrid Suski + * Class C + * @package Sobi */ abstract class C { @@ -42,5 +44,5 @@ abstract class C const NO = 0; const ROOT = JPATH_ROOT; const DS = DIRECTORY_SEPARATOR; - const VERSION = '1.0.11'; + const VERSION = '1.0.12'; } diff --git a/Communication/CURL.php b/Communication/CURL.php index b526dd52ea3983490acfafc0d573b48610604466..bf7f09471e29f72f53278cf05ecc4654ec842dd1 100644 --- a/Communication/CURL.php +++ b/Communication/CURL.php @@ -1,19 +1,23 @@ CURLINFO_EFFECTIVE_URL, - 'http_code' => CURLINFO_HTTP_CODE, - 'response_code' => CURLINFO_HTTP_CODE, - 'filetime' => CURLINFO_FILETIME, - 'total_time' => CURLINFO_TOTAL_TIME, - 'namelookup_time' => CURLINFO_NAMELOOKUP_TIME, - 'connect_time' => CURLINFO_CONNECT_TIME, - 'pretransfer_time' => CURLINFO_PRETRANSFER_TIME, - 'starttransfer_time' => CURLINFO_STARTTRANSFER_TIME, - 'redirect_time' => CURLINFO_REDIRECT_TIME, - 'size_upload' => CURLINFO_SIZE_UPLOAD, - 'size_download' => CURLINFO_SIZE_DOWNLOAD, - 'speed_download' => CURLINFO_SPEED_DOWNLOAD, - 'speed_upload' => CURLINFO_SPEED_UPLOAD, - 'header_size' => CURLINFO_HEADER_SIZE, - 'header_out' => CURLINFO_HEADER_OUT, - 'request_size' => CURLINFO_REQUEST_SIZE, - 'ssl_verifyresult' => CURLINFO_SSL_VERIFYRESULT, - 'content_length_download' => CURLINFO_CONTENT_LENGTH_DOWNLOAD, - 'content_length_upload' => CURLINFO_CONTENT_LENGTH_UPLOAD, - 'content_type' => CURLINFO_CONTENT_TYPE + 'effective_url' => CURLINFO_EFFECTIVE_URL, + 'http_code' => CURLINFO_HTTP_CODE, + 'response_code' => CURLINFO_HTTP_CODE, + 'filetime' => CURLINFO_FILETIME, + 'total_time' => CURLINFO_TOTAL_TIME, + 'namelookup_time' => CURLINFO_NAMELOOKUP_TIME, + 'connect_time' => CURLINFO_CONNECT_TIME, + 'pretransfer_time' => CURLINFO_PRETRANSFER_TIME, + 'starttransfer_time' => CURLINFO_STARTTRANSFER_TIME, + 'redirect_time' => CURLINFO_REDIRECT_TIME, + 'size_upload' => CURLINFO_SIZE_UPLOAD, + 'size_download' => CURLINFO_SIZE_DOWNLOAD, + 'speed_download' => CURLINFO_SPEED_DOWNLOAD, + 'speed_upload' => CURLINFO_SPEED_UPLOAD, + 'header_size' => CURLINFO_HEADER_SIZE, + 'header_out' => CURLINFO_HEADER_OUT, + 'request_size' => CURLINFO_REQUEST_SIZE, + 'ssl_verifyresult' => CURLINFO_SSL_VERIFYRESULT, + 'content_length_download' => CURLINFO_CONTENT_LENGTH_DOWNLOAD, + 'content_length_upload' => CURLINFO_CONTENT_LENGTH_UPLOAD, + 'content_type' => CURLINFO_CONTENT_TYPE, ]; + /** + * @var array + */ static $optionsCodes = [ - 'autoreferer' => CURLOPT_AUTOREFERER, - 'binarytransfer' => CURLOPT_BINARYTRANSFER, - 'cookiesession' => CURLOPT_COOKIESESSION, - 'crlf' => CURLOPT_CRLF, - 'dns_use_global_cache' => CURLOPT_DNS_USE_GLOBAL_CACHE, - 'failonerror' => CURLOPT_FAILONERROR, - 'filetime' => CURLOPT_FILETIME, - 'followlocation' => CURLOPT_FOLLOWLOCATION, - 'forbid_reuse' => CURLOPT_FORBID_REUSE, - 'fresh_connect' => CURLOPT_FRESH_CONNECT, - 'ftp_use_eprt' => CURLOPT_FTP_USE_EPRT, - 'ftp_use_epsv' => CURLOPT_FTP_USE_EPSV, - 'ftpappend' => CURLOPT_FTPAPPEND, - 'ftpascii' => CURLOPT_TRANSFERTEXT, - 'ftplistonly' => CURLOPT_FTPLISTONLY, - 'header' => CURLOPT_HEADER, - 'httpget' => CURLOPT_HTTPGET, - 'httpproxytunnel' => CURLOPT_HTTPPROXYTUNNEL, -// 'mute' => CURLOPT_MUTE, - 'netrc' => CURLOPT_NETRC, - 'nobody' => CURLOPT_NOBODY, - 'noprogress' => CURLOPT_NOPROGRESS, - 'nosignal' => CURLOPT_NOSIGNAL, - 'post' => CURLOPT_POST, - 'put' => CURLOPT_PUT, - 'returntransfer' => CURLOPT_RETURNTRANSFER, - 'ssl_verifypeer' => CURLOPT_SSL_VERIFYPEER, - 'transfertext' => CURLOPT_TRANSFERTEXT, - 'unrestricted_auth' => CURLOPT_UNRESTRICTED_AUTH, - 'upload' => CURLOPT_UPLOAD, - 'verbose' => CURLOPT_VERBOSE, - 'buffersize' => CURLOPT_BUFFERSIZE, -// 'closepolicy' => CURLOPT_CLOSEPOLICY, - 'connecttimeout' => CURLOPT_CONNECTTIMEOUT, - 'dns_cache_timeout' => CURLOPT_DNS_CACHE_TIMEOUT, - 'ftpsslauth' => CURLOPT_FTPSSLAUTH, - 'http_version' => CURLOPT_HTTP_VERSION, - 'httpauth' => CURLOPT_HTTPAUTH, - 'curlauth_any' => CURLAUTH_ANY, - 'curlauth_anysafe' => CURLAUTH_ANYSAFE, - 'infilesize' => CURLOPT_INFILESIZE, - 'low_speed_limit' => CURLOPT_LOW_SPEED_LIMIT, - 'low_speed_time' => CURLOPT_LOW_SPEED_TIME, - 'maxconnects' => CURLOPT_MAXCONNECTS, - 'maxredirs' => CURLOPT_MAXREDIRS, - 'port' => CURLOPT_PORT, - 'proxyauth' => CURLOPT_PROXYAUTH, - 'proxyport' => CURLOPT_PROXYPORT, - 'proxytype' => CURLOPT_PROXYTYPE, - 'resume_from' => CURLOPT_RESUME_FROM, - 'ssl_verifyhost' => CURLOPT_SSL_VERIFYHOST, - 'sslversion' => CURLOPT_SSLVERSION, - 'timecondition' => CURLOPT_TIMECONDITION, - 'timeout' => CURLOPT_TIMEOUT, - 'timevalue' => CURLOPT_TIMEVALUE, - 'cainfo' => CURLOPT_CAINFO, - 'capath' => CURLOPT_CAPATH, - 'cookie' => CURLOPT_COOKIE, - 'cookiefile' => CURLOPT_COOKIEFILE, - 'cookiejar' => CURLOPT_COOKIEJAR, - 'customrequest' => CURLOPT_CUSTOMREQUEST, - 'egdsocket' => CURLOPT_EGDSOCKET, - 'encoding' => CURLOPT_ENCODING, - 'ftpport' => CURLOPT_FTPPORT, - 'interface' => CURLOPT_INTERFACE, - 'krb4level' => CURLOPT_KRB4LEVEL, - 'postfields' => CURLOPT_POSTFIELDS, - 'proxy' => CURLOPT_PROXY, - 'proxyuserpwd' => CURLOPT_PROXYUSERPWD, - 'random_file' => CURLOPT_RANDOM_FILE, - 'range' => CURLOPT_RANGE, - 'referer' => CURLOPT_REFERER, - 'ssl_cipher_list' => CURLOPT_SSL_CIPHER_LIST, - 'sslcert' => CURLOPT_SSLCERT, - 'sslcertpasswd' => CURLOPT_SSLCERTPASSWD, - 'sslcerttype' => CURLOPT_SSLCERTTYPE, - 'sslengine' => CURLOPT_SSLENGINE, - 'sslengine_default' => CURLOPT_SSLENGINE_DEFAULT, - 'sslkey' => CURLOPT_SSLKEY, - 'sslkeypasswd' => CURLOPT_SSLKEYPASSWD, - 'sslkeytype' => CURLOPT_SSLKEYTYPE, - 'url' => CURLOPT_URL, - 'useragent' => CURLOPT_USERAGENT, - 'userpwd' => CURLOPT_USERPWD, - 'http200aliases' => CURLOPT_HTTP200ALIASES, - 'httpheader' => CURLOPT_HTTPHEADER, - 'postquote' => CURLOPT_POSTQUOTE, - 'quote' => CURLOPT_QUOTE, - 'file' => CURLOPT_FILE, - 'infile' => CURLOPT_INFILE, - 'stderr' => CURLOPT_STDERR, - 'writeheader' => CURLOPT_WRITEHEADER, - 'headerfunction' => CURLOPT_HEADERFUNCTION, - 'readfunction' => CURLOPT_READFUNCTION, - 'writefunction' => CURLOPT_WRITEFUNCTION, + 'autoreferer' => CURLOPT_AUTOREFERER, + 'binarytransfer' => CURLOPT_BINARYTRANSFER, + 'cookiesession' => CURLOPT_COOKIESESSION, + 'crlf' => CURLOPT_CRLF, + 'dns_use_global_cache' => CURLOPT_DNS_USE_GLOBAL_CACHE, + 'failonerror' => CURLOPT_FAILONERROR, + 'filetime' => CURLOPT_FILETIME, + 'followlocation' => CURLOPT_FOLLOWLOCATION, + 'forbid_reuse' => CURLOPT_FORBID_REUSE, + 'fresh_connect' => CURLOPT_FRESH_CONNECT, + 'ftp_use_eprt' => CURLOPT_FTP_USE_EPRT, + 'ftp_use_epsv' => CURLOPT_FTP_USE_EPSV, + 'ftpappend' => CURLOPT_FTPAPPEND, + 'ftpascii' => CURLOPT_TRANSFERTEXT, + 'ftplistonly' => CURLOPT_FTPLISTONLY, + 'header' => CURLOPT_HEADER, + 'httpget' => CURLOPT_HTTPGET, + 'httpproxytunnel' => CURLOPT_HTTPPROXYTUNNEL, + // 'mute' => CURLOPT_MUTE, + 'netrc' => CURLOPT_NETRC, + 'nobody' => CURLOPT_NOBODY, + 'noprogress' => CURLOPT_NOPROGRESS, + 'nosignal' => CURLOPT_NOSIGNAL, + 'post' => CURLOPT_POST, + 'put' => CURLOPT_PUT, + 'returntransfer' => CURLOPT_RETURNTRANSFER, + 'ssl_verifypeer' => CURLOPT_SSL_VERIFYPEER, + 'transfertext' => CURLOPT_TRANSFERTEXT, + 'unrestricted_auth' => CURLOPT_UNRESTRICTED_AUTH, + 'upload' => CURLOPT_UPLOAD, + 'verbose' => CURLOPT_VERBOSE, + 'buffersize' => CURLOPT_BUFFERSIZE, + // 'closepolicy' => CURLOPT_CLOSEPOLICY, + 'connecttimeout' => CURLOPT_CONNECTTIMEOUT, + 'dns_cache_timeout' => CURLOPT_DNS_CACHE_TIMEOUT, + 'ftpsslauth' => CURLOPT_FTPSSLAUTH, + 'http_version' => CURLOPT_HTTP_VERSION, + 'httpauth' => CURLOPT_HTTPAUTH, + 'curlauth_any' => CURLAUTH_ANY, + 'curlauth_anysafe' => CURLAUTH_ANYSAFE, + 'infilesize' => CURLOPT_INFILESIZE, + 'low_speed_limit' => CURLOPT_LOW_SPEED_LIMIT, + 'low_speed_time' => CURLOPT_LOW_SPEED_TIME, + 'maxconnects' => CURLOPT_MAXCONNECTS, + 'maxredirs' => CURLOPT_MAXREDIRS, + 'port' => CURLOPT_PORT, + 'proxyauth' => CURLOPT_PROXYAUTH, + 'proxyport' => CURLOPT_PROXYPORT, + 'proxytype' => CURLOPT_PROXYTYPE, + 'resume_from' => CURLOPT_RESUME_FROM, + 'ssl_verifyhost' => CURLOPT_SSL_VERIFYHOST, + 'sslversion' => CURLOPT_SSLVERSION, + 'timecondition' => CURLOPT_TIMECONDITION, + 'timeout' => CURLOPT_TIMEOUT, + 'timevalue' => CURLOPT_TIMEVALUE, + 'cainfo' => CURLOPT_CAINFO, + 'capath' => CURLOPT_CAPATH, + 'cookie' => CURLOPT_COOKIE, + 'cookiefile' => CURLOPT_COOKIEFILE, + 'cookiejar' => CURLOPT_COOKIEJAR, + 'customrequest' => CURLOPT_CUSTOMREQUEST, + 'egdsocket' => CURLOPT_EGDSOCKET, + 'encoding' => CURLOPT_ENCODING, + 'ftpport' => CURLOPT_FTPPORT, + 'interface' => CURLOPT_INTERFACE, + 'krb4level' => CURLOPT_KRB4LEVEL, + 'postfields' => CURLOPT_POSTFIELDS, + 'proxy' => CURLOPT_PROXY, + 'proxyuserpwd' => CURLOPT_PROXYUSERPWD, + 'random_file' => CURLOPT_RANDOM_FILE, + 'range' => CURLOPT_RANGE, + 'referer' => CURLOPT_REFERER, + 'ssl_cipher_list' => CURLOPT_SSL_CIPHER_LIST, + 'sslcert' => CURLOPT_SSLCERT, + 'sslcertpasswd' => CURLOPT_SSLCERTPASSWD, + 'sslcerttype' => CURLOPT_SSLCERTTYPE, + 'sslengine' => CURLOPT_SSLENGINE, + 'sslengine_default' => CURLOPT_SSLENGINE_DEFAULT, + 'sslkey' => CURLOPT_SSLKEY, + 'sslkeypasswd' => CURLOPT_SSLKEYPASSWD, + 'sslkeytype' => CURLOPT_SSLKEYTYPE, + 'url' => CURLOPT_URL, + 'useragent' => CURLOPT_USERAGENT, + 'userpwd' => CURLOPT_USERPWD, + 'http200aliases' => CURLOPT_HTTP200ALIASES, + 'httpheader' => CURLOPT_HTTPHEADER, + 'postquote' => CURLOPT_POSTQUOTE, + 'quote' => CURLOPT_QUOTE, + 'file' => CURLOPT_FILE, + 'infile' => CURLOPT_INFILE, + 'stderr' => CURLOPT_STDERR, + 'writeheader' => CURLOPT_WRITEHEADER, + 'headerfunction' => CURLOPT_HEADERFUNCTION, + 'readfunction' => CURLOPT_READFUNCTION, + 'writefunction' => CURLOPT_WRITEFUNCTION, ]; /** @noinspection PhpInconsistentReturnPointsInspection */ /** * Initialize a cURL session + * * @param null $url - * @throws Exception + * * @return CURL + * @throws Exception */ public function __construct( $url = null ) { if ( function_exists( 'curl_init' ) ) { - /* - * For some reason on certain PHP/CURL version it causes error if the $url is null - */ + /* For some reason on certain PHP/CURL version it causes error if the $url is null */ if ( $url ) { $this->resource = curl_init( $url ); } @@ -168,14 +189,16 @@ class CURL } } else { - throw new Exception( Framework::Txt( 'CURL_NOT_INSTALLED' ) ); + $this->curlStatus = 999; } } /** * Sets an option on the given cURL session handle. + * * @param string $option - The CURLOPT_XXX option to set. * @param mixed $value - The value to be set on option + * * @return bool */ public function setOption( $option, $value ) @@ -183,12 +206,15 @@ class CURL if ( is_string( $option ) && isset( self::$optionsCodes[ $option ] ) ) { $option = self::$optionsCodes[ $option ]; } + return curl_setopt( $this->resource, $option, $value ); } /** * Sets multiple options for a cURL session + * * @param array $options - An array specifying which options to set and their values. The keys should be valid curl_setopt() constants or their integer equivalents. + * * @return bool */ public function setOptions( $options ) @@ -201,12 +227,23 @@ class CURL } } } + return curl_setopt_array( $this->resource, $options ); } /** - * set an URL + * @return array + */ + public function status() + { + return $this->curlStatus; + } + + /** + * Sets an URL. + * * @param string $url + * * @return bool */ public function setUrl( $url ) @@ -216,9 +253,11 @@ class CURL /** * TRUE to include the header in the output. + * * @param $timeOut - * @internal param bool $header + * * @return bool + * @internal param bool $header */ public function setTimeOut( $timeOut ) { @@ -227,7 +266,9 @@ class CURL /** * TRUE to include the header in the output. + * * @param bool $header + * * @return bool */ public function setHeader( $header ) @@ -244,7 +285,8 @@ class CURL } /** - * Closes a cURL session and frees all resources + * Closes a cURL session and frees all resources. + * * @return mixed */ public function close() @@ -265,6 +307,7 @@ class CURL */ public function exec() { + $this->error[ 'active' ] = false; $r = curl_exec( $this->resource ); $inf = $this->info(); if ( $inf[ 'http_code' ] == 301 || $inf[ 'http_code' ] == 302 ) { @@ -273,20 +316,25 @@ class CURL preg_match( '/Location: (http.*)/', $r, $newUrl ); $this->setOption( 'header', false ); $this->setOption( 'url', $newUrl[ 1 ] ); + return $this->exec(); } + return $r; } /** - * Gets information about the last transfer + * Gets information about the last transfer. + * * @param string $opt - correspond option + * * @return bool */ public function info( $opt = null ) { if ( $opt && is_string( $opt ) && isset( self::$infoCodes[ $opt ] ) ) { $opt = self::$infoCodes[ $opt ]; + return curl_getinfo( $this->resource, $opt ); } else { @@ -294,6 +342,12 @@ class CURL } } + /** + * @param $url + * + * @return array|false + * @throws Exception + */ public function certificate( $url ) { $errno = null; @@ -309,18 +363,19 @@ class CURL return [ 'err' => 500, 'msg' => Framework::Txt( 'INVALID_URL', 'https://' . $url ) ]; } $this->setOptions( - [ - 'url' => 'https://' . $url, - 'connecttimeout' => 10, - 'header' => true, - 'returntransfer' => true, - 'ssl_verifypeer' => false, - 'ssl_verifyhost' => 2, - 'nobody' => true, - ] + [ + 'url' => 'https://' . $url, + 'connecttimeout' => 10, + 'header' => true, + 'returntransfer' => true, + 'ssl_verifypeer' => false, + 'ssl_verifyhost' => 2, + 'nobody' => true, + ] ); if ( !( $this->validCode( $this->exec() ) ) ) { $err = $this->info(); + return [ 'err' => $err[ 'http_code' ], 'msg' => Framework::Txt( 'NO_CONNECT', 'https://' . $url ) ]; } $res = stream_context_create( [ 'ssl' => [ 'capture_peer_cert' => true ] ] ); @@ -334,42 +389,65 @@ class CURL } } + /** + * @param $url + * + * @return false|int + */ private function validateHttp( $url ) { return preg_match( '|http[s]?:\/\/[a-z0-9\.\-\_]{3,}\.[a-z]{2,5}.*|i', $url ); } + /** + * @param $response + * + * @return bool|int + */ public function getCode( $response ) { $matches = []; if ( preg_match( '/HTTP\/1\.\d+\s+(\d+)/', $response, $matches ) ) { - return ( int )$matches[ 1 ]; + return ( int ) $matches[ 1 ]; } else { return false; } } + /** + * @param $response + * + * @return bool + */ public function validCode( $response ) { return ( ( $this->getCode( $response ) >= 200 ) && ( $this->getCode( $response ) < 400 ) ); } /** - * Return the last error number and/or a string containing the last error for the current session + * Returns the last error number and/or a string containing the last error for the current session. + * * @param bool $message - return string containing the last error * @param bool $number - return error number + * * @return string */ public function error( $message = true, $number = true ) { $err = []; + $errno = curl_errno( $this->resource ); if ( $number ) { - $err[] = curl_errno( $this->resource ); + $err[] = $errno; } if ( $message ) { $err[] = curl_error( $this->resource ); } - return implode( ', ', $err ); + if ( $errno ) { + return implode( ': ', $err ); + } + else { + return null; + } } } diff --git a/Framework.php b/Framework.php index 94bb9735cf4e0e502314e7f3c7765e60f7b093a0..20f43254818da66092281ff278faec2e6f173e38 100644 --- a/Framework.php +++ b/Framework.php @@ -1,19 +1,23 @@