PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/assets/snippets/ajaxSearch/classes/ajaxSearchConfig.class.inc.php

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