<?PHP
//"wrap-functions" for ASP-->PHP conversion;
//version 40;
//some features available only from PHP4;

//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//  to imitate ASP-type err object exception handling.
//  PHP 4.1.0
//------------------------------------------------------------------------------
//in particular this function sets important global $err_number variable
//which will be used instead of err.number in converted PHP result.
//mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
//  IMPORTANT
    global $err_number_c_;
    global $err_description_c_;
    global $err_display_c_;
    global $err_notify_c_;
    global $err_helpcontext_c_;              //rudi: useless unless use manually 
                                             //added to RESULT
    global $err_helpfile_c_;                 //rudi
//  set_error_handler( handle_exc_c_ );      //to exclude a=1/0 reports.
//  error_reporting (0);                     //set this to suppress some exceptions.
//mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm


function handle_exc_c_($errno, $errstr, $file_name, $string_number) {
global $err_number_c_;
global $err_description_c_;
global $err_display_c_;
global $err_notify_c_;
$err_number_c_ = $errno;
$err_description_c_ = $errstr . "\n" . 
                       "in file: " .  $file_name . "\n" . 
                       "in line: " . $string_number;
$err_notify_c_ =false;
$err_display_c_ = false;
}

//--------------------------------------------------------------------
//preserves VB err.rase values, restricts err.number accroding to PHP;
//no input restrictions;
//only first an third arguments: number and description close to their
//ASP equivalents;
//NULL is used to imitate skipped VB arguments;
//--------------------------------------------------------------------
function err_raise_c_() {
     $n=func_num_args();
     $number = func_get_arg(0);
     $fdescr = "number: " . $number;
     if( $n>=2 ) { 
          $source = func_get_arg(1);
          if($source != NULL) {$fdescr .= "\nsource: " . $source; }
          if( $n>=3 ) { 
              $description = func_get_arg(2);
              if($description != NULL) {$fdescr .= "\ndescription: " . $description;}
              if( $n>=4 ) { 
                  $helpfile = func_get_arg (3);
                  if($helpfile  != NULL) {$fdescr .= "\nhelp file: " . $helpfile; }
                  if( $n==5 ) { 
                      $helpcontext = func_get_arg (4);
                      if($helpcontext  != NULL) {$fdescr .= "\nhelp context: " . $helpcontext;}
                  }
              }
          }
     }

     if( $number != E_USER_ERROR  &&  
         $number != E_USER_NOTICE &&
         $number != E_USER_WARNING  )  { $number = E_USER_ERROR; }
     trigger_error ( $fdescr, $number );
}


function err_clear_c_() {

   global $err_number_c_, $err_description_c_;
   global $err_display_c_, $err_notify_c_;
   $err_number_c_ = 0;  $err_description_c_="";
}

function on_error_resume_next_c_() {
   err_clear_c_();
   restore_error_handler(); 
}
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM




//==================================================================================
// replace
//----------------------------------------------------------------------------------
//! if extra 4, 5, 6 arguments (""start"",""count"", . .) are submitted, 
//manual correction is required;
function replace_c_($where_to_replace, $what_to_replace, $with_to_replace) {
         return (str_replace($what_to_replace, $with_to_replace,$where_to_replace));
}
//==================================================================================




//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//because 
// 1) string numeration in VB, posVB, and string position in PHP, posPHP, are different:
//    posVB = posPHP+1
// 2) and PHP has vague return value 0 (can be 0, but can be ""false"",)
//
//then the following approach solves this in this schema: 
//  extra preceding character ""x"" has been added to a string;
//  so,  variables and function returns have VB meaning,
//  and 0 matches ""false"" as it is in VB and PHP
//  so, call instr(2,''moo'',''0'') will be converted to strpos(''x''.''moo'',''0'',2);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


