tss
parent
cb83b2b98c
commit
ec8469333e
|
@ -16,9 +16,12 @@
|
||||||
--------------------------------------------------------------------- */
|
--------------------------------------------------------------------- */
|
||||||
|
|
||||||
namespace CHV;
|
namespace CHV;
|
||||||
use G, Exception;
|
|
||||||
|
|
||||||
class Upload {
|
use G;
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
class Upload
|
||||||
|
{
|
||||||
// filename => name.ext
|
// filename => name.ext
|
||||||
// file => /full/path/to/name.ext
|
// file => /full/path/to/name.ext
|
||||||
// name => name
|
// name => name
|
||||||
|
@ -27,47 +30,55 @@ class Upload {
|
||||||
public $uploaded;
|
public $uploaded;
|
||||||
|
|
||||||
// Sets the type of resource being uploaded
|
// Sets the type of resource being uploaded
|
||||||
public function setType($type) {
|
public function setType($type)
|
||||||
|
{
|
||||||
$this->type = $type;
|
$this->type = $type;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set source
|
// Set source
|
||||||
public function setSource($source) {
|
public function setSource($source)
|
||||||
|
{
|
||||||
$this->source = $source;
|
$this->source = $source;
|
||||||
$this->type = G\is_url($this->source) ? 'url' : 'file';
|
$this->type = G\is_url($this->source) ? 'url' : 'file';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set destination
|
// Set destination
|
||||||
public function setDestination($destination) {
|
public function setDestination($destination)
|
||||||
|
{
|
||||||
$this->destination = G\forward_slash($destination);
|
$this->destination = G\forward_slash($destination);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set storage
|
// Set storage
|
||||||
public function setStorageId($storage_id) {
|
public function setStorageId($storage_id)
|
||||||
$this->storage_id = is_numeric($storage_id) ? $storage_id : NULL;
|
{
|
||||||
|
$this->storage_id = is_numeric($storage_id) ? $storage_id : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set file basename
|
// Set file basename
|
||||||
public function setFilename($name) {
|
public function setFilename($name)
|
||||||
|
{
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set options
|
// Set options
|
||||||
public function setOptions($options) {
|
public function setOptions($options)
|
||||||
|
{
|
||||||
$this->options = $options;
|
$this->options = $options;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set individual option
|
// Set individual option
|
||||||
public function setOption($key, $value) {
|
public function setOption($key, $value)
|
||||||
|
{
|
||||||
$this->options[$key] = $value;
|
$this->options[$key] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default options
|
// Default options
|
||||||
public static function getDefaultOptions() {
|
public static function getDefaultOptions()
|
||||||
|
{
|
||||||
return array(
|
return array(
|
||||||
'max_size' => G\get_bytes('2 MB'), // it should be 'max_filesize'
|
'max_size' => G\get_bytes('2 MB'), // it should be 'max_filesize'
|
||||||
'filenaming' => 'original',
|
'filenaming' => 'original',
|
||||||
'exif' => TRUE,
|
'exif' => true,
|
||||||
'allowed_formats' => self::getAvailableImageFormats(), // array
|
'allowed_formats' => self::getAvailableImageFormats(), // array
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -76,7 +87,8 @@ class Upload {
|
||||||
* Do the thing
|
* Do the thing
|
||||||
* @Exeption 4xx
|
* @Exeption 4xx
|
||||||
*/
|
*/
|
||||||
public function exec() {
|
public function exec()
|
||||||
|
{
|
||||||
|
|
||||||
// Merge options
|
// Merge options
|
||||||
$this->options = array_merge(self::getDefaultOptions(), (array) $this->options);
|
$this->options = array_merge(self::getDefaultOptions(), (array) $this->options);
|
||||||
|
@ -92,7 +104,7 @@ class Upload {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save the source name
|
// Save the source name
|
||||||
$this->source_name = G\get_filename_without_extension($this->type == "url" ? $this->source : $this->source["name"]);
|
$this->source_name = G\get_filename_without_extension($this->type == "url" ? $this->getNameFromURL($this->source) : $this->source["name"]);
|
||||||
|
|
||||||
// Set file extension
|
// Set file extension
|
||||||
$this->extension = $this->source_image_fileinfo["extension"];
|
$this->extension = $this->source_image_fileinfo["extension"];
|
||||||
|
@ -115,7 +127,7 @@ class Upload {
|
||||||
|
|
||||||
// Workaround for JPEG Exif data
|
// Workaround for JPEG Exif data
|
||||||
if ($this->extension == 'jpg' and array_key_exists('exif', $this->options)) {
|
if ($this->extension == 'jpg' and array_key_exists('exif', $this->options)) {
|
||||||
$this->source_image_exif = NULL;
|
$this->source_image_exif = null;
|
||||||
if ($this->options['exif']) {
|
if ($this->options['exif']) {
|
||||||
// Fetch JPEG Exif data (when available)
|
// Fetch JPEG Exif data (when available)
|
||||||
if (function_exists('exif_read_data')) {
|
if (function_exists('exif_read_data')) {
|
||||||
|
@ -175,17 +187,28 @@ class Upload {
|
||||||
'name' => G\get_filename_without_extension($this->uploaded_file),
|
'name' => G\get_filename_without_extension($this->uploaded_file),
|
||||||
'fileinfo' => G\get_image_fileinfo($this->uploaded_file)
|
'fileinfo' => G\get_image_fileinfo($this->uploaded_file)
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get available (supported) extensions
|
// Get available (supported) extensions
|
||||||
public static function getAvailableImageFormats() {
|
public static function getAvailableImageFormats()
|
||||||
|
{
|
||||||
$formats = Settings::get('upload_available_image_formats');
|
$formats = Settings::get('upload_available_image_formats');
|
||||||
return explode(',', $formats);
|
return explode(',', $formats);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//remove query string from url to get correct image name
|
||||||
|
protected function getNameFromURL()
|
||||||
|
{
|
||||||
|
if (strpos($this->source, '?')) {
|
||||||
|
return substr($this->source, 0, strpos($this->source, '?'));
|
||||||
|
} else {
|
||||||
|
return $this->source;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Failover since v3.8.12
|
// Failover since v3.8.12
|
||||||
public static function getEnabledImageFormats() {
|
public static function getEnabledImageFormats()
|
||||||
|
{
|
||||||
return Image::getEnabledImageFormats();
|
return Image::getEnabledImageFormats();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -194,8 +217,8 @@ class Upload {
|
||||||
* This checks for valid input source data
|
* This checks for valid input source data
|
||||||
* @Exception 1XX
|
* @Exception 1XX
|
||||||
*/
|
*/
|
||||||
protected function validateInput() {
|
protected function validateInput()
|
||||||
|
{
|
||||||
$check_missing = ["type", "source", "destination"];
|
$check_missing = ["type", "source", "destination"];
|
||||||
missing_values_to_exception($this, "CHV\UploadException", $check_missing, 100);
|
missing_values_to_exception($this, "CHV\UploadException", $check_missing, 100);
|
||||||
|
|
||||||
|
@ -235,7 +258,6 @@ class Upload {
|
||||||
if (!$make_destination) {
|
if (!$make_destination) {
|
||||||
throw new UploadException('$destination '.$this->destination.' is not a dir', 130);
|
throw new UploadException('$destination '.$this->destination.' is not a dir', 130);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Can read $destination dir?
|
// Can read $destination dir?
|
||||||
|
@ -250,14 +272,14 @@ class Upload {
|
||||||
|
|
||||||
// Fix $destination trailing
|
// Fix $destination trailing
|
||||||
$this->destination = G\add_ending_slash($this->destination);
|
$this->destination = G\add_ending_slash($this->destination);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch the $source file
|
* Fetch the $source file
|
||||||
* @Exception 2XX
|
* @Exception 2XX
|
||||||
*/
|
*/
|
||||||
protected function fetchSource() {
|
protected function fetchSource()
|
||||||
|
{
|
||||||
|
|
||||||
// Set the downstream file
|
// Set the downstream file
|
||||||
$this->downstream = @tempnam(sys_get_temp_dir(), 'chvtemp');
|
$this->downstream = @tempnam(sys_get_temp_dir(), 'chvtemp');
|
||||||
|
@ -270,9 +292,7 @@ class Upload {
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->type == 'file') {
|
if ($this->type == 'file') {
|
||||||
|
|
||||||
if ($this->source['error'] !== UPLOAD_ERR_OK) {
|
if ($this->source['error'] !== UPLOAD_ERR_OK) {
|
||||||
|
|
||||||
switch ($this->source['error']) {
|
switch ($this->source['error']) {
|
||||||
case UPLOAD_ERR_INI_SIZE: // 1
|
case UPLOAD_ERR_INI_SIZE: // 1
|
||||||
throw new UploadException('File too big', 201);
|
throw new UploadException('File too big', 201);
|
||||||
|
@ -296,13 +316,11 @@ class Upload {
|
||||||
throw new UploadException('The upload was stopped', 201);
|
throw new UploadException('The upload was stopped', 201);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!@rename($this->source['tmp_name'], $this->downstream)) {
|
if (!@rename($this->source['tmp_name'], $this->downstream)) {
|
||||||
throw new UploadException("Can't move temp file to the target upload dir", 203);
|
throw new UploadException("Can't move temp file to the target upload dir", 203);
|
||||||
}
|
}
|
||||||
|
|
||||||
} elseif ($this->type == "url") {
|
} elseif ($this->type == "url") {
|
||||||
try {
|
try {
|
||||||
G\fetch_url($this->source, $this->downstream);
|
G\fetch_url($this->source, $this->downstream);
|
||||||
|
@ -312,11 +330,13 @@ class Upload {
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->source_filename = basename($this->type == "file" ? $this->source["name"] : $this->source);
|
$this->source_filename = basename($this->type == "file" ? $this->source["name"] : $this->source);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function fixImageOrientation($image_filename, $exif) {
|
protected function fixImageOrientation($image_filename, $exif)
|
||||||
if($exif['Orientation'] == 1) return;
|
{
|
||||||
|
if ($exif['Orientation'] == 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
switch ($this->extension) {
|
switch ($this->extension) {
|
||||||
case 'jpg':
|
case 'jpg':
|
||||||
$image = imagecreatefromjpeg($image_filename);
|
$image = imagecreatefromjpeg($image_filename);
|
||||||
|
@ -341,7 +361,8 @@ class Upload {
|
||||||
* This checks for valid input source data
|
* This checks for valid input source data
|
||||||
* @Exception 3XX
|
* @Exception 3XX
|
||||||
*/
|
*/
|
||||||
protected function validateSourceFile() {
|
protected function validateSourceFile()
|
||||||
|
{
|
||||||
|
|
||||||
// Nothing to do here
|
// Nothing to do here
|
||||||
if (!file_exists($this->downstream)) {
|
if (!file_exists($this->downstream)) {
|
||||||
|
@ -389,16 +410,15 @@ class Upload {
|
||||||
$this->downstream = $this->ImageConvert->out;
|
$this->downstream = $this->ImageConvert->out;
|
||||||
$this->source_image_fileinfo = G\get_image_fileinfo($this->downstream);
|
$this->source_image_fileinfo = G\get_image_fileinfo($this->downstream);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle flood uploads
|
// Handle flood uploads
|
||||||
protected static function handleFlood() {
|
protected static function handleFlood()
|
||||||
|
{
|
||||||
$logged_user = Login::getUser();
|
$logged_user = Login::getUser();
|
||||||
|
|
||||||
if (!getSetting('flood_uploads_protection') || $logged_user['is_admin']) {
|
if (!getSetting('flood_uploads_protection') || $logged_user['is_admin']) {
|
||||||
return FALSE;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$flood_limit = [];
|
$flood_limit = [];
|
||||||
|
@ -415,15 +435,17 @@ class Upload {
|
||||||
COUNT(IF(image_date_gmt >= DATE_SUB(UTC_TIMESTAMP(), INTERVAL 1 DAY), 1, NULL)) AS day,
|
COUNT(IF(image_date_gmt >= DATE_SUB(UTC_TIMESTAMP(), INTERVAL 1 DAY), 1, NULL)) AS day,
|
||||||
COUNT(IF(image_date_gmt >= DATE_SUB(UTC_TIMESTAMP(), INTERVAL 1 WEEK), 1, NULL)) AS week,
|
COUNT(IF(image_date_gmt >= DATE_SUB(UTC_TIMESTAMP(), INTERVAL 1 WEEK), 1, NULL)) AS week,
|
||||||
COUNT(IF(image_date_gmt >= DATE_SUB(UTC_TIMESTAMP(), INTERVAL 1 MONTH), 1, NULL)) AS month
|
COUNT(IF(image_date_gmt >= DATE_SUB(UTC_TIMESTAMP(), INTERVAL 1 MONTH), 1, NULL)) AS month
|
||||||
FROM ".DB::getTable('images')." WHERE image_uploader_ip='".G\get_client_ip()."' AND image_date_gmt >= DATE_SUB(UTC_TIMESTAMP(), INTERVAL 1 MONTH)");
|
FROM ".DB::getTable('images')." WHERE image_uploader_ip='".G\get_client_ip()."' AND image_date_gmt >= DATE_SUB(UTC_TIMESTAMP(), INTERVAL 1 MONTH)"
|
||||||
} catch(Exception $e) {} // Silence
|
);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
} // Silence
|
||||||
|
|
||||||
$is_flood = FALSE;
|
$is_flood = false;
|
||||||
$flood_by = '';
|
$flood_by = '';
|
||||||
foreach (['minute', 'hour', 'day', 'week', 'month'] as $v) {
|
foreach (['minute', 'hour', 'day', 'week', 'month'] as $v) {
|
||||||
if ($flood_limit[$v] > 0 and $flood_db[$v] >= $flood_limit[$v]) {
|
if ($flood_limit[$v] > 0 and $flood_db[$v] >= $flood_limit[$v]) {
|
||||||
$flood_by = $v;
|
$flood_by = $v;
|
||||||
$is_flood = TRUE;
|
$is_flood = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -443,22 +465,27 @@ class Upload {
|
||||||
$message .= 'Month: '.$flood_db['week']."<br>";
|
$message .= 'Month: '.$flood_db['week']."<br>";
|
||||||
system_notification_email(['subject' => 'Flood report IP '. G\get_client_ip(), 'message' => $message]);
|
system_notification_email(['subject' => 'Flood report IP '. G\get_client_ip(), 'message' => $message]);
|
||||||
$_SESSION['flood_uploads_notify'][$flood_by] = true;
|
$_SESSION['flood_uploads_notify'][$flood_by] = true;
|
||||||
} catch(Exception $e) {} // Silence
|
} catch (Exception $e) {
|
||||||
|
} // Silence
|
||||||
}
|
}
|
||||||
|
|
||||||
return ['flood' => TRUE, 'limit' => $flood_limit[$flood_by], 'count' => $flood_db[$flood_by], 'by' => $flood_by];
|
return ['flood' => true, 'limit' => $flood_limit[$flood_by], 'count' => $flood_db[$flood_by], 'by' => $flood_by];
|
||||||
}
|
}
|
||||||
|
|
||||||
return FALSE;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function isValidImageMime($mime) {
|
protected function isValidImageMime($mime)
|
||||||
|
{
|
||||||
return preg_match("@image/(gif|pjpeg|jpeg|png|x-png|bmp|x-ms-bmp|x-windows-bmp)$@", $mime);
|
return preg_match("@image/(gif|pjpeg|jpeg|png|x-png|bmp|x-ms-bmp|x-windows-bmp)$@", $mime);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function isValidNamingOption($string) {
|
protected function isValidNamingOption($string)
|
||||||
|
{
|
||||||
return in_array($string, array("mixed", "random", "original"));
|
return in_array($string, array("mixed", "random", "original"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class UploadException extends Exception {}
|
class UploadException extends Exception
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
|
@ -20,10 +20,13 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace G;
|
namespace G;
|
||||||
use PDO, PDOException, Exception;
|
|
||||||
|
|
||||||
class DB {
|
use PDO;
|
||||||
|
use PDOException;
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
class DB
|
||||||
|
{
|
||||||
private static $instance;
|
private static $instance;
|
||||||
|
|
||||||
private $host = G_APP_DB_HOST;
|
private $host = G_APP_DB_HOST;
|
||||||
|
@ -34,19 +37,19 @@ class DB {
|
||||||
private $driver = G_APP_DB_DRIVER;
|
private $driver = G_APP_DB_DRIVER;
|
||||||
private $pdo_attrs = G_APP_DB_PDO_ATTRS;
|
private $pdo_attrs = G_APP_DB_PDO_ATTRS;
|
||||||
|
|
||||||
static $dbh;
|
public static $dbh;
|
||||||
public $query;
|
public $query;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Connect to the DB server
|
* Connect to the DB server
|
||||||
* Throws an Exception on error (tay weando? en serio?)
|
* Throws an Exception on error (tay weando? en serio?)
|
||||||
*/
|
*/
|
||||||
public function __construct($conn=[]) {
|
public function __construct($conn=[])
|
||||||
|
{
|
||||||
try {
|
try {
|
||||||
// PDO already connected
|
// PDO already connected
|
||||||
if (empty($conn) and isset(self::$dbh) and get_class(self::$dbh) == 'PDO') {
|
if (empty($conn) and isset(self::$dbh) and get_class(self::$dbh) == 'PDO') {
|
||||||
return TRUE;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($conn)) {
|
if (!empty($conn)) {
|
||||||
|
@ -92,19 +95,18 @@ class DB {
|
||||||
}
|
}
|
||||||
|
|
||||||
self::$instance = $this;
|
self::$instance = $this;
|
||||||
|
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
self::$dbh = NULL;
|
self::$dbh = null;
|
||||||
throw new DBException($e->getMessage(), 400);
|
throw new DBException($e->getMessage(), 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Singleton instance handler
|
* Singleton instance handler
|
||||||
* Used for the static methods of this class
|
* Used for the static methods of this class
|
||||||
*/
|
*/
|
||||||
public static function getInstance() {
|
public static function getInstance()
|
||||||
|
{
|
||||||
if (is_null(self::$instance)) {
|
if (is_null(self::$instance)) {
|
||||||
self::$instance = new self;
|
self::$instance = new self;
|
||||||
}
|
}
|
||||||
|
@ -115,7 +117,8 @@ class DB {
|
||||||
* Populates the class DB own PDO attributes array with an entire array
|
* Populates the class DB own PDO attributes array with an entire array
|
||||||
* Attribute list here: http://php.net/manual/en/pdo.setattribute.php
|
* Attribute list here: http://php.net/manual/en/pdo.setattribute.php
|
||||||
*/
|
*/
|
||||||
public function setPDOAttrs($attributes) {
|
public function setPDOAttrs($attributes)
|
||||||
|
{
|
||||||
$this->pdo_attrs = $attributes;
|
$this->pdo_attrs = $attributes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,11 +126,13 @@ class DB {
|
||||||
* Populates the class DB own PDO attributes array with a single key
|
* Populates the class DB own PDO attributes array with a single key
|
||||||
* Attributes list here: http://php.net/manual/en/pdo.setattribute.php
|
* Attributes list here: http://php.net/manual/en/pdo.setattribute.php
|
||||||
*/
|
*/
|
||||||
public function setPDOAttr($key, $value) {
|
public function setPDOAttr($key, $value)
|
||||||
|
{
|
||||||
$this->pdo_attrs[$key] = $value;
|
$this->pdo_attrs[$key] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getAttr($attr) {
|
public function getAttr($attr)
|
||||||
|
{
|
||||||
return self::$dbh->getAttribute($attr);
|
return self::$dbh->getAttribute($attr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,11 +140,13 @@ class DB {
|
||||||
* Prepares an SQL statement to be executed by the PDOStatement::execute() method
|
* Prepares an SQL statement to be executed by the PDOStatement::execute() method
|
||||||
* http://php.net/manual/en/pdo.prepare.php
|
* http://php.net/manual/en/pdo.prepare.php
|
||||||
*/
|
*/
|
||||||
public function query($query) {
|
public function query($query)
|
||||||
|
{
|
||||||
$this->query = self::$dbh->prepare($query);
|
$this->query = self::$dbh->prepare($query);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function errorInfo() {
|
public function errorInfo()
|
||||||
|
{
|
||||||
return self::$dbh->errorInfo();
|
return self::$dbh->errorInfo();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,7 +154,8 @@ class DB {
|
||||||
* Binds a value to a corresponding named or question mark placeholder in the SQL statement that was used to prepare the statement
|
* Binds a value to a corresponding named or question mark placeholder in the SQL statement that was used to prepare the statement
|
||||||
* http://php.net/manual/en/pdostatement.bindvalue.php
|
* http://php.net/manual/en/pdostatement.bindvalue.php
|
||||||
*/
|
*/
|
||||||
public function bind($param, $value, $type = null) {
|
public function bind($param, $value, $type = null)
|
||||||
|
{
|
||||||
if (is_null($type)) {
|
if (is_null($type)) {
|
||||||
switch (true) {
|
switch (true) {
|
||||||
case is_int($value):
|
case is_int($value):
|
||||||
|
@ -167,19 +175,23 @@ class DB {
|
||||||
$this->query->bindValue($param, $value, $type);
|
$this->query->bindValue($param, $value, $type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function exec() {
|
public function exec()
|
||||||
|
{
|
||||||
return $this->query->execute();
|
return $this->query->execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fetchColumn() {
|
public function fetchColumn()
|
||||||
|
{
|
||||||
return $this->query->fetchColumn();
|
return $this->query->fetchColumn();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function closeCursor() {
|
public function closeCursor()
|
||||||
|
{
|
||||||
return $this->query->closeCursor();
|
return $this->query->closeCursor();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fetchAll($mode=PDO::FETCH_ASSOC) {
|
public function fetchAll($mode=PDO::FETCH_ASSOC)
|
||||||
|
{
|
||||||
$this->exec();
|
$this->exec();
|
||||||
return $this->query->fetchAll(is_int($mode) ? $mode : PDO::FETCH_ASSOC);
|
return $this->query->fetchAll(is_int($mode) ? $mode : PDO::FETCH_ASSOC);
|
||||||
}
|
}
|
||||||
|
@ -188,7 +200,8 @@ class DB {
|
||||||
* Execute and returns the single result from the prepared statement
|
* Execute and returns the single result from the prepared statement
|
||||||
* http://php.net/manual/en/pdostatement.fetch.php
|
* http://php.net/manual/en/pdostatement.fetch.php
|
||||||
*/
|
*/
|
||||||
public function fetchSingle($mode=PDO::FETCH_ASSOC) {
|
public function fetchSingle($mode=PDO::FETCH_ASSOC)
|
||||||
|
{
|
||||||
$this->exec();
|
$this->exec();
|
||||||
return $this->query->fetch(is_int($mode) ? $mode : PDO::FETCH_ASSOC);
|
return $this->query->fetch(is_int($mode) ? $mode : PDO::FETCH_ASSOC);
|
||||||
}
|
}
|
||||||
|
@ -196,11 +209,12 @@ class DB {
|
||||||
/**
|
/**
|
||||||
* Query and exec, return number of affected rows or FALSE
|
* Query and exec, return number of affected rows or FALSE
|
||||||
*/
|
*/
|
||||||
public static function queryExec($query) {
|
public static function queryExec($query)
|
||||||
|
{
|
||||||
try {
|
try {
|
||||||
$db = self::getInstance();
|
$db = self::getInstance();
|
||||||
$db->query($query);
|
$db->query($query);
|
||||||
return $db->exec() ? $db->rowCount() : FALSE;
|
return $db->exec() ? $db->rowCount() : false;
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
throw new DBException($e->getMessage(), 400);
|
throw new DBException($e->getMessage(), 400);
|
||||||
}
|
}
|
||||||
|
@ -209,7 +223,8 @@ class DB {
|
||||||
/**
|
/**
|
||||||
* Query and fetch single record
|
* Query and fetch single record
|
||||||
*/
|
*/
|
||||||
public static function queryFetchSingle($query, $fetch_style=NULL) {
|
public static function queryFetchSingle($query, $fetch_style=null)
|
||||||
|
{
|
||||||
try {
|
try {
|
||||||
return self::queryFetch($query, 1, $fetch_style);
|
return self::queryFetch($query, 1, $fetch_style);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
|
@ -220,9 +235,10 @@ class DB {
|
||||||
/**
|
/**
|
||||||
* Query and fetch all records
|
* Query and fetch all records
|
||||||
*/
|
*/
|
||||||
public static function queryFetchAll($query, $fetch_style=NULL) {
|
public static function queryFetchAll($query, $fetch_style=null)
|
||||||
|
{
|
||||||
try {
|
try {
|
||||||
return self::queryFetch($query, NULL, $fetch_style);
|
return self::queryFetch($query, null, $fetch_style);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
throw new DBException($e->getMessage(), 400);
|
throw new DBException($e->getMessage(), 400);
|
||||||
}
|
}
|
||||||
|
@ -231,7 +247,8 @@ class DB {
|
||||||
/**
|
/**
|
||||||
* Query fetch (core version)
|
* Query fetch (core version)
|
||||||
*/
|
*/
|
||||||
public static function queryFetch($query, $limit=1, $fetch_style=NULL) {
|
public static function queryFetch($query, $limit=1, $fetch_style=null)
|
||||||
|
{
|
||||||
try {
|
try {
|
||||||
$db = self::getInstance();
|
$db = self::getInstance();
|
||||||
$db->query($query);
|
$db->query($query);
|
||||||
|
@ -245,7 +262,8 @@ class DB {
|
||||||
* Returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed
|
* Returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed
|
||||||
* http://php.net/manual/en/pdostatement.rowcount.php
|
* http://php.net/manual/en/pdostatement.rowcount.php
|
||||||
*/
|
*/
|
||||||
public function rowCount() {
|
public function rowCount()
|
||||||
|
{
|
||||||
return $this->query->rowCount();
|
return $this->query->rowCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -253,7 +271,8 @@ class DB {
|
||||||
* Returns the ID of the last inserted row, or the last value from a sequence object, depending on the underlying driver
|
* Returns the ID of the last inserted row, or the last value from a sequence object, depending on the underlying driver
|
||||||
* http://php.net/manual/en/pdo.lastinsertid.php
|
* http://php.net/manual/en/pdo.lastinsertid.php
|
||||||
*/
|
*/
|
||||||
public function lastInsertId() {
|
public function lastInsertId()
|
||||||
|
{
|
||||||
return self::$dbh->lastInsertId();
|
return self::$dbh->lastInsertId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -261,7 +280,8 @@ class DB {
|
||||||
* Turns off autocommit mode
|
* Turns off autocommit mode
|
||||||
* http://php.net/manual/en/pdo.begintransaction.php
|
* http://php.net/manual/en/pdo.begintransaction.php
|
||||||
*/
|
*/
|
||||||
public function beginTransaction(){
|
public function beginTransaction()
|
||||||
|
{
|
||||||
return self::$dbh->beginTransaction();
|
return self::$dbh->beginTransaction();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -269,7 +289,8 @@ class DB {
|
||||||
* Commits a transaction, returning the database connection to autocommit mode until the next call to PDO::beginTransaction() starts a new transaction
|
* Commits a transaction, returning the database connection to autocommit mode until the next call to PDO::beginTransaction() starts a new transaction
|
||||||
* http://php.net/manual/en/pdo.commit.php
|
* http://php.net/manual/en/pdo.commit.php
|
||||||
*/
|
*/
|
||||||
public function endTransaction(){
|
public function endTransaction()
|
||||||
|
{
|
||||||
return self::$dbh->commit();
|
return self::$dbh->commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -277,7 +298,8 @@ class DB {
|
||||||
* Rolls back the current transaction, as initiated by PDO::beginTransaction()
|
* Rolls back the current transaction, as initiated by PDO::beginTransaction()
|
||||||
* http://php.net/manual/en/pdo.rollback.php
|
* http://php.net/manual/en/pdo.rollback.php
|
||||||
*/
|
*/
|
||||||
public function cancelTransaction(){
|
public function cancelTransaction()
|
||||||
|
{
|
||||||
return self::$dbh->rollBack();
|
return self::$dbh->rollBack();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -285,7 +307,8 @@ class DB {
|
||||||
* Dumps the informations contained by a prepared statement directly on the output
|
* Dumps the informations contained by a prepared statement directly on the output
|
||||||
* http://php.net/manual/en/pdostatement.debugdumpparams.php
|
* http://php.net/manual/en/pdostatement.debugdumpparams.php
|
||||||
*/
|
*/
|
||||||
public function debugDumpParams(){
|
public function debugDumpParams()
|
||||||
|
{
|
||||||
return $this->query->debugDumpParams();
|
return $this->query->debugDumpParams();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -294,15 +317,16 @@ class DB {
|
||||||
/**
|
/**
|
||||||
* Get the table with its prefix
|
* Get the table with its prefix
|
||||||
*/
|
*/
|
||||||
public static function getTable($table) {
|
public static function getTable($table)
|
||||||
|
{
|
||||||
return get_app_setting('db_table_prefix') . $table;
|
return get_app_setting('db_table_prefix') . $table;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get values from DB
|
* Get values from DB
|
||||||
*/
|
*/
|
||||||
public static function get($table, $values, $clause='AND', $sort=[], $limit=NULL, $fetch_style=NULL) {
|
public static function get($table, $values, $clause='AND', $sort=[], $limit=null, $fetch_style=null)
|
||||||
|
{
|
||||||
if (!is_array($values) and $values !== 'all') {
|
if (!is_array($values) and $values !== 'all') {
|
||||||
throw new DBException('Expecting array values, '.gettype($values).' given in ' . __METHOD__, 100);
|
throw new DBException('Expecting array values, '.gettype($values).' given in ' . __METHOD__, 100);
|
||||||
}
|
}
|
||||||
|
@ -318,7 +342,7 @@ class DB {
|
||||||
|
|
||||||
$query = 'SELECT * FROM '.$table;
|
$query = 'SELECT * FROM '.$table;
|
||||||
|
|
||||||
if($join) {
|
if (isset($join) && $join) {
|
||||||
$query .= ' ' . $join . ' ';
|
$query .= ' ' . $join . ' ';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -367,8 +391,8 @@ class DB {
|
||||||
* Update target table row(s)
|
* Update target table row(s)
|
||||||
* Returns the number of affected rows or false
|
* Returns the number of affected rows or false
|
||||||
*/
|
*/
|
||||||
public static function update($table, $values, $wheres, $clause='AND') {
|
public static function update($table, $values, $wheres, $clause='AND')
|
||||||
|
{
|
||||||
if (!is_array($values)) {
|
if (!is_array($values)) {
|
||||||
throw new DBException('Expecting array values, '.gettype($values).' given in '. __METHOD__, 100);
|
throw new DBException('Expecting array values, '.gettype($values).' given in '. __METHOD__, 100);
|
||||||
}
|
}
|
||||||
|
@ -406,18 +430,17 @@ class DB {
|
||||||
$db->bind(':where_'.$k, $v);
|
$db->bind(':where_'.$k, $v);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $db->exec() ? $db->rowCount() : FALSE;
|
return $db->exec() ? $db->rowCount() : false;
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
throw new DBException($e->getMessage(), 400);
|
throw new DBException($e->getMessage(), 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Insert single row to the table
|
* Insert single row to the table
|
||||||
*/
|
*/
|
||||||
public static function insert($table, $values) {
|
public static function insert($table, $values)
|
||||||
|
{
|
||||||
if (!is_array($values)) {
|
if (!is_array($values)) {
|
||||||
throw new DBException('Expecting array values, '.gettype($values).' given in '. __METHOD__, 100);
|
throw new DBException('Expecting array values, '.gettype($values).' given in '. __METHOD__, 100);
|
||||||
}
|
}
|
||||||
|
@ -439,11 +462,10 @@ class DB {
|
||||||
foreach ($values as $k => $v) {
|
foreach ($values as $k => $v) {
|
||||||
$db->bind(':'.$k, $v);
|
$db->bind(':'.$k, $v);
|
||||||
}
|
}
|
||||||
return $db->exec() ? $db->lastInsertId() : FALSE;
|
return $db->exec() ? $db->lastInsertId() : false;
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
throw new DBException($e->getMessage(), 400);
|
throw new DBException($e->getMessage(), 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -451,8 +473,8 @@ class DB {
|
||||||
* Returns the number of affected rows or false
|
* Returns the number of affected rows or false
|
||||||
* Note: Minimum value to be set is zero, no negative values here
|
* Note: Minimum value to be set is zero, no negative values here
|
||||||
*/
|
*/
|
||||||
public static function increment($table, $values, $wheres, $clause='AND') {
|
public static function increment($table, $values, $wheres, $clause='AND')
|
||||||
|
{
|
||||||
foreach (['values', 'wheres'] as $k) {
|
foreach (['values', 'wheres'] as $k) {
|
||||||
if (!is_array(${$k})) {
|
if (!is_array(${$k})) {
|
||||||
throw new DBException('Expecting array values, '.gettype(${$k}).' given in '. __METHOD__, 100);
|
throw new DBException('Expecting array values, '.gettype(${$k}).' given in '. __METHOD__, 100);
|
||||||
|
@ -492,15 +514,14 @@ class DB {
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
throw new DBException($e->getMessage(), 400);
|
throw new DBException($e->getMessage(), 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete row(s) from table
|
* Delete row(s) from table
|
||||||
* Returns the number of affected rows or false
|
* Returns the number of affected rows or false
|
||||||
*/
|
*/
|
||||||
public static function delete($table, $values, $clause='AND') {
|
public static function delete($table, $values, $clause='AND')
|
||||||
|
{
|
||||||
if (!is_array($values)) {
|
if (!is_array($values)) {
|
||||||
throw new DBException('Expecting array values, '.gettype($values).' given in '. __METHOD__, 100);
|
throw new DBException('Expecting array values, '.gettype($values).' given in '. __METHOD__, 100);
|
||||||
}
|
}
|
||||||
|
@ -522,17 +543,17 @@ class DB {
|
||||||
foreach ($values as $k => $v) {
|
foreach ($values as $k => $v) {
|
||||||
$db->bind(':'.$k, $v);
|
$db->bind(':'.$k, $v);
|
||||||
}
|
}
|
||||||
return $db->exec() ? $db->rowCount() : FALSE;
|
return $db->exec() ? $db->rowCount() : false;
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
throw new DBException($e->getMessage(), 400);
|
throw new DBException($e->getMessage(), 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate clause
|
* Validate clause
|
||||||
*/
|
*/
|
||||||
private static function validateClause($clause, $method=NULL) {
|
private static function validateClause($clause, $method=null)
|
||||||
|
{
|
||||||
if (!is_null($clause)) {
|
if (!is_null($clause)) {
|
||||||
$clause = strtoupper($clause);
|
$clause = strtoupper($clause);
|
||||||
if (!in_array($clause, ['AND', 'OR'])) {
|
if (!in_array($clause, ['AND', 'OR'])) {
|
||||||
|
@ -540,8 +561,9 @@ class DB {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DB class own Exception
|
// DB class own Exception
|
||||||
class DBException extends Exception {}
|
class DBException extends Exception
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue