/** * Retrieve data for Phorum users. * * @param mixed $user_id * Either a single user_id or an array of user_ids. * * @param boolean $detailed * If this parameter is TRUE (default is FALSE), then the user's * groups and permissions are included in the user data. * * @param boolean $use_write_server * This parameter is for internal use only. It is used to flag that * the database layer has to run the query against the master database * server (known as the "write server"; only applicable if the database * system is setup as a replicated master/slave environment). When you * are using this API call in your own code, then you most probably do * not need to use this parameter. * * @return mixed * If the $user_id parameter is a single user_id, then either an array * containing user data is returned or NULL if the user was not found. * If the $user_id parameter is an array of user_ids, then an array * of user data arrays is returned, indexed by the user_id. * Users for user_ids that are not found are not included in the * returned array. */ function phorum_api_user_get($user_id, $detailed = FALSE, $use_write_server = FALSE, $raw_data = FALSE) { $PHORUM = $GLOBALS['PHORUM']; if (!is_array($user_id)) { $user_ids = array($user_id); } else { $user_ids = $user_id; } // Prepare the return data array. For each requested user_id, // a slot is prepared in this array. Also, turn the user id array // into an array which has the user_id as both the key and value. $users = array(); $new_user_ids = array(); foreach ($user_ids as $id) { $users[$id] = NULL; $new_user_ids[$id] = $id; } $user_ids = $new_user_ids; // First, try to retrieve user data from the user cache, // if user caching is enabled. if ($raw_data === FALSE && !empty($PHORUM['cache_users'])) { $cached_users = phorum_cache_get('user', $user_ids); if (is_array($cached_users)) { foreach ($cached_users as $id => $user) { $users[$id] = $user; unset($user_ids[$id]); } // We need to retrieve the data for some dynamic fields // from the database. $dynamic_data = phorum_db_user_get_fields(array_keys($cached_users), array('date_last_active', 'last_active_forum', 'posts')); // Store the results in the users array. foreach ($dynamic_data as $id => $data) { $users[$id] = array_merge($users[$id], $data); } } } // Retrieve user data for the users for which no data was // retrieved from the cache. if (count($user_ids)) { $db_users = phorum_db_user_get($user_ids, $detailed, $use_write_server, $raw_data); foreach ($db_users as $id => $user) { // Merge the group and forum permissions into a final // permission value per forum. Forum permissions that are // assigned to a user directly override any group based // permission. if (!$user['admin']) { if (!empty($user['group_permissions'])) { foreach ($user['group_permissions'] as $fid => $perm) { if (!isset($user['permissions'][$fid])) { $user['permissions'][$fid] = $perm; } else { $user['permissions'][$fid] |= $perm; } } } if (!empty($user['forum_permissions'])) { foreach ($user['forum_permissions'] as $fid => $perm) { $user['permissions'][$fid] = $perm; } } } // If detailed information was requested, we store the data in // the cache. For non-detailed information, we do not cache the // data, because there is not much to gain there by caching. if ($detailed && !empty($PHORUM['cache_users']) && $raw_data === FALSE) { phorum_cache_put('user', $id, $user); } // Store the results in the users array. $users[$id] = $user; } } // Remove the users for which we did not find data from the array. foreach ($users as $id => $user) { if ($user === NULL) { unset($users[$id]); } } /** * [hook] * user_get * * [description] * This hook can be used to handle the data that was retrieved * from the database for a user. Modules can add and modify the * user data.<sbr/> * <sbr/> * In combination with the <hook>user_save</hook> hook, this hook * could also be used to store and retrieve some of the Phorum * user fields in some external system * * [category] * User data handling * * [when] * Just after user data has been retrieved from the database. * * [input] * This hook receives two arguments.<sbr/> * The first argument contains an array of users. * Each item in this array is an array containing data for * a single user, which can be updated.<sbr/> * The second argument contains a boolean that indicates whether * detailed information (i.e. including group info) is retrieved. * * [output] * The array that was used as the first argument for the hook call, * possibly with some updated users in it. * * [example] * <hookcode> * function phorum_mod_foo_user_get($user, $detailed) * { * // Let's asume that our usernames are based on the * // system users on a UNIX system. We could merge some * // info from the password file with the Phorum info here. * * // First try to lookup the password file entry. * // Return if this lookup fails. * $pw = posix_getpwnam($user['username']); * if (empty($pw)) return $user; * * // On a lot of systems, the "gecos" field contains * // the real name for the user. * $user['real_name'] = $pw["gecos"] != '' * ? $pw["gecos"] * : $user["real_name"]; * * // If a custom profile field "shell" was created, then * // we could also put the user's shell in the data. * $user['shell'] = $pw['shell']; * * return $user; * } * </hookcode> */ if (isset($PHORUM['hooks']['user_get'])) { $users = phorum_hook('user_get', $users, $detailed); } // Return the results. if (is_array($user_id)) { return $users; } else { return isset($users[$user_id]) ? $users[$user_id] : NULL; } }
/** * Add a buddy for a user. * * @param integer $buddy_user_id * The user_id that has to be added as a buddy. * * @param mixed $user_id * The user_id the buddy has to be added for * or NULL to use the active Phorum user (default). * * @return mixed * The id that was assigned to the new buddy or the existing id if * the buddy already existed. If no user can be found for the * $buddy_user_id parameter, then NULL will be returned. */ function phorum_db_pm_buddy_add($buddy_user_id, $user_id = NULL) { $PHORUM = $GLOBALS['PHORUM']; settype($buddy_user_id, 'int'); if ($user_id === NULL) { $user_id = $PHORUM['user']['user_id']; } settype($user_id, 'int'); // Check if the buddy_user_id is a valid user_id. $valid = phorum_db_user_get($buddy_user_id, FALSE); if (!$valid) { return NULL; } // See if the user is already a buddy. $pm_buddy_id = phorum_db_pm_is_buddy($buddy_user_id); // If not, then create insert a new buddy relation. if ($pm_buddy_id === NULL) { $pm_buddy_id = phorum_db_interact(DB_RETURN_NEWID, "INSERT INTO {$PHORUM['pm_buddies_table']}\n (user_id, buddy_user_id)\n VALUES ({$user_id}, {$buddy_user_id})", NULL, DB_MASTERQUERY); } return $pm_buddy_id; }
/** * This function retrieves a user from the database, given the user id. * If $user_id is an array of user ids, it will retrieve all of the users * in the array. If $detailed is set to true, the function gets the users * full information. Setting this to false omits permission data, pm counts, * and group membership. $detailed is true by default and may be omitted. * @param user_id - can be a single user id, or an array of user ids. * @param detailed - get detailed user information (defaults to true). * @param checknewpm - check for new private messages for the user (defaults to false). * @return array - either an array representing a single user's information, * or an array of users */ function phorum_user_get( $user_id, $detailed = true, $checkpm = false ) { $PHORUM = $GLOBALS["PHORUM"]; if ( !is_array( $user_id ) ) { $user_ids = array( $user_id ); } else { $user_ids = $user_id; } if ( count( $user_ids ) ) { $cache_users=array(); $tmp_users=array(); $cachecnt=0; // get users from cache if enabled if(isset($PHORUM['cache_users']) && $PHORUM['cache_users']) { foreach($user_ids as $id => $cur_user_id) { $data=phorum_cache_get('user',$cur_user_id); if($data != null) { // null if no key found $cache_users[$cur_user_id]=$data; unset($user_ids[$id]); $cachecnt++; } } unset($data); // we need to get the dynamic data too! // only selecting date_last_active, forum_last_active, // posts ... any more? if($cachecnt > 0) { $dynamic_data=phorum_db_user_get_fields(array_keys($cache_users),array('date_last_active','last_active_forum','posts')); foreach($dynamic_data as $d_uid => $d_data) { $cache_users[$d_uid]=array_merge($cache_users[$d_uid],$d_data); } } } if(count($user_ids)) { $tmp_users = phorum_db_user_get( $user_ids, $detailed ); foreach( $tmp_users as $uid => $user ) { if ( !$user["admin"] ) { if ( isset( $user["group_permissions"] ) ) { foreach( $user["group_permissions"] as $forum_id => $perm ) { if(!isset($user["permissions"][$forum_id])) $user["permissions"][$forum_id]=0; $user["permissions"][$forum_id] = $user["permissions"][$forum_id] | $perm; } } if ( isset( $user["forum_permissions"] ) ) { foreach( $user["forum_permissions"] as $forum_id => $perm ) { $user["permissions"][$forum_id] = $perm; } } } // check if the user has new private messages if ( ($checkpm || (isset($PHORUM['cache_users']) && $PHORUM['cache_users'])) && $PHORUM["enable_pm"] && $PHORUM["enable_new_pm_count"] ) { $user["new_private_messages"] = phorum_db_pm_checknew( $uid ); } // store users in cache if enabled if( $detailed && isset($PHORUM['cache_users']) && $PHORUM['cache_users']) { phorum_cache_put('user',$uid,$user); } $tmp_users[$uid] = $user; } } } // merging cached and retrieved users $ret = $tmp_users + $cache_users; if ( !is_array( $user_id ) ) { if (isset($ret[$user_id])) $ret = $ret[$user_id]; else $ret = NULL; } return $ret; }
<?php // Find all private messages. $res = phorum_db_interact(DB_RETURN_RES, "SELECT pm_message_id, user_id, meta\n FROM {$PHORUM["pm_messages_table"]}"); // Update the meta author + rcpt info for each private message. while ($row = phorum_db_fetch_row($res, DB_RETURN_ASSOC)) { // Update the PM author. $m = $row["pm_message_id"]; $u = $row["user_id"]; $user = phorum_db_user_get($u); if ($user) { $author = phorum_db_interact(DB_RETURN_QUOTED, $user["display_name"]); phorum_db_interact(DB_RETURN_RES, "UPDATE {$PHORUM["pm_messages_table"]}\n SET author = '{$author}'\n WHERE user_id = {$u}", NULL, DB_MASTERQUERY); } // Update the PM recipients. $meta = unserialize($row["meta"]); $userids = array_keys($meta["recipients"]); $users = phorum_db_user_get($userids); foreach ($users as $user) { unset($meta["recipients"][$user["user_id"]]["username"]); $meta["recipients"][$user["user_id"]]["display_name"] = $user["display_name"]; } $meta = phorum_db_interact(DB_RETURN_QUOTED, serialize($meta)); phorum_db_interact(DB_RETURN_RES, "UPDATE {$PHORUM["pm_messages_table"]}\n SET meta = '{$meta}'\n WHERE pm_message_id = {$m}", NULL, DB_MASTERQUERY); }
/** * This function adds a buddy for a user. It will return the * pm_buddy_id for the new buddy. If the buddy already exists, * it will return the existing pm_buddy_id. If a non existant * user_id is used for the buddy_user_id, the function will * return NULL. * @param buddy_user_id - The user_id that has to be added as a buddy. * @param user_id - The user_id the buddy has to be added for or * NULL to use the current user (default). */ function phorum_db_pm_buddy_add($buddy_user_id, $user_id = NULL) { $PHORUM = $GLOBALS['PHORUM']; $conn = phorum_db_postgresql_connect(); settype($buddyuser_id, "int"); if (is_null($user_id)) $user_id = $PHORUM["user"]["user_id"]; settype($user_id, "int"); // Check if the buddy_user_id is a valid user_id. $valid = phorum_db_user_get($buddy_user_id, false); if (!$valid) return NULL; $pm_buddy_id = phorum_db_pm_is_buddy($buddy_user_id); if (is_null($pm_buddy_id)) { $sql = "INSERT INTO {$PHORUM["pm_buddies_table"]} (user_id, buddy_user_id) " . "values($user_id, $buddy_user_id)"; $res = pg_query($conn, $sql); if ($err = pg_last_error()) phorum_db_pg_last_error("$err: $sql"); $pm_buddy_id = pgsql_insert_id($conn, "{$PHORUM["pm_buddies_table"]}_pm_buddy_id_seq"); } return $pm_buddy_id; }
/** * Add a buddy for a user. It will return the * pm_buddy_id for the new buddy. If the buddy already exists, * it will return the existing pm_buddy_id. If a non-existant * user_id is used for the buddy_user_id, the function will * return NULL. * * @param int $buddy_user_id - The user_id that has to be added as a buddy. * @param int $user_id - The user_id the buddy has to be added for or * NULL to use the current user (default). * * @return mixed */ function phorum_db_pm_buddy_add($buddy_user_id, $user_id = NULL) { $PHORUM = $GLOBALS['PHORUM']; $conn = phorum_db_mysql_connect(); settype($buddyuser_id, "int"); if (is_null($user_id)) $user_id = $PHORUM["user"]["user_id"]; settype($user_id, "int"); // Check if the buddy_user_id is a valid user_id. $valid = phorum_db_user_get($buddy_user_id, false); if (! $valid) return NULL; $pm_buddy_id = phorum_db_pm_is_buddy($buddy_user_id); if (is_null($pm_buddy_id)) { $sql = "INSERT INTO {$PHORUM["pm_buddies_table"]} SET " . "user_id = $user_id, " . "buddy_user_id = $buddy_user_id"; $res = mysql_query($sql, $conn); if ($err = mysql_error()) phorum_db_mysql_error("$err: $sql"); $pm_buddy_id = mysql_insert_id($conn); } return $pm_buddy_id; }