common_utilities.php

<?php
  // A library of utilities that can be used by PHP scripts  
  // comprising an external captive portal. 
  // A utility that translates error codes to error messages. 
  function code_2_message($code, $content_type)
  {
    $errMsgList = array (
      0 =>
      array (
        'label' => 'Invalid',
        'content' => '<span style=\'color:red\'>Empty id / 
password not allowed. Please try again.</span>'
      ),
      1 =>
      array (
        'label' => 'Success',
        'content' => 'Success',
      ),
      2 =>
      array (
        'label' => 'Access Fail',
        'content' => '<span style=\'color:red\'>Userid or 
password incorrect. Please try again.</span>',
      ),
      3 =>
      array (
        'label' => 'Fail',
        'content' => '<span style=\'color:red\'>A problem has 
occurred while trying to validate your userid & password.<br>Please contact
your system administrator.</span>',
      ),
      4 =>
      array (
        'label' => 'Timeout',
        'content' => '<span style=\'color:red\'>A problem has 
occurred while trying to validate your userid & password.<br>Please contact
your system administrator.</span>',
      ),
      5 =>
      array (
        'label' => 'RADIUS shared security key fail',
        'content' => '<span style=\'color:red\'>A problem has 
occurred while trying to validate your userid & password.<br>Please contact
your system administrator.</span>',
      ),
      6 =>
      array (
        'label' => 'RADIUS internal error',
        'content' => '<span style=\'color:red\'>A problem has 
occurred while trying to validate your userid & password.<br>Please contact
your system administrator.</span>',
      ),
      7 =>
      array (
        'label' => 'Max RADIUS login fail',
        'content' => '<span style=\'color:red\'>Too many users 
trying to login at the same time.Please try again later.</span>',
      ),
      8 =>
      array (
        'label' => 'Invalid Login parameters',
        'content' => '<span style=\'color:red\'>Userid or 
password incorrect. Please try again.</span>',
      ),
      9 =>
      array (
        'label' => 'General failure',
        'content' => '<span style=\'color:red\'>A problem has 
occurred while trying to validate your userid & password.<br>Please contact
your system administrator.</span>',
      ),
      14 =>
      array (
        'label' => 'Invalid third party parameters',
        'content' => '<span style=\'color:red\'>Invalid third 
party parameters.</span>',
      ),
      15 =>
      array (
        'label' => 'Authentication in progress failure',
        'content' => '<span style=\'color:red\'>Authentication is 
in progress.</span>',
      ),
      17 =>
      array (
        'label' => 'Max concurrent session failure',
        'content' => '<span style=\'color:red\'>Login rejected 
because the maximum number of concurrent sessions for this set of credentials
has been reached. Please try again later.</span>',
      ),
      18 =>
      array (
        'label' => 'Identified session not found',
        'content' => '<span style=\'color:red\'>Login failed 
because could not find a session for the specified identifiers.</span>'
      ),
      99 =>
      array (
        'label' => 'Timeout while trying to authorize a session',
        'content' => '<span style=\'color:red\'>Login failed 
because because the controller took too long to authorize the
session.</span>'
      )
      );
      return (isset($errMsgList[$code])) ?
        $errMsgList[$code][$content_type] :
        "Unrecognized error code: ".$code;
  }
  // General purpose error reporting procedure. 
  function printError($errorMsg) {
    header('Content-type: text/html; charset=iso-8859-1');
    print
"<html>\n<head><title>Error</title></head><body>\n<p>\n$errorMsg\n</p>\n</bod
y>\n</html>\n";
  }
  // Use base64 url safe encode/decode when dealing 
  // with AES-encrypted strings. 
  // encode: '+'=>'-', '/' => '_' , '=' => '!' 
  function base64_url_encode($input) {
    return strtr(base64_encode($input), '+/=', '-_!');
  }
  // Decode: '-'=>'+', '_' => '/' , '!' => '=' 
  function base64_url_decode($input) {
    return base64_decode(strtr($input, '-_!', '+/='));
  }
  // xml parsing functions 
  function my_xml2array($contents)
  {
    $xml_values = array();
    if (! isset($contents)) {
      return false;
    }
    $parser = xml_parser_create('');
    if(!$parser) {
      return false;
    }
    xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING,
                  'UTF-8');
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
    xml_parse_into_struct($parser, trim($contents), $xml_values);
    xml_parser_free($parser);
    if (!$xml_values) {
      return array();
    }
    $xml_array = array();
    $last_tag_ar =& $xml_array;
    $parents = array();
    $last_counter_in_tag = array(1=>0);
    foreach ($xml_values as $data)
    {
      switch($data['type'])
      {
        case 'open':
          $last_counter_in_tag[$data['level']+1] = 0;
          $new_tag = array('name' => $data['tag']);
          if(isset($data['attributes']))
          $new_tag['attributes'] = $data['attributes'];
          if(isset($data['value']) && trim($data['value']))
          $new_tag['value'] = trim($data['value']);
          $last_tag_ar[$last_counter_in_tag[
$data['level']]] = $new_tag;
          $parents[$data['level']] =& $last_tag_ar;
          $last_tag_ar =& $last_tag_ar[
            $last_counter_in_tag[$data['level']]++];
          break;
        case 'complete':
          $new_tag = array('name' => $data['tag']);
          if(isset($data['attributes']))
          $new_tag['attributes'] = $data['attributes'];
          if(isset($data['value']) &&
            trim($data['value']))
          $new_tag['value'] = trim($data['value']);
          $last_count = count($last_tag_ar)-1;
            $last_tag_ar[ $last_counter_in_tag[$data[
'level' ]]++ ] = $new_tag;
          break;
        case 'close':
          $last_tag_ar =& $parents[$data['level']];
          break;
        default:
          break;
      };
    }
    return $xml_array;
  }
  function get_value_by_path($__xml_tree, $__tag_path)
  {
    $tmp_arr =& $__xml_tree;
    $tag_path = explode('/', $__tag_path);
    foreach($tag_path as $tag_name)
    {
      $res = false;
      foreach($tmp_arr as $key => $node)
      {
        if(is_int($key) && $node['name'] == $tag_name)
        {
          $tmp_arr = $node;
          $res = true;
          break;
        }
      }
      if(!$res) {
        return false;
      }
    }
    if( isset($tmp_arr['value']) ) {
      return $tmp_arr['value'];
    } else {
      return null;
    }
  }
  function is_not_empty($string) {
    return (isset($string) && (0 < strlen($string)));
  }
  //check token format
    function tokenCheck($val){
  	  return preg_match('/^([a-zA-Z0-9-_!]){0,24}$/', $val);
    }
    //check the mac address
    function macCheck($val){
    	return preg_match("/^[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}$/", $val);
    }
    //encode the input string to avoid script attack
    function convertUrlParam($input) {
    	return htmlentities($input, ENT_QUOTES);
  }
?>