[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/include/Webservices/ -> OperationManager.php (source)

   1  <?php
   2  /*+***********************************************************************************
   3   * The contents of this file are subject to the vtiger CRM Public License Version 1.0
   4   * ("License"); You may not use this file except in compliance with the License
   5   * The Original Code is:  vtiger CRM Open Source
   6   * The Initial Developer of the Original Code is vtiger.
   7   * Portions created by vtiger are Copyright (C) vtiger.
   8   * All Rights Reserved.
   9   *************************************************************************************/
  10      
  11  	function setBuiltIn($json){
  12          $json->useBuiltinEncoderDecoder = true;
  13      }
  14      
  15      class OperationManager{
  16          private $format;
  17          private $formatsData=array(
  18              "json"=>array(
  19                  "includePath"=>"include/Zend/Json.php",
  20                  "class"=>"Zend_Json",
  21                  "encodeMethod"=>"encode",
  22                  "decodeMethod"=>"decode",
  23                  "postCreate"=>"setBuiltIn"
  24              )
  25          );
  26          private $operationMeta = null;
  27          private $formatObjects ;
  28          private $inParamProcess ;
  29          private $sessionManager;
  30          private $pearDB;
  31          private $operationName;
  32          private $type;
  33          private $handlerPath;
  34          private $handlerMethod;
  35          private $preLogin;
  36          private $operationId;
  37          private $operationParams;
  38          
  39  		function OperationManager($adb,$operationName,$format, $sessionManager){
  40              
  41              $this->format = strtolower($format);
  42              $this->sessionManager = $sessionManager;
  43              $this->formatObjects = array();
  44              
  45              foreach($this->formatsData as $frmt=>$frmtData){
  46                  require_once($frmtData["includePath"]);
  47                  $instance = new $frmtData["class"]();
  48                  $this->formatObjects[$frmt]["encode"] = array(&$instance,$frmtData["encodeMethod"]);
  49                  $this->formatObjects[$frmt]["decode"] = array(&$instance,$frmtData["decodeMethod"]);
  50                  if($frmtData["postCreate"]){
  51                      call_user_func($frmtData["postCreate"],$instance);
  52                  }
  53              }
  54              
  55              $this->pearDB = $adb;
  56              $this->operationName = $operationName;
  57              $this->inParamProcess = array();
  58              $this->inParamProcess["encoded"] = &$this->formatObjects[$this->format]["decode"];
  59              $this->fillOperationDetails($operationName);
  60          }
  61          
  62  		function isPreLoginOperation(){
  63              return $this->preLogin == 1;
  64          }
  65          
  66  		private function fillOperationDetails($operationName){
  67              $sql = "select * from vtiger_ws_operation where name=?";
  68              $result = $this->pearDB->pquery($sql,array($operationName));
  69              if($result){
  70                  $rowCount = $this->pearDB->num_rows($result);
  71                  if($rowCount > 0){
  72                      $row = $this->pearDB->query_result_rowdata($result,0);
  73                      $this->type = $row['type'];
  74                      $this->handlerMethod = $row['handler_method'];
  75                      $this->handlerPath = $row['handler_path'];
  76                      $this->preLogin = $row['prelogin'];
  77                      $this->operationName = $row['name'];
  78                      $this->operationId = $row['operationid'];
  79                      $this->fillOperationParameters();
  80                      return;
  81                  }
  82              }
  83              throw new WebServiceException(WebServiceErrorCode::$UNKNOWNOPERATION,"Unknown operation requested");
  84          }
  85          
  86  		private function fillOperationParameters(){
  87              $sql = "select * from vtiger_ws_operation_parameters where operationid=? order by sequence";
  88              $result = $this->pearDB->pquery($sql,array($this->operationId));
  89              $this->operationParams = array();
  90              if($result){
  91                  $rowCount = $this->pearDB->num_rows($result);
  92                  if($rowCount > 0){
  93                      for ($i=0;$i<$rowCount;++$i){
  94                          $row = $this->pearDB->query_result_rowdata($result,$i);
  95                          array_push($this->operationParams,array($row['name']=>$row['type']));
  96                      }
  97                  }
  98              }
  99          }
 100          
 101  		public function getOperationInput(){
 102              $type = strtolower($this->type);
 103              switch($type){
 104                  case 'get': $input = &$_GET;
 105                      return $input;
 106                  case 'post': $input = &$_POST;
 107                      return $input;
 108                  default: $input = &$_REQUEST;
 109                      return $input;
 110              }
 111          }
 112          
 113  		function sanitizeOperation($input){
 114              return $this->sanitizeInputForType($input);
 115          }
 116          
 117  		function sanitizeInputForType($input){
 118              
 119              $sanitizedInput = array();
 120              foreach($this->operationParams as $ind=>$columnDetails){
 121                  foreach ($columnDetails as $columnName => $type) {
 122                      $sanitizedInput[$columnName] = $this->handleType($type,vtws_getParameter($input,$columnName));;
 123                  }
 124              }
 125              return $sanitizedInput;
 126          }
 127          
 128  		function handleType($type,$value){
 129              $result;
 130              $value = stripslashes($value);
 131              $type = strtolower($type);
 132              if($this->inParamProcess[$type]){
 133                  $result = call_user_func($this->inParamProcess[$type],$value);
 134              }else{
 135                  $result = $value;
 136              }
 137              return $result;
 138          }
 139          
 140  		function runOperation($params,$user){
 141              global $API_VERSION;
 142              try{
 143                  $operation = strtolower($this->operationName);
 144                  if(!$this->preLogin){
 145                      $params[] = $user;
 146                      return call_user_func_array($this->handlerMethod,$params);
 147                  }else{
 148                      $userDetails = call_user_func_array($this->handlerMethod,$params);
 149                      if(is_array($userDetails)){
 150                          return $userDetails;
 151                      }else{
 152                          $this->sessionManager->set("authenticatedUserId", $userDetails->id);
 153                          global $adb;
 154                          $webserviceObject = VtigerWebserviceObject::fromName($adb,"Users");
 155                          $userId = vtws_getId($webserviceObject->getEntityId(),$userDetails->id);
 156                          $vtigerVersion = vtws_getVtigerVersion();
 157                          $resp = array("sessionName"=>$this->sessionManager->getSessionId(),"userId"=>$userId,"version"=>$API_VERSION,"vtigerVersion"=>$vtigerVersion);
 158                          return $resp;
 159                      }
 160                  }
 161              }catch(WebServiceException $e){
 162                  throw $e;
 163              }catch(Exception $e){
 164                  throw new WebServiceException(WebServiceErrorCode::$INTERNALERROR,"Unknown Error while processing request");
 165              }
 166              
 167          }
 168          
 169  		function encode($param){
 170              return call_user_func($this->formatObjects[$this->format]["encode"],$param);
 171          }
 172          
 173  		function getOperationIncludes(){
 174              $includes = array();
 175              array_push($includes,$this->handlerPath);
 176              return $includes;
 177          }
 178          
 179      }
 180      
 181  ?>


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1