You are here

function shurly_rate_limit_allowed in ShURLy 7

Same name and namespace in other branches
  1. 8 shurly.module \shurly_rate_limit_allowed()
  2. 6 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
Validation of the main form
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

File

./shurly.module, line 747
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 (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[$use_rid]['weight'] = isset($settings[$use_rid]['weight']) ? $settings[$use_rid]['weight'] : 0;
        $settings[$rid]['weight'] = isset($settings[$rid]['weight']) ? $settings[$rid]['weight'] : 0;
        $use_rid = $settings[$use_rid]['weight'] < $settings[$rid]['weight'] ? $use_rid : $rid;

        //Create array index if not exists for rate and time
        $settings[$use_rid]['rate'] = isset($settings[$use_rid]['rate']) ? $settings[$use_rid]['rate'] : NULL;
        $settings[$use_rid]['time'] = isset($settings[$use_rid]['time']) ? $settings[$use_rid]['time'] : NULL;
      }
    }
  }
  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;
}