First commit

This commit is contained in:
Theodotos Andreou 2018-01-14 13:10:16 +00:00
commit c6e2478c40
13918 changed files with 2303184 additions and 0 deletions

View file

@ -0,0 +1,255 @@
<?php
namespace kcfinder;
/**
* FastImage - Because sometimes you just want the size!
* Based on the Ruby Implementation by Steven Sykes (https://github.com/sdsykes/fastimage)
*
* Copyright (c) 2012 Tom Moor
* Tom Moor, http://tommoor.com
*
* MIT Licensed
* @version 0.1
*/
class fastImage
{
private $strpos = 0;
private $str;
private $uri;
private $type;
private $handle;
public function __construct($uri = null)
{
if ($uri) $this->load($uri);
}
public function load($uri)
{
if ($this->handle) $this->close();
$this->uri = $uri;
// Joy - this is a fix for URLs missing "http:"
if ($uri[0] == '/' && $uri[1] == '/') {
$uri = 'http:' . $uri;
}
$this->handle = fopen(
$uri,
'r',
false,
stream_context_create(array(
'http'=> array('timeout' => 0.5),
))
);
}
public function isValid()
{
return empty($this->handle) ? false : true;
}
public function close()
{
if (is_resource($this->handle)) fclose($this->handle);
}
public function getSize()
{
$this->strpos = 0;
if ($this->getType())
{
return array_values($this->parseSize());
}
return false;
}
public function getType()
{
$this->strpos = 0;
if (!$this->type)
{
switch ($this->getChars(2))
{
case "BM":
return $this->type = 'bmp';
case "GI":
return $this->type = 'gif';
case chr(0xFF).chr(0xd8):
return $this->type = 'jpeg';
case chr(0x89).'P':
return $this->type = 'png';
default:
return false;
}
}
return $this->type;
}
private function parseSize()
{
$this->strpos = 0;
switch ($this->type)
{
case 'png':
return $this->parseSizeForPNG();
case 'gif':
return $this->parseSizeForGIF();
case 'bmp':
return $this->parseSizeForBMP();
case 'jpeg':
return $this->parseSizeForJPEG();
}
return null;
}
private function parseSizeForPNG()
{
$chars = $this->getChars(25);
return unpack("N*", substr($chars, 16, 8));
}
private function parseSizeForGIF()
{
$chars = $this->getChars(11);
return unpack("S*", substr($chars, 6, 4));
}
private function parseSizeForBMP()
{
$chars = $this->getChars(29);
$chars = substr($chars, 14, 14);
$type = unpack('C', $chars);
return (reset($type) == 40) ? unpack('L*', substr($chars, 4)) : unpack('L*', substr($chars, 4, 8));
}
private function parseSizeForJPEG()
{
$state = null;
$i = 0;
while (true)
{
switch ($state)
{
default:
$this->getChars(2);
$state = 'started';
break;
case 'started':
$b = $this->getByte();
if ($b === false) return false;
$state = $b == 0xFF ? 'sof' : 'started';
break;
case 'sof':
$b = $this->getByte();
if (in_array($b, range(0xe0, 0xef)))
{
$state = 'skipframe';
}
elseif (in_array($b, array_merge(range(0xC0,0xC3), range(0xC5,0xC7), range(0xC9,0xCB), range(0xCD,0xCF))))
{
$state = 'readsize';
}
elseif ($b == 0xFF)
{
$state = 'sof';
}
else
{
$state = 'skipframe';
}
break;
case 'skipframe':
$skip = $this->readInt($this->getChars(2)) - 2;
$state = 'doskip';
break;
case 'doskip':
$this->getChars($skip);
$state = 'started';
break;
case 'readsize':
$c = $this->getChars(7);
return array($this->readInt(substr($c, 5, 2)), $this->readInt(substr($c, 3, 2)));
}
}
}
private function getChars($n)
{
$response = null;
// do we need more data?
if ($this->strpos + $n -1 >= strlen($this->str))
{
$end = ($this->strpos + $n);
while (strlen($this->str) < $end && $response !== false)
{
// read more from the file handle
$need = $end - ftell($this->handle);
if ($response = fread($this->handle, $need))
{
$this->str .= $response;
}
else
{
return false;
}
}
}
$result = substr($this->str, $this->strpos, $n);
$this->strpos += $n;
return $result;
}
private function getByte()
{
$c = $this->getChars(1);
$b = unpack("C", $c);
return reset($b);
}
private function readInt($str)
{
$size = unpack("C*", $str);
return ($size[1] << 8) + $size[2];
}
public function __destruct()
{
$this->close();
}
}

View file

