Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix quota check on category #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions add_photos.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
// +-----------------------------------------------------------------------+

// has the user reached its limits?
$user['community_usage'] = community_get_user_limits($user['id']);
//$user['community_usage'] = community_get_user_limits($user['id']);
// echo '<pre>'; print_r($user['community_usage']); echo '</pre>';

// +-----------------------------------------------------------------------+
Expand All @@ -98,6 +98,16 @@
$images = array_from_query($query);

$nb_images_deleted = 0;
if (is_admin())
{
$user_permissions['storage'] = -1;
$user_permissions['nb_photos'] = -1;
} else {
$upload_limit = community_get_upload_limit($user['id'], $_POST['category']);
$user_permissions['storage'] = $upload_limit['storage'];
$user_permissions['nb_photos'] = $upload_limit['nb_photos'];
$user['community_usage'] = community_get_user_limits($user['id'], $_POST['category']);
}

// upload has just happened, maybe the user is over quota
if ($user_permissions['storage'] > 0 and $user['community_usage']['storage'] > $user_permissions['storage'])
Expand All @@ -119,7 +129,7 @@
}
}

$user['community_usage'] = community_get_user_limits($user['id']);
$user['community_usage'] = community_get_user_limits($user['id'], $_POST['category']);

if ($user['community_usage']['storage'] <= $user_permissions['storage'])
{
Expand Down Expand Up @@ -148,7 +158,7 @@
}
}

$user['community_usage'] = community_get_user_limits($user['id']);
$user['community_usage'] = community_get_user_limits($user['id'], $_POST['category']);

if ($user['community_usage']['nb_photos'] <= $user_permissions['nb_photos'])
{
Expand Down
45 changes: 41 additions & 4 deletions include/functions_community.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,19 +383,56 @@ function community_get_cache_key()
}
}

function community_get_user_limits($user_id)
function community_get_user_limits($user_id, $category_id)
{
// how many photos and storage for this user?
$query = '
SELECT
COUNT(id) AS nb_photos,
IFNULL(FLOOR(SUM(filesize)/1024), 0) AS storage
FROM '.IMAGES_TABLE.'
COUNT(it.id) AS nb_photos,
IFNULL(FLOOR(SUM(it.filesize)/1024), 0) AS storage
FROM '.IMAGES_TABLE.' AS it
INNER JOIN ' . IMAGE_CATEGORY_TABLE . ' AS ic ON ic.image_id = it.id
WHERE added_by = '.$user_id.'
AND ic.category_id = '. $category_id .'
;';
return pwg_db_fetch_assoc(pwg_query($query));
}

function community_get_upload_limit($user_id, $category_id)
{
global $conf;

$query = '
SELECT
group_id
FROM '.USER_GROUP_TABLE.'
WHERE user_id = '.$user_id.'
;';
$user_group_ids = array_from_query($query, 'group_id');
$query = '
SELECT
nb_photos,
storage
FROM '.COMMUNITY_PERMISSIONS_TABLE.'
WHERE ((type = \'any_visitor\')';
if ($user_id != $conf['guest_id'])
{
$query.= '
OR (type = \'any_registered_user\')
OR (type = \'user\' AND user_id = '.$user_id.')';
if (count($user_group_ids) > 0)
{
$query.= '
OR (type = \'group\' AND group_id IN ('.implode(',', $user_group_ids).'))';
}
}
$query.= ')
AND (category_id = ' . $category_id . ')
;';
$recursive_categories = array();
return pwg_db_fetch_assoc(pwg_query($query));
}

// will be included in Piwigo 2.6
if (!function_exists('safe_version_compare'))
{
Expand Down