]> 91.132.146.200 Git - insipid.git/commitdiff
thumbnails optional locally storeable
authorBanana <banana@optimus.de>
Tue, 16 Jul 2019 15:21:57 +0000 (17:21 +0200)
committerBanana <banana@optimus.de>
Tue, 16 Jul 2019 15:21:57 +0000 (17:21 +0200)
webroot/asset/css/style.css
webroot/config.default.php
webroot/lib/link.class.php
webroot/lib/summoner.class.php
webroot/localdata/.gitignore [new file with mode: 0644]
webroot/view/editlink.inc.php
webroot/view/editlink.php
webroot/view/home.php
webroot/view/linkinfo.php
webroot/view/overview.php

index e2f65b8a37deb1189a29d1238f1814c45e8c6399..5e375221a4ad0da1eacadd530ec968c0b8f3bf29 100644 (file)
@@ -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
+}
index ea7486fefe04c9b217d91dd72ca14a72dcc2e710..117a4ed64260e1f8de4d7428e578f197a1c296b3 100644 (file)
@@ -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- ');
index 340dbf1b69ddc963d8ee5896c5673d84be0c5664..bd4867fa9b9fed328fb8193a8066c59c09a4e1b1 100644 (file)
@@ -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;
+                       }
+               }
+       }
 }
- ?>
+
index d5845288f040581200de4c924e264d459b4a95ed..7b5487faa8e9c081a1e5b1658d4301915bcd8f0b 100644 (file)
@@ -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 (file)
index 0000000..d6b7ef3
--- /dev/null
@@ -0,0 +1,2 @@
+*
+!.gitignore
index e3d0b7f6ae4330289ae3d3015b39d4ab9c57d400..4aaa1ad0941008bde43dcfa30d9556518b6f9dc0 100644 (file)
@@ -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']);
index 07fbbd3cf6fc039b834f260731abbd6c89aad2a9..8152f1aba264f9a892e75e936a1eaa8c8c20ae3e 100644 (file)
                </div>
                <div class="column">
                        <p>
-                               <img class="linkthumbnail" src="<?php echo $linkData['image']; ?>" alt="Image if provided...">
+                               <img class="linkthumbnail" src="<?php echo $linkData['imageToShow']; ?>" alt="Image if provided...">
                        </p>
-                       <input class="input" type="text" name="data[image]" value="<?php echo Summoner::ifset($formData, 'image'); ?>" />
+                       <input class="input" type="text" name="data[image]" value="<?php echo Summoner::ifset($formData, 'image'); ?>" /><br />
+                               <br />
+                               <input class="checkbox" type="checkbox" name="data[localImage]" value="1" <?php if(Summoner::ifset($formData, 'localImage')) echo "checked"; ?> />
+                               Store image locally
                </div>
         </div>
 
index f448077d9cc861fba2246fe86b3918260d21d1b4..ed7b850c855af28915b6c420314676fb3d06207f 100644 (file)
 
         <div class="columns">
                <div class="column is-half">
-                       <img class="linkthumbnail" src="<?php echo Summoner::ifset($formData, 'image'); ?>" alt="Image from provided link" />
+                       <img class="linkthumbnail" src="<?php echo Summoner::ifset($formData, 'imageToShow'); ?>" alt="Image from provided link" />
                </div>
                <div class="column is-half">
                        <div class="field">
index 3f266b80695024bf030b91e5ffcface2e2c4d466..664286e2fc3dc15c0c19526917e6687399113227 100644 (file)
@@ -91,7 +91,7 @@
        </div>
        <div class="column is-two-third">
                <p>
-                       <img class="linkthumbnail" src="<?php echo $linkData['image']; ?>" alt="Image if provided...">
+                       <img class="linkthumbnail" src="<?php echo $linkData['imageToShow']; ?>" alt="Image if provided...">
                </p>
        </div>
     </div>
index d9ee020ed4886423fba8f296cc398c233c0efc8c..540cf49a726e37952c5ddd4cfbb23fa46118fc23 100644 (file)
@@ -63,7 +63,7 @@
                        <figure class="image is-4by3">
                        <a href="<?php echo $link['link']; ?>" target="_blank">
             <?php if(!empty($link['image'])) { ?>
-               <img class="" src= "<?php echo $link['image']; ?>">
+               <img class="linkthumbnail" src= "<?php echo $link['image']; ?>">
                        <?php } else { ?>
                 <img class="" src= "asset/img/no-link-picture.png">
             <?php } ?>