function sms_valid_validate in SMS Framework 6
Same name and namespace in other branches
- 6.2 modules/sms_valid/sms_valid.module \sms_valid_validate()
- 7 modules/sms_valid/sms_valid.module \sms_valid_validate()
Validate a number
Parameters
$number: A phone number.
$options: An array of options.
- dir : Direction of message. See SMS_DIR_* constants.
Return value
Array with the validation result.
- pass : The validation result.
- 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 strings. The last record is the most significant.
2 calls to sms_valid_validate()
- sms_valid_admin_test_form_submit in modules/
sms_valid/ sms_valid.admin.inc - sms_valid_sms_validate in modules/
sms_valid/ sms_valid.module - Implement hook_sms_validate()
File
- modules/
sms_valid/ sms_valid.module, line 460 - Number validation feature module for Drupal SMS Framework.
Code
function sms_valid_validate(&$number, &$options = array()) {
$result = array(
'pass' => NULL,
'log' => 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.');
return $result;
}
// Remove all whitespace
$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'][] = 'Using the global prefix validation ruleset.';
$prefix = $global_ruleset;
$ruleset = sms_valid_get_ruleset($prefix);
$num = $number;
}
elseif (sms_valid_is_local_number($number)) {
$prefix = $local_number_ruleset;
$ruleset = sms_valid_get_ruleset($prefix);
$result['log'][] = 'Identified local number. Using ruleset 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) {
$result['log'][] = 'Identified ruleset prefix ' . $prefix;
$prefix = $ruleset['prefix'];
}
else {
// Could not identify ruleset prefix
$result['log'][] = 'Could NOT identify the ruleset prefix for number ' . $number;
if ($last_resort_enabled && $last_resort_ruleset) {
// We have a last resort to use
$result['log'][] = 'Using last resort ruleset prefix ' . $last_resort_ruleset;
$prefix = $last_resort_ruleset;
$ruleset = sms_valid_get_ruleset($prefix);
}
else {
// No last resort. Fail hard.
$result['log'][] = 'No matching rulesets and no last resort ruleset configured.';
$result['log'][] = 'Cannot validate this number. Denied by default.';
return $result;
}
}
// Strip the ruleset prefix from the number
$num = preg_replace("/^{$prefix}/", '', $number);
}
// Get the rules for this ruleset
$rules = $ruleset['rules'];
// 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'][] = "Number prefix {$prefix} does not allow messages in this direction.";
$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'][] = "Explicit allow for prefix {$prefix} {$rule_prefix}";
$result['pass'] = TRUE;
return $result;
}
else {
$result['log'][] = "Explicit deny for prefix {$prefix} {$rule_prefix}";
$result['pass'] = FALSE;
return $result;
}
}
}
}
# No matching rules. Default deny.
$result['log'][] = "Cannot validate this number. Denied by default.";
return $result;
}