2003-05-09 14:42:42 +02:00
|
|
|
<?php
|
2004-02-12 00:20:38 +01:00
|
|
|
// +-----------------------------------------------------------------------+
|
2004-11-06 22:12:59 +01:00
|
|
|
// | PhpWebGallery - a PHP based picture gallery |
|
|
|
|
// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
|
2006-03-10 21:17:18 +01:00
|
|
|
// | Copyright (C) 2003-2006 PhpWebGallery Team - http://phpwebgallery.net |
|
2004-02-12 00:20:38 +01:00
|
|
|
// +-----------------------------------------------------------------------+
|
2004-11-06 22:12:59 +01:00
|
|
|
// | branch : BSF (Best So Far)
|
2006-03-30 02:37:07 +02:00
|
|
|
// | file : $Id$
|
2004-02-12 00:20:38 +01:00
|
|
|
// | last update : $Date$
|
|
|
|
// | last modifier : $Author$
|
|
|
|
// | revision : $Revision$
|
|
|
|
// +-----------------------------------------------------------------------+
|
|
|
|
// | This program is free software; you can redistribute it and/or modify |
|
|
|
|
// | it under the terms of the GNU General Public License as published by |
|
|
|
|
// | the Free Software Foundation |
|
|
|
|
// | |
|
|
|
|
// | This program is distributed in the hope that it will be useful, but |
|
|
|
|
// | WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
|
|
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
|
|
|
// | General Public License for more details. |
|
|
|
|
// | |
|
|
|
|
// | You should have received a copy of the GNU General Public License |
|
|
|
|
// | along with this program; if not, write to the Free Software |
|
|
|
|
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
|
|
|
|
// | USA. |
|
|
|
|
// +-----------------------------------------------------------------------+
|
2003-05-13 12:02:06 +02:00
|
|
|
|
|
|
|
// validate_mail_address verifies whether the given mail address has the
|
|
|
|
// right format. ie someone@domain.com "someone" can contain ".", "-" or
|
|
|
|
// even "_". Exactly as "domain". The extension doesn't have to be
|
|
|
|
// "com". The mail address can also be empty.
|
|
|
|
// If the mail address doesn't correspond, an error message is returned.
|
2003-05-09 14:42:42 +02:00
|
|
|
function validate_mail_address( $mail_address )
|
|
|
|
{
|
|
|
|
global $lang;
|
|
|
|
|
2003-05-13 12:02:06 +02:00
|
|
|
if ( $mail_address == '' )
|
2003-05-09 14:42:42 +02:00
|
|
|
{
|
2003-05-13 12:02:06 +02:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
$regex = '/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)*\.[a-z]+$/';
|
|
|
|
if ( !preg_match( $regex, $mail_address ) )
|
|
|
|
{
|
|
|
|
return $lang['reg_err_mail_address'];
|
2003-05-09 14:42:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-08 22:52:19 +02:00
|
|
|
function register_user($login, $password, $mail_address)
|
2003-05-09 14:42:42 +02:00
|
|
|
{
|
2004-12-28 18:56:33 +01:00
|
|
|
global $lang, $conf;
|
2003-05-09 14:42:42 +02:00
|
|
|
|
2004-12-28 18:56:33 +01:00
|
|
|
$errors = array();
|
|
|
|
if ($login == '')
|
|
|
|
{
|
|
|
|
array_push($errors, $lang['reg_err_login1']);
|
|
|
|
}
|
|
|
|
if (ereg("^.* $", $login))
|
|
|
|
{
|
|
|
|
array_push($errors, $lang['reg_err_login2']);
|
|
|
|
}
|
|
|
|
if (ereg("^ .*$", $login))
|
|
|
|
{
|
|
|
|
array_push($errors, $lang['reg_err_login3']);
|
|
|
|
}
|
2005-08-08 22:52:19 +02:00
|
|
|
if (get_userid($login))
|
2005-07-17 17:06:39 +02:00
|
|
|
{
|
|
|
|
array_push($errors, $lang['reg_err_login5']);
|
2003-05-09 14:42:42 +02:00
|
|
|
}
|
2005-08-08 22:52:19 +02:00
|
|
|
$mail_error = validate_mail_address($mail_address);
|
|
|
|
if ('' != $mail_error)
|
2004-12-28 18:56:33 +01:00
|
|
|
{
|
2005-08-08 22:52:19 +02:00
|
|
|
array_push($errors, $mail_error);
|
2004-12-28 18:56:33 +01:00
|
|
|
}
|
2003-05-13 12:02:06 +02:00
|
|
|
|
|
|
|
// if no error until here, registration of the user
|
2004-12-28 18:56:33 +01:00
|
|
|
if (count($errors) == 0)
|
2003-05-09 14:42:42 +02:00
|
|
|
{
|
2005-10-22 11:53:12 +02:00
|
|
|
// what will be the inserted id ?
|
|
|
|
$query = '
|
|
|
|
SELECT MAX('.$conf['user_fields']['id'].') + 1
|
|
|
|
FROM '.USERS_TABLE.'
|
|
|
|
;';
|
|
|
|
list($next_id) = mysql_fetch_array(pwg_query($query));
|
2006-03-07 02:20:02 +01:00
|
|
|
|
2005-08-08 22:52:19 +02:00
|
|
|
$insert =
|
|
|
|
array(
|
2005-10-22 11:53:12 +02:00
|
|
|
$conf['user_fields']['id'] => $next_id,
|
2005-08-08 22:52:19 +02:00
|
|
|
$conf['user_fields']['username'] => mysql_escape_string($login),
|
|
|
|
$conf['user_fields']['password'] => $conf['pass_convert']($password),
|
|
|
|
$conf['user_fields']['email'] => $mail_address
|
|
|
|
);
|
2004-12-28 18:56:33 +01:00
|
|
|
|
2005-08-08 22:52:19 +02:00
|
|
|
include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
|
|
|
|
mass_inserts(USERS_TABLE, array_keys($insert), array($insert));
|
2006-03-07 02:20:02 +01:00
|
|
|
|
2006-10-31 00:34:31 +01:00
|
|
|
// Assign by default groups
|
2006-10-29 11:36:54 +01:00
|
|
|
{
|
|
|
|
$query = '
|
2006-10-31 00:34:31 +01:00
|
|
|
SELECT id
|
|
|
|
FROM '.GROUPS_TABLE.'
|
|
|
|
WHERE is_default = \''.boolean_to_string(true).'\'
|
|
|
|
ORDER BY id ASC
|
|
|
|
;';
|
|
|
|
$result = pwg_query($query);
|
2006-10-29 11:36:54 +01:00
|
|
|
|
2006-10-31 00:34:31 +01:00
|
|
|
$inserts = array();
|
|
|
|
while ($row = mysql_fetch_array($result))
|
2006-10-29 11:36:54 +01:00
|
|
|
{
|
2006-10-31 00:34:31 +01:00
|
|
|
array_push
|
|
|
|
(
|
|
|
|
$inserts,
|
|
|
|
array
|
|
|
|
(
|
2006-10-29 11:36:54 +01:00
|
|
|
'user_id' => $next_id,
|
2006-10-31 00:34:31 +01:00
|
|
|
'group_id' => $row['id']
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2006-10-29 11:36:54 +01:00
|
|
|
|
2006-10-31 00:34:31 +01:00
|
|
|
if (count($inserts) != 0)
|
|
|
|
{
|
2006-10-29 11:36:54 +01:00
|
|
|
include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
|
2006-10-31 00:34:31 +01:00
|
|
|
mass_inserts(USER_GROUP_TABLE, array('user_id', 'group_id'), $inserts);
|
2006-10-29 11:36:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-22 11:53:12 +02:00
|
|
|
create_user_infos($next_id);
|
2003-05-09 14:42:42 +02:00
|
|
|
}
|
2005-10-22 11:53:12 +02:00
|
|
|
|
2005-08-08 22:52:19 +02:00
|
|
|
return $errors;
|
2003-05-09 14:42:42 +02:00
|
|
|
}
|
|
|
|
|
2004-02-19 01:31:09 +01:00
|
|
|
function setup_style($style)
|
|
|
|
{
|
2005-01-06 23:16:21 +01:00
|
|
|
return new Template(PHPWG_ROOT_PATH.'template/'.$style);
|
2004-02-19 01:31:09 +01:00
|
|
|
}
|
|
|
|
|
2006-10-20 04:17:53 +02:00
|
|
|
function build_user( $user_id, $use_cache )
|
|
|
|
{
|
|
|
|
global $conf;
|
|
|
|
$user['id'] = $user_id;
|
|
|
|
$user = array_merge( $user, getuserdata($user_id, $use_cache) );
|
|
|
|
if ( $user['id'] == $conf['guest_id'])
|
|
|
|
{
|
|
|
|
$user['is_the_guest']=true;
|
|
|
|
$user['template'] = $conf['default_template'];
|
|
|
|
$user['nb_image_line'] = $conf['nb_image_line'];
|
|
|
|
$user['nb_line_page'] = $conf['nb_line_page'];
|
|
|
|
$user['language'] = $conf['default_language'];
|
|
|
|
$user['maxwidth'] = $conf['default_maxwidth'];
|
|
|
|
$user['maxheight'] = $conf['default_maxheight'];
|
|
|
|
$user['recent_period'] = $conf['recent_period'];
|
|
|
|
$user['expand'] = $conf['auto_expand'];
|
|
|
|
$user['show_nb_comments'] = $conf['show_nb_comments'];
|
|
|
|
$user['enabled_high'] = $conf['newuser_default_enabled_high'];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$user['is_the_guest']=false;
|
|
|
|
}
|
|
|
|
// calculation of the number of picture to display per page
|
|
|
|
$user['nb_image_page'] = $user['nb_image_line'] * $user['nb_line_page'];
|
|
|
|
|
|
|
|
// include template/theme configuration
|
|
|
|
if (defined('IN_ADMIN') and IN_ADMIN)
|
|
|
|
{
|
|
|
|
list($user['template'], $user['theme']) =
|
|
|
|
explode
|
|
|
|
(
|
|
|
|
'/',
|
|
|
|
isset($conf['default_admin_layout']) ? $conf['default_admin_layout']
|
|
|
|
: $user['template']
|
|
|
|
);
|
|
|
|
// TODO : replace $conf['admin_layout'] by $user['admin_layout']
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
list($user['template'], $user['theme']) = explode('/', $user['template']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2005-08-08 22:52:19 +02:00
|
|
|
/**
|
|
|
|
* find informations related to the user identifier
|
|
|
|
*
|
|
|
|
* @param int user identifier
|
|
|
|
* @param boolean use_cache
|
|
|
|
* @param array
|
|
|
|
*/
|
|
|
|
function getuserdata($user_id, $use_cache)
|
2004-03-20 01:52:37 +01:00
|
|
|
{
|
2005-08-08 22:52:19 +02:00
|
|
|
global $conf;
|
|
|
|
|
|
|
|
$userdata = array();
|
2006-03-07 02:20:02 +01:00
|
|
|
|
2005-08-08 22:52:19 +02:00
|
|
|
$query = '
|
|
|
|
SELECT ';
|
|
|
|
$is_first = true;
|
|
|
|
foreach ($conf['user_fields'] as $pwgfield => $dbfield)
|
|
|
|
{
|
|
|
|
if ($is_first)
|
|
|
|
{
|
|
|
|
$is_first = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$query.= '
|
|
|
|
, ';
|
|
|
|
}
|
|
|
|
$query.= $dbfield.' AS '.$pwgfield;
|
|
|
|
}
|
|
|
|
$query.= '
|
|
|
|
FROM '.USERS_TABLE.'
|
|
|
|
WHERE '.$conf['user_fields']['id'].' = \''.$user_id.'\'
|
|
|
|
;';
|
2006-03-07 02:20:02 +01:00
|
|
|
|
2005-08-08 22:52:19 +02:00
|
|
|
$row = mysql_fetch_array(pwg_query($query));
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
$query = '
|
|
|
|
SELECT ui.*, uc.*
|
|
|
|
FROM '.USER_INFOS_TABLE.' AS ui LEFT JOIN '.USER_CACHE_TABLE.' AS uc
|
|
|
|
ON ui.user_id = uc.user_id
|
|
|
|
WHERE ui.user_id = \''.$user_id.'\'
|
|
|
|
;';
|
|
|
|
$result = pwg_query($query);
|
|
|
|
if (mysql_num_rows($result) > 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
create_user_infos($user_id);
|
|
|
|
}
|
|
|
|
}
|
2006-03-07 02:20:02 +01:00
|
|
|
|
2005-08-08 22:52:19 +02:00
|
|
|
$row = array_merge($row, mysql_fetch_array($result));
|
2006-03-07 02:20:02 +01:00
|
|
|
|
2005-08-08 22:52:19 +02:00
|
|
|
foreach ($row as $key => $value)
|
|
|
|
{
|
|
|
|
if (!is_numeric($key))
|
|
|
|
{
|
|
|
|
// If the field is true or false, the variable is transformed into a
|
|
|
|
// boolean value.
|
|
|
|
if ($value == 'true' or $value == 'false')
|
|
|
|
{
|
|
|
|
$userdata[$key] = get_boolean($value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$userdata[$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($use_cache)
|
|
|
|
{
|
|
|
|
if (!isset($userdata['need_update'])
|
|
|
|
or !is_bool($userdata['need_update'])
|
|
|
|
or $userdata['need_update'] == true)
|
|
|
|
{
|
|
|
|
$userdata['forbidden_categories'] =
|
|
|
|
calculate_permissions($userdata['id'], $userdata['status']);
|
|
|
|
|
2006-03-15 03:26:25 +01:00
|
|
|
$query = '
|
|
|
|
SELECT COUNT(DISTINCT(image_id)) as total
|
|
|
|
FROM '.IMAGE_CATEGORY_TABLE.'
|
|
|
|
WHERE category_id NOT IN ('.$userdata['forbidden_categories'].')
|
|
|
|
;';
|
|
|
|
list($userdata['nb_total_images']) = mysql_fetch_array(pwg_query($query));
|
|
|
|
|
2005-08-08 22:52:19 +02:00
|
|
|
// update user cache
|
|
|
|
$query = '
|
|
|
|
DELETE FROM '.USER_CACHE_TABLE.'
|
|
|
|
WHERE user_id = '.$userdata['id'].'
|
|
|
|
;';
|
|
|
|
pwg_query($query);
|
2006-03-07 02:20:02 +01:00
|
|
|
|
2005-08-08 22:52:19 +02:00
|
|
|
$query = '
|
|
|
|
INSERT INTO '.USER_CACHE_TABLE.'
|
2006-03-15 03:26:25 +01:00
|
|
|
(user_id,need_update,forbidden_categories,nb_total_images)
|
2005-08-08 22:52:19 +02:00
|
|
|
VALUES
|
2006-03-15 03:26:25 +01:00
|
|
|
('.$userdata['id'].',\'false\',\''
|
|
|
|
.$userdata['forbidden_categories'].'\','.$userdata['nb_total_images'].')
|
2005-08-08 22:52:19 +02:00
|
|
|
;';
|
|
|
|
pwg_query($query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $userdata;
|
2004-02-19 01:31:09 +01:00
|
|
|
}
|
2004-12-18 23:05:30 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* deletes favorites of the current user if he's not allowed to see them
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function check_user_favorites()
|
|
|
|
{
|
|
|
|
global $user;
|
|
|
|
|
|
|
|
if ($user['forbidden_categories'] == '')
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2005-08-18 23:40:59 +02:00
|
|
|
|
|
|
|
// retrieving images allowed : belonging to at least one authorized
|
|
|
|
// category
|
2004-12-18 23:05:30 +01:00
|
|
|
$query = '
|
2005-08-18 23:40:59 +02:00
|
|
|
SELECT DISTINCT f.image_id
|
2004-12-18 23:05:30 +01:00
|
|
|
FROM '.FAVORITES_TABLE.' AS f INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic
|
|
|
|
ON f.image_id = ic.image_id
|
|
|
|
WHERE f.user_id = '.$user['id'].'
|
2005-08-18 23:40:59 +02:00
|
|
|
AND ic.category_id NOT IN ('.$user['forbidden_categories'].')
|
2004-12-18 23:05:30 +01:00
|
|
|
;';
|
|
|
|
$result = pwg_query($query);
|
2005-08-18 23:40:59 +02:00
|
|
|
$authorizeds = array();
|
2004-12-18 23:05:30 +01:00
|
|
|
while ($row = mysql_fetch_array($result))
|
|
|
|
{
|
2005-08-18 23:40:59 +02:00
|
|
|
array_push($authorizeds, $row['image_id']);
|
2004-12-18 23:05:30 +01:00
|
|
|
}
|
|
|
|
|
2005-08-18 23:40:59 +02:00
|
|
|
$query = '
|
|
|
|
SELECT image_id
|
|
|
|
FROM '.FAVORITES_TABLE.'
|
|
|
|
WHERE user_id = '.$user['id'].'
|
|
|
|
;';
|
|
|
|
$result = pwg_query($query);
|
|
|
|
$favorites = array();
|
|
|
|
while ($row = mysql_fetch_array($result))
|
|
|
|
{
|
|
|
|
array_push($favorites, $row['image_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$to_deletes = array_diff($favorites, $authorizeds);
|
|
|
|
|
|
|
|
if (count($to_deletes) > 0)
|
2004-12-18 23:05:30 +01:00
|
|
|
{
|
|
|
|
$query = '
|
|
|
|
DELETE FROM '.FAVORITES_TABLE.'
|
2005-08-18 23:40:59 +02:00
|
|
|
WHERE image_id IN ('.implode(',', $to_deletes).')
|
2004-12-18 23:05:30 +01:00
|
|
|
AND user_id = '.$user['id'].'
|
|
|
|
;';
|
|
|
|
pwg_query($query);
|
|
|
|
}
|
|
|
|
}
|
2004-12-20 13:30:36 +01:00
|
|
|
|
|
|
|
/**
|
2005-08-08 22:52:19 +02:00
|
|
|
* calculates the list of forbidden categories for a given user
|
2004-12-20 13:30:36 +01:00
|
|
|
*
|
2005-08-08 22:52:19 +02:00
|
|
|
* Calculation is based on private categories minus categories authorized to
|
|
|
|
* the groups the user belongs to minus the categories directly authorized
|
|
|
|
* to the user. The list contains at least -1 to be compliant with queries
|
|
|
|
* such as "WHERE category_id NOT IN ($forbidden_categories)"
|
2004-12-20 13:30:36 +01:00
|
|
|
*
|
|
|
|
* @param int user_id
|
2005-01-08 12:23:52 +01:00
|
|
|
* @param string user_status
|
2004-12-20 13:30:36 +01:00
|
|
|
* @return string forbidden_categories
|
|
|
|
*/
|
2005-01-08 12:23:52 +01:00
|
|
|
function calculate_permissions($user_id, $user_status)
|
2004-12-20 13:30:36 +01:00
|
|
|
{
|
2006-03-10 21:17:18 +01:00
|
|
|
global $user;
|
|
|
|
|
2004-12-20 13:30:36 +01:00
|
|
|
$private_array = array();
|
|
|
|
$authorized_array = array();
|
|
|
|
|
|
|
|
$query = '
|
|
|
|
SELECT id
|
|
|
|
FROM '.CATEGORIES_TABLE.'
|
|
|
|
WHERE status = \'private\'
|
|
|
|
;';
|
|
|
|
$result = pwg_query($query);
|
|
|
|
while ($row = mysql_fetch_array($result))
|
|
|
|
{
|
|
|
|
array_push($private_array, $row['id']);
|
|
|
|
}
|
2005-01-08 12:23:52 +01:00
|
|
|
|
2004-12-20 13:30:36 +01:00
|
|
|
// retrieve category ids directly authorized to the user
|
|
|
|
$query = '
|
|
|
|
SELECT cat_id
|
|
|
|
FROM '.USER_ACCESS_TABLE.'
|
|
|
|
WHERE user_id = '.$user_id.'
|
|
|
|
;';
|
2005-08-08 22:52:19 +02:00
|
|
|
$authorized_array = array_from_query($query, 'cat_id');
|
2004-12-20 13:30:36 +01:00
|
|
|
|
|
|
|
// retrieve category ids authorized to the groups the user belongs to
|
|
|
|
$query = '
|
|
|
|
SELECT cat_id
|
|
|
|
FROM '.USER_GROUP_TABLE.' AS ug INNER JOIN '.GROUP_ACCESS_TABLE.' AS ga
|
|
|
|
ON ug.group_id = ga.group_id
|
|
|
|
WHERE ug.user_id = '.$user_id.'
|
|
|
|
;';
|
2005-08-08 22:52:19 +02:00
|
|
|
$authorized_array =
|
|
|
|
array_merge(
|
|
|
|
$authorized_array,
|
|
|
|
array_from_query($query, 'cat_id')
|
|
|
|
);
|
2004-12-20 13:30:36 +01:00
|
|
|
|
|
|
|
// uniquify ids : some private categories might be authorized for the
|
|
|
|
// groups and for the user
|
|
|
|
$authorized_array = array_unique($authorized_array);
|
|
|
|
|
|
|
|
// only unauthorized private categories are forbidden
|
|
|
|
$forbidden_array = array_diff($private_array, $authorized_array);
|
|
|
|
|
2006-04-01 03:24:21 +02:00
|
|
|
// if user is not an admin, locked categories are forbidden
|
|
|
|
if (!is_admin($user_status))
|
|
|
|
{
|
|
|
|
$query = '
|
|
|
|
SELECT id
|
|
|
|
FROM '.CATEGORIES_TABLE.'
|
|
|
|
WHERE visible = \'false\'
|
|
|
|
;';
|
|
|
|
$result = pwg_query($query);
|
|
|
|
while ($row = mysql_fetch_array($result))
|
|
|
|
{
|
|
|
|
array_push($forbidden_array, $row['id']);
|
|
|
|
}
|
|
|
|
$forbidden_array = array_unique($forbidden_array);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( empty($forbidden_array) )
|
2006-05-31 23:44:46 +02:00
|
|
|
{// at least, the list contains 0 value. This category does not exists so
|
|
|
|
// where clauses such as "WHERE category_id NOT IN(0)" will always be
|
2006-04-01 03:24:21 +02:00
|
|
|
// true.
|
2006-05-31 23:44:46 +02:00
|
|
|
array_push($forbidden_array, 0);
|
2006-04-01 03:24:21 +02:00
|
|
|
}
|
2006-03-07 02:20:02 +01:00
|
|
|
|
2005-08-08 22:52:19 +02:00
|
|
|
return implode(',', $forbidden_array);
|
2004-12-20 13:30:36 +01:00
|
|
|
}
|
2005-01-20 00:34:42 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* returns the username corresponding to the given user identifier if exists
|
|
|
|
*
|
|
|
|
* @param int user_id
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function get_username($user_id)
|
|
|
|
{
|
2005-08-08 22:52:19 +02:00
|
|
|
global $conf;
|
2006-03-07 02:20:02 +01:00
|
|
|
|
2005-01-20 00:34:42 +01:00
|
|
|
$query = '
|
2005-08-08 22:52:19 +02:00
|
|
|
SELECT '.$conf['user_fields']['username'].'
|
2005-01-20 00:34:42 +01:00
|
|
|
FROM '.USERS_TABLE.'
|
2005-08-08 22:52:19 +02:00
|
|
|
WHERE '.$conf['user_fields']['id'].' = '.intval($user_id).'
|
2005-01-20 00:34:42 +01:00
|
|
|
;';
|
|
|
|
$result = pwg_query($query);
|
|
|
|
if (mysql_num_rows($result) > 0)
|
|
|
|
{
|
|
|
|
list($username) = mysql_fetch_row($result);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2006-03-07 02:20:02 +01:00
|
|
|
|
2005-01-20 00:34:42 +01:00
|
|
|
return $username;
|
|
|
|
}
|
2005-07-16 16:29:35 +02:00
|
|
|
|
2005-08-08 22:52:19 +02:00
|
|
|
/**
|
|
|
|
* returns user identifier thanks to his name, false if not found
|
|
|
|
*
|
|
|
|
* @param string username
|
|
|
|
* @param int user identifier
|
|
|
|
*/
|
|
|
|
function get_userid($username)
|
|
|
|
{
|
|
|
|
global $conf;
|
|
|
|
|
|
|
|
$username = mysql_escape_string($username);
|
|
|
|
|
|
|
|
$query = '
|
|
|
|
SELECT '.$conf['user_fields']['id'].'
|
|
|
|
FROM '.USERS_TABLE.'
|
|
|
|
WHERE '.$conf['user_fields']['username'].' = \''.$username.'\'
|
|
|
|
;';
|
|
|
|
$result = pwg_query($query);
|
|
|
|
|
|
|
|
if (mysql_num_rows($result) == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
list($user_id) = mysql_fetch_row($result);
|
|
|
|
return $user_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-16 16:29:35 +02:00
|
|
|
/**
|
|
|
|
* search an available feed_id
|
|
|
|
*
|
|
|
|
* @return string feed identifier
|
|
|
|
*/
|
|
|
|
function find_available_feed_id()
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
$key = generate_key(50);
|
|
|
|
$query = '
|
|
|
|
SELECT COUNT(*)
|
2005-08-19 15:54:40 +02:00
|
|
|
FROM '.USER_FEED_TABLE.'
|
|
|
|
WHERE id = \''.$key.'\'
|
2005-07-16 16:29:35 +02:00
|
|
|
;';
|
|
|
|
list($count) = mysql_fetch_row(pwg_query($query));
|
|
|
|
if (0 == $count)
|
|
|
|
{
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-08-08 22:52:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* add user informations based on default values
|
|
|
|
*
|
|
|
|
* @param int user_id
|
|
|
|
*/
|
|
|
|
function create_user_infos($user_id)
|
|
|
|
{
|
|
|
|
global $conf;
|
2006-03-07 02:20:02 +01:00
|
|
|
|
2005-08-08 22:52:19 +02:00
|
|
|
list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
|
|
|
|
|
2006-04-27 23:08:50 +02:00
|
|
|
if ($user_id == $conf['webmaster_id'])
|
|
|
|
{
|
|
|
|
$status = 'webmaster';
|
|
|
|
}
|
|
|
|
else if ($user_id == $conf['guest_id'])
|
|
|
|
{
|
|
|
|
$status = 'guest';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$status = 'normal';
|
|
|
|
}
|
2006-10-19 23:53:18 +02:00
|
|
|
|
2005-08-08 22:52:19 +02:00
|
|
|
$insert =
|
|
|
|
array(
|
|
|
|
'user_id' => $user_id,
|
2006-04-27 23:08:50 +02:00
|
|
|
'status' => $status,
|
2005-08-08 22:52:19 +02:00
|
|
|
'template' => $conf['default_template'],
|
|
|
|
'nb_image_line' => $conf['nb_image_line'],
|
|
|
|
'nb_line_page' => $conf['nb_line_page'],
|
|
|
|
'language' => $conf['default_language'],
|
|
|
|
'recent_period' => $conf['recent_period'],
|
|
|
|
'expand' => boolean_to_string($conf['auto_expand']),
|
|
|
|
'show_nb_comments' => boolean_to_string($conf['show_nb_comments']),
|
|
|
|
'maxwidth' => $conf['default_maxwidth'],
|
|
|
|
'maxheight' => $conf['default_maxheight'],
|
2006-03-14 22:31:31 +01:00
|
|
|
'registration_date' => $dbnow,
|
2006-04-27 23:08:50 +02:00
|
|
|
'enabled_high' =>
|
|
|
|
boolean_to_string($conf['newuser_default_enabled_high']),
|
2005-08-08 22:52:19 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
|
|
|
|
mass_inserts(USER_INFOS_TABLE, array_keys($insert), array($insert));
|
|
|
|
}
|
2005-08-17 16:25:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* returns the groupname corresponding to the given group identifier if
|
|
|
|
* exists
|
|
|
|
*
|
|
|
|
* @param int group_id
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function get_groupname($group_id)
|
|
|
|
{
|
|
|
|
$query = '
|
|
|
|
SELECT name
|
|
|
|
FROM '.GROUPS_TABLE.'
|
|
|
|
WHERE id = '.intval($group_id).'
|
|
|
|
;';
|
|
|
|
$result = pwg_query($query);
|
|
|
|
if (mysql_num_rows($result) > 0)
|
|
|
|
{
|
|
|
|
list($groupname) = mysql_fetch_row($result);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2006-03-07 02:20:02 +01:00
|
|
|
|
2005-08-17 16:25:38 +02:00
|
|
|
return $groupname;
|
|
|
|
}
|
2005-09-27 23:57:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* return the file path of the given language filename, depending on the
|
|
|
|
* availability of the file
|
|
|
|
*
|
|
|
|
* in descending order of preference: user language, default language,
|
|
|
|
* PhpWebGallery default language.
|
|
|
|
*
|
|
|
|
* @param string filename
|
|
|
|
* @return string filepath
|
|
|
|
*/
|
|
|
|
function get_language_filepath($filename)
|
|
|
|
{
|
|
|
|
global $user, $conf;
|
2006-03-07 02:20:02 +01:00
|
|
|
|
2006-10-19 23:53:18 +02:00
|
|
|
$directories = array();
|
|
|
|
if ( isset($user['language']) )
|
|
|
|
{
|
|
|
|
$directories[] = PHPWG_ROOT_PATH.'language/'.$user['language'];
|
|
|
|
}
|
|
|
|
$directories[] = PHPWG_ROOT_PATH.'language/'.$conf['default_language'];
|
|
|
|
$directories[] = PHPWG_ROOT_PATH.'language/'.PHPWG_DEFAULT_LANGUAGE;
|
2005-09-27 23:57:14 +02:00
|
|
|
|
|
|
|
foreach ($directories as $directory)
|
|
|
|
{
|
|
|
|
$filepath = $directory.'/'.$filename;
|
2006-03-07 02:20:02 +01:00
|
|
|
|
2005-09-27 23:57:14 +02:00
|
|
|
if (file_exists($filepath))
|
|
|
|
{
|
|
|
|
return $filepath;
|
|
|
|
}
|
|
|
|
}
|
2006-03-07 02:20:02 +01:00
|
|
|
|
2005-09-27 23:57:14 +02:00
|
|
|
return false;
|
|
|
|
}
|
2006-03-07 02:20:02 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Performs all required actions for user login
|
|
|
|
* @param int user_id
|
|
|
|
* @param bool remember_me
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function log_user($user_id, $remember_me)
|
|
|
|
{
|
2006-07-28 11:34:27 +02:00
|
|
|
global $conf, $user;
|
2006-07-23 17:25:49 +02:00
|
|
|
|
2006-03-07 02:20:02 +01:00
|
|
|
if ($remember_me)
|
|
|
|
{
|
2006-07-23 17:25:49 +02:00
|
|
|
// search for an existing auto_login_key
|
|
|
|
$query = '
|
2006-10-19 23:53:18 +02:00
|
|
|
SELECT auto_login_key
|
2006-07-23 17:25:49 +02:00
|
|
|
FROM '.USERS_TABLE.'
|
|
|
|
WHERE '.$conf['user_fields']['id'].' = '.$user_id.'
|
|
|
|
;';
|
2006-10-19 23:53:18 +02:00
|
|
|
|
2006-07-23 17:25:49 +02:00
|
|
|
$auto_login_key = current(mysql_fetch_assoc(pwg_query($query)));
|
2006-10-19 23:53:18 +02:00
|
|
|
if (empty($auto_login_key))
|
2006-07-23 17:25:49 +02:00
|
|
|
{
|
|
|
|
$auto_login_key = base64_encode(md5(uniqid(rand(), true)));
|
|
|
|
$query = '
|
|
|
|
UPDATE '.USERS_TABLE.'
|
|
|
|
SET auto_login_key=\''.$auto_login_key.'\'
|
|
|
|
WHERE '.$conf['user_fields']['id'].' = '.$user_id.'
|
|
|
|
;';
|
|
|
|
pwg_query($query);
|
|
|
|
}
|
|
|
|
$cookie = array('id' => $user_id, 'key' => $auto_login_key);
|
|
|
|
setcookie($conf['remember_me_name'],
|
2006-10-19 23:53:18 +02:00
|
|
|
serialize($cookie),
|
2006-07-23 17:25:49 +02:00
|
|
|
time()+$conf['remember_me_length'],
|
|
|
|
cookie_path()
|
|
|
|
);
|
2006-03-07 02:20:02 +01:00
|
|
|
}
|
2006-10-20 04:17:53 +02:00
|
|
|
else
|
|
|
|
{ // make sure we clean any remember me ...
|
|
|
|
setcookie($conf['remember_me_name'], '', 0, cookie_path());
|
|
|
|
}
|
|
|
|
if ( session_id()!="" )
|
|
|
|
{ // this can happpen when the session is expired and auto_login
|
|
|
|
session_regenerate_id();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
session_start();
|
|
|
|
}
|
2006-04-21 04:11:29 +02:00
|
|
|
$_SESSION['pwg_uid'] = $user_id;
|
2006-07-28 11:34:27 +02:00
|
|
|
|
|
|
|
$user['id'] = $_SESSION['pwg_uid'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Performs auto-connexion when cookie remember_me exists
|
2006-10-20 04:17:53 +02:00
|
|
|
* @return true/false
|
2006-07-28 11:34:27 +02:00
|
|
|
*/
|
2006-10-19 23:53:18 +02:00
|
|
|
function auto_login() {
|
2006-07-28 11:34:27 +02:00
|
|
|
global $conf;
|
|
|
|
|
2006-10-20 04:17:53 +02:00
|
|
|
if ( isset( $_COOKIE[$conf['remember_me_name']] ) )
|
|
|
|
{
|
|
|
|
// must remove slash added in include/common.inc.php
|
|
|
|
$cookie = unserialize(stripslashes($_COOKIE[$conf['remember_me_name']]));
|
2006-08-07 15:39:19 +02:00
|
|
|
|
2006-10-20 04:17:53 +02:00
|
|
|
$query = '
|
2006-07-28 11:34:27 +02:00
|
|
|
SELECT auto_login_key
|
|
|
|
FROM '.USERS_TABLE.'
|
|
|
|
WHERE '.$conf['user_fields']['id'].' = '.$cookie['id'].'
|
|
|
|
;';
|
|
|
|
|
2006-10-20 04:17:53 +02:00
|
|
|
$auto_login_key = current(mysql_fetch_assoc(pwg_query($query)));
|
|
|
|
if ($auto_login_key == $cookie['key'])
|
|
|
|
{
|
|
|
|
log_user($cookie['id'], true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setcookie($conf['remember_me_name'], '', 0, cookie_path());
|
|
|
|
}
|
2006-10-19 23:53:18 +02:00
|
|
|
}
|
2006-10-20 04:17:53 +02:00
|
|
|
return false;
|
2006-03-07 02:20:02 +01:00
|
|
|
}
|
|
|
|
|
2006-03-09 00:14:53 +01:00
|
|
|
/*
|
2006-03-16 23:58:16 +01:00
|
|
|
* Return access_type definition of uuser
|
2006-03-09 23:46:28 +01:00
|
|
|
* Test does with user status
|
2006-03-09 00:14:53 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2006-03-16 23:58:16 +01:00
|
|
|
function get_access_type_status($user_status = '')
|
2006-03-09 00:14:53 +01:00
|
|
|
{
|
|
|
|
global $user;
|
2006-03-09 23:46:28 +01:00
|
|
|
|
2006-03-10 21:17:18 +01:00
|
|
|
if (($user_status == '') and isset($user['status']))
|
|
|
|
{
|
|
|
|
$user_status = $user['status'];
|
|
|
|
}
|
|
|
|
|
2006-03-09 23:46:28 +01:00
|
|
|
$access_type_status = ACCESS_NONE;
|
2006-03-10 21:17:18 +01:00
|
|
|
switch ($user_status)
|
2006-03-09 23:46:28 +01:00
|
|
|
{
|
2006-03-10 21:17:18 +01:00
|
|
|
case 'guest':
|
|
|
|
case 'generic':
|
2006-03-09 23:46:28 +01:00
|
|
|
{
|
2006-03-10 21:17:18 +01:00
|
|
|
$access_type_status = ACCESS_GUEST;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'normal':
|
|
|
|
{
|
|
|
|
$access_type_status = ACCESS_CLASSIC;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'admin':
|
|
|
|
{
|
|
|
|
$access_type_status = ACCESS_ADMINISTRATOR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'webmaster':
|
|
|
|
{
|
|
|
|
$access_type_status = ACCESS_WEBMASTER;
|
|
|
|
break;
|
2006-03-09 23:46:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-16 23:58:16 +01:00
|
|
|
return $access_type_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return if user have access to access_type definition
|
|
|
|
* Test does with user status
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function is_autorize_status($access_type, $user_status = '')
|
|
|
|
{
|
|
|
|
return (get_access_type_status($user_status) >= $access_type);
|
2006-03-09 23:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-03-16 23:58:16 +01:00
|
|
|
* Check if user have access to access_type definition
|
2006-03-09 23:46:28 +01:00
|
|
|
* Stop action if there are not access
|
|
|
|
* Test does with user status
|
|
|
|
* @return none
|
|
|
|
*/
|
2006-03-10 21:17:18 +01:00
|
|
|
function check_status($access_type, $user_status = '')
|
2006-03-09 23:46:28 +01:00
|
|
|
{
|
2006-03-10 21:17:18 +01:00
|
|
|
if (!is_autorize_status($access_type, $user_status))
|
2006-03-09 23:46:28 +01:00
|
|
|
{
|
2006-03-30 02:37:07 +02:00
|
|
|
access_denied();
|
2006-03-09 23:46:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-03-16 23:58:16 +01:00
|
|
|
* Return if user is an administrator
|
2006-03-09 23:46:28 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2006-03-10 21:17:18 +01:00
|
|
|
function is_admin($user_status = '')
|
2006-03-09 23:46:28 +01:00
|
|
|
{
|
2006-03-10 21:17:18 +01:00
|
|
|
return is_autorize_status(ACCESS_ADMINISTRATOR, $user_status);
|
2006-03-09 00:14:53 +01:00
|
|
|
}
|
|
|
|
|
2006-03-16 23:58:16 +01:00
|
|
|
/*
|
|
|
|
* Return if current user is an adviser
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function is_adviser()
|
|
|
|
{
|
|
|
|
global $user;
|
|
|
|
|
|
|
|
return ($user['adviser'] == 'true');
|
|
|
|
}
|
2006-07-11 22:51:24 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return mail address as display text
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function get_email_address_as_display_text($email_address)
|
|
|
|
{
|
|
|
|
global $conf;
|
|
|
|
|
2006-07-13 22:58:01 +02:00
|
|
|
if (!isset($email_address) or (trim($email_address) == ''))
|
2006-07-11 22:51:24 +02:00
|
|
|
{
|
2006-07-13 22:58:01 +02:00
|
|
|
return '';
|
2006-07-11 22:51:24 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-07-13 22:58:01 +02:00
|
|
|
if (is_adviser())
|
|
|
|
{
|
|
|
|
return 'adviser.mode@'.$_SERVER['SERVER_NAME'];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return $email_address;
|
|
|
|
}
|
2006-07-11 22:51:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-31 23:44:46 +02:00
|
|
|
?>
|