Просмотр исходного кода

#15 - Developer Module Problems (Export)

AntonLV 12 лет назад
Родитель
Сommit
a7c6afd4f2

+ 9 - 1
modules/boonex/developer/classes/BxDevDb.php

@@ -17,6 +17,8 @@ class BxDevDb extends BxDolModuleDb {
     }
 
     function getQueryInsert($sTable, $aItems, $mixedComment = false, $aExclude = array('id')) {
+    	bx_import('BxDevFunctions');
+
         $bFirst = true;
         $sContent = $sComment = "";
         foreach($aItems as $aItem) {
@@ -28,13 +30,19 @@ class BxDevDb extends BxDolModuleDb {
             $aValues = array_values($aItem);
             $iValues = count($aValues);
 
+            bx_import('BxDevFunctions');
+            foreach ($aValues as $iKey => $sValue)
+            	$aValues[$iKey] = BxDevFunctions::dbAddSlashes($sValue, true);
+
             if($bFirst) {
                 $sContent .= "INSERT INTO `" . $sTable . "`(`" . implode("`, `", $aKeys) . "`) VALUES \n";
                 $bFirst = false;
             }
 
             $sSql = "(" . implode(", ", array_fill(0, $iValues, "?")) . "),\n";
-            $sContent .= call_user_func_array(array($this, 'prepare'), array_merge(array($sSql), $aValues));
+            $sSql = call_user_func_array(array($this, 'prepare'), array_merge(array($sSql), $aValues));
+
+            $sContent .= $sSql;
         }
         $sContent = substr($sContent, 0, -2) . ";\n\n";
 

+ 16 - 0
modules/boonex/developer/classes/BxDevFunctions.php

@@ -67,6 +67,22 @@ class BxDevFunctions {
 
         return var_export($aValue, true);        
     }
+
+    /**
+     * Add slashes before "'" and "\" characters that the value containing them can be used in export feature.
+     *
+     * @param string $sString - the input string to add slashes
+     * @param boolean $bDoubleEscape - whether the string needs to be two more escaped or not
+     * @return string the slashed string
+     */
+    public static function dbAddSlashes($sString = '', $bDoubleEscape = false) {
+        if ($bDoubleEscape)
+            $sString = str_replace('\\', '\\\\\\\\', $sString);
+        else
+            $sString = str_replace('\\', '\\\\', $sString);
+
+        return str_replace('\'', '\\\'', $sString);
+    }
 }
 
 /** @} */