From 7e6ddbd9fc17ea6b095e76f9869a64dc8fd081eb Mon Sep 17 00:00:00 2001 From: Banana Date: Tue, 23 Jul 2019 21:22:03 +0200 Subject: [PATCH] code cleanup --- webroot/lib/category.class.php | 119 ++++++----- webroot/lib/link.class.php | 77 ++++--- webroot/lib/management.class.php | 340 +++++++++++++++--------------- webroot/lib/simple-imap.class.php | 2 - webroot/lib/summoner.class.php | 2 - webroot/lib/tag.class.php | 119 ++++++----- webroot/view/editlink.php | 1 - webroot/view/home.inc.php | 272 ++++++++++++------------ 8 files changed, 467 insertions(+), 465 deletions(-) diff --git a/webroot/lib/category.class.php b/webroot/lib/category.class.php index 197d222..db216f8 100644 --- a/webroot/lib/category.class.php +++ b/webroot/lib/category.class.php @@ -27,69 +27,68 @@ */ class Category { - /** - * the database object - * @var object - */ - private $DB; + /** + * the database object + * @var object + */ + private $DB; - /** - * the current loaded category by DB id - * @var int - */ - private $id; + /** + * the current loaded category by DB id + * @var int + */ + private $id; - public function __construct($databaseConnectionObject) { - $this->DB = $databaseConnectionObject; - } + public function __construct($databaseConnectionObject) { + $this->DB = $databaseConnectionObject; + } - /** - * by given string load the info from the DB and even create if not existing - * @param string $string - */ - public function initbystring($string) { - $this->id = false; - if(!empty($string)) { - $queryStr = "SELECT id FROM `".DB_PREFIX."_category` - WHERE `name` = '".$this->DB->real_escape_string($string)."'"; - $query = $this->DB->query($queryStr); - if(!empty($query) && $query->num_rows > 0) { - $result = $query->fetch_assoc(); - $this->id = $result['id']; - } - else { - $queryStr = "INSERT INTO `".DB_PREFIX."_category` - SET `name` = '".$this->DB->real_escape_string($string)."'"; - $this->DB->query($queryStr); - if(!empty($this->DB->insert_id)) { - $this->id = $this->DB->insert_id; - } - } - } - } + /** + * by given string load the info from the DB and even create if not existing + * @param string $string + */ + public function initbystring($string) { + $this->id = false; + if(!empty($string)) { + $queryStr = "SELECT id FROM `".DB_PREFIX."_category` + WHERE `name` = '".$this->DB->real_escape_string($string)."'"; + $query = $this->DB->query($queryStr); + if(!empty($query) && $query->num_rows > 0) { + $result = $query->fetch_assoc(); + $this->id = $result['id']; + } + else { + $queryStr = "INSERT INTO `".DB_PREFIX."_category` + SET `name` = '".$this->DB->real_escape_string($string)."'"; + $this->DB->query($queryStr); + if(!empty($this->DB->insert_id)) { + $this->id = $this->DB->insert_id; + } + } + } + } - /** - * by given DB table id load all the info we need - * @param int $id - */ - public function initbyid($id) { - if(!empty($id)) { - $this->id = $id; - } - } + /** + * by given DB table id load all the info we need + * @param int $id + */ + public function initbyid($id) { + if(!empty($id)) { + $this->id = $id; + } + } - /** - * set the relation to the given link to the loaded category - * @param int $linkid - * @return void - */ - public function setRelation($linkid) { - if(!empty($linkid) && !empty($this->id)) { - $queryStr = "INSERT IGNORE INTO `".DB_PREFIX."_categoryrelation` - SET `linkid` = '".$this->DB->real_escape_string($linkid)."', - `categoryid` = '".$this->DB->real_escape_string($this->id)."'"; - $this->DB->query($queryStr); - } - } + /** + * set the relation to the given link to the loaded category + * @param int $linkid + * @return void + */ + public function setRelation($linkid) { + if(!empty($linkid) && !empty($this->id)) { + $queryStr = "INSERT IGNORE INTO `".DB_PREFIX."_categoryrelation` + SET `linkid` = '".$this->DB->real_escape_string($linkid)."', + `categoryid` = '".$this->DB->real_escape_string($this->id)."'"; + $this->DB->query($queryStr); + } + } } - ?> \ No newline at end of file diff --git a/webroot/lib/link.class.php b/webroot/lib/link.class.php index df36008..8cecbf4 100644 --- a/webroot/lib/link.class.php +++ b/webroot/lib/link.class.php @@ -121,7 +121,7 @@ class Link { $this->DB->query($queryStr); if($returnId === true) { $ret = $this->DB->insert_id; - } + } return $ret; } @@ -146,6 +146,8 @@ class Link { $search .= ' '.implode(" ",$tagArr); $search .= ' '.implode(" ",$catArr); + $this->DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE); + # did the image url change? $_imageUrlChanged = false; if($this->_data['image'] != $data['image']) { @@ -162,41 +164,48 @@ class Link { $query = $this->DB->query($queryStr); - $catObj = new Category($this->DB); - $tagObj = new Tag($this->DB); - // clean the relations first - $this->_removeTagRelation(false); - $this->_removeCategoryRelation(false); - - if(!empty($catArr)) { - foreach($catArr as $c) { - $catObj->initbystring($c); - $catObj->setRelation($this->_data['id']); + if($query !== false) { + $catObj = new Category($this->DB); + $tagObj = new Tag($this->DB); + // clean the relations first + $this->_removeTagRelation(false); + $this->_removeCategoryRelation(false); + + if(!empty($catArr)) { + foreach($catArr as $c) { + $catObj->initbystring($c); + $catObj->setRelation($this->_data['id']); + } } - } - if(!empty($tagArr)) { - foreach($tagArr as $t) { - $tagObj->initbystring($t); - $tagObj->setRelation($this->_data['id']); + if(!empty($tagArr)) { + foreach($tagArr as $t) { + $tagObj->initbystring($t); + $tagObj->setRelation($this->_data['id']); + } } - } - # 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); + # 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); + elseif($data['localImage'] === false) { + if(file_exists($image)) { + unlink($image); + } } } + + $this->DB->commit(); + $ret = true; + } + else { + $this->DB->rollback(); } - $ret = true; } return $ret; @@ -212,7 +221,7 @@ class Link { $ret = false; if(!empty($link)) { - $queryStr = "SELECT + $queryStr = "SELECT any_value(`hash`) as hash FROM `".DB_PREFIX."_link` WHERE `link` = '".$this->DB->real_escape_string($link)."'"; @@ -234,7 +243,7 @@ class Link { $ret = array(); if(!empty($this->_data['hash'])) { - $queryStr = "SELECT + $queryStr = "SELECT DISTINCT(tag) as tag FROM `".DB_PREFIX."_combined` WHERE `hash` = '".$this->DB->real_escape_string($this->_data['hash'])."'"; @@ -260,7 +269,7 @@ class Link { $ret = array(); if(!empty($this->_data['hash'])) { - $queryStr = "SELECT + $queryStr = "SELECT DISTINCT(category) FROM `".DB_PREFIX."_combined` WHERE `hash` = '".$this->DB->real_escape_string($this->_data['hash'])."'"; $query = $this->DB->query($queryStr); @@ -284,12 +293,12 @@ class Link { if(!empty($this->_data['id'])) { $queryStr = false; if($tagid === false) { - $queryStr = "DELETE + $queryStr = "DELETE FROM `".DB_PREFIX."_tagrelation` WHERE `linkid` = '".$this->DB->real_escape_string($this->_data['id'])."'"; } elseif(is_numeric($tagid)) { - $queryStr = "DELETE + $queryStr = "DELETE FROM `".DB_PREFIX."_tagrelation` WHERE `linkid` = '".$this->DB->real_escape_string($this->_data['id'])."' AND `tagid` = '".$this->DB->real_escape_string($tagid)."'"; @@ -308,7 +317,7 @@ class Link { if(!empty($this->_data['id'])) { $queryStr = false; if($categoryid === false) { - $queryStr = "DELETE + $queryStr = "DELETE FROM `".DB_PREFIX."_categoryrelation` WHERE `linkid` = '".$this->DB->real_escape_string($this->_data['id'])."'"; } diff --git a/webroot/lib/management.class.php b/webroot/lib/management.class.php index 7f62b55..f66f8da 100644 --- a/webroot/lib/management.class.php +++ b/webroot/lib/management.class.php @@ -27,28 +27,28 @@ */ class Management { - /** - * the database object - * @var object - */ - private $DB; - - protected $COMBINED_SELECT_VALUES = "any_value(`id`) as id, - any_value(`link`) as link, - any_value(`created`) as created, - any_value(`status`) as `status`, - any_value(`description`) as description, - any_value(`title`) as title, - any_value(`image`) as image, - any_value(`hash`) as hash, - any_value(`tag`) as tag, - any_value(`category`) as category, - any_value(`categoryId`) as categoryId, - any_value(`tagId`) as tagId"; - - public function __construct($databaseConnectionObject) { - $this->DB = $databaseConnectionObject; - } + /** + * the database object + * @var object + */ + private $DB; + + protected $COMBINED_SELECT_VALUES = "any_value(`id`) as id, + any_value(`link`) as link, + any_value(`created`) as created, + any_value(`status`) as `status`, + any_value(`description`) as description, + any_value(`title`) as title, + any_value(`image`) as image, + any_value(`hash`) as hash, + any_value(`tag`) as tag, + any_value(`category`) as category, + any_value(`categoryId`) as categoryId, + any_value(`tagId`) as tagId"; + + public function __construct($databaseConnectionObject) { + $this->DB = $databaseConnectionObject; + } /** * get all the available categories from the DB. @@ -58,15 +58,15 @@ class Management { * @param bool $stats * @return array */ - public function categories($limit=false, $stats=false) { - $ret = array(); + public function categories($limit=false, $stats=false) { + $ret = array(); $statsInfo = array(); if($stats === true) { - $queryStr = "SELECT + $queryStr = "SELECT COUNT(*) as amount, any_value(categoryid) as categoryId - FROM `".DB_PREFIX."_categoryrelation` + FROM `".DB_PREFIX."_categoryrelation` GROUP BY categoryid"; $query = $this->DB->query($queryStr); if(!empty($query)) { @@ -76,28 +76,28 @@ class Management { } } - $queryStr = "SELECT - any_value(`id`) as id, - any_value(`name`) as name - FROM `".DB_PREFIX."_category` - ORDER BY `name` ASC"; - if(!empty($limit)) { - $queryStr .= " LIMIT $limit"; - } - $query = $this->DB->query($queryStr); - if(!empty($query)) { - while($result = $query->fetch_assoc()) { - if($stats === true) { + $queryStr = "SELECT + any_value(`id`) as id, + any_value(`name`) as name + FROM `".DB_PREFIX."_category` + ORDER BY `name` ASC"; + if(!empty($limit)) { + $queryStr .= " LIMIT $limit"; + } + $query = $this->DB->query($queryStr); + if(!empty($query)) { + while($result = $query->fetch_assoc()) { + if($stats === true) { $ret[$result['id']] = array('name' => $result['name'], 'amount' => $statsInfo[$result['id']]); } else { $ret[$result['id']] = array('name' => $result['name']); } } - } + } - return $ret; - } + return $ret; + } /** * get all the available tags from the DB. @@ -107,15 +107,15 @@ class Management { * @param bool $stats * @return array */ - public function tags($limit=false, $stats=false) { - $ret = array(); + public function tags($limit=false, $stats=false) { + $ret = array(); $statsInfo = array(); if($stats === true) { - $queryStr = "SELECT + $queryStr = "SELECT COUNT(*) as amount, any_value(`tagid`) as tagId - FROM `".DB_PREFIX."_tagrelation` + FROM `".DB_PREFIX."_tagrelation` GROUP BY tagId"; $query = $this->DB->query($queryStr); if(!empty($query)) { @@ -125,16 +125,16 @@ class Management { } } - $queryStr = "SELECT - any_value(`id`) as id, - any_value(`name`) as name - FROM `".DB_PREFIX."_tag` - ORDER BY `name` ASC"; - if(!empty($limit)) { - $queryStr .= " LIMIT $limit"; - } - $query = $this->DB->query($queryStr); - if(!empty($query)) { + $queryStr = "SELECT + any_value(`id`) as id, + any_value(`name`) as name + FROM `".DB_PREFIX."_tag` + ORDER BY `name` ASC"; + if(!empty($limit)) { + $queryStr .= " LIMIT $limit"; + } + $query = $this->DB->query($queryStr); + if(!empty($query)) { while($result = $query->fetch_assoc()) { if($stats === true) { $ret[$result['id']] = array('name' => $result['name'], 'amount' => $statsInfo[$result['id']]); @@ -143,50 +143,50 @@ class Management { $ret[$result['id']] = array('name' => $result['name']); } } - } + } - return $ret; - } + return $ret; + } /** * return the latest added links * @param int $limit * @return array */ - public function latestLinks($limit=5) { - $ret = array(); - - $queryStr = "SELECT * FROM `".DB_PREFIX."_link` WHERE `status` = 2 ORDER BY `created` DESC"; - if(!empty($limit)) { - $queryStr .= " LIMIT $limit"; - } - $query = $this->DB->query($queryStr); - if(!empty($query) && $query->num_rows > 0) { - $ret = $query->fetch_all(MYSQLI_ASSOC); - } - - return $ret; - } - - /** - * get all the categories ordered by link added date - */ - public function categoriesByDateAdded() { - $ret = array(); - - $categories = $this->categories(); - foreach($categories as $k=>$v) { + public function latestLinks($limit=5) { + $ret = array(); + + $queryStr = "SELECT * FROM `".DB_PREFIX."_link` WHERE `status` = 2 ORDER BY `created` DESC"; + if(!empty($limit)) { + $queryStr .= " LIMIT $limit"; + } + $query = $this->DB->query($queryStr); + if(!empty($query) && $query->num_rows > 0) { + $ret = $query->fetch_all(MYSQLI_ASSOC); + } + + return $ret; + } + + /** + * get all the categories ordered by link added date + */ + public function categoriesByDateAdded() { + $ret = array(); + + $categories = $this->categories(); + foreach($categories as $k=>$v) { $latestLink = $this->latestLinkForCategory($k); if(!empty($latestLink)) { array_push($ret, array('created' => $latestLink[0]['created'], 'id' => $k, 'name' => $v['name'])); } - } + } $_created = array_column($ret, 'created'); array_multisort($_created, SORT_DESC, $ret); - return $ret; - } + return $ret; + } /** * find all links by given category string or id. @@ -196,12 +196,12 @@ class Management { * @param int $limit * @return array */ - public function linksByCategory($id,$string,$limit=5) { - $ret = array(); + public function linksByCategory($id,$string,$limit=5) { + $ret = array(); - $queryStr = "SELECT ".$this->COMBINED_SELECT_VALUES." + $queryStr = "SELECT ".$this->COMBINED_SELECT_VALUES." FROM `".DB_PREFIX."_combined` - WHERE `status` = 2"; + WHERE `status` = 2"; if(!empty($id) && is_numeric($id)) { $queryStr .= " AND `categoryId` = '" . $this->DB->real_escape_string($id) . "'"; } @@ -213,17 +213,17 @@ class Management { } $queryStr .= "GROUP BY `hash` - ORDER BY `created` DESC"; - if(!empty($limit)) { - $queryStr .= " LIMIT $limit"; - } - $query = $this->DB->query($queryStr); - if(!empty($query) && $query->num_rows > 0) { - $ret = $query->fetch_all(MYSQLI_ASSOC); - } - - return $ret; - } + ORDER BY `created` DESC"; + if(!empty($limit)) { + $queryStr .= " LIMIT $limit"; + } + $query = $this->DB->query($queryStr); + if(!empty($query) && $query->num_rows > 0) { + $ret = $query->fetch_all(MYSQLI_ASSOC); + } + + return $ret; + } /** * find all links by given tag string or id. @@ -233,12 +233,12 @@ class Management { * @param int $limit * @return array */ - public function linksByTag($id,$string,$limit=5) { - $ret = array(); + public function linksByTag($id,$string,$limit=5) { + $ret = array(); $queryStr = "SELECT ".$this->COMBINED_SELECT_VALUES." FROM `".DB_PREFIX."_combined` - WHERE `status` = 2"; + WHERE `status` = 2"; if(!empty($id) && is_numeric($id)) { $queryStr .= " AND `tagId` = '" . $this->DB->real_escape_string($id) . "'"; } @@ -250,99 +250,99 @@ class Management { } $queryStr .= "GROUP BY `hash` - ORDER BY `created` DESC"; - if(!empty($limit)) { - $queryStr .= " LIMIT $limit"; - } - $query = $this->DB->query($queryStr); - if(!empty($query) && $query->num_rows > 0) { - $ret = $query->fetch_all(MYSQLI_ASSOC); - } - - return $ret; - } + ORDER BY `created` DESC"; + if(!empty($limit)) { + $queryStr .= " LIMIT $limit"; + } + $query = $this->DB->query($queryStr); + if(!empty($query) && $query->num_rows > 0) { + $ret = $query->fetch_all(MYSQLI_ASSOC); + } + + return $ret; + } /** * return all links and Info we have from the combined view * @param bool | int $limit * @return array */ - public function links($limit=false) { - $ret = array(); + public function links($limit=false) { + $ret = array(); - $queryStr = "SELECT ".$this->COMBINED_SELECT_VALUES." + $queryStr = "SELECT ".$this->COMBINED_SELECT_VALUES." FROM `".DB_PREFIX."_combined` - WHERE `status` = 2 - GROUP BY `hash` - ORDER BY `created` DESC"; - $query = $this->DB->query($queryStr); - if(!empty($query) && $query->num_rows > 0) { - $ret = $query->fetch_all(MYSQLI_ASSOC); - } + WHERE `status` = 2 + GROUP BY `hash` + ORDER BY `created` DESC"; + $query = $this->DB->query($queryStr); + if(!empty($query) && $query->num_rows > 0) { + $ret = $query->fetch_all(MYSQLI_ASSOC); + } - return $ret; - } + return $ret; + } /** * return the latest added link for given category id * @param int $categoryid * @return array */ - public function latestLinkForCategory($categoryid) { - $ret = array(); + public function latestLinkForCategory($categoryid) { + $ret = array(); - if(!empty($categoryid) && is_numeric($categoryid)) { + if(!empty($categoryid) && is_numeric($categoryid)) { $queryStr = "SELECT ".$this->COMBINED_SELECT_VALUES." FROM `".DB_PREFIX."_combined` - WHERE `status` = 2 - AND `categoryId` = '" . $this->DB->real_escape_string($categoryid) . "' - ORDER BY `created` DESC - LIMIT 1"; + WHERE `status` = 2 + AND `categoryId` = '" . $this->DB->real_escape_string($categoryid) . "' + ORDER BY `created` DESC + LIMIT 1"; $query = $this->DB->query($queryStr); if(!empty($query) && $query->num_rows > 0) { $ret = $query->fetch_all(MYSQLI_ASSOC); } } - return $ret; + return $ret; } - /** - * for simpler management we have the search data in a separate column - * it is not fancy or even technical nice but it damn works - */ - private function _updateSearchIndex() { - $allLinks = array(); - $queryStr = "SELECT hash FROM `".DB_PREFIX."_link`"; - $query = $this->DB->query($queryStr); - if(!empty($query) && $query->num_rows > 0) { - $allLinks = $query->fetch_all(MYSQLI_ASSOC); - } - - if(!empty($allLinks)) { - foreach($allLinks as $link) { - $LinkObj = new Link($this->DB); - $l = $LinkObj->load($link['hash']); - - $searchStr = $l['title']; - $searchStr .= ' '.$l['description']; - foreach($l['tags'] as $t) { - $searchStr .= ' '.$t['tag']; - } - foreach($l['categories'] as $c) { - $searchStr .= ' '.$c['category']; - } - - # now update the search string - $queryStr = "UPDATE `".DB_PREFIX."_link` - SET `search` = '".$this->DB->real_escape_string($searchStr)."' - WHERE `hash` = '".$this->DB->real_escape_string($link['hash'])."'"; - - $this->DB->query($queryStr); - - unset($LinkObj,$l,$searchStr,$t,$c,$queryStr); - } - } - } + /** + * for simpler management we have the search data in a separate column + * it is not fancy or even technical nice but it damn works + */ + private function _updateSearchIndex() { + $allLinks = array(); + $queryStr = "SELECT hash FROM `".DB_PREFIX."_link`"; + $query = $this->DB->query($queryStr); + if(!empty($query) && $query->num_rows > 0) { + $allLinks = $query->fetch_all(MYSQLI_ASSOC); + } + + if(!empty($allLinks)) { + foreach($allLinks as $link) { + $LinkObj = new Link($this->DB); + $l = $LinkObj->load($link['hash']); + + $searchStr = $l['title']; + $searchStr .= ' '.$l['description']; + foreach($l['tags'] as $t) { + $searchStr .= ' '.$t['tag']; + } + foreach($l['categories'] as $c) { + $searchStr .= ' '.$c['category']; + } + + # now update the search string + $queryStr = "UPDATE `".DB_PREFIX."_link` + SET `search` = '".$this->DB->real_escape_string($searchStr)."' + WHERE `hash` = '".$this->DB->real_escape_string($link['hash'])."'"; + + $this->DB->query($queryStr); + + unset($LinkObj,$l,$searchStr,$t,$c,$queryStr); + } + } + } } ?> diff --git a/webroot/lib/simple-imap.class.php b/webroot/lib/simple-imap.class.php index 59e6551..1869742 100644 --- a/webroot/lib/simple-imap.class.php +++ b/webroot/lib/simple-imap.class.php @@ -228,5 +228,3 @@ class SimpleImap { imap_close($this->_connection); } } - -?> \ No newline at end of file diff --git a/webroot/lib/summoner.class.php b/webroot/lib/summoner.class.php index 6400cec..576df01 100644 --- a/webroot/lib/summoner.class.php +++ b/webroot/lib/summoner.class.php @@ -460,5 +460,3 @@ class Summoner { return $ret; } } - -?> diff --git a/webroot/lib/tag.class.php b/webroot/lib/tag.class.php index ccf4eae..b15f749 100644 --- a/webroot/lib/tag.class.php +++ b/webroot/lib/tag.class.php @@ -27,69 +27,68 @@ */ class Tag { - /** - * the database object - * @var object - */ - private $DB; + /** + * the database object + * @var object + */ + private $DB; - /** - * the current loaded tag by DB id - * @var int - */ - private $id; + /** + * the current loaded tag by DB id + * @var int + */ + private $id; - public function __construct($databaseConnectionObject) { - $this->DB = $databaseConnectionObject; - } + public function __construct($databaseConnectionObject) { + $this->DB = $databaseConnectionObject; + } - /** - * by given string load the info from the DB and even create if not existing - * @param string $string - */ - public function initbystring($string) { - $this->id = false; - if(!empty($string)) { - $queryStr = "SELECT id FROM `".DB_PREFIX."_tag` - WHERE `name` = '".$this->DB->real_escape_string($string)."'"; - $query = $this->DB->query($queryStr); - if(!empty($query) && $query->num_rows > 0) { - $result = $query->fetch_assoc(); - $this->id = $result['id']; - } - else { - $queryStr = "INSERT INTO `".DB_PREFIX."_tag` - SET `name` = '".$this->DB->real_escape_string($string)."'"; - $this->DB->query($queryStr); - if(!empty($this->DB->insert_id)) { - $this->id = $this->DB->insert_id; - } - } - } - } + /** + * by given string load the info from the DB and even create if not existing + * @param string $string + */ + public function initbystring($string) { + $this->id = false; + if(!empty($string)) { + $queryStr = "SELECT id FROM `".DB_PREFIX."_tag` + WHERE `name` = '".$this->DB->real_escape_string($string)."'"; + $query = $this->DB->query($queryStr); + if(!empty($query) && $query->num_rows > 0) { + $result = $query->fetch_assoc(); + $this->id = $result['id']; + } + else { + $queryStr = "INSERT INTO `".DB_PREFIX."_tag` + SET `name` = '".$this->DB->real_escape_string($string)."'"; + $this->DB->query($queryStr); + if(!empty($this->DB->insert_id)) { + $this->id = $this->DB->insert_id; + } + } + } + } - /** - * by given DB table id load all the info we need - * @param int $id - */ - public function initbyid($id) { - if(!empty($id)) { - $this->id = $id; - } - } + /** + * by given DB table id load all the info we need + * @param int $id + */ + public function initbyid($id) { + if(!empty($id)) { + $this->id = $id; + } + } - /** - * set the relation to the given link to the loaded tag - * @param int $linkid - * @return boolean - */ - public function setRelation($linkid) { - if(!empty($linkid) && !empty($this->id)) { - $queryStr = "INSERT IGNORE INTO `".DB_PREFIX."_tagrelation` - SET `linkid` = '".$this->DB->real_escape_string($linkid)."', - `tagid` = '".$this->DB->real_escape_string($this->id)."'"; - $this->DB->query($queryStr); - } - } + /** + * set the relation to the given link to the loaded tag + * @param int $linkid + * @return boolean + */ + public function setRelation($linkid) { + if(!empty($linkid) && !empty($this->id)) { + $queryStr = "INSERT IGNORE INTO `".DB_PREFIX."_tagrelation` + SET `linkid` = '".$this->DB->real_escape_string($linkid)."', + `tagid` = '".$this->DB->real_escape_string($this->id)."'"; + $this->DB->query($queryStr); + } + } } - ?> \ No newline at end of file diff --git a/webroot/view/editlink.php b/webroot/view/editlink.php index f3676a2..84bf049 100644 --- a/webroot/view/editlink.php +++ b/webroot/view/editlink.php @@ -26,7 +26,6 @@ * */ ?> -
diff --git a/webroot/view/home.inc.php b/webroot/view/home.inc.php index de1321e..118df57 100644 --- a/webroot/view/home.inc.php +++ b/webroot/view/home.inc.php @@ -36,153 +36,153 @@ $formData = false; $honeypotCheck = false; if((isset($_POST['password']) && !empty($_POST['password'])) || (isset($_POST['username']) && !empty($_POST['username']))) { - # those are hidden fields. A robot may input these. A valid user does not. - $honeypotCheck = true; + # those are hidden fields. A robot may input these. A valid user does not. + $honeypotCheck = true; } # search or new one. if(isset($_POST['data']) && !empty($_POST['data']) && isset($_POST['submitsearch']) && $honeypotCheck === false) { - $searchValue = trim($_POST['data']['searchfield']); - $isUrl = Summoner::validate($searchValue,'url'); - if($isUrl === true) { - # search for URL - $queryStr = "SELECT * FROM `".DB_PREFIX."_link` - WHERE `link` = '".$DB->real_escape_string($searchValue)."'"; - } - elseif(Summoner::validate($searchValue,'text')) { - $queryStr = "SELECT *, - MATCH (`search`) AGAINST ('".$DB->real_escape_string($searchValue)."' IN BOOLEAN MODE) AS score + $searchValue = trim($_POST['data']['searchfield']); + $isUrl = Summoner::validate($searchValue,'url'); + if($isUrl === true) { + # search for URL + $queryStr = "SELECT * FROM `".DB_PREFIX."_link` + WHERE `link` = '".$DB->real_escape_string($searchValue)."'"; + } + elseif(Summoner::validate($searchValue,'text')) { + $queryStr = "SELECT *, + MATCH (`search`) AGAINST ('".$DB->real_escape_string($searchValue)."' IN BOOLEAN MODE) AS score FROM `".DB_PREFIX."_link` WHERE MATCH (`search`) AGAINST ('".$DB->real_escape_string($searchValue)."' IN BOOLEAN MODE) ORDER BY score DESC"; - } - else { - $submitFeedback['message'] = 'Invalid input'; - $submitFeedback['status'] = 'error'; - } - - if(!empty($queryStr)) { - $query = $DB->query($queryStr); - if(!empty($query) && $query->num_rows > 0) { - $searchResult = $query->fetch_all(MYSQLI_ASSOC); - } - } - - # new one? - if(empty($searchResult) && $isUrl === true) { - # try to gather some information automatically - $linkInfo = Summoner::gatherInfoFromURL($searchValue); - if(!empty($linkInfo)) { - if(isset($linkInfo['description'])) { - $formData['description'] = $linkInfo['description']; - } - if(isset($linkInfo['title'])) { - $formData['title'] = $linkInfo['title']; - } - if(isset($linkInfo['image'])) { - $formData['image'] = $linkInfo['image']; - } - } - # show the add form - $showAddForm = true; - $formData['url'] = $searchValue; - } - elseif(!empty($searchResult)) { - # something has been found - } - else { - # nothing found - $submitFeedback['message'] = 'Nothing found...'; - $submitFeedback['status'] = 'error'; - } + } + else { + $submitFeedback['message'] = 'Invalid input'; + $submitFeedback['status'] = 'error'; + } + + if(!empty($queryStr)) { + $query = $DB->query($queryStr); + if(!empty($query) && $query->num_rows > 0) { + $searchResult = $query->fetch_all(MYSQLI_ASSOC); + } + } + + # new one? + if(empty($searchResult) && $isUrl === true) { + # try to gather some information automatically + $linkInfo = Summoner::gatherInfoFromURL($searchValue); + if(!empty($linkInfo)) { + if(isset($linkInfo['description'])) { + $formData['description'] = $linkInfo['description']; + } + if(isset($linkInfo['title'])) { + $formData['title'] = $linkInfo['title']; + } + if(isset($linkInfo['image'])) { + $formData['image'] = $linkInfo['image']; + } + } + # show the add form + $showAddForm = true; + $formData['url'] = $searchValue; + } + elseif(!empty($searchResult)) { + # something has been found + } + else { + # nothing found + $submitFeedback['message'] = 'Nothing found...'; + $submitFeedback['status'] = 'error'; + } } # add a new one if(isset($_POST['data']) && !empty($_POST['data']) && isset($_POST['addnewone']) && $honeypotCheck === false) { - $fData = $_POST['data']; - - # very simple security check. - # can/should be extended in the future. - Summoner::simpleAuth(); - - $formData['private'] = 2; - if(isset($fData['private'])) { - $formData['private'] = 1; - } - - $formData['url'] = trim($fData['url']); - $formData['description'] = trim($fData['description']); - $formData['title'] = trim($fData['title']); - $formData['image'] = trim($fData['image']); - $formData['category'] = trim($fData['category']); - $formData['tag'] = trim($fData['tag']); - - $isUrl = Summoner::validate($formData['url'],'url'); - - if($isUrl === true && !empty($formData['title'])) { - $hash = md5($formData['url']); - - # categories and tag stuff - $catArr = Summoner::prepareTagOrCategoryStr($formData['category']); - $tagArr = Summoner::prepareTagOrCategoryStr($formData['tag']); - - $search = $formData['title']; - $search .= ' '.$formData['description']; - $search .= ' '.implode(" ",$tagArr); - $search .= ' '.implode(" ",$catArr); - - $DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE); - - $linkObj = new Link($DB); - $linkID = $linkObj->create(array( - 'hash' => $hash, - 'search' => $search, - 'link' => $formData['url'], - 'status' => $formData['private'], - 'description' => $formData['description'], - 'title' => $formData['title'], - 'image' => $formData['image'] - ),true); - - if(!empty($linkID)) { - - if(!empty($catArr)) { - foreach($catArr as $c) { - $catObj = new Category($DB); - $catObj->initbystring($c); - $catObj->setRelation($linkID); - - unset($catObj); - } - } - if(!empty($tagArr)) { - foreach($tagArr as $t) { - $tagObj = new Tag($DB); - $tagObj->initbystring($t); - $tagObj->setRelation($linkID); - - unset($tagObj); - } - } - - $DB->commit(); - - $submitFeedback['message'] = 'Link added successfully.'; - $submitFeedback['status'] = 'success'; - $TemplateData['refresh'] = 'index.php?p=linkinfo&id='.$hash; - } - else { - $DB->rollback(); - $submitFeedback['message'] = 'Something went wrong...'; - $submitFeedback['status'] = 'error'; - $showAddForm = true; - } - } - else { - $submitFeedback['message'] = 'Please provide a valid URL and title.'; - $submitFeedback['status'] = 'error'; - $showAddForm = true; - } + $fData = $_POST['data']; + + # very simple security check. + # can/should be extended in the future. + Summoner::simpleAuth(); + + $formData['private'] = 2; + if(isset($fData['private'])) { + $formData['private'] = 1; + } + + $formData['url'] = trim($fData['url']); + $formData['description'] = trim($fData['description']); + $formData['title'] = trim($fData['title']); + $formData['image'] = trim($fData['image']); + $formData['category'] = trim($fData['category']); + $formData['tag'] = trim($fData['tag']); + + $isUrl = Summoner::validate($formData['url'],'url'); + + if($isUrl === true && !empty($formData['title'])) { + $hash = md5($formData['url']); + + # categories and tag stuff + $catArr = Summoner::prepareTagOrCategoryStr($formData['category']); + $tagArr = Summoner::prepareTagOrCategoryStr($formData['tag']); + + $search = $formData['title']; + $search .= ' '.$formData['description']; + $search .= ' '.implode(" ",$tagArr); + $search .= ' '.implode(" ",$catArr); + + $DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE); + + $linkObj = new Link($DB); + $linkID = $linkObj->create(array( + 'hash' => $hash, + 'search' => $search, + 'link' => $formData['url'], + 'status' => $formData['private'], + 'description' => $formData['description'], + 'title' => $formData['title'], + 'image' => $formData['image'] + ),true); + + if(!empty($linkID)) { + + if(!empty($catArr)) { + foreach($catArr as $c) { + $catObj = new Category($DB); + $catObj->initbystring($c); + $catObj->setRelation($linkID); + + unset($catObj); + } + } + if(!empty($tagArr)) { + foreach($tagArr as $t) { + $tagObj = new Tag($DB); + $tagObj->initbystring($t); + $tagObj->setRelation($linkID); + + unset($tagObj); + } + } + + $DB->commit(); + + $submitFeedback['message'] = 'Link added successfully.'; + $submitFeedback['status'] = 'success'; + $TemplateData['refresh'] = 'index.php?p=linkinfo&id='.$hash; + } + else { + $DB->rollback(); + $submitFeedback['message'] = 'Something went wrong...'; + $submitFeedback['status'] = 'error'; + $showAddForm = true; + } + } + else { + $submitFeedback['message'] = 'Please provide a valid URL and title.'; + $submitFeedback['status'] = 'error'; + $showAddForm = true; + } } $existingCategories = $Management->categories(); -- 2.39.5