first commit
This commit is contained in:
64
catalog/model/tool/image.php
Normal file
64
catalog/model/tool/image.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
namespace Opencart\Catalog\Model\Tool;
|
||||
/**
|
||||
* Class Image
|
||||
*
|
||||
* @package Opencart\Catalog\Model\Tool
|
||||
*/
|
||||
class Image extends \Opencart\System\Engine\Model {
|
||||
/**
|
||||
* @param string $filename
|
||||
* @param int $width
|
||||
* @param int $height
|
||||
* @param string $default
|
||||
*
|
||||
* @return string
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function resize(string $filename, int $width, int $height, string $default = ''): string {
|
||||
if (!is_file(DIR_IMAGE . $filename) || substr(str_replace('\\', '/', realpath(DIR_IMAGE . $filename)), 0, strlen(DIR_IMAGE)) != DIR_IMAGE) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$extension = pathinfo($filename, PATHINFO_EXTENSION);
|
||||
|
||||
$image_old = $filename;
|
||||
$image_new = 'cache/' . oc_substr($filename, 0, oc_strrpos($filename, '.')) . '-' . (int)$width . 'x' . (int)$height . '.' . $extension;
|
||||
|
||||
if (!is_file(DIR_IMAGE . $image_new) || (filemtime(DIR_IMAGE . $image_old) > filemtime(DIR_IMAGE . $image_new))) {
|
||||
list($width_orig, $height_orig, $image_type) = getimagesize(DIR_IMAGE . $image_old);
|
||||
|
||||
if (!in_array($image_type, [IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_WEBP])) {
|
||||
return $this->config->get('config_url') . 'image/' . $image_old;
|
||||
}
|
||||
|
||||
$path = '';
|
||||
|
||||
$directories = explode('/', dirname($image_new));
|
||||
|
||||
foreach ($directories as $directory) {
|
||||
if (!$path) {
|
||||
$path = $directory;
|
||||
} else {
|
||||
$path = $path . '/' . $directory;
|
||||
}
|
||||
|
||||
if (!is_dir(DIR_IMAGE . $path)) {
|
||||
@mkdir(DIR_IMAGE . $path, 0777);
|
||||
}
|
||||
}
|
||||
|
||||
if ($width_orig != $width || $height_orig != $height) {
|
||||
$image = new \Opencart\System\Library\Image(DIR_IMAGE . $image_old);
|
||||
$image->resize($width, $height, $default);
|
||||
$image->save(DIR_IMAGE . $image_new);
|
||||
} else {
|
||||
copy(DIR_IMAGE . $image_old, DIR_IMAGE . $image_new);
|
||||
}
|
||||
}
|
||||
|
||||
$image_new = str_replace(' ', '%20', $image_new); // fix bug when attach image on email (gmail.com). it is automatically changing space from " " to +
|
||||
|
||||
return $this->config->get('config_url') . 'image/' . $image_new;
|
||||
}
|
||||
}
|
22
catalog/model/tool/online.php
Normal file
22
catalog/model/tool/online.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
namespace Opencart\Catalog\Model\Tool;
|
||||
/**
|
||||
* Class Online
|
||||
*
|
||||
* @package Opencart\Catalog\Model\Tool
|
||||
*/
|
||||
class Online extends \Opencart\System\Engine\Model {
|
||||
/**
|
||||
* @param string $ip
|
||||
* @param int $customer_id
|
||||
* @param string $url
|
||||
* @param string $referer
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function addOnline(string $ip, int $customer_id, string $url, string $referer): void {
|
||||
$this->db->query("DELETE FROM `" . DB_PREFIX . "customer_online` WHERE `date_added` < '" . date('Y-m-d H:i:s', strtotime('-' . (int)$this->config->get('config_customer_online_expire') . ' hour')) . "'");
|
||||
|
||||
$this->db->query("REPLACE INTO `" . DB_PREFIX . "customer_online` SET `ip` = '" . $this->db->escape($ip) . "', `customer_id` = '" . (int)$customer_id . "', `url` = '" . $this->db->escape($url) . "', `referer` = '" . $this->db->escape($referer) . "', `date_added` = '" . $this->db->escape(date('Y-m-d H:i:s')) . "'");
|
||||
}
|
||||
}
|
33
catalog/model/tool/upload.php
Normal file
33
catalog/model/tool/upload.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
namespace Opencart\Catalog\Model\Tool;
|
||||
/**
|
||||
* Class Upload
|
||||
*
|
||||
* @package Opencart\Catalog\Model\Tool
|
||||
*/
|
||||
class Upload extends \Opencart\System\Engine\Model {
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string $filename
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function addUpload(string $name, string $filename): string {
|
||||
$code = oc_token(32);
|
||||
|
||||
$this->db->query("INSERT INTO `" . DB_PREFIX . "upload` SET `name` = '" . $this->db->escape($name) . "', `filename` = '" . $this->db->escape($filename) . "', `code` = '" . $this->db->escape($code) . "', `date_added` = NOW()");
|
||||
|
||||
return $code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $code
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getUploadByCode(string $code): array {
|
||||
$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "upload` WHERE code = '" . $this->db->escape($code) . "'");
|
||||
|
||||
return $query->row;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user