Ejemplo n.º 1
0
function phorum_htmlpurifier_migrate_sigs($offset)
{
    global $PHORUM;
    if (!$offset) {
        return;
    }
    // bail out quick if $offset == 0
    // theoretically, we could get rid of this multi-request
    // doo-hickery if safe mode is off
    @set_time_limit(0);
    // attempt to let this run
    $increment = $PHORUM['mod_htmlpurifier']['migrate-sigs-increment'];
    require_once dirname(__FILE__) . '/../migrate.php';
    // migrate signatures
    // do this in batches so we don't run out of time/space
    $end = $offset + $increment;
    $user_ids = array();
    for ($i = $offset; $i < $end; $i++) {
        $user_ids[] = $i;
    }
    $userinfos = phorum_db_user_get_fields($user_ids, 'signature');
    foreach ($userinfos as $i => $user) {
        if (empty($user['signature'])) {
            continue;
        }
        $sig = $user['signature'];
        // perform standard Phorum processing on the sig
        $sig = str_replace(array("&", "<", ">"), array("&amp;", "&lt;", "&gt;"), $sig);
        $sig = preg_replace("/<((http|https|ftp):\\/\\/[a-z0-9;\\/\\?:@=\\&\$\\-_\\.\\+!*'\\(\\),~%]+?)>/i", "\$1", $sig);
        // prepare fake data to pass to migration function
        $fake_data = array(array("author" => "", "email" => "", "subject" => "", 'body' => $sig));
        list($fake_message) = phorum_htmlpurifier_migrate($fake_data);
        $user['signature'] = $fake_message['body'];
        if (!phorum_api_user_save($user)) {
            exit('Error while saving user data');
        }
    }
    unset($userinfos);
    // free up memory
    // query for highest ID in database
    $type = $PHORUM['DBCONFIG']['type'];
    $sql = "select MAX(user_id) from {$PHORUM['user_table']}";
    $row = phorum_db_interact(DB_RETURN_ROW, $sql);
    $top_id = (int) $row[0];
    $offset += $increment;
    if ($offset > $top_id) {
        // test for end condition
        echo 'Migration finished';
        $PHORUM['mod_htmlpurifier']['migrate-sigs'] = FALSE;
        phorum_htmlpurifier_commit_settings();
        return TRUE;
    }
    $host = $_SERVER['HTTP_HOST'];
    $uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    $extra = 'admin.php?module=modsettings&mod=htmlpurifier&migrate-sigs=' . $offset;
    // relies on output buffering to work
    header("Location: http://{$host}{$uri}/{$extra}");
    exit;
}
Ejemplo n.º 2
0
/**
 * 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;
    }
}
Ejemplo n.º 3
0
/**
 * 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;
}