Esempio n. 1
0
 /**
  * Add a bookmark
  * @param $userid UserId
  * @param IDb $db Database Interface
  * @param string $url
  * @param string $title Name of the bookmark
  * @param array $tags Simple array of tags to qualify the bookmark (different tags are taken from values)
  * @param string $description A longer description about the bookmark
  * @param boolean $public True if the bookmark is publishable to not registered users
  * @return int The id of the bookmark created
  */
 public static function addBookmark($userid, IDb $db, $url, $title, $tags = array(), $description = '', $is_public = false)
 {
     $public = $is_public ? 1 : 0;
     $url_without_prefix = substr($url, strpos($url, "://") + 3);
     // Removes everything from the url before the "://" pattern (included)
     $enc_url_noprefix = htmlspecialchars_decode($url_without_prefix);
     $enc_url = htmlspecialchars_decode($url);
     // Change lastmodified date if the record if already exists
     $sql = "SELECT * from  `*PREFIX*bookmarks` WHERE `url` like ? AND `user_id` = ?";
     $query = $db->prepareQuery($sql, 1);
     $result = $query->execute(array('%' . $enc_url_noprefix, $userid));
     // Find url in the db independantly from its protocol
     if ($row = $result->fetchRow()) {
         $params = array();
         $title_str = '';
         if (trim($title) != '') {
             // Do we replace the old title
             $title_str = ' , title = ?';
             $params[] = $title;
         }
         $desc_str = '';
         if (trim($description) != '') {
             // Do we replace the old description
             $desc_str = ' , description = ?';
             $params[] = $description;
         }
         $sql = "UPDATE `*PREFIX*bookmarks` SET `lastmodified` = " . "UNIX_TIMESTAMP() {$title_str} {$desc_str} , `url` = ? WHERE `url` like ? and `user_id` = ?";
         $params[] = $enc_url;
         $params[] = '%' . $enc_url_noprefix;
         $params[] = $userid;
         $query = $db->prepareQuery($sql);
         $query->execute($params);
         return $row['id'];
     } else {
         $query = $db->prepareQuery("\n\t\t\tINSERT INTO `*PREFIX*bookmarks`\n\t\t\t(`url`, `title`, `user_id`, `public`, `added`, `lastmodified`, `description`)\n\t\t\tVALUES (?, ?, ?, ?, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), ?)\n\t\t\t");
         $params = array($enc_url, htmlspecialchars_decode($title), $userid, $public, $description);
         $query->execute($params);
         $b_id = $db->getInsertId('*PREFIX*bookmarks');
         if ($b_id !== false) {
             self::addTags($db, $b_id, $tags);
             return $b_id;
         }
     }
 }
 /**
  * Add a bookmark
  * @param $userid UserId
  * @param IDb $db Database Interface
  * @param string $url
  * @param string $title Name of the bookmark
  * @param array $tags Simple array of tags to qualify the bookmark (different tags are taken from values)
  * @param string $description A longer description about the bookmark
  * @param boolean $public True if the bookmark is publishable to not registered users
  * @return int The id of the bookmark created
  */
 public static function addBookmark($userid, IDb $db, $url, $title, $tags = array(), $description = '', $is_public = false)
 {
     $public = $is_public ? 1 : 0;
     $enc_url = htmlspecialchars_decode($url);
     // Change lastmodified date if the record if already exists
     $sql = "SELECT * from  `*PREFIX*bookmarks` WHERE `url` = ? AND `user_id` = ?";
     $query = $db->prepareQuery($sql, 1);
     $result = $query->execute(array($enc_url, $userid));
     if ($row = $result->fetchRow()) {
         $params = array();
         $title_str = '';
         if (trim($title) != '') {
             // Do we replace the old title
             $title_str = ' , title = ?';
             $params[] = $title;
         }
         $desc_str = '';
         if (trim($description) != '') {
             // Do we replace the old description
             $desc_str = ' , description = ?';
             $params[] = $description;
         }
         $sql = "UPDATE `*PREFIX*bookmarks` SET `lastmodified` = " . "UNIX_TIMESTAMP() {$title_str} {$desc_str} WHERE `url` = ? and `user_id` = ?";
         $params[] = $enc_url;
         $params[] = $userid;
         $query = $db->prepareQuery($sql);
         $query->execute($params);
         return $row['id'];
     } else {
         $query = $db->prepareQuery("\n\t\t\tINSERT INTO `*PREFIX*bookmarks`\n\t\t\t(`url`, `title`, `user_id`, `public`, `added`, `lastmodified`, `description`)\n\t\t\tVALUES (?, ?, ?, ?, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), ?)\n\t\t\t");
         $params = array($enc_url, htmlspecialchars_decode($title), $userid, $public, $description);
         $query->execute($params);
         $b_id = $db->getInsertId('*PREFIX*bookmarks');
         if ($b_id !== false) {
             self::addTags($db, $b_id, $tags);
             return $b_id;
         }
     }
 }