File indexing completed on 2025-05-04 05:28:56
0001 <?php 0002 0003 /* 0004 * GFX 4 0005 * 0006 * support: happy.snizzo@gmail.com 0007 * website: http://trt-gfx.googlecode.com 0008 * credits: Claudio Desideri 0009 * 0010 * This software is released under the MIT License. 0011 * http://opensource.org/licenses/mit-license.php 0012 */ 0013 0014 /* 0015 * This class is used to serve as an OCS compatible client. 0016 * Takes $url and $data (as for post data) and returns an associative array 0017 * with data returned mapped in it. 0018 */ 0019 class OCSClient{ 0020 0021 private $target_server; 0022 private $login; 0023 private $password; 0024 private $uploaded_file = 0; 0025 private $postdata = 0; 0026 private $use_auth = false; 0027 private $last_raw_result = 0; 0028 private $ocs_data_fields; 0029 0030 public function __construct($srv="default"){ 0031 0032 //setting manually fields that will be parsed as multiple when 0033 //converting from xml to array 0034 $this->ocs_data_fields = array(); 0035 $this->ocs_data_fields[] = "person"; 0036 $this->ocs_data_fields[] = "content"; 0037 $this->ocs_data_fields[] = "comment"; 0038 $this->ocs_data_fields[] = "activity"; 0039 0040 if($srv=="default"){ 0041 if(isset(EConfig::$data["ocs"])){ 0042 $this->set_target_server(EConfig::$data["ocs"]["host"]); 0043 } 0044 } else { 0045 $this->set_target_server($srv); 0046 } 0047 } 0048 0049 /** 0050 * Check if string is json encoded. 0051 */ 0052 function is_json_encoded($string) { 0053 json_decode($string); 0054 return (json_last_error() == JSON_ERROR_NONE); 0055 } 0056 0057 /** 0058 * Manually set target server for ocs client. 0059 */ 0060 public function set_target_server($srv){ 0061 $this->target_server = rtrim($srv,"/")."/"; 0062 } 0063 0064 public function set_auth_info($login,$password){ 0065 $this->login = $login; 0066 $this->password = $password; 0067 $this->use_auth = true; 0068 } 0069 0070 public function generate_server(){ 0071 if($this->use_auth){ 0072 return "http://".$this->login.":".$this->password."@".$this->target_server; 0073 } else { 0074 return "http://".$this->target_server; 0075 } 0076 } 0077 0078 /** 0079 * Send a get request to the server. 0080 */ 0081 public function get($url){ 0082 $server = $this->generate_server(); 0083 $socket = new ENetworkSocket($server); 0084 $raw_data = $socket->get($url); 0085 //if result is in json format, parse json 0086 // else parse xml 0087 if($this->is_json_encoded($raw_data)){ 0088 return json_decode($raw_data); 0089 } else { 0090 $data = EXmlParser::to_array($raw_data, $this->ocs_data_fields); 0091 return $data; 0092 } 0093 } 0094 0095 public function set_upload_file($file){ 0096 $this->uploaded_file = $file; 0097 } 0098 0099 public function set_post_data($data){ 0100 $this->postdata = $data; 0101 } 0102 0103 public function get_last_raw_result(){ 0104 if(!empty($this->last_raw_result)){ 0105 return $this->last_raw_result; 0106 } else { 0107 ELog::warning("no request performed"); 0108 return false; 0109 } 0110 } 0111 0112 /** 0113 * Send a post request to the server. 0114 * $data should be provided as an associative array 0115 * following this example: 0116 * "name" => "value" 0117 */ 0118 public function post($url,$data=""){ 0119 $server = $this->generate_server(); 0120 $socket = new ENetworkSocket($server); 0121 0122 //deprecated. use OCSClient->set_post_data($data) instead 0123 if(!empty($data)){ $socket->set_post_data($data); } 0124 //use this instead 0125 if($this->postdata){ $socket->set_post_data($this->postdata); } 0126 //eventual uploaded file 0127 if($this->uploaded_file){ 0128 $socket->set_upload_file($this->uploaded_file); 0129 } 0130 0131 $raw_data = $socket->post($url); 0132 //if result is in json format, parse json 0133 // else parse xml 0134 if($this->is_json_encoded($raw_data)){ 0135 return json_decode($raw_data); 0136 } else { 0137 $this->last_raw_result = $raw_data; 0138 $data = EXmlParser::to_array($raw_data, $this->ocs_data_fields); 0139 return $data; 0140 } 0141 } 0142 0143 } 0144 0145 ?>