You are here

function sms_valid_validate in SMS Framework 7

Same name and namespace in other branches
  1. 6.2 modules/sms_valid/sms_valid.module \sms_valid_validate()
  2. 6 modules/sms_valid/sms_valid.module \sms_valid_validate()

Validates a number.

Parameters

string $number: A phone number to validate.

array $options: An array of options containing the following:

  • dir: Direction of message. See SMS_DIR_* constants.

Return value

array Array with the validation results, a key-value array with the following:

  • pass: array The validation result having the following possible values

    • true if the number passed validation checks.
    • false if the number is denied by validation.
    • null if the number could not be validated.
  • log: Array of log messages. The last record is the most significant.
  • errors: Array of error messages. The last record is the most significant.
4 calls to sms_valid_validate()
SmsValidWebTest::testSmsValidSettingsForm in modules/sms_valid/sms_valid.test
Tests the application of the sms_valid settings form and settings.
SmsValidWebTest::testSmsValidValidate in modules/sms_valid/sms_valid.test
Tests the sms_valid_validate() function.
sms_valid_admin_test_form_submit in modules/sms_valid/sms_valid.admin.inc
Submit handler for sms_valid_admin_test_form()
sms_valid_sms_validate_number in modules/sms_valid/sms_valid.module
Implements hook_sms_validate_number().

File

modules/sms_valid/sms_valid.module, line 472
Number validation feature module for Drupal SMS Framework.

Code

function sms_valid_validate($number, $options = array()) {
  $result = array(
    'pass' => NULL,
    'log' => array(),
    'errors' => array(),
  );

  // Set the default direction if not specified in options.
  $dir = array_key_exists('dir', $options) ? $options['dir'] : SMS_DIR_OUT;
  $use_global_ruleset = variable_get('sms_valid_use_global_ruleset', FALSE);
  $global_ruleset = variable_get('sms_valid_global_ruleset', '64');
  $local_number_prefix = variable_get('sms_valid_local_number_prefix', '0');
  $local_number_ruleset = variable_get('sms_valid_local_number_ruleset', '64');
  $last_resort_enabled = variable_get('sms_valid_last_resort_enabled', FALSE);
  $last_resort_ruleset = variable_get('sms_valid_last_resort_ruleset', NULL);

  // Check if we should use a specific ruleset prefix.
  if (array_key_exists('prefix', $options) && $options['prefix'] >= 0) {
    $specific_prefix = $options['prefix'];
  }
  else {
    $specific_prefix = NULL;
  }

  // Check for zero-length value.
  if (!strlen($number)) {
    $result['log'][] = t('You must enter a phone number.');
    $result['errors'][] = t('Zero length phone number specified.');
    $result['pass'] = FALSE;
    return $result;
  }

  // Remove all non-numeric characters.
  $number = preg_replace('/[^\\d]/', '', $number);

  // Check if we should use a specific ruleset.
  if ($specific_prefix) {
    $prefix = $specific_prefix;
    $ruleset = sms_valid_get_ruleset($prefix);

    // Strip ruleset prefix (if exist) and leading zeros from the number.
    $num = preg_replace("/^{$prefix}/", '', $number);
    $num = ltrim($num, '0');
  }
  elseif ($use_global_ruleset) {
    $result['log'][] = t('Using the global prefix validation ruleset.');
    $prefix = $global_ruleset;
    $ruleset = sms_valid_get_ruleset($prefix);

    // @todo - why don't we strip the prefix in the global ruleset case.
    $num = $number;
  }
  elseif (sms_valid_is_local_number($number)) {
    $prefix = $local_number_ruleset;
    $ruleset = sms_valid_get_ruleset($prefix);
    $result['log'][] = t('Identified local number. Using ruleset prefix @prefix', array(
      '@prefix' => $prefix,
    ));

    // Strip the local prefix from number
    $num = preg_replace("/^{$local_number_prefix}/", '', $number);
  }
  else {
    $ruleset = sms_valid_get_ruleset_for_number($number);
    if ($ruleset) {
      $prefix = $ruleset['prefix'];
      $result['log'][] = t('Identified ruleset prefix @prefix', array(
        '@prefix' => $prefix,
      ));
    }
    else {

      // Could not identify ruleset prefix.
      $result['log'][] = t('Could NOT identify the ruleset prefix for number @number', array(
        '@number' => $number,
      ));
      if ($last_resort_enabled && $last_resort_ruleset) {

        // We have a last resort to use
        $result['log'][] = t('Using last resort ruleset prefix @prefix', array(
          '@prefix' => $last_resort_ruleset,
        ));
        $prefix = $last_resort_ruleset;
        $ruleset = sms_valid_get_ruleset($prefix);
      }
      else {

        // No last resort. Fail hard.
        $result['log'][] = t('No matching rulesets and no last resort ruleset configured.');
        $result['log'][] = t('Cannot validate this number. Denied by default.');
        $result['errors'][] = t('Cannot validate this number. Denied by default.');
        $result['pass'] = FALSE;
        return $result;
      }
    }

    // Strip the ruleset prefix from the number.
    $num = preg_replace("/^{$prefix}/", '', $number);
  }

  // Get the rules for this ruleset.
  $rules = $ruleset['rules'];

  // Lets make sure we have a rule before trying to sort it.
  if ($rules !== NULL) {

    // Sort the rules by prefix (key) in reverse order (largest to smallest).
    krsort($rules);
  }

  // Check whether this ruleset is enabled for the direction of communication.
  if (!sms_valid_ruleset_is_enabled($prefix, $dir)) {
    $result['log'][] = t('Number prefix @prefix does not allow messages in this direction.', array(
      '@prefix' => $prefix,
    ));
    $result['errors'][] = t('Number prefix @prefix does not allow messages in this direction.', array(
      '@prefix' => $prefix,
    ));
    $result['pass'] = FALSE;
    return $result;
  }

  // Test the number against each rule prefix until we get a match.
  if (!empty($rules)) {
    foreach ($rules as $rule_prefix => $rule) {
      $result['log'][] = 'Trying rule with prefix ' . $rule_prefix . ' on number ' . $num;
      if (preg_match("/^{$rule_prefix}/", $num)) {
        if ($rule['allow']) {

          // Set the full validated number and return.
          $number = $prefix . $num;
          $result['log'][] = t('Explicit allow for prefix @prefix @rule_prefix', array(
            '@prefix' => $prefix,
            '@rule_prefix' => $rule_prefix,
          ));
          $result['pass'] = TRUE;
          return $result;
        }
        else {
          $result['log'][] = t('Explicit deny for prefix @prefix @rule_prefix', array(
            '@prefix' => $prefix,
            '@rule_prefix' => $rule_prefix,
          ));
          $result['errors'][] = t('Number @number denied by sms validation rule @prefix @rule_prefix', array(
            '@number' => $number,
            '@prefix' => $prefix,
            '@rule_prefix' => $rule_prefix,
          ));
          $result['pass'] = FALSE;
          return $result;
        }
      }
    }
  }

  // No matching rules. Default deny.
  $result['log'][] = t('Cannot validate this number. Denied by default.');
  $result['errors'][] = t('Cannot validate this number. No matching sms valid rule found.');
  $result['pass'] = FALSE;
  return $result;
}