Changeset 761
- Timestamp:
- 01/15/10 10:10:35 (2 years ago)
- Files:
-
- 1 modified
-
trunk/phpbms/modules/api/apiwrapper.php (modified) (37 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/phpbms/modules/api/apiwrapper.php
r759 r761 2 2 3 3 class apiwrapper{ 4 4 5 5 /** 6 * @var string api username 6 * @var string api username 7 7 */ 8 8 var $username; 9 9 10 10 /** 11 * @var string api password 11 * @var string api password 12 12 */ 13 13 var $password; 14 14 15 15 /** 16 16 * @var string the hostname of the target script 17 17 */ 18 18 var $apiHostname; 19 19 20 20 /** 21 * @var string the url path from $apiHostname to the target script 21 * @var string the url path from $apiHostname to the target script 22 22 */ 23 23 var $apiUrl; 24 24 25 25 /** 26 * @var integer Custom port 26 * @var integer Custom port 27 27 */ 28 28 var $port = NULL; 29 29 30 30 /** 31 * @var bool Whether or not to use an ssl connection 31 * @var bool Whether or not to use an ssl connection 32 32 */ 33 33 var $secure = false; 34 34 35 35 /** 36 * @var integer size (in bytes) of each fread 36 * @var integer size (in bytes) of each fread 37 37 */ 38 38 var $chunkSize = 8192; 39 39 40 40 /** 41 * @var integer time (in seconds) of connection timeout 41 * @var integer time (in seconds) of connection timeout 42 42 */ 43 43 var $timeout = 300; 44 45 44 45 46 46 /* 47 47 * function __construct 48 * 48 * 49 49 * @param string $hostname The hostname of the target script 50 50 * @param string $urlPath The location of the target script in relation to $hostname (usually something like 'modules/api/api_json.php'). … … 54 54 * @param integer $port If there is a non-standard port (i.e. different than 80 if not using an ssl connection, and different than 443 if using an ssl connection). 55 55 */ 56 56 57 57 public function __construct($hostname, $urlPath, $username, $password, $secure = false, $port = NULL) { 58 58 59 59 $this->apiHostname = $hostname; 60 60 $this->apiUrl = $urlPath; … … 64 64 if($port === 0 || (int)$port > 0) 65 65 $this->port = $port; 66 67 }//end function 68 66 67 }//end function 68 69 69 /* 70 70 * function ping … … 75 75 * @returnf string message The related message. This will be 'Everything is phpBMSy!' if no errors have occurred and the tabledefuuid is api accessible. 76 76 */ 77 77 78 78 function ping($tabledefuuid) { 79 79 80 80 $params["request"][0]["tabledefid"] = $tabledefuuid; 81 81 $params["request"][0]["command"] = "ping"; 82 82 $params["request"][0]["data"] = array(); 83 83 84 84 $response = $this->_callServer($params); 85 85 86 86 if($response !== false) 87 87 return $response[0]; 88 88 else 89 89 return false; 90 90 91 91 }//end funciton 92 93 92 93 94 94 /* 95 95 * function insertRecords 96 * 96 * 97 97 * @param string $tabledefuuid The uuid of the tabledefinition that you wish to insert records. 98 98 * @param array $data An array of associative arrays consising of fieldname => value pairs. … … 101 101 * @param string $dateFormat The format of the dates (if any) in $data. Possible choices are : 'SQL', 'English, US', 'English, UK', or 'Dutch, NL'. If none of these are chosen, it will default to 'SQL'. 102 102 * @param string $timeFormat The format of the times (if any) in $data. Possible choices are : '24 Hour' or '12 Hour'. If none of these are chosen, it will default to '24 Hour'. 103 * 103 * 104 104 * 105 105 * @return array An array of associative arrays of responses for each insert … … 109 109 * @returnff string extras The uuid of the inserted record (or the integer id if the 'useUuid' option is false). <em>Note:</em> his field only exists if type is not 'error'. 110 110 */ 111 111 112 112 public function insertRecords($tabledefuuid, $data, $generateUuid = true, $keepDestId = true, $dateFormat = NULL, $timeFormat = NULL) { 113 113 114 114 if($generateUuid !== true) 115 115 $generateUuid = false; 116 116 117 117 if($keepDestId !== true) 118 118 $keepDestId = false; 119 119 120 120 switch($dateFormat){ 121 121 122 122 case "SQL": 123 123 case "English, US": … … 129 129 $dateFormat = "SQL"; 130 130 break; 131 131 132 132 }//end switch 133 133 134 134 switch($timeFormat){ 135 135 136 136 case "24 Hour": 137 137 case "12 Hour": … … 141 141 $timeFormat = "24 Hour"; 142 142 break; 143 143 144 144 }//end switch 145 145 146 146 $options = array( 147 147 "useUuid" => $generateUuid, … … 150 150 "keepDestId" => $keepDestId 151 151 ); 152 152 153 153 return $this->_runTableCommnad("insert", $tabledefuuid, $data, $options); 154 155 }//end function 156 157 154 155 }//end function 156 157 158 158 /* 159 159 * function updateRecords 160 * 160 * 161 161 * @param string $tabledefuuid The uuid of the tabledefinition that you wish to update records. 162 162 * @param array $data An array of associative arrays consising of fieldname => value pairs. … … 170 170 * @returnff string message The detailed message describing the result 171 171 */ 172 172 173 173 public function updateRecords($tabledefuuid, $data, $useUuid = true, $dateFormat = NULL, $timeFormat = NULL) { 174 174 175 175 if($useUuid !== true) 176 176 $useUuid = false; 177 177 178 178 switch($dateFormat){ 179 179 180 180 case "SQL": 181 181 case "English, US": … … 187 187 $dateFormat = "SQL"; 188 188 break; 189 189 190 190 }//end switch 191 191 192 192 switch($timeFormat){ 193 193 194 194 case "24 Hour": 195 195 case "12 Hour": … … 199 199 $timeFormat = "24 Hour"; 200 200 break; 201 201 202 202 }//end switch 203 203 204 204 $options = array( 205 205 "useUuid" => $useUuid, … … 207 207 "timeFormat" => $timeFormat 208 208 ); 209 209 210 210 return $this->_runTableCommnad("update", $tabledefuuid, $data, $options); 211 212 }//end function 213 214 211 212 }//end function 213 214 215 215 /* 216 216 * function getRecords 217 * 217 * 218 218 * @param string $tabledefuuid The uuid of the tabledefinition that you wish to get records. 219 219 * @param mixed $ids Either an array uuids (or ids if $useUuid = false), or an individual uuid (or id). … … 226 226 * @returnff array extras The associative array containing the record retrieved. <em>Note:</em> This field only exists if type is not 'error' AND there is a record that corresponds to the uuid/id searched for. 227 227 */ 228 228 229 229 public function getRecords($tabledefuuid, $ids, $useUuid = true) { 230 230 231 231 if($useUuid !== true) 232 232 $useUuid = false; 233 233 234 234 if($useUuid) 235 235 $keyName = "uuid"; 236 236 else 237 237 $keyName = "id"; 238 238 239 239 if(!is_array($ids)) 240 240 $ids = array($ids); 241 241 242 242 $data = array(); 243 243 foreach($ids as $id){ 244 244 245 245 if($useUuid) 246 246 $id = (string)$id; … … 248 248 $id = (int)$id; 249 249 $data[][$keyName] = $id; 250 250 251 251 }//end foreach 252 252 253 253 $options = array( 254 254 "useUuid" => $useUuid 255 255 ); 256 256 257 257 return $this->_runTableCommnad("get", $tabledefuuid, $data, $options); 258 259 }//end function 260 261 258 259 }//end function 260 261 262 262 /* 263 263 * function deleteRecords 264 * 264 * 265 265 * @param string $tabledefuuid The uuid of the tabledefinition that you wish to delete (or inactivate, depending upon the tabledef) records. 266 266 * @param mixed $ids Either an array uuids (or ids if $useUuid is false), or an individual uuid (or id). … … 272 272 * @returnff string message The detailed message describing the result 273 273 */ 274 274 275 275 public function deleteRecords($tabledefuuid, $ids, $useUuid = true) { 276 276 277 277 if($useUuid !== true) 278 278 $useUuid = false; 279 279 280 280 if($useUuid) 281 281 $keyName = "uuid"; 282 282 else 283 283 $keyName = "id"; 284 284 285 285 if(!is_array($ids)) 286 286 $ids = array($ids); 287 287 288 288 $data = array(); 289 289 foreach($ids as $id){ 290 290 291 291 if($useUuid) 292 292 $id = (string)$id; … … 294 294 $id = (int)$id; 295 295 $data[][$keyName] = $id; 296 296 297 297 }//end foreach 298 298 299 299 $options = array( 300 300 "useUuid" => $useUuid 301 301 ); 302 302 303 303 return $this->_runTableCommnad("delete", $tabledefuuid, $data, $options); 304 305 }//end function 306 307 304 305 }//end function 306 307 308 308 /* 309 309 * function runStoredProcedure … … 316 316 * @returnf string message The detailed message describing the result 317 317 */ 318 318 319 319 public function runStoredProcedure($tabledefuuid, $procedureName) { 320 320 321 321 $params["request"][0]["command"] = "procedure"; 322 322 $params["request"][0]["data"]["name"] = $procedurename; 323 323 $params["request"][0]["tabledefid"] = $tabledefuuid; 324 324 325 325 $response = $this->_callServer($params); 326 326 327 327 if($response !== false) 328 328 return $response[0]; 329 329 else 330 330 return false; 331 332 }//end function 333 334 331 332 }//end function 333 334 335 335 /* 336 336 * function getSetting 337 * 337 * 338 338 * @param array $settings Array of settings names 339 339 * … … 343 343 * @returnf array extras The associative array containing the settings retrieved. <em>Note:</em> This field only exists if type is not 'error'. 344 344 */ 345 345 346 346 public function getSettings($settings) { 347 347 348 348 $params["request"][0]["command"] = "getsettings"; 349 349 $params["request"][0]["data"] = $settings; 350 350 351 351 $response = $this->_callServer($params); 352 352 353 353 if($response !== false) 354 354 return $response[0]; 355 355 else 356 356 return false; 357 358 }//end function 359 360 357 358 }//end function 359 360 361 361 /* 362 362 * function searchClientByEmail … … 369 369 * @returnf array extras If the type is 'result', this will be a (possibly empty) array of uuids (or ids if the 'useUuid' option is false). <em>Note:</em> This field only exists if type is not 'error'. 370 370 */ 371 371 372 372 public function searchClientByEmail($email, $useUuid = true) { 373 373 374 374 $method = "api_searchByEmail"; 375 375 $tabledefuuid = "tbld:6d290174-8b73-e199-fe6c-bcf3d4b61083"; 376 376 $data["email"] = (string)$email; 377 377 378 378 if($useUuid !== true) 379 379 $useUuid = false; 380 380 381 381 $options = array( 382 "useUuid" => $useUuid 382 "useUuid" => $useUuid 383 383 ); 384 384 385 385 $response = $this->runApiMethod($method, $tabledefuuid, $data, $options); 386 386 387 387 if($response !== false) 388 388 return $response[0]; 389 389 else 390 390 return false; 391 392 }//end function 393 391 392 }//end function 393 394 394 /* 395 395 * function searchClientByNameAndPostalcode … … 404 404 * @returnf array extras If the type is 'result', this will be a (possibly empty) array of uuids (or ids if the 'useUuid' option is false). <em>Note:</em> This field only exists if type is not 'error'. 405 405 */ 406 406 407 407 public function searchClientByNameAndPostalcode($firstname, $lastname, $postalcode, $useUuid = true) { 408 408 409 409 $method = "api_searchByNameAndPostalcode"; 410 410 $tabledefuuid = "tbld:6d290174-8b73-e199-fe6c-bcf3d4b61083"; … … 412 412 $data["lastname"] = (string)$lastname; 413 413 $data["postalcode"] = (string)$postalcode; 414 414 415 415 if($useUuid !== true) 416 416 $useUuid = false; 417 417 418 418 $options = array( 419 "useUuid" => $useUuid 419 "useUuid" => $useUuid 420 420 ); 421 421 422 422 $response = $this->runApiMethod($method, $tabledefuuid, $data, $options); 423 423 424 424 if($response !== false) 425 425 return $response[0]; 426 426 else 427 427 return false; 428 429 }//end function 430 431 428 429 }//end function 430 431 432 432 /* 433 433 * function searchClientByUsernameAndPassword … … 441 441 * @returnf array extras If the type is 'result', this will be a (possibly empty) array of uuids (or ids if the 'useUuid' option is false). <em>Note:</em> This field only exists if type is not 'error'. 442 442 */ 443 443 444 444 public function searchClientByUsernameAndPassword($username, $password, $useUuid = true) { 445 445 446 446 $method = "api_searchByUsernameAndPassword"; 447 447 $tabledefuuid = "tbld:6d290174-8b73-e199-fe6c-bcf3d4b61083"; 448 448 $data["username"] = (string)$username; 449 449 $data["password"] = (string)$password; 450 450 451 451 if($useUuid !== true) 452 452 $useUuid = false; 453 453 454 454 $options = array( 455 "useUuid" => $useUuid 455 "useUuid" => $useUuid 456 456 ); 457 457 458 458 $response = $this->runApiMethod($method, $tabledefuuid, $data, $options); 459 459 460 460 if($response !== false) 461 461 return $response[0]; 462 462 else 463 463 return false; 464 465 }//end function 466 467 464 465 }//end function 466 467 468 468 /* 469 469 * function searchSalesOrdersByClientUuid … … 479 479 * @returnf array extras If the type is 'result', this will be a (possibly empty) array of uuids (or ids if the 'useUuid' option is false). <em>Note:</em> This field only exists if type is not 'error'. 480 480 */ 481 481 482 482 function searchSalesOrdersByClientUuid($clientuuid, $ordertype = NULL, $startdate = NULL, $enddate = NULL, $useUuid = true) { 483 483 484 484 $method = "api_searchByClientUuid"; 485 485 $tabledefuuid = "tbld:62fe599d-c18f-3674-9e54-b62c2d6b1883"; … … 491 491 if($enddate !== NULL) 492 492 $data["enddate"] = $enddate; 493 493 494 494 if($useUuid !== true) 495 495 $useUuid = false; 496 496 497 497 $options = array( 498 "useUuid" => $useUuid 498 "useUuid" => $useUuid 499 499 ); 500 500 501 501 $response = $this->runApiMethod($method, $tabledefuuid, $data, $options); 502 502 503 503 if($response !== false) 504 504 return $response[0]; 505 505 else 506 506 return false; 507 508 }//end function 509 510 507 508 }//end function 509 510 511 511 /* 512 512 * function getFile … … 515 515 * @return mixed The file if successful, false, if not. 516 516 */ 517 517 518 518 public function getFile($url) { 519 519 520 520 $urlInfo = parse_url($url); 521 521 522 522 $oldHost = $this->apiHostname; 523 523 $oldPort = $this->port; 524 524 $oldUrl = $this->apiUrl; 525 525 526 526 $this->apiHostname = $urlInfo["host"]; 527 527 if($port === 0 || (int)$port > 0) … … 530 530 $this->port = NULL; 531 531 $this->apiUrl = $urlInfo["path"]."?".$urlInfo["query"]; 532 532 533 533 $response = $this->_callServer(array()); 534 534 535 535 $this->apiUrl = $oldUrl; 536 536 $this->apiHostname = $oldHost; 537 537 $this->port = $oldPort; 538 538 539 539 if($response !== false) 540 540 return $response; … … 543 543 return false; 544 544 }//end if 545 546 }//end function 547 548 545 546 }//end function 547 548 549 549 /* 550 550 * function runApiMethod 551 * 551 * 552 552 * @param string $method The name of the api method to be called. This must start with 'api_'. 553 553 * @param string $tabledefuuid The uuid of the tabledefinition with the relevant api method. … … 557 557 * @return mixed Response dependent upon the api command 558 558 */ 559 559 560 560 public function runApiMethod($method, $tabledefuuid, $data, $options = NULL) { 561 562 if(substr($method, 0, 4) == "api_"){ 561 562 if(substr($method, 0, 4) != "api_"){ 563 563 564 $this->errorMessage = "The command is not a valid api method"; 564 565 return false; 566 565 567 }//end if 566 568 567 569 $params["request"][0]["tabledefid"] = $tabledefuuid; 568 570 $params["request"][0]["options"] = $options; 569 571 $params["request"][0]["command"] = $method; 570 572 $params["request"][0]["data"] = $data; 571 573 572 574 return $this->_callServer($params); 573 574 }//end function 575 576 575 576 }//end function 577 578 577 579 /* 578 580 * function runSearchMethod 579 * 581 * 580 582 * @param string $method The name of the api method to be called. This must start with 'api_'. 581 583 * @param string $tabledefuuid The uuid of the tabledefinition with the relevant api method. … … 589 591 * @returnff array extras The associative array containing the record retrieved. <em>Note:</em> This field only exists if type is 'error'. 590 592 */ 591 593 592 594 public function runSearchMethod($method, $tabledefuuid, $data, $options = NULL) { 593 595 594 596 if(substr($method, 0, 4) == "api_"){ 595 597 $this->errorMessage = "The command is not a valid search method"; 596 598 return false; 597 599 }//end if 598 600 599 601 switch($method){ 600 602 601 603 case "insert": 602 604 case "update": … … 608 610 return false; 609 611 break; 610 612 611 613 }//end switch 612 614 613 615 return $this->_runTableCommnad($method, $tabledefuuid, $data, $options); 614 615 }//end function 616 617 616 617 }//end function 618 619 618 620 /* 619 621 * function _runTableCommnad 620 * 622 * 621 623 * @param string $command The api command 622 624 * @param string $tabledefuuid The uuid of the tabledefinition with the relevant method. … … 626 628 * @return array The response from _callServer 627 629 */ 628 630 629 631 private function _runTableCommnad($command, $tabledefuuid, $records, $options) { 630 632 631 633 $params = array(); 632 634 $i = 0; … … 638 640 $i++; 639 641 }//end foreach 640 642 641 643 return $this->_callServer($params); 642 643 644 }//end function 645 646 644 645 646 }//end function 647 648 647 649 /* 648 650 * function _encode 649 * 651 * 650 652 * @param array $message Message to be encoded 651 * 653 * 652 654 * @return string Encoded message 653 655 */ 654 656 655 657 private function _encode($message) { 656 658 657 659 return json_encode($message); 658 659 }//end function 660 661 660 661 }//end function 662 663 662 664 /* 663 665 * function _decode 664 * 666 * 665 667 * @param string $response Encoded api response 666 * 668 * 667 669 * @return array Decoded api response 668 670 */ 669 671 670 672 private function _decode($response) { 671 673 672 674 return json_decode($response, true); 673 674 }//end function 675 676 675 676 }//end function 677 678 677 679 /* 678 680 * function _callServer 679 * 681 * 680 682 * @param array $params 681 * 683 * 682 684 * @return array False if major (i.e. connection) error has occurred. Otherwise, returns an array of associative arrays. 683 685 */ 684 686 685 687 private function _callServer($params) { 686 688 687 689 $params["phpbmsusername"] = $this->username; 688 690 $params["phpbmspassword"] = $this->password; 689 691 690 692 if(!isset($params["request"])) 691 693 $params["request"] = array(); 692 694 693 695 $params["request"] = $this->_encode($params["request"]); 694 695 696 697 696 698 $this->errorMessage = ""; 697 699 $this->errorCode = ""; 698 700 699 701 $post_vars = http_build_query($params); 700 702 701 703 $payload = "POST " .$this->apiUrl. " HTTP/1.0\r\n"; 702 704 $payload .= "Host: " . $this->apiHostname . "\r\n"; … … 705 707 $payload .= "Connection: close \r\n\r\n"; 706 708 $payload .= $post_vars; 707 709 708 710 ob_start(); 709 711 if ($this->secure){ 710 712 711 713 if($this->port !== NULL) 712 714 $port = $this->port; 713 715 else 714 716 $port = 443; 715 717 716 718 $sock = fsockopen("ssl://".$this->apiHostname, $port, $errno, $errstr, 30); 717 719 } else { 718 720 719 721 if($this->port !== NULL) 720 722 $port = $this->port; 721 723 else 722 724 $port = 80; 723 725 724 726 $sock = fsockopen($this->apiHostname, $port, $errno, $errstr, 30); 725 727 } … … 730 732 return false; 731 733 } 732 734 733 735 $response = ""; 734 736 fwrite($sock, $payload); … … 746 748 ob_end_clean(); 747 749 list($throw, $response) = explode("\r\n\r\n", $response, 2); 748 750 749 751 if ($info["timed_out"]) return false; 750 752 751 753 if(ini_get("magic_quotes_runtime")) $response = stripslashes($response); 752 753 754 755 754 756 $decodedResponse = $this->_decode($response); 755 757 756 758 if($response && $decodedResponse === false) { 757 759 $this->errorMessage = "Bad Response. Got This:".$response; … … 760 762 $response = $decodedResponse; 761 763 } 762 764 763 765 return $response; 764 766 765 767 }//end function --_callServer-- 766 768 767 769 }//end class 768 770