issue 2662 coding convention in xml class

git-svn-id: http://xe-core.googlecode.com/svn/branches/maserati@12225 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
ovclas 2012-11-15 01:59:10 +00:00
parent fb0df50f3f
commit 2cb9487ba1
10 changed files with 1924 additions and 1660 deletions

View file

@ -1,193 +1,214 @@
<?php
/**
* DBParser class
* @author NHN (developers@xpressengine.com)
* @package /classes/xml/xmlquery
* @version 0.1
*/
class DBParser
{
/**
* DBParser class
* @author NHN (developers@xpressengine.com)
* @package /classes/xml/xmlquery
* @version 0.1
* Character for escape target value on the left
* @var string
*/
class DBParser {
/**
* Character for escape target value on the left
* @var string
*/
var $escape_char_left;
/**
* Character for escape target value on the right
* @var string
*/
var $escape_char_right;
/**
* Table prefix string
* @var string
*/
var $table_prefix;
/**
* constructor
* @param string $escape_char_left
* @param string $escape_char_right
* @param string $table_prefix
* @return void
*/
function DBParser($escape_char_left, $escape_char_right = "", $table_prefix = "xe_"){
$this->escape_char_left = $escape_char_left;
if ($escape_char_right !== "")$this->escape_char_right = $escape_char_right;
else $this->escape_char_right = $escape_char_left;
$this->table_prefix = $table_prefix;
}
/**
* Get escape character
* @param string $leftOrRight left or right
* @return string
*/
function getEscapeChar($leftOrRight){
if ($leftOrRight === 'left')return $this->escape_char_left;
else return $this->escape_char_right;
}
/**
* escape the value
* @param mixed $name
* @return string
*/
function escape($name){
return $this->escape_char_left . $name . $this->escape_char_right;
}
/**
* escape the string value
* @param string $name
* @return string
*/
function escapeString($name){
return "'".$this->escapeStringValue($name)."'";
}
/**
* escape the string value
* @param string $value
* @return string
*/
function escapeStringValue($value){
if($value == "*") return $value;
if (is_string($value)) return $value = str_replace("'","''",$value);
return $value;
}
/**
* Return table full name
* @param string $name table name without table prefix
* @return string table full name with table prefix
*/
function parseTableName($name){
return $this->table_prefix . $name;
}
/**
* Return colmun name after escape
* @param string $name column name before escape
* @return string column name after escape
*/
function parseColumnName($name){
return $this->escapeColumn($name);
}
/**
* Escape column
* @param string $column_name
* @return string column name with db name
*/
function escapeColumn($column_name){
if($this->isUnqualifiedColumnName($column_name))
return $this->escape($column_name);
if($this->isQualifiedColumnName($column_name)){
list($table, $column) = explode('.', $column_name);
// $table can also be an alias, so the prefix should not be added
return $this->escape($table).'.'.$this->escape($column);
//return $this->escape($this->parseTableName($table)).'.'.$this->escape($column);
}
}
var $escape_char_left;
/**
* Character for escape target value on the right
* @var string
*/
var $escape_char_right;
/**
* Table prefix string
* @var string
*/
var $table_prefix;
/**
* Column name is suitable for use in checking
* @param string $column_name
* @return bool
*/
function isUnqualifiedColumnName($column_name){
if(strpos($column_name,'.')===false && strpos($column_name,'(')===false) return true;
return false;
}
/**
* Column name is suitable for use in checking
* @param string $column_name
* @return bool
*/
function isQualifiedColumnName($column_name){
if(strpos($column_name,'.')!==false && strpos($column_name,'(')===false) return true;
return false;
}
/**
* constructor
* @param string $escape_char_left
* @param string $escape_char_right
* @param string $table_prefix
* @return void
*/
function DBParser($escape_char_left, $escape_char_right = "", $table_prefix = "xe_")
{
$this->escape_char_left = $escape_char_left;
if ($escape_char_right !== "")$this->escape_char_right = $escape_char_right;
else $this->escape_char_right = $escape_char_left;
$this->table_prefix = $table_prefix;
}
function parseExpression($column_name){
$functions = preg_split('/([\+\-\*\/\ ])/', $column_name, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
foreach($functions as $k => $v){
$function = &$functions[$k];
if(strlen($function)==1) continue; // skip delimiters
$pos = strrpos("(", $function);
$matches = preg_split('/([a-zA-Z0-9_*]+)/', $function, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
$total_brackets = substr_count($function, "(");
$brackets = 0;
foreach($matches as $i => $j){
$match = &$matches[$i];
if($match == '(') {$brackets++; continue;}
if(strpos($match,')') !== false) continue;
if(in_array($match, array(',', '.'))) continue;
if($brackets == $total_brackets){
if(!is_numeric($match)) {
$match = $this->escapeColumnExpression($match);
}
/**
* Get escape character
* @param string $leftOrRight left or right
* @return string
*/
function getEscapeChar($leftOrRight)
{
if ($leftOrRight === 'left')return $this->escape_char_left;
else return $this->escape_char_right;
}
/**
* escape the value
* @param mixed $name
* @return string
*/
function escape($name)
{
return $this->escape_char_left . $name . $this->escape_char_right;
}
/**
* escape the string value
* @param string $name
* @return string
*/
function escapeString($name)
{
return "'".$this->escapeStringValue($name)."'";
}
/**
* escape the string value
* @param string $value
* @return string
*/
function escapeStringValue($value)
{
if($value == "*") return $value;
if (is_string($value)) return $value = str_replace("'","''",$value);
return $value;
}
/**
* Return table full name
* @param string $name table name without table prefix
* @return string table full name with table prefix
*/
function parseTableName($name)
{
return $this->table_prefix . $name;
}
/**
* Return colmun name after escape
* @param string $name column name before escape
* @return string column name after escape
*/
function parseColumnName($name)
{
return $this->escapeColumn($name);
}
/**
* Escape column
* @param string $column_name
* @return string column name with db name
*/
function escapeColumn($column_name)
{
if($this->isUnqualifiedColumnName($column_name))
return $this->escape($column_name);
if($this->isQualifiedColumnName($column_name))
{
list($table, $column) = explode('.', $column_name);
// $table can also be an alias, so the prefix should not be added
return $this->escape($table).'.'.$this->escape($column);
//return $this->escape($this->parseTableName($table)).'.'.$this->escape($column);
}
}
/**
* Column name is suitable for use in checking
* @param string $column_name
* @return bool
*/
function isUnqualifiedColumnName($column_name)
{
if(strpos($column_name,'.')===false && strpos($column_name,'(')===false) return true;
return false;
}
/**
* Column name is suitable for use in checking
* @param string $column_name
* @return bool
*/
function isQualifiedColumnName($column_name)
{
if(strpos($column_name,'.')!==false && strpos($column_name,'(')===false) return true;
return false;
}
function parseExpression($column_name)
{
$functions = preg_split('/([\+\-\*\/\ ])/', $column_name, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
foreach($functions as $k => $v)
{
$function = &$functions[$k];
if(strlen($function)==1) continue; // skip delimiters
$pos = strrpos("(", $function);
$matches = preg_split('/([a-zA-Z0-9_*]+)/', $function, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
$total_brackets = substr_count($function, "(");
$brackets = 0;
foreach($matches as $i => $j)
{
$match = &$matches[$i];
if($match == '(') {$brackets++; continue;}
if(strpos($match,')') !== false) continue;
if(in_array($match, array(',', '.'))) continue;
if($brackets == $total_brackets)
{
if(!is_numeric($match))
{
$match = $this->escapeColumnExpression($match);
}
}
$function = implode('', $matches);
}
return implode('', $functions);
}
/*
* Checks argument is asterisk
* @param string $column_name
* @return bool
*/
function isStar($column_name){
if(substr($column_name,-1) == '*') return true;
return false;
}
/*
* Checks to see if expression is an aggregate star function
* like count(*)
* @param string $column_name
* @return bool
*/
function isStarFunction($column_name){
if(strpos($column_name, "(*)")!==false) return true;
return false;
}
/*
* Return column name after escape
* @param string $column_name
* @return string
*/
function escapeColumnExpression($column_name){
if($this->isStar($column_name)) return $column_name;
if($this->isStarFunction($column_name)){
return $column_name;
}
if(strpos(strtolower($column_name), 'distinct') !== false) return $column_name;
return $this->escapeColumn($column_name);
}
$function = implode('', $matches);
}
return implode('', $functions);
}
/*
* Checks argument is asterisk
* @param string $column_name
* @return bool
*/
function isStar($column_name)
{
if(substr($column_name,-1) == '*') return true;
return false;
}
/*
* Checks to see if expression is an aggregate star function
* like count(*)
* @param string $column_name
* @return bool
*/
function isStarFunction($column_name)
{
if(strpos($column_name, "(*)")!==false) return true;
return false;
}
/*
* Return column name after escape
* @param string $column_name
* @return string
*/
function escapeColumnExpression($column_name)
{
if($this->isStar($column_name)) return $column_name;
if($this->isStarFunction($column_name))
{
return $column_name;
}
if(strpos(strtolower($column_name), 'distinct') !== false) return $column_name;
return $this->escapeColumn($column_name);
}
}
/* End of file DBParser.class.php */
/* Location: ./classes/xml/xmlquery/DBParser.class.php */

View file

@ -1,92 +1,104 @@
<?php
/**
* QueryParser class
* @author NHN (developers@xpressengine.com)
* @package /classes/xml/xmlquery
* @version 0.1
*/
class QueryParser
{
/**
* QueryParser class
* @author NHN (developers@xpressengine.com)
* @package /classes/xml/xmlquery
* @version 0.1
* QueryTag object
* @var QueryTag object
*/
class QueryParser {
/**
* QueryTag object
* @var QueryTag object
*/
var $queryTag;
var $queryTag;
/**
* constructor
* @param object $query
* @param bool $isSubQuery
* @return void
*/
function QueryParser($query = NULL, $isSubQuery = false) {
if ($query)
$this->queryTag = new QueryTag($query, $isSubQuery);
}
/**
* constructor
* @param object $query
* @param bool $isSubQuery
* @return void
*/
function QueryParser($query = NULL, $isSubQuery = false)
{
if($query)
$this->queryTag = new QueryTag($query, $isSubQuery);
}
/**
* Return table information
* @param object $query_id
* @param bool $table_name
* @return array
*/
function getTableInfo($query_id, $table_name) {
$column_type = array();
$module = '';
$id_args = explode('.', $query_id);
if (count($id_args) == 2) {
$target = 'modules';
$module = $id_args[0];
$id = $id_args[1];
} elseif (count($id_args) == 3) {
$target = $id_args[0];
$targetList = array('modules'=>1, 'addons'=>1, 'widgets'=>1);
if (!isset($targetList[$target]))
return;
$module = $id_args[1];
$id = $id_args[2];
}
/**
* Return table information
* @param object $query_id
* @param bool $table_name
* @return array
*/
function getTableInfo($query_id, $table_name)
{
$column_type = array();
$module = '';
// get column properties from the table
$table_file = sprintf('%s%s/%s/schemas/%s.xml', _XE_PATH_, 'modules', $module, $table_name);
if (!file_exists($table_file)) {
$searched_list = FileHandler::readDir(_XE_PATH_ . 'modules');
$searched_count = count($searched_list);
for ($i = 0; $i < $searched_count; $i++) {
$table_file = sprintf('%s%s/%s/schemas/%s.xml', _XE_PATH_, 'modules', $searched_list[$i], $table_name);
if (file_exists($table_file))
break;
}
}
$id_args = explode('.', $query_id);
if(count($id_args) == 2)
{
$target = 'modules';
$module = $id_args[0];
$id = $id_args[1];
}
else if(count($id_args) == 3)
{
$target = $id_args[0];
$targetList = array('modules'=>1, 'addons'=>1, 'widgets'=>1);
if (!isset($targetList[$target]))
return;
$module = $id_args[1];
$id = $id_args[2];
}
if (file_exists($table_file)) {
$table_xml = FileHandler::readFile($table_file);
$xml_parser = new XmlParser();
$table_obj = $xml_parser->parse($table_xml);
if ($table_obj->table) {
if (isset($table_obj->table->column) && !is_array($table_obj->table->column)) {
$table_obj->table->column = array($table_obj->table->column);
}
// get column properties from the table
$table_file = sprintf('%s%s/%s/schemas/%s.xml', _XE_PATH_, 'modules', $module, $table_name);
if(!file_exists($table_file))
{
$searched_list = FileHandler::readDir(_XE_PATH_ . 'modules');
$searched_count = count($searched_list);
for($i = 0; $i < $searched_count; $i++)
{
$table_file = sprintf('%s%s/%s/schemas/%s.xml', _XE_PATH_, 'modules', $searched_list[$i], $table_name);
if(file_exists($table_file))
break;
}
}
foreach ($table_obj->table->column as $k => $v) {
$column_type[$v->attrs->name] = $v->attrs->type;
}
}
}
if(file_exists($table_file))
{
$table_xml = FileHandler::readFile($table_file);
$xml_parser = new XmlParser();
$table_obj = $xml_parser->parse($table_xml);
if($table_obj->table)
{
if(isset($table_obj->table->column) && !is_array($table_obj->table->column))
{
$table_obj->table->column = array($table_obj->table->column);
}
return $column_type;
}
foreach($table_obj->table->column as $k => $v)
{
$column_type[$v->attrs->name] = $v->attrs->type;
}
}
}
/**
* Change code string from queryTag object
* @return string
*/
function toString() {
return "<?php if(!defined('__ZBXE__')) exit();\n"
. $this->queryTag->toString()
. 'return $query; ?>';
}
return $column_type;
}
}
?>
/**
* Change code string from queryTag object
* @return string
*/
function toString()
{
return "<?php if(!defined('__ZBXE__')) exit();\n"
. $this->queryTag->toString()
. 'return $query; ?>';
}
}
/* End of file QueryParser.class.php */
/* Location: ./classes/xml/xmlquery/QueryParser.class.php */

View file

@ -5,7 +5,8 @@
* @package /classes/xml/xmlquery/argument
* @version 0.1
*/
class Argument {
class Argument
{
/**
* argument value
* @var mixed
@ -52,51 +53,62 @@ class Argument {
* @param mixed $value
* @return void
*/
function Argument($name, $value) {
function Argument($name, $value)
{
$this->value = $value;
$this->name = $name;
$this->isValid = true;
}
function getType() {
if (isset($this->type))
function getType()
{
if(isset($this->type))
{
return $this->type;
}
if (is_string($this->value))
if(is_string($this->value))
return 'column_name';
return 'number';
}
function setColumnType($value) {
function setColumnType($value)
{
$this->type = $value;
}
function setColumnOperation($operation) {
function setColumnOperation($operation)
{
$this->column_operation = $operation;
}
function getName() {
function getName()
{
return $this->name;
}
function getValue() {
if (!isset($this->_value)) {
function getValue()
{
if(!isset($this->_value))
{
$value = $this->getEscapedValue();
$this->_value = $this->toString($value);
}
return $this->_value;
}
function getColumnOperation() {
function getColumnOperation()
{
return $this->column_operation;
}
function getEscapedValue() {
function getEscapedValue()
{
return $this->escapeValue($this->value);
}
function getUnescapedValue() {
function getUnescapedValue()
{
return $this->value;
}
@ -105,11 +117,13 @@ class Argument {
* @param mixed $value
* @return string
*/
function toString($value) {
if (is_array($value)) {
if (count($value) === 0)
function toString($value)
{
if(is_array($value))
{
if(count($value) === 0)
return '';
if (count($value) === 1 && $value[0] === '')
if(count($value) === 1 && $value[0] === '')
return '';
return '(' . implode(',', $value) . ')';
}
@ -121,35 +135,45 @@ class Argument {
* @param mixed $value
* @return mixed
*/
function escapeValue($value) {
function escapeValue($value)
{
$column_type = $this->getType();
if ($column_type == 'column_name') {
if($column_type == 'column_name')
{
$dbParser = DB::getParser();
return $dbParser->parseExpression($value);
}
if (!isset($value))
if(!isset($value))
return null;
$columnTypeList = array('date'=>1, 'varchar'=>1, 'char'=>1, 'text'=>1, 'bigtext'=>1);
if (isset($columnTypeList[$column_type])) {
if (!is_array($value))
if(isset($columnTypeList[$column_type]))
{
if(!is_array($value))
$value = $this->_escapeStringValue($value);
else {
else
{
$total = count($value);
for ($i = 0; $i < $total; $i++)
for($i = 0; $i < $total; $i++)
$value[$i] = $this->_escapeStringValue($value[$i]);
//$value[$i] = '\''.$value[$i].'\'';
}
}
if($this->uses_default_value) return $value;
if ($column_type == 'number') {
if (is_array($value)) {
foreach ($value AS $key => $val) {
if (isset($val) && $val !== '') {
if($column_type == 'number')
{
if(is_array($value))
{
foreach ($value AS $key => $val)
{
if(isset($val) && $val !== '')
{
$value[$key] = (int) $val;
}
}
} else {
}
else
{
$value = (int) $value;
}
}
@ -162,7 +186,8 @@ class Argument {
* @param string $value
* @return string
*/
function _escapeStringValue($value) {
function _escapeStringValue($value)
{
// Remove non-utf8 chars.
$regex = '@((?:[\x00-\x7F]|[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}){1,100})|([\xF0-\xF7][\x80-\xBF]{3})|([\x80-\xBF])|([\xC0-\xFF])@x';
@ -172,13 +197,14 @@ class Argument {
return '\'' . $value . '\'';
}
function utf8Replacer($captures) {
if (!empty($captures[1]))
function utf8Replacer($captures)
{
if(!empty($captures[1]))
{
// Valid byte sequence. Return unmodified.
return $captures[1];
}
elseif(!empty($captures[2]))
else if(!empty($captures[2]))
{
// Remove user defined area
if("\xF3\xB0\x80\x80" <= $captures[2])
@ -194,23 +220,27 @@ class Argument {
}
}
function isValid() {
function isValid()
{
return $this->isValid;
}
function isColumnName(){
function isColumnName()
{
$type = $this->getType();
if($type == 'column_name') return true;
if($type == 'number' && !is_numeric($this->value) && $this->uses_default_value) return true;
return false;
}
function getErrorMessage() {
function getErrorMessage()
{
return $this->errorMessage;
}
function ensureDefaultValue($default_value) {
if (!isset($this->value) || $this->value == '')
function ensureDefaultValue($default_value)
{
if(!isset($this->value) || $this->value == '')
{
$this->value = $default_value;
$this->uses_default_value = true;
@ -222,49 +252,58 @@ class Argument {
* @param string $filter_type
* @return void
*/
function checkFilter($filter_type) {
if (isset($this->value) && $this->value != '') {
function checkFilter($filter_type)
{
if(isset($this->value) && $this->value != '')
{
global $lang;
$val = $this->value;
$key = $this->name;
switch ($filter_type) {
switch ($filter_type)
{
case 'email' :
case 'email_address' :
if (!preg_match('/^[\w-]+((?:\.|\+|\~)[\w-]+)*@[\w-]+(\.[\w-]+)+$/is', $val)) {
if(!preg_match('/^[\w-]+((?:\.|\+|\~)[\w-]+)*@[\w-]+(\.[\w-]+)+$/is', $val))
{
$this->isValid = false;
$this->errorMessage = new Object(-1, sprintf($lang->filter->invalid_email, $lang->{$key} ? $lang->{$key} : $key));
}
break;
case 'homepage' :
if (!preg_match('/^(http|https)+(:\/\/)+[0-9a-z_-]+\.[^ ]+$/is', $val)) {
if(!preg_match('/^(http|https)+(:\/\/)+[0-9a-z_-]+\.[^ ]+$/is', $val))
{
$this->isValid = false;
$this->errorMessage = new Object(-1, sprintf($lang->filter->invalid_homepage, $lang->{$key} ? $lang->{$key} : $key));
}
break;
case 'userid' :
case 'user_id' :
if (!preg_match('/^[a-zA-Z]+([_0-9a-zA-Z]+)*$/is', $val)) {
if(!preg_match('/^[a-zA-Z]+([_0-9a-zA-Z]+)*$/is', $val))
{
$this->isValid = false;
$this->errorMessage = new Object(-1, sprintf($lang->filter->invalid_userid, $lang->{$key} ? $lang->{$key} : $key));
}
break;
case 'number' :
case 'numbers' :
if (is_array($val))
if(is_array($val))
$val = join(',', $val);
if (!preg_match('/^(-?)[0-9]+(,\-?[0-9]+)*$/is', $val)) {
if(!preg_match('/^(-?)[0-9]+(,\-?[0-9]+)*$/is', $val))
{
$this->isValid = false;
$this->errorMessage = new Object(-1, sprintf($lang->filter->invalid_number, $lang->{$key} ? $lang->{$key} : $key));
}
break;
case 'alpha' :
if (!preg_match('/^[a-z]+$/is', $val)) {
if(!preg_match('/^[a-z]+$/is', $val))
{
$this->isValid = false;
$this->errorMessage = new Object(-1, sprintf($lang->filter->invalid_alpha, $lang->{$key} ? $lang->{$key} : $key));
}
break;
case 'alpha_number' :
if (!preg_match('/^[0-9a-z]+$/is', $val)) {
if(!preg_match('/^[0-9a-z]+$/is', $val))
{
$this->isValid = false;
$this->errorMessage = new Object(-1, sprintf($lang->filter->invalid_alpha_number, $lang->{$key} ? $lang->{$key} : $key));
}
@ -273,8 +312,10 @@ class Argument {
}
}
function checkMaxLength($length) {
if ($this->value && (strlen($this->value) > $length)) {
function checkMaxLength($length)
{
if($this->value && (strlen($this->value) > $length))
{
global $lang;
$this->isValid = false;
$key = $this->name;
@ -282,8 +323,10 @@ class Argument {
}
}
function checkMinLength($length) {
if ($this->value && (strlen($this->value) < $length)) {
function checkMinLength($length)
{
if($this->value && (strlen($this->value) < $length))
{
global $lang;
$this->isValid = false;
$key = $this->name;
@ -291,15 +334,16 @@ class Argument {
}
}
function checkNotNull() {
if (!isset($this->value)) {
function checkNotNull()
{
if(!isset($this->value))
{
global $lang;
$this->isValid = false;
$key = $this->name;
$this->errorMessage = new Object(-1, sprintf($lang->filter->isnull, $lang->{$key} ? $lang->{$key} : $key));
}
}
}
?>
/* End of file Argument.class.php */
/* Location: ./classes/xml/xmlquery/Argument.class.php */