phpBMS

Changeset 761

Show
Ignore:
Timestamp:
01/15/10 10:10:35 (2 years ago)
Author:
brieb
Message:
  • apiApiMethod had wrong logic for api_mehtod checking
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/phpbms/modules/api/apiwrapper.php

    r759 r761  
    22 
    33class apiwrapper{ 
    4      
     4 
    55    /** 
    6       *  @var string api username  
     6      *  @var string api username 
    77      */ 
    88    var $username; 
    9      
     9 
    1010    /** 
    11       *  @var string api password  
     11      *  @var string api password 
    1212      */ 
    1313    var $password; 
    14      
     14 
    1515    /** 
    1616      *  @var string the hostname of the target script 
    1717      */ 
    1818    var $apiHostname; 
    19      
     19 
    2020    /** 
    21       *  @var string the url path from $apiHostname to the target script  
     21      *  @var string the url path from $apiHostname to the target script 
    2222      */ 
    2323    var $apiUrl; 
    24      
     24 
    2525    /** 
    26       *  @var integer Custom port  
     26      *  @var integer Custom port 
    2727      */ 
    2828    var $port = NULL; 
    29      
     29 
    3030    /** 
    31       *  @var bool Whether or not to use an ssl connection  
     31      *  @var bool Whether or not to use an ssl connection 
    3232      */ 
    3333    var $secure = false; 
    34      
     34 
    3535    /** 
    36       *  @var integer size (in bytes) of each fread  
     36      *  @var integer size (in bytes) of each fread 
    3737      */ 
    3838    var $chunkSize = 8192; 
    39      
     39 
    4040    /** 
    41       *  @var integer time (in seconds) of connection timeout  
     41      *  @var integer time (in seconds) of connection timeout 
    4242      */ 
    4343    var $timeout = 300; 
    44      
    45      
     44 
     45 
    4646    /* 
    4747     * function __construct 
    48      *  
     48     * 
    4949     * @param string $hostname The hostname of the target script 
    5050     * @param string $urlPath The location of the target script in relation to $hostname (usually something like 'modules/api/api_json.php'). 
     
    5454     * @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). 
    5555     */ 
    56      
     56 
    5757    public function __construct($hostname, $urlPath, $username, $password, $secure = false, $port = NULL) { 
    58          
     58 
    5959        $this->apiHostname = $hostname; 
    6060        $this->apiUrl = $urlPath; 
     
    6464        if($port === 0 || (int)$port > 0) 
    6565            $this->port = $port; 
    66          
    67     }//end function 
    68      
     66 
     67    }//end function 
     68 
    6969    /* 
    7070     * function ping 
     
    7575     * @returnf string message The related message.  This will be 'Everything is phpBMSy!' if no errors have occurred and the tabledefuuid is api accessible. 
    7676     */ 
    77      
     77 
    7878    function ping($tabledefuuid) { 
    79          
     79 
    8080        $params["request"][0]["tabledefid"] = $tabledefuuid; 
    8181        $params["request"][0]["command"] = "ping"; 
    8282        $params["request"][0]["data"] = array(); 
    83          
     83 
    8484        $response = $this->_callServer($params); 
    85          
     85 
    8686        if($response !== false) 
    8787            return $response[0]; 
    8888        else 
    8989            return false; 
    90          
     90 
    9191    }//end funciton 
    92      
    93      
     92 
     93 
    9494    /* 
    9595     * function insertRecords 
    96      *  
     96     * 
    9797     * @param string $tabledefuuid The uuid of the tabledefinition that you wish to insert records. 
    9898     * @param array $data An array of associative arrays consising of fieldname => value pairs. 
     
    101101     * @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'. 
    102102     * @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     * 
    104104     * 
    105105     * @return array An array of associative arrays of responses for each insert 
     
    109109     * @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'. 
    110110     */ 
    111      
     111 
    112112    public function insertRecords($tabledefuuid, $data, $generateUuid = true, $keepDestId = true, $dateFormat = NULL, $timeFormat = NULL) { 
    113          
     113 
    114114        if($generateUuid !== true) 
    115115            $generateUuid = false; 
    116          
     116 
    117117        if($keepDestId !== true) 
    118118            $keepDestId = false; 
    119              
     119 
    120120        switch($dateFormat){ 
    121              
     121 
    122122            case "SQL": 
    123123            case "English, US": 
     
    129129                $dateFormat = "SQL"; 
    130130            break; 
    131          
     131 
    132132        }//end switch 
    133          
     133 
    134134        switch($timeFormat){ 
    135              
     135 
    136136            case "24 Hour": 
    137137            case "12 Hour": 
     
    141141                $timeFormat = "24 Hour"; 
    142142            break; 
    143          
     143 
    144144        }//end switch 
    145          
     145 
    146146        $options = array( 
    147147                    "useUuid" => $generateUuid, 
     
    150150                    "keepDestId" => $keepDestId 
    151151                    ); 
    152          
     152 
    153153        return $this->_runTableCommnad("insert", $tabledefuuid, $data, $options); 
    154          
    155     }//end function 
    156      
    157      
     154 
     155    }//end function 
     156 
     157 
    158158    /* 
    159159     * function updateRecords 
    160      *  
     160     * 
    161161     * @param string $tabledefuuid The uuid of the tabledefinition that you wish to update records. 
    162162     * @param array $data An array of associative arrays consising of fieldname => value pairs. 
     
    170170     * @returnff string message The detailed message describing the result 
    171171     */ 
    172      
     172 
    173173    public function updateRecords($tabledefuuid, $data, $useUuid = true, $dateFormat = NULL, $timeFormat = NULL) { 
    174          
     174 
    175175        if($useUuid !== true) 
    176176            $useUuid = false; 
    177          
     177 
    178178        switch($dateFormat){ 
    179              
     179 
    180180            case "SQL": 
    181181            case "English, US": 
     
    187187                $dateFormat = "SQL"; 
    188188            break; 
    189          
     189 
    190190        }//end switch 
    191          
     191 
    192192        switch($timeFormat){ 
    193              
     193 
    194194            case "24 Hour": 
    195195            case "12 Hour": 
     
    199199                $timeFormat = "24 Hour"; 
    200200            break; 
    201          
     201 
    202202        }//end switch 
    203          
     203 
    204204        $options = array( 
    205205                    "useUuid" => $useUuid, 
     
    207207                    "timeFormat" => $timeFormat 
    208208                    ); 
    209          
     209 
    210210        return $this->_runTableCommnad("update", $tabledefuuid, $data, $options); 
    211          
    212     }//end function 
    213      
    214      
     211 
     212    }//end function 
     213 
     214 
    215215    /* 
    216216     * function getRecords 
    217      *  
     217     * 
    218218     * @param string $tabledefuuid The uuid of the tabledefinition that you wish to get records. 
    219219     * @param mixed $ids Either an array uuids (or ids if $useUuid = false), or an individual uuid (or id). 
     
    226226     * @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. 
    227227     */ 
    228      
     228 
    229229    public function getRecords($tabledefuuid, $ids, $useUuid = true) { 
    230          
     230 
    231231        if($useUuid !== true) 
    232232            $useUuid = false; 
    233          
     233 
    234234        if($useUuid) 
    235235            $keyName = "uuid"; 
    236236        else 
    237237            $keyName = "id"; 
    238              
     238 
    239239        if(!is_array($ids)) 
    240240            $ids = array($ids); 
    241          
     241 
    242242        $data = array(); 
    243243        foreach($ids as $id){ 
    244              
     244 
    245245            if($useUuid) 
    246246                $id = (string)$id; 
     
    248248                $id = (int)$id; 
    249249            $data[][$keyName] = $id; 
    250              
     250 
    251251        }//end foreach 
    252          
     252 
    253253        $options = array( 
    254254                         "useUuid" => $useUuid 
    255255                        ); 
    256          
     256 
    257257        return $this->_runTableCommnad("get", $tabledefuuid, $data, $options); 
    258          
    259     }//end function 
    260      
    261      
     258 
     259    }//end function 
     260 
     261 
    262262    /* 
    263263     * function deleteRecords 
    264      *  
     264     * 
    265265     * @param string $tabledefuuid The uuid of the tabledefinition that you wish to delete (or inactivate, depending upon the tabledef) records. 
    266266     * @param mixed $ids Either an array uuids (or ids if $useUuid is false), or an individual uuid (or id). 
     
    272272     * @returnff string message The detailed message describing the result 
    273273     */ 
    274      
     274 
    275275    public function deleteRecords($tabledefuuid, $ids, $useUuid = true) { 
    276          
     276 
    277277        if($useUuid !== true) 
    278278            $useUuid = false; 
    279          
     279 
    280280        if($useUuid) 
    281281            $keyName = "uuid"; 
    282282        else 
    283283            $keyName = "id"; 
    284              
     284 
    285285        if(!is_array($ids)) 
    286286            $ids = array($ids); 
    287          
     287 
    288288        $data = array(); 
    289289        foreach($ids as $id){ 
    290              
     290 
    291291            if($useUuid) 
    292292                $id = (string)$id; 
     
    294294                $id = (int)$id; 
    295295            $data[][$keyName] = $id; 
    296              
     296 
    297297        }//end foreach 
    298          
     298 
    299299        $options = array( 
    300300                         "useUuid" => $useUuid 
    301301                        ); 
    302          
     302 
    303303        return $this->_runTableCommnad("delete", $tabledefuuid, $data, $options); 
    304          
    305     }//end function 
    306      
    307      
     304 
     305    }//end function 
     306 
     307 
    308308    /* 
    309309     * function runStoredProcedure 
     
    316316     * @returnf string message The detailed message describing the result 
    317317     */ 
    318      
     318 
    319319    public function runStoredProcedure($tabledefuuid, $procedureName) { 
    320          
     320 
    321321        $params["request"][0]["command"] = "procedure"; 
    322322        $params["request"][0]["data"]["name"] = $procedurename; 
    323323        $params["request"][0]["tabledefid"] = $tabledefuuid; 
    324          
     324 
    325325        $response = $this->_callServer($params); 
    326          
     326 
    327327        if($response !== false) 
    328328            return $response[0]; 
    329329        else 
    330330            return false; 
    331          
    332     }//end function 
    333      
    334      
     331 
     332    }//end function 
     333 
     334 
    335335    /* 
    336336     * function getSetting 
    337      *  
     337     * 
    338338     * @param array $settings Array of settings names 
    339339     * 
     
    343343     * @returnf array extras The associative array containing the settings retrieved.  <em>Note:</em> This field only exists if type is not 'error'. 
    344344     */ 
    345      
     345 
    346346    public function getSettings($settings) { 
    347          
     347 
    348348        $params["request"][0]["command"] = "getsettings"; 
    349349        $params["request"][0]["data"] = $settings; 
    350          
     350 
    351351        $response = $this->_callServer($params); 
    352          
     352 
    353353        if($response !== false) 
    354354            return $response[0]; 
    355355        else 
    356356            return false; 
    357          
    358     }//end function 
    359      
    360      
     357 
     358    }//end function 
     359 
     360 
    361361    /* 
    362362     * function searchClientByEmail 
     
    369369     * @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'. 
    370370     */ 
    371      
     371 
    372372    public function searchClientByEmail($email, $useUuid = true) { 
    373          
     373 
    374374        $method = "api_searchByEmail"; 
    375375        $tabledefuuid = "tbld:6d290174-8b73-e199-fe6c-bcf3d4b61083"; 
    376376        $data["email"] = (string)$email; 
    377          
     377 
    378378        if($useUuid !== true) 
    379379            $useUuid = false; 
    380          
     380 
    381381        $options = array( 
    382                           "useUuid" => $useUuid   
     382                          "useUuid" => $useUuid 
    383383                        ); 
    384          
     384 
    385385        $response = $this->runApiMethod($method, $tabledefuuid, $data, $options); 
    386          
     386 
    387387        if($response !== false) 
    388388            return $response[0]; 
    389389        else 
    390390            return false; 
    391          
    392     }//end function 
    393      
     391 
     392    }//end function 
     393 
    394394    /* 
    395395     * function searchClientByNameAndPostalcode 
     
    404404     * @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'. 
    405405     */ 
    406      
     406 
    407407    public function searchClientByNameAndPostalcode($firstname, $lastname, $postalcode, $useUuid = true) { 
    408          
     408 
    409409        $method = "api_searchByNameAndPostalcode"; 
    410410        $tabledefuuid = "tbld:6d290174-8b73-e199-fe6c-bcf3d4b61083"; 
     
    412412        $data["lastname"] = (string)$lastname; 
    413413        $data["postalcode"] = (string)$postalcode; 
    414          
     414 
    415415        if($useUuid !== true) 
    416416            $useUuid = false; 
    417          
     417 
    418418        $options = array( 
    419                           "useUuid" => $useUuid   
     419                          "useUuid" => $useUuid 
    420420                        ); 
    421          
     421 
    422422        $response = $this->runApiMethod($method, $tabledefuuid, $data, $options); 
    423          
     423 
    424424        if($response !== false) 
    425425            return $response[0]; 
    426426        else 
    427427            return false; 
    428          
    429     }//end function 
    430      
    431      
     428 
     429    }//end function 
     430 
     431 
    432432    /* 
    433433     * function searchClientByUsernameAndPassword 
     
    441441     * @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'. 
    442442     */ 
    443      
     443 
    444444    public function searchClientByUsernameAndPassword($username, $password, $useUuid = true) { 
    445          
     445 
    446446        $method = "api_searchByUsernameAndPassword"; 
    447447        $tabledefuuid = "tbld:6d290174-8b73-e199-fe6c-bcf3d4b61083"; 
    448448        $data["username"] = (string)$username; 
    449449        $data["password"] = (string)$password; 
    450          
     450 
    451451        if($useUuid !== true) 
    452452            $useUuid = false; 
    453          
     453 
    454454        $options = array( 
    455                           "useUuid" => $useUuid   
     455                          "useUuid" => $useUuid 
    456456                        ); 
    457          
     457 
    458458        $response = $this->runApiMethod($method, $tabledefuuid, $data, $options); 
    459          
     459 
    460460        if($response !== false) 
    461461            return $response[0]; 
    462462        else 
    463463            return false; 
    464          
    465     }//end function 
    466      
    467      
     464 
     465    }//end function 
     466 
     467 
    468468    /* 
    469469     * function searchSalesOrdersByClientUuid 
     
    479479     * @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'. 
    480480     */ 
    481      
     481 
    482482    function searchSalesOrdersByClientUuid($clientuuid, $ordertype = NULL, $startdate = NULL, $enddate = NULL, $useUuid = true) { 
    483          
     483 
    484484        $method = "api_searchByClientUuid"; 
    485485        $tabledefuuid = "tbld:62fe599d-c18f-3674-9e54-b62c2d6b1883"; 
     
    491491        if($enddate !== NULL) 
    492492            $data["enddate"] = $enddate; 
    493              
     493 
    494494        if($useUuid !== true) 
    495495            $useUuid = false; 
    496          
     496 
    497497        $options = array( 
    498                           "useUuid" => $useUuid   
     498                          "useUuid" => $useUuid 
    499499                        ); 
    500              
     500 
    501501        $response = $this->runApiMethod($method, $tabledefuuid, $data, $options); 
    502          
     502 
    503503        if($response !== false) 
    504504            return $response[0]; 
    505505        else 
    506506            return false; 
    507          
    508     }//end function 
    509      
    510      
     507 
     508    }//end function 
     509 
     510 
    511511    /* 
    512512     * function getFile 
     
    515515     * @return mixed The file if successful, false, if not. 
    516516     */ 
    517      
     517 
    518518    public function getFile($url) { 
    519          
     519 
    520520        $urlInfo = parse_url($url); 
    521          
     521 
    522522        $oldHost = $this->apiHostname; 
    523523        $oldPort = $this->port; 
    524524        $oldUrl = $this->apiUrl; 
    525          
     525 
    526526        $this->apiHostname = $urlInfo["host"]; 
    527527        if($port === 0 || (int)$port > 0) 
     
    530530            $this->port = NULL; 
    531531        $this->apiUrl = $urlInfo["path"]."?".$urlInfo["query"]; 
    532          
     532 
    533533        $response = $this->_callServer(array()); 
    534          
     534 
    535535        $this->apiUrl = $oldUrl; 
    536536        $this->apiHostname = $oldHost; 
    537537        $this->port = $oldPort; 
    538          
     538 
    539539        if($response !== false) 
    540540            return $response; 
     
    543543            return false; 
    544544        }//end if 
    545          
    546     }//end function 
    547      
    548      
     545 
     546    }//end function 
     547 
     548 
    549549    /* 
    550550     * function runApiMethod 
    551      *  
     551     * 
    552552     * @param string $method The name of the api method to be called.  This must start with 'api_'. 
    553553     * @param string $tabledefuuid The uuid of the tabledefinition with the relevant api method. 
     
    557557     * @return mixed Response dependent upon the api command 
    558558     */ 
    559      
     559 
    560560    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 
    563564            $this->errorMessage = "The command is not a valid api method"; 
    564565            return false; 
     566 
    565567        }//end if 
    566          
     568 
    567569        $params["request"][0]["tabledefid"] = $tabledefuuid; 
    568570        $params["request"][0]["options"] = $options; 
    569571        $params["request"][0]["command"] = $method; 
    570572        $params["request"][0]["data"] = $data; 
    571          
     573 
    572574        return $this->_callServer($params); 
    573          
    574     }//end function 
    575      
    576      
     575 
     576    }//end function 
     577 
     578 
    577579    /* 
    578580     * function runSearchMethod 
    579      *  
     581     * 
    580582     * @param string $method The name of the api method to be called.  This must start with 'api_'. 
    581583     * @param string $tabledefuuid The uuid of the tabledefinition with the relevant api method. 
     
    589591     * @returnff array extras The associative array containing the record retrieved.  <em>Note:</em> This field only exists if type is 'error'. 
    590592     */ 
    591      
     593 
    592594    public function runSearchMethod($method, $tabledefuuid, $data, $options = NULL) { 
    593          
     595 
    594596        if(substr($method, 0, 4) == "api_"){ 
    595597            $this->errorMessage = "The command is not a valid search method"; 
    596598            return false; 
    597599        }//end if 
    598          
     600 
    599601        switch($method){ 
    600              
     602 
    601603            case "insert": 
    602604            case "update": 
     
    608610                return false; 
    609611                break; 
    610              
     612 
    611613        }//end switch 
    612          
     614 
    613615        return $this->_runTableCommnad($method, $tabledefuuid, $data, $options); 
    614          
    615     }//end function 
    616      
    617      
     616 
     617    }//end function 
     618 
     619 
    618620    /* 
    619621     * function _runTableCommnad 
    620      *  
     622     * 
    621623     * @param string $command The api command 
    622624     * @param string $tabledefuuid The uuid of the tabledefinition with the relevant method. 
     
    626628     * @return array The response from _callServer 
    627629     */ 
    628      
     630 
    629631    private function _runTableCommnad($command, $tabledefuuid, $records, $options) { 
    630          
     632 
    631633        $params = array(); 
    632634        $i = 0; 
     
    638640            $i++; 
    639641        }//end foreach 
    640          
     642 
    641643        return $this->_callServer($params); 
    642          
    643          
    644     }//end function 
    645      
    646      
     644 
     645 
     646    }//end function 
     647 
     648 
    647649    /* 
    648650     * function _encode 
    649      *  
     651     * 
    650652     * @param array $message Message to be encoded 
    651      *  
     653     * 
    652654     * @return string Encoded message 
    653655     */ 
    654      
     656 
    655657    private function _encode($message) { 
    656          
     658 
    657659        return json_encode($message); 
    658          
    659     }//end function 
    660      
    661      
     660 
     661    }//end function 
     662 
     663 
    662664    /* 
    663665     * function _decode 
    664      *  
     666     * 
    665667     * @param string $response Encoded api response 
    666      *  
     668     * 
    667669     * @return array Decoded api response 
    668670     */ 
    669      
     671 
    670672    private function _decode($response) { 
    671          
     673 
    672674        return json_decode($response, true); 
    673          
    674     }//end function 
    675      
    676      
     675 
     676    }//end function 
     677 
     678 
    677679    /* 
    678680     * function _callServer 
    679      *  
     681     * 
    680682     * @param array $params 
    681      *  
     683     * 
    682684     * @return array False if major (i.e. connection) error has occurred.  Otherwise, returns an array of associative arrays. 
    683685     */ 
    684      
     686 
    685687    private function _callServer($params) { 
    686          
     688 
    687689        $params["phpbmsusername"] = $this->username; 
    688690        $params["phpbmspassword"] = $this->password; 
    689          
     691 
    690692        if(!isset($params["request"])) 
    691693            $params["request"] = array(); 
    692          
     694 
    693695        $params["request"] = $this->_encode($params["request"]); 
    694              
    695          
     696 
     697 
    696698        $this->errorMessage = ""; 
    697699        $this->errorCode = ""; 
    698          
     700 
    699701        $post_vars = http_build_query($params); 
    700          
     702 
    701703        $payload = "POST " .$this->apiUrl. " HTTP/1.0\r\n"; 
    702704        $payload .= "Host: " . $this->apiHostname . "\r\n"; 
     
    705707        $payload .= "Connection: close \r\n\r\n"; 
    706708        $payload .= $post_vars; 
    707          
     709 
    708710        ob_start(); 
    709711        if ($this->secure){ 
    710              
     712 
    711713            if($this->port !== NULL) 
    712714                $port = $this->port; 
    713715            else 
    714716                $port = 443; 
    715              
     717 
    716718            $sock = fsockopen("ssl://".$this->apiHostname, $port, $errno, $errstr, 30); 
    717719        } else { 
    718              
     720 
    719721            if($this->port !== NULL) 
    720722                $port = $this->port; 
    721723            else 
    722724                $port = 80; 
    723              
     725 
    724726            $sock = fsockopen($this->apiHostname, $port, $errno, $errstr, 30); 
    725727        } 
     
    730732            return false; 
    731733        } 
    732          
     734 
    733735        $response = ""; 
    734736        fwrite($sock, $payload); 
     
    746748        ob_end_clean(); 
    747749        list($throw, $response) = explode("\r\n\r\n", $response, 2); 
    748          
     750 
    749751        if ($info["timed_out"]) return false; 
    750          
     752 
    751753        if(ini_get("magic_quotes_runtime")) $response = stripslashes($response); 
    752          
    753          
     754 
     755 
    754756        $decodedResponse = $this->_decode($response); 
    755              
     757 
    756758        if($response && $decodedResponse === false) { 
    757759            $this->errorMessage = "Bad Response. Got This:".$response; 
     
    760762            $response = $decodedResponse; 
    761763        } 
    762          
     764 
    763765        return $response; 
    764          
     766 
    765767    }//end function --_callServer-- 
    766      
     768 
    767769}//end class 
    768770 
Scanned by Orvant Copyright © 2010 Kreotek, LLC. All Rights reserved.