You are here

function simplenews_statistics_encoder_decode in Simplenews Statistics 7.2

Same name and namespace in other branches
  1. 7 simplenews_statistics_encoder/encoder.inc \simplenews_statistics_encoder_decode()
1 call to simplenews_statistics_encoder_decode()
simplenews_statistics_encoder_simplenews_statistics_decode in simplenews_statistics_encoder/simplenews_statistics_encoder.module
Implements hook_simplenews_statistics_decode().

File

simplenews_statistics_encoder/encoder.inc, line 31
Encoder for IDs. Some logic borrowed from shorturl module.

Code

function simplenews_statistics_encoder_decode($input) {
  $num = 0;
  $mapping = simplenews_statistics_encoder_base_decode_mapping();
  $base = sizeof($mapping);

  // There's just no chance encoded input will ever be so long so if we get
  // something like that - somebody is messing with us trying to eat up CPU
  // cycles on decoding or cause some other kind of overflow.
  if (strlen($input) > 15) {
    return -1;
  }
  $seq = str_split($input);
  if (!is_array($seq) || !(sizeof($seq) > 0)) {
    return -1;
  }
  $seq = array_reverse(str_split($input));
  $i = 0;
  foreach ($seq as $c) {
    if (isset($mapping[$c])) {
      $val = (int) $mapping[$c];
      $num += $val * pow($base, $i);
      $i++;
    }
  }
  return $num;
}