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