issue 2119. supporting php 5.4. editor, extravar and file classes.

git-svn-id: http://xe-core.googlecode.com/svn/branches/maserati@12688 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
flyskyko 2013-02-05 01:33:47 +00:00
parent 1de3e9cd17
commit b69abcca68
5 changed files with 700 additions and 356 deletions

View file

@ -1,4 +1,5 @@
<?php
/**
* Superclass of the edit component.
* Set up the component variables
@ -8,16 +9,20 @@
*/
class EditorHandler extends Object
{
/**
* set the xml and other information of the component
* @param object $info editor information
* @return void
**/
* */
function setInfo($info)
{
Context::set('component_info', $info);
if(!$info->extra_vars) return;
if(!$info->extra_vars)
{
return;
}
foreach($info->extra_vars as $key => $val)
{

View file

@ -1,4 +1,5 @@
<?php
/**
* A class to handle extra variables used in posts, member and others
*
@ -6,6 +7,7 @@
*/
class ExtraVar
{
/**
* sequence of module
* @var int
@ -48,7 +50,11 @@ class ExtraVar
*/
function setExtraVarKeys($extra_keys)
{
if(!is_array($extra_keys) || !count($extra_keys)) return;
if(!is_array($extra_keys) || !count($extra_keys))
{
return;
}
foreach($extra_keys as $key => $val)
{
$obj = null;
@ -66,6 +72,7 @@ class ExtraVar
{
return $this->keys;
}
}
/**
@ -75,6 +82,7 @@ class ExtraVar
*/
class ExtraItem
{
/**
* Sequence of module
* @var int
@ -151,7 +159,11 @@ class ExtraItem
*/
function ExtraItem($module_srl, $idx, $name, $type = 'text', $default = null, $desc = '', $is_required = 'N', $search = 'N', $value = null, $eid = '')
{
if(!$idx) return;
if(!$idx)
{
return;
}
$this->module_srl = $module_srl;
$this->idx = $idx;
$this->name = $name;
@ -185,47 +197,92 @@ class ExtraItem
function _getTypeValue($type, $value)
{
$value = trim($value);
if(!isset($value)) return;
if(!isset($value))
{
return;
}
switch($type)
{
case 'homepage' :
if($value && !preg_match('/^([a-z]+):\/\//i',$value)) $value = 'http://'.$value;
if($value && !preg_match('/^([a-z]+):\/\//i', $value))
{
$value = 'http://' . $value;
}
return htmlspecialchars($value);
break;
case 'tel' :
if(is_array($value)) $values = $value;
elseif(strpos($value,'|@|')!==false) $values = explode('|@|', $value);
elseif(strpos($value,',')!==false) $values = explode(',', $value);
if(is_array($value))
{
$values = $value;
}
elseif(strpos($value, '|@|') !== FALSE)
{
$values = explode('|@|', $value);
}
elseif(strpos($value, ',') !== FALSE)
{
$values = explode(',', $value);
}
$values[0] = $values[0];
$values[1] = $values[1];
$values[2] = $values[2];
return $values;
break;
break;
case 'checkbox' :
case 'radio' :
case 'select' :
if(is_array($value)) $values = $value;
elseif(strpos($value,'|@|')!==false) $values = explode('|@|', $value);
elseif(strpos($value,',')!==false) $values = explode(',', $value);
else $values = array($value);
for($i=0;$i<count($values);$i++) $values[$i] = htmlspecialchars($values[$i]);
if(is_array($value))
{
$values = $value;
}
elseif(strpos($value, '|@|') !== FALSE)
{
$values = explode('|@|', $value);
}
elseif(strpos($value, ',') !== FALSE)
{
$values = explode(',', $value);
}
else
{
$values = array($value);
}
for($i = 0; $i < count($values); $i++)
{
$values[$i] = htmlspecialchars($values[$i]);
}
return $values;
break;
case 'kr_zip' :
if(is_array($value)) $values = $value;
elseif(strpos($value,'|@|')!==false) $values = explode('|@|', $value);
elseif(strpos($value,',')!==false) $values = explode(',', $value);
else $values = array($value);
if(is_array($value))
{
$values = $value;
}
elseif(strpos($value, '|@|') !== false)
{
$values = explode('|@|', $value);
}
elseif(strpos($value, ',') !== false)
{
$values = explode(',', $value);
}
else
{
$values = array($value);
}
return $values;
break;
//case 'date' :
//case 'email_address' :
//case 'text' :
//case 'textarea' :
default :
return htmlspecialchars($value);
break;
}
}
@ -237,35 +294,55 @@ class ExtraItem
function getValueHTML()
{
$value = $this->_getTypeValue($this->type, $this->value);
switch($this->type)
{
case 'homepage' :
return ($value)?(sprintf('<a href="%s" target="_blank">%s</a>', $value, strlen($value)>60?substr($value,0,40).'...'.substr($value,-10):$value)):"";
return ($value) ? (sprintf('<a href="%s" target="_blank">%s</a>', $value, strlen($value) > 60 ? substr($value, 0, 40) . '...' . substr($value, -10) : $value)) : "";
case 'email_address' :
return ($value)?sprintf('<a href="mailto:%s">%s</a>', $value, $value):"";
break;
return ($value) ? sprintf('<a href="mailto:%s">%s</a>', $value, $value) : "";
case 'tel' :
return sprintf('%s - %s - %s', $value[0],$value[1],$value[2]);
break;
return sprintf('%s - %s - %s', $value[0], $value[1], $value[2]);
case 'textarea' :
return nl2br($value);
break;
case 'checkbox' :
if(is_array($value)) return implode(', ',$value);
else return $value;
break;
if(is_array($value))
{
return implode(', ', $value);
}
else
{
return $value;
}
case 'date' :
return zdate($value,"Y-m-d");
break;
return zdate($value, "Y-m-d");
case 'select' :
case 'radio' :
if(is_array($value)) return implode(', ',$value);
else return $value;
break;
if(is_array($value))
{
return implode(', ', $value);
}
else
{
return $value;
}
case 'kr_zip' :
if(is_array($value)) return implode(' ',$value);
else return $value;
break;
if(is_array($value))
{
return implode(' ', $value);
}
else
{
return $value;
}
// case 'text' :
default :
return $value;
@ -285,54 +362,66 @@ class ExtraItem
$name = $this->name;
$value = $this->_getTypeValue($this->type, $this->value);
$default = $this->_getTypeValue($this->type, $this->default);
$column_name = 'extra_vars'.$this->idx;
$tmp_id = $column_name.'-'.$id_num++;
$column_name = 'extra_vars' . $this->idx;
$tmp_id = $column_name . '-' . $id_num++;
$buff = '';
switch($type)
{
// Homepage
case 'homepage' :
$buff .= '<input type="text" name="'.$column_name.'" value="'.$value.'" class="homepage" />';
$buff .= '<input type="text" name="' . $column_name . '" value="' . $value . '" class="homepage" />';
break;
// Email Address
case 'email_address' :
$buff .= '<input type="text" name="'.$column_name.'" value="'.$value.'" class="email_address" />';
$buff .= '<input type="text" name="' . $column_name . '" value="' . $value . '" class="email_address" />';
break;
// Phone Number
case 'tel' :
$buff .=
'<input type="text" name="'.$column_name.'[]" value="'.$value[0].'" size="4" maxlength="4" class="tel" />'.
'<input type="text" name="'.$column_name.'[]" value="'.$value[1].'" size="4" maxlength="4" class="tel" />'.
'<input type="text" name="'.$column_name.'[]" value="'.$value[2].'" size="4" maxlength="4" class="tel" />';
'<input type="text" name="' . $column_name . '[]" value="' . $value[0] . '" size="4" maxlength="4" class="tel" />' .
'<input type="text" name="' . $column_name . '[]" value="' . $value[1] . '" size="4" maxlength="4" class="tel" />' .
'<input type="text" name="' . $column_name . '[]" value="' . $value[2] . '" size="4" maxlength="4" class="tel" />';
break;
// textarea
case 'textarea' :
$buff .= '<textarea name="'.$column_name.'" rows="8" cols="42">'.$value.'</textarea>';
$buff .= '<textarea name="' . $column_name . '" rows="8" cols="42">' . $value . '</textarea>';
break;
// multiple choice
case 'checkbox' :
$buff .= '<ul>';
foreach($default as $v)
{
if($value && in_array(trim($v), $value)) $checked = ' checked="checked"';
else $checked = '';
if($value && in_array(trim($v), $value))
{
$checked = ' checked="checked"';
}
else
{
$checked = '';
}
// Temporary ID for labeling
$tmp_id = $column_name.'-'.$id_num++;
$tmp_id = $column_name . '-' . $id_num++;
$buff .='<li><input type="checkbox" name="'.$column_name.'[]" id="'.$tmp_id.'" value="'.htmlspecialchars($v).'" '.$checked.' /><label for="'.$tmp_id.'">'.$v.'</label></li>';
$buff .='<li><input type="checkbox" name="' . $column_name . '[]" id="' . $tmp_id . '" value="' . htmlspecialchars($v) . '" ' . $checked . ' /><label for="' . $tmp_id . '">' . $v . '</label></li>';
}
$buff .= '</ul>';
break;
// single choice
case 'select' :
$buff .= '<select name="'.$column_name.'" class="select">';
$buff .= '<select name="' . $column_name . '" class="select">';
foreach($default as $v)
{
if($value && in_array(trim($v),$value)) $selected = ' selected="selected"';
else $selected = '';
$buff .= '<option value="'.$v.'" '.$selected.'>'.$v.'</option>';
if($value && in_array(trim($v), $value))
{
$selected = ' selected="selected"';
}
else
{
$selected = '';
}
$buff .= '<option value="' . $v . '" ' . $selected . '>' . $v . '</option>';
}
$buff .= '</select>';
break;
@ -341,13 +430,19 @@ class ExtraItem
$buff .= '<ul>';
foreach($default as $v)
{
if($value && in_array(trim($v),$value)) $checked = ' checked="checked"';
else $checked = '';
if($value && in_array(trim($v), $value))
{
$checked = ' checked="checked"';
}
else
{
$checked = '';
}
// Temporary ID for labeling
$tmp_id = $column_name.'-'.$id_num++;
$tmp_id = $column_name . '-' . $id_num++;
$buff .= '<li><input type="radio" name="'.$column_name.'" id="'.$tmp_id.'" '.$checked.' value="'.$v.'" class="radio" /><label for="'.$tmp_id.'">'.$v.'</label></li>';
$buff .= '<li><input type="radio" name="' . $column_name . '" id="' . $tmp_id . '" ' . $checked . ' value="' . $v . '" class="radio" /><label for="' . $tmp_id . '">' . $v . '</label></li>';
}
$buff .= '</ul>';
break;
@ -357,22 +452,22 @@ class ExtraItem
Context::loadJavascriptPlugin('ui.datepicker');
$buff .=
'<input type="hidden" name="'.$column_name.'" value="'.$value.'" />'.
'<input type="text" id="date_'.$column_name.'" value="'.zdate($value,'Y-m-d').'" class="date" /> <input type="button" value="' . Context::getLang('cmd_delete') . '" id="dateRemover_' . $column_name . '" />'."\n".
'<script>'."\n".
'(function($){'."\n".
' $(function(){'."\n".
' var option = { dateFormat: "yy-mm-dd", changeMonth:true, changeYear:true, gotoCurrent: false,yearRange:\'-100:+10\', onSelect:function(){'."\n".
' $(this).prev(\'input[type="hidden"]\').val(this.value.replace(/-/g,""))}'."\n".
' };'."\n".
' $.extend(option,$.datepicker.regional[\''.Context::getLangType().'\']);'."\n".
' $("#date_'.$column_name.'").datepicker(option);'."\n".
'<input type="hidden" name="' . $column_name . '" value="' . $value . '" />' .
'<input type="text" id="date_' . $column_name . '" value="' . zdate($value, 'Y-m-d') . '" class="date" /> <input type="button" value="' . Context::getLang('cmd_delete') . '" id="dateRemover_' . $column_name . '" />' . "\n" .
'<script>' . "\n" .
'(function($){' . "\n" .
' $(function(){' . "\n" .
' var option = { dateFormat: "yy-mm-dd", changeMonth:true, changeYear:true, gotoCurrent: false,yearRange:\'-100:+10\', onSelect:function(){' . "\n" .
' $(this).prev(\'input[type="hidden"]\').val(this.value.replace(/-/g,""))}' . "\n" .
' };' . "\n" .
' $.extend(option,$.datepicker.regional[\'' . Context::getLangType() . '\']);' . "\n" .
' $("#date_' . $column_name . '").datepicker(option);' . "\n" .
' $("#dateRemover_' . $column_name . '").click(function(){' . "\n" .
' $(this).siblings("input").val("");' . "\n" .
' return false;' . "\n" .
' })' . "\n" .
' });'."\n".
'})(jQuery);'."\n".
' });' . "\n" .
'})(jQuery);' . "\n" .
'</script>';
break;
// address
@ -381,33 +476,35 @@ class ExtraItem
Context::loadJavascriptPlugin('ui.krzip');
$buff .=
'<div id="addr_searched_'.$column_name.'" style="display:'.($value[0]?'block':'none').';">'.
'<input type="text" readonly="readonly" name="'.$column_name.'[]" value="'.$value[0].'" class="address" />'.
'<a href="#" onclick="doShowKrZipSearch(this, \''.$column_name.'\'); return false;" class="button red"><span>'.Context::getLang('cmd_cancel').'</span></a>'.
'</div>'.
'<div id="addr_list_'.$column_name.'" style="display:none;">'.
'<select name="addr_list_'.$column_name.'"></select>'.
'<a href="#" onclick="doSelectKrZip(this, \''.$column_name.'\'); return false;" class="button blue"><span>'.Context::getLang('cmd_select').'</span></a>'.
'<a href="#" onclick="doHideKrZipList(this, \''.$column_name.'\'); return false;" class="button red"><span>'.Context::getLang('cmd_cancel').'</span></a>'.
'</div>'.
'<div id="addr_search_'.$column_name.'" style="display:'.($value[0]?'none':'block').'">'.
'<input type="text" name="addr_search_'.$column_name.'" class="address" value="" />'.
'<a href="#" onclick="doSearchKrZip(this, \''.$column_name.'\'); return false;" class="button green"><span>'.Context::getLang('cmd_search').'</span></a>'.
'</div>'.
'<input type="text" name="'.$column_name.'[]" value="'.htmlspecialchars($value[1]).'" class="address" />'.
'<div id="addr_searched_' . $column_name . '" style="display:' . ($value[0] ? 'block' : 'none') . ';">' .
'<input type="text" readonly="readonly" name="' . $column_name . '[]" value="' . $value[0] . '" class="address" />' .
'<a href="#" onclick="doShowKrZipSearch(this, \'' . $column_name . '\'); return false;" class="button red"><span>' . Context::getLang('cmd_cancel') . '</span></a>' .
'</div>' .
'<div id="addr_list_' . $column_name . '" style="display:none;">' .
'<select name="addr_list_' . $column_name . '"></select>' .
'<a href="#" onclick="doSelectKrZip(this, \'' . $column_name . '\'); return false;" class="button blue"><span>' . Context::getLang('cmd_select') . '</span></a>' .
'<a href="#" onclick="doHideKrZipList(this, \'' . $column_name . '\'); return false;" class="button red"><span>' . Context::getLang('cmd_cancel') . '</span></a>' .
'</div>' .
'<div id="addr_search_' . $column_name . '" style="display:' . ($value[0] ? 'none' : 'block') . '">' .
'<input type="text" name="addr_search_' . $column_name . '" class="address" value="" />' .
'<a href="#" onclick="doSearchKrZip(this, \'' . $column_name . '\'); return false;" class="button green"><span>' . Context::getLang('cmd_search') . '</span></a>' .
'</div>' .
'<input type="text" name="' . $column_name . '[]" value="' . htmlspecialchars($value[1]) . '" class="address" />' .
'';
break;
// General text
default :
$buff .=' <input type="text" name="'.$column_name.'" value="'.($value ? $value : $default).'" class="text" />';
$buff .=' <input type="text" name="' . $column_name . '" value="' . ($value ? $value : $default) . '" class="text" />';
break;
}
if($this->desc) $buff .= '<p>'.$this->desc.'</p>';
if($this->desc)
{
$buff .= '<p>' . $this->desc . '</p>';
}
return $buff;
}
}
/* End of file ExtraVar.class.php */
/* Location: ./classes/extravar/ExtraVar.class.php */

View file

@ -1,4 +1,5 @@
<?php
/**
* Contains methods for accessing file system
*
@ -6,6 +7,7 @@
*/
class FileHandler
{
/**
* Changes path of target file, directory into absolute path
*
@ -15,7 +17,10 @@ class FileHandler
function getRealPath($source)
{
$temp = explode('/', $source);
if($temp[0] == '.') $source = _XE_PATH_.substr($source, 2);
if($temp[0] == '.')
{
$source = _XE_PATH_ . substr($source, 2);
}
return $source;
}
@ -30,35 +35,60 @@ class FileHandler
* @param string $type If set as 'force'. Even if the file exists in target, the file is copied.
* @return void
*/
function copyDir($source_dir, $target_dir, $filter=null,$type=null)
function copyDir($source_dir, $target_dir, $filter = null, $type = null)
{
$source_dir = FileHandler::getRealPath($source_dir);
$target_dir = FileHandler::getRealPath($target_dir);
if(!is_dir($source_dir)) return false;
// generate when no target exists
if(!file_exists($target_dir)) FileHandler::makeDir($target_dir);
if(!is_dir($source_dir))
{
return false;
}
if(substr($source_dir, -1) != '/') $source_dir .= '/';
if(substr($target_dir, -1) != '/') $target_dir .= '/';
// generate when no target exists
if(!file_exists($target_dir))
{
FileHandler::makeDir($target_dir);
}
if(substr($source_dir, -1) != '/')
{
$source_dir .= '/';
}
if(substr($target_dir, -1) != '/')
{
$target_dir .= '/';
}
$oDir = dir($source_dir);
while($file = $oDir->read())
{
if(substr($file,0,1)=='.') continue;
if($filter && preg_match($filter, $file)) continue;
if(is_dir($source_dir.$file))
if(substr($file, 0, 1) == '.')
{
FileHandler::copyDir($source_dir.$file,$target_dir.$file,$type);
continue;
}
if($filter && preg_match($filter, $file))
{
continue;
}
if(is_dir($source_dir . $file))
{
FileHandler::copyDir($source_dir . $file, $target_dir . $file, $type);
}
else
{
if($type == 'force')
{
@unlink($target_dir.$file);
@unlink($target_dir . $file);
}
else
{
if(!file_exists($target_dir.$file)) @copy($source_dir.$file,$target_dir.$file);
if(!file_exists($target_dir . $file))
{
@copy($source_dir . $file, $target_dir . $file);
}
}
}
}
@ -72,15 +102,24 @@ class FileHandler
* @param string $force Y: overwrite
* @return void
*/
function copyFile($source, $target, $force='Y')
function copyFile($source, $target, $force = 'Y')
{
setlocale(LC_CTYPE, 'en_US.UTF8', 'ko_KR.UTF8');
$source = FileHandler::getRealPath($source);
$target_dir = FileHandler::getRealPath(dirname($target));
$target = basename($target);
if(!file_exists($target_dir)) FileHandler::makeDir($target_dir);
if($force=='Y') @unlink($target_dir.'/'.$target);
@copy($source, $target_dir.'/'.$target);
if(!file_exists($target_dir))
{
FileHandler::makeDir($target_dir);
}
if($force == 'Y')
{
@unlink($target_dir . '/' . $target);
}
@copy($source, $target_dir . '/' . $target);
}
/**
@ -93,24 +132,18 @@ class FileHandler
{
$file_name = FileHandler::getRealPath($file_name);
if(!file_exists($file_name)) return;
if(!file_exists($file_name))
{
return;
}
$filesize = filesize($file_name);
if($filesize<1) return;
if(function_exists('file_get_contents')) return @file_get_contents($file_name);
$fp = fopen($file_name, "r");
$buff = '';
if($fp)
if($filesize < 1)
{
while(!feof($fp) && strlen($buff)<=$filesize)
{
$str = fgets($fp, 1024);
$buff .= $str;
return;
}
fclose($fp);
}
return $buff;
return @file_get_contents($file_name);
}
/**
@ -127,13 +160,22 @@ class FileHandler
$pathinfo = pathinfo($file_name);
$path = $pathinfo['dirname'];
if(!is_dir($path)) FileHandler::makeDir($path);
if(!is_dir($path))
{
FileHandler::makeDir($path);
}
$mode = strtolower($mode);
if($mode != "a") $mode = "w";
if(@!$fp = fopen($file_name,$mode)) return false;
fwrite($fp, $buff);
fclose($fp);
if($mode == 'a')
{
$flags = FILE_APPEND;
}
else
{
$flags = 0;
}
@file_put_contents($file_name, $buff, $flags);
@chmod($file_name, 0644);
}
@ -212,20 +254,42 @@ class FileHandler
{
$path = FileHandler::getRealPath($path);
if(substr($path,-1)!='/') $path .= '/';
if(!is_dir($path)) return array();
if(substr($path, -1) != '/')
{
$path .= '/';
}
if(!is_dir($path))
{
return array();
}
$oDir = dir($path);
while($file = $oDir->read())
{
if(substr($file,0,1)=='.') continue;
if(substr($file, 0, 1) == '.')
{
continue;
}
if($filter && !preg_match($filter, $file)) continue;
if($filter && !preg_match($filter, $file))
{
continue;
}
if($to_lower) $file = strtolower($file);
if($to_lower)
{
$file = strtolower($file);
}
if($filter) $file = preg_replace($filter, '$1', $file);
else $file = $file;
if($filter)
{
$file = preg_replace($filter, '$1', $file);
}
else
{
$file = $file;
}
if($concat_prefix)
{
@ -234,7 +298,11 @@ class FileHandler
$output[] = $file;
}
if(!$output) return array();
if(!$output)
{
return array();
}
return $output;
}
@ -257,13 +325,25 @@ class FileHandler
$ftp_info = Context::getFTPInfo();
if($oFtp == null)
{
if(!Context::isFTPRegisted()) return;
if(!Context::isFTPRegisted())
{
return;
}
require_once(_XE_PATH_.'libs/ftp.class.php');
require_once(_XE_PATH_ . 'libs/ftp.class.php');
$oFtp = new ftp();
if(!$ftp_info->ftp_host) $ftp_info->ftp_host = "127.0.0.1";
if(!$ftp_info->ftp_port) $ftp_info->ftp_port = 21;
if(!$oFtp->ftp_connect($ftp_info->ftp_host, $ftp_info->ftp_port)) return;
if(!$ftp_info->ftp_host)
{
$ftp_info->ftp_host = "127.0.0.1";
}
if(!$ftp_info->ftp_port)
{
$ftp_info->ftp_port = 21;
}
if(!$oFtp->ftp_connect($ftp_info->ftp_host, $ftp_info->ftp_port))
{
return;
}
if(!$oFtp->ftp_login($ftp_info->ftp_user, $ftp_info->ftp_password))
{
$oFtp->ftp_quit();
@ -271,24 +351,31 @@ class FileHandler
}
}
$ftp_path = $ftp_info->ftp_root_path;
if(!$ftp_path) $ftp_path = "/";
if(!$ftp_path)
{
$ftp_path = "/";
}
}
$path_string = str_replace(_XE_PATH_,'',$path_string);
$path_string = str_replace(_XE_PATH_, '', $path_string);
$path_list = explode('/', $path_string);
$path = _XE_PATH_;
for($i=0;$i<count($path_list);$i++)
for($i = 0; $i < count($path_list); $i++)
{
if(!$path_list[$i]) continue;
$path .= $path_list[$i].'/';
$ftp_path .= $path_list[$i].'/';
if(!$path_list[$i])
{
continue;
}
$path .= $path_list[$i] . '/';
$ftp_path .= $path_list[$i] . '/';
if(!is_dir($path))
{
if(ini_get('safe_mode'))
{
$oFtp->ftp_mkdir($ftp_path);
$oFtp->ftp_site("CHMOD 777 ".$ftp_path);
$oFtp->ftp_site("CHMOD 777 " . $ftp_path);
}
else
{
@ -310,19 +397,23 @@ class FileHandler
function removeDir($path)
{
$path = FileHandler::getRealPath($path);
if(!is_dir($path)) return;
if(!is_dir($path))
{
return;
}
$directory = dir($path);
while($entry = $directory->read())
{
if ($entry != "." && $entry != "..")
if($entry != "." && $entry != "..")
{
if (is_dir($path."/".$entry))
if(is_dir($path . "/" . $entry))
{
FileHandler::removeDir($path."/".$entry);
FileHandler::removeDir($path . "/" . $entry);
}
else
{
@unlink($path."/".$entry);
@unlink($path . "/" . $entry);
}
}
}
@ -341,18 +432,30 @@ class FileHandler
$item_cnt = 0;
$path = FileHandler::getRealPath($path);
if(!is_dir($path)) return;
if(!is_dir($path))
{
return;
}
$directory = dir($path);
while($entry = $directory->read())
{
if ($entry == "." || $entry == "..") continue;
if (is_dir($path."/".$entry)) $item_cnt = FileHandler::removeBlankDir($path.'/'.$entry);
if($entry == "." || $entry == "..")
{
continue;
}
if(is_dir($path . "/" . $entry))
{
$item_cnt = FileHandler::removeBlankDir($path . '/' . $entry);
}
}
$directory->close();
if($item_cnt < 1) @rmdir($path);
if($item_cnt < 1)
{
@rmdir($path);
}
}
/**
* Remove files in the target directory
@ -365,19 +468,23 @@ class FileHandler
function removeFilesInDir($path)
{
$path = FileHandler::getRealPath($path);
if(!is_dir($path)) return;
if(!is_dir($path))
{
return;
}
$directory = dir($path);
while($entry = $directory->read())
{
if ($entry != "." && $entry != "..")
if($entry != "." && $entry != "..")
{
if (is_dir($path."/".$entry))
if(is_dir($path . "/" . $entry))
{
FileHandler::removeFilesInDir($path."/".$entry);
FileHandler::removeFilesInDir($path . "/" . $entry);
}
else
{
@unlink($path."/".$entry);
@unlink($path . "/" . $entry);
}
}
}
@ -393,11 +500,27 @@ class FileHandler
*/
function filesize($size)
{
if(!$size) return '0Byte';
if($size === 1) return '1Byte';
if($size < 1024) return $size.'Bytes';
if($size >= 1024 && $size < 1024*1024) return sprintf("%0.1fKB",$size / 1024);
return sprintf("%0.2fMB",$size / (1024*1024));
if(!$size)
{
return '0Byte';
}
if($size === 1)
{
return '1Byte';
}
if($size < 1024)
{
return $size . 'Bytes';
}
if($size >= 1024 && $size < 1024 * 1024)
{
return sprintf("%0.1fKB", $size / 1024);
}
return sprintf("%0.2fMB", $size / (1024 * 1024));
}
/**
@ -417,32 +540,7 @@ class FileHandler
*/
function getRemoteResource($url, $body = null, $timeout = 3, $method = 'GET', $content_type = null, $headers = array(), $cookies = array(), $post_data = array())
{
if(version_compare(PHP_VERSION, '5.0.0', '>='))
{
return include _XE_PATH_ . 'classes/file/getRemoteResourcePHP5.php';
}
else
{
return FileHandler::_getRemoteResource($url, $boyd, $timeout, $mehtod, $conent_type, $headers, $cookies, $post_data);
}
}
/**
* Return remote file's content via HTTP
*
* If the target is moved (when return code is 300~399), this function follows the location specified response header.
*
* @param string $url The address of the target file
* @param string $body HTTP request body
* @param int $timeout Connection timeout
* @param string $method GET/POST
* @param string $content_type Content type header of HTTP request
* @param string[] $headers Headers key vaule array.
* @param string[] $cookies Cookies key value array.
* @param string $post_data Request arguments array for POST method
* @return string If success, the content of the target file. Otherwise: none
*/
function _getRemoteResource($url, $body = null, $timeout = 3, $method = 'GET', $content_type = null, $headers = array(), $cookies = array(), $post_data = array())
try
{
requirePear();
require_once('HTTP/Request.php');
@ -453,7 +551,7 @@ class FileHandler
$oRequest = new HTTP_Request(__PROXY_SERVER__);
$oRequest->setMethod('POST');
$oRequest->_timeout = $timeout;
$oRequest->addPostData('arg', serialize(array('Destination'=>$url, 'method'=>$method, 'body'=>$body, 'content_type'=>$content_type, "headers"=>$headers, "post_data"=>$post_data)));
$oRequest->addPostData('arg', serialize(array('Destination' => $url, 'method' => $method, 'body' => $body, 'content_type' => $content_type, "headers" => $headers, "post_data" => $post_data)));
}
else
{
@ -479,10 +577,13 @@ class FileHandler
$oRequest->addPostData($key, $val);
}
}
if(!$content_type) $oRequest->addHeader('Content-Type', 'text/html');
else $oRequest->addHeader('Content-Type', $content_type);
if(!$content_type)
$oRequest->addHeader('Content-Type', 'text/html');
else
$oRequest->addHeader('Content-Type', $content_type);
$oRequest->setMethod($method);
if($body) $oRequest->setBody($body);
if($body)
$oRequest->setBody($body);
$oRequest->_timeout = $timeout;
}
@ -505,10 +606,16 @@ class FileHandler
return FileHandler::getRemoteResource($header['location'], $body, $timeout, $method, $content_type, $headers, $cookies, $post_data);
}
if($code != 200) return;
if($code != 200)
return;
return $response;
}
catch(Exception $e)
{
return NULL;
}
}
/**
* Retrieves remote file, then stores it into target path.
@ -525,10 +632,13 @@ class FileHandler
function getRemoteFile($url, $target_filename, $body = null, $timeout = 3, $method = 'GET', $content_type = null, $headers = array())
{
$body = FileHandler::getRemoteResource($url, $body, $timeout, $method, $content_type, $headers);
if(!$body) return false;
if(!$body)
{
return FALSE;
}
$target_filename = FileHandler::getRealPath($target_filename);
FileHandler::writeFile($target_filename, $body);
return true;
return TRUE;
}
/**
@ -542,10 +652,22 @@ class FileHandler
{
$val = trim($val);
$last = strtolower(substr($val, -1));
if($last == 'g') $val *= 1024*1024*1024;
else if($last == 'm') $val *= 1024*1024;
else if($last == 'k') $val *= 1024;
else $val *= 1;
if($last == 'g')
{
$val *= 1024 * 1024 * 1024;
}
else if($last == 'm')
{
$val *= 1024 * 1024;
}
else if($last == 'k')
{
$val *= 1024;
}
else
{
$val *= 1;
}
return $val;
}
@ -558,15 +680,25 @@ class FileHandler
*/
function checkMemoryLoadImage(&$imageInfo)
{
if(!function_exists('memory_get_usage')) return true;
if(!function_exists('memory_get_usage'))
{
return TRUE;
}
$K64 = 65536;
$TWEAKFACTOR = 2.0;
$channels = $imageInfo['channels'];
if(!$channels) $channels = 6; //for png
$memoryNeeded = round( ($imageInfo[0] * $imageInfo[1] * $imageInfo['bits'] * $channels / 8 + $K64 ) * $TWEAKFACTOR );
if(!$channels)
{
$channels = 6; //for png
}
$memoryNeeded = round(($imageInfo[0] * $imageInfo[1] * $imageInfo['bits'] * $channels / 8 + $K64 ) * $TWEAKFACTOR);
$availableMemory = FileHandler::returnBytes(ini_get('memory_limit')) - memory_get_usage();
if($availableMemory < $memoryNeeded) return false;
return true;
if($availableMemory < $memoryNeeded)
{
return FALSE;
}
return TRUE;
}
/**
@ -585,16 +717,31 @@ class FileHandler
$source_file = FileHandler::getRealPath($source_file);
$target_file = FileHandler::getRealPath($target_file);
if(!file_exists($source_file)) return;
if(!$resize_width) $resize_width = 100;
if(!$resize_height) $resize_height = $resize_width;
if(!file_exists($source_file))
{
return;
}
if(!$resize_width)
{
$resize_width = 100;
}
if(!$resize_height)
{
$resize_height = $resize_width;
}
// retrieve source image's information
$imageInfo = getimagesize($source_file);
if(!FileHandler::checkMemoryLoadImage($imageInfo)) return false;
if(!FileHandler::checkMemoryLoadImage($imageInfo))
{
return FALSE;
}
list($width, $height, $type, $attrs) = $imageInfo;
if($width<1 || $height<1) return;
if($width < 1 || $height < 1)
{
return;
}
switch($type)
{
@ -616,62 +763,116 @@ class FileHandler
}
// if original image is larger than specified size to resize, calculate the ratio
if($resize_width > 0 && $width >= $resize_width) $width_per = $resize_width / $width;
else $width_per = 1;
if($resize_width > 0 && $width >= $resize_width)
{
$width_per = $resize_width / $width;
}
else
{
$width_per = 1;
}
if($resize_height>0 && $height >= $resize_height) $height_per = $resize_height / $height;
else $height_per = 1;
if($resize_height > 0 && $height >= $resize_height)
{
$height_per = $resize_height / $height;
}
else
{
$height_per = 1;
}
if($thumbnail_type == 'ratio')
{
if($width_per>$height_per) $per = $height_per;
else $per = $width_per;
if($width_per > $height_per)
{
$per = $height_per;
}
else
{
$per = $width_per;
}
$resize_width = $width * $per;
$resize_height = $height * $per;
}
else
{
if($width_per < $height_per) $per = $height_per;
else $per = $width_per;
if($width_per < $height_per)
{
$per = $height_per;
}
else
{
$per = $width_per;
}
}
if(!$per) $per = 1;
if(!$per)
{
$per = 1;
}
// get type of target file
if(!$target_type) $target_type = $type;
if(!$target_type)
{
$target_type = $type;
}
$target_type = strtolower($target_type);
// create temporary image with target size
if(function_exists('imagecreatetruecolor')) $thumb = imagecreatetruecolor($resize_width, $resize_height);
else if(function_exists('imagecreate')) $thumb = imagecreate($resize_width, $resize_height);
else return false;
if(!$thumb) return false;
if(function_exists('imagecreatetruecolor'))
{
$thumb = imagecreatetruecolor($resize_width, $resize_height);
}
else if(function_exists('imagecreate'))
{
$thumb = imagecreate($resize_width, $resize_height);
}
else
{
return FALSE;
}
if(!$thumb)
{
return FALSE;
}
$white = imagecolorallocate($thumb, 255,255,255);
imagefilledrectangle($thumb,0,0,$resize_width-1,$resize_height-1,$white);
$white = imagecolorallocate($thumb, 255, 255, 255);
imagefilledrectangle($thumb, 0, 0, $resize_width - 1, $resize_height - 1, $white);
// create temporary image having original type
switch($type)
{
case 'gif' :
if(!function_exists('imagecreatefromgif')) return false;
if(!function_exists('imagecreatefromgif'))
{
return FALSE;
}
$source = @imagecreatefromgif($source_file);
break;
// jpg
case 'jpeg' :
case 'jpg' :
if(!function_exists('imagecreatefromjpeg')) return false;
if(!function_exists('imagecreatefromjpeg'))
{
return FALSE;
}
$source = @imagecreatefromjpeg($source_file);
break;
// png
case 'png' :
if(!function_exists('imagecreatefrompng')) return false;
if(!function_exists('imagecreatefrompng'))
{
return FALSE;
}
$source = @imagecreatefrompng($source_file);
break;
// bmp
case 'wbmp' :
case 'bmp' :
if(!function_exists('imagecreatefromwbmp')) return false;
if(!function_exists('imagecreatefromwbmp'))
{
return FALSE;
}
$source = @imagecreatefromwbmp($source_file);
break;
default :
@ -679,13 +880,13 @@ class FileHandler
}
// resize original image and put it into temporary image
$new_width = (int)($width * $per);
$new_height = (int)($height * $per);
$new_width = (int) ($width * $per);
$new_height = (int) ($height * $per);
if($thumbnail_type == 'crop')
{
$x = (int)($resize_width/2 - $new_width/2);
$y = (int)($resize_height/2 - $new_height/2);
$x = (int) ($resize_width / 2 - $new_width / 2);
$y = (int) ($resize_height / 2 - $new_height / 2);
}
else
{
@ -695,34 +896,58 @@ class FileHandler
if($source)
{
if(function_exists('imagecopyresampled')) imagecopyresampled($thumb, $source, $x, $y, 0, 0, $new_width, $new_height, $width, $height);
else imagecopyresized($thumb, $source, $x, $y, 0, 0, $new_width, $new_height, $width, $height);
if(function_exists('imagecopyresampled'))
{
imagecopyresampled($thumb, $source, $x, $y, 0, 0, $new_width, $new_height, $width, $height);
}
else
{
imagecopyresized($thumb, $source, $x, $y, 0, 0, $new_width, $new_height, $width, $height);
}
}
else
{
return FALSE;
}
else return false;
// create directory
$path = dirname($target_file);
if(!is_dir($path)) FileHandler::makeDir($path);
if(!is_dir($path))
{
FileHandler::makeDir($path);
}
// write into the file
switch($target_type)
{
case 'gif' :
if(!function_exists('imagegif')) return false;
if(!function_exists('imagegif'))
{
return FALSE;
}
$output = imagegif($thumb, $target_file);
break;
case 'jpeg' :
case 'jpg' :
if(!function_exists('imagejpeg')) return false;
if(!function_exists('imagejpeg'))
{
return FALSE;
}
$output = imagejpeg($thumb, $target_file, 100);
break;
case 'png' :
if(!function_exists('imagepng')) return false;
if(!function_exists('imagepng'))
{
return FALSE;
}
$output = imagepng($thumb, $target_file, 9);
break;
case 'wbmp' :
case 'bmp' :
if(!function_exists('imagewbmp')) return false;
if(!function_exists('imagewbmp'))
{
return FALSE;
}
$output = imagewbmp($thumb, $target_file, 100);
break;
}
@ -730,10 +955,13 @@ class FileHandler
imagedestroy($thumb);
imagedestroy($source);
if(!$output) return false;
if(!$output)
{
return FALSE;
}
@chmod($target_file, 0644);
return true;
return TRUE;
}
/**
@ -746,10 +974,19 @@ class FileHandler
function readIniFile($filename)
{
$filename = FileHandler::getRealPath($filename);
if(!file_exists($filename)) return false;
$arr = parse_ini_file($filename, true);
if(is_array($arr) && count($arr)>0) return $arr;
else return array();
if(!file_exists($filename))
{
return FALSE;
}
$arr = parse_ini_file($filename, TRUE);
if(is_array($arr) && count($arr) > 0)
{
return $arr;
}
else
{
return array();
}
}
/**
@ -768,9 +1005,12 @@ class FileHandler
*/
function writeIniFile($filename, $arr)
{
if(count($arr)==0) return false;
if(count($arr) == 0)
{
return FALSE;
}
FileHandler::writeFile($filename, FileHandler::_makeIniBuff($arr));
return true;
return TRUE;
}
/**
@ -787,16 +1027,16 @@ class FileHandler
// section
if(is_array($val))
{
$return .= sprintf("[%s]\n",$key);
$return .= sprintf("[%s]\n", $key);
foreach($val as $k => $v)
{
$return .= sprintf("%s=\"%s\"\n",$k,$v);
$return .= sprintf("%s=\"%s\"\n", $k, $v);
}
// value
}
else if(is_string($val) || is_int($val))
{
$return .= sprintf("%s=\"%s\"\n",$key,$val);
$return .= sprintf("%s=\"%s\"\n", $key, $val);
}
}
return $return;
@ -815,7 +1055,10 @@ class FileHandler
{
$pathinfo = pathinfo($filename);
$path = $pathinfo['dirname'];
if(!is_dir($path)) FileHandler::makeDir($path);
if(!is_dir($path))
{
FileHandler::makeDir($path);
}
require_once("FileObject.class.php");
$file_object = new FileObject($file_name, $mode);
@ -832,7 +1075,7 @@ class FileHandler
{
return (is_readable($filename) && !!filesize($filename));
}
}
}
/* End of file FileHandler.class.php */
/* Location: ./classes/file/FileHandler.class.php */

View file

@ -1,4 +1,5 @@
<?php
/**
* File abstraction class
*
@ -6,17 +7,18 @@
*/
class FileObject extends Object
{
/**
* File descriptor
* @var resource
*/
var $fp = null;
var $fp = NULL;
/**
* File path
* @var string
*/
var $path = null;
var $path = NULL;
/**
* File open mode
@ -33,7 +35,10 @@ class FileObject extends Object
*/
function FileObject($path, $mode)
{
if($path != null) $this->Open($path, $mode);
if($path != NULL)
{
$this->Open($path, $mode);
}
}
/**
@ -74,7 +79,6 @@ class FileObject extends Object
return fread($this->fp, $size);
}
/**
* Write string to current file
*
@ -84,8 +88,14 @@ class FileObject extends Object
function write($str)
{
$len = strlen($str);
if(!$str || $len <= 0) return false;
if(!$this->fp) return false;
if(!$str || $len <= 0)
{
return FALSE;
}
if(!$this->fp)
{
return FALSE;
}
$written = fwrite($this->fp, $str);
return $written;
}
@ -101,18 +111,18 @@ class FileObject extends Object
*/
function open($path, $mode)
{
if($this->fp != null)
if($this->fp != NULL)
{
$this->close();
}
$this->fp = fopen($path, $mode);
if(! is_resource($this->fp) )
if(!is_resource($this->fp))
{
$this->fp = null;
return false;
$this->fp = NULL;
return FALSE;
}
$this->path = $path;
return true;
return TRUE;
}
/**
@ -122,13 +132,13 @@ class FileObject extends Object
*/
function getPath()
{
if($this->fp != null)
if($this->fp != NULL)
{
return $this->path;
}
else
{
return null;
return NULL;
}
}
@ -139,13 +149,13 @@ class FileObject extends Object
*/
function close()
{
if($this->fp != null)
if($this->fp != NULL)
{
fclose($this->fp);
$this->fp = null;
$this->fp = NULL;
}
}
}
}
/* End of file FileObject.class.php */
/* Location: ./classes/file/FileObject.class.php */

View file

@ -1,11 +0,0 @@
<?php
try
{
return self::_getRemoteResource($url, $body, $timeout, $method, $content_type, $headers, $cookies, $post_data);
}
catch(Exception $e)
{
return NULL;
}
/* End of file getRemoteResourcePHP5.php */
/* Location: ./classes/file/getRemoteResourcePHP5.php */