From ff948b8e6645fb2e779d6172349635d2892b0dd1 Mon Sep 17 00:00:00 2001 From: Banana Date: Sun, 10 Sep 2023 00:07:01 +0200 Subject: [PATCH] try/catch, logging and cleanup --- webroot/lib/category.class.php | 365 ++++++----- webroot/lib/link.class.php | 1110 +++++++++++++++++--------------- 2 files changed, 781 insertions(+), 694 deletions(-) diff --git a/webroot/lib/category.class.php b/webroot/lib/category.class.php index 906b9c4..f1f74d3 100644 --- a/webroot/lib/category.class.php +++ b/webroot/lib/category.class.php @@ -3,7 +3,7 @@ * Insipid * Personal web-bookmark-system * - * Copyright 2016-2022 Johannes Keßler + * Copyright 2016-2023 Johannes Keßler * * Development starting from 2011: Johannes Keßler * https://www.bananas-playground.net/projekt/insipid/ @@ -27,196 +27,233 @@ */ class Category { - /** - * the database object - * @var mysqli - */ - private mysqli $DB; - - /** - * the current loaded category by DB id - * @var string - */ - private string $_id; - - /** - * current loaded tag data - * @var array - */ - private array $_data; - - /** - * @param mysqli $databaseConnectionObject - */ - public function __construct(mysqli $databaseConnectionObject) { - $this->DB = $databaseConnectionObject; - } - - /** - * by given string load the info from the DB and even create if not existing - * - * @param string $string - * @param bool $doNotCreate - * @return int 0=fail, 1=existing, 2=new, 3=newNotCreated - */ + /** + * the database object + * @var mysqli + */ + private mysqli $DB; + + /** + * the current loaded category by DB id + * @var string + */ + private string $_id; + + /** + * current loaded tag data + * @var array + */ + private array $_data; + + /** + * @param mysqli $databaseConnectionObject + */ + public function __construct(mysqli $databaseConnectionObject) { + $this->DB = $databaseConnectionObject; + } + + /** + * by given string load the info from the DB and even create if not existing + * + * @param string $string + * @param bool $doNotCreate + * @return int 0=fail, 1=existing, 2=new, 3=newNotCreated + */ public function initbystring(string $string, bool $doNotCreate=false): int { $ret = 0; - $this->_id = false; + $this->_id = false; if(!empty($string)) { $queryStr = "SELECT `id`,`name` 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']; - $this->_data = $result; - $ret = 1; - } - else { - if(!$doNotCreate) { - $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; - $this->_data['id'] = $this->_id; - $this->_data['name'] = $string; - $ret = 2; - } + WHERE `name` = '".$this->DB->real_escape_string($string)."'"; + + if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr)); + + try { + $query = $this->DB->query($queryStr); + if(!empty($query) && $query->num_rows > 0) { + $result = $query->fetch_assoc(); + $this->_id = $result['id']; + $this->_data = $result; + $ret = 1; } else { - $ret = 3; + if(!$doNotCreate) { + $queryStr = "INSERT INTO `" . DB_PREFIX . "_category` + SET `name` = '" . $this->DB->real_escape_string($string) . "'"; + + if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr)); + + $this->DB->query($queryStr); + if (!empty($this->DB->insert_id)) { + $this->_id = $this->DB->insert_id; + $this->_data['id'] = $this->_id; + $this->_data['name'] = $string; + $ret = 2; + } + } + else { + $ret = 3; + } + } + } catch (Exception $e) { + Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage()); + } + } + return $ret; + } + + /** + * by given DB table id load all the info we need + * + * @param string $id + * @return string + */ + public function initbyid(string $id): string { + $this->_id = 0; + + if(!empty($id)) { + $queryStr = "SELECT id,name + FROM `".DB_PREFIX."_category` + WHERE `id` = '".$this->DB->real_escape_string($id)."'"; + + if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr)); + + try { + $query = $this->DB->query($queryStr); + if(!empty($query) && $query->num_rows > 0) { + $result = $query->fetch_assoc(); + $this->_id = $id; + $this->_data = $result; } + } catch (Exception $e) { + Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage()); + } + } + + return $this->_id; + } + + /** + * return all or data for given key on the current loaded category + * + * @param string $key + * @return string|array + */ + public function getData(string $key=''): string|array { + $ret = $this->_data; + + if(!empty($key) && isset($this->_data[$key])) { + $ret = $this->_data[$key]; + } + + return $ret; + } + + /** + * set the relation to the given link to the loaded category + * + * @param string $linkid + * @return void + */ + public function setRelation(string $linkid): void { + 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)."'"; + + if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr)); + + try { + $this->DB->query($queryStr); + } catch (Exception $e) { + Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage()); } } - return $ret; - } - - /** - * by given DB table id load all the info we need - * - * @param string $id - * @return string - */ - public function initbyid(string $id): string { - $this->_id = 0; - - if(!empty($id)) { - $queryStr = "SELECT id,name - FROM `".DB_PREFIX."_category` - WHERE `id` = '".$this->DB->real_escape_string($id)."'"; - $query = $this->DB->query($queryStr); - if(!empty($query) && $query->num_rows > 0) { - $result = $query->fetch_assoc(); - $this->_id = $id; - $this->_data = $result; - } - } - - return $this->_id; - } - - /** - * return all or data for given key on the current loaded category - * - * @param string $key - * @return string|array - */ - public function getData(string $key=''): string|array { - $ret = $this->_data; - - if(!empty($key) && isset($this->_data[$key])) { - $ret = $this->_data[$key]; - } - - return $ret; - } - - /** - * set the relation to the given link to the loaded category - * - * @param string $linkid - * @return void - */ - public function setRelation(string $linkid): void { - 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); - } - } + } /** * Return an array of any linkid related to the current loaded category - * + * * @return array */ public function getRelations(): array { $ret = array(); $queryStr = "SELECT linkid - FROM `".DB_PREFIX."_categoryrelation` - WHERE `categoryid` = '".$this->DB->real_escape_string($this->_id)."'"; - $query = $this->DB->query($queryStr); - if(!empty($query) && $query->num_rows > 0) { - while($result = $query->fetch_assoc()) { - $ret[] = $result['linkid']; + FROM `".DB_PREFIX."_categoryrelation` + WHERE `categoryid` = '".$this->DB->real_escape_string($this->_id)."'"; + + if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr)); + + try { + $query = $this->DB->query($queryStr); + if(!empty($query) && $query->num_rows > 0) { + while($result = $query->fetch_assoc()) { + $ret[] = $result['linkid']; + } + } + } catch (Exception $e) { + Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage()); + } + + return $ret; + } + + /** + * deletes the current loaded category from db + * + * @return boolean + */ + public function delete(): bool { + $ret = false; + + if(!empty($this->_id)) { + $this->DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE); + + $queryStr = "DELETE + FROM `".DB_PREFIX."_categoryrelation` + WHERE `categoryid` = '".$this->DB->real_escape_string($this->_id)."'"; + $this->DB->query($queryStr); + + $queryStr = "DELETE + FROM `".DB_PREFIX."_category` + WHERE `id` = '".$this->DB->real_escape_string($this->_id)."'"; + + if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr)); + + try { + $this->DB->query($queryStr); + $this->DB->commit(); + $ret = true; + } catch (Exception $e) { + Summoner::sysLog('ERROR Failed to remove category: '.var_export($e->getMessage(),true)); + $this->DB->rollback(); } } return $ret; } - /** - * deletes the current loaded category from db - * - * @return boolean - */ - public function delete(): bool { - $ret = false; - - if(!empty($this->_id)) { - $this->DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE); - - try { - $queryStr = "DELETE - FROM `".DB_PREFIX."_categoryrelation` - WHERE `categoryid` = '".$this->DB->real_escape_string($this->_id)."'"; - $this->DB->query($queryStr); - - $queryStr = "DELETE - FROM `".DB_PREFIX."_category` - WHERE `id` = '".$this->DB->real_escape_string($this->_id)."'"; - $this->DB->query($queryStr); - - $this->DB->commit(); - $ret = true; - } catch (Exception $e) { - if(DEBUG) { - var_dump($e->getMessage()); - } - error_log('ERROR Failed to remove category: '.var_export($e->getMessage(),true)); - - $this->DB->rollback(); - } - } - - return $ret; - } - - /** - * Rename current loaded cat name - * - * @param string $newValue - * @return void - */ + /** + * Rename current loaded cat name + * + * @param string $newValue + * @return void + */ public function rename(string $newValue): void { if(!empty($newValue)) { $queryStr = "UPDATE `".DB_PREFIX."_category` - SET `name` = '".$this->DB->real_escape_string($newValue)."' - WHERE `id` = '".$this->DB->real_escape_string($this->_id)."'"; - $this->DB->query($queryStr); + SET `name` = '".$this->DB->real_escape_string($newValue)."' + WHERE `id` = '".$this->DB->real_escape_string($this->_id)."'"; + + if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr)); + + try { + $this->DB->query($queryStr); + } catch (Exception $e) { + Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage()); + } + $this->_data['name'] = $newValue; } } diff --git a/webroot/lib/link.class.php b/webroot/lib/link.class.php index 2e304c1..fb0c4ce 100644 --- a/webroot/lib/link.class.php +++ b/webroot/lib/link.class.php @@ -31,164 +31,177 @@ */ class Link { - /** - * the database object - * - * @var mysqli - */ - private mysqli $DB; - - /** - * the current loaded link data - * - * @var array - */ - private array $_data; - - /** - * Link constructor. - * - * @param mysqli $databaseConnectionObject - */ - public function __construct(mysqli $databaseConnectionObject) { - $this->DB = $databaseConnectionObject; - } - - /** - * load all the info we have about a link by given hash - * - * @param string $hash - * @return array - */ - public function load(string $hash): array { - - $this->_data = array(); - - if (!empty($hash)) { - $queryStr = "SELECT - `id`, - `link`, - `created`, - `updated`, - `status`, - `description`, - `title`, - `image`, - `hash` - FROM `".DB_PREFIX."_link` - WHERE `hash` = '" . $this->DB->real_escape_string($hash) . "'"; - $query = $this->DB->query($queryStr); - if (!empty($query) && $query->num_rows == 1) { - $this->_data = $query->fetch_assoc(); - - # add stuff - $this->_tags(); - $this->_categories(); - $this->_image(); - $this->_private(); - $this->_snapshot(); - $this->_pageScreenshot(); - } - } - - return $this->_data; - } - - /** - * loads only the info needed to display the link - * for edit use $this->load - * - * @param string $hash - * @return array - */ - public function loadShortInfo(string $hash): array { - $this->_data = array(); - - if (!empty($hash)) { - $queryStr = "SELECT `id`,`link`,`description`,`title`,`image`,`hash`, `created` - FROM `".DB_PREFIX."_link` - WHERE `hash` = '" . $this->DB->real_escape_string($hash) . "'"; - - $query = $this->DB->query($queryStr); - if (!empty($query) && $query->num_rows == 1) { - $this->_data = $query->fetch_assoc(); - - # add stuff - $this->_image(); - } - } - - return $this->_data; - } - - /** - * Get shortinfo from given data array - * - * @param array $data - * @return array - */ - public function loadFromDataShortInfo(array $data): array { - $this->_data = array(); - - if(isset($data['id']) && isset($data['link']) && isset($data['created']) && isset($data['status']) - && isset($data['title']) && isset($data['hash']) && isset($data['description']) && isset($data['image'])) { - $this->_data = $data; - $this->_image(); - } - - return $this->_data; - } - - /** - * return all or data for given key on the current loaded link - * - * @param string $key - * @return string|array - */ - public function getData(string $key = ''): string|array { - $ret = $this->_data; - - if (!empty($key) && isset($this->_data[$key])) { - $ret = $this->_data[$key]; - } - - return $ret; - } - - /** - * reload the current id from DB - * - * @return void - */ - public function reload(): void { - $this->load($this->_data['hash']); - } - - /** - * create a new link with the given data - * - * @param array $data - * @param bool $returnId - * @return string - */ - public function create(array $data, bool $returnId = false): string { - $ret = ''; - - if (!isset($data['link']) || empty($data['link'])) return $ret; - if (!isset($data['hash']) || empty($data['hash'])) return $ret; - if (!isset($data['title']) || empty($data['title'])) return $ret; - - $_t = parse_url($data['link']); - $data['search'] = $data['title']; - $data['search'] .= ' '.$data['description']; - $data['search'] .= ' '.implode(" ",$data['tagArr']); - $data['search'] .= ' '.implode(" ",$data['catArr']); - $data['search'] .= ' '.$_t['host']; - $data['search'] .= ' '.implode(' ',explode('/',$_t['path'])); - $data['search'] = trim($data['search']); - $data['search'] = strtolower($data['search']); - - $queryStr = "INSERT INTO `" . DB_PREFIX . "_link` SET + /** + * the database object + * + * @var mysqli + */ + private mysqli $DB; + + /** + * the current loaded link data + * + * @var array + */ + private array $_data; + + /** + * Link constructor. + * + * @param mysqli $databaseConnectionObject + */ + public function __construct(mysqli $databaseConnectionObject) { + $this->DB = $databaseConnectionObject; + } + + /** + * load all the info we have about a link by given hash + * + * @param string $hash + * @return array + */ + public function load(string $hash): array { + + $this->_data = array(); + + if (!empty($hash)) { + $queryStr = "SELECT + `id`, + `link`, + `created`, + `updated`, + `status`, + `description`, + `title`, + `image`, + `hash` + FROM `".DB_PREFIX."_link` + WHERE `hash` = '" . $this->DB->real_escape_string($hash) . "'"; + + if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr)); + + try { + $query = $this->DB->query($queryStr); + if (!empty($query) && $query->num_rows == 1) { + $this->_data = $query->fetch_assoc(); + + # add stuff + $this->_tags(); + $this->_categories(); + $this->_image(); + $this->_private(); + $this->_snapshot(); + $this->_pageScreenshot(); + } + } catch (Exception $e) { + Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage()); + } + } + + return $this->_data; + } + + /** + * loads only the info needed to display the link + * for edit use $this->load + * + * @param string $hash + * @return array + */ + public function loadShortInfo(string $hash): array { + $this->_data = array(); + + if (!empty($hash)) { + $queryStr = "SELECT `id`,`link`,`description`,`title`,`image`,`hash`, `created` + FROM `".DB_PREFIX."_link` + WHERE `hash` = '" . $this->DB->real_escape_string($hash) . "'"; + + if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr)); + + try { + $query = $this->DB->query($queryStr); + if (!empty($query) && $query->num_rows == 1) { + $this->_data = $query->fetch_assoc(); + + # add stuff + $this->_image(); + } + } catch (Exception $e) { + Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage()); + } + } + + return $this->_data; + } + + /** + * Get shortinfo from given data array + * + * @param array $data + * @return array + */ + public function loadFromDataShortInfo(array $data): array { + $this->_data = array(); + + if(isset($data['id']) && isset($data['link']) && isset($data['created']) && isset($data['status']) + && isset($data['title']) && isset($data['hash']) && isset($data['description']) && isset($data['image'])) { + $this->_data = $data; + $this->_image(); + } + + return $this->_data; + } + + /** + * return all or data for given key on the current loaded link + * + * @param string $key + * @return string|array + */ + public function getData(string $key = ''): string|array { + $ret = $this->_data; + + if (!empty($key) && isset($this->_data[$key])) { + $ret = $this->_data[$key]; + } + + return $ret; + } + + /** + * reload the current id from DB + * + * @return void + */ + public function reload(): void { + $this->load($this->_data['hash']); + } + + /** + * create a new link with the given data + * + * @param array $data + * @param bool $returnId + * @return string + */ + public function create(array $data, bool $returnId = false): string { + $ret = ''; + + if (!isset($data['link']) || empty($data['link'])) return $ret; + if (!isset($data['hash']) || empty($data['hash'])) return $ret; + if (!isset($data['title']) || empty($data['title'])) return $ret; + + $_t = parse_url($data['link']); + $data['search'] = $data['title']; + $data['search'] .= ' '.$data['description']; + $data['search'] .= ' '.implode(" ",$data['tagArr']); + $data['search'] .= ' '.implode(" ",$data['catArr']); + $data['search'] .= ' '.$_t['host']; + $data['search'] .= ' '.implode(' ',explode('/',$_t['path'])); + $data['search'] = trim($data['search']); + $data['search'] = strtolower($data['search']); + + $queryStr = "INSERT INTO `" . DB_PREFIX . "_link` SET `link` = '" . $this->DB->real_escape_string($data['link']) . "', `created` = NOW(), `status` = '" . $this->DB->real_escape_string($data['status']) . "', @@ -198,377 +211,414 @@ class Link { `hash` = '" . $this->DB->real_escape_string($data['hash']) . "', `search` = '" . $this->DB->real_escape_string($data['search']) . "'"; - $this->DB->query($queryStr); - if ($returnId === true) { - $ret = $this->DB->insert_id; - } - else { - error_log('ERROR Failed to create link: '.var_export($data,true)); - } - - return $ret; - } - - /** - * update the current loaded link with the given data - * - * @param array $data - * @return boolean - */ - public function update(array $data): bool { - $ret = false; - - if (isset($data['title']) && !empty($data['title']) && !empty($this->_data)) { - - # categories and tag stuff - $catArr = Summoner::prepareTagOrCategoryStr($data['category']); - $tagArr = Summoner::prepareTagOrCategoryStr($data['tag']); - - $_t = parse_url($this->_data['link']); - $search = $data['title']; - $search .= ' '.$data['description']; - $search .= ' '.implode(" ", $tagArr); - $search .= ' '.implode(" ", $catArr); - $search .= ' '.$_t['host']; - if(isset($_t['path'])) { - $search .= ' '.implode(' ',explode('/',$_t['path'])); - } + if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr)); + + try { + $this->DB->query($queryStr); + if ($returnId === true) { + $ret = $this->DB->insert_id; + } + else { + Summoner::sysLog('ERROR Failed to create link: '.var_export($data,true)); + } + } catch (Exception $e) { + Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage()); + } + + return $ret; + } + + /** + * update the current loaded link with the given data + * + * @param array $data + * @return boolean + */ + public function update(array $data): bool { + $ret = false; + + if (isset($data['title']) && !empty($data['title']) && !empty($this->_data)) { + + # categories and tag stuff + $catArr = Summoner::prepareTagOrCategoryStr($data['category']); + $tagArr = Summoner::prepareTagOrCategoryStr($data['tag']); + + $_t = parse_url($this->_data['link']); + $search = $data['title']; + $search .= ' '.$data['description']; + $search .= ' '.implode(" ", $tagArr); + $search .= ' '.implode(" ", $catArr); + $search .= ' '.$_t['host']; + if(isset($_t['path'])) { + $search .= ' '.implode(' ',explode('/',$_t['path'])); + } $search = trim($search); - $search = strtolower($search); - - $this->DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE); - - # 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']) . "', - `title` = '" . $this->DB->real_escape_string($data['title']) . "', - `image` = '" . $this->DB->real_escape_string($data['image']) . "', - `search` = '" . $this->DB->real_escape_string($search) . "' - WHERE `hash` = '" . $this->DB->real_escape_string($this->_data['hash']) . "'"; - $query = $this->DB->query($queryStr); - - if ($query !== false) { - $catObj = new Category($this->DB); - $tagObj = new Tag($this->DB); - // clean the relations first - $this->_removeTagRelation(); - $this->_removeCategoryRelation(); - - 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']); - } - } - - $this->DB->commit(); - - # decide to store or remove the image - if (isset($data['localImage'])) { - $image = ABSOLUTE_PATH . '/' . LOCAL_STORAGE . '/thumbnail-' . $this->_data['hash'].'.jpg'; - if(DEBUG) { - error_log("DEBUG Try to save local image to: $image"); - } - if ($data['localImage'] === true) { - if(DEBUG) { - error_log("DEBUG want to save local image to: $image"); - } - if (!file_exists($image) || $_imageUrlChanged === true) { - if(DEBUG) { - error_log("DEBUG Image new or not there yet: $image"); - } - Summoner::downloadFile($data['image'], $image); - } - } elseif ($data['localImage'] === false) { - if(DEBUG) { - error_log("DEBUG Image to be removed: $image"); - } - if (file_exists($image)) { - unlink($image); - } - } - } - - # decide if we want to make a local snapshot - if(isset($data['snapshot'])) { - $snapshot = ABSOLUTE_PATH . '/' . LOCAL_STORAGE . '/snapshot-' . $this->_data['hash'].'.jpg'; - if ($data['snapshot'] === true) { - if (!file_exists($snapshot) || $_imageUrlChanged === true) { - require_once 'lib/snapshot.class.php'; - $snap = new Snapshot(); - $do = $snap->doSnapshot($this->_data['link'], $snapshot); - if(empty($do)) { - error_log('ERROR Failed to create snapshot: '.var_export($data,true)); - } - } - } elseif ($data['snapshot'] === false) { - if (file_exists($snapshot)) { - unlink($snapshot); - } - } - } - - # decide if we want to make a local full page screenshot - if(isset($data['pagescreenshot'])) { - $pagescreenshot = ABSOLUTE_PATH . '/' . LOCAL_STORAGE . '/pagescreenshot-' . $this->_data['hash'].'.jpg'; - if ($data['pagescreenshot'] === true) { - if (!file_exists($pagescreenshot) || $_imageUrlChanged === true) { - require_once 'lib/snapshot.class.php'; - $snap = new Snapshot(); - $do = $snap->wholePageSnapshot($this->_data['link'], $pagescreenshot); - if(!empty($do)) { - error_log('ERROR Failed to create snapshot: '.var_export($data,true)); - } - } - } elseif ($data['pagescreenshot'] === false) { - if (file_exists($pagescreenshot)) { - unlink($pagescreenshot); - } - } - } - - $ret = true; - } else { - $this->DB->rollback(); - error_log('ERROR Failed to update link: '.var_export($data,true)); - } - - } - - return $ret; - } - - /** - * call this to delete all the relations to this link. - * To completely remove the link use Management->deleteLink() - * - * @return void - */ - public function deleteRelations(): void { - $this->_removeTagRelation(); - $this->_removeCategoryRelation(); - $this->_deleteImage(); - $this->_deleteSnapshot(); - $this->_deletePageScreenshot(); - } - - /** - * load all the tags we have to the already loaded link - * needs $this->load called first - * - * @return void - */ - private function _tags(): void { - $ret = array(); - - if (!empty($this->_data['hash'])) { - $queryStr = "SELECT - DISTINCT tag, tagId - FROM `" . DB_PREFIX . "_combined` - WHERE `hash` = '" . $this->DB->real_escape_string($this->_data['hash']) . "'"; - $query = $this->DB->query($queryStr); - if (!empty($query) && $query->num_rows > 0) { - while ($result = $query->fetch_assoc()) { - if ($result['tag'] !== NULL) { - $ret[$result['tagId']] = $result['tag']; - } - } - - } - } - - $this->_data['tags'] = $ret; - } - - /** - * load all the categories we have to the already loaded link - * needs $this->load called first - * - * @return void - */ - private function _categories(): void { - $ret = array(); - - if (!empty($this->_data['hash'])) { - $queryStr = "SELECT - DISTINCT category, categoryId - FROM `" . DB_PREFIX . "_combined` - WHERE `hash` = '" . $this->DB->real_escape_string($this->_data['hash']) . "'"; - $query = $this->DB->query($queryStr); - if (!empty($query) && $query->num_rows > 0) { - while ($result = $query->fetch_assoc()) { - if ($result['category'] !== NULL) { - $ret[$result['categoryId']] = $result['category']; - } - } - } - } - - $this->_data['categories'] = $ret; - } - - /** - * remove all or given tag relation to the current loaded link - * - * @param string $tagid - * @return void - */ - private function _removeTagRelation(string $tagid = ''): void { - if (!empty($this->_data['id'])) { - $queryStr = ''; - if (is_numeric($tagid)) { - $queryStr = "DELETE - FROM `" . DB_PREFIX . "_tagrelation` - WHERE `linkid` = '" . $this->DB->real_escape_string($this->_data['id']) . "' - AND `tagid` = '" . $this->DB->real_escape_string($tagid) . "'"; - - } else { - $queryStr = "DELETE - FROM `" . DB_PREFIX . "_tagrelation` - WHERE `linkid` = '" . $this->DB->real_escape_string($this->_data['id']) . "'"; - } - if (!empty($queryStr)) { - $this->DB->query($queryStr); - } - } - } - - /** - * remove all or given category relation to the current loaded link - * - * @param string $categoryid - * @return void - */ - private function _removeCategoryRelation(string $categoryid=''): void { - if (!empty($this->_data['id'])) { - $queryStr = ''; - if (is_numeric($categoryid)) { - $queryStr = "DELETE - FROM `" . DB_PREFIX . "_categoryrelation` - WHERE `linkid` = '" . $this->DB->real_escape_string($this->_data['id']) . "' - AND `categoryid` = '" . $this->DB->real_escape_string($categoryid) . "'"; - } else { - $queryStr = "DELETE - FROM `" . DB_PREFIX . "_categoryrelation` - WHERE `linkid` = '" . $this->DB->real_escape_string($this->_data['id']) . "'"; - } - if (!empty($queryStr)) { - $this->DB->query($queryStr); - } - } - } - - /** - * determine of we have a local stored image - * if so populate the localImage attribute - * - * @return void - */ - private function _image(): void { - if (!empty($this->_data['hash'])) { - $this->_data['imageToShow'] = $this->_data['image']; - $image = ABSOLUTE_PATH.'/'.LOCAL_STORAGE.'/thumbnail-'.$this->_data['hash'].'.jpg'; - if (file_exists($image)) { - $this->_data['imageToShow'] = LOCAL_STORAGE.'/thumbnail-'.$this->_data['hash'].'.jpg'; - $this->_data['localImage'] = true; - } - } - } - - /** - * determine if we have a local stored snapshot - * if so populate the snapshotLink attribute - * - * @return void - */ - private function _snapshot(): void { - if (!empty($this->_data['hash'])) { - $snapshot = ABSOLUTE_PATH.'/'.LOCAL_STORAGE.'/snapshot-'.$this->_data['hash'].'.jpg'; - if (file_exists($snapshot)) { - $this->_data['snapshotLink'] = LOCAL_STORAGE.'/snapshot-'.$this->_data['hash'].'.jpg'; - $this->_data['snapshot'] = true; - } - } - } - - /** - * determine if we have a local full page screenshot - * if so populate the pagescreenshotLink attribute - * - * @return void - */ - private function _pageScreenshot(): void { - if (!empty($this->_data['hash'])) { - $pagescreenshot = ABSOLUTE_PATH.'/'.LOCAL_STORAGE.'/pagescreenshot-'.$this->_data['hash'].'.jpg'; - if (file_exists($pagescreenshot)) { - $this->_data['pagescreenshotLink'] = LOCAL_STORAGE.'/pagescreenshot-'.$this->_data['hash'].'.jpg'; - $this->_data['pagescreenshot'] = true; - } - } - } - - /** - * remove the local stored image - * - * @return void - */ - private function _deleteImage(): void { - if (!empty($this->_data['hash']) && !empty($this->_data['imageToShow'])) { - $image = ABSOLUTE_PATH.'/'.$this->_data['imageToShow']; - if (file_exists($image)) { - unlink($image); - } - } - } - - /** - * remove the local stored snapshot - * - * @return void - */ - private function _deleteSnapshot(): void { - if (!empty($this->_data['hash']) && !empty($this->_data['snapshotLink'])) { - $snapshot = LOCAL_STORAGE.'/snapshot-'.$this->_data['hash'].'.jpg'; - if (file_exists($snapshot)) { - unlink($snapshot); - } - } - } - - /** - * remove the local stored pagescreenshot - * - * @return void - */ - private function _deletePageScreenshot(): void { - if (!empty($this->_data['hash']) && !empty($this->_data['pagescreenshotLink'])) { - $pagescreenshot = LOCAL_STORAGE.'/pagescreenshot-'.$this->_data['hash'].'.jpg'; - if (file_exists($pagescreenshot)) { - unlink($pagescreenshot); - } - } - } - - /** - * check if the status is private and set the info - * - * @return void - */ - private function _private(): void { - if (!empty($this->_data['status']) && $this->_data['status'] == "1") { - $this->_data['private'] = "1"; - } - } + $search = strtolower($search); + + # 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']) . "', + `title` = '" . $this->DB->real_escape_string($data['title']) . "', + `image` = '" . $this->DB->real_escape_string($data['image']) . "', + `search` = '" . $this->DB->real_escape_string($search) . "' + WHERE `hash` = '" . $this->DB->real_escape_string($this->_data['hash']) . "'"; + + if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr)); + + $this->DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE); + try { + $query = $this->DB->query($queryStr); + } catch (Exception $e) { + Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage()); + } + + + if ($query !== false) { + $catObj = new Category($this->DB); + $tagObj = new Tag($this->DB); + // clean the relations first + $this->_removeTagRelation(); + $this->_removeCategoryRelation(); + + 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']); + } + } + + $this->DB->commit(); + + # decide to store or remove the image + if (isset($data['localImage'])) { + $image = ABSOLUTE_PATH . '/' . LOCAL_STORAGE . '/thumbnail-' . $this->_data['hash'].'.jpg'; + + if(DEBUG) Summoner::sysLog("DEBUG Try to save local image to: $image"); + + if ($data['localImage'] === true) { + if(DEBUG) Summoner::sysLog("DEBUG want to save local image to: $image"); + + if (!file_exists($image) || $_imageUrlChanged === true) { + if(DEBUG) Summoner::sysLog("DEBUG Image new or not there yet: $image"); + + Summoner::downloadFile($data['image'], $image); + } + } elseif ($data['localImage'] === false) { + if(DEBUG) Summoner::sysLog("DEBUG Image to be removed: $image"); + + if (file_exists($image)) { + unlink($image); + } + } + } + + # decide if we want to make a local snapshot + if(isset($data['snapshot'])) { + $snapshot = ABSOLUTE_PATH . '/' . LOCAL_STORAGE . '/snapshot-' . $this->_data['hash'].'.jpg'; + if ($data['snapshot'] === true) { + if (!file_exists($snapshot) || $_imageUrlChanged === true) { + require_once 'lib/snapshot.class.php'; + $snap = new Snapshot(); + $do = $snap->doSnapshot($this->_data['link'], $snapshot); + if(empty($do)) { + Summoner::sysLog('ERROR Failed to create snapshot: '.var_export($data,true)); + } + } + } elseif ($data['snapshot'] === false) { + if (file_exists($snapshot)) { + unlink($snapshot); + } + } + } + + # decide if we want to make a local full page screenshot + if(isset($data['pagescreenshot'])) { + $pagescreenshot = ABSOLUTE_PATH . '/' . LOCAL_STORAGE . '/pagescreenshot-' . $this->_data['hash'].'.jpg'; + if ($data['pagescreenshot'] === true) { + if (!file_exists($pagescreenshot) || $_imageUrlChanged === true) { + require_once 'lib/snapshot.class.php'; + $snap = new Snapshot(); + $do = $snap->wholePageSnapshot($this->_data['link'], $pagescreenshot); + if(!empty($do)) { + Summoner::sysLog('ERROR Failed to create snapshot: '.var_export($data,true)); + } + } + } elseif ($data['pagescreenshot'] === false) { + if (file_exists($pagescreenshot)) { + unlink($pagescreenshot); + } + } + } + + $ret = true; + } else { + $this->DB->rollback(); + Summoner::sysLog('ERROR Failed to update link: '.var_export($data,true)); + } + + } + + return $ret; + } + + /** + * call this to delete all the relations to this link. + * To completely remove the link use Management->deleteLink() + * + * @return void + */ + public function deleteRelations(): void { + $this->_removeTagRelation(); + $this->_removeCategoryRelation(); + $this->_deleteImage(); + $this->_deleteSnapshot(); + $this->_deletePageScreenshot(); + } + + /** + * load all the tags we have to the already loaded link + * needs $this->load called first + * + * @return void + */ + private function _tags(): void { + $ret = array(); + + if (!empty($this->_data['hash'])) { + $queryStr = "SELECT + DISTINCT tag, tagId + FROM `" . DB_PREFIX . "_combined` + WHERE `hash` = '" . $this->DB->real_escape_string($this->_data['hash']) . "'"; + + if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr)); + + try { + $query = $this->DB->query($queryStr); + if (!empty($query) && $query->num_rows > 0) { + while ($result = $query->fetch_assoc()) { + if ($result['tag'] !== NULL) { + $ret[$result['tagId']] = $result['tag']; + } + } + } + } catch (Exception $e) { + Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage()); + } + } + + $this->_data['tags'] = $ret; + } + + /** + * load all the categories we have to the already loaded link + * needs $this->load called first + * + * @return void + */ + private function _categories(): void { + $ret = array(); + + if (!empty($this->_data['hash'])) { + $queryStr = "SELECT + DISTINCT category, categoryId + FROM `" . DB_PREFIX . "_combined` + WHERE `hash` = '" . $this->DB->real_escape_string($this->_data['hash']) . "'"; + + if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr)); + + try { + $query = $this->DB->query($queryStr); + if (!empty($query) && $query->num_rows > 0) { + while ($result = $query->fetch_assoc()) { + if ($result['category'] !== NULL) { + $ret[$result['categoryId']] = $result['category']; + } + } + } + } catch (Exception $e) { + Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage()); + } + } + + $this->_data['categories'] = $ret; + } + + /** + * remove all or given tag relation to the current loaded link + * + * @param string $tagid + * @return void + */ + private function _removeTagRelation(string $tagid = ''): void { + if (!empty($this->_data['id'])) { + $queryStr = ''; + if (is_numeric($tagid)) { + $queryStr = "DELETE + FROM `" . DB_PREFIX . "_tagrelation` + WHERE `linkid` = '" . $this->DB->real_escape_string($this->_data['id']) . "' + AND `tagid` = '" . $this->DB->real_escape_string($tagid) . "'"; + + } else { + $queryStr = "DELETE + FROM `" . DB_PREFIX . "_tagrelation` + WHERE `linkid` = '" . $this->DB->real_escape_string($this->_data['id']) . "'"; + } + + if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr)); + + if (!empty($queryStr)) { + try { + $this->DB->query($queryStr); + } catch (Exception $e) { + Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage()); + } + } + } + } + + /** + * remove all or given category relation to the current loaded link + * + * @param string $categoryid + * @return void + */ + private function _removeCategoryRelation(string $categoryid=''): void { + if (!empty($this->_data['id'])) { + $queryStr = ''; + if (is_numeric($categoryid)) { + $queryStr = "DELETE + FROM `" . DB_PREFIX . "_categoryrelation` + WHERE `linkid` = '" . $this->DB->real_escape_string($this->_data['id']) . "' + AND `categoryid` = '" . $this->DB->real_escape_string($categoryid) . "'"; + } else { + $queryStr = "DELETE + FROM `" . DB_PREFIX . "_categoryrelation` + WHERE `linkid` = '" . $this->DB->real_escape_string($this->_data['id']) . "'"; + } + + if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr)); + + if (!empty($queryStr)) { + try { + $this->DB->query($queryStr); + } catch (Exception $e) { + Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage()); + } + } + } + } + + /** + * determine of we have a local stored image + * if so populate the localImage attribute + * + * @return void + */ + private function _image(): void { + if (!empty($this->_data['hash'])) { + $this->_data['imageToShow'] = $this->_data['image']; + $image = ABSOLUTE_PATH.'/'.LOCAL_STORAGE.'/thumbnail-'.$this->_data['hash'].'.jpg'; + if (file_exists($image)) { + $this->_data['imageToShow'] = LOCAL_STORAGE.'/thumbnail-'.$this->_data['hash'].'.jpg'; + $this->_data['localImage'] = true; + } + } + } + + /** + * determine if we have a local stored snapshot + * if so populate the snapshotLink attribute + * + * @return void + */ + private function _snapshot(): void { + if (!empty($this->_data['hash'])) { + $snapshot = ABSOLUTE_PATH.'/'.LOCAL_STORAGE.'/snapshot-'.$this->_data['hash'].'.jpg'; + if (file_exists($snapshot)) { + $this->_data['snapshotLink'] = LOCAL_STORAGE.'/snapshot-'.$this->_data['hash'].'.jpg'; + $this->_data['snapshot'] = true; + } + } + } + + /** + * determine if we have a local full page screenshot + * if so populate the pagescreenshotLink attribute + * + * @return void + */ + private function _pageScreenshot(): void { + if (!empty($this->_data['hash'])) { + $pagescreenshot = ABSOLUTE_PATH.'/'.LOCAL_STORAGE.'/pagescreenshot-'.$this->_data['hash'].'.jpg'; + if (file_exists($pagescreenshot)) { + $this->_data['pagescreenshotLink'] = LOCAL_STORAGE.'/pagescreenshot-'.$this->_data['hash'].'.jpg'; + $this->_data['pagescreenshot'] = true; + } + } + } + + /** + * remove the local stored image + * + * @return void + */ + private function _deleteImage(): void { + if (!empty($this->_data['hash']) && !empty($this->_data['imageToShow'])) { + $image = ABSOLUTE_PATH.'/'.$this->_data['imageToShow']; + if (file_exists($image)) { + unlink($image); + } + } + } + + /** + * remove the local stored snapshot + * + * @return void + */ + private function _deleteSnapshot(): void { + if (!empty($this->_data['hash']) && !empty($this->_data['snapshotLink'])) { + $snapshot = LOCAL_STORAGE.'/snapshot-'.$this->_data['hash'].'.jpg'; + if (file_exists($snapshot)) { + unlink($snapshot); + } + } + } + + /** + * remove the local stored pagescreenshot + * + * @return void + */ + private function _deletePageScreenshot(): void { + if (!empty($this->_data['hash']) && !empty($this->_data['pagescreenshotLink'])) { + $pagescreenshot = LOCAL_STORAGE.'/pagescreenshot-'.$this->_data['hash'].'.jpg'; + if (file_exists($pagescreenshot)) { + unlink($pagescreenshot); + } + } + } + + /** + * check if the status is private and set the info + * + * @return void + */ + private function _private(): void { + if (!empty($this->_data['status']) && $this->_data['status'] == "1") { + $this->_data['private'] = "1"; + } + } } -- 2.39.5