/**
  * Checks the Language Confidence by trying to detect the correct language for a text using the LangId.Py Service.
  * If LangId.Py is not set in configuration as it may not be available will return a negative confidence score and the default language: en.
  *
  * @param STRING $the_text						:: The text to be checked
  * @return ARRAY 								:: The LangId.Py detection result: [ service-available, lang-id, confidence-score, error-message ]
  *
  */
 public function getLanguageConfidence($the_text)
 {
     //--
     if ((string) $this->langid_service_cfg['url'] == '') {
         return (array) $this->formatLanguageConfidenceAnswer('en', -1);
         // OK (LangID Service is not available ...)
     }
     //end if
     //--
     $this->is_service_available = true;
     //--
     $the_text = (string) trim((string) $the_text);
     if ((string) $the_text == '') {
         return (array) $this->formatLanguageConfidenceAnswer('en', -2, 'NOTICE: Empty Text to check ...');
     }
     //end if
     //--
     $http_client = new SmartHttpClient();
     if ((string) SMART_FRAMEWORK_DEBUG_MODE == 'yes') {
         $http_client->debug = 1;
     }
     //end if
     //--
     $http_timeout = (int) $http_timeout;
     if ($http_timeout < 30) {
         $http_timeout = 30;
     }
     //end if
     if ($http_timeout > 300) {
         $http_timeout = 300;
     }
     //end if
     $http_client->connect_timeout = $http_timeout;
     //--
     $http_client->postvars = array('q' => (string) $the_text);
     //--
     $http_data = (array) $http_client->browse_url((string) $this->langid_service_cfg['url'], 'POST', (string) $this->langid_service_cfg['ssl'], (string) $this->langid_service_cfg['auth-user'], (string) $this->langid_service_cfg['auth-pass']);
     //--
     if ((string) $http_data['code'] != '200') {
         return (array) $this->formatLanguageConfidenceAnswer('en', -3, 'Invalid HTTP Code != 200');
     }
     //end if
     //--
     $result = (array) Smart::json_decode($http_data['content']);
     //--
     if ($result['responseStatus'] != 200) {
         return (array) $this->formatLanguageConfidenceAnswer('en', -4, 'Invalid (Json) Response Status != 200');
     }
     //end if
     if (!is_array($result['responseData'])) {
         return (array) $this->formatLanguageConfidenceAnswer('en', -5, 'Invalid (Json) Response Data Array');
     }
     //end if
     //--
     $result['responseData']['language'] = (string) trim(strtolower((string) $result['responseData']['language']));
     $result['responseData']['confidence'] = (double) $result['responseData']['confidence'];
     //--
     if (strlen($result['responseData']['language']) != 2) {
         return (array) $this->formatLanguageConfidenceAnswer('en', -6, 'Invalid Language ID Length: ' . $result['responseData']['language']);
     }
     //end if
     //--
     return (array) $this->formatLanguageConfidenceAnswer($result['responseData']['language'], $result['responseData']['confidence']);
     // OK
     //--
 }