@ -0,0 +1,243 @@
<?php
/** This file is part of KCFinder project
*
* @desc Abstract image driver class
* @package KCFinder
* @version 3.12
* @author Pavel Tzonkov <sunhater@sunhater.com>
* @copyright 2010-2014 KCFinder Project
* @license http://opensource.org/licenses/GPL-3.0 GPLv3
* @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
* @link http://kcfinder.sunhater.com
*/
namespace kcfinder;
abstract class image {
const DEFAULT_JPEG_QUALITY = 75;
/** Image resource or object
* @var mixed */
protected $image;
/** Image width in pixels
* @var integer */
protected $width;
/** Image height in pixels
* @var integer */
protected $height;
/** Init error
* @var bool */
protected $initError = false;
/** Driver specific options
* @var array */
protected $options = array();
/** Magic method which allows read-only access to all protected or private
* class properties
* @param string $property
* @return mixed */
final public function __get($property) {
return property_exists($this, $property) ? $this->$property : null;
}
/** Constructor. Parameter $image should be:
* 1. An instance of image driver class (copy instance).
* 2. An image represented by the type of the $image property
* (resource or object).
* 3. An array with two elements. First - width, second - height.
* Creates a blank image.
* 4. A filename string. Get image form file.
* Second paramaeter is used by pass some specific image driver options
* @param mixed $image
* @param array $options */
public function __construct($image, array $options=array()) {
$this->image = $this->width = $this->height = null;
$imageDetails = $this->buildImage($image);
if ($imageDetails !== false)
list($this->image, $this->width, $this->height) = $imageDetails;
else
$this->initError = true;
$this->options = $options;
}
/** Factory pattern to load selected driver. $image and $options are passed
* to the constructor of the image driver
* @param string $driver
* @param mixed $image
* @return object */
final static function factory($driver, $image, array $options=array()) {
$class = __NAMESPACE__ . "\\image_$driver";
return new $class($image, $options);
}
/** Checks if the drivers in the array parameter could be used. Returns first
* found one
* @param array $drivers
* @return string */
final static function getDriver(array $drivers=array('gd')) {
foreach ($drivers as $driver) {
if (!preg_match('/^[a-z0-9\_]+$/i', $driver))
continue;
$class = __NAMESPACE__ . "\\image_$driver";
if (class_exists($class) && method_exists($class, "available")) {
eval("\$avail = $class::available();");
if ($avail) return $driver;
}
}
return false;
}
/** Returns an array. Element 0 - image resource. Element 1 - width. Element 2 - height.
* Returns FALSE on failure.
* @param mixed $image
* @return array */
final protected function buildImage($image) {
$class = get_class($this);
if ($image instanceof $class) {
$width = $image->width;
$height = $image->height;
$img = $image->image;
} elseif (is_array($image)) {
list($key, $width) = each($image);
list($key, $height) = each($image);
$img = $this->getBlankImage($width, $height);
} else
$img = $this->getImage($image, $width, $height);
return ($img !== false)
? array($img, $width, $height)
: false;
}
/** Returns calculated proportional width from the given height
* @param integer $resizedHeight
* @return integer */
final public function getPropWidth($resizedHeight) {
$width = round(($this->width * $resizedHeight) / $this->height);
if (!$width) $width = 1;
return $width;
}
/** Returns calculated proportional height from the given width
* @param integer $resizedWidth
* @return integer */
final public function getPropHeight($resizedWidth) {
$height = round(($this->height * $resizedWidth) / $this->width);
if (!$height) $height = 1;
return $height;
}
/** Checks if PHP needs some extra extensions to use the image driver. This
* static method should be implemented into driver classes like abstract
* methods
* @return bool */
static function available() { return false; }
/** Checks if file is an image. This static method should be implemented into
* driver classes like abstract methods
* @param string $file
* @return bool */
static function checkImage($file) { return false; }
/** Resize image. Should return TRUE on success or FALSE on failure
* @param integer $width
* @param integer $height
* @return bool */
abstract public function resize($width, $height);
/** Resize image to fit in given resolution. Should returns TRUE on success
* or FALSE on failure. If $background is set, the image size will be
* $width x $height and the empty spaces (if any) will be filled with defined
* color. Background color examples: "#5f5", "#ff67ca", array(255, 255, 255)
* @param integer $width
* @param integer $height
* @param mixed $background
* @return bool */
abstract public function resizeFit($width, $height, $background=false);
/** Resize and crop the image to fit in given resolution. Returns TRUE on
* success or FALSE on failure
* @param mixed $src
* @param integer $offset
* @return bool */
abstract public function resizeCrop($width, $height, $offset=false);
/** Rotate image
* @param integer $angle
* @param string $background
* @return bool */
abstract public function rotate($angle, $background="#000000");
abstract public function flipHorizontal();
abstract public function flipVertical();
/** Apply a PNG or GIF watermark to the image. $top and $left parameters sets
* the offset of the watermark in pixels. Boolean and NULL values are possible
* too. In default case (FALSE, FALSE) the watermark should be applyed to
* the bottom right corner. NULL values means center aligning. If the
* watermark is bigger than the image or it's partialy or fully outside the
* image, it shoudn't be applied
* @param string $file
* @param mixed $top
* @param mixed $left
* @return bool */
abstract public function watermark($file, $left=false, $top=false);
/** Should output the image. Second parameter is used to pass some options like
* 'file' - if is set, the output will be written to a file
* 'quality' - compression quality
* It's possible to use extra specific options required by image type ($type)
* @param string $type
* @param array $options
* @return bool */
abstract public function output($type='jpeg', array $options=array());
/** This method should create a blank image with selected size. Should returns
* resource or object related to the created image, which will be passed to
* $image property
* @param integer $width
* @param integer $height
* @return mixed */
abstract protected function getBlankImage($width, $height);
/** This method should create an image from source image. Only first parameter
* ($image) is input. Its type should be filename string or a type of the
* $image property. See the constructor reference for details. The
* parametters $width and $height are output only. Should returns resource or
* object related to the created image, which will be passed to $image
* property
* @param mixed $image
* @param integer $width
* @param integer $height
* @return mixed */
abstract protected function getImage($image, &$width, &$height);
}
?>

View file

