1 <?php
2 namespace rpf\api;
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
19 class bbRpc{
20 protected static $rpc_url = null;
21 protected static $msgs = array();
22 private static $rpc_user = null;
23 private static $rpc_pass = null;
24 private static $client = null;
25 private static $session = array();
26 private static $cookie = null;
27 private static $response = null;
28 private static $guest_token = null;
29 private static $utf8 = 0;
30 const result = 'Return';
31 const error_msg = 'Error';
32 33 34 35 36 37 38 39 40 41
42 final public static function setUrl($sUrl=null){
43 if (isset($sUrl)){
44 if (substr($sUrl, -1) !== '/') {
45 $sUrl .= '/';
46 }
47 static::$rpc_url = $sUrl;
48 }
49 }
50 51 52 53 54 55 56 57 58 59
60 final public static function setGUESTToken($sToken=null){
61 if (isset($sToken)){
62 static::$guest_token = $sToken;
63 }
64 }
65 66 67 68 69 70 71 72 73 74
75 final public static function setUTF8Native($bUTF8=0){
76 static::$utf8 = ($bUTF8) ? 1 : 0;
77 }
78 79 80 81 82 83 84 85 86 87 88
89 final public static function auth($sUser=null,$sPass=null){
90 if (!is_null($sUser)){
91 static::$rpc_user = $sUser;
92 }
93 if (!is_null($sPass)){
94 static::$rpc_pass = $sPass;
95 }
96 return static::post('auth',array(
97 'user' => static::$rpc_user,
98 'pass' => static::$rpc_pass,
99 ),'auth');
100 }
101 102 103 104 105 106 107
108 final public static function getSid(){
109 if (!array_key_exists('sid',static::$session) || !static::$session['sid']){
110 static::auth();
111 }
112 if (!array_key_exists('sid',static::$session) || !static::$session['sid']){
113 return null;
114 }
115 return static::$session['sid'];
116 }
117 118 119 120 121 122 123 124 125 126
127 final public static function setSid($sSid){
128 if (strlen($sSid)){
129 static::$session['sid'] = $sSid;
130 return 1;
131 }
132 return 0;
133 }
134 135 136 137 138 139 140 141 142 143 144 145
146 final public static function call($sMethod,$hArgs=array(),$hPlaceholders=null){
147 if (!array_key_exists('sid',static::$session) || !static::$session['sid']){
148 static::auth();
149 }
150 return static::post($sMethod,$hArgs,'call',$hPlaceholders);
151 }
152 153 154 155 156 157 158 159 160 161
162 final public static function getMessages($hParams=array()){
163 $ahMsgs = static::$msgs;
164 if ( array_key_exists('delete_previous', $hParams) && $hParams['delete_previous'] ) {
165 static::$msgs = array();
166 }
167 return $ahMsgs;
168 }
169 170 171 172 173 174 175
176 final public static function logout(){
177 return static::post('logout',null,'logout');
178 }
179 180 181 182 183 184 185 186 187 188
189 final public static function setOrder($iOeid){
190 return static::post('setOrder',array(
191 'oeid' => $iOeid,
192 ),'setOrder');
193 }
194 195 196 197 198 199
200 private static function addNotice($sMsg){
201 static::$msgs[] = array(
202 'typ' => 'notice',
203 'msg' => $sMsg,
204 );
205 }
206 private static function addError($sMsg){
207 static::$msgs[] = array(
208 'typ' => 'hard',
209 'msg' => $sMsg,
210 );
211 }
212 final private static function setOpt($sOption,$xValue){
213 if (!curl_setopt(static::$client,$sOption,$xValue)){
214 static::addError('Fehler beim Setzen der Client-Option: '.$sOption.' auf: '.$xValue);
215 return 0;
216 }
217 return 1;
218 }
219 final private static function init(){
220 if (is_null(static::$cookie)){
221 static::$cookie = (defined('BBRPC_COOKIE')) ? BBRPC_COOKIE : getcwd().'/bb.rpc.cookie';
222 }
223 if (is_null(static::$rpc_url)){
224 static::$rpc_url = BBRPC_URL;
225 }
226 if (is_null(static::$client)){
227 static::$client = curl_init();
228 static::setOpt(CURLOPT_SSL_VERIFYPEER, FALSE);
229 static::setOpt(CURLOPT_RETURNTRANSFER, TRUE);
230 static::setOpt(CURLOPT_TIMEOUT, 300);
231 static::setOpt(CURLOPT_HEADER, FALSE);
232 static::setOpt(CURLOPT_FORBID_REUSE, FALSE);
233 static::setOpt(CURLOPT_VERBOSE, FALSE);
234 static::setOpt(CURLOPT_COOKIEFILE, static::$cookie);
235 static::setOpt(CURLOPT_COOKIEJAR, static::$cookie);
236 static::setOpt(CURLOPT_HTTPHEADER, array(
237 'Connection: keep-alive',
238 'Keep-Alive: 300',
239 'Expect: ',
240 ));
241 return 1;
242 }
243 return 0;
244 }
245 final private static function isCriticalError(){
246 return (static::getResponse('IsCriticalError')) ? TRUE : FALSE;
247 }
248 final private static function getResponse($sName){
249 return (is_array(static::$response) && array_key_exists($sName,static::$response)) ? static::$response[$sName] : NULL;
250 }
251 final private static function getErrors(){
252 return static::getResponse(static::error_msg);
253 }
254 final private static function replacePlaceholder($sMsg,$hPlaceholders){
255 foreach($hPlaceholders as $sFld => $sDscr){
256 $sMsg = str_replace("\$".$sFld,$sDscr,$sMsg);
257 }
258 return $sMsg;
259 }
260 final private static function displayMessages($hPlaceholders=null){
261 $ahMsgs = static::getErrors();
262 if (is_array($ahMsgs)){
263 foreach($ahMsgs as $hMsg){
264 if (isset($hPlaceholders)){
265 $hMsg['msg'] = static::replacePlaceholder($hMsg['msg'],$hPlaceholders);
266 }
267 static::$msgs[] = $hMsg;
268 }
269 }
270 }
271 final private static function utf8_encode_data($xData){
272 if (is_array($xData)){
273 $aKeys = array_keys($xData);
274 while(($xKey = array_shift($aKeys)) !== null){
275 $xVal = static::utf8_encode_data($xData[$xKey]);
276 unset($xData[$xKey]);
277 $xKey = static::utf8_encode_data($xKey);
278 $xData[$xKey] = $xVal;
279 }
280 return $xData;
281 }elseif (is_string($xData)){
282 return utf8_encode($xData);
283 }else{
284 return $xData;
285 }
286 }
287 final private static function utf8_decode_data($xData){
288 if (is_array($xData)){
289 $aKeys = array_keys($xData);
290 while(($xKey = array_shift($aKeys)) !== null){
291 $xVal = static::utf8_decode_data($xData[$xKey]);
292 unset($xData[$xKey]);
293 $xKey = static::utf8_decode_data($xKey);
294 $xData[$xKey] = $xVal;
295 }
296 return $xData;
297 }elseif (is_string($xData)){
298 return utf8_decode($xData);
299 }elseif (is_float($xData) || is_double($xData)){
300 if (strpos((string)$xData,'.') === false && strpos((string)$xData,',') === false){
301 return (integer) $xData;
302 }else{
303 return (float) $xData;
304 }
305 }else{
306 return $xData;
307 }
308 }
309 final private static function json_encode($xData){
310 return (static::$utf8) ? json_encode($xData) : json_encode(static::utf8_encode_data($xData));
311 }
312 final private static function json_decode($xData){
313 return (static::$utf8) ? json_decode($xData,1) : static::utf8_decode_data(json_decode($xData,1));
314 }
315 final private static function post($sMethod,$xArgs=null,$sTyp='call',$hPlaceholders=null){
316 static::init();
317 $sSessionID = (array_key_exists('sid',static::$session) && static::$session['sid']) ? static::$session['sid'] : '__';
318 if ($sTyp == 'call'){
319 static::setOpt(CURLOPT_URL, static::$rpc_url.$sSessionID.'/rpc/call');
320 }elseif($sTyp == 'auth'){
321 if ((is_null($xArgs['user']) || !strlen($xArgs['user'])) && (is_null($xArgs['pass']) || !strlen($xArgs['pass']))){
322 unset($xArgs['user']);
323 unset($xArgs['pass']);
324 }
325 static::setOpt(CURLOPT_URL, static::$rpc_url.$sSessionID.'/rpc/auth');
326 }elseif($sTyp == 'setOrder'){
327 static::setOpt(CURLOPT_URL, static::$rpc_url.static::$session['sid'].'/rpc/setOrder');
328 }elseif($sTyp == 'logout'){
329 static::setOpt(CURLOPT_URL, static::$rpc_url.$sSessionID.'/rpc/logout');
330 }
331
332 $hMsg = array('method' => $sMethod);
333 if (isset(static::$guest_token)){
334 $hMsg['guest_token'] = static::$guest_token;
335 }
336 if (isset($xArgs)) {
337 $hMsg['params'] = $xArgs;
338 }
339 $sMsg = static::json_encode($hMsg);
340 static::setOpt(CURLOPT_POSTFIELDS, array(
341 'data' => $sMsg,
342 ));
343 $xResponseOrig = @curl_exec(static::$client);
344 $bTmpErr = 0;
345 if ($xResponseOrig === FALSE){
346 static::addError('Verbindung zum BB-System nicht moeglich.');
347 $bTmpErr = 1;
348 }
349 if ($xResponseOrig !== FALSE && (static::$response = static::json_decode($xResponseOrig)) === FALSE && !$bTmpErr){
350 static::addError('Ungueltiger Rueckgabewert des BB-Systems: '.$xResponseOrig);
351 echo('Error: curl_exec.response['.$sMethod.']=> '.$xResponseOrig."\n");
352 die;
353 }
354 if (static::isCriticalError()){
355 trigger_error(static::getResponse(static::result), E_USER_ERROR);
356 die;
357 }
358 if ($sTyp == 'auth'){
359 static::$session['sid'] = static::getResponse('sid');
360 }
361 static::displayMessages($hPlaceholders);
362 return static::getResponse(static::result);
363 }
364 }
365