moved methods where they belong

This commit is contained in:
Christian Haschek
2015-11-02 12:18:47 +01:00
parent df5c74f04c
commit c511a81e08
2 changed files with 171 additions and 132 deletions

View File

@@ -41,48 +41,21 @@ function aasort (&$array, $key)
$array=$ret;
}
function sanatizeString($string)
{
return preg_replace("/[^a-zA-Z0-9._]+/", "", $string);
}
function callHook()
{
global $url;
global $default;
$urlArray = explode("/",$url);
whatToDo($urlArray);
whatToDo($url);
}
function whatToDo($url)
{
$pm = new PictshareModel();
foreach($url as $el)
{
$el = sanatizeString($el);
$el = strtolower($el);
if(!$el) continue;
if(IMAGE_CHANGE_CODE!=false && substr($el,0,10)=='changecode')
$changecode = substr($el,11);
if(isImage($el))
$data['hash']=$el;
else if(isSize($el))
$data['size'] = $el;
else if(isRotation($el))
$data['rotate'] = $el;
else if(isFilter($el))
$data['filter'][] = $el;
else if($legacy = isLegacyThumbnail($el)) //so old uploads will still work
{
$data['hash'] = $legacy['hash'];
$data['size'] = $legacy['size'];
}
}
$data = $pm->urlToData($url);
if(!is_array($data) || !$data['hash'])
{
@@ -97,86 +70,29 @@ function whatToDo($url)
render($vars);
}
else
renderImage($data,$changecode);
renderImage($data);
}
function isLegacyThumbnail($val)
{
if(strpos($val,'_'))
{
$a = explode('_',$val);
$size = $a[0];
$hash = $a[1];
if(!isSize($size) || !isImage($hash)) return false;
return array('hash'=>$hash,'size'=>$size);
}
else return false;
}
function isFilter($var)
{
if(strpos($var,'_'))
{
$a = explode('_',$var);
$var = $a[0];
$val = $a[1];
if(!is_numeric($val)) return false;
}
switch($var)
{
case 'negative':
case 'grayscale':
case 'brightness':
case 'edgedetect':
case 'smooth':
case 'contrast':
case 'pixelate': return true;
default: return false;
}
}
function isRotation($var)
{
switch($var)
{
case 'upside':
case 'left':
case 'right': return true;
default: return false;
}
}
function renderLegacyResized($path)
{
$a = explode('_',$path);
if(count($a)!=2) return false;
$pm = new PictshareModel();
$hash = $a[1];
$size = $a[0];
if(!$pm->hashExists($hash)) return false;
renderResizedImage($size,$hash);
}
function renderImage($data,$changecode)
function renderImage($data)
{
$hash = $data['hash'];
if($data['changecode'])
{
$changecode = $data['changecode'];
unset($data['changecode']);
}
$pm = new PictshareModel();
$base_path = ROOT.DS.'upload'.DS.$hash.DS;
$path = $base_path.$hash;
$type = $pm->isTypeAllowed($pm->getTypeOfFile($path));
$cached = false;
$cachename = getCacheName($data);
$cachename = $pm->getCacheName($data);
$cachepath = $base_path.$cachename;
if(file_exists($cachepath))
{
@@ -240,41 +156,7 @@ function changeImage(&$im,$data)
}
}
function getCacheName($data)
{
ksort($data);
$name = array();
foreach($data as $key=>$val)
{
if($key!='hash')
{
if(!is_array($val))
$name[] = $key.'_'.$val;
else
foreach($val as $valdata)
$name[] = $valdata;
}
}
return implode('.',$name).'.'.$data['hash'];
}
function isSize($var)
{
if(is_numeric($var)) return true;
$a = explode('x',$var);
if(count($a)!=2 || !is_numeric($a[0]) || !is_numeric($a[1])) return false;
return true;
}
function isImage($hash)
{
$pm = new PictshareModel();
if(!$hash) return false;
return $pm->hashExists($hash);
}
function render($variables=null)
{

View File

@@ -7,6 +7,162 @@ class PictshareModel extends Model
return array('status'=>'ok');
}
function getSizeOfURL($url)
{
$url = rawurldecode($url);
$data = $this->urlToData($url);
$hash = $data['hash'];
if(!$hash)
return array('status'=>'ERR','Reason'=>'Image not found');
$file = $this->getCacheName($data);
$html = new HTML;
$path = ROOT.DS.'upload'.DS.$hash.DS.$file;
if(file_exists($path))
{
$byte = filesize($path);
return array('status'=>'ok','size'=>$byte,'humansize'=>$html->renderSize($byte));
}
else
return array('status'=>'ERR','Reason'=>'Image not found');
}
function urlToData($url)
{
$html = new HTML;
$url = explode("/",$url);
foreach($url as $el)
{
$el = $html->sanatizeString($el);
$el = strtolower($el);
if(!$el) continue;
if(IMAGE_CHANGE_CODE!=false && substr($el,0,10)=='changecode')
$data['changecode'] = substr($el,11);
if($this->isImage($el))
$data['hash']=$el;
else if($this->isSize($el))
$data['size'] = $el;
else if($this->isRotation($el))
$data['rotate'] = $el;
else if($this->isFilter($el))
$data['filter'][] = $el;
else if($legacy = $this->isLegacyThumbnail($el)) //so old uploads will still work
{
$data['hash'] = $legacy['hash'];
$data['size'] = $legacy['size'];
}
}
return $data;
}
function isLegacyThumbnail($val)
{
if(strpos($val,'_'))
{
$a = explode('_',$val);
$size = $a[0];
$hash = $a[1];
if(!$this->isSize($size) || !$this->isImage($hash)) return false;
return array('hash'=>$hash,'size'=>$size);
}
else return false;
}
function isFilter($var)
{
if(strpos($var,'_'))
{
$a = explode('_',$var);
$var = $a[0];
$val = $a[1];
if(!is_numeric($val)) return false;
}
switch($var)
{
case 'negative':
case 'grayscale':
case 'brightness':
case 'edgedetect':
case 'smooth':
case 'contrast':
case 'pixelate': return true;
default: return false;
}
}
function isRotation($var)
{
switch($var)
{
case 'upside':
case 'left':
case 'right': return true;
default: return false;
}
}
function renderLegacyResized($path)
{
$a = explode('_',$path);
if(count($a)!=2) return false;
$hash = $a[1];
$size = $a[0];
if(!$this->hashExists($hash)) return false;
renderResizedImage($size,$hash);
}
function getCacheName($data)
{
ksort($data);
$name = false;
foreach($data as $key=>$val)
{
if($key!='hash')
{
if(!is_array($val))
$name[] = $key.'_'.$val;
else
foreach($val as $valdata)
$name[] = $valdata;
}
}
if(is_array($name))
$name = implode('.',$name);
return ($name?$name.'.':'').$data['hash'];
}
function isSize($var)
{
if(is_numeric($var)) return true;
$a = explode('x',$var);
if(count($a)!=2 || !is_numeric($a[0]) || !is_numeric($a[1])) return false;
return true;
}
function isImage($hash)
{
if(!$hash) return false;
return $this->hashExists($hash);
}
function renderUploadForm()
{
$maxfilesize = (int)(ini_get('upload_max_filesize'));
@@ -147,6 +303,7 @@ class PictshareModel extends Model
function changeCodeExists($code)
{
var_dump($code);
if(IMAGE_CHANGE_CODE===false) return true;
if(strpos(IMAGE_CHANGE_CODE,';'))
{