From ff7116bf7eed70f8848cdc1ba1702c5cf1cc8125 Mon Sep 17 00:00:00 2001 From: Piotr Przybylski Date: Thu, 14 Jul 2011 21:28:41 +0200 Subject: [PATCH] Fix CodeGen export --- libraries/export/codegen.php | 76 ++++++++++++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 27 deletions(-) diff --git a/libraries/export/codegen.php b/libraries/export/codegen.php index fb81cf9502..859c7a60dc 100644 --- a/libraries/export/codegen.php +++ b/libraries/export/codegen.php @@ -123,12 +123,12 @@ function PMA_exportDBCreate($db) */ function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) { - global $CG_FORMATS, $CG_HANDLERS; - $format = cgGetOption("format"); - $index = array_search($format, $CG_FORMATS); - if ($index >= 0) - return PMA_exportOutputHandler($CG_HANDLERS[$index]($db, $table, $crlf)); - return PMA_exportOutputHandler(sprintf("%s is not supported.", $format)); + global $CG_FORMATS, $CG_HANDLERS; + $format = cgGetOption("format"); + if (isset($CG_FORMATS[$format])) { + return PMA_exportOutputHandler($CG_HANDLERS[$format]($db, $table, $crlf)); + } + return PMA_exportOutputHandler(sprintf("%s is not supported.", $format)); } /** @@ -195,28 +195,50 @@ class TableProperty function getIndexName() { if (strlen($this->key)>0) - return "index=\"" . $this->name . "\""; + return "index=\"" . htmlspecialchars($this->name, ENT_COMPAT, 'UTF-8') . "\""; return ""; } function isPK() { return $this->key=="PRI"; } - function format($pattern) + function formatCs($text) + { + $text=str_replace("#name#", cgMakeIdentifier($this->name, false), $text); + return $this->format($text); + } + function formatXml($text) + { + $text=str_replace("#name#", htmlspecialchars($this->name, ENT_COMPAT, 'UTF-8'), $text); + $text=str_replace("#indexName#", $this->getIndexName(), $text); + return $this->format($text); + } + function format($text) { - $text=$pattern; - $text=str_replace("#name#", $this->name, $text); + $text=str_replace("#ucfirstName#", cgMakeIdentifier($this->name), $text); + $text=str_replace("#dotNetPrimitiveType#", $this->getDotNetPrimitiveType(), $text); + $text=str_replace("#dotNetObjectType#", $this->getDotNetObjectType(), $text); $text=str_replace("#type#", $this->getPureType(), $text); $text=str_replace("#notNull#", $this->isNotNull(), $text); $text=str_replace("#unique#", $this->isUnique(), $text); - $text=str_replace("#ucfirstName#", ucfirst($this->name), $text); - $text=str_replace("#dotNetPrimitiveType#", $this->getDotNetPrimitiveType(), $text); - $text=str_replace("#dotNetObjectType#", $this->getDotNetObjectType(), $text); - $text=str_replace("#indexName#", $this->getIndexName(), $text); return $text; } } + function cgMakeIdentifier($str, $ucfirst = true) + { + // remove unsafe characters + $str = preg_replace('/[^\p{L}\p{Nl}_]/u', '', $str); + // make sure first character is a letter or _ + if (!preg_match('/^\pL/u', $str)) { + $str = '_' . $str; + } + if ($ucfirst) { + $str = ucfirst($str); + } + return $str; + } + function handleNHibernateCSBody($db, $table, $crlf) { $lines=array(); @@ -230,31 +252,31 @@ class TableProperty $lines[] = "using System.Collections;"; $lines[] = "using System.Collections.Generic;"; $lines[] = "using System.Text;"; - $lines[] = "namespace ".ucfirst($db); + $lines[] = "namespace ".cgMakeIdentifier($db); $lines[] = "{"; - $lines[] = " #region ".ucfirst($table); - $lines[] = " public class ".ucfirst($table); + $lines[] = " #region ".cgMakeIdentifier($table); + $lines[] = " public class ".cgMakeIdentifier($table); $lines[] = " {"; $lines[] = " #region Member Variables"; foreach ($tableProperties as $tablePropertie) - $lines[] = $tablePropertie->format(" protected #dotNetPrimitiveType# _#name#;"); + $lines[] = $tablePropertie->formatCs(" protected #dotNetPrimitiveType# _#name#;"); $lines[] = " #endregion"; $lines[] = " #region Constructors"; - $lines[] = " public ".ucfirst($table)."() { }"; + $lines[] = " public ".cgMakeIdentifier($table)."() { }"; $temp = array(); foreach ($tableProperties as $tablePropertie) if (! $tablePropertie->isPK()) - $temp[] = $tablePropertie->format("#dotNetPrimitiveType# #name#"); - $lines[] = " public ".ucfirst($table)."(".implode(", ", $temp).")"; + $temp[] = $tablePropertie->formatCs("#dotNetPrimitiveType# #name#"); + $lines[] = " public ".cgMakeIdentifier($table)."(".implode(", ", $temp).")"; $lines[] = " {"; foreach ($tableProperties as $tablePropertie) if (! $tablePropertie->isPK()) - $lines[] = $tablePropertie->format(" this._#name#=#name#;"); + $lines[] = $tablePropertie->formatCs(" this._#name#=#name#;"); $lines[] = " }"; $lines[] = " #endregion"; $lines[] = " #region Public Properties"; foreach ($tableProperties as $tablePropertie) - $lines[] = $tablePropertie->format(" public virtual #dotNetPrimitiveType# _#ucfirstName#\n {\n get {return _#name#;}\n set {_#name#=value;}\n }"); + $lines[] = $tablePropertie->formatCs(" public virtual #dotNetPrimitiveType# #ucfirstName#\n {\n get {return _#name#;}\n set {_#name#=value;}\n }"); $lines[] = " #endregion"; $lines[] = " }"; $lines[] = " #endregion"; @@ -268,8 +290,8 @@ class TableProperty { $lines=array(); $lines[] = ""; - $lines[] = ""; - $lines[] = " "; + $lines[] = ""; + $lines[] = " "; $result = PMA_DBI_query(sprintf("DESC %s.%s", PMA_backquote($db), PMA_backquote($table))); if ($result) { @@ -279,9 +301,9 @@ class TableProperty foreach ($tableProperties as $tablePropertie) { if ($tablePropertie->isPK()) - $lines[] = $tablePropertie->format(" \n \n \n "); + $lines[] = $tablePropertie->formatXml(" \n \n \n "); else - $lines[] = $tablePropertie->format(" \n \n "); + $lines[] = $tablePropertie->formatXml(" \n \n "); } PMA_DBI_free_result($result); } -- 2.11.4.GIT