0byt3m1n1
Path:
/
data
/
applications
/
aps
/
gallery
/
2.2-08
/
htdocs
/
modules
/
quotas
/
classes
/
[
Home
]
File: GalleryQuotasHelper.class
<?php /* * Gallery - a web based photo album viewer and editor * Copyright (C) 2000-2007 Bharat Mediratta * * 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; either version 2 of the License, or (at * your option) any later version. * * 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ /** * Utility functions useful in managing Quotas * @package Quotas * @subpackage Classes * @author Robert Balousek <volksport@users.sf.net> * @version $Revision: 15513 $ * @static */ class GalleryQuotasHelper { /** * Get the total size of all the user's current files, excluding the files in the * excludeIds param array. * * @return array object GalleryStatus a status code * integer user's disk usage in bytes */ function getUserDiskUsage($userId, $excludeIds=array()) { global $gallery; $storage =& $gallery->getStorage(); $query = ' SELECT SUM([GalleryDataItem::size]/1024) FROM [GalleryItem], [GalleryDataItem] WHERE [GalleryItem::id] = [GalleryDataItem::id] AND [GalleryItem::ownerId] = ? '; $data = array((int)$userId); if (!empty($excludeIds)) { $data = array_merge($data, $excludeIds); $itemIdMarkers = GalleryUtilities::makeMarkers(count($excludeIds)); $query .= ' AND [GalleryItem::id] NOT IN (' . $itemIdMarkers . ')'; } list ($ret, $searchResults) = $gallery->search($query, $data); if ($ret) { return array($ret, null); } /* Get our user's total disk usage */ $result = $searchResults->nextResult(); if (!empty($result[0])) { $userDiskUsage = (int)$result[0]; } else { $userDiskUsage = 0; } return array(null, $userDiskUsage); } /** * Get the user's quota if one exists. This is achieved by checking for a direct user quota, * if one does not exist, then quotas for groups the user belongs to is checked. * If the user belongs to one or more groups with quotas, the group with the highest quota * is used as their quota. * * @return array object GalleryStatus a status code * bool does a quota exists for this user * integer user's disk quota in Kilobytes */ function getUserDiskQuota($userId) { global $gallery; $userDiskQuota = null; list ($ret, $userQuotaExists, $userQuotaInfo) = GalleryQuotasHelper::fetchUserQuota($userId); if ($ret) { return array($ret, null, null); } if (!$userQuotaExists) { list ($ret, $groupQuotaExists, $groupQuotaInfo) = GalleryQuotasHelper::fetchHighestGroupQuota($userId); if ($ret) { return array($ret, null, null); } if ($groupQuotaExists) { $userDiskQuota = $groupQuotaInfo['quota']; } } else { $userDiskQuota = $userQuotaInfo['quota']; $groupQuotaExists = false; } return array(null, $userQuotaExists | $groupQuotaExists, $userDiskQuota); } /** * Get the users highest group quota, if they have multiple (or one), or return no * quota if they have no group quotas assigned. * * @return array object GalleryStatus a status code * boolean: does a quota exists for this user * array('quota' => integer user's disk quota in Kilobytes, * 'name' => string group's name) */ function fetchHighestGroupQuota($userId) { global $gallery; list ($ret, $groupQuotaIds) = GalleryCoreApi::fetchGroupsForUser($userId); if ($ret) { return array($ret, null, null); } $storage =& $gallery->getStorage(); $groupQuotaIds = array_keys($groupQuotaIds); $groupQuotaIdMarkers = GalleryUtilities::makeMarkers(count($groupQuotaIds)); $query = ' SELECT [GalleryQuotasMap::quotaSize], [GalleryGroup::groupName] FROM [GalleryQuotasMap], [GalleryGroup] WHERE [GalleryQuotasMap::userOrGroupId] IN (' . $groupQuotaIdMarkers . ') AND [GalleryQuotasMap::userOrGroupId] = [GalleryGroup::id] ORDER BY [GalleryQuotasMap::quotaSize] DESC '; $data = $groupQuotaIds; $options = array(); $options['limit'] = array('count' => 1); list ($ret, $searchResults) = $gallery->search($query, $data, $options); if ($ret) { return array($ret, null, null); } /* Get our group's quota size and name */ $result = $searchResults->nextResult(); if (!empty($result)) { $groupQuotaExists = true; $groupQuotaInfo = array('quota' => (int)$result[0], 'name' => $result[1]); } else { $groupQuotaExists = false; $groupQuotaInfo = array(); } return array(null, $groupQuotaExists, $groupQuotaInfo); } /** * Convert Kilobytes to a human readable format (ie: KB, MB, GB) * * @return array object GalleryStatus a status code * string user's quota in human readable format * string the unit of measurement for the user's quota (ie: KB, MB, GB) */ function humanReadableFromKilobytes($kilobytes) { global $gallery; list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'quotas'); if ($ret) { return array($ret, null, null); } $suffixes = array($gallery->i18n('KB'), $gallery->i18n('MB'), $gallery->i18n('GB')); $base = 1024; $useSuffix = 0; $kilobytes = (float)$kilobytes; while ($kilobytes >= $base && $useSuffix < count($suffixes)-1) { $kilobytes /= (float)$base; $useSuffix++; } $humanReadable = (string)round($kilobytes, 2); $unit = $module->translate($suffixes[$useSuffix]); return array(null, $humanReadable, $unit); } /** * Get list of groups who have quotas * * @return array object GalleryStatus a status code * array(groupId => array('quota' => group's disk quota, * 'name' => group's groupname)) */ function fetchQuotaGroupIdList($count, $offset=0) { global $gallery; $storage =& $gallery->getStorage(); $query = ' SELECT [GalleryQuotasMap::userOrGroupId], [GalleryQuotasMap::quotaSize], [GalleryGroup::groupName] FROM [GalleryQuotasMap], [GalleryGroup] WHERE [GalleryQuotasMap::userOrGroupId] = [GalleryGroup::id] ORDER BY [GalleryQuotasMap::userOrGroupId] ASC '; $data = $options = array(); $options['limit'] = array('count' => (int)$count, 'offset' => (int)$offset); list ($ret, $searchResults) = $gallery->search($query, $data, $options); if ($ret) { return array($ret, null); } /* Get our groups' id, quota size, and name */ $groupQuotasList = array(); while ($result = $searchResults->nextResult()) { $groupQuotasList[(int)$result[0]] = array('quota' => (int)$result[1], 'name' => $result[2]); } return array(null, $groupQuotasList); } /** * Get the users highest group quota, if they have multiple (or one), or return no * quota if they have no group quotas assigned. * * @return array object GalleryStatus a status code * integer the number of groups that have quotas assigned */ function fetchQuotaGroupCount() { global $gallery; $storage =& $gallery->getStorage(); $query = ' SELECT COUNT([GalleryQuotasMap::userOrGroupId]) FROM [GalleryQuotasMap], [GalleryGroup] WHERE [GalleryQuotasMap::userOrGroupId] = [GalleryGroup::id] '; list ($ret, $searchResults) = $gallery->search($query); if ($ret) { return array($ret, null); } /* Get the number of groups that have quotas assigned */ $result = $searchResults->nextResult(); $groupCount = (int)$result[0]; return array(null, $groupCount); } /** * Get list of user's who have quotas * * @return array object GalleryStatus a status code * array(groupId => array('quota' => user's disk quota, * 'name' => user's username)) */ function fetchQuotaUserIdList($count, $offset=0) { global $gallery; $storage =& $gallery->getStorage(); $query = ' SELECT [GalleryQuotasMap::userOrGroupId], [GalleryQuotasMap::quotaSize], [GalleryUser::userName] FROM [GalleryQuotasMap], [GalleryUser] WHERE [GalleryQuotasMap::userOrGroupId] = [GalleryUser::id] ORDER BY [GalleryQuotasMap::userOrGroupId] ASC '; $data = $options = array(); $options['limit'] = array('count' => (int)$count, 'offset' => (int)$offset); list ($ret, $searchResults) = $gallery->search($query, $data, $options); if ($ret) { return array($ret, null); } /* Get our users' id, quota size, and name */ $userQuotasList = array(); while ($result = $searchResults->nextResult()) { $userQuotasList[(int)$result[0]] = array('quota' => (int)$result[1], 'name' => $result[2]); } return array(null, $userQuotasList); } /** * Get the number of users that have a quota assigned directly to them * * @return array object GalleryStatus a status code * integer the number of users that have quotas assigned */ function fetchQuotaUserCount() { global $gallery; $storage =& $gallery->getStorage(); $query = ' SELECT COUNT([GalleryQuotasMap::userOrGroupId]) FROM [GalleryQuotasMap], [GalleryUser] WHERE [GalleryQuotasMap::userOrGroupId] = [GalleryUser::id] '; list ($ret, $searchResults) = $gallery->search($query); if ($ret) { return array($ret, null); } /* Get the number of users that have quotas assigned */ $result = $searchResults->nextResult(); $userCount = (int)$result[0]; return array(null, $userCount); } /** * Get the quota assigned to a group * * @return array object GalleryStatus a status code * boolean does a quota exist * array('id' => group's groupId * 'quota' => group's disk quota, * 'name' => group's groupname) */ function fetchGroupQuota($groupId) { global $gallery; $storage =& $gallery->getStorage(); $query = ' SELECT [GalleryQuotasMap::userOrGroupId], [GalleryQuotasMap::quotaSize], [GalleryGroup::groupName] FROM [GalleryQuotasMap], [GalleryGroup] WHERE [GalleryQuotasMap::userOrGroupId] = ? AND [GalleryQuotasMap::userOrGroupId] = [GalleryGroup::id] '; $data = array((int)$groupId); list ($ret, $searchResults) = $gallery->search($query, $data); if ($ret) { return array($ret, null, null); } /* Get our group's quota size and name */ $result = $searchResults->nextResult(); if (!empty($result)) { $quotaExists = true; $groupQuotaInfo = array('id' => (int)$result[0], 'quota' => (int)$result[1], 'name' => $result[2]); } else { $quotaExists = false; $groupQuotaInfo = array(); } return array(null, $quotaExists, $groupQuotaInfo); } /** * Get the quota assigned to a user * * @return array object GalleryStatus a status code * boolean does a quota exist * array('id' => user's userId * 'quota' => user's disk quota, * 'name' => user's username) */ function fetchUserQuota($userId) { global $gallery; $storage =& $gallery->getStorage(); $query = ' SELECT [GalleryQuotasMap::userOrGroupId], [GalleryQuotasMap::quotaSize], [GalleryUser::userName] FROM [GalleryQuotasMap], [GalleryUser] WHERE [GalleryQuotasMap::userOrGroupId] = ? AND [GalleryQuotasMap::userOrGroupId] = [GalleryUser::id] '; $data = array((int)$userId); list ($ret, $searchResults) = $gallery->search($query, $data); if ($ret) { return array($ret, null, null); } /* Get our users' quota size and name */ $result = $searchResults->nextResult(); if (!empty($result)) { $quotaExists = true; $userQuotaInfo = array('id' => (int)$result[0], 'quota' => (int)$result[1], 'name' => $result[2]); } else { $quotaExists = false; $userQuotaInfo = array(); } return array(null, $quotaExists, $userQuotaInfo); } } ?>