@ -0,0 +1,354 @@
<?php
/** This file is part of KCFinder project
*
* @desc GD image driver class
* @package KCFinder
* @version 3.12
* @author Pavel Tzonkov <sunhater@sunhater.com>
* @copyright 2010-2014 KCFinder Project
* @license http://opensource.org/licenses/GPL-3.0 GPLv3
* @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
* @link http://kcfinder.sunhater.com
*/
namespace kcfinder;
class image_gd extends image {
// ABSTRACT PUBLIC METHODS
public function resize($width, $height) {
if (!$width) $width = 1;
if (!$height) $height = 1;
return (
(false !== ($img = new image_gd(array($width, $height)))) &&
$img->imageCopyResampled($this) &&
(false !== ($this->image = $img->image)) &&
(false !== ($this->width = $img->width)) &&
(false !== ($this->height = $img->height))
);
}
public function resizeFit($width, $height, $background=false) {
if ((!$width && !$height) || (($width == $this->width) && ($height == $this->height)))
return true;
if (!$width || (($height / $width) < ($this->height / $this->width))) {
$h = $height;
$w = round(($this->width * $h) / $this->height);
} elseif (!$height || (($width / $height) < ($this->width / $this->height))) {
$w = $width;
$h = round(($this->height * $w) / $this->width);
} else {
$w = $width;
$h = $height;
}
if (!$w) $w = 1;
if (!$h) $h = 1;
if ($background === false)
return $this->resize($w, $h);
else {
$img = new image_gd(array($width, $height));
$x = round(($width - $w) / 2);
$y = round(($height - $h) / 2);
if ((false === $this->resize($w, $h)) ||
(false === $img->imageFilledRectangle(0, 0, $width, $height, $background)) ||
(false === $img->imageCopyResampled($this->image, $x, $y, 0, 0, $w, $h))
)
return false;
$this->image = $img->image;
$this->width = $width;
$this->height = $height;
return true;
}
}
public function resizeCrop($width, $height, $offset=false) {
if (($this->width / $this->height) > ($width / $height)) {
$h = $height;
$w = ($this->width * $h) / $this->height;
$y = 0;
if ($offset !== false) {
if ($offset > 0)
$offset = -$offset;
if (($w + $offset) <= $width)
$offset = $width - $w;
$x = $offset;
} else
$x = ($width - $w) / 2;
} else {
$w = $width;
$h = ($this->height * $w) / $this->width;
$x = 0;
if ($offset !== false) {
if ($offset > 0)
$offset = -$offset;
if (($h + $offset) <= $height)
$offset = $height - $h;
$y = $offset;
} else
$y = ($height - $h) / 2;
}
$x = round($x);
$y = round($y);
$w = round($w);
$h = round($h);
if (!$w) $w = 1;
if (!$h) $h = 1;
$return = (
(false !== ($img = new image_gd(array($width, $height))))) &&
(false !== ($img->imageCopyResampled($this->image, $x, $y, 0, 0, $w, $h))
);
if ($return) {
$this->image = $img->image;
$this->width = $w;
$this->height = $h;
}
return $return;
}
public function rotate($angle, $background="#000000") {
$angle = -$angle;
$img = @imagerotate($this->image, $angle, $this->gdColor($background));
if ($img === false)
return false;
$this->width = imagesx($img);
$this->height = imagesy($img);
$this->image = $img;
return true;
}
public function flipHorizontal() {
$img = imagecreatetruecolor($this->width, $this->height);
if (imagecopyresampled($img, $this->image, 0, 0, ($this->width - 1), 0, $this->width, $this->height, -$this->width, $this->height))
$this->image = $img;
else
return false;
return true;
}
public function flipVertical() {
$img = imagecreatetruecolor($this->width, $this->height);
if (imagecopyresampled($img, $this->image, 0, 0, 0, ($this->height - 1), $this->width, $this->height, $this->width, -$this->height))
$this->image = $img;
else
return false;
return true;
}
public function watermark($file, $left=false, $top=false) {
$info = getimagesize($file);
list($w, $h, $t) = $info;
if (!in_array($t, array(IMAGETYPE_PNG, IMAGETYPE_GIF)))
return false;
$imagecreate = ($t == IMAGETYPE_PNG) ? "imagecreatefrompng" : "imagecreatefromgif";
if (!@imagealphablending($this->image, true) ||
(false === ($wm = @$imagecreate($file)))
)
return false;
$w = imagesx($wm);
$h = imagesy($wm);
$x =
($left === true) ? 0 : (
($left === null) ? round(($this->width - $w) / 2) : (
(($left === false) || !preg_match('/^\d+$/', $left)) ? ($this->width - $w) : $left));
$y =
($top === true) ? 0 : (
($top === null) ? round(($this->height - $h) / 2) : (
(($top === false) || !preg_match('/^\d+$/', $top)) ? ($this->height - $h) : $top));
if ((($x + $w) > $this->width) ||
(($y + $h) > $this->height) ||
($x < 0) || ($y < 0)
)
return false;
if (($wm === false) || !@imagecopy($this->image, $wm, $x, $y, 0, 0, $w, $h))
return false;
@imagealphablending($this->image, false);
@imagesavealpha($this->image, true);
return true;
}
public function output($type='jpeg', array $options=array()) {
$method = "output_$type";
if (!method_exists($this, $method))
return false;
return $this->$method($options);
}
// ABSTRACT PROTECTED METHODS
protected function getBlankImage($width, $height) {
$image = imagecreatetruecolor($width, $height);
imagealphablending($image, false);
imagesavealpha($image, true);
return $image;
}
protected function getImage($image, &$width, &$height) {
if (is_resource($image) && (get_resource_type($image) == "gd")) {
$width = @imagesx($image);
$height = @imagesy($image);
imagealphablending($image, false);
imagesavealpha($image, true);
return $image;
} elseif (is_string($image) &&
(false !== (list($width, $height, $t) = @getimagesize($image)))
) {
$image =
($t == IMAGETYPE_GIF) ? @imagecreatefromgif($image) : (
($t == IMAGETYPE_WBMP) ? @imagecreatefromwbmp($image) : (
($t == IMAGETYPE_JPEG) ? @imagecreatefromjpeg($image) : (
($t == IMAGETYPE_PNG) ? @imagecreatefrompng($image) : (
($t == IMAGETYPE_XBM) ? @imagecreatefromxbm($image) : false
))));
if ($t == IMAGETYPE_PNG) {
imagealphablending($image, false);
imagesavealpha($image, true);
}
return $image;
} else
return false;
}
// PSEUDO-ABSTRACT STATIC METHODS
static function available() {
return function_exists("imagecreatefromjpeg");
}
static function checkImage($file) {
if (!is_string($file) ||
((false === (list($width, $height, $t) = @getimagesize($file))))
)
return false;
$img =
($t == IMAGETYPE_GIF) ? @imagecreatefromgif($file) : (
($t == IMAGETYPE_WBMP) ? @imagecreatefromwbmp($file) : (
($t == IMAGETYPE_JPEG) ? @imagecreatefromjpeg($file) : (
($t == IMAGETYPE_PNG) ? @imagecreatefrompng($file) : (
($t == IMAGETYPE_XBM) ? @imagecreatefromxbm($file) : false
))));
return ($img !== false);
}
// OWN METHODS
protected function output_png(array $options=array()) {
$file = isset($options['file']) ? $options['file'] : null;
$quality = isset($options['quality']) ? $options['quality'] : null;
$filters = isset($options['filters']) ? $options['filters'] : null;
if (($file === null) && !headers_sent())
header("Content-Type: image/png");
return imagepng($this->image, $file, $quality, $filters);
}
protected function output_jpeg(array $options=array()) {
$file = isset($options['file']) ? $options['file'] : null;
$quality = isset($options['quality'])
? $options['quality']
: self::DEFAULT_JPEG_QUALITY;
if (($file === null) && !headers_sent())
header("Content-Type: image/jpeg");
return imagejpeg($this->image, $file, $quality);
}
protected function output_gif(array $options=array()) {
$file = isset($options['file']) ? $options['file'] : null;
if (isset($options['file']) && !headers_sent())
header("Content-Type: image/gif");
return imagegif($this->image, $file);
}
protected function gdColor() {
$args = func_get_args();
$exprRGB = '/^rgb\(\s*(\d{1,3})\s*\,\s*(\d{1,3})\s*\,\s*(\d{1,3})\s*\)$/i';
$exprHex1 = '/^\#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i';
$exprHex2 = '/^\#?([0-9a-f])([0-9a-f])([0-9a-f])$/i';
$exprByte = '/^([01]?\d?\d|2[0-4]\d|25[0-5])$/';
if (!isset($args[0]))
return false;
if (count($args[0]) == 3) {
list($r, $g, $b) = $args[0];
} elseif (preg_match($exprRGB, $args[0], $match)) {
list($tmp, $r, $g, $b) = $match;
} elseif (preg_match($exprHex1, $args[0], $match)) {
list($tmp, $r, $g, $b) = $match;
$r = hexdec($r);
$g = hexdec($g);
$b = hexdec($b);
} elseif (preg_match($exprHex2, $args[0], $match)) {
list($tmp, $r, $g, $b) = $match;
$r = hexdec("$r$r");
$g = hexdec("$g$g");
$b = hexdec("$b$b");
} elseif ((count($args) == 3) &&
preg_match($exprByte, $args[0]) &&
preg_match($exprByte, $args[1]) &&
preg_match($exprByte, $args[2])
) {
list($r, $g, $b) = $args;
} else
return false;
return imagecolorallocate($this->image, $r, $g, $b);
}
protected function imageFilledRectangle($x1, $y1, $x2, $y2, $color) {
$color = $this->gdColor($color);
if ($color === false) return false;
return imageFilledRectangle($this->image, $x1, $y1, $x2, $y2, $color);
}
protected function imageCopyResampled(
$src, $dstX=0, $dstY=0, $srcX=0, $srcY=0, $dstW=null, $dstH=null, $srcW=null, $srcH=null
) {
$imageDetails = $this->buildImage($src);
if ($imageDetails === false)
return false;
list($src, $srcWidth, $srcHeight) = $imageDetails;
if (is_null($dstW)) $dstW = $this->width - $dstW;
if (is_null($dstH)) $dstH = $this->height - $dstY;
if (is_null($srcW)) $srcW = $srcWidth - $srcX;
if (is_null($srcH)) $srcH = $srcHeight - $srcY;
return imageCopyResampled($this->image, $src, $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH);
}
}
?>

