Skip to content

Commit

Permalink
Merge pull request #11699 from totten/master-i18n-sql
Browse files Browse the repository at this point in the history
(civicrm-setup/1) CRM_Core_I18n - Loosen coupling to DB layer
  • Loading branch information
monishdeb authored Feb 22, 2018
2 parents 9bf49eb + 2c4bba5 commit 50671bb
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions CRM/Core/I18n.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,34 @@ class CRM_Core_I18n {
*/
const NONE = 'none', AUTO = 'auto';

/**
* @var callable|NULL
* A callback function which handles SQL string encoding.
* Set NULL to use the default, CRM_Core_DAO::escapeString().
* This is used by `ts(..., [escape=>sql])`.
*
* This option is not intended for general consumption. It is only intended
* for certain pre-boot/pre-install contexts.
*
* You might ask, "Why on Earth does string-translation have an opinion on
* SQL escaping?" Good question!
*/
public static $SQL_ESCAPER = NULL;

/**
* Encode a string for use in SQL.
*
* @param string $text
* @return string
*/
protected static function escapeSql($text) {
if (self::$SQL_ESCAPER == NULL) {
return CRM_Core_DAO::escapeString($text);
}
else {
return call_user_func(self::$SQL_ESCAPER, $text);
}
}

/**
* A PHP-gettext instance for string translation;
Expand Down Expand Up @@ -289,7 +317,7 @@ public function crm_translate($text, $params = array()) {
// in such cases we return early, only doing SQL/JS escaping
if (isset($params['skip']) and $params['skip']) {
if (isset($escape) and ($escape == 'sql')) {
$text = CRM_Core_DAO::escapeString($text);
$text = self::escapeSql($text);
}
if (isset($escape) and ($escape == 'js')) {
$text = addcslashes($text, "'");
Expand Down Expand Up @@ -351,7 +379,7 @@ public function crm_translate($text, $params = array()) {

// escape SQL if we were asked for it
if (isset($escape) and ($escape == 'sql')) {
$text = CRM_Core_DAO::escapeString($text);
$text = self::escapeSql($text);
}

// escape for JavaScript (if requested)
Expand Down

0 comments on commit 50671bb

Please sign in to comment.