//==============================================================================
//  instr 
//  will work for case of two parameters: $a - where to search
//                                        $b - what to search
//            for case of three parameters: $a - position from which to search 
//                                          $b - where to search
//                                          $c - what to search
//------------------------------------------------------------------------------
function instr_c_() {
         $n=func_num_args();
         // echo ("args number = " . $n );
         $a = func_get_arg (0);
         $b = func_get_arg (1);
         if( $n==2 ) { 
              $pos =  strpos( "x".$a, $b, 1); 
              return $pos;
	   } else {
   	             $c = func_get_arg (2);
                   $pos =  strpos("x".$b, $c, $a ); 
         		 return $pos;
//			 return (strpos("x".$b, $c, $a )); 
	   }
}
//==============================================================================



//===============================================================
//  instrrev  
//---------------------------------------------------------------
//  done for two or three arguments;
//  for debug and comments, use //echo statement;
function instrrev_c_() {

    $n=func_num_args();

    if( $n==2 ) { 

        $a = func_get_arg (0);
        $la = strlen($a);
        $b = func_get_arg (1);
        $s = $la;
    } else {
      
        $a = func_get_arg (1);
        $la = strlen($a);
        $b = func_get_arg (2);
        $s = func_get_arg (0);           

    }         

    $lb = strlen($b);

    //echo "\r\n where to search=" . $a;
    //echo "\r\n what to search="  . $b;
    //echo "\r\n where to start="  . $s;
    
    
    //this formula inverses origin 1 ---> a:
    // 1.........s......s+b-1........a    
    // and sets following restriction on s:
    if( $s + $lb - 1 > $la || $s < 1 ) { return 0; }
    $start = $la - ($s + $lb - 1) + 1;
    // add char "x" to avoid 0-origin:
    // char "x" will be always skipped in search
    // because starting position will be never 0;
    $r1    = strrev($a . "x");
    $r2    = strrev($b);

    //echo "\r\n where reversed=" . $r1;
    //echo "\r\n what reversed="  . $r2;
    //echo "\r\n start reversed=" . $start;

    $pos = strpos( $r1, $r2, $start);

    //echo "\r\n pos reversed="   . $pos;
    if( $pos <> 0 ) {
        //inverse back:
        $pos = $la - ($pos + $lb - 1) + 1;
    }
    //echo "\r\n pos found="      . $pos;
    return $pos;
}
//===============================================================




//===============================================================
//  right 
//---------------------------------------------------------------
function right_c_($swhere,$spos) {
         return (substr($swhere, strlen($swhere)-$spos, $spos));
}


//===============================================================
//  space
//  returns empty string padded with spaces $size times;
//---------------------------------------------------------------
function space_c_($size) {
         return (str_pad("",$size));
}


//===============================================================
//  Request.Form
//  This is depricated in 4.1.0. New var is $_POST
//---------------------------------------------------------------
function request_form_c_($a) {
         global $HTTP_POST_VARS;
         return $HTTP_POST_VARS[$a];
}


//===============================================================
//  Request.QueryString. Use $_GET from 4.1.0.
//---------------------------------------------------------------
function request_query_string_c_($a) {
         global $HTTP_GET_VARS;
         return $HTTP_GET_VARS[$a];
}