View file

@ -0,0 +1,304 @@
<?php
/** This file is part of KCFinder project
*
* @desc GraphicsMagick image driver class
* @package KCFinder
* @version 3.12
* @author Pavel Tzonkov <sunhater@sunhater.com>
* @copyright 2010-2014 KCFinder Project
* @license http://opensource.org/licenses/GPL-3.0 GPLv3
* @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
* @link http://kcfinder.sunhater.com
*/
namespace kcfinder;
class image_gmagick extends image {
static $MIMES = array(
//'tif' => "image/tiff"
);
// ABSTRACT PUBLIC METHODS
public function resize($width, $height) {//
if (!$width) $width = 1;
if (!$height) $height = 1;
try {
$this->image->scaleImage($width, $height);
} catch (\Exception $e) {
return false;
}
$this->width = $width;
$this->height = $height;
return true;
}
public function resizeFit($width, $height, $background=false) {//
if (!$width) $width = 1;
if (!$height) $height = 1;
try {
$this->image->scaleImage($width, $height, true);
$w = $this->image->getImageWidth();
$h = $this->image->getImageHeight();
} catch (\Exception $e) {
return false;
}
if ($background === false) {
$this->width = $w;
$this->height = $h;
return true;
} else {
try {
$this->image->setImageBackgroundColor($background);
$x = round(($width - $w) / 2);
$y = round(($height - $h) / 2);
$img = new \Gmagick();
$img->newImage($width, $height, $background);
$img->compositeImage($this->image, 1, $x, $y);
} catch (\Exception $e) {
return false;
}
$this->image = $img;
$this->width = $width;
$this->height = $height;
return true;
}
}
public function resizeCrop($width, $height, $offset=false) {
if (!$width) $width = 1;
if (!$height) $height = 1;
if (($this->width / $this->height) > ($width / $height)) {
$h = $height;
$w = ($this->width * $h) / $this->height;
$y = 0;
if ($offset !== false) {
if ($offset > 0)
$offset = -$offset;
if (($w + $offset) <= $width)
$offset = $width - $w;
$x = $offset;
} else
$x = ($width - $w) / 2;
} else {
$w = $width;
$h = ($this->height * $w) / $this->width;
$x = 0;
if ($offset !== false) {
if ($offset > 0)
$offset = -$offset;
if (($h + $offset) <= $height)
$offset = $height - $h;
$y = $offset;
} else
$y = ($height - $h) / 2;
}
$x = round($x);
$y = round($y);
$w = round($w);
$h = round($h);
if (!$w) $w = 1;
if (!$h) $h = 1;
try {
$this->image->scaleImage($w, $h);
$this->image->cropImage($width, $height, -$x, -$y);
} catch (\Exception $e) {
return false;
}
$this->width = $width;
$this->height = $height;
return true;
}
public function rotate($angle, $background="#000000") {
try {
$this->image->rotateImage($background, $angle);
$w = $this->image->getImageWidth();
$h = $this->image->getImageHeight();
} catch (\Exception $e) {
return false;
}
$this->width = $w;
$this->height = $h;
return true;
}
public function flipHorizontal() {
try {
$this->image->flopImage();
} catch (\Exception $e) {
return false;
}
return true;
}
public function flipVertical() {
try {
$this->image->flipImage();
} catch (\Exception $e) {
return false;
}
return true;
}
public function watermark($file, $left=false, $top=false) {
try {
$wm = new \Gmagick($file);
$w = $wm->getImageWidth();
$h = $wm->getImageHeight();
} catch (\Exception $e) {
return false;
}
$x =
($left === true) ? 0 : (
($left === null) ? round(($this->width - $w) / 2) : (
(($left === false) || !preg_match('/^\d+$/', $left)) ? ($this->width - $w) : $left));
$y =
($top === true) ? 0 : (
($top === null) ? round(($this->height - $h) / 2) : (
(($top === false) || !preg_match('/^\d+$/', $top)) ? ($this->height - $h) : $top));
if ((($x + $w) > $this->width) ||
(($y + $h) > $this->height) ||
($x < 0) || ($y < 0)
)
return false;
try {
$this->image->compositeImage($wm, 1, $x, $y);
} catch (\Exception $e) {
return false;
}
return true;
}
// ABSTRACT PROTECTED METHODS
protected function getBlankImage($width, $height) {
try {
$img = new \Gmagick();
$img->newImage($width, $height, "none");
} catch (\Exception $e) {
return false;
}
return $img;
}
protected function getImage($image, &$width, &$height) {
if (is_object($image) && ($image instanceof image_gmagick)) {
$width = $image->width;
$height = $image->height;
return $image->image;
} elseif (is_object($image) && ($image instanceof \Gmagick)) {
try {
$w = $image->getImageWidth();
$h = $image->getImageHeight();
} catch (\Exception $e) {
return false;
}
$width = $w;
$height = $h;
return $image;
} elseif (is_string($image)) {
try {
$image = new \Gmagick($image);
$w = $image->getImageWidth();
$h = $image->getImageHeight();
} catch (\Exception $e) {
return false;
}
$width = $w;
$height = $h;
return $image;
} else
return false;
}
// PSEUDO-ABSTRACT STATIC METHODS
static function available() {
return class_exists("Gmagick");
}
static function checkImage($file) {
try {
$img = new \Gmagick($file);
} catch (\Exception $e) {
return false;
}
return true;
}
// INHERIT METHODS
public function output($type="jpeg", array $options=array()) {
$type = strtolower($type);
try {
$this->image->setImageFormat($type);
} catch (\Exception $e) {
return false;
}
$method = "optimize_$type";
if (method_exists($this, $method) && !$this->$method($options))
return false;
if (!isset($options['file'])) {
if (!headers_sent()) {
$mime = isset(self::$MIMES[$type]) ? self::$MIMES[$type] : "image/$type";
header("Content-Type: $mime");
}
echo $this->image;
} else {
$file = $options['file'] . ".$type";
try {
$this->image->writeImage($file);
} catch (\Exception $e) {
@unlink($file);
return false;
}
if (!@rename($file, $options['file'])) {
@unlink($file);
return false;
}
}
return true;
}
// OWN METHODS
protected function optimize_jpeg(array $options=array()) {
$quality = isset($options['quality']) ? $options['quality'] : self::DEFAULT_JPEG_QUALITY;
try {
$this->image->setCompressionQuality($quality);
} catch (\Exception $e) {
return false;
}
return true;
}
}
?>

View file

