You are here

function shurly_rate_limit_allowed in ShURLy 6

Same name and namespace in other branches
  1. 8 shurly.module \shurly_rate_limit_allowed()
  2. 7 shurly.module \shurly_rate_limit_allowed()

Check rate limit for this user return an array in the following format array( 'allowed' => TRUE/FALSE 'rate' => number of requests allowed 'time' => period of time in minutes )

3 calls to shurly_rate_limit_allowed()
shurly_create_form_validate in ./shurly.module
shurly_expand in ./shurly.module
Function to get the long url.
shurly_shorten in ./shurly.module
API function to shorten a URL @arg $long_url - the long URL to shorten @arg $custom - optional custom short URL, user_access('enter custom URLs') should be checked prior to calling shurly_shorten()

File

./shurly.module, line 480
description http://www.youtube.com/watch?v=Qo7qoonzTCE

Code

function shurly_rate_limit_allowed($account = NULL) {
  if (!isset($account)) {
    global $user;
    $account = $user;
  }
  $settings = variable_get('shurly_throttle', array());
  if (!empty($settings) && is_array($account->roles)) {
    $rids = array_keys($account->roles);
    $use_rid = array_shift($rids);

    // get list of roles with permission to create short URLs
    $creating_roles = user_roles(FALSE, 'Create short URLs');
    foreach ($account->roles as $rid => $name) {

      // check that this role has permission to create URLs, otherwise discard it
      if (array_key_exists($rid, $creating_roles)) {

        // find the lightest role... if roles are the same weight, use the next role
        $settings[$rid]['weight'] = isset($settings[$rid]['weight']) ? $settings[$rid]['weight'] : 0;
        $use_rid = $settings[$use_rid]['weight'] < $settings[$rid]['weight'] ? $use_rid : $rid;
      }
    }
  }
  if (!empty($settings) && is_numeric($settings[$use_rid]['rate']) && is_numeric($settings[$use_rid]['time'])) {

    // see if it's allowed
    $allowed = shurly_flood_is_allowed('shurly', $settings[$use_rid]['rate'], $settings[$use_rid]['time'] * 60);

    // increment the counter
    shurly_flood_register_event('shurly', $settings[$use_rid]['time'] * 60);
    $return = array(
      'allowed' => $allowed,
      'rate' => $settings[$use_rid]['rate'],
      'time' => $settings[$use_rid]['time'],
    );
  }
  else {

    // not set... don't do a flood check
    $return = array(
      'allowed' => TRUE,
    );
  }
  return $return;
}