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 |
|
2005-01-08 00:10:51 +01:00
|
|
|
// | Copyright (C) 2003-2005 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)
|
2004-02-12 00:20:38 +01:00
|
|
|
// | file : $RCSfile$
|
|
|
|
// | 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-28 18:56:33 +01:00
|
|
|
function register_user($login, $password, $password_conf,
|
|
|
|
$mail_address, $status = 'guest')
|
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();
|
2003-05-13 12:02:06 +02:00
|
|
|
// login must not
|
|
|
|
// 1. be empty
|
|
|
|
// 2. start ou end with space character
|
|
|
|
// 4. be already used
|
2004-12-28 18:56:33 +01:00
|
|
|
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']);
|
|
|
|
}
|
2004-02-02 01:55:18 +01:00
|
|
|
|
2005-07-17 17:06:39 +02:00
|
|
|
$query = '
|
2004-12-28 18:56:33 +01:00
|
|
|
SELECT id
|
|
|
|
FROM '.USERS_TABLE.'
|
2005-07-17 17:06:39 +02:00
|
|
|
WHERE username = \''.mysql_escape_string($login).'\'
|
2004-12-28 18:56:33 +01:00
|
|
|
;';
|
2005-07-17 17:06:39 +02:00
|
|
|
$result = pwg_query($query);
|
|
|
|
if (mysql_num_rows($result) > 0)
|
|
|
|
{
|
|
|
|
array_push($errors, $lang['reg_err_login5']);
|
2003-05-09 14:42:42 +02:00
|
|
|
}
|
2005-07-17 17:06:39 +02:00
|
|
|
|
2003-05-13 12:02:06 +02:00
|
|
|
// given password must be the same as the confirmation
|
2004-12-28 18:56:33 +01:00
|
|
|
if ($password != $password_conf)
|
|
|
|
{
|
|
|
|
array_push($errors, $lang['reg_err_pass']);
|
|
|
|
}
|
2003-05-09 14:42:42 +02:00
|
|
|
|
2004-12-28 18:56:33 +01:00
|
|
|
$error_mail_address = validate_mail_address($mail_address);
|
|
|
|
if ($error_mail_address != '')
|
|
|
|
{
|
|
|
|
array_push($errors, $error_mail_address);
|
|
|
|
}
|
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
|
|
|
{
|
2004-12-28 18:56:33 +01:00
|
|
|
$insert = array();
|
2005-07-17 17:06:39 +02:00
|
|
|
$insert['username'] = mysql_escape_string($login);
|
2004-12-28 18:56:33 +01:00
|
|
|
$insert['password'] = md5($password);
|
|
|
|
$insert['status'] = $status;
|
|
|
|
$insert['template'] = $conf['default_template'];
|
|
|
|
$insert['nb_image_line'] = $conf['nb_image_line'];
|
|
|
|
$insert['nb_line_page'] = $conf['nb_line_page'];
|
|
|
|
$insert['language'] = $conf['default_language'];
|
|
|
|
$insert['recent_period'] = $conf['recent_period'];
|
2005-07-16 16:29:35 +02:00
|
|
|
$insert['feed_id'] = find_available_feed_id();
|
2004-12-28 18:56:33 +01:00
|
|
|
$insert['expand'] = boolean_to_string($conf['auto_expand']);
|
|
|
|
$insert['show_nb_comments'] = boolean_to_string($conf['show_nb_comments']);
|
|
|
|
if ( $mail_address != '' )
|
|
|
|
{
|
|
|
|
$insert['mail_address'] = $mail_address;
|
|
|
|
}
|
|
|
|
if ($conf['default_maxwidth'] != '')
|
2003-05-09 14:42:42 +02:00
|
|
|
{
|
2004-12-28 18:56:33 +01:00
|
|
|
$insert['maxwidth'] = $conf['default_maxwidth'];
|
2003-05-09 14:42:42 +02:00
|
|
|
}
|
2004-12-28 18:56:33 +01:00
|
|
|
if ($conf['default_maxheight'] != '')
|
2003-05-09 14:42:42 +02:00
|
|
|
{
|
2004-12-28 18:56:33 +01:00
|
|
|
$insert['maxheight'] = $conf['default_maxheight'];
|
2003-05-09 14:42:42 +02:00
|
|
|
}
|
2004-12-28 18:56:33 +01:00
|
|
|
|
|
|
|
$query = '
|
|
|
|
INSERT INTO '.USERS_TABLE.'
|
|
|
|
('.implode(',', array_keys($insert)).')
|
|
|
|
VALUES
|
|
|
|
(';
|
|
|
|
$is_first = true;
|
|
|
|
foreach (array_keys($insert) as $field)
|
|
|
|
{
|
|
|
|
if (!$is_first)
|
|
|
|
{
|
|
|
|
$query.= ',';
|
|
|
|
}
|
|
|
|
$query.= "'".$insert[$field]."'";
|
|
|
|
$is_first = false;
|
2003-05-09 14:42:42 +02:00
|
|
|
}
|
2004-12-28 18:56:33 +01:00
|
|
|
$query.= ')
|
2005-07-16 16:29:35 +02:00
|
|
|
;';
|
|
|
|
pwg_query($query);
|
|
|
|
|
|
|
|
$query = '
|
|
|
|
UPDATE '.USERS_TABLE.'
|
|
|
|
SET registration_date = NOW()
|
|
|
|
WHERE id = '.mysql_insert_id().'
|
2004-12-28 18:56:33 +01:00
|
|
|
;';
|
|
|
|
pwg_query($query);
|
2003-05-09 14:42:42 +02:00
|
|
|
}
|
2004-12-28 18:56:33 +01:00
|
|
|
return $errors;
|
2003-05-09 14:42:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function update_user( $user_id, $mail_address, $status,
|
|
|
|
$use_new_password = false, $password = '' )
|
|
|
|
{
|
|
|
|
$error = array();
|
|
|
|
$i = 0;
|
|
|
|
|
|
|
|
$error_mail_address = validate_mail_address( $mail_address );
|
|
|
|
if ( $error_mail_address != '' )
|
|
|
|
{
|
|
|
|
$error[$i++] = $error_mail_address;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( sizeof( $error ) == 0 )
|
|
|
|
{
|
2004-02-19 01:31:09 +01:00
|
|
|
$query = 'UPDATE '.USERS_TABLE;
|
2003-07-01 11:27:20 +02:00
|
|
|
$query.= " SET status = '".$status."'";
|
2003-05-09 14:42:42 +02:00
|
|
|
if ( $use_new_password )
|
|
|
|
{
|
|
|
|
$query.= ", password = '".md5( $password )."'";
|
|
|
|
}
|
|
|
|
$query.= ', mail_address = ';
|
|
|
|
if ( $mail_address != '' )
|
|
|
|
{
|
|
|
|
$query.= "'".$mail_address."'";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$query.= 'NULL';
|
|
|
|
}
|
2003-07-01 11:27:20 +02:00
|
|
|
$query.= ' WHERE id = '.$user_id;
|
2003-05-09 14:42:42 +02:00
|
|
|
$query.= ';';
|
2004-10-30 17:42:29 +02:00
|
|
|
pwg_query( $query );
|
2003-05-09 14:42:42 +02:00
|
|
|
}
|
|
|
|
return $error;
|
|
|
|
}
|
|
|
|
|
2004-12-03 17:30:12 +01:00
|
|
|
function check_login_authorization($guest_allowed = true)
|
2003-05-09 14:42:42 +02:00
|
|
|
{
|
2004-12-21 00:54:59 +01:00
|
|
|
global $user,$lang,$conf,$template;
|
2003-05-18 23:42:32 +02:00
|
|
|
|
2004-12-20 23:03:04 +01:00
|
|
|
if ($user['is_the_guest'] and !$guest_allowed)
|
2003-05-09 14:42:42 +02:00
|
|
|
{
|
|
|
|
echo '<div style="text-align:center;">'.$lang['only_members'].'<br />';
|
|
|
|
echo '<a href="./identification.php">'.$lang['ident_title'].'</a></div>';
|
|
|
|
exit();
|
|
|
|
}
|
2004-12-21 00:54:59 +01:00
|
|
|
|
|
|
|
if ($conf['gallery_locked'])
|
|
|
|
{
|
|
|
|
echo '<div style="text-align:center;">';
|
|
|
|
echo $lang['gallery_locked_message'];
|
|
|
|
echo '</div>';
|
|
|
|
if ($user['status'] != 'admin')
|
|
|
|
{
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2004-03-20 01:52:37 +01:00
|
|
|
function getuserdata($user)
|
|
|
|
{
|
|
|
|
$sql = "SELECT * FROM " . USERS_TABLE;
|
|
|
|
$sql.= " WHERE ";
|
|
|
|
$sql .= ( ( is_integer($user) ) ? "id = $user" : "username = '" . str_replace("\'", "''", $user) . "'" ) . " AND id <> " . ANONYMOUS;
|
2004-10-30 17:42:29 +02:00
|
|
|
$result = pwg_query($sql);
|
2004-03-20 01:52:37 +01:00
|
|
|
return ( $row = mysql_fetch_array($result) ) ? $row : false;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = '
|
|
|
|
SELECT f.image_id
|
|
|
|
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'].'
|
|
|
|
AND ic.category_id IN ('.$user['forbidden_categories'].')
|
|
|
|
;';
|
|
|
|
$result = pwg_query($query);
|
|
|
|
$elements = array();
|
|
|
|
while ($row = mysql_fetch_array($result))
|
|
|
|
{
|
|
|
|
array_push($elements, $row['image_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($elements) > 0)
|
|
|
|
{
|
|
|
|
$query = '
|
|
|
|
DELETE FROM '.FAVORITES_TABLE.'
|
|
|
|
WHERE image_id IN ('.implode(',', $elements).')
|
|
|
|
AND user_id = '.$user['id'].'
|
|
|
|
;';
|
|
|
|
pwg_query($query);
|
|
|
|
}
|
|
|
|
}
|
2004-12-20 13:30:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* update table user_forbidden for the given user
|
|
|
|
*
|
|
|
|
* table user_forbidden contains calculated data. Calculation is based on
|
|
|
|
* private categories minus categories authorized to the groups the user
|
|
|
|
* belongs to minus the categories directly authorized to the user
|
|
|
|
*
|
|
|
|
* @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
|
|
|
{
|
|
|
|
$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
|
|
|
|
|
|
|
// if user is not an admin, locked categories can be considered as private$
|
|
|
|
if ($user_status != 'admin')
|
|
|
|
{
|
|
|
|
$query = '
|
|
|
|
SELECT id
|
|
|
|
FROM '.CATEGORIES_TABLE.'
|
|
|
|
WHERE visible = \'false\'
|
|
|
|
;';
|
|
|
|
$result = pwg_query($query);
|
|
|
|
while ($row = mysql_fetch_array($result))
|
|
|
|
{
|
|
|
|
array_push($private_array, $row['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$private_array = array_unique($private_array);
|
|
|
|
}
|
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.'
|
|
|
|
;';
|
|
|
|
$result = pwg_query($query);
|
|
|
|
while ($row = mysql_fetch_array($result))
|
|
|
|
{
|
|
|
|
array_push($authorized_array, $row['cat_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.'
|
|
|
|
;';
|
|
|
|
$result = pwg_query($query);
|
|
|
|
while ($row = mysql_fetch_array($result))
|
|
|
|
{
|
|
|
|
array_push($authorized_array, $row['cat_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
$query = '
|
|
|
|
DELETE FROM '.USER_FORBIDDEN_TABLE.'
|
|
|
|
WHERE user_id = '.$user_id.'
|
|
|
|
;';
|
|
|
|
pwg_query($query);
|
|
|
|
|
|
|
|
$forbidden_categories = implode(',', $forbidden_array);
|
|
|
|
|
|
|
|
$query = '
|
|
|
|
INSERT INTO '.USER_FORBIDDEN_TABLE.'
|
|
|
|
(user_id,need_update,forbidden_categories)
|
|
|
|
VALUES
|
|
|
|
('.$user_id.',\'false\',\''.$forbidden_categories.'\')
|
|
|
|
;';
|
|
|
|
pwg_query($query);
|
|
|
|
|
|
|
|
return $forbidden_categories;
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
$query = '
|
|
|
|
SELECT username
|
|
|
|
FROM '.USERS_TABLE.'
|
|
|
|
WHERE id = '.intval($user_id).'
|
|
|
|
;';
|
|
|
|
$result = pwg_query($query);
|
|
|
|
if (mysql_num_rows($result) > 0)
|
|
|
|
{
|
|
|
|
list($username) = mysql_fetch_row($result);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $username;
|
|
|
|
}
|
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(*)
|
|
|
|
FROM '.USERS_TABLE.'
|
|
|
|
WHERE feed_id = \''.$key.'\'
|
|
|
|
;';
|
|
|
|
list($count) = mysql_fetch_row(pwg_query($query));
|
|
|
|
if (0 == $count)
|
|
|
|
{
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-02-20 20:07:43 +01:00
|
|
|
?>
|