@ -0,0 +1,307 @@
<?php
/** This file is part of KCFinder project
*
* @desc ImageMagick image driver class
* @package KCFinder
* @version 3.12
* @author Pavel Tzonkov <sunhater@sunhater.com>
* @copyright 2010-2014 KCFinder Project
* @license http://opensource.org/licenses/GPL-3.0 GPLv3
* @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
* @link http://kcfinder.sunhater.com
*/
namespace kcfinder;
class image_imagick extends image {
static $MIMES = array(
//'tif' => "image/tiff"
);
// ABSTRACT PUBLIC METHODS
public function resize($width, $height) {//
if (!$width) $width = 1;
if (!$height) $height = 1;
try {
$this->image->scaleImage($width, $height);
} catch (\Exception $e) {
return false;
}
$this->width = $width;
$this->height = $height;
return true;
}
public function resizeFit($width, $height, $background=false) {//
if (!$width) $width = 1;
if (!$height) $height = 1;
try {
$this->image->scaleImage($width, $height, true);
$size = $this->image->getImageGeometry();
} catch (\Exception $e) {
return false;
}
if ($background === false) {
$this->width = $size['width'];
$this->height = $size['height'];
return true;
} else {
try {
$this->image->setImageBackgroundColor($background);
$x = -round(($width - $size['width']) / 2);
$y = -round(($height - $size['height']) / 2);
$this->image->extentImage($width, $height, $x, $y);
} catch (\Exception $e) {
return false;
}
$this->width = $width;
$this->height = $height;
return true;
}
}
public function resizeCrop($width, $height, $offset=false) {
if (!$width) $width = 1;
if (!$height) $height = 1;
if (($this->width / $this->height) > ($width / $height)) {
$h = $height;
$w = ($this->width * $h) / $this->height;
$y = 0;
if ($offset !== false) {
if ($offset > 0)
$offset = -$offset;
if (($w + $offset) <= $width)
$offset = $width - $w;
$x = $offset;
} else
$x = ($width - $w) / 2;
} else {
$w = $width;
$h = ($this->height * $w) / $this->width;
$x = 0;
if ($offset !== false) {
if ($offset > 0)
$offset = -$offset;
if (($h + $offset) <= $height)
$offset = $height - $h;
$y = $offset;
} else
$y = ($height - $h) / 2;
}
$x = round($x);
$y = round($y);
$w = round($w);
$h = round($h);
if (!$w) $w = 1;
if (!$h) $h = 1;
try {
$this->image->scaleImage($w, $h);
$this->image->cropImage($width, $height, -$x, -$y);
} catch (\Exception $e) {
return false;
}
$this->width = $width;
$this->height = $height;
return true;
}
public function rotate($angle, $background="#000000") {
try {
$this->image->rotateImage(new \ImagickPixel($background), $angle);
$size = $this->image->getImageGeometry();
} catch (\Exception $e) {
return false;
}
$this->width = $size['width'];
$this->height = $size['height'];
return true;
}
public function flipHorizontal() {
try {
$this->image->flopImage();
} catch (\Exception $e) {
return false;
}
return true;
}
public function flipVertical() {
try {
$this->image->flipImage();
} catch (\Exception $e) {
return false;
}
return true;
}
public function watermark($file, $left=false, $top=false) {
try {
$wm = new \Imagick($file);
$size = $wm->getImageGeometry();
} catch (\Exception $e) {
return false;
}
$w = $size['width'];
$h = $size['height'];
$x =
($left === true) ? 0 : (
($left === null) ? round(($this->width - $w) / 2) : (
(($left === false) || !preg_match('/^\d+$/', $left)) ? ($this->width - $w) : $left));
$y =
($top === true) ? 0 : (
($top === null) ? round(($this->height - $h) / 2) : (
(($top === false) || !preg_match('/^\d+$/', $top)) ? ($this->height - $h) : $top));
if ((($x + $w) > $this->width) ||
(($y + $h) > $this->height) ||
($x < 0) || ($y < 0)
)
return false;
try {
$this->image->compositeImage($wm, \Imagick::COMPOSITE_DEFAULT, $x, $y);
} catch (\Exception $e) {
return false;
}
return true;
}
// ABSTRACT PROTECTED METHODS
protected function getBlankImage($width, $height) {
try {
$img = new \Imagick();
$img->newImage($width, $height, "none");
$img->setImageCompressionQuality(100);
} catch (\Exception $e) {
return false;
}
return $img;
}
protected function getImage($image, &$width, &$height) {
if (is_object($image) && ($image instanceof image_imagick)) {
try {
$image->image->setImageCompressionQuality(100);
} catch (\Exception $e) {
return false;
}
$width = $image->width;
$height = $image->height;
return $image->image;
} elseif (is_object($image) && ($image instanceof \Imagick)) {
try {
$image->setImageCompressionQuality(100);
$size = $image->getImageGeometry();
} catch (\Exception $e) {
return false;
}
$width = $size['width'];
$height = $size['height'];
return $image;
} elseif (is_string($image)) {
try {
$image = new \Imagick($image);
$image->setImageCompressionQuality(100);
$size = $image->getImageGeometry();
} catch (\Exception $e) {
return false;
}
$width = $size['width'];
$height = $size['height'];
return $image;
} else
return false;
}
// PSEUDO-ABSTRACT STATIC METHODS
static function available() {
return class_exists("\\Imagick");
}
static function checkImage($file) {
try {
$img = new \Imagick($file);
} catch (\Exception $e) {
return false;
}
return true;
}
// INHERIT METHODS
public function output($type="jpeg", array $options=array()) {
$type = strtolower($type);
try {
$this->image->setImageFormat($type);
} catch (\Exception $e) {
return false;
}
$method = "optimize_$type";
if (method_exists($this, $method) && !$this->$method($options))
return false;
if (!isset($options['file'])) {
if (!headers_sent()) {
$mime = isset(self::$MIMES[$type]) ? self::$MIMES[$type] : "image/$type";
header("Content-Type: $mime");
}
echo $this->image;
} else {
$file = $options['file'] . ".$type";
try {
$this->image->writeImage($file);
} catch (\Exception $e) {
@unlink($file);
return false;
}
if (!@rename($file, $options['file'])) {
@unlink($file);
return false;
}
}
return true;
}
// OWN METHODS
protected function optimize_jpeg(array $options=array()) {
$quality = isset($options['quality']) ? $options['quality'] : self::DEFAULT_JPEG_QUALITY;
try {
$this->image->setImageCompression(\Imagick::COMPRESSION_JPEG);
$this->image->setImageCompressionQuality($quality);
} catch (\Exception $e) {
return false;
}
return true;
}
}
?>

View file