//===============================================================
//  Request.ServerVariables
//---------------------------------------------------------------
function request_server_var_c_($a) {
     //$return_it = $HTTP_ENV_VARS[$a]; - may be useful.
     //[1]ASP:  http://localhost/iishelp/iis/htm/asp/intr5vsj.htm
     //[2]PHP:  http://www.php.net/manual/en/reserved.variables.php#reserved.variables.server
     switch (strtoupper($a)) {

        case "SCRIPT_NAME":         $a="PHP_SELF"; 
             //on olm.net hosting: PHP-"SCRIPT_NAME" = /cgi-bin/php 
             //PHP-"PATH_INFO" was not documented, but was the same as PHP-"PHP_SELF";
             break;
        case "APPL_PHYSICAL_PATH":  $a="PATH_TRANSLATED"; 
             //not always precise: on olm.net hosting: it is "rooted" to /www/...
             //PHP-"SCRIPT_FILENAME"  would be incorrect; this is a path to an "engine" itself;
             break;
        case "REMOTE_HOST":         $a="REMOTE_ADDR";
             //! not precise: please see [1]; 
             break;
        case "URL":                 $a="REQUEST_URI";   //!
             break;
        
        //nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
        //no difference between ASP and PHP.
        //-------------------------------------------------------

        // - - - - - - - - - - - - - - - - - 
        //HTTP headers sent from the client:
        //HTTP_ACCEPT
        //HTTP_ACCEPT_LANGUAGE
        //HTTP_CONNECTION
        //HTTP_HOST                   
        //HTTP_USER_AGENT
        //HTTP_COOKIE  assumed
        //HTTP_ACCEPT_ENCODING
        //assumed that other headers from the 
        //client are the same.
        //- - - - - - - - - - - - - - - - - - 

       
        //SERVER_NAME
        //SERVER_PORT 
        //PATH_INFO          '! assumed that is the same 
        //QUERY_STRING
        //SERVER_SOFTWARE
        //SERVER_PROTOCOL
        //REQUEST_METHOD
        //GATEWAY_INTERFACE
        //REMOTE_ADDR
        //nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
    
        //It is notable that PHP has "REMOTE_PORT" and ASP does not.
        //It seems that in ASP most reliable are 
        //HTTP_HOST, SERVER_PORT, REMOTE_ADDR, SCRIPT_NAME.
 
        //ALL OTHER ASP server variables will be submitted to
        //PHP $_SERVER array "as is". To correct this, more 
        //"case" lines can be added in the body of this function.

        //ALL_RAW ??
        //APPL_MD_PATH  ??
}
         $return_it = $_SERVER[$a];
         //if(substr($return_it,0,1)=="/") {$return_it = substr($return_it,1); }   //! cut first "/" if any;
         return $return_it;
}
//===============================================================


//===============================================================
//  Ubound
//---------------------------------------------------------------
//this wrapper simply shifts elements count:
//! done only for the first argument:
//low bound in ASP is 0, so first argument case matches PHP syntax precisely;
//must be updated for full case: UBound(arrayname[, dimension])

function ubound_c_($array) { return(sizeof($array) - 1); }	
//===============================================================


//===============================================================
//  split
//---------------------------------------------------------------
//this wrapper simply swaps arguments:
//! done only for 2 arguments. when more than two, must be rewritten:
//two more are: [, count[, compare]]]
function split_c_($string, $separator) { return (explode($separator, $string)); }
//===============================================================


//===============================================================
//  rnd
//---------------------------------------------------------------
//  If rnd has positive argument or no arguments, then
//  this wrapper should be a right replacement for rnd.
//  If argument is zero or less than zero, this wrapper will 
//  give incorrect results than in ASP specifications.
//
//  accuracy is about 9 digits 
//
function rand_c_() {
    return (float)rand(0,2000000000.)/2e9;
}
//===============================================================


//===============================================================
//  randomize [number]
//---------------------------------------------------------------
function srand_c_() {
  static $already_called = false;
  $n=func_num_args();
  if($n==0) { //pick up seed from timer as this is done in ASP:
              if(!$already_called) {
                  $seed=  (double)microtime() * 1000000; 
                  $already_called = true;
			  //or use something like this to avoid duplicate call:
			  //credit to: hagen@von-eitzen.de in php.net
			  //10-Oct-2000 03:51 
                    //if (!$GLOBALS["IHaveCalledSrandBefore"]++) { 
                    //  srand((double) microtime() * 1000000);
              }
  }  else     {     //trying to convert ASP supplied seed to big enough integer:
              $seed = func_get_arg (0);
              if(abs($seed) < 100000 ) { $seed = abs($seed) * 10000; }
  }
  srand($seed);
}
//===============================================================



//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//     RESPONSE OBJECT
//================================================================================
//Response.Buffer [=flag] False is default.
//in PHP: ob_start ([string output_when_flush]) 
function set_buffer_c_() {
   $n=func_num_args();
   if($n==0) {   ob_end_flush();
   } else    {
      $Flag = func_get_arg (0); 
      if(Flag) { ob_start(); 
      } else   { ob_end_flush(); 
      } 
   }
}
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
?>