*
* @param string $permission The permission to check.
* @param mixed $allowed The allowed permissions.
- * @param array $opts Additional options ('value').
+ * @param array $opts Additional options:
+ * - For 'max_recipients' and 'max_timelimit', 'value' is the number of
+ * recipients in the current message.
*
- * @return mixed The value of the specified permission.
+ * @return boolean Does the user has permission?
*/
public function hasPermission($permission, $allowed, $opts = array())
{
switch ($permission) {
case 'create_folders':
- $allowed = (bool)count(array_filter($allowed));
+ // No-op
break;
case 'max_folders':
- $allowed = max($allowed);
if (empty($opts['value'])) {
- return ($allowed > count($GLOBALS['injector']->getInstance('IMP_Folder')->flist_IMP(array(), false)));
+ return ($allowed >= count($GLOBALS['injector']->getInstance('IMP_Folder')->flist_IMP(array(), false)));
}
break;
case 'max_recipients':
+ if (isset($opts['value'])) {
+ return ($allowed >= $opts['value']);
+ }
+ break;
+
case 'max_timelimit':
- $allowed = max($allowed);
+ if (isset($opts['value'])) {
+ $sentmail = $GLOBALS['injector']->getInstance('IMP_Sentmail');
+ if (!($sentmail instanceof IMP_Sentmail_Base)) {
+ Horde::logMessage('The permission for the maximum number of recipients per time period has been enabled, but no backend for the sent-mail logging has been configured for IMP.', 'ERR');
+ return true;
+ }
+
+ try {
+ $opts['value'] += $sentmail->numberOfRecipients($GLOBALS['conf']['sentmail']['params']['limit_period'], true);
+ } catch (IMP_Exception $e) {}
+
+ return ($allowed >= $opts['value']);
+ }
break;
}
- return $allowed;
+ return (bool)$allowed;
}
/* Menu methods. */
protected function _prepSendMessage($email, $headers = null,
$message = null)
{
- $timelimit = $GLOBALS['injector']->getInstance('Horde_Perms')->hasAppPermission('max_timelimit');
- if ($timelimit !== true) {
- $sentmail = $GLOBALS['injector']->getInstance('IMP_Sentmail');
- if (!($sentmail instanceof IMP_Sentmail_Base)) {
- Horde::logMessage('The permission for the maximum number of recipients per time period has been enabled, but no backend for the sent-mail logging has been configured for IMP.', 'ERR');
- throw new IMP_Compose_Exception(_("The system is not properly configured. A detailed error description has been logged for the administrator."));
- }
-
- try {
- $recipients = $sentmail->numberOfRecipients($GLOBALS['conf']['sentmail']['params']['limit_period'], true);
- } catch (IMP_Exception $e) {
- $recipients = 0;
- }
+ foreach (Horde_Mime_Address::parseAddressList($email) as $address) {
+ $recipients += isset($address['grounpname'])
+ ? count($address['addresses'])
+ : 1;
+ }
- foreach (Horde_Mime_Address::parseAddressList($email) as $address) {
- $recipients += isset($address['grounpname'])
- ? count($address['addresses'])
- : 1;
- }
+ $perms = $GLOBALS['injector']->getInstance('Horde_Perms');
- if ($recipients > $timelimit) {
- Horde::permissionDeniedError('imp', 'max_timelimit');
- throw new IMP_Compose_Exception(sprintf(_("You are not allowed to send messages to more than %d recipients within %d hours."), $timelimit, $GLOBALS['conf']['sentmail']['params']['limit_period']));
- }
+ if (!$perms->hasAppPermission('max_timelimit', array('opts' => array('value' => $recipients)))) {
+ Horde::permissionDeniedError('imp', 'max_timelimit');
+ throw new IMP_Compose_Exception(sprintf(_("You are not allowed to send messages to more than %d recipients within %d hours."), $perms->getPermissions('max_timelimit'), $GLOBALS['conf']['sentmail']['params']['limit_period']));
}
/* Pass to hook to allow alteration of message details. */
/* Count recipients if necessary. We need to split email groups
* because the group members count as separate recipients. */
if ($exceed) {
- $max_recipients = $GLOBALS['injector']->getInstance('Horde_Perms')->hasAppPermission('max_recipients');
- if ($max_recipients !== true) {
- $num_recipients = 0;
- foreach ($addrlist as $recipient) {
- $num_recipients += count(explode(',', $recipient));
- }
- if ($num_recipients > $max_recipients) {
- Horde::permissionDeniedError('imp', 'max_recipients');
- throw new IMP_Compose_Exception(sprintf(_("You are not allowed to send messages to more than %d recipients."), $max_recipients));
- }
+ $recipients = 0;
+ foreach ($addrlist as $recipient) {
+ $recipients += count(explode(',', $recipient));
+ }
+
+ $perms = $GLOBALS['injector']->getInstance('Horde_Perms');
+
+ if (!$perms->hasAppPermission('max_recipients', array('opts' => array('value' => $recipients)))) {
+ Horde::permissionDeniedError('imp', 'max_recipients');
+ throw new IMP_Compose_Exception(sprintf(_("You are not allowed to send messages to more than %d recipients."), $perms->getPermissions('max_recipients')));
}
}