@ -0,0 +1,62 @@
<?php
/** This file is part of KCFinder project. The class are taken from
* http://www.php.net/manual/en/function.ziparchive-addemptydir.php
*
* @desc Directory to ZIP file archivator
* @package KCFinder
* @version 3.12
* @author Pavel Tzonkov <sunhater@sunhater.com>
* @copyright 2010-2014 KCFinder Project
* @license http://opensource.org/licenses/GPL-3.0 GPLv3
* @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
* @link http://kcfinder.sunhater.com
*/
namespace kcfinder;
class zipFolder {
protected $zip;
protected $root;
protected $ignored;
function __construct($file, $folder, $ignored=null) {
$this->zip = new \ZipArchive();
$this->ignored = is_array($ignored)
? $ignored
: ($ignored ? array($ignored) : array());
if ($this->zip->open($file, \ZipArchive::CREATE) !== TRUE)
throw new \Exception("cannot open <$file>\n");
$folder = rtrim($folder, '/');
if (strstr($folder, '/')) {
$this->root = substr($folder, 0, strrpos($folder, '/') + 1);
$folder = substr($folder, strrpos($folder, '/') + 1);
}
$this->zip($folder);
$this->zip->close();
}
function zip($folder, $parent=null) {
$full_path = "{$this->root}$parent$folder";
$zip_path = "$parent$folder";
$this->zip->addEmptyDir($zip_path);
$dir = new \DirectoryIterator($full_path);
foreach ($dir as $file)
if (!$file->isDot()) {
$filename = $file->getFilename();
if (!in_array($filename, $this->ignored)) {
if ($file->isDir())
$this->zip($filename, "$zip_path/");
else
$this->zip->addFile("$full_path/$filename", "$zip_path/$filename");
}
}
}
}
?>

View file

@ -0,0 +1,158 @@
<?php
/** This file is part of KCFinder project
*
* @desc Directory helper class
* @package KCFinder
* @version 3.12
* @author Pavel Tzonkov <sunhater@sunhater.com>
* @copyright 2010-2014 KCFinder Project
* @license http://opensource.org/licenses/GPL-3.0 GPLv3
* @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
* @link http://kcfinder.sunhater.com
*/
namespace kcfinder;
class dir {
/** Checks if the given directory is really writable. The standard PHP
* function is_writable() does not work properly on Windows servers
* @param string $dir
* @return bool */
static function isWritable($dir) {
$dir = path::normalize($dir);
if (!is_dir($dir))
return false;
$i = 0;
do {
$file = "$dir/is_writable_" . md5($i++);
} while (file_exists($file));
if (!@touch($file))
return false;
unlink($file);
return true;
}
/** Recursively delete the given directory. Returns TRUE on success.
* If $firstFailExit parameter is true (default), the method returns the
* path to the first failed file or directory which cannot be deleted.
* If $firstFailExit is false, the method returns an array with failed
* files and directories which cannot be deleted. The third parameter
* $failed is used for internal use only.
* @param string $dir
* @param bool $firstFailExit
* @param array $failed
* @return mixed */
static function prune($dir, $firstFailExit=true, array $failed=null) {
if ($failed === null) $failed = array();
$files = self::content($dir);
if ($files === false) {
if ($firstFailExit)
return $dir;
$failed[] = $dir;
return $failed;
}
foreach ($files as $file) {
if (is_dir($file)) {
$failed_in = self::prune($file, $firstFailExit, $failed);
if ($failed_in !== true) {
if ($firstFailExit)
return $failed_in;
if (is_array($failed_in))
$failed = array_merge($failed, $failed_in);
else
$failed[] = $failed_in;
}
} elseif (!@unlink($file)) {
if ($firstFailExit)
return $file;
$failed[] = $file;
}
}
if (!@rmdir($dir)) {
if ($firstFailExit)
return $dir;
$failed[] = $dir;
}
return count($failed) ? $failed : true;
}
/** Get the content of the given directory. Returns an array with filenames
* or FALSE on failure
* @param string $dir
* @param array $options
* @return mixed */
static function content($dir, array $options=null) {
$defaultOptions = array(
'types' => "all", // Allowed: "all" or possible return values
// of filetype(), or an array with them
'addPath' => true, // Whether to add directory path to filenames
'pattern' => '/./', // Regular expression pattern for filename
'followLinks' => true
);
if (!is_dir($dir) || !is_readable($dir))
return false;
if (strtoupper(substr(PHP_OS, 0, 3)) == "WIN")
$dir = str_replace("\\", "/", $dir);
$dir = rtrim($dir, "/");
$dh = @opendir($dir);
if ($dh === false)
return false;
if ($options === null)
$options = $defaultOptions;
foreach ($defaultOptions as $key => $val)
if (!isset($options[$key]))
$options[$key] = $val;
$files = array();
while (($file = @readdir($dh)) !== false) {
if (($file == '.') || ($file == '..') ||
!preg_match($options['pattern'], $file)
)
continue;
$fullpath = "$dir/$file";
$type = filetype($fullpath);
// If file is a symlink, get the true type of its destination
if ($options['followLinks'] && ($type == "link"))
$type = filetype(realpath($fullpath));
if (($options['types'] === "all") || ($type === $options['types']) ||
(is_array($options['types']) && in_array($type, $options['types']))
)
$files[] = $options['addPath'] ? $fullpath : $file;
}
closedir($dh);
usort($files, array(__NAMESPACE__ . "\\dir", "fileSort"));
return $files;
}
static function fileSort($a, $b) {
if (function_exists("mb_strtolower")) {
$a = mb_strtolower($a);
$b = mb_strtolower($b);
} else {
$a = strtolower($a);
$b = strtolower($b);
}
if ($a == $b) return 0;
return ($a < $b) ? -1 : 1;
}
}
?>

View file

