Check database connection character set is utf8.
public static isDatabaseConnectionUTF8 ( ) : boolean | ||
return | boolean | True if it is (or doesn't matter); false otherwise |
/** * Performs a batch insert into a specific table using either LOAD DATA INFILE or plain INSERTs, * as a fallback. On MySQL, LOAD DATA INFILE is 20x faster than a series of plain INSERTs. * * @param string $tableName PREFIXED table name! you must call Common::prefixTable() before passing the table name * @param array $fields array of unquoted field names * @param array $values array of data to be inserted * @param bool $throwException Whether to throw an exception that was caught while trying * LOAD DATA INFILE, or not. * @throws Exception * @return bool True if the bulk LOAD was used, false if we fallback to plain INSERTs */ public static function tableInsertBatch($tableName, $fields, $values, $throwException = false) { $filePath = PIWIK_USER_PATH . '/tmp/assets/' . $tableName . '-' . Common::generateUniqId() . '.csv'; $filePath = SettingsPiwik::rewriteTmpPathWithInstanceId($filePath); $loadDataInfileEnabled = Config::getInstance()->General['enable_load_data_infile']; if ($loadDataInfileEnabled && Db::get()->hasBulkLoader()) { try { $fileSpec = array('delim' => "\t", 'quote' => '"', 'escape' => '\\\\', 'escapespecial_cb' => function ($str) { return str_replace(array(chr(92), chr(34)), array(chr(92) . chr(92), chr(92) . chr(34)), $str); }, 'eol' => "\r\n", 'null' => 'NULL'); // hack for charset mismatch if (!DbHelper::isDatabaseConnectionUTF8() && !isset(Config::getInstance()->database['charset'])) { $fileSpec['charset'] = 'latin1'; } self::createCSVFile($filePath, $fileSpec, $values); if (!is_readable($filePath)) { throw new Exception("File {$filePath} could not be read."); } $rc = self::createTableFromCSVFile($tableName, $fields, $filePath, $fileSpec); if ($rc) { unlink($filePath); return true; } } catch (Exception $e) { Log::info("LOAD DATA INFILE failed or not supported, falling back to normal INSERTs... Error was: %s", $e->getMessage()); if ($throwException) { throw $e; } } } // if all else fails, fallback to a series of INSERTs @unlink($filePath); self::tableInsertBatchIterate($tableName, $fields, $values); return false; }
/** * Performs a batch insert into a specific table using either LOAD DATA INFILE or plain INSERTs, * as a fallback. On MySQL, LOAD DATA INFILE is 20x faster than a series of plain INSERTs. * * @param string $tableName PREFIXED table name! you must call Common::prefixTable() before passing the table name * @param array $fields array of unquoted field names * @param array $values array of data to be inserted * @param bool $throwException Whether to throw an exception that was caught while trying * LOAD DATA INFILE, or not. * @throws Exception * @return bool True if the bulk LOAD was used, false if we fallback to plain INSERTs */ public static function tableInsertBatch($tableName, $fields, $values, $throwException = false) { $filePath = StaticContainer::get('path.tmp') . '/assets/' . $tableName . '-' . Common::generateUniqId() . '.csv'; $loadDataInfileEnabled = Config::getInstance()->General['enable_load_data_infile']; if ($loadDataInfileEnabled && Db::get()->hasBulkLoader()) { try { $fileSpec = array('delim' => "\t", 'quote' => '"', 'escape' => '\\\\', 'escapespecial_cb' => function ($str) { return str_replace(array(chr(92), chr(34)), array(chr(92) . chr(92), chr(92) . chr(34)), $str); }, 'eol' => "\r\n", 'null' => 'NULL'); // hack for charset mismatch if (!DbHelper::isDatabaseConnectionUTF8() && !isset(Config::getInstance()->database['charset'])) { $fileSpec['charset'] = 'latin1'; } self::createCSVFile($filePath, $fileSpec, $values); if (!is_readable($filePath)) { throw new Exception("File {$filePath} could not be read."); } $rc = self::createTableFromCSVFile($tableName, $fields, $filePath, $fileSpec); if ($rc) { unlink($filePath); return true; } } catch (Exception $e) { if ($throwException) { throw $e; } } } // if all else fails, fallback to a series of INSERTs @unlink($filePath); self::tableInsertBatchIterate($tableName, $fields, $values); return false; }
/** * Write configuration file from session-store */ private function createConfigFile($dbInfos) { $config = Config::getInstance(); // make sure DB sessions are used if the filesystem is NFS if (Filesystem::checkIfFileSystemIsNFS()) { $config->General['session_save_handler'] = 'dbtable'; } if (count($headers = ProxyHeaders::getProxyClientHeaders()) > 0) { $config->General['proxy_client_headers'] = $headers; } if (count($headers = ProxyHeaders::getProxyHostHeaders()) > 0) { $config->General['proxy_host_headers'] = $headers; } if (Common::getRequestVar('clientProtocol', 'http', 'string') == 'https') { $protocol = 'https'; } else { $protocol = ProxyHeaders::getProtocolInformation(); } if (!empty($protocol) && !\Piwik\ProxyHttp::isHttps()) { $config->General['assume_secure_protocol'] = '1'; } $config->General['salt'] = Common::generateUniqId(); $config->General['installation_in_progress'] = 1; $config->database = $dbInfos; if (!DbHelper::isDatabaseConnectionUTF8()) { $config->database['charset'] = 'utf8'; } $config->forceSave(); }
/** * Installation Step 4: Database Check */ function databaseCheck() { $this->checkPreviousStepIsValid(__FUNCTION__); $view = new View('@Installation/databaseCheck', $this->getInstallationSteps(), __FUNCTION__); $error = false; $this->skipThisStep(__FUNCTION__); if (isset($this->session->databaseVersionOk) && $this->session->databaseVersionOk === true) { $view->databaseVersionOk = true; } else { $error = true; } if (isset($this->session->databaseCreated) && $this->session->databaseCreated === true) { $dbInfos = $this->session->db_infos; $view->databaseName = $dbInfos['dbname']; $view->databaseCreated = true; } else { $error = true; } $this->createDbFromSessionInformation(); $db = Db::get(); try { $db->checkClientVersion(); } catch (Exception $e) { $view->clientVersionWarning = $e->getMessage(); $error = true; } if (!DbHelper::isDatabaseConnectionUTF8()) { $dbInfos = $this->session->db_infos; $dbInfos['charset'] = 'utf8'; $this->session->db_infos = $dbInfos; } $view->showNextStep = true; $this->session->currentStepDone = __FUNCTION__; if ($error === false) { $this->redirectToNextStep(__FUNCTION__); } return $view->render(); }
/** * Write configuration file from session-store */ private function createConfigFile($dbInfos) { $config = Config::getInstance(); // make sure DB sessions are used if the filesystem is NFS if (Filesystem::checkIfFileSystemIsNFS()) { $config->General['session_save_handler'] = 'dbtable'; } if (count($headers = ProxyHeaders::getProxyClientHeaders()) > 0) { $config->General['proxy_client_headers'] = $headers; } if (count($headers = ProxyHeaders::getProxyHostHeaders()) > 0) { $config->General['proxy_host_headers'] = $headers; } if (Common::getRequestVar('clientProtocol', 'http', 'string') == 'https') { $protocol = 'https'; } else { $protocol = ProxyHeaders::getProtocolInformation(); } if (!empty($protocol) && !\Piwik\ProxyHttp::isHttps()) { $config->General['assume_secure_protocol'] = '1'; } $config->General['salt'] = Common::generateUniqId(); $config->General['installation_in_progress'] = 1; $config->database = $dbInfos; if (!DbHelper::isDatabaseConnectionUTF8()) { $config->database['charset'] = 'utf8'; } # Improved Security with IBM Bluemix # With SSL ALWAYS available for all Bluemix apps, let's require all requests # to be made over SSL (https) so that data is NOT sent in the clear. # Non-ssl requests will trigger a # Error: Form security failed. # Please reload the form and check that your cookies are enabled # Reference: http://piwik.org/faq/how-to/faq_91/ # Reference: https://developer.ibm.com/answers/questions/8312/how-do-i-enable-tlsssl-for-my-bluemix-application/ $config->General['assume_secure_protocol'] = 1; $config->General['force_ssl'] = 1; # Setup proxy_client_headers to accurately detect GeoIPs of visiting clients $config->General['proxy_client_headers'] = array("HTTP_X_CLIENT_IP", "HTTP_X_FORWARDED_FOR", "HTTP_X_CLUSTER_CLIENT_IP", "HTTP_CLIENT_IP"); $config->General['proxy_host_headers'] = "HTTP_X_FORWARDED_HOST"; # Implement some default settings that optimize performance $config->General['enabled_periods_UI'] = "day,week,month,year"; $config->General['enabled_periods_API'] = "day,week,month,year"; $config->General['action_category_level_limit'] = 3; $config->General['show_multisites_sparklines'] = 0; $config->General['anonymous_user_enable_use_segments_API'] = 0; $config->General['browser_archiving_disabled_enforce'] = 1; $config->General['enable_create_realtime_segments'] = 0; $config->General['enable_segment_suggested_values'] = 0; $config->General['adding_segment_requires_access'] = "superuser"; $config->General['allow_adding_segments_for_all_websites'] = 0; $config->General['datatable_row_limits'] = "5,10,25,50"; $config->General['enable_browser_archiving_triggering'] = 0; $config->General['multisites_refresh_after_seconds'] = 0; $config->General['enable_delete_old_data_settings_admin'] = 0; $config->General['enable_auto_update'] = 0; $config->Debug['enable_measure_piwik_usage_in_idsite'] = 0; $config->Debug['allow_upgrades_to_beta'] = 0; $config->Tracker['new_visit_api_requires_admin'] = 0; # Let us have this Piwik deploy track itself to get some early data and success :-) # $config->Debug['enable_measure_piwik_usage_in_idsite'] = 1; # Emailing the easy way with IBM Bluemix + the SendGrid Service if (isset($_ENV["REDISHOSTNAME"])) { $config->RedisCache['host'] = $_ENV["REDISHOSTNAME"]; $config->RedisCache['port'] = $_ENV["REDISPORT"]; $config->RedisCache['timeout'] = 0.0; $config->RedisCache['password'] = $_ENV["REDISPASSWORD"]; $config->RedisCache['database'] = 14; $config->ChainedCache['backends'] = array("array", "redis"); } # Let's setup the config files trusted hosts entries to handle # 1...N amount of user-defined IBM Bluemix app routes if (isset($_ENV["APPURIS"])) { foreach ($_ENV["APPURIS"] as $application_uri) { $this->addTrustedHosts("https://" . $application_uri); } } # Emailing the easy way with IBM Bluemix + the SendGrid Service if (isset($_ENV["MAILHOST"])) { $config->mail['transport'] = "smtp"; $config->mail['port'] = 587; $config->mail['type'] = "Plain"; $config->mail['host'] = $_ENV["MAILHOST"]; $config->mail['username'] = $_ENV["MAILUSER"]; $config->mail['password'] = $_ENV["MAILPASSWORD"]; } $config->forceSave(); // re-save the currently viewed language (since we saved the config file, there is now a salt which makes the // existing session cookie invalid) $this->resetLanguageCookie(); }