[1] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * The MCVE:: class provides functions for communicating with a MCVE server. |
---|
| 4 | * |
---|
| 5 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 6 | * @version 1.0 |
---|
| 7 | */ |
---|
| 8 | class MCVE { |
---|
| 9 | |
---|
| 10 | var $username; |
---|
| 11 | var $password; |
---|
| 12 | var $host = 'localhost'; |
---|
| 13 | var $ip_port = 8333; |
---|
| 14 | var $ssl_port = 8444; |
---|
| 15 | var $connect_method = 'ip'; |
---|
| 16 | var $timeout = 10; |
---|
| 17 | var $blocking = 1; |
---|
| 18 | var $connected = false; |
---|
| 19 | var $conn; |
---|
| 20 | |
---|
| 21 | function MCVE($username, $password) |
---|
| 22 | { |
---|
| 23 | // Ensure PHP was compiled with the MCVE functions. |
---|
| 24 | if (!extension_loaded('mcve')) { |
---|
| 25 | trigger_error('MCVE class instantiation failed: MCVE extension not available.', E_USER_WARNING); |
---|
| 26 | die; |
---|
| 27 | } |
---|
| 28 | $this->username = $username; |
---|
| 29 | $this->password = $password; |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | function _connect() |
---|
| 33 | { |
---|
| 34 | if ($this->connected) { |
---|
| 35 | return true; |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | // Initialize SSL structures and definitions. |
---|
| 39 | MCVE_InitEngine(CODE_BASE . '/config/ca-bundle.crt'); |
---|
| 40 | |
---|
| 41 | // Allocate Connection Structures |
---|
| 42 | $this->conn = MCVE_InitConn(); |
---|
| 43 | |
---|
| 44 | // Set Connection Method and Locations |
---|
| 45 | switch ($this->connect_method) { |
---|
| 46 | case 'ip' : |
---|
| 47 | if (!MCVE_SetIP($this->conn, $this->host, $this->ip_port)) { |
---|
| 48 | logMsg('Could not set method to IP.', LOG_ERR, __FILE__, __LINE__); |
---|
| 49 | return false; |
---|
| 50 | } |
---|
| 51 | break; |
---|
| 52 | case 'ssl' : |
---|
| 53 | if (!MCVE_SetSSL($this->conn, $this->host, $this->ssl_port)) { |
---|
| 54 | logMsg('Could not set method to IP.', LOG_ERR, __FILE__, __LINE__); |
---|
| 55 | return false; |
---|
| 56 | } |
---|
| 57 | break; |
---|
| 58 | case 'dropfile' : |
---|
| 59 | if (!MCVE_SetDropFile($this->conn, '/var/mcve/trans')) { |
---|
| 60 | logMsg('Could not set method to IP.', LOG_ERR, __FILE__, __LINE__); |
---|
| 61 | return false; |
---|
| 62 | } |
---|
| 63 | break; |
---|
| 64 | default : |
---|
| 65 | logMsg('Connection method not defined.', LOG_ERR, __FILE__, __LINE__); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | // Put connection into non-blocking mode, meaning that the client must |
---|
| 69 | // loop waiting for the transaction to complete. You should always specify |
---|
| 70 | // this function with your blocking preference as the default is currently |
---|
| 71 | // non-blocking, but future versions of php_mcve will default to blocking. |
---|
| 72 | if (!MCVE_SetBlocking($this->conn, $this->blocking)) { |
---|
| 73 | logMsg('Could not set blocking mode.', LOG_ERR, __FILE__, __LINE__); |
---|
| 74 | return false; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | // Maximum of 30s per transaction allowed. Timeout occurs on server-end, not client-end |
---|
| 78 | if (!MCVE_SetTimeout($this->conn, $this->timeout)) { |
---|
| 79 | logMsg('Could not set timeout.', LOG_ERR, __FILE__, __LINE__); |
---|
| 80 | return false; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | // Connect to MCVE, if there's an error, print the exact reason for connection failure |
---|
| 84 | if (!MCVE_Connect($this->conn)) { |
---|
| 85 | $error = MCVE_ConnectionError($this->conn); |
---|
| 86 | logMsg("Connection failed: $error. Are you sure the MCVE engine is running?", LOG_ERR, __FILE__, __LINE__); |
---|
| 87 | return false; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | $this->connected = true; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | function beginTrans() |
---|
| 94 | { |
---|
| 95 | $this->_connect(); |
---|
| 96 | $tid = MCVE_TransNew($this->conn); // Allocate memory for sending a transaction. |
---|
| 97 | $this->transParam($tid, MC_USERNAME, $this->username); |
---|
| 98 | $this->transParam($tid, MC_PASSWORD, $this->password); |
---|
| 99 | return $tid; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | function transParam($tid, $key, $var1, $var2=null) |
---|
| 103 | { |
---|
| 104 | if (!isset($var2)) { |
---|
| 105 | return MCVE_TransParam($this->conn, $tid, $key, $var1); |
---|
| 106 | } else { |
---|
| 107 | return MCVE_TransParam($this->conn, $tid, $key, $var1, $var2); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | function sendTrans($tid, $type='', $hide_msg=false) |
---|
| 113 | { |
---|
| 114 | // Finalize structuring of to MCVE and ready it to be sent |
---|
| 115 | if (!MCVE_TransSend($this->conn, $tid)) { |
---|
| 116 | logMsg('Transaction improperly structured, possibly not enough info.', LOG_ERR, __FILE__, __LINE__); |
---|
| 117 | return false; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | // Perform actual communication with MCVE engine. |
---|
| 121 | // If blocking method is used, loop until transaction is complete |
---|
| 122 | if ($this->blocking != 1) { |
---|
| 123 | while (MCVE_CheckStatus($this->conn, $tid) != MCVE_DONE) { |
---|
| 124 | MCVE_Monitor($this->conn); // Perform communication on connection |
---|
| 125 | MCVE_uwait(10000); // Microsecond thread-safe sleep timer. Better than usleep() |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | $ret_status = MCVE_ReturnStatus($this->conn, $tid); |
---|
| 130 | $ret_code = MCVE_ReturnCode($this->conn, $tid); |
---|
| 131 | $ret_text = mcve_text_code($ret_code); |
---|
| 132 | $verbiage = MCVE_TransactionText($this->conn, $tid); |
---|
| 133 | |
---|
| 134 | // Check to see if transaction was successful or not using a strict success/fail function |
---|
| 135 | if ($ret_status == MCVE_FAIL) { |
---|
| 136 | raiseMsg(sprintf('MCVE %s failure: %s %s', $type, $ret_text, ('' == $verbiage ? '' : '(' . trim($verbiage) . ')')), MSG_WARNING, __FILE__, __LINE__); |
---|
| 137 | return false; |
---|
| 138 | } else if ($ret_status == MCVE_SUCCESS) { |
---|
| 139 | if (!$hide_msg) { |
---|
| 140 | raiseMsg(sprintf(_("MCVE %s success: %s %s"), $type, $ret_text, ('' == $verbiage ? '' : '(' . trim($verbiage) . ')')), MSG_SUCCESS, __FILE__, __LINE__); |
---|
| 141 | } |
---|
| 142 | logMsg(sprintf(_("MCVE success details. Auth: %s; Batch: %s; Item: %s; TTID: %s; AVS: %s; CV: %s."), |
---|
| 143 | MCVE_TransactionAuth($this->conn, $tid), |
---|
| 144 | MCVE_TransactionBatch($this->conn, $tid), |
---|
| 145 | MCVE_TransactionItem($this->conn, $tid), |
---|
| 146 | MCVE_TransactionID($this->conn, $tid), |
---|
| 147 | mcve_text_avs(MCVE_TransactionAVS($this->conn, $tid)), |
---|
| 148 | mcve_text_cv(MCVE_TransactionCV($this->conn, $tid)) |
---|
| 149 | ), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 150 | return true; |
---|
| 151 | } else { |
---|
| 152 | logMsg(sprintf('Transaction failed. Unknown return code: %s', $ret_status), LOG_ERR, __FILE__, __LINE__); |
---|
| 153 | return false; |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | function disconnect($tid) |
---|
| 158 | { |
---|
| 159 | if ($this->connected) { |
---|
| 160 | // Optional, clean up memory by transaction ... This is done automatically by |
---|
| 161 | // MCVE_DestroyConn though |
---|
| 162 | MCVE_DeleteTrans($this->conn, $tid); |
---|
| 163 | |
---|
| 164 | // Clean up and close MCVE. |
---|
| 165 | MCVE_DestroyConn($this->conn); |
---|
| 166 | MCVE_DestroyEngine(); |
---|
| 167 | |
---|
| 168 | $this->connected = false; |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | // End of class. |
---|
| 173 | |
---|
| 174 | ?> |
---|