From eba8d93c604cf616b928094d0d865330b666b3e8 Mon Sep 17 00:00:00 2001 From: Banana Date: Tue, 16 Jul 2019 17:21:57 +0200 Subject: [PATCH] thumbnails optional locally storeable --- webroot/asset/css/style.css | 6 +++-- webroot/config.default.php | 9 +++++-- webroot/lib/link.class.php | 44 ++++++++++++++++++++++++++++++---- webroot/lib/summoner.class.php | 38 +++++++++++++++++++++++++++++ webroot/localdata/.gitignore | 2 ++ webroot/view/editlink.inc.php | 6 +++++ webroot/view/editlink.php | 7 ++++-- webroot/view/home.php | 2 +- webroot/view/linkinfo.php | 2 +- webroot/view/overview.php | 2 +- 10 files changed, 104 insertions(+), 14 deletions(-) create mode 100644 webroot/localdata/.gitignore diff --git a/webroot/asset/css/style.css b/webroot/asset/css/style.css index e2f65b8..5e37522 100644 --- a/webroot/asset/css/style.css +++ b/webroot/asset/css/style.css @@ -4,7 +4,9 @@ } .linkthumbnail { - max-height: 200px; + height: 230px; + object-fit: cover; + width: 306px; } h4 a { @@ -22,4 +24,4 @@ h4 a { .card { border-top: 1px solid #cccccc; -} \ No newline at end of file +} diff --git a/webroot/config.default.php b/webroot/config.default.php index ea7486f..117a4ed 100644 --- a/webroot/config.default.php +++ b/webroot/config.default.php @@ -31,12 +31,17 @@ define('DB_HOST','127.0.0.1'); define('DB_USERNAME','user'); define('DB_PASSWORD','test'); define('DB_NAME','insipid'); -define('DB_PREFIX','insipid'); # a _ is added automatically as seperation +define('DB_PREFIX','insipid'); # a _ is added automatically as separation # user config define('FRONTEND_USERNAME','luke'); define('FRONTEND_PASSWORD','father'); +# absolute path of webroot +define('ABSOLUTE_PATH', '/home/banana/code/insipid/webroot'); +# relative to absolute path the name of the storage folder +define('LOCAL_STORAGE', 'localdata'); + # complete restricted access not only the private links or the edit functions # username and password see above define("USE_PAGE_AUTH",false); @@ -49,4 +54,4 @@ define('EMAIL_SERVER_USER',''); define('EMAIL_SERVER_PASS',''); define('EMAIL_SERVER_PORT',993); define('EMAIL_SERVER_MAILBOX','INBOX'); # default INBOX -define('EMAIL_MARKER','to-insipid- '); \ No newline at end of file +define('EMAIL_MARKER','to-insipid- '); diff --git a/webroot/lib/link.class.php b/webroot/lib/link.class.php index 340dbf1..bd4867f 100644 --- a/webroot/lib/link.class.php +++ b/webroot/lib/link.class.php @@ -49,7 +49,6 @@ class Link { * @return mixed */ public function load($hash) { - $ret = false; $this->_data = array(); @@ -68,13 +67,12 @@ class Link { WHERE `hash` = '".$this->DB->real_escape_string($hash)."'"; $query = $this->DB->query($queryStr); if(!empty($query) && $query->num_rows == 1) { - $ret = $query->fetch_assoc(); - - $this->_data = $ret; + $this->_data = $query->fetch_assoc(); # add stuff $this->_tags(); $this->_categories(); + $this->_image(); } } @@ -125,6 +123,12 @@ class Link { $search .= ' '.implode(" ",$tagArr); $search .= ' '.implode(" ",$catArr); + # did the image url change? + $_imageUrlChanged = false; + if($this->_data['image'] != $data['image']) { + $_imageUrlChanged = true; + } + $queryStr = "UPDATE `".DB_PREFIX."_link` SET `status` = '".$this->DB->real_escape_string($data['private'])."', `description` = '".$this->DB->real_escape_string($data['description'])."', @@ -154,6 +158,21 @@ class Link { } } + # decide to store or remove the image + if(isset($data['localImage'])) { + $image = ABSOLUTE_PATH.'/'.LOCAL_STORAGE.'/thumbnail-'.$this->_data['hash']; + if($data['localImage'] === true) { + if(!file_exists($image) || $_imageUrlChanged === true) { + Summoner::downloadFile($data['image'],$image); + } + } + elseif($data['localImage'] === false) { + if(file_exists($image)) { + unlink($image); + } + } + } + $ret = true; } @@ -281,5 +300,20 @@ class Link { } } } + + /** + * determine of we have a local stored image + * if so populate the localImage attribute + */ + private function _image() { + if(!empty($this->_data['hash'])) { + $this->_data['imageToShow'] = $this->_data['image']; + $image = ABSOLUTE_PATH.'/'.LOCAL_STORAGE.'/thumbnail-'.$this->_data['hash']; + if(file_exists($image)) { + $this->_data['imageToShow'] = LOCAL_STORAGE.'/thumbnail-'.$this->_data['hash']; + $this->_data['localImage'] = true; + } + } + } } - ?> + diff --git a/webroot/lib/summoner.class.php b/webroot/lib/summoner.class.php index d584528..7b5487f 100644 --- a/webroot/lib/summoner.class.php +++ b/webroot/lib/summoner.class.php @@ -165,7 +165,9 @@ class Summoner { curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_MAXREDIRS, 2); + curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0'); + // curl_setopt($ch, CURLOPT_VERBOSE, true); //curl_setopt($ch, CURLOPT_HEADER, true); if(!empty($port)) { @@ -186,6 +188,42 @@ class Summoner { return $ret; } + /** + * Download given url to given file + * @param $url + * @param $whereToStore + * @param bool $port + * @return bool + */ + static function downloadFile($url, $whereToStore, $port=false) { + $fh = fopen($whereToStore, 'w+'); + + $ret = false; + + if($fh !== false) { + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_FILE, $fh); + + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); + curl_setopt($ch, CURLOPT_TIMEOUT, 30); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($ch, CURLOPT_MAXREDIRS, 2); + curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0'); + + if(!empty($port)) { + curl_setopt($ch, CURLOPT_PORT, $port); + } + curl_exec($ch); + curl_close($ch); + + $ret = true; + } + + fclose($fh); + + return $ret; + } + /** * check if a string starts with a given string * diff --git a/webroot/localdata/.gitignore b/webroot/localdata/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/webroot/localdata/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/webroot/view/editlink.inc.php b/webroot/view/editlink.inc.php index e3d0b7f..4aaa1ad 100644 --- a/webroot/view/editlink.inc.php +++ b/webroot/view/editlink.inc.php @@ -54,6 +54,12 @@ if(isset($_POST['data']) && !empty($_POST['data']) && isset($_POST['editlink'])) $formData['private'] = 1; } + $formData['localImage'] = false; + if(isset($fData['localImage'])) { + $formData['localImage'] = true; + } + + $formData['description'] = trim($fData['description']); $formData['title'] = trim($fData['title']); $formData['image'] = trim($fData['image']); diff --git a/webroot/view/editlink.php b/webroot/view/editlink.php index 07fbbd3..8152f1a 100644 --- a/webroot/view/editlink.php +++ b/webroot/view/editlink.php @@ -119,9 +119,12 @@

- Image if provided... + Image if provided...

- +
+
+ /> + Store image locally
diff --git a/webroot/view/home.php b/webroot/view/home.php index f448077..ed7b850 100644 --- a/webroot/view/home.php +++ b/webroot/view/home.php @@ -135,7 +135,7 @@
- Image from provided link + Image from provided link
diff --git a/webroot/view/linkinfo.php b/webroot/view/linkinfo.php index 3f266b8..664286e 100644 --- a/webroot/view/linkinfo.php +++ b/webroot/view/linkinfo.php @@ -91,7 +91,7 @@

- Image if provided... + Image if provided...

diff --git a/webroot/view/overview.php b/webroot/view/overview.php index d9ee020..540cf49 100644 --- a/webroot/view/overview.php +++ b/webroot/view/overview.php @@ -63,7 +63,7 @@
- + -- 2.39.5