AnonymousNamefag

cabs.php

Jun 30th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.92 KB | None | 0 0
  1. <?php
  2.  
  3. /* Values for TYPE field */
  4. define( 'ARRAY_T',  "A\036" );
  5. define( 'FILE_T',   "F\036" );
  6. define( 'SCALAR_T', "S\036" );
  7. define( 'TUPLE_T',  "T\036" );
  8.  
  9. // Class enforces type uniformity for array elements.
  10. // This is a design decision that I made to make the
  11. // serialization process easier.  If you want to store
  12. // multiple objects of different types, use a tuple.
  13. class array_t implements Countable {
  14.     private $type;
  15.     private $types = array(); // Only used if elements are arrays
  16.     private $members = array();
  17.     public function __construct(){
  18.         if( !($argc = func_num_args()) ) return;
  19.         $argv = func_get_args();
  20.         if( is_array( $argv[0] ) ){
  21.             if( $argc == 1 ){
  22.                 $argv = $argv[0];
  23.                 $argc = sizeof( $argv );
  24.             }
  25.             if( is_array( $argv[0] ) ){
  26.                 $this->type = "array";
  27.                 $this->map( $argv[0], $this->types );
  28.                 $size = sizeof( $argv );
  29.                 for( $i = 0; $i < $size; $i++ ){
  30.                     if( $this->isomorphism( $argv[$i], $this->types ) == -1 ){
  31.                         echo "Error: Structured array_t elements must be isomorphic.";
  32.                         exit;
  33.                     }
  34.                     $this->members[$i] = $argv[$i];
  35.                 }
  36.             }
  37.         }
  38.         else{
  39.             $this->type = gettype( $argv[0] );
  40.             for( $i = 0; $i < $argc; $i++ ){
  41.                 if( gettype( $argv[$i] ) != $this->type ){
  42.                     echo "Error: Elements of array_t object must all be the same type.";
  43.                     exit;
  44.                 }
  45.                 $this->members[$i] = $argv[$i];
  46.             }
  47.         }
  48.     }
  49.     public function set_element( $index, $value ){
  50.         if( is_array( $this->members[$index] ) ){
  51.             if( !is_array( $value ) ){
  52.                 echo "Error: Tried to set array field to non-array";
  53.                 return -1;
  54.             }
  55.             if( $this->isomorphism( $value, $this->types ) == -1 ){
  56.                 echo "Error: Structured array_t elements must be isomorphic.";
  57.                 return -1;
  58.             }
  59.         }
  60.         else if( gettype( $value != $this->type ) ){
  61.             echo "Error: Tried to set array_t element to an incompatible type";
  62.             return -1;
  63.         }
  64.         $this->members[$index] = $value;
  65.     }
  66.     public function get_element( $index ){
  67.         return $this->members[$index];
  68.     }
  69.     public function get_type(){
  70.         return $this->type;
  71.     }
  72.     public function get_types(){
  73.         return $this->types;
  74.     }
  75.     public function count(){
  76.         return count( $this->members );
  77.     }
  78.     /* Function builds a tree of all types in a structure */
  79.     private function map( $arg, &$types ){
  80.         $size = sizeof( $arg );
  81.         for( $i = 0; $i < $size; $i++ ){
  82.             if( is_array( $arg[$i] ) ){
  83.                 $types[$i] = array();
  84.                 $this->map( $arg[$i], $types[$i] );
  85.             }
  86.             else{
  87.                 $types[$i] = gettype( $arg[$i] );
  88.             }
  89.         }
  90.     }
  91.     /* Function compares two trees for isomorphism and type uniformity */
  92.     private function isomorphism( $arg, $types ){
  93.         $size = sizeof( $types );
  94.         if( sizeof( $arg ) != $size ) return -1;
  95.         for( $i = 0; $i < $size; $i++ ){
  96.             if( is_array( $arg[$i] ) && is_array( $types[$i] ) ){
  97.                 if( $this->isomorphism( $arg[$i], $types[$i] ) == -1 )
  98.                     return -1;
  99.             }
  100.             else if( gettype( $arg[$i] ) != $types[$i] )
  101.                 return -1;
  102.         }
  103.     }
  104. }
  105.  
  106. // tuple_t is basically just an alias for PHP hash tables.
  107. // Tuples are more flexible than arrays but much slower.
  108. // Serialization of tuples is done using var_dump().
  109. // Key/value pairs are passed as alternating arguments: key, value, key, value...
  110. class tuple_t {
  111.     private $members = array();
  112.     private $keys = array();
  113.     public function __construct(){
  114.         if( !($argc = func_num_args()) ) return;
  115.         $argv = func_get_args();
  116.         if( $argc % 2 == 1 ){
  117.             echo "Error: Mismatched key/value pairs in tuple_t initialization";
  118.             exit;
  119.         }
  120.         $argc /= 2;
  121.         for( $i = 0; $i < $argc; $i++ ){
  122.             $i2 = $i * 2;
  123.             if( !is_string( $argv[$i2] ) ){
  124.                 echo "Error: Non-string object used for key in tuple_t object";
  125.                 exit;
  126.             }
  127.             $this->keys[$i] = $argv[$i2];
  128.             $this->members[$argv[$i2]] = $argv[$i2+1];
  129.         }
  130.     }
  131.     public function set_element( $key, $value ){
  132.         $this->members[$key] = $value;
  133.     }
  134.     public function get_element( $key ){
  135.         return $this->members[$key];
  136.     }
  137.     public function getkeys(){
  138.         return $this->keys;
  139.     }
  140. }
  141.  
  142. function cabs( $exec, $msg ){
  143.     $descriptorspec = array(
  144.         0 => array( "pipe", "r" ),
  145.         1 => array( "pipe", "w" ),
  146.         2 => array( "file", "error_log.txt", "a" ) /* No error log */
  147.     );
  148.     $cwd = sys_get_temp_dir();
  149.     $env = array( 'blank' => 'blank' ); /* TODO: Add OS-specific environment */
  150.     $process = proc_open( $exec, $descriptorspec, $pipes, $cwd, $env );
  151.     if( is_resource( $process ) ){
  152.         fwrite( $pipes[0], $msg );
  153.         fclose( $pipes[0] );
  154.         $result = stream_get_contents( $pipes[1] );
  155.         fclose( $pipes[1] );
  156.         if( proc_close( $process ) ){ /* Nonzero exit status indicates error */
  157.             echo $result; /* Result will be an error message */
  158.             return -1;
  159.         }
  160.         else return $result;
  161.     }
  162. }
  163.  
  164. function array_to_msg( $arg ){
  165.     if( !is_a( $arg, "array_t" ) ){
  166.         echo "Error: Type checking error for function array_to_msg()";
  167.         exit;
  168.     }
  169.     $msg = ARRAY_T;
  170.     $size = sizeof( $arg )-1;
  171.     /*
  172.      * I used two separate loops to avoid having to check
  173.      * the value of $arg->type every iteration.
  174.      */
  175.     if( $arg->get_type() == "array" ){
  176.         $msg .= serialize_tree( $arg->get_types() ) . "\035"; /* Group separator */
  177.         for( $i = 0; $i < $size; $i++ ){
  178.             $msg .= serialize_tree( $arg->get_element( $i ) ) . "\036"; /* Record separator */
  179.         }
  180.         $msg .= serialize_tree( $arg->get_element( $size ) ) . "\004"; /* End of Transmission */
  181.     }
  182.     else{
  183.         $msg .= $arg->get_type() . "\035";
  184.         for( $i = 0; $i < $size; $i++ ){
  185.             $msg .= $arg->get_element( $i ) . "\036";
  186.         }
  187.         $msg .= $arg->get_element( $size ) . "\004";
  188.     }
  189.     return $msg;
  190. }
  191.  
  192. function msg_to_array( $msg ){
  193.     if( $msg[0] != 'A' ){
  194.         echo "Error: Type checking error for function msg_to_array()";
  195.         exit;
  196.     }
  197.     $len = strlen( $msg );
  198.     $i = 0;
  199.     while( $msg[++$i] != "\035" ); /* Move to end of heading */
  200.     $cmdstr1 = "";
  201.     while( ++$i < $len ){
  202.         switch( $msg[$i] ){
  203.             case "\002" : $cmdstr1[$i] = '['; break; /* STX */
  204.             case "\003" : $cmdstr1[$i] = ']'; break; /* ETX */
  205.             case "\036" : $cmdstr1[$i] = ','; break; /* RS  */
  206.             case "\037" : $cmdstr1[$i] = ','; break; /* US  */
  207.             case "\004" : break 2;                   /* EOT */
  208.             default     : $cmdstr1[$i] = $msg[$i];
  209.         }
  210.     }
  211.     $cmdstr2 = "\$a = array(" . $cmdstr1 . ");";
  212.     eval( $cmdstr2 );
  213.     return $a;
  214. }
  215.  
  216. function serialize_tree( $tree ){
  217.     $size = sizeof( $tree )-1;
  218.     $str = "\002"; /* STX character used to denote start of array */
  219.     for( $i = 0; $i < $size; $i++ ){
  220.         if( gettype( $tree[$i] ) == "array" )
  221.             $str .= serialize_tree( $tree[$i] );
  222.         else
  223.             $str .= $tree[$i];
  224.         $str .= "\037"; /* Unit Separator character separates */
  225.                         /* array elements within a record.    */
  226.     }
  227.     /*
  228.      * Last element is processed separately to avoid having to use
  229.      * a decision statemnt every iteration to add the comma on only
  230.      * the last one
  231.      */
  232.     if( gettype( $tree[$size] ) == "array" )
  233.         $str .= serialize_tree( $tree[$size] );
  234.     else
  235.         $str .= $tree[$size];
  236.     $str .= "\003"; /* ETX character used to denote end of array */
  237.     return $str;
  238. }
  239.  
  240. ?>
Add Comment
Please, Sign In to add comment