aboutsummaryrefslogtreecommitdiffstats
path: root/public/admin/model/tool
diff options
context:
space:
mode:
authorJesús <heckyel@hyperbola.info>2019-08-18 21:14:58 -0500
committerJesús <heckyel@hyperbola.info>2019-08-18 21:14:58 -0500
commit2eed7b082f83630301e51f57ca8394de228a8605 (patch)
tree1d19962d22d30f99317d9276e4bae7744fc93fc2 /public/admin/model/tool
downloadlibrecart-2eed7b082f83630301e51f57ca8394de228a8605.tar.lz
librecart-2eed7b082f83630301e51f57ca8394de228a8605.tar.xz
librecart-2eed7b082f83630301e51f57ca8394de228a8605.zip
first commit
Diffstat (limited to 'public/admin/model/tool')
-rw-r--r--public/admin/model/tool/backup.php68
-rw-r--r--public/admin/model/tool/image.php47
-rw-r--r--public/admin/model/tool/upload.php108
3 files changed, 223 insertions, 0 deletions
diff --git a/public/admin/model/tool/backup.php b/public/admin/model/tool/backup.php
new file mode 100644
index 0000000..d8be774
--- /dev/null
+++ b/public/admin/model/tool/backup.php
@@ -0,0 +1,68 @@
+<?php
+class ModelToolBackup extends Model {
+ public function getTables() {
+ $table_data = array();
+
+ $query = $this->db->query("SHOW TABLES FROM `" . DB_DATABASE . "`");
+
+ foreach ($query->rows as $result) {
+ if (utf8_substr($result['Tables_in_' . DB_DATABASE], 0, strlen(DB_PREFIX)) == DB_PREFIX) {
+ if (isset($result['Tables_in_' . DB_DATABASE])) {
+ $table_data[] = $result['Tables_in_' . DB_DATABASE];
+ }
+ }
+ }
+
+ return $table_data;
+ }
+
+ public function backup($tables) {
+ $output = '';
+
+ foreach ($tables as $table) {
+ if (DB_PREFIX) {
+ if (strpos($table, DB_PREFIX) === false) {
+ $status = false;
+ } else {
+ $status = true;
+ }
+ } else {
+ $status = true;
+ }
+
+ if ($status) {
+ $output .= 'TRUNCATE TABLE `' . $table . '`;' . "\n\n";
+
+ $query = $this->db->query("SELECT * FROM `" . $table . "`");
+
+ foreach ($query->rows as $result) {
+ $fields = '';
+
+ foreach (array_keys($result) as $value) {
+ $fields .= '`' . $value . '`, ';
+ }
+
+ $values = '';
+
+ foreach (array_values($result) as $value) {
+ $value = str_replace(array("\x00", "\x0a", "\x0d", "\x1a"), array('\0', '\n', '\r', '\Z'), $value);
+ $value = str_replace(array("\n", "\r", "\t"), array('\n', '\r', '\t'), $value);
+ $value = str_replace('\\', '\\\\', $value);
+ $value = str_replace('\'', '\\\'', $value);
+ $value = str_replace('\\\n', '\n', $value);
+ $value = str_replace('\\\r', '\r', $value);
+ $value = str_replace('\\\t', '\t', $value);
+
+ $values .= '\'' . $value . '\', ';
+ }
+
+ $output .= 'INSERT INTO `' . $table . '` (' . preg_replace('/, $/', '', $fields) . ') VALUES (' . preg_replace('/, $/', '', $values) . ');' . "\n";
+ }
+
+ $output .= "\n\n";
+ }
+ }
+
+ return $output;
+ }
+} \ No newline at end of file
diff --git a/public/admin/model/tool/image.php b/public/admin/model/tool/image.php
new file mode 100644
index 0000000..5a3c536
--- /dev/null
+++ b/public/admin/model/tool/image.php
@@ -0,0 +1,47 @@
+<?php
+class ModelToolImage extends Model {
+ public function resize($filename, $width, $height) {
+ if (!is_file(DIR_IMAGE . $filename) || substr(str_replace('\\', '/', realpath(DIR_IMAGE . $filename)), 0, strlen(DIR_IMAGE)) != str_replace('\\', '/', DIR_IMAGE)) {
+ return;
+ }
+
+ $extension = pathinfo($filename, PATHINFO_EXTENSION);
+
+ $image_old = $filename;
+ $image_new = 'cache/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . $width . 'x' . $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, array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF))) {
+ return DIR_IMAGE . $image_old;
+ }
+
+ $path = '';
+
+ $directories = explode('/', dirname($image_new));
+
+ foreach ($directories as $directory) {
+ $path = $path . '/' . $directory;
+
+ if (!is_dir(DIR_IMAGE . $path)) {
+ @mkdir(DIR_IMAGE . $path, 0777);
+ }
+ }
+
+ if ($width_orig != $width || $height_orig != $height) {
+ $image = new Image(DIR_IMAGE . $image_old);
+ $image->resize($width, $height);
+ $image->save(DIR_IMAGE . $image_new);
+ } else {
+ copy(DIR_IMAGE . $image_old, DIR_IMAGE . $image_new);
+ }
+ }
+
+ if ($this->request->server['HTTPS']) {
+ return HTTPS_CATALOG . 'image/' . $image_new;
+ } else {
+ return HTTP_CATALOG . 'image/' . $image_new;
+ }
+ }
+}
diff --git a/public/admin/model/tool/upload.php b/public/admin/model/tool/upload.php
new file mode 100644
index 0000000..3e77a9e
--- /dev/null
+++ b/public/admin/model/tool/upload.php
@@ -0,0 +1,108 @@
+<?php
+class ModelToolUpload extends Model {
+ public function addUpload($name, $filename) {
+ $code = sha1(uniqid(mt_rand(), true));
+
+ $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;
+ }
+
+ public function deleteUpload($upload_id) {
+ $this->db->query("DELETE FROM " . DB_PREFIX . "upload WHERE upload_id = '" . (int)$upload_id . "'");
+ }
+
+ public function getUpload($upload_id) {
+ $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "upload` WHERE upload_id = '" . (int)$upload_id . "'");
+
+ return $query->row;
+ }
+
+ public function getUploadByCode($code) {
+ $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "upload WHERE code = '" . $this->db->escape($code) . "'");
+
+ return $query->row;
+ }
+
+ public function getUploads($data = array()) {
+ $sql = "SELECT * FROM " . DB_PREFIX . "upload";
+
+ $implode = array();
+
+ if (!empty($data['filter_name'])) {
+ $implode[] = "name LIKE '" . $this->db->escape($data['filter_name']) . "%'";
+ }
+
+ if (!empty($data['filter_filename'])) {
+ $implode[] = "filename LIKE '" . $this->db->escape($data['filter_filename']) . "%'";
+ }
+
+ if (!empty($data['filter_date_added'])) {
+ $implode[] = "date_added = '" . $this->db->escape($data['filter_date_added']) . "%'";
+ }
+
+ if ($implode) {
+ $sql .= " WHERE " . implode(" AND ", $implode);
+ }
+
+ $sort_data = array(
+ 'name',
+ 'filename',
+ 'date_added'
+ );
+
+ if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
+ $sql .= " ORDER BY " . $data['sort'];
+ } else {
+ $sql .= " ORDER BY date_added";
+ }
+
+ if (isset($data['order']) && ($data['order'] == 'DESC')) {
+ $sql .= " DESC";
+ } else {
+ $sql .= " ASC";
+ }
+
+ if (isset($data['start']) || isset($data['limit'])) {
+ if ($data['start'] < 0) {
+ $data['start'] = 0;
+ }
+
+ if ($data['limit'] < 1) {
+ $data['limit'] = 20;
+ }
+
+ $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
+ }
+
+ $query = $this->db->query($sql);
+
+ return $query->rows;
+ }
+
+ public function getTotalUploads() {
+ $sql = "SELECT COUNT(*) AS total FROM " . DB_PREFIX . "upload";
+
+ $implode = array();
+
+ if (!empty($data['filter_name'])) {
+ $implode[] = "name LIKE '" . $this->db->escape($data['filter_name']) . "%'";
+ }
+
+ if (!empty($data['filter_filename'])) {
+ $implode[] = "filename LIKE '" . $this->db->escape($data['filter_filename']) . "%'";
+ }
+
+ if (!empty($data['filter_date_added'])) {
+ $implode[] = "date_added = '" . $this->db->escape($data['filter_date_added']) . "'";
+ }
+
+ if ($implode) {
+ $sql .= " WHERE " . implode(" AND ", $implode);
+ }
+
+ $query = $this->db->query($sql);
+
+ return $query->row['total'];
+ }
+} \ No newline at end of file