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