@ -0,0 +1,216 @@
<?php
/** This file is part of KCFinder project
*
* @desc File helper class
* @package KCFinder
* @version 3.12
* @author Pavel Tzonkov <sunhater@sunhater.com>
* @copyright 2010-2014 KCFinder Project
* @license http://opensource.org/licenses/GPL-3.0 GPLv3
* @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
* @link http://kcfinder.sunhater.com
*/
namespace kcfinder;
class file {
static $MIME = array(
'ai' => 'application/postscript',
'aif' => 'audio/x-aiff',
'aifc' => 'audio/x-aiff',
'aiff' => 'audio/x-aiff',
'avi' => 'video/x-msvideo',
'bin' => 'application/macbinary',
'bmp' => 'image/bmp',
'cpt' => 'application/mac-compactpro',
'css' => 'text/css',
'csv' => 'text/x-comma-separated-values',
'dcr' => 'application/x-director',
'dir' => 'application/x-director',
'doc' => 'application/msword',
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'dvi' => 'application/x-dvi',
'dxr' => 'application/x-director',
'eml' => 'message/rfc822',
'eps' => 'application/postscript',
'flv' => 'video/x-flv',
'gif' => 'image/gif',
'gtar' => 'application/x-gtar',
'gz' => 'application/x-gzip',
'hqx' => 'application/mac-binhex40',
'htm' => 'text/html',
'html' => 'text/html',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'js' => 'application/x-javascript',
'log' => 'text/plain',
'mid' => 'audio/midi',
'midi' => 'audio/midi',
'mif' => 'application/vnd.mif',
'mov' => 'video/quicktime',
'movie' => 'video/x-sgi-movie',
'mp2' => 'audio/mpeg',
'mp3' => 'audio/mpeg',
'mp4' => 'video/mpeg',
'mpe' => 'video/mpeg',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpga' => 'audio/mpeg',
'oda' => 'application/oda',
'pdf' => 'application/pdf',
'php' => 'application/x-httpd-php',
'php3' => 'application/x-httpd-php',
'php4' => 'application/x-httpd-php',
'phps' => 'application/x-httpd-php-source',
'phtml' => 'application/x-httpd-php',
'png' => 'image/png',
'ppt' => 'application/powerpoint',
'ps' => 'application/postscript',
'psd' => 'application/x-photoshop',
'qt' => 'video/quicktime',
'ra' => 'audio/x-realaudio',
'ram' => 'audio/x-pn-realaudio',
'rm' => 'audio/x-pn-realaudio',
'rpm' => 'audio/x-pn-realaudio-plugin',
'rtf' => 'text/rtf',
'rtx' => 'text/richtext',
'rv' => 'video/vnd.rn-realvideo',
'shtml' => 'text/html',
'sit' => 'application/x-stuffit',
'smi' => 'application/smil',
'smil' => 'application/smil',
'swf' => 'application/x-shockwave-flash',
'tar' => 'application/x-tar',
'tgz' => 'application/x-tar',
'text' => 'text/plain',
'tif' => 'image/tiff',
'tiff' => 'image/tiff',
'txt' => 'text/plain',
'wav' => 'audio/x-wav',
'wbxml' => 'application/wbxml',
'wmlc' => 'application/wmlc',
'word' => 'application/msword',
'xht' => 'application/xhtml+xml',
'xhtml' => 'application/xhtml+xml',
'xl' => 'application/excel',
'xls' => 'application/excel',
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'xml' => 'text/xml',
'xsl' => 'text/xml',
'zip' => 'application/x-zip'
);
/** Checks if the given file is really writable. The standard PHP function
* is_writable() does not work properly on Windows servers.
* @param string $filename
* @return bool */
static function isWritable($filename) {
$filename = path::normalize($filename);
if (!is_file($filename) || (false === ($fp = @fopen($filename, 'a+'))))
return false;
fclose($fp);
return true;
}
/** Get the extension from filename
* @param string $filename
* @param bool $toLower
* @return string */
static function getExtension($filename, $toLower=true) {
return preg_match('/^.*\.([^\.]*)$/s', $filename, $patt)
? ($toLower ? strtolower($patt[1]) : $patt[1]) : "";
}
/** Get MIME type of the given filename. If Fileinfo PHP extension is
* available the MIME type will be fetched by the file's content. The
* second parameter is optional and defines the magic file path. If you
* skip it, the default one will be loaded.
* If Fileinfo PHP extension is not available the MIME type will be fetched
* by filename extension regarding $MIME property. If the file extension
* does not exist there, returned type will be application/octet-stream
* @param string $filename
* @param string $magic
* @return string */
static function getMimeType($filename, $magic=null) {
if (class_exists("finfo")) {
$finfo = new \finfo(FILEINFO_MIME, $magic);
if ($finfo) {
$mime = $finfo->file($filename);
$mime = substr($mime, 0, strrpos($mime, ";"));
return $mime;
}
}
$ext = self::getExtension($filename, true);
return isset(self::$MIME[$ext]) ? self::$MIME[$ext] : "application/octet-stream";
}
/** Get inexistant filename based on the given filename. If you skip $dir
* parameter the directory will be fetched from $filename and returned
* value will be full filename path. The third parameter is optional and
* defines the template, the filename will be renamed to. Default template
* is {name}({sufix}){ext}. Examples:
*
* file::getInexistantFilename("/my/directory/myfile.txt");
* If myfile.txt does not exist - returns the same path to the file
* otherwise returns "/my/directory/myfile(1).txt"
*
* file::getInexistantFilename("myfile.txt", "/my/directory");
* returns "myfile.txt" or "myfile(1).txt" or "myfile(2).txt" etc...
*
* file::getInexistantFilename("myfile.txt", "/dir", "{name}[{sufix}]{ext}");
* returns "myfile.txt" or "myfile[1].txt" or "myfile[2].txt" etc...
*
* @param string $filename
* @param string $dir
* @param string $tpl
* @return string */
static function getInexistantFilename($filename, $dir=null, $tpl=null) {
if ($tpl === null) $tpl = "{name}({sufix}){ext}";
$fullPath = ($dir === null);
if ($fullPath)
$dir = path::normalize(dirname($filename));
else {
$fdir = dirname($filename);
$dir = strlen($fdir)
? path::normalize("$dir/$fdir")
: path::normalize($dir);
}
$filename = basename($filename);
$ext = self::getExtension($filename, false);
$name = strlen($ext) ? substr($filename, 0, -strlen($ext) - 1) : $filename;
$tpl = str_replace('{name}', $name, $tpl);
$tpl = str_replace('{ext}', (strlen($ext) ? ".$ext" : ""), $tpl);
$i = 1; $file = "$dir/$filename";
while (file_exists($file))
$file = "$dir/" . str_replace('{sufix}', $i++, $tpl);
return $fullPath
? $file
: (strlen($fdir)
? "$fdir/" . basename($file)
: basename($file));
}
/** Normalize given filename. Accented characters becomes non-accented and
* removes any other special characters. Usable for non-unicode filesystems
* @param $filename
* @return string */
static function normalizeFilename($filename) {
$string = htmlentities($filename, ENT_QUOTES, 'UTF-8');
if (strpos($string, '&') !== false)
$filename = html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|tilde|uml);~i', '$1', $string), ENT_QUOTES, 'UTF-8');
$filename = trim(preg_replace('~[^0-9a-z\.\- ]~i', "_", $filename));
return $filename;
}
}
?>

View file

@ -0,0 +1,100 @@
<?php
/** This file is part of KCFinder project
*
* @desc HTTP cache helper class
* @package KCFinder
* @version 3.12
* @author Pavel Tzonkov <sunhater@sunhater.com>
* @copyright 2010-2014 KCFinder Project
* @license http://opensource.org/licenses/GPL-3.0 GPLv3
* @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
* @link http://kcfinder.sunhater.com
*/
namespace kcfinder;
class httpCache {
const DEFAULT_TYPE = "text/html";
const DEFAULT_EXPIRE = 604800; // in seconds
/** Cache a file. The $type parameter might define the MIME type of the file
* or path to magic file to autodetect the MIME type. If you skip $type
* parameter the method will try with the default magic file. Autodetection
* of MIME type requires Fileinfo PHP extension used in file::getMimeType()
* @param string $file
* @param string $type
* @param integer $expire
* @param array $headers */
static function file($file, $type=null, $expire=null, array $headers=null) {
$mtime = @filemtime($file);
if ($mtime !== false) self::checkMTime($mtime);
if ($type === null) {
$magic = ((substr($type, 0, 1) == "/") || preg_match('/^[a-z]\:/i', $type))
? $type : null;
$type = file::getMimeType($file, $magic);
if (!$type) $type = null;
}
self::content(@file_get_contents($file), $mtime, $type, $expire, $headers, false);
}
/** Cache the given $content with $mtime modification time.
* @param binary $content
* @param integer $mtime
* @param string $type
* @param integer $expire
* @param array $headers
* @param bool $checkMTime */
static function content($content, $mtime, $type=null, $expire=null, array $headers=null, $checkMTime=true) {
if ($checkMTime) self::checkMTime($mtime);
if ($type === null) $type = self::DEFAULT_TYPE;
if ($expire === null) $expire = self::DEFAULT_EXPIRE;
$size = strlen($content);
$expires = gmdate("D, d M Y H:i:s", time() + $expire) . " GMT";
header("Content-Type: $type");
header("Expires: $expires");
header("Cache-Control: max-age=$expire");
header("Pragma: !invalid");
header("Content-Length: $size");
if ($headers !== null) foreach ($headers as $header) header($header);
echo $content;
}
/** Check if given modification time is newer than client-side one. If not,
* the method will tell the client to get the content from its own cache.
* Afterwards the script process will be terminated. This feature requires
* the PHP to be configured as Apache module.
* @param integer $mtime
* @param mixed $sendHeaders */
static function checkMTime($mtime, $sendHeaders=null) {
header("Last-Modified: " . gmdate("D, d M Y H:i:s", $mtime) . " GMT");
$headers = function_exists("getallheaders")
? getallheaders()
: (function_exists("apache_request_headers")
? apache_request_headers()
: false);
if (is_array($headers) && isset($headers['If-Modified-Since'])) {
$client_mtime = explode(';', $headers['If-Modified-Since']);
$client_mtime = @strtotime($client_mtime[0]);
if ($client_mtime >= $mtime) {
header('HTTP/1.1 304 Not Modified');
if (is_array($sendHeaders) && count($sendHeaders))
foreach ($sendHeaders as $header)
header($header);
elseif ($sendHeaders !== null)
header($sendHeaders);
die;
}
}
}
}
?>

