|
| 1 | +<?php |
| 2 | +/* ----------------------------------------------------------------------------- |
| 3 | +* Snippet: AjaxSearch |
| 4 | +* ----------------------------------------------------------------------------- |
| 5 | +* @package AjaxSearchConfig |
| 6 | +* |
| 7 | +* @author Coroico - www.evo.wangba.fr |
| 8 | +* @version 1.10.2 |
| 9 | +* @date 12/04/2016 |
| 10 | +* |
| 11 | +* Purpose: |
| 12 | +* The AjaxSearchConfig class contains all functions and data used to manage configuration context |
| 13 | +* |
| 14 | +*/ |
| 15 | + |
| 16 | +class AjaxSearchConfig { |
| 17 | + |
| 18 | + // public variables |
| 19 | + var $pgCharset; |
| 20 | + var $dbCharset; |
| 21 | + var $isAjax; |
| 22 | + var $cfg = array(); |
| 23 | + var $dcfg = array(); |
| 24 | + var $ucfg; |
| 25 | + var $bcfg = array(); |
| 26 | + var $scfg = array(); |
| 27 | + var $lang; |
| 28 | + |
| 29 | + // private variables |
| 30 | + // Conversion code name between html page character encoding and Mysql character encoding |
| 31 | + // Some others conversions should be added if needed. Otherwise Page charset = Database charset |
| 32 | + var $_pageCharset = array('utf8' => 'UTF-8', 'latin1' => 'ISO-8859-1', 'latin2' => 'ISO-8859-2', 'cp1251' => 'windows-1251'); |
| 33 | + |
| 34 | + function __construct($dcfg, $cfg) { |
| 35 | + global $modx; |
| 36 | + $this->dbCharset = $modx->db->config['charset']; |
| 37 | + if($this->dbCharset=='utf8mb4') $this->dbCharset = 'utf8'; |
| 38 | + $this->pcreModifier = ($this->dbCharset == "utf8") ? 'iu' : 'i'; |
| 39 | + $this->dcfg = $dcfg; |
| 40 | + $this->cfg = $cfg; |
| 41 | + } |
| 42 | + /* |
| 43 | + * Init the configuration |
| 44 | + */ |
| 45 | + function initConfig(&$msgErr) { |
| 46 | + $msgErr = ''; |
| 47 | + if (!isset($_POST['ucfg'])) { |
| 48 | + |
| 49 | + $this->isAjax = false; |
| 50 | + |
| 51 | + $this->ucfg = $this->getUserConfig(); |
| 52 | + |
| 53 | + $this->bcfg = array_merge($this->dcfg, (array)$this->ucfg); |
| 54 | + |
| 55 | + $this->scfg[DEFAULT_SITE][DEFAULT_SUBSITE] = array(); |
| 56 | + } else { |
| 57 | + |
| 58 | + $this->isAjax = true; |
| 59 | + |
| 60 | + $this->ucfg = $this->parseUserConfig(strip_tags($_POST['ucfg'])); |
| 61 | + |
| 62 | + $this->bcfg = array_merge($this->dcfg, (array)$this->ucfg); |
| 63 | + |
| 64 | + $this->cfg = $this->bcfg; |
| 65 | + |
| 66 | + $this->scfg[DEFAULT_SITE][DEFAULT_SUBSITE] = array(); |
| 67 | + } |
| 68 | + |
| 69 | + $this->_loadLang(); |
| 70 | + |
| 71 | + $valid = $this->_setCharset($msgErr); |
| 72 | + return $valid; |
| 73 | + } |
| 74 | + /* |
| 75 | + * Load the language file |
| 76 | + */ |
| 77 | + function _loadLang() { |
| 78 | + $_lang = array(); |
| 79 | + |
| 80 | + $language = 'english'; |
| 81 | + include AS_PATH . "lang/{$language}.inc.php"; |
| 82 | + |
| 83 | + if (($this->cfg['language'] != '') && ($this->cfg['language'] != $language)) { |
| 84 | + if (file_exists(AS_PATH . "lang/{$this->cfg['language']}.inc.php")) include AS_PATH . "lang/" . $this->cfg['language'] . ".inc.php"; |
| 85 | + } |
| 86 | + $this->lang = $_lang; |
| 87 | + } |
| 88 | + /* |
| 89 | + * Display config arrays |
| 90 | + */ |
| 91 | + function displayConfig(& $asUtil) { |
| 92 | + if ($asUtil->dbg) { |
| 93 | + if ($this->cfg['config']) $asUtil->dbgRecord($this->readConfigFile($this->cfg['config']), __FUNCTION__ . ' - ' . $this->cfg['config']); |
| 94 | + $asUtil->dbgRecord($this->cfg, __FUNCTION__ . ' - Config before parameter checking'); |
| 95 | + } |
| 96 | + } |
| 97 | + /* |
| 98 | + * Set the Page charset |
| 99 | + */ |
| 100 | + function _setCharset(&$msgErr) { |
| 101 | + $valid = false; |
| 102 | + $msgErr = ''; |
| 103 | + |
| 104 | + $this->pgCharset = array_key_exists($this->dbCharset, $this->_pageCharset) ? $this->_pageCharset[$this->dbCharset] : $this->dbCharset; |
| 105 | + |
| 106 | + if (isset($this->dbCharset) && isset($this->_pageCharset[$this->dbCharset])) { |
| 107 | + |
| 108 | + if ($this->dbCharset == 'utf8' && !extension_loaded('mbstring')) { |
| 109 | + $msgErr = "AjaxSearch error: php_mbstring extension required"; |
| 110 | + } else { |
| 111 | + if ($this->dbCharset == 'utf8' && $this->cfg['mbstring']) mb_internal_encoding("UTF-8"); |
| 112 | + $this->pgCharset = $this->_pageCharset[$this->dbCharset]; |
| 113 | + $valid = true; |
| 114 | + } |
| 115 | + } elseif (!isset($this->dbCharset)) { |
| 116 | + $msgErr = "AjaxSearch error: database_connection_charset not set. Check your MODX config file"; |
| 117 | + } elseif (!strlen($this->dbCharset)) { |
| 118 | + $msgErr = "AjaxSearch error: database_connection_charset is null. Check your MODX config file"; |
| 119 | + } else { |
| 120 | + // if you get this message, simply update the $pageCharset array in search.class.inc.php file |
| 121 | + // with the appropriate mapping between Mysql Charset and Html charset |
| 122 | + // eg: 'latin2' => 'ISO-8859-2' |
| 123 | + $msgErr = "AjaxSearch error: unknown database_connection_charset = {$this->dbCharset}<br />Add the appropriate Html charset mapping in the ajaxSearchConfig.class.inc.php file"; |
| 124 | + } |
| 125 | + return $valid; |
| 126 | + } |
| 127 | + /* |
| 128 | + * Save the current configuration |
| 129 | + */ |
| 130 | + function saveConfig($site, $subsite) { |
| 131 | + if (!isset($this->scfg[$site][$subsite])) $this->scfg[$site][$subsite] = array(); |
| 132 | + foreach ($this->cfg as $key => $value) { |
| 133 | + if (!isset($this->bcfg[$key]) || ($this->bcfg[$key] != $value)) $this->scfg[$site][$subsite][$key] = $value; |
| 134 | + } |
| 135 | + } |
| 136 | + /* |
| 137 | + * Restore a named configuration |
| 138 | + */ |
| 139 | + function restoreConfig($site, $subsite) { |
| 140 | + if (isset($this->scfg[$site][$subsite])) $this->cfg = array_merge($this->bcfg, $this->scfg[$site][$subsite]); |
| 141 | + else $this->cfg = array_merge($this->bcfg, $this->scfg[DEFAULT_SITE][DEFAULT_SUBSITE]); |
| 142 | + } |
| 143 | + /* |
| 144 | + * Choose the appropriate configuration for displaying results |
| 145 | + */ |
| 146 | + function chooseConfig($site, $subsite, $display) { |
| 147 | + $s = ($display != MIXED) ? $site : DEFAULT_SITE; |
| 148 | + $ss = ($display != MIXED) ? $subsite : DEFAULT_SUBSITE; |
| 149 | + $this->restoreConfig($s, $ss); |
| 150 | + } |
| 151 | + /* |
| 152 | + * Create a config by merging site and category config |
| 153 | + */ |
| 154 | + function addConfigFromCateg($site, $categ, $ctg) { |
| 155 | + if (($site) && ($categ) && (!isset($this->scfg[$site][$categ]))) { |
| 156 | + if (isset($this->scfg[$site][$DEFAULT_SUBSITE])) $s = $this->scfg[$site][$DEFAULT_SUBSITE]; |
| 157 | + else $s = array(); |
| 158 | + $this->scfg[$site][$categ] = array_merge((array)$s, (array)$ctg); |
| 159 | + } |
| 160 | + } |
| 161 | + /* |
| 162 | + * Get the non default configuration (advSearch and subSearch excepted) |
| 163 | + */ |
| 164 | + function getUserConfig() { |
| 165 | + $ucfg = array(); |
| 166 | + foreach ($this->cfg as $key => $value) { |
| 167 | + if ($key != 'subSearch' && $value != $this->dcfg[$key]) $ucfg[$key] = $this->cfg[$key]; |
| 168 | + } |
| 169 | + return $ucfg; |
| 170 | + } |
| 171 | + /* |
| 172 | + * Parse the non default configuration from string |
| 173 | + */ |
| 174 | + function parseUserConfig($strUcfg) { |
| 175 | + $ucfg = array(); |
| 176 | + $pattern = '/&([^=]*)=`([^`]*)`/'; |
| 177 | + preg_match_all($pattern, $strUcfg, $out); |
| 178 | + foreach ($out[1] as $key => $values) { |
| 179 | + // remove any @BINDINGS in posted user config for security reasons |
| 180 | + $ucfg[$out[1][$key]] = preg_replace('/@(#|FILE|DIRECTORY|DOCUMENT|CHUNK|INHERIT|SELECT|EVAL|CHUNK)[: ]/i', '', $out[2][$key]); |
| 181 | + |
| 182 | + } |
| 183 | + return $ucfg; |
| 184 | + } |
| 185 | + /* |
| 186 | + * Set the AjaxSearch snippet call |
| 187 | + */ |
| 188 | + function setAsCall($ucfg) { |
| 189 | + $tpl = "&%s=`%s` "; |
| 190 | + $asCall = ''; |
| 191 | + foreach ($ucfg as $key => $value) $asCall.= sprintf($tpl, $key, $value); |
| 192 | + return $asCall; |
| 193 | + } |
| 194 | + /* |
| 195 | + * Read config file |
| 196 | + */ |
| 197 | + function readConfigFile($config) { |
| 198 | + global $modx; |
| 199 | + $configFile = (substr($config, 0, 6) != "@FILE:") ? AS_PATH . "configs/$config.config.php" : $modx->config['base_path'] . trim(substr($config, 6, strlen($config)-6)); |
| 200 | + $fh = fopen($configFile, 'r'); |
| 201 | + $output = fread($fh, filesize($configFile)); |
| 202 | + fclose($fh); |
| 203 | + return "\n" . $output; |
| 204 | + } |
| 205 | +} |
0 commit comments