From 5a8ecfbfb140333e08d85b1d4a7e97bbba78bfcc Mon Sep 17 00:00:00 2001 From: plegall Date: Mon, 20 Dec 2004 12:30:36 +0000 Subject: - in picture.php, $user['maxwidth'] and $user['maxheight'] can be unset if NULL in database - new table user_forbidden {user_id,need_update,forbidden_categories} and deletion of field users.forbidden_categories - new function calculate_permissions to update table user_forbidden when needed - simplification of include/user.inc.php - in footer of each page, use "-" instead of "::" to separate page information git-svn-id: http://piwigo.org/svn/trunk@648 68402e56-0260-453c-a942-63ccdbb3a9ee --- include/constants.php | 1 + include/functions_user.inc.php | 77 +++++++++++++++++++++++++++++++++++++ include/user.inc.php | 73 ++++++++++++++++++++--------------- install/dbscheme.txt | 6 ++- install/phpwebgallery_structure.sql | 13 ++++++- picture.php | 4 +- template/default/footer.tpl | 4 +- 7 files changed, 141 insertions(+), 37 deletions(-) diff --git a/include/constants.php b/include/constants.php index 64b55a099..b6010787e 100644 --- a/include/constants.php +++ b/include/constants.php @@ -57,4 +57,5 @@ define('USERS_TABLE', $table_prefix.'users'); define('WAITING_TABLE', $table_prefix.'waiting'); define('IMAGE_METADATA_TABLE', $table_prefix.'image_metadata'); define('RATE_TABLE', $table_prefix.'rate'); +define('USER_FORBIDDEN_TABLE', $table_prefix.'user_forbidden'); ?> diff --git a/include/functions_user.inc.php b/include/functions_user.inc.php index c00ba2f4a..474fa8154 100644 --- a/include/functions_user.inc.php +++ b/include/functions_user.inc.php @@ -276,4 +276,81 @@ DELETE FROM '.FAVORITES_TABLE.' pwg_query($query); } } + +/** + * 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 + * @return string forbidden_categories + */ +function calculate_permissions($user_id) +{ + $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']); + } + + // 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; +} ?> diff --git a/include/user.inc.php b/include/user.inc.php index eb5540f8b..4c1561071 100644 --- a/include/user.inc.php +++ b/include/user.inc.php @@ -25,19 +25,7 @@ // | USA. | // +-----------------------------------------------------------------------+ -// retrieving user informations -// $infos array is used to know the fields to retrieve in the table "users" -// Each field becomes an information of the array $user. -// Example : -// status --> $user['status'] -$infos = array('id','username','mail_address','nb_image_line','nb_line_page', - 'status','language','maxwidth','maxheight','expand', - 'show_nb_comments','recent_period','template', - 'forbidden_categories'); - -$query_user = 'SELECT * FROM '.USERS_TABLE; -$query_done = false; -$user['is_the_guest'] = false; +// retrieving connected user informations if (isset($_COOKIE['id'])) { @@ -79,37 +67,59 @@ DELETE FROM '.SESSIONS_TABLE.' } else { - $query_user .= ' WHERE id = '.$row['user_id']; - $query_done = true; + $user['id'] = $row['user_id']; + $user['is_the_guest'] = false; } } } -if (!$query_done) +if (!isset($user['id'])) { - $query_user .= ' WHERE id = 2'; + $user['id'] = 2; $user['is_the_guest'] = true; } -$query_user .= ';'; -$row = mysql_fetch_array(pwg_query($query_user)); -// affectation of each value retrieved in the users table into a variable -// of the array $user. -foreach ($infos as $info) { - if (isset($row[$info])) +$query = ' +SELECT u.*, uf.* + FROM '.USERS_TABLE.' AS u LEFT JOIN '.USER_FORBIDDEN_TABLE.' AS uf + ON id = user_id + WHERE u.id = '.$user['id'].' +;'; +$row = mysql_fetch_array(pwg_query($query)); + +// affectation of each value retrieved in the users table into a variable of +// the array $user. +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 ($row[$info] == 'true' or $row[$info] == 'false') - $user[$info] = get_boolean($row[$info]); + if ($value == 'true' or $value == 'false') + { + $user[$key] = get_boolean($value); + } else - $user[$info] = $row[$info]; - } - else - { - $user[$info] = ''; + { + $user[$key] = $value; + } } } +// if no information were found about user in user_forbidden table OR the +// forbidden categories must be updated +if (!isset($user['need_update']) + or !is_bool($user['need_update']) + or $user['need_update'] == true) +{ + $user['forbidden_categories'] = calculate_permissions($user['id']); +} + +// forbidden_categories is a must be empty, at least +if (!isset($user['forbidden_categories'])) +{ + $user['forbidden_categories'] = ''; +} + // special for $user['restrictions'] array $user['restrictions'] = explode(',', $user['forbidden_categories']); if ($user['restrictions'][0] == '') @@ -120,9 +130,10 @@ if ($user['restrictions'][0] == '') $isadmin = false; if ($user['status'] == 'admin') { - $isadmin =true; + $isadmin = true; } // calculation of the number of picture to display per page $user['nb_image_page'] = $user['nb_image_line'] * $user['nb_line_page']; + init_userprefs($user); ?> diff --git a/install/dbscheme.txt b/install/dbscheme.txt index 8caeb48e6..d53e742c4 100644 --- a/install/dbscheme.txt +++ b/install/dbscheme.txt @@ -12,6 +12,7 @@ table:rate table:sessions table:sites table:user_access +table:user_forbidden table:user_group table:users table:waiting @@ -83,6 +84,9 @@ column:id table:sites type:tinyint column:galleries_url table:sites type:varchar nullable:N length:255 binary:N column:user_id table:user_access type:smallint nullable:N length:5 signed:N column:cat_id table:user_access type:smallint nullable:N length:5 signed:N +column:user_id table:user_forbidden type:smallint nullable:N length:5 signed:N +column:need_update table:user_forbidden type:enum('true','false') nullable:N +column:forbidden_categories table:user_forbidden type:text nullable:Y column:user_id table:user_group type:smallint nullable:N length:5 signed:N column:group_id table:user_group type:smallint nullable:N length:5 signed:N column:id table:users type:smallint nullable:N length:5 signed:N @@ -99,7 +103,6 @@ column:expand table:users type:enum('true','false') column:show_nb_comments table:users type:enum('true','false') nullable:N column:recent_period table:users type:tinyint nullable:N length:3 signed:N column:template table:users type:varchar nullable:N length:255 binary:N -column:forbidden_categories table:users type:text nullable:Y column:id table:waiting type:int nullable:N length:10 signed:N column:storage_category_id table:waiting type:smallint nullable:N length:5 signed:N column:file table:waiting type:varchar nullable:N length:255 binary:N @@ -127,6 +130,7 @@ PK:sessions_pk table:sessions column:id PK:sites_pk table:sites column:id PK:user_access_pk table:user_access column:user_id PK:user_access_pk table:user_access column:cat_id +PK:user_forbidden_pk table:user_forbidden column:user_id PK:user_group_pk table:user_group column:group_id PK:user_group_pk table:user_group column:user_id PK:users_pk table:users column:id diff --git a/install/phpwebgallery_structure.sql b/install/phpwebgallery_structure.sql index 4bf05af9c..45cfc8176 100644 --- a/install/phpwebgallery_structure.sql +++ b/install/phpwebgallery_structure.sql @@ -197,6 +197,18 @@ CREATE TABLE phpwebgallery_user_access ( PRIMARY KEY (user_id,cat_id) ) TYPE=MyISAM; +-- +-- Table structure for table 'phpwebgallery_user_forbidden' +-- + +DROP TABLE IF EXISTS phpwebgallery_user_forbidden; +CREATE TABLE phpwebgallery_user_forbidden ( + user_id smallint(5) unsigned NOT NULL default '0', + need_update enum('true','false') NOT NULL default 'true', + forbidden_categories text, + PRIMARY KEY (user_id) +) TYPE=MyISAM; + -- -- Table structure for table 'phpwebgallery_user_group' -- @@ -228,7 +240,6 @@ CREATE TABLE phpwebgallery_users ( show_nb_comments enum('true','false') NOT NULL default 'false', recent_period tinyint(3) unsigned NOT NULL default '7', template varchar(255) NOT NULL default 'default', - forbidden_categories text, PRIMARY KEY (id), UNIQUE KEY users_ui1 (username) ) TYPE=MyISAM; diff --git a/picture.php b/picture.php index a85f55b64..6e4a73709 100644 --- a/picture.php +++ b/picture.php @@ -416,8 +416,8 @@ else $original_height = $picture['current']['height']; } -$picture_size = get_picture_size( $original_width, $original_height, - $user['maxwidth'], $user['maxheight'] ); +$picture_size = get_picture_size($original_width, $original_height, + @$user['maxwidth'], @$user['maxheight']); // metadata if ($conf['show_exif'] or $conf['show_iptc']) diff --git a/template/default/footer.tpl b/template/default/footer.tpl index 2dfa4db57..190f3f2b2 100644 --- a/template/default/footer.tpl +++ b/template/default/footer.tpl @@ -1,6 +1,6 @@