aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--include/constants.php1
-rw-r--r--include/functions_user.inc.php77
-rw-r--r--include/user.inc.php73
3 files changed, 120 insertions, 31 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);
?>