View file

@ -0,0 +1,149 @@
<?php
/** This file is part of KCFinder project
*
* @desc Path helper class
* @package KCFinder
* @version 3.12
* @author Pavel Tzonkov <sunhater@sunhater.com>
* @copyright 2010-2014 KCFinder Project
* @license http://opensource.org/licenses/GPL-3.0 GPLv3
* @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
* @link http://kcfinder.sunhater.com
*/
namespace kcfinder;
class path {
/** Get the absolute URL path of the given one. Returns FALSE if the URL
* is not valid or the current directory cannot be resolved (getcwd())
* @param string $path
* @return string */
static function rel2abs_url($path) {
if (substr($path, 0, 1) == "/") return $path;
$dir = @getcwd();
if (!isset($_SERVER['DOCUMENT_ROOT']) || ($dir === false))
return false;
$dir = self::normalize($dir);
$doc_root = self::normalize($_SERVER['DOCUMENT_ROOT']);
if (substr($dir, 0, strlen($doc_root)) != $doc_root)
return false;
$return = self::normalize(substr($dir, strlen($doc_root)) . "/$path");
if (substr($return, 0, 1) !== "/")
$return = "/$return";
return $return;
}
/** Resolve full filesystem path of given URL. Returns FALSE if the URL
* cannot be resolved
* @param string $url
* @return string */
static function url2fullPath($url) {
$url = self::normalize($url);
$uri = isset($_SERVER['SCRIPT_NAME'])
? $_SERVER['SCRIPT_NAME'] : (isset($_SERVER['PHP_SELF'])
? $_SERVER['PHP_SELF']
: false);
$uri = self::normalize($uri);
if (substr($url, 0, 1) !== "/") {
if ($uri === false) return false;
$url = dirname($uri) . "/$url";
}
if (isset($_SERVER['DOCUMENT_ROOT'])) {
return self::normalize($_SERVER['DOCUMENT_ROOT'] . "/$url");
} else {
if ($uri === false) return false;
if (isset($_SERVER['SCRIPT_FILENAME'])) {
$scr_filename = self::normalize($_SERVER['SCRIPT_FILENAME']);
return self::normalize(substr($scr_filename, 0, -strlen($uri)) . "/$url");
}
$count = count(explode('/', $uri)) - 1;
for ($i = 0, $chdir = ""; $i < $count; $i++)
$chdir .= "../";
$chdir = self::normalize($chdir);
$dir = getcwd();
if (($dir === false) || !@chdir($chdir))
return false;
$rdir = getcwd();
chdir($dir);
return ($rdir !== false) ? self::normalize($rdir . "/$url") : false;
}
}
/** Normalize the given path. On Windows servers backslash will be replaced
* with slash. Removes unnecessary double slashes and double dots. Removes
* last slash if it exists. Examples:
* path::normalize("C:\\any\\path\\") returns "C:/any/path"
* path::normalize("/your/path/..//home/") returns "/your/home"
* @param string $path
* @return string */
static function normalize($path) {
// Backslash to slash convert
if (strtoupper(substr(PHP_OS, 0, 3)) == "WIN") {
$path = preg_replace('/([^\\\])\\\+([^\\\])/s', "$1/$2", $path);
if (substr($path, -1) == "\\") $path = substr($path, 0, -1);
if (substr($path, 0, 1) == "\\") $path = "/" . substr($path, 1);
}
$path = preg_replace('/\/+/s', "/", $path);
$path = "/$path";
if (substr($path, -1) != "/")
$path .= "/";
$expr = '/\/([^\/]{1}|[^\.\/]{2}|[^\/]{3,})\/\.\.\//s';
while (preg_match($expr, $path))
$path = preg_replace($expr, "/", $path);
$path = substr($path, 0, -1);
$path = substr($path, 1);
return $path;
}
/** Encode URL Path
* @param string $path
* @return string */
static function urlPathEncode($path) {
$path = self::normalize($path);
$encoded = "";
foreach (explode("/", $path) as $dir)
$encoded .= rawurlencode($dir) . "/";
$encoded = substr($encoded, 0, -1);
return $encoded;
}
/** Decode URL Path
* @param string $path
* @return string */
static function urlPathDecode($path) {
$path = self::normalize($path);
$decoded = "";
foreach (explode("/", $path) as $dir)
$decoded .= rawurldecode($dir) . "/";
$decoded = substr($decoded, 0, -1);
return $decoded;
}
}
?>

View file

@ -0,0 +1,55 @@
<?php
/** This file is part of KCFinder project
*
* @desc Text processing helper class
* @package KCFinder
* @version 3.12
* @author Pavel Tzonkov <sunhater@sunhater.com>
* @copyright 2010-2014 KCFinder Project
* @license http://opensource.org/licenses/GPL-3.0 GPLv3
* @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
* @link http://kcfinder.sunhater.com
*/
namespace kcfinder;
class text {
/** Replace repeated white spaces to single space
* @param string $string
* @return string */
static function clearWhitespaces($string) {
return trim(preg_replace('/\s+/s', " ", $string));
}
/** Normalize the string for HTML attribute value
* @param string $string
* @return string */
static function htmlValue($string) {
return
str_replace('"', "&quot;",
str_replace("'", '&#39;',
str_replace('<', '&lt;',
str_replace('&', "&amp;",
$string))));
}
/** Normalize the string for JavaScript string value
* @param string $string
* @return string */
static function jsValue($string) {
return
preg_replace('/\r?\n/', "\\n",
str_replace('"', "\\\"",
str_replace("'", "\\'",
str_replace("\\", "\\\\",
$string))));
}
}
?>