aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--engine/lib/friends.php197
-rw-r--r--engine/lib/users.php397
-rw-r--r--engine/schema/mysql.sql28
-rw-r--r--index.php4
4 files changed, 618 insertions, 8 deletions
diff --git a/engine/lib/friends.php b/engine/lib/friends.php
new file mode 100644
index 000000000..618f99d05
--- /dev/null
+++ b/engine/lib/friends.php
@@ -0,0 +1,197 @@
+<?php
+
+ /**
+ * Elgg friends
+ * Functions for friendship management
+ *
+ * @package Elgg
+ * @subpackage Core
+ * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
+ * @author Curverider Ltd
+ * @copyright Curverider Ltd 2008
+ * @link http://elgg.org/
+ */
+
+
+ /**
+ * Make a friend
+ *
+ * @param $user_id int User ID of the user being befriended.
+ * @param $friend_id int User ID of the new friend.
+ *
+ * @return mixed record id if success, False if attempt failed
+ */
+
+ function make_friend($user_id,$friend_id, $site_id)
+ {
+
+ global $CONFIG;
+
+ $user_id = (int) $user_id;
+ $friend_id = (int) $friend_id;
+ $site_id = (int) $site_id;
+ if (!get_data_row("SELECT id FROM {$CONFIG->dbprefix}friends WHERE user_id = $user_id AND friend_id = $friend_id AND site_id = {$site_id}")) {
+ return insert_data("INSERT INTO {$CONFIG->dbprefix}friends (user_id,friend_id,site_id) VALUES ($user_id,$friend_id,$site_id)");
+ } else {
+ return false;
+ }
+
+ }
+
+ /**
+ * Remove friend
+ *
+ * @param $user_id int User ID of the user having the friend removed.
+ * @param $friend_id int User ID of the friend being removed.
+ */
+
+ function remove_friend($user_id,$friend_id,$site)
+ {
+ global $CONFIG;
+ $user_id = (int) $user_id;
+ $friend_id = (int) $friend_id;
+ $site = (int) $site;
+ delete_record("DELETE FROM {$CONFIG->dbprefix}friends WHERE user_id = $user_id AND friend_id = $friend_id AND site_id = {$site}");
+ return true;
+ }
+
+ /**
+ * Determines if the user $user_id is friends with the currently logged in user, or a specified user
+ * @param int $user_id The user we're testing to see if they're a friend
+ * @param int $friend_of Optionally, the user we're testing to see if they're a friend of (the logged in user if unspecified)
+ * @return true|false Wehther $user_id is a friend of $friend_of
+ */
+ function is_friend($user_id, $friend_of, $site) {
+ global $CONFIG;
+ $user_id = (int) $user_id;
+ $friend_of = (int) $friend_of;
+ $site = (int) $site;
+ if ($friend_of < 1) {
+ return false;
+ }
+ if (get_data_row("select id FROM {$CONFIG->dbprefix}friends where user_id = {$friend_of} and friend_id = {$user_id} and site_id = {$site}")) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Get a user's friends
+ *
+ * @param int $user_id The user ID
+ * @param int $site The site ID
+ * @return unknown
+ */
+
+ function get_friends($user_id, $site) {
+
+ global $CONFIG;
+ $user_id = (int) $user_id;
+ $site = (int) $site;
+ return get_data("SELECT u.* FROM {$CONFIG->dbprefix}friends f LEFT JOIN {$CONFIG->dbprefix}users u on u.id = f.friend_id WHERE user_id = {$user_id} and site_id = {$site}","row_to_elgguser");
+
+ }
+
+ /**
+ * Return friend IDs in a way suitable for including in a 'where' statement.
+ *
+ * @param unknown_type $user_id
+ * @return unknown
+ */
+ function get_friends_where($user_id, $site) {
+ global $CONFIG;
+ $friendswhere = "";
+ if ($friends = get_friends($user_id,1,999,$site)) {
+ $friendswhere .= $user_id;
+ foreach($friends as $friend) {
+ if (!empty($friendswhere)) $friendswhere .= ",";
+ $friendswhere .= $friend;
+ }
+ }
+ if (empty($friendswhere)) {
+ $friendswhere = "-1";
+ }
+ return "({$friendswhere})";
+ }
+
+ /**
+ * Get all the people who have befriended friend_id
+ *
+ * @param $friend_id int User ID of the user in question
+ * @param $degree int
+ *
+ * @return array of ElggUsers
+ */
+
+ function get_friends_reverse($friend_id, $site)
+ {
+ global $CONFIG;
+ $friend_id = (int) $friend_id;
+ $site = (int) $site;
+ return get_data("SELECT u.* FROM {$CONFIG->dbprefix}friends f LEFT JOIN {$CONFIG->dbprefix}users u on u.id = f.user_id WHERE friend_id = {$friend_id} and site_id = {$site}","row_to_elgguser");
+ }
+
+ /**
+ * Returns a stirng of reverse friend IDs suitable for dropping into an SQL statement
+ *
+ * @param unknown_type $user_id
+ * @return unknown
+ */
+ function get_friends_reverse_where($user_id, $site) {
+
+ $friendswhere = "";
+ if ($friends = get_friends_reverse($user_id,$site)) {
+ $friendswhere .= $user_id;
+ foreach($friends as $friend) {
+ if (!empty($friendswhere)) $friendswhere .= ",";
+ $friendswhere .= $friend;
+ }
+ }
+ if (empty($friendswhere)) {
+ $friendswhere = "-1";
+ }
+ return "({$friendswhere})";
+ }
+
+ /**
+ * Get all the mutual friends of $user_id
+ *
+ * @param $user_id int User ID of the user in question
+ * @param $site int Optionally, the ID of the current site
+ *
+ * @return array of ElggUsers
+ */
+
+ function get_mutual_friends($user_id, $site) {
+
+ global $CONFIG;
+
+ $friend_id = (int) $user_id;
+ $friends = get_friends_where($user_id, $site);
+
+ return get_data("SELECT u.* FROM {$CONFIG->dbprefix}friends f LEFT JOIN {$CONFIG->dbprefix}users u on u.id = f.user_id WHERE friend_id = {$friend_id} and site_id = {$site} and user_id in {$friends}","row_to_elgguser");
+
+ }
+
+ /**
+ * Returns a stirng of mutual friend IDs suitable for dropping into an SQL statement
+ *
+ * @param unknown_type $user_id
+ * @return unknown
+ */
+ function get_mutual_friends_where($user_id, $site) {
+
+ $friendswhere = "";
+ if ($friends = get_mutual_friends($user_id, $site)) {
+ $friendswhere .= $user_id;
+ foreach($friends as $friend) {
+ if (!empty($friendswhere)) $friendswhere .= ",";
+ $friendswhere .= $friend;
+ }
+ }
+ return "({$friendswhere})";
+
+ }
+
+
+?> \ No newline at end of file
diff --git a/engine/lib/users.php b/engine/lib/users.php
index 9d5e40288..c88c50535 100644
--- a/engine/lib/users.php
+++ b/engine/lib/users.php
@@ -12,12 +12,403 @@
* @link http://elgg.org/
*/
- // This is here as a dummy for now
- function get_users($param, $param2) {
- return false;
+ /**
+ * @class ElggUser
+ * This class represents an Elgg user.
+ */
+ class ElggUser {
+
+ /**
+ * This contains the user's main properties (id, etc)
+ * @var array
+ */
+ private $attributes;
+
+ /**
+ * Construct a new user object, optionally from a given id value.
+ *
+ * @param mixed $id
+ */
+ function __construct($id = null)
+ {
+ $this->attributes = array();
+
+ if (!empty($id)) {
+
+ $user = null;
+
+ if (is_int($id))
+ $user = get_user($id); // This is an integer ID
+ else if ($id instanceof stdClass)
+ $user = $id; // This is a db row, so serialise directly
+ else
+ throw new InvalidParameterException("Unrecognised or unsupported type passed to ElggUser constructor.");
+
+ if ($user) {
+ $objarray = (array) $user;
+ foreach($objarray as $key => $value) {
+ $this->attributes[$key] = $value;
+ error_log("$key => $value");
+ }
+ }
+ else
+ throw new IOException("Could not create ElggUser object");
+ }
+ }
+
+ function __get($name) {
+ if (isset($this->attributes[$name])) {
+ return $this->attributes[$name];
+ }
+ return null;
+ }
+
+ function __set($name, $value) {
+ $this->attributes[$name] = $value;
+ return true;
+ }
+
+ /**
+ * Saves or updates this user
+ *
+ * @return true|false
+ */
+ function save() {
+ if (!empty($this->id)) {
+ update_user($this->id, $this->name, $this->username, $this->password, $this->email, $this->language);
+ } else if ($id = create_user($this->name, $this->username, $this->password, $this->email, $this->language)) {
+ $this->id = $id;
+ return true;
+ }
+ }
+
+ /**
+ * Deletes this user
+ *
+ * @return true|false
+ */
+ function delete() {
+ if (!empty($this->id)) {
+ return delete_user($this->id);
+ }
+ return false;
+ }
+
+ /**
+ * Get this user's owned objects
+ *
+ * @param string $type The type of object
+ * @param int $limit The number of objects
+ * @param int $offset Indexing offset
+ * @param int $site_id The site ID
+ * @return array List of objects
+ */
+ function getObjects($type = "", $limit = 20, $offset = 0, $site_id = 0) {
+ return get_objects($this->id, $type, "", "", $limit, $offset, $site_id);
+ }
+
+ /**
+ * Get this user's owned objects by metadata value
+ *
+ * @param string $type The type of object
+ * @param string $metadata_type The name of the metadata type
+ * @param string $metadata_value The value of the metadata
+ * @param int $limit The number of objects to return
+ * @param int $offset Indexing offset
+ * @param int $site_id The site
+ * @return array List of objects
+ */
+ function getObjectsByMetadata($type = "", $metadata_type, $metadata_value, $limit = 20, $offset = 0, $site_id = 0) {
+ return get_objects($this->id, $type, $metadata_type, $metadata_value, $limit, $offset, $site_id);
+ }
+
+ /**
+ * Gets this user's sites
+ *
+ * @return array List of ElggSites
+ */
+ function getSites() { return get_user_sites($this->id); }
+
+ /**
+ * Adds this user to the currently logged in user's friends list
+ *
+ * @return true|false
+ */
+ function addAsFriend() {
+ global $CONFIG;
+ if (isloggedin()) {
+ return make_friend($_SESSION['id'],$this-id,$CONFIG->site_id);
+ }
+ return false;
+ }
+
+ /**
+ * Add a user to this user's friends list
+ *
+ * @param int $friend_id The user ID to add
+ * @param int $site_id The site ID to add
+ * @return true|false On success
+ */
+ function addFriend($friend_id, $site_id = 0) {
+ global $CONFIG;
+ if ($site_id == 0) $site_id = $CONFIG->site_id;
+ return make_friend($this->id, $friend_id, $site_id);
+ }
+
+ /**
+ * Removes this user from the currently logged in user's friends list
+ *
+ * @return true|false
+ */
+ function removeAsFriend() {
+ global $CONFIG;
+ return remove_friend($_SESSION['id'], $this->id, $CONFIG->site_id);
+ }
+
+ /**
+ * Remove a user from this user's friends list
+ *
+ * @param int $friend_id The user ID to remove
+ * @param int $site_id The site ID to remove
+ * @return true|false On success
+ */
+ function removeFriend($friend_id, $site_id = 0) {
+ global $CONFIG;
+ if ($site_id == 0) $site_id = $CONFIG->site_id;
+ return remove_friend($this->id, $friend_id, $site_id);
+ }
+
+ /**
+ * Get a list of this user's friends
+ *
+ * @param int $site_id The site ID
+ * @return array List of ElggUsers
+ */
+ function getFriends($site_id = 0) {
+ global $CONFIG;
+ if ($site_id == 0) $site_id = $CONFIG->site_id;
+ return get_friends($this->id, $site_id);
+ }
+
+ /**
+ * Get a list of users who have marked this user as a friend
+ *
+ * @param int $site_id The site ID
+ * @return array List of ElggUsers
+ */
+ function getFriendsOf($site_id = 0) {
+ global $CONFIG;
+ if ($site_id == 0) $site_id = $CONFIG->site_id;
+ return get_friends_reverse($this->id, $site_id);
+ }
+
+ /**
+ * Determines whether or not this user is a friend of the currently logged in user
+ *
+ * @return true|false
+ */
+ function isFriend() {
+ global $CONFIG;
+ return is_friend($_SESSION['id'],$this->id, $CONFIG->site_id);
+ }
+
+ /**
+ * Determines whether this user is friends with a particular user
+ *
+ * @param int $user_id The ID of the user to check friendship with
+ * @param int $site_id The site ID
+ * @return true|false
+ */
+ function isFriendsWith($user_id, $site_id = 0) {
+ global $CONFIG;
+ if ($site_id == 0) $site_id = $CONFIG->site_id;
+ return is_friend($this->id, $user_id, $site_id);
+ }
+
+ /**
+ * Determines whether this user has been made a friend of a particular user
+ *
+ * @param int $user_id The ID of the user to check friendship of
+ * @param int $site_id The site ID
+ * @return true|false
+ */
+ function isFriendOf($user_id, $site_id = 0) {
+ global $CONFIG;
+ if ($site_id == 0) $site_id = $CONFIG->site_id;
+ return is_friend($user_id, $this->id, $site_id);
+ }
+
+
+
}
/**
+ * Gets a user with a particular ID, if they exist
+ *
+ * @param int $id The user ID
+ * @return ElggUser or false
+ */
+ function get_user($id) {
+
+ global $CONFIG;
+ $id = (int) $id;
+ return row_to_elgguser(get_data_row("select * from {$CONFIG->dbprefix}users where id = {$id}"));
+
+ }
+
+ /**
+ * Enter description here...
+ *
+ * @param unknown_type $criteria
+ * @param unknown_type $fields
+ * @return unknown
+ */
+ function get_users($criteria, $fields = "*") {
+
+ global $CONFIG;
+ $where = "";
+
+ if (!empty($criteria) && is_array($criteria)) {
+
+ foreach($criteria as $name => $value) {
+ if (!empty($where)) {
+ $where .= " and ";
+ }
+ $where .= " {$name} = '" . sanitise_string($value) . "'";
+ }
+
+ } else if (!empty($criteria) && !is_array($criteria)) {
+
+ $where = $criteria;
+
+ }
+
+ if (!empty($where)) {
+ $where = "where (" . $where . ")";
+ }
+
+ return get_data("select {$fields}, 'users' as type from {$CONFIG->dbprefix}users {$where}");
+
+ }
+
+ /**
+ * Update a user row
+ *
+ * @param int $id The user ID
+ * @param string $name The user's name
+ * @param string $username The user's username
+ * @param string $password The user's password
+ * @param string $email The user's email address
+ * @param string $language The user's language
+ * @return true|false
+ */
+ function update_user($id, $name, $username, $password, $email, $language) {
+
+ global $CONFIG;
+ $id = (int) $id;
+ $name = sanitise_string($name);
+ $username = sanitise_string($username);
+ $password = sanitise_string($password);
+ $email = sanitise_string($email);
+ $language = sanitise_string($language);
+
+ foreach(array('name','username','password','email','language') as $param) {
+ if ($$param != null) {
+ $params[] = "{$param} = '{$$param}'";
+ }
+ }
+
+ return update_data("update {$CONFIG->dbprefix}users set " . implode(",",$params) . " where id = {$id}");
+
+ }
+
+ /**
+ * Create a new user
+ *
+ * @param string $name The user's name
+ * @param string $username The user's username
+ * @param string $password The user's password
+ * @param string $email The user's email address
+ * @param string $language The user's language
+ * @return int The user's ID
+ */
+ function create_user($name, $username, $password, $email, $language) {
+
+ global $CONFIG;
+ $name = sanitise_string($name);
+ $username = sanitise_string($username);
+ $password = sanitise_string($password);
+ $email = sanitise_string($email);
+ $language = sanitise_string($language);
+ $created = time();
+ foreach(array('name','username','password','email','language','created') as $param) {
+ if ($$param != null) {
+ $params[] = "{$param} = '{$$param}'";
+ }
+ }
+ return insert_data("insert into {$CONFIG->dbprefix}users set " . implode(",",$params));
+
+ }
+
+ /**
+ * Deletes a given user, completely
+ *
+ * @param int $id The user ID
+ * @return true|false
+ */
+ function delete_user($id) {
+
+ global $CONFIG;
+ $id = (int) $id;
+ if ($user = get_user($id)) {
+
+ if (!(trigger_event("delete","user",$user)))
+ return false;
+
+ // Get user sites
+ $sites = $user->getSites();
+
+ if (!empty($sites))
+ foreach($sites as $site) {
+ // Remove friends
+ if ($friends = $user->getFriends($site->id)) {
+ foreach($friends as $friend) {
+ $user->removeFriend($friend->id,$site->id);
+ }
+ }
+ // Remove friends of
+ if ($friendsof = $user->getFriendsOf($site->id)) {
+ foreach($friendsof as $friend) {
+ $friend->removeFriend($user->id,$site->id);
+ }
+ }
+ // Remove objects
+ if ($objects = $user->getObjects("",999999999, 0,$site->id)) {
+ foreach($objects as $object) {
+ $object->delete();
+ }
+ }
+ }
+ }
+
+ return true;
+
+ }
+
+ /**
+ * Convert a database row to a new ElggUser
+ *
+ * @param stdClass $row
+ * @return stdClass or ElggUser
+ */
+ function row_to_elgguser($row) {
+ if (!($row instanceof stdClass))
+ return $row;
+ return new ElggSite($row);
+ }
+
+
+ /**
* Session management
*/
diff --git a/engine/schema/mysql.sql b/engine/schema/mysql.sql
index 2ce3f71e4..5ba16d344 100644
--- a/engine/schema/mysql.sql
+++ b/engine/schema/mysql.sql
@@ -106,10 +106,9 @@ CREATE TABLE `prefix_users` (
`username` varchar(12) NOT NULL default '',
`password` varchar(32) NOT NULL default '',
`email` text NOT NULL,
+ `language` varchar(6) NOT NULL default '',
`code` varchar(32) NOT NULL default '',
- `last_updated` int(11) NOT NULL default '0',
- `registered` int(11) NOT NULL default '0',
- `enabled` enum('yes','no') NOT NULL default 'no',
+ `created` int(11) NOT NULL default '0',
`last_action` int(11) NOT NULL default '0',
`prev_last_action` int(11) NOT NULL default '0',
`last_login` int(11) NOT NULL default '0',
@@ -141,6 +140,29 @@ CREATE TABLE `prefix_sites` (
PRIMARY KEY (`id`)
) ENGINE=MyISAM ;
+--
+-- Link table between users and sites
+--
+
+CREATE TABLE `prefix_users_sites` (
+ `user_id` int(11) NOT NULL,
+ `site_id` int(11) NOT NULL,
+ PRIMARY KEY (`user_id`,`site_id`)
+) ENGINE=MyISAM ;
+
+--
+-- Table structure for friends
+--
+
+CREATE TABLE `prefix_friends` (
+ `id` int(11) NOT NULL auto_increment,
+ `user_id` int(11) NOT NULL,
+ `friend_id` int(11) NOT NULL,
+ `site_id` int(11) NOT NULL,
+ PRIMARY KEY (`id`),
+ KEY `user_id` (`user_id`),
+ KEY `friend_id` (`friend_id`)
+) ENGINE=MyISAM;
--
-- Table structure for annotations
diff --git a/index.php b/index.php
index 2ba68800d..e0b2774a3 100644
--- a/index.php
+++ b/index.php
@@ -35,8 +35,8 @@
// Create / load a site
$site = get_site(1);
error_log("GETTIGN SITE " . print_r($site, true));
-
- if (!$site)
+
+ if (empty($site))
{
$site = new ElggSite();