From 2cc22c8aba33ad12b3d98905d6dfc29f7c878837 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 8e36f408f6..7160122e97 100644 --- a/libraries/export/codegen.php +++ b/libraries/export/codegen.php @@ -138,12 +138,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)); } /** @@ -209,28 +209,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(); @@ -244,31 +266,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"; @@ -282,8 +304,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) { @@ -293,9 +315,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