Commits

toby...@gmail.com  committed 7726975

interface change - classes tend to know better how to save/retrieve themselves

  • Participants
  • Parent commits bdb6561

Comments (0)

Files changed (4)

   private $url;
 
   public static function publicMethods($obj){
-    $ignored=is_a($obj,'RemoteObject')?get_class_methods('RemoteObject'):array();
-    return array_values(array_diff(get_class_methods($obj),$ignored));
+    $ignored=($obj instanceof RemoteObject)?get_class_methods('RemoteObject'):array();
+    return array_values(array_diff(get_class_methods($obj),$ignored,array('__construct')));
   }
 
   public static function rpcException($exception,$msg=null){
 
   public static function rpcSuccess($obj,$baseURL){
     $success=array();
-    if($obj instanceof RemoteObject){
+    //See if this is a Remote3Object and had a useful ID
+    if($obj instanceof RemoteObject && ($id = $obj->getID()) ){
       $success['methods']=$obj->getRemoteMethods();
-      $success['uri']=$baseURL.'?'.http_build_query(array_merge($_GET,array('obj_id'=>$obj->save($_SESSION))));
+      $success['uri']=$baseURL.'?'.
+	http_build_query(array_merge($_GET,
+				     array('obj_id'=>$id,
+					   'obj_cls'=>get_class($obj))));
     }
     $success['success']=$obj;
     $success['output']=ob_get_clean();
     $rpc->url=$_SERVER['PHP_SELF'];
 
     if(isset($_GET['obj_id'])){
-      $obj=$_SESSION[$_GET['obj_id']];
-      if(! $obj instanceof RemoteObject){
-	RPC::rpcError(0,"Cannot find object");
-	return;
+      $cls=$_GET['obj_cls'];
+      $id = $_GET['obj_id'];
+      try{
+	//ensure class name is reasonable
+	//and is a subclass of RemoteObject
+	if(is_callable($cls,true) && 
+	   is_subclass_of($cls,'RemoteObject')){
+	  $obj=call_user_func(array($cls,'getObj'),$id);
+	  if(! $obj instanceof RemoteObject){
+	    RPC::rpcError(0,"Cannot find object");
+	    return;
+	  }
+	}else{
+	  RPC::rpcError(0,"$cls is not a remote class");
+	}
+      }catch(Exception $e){
+	  RPC::rpcError(0,"Cannot find class $cls");
       }
     }
 
     echo RPC::rpcSuccess($result,$rpc->url);
       
   }
-  
-
 }
 

File php/RemoteObject.php

 <?php
 abstract class RemoteObject {
-  protected $id = null;
   protected $remoteMethods=null;
-
-  public function __construct($id=null){
-    if($id===null){
-      $this->id=uniqid(get_class($this).'::',true);
-    }else{
-      $this->id=$id;
-    }
+  
+  //return an ID suitable for use with getObj($id)
+  //this id will be stored client side, so some security 
+  //(e.g. hashes) is appropriate.
+  public function getID(){
+    return false;
   }
-
-  public function save(&$store){
-    $store[$this->id]=$this;
-    return $this->id;
+  
+  // return an instance (presumably of this class)
+  // for a given ID (stored client side) 
+  // so this must incorporate error checking!
+  public static function getObj($id){
+    return false;
   }
 
   public function getRemoteMethods(){

File php/autoload.php

 <?php
-  if(!function_exists('__autoload')){
-    function __autoload($class){
-      include_once dirname(__FILE__).'/'.$class.".php";
+  if(!function_exists('TRO_autoload')){
+    function TRO_autoload($class){
+      if(!preg_match('!\.|\/!',$class)){
+	include_once dirname(__FILE__).'/'.preg_replace('!\\\\!','/',$class).".php";
+      }
     }
-  }
+    spl_autoload_register('TGL_autoload');
+  }

File php/server.php

   RPCFunctions::serveRPC($rpcs);
 }
 
-
-/*
-  Utility Functions for composing safe query based RPCs
-*/
-
-$qProcessors=array(); // store result types available
-
-//escape args and insert them into the the appropriate placeholders
-// '?' is replaced with the next numeric arg
-// {argName}  in replaced with the value of the key 'argName' in $args
-// this form also works for {n} for some integer n
-function query($pattern,$db,$args=array(),$resultType=NULL){
-  global $qProcessors;
-
-  //initialize result type dependant on query
-  if($resultType==NULL){
-    if(preg_match('/^\s*(SELECT|SHOW|DESCRIBE|EXPLAIN)\b/i',$pattern)){
-      $resultType="assocs";
-    }else if(preg_match('/^\s*INSERT\b/i',$pattern)){
-      $resultType='insert_id';
-    }else{
-      $resultType="count";
-    }
-  }
-
-  foreach ($args as $argName => $argVal){
-    if(is_int($argName)){
-      //replace ? with $argVal
-      $pattern = preg_replace("/\\?/",mysql_real_escape_string("$argVal"),$pattern,1);
-    }
-    //replace {$argname} with $argVal
-    $pattern = preg_replace("/\\{".preg_quote("$argName")."\\}/",mysql_real_escape_string("$argVal"),$pattern);
-  }
-  //  echo $pattern;
-  $res=mysql_query($pattern);
-  if(($e=mysql_error())) 
-    throw new Exception($e);
-  // use the appropriate query processor to return the result, or false if query failed
-  return $res?call_user_func($qProcessors[$resultType],$res):$res;
-}
-
-//return a single value from a result set
-$qProcessors['value']="result_value";
-function result_value($res){
-  list($r)=mysql_fetch_row($res);
-  return $r;
-}
-
-//return a single row as an integer indexed array
-$qProcessors['list']="result_list";
-function result_list($res){
-  return mysql_fetch_row($res);
-}
-
-//return a single row as an associative array
-$qProcessors['assoc']="result_assoc";
-function result_assoc($res){
-  return mysql_fetch_assoc($res);
-}
-
-//return a list of values (single element rows)
-$qProcessors['values']="result_values";
-function result_values($res){
-  $rows=array();
-  while((list($row)=mysql_fetch_row($res)))
-    $rows[]=$row;
-  return $rows;
-}
-
-//return anarray of 0-indexed rows
-$qProcessors['lists']="result_lists";
-function result_lists($res){
-  $rows=array();
-  while(($row=mysql_fetch_row($res)))
-    $rows[]=$row;
-  return $rows;
-}
-
-//return an array of assocs
-$qProcessors['assocs']="result_assocs";
-function result_assocs($res){
-  $rows=array();
-  while(($row=mysql_fetch_assoc($res)))
-    $rows[]=$row;
-  return $rows;
-}
-
-//return the number of rows affected
-$qProcessors['count']="result_count";
-function result_count($res){
-  return $res===True?mysql_affected_rows():mysql_num_rows();
-}
-
-//return the number of rows returned/modified
-$qProcessors['pairs']="result_pairs";
-function result_pairs($res){
-  $rows=array();
-  while(($row=mysql_fetch_row($res)))
-    $rows[$row[0]]=$row[1];
-  return $rows;
-}
-
-//return the autoincrement ID of the last row inserted
-$qProcessors['insert_id']="result_insert_id";
-function result_insert_id($res){
-  return mysql_insert_id();
-}
-
-?>