BackupGuard
6 years ago
Dropbox
6 years ago
Request
6 years ago
SGArchive.php
6 years ago
SGAuthClient.php
6 years ago
SGCallback.php
6 years ago
SGCdrEntry.php
6 years ago
SGCharsetHandler.php
6 years ago
SGDBState.php
6 years ago
SGEntry.php
6 years ago
SGFileEntry.php
6 years ago
SGFileState.php
6 years ago
SGMigrateState.php
6 years ago
SGMysqldump.php
6 years ago
SGReloadHandler.php
6 years ago
SGReloader.php
6 years ago
SGReloaderState.php
6 years ago
SGState.php
6 years ago
SGUploadHandler.php
6 years ago
SGUploadState.php
6 years ago
SGCharsetHandler.php
94 lines
| 1 | <?php |
| 2 | |
| 3 | class SGCharsetHandler |
| 4 | { |
| 5 | private $utf8mb4Available = false; |
| 6 | private $utf8mb4520Available = false; |
| 7 | |
| 8 | public function __construct() |
| 9 | { |
| 10 | $this->utf8mb4Available = $this->mysqlHasCapability('utf8mb4'); |
| 11 | $this->utf8mb4520Available = $this->mysqlHasCapability('utf8mb4_520'); |
| 12 | } |
| 13 | |
| 14 | public function replaceInvalidCharsets($query) |
| 15 | { |
| 16 | $search = array(); |
| 17 | $replace = array(); |
| 18 | |
| 19 | //check for utf8mb4_520 support first |
| 20 | if (!$this->utf8mb4520Available) { |
| 21 | $search[] = 'COLLATE=utf8mb4_unicode_520_ci'; |
| 22 | $replace[] = $this->utf8mb4Available?'COLLATE=utf8mb4_unicode_ci':'COLLATE=utf8_general_ci'; |
| 23 | |
| 24 | $search[] = 'COLLATE utf8mb4_unicode_520_ci'; |
| 25 | $replace[] = $this->utf8mb4Available?'COLLATE utf8mb4_unicode_ci':'COLLATE utf8_general_ci'; |
| 26 | } |
| 27 | |
| 28 | //then check for utf8mb4 support |
| 29 | if (!$this->utf8mb4Available) { |
| 30 | $search[] = 'COLLATE=utf8mb4_unicode_ci'; |
| 31 | $replace[] = 'COLLATE=utf8_general_ci'; |
| 32 | |
| 33 | $search[] = 'COLLATE utf8mb4_unicode_ci'; |
| 34 | $replace[] = 'COLLATE utf8_general_ci'; |
| 35 | |
| 36 | $search[] = 'CHARACTER SET utf8mb4'; |
| 37 | $replace[] = 'CHARACTER SET utf8'; |
| 38 | |
| 39 | $search[] = 'CHARSET=utf8mb4'; |
| 40 | $replace[] = 'CHARSET=utf8'; |
| 41 | |
| 42 | $search[] = 'character_set_client = utf8mb4'; |
| 43 | $replace[] = 'character_set_client = utf8'; |
| 44 | |
| 45 | $search[] = 'SET NAMES utf8mb4'; |
| 46 | $replace[] = 'SET NAMES utf8'; |
| 47 | } |
| 48 | |
| 49 | if (count($search) && count($replace)) { |
| 50 | $query = str_replace($search, $replace, $query); |
| 51 | } |
| 52 | |
| 53 | return $query; |
| 54 | } |
| 55 | |
| 56 | public function getMysqlClientInfo() |
| 57 | { |
| 58 | if (function_exists('mysqli_connect')) { |
| 59 | if (version_compare(phpversion(), '5.5', '>=') || !function_exists('mysql_connect')) { |
| 60 | return @mysqli_get_client_info(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return SG_MYSQL_VERSION; |
| 65 | } |
| 66 | |
| 67 | public function mysqlHasCapability($cap) |
| 68 | { |
| 69 | if ($cap == 'utf8mb4') { |
| 70 | if (version_compare(SG_MYSQL_VERSION, '5.5.3', '<')) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | $clientVersion = $this->getMysqlClientInfo(); |
| 75 | /* |
| 76 | * libmysql has supported utf8mb4 since 5.5.3, same as the MySQL server. |
| 77 | * mysqlnd has supported utf8mb4 since 5.0.9. |
| 78 | */ |
| 79 | if (false !== strpos($clientVersion, 'mysqlnd')) { |
| 80 | $clientVersion = preg_replace('/^\D+([\d.]+).*/', '$1', $clientVersion); |
| 81 | return version_compare($clientVersion, '5.0.9', '>='); |
| 82 | } |
| 83 | else { |
| 84 | return version_compare($clientVersion, '5.5.3', '>='); |
| 85 | } |
| 86 | } |
| 87 | else if ($cap == 'utf8mb4_520') { |
| 88 | return version_compare(SG_MYSQL_VERSION, '5.6', '>='); |
| 89 | } |
| 90 | |
| 91 | return false; |
| 92 | } |
| 93 | } |
| 94 |