Пример #2
0
 /**
  * Load a File or an URL
  * it may use 3 methods: FileRead, CURL or HTTP-Browser class
  *
  * @param STRING 	$y_url_or_path			:: /path/to/file | http(s)://some.url:port/path (port is optional)
  * @param NUMBER 	$y_timeout				:: timeout in seconds
  * @param ENUM 		$y_method 				:: used only for URLs, the browsing method: GET | POST
  * @param ENUM		$y_ssl_method			:: SSL Mode: tls | sslv3 | sslv2 | ssl
  * @param STRING 	$y_auth_name			:: used only for URLs, the auth user name
  * @param STRING 	$y_auth_pass			:: used only for URLs, the auth password
  * @param YES/NO	y_allow_set_credentials	:: DEFAULT MUST BE set to NO ; if YES must be set just for internal URLs ; if the $y_url_or_path to get is detected to be under current URL will send also the Unique / session IDs ; more if detected that is from admin.php and if this is set to YES will send the HTTP-BASIC Auth credentials if detected (using YES with other URLs than SmartFramework's current URL can be a serious SECURITY ISSUE, so don't !)
  */
 public static function load_url_or_file($y_url_or_path, $y_timeout = 30, $y_method = 'GET', $y_ssl_method = '', $y_auth_name = '', $y_auth_pass = '', $y_allow_set_credentials = 'no')
 {
     //-- v.2016-01-15
     // fixed sessionID with new Dynamic generated
     // TODO: use the CURL to browse also FTP and SSH ...
     //--
     $y_url_or_path = (string) $y_url_or_path;
     //--
     if ((string) $y_url_or_path == '') {
         //--
         return array('log' => 'ERROR: FILE Name is Empty ...', 'mode' => 'file', 'result' => '0', 'code' => '400', 'headers' => '', 'content' => '', 'debuglog' => '');
         //--
     }
     //end if
     //-- detect if file or url
     if (substr($y_url_or_path, 0, 7) == 'http://' or substr($y_url_or_path, 0, 8) == 'https://') {
         $is_file = 0;
         // it is a url
     } else {
         $is_file = 1;
         // it is a file
     }
     //end if
     //--
     if ($is_file == 1) {
         //--
         $y_url_or_path = trim($y_url_or_path);
         //-- try to detect if data:image/ :: {{{SYNC-DATA-IMAGE}}}
         if (strtolower(substr($y_url_or_path, 0, 11)) == 'data:image/' and stripos($y_url_or_path, ';base64,') !== false) {
             //--
             $eimg = explode(';base64,', $y_url_or_path);
             //--
             return array('log' => 'OK ? Not sure, decoded from embedded b64 image: ', 'mode' => 'embedded', 'result' => '1', 'code' => '200', 'headers' => SmartUnicode::sub_str($y_url_or_path, 0, 50) . '...', 'content' => @base64_decode(trim($eimg[1])), 'debuglog' => '');
             //--
         } elseif (is_file($y_url_or_path)) {
             //--
             return array('log' => 'OK: FILE Exists', 'mode' => 'file', 'result' => '1', 'code' => '200', 'headers' => 'Content-Disposition: inline; filename="' . basename($y_url_or_path) . '"' . "\n", 'content' => SmartFileSystem::read($y_url_or_path), 'debuglog' => '');
             //--
         } else {
             //--
             return array('log' => 'ERROR: FILE Not Found or Invalid Data ...', 'mode' => 'file', 'result' => '0', 'code' => '404', 'headers' => '', 'content' => '', 'debuglog' => '');
             //--
         }
         //end if else
         //--
     } else {
         //--
         if ((string) $y_ssl_method == '') {
             if (defined('SMART_FRAMEWORK_SSL_MODE')) {
                 $y_ssl_method = (string) SMART_FRAMEWORK_SSL_MODE;
             } else {
                 Smart::log_notice('NOTICE: LibUtils/Load-URL-or-File // The SSL Method not defined and SMART_FRAMEWORK_SSL_MODE was not defined. Using the `tls` as default ...');
                 $y_ssl_method = 'tls';
             }
             //end if else
         }
         //end if
         //--
         $browser = new SmartHttpClient();
         //--
         $y_timeout = Smart::format_number_int($y_timeout, '+');
         if ($y_timeout <= 0) {
             $y_timeout = 30;
             // default value
         }
         //end if
         $browser->connect_timeout = (int) $y_timeout;
         //--
         if ((string) SMART_FRAMEWORK_DEBUG_MODE == 'yes') {
             $browser->debug = 1;
         }
         //end if
         //--
         if ((string) self::get_server_current_protocol() == 'https://') {
             $tmp_current_protocol = 'https://';
         } else {
             $tmp_current_protocol = 'http://';
         }
         //end if else
         //--
         $tmp_current_server = self::get_server_current_domain_name();
         $tmp_current_port = self::get_server_current_port();
         //--
         $tmp_current_path = self::get_server_current_request_uri();
         $tmp_current_script = self::get_server_current_full_script();
         //--
         $tmp_test_url_arr = Smart::separe_url_parts($y_url_or_path);
         $tmp_test_browser_id = self::get_os_browser_ip();
         //--
         $tmp_extra_log = '';
         if ((string) SMART_FRAMEWORK_DEBUG_MODE == 'yes') {
             $tmp_extra_log .= "\n" . '===== # =====' . "\n";
         }
         //end if
         //--
         $cookies = array();
         $auth_name = (string) $y_auth_name;
         $auth_pass = (string) $y_auth_pass;
         //--
         if ((string) $y_allow_set_credentials == 'yes') {
             //--
             if ((string) SMART_FRAMEWORK_DEBUG_MODE == 'yes') {
                 $tmp_extra_log .= '[EXTRA]: I will try to detect if this is my current Domain and I will check if it is safe to send my sessionID COOKIE and my Auth CREDENTIALS ...' . "\n";
             }
             //end if
             //--
             if ((string) $tmp_current_protocol == (string) $tmp_test_url_arr['protocol'] and (string) $tmp_current_server == (string) $tmp_test_url_arr['server'] and (string) $tmp_current_port == (string) $tmp_test_url_arr['port']) {
                 //--
                 if ((string) SMART_FRAMEWORK_DEBUG_MODE == 'yes') {
                     $tmp_extra_log .= '[EXTRA]: OK, Seems that the browsed Domain is identical with my current Domain which is: ' . $tmp_current_protocol . $tmp_current_server . ':' . $tmp_current_port . ' and the browsed one is: ' . $tmp_test_url_arr['protocol'] . $tmp_test_url_arr['server'] . ':' . $tmp_test_url_arr['port'] . "\n";
                     $tmp_extra_log .= '[EXTRA]: I will also check if my current script and path are identical with the browsed ones ...' . "\n";
                 }
                 //end if
                 //--
                 if ((string) $tmp_current_script == (string) $tmp_test_url_arr['scriptname'] and substr($tmp_current_path, 0, strlen($tmp_current_script)) == (string) $tmp_test_url_arr['scriptname']) {
                     //--
                     if ((string) SMART_FRAMEWORK_DEBUG_MODE == 'yes') {
                         $tmp_extra_log .= '[EXTRA]: OK, Seems that the current script is identical with the browsed one :: ' . 'Current Path is: \'' . $tmp_current_script . '\' / Browsed Path is: \'' . $tmp_test_url_arr['scriptname'] . '\' !' . "\n";
                         $tmp_extra_log .= '[EXTRA]: I will check if I have to send my SessionID so I will check the browserID ...' . "\n";
                     }
                     //end if
                     //--
                     $browser->useragent = (string) self::get_selfrobot_useragent_name();
                     // this must be set just when detected the same path and script ; it is a requirement to detect it as the self-robot [ @s# ] in order to send the credentials or the current
                     //-- {{{SYNC-SMART-UNIQUE-COOKIE}}}
                     if (defined('SMART_FRAMEWORK_UNIQUE_ID_COOKIE_NAME') and !defined('SMART_FRAMEWORK_UNIQUE_ID_COOKIE_SKIP')) {
                         if ((string) SMART_FRAMEWORK_UNIQUE_ID_COOKIE_NAME != '') {
                             if (SmartFrameworkSecurity::ValidateVariableName(strtolower((string) SMART_FRAMEWORK_UNIQUE_ID_COOKIE_NAME))) {
                                 //--
                                 if ((string) SMART_APP_VISITOR_COOKIE != '') {
                                     // if set, then forward
                                     if ((string) SMART_FRAMEWORK_DEBUG_MODE == 'yes') {
                                         $tmp_extra_log .= '[EXTRA]: OK, I will send my current Visitor Unique Cookie ID as it is set and not empty ...' . "\n";
                                     }
                                     //end if
                                     $cookies[(string) SMART_FRAMEWORK_UNIQUE_ID_COOKIE_NAME] = (string) SMART_APP_VISITOR_COOKIE;
                                     // this is a requirement
                                 }
                                 //end if
                                 //--
                             }
                             //end if
                         }
                         //end if
                     }
                     //end if
                     //-- #end# sync
                     if ((string) SmartAuth::get_login_method() == 'HTTP-BASIC' and (string) $auth_name == '' and (string) $auth_pass == '' and strpos($tmp_current_script, '/admin.php') !== false and strpos($tmp_test_url_arr['scriptname'], '/admin.php') !== false) {
                         //--
                         if ((string) SMART_FRAMEWORK_DEBUG_MODE == 'yes') {
                             $tmp_extra_log .= '[EXTRA]: HTTP-BASIC Auth method detected / Allowed to pass the Credentials - as the browsed URL belongs to this ADMIN Server as I run, the Auth credentials are set but passed as empty - everything seems to be safe I will send my credentials: USERNAME = \'' . SmartAuth::get_login_id() . '\' ; PASS = *****' . "\n";
                         }
                         //end if
                         //--
                         $auth_name = (string) SmartAuth::get_login_id();
                         $auth_pass = (string) SmartAuth::get_login_password();
                         //--
                     }
                     //end if
                     //--
                 } else {
                     //--
                     if ((string) SMART_FRAMEWORK_DEBUG_MODE == 'yes') {
                         $tmp_extra_log .= '[EXTRA]: Seems that the scripts are NOT identical :: ' . 'Current Script is: \'' . $tmp_current_script . '\' / Browsed Script is: \'' . $tmp_test_url_arr['scriptname'] . '\' !' . "\n";
                         $tmp_extra_log .= '[EXTRA]: This is the diff for having a comparation: ' . substr($tmp_current_path, 0, strlen($tmp_current_script)) . "\n";
                     }
                     //end if
                     //--
                 }
                 //end if
                 //--
             }
             //end if
             //--
         }
         //end if
         //--
         $browser->cookies = (array) $cookies;
         //--
         $data = (array) $browser->browse_url($y_url_or_path, $y_method, $y_ssl_method, $auth_name, $auth_pass);
         // do browse
         //--
         return array('log' => (string) $data['log'] . $tmp_extra_log, 'mode' => (string) $data['mode'], 'result' => (string) $data['result'], 'code' => (string) $data['code'], 'headers' => (string) $data['headers'], 'content' => (string) $data['content'], 'debuglog' => (string) $data['debuglog']);
         //--
     }
     //end if else
     //--
 }