aboutsummaryrefslogtreecommitdiff
path: root/services
diff options
context:
space:
mode:
Diffstat (limited to 'services')
-rw-r--r--services/bookmarkservice.php416
-rw-r--r--services/cacheservice.php38
-rw-r--r--services/servicefactory.php33
-rw-r--r--services/tagservice.php363
-rw-r--r--services/templateservice.php46
-rw-r--r--services/userservice.php362
6 files changed, 1258 insertions, 0 deletions
diff --git a/services/bookmarkservice.php b/services/bookmarkservice.php
new file mode 100644
index 0000000..afc7179
--- /dev/null
+++ b/services/bookmarkservice.php
@@ -0,0 +1,416 @@
+<?php
+class BookmarkService {
+ var $db;
+
+ function & getInstance(& $db) {
+ static $instance;
+ if (!isset ($instance))
+ $instance = & new BookmarkService($db);
+ return $instance;
+ }
+
+ function BookmarkService(& $db) {
+ $this->db = & $db;
+ }
+
+ function _getbookmark($fieldname, $value, $all = false) {
+ if (!$all) {
+ $userservice = & ServiceFactory :: getServiceInstance('UserService');
+ $sId = $userservice->getCurrentUserId();
+ $range = ' AND uId = '. $sId;
+ }
+
+ $query = 'SELECT * FROM '. $GLOBALS['tableprefix'] .'bookmarks WHERE '. $fieldname .' = "'. $this->db->sql_escape($value) .'"'. $range;
+
+ if (!($dbresult = & $this->db->sql_query_limit($query, 1, 0))) {
+ message_die(GENERAL_ERROR, 'Could not get bookmark', '', __LINE__, __FILE__, $query, $this->db);
+ return false;
+ }
+
+ if ($row =& $this->db->sql_fetchrow($dbresult)) {
+ return $row;
+ } else {
+ return false;
+ }
+ }
+
+ function & getBookmark($bid, $include_tags = false) {
+ if (!is_numeric($bid))
+ return;
+
+ $sql = 'SELECT * FROM '. $GLOBALS['tableprefix'] .'bookmarks WHERE bId = '. $this->db->sql_escape($bid);
+
+ if (!($dbresult = & $this->db->sql_query($sql)))
+ message_die(GENERAL_ERROR, 'Could not get vars', '', __LINE__, __FILE__, $sql, $this->db);
+
+ if ($row = & $this->db->sql_fetchrow($dbresult)) {
+ if ($include_tags) {
+ $tagservice = & ServiceFactory :: getServiceInstance('TagService');
+ $row['tags'] = $tagservice->getTagsForBookmark($bid);
+ }
+ return $row;
+ } else {
+ return false;
+ }
+ }
+
+ function getBookmarkByAddress($address) {
+ $hash = md5($address);
+ return $this->getBookmarkByHash($hash);
+ }
+
+ function getBookmarkByHash($hash) {
+ return $this->_getbookmark('bHash', $hash, true);
+ }
+
+ function editAllowed($bookmark) {
+ if (!is_numeric($bookmark) && (!is_array($bookmark) || !is_numeric($bookmark['bId'])))
+ return false;
+
+ if (!is_array($bookmark))
+ if (!($bookmark = $this->getBookmark($bookmark)))
+ return false;
+
+ $userservice = & ServiceFactory :: getServiceInstance('UserService');
+ $userid = $userservice->getCurrentUserId();
+ if ($userservice->isAdmin($userid))
+ return true;
+ else
+ return ($bookmark['uId'] == $userid);
+ }
+
+ function bookmarkExists($address = false, $uid = NULL) {
+ if (!$address) {
+ return;
+ }
+
+ // If address doesn't contain ":", add "http://" as the default protocol
+ if (strpos($address, ':') === false) {
+ $address = 'http://'. $address;
+ }
+
+ $crit = array ('bHash' => md5($address));
+ if (isset ($uid)) {
+ $crit['uId'] = $uid;
+ }
+
+ $sql = 'SELECT COUNT(*) FROM '. $GLOBALS['tableprefix'] .'bookmarks WHERE '. $this->db->sql_build_array('SELECT', $crit);
+ if (!($dbresult = & $this->db->sql_query($sql))) {
+ message_die(GENERAL_ERROR, 'Could not get vars', '', __LINE__, __FILE__, $sql, $this->db);
+ }
+ return ($this->db->sql_fetchfield(0, 0) > 0);
+ }
+
+ // Adds a bookmark to the database.
+ // Note that date is expected to be a string that's interpretable by strtotime().
+ function addBookmark($address, $title, $description, $status, $categories, $date = NULL, $fromApi = false, $fromImport = false) {
+ $userservice = & ServiceFactory :: getServiceInstance('UserService');
+ $sId = $userservice->getCurrentUserId();
+
+ // If bookmark address doesn't contain ":", add "http://" to the start as a default protocol
+ if (strpos($address, ':') === false) {
+ $address = 'http://'. $address;
+ }
+
+ // Get the client's IP address and the date; note that the date is in GMT.
+ if (getenv('HTTP_CLIENT_IP'))
+ $ip = getenv('HTTP_CLIENT_IP');
+ else
+ if (getenv('REMOTE_ADDR'))
+ $ip = getenv('REMOTE_ADDR');
+ else
+ $ip = getenv('HTTP_X_FORWARDED_FOR');
+
+ // Note that if date is NULL, then it's added with a date and time of now, and if it's present,
+ // it's expected to be a string that's interpretable by strtotime().
+ if (is_null($date))
+ $time = time();
+ else
+ $time = strtotime($date);
+ $datetime = gmdate('Y-m-d H:i:s', $time);
+
+ // Set up the SQL insert statement and execute it.
+ $values = array('uId' => intval($sId), 'bIp' => $ip, 'bDatetime' => $datetime, 'bModified' => $datetime, 'bTitle' => $title, 'bAddress' => $address, 'bDescription' => $description, 'bStatus' => intval($status), 'bHash' => md5($address));
+ $sql = 'INSERT INTO '. $GLOBALS['tableprefix'] .'bookmarks '. $this->db->sql_build_array('INSERT', $values);
+ $this->db->sql_transaction('begin');
+ if (!($dbresult = & $this->db->sql_query($sql))) {
+ $this->db->sql_transaction('rollback');
+ message_die(GENERAL_ERROR, 'Could not insert bookmark', '', __LINE__, __FILE__, $sql, $this->db);
+ return false;
+ }
+ // Get the resultant row ID for the bookmark.
+ $bId = $this->db->sql_nextid($dbresult);
+ if (!isset($bId) || !is_int($bId)) {
+ $this->db->sql_transaction('rollback');
+ message_die(GENERAL_ERROR, 'Could not insert bookmark', '', __LINE__, __FILE__, $sql, $this->db);
+ return false;
+ }
+
+ $uriparts = explode('.', $address);
+ $extension = end($uriparts);
+ unset($uriparts);
+
+ $tagservice = & ServiceFactory :: getServiceInstance('TagService');
+ if (!$tagservice->attachTags($bId, $categories, $fromApi, $extension, false, $fromImport)) {
+ $this->db->sql_transaction('rollback');
+ message_die(GENERAL_ERROR, 'Could not insert bookmark', '', __LINE__, __FILE__, $sql, $this->db);
+ return false;
+ }
+ $this->db->sql_transaction('commit');
+ // Everything worked out, so return the new bookmark's bId.
+ return $bId;
+ }
+
+ function updateBookmark($bId, $address, $title, $description, $status, $categories, $date = NULL, $fromApi = false) {
+ if (!is_numeric($bId))
+ return false;
+
+ // Get the client's IP address and the date; note that the date is in GMT.
+ if (getenv('HTTP_CLIENT_IP'))
+ $ip = getenv('HTTP_CLIENT_IP');
+ else
+ if (getenv('REMOTE_ADDR'))
+ $ip = getenv('REMOTE_ADDR');
+ else
+ $ip = getenv('HTTP_X_FORWARDED_FOR');
+
+ $moddatetime = gmdate('Y-m-d H:i:s', time());
+
+ // Set up the SQL update statement and execute it.
+ $updates = array('bModified' => $moddatetime, 'bTitle' => $title, 'bAddress' => $address, 'bDescription' => $description, 'bStatus' => $status, 'bHash' => md5($address));
+
+ if (!is_null($date)) {
+ $datetime = gmdate('Y-m-d H:i:s', strtotime($date));
+ $updates[] = array('bDateTime' => $datetime);
+ }
+
+ $sql = 'UPDATE '. $GLOBALS['tableprefix'] .'bookmarks SET '. $this->db->sql_build_array('UPDATE', $updates) .' WHERE bId = '. intval($bId);
+ $this->db->sql_transaction('begin');
+
+ if (!($dbresult = & $this->db->sql_query($sql))) {
+ $this->db->sql_transaction('rollback');
+ message_die(GENERAL_ERROR, 'Could not update bookmark', '', __LINE__, __FILE__, $sql, $this->db);
+ return false;
+ }
+
+ $uriparts = explode('.', $address);
+ $extension = end($uriparts);
+ unset($uriparts);
+
+ $tagservice = & ServiceFactory :: getServiceInstance('TagService');
+ if (!$tagservice->attachTags($bId, $categories, $fromApi, $extension)) {
+ $this->db->sql_transaction('rollback');
+ message_die(GENERAL_ERROR, 'Could not update bookmark', '', __LINE__, __FILE__, $sql, $this->db);
+ return false;
+ }
+
+ $this->db->sql_transaction('commit');
+ // Everything worked out, so return true.
+ return true;
+ }
+
+ function & getBookmarks($start = 0, $perpage = NULL, $user = NULL, $tags = NULL, $terms = NULL, $sortOrder = NULL, $watched = NULL, $startdate = NULL, $enddate = NULL, $hash = NULL) {
+ // Only get the bookmarks that are visible to the current user. Our rules:
+ // - if the $user is NULL, that means get bookmarks from ALL users, so we need to make
+ // sure to check the logged-in user's watchlist and get the contacts-only bookmarks from
+ // those users. If the user isn't logged-in, just get the public bookmarks.
+ // - if the $user is set and isn't the logged-in user, then get that user's bookmarks, and
+ // if that user is on the logged-in user's watchlist, get the public AND contacts-only
+ // bookmarks; otherwise, just get the public bookmarks.
+ // - if the $user is set and IS the logged-in user, then get all bookmarks.
+ $userservice =& ServiceFactory::getServiceInstance('UserService');
+ $tagservice =& ServiceFactory::getServiceInstance('TagService');
+ $sId = $userservice->getCurrentUserId();
+
+ if ($userservice->isLoggedOn()) {
+ // All public bookmarks, user's own bookmarks and any shared with user
+ $privacy = ' AND ((B.bStatus = 0) OR (B.uId = '. $sId .')';
+ $watchnames = $userservice->getWatchNames($sId, true);
+ foreach($watchnames as $watchuser) {
+ $privacy .= ' OR (U.username = "'. $watchuser .'" AND B.bStatus = 1)';
+ }
+ $privacy .= ')';
+ } else {
+ // Just public bookmarks
+ $privacy = ' AND B.bStatus = 0';
+ }
+
+ // Set up the tags, if need be.
+ if (!is_array($tags) && !is_null($tags)) {
+ $tags = explode('+', trim($tags));
+ }
+
+ $tagcount = count($tags);
+ for ($i = 0; $i < $tagcount; $i ++) {
+ $tags[$i] = trim($tags[$i]);
+ }
+
+ // Set up the SQL query.
+ $query_1 = 'SELECT DISTINCT ';
+ if (SQL_LAYER == 'mysql4') {
+ $query_1 .= 'SQL_CALC_FOUND_ROWS ';
+ }
+ $query_1 .= 'B.*, U.'. $userservice->getFieldName('username');
+
+ $query_2 = ' FROM '. $userservice->getTableName() .' AS U, '. $GLOBALS['tableprefix'] .'bookmarks AS B';
+
+ $query_3 = ' WHERE B.uId = U.'. $userservice->getFieldName('primary') . $privacy;
+ if (is_null($watched)) {
+ if (!is_null($user)) {
+ $query_3 .= ' AND B.uId = '. $user;
+ }
+ } else {
+ $arrWatch = $userservice->getWatchlist($user);
+ if (count($arrWatch) > 0) {
+ foreach($arrWatch as $row) {
+ $query_3_1 .= 'B.uId = '. intval($row) .' OR ';
+ }
+ $query_3_1 = substr($query_3_1, 0, -3);
+ } else {
+ $query_3_1 = 'B.uId = -1';
+ }
+ $query_3 .= ' AND ('. $query_3_1 .') AND B.bStatus IN (0, 1)';
+ }
+
+ switch($sortOrder) {
+ case 'date_asc':
+ $query_5 = ' ORDER BY B.bDatetime ASC ';
+ break;
+ case 'title_desc':
+ $query_5 = ' ORDER BY B.bTitle DESC ';
+ break;
+ case 'title_asc':
+ $query_5 = ' ORDER BY B.bTitle ASC ';
+ break;
+ case 'url_desc':
+ $query_5 = ' ORDER BY B.bAddress DESC ';
+ break;
+ case 'url_asc':
+ $query_5 = ' ORDER BY B.bAddress ASC ';
+ break;
+ default:
+ $query_5 = ' ORDER BY B.bDatetime DESC ';
+ }
+
+ // Handle the parts of the query that depend on any tags that are present.
+ $query_4 = '';
+ for ($i = 0; $i < $tagcount; $i ++) {
+ $query_2 .= ', '. $GLOBALS['tableprefix'] .'tags AS T'. $i;
+ $query_4 .= ' AND T'. $i .'.tag = "'. $this->db->sql_escape($tags[$i]) .'" AND T'. $i .'.bId = B.bId';
+ }
+
+ // Search terms
+ if ($terms) {
+ // Multiple search terms okay
+ $aTerms = explode(' ', $terms);
+ $aTerms = array_map('trim', $aTerms);
+
+ // Search terms in tags as well when none given
+ if (!count($tags)) {
+ $query_2 .= ' LEFT JOIN '. $GLOBALS['tableprefix'] .'tags AS T ON B.bId = T.bId';
+ $dotags = true;
+ } else {
+ $dotags = false;
+ }
+
+ $query_4 = '';
+ for ($i = 0; $i < count($aTerms); $i++) {
+ $query_4 .= ' AND (B.bTitle LIKE "%'. $this->db->sql_escape($aTerms[$i]) .'%"';
+ $query_4 .= ' OR B.bDescription LIKE "%'. $this->db->sql_escape($aTerms[$i]) .'%"';
+ if ($dotags) {
+ $query_4 .= ' OR T.tag = "'. $this->db->sql_escape($aTerms[$i]) .'"';
+ }
+ $query_4 .= ')';
+ }
+ }
+
+ // Start and end dates
+ if ($startdate) {
+ $query_4 .= ' AND B.bDatetime > "'. $startdate .'"';
+ }
+ if ($enddate) {
+ $query_4 .= ' AND B.bDatetime < "'. $enddate .'"';
+ }
+
+ // Hash
+ if ($hash) {
+ $query_4 .= ' AND B.bHash = "'. $hash .'"';
+ }
+
+ $query = $query_1 . $query_2 . $query_3 . $query_4 . $query_5;
+ if (!($dbresult = & $this->db->sql_query_limit($query, intval($perpage), intval($start)))) {
+ message_die(GENERAL_ERROR, 'Could not get bookmarks', '', __LINE__, __FILE__, $query, $this->db);
+ return false;
+ }
+
+ if (SQL_LAYER == 'mysql4') {
+ $totalquery = 'SELECT FOUND_ROWS() AS total';
+ } else {
+ $totalquery = 'SELECT COUNT(*) AS total'. $query_2 . $query_3 . $query_4;
+ }
+
+ if (!($totalresult = & $this->db->sql_query($totalquery)) || (!($row = & $this->db->sql_fetchrow($totalresult)))) {
+ message_die(GENERAL_ERROR, 'Could not get total bookmarks', '', __LINE__, __FILE__, $totalquery, $this->db);
+ return false;
+ }
+
+ $total = $row['total'];
+
+ $bookmarks = array();
+ while ($row = & $this->db->sql_fetchrow($dbresult)) {
+ $row['tags'] = $tagservice->getTagsForBookmark(intval($row['bId']));
+ $bookmarks[] = $row;
+ }
+ return array ('bookmarks' => $bookmarks, 'total' => $total);
+ }
+
+ function deleteBookmark($bookmarkid) {
+ $query = 'DELETE FROM '. $GLOBALS['tableprefix'] .'bookmarks WHERE bId = '. intval($bookmarkid);
+ $this->db->sql_transaction('begin');
+ if (!($dbresult = & $this->db->sql_query($query))) {
+ $this->db->sql_transaction('rollback');
+ message_die(GENERAL_ERROR, 'Could not delete bookmarks', '', __LINE__, __FILE__, $query, $this->db);
+ return false;
+ }
+
+ $query = 'DELETE FROM '. $GLOBALS['tableprefix'] .'tags WHERE bId = '. intval($bookmarkid);
+ $this->db->sql_transaction('begin');
+ if (!($dbresult = & $this->db->sql_query($query))) {
+ $this->db->sql_transaction('rollback');
+ message_die(GENERAL_ERROR, 'Could not delete bookmarks', '', __LINE__, __FILE__, $query, $this->db);
+ return false;
+ }
+
+ $this->db->sql_transaction('commit');
+ return true;
+ }
+
+ function countOthers($address) {
+ if (!$address) {
+ return false;
+ }
+
+ $userservice = & ServiceFactory :: getServiceInstance('UserService');
+ $sId = $userservice->getCurrentUserId();
+
+ if ($userservice->isLoggedOn()) {
+ // All public bookmarks, user's own bookmarks and any shared with user
+ $privacy = ' AND ((B.bStatus = 0) OR (B.uId = '. $sId .')';
+ $watchnames = $userservice->getWatchNames($sId, true);
+ foreach($watchnames as $watchuser) {
+ $privacy .= ' OR (U.username = "'. $watchuser .'" AND B.bStatus = 1)';
+ }
+ $privacy .= ')';
+ } else {
+ // Just public bookmarks
+ $privacy = ' AND B.bStatus = 0';
+ }
+
+ $sql = 'SELECT COUNT(*) FROM '. $userservice->getTableName() .' AS U, '. $GLOBALS['tableprefix'] .'bookmarks AS B WHERE U.'. $userservice->getFieldName('primary') .' = B.uId AND B.bHash = "'. md5($address) .'"'. $privacy;
+ if (!($dbresult = & $this->db->sql_query($sql))) {
+ message_die(GENERAL_ERROR, 'Could not get vars', '', __LINE__, __FILE__, $sql, $this->db);
+ }
+ return $this->db->sql_fetchfield(0, 0) - 1;
+ }
+}
+?>
diff --git a/services/cacheservice.php b/services/cacheservice.php
new file mode 100644
index 0000000..fe66d38
--- /dev/null
+++ b/services/cacheservice.php
@@ -0,0 +1,38 @@
+<?php
+class CacheService {
+ var $basedir;
+ var $fileextension = '.cache';
+
+ function &getInstance() {
+ static $instance;
+
+ if (!isset($instance))
+ $instance =& new CacheService();
+
+ return $instance;
+ }
+
+ function CacheService() {
+ $this->basedir = $GLOBALS['dir_cache'];
+ }
+
+ function Start($hash, $time = 300) {
+ $cachefile = $this->basedir .'/'. $hash . $this->fileextension;
+ if (file_exists($cachefile) && time() < filemtime($cachefile) + $time) {
+ @readfile($cachefile);
+ echo "\n<!-- Cached: ". date('r', filemtime($cachefile)) ." -->\n";
+ unset($cachefile);
+ exit;
+ }
+ ob_start("ob_gzhandler");
+ }
+
+ function End($hash) {
+ $cachefile = $this->basedir .'/'. $hash . $this->fileextension;
+ $handle = fopen($cachefile, 'w');
+ fwrite($handle, ob_get_contents());
+ fclose($handle);
+ ob_flush();
+ }
+}
+?> \ No newline at end of file
diff --git a/services/servicefactory.php b/services/servicefactory.php
new file mode 100644
index 0000000..ba2d6d7
--- /dev/null
+++ b/services/servicefactory.php
@@ -0,0 +1,33 @@
+<?php
+class ServiceFactory {
+ function ServiceFactory(&$db, $serviceoverrules = array()) {
+ }
+
+ function &getServiceInstance($name, $servicedir = NULL) {
+ global $dbhost, $dbuser, $dbpass, $dbname, $dbport, $dbpersist, $dbtype;
+ static $instances = array();
+ static $db;
+ if (!isset($db)) {
+ require_once(dirname(__FILE__) .'/../includes/db/'. $dbtype .'.php');
+ $db = new sql_db();
+ $db->sql_connect($dbhost, $dbuser, $dbpass, $dbname, $dbport, $dbpersist);
+ if(!$db->db_connect_id) {
+ message_die(CRITICAL_ERROR, "Could not connect to the database", $db);
+ }
+ }
+ if (!isset($instances[$name])) {
+ if (isset($serviceoverrules[$name])) {
+ $name = $serviceoverrules[$name];
+ }
+ if (!class_exists($name)) {
+ if (!isset($servicedir)) {
+ $servicedir = dirname(__FILE__) .'/';
+ }
+ require_once($servicedir . strtolower($name) . '.php');
+ }
+ $instances[$name] = call_user_func(array($name, 'getInstance'), $db);
+ }
+ return $instances[$name];
+ }
+}
+?> \ No newline at end of file
diff --git a/services/tagservice.php b/services/tagservice.php
new file mode 100644
index 0000000..6bfbf15
--- /dev/null
+++ b/services/tagservice.php
@@ -0,0 +1,363 @@
+<?php
+class TagService {
+ var $db;
+ var $tablename;
+
+ function &getInstance(&$db) {
+ static $instance;
+ if (!isset($instance))
+ $instance =& new TagService($db);
+ return $instance;
+ }
+
+ function TagService(&$db) {
+ $this->db =& $db;
+ $this->tablename = $GLOBALS['tableprefix'] .'tags';
+ }
+
+ function isNotSystemTag($var) {
+ if (utf8_substr($var, 0, 7) == 'system:')
+ return false;
+ else
+ return true;
+ }
+
+ function attachTags($bookmarkid, $tags, $fromApi = false, $extension = NULL, $replace = true, $fromImport = false) {
+ // Make sure that categories is an array of trimmed strings, and that if the categories are
+ // coming in from an API call to add a bookmark, that underscores are converted into strings.
+ if (!is_array($tags)) {
+ $tags = trim($tags);
+ if ($tags != '') {
+ if (substr($tags, -1) == ',') {
+ $tags = substr($tags, 0, -1);
+ }
+ if ($fromApi) {
+ $tags = explode(' ', $tags);
+ } else {
+ $tags = explode(',', $tags);
+ }
+ } else {
+ $tags = null;
+ }
+ }
+
+ $tags_count = count($tags);
+ for ($i = 0; $i < $tags_count; $i++) {
+ $tags[$i] = trim(strtolower($tags[$i]));
+ if ($fromApi) {
+ include_once(dirname(__FILE__) .'/../functions.inc.php');
+ $tags[$i] = convertTag($tags[$i], 'in');
+ }
+ }
+
+ if ($tags_count > 0) {
+ // Remove system tags
+ $tags = array_filter($tags, array($this, "isNotSystemTag"));
+
+ // Eliminate any duplicate categories
+ $temp = array_unique($tags);
+ $tags = array_values($temp);
+ } else {
+ // Unfiled
+ $tags[] = 'system:unfiled';
+ }
+
+ // Media and file types
+ if (!is_null($extension)) {
+ include_once(dirname(__FILE__) .'/../functions.inc.php');
+ if ($keys = multi_array_search($extension, $GLOBALS['filetypes'])) {
+ $tags[] = 'system:filetype:'. $extension;
+ $tags[] = 'system:media:'. array_shift($keys);
+ }
+ }
+
+ // Imported
+ if ($fromImport) {
+ $tags[] = 'system:imported';
+ }
+
+ $this->db->sql_transaction('begin');
+
+ if ($replace) {
+ if (!$this->deleteTagsForBookmark($bookmarkid)){
+ $this->db->sql_transaction('rollback');
+ message_die(GENERAL_ERROR, 'Could not attach tags (deleting old ones failed)', '', __LINE__, __FILE__, $sql, $this->db);
+ return false;
+ }
+ }
+
+ // Add the categories to the DB.
+ for ($i = 0; $i < count($tags); $i++) {
+ if ($tags[$i] != '') {
+ $values = array(
+ 'bId' => intval($bookmarkid),
+ 'tag' => $tags[$i]
+ );
+
+ if (!$this->hasTag($bookmarkid, $tags[$i])) {
+ $sql = 'INSERT INTO '. $this->getTableName() .' '. $this->db->sql_build_array('INSERT', $values);
+ if (!($dbresult =& $this->db->sql_query($sql))) {
+ $this->db->sql_transaction('rollback');
+ message_die(GENERAL_ERROR, 'Could not attach tags', '', __LINE__, __FILE__, $sql, $this->db);
+ return false;
+ }
+ }
+ }
+ }
+ $this->db->sql_transaction('commit');
+ return true;
+ }
+
+ function deleteTag($tag) {
+ $userservice =& ServiceFactory::getServiceInstance('UserService');
+ $logged_on_user = $userservice->getCurrentUserId();
+
+ $query = 'DELETE FROM '. $this->getTableName() .' USING '. $GLOBALS['tableprefix'] .'tags, '. $GLOBALS['tableprefix'] .'bookmarks WHERE '. $GLOBALS['tableprefix'] .'tags.bId = '. $GLOBALS['tableprefix'] .'bookmarks.bId AND '. $GLOBALS['tableprefix'] .'bookmarks.uId = '. $logged_on_user .' AND '. $GLOBALS['tableprefix'] .'tags.tag = "'. $this->db->sql_escape($tag) .'"';
+
+ if (!($dbresult =& $this->db->sql_query($query))) {
+ message_die(GENERAL_ERROR, 'Could not delete tags', '', __LINE__, __FILE__, $query, $this->db);
+ return false;
+ }
+
+ return true;
+ }
+
+ function deleteTagsForBookmark($bookmarkid) {
+ if (!is_int($bookmarkid)) {
+ message_die(GENERAL_ERROR, 'Could not delete tags (invalid bookmarkid)', '', __LINE__, __FILE__, $query);
+ return false;
+ }
+
+ $query = 'DELETE FROM '. $this->getTableName() .' WHERE bId = '. intval($bookmarkid);
+
+ if (!($dbresult =& $this->db->sql_query($query))) {
+ message_die(GENERAL_ERROR, 'Could not delete tags', '', __LINE__, __FILE__, $query, $this->db);
+ return false;
+ }
+
+ return true;
+ }
+
+ function &getTagsForBookmark($bookmarkid) {
+ if (!is_int($bookmarkid)) {
+ message_die(GENERAL_ERROR, 'Could not get tags (invalid bookmarkid)', '', __LINE__, __FILE__, $query);
+ return false;
+ }
+
+ $query = 'SELECT tag FROM '. $this->getTableName() .' WHERE bId = '. intval($bookmarkid) .' AND LEFT(tag, 7) <> "system:" ORDER BY tag';
+
+ if (!($dbresult =& $this->db->sql_query($query))) {
+ message_die(GENERAL_ERROR, 'Could not get tags', '', __LINE__, __FILE__, $query, $this->db);
+ return false;
+ }
+
+ $tags = array();
+ while ($row =& $this->db->sql_fetchrow($dbresult)) {
+ $tags[] = $row['tag'];
+ }
+
+ return $tags;
+ }
+
+ function &getTags($userid = NULL) {
+ $userservice =& ServiceFactory::getServiceInstance('UserService');
+ $logged_on_user = $userservice->getCurrentUserId();
+
+ $query = 'SELECT T.tag, COUNT(B.bId) AS bCount FROM '. $GLOBALS['tableprefix'] .'bookmarks AS B INNER JOIN '. $userservice->getTableName() .' AS U ON B.uId = U.'. $userservice->getFieldName('primary') .' INNER JOIN '. $GLOBALS['tableprefix'] .'tags AS T ON B.bId = T.bId';
+
+ $conditions = array();
+ if (!is_null($userid)) {
+ $conditions['U.'. $userservice->getFieldName('primary')] = intval($userid);
+ if ($logged_on_user != $userid)
+ $conditions['B.bStatus'] = 0;
+ } else {
+ $conditions['B.bStatus'] = 0;
+ }
+
+ $query .= ' WHERE '. $this->db->sql_build_array('SELECT', $conditions) .' AND LEFT(T.tag, 7) <> "system:" GROUP BY T.tag ORDER BY bCount DESC, tag';
+
+ if (!($dbresult =& $this->db->sql_query($query))) {
+ message_die(GENERAL_ERROR, 'Could not get tags', '', __LINE__, __FILE__, $query, $this->db);
+ return false;
+ }
+ return $this->db->sql_fetchrowset($dbresult);
+ }
+
+
+ // Returns the tags related to the specified tags; i.e. attached to the same bookmarks
+ function &getRelatedTags($tags, $for_user = NULL, $logged_on_user = NULL, $limit = 10) {
+ $conditions = array();
+ // Only count the tags that are visible to the current user.
+ if ($for_user != $logged_on_user || is_null($for_user))
+ $conditions['B.bStatus'] = 0;
+
+ if (!is_null($for_user))
+ $conditions['B.uId'] = $for_user;
+
+ // Set up the tags, if need be.
+ if (is_numeric($tags))
+ $tags = NULL;
+ if (!is_array($tags) and !is_null($tags))
+ $tags = explode('+', trim($tags));
+
+ $tagcount = count($tags);
+ for ($i = 0; $i < $tagcount; $i++) {
+ $tags[$i] = trim($tags[$i]);
+ }
+
+ // Set up the SQL query.
+ $query_1 = 'SELECT DISTINCTROW T0.tag, COUNT(B.bId) AS bCount FROM '. $GLOBALS['tableprefix'] .'bookmarks AS B, '. $this->getTableName() .' AS T0';
+ $query_2 = '';
+ $query_3 = ' WHERE B.bId = T0.bId ';
+ if (count($conditions) > 0)
+ $query_4 = ' AND '. $this->db->sql_build_array('SELECT', $conditions);
+ else
+ $query_4 = '';
+ // Handle the parts of the query that depend on any tags that are present.
+ for ($i = 1; $i <= $tagcount; $i++) {
+ $query_2 .= ', '. $this->getTableName() .' AS T'. $i;
+ $query_4 .= ' AND T'. $i .'.bId = B.bId AND T'. $i .'.tag = "'. $this->db->sql_escape($tags[$i - 1]) .'" AND T0.tag <> "'. $this->db->sql_escape($tags[$i - 1]) .'"';
+ }
+ $query_5 = ' AND LEFT(T0.tag, 7) <> "system:" GROUP BY T0.tag ORDER BY bCount DESC, T0.tag';
+ $query = $query_1 . $query_2 . $query_3 . $query_4 . $query_5;
+
+ if (! ($dbresult =& $this->db->sql_query_limit($query, $limit)) ){
+ message_die(GENERAL_ERROR, 'Could not get related tags', '', __LINE__, __FILE__, $query, $this->db);
+ return false;
+ }
+ return $this->db->sql_fetchrowset($dbresult);
+ }
+
+ // Returns the most popular tags used for a particular bookmark hash
+ function &getRelatedTagsByHash($hash, $limit = 20) {
+ $userservice = & ServiceFactory :: getServiceInstance('UserService');
+ $sId = $userservice->getCurrentUserId();
+ // Logged in
+ if ($userservice->isLoggedOn()) {
+ $arrWatch = $userservice->getWatchList($sId);
+ // From public bookmarks or user's own
+ $privacy = ' AND ((B.bStatus = 0) OR (B.uId = '. $sId .')';
+ // From shared bookmarks in watchlist
+ foreach ($arrWatch as $w) {
+ $privacy .= ' OR (B.uId = '. $w .' AND B.bStatus = 1)';
+ }
+ $privacy .= ') ';
+ // Not logged in
+ } else {
+ $privacy = ' AND B.bStatus = 0 ';
+ }
+
+ $query = 'SELECT T.tag, COUNT(T.tag) AS bCount FROM sc_bookmarks AS B LEFT JOIN sc_tags AS T ON B.bId = T.bId WHERE B.bHash = "'. $hash .'" '. $privacy .'AND LEFT(T.tag, 7) <> "system:" GROUP BY T.tag ORDER BY bCount DESC';
+
+ if (!($dbresult =& $this->db->sql_query_limit($query, $limit))) {
+ message_die(GENERAL_ERROR, 'Could not get related tags for this hash', '', __LINE__, __FILE__, $query, $this->db);
+ return false;
+ }
+ return $this->db->sql_fetchrowset($dbresult);
+ }
+
+ function &getPopularTags($user = NULL, $limit = 30, $logged_on_user = NULL, $days = NULL) {
+ // Only count the tags that are visible to the current user.
+ if (($user != $logged_on_user) || is_null($user) || ($user === false))
+ $privacy = ' AND B.bStatus = 0';
+ else
+ $privacy = '';
+
+ if (is_null($days) || !is_int($days))
+ $span = '';
+ else
+ $span = ' AND B.bDatetime > "'. date('Y-m-d H:i:s', time() - (86400 * $days)) .'"';
+
+ $query = 'SELECT T.tag, COUNT(T.bId) AS bCount FROM '. $this->getTableName() .' AS T, '. $GLOBALS['tableprefix'] .'bookmarks AS B WHERE ';
+ if (is_null($user) || ($user === false)) {
+ $query .= 'B.bId = T.bId AND B.bStatus = 0';
+ } else {
+ $query .= 'B.uId = '. $this->db->sql_escape($user) .' AND B.bId = T.bId'. $privacy;
+ }
+ $query .= $span .' AND LEFT(T.tag, 7) <> "system:" GROUP BY T.tag ORDER BY bCount DESC, tag';
+
+ if (!($dbresult =& $this->db->sql_query_limit($query, $limit))) {
+ message_die(GENERAL_ERROR, 'Could not get popular tags', '', __LINE__, __FILE__, $query, $this->db);
+ return false;
+ }
+
+ return $this->db->sql_fetchrowset($dbresult);
+ }
+
+ function hasTag($bookmarkid, $tag) {
+ $query = 'SELECT COUNT(*) AS tCount FROM '. $this->getTableName() .' WHERE bId = '. intval($bookmarkid) .' AND tag ="'. $this->db->sql_escape($tag) .'"';
+
+ if (! ($dbresult =& $this->db->sql_query($query)) ) {
+ message_die(GENERAL_ERROR, 'Could not find tag', '', __LINE__, __FILE__, $query, $this->db);
+ return false;
+ }
+
+ if ($row =& $this->db->sql_fetchrow($dbresult)) {
+ if ($row['tCount'] > 0) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ function renameTag($userid, $old, $new, $fromApi = false) {
+ $bookmarkservice =& ServiceFactory::getServiceInstance('BookmarkService');
+
+ if (is_null($userid) || is_null($old) || is_null($new))
+ return false;
+
+ // Find bookmarks with old tag
+ $bookmarksInfo =& $bookmarkservice->getBookmarks(0, NULL, $userid, $old);
+ $bookmarks =& $bookmarksInfo['bookmarks'];
+
+ // Delete old tag
+ $this->deleteTag($old);
+
+ // Attach new tags
+ foreach(array_keys($bookmarks) as $key) {
+ $row =& $bookmarks[$key];
+ $this->attachTags($row['bId'], $new, $fromApi, NULL, false);
+ }
+
+ return true;
+ }
+
+ function &tagCloud($tags = NULL, $steps = 5, $sizemin = 90, $sizemax = 225, $sortOrder = NULL) {
+
+ if (is_null($tags) || count($tags) < 1) {
+ return false;
+ }
+
+ $min = $tags[count($tags) - 1]['bCount'];
+ $max = $tags[0]['bCount'];
+
+ for ($i = 1; $i <= $steps; $i++) {
+ $delta = ($max - $min) / (2 * $steps - $i);
+ $limit[$i] = $i * $delta + $min;
+ }
+ $sizestep = ($sizemax - $sizemin) / $steps;
+ foreach ($tags as $row) {
+ $next = false;
+ for ($i = 1; $i <= $steps; $i++) {
+ if (!$next && $row['bCount'] <= $limit[$i]) {
+ $size = $sizestep * ($i - 1) + $sizemin;
+ $next = true;
+ }
+ }
+ $tempArray = array('size' => $size .'%');
+ $row = array_merge($row, $tempArray);
+ $output[] = $row;
+ }
+
+ if ($sortOrder == 'alphabet_asc') {
+ usort($output, create_function('$a,$b','return strcasecmp(utf8_deaccent($a["tag"]), utf8_deaccent($b["tag"]));'));
+ }
+
+ return $output;
+ }
+
+ // Properties
+ function getTableName() { return $this->tablename; }
+ function setTableName($value) { $this->tablename = $value; }
+}
+?> \ No newline at end of file
diff --git a/services/templateservice.php b/services/templateservice.php
new file mode 100644
index 0000000..191ab8d
--- /dev/null
+++ b/services/templateservice.php
@@ -0,0 +1,46 @@
+<?php
+class TemplateService {
+ var $basedir;
+
+ function &getInstance() {
+ static $instance;
+ if (!isset($instance))
+ $instance =& new TemplateService();
+ return $instance;
+ }
+
+ function TemplateService() {
+ $this->basedir = $GLOBALS['TEMPLATES_DIR'];
+ }
+
+ function loadTemplate($template, $vars = NULL) {
+ if (substr($template, -4) != '.php')
+ $template .= '.php';
+ $tpl =& new Template($this->basedir .'/'. $template, $vars, $this);
+ $tpl->parse();
+ return $tpl;
+ }
+}
+
+class Template {
+ var $vars = array();
+ var $file = '';
+ var $templateservice;
+
+ function Template($file, $vars = NULL, &$templateservice) {
+ $this->vars = $vars;
+ $this->file = $file;
+ $this->templateservice = $templateservice;
+ }
+
+ function parse() {
+ if (isset($this->vars))
+ extract($this->vars);
+ include($this->file);
+ }
+
+ function includeTemplate($name) {
+ return $this->templateservice->loadTemplate($name, $this->vars);
+ }
+}
+?> \ No newline at end of file
diff --git a/services/userservice.php b/services/userservice.php
new file mode 100644
index 0000000..1e7ed46
--- /dev/null
+++ b/services/userservice.php
@@ -0,0 +1,362 @@
+<?php
+class UserService {
+ var $db;
+
+ function &getInstance(&$db) {
+ static $instance;
+ if (!isset($instance))
+ $instance =& new UserService($db);
+ return $instance;
+ }
+
+ var $fields = array(
+ 'primary' => 'uId',
+ 'username' => 'username',
+ 'password' => 'password'
+ );
+ var $profileurl;
+ var $tablename;
+ var $sessionkey;
+ var $cookiekey;
+ var $cookietime = 1209600; // 2 weeks
+
+ function UserService(&$db) {
+ $this->db =& $db;
+ $this->tablename = $GLOBALS['tableprefix'] .'users';
+ $this->sessionkey = $GLOBALS['cookieprefix'] .'-currentuserid';
+ $this->cookiekey = $GLOBALS['cookieprefix'] .'-login';
+ $this->profileurl = createURL('profile', '%2$s');
+ }
+
+ function _checkdns($host) {
+ if (function_exists('checkdnsrr')) {
+ return checkdnsrr($host);
+ } else {
+ return $this->_checkdnsrr($host);
+ }
+ }
+
+ function _checkdnsrr($host, $type = "MX") {
+ if(!empty($host)) {
+ @exec("nslookup -type=$type $host", $output);
+ while(list($k, $line) = each($output)) {
+ if(eregi("^$host", $line)) {
+ return true;
+ }
+ }
+ return false;
+ }
+ }
+
+ function _getuser($fieldname, $value) {
+ $query = 'SELECT * FROM '. $this->getTableName() .' WHERE '. $fieldname .' = "'. $this->db->sql_escape($value) .'"';
+
+ if (! ($dbresult =& $this->db->sql_query($query)) ) {
+ message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db);
+ return false;
+ }
+
+ if ($row =& $this->db->sql_fetchrow($dbresult))
+ return $row;
+ else
+ return false;
+ }
+
+ function _randompassword() {
+ $seed = (integer) md5(microtime());
+ mt_srand($seed);
+ $password = mt_rand(1, 99999999);
+ $password = substr(md5($password), mt_rand(0, 19), mt_rand(6, 12));
+ return $password;
+ }
+
+ function _updateuser($uId, $fieldname, $value) {
+ $updates = array ($fieldname => $value);
+ $sql = 'UPDATE '. $this->getTableName() .' SET '. $this->db->sql_build_array('UPDATE', $updates) .' WHERE '. $this->getFieldName('primary') .'='. intval($uId);
+
+ // Execute the statement.
+ $this->db->sql_transaction('begin');
+ if (!($dbresult = & $this->db->sql_query($sql))) {
+ $this->db->sql_transaction('rollback');
+ message_die(GENERAL_ERROR, 'Could not update user', '', __LINE__, __FILE__, $sql, $this->db);
+ return false;
+ }
+ $this->db->sql_transaction('commit');
+
+ // Everything worked out, so return true.
+ return true;
+ }
+
+ function getProfileUrl($id, $username) {
+ return sprintf($this->profileurl, urlencode($id), urlencode($username));
+ }
+
+ function getUserByUsername($username) {
+ return $this->_getuser($this->getFieldName('username'), $username);
+ }
+
+ function getUser($id) {
+ return $this->_getuser($this->getFieldName('primary'), $id);
+ }
+
+ function isLoggedOn() {
+ return ($this->getCurrentUserId() !== false);
+ }
+
+ function &getCurrentUser($refresh = FALSE, $newval = NULL) {
+ static $currentuser;
+ if (!is_null($newval)) //internal use only: reset currentuser
+ $currentuser = $newval;
+ else if ($refresh || !isset($currentuser)) {
+ if ($id = $this->getCurrentUserId())
+ $currentuser = $this->getUser($id);
+ else
+ return;
+ }
+ return $currentuser;
+ }
+
+ function isAdmin($userid) {
+ return false; //not implemented yet
+ }
+
+ function getCurrentUserId() {
+ if (isset($_SESSION[$this->getSessionKey()])) {
+ return $_SESSION[$this->getSessionKey()];
+ } else if (isset($_COOKIE[$this->getCookieKey()])) {
+ $cook = split(':', $_COOKIE[$this->getCookieKey()]);
+ //cookie looks like this: 'id:md5(username+password)'
+ $query = 'SELECT * FROM '. $this->getTableName() .
+ ' WHERE MD5(CONCAT('.$this->getFieldName('username') .
+ ', '.$this->getFieldName('password') .
+ ')) = \''.$this->db->sql_escape($cook[1]).'\' AND '.
+ $this->getFieldName('primary'). ' = '. $this->db->sql_escape($cook[0]);
+
+ if (! ($dbresult =& $this->db->sql_query($query)) ) {
+ message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db);
+ return false;
+ }
+
+ if ($row = $this->db->sql_fetchrow($dbresult)) {
+ $_SESSION[$this->getSessionKey()] = $row[$this->getFieldName('primary')];
+ return $_SESSION[$this->getSessionKey()];
+ }
+ }
+ return false;
+ }
+
+ function login($username, $password, $remember = FALSE) {
+ $password = $this->sanitisePassword($password);
+ $query = 'SELECT '. $this->getFieldName('primary') .' FROM '. $this->getTableName() .' WHERE '. $this->getFieldName('username') .' = "'. $this->db->sql_escape($username) .'" AND '. $this->getFieldName('password') .' = "'. $this->db->sql_escape($password) .'"';
+
+ if (! ($dbresult =& $this->db->sql_query($query)) ) {
+ message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db);
+ return false;
+ }
+
+ if ($row =& $this->db->sql_fetchrow($dbresult)) {
+ $id = $_SESSION[$this->getSessionKey()] = $row[$this->getFieldName('primary')];
+ if ($remember) {
+ $cookie = $id .':'. md5($username.$password);
+ setcookie($this->cookiekey, $cookie, time() + $this->cookietime);
+ }
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ function logout() {
+ @setcookie($this->cookiekey, NULL, time() - 1);
+ unset($_COOKIE[$this->cookiekey]);
+ session_unset();
+ $this->getCurrentUser(TRUE, false);
+ }
+
+ function getWatchlist($uId) {
+ // Gets the list of user IDs being watched by the given user.
+ $query = 'SELECT watched FROM '. $GLOBALS['tableprefix'] .'watched WHERE uId = '. intval($uId);
+
+ if (! ($dbresult =& $this->db->sql_query($query)) ) {
+ message_die(GENERAL_ERROR, 'Could not get watchlist', '', __LINE__, __FILE__, $query, $this->db);
+ return false;
+ }
+
+ $arrWatch = array();
+ if ($this->db->sql_numrows($dbresult) == 0)
+ return $arrWatch;
+ while ($row =& $this->db->sql_fetchrow($dbresult))
+ $arrWatch[] = $row['watched'];
+ return $arrWatch;
+ }
+
+ function getWatchNames($uId, $watchedby = false) {
+ // Gets the list of user names being watched by the given user.
+ // - If $watchedby is false get the list of users that $uId watches
+ // - If $watchedby is true get the list of users that watch $uId
+ if ($watchedby) {
+ $table1 = 'b';
+ $table2 = 'a';
+ } else {
+ $table1 = 'a';
+ $table2 = 'b';
+ }
+ $query = 'SELECT '. $table1 .'.'. $this->getFieldName('username') .' FROM '. $GLOBALS['tableprefix'] .'watched AS W, '. $this->getTableName() .' AS a, '. $this->getTableName() .' AS b WHERE W.watched = a.'. $this->getFieldName('primary') .' AND W.uId = b.'. $this->getFieldName('primary') .' AND '. $table2 .'.'. $this->getFieldName('primary') .' = '. intval($uId) .' ORDER BY '. $table1 .'.'. $this->getFieldName('username');
+
+ if (!($dbresult =& $this->db->sql_query($query))) {
+ message_die(GENERAL_ERROR, 'Could not get watchlist', '', __LINE__, __FILE__, $query, $this->db);
+ return false;
+ }
+
+ $arrWatch = array();
+ if ($this->db->sql_numrows($dbresult) == 0) {
+ return $arrWatch;
+ }
+ while ($row =& $this->db->sql_fetchrow($dbresult)) {
+ $arrWatch[] = $row[$this->getFieldName('username')];
+ }
+ return $arrWatch;
+ }
+
+ function getWatchStatus($watcheduser, $currentuser) {
+ // Returns true if the current user is watching the given user, and false otherwise.
+ $query = 'SELECT watched FROM '. $GLOBALS['tableprefix'] .'watched AS W INNER JOIN '. $this->getTableName() .' AS U ON U.'. $this->getFieldName('primary') .' = W.watched WHERE U.'. $this->getFieldName('primary') .' = '. intval($watcheduser) .' AND W.uId = '. intval($currentuser);
+
+ if (! ($dbresult =& $this->db->sql_query($query)) ) {
+ message_die(GENERAL_ERROR, 'Could not get watchstatus', '', __LINE__, __FILE__, $query, $this->db);
+ return false;
+ }
+
+ $arrWatch = array();
+ if ($this->db->sql_numrows($dbresult) == 0)
+ return false;
+ else
+ return true;
+ }
+
+ function setWatchStatus($subjectUserID) {
+ if (!is_numeric($subjectUserID))
+ return false;
+
+ $currentUserID = $this->getCurrentUserId();
+ $watched = $this->getWatchStatus($subjectUserID, $currentUserID);
+
+ if ($watched) {
+ $sql = 'DELETE FROM '. $GLOBALS['tableprefix'] .'watched WHERE uId = '. intval($currentUserID) .' AND watched = '. intval($subjectUserID);
+ if (!($dbresult =& $this->db->sql_query($sql))) {
+ $this->db->sql_transaction('rollback');
+ message_die(GENERAL_ERROR, 'Could not add user to watch list', '', __LINE__, __FILE__, $sql, $this->db);
+ return false;
+ }
+ } else {
+ $values = array(
+ 'uId' => intval($currentUserID),
+ 'watched' => intval($subjectUserID)
+ );
+ $sql = 'INSERT INTO '. $GLOBALS['tableprefix'] .'watched '. $this->db->sql_build_array('INSERT', $values);
+ if (!($dbresult =& $this->db->sql_query($sql))) {
+ $this->db->sql_transaction('rollback');
+ message_die(GENERAL_ERROR, 'Could not add user to watch list', '', __LINE__, __FILE__, $sql, $this->db);
+ return false;
+ }
+ }
+
+ $this->db->sql_transaction('commit');
+ return true;
+ }
+
+ function addUser($username, $password, $email) {
+ // Set up the SQL UPDATE statement.
+ $datetime = gmdate('Y-m-d H:i:s', time());
+ $password = $this->sanitisePassword($password);
+ $values = array('username' => $username, 'password' => $password, 'email' => $email, 'uDatetime' => $datetime, 'uModified' => $datetime);
+ $sql = 'INSERT INTO '. $this->getTableName() .' '. $this->db->sql_build_array('INSERT', $values);
+
+ // Execute the statement.
+ $this->db->sql_transaction('begin');
+ if (!($dbresult = & $this->db->sql_query($sql))) {
+ $this->db->sql_transaction('rollback');
+ message_die(GENERAL_ERROR, 'Could not insert user', '', __LINE__, __FILE__, $sql, $this->db);
+ return false;
+ }
+ $this->db->sql_transaction('commit');
+
+ // Everything worked out, so return true.
+ return true;
+ }
+
+ function updateUser($uId, $password, $name, $email, $homepage, $uContent) {
+ if (!is_numeric($uId))
+ return false;
+
+ // Set up the SQL UPDATE statement.
+ $moddatetime = gmdate('Y-m-d H:i:s', time());
+ if ($password == '')
+ $updates = array ('uModified' => $moddatetime, 'name' => $name, 'email' => $email, 'homepage' => $homepage, 'uContent' => $uContent);
+ else
+ $updates = array ('uModified' => $moddatetime, 'password' => $this->sanitisePassword($password), 'name' => $name, 'email' => $email, 'homepage' => $homepage, 'uContent' => $uContent);
+ $sql = 'UPDATE '. $this->getTableName() .' SET '. $this->db->sql_build_array('UPDATE', $updates) .' WHERE '. $this->getFieldName('primary') .'='. intval($uId);
+
+ // Execute the statement.
+ $this->db->sql_transaction('begin');
+ if (!($dbresult = & $this->db->sql_query($sql))) {
+ $this->db->sql_transaction('rollback');
+ message_die(GENERAL_ERROR, 'Could not update user', '', __LINE__, __FILE__, $sql, $this->db);
+ return false;
+ }
+ $this->db->sql_transaction('commit');
+
+ // Everything worked out, so return true.
+ return true;
+ }
+
+ function sanitisePassword($password) {
+ return sha1(trim($password));
+ }
+
+ function generatePassword($uId) {
+ if (!is_numeric($uId))
+ return false;
+
+ $password = $this->_randompassword();
+
+ if ($this->_updateuser($uId, $this->getFieldName('password'), $this->sanitisePassword($password)))
+ return $password;
+ else
+ return false;
+ }
+
+ function isReserved($username) {
+ if (in_array($username, $GLOBALS['reservedusers'])) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ function isValidEmail($email) {
+ if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$", $email)) {
+ list($emailUser, $emailDomain) = split("@", $email);
+
+ // Check if the email domain has a DNS record
+ if ($this->_checkdns($emailDomain)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ // Properties
+ function getTableName() { return $this->tablename; }
+ function setTableName($value) { $this->tablename = $value; }
+
+ function getFieldName($field) { return $this->fields[$field]; }
+ function setFieldName($field, $value) { $this->fields[$field] = $value; }
+
+ function getSessionKey() { return $this->sessionkey; }
+ function setSessionKey($value) { $this->sessionkey = $value; }
+
+ function getCookieKey() { return $this->cookiekey; }
+ function setCookieKey($value) { $this->cookiekey = $value; }
+}
+?>