[1] | 1 | <?php |
---|
| 2 | /** |
---|
[362] | 3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
| 4 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
[396] | 5 | * Copyright 2001-2012 Strangecode, LLC |
---|
[362] | 6 | * |
---|
| 7 | * This file is part of The Strangecode Codebase. |
---|
| 8 | * |
---|
| 9 | * The Strangecode Codebase is free software: you can redistribute it and/or |
---|
| 10 | * modify it under the terms of the GNU General Public License as published by the |
---|
| 11 | * Free Software Foundation, either version 3 of the License, or (at your option) |
---|
| 12 | * any later version. |
---|
| 13 | * |
---|
| 14 | * The Strangecode Codebase is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
| 17 | * details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License along with |
---|
| 20 | * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | /** |
---|
[136] | 24 | * MCVE.inc.php |
---|
[1] | 25 | * |
---|
[136] | 26 | * The MCVE class provides functions for communicating with a MCVE server. |
---|
| 27 | * |
---|
[1] | 28 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 29 | * @version 1.0 |
---|
| 30 | */ |
---|
| 31 | class MCVE { |
---|
| 32 | |
---|
[468] | 33 | public $username; |
---|
| 34 | public $password; |
---|
| 35 | public $host = 'localhost'; |
---|
| 36 | public $ip_port = 8333; |
---|
| 37 | public $ssl_port = 8444; |
---|
| 38 | public $ca_bundle_file = '/usr/share/ssl/certs/ca-bundle.crt'; |
---|
| 39 | public $connect_method = 'ip'; |
---|
| 40 | public $timeout = 10; |
---|
| 41 | public $blocking = 1; |
---|
| 42 | public $connected = false; |
---|
| 43 | public $conn; |
---|
[42] | 44 | |
---|
[468] | 45 | public function __construct($username, $password) |
---|
[1] | 46 | { |
---|
[136] | 47 | $app =& App::getInstance(); |
---|
| 48 | |
---|
[1] | 49 | // Ensure PHP was compiled with the MCVE functions. |
---|
| 50 | if (!extension_loaded('mcve')) { |
---|
[116] | 51 | trigger_error('MCVE class instantiation failed: MCVE extension not available.', E_USER_ERROR); |
---|
[1] | 52 | } |
---|
[38] | 53 | if ('' == $username || '' == $password) { |
---|
[136] | 54 | $app->logMsg(sprintf('Empty username or password provided.', null), LOG_ERR, __FILE__, __LINE__); |
---|
[38] | 55 | } |
---|
[1] | 56 | $this->username = $username; |
---|
| 57 | $this->password = $password; |
---|
| 58 | } |
---|
| 59 | |
---|
[468] | 60 | private function _connect() |
---|
[1] | 61 | { |
---|
[136] | 62 | $app =& App::getInstance(); |
---|
| 63 | |
---|
[1] | 64 | if ($this->connected) { |
---|
| 65 | return true; |
---|
| 66 | } |
---|
[42] | 67 | |
---|
[1] | 68 | // Initialize SSL structures and definitions. |
---|
[268] | 69 | MCVE_InitEngine($this->ca_bundle_file); |
---|
[42] | 70 | |
---|
[1] | 71 | // Allocate Connection Structures |
---|
| 72 | $this->conn = MCVE_InitConn(); |
---|
[42] | 73 | |
---|
[1] | 74 | // Set Connection Method and Locations |
---|
| 75 | switch ($this->connect_method) { |
---|
| 76 | case 'ip' : |
---|
| 77 | if (!MCVE_SetIP($this->conn, $this->host, $this->ip_port)) { |
---|
[136] | 78 | $app->logMsg('Could not set method to IP.', LOG_ERR, __FILE__, __LINE__); |
---|
[1] | 79 | return false; |
---|
| 80 | } |
---|
| 81 | break; |
---|
| 82 | case 'ssl' : |
---|
| 83 | if (!MCVE_SetSSL($this->conn, $this->host, $this->ssl_port)) { |
---|
[383] | 84 | $app->logMsg('Could not set method to SSL.', LOG_ERR, __FILE__, __LINE__); |
---|
[1] | 85 | return false; |
---|
| 86 | } |
---|
| 87 | break; |
---|
| 88 | case 'dropfile' : |
---|
| 89 | if (!MCVE_SetDropFile($this->conn, '/var/mcve/trans')) { |
---|
[383] | 90 | $app->logMsg('Could not set method to dropfile.', LOG_ERR, __FILE__, __LINE__); |
---|
[1] | 91 | return false; |
---|
| 92 | } |
---|
| 93 | break; |
---|
| 94 | default : |
---|
[136] | 95 | $app->logMsg('Connection method not defined.', LOG_ERR, __FILE__, __LINE__); |
---|
[116] | 96 | return false; |
---|
[1] | 97 | } |
---|
[42] | 98 | |
---|
[1] | 99 | // Put connection into non-blocking mode, meaning that the client must |
---|
| 100 | // loop waiting for the transaction to complete. You should always specify |
---|
| 101 | // this function with your blocking preference as the default is currently |
---|
| 102 | // non-blocking, but future versions of php_mcve will default to blocking. |
---|
| 103 | if (!MCVE_SetBlocking($this->conn, $this->blocking)) { |
---|
[136] | 104 | $app->logMsg('Could not set blocking mode.', LOG_ERR, __FILE__, __LINE__); |
---|
[1] | 105 | return false; |
---|
| 106 | } |
---|
[42] | 107 | |
---|
[1] | 108 | // Maximum of 30s per transaction allowed. Timeout occurs on server-end, not client-end |
---|
| 109 | if (!MCVE_SetTimeout($this->conn, $this->timeout)) { |
---|
[136] | 110 | $app->logMsg('Could not set timeout.', LOG_ERR, __FILE__, __LINE__); |
---|
[1] | 111 | return false; |
---|
| 112 | } |
---|
[42] | 113 | |
---|
[1] | 114 | // Connect to MCVE, if there's an error, print the exact reason for connection failure |
---|
| 115 | if (!MCVE_Connect($this->conn)) { |
---|
| 116 | $error = MCVE_ConnectionError($this->conn); |
---|
[279] | 117 | $app->logMsg("$error. Are you sure the MCVE engine is running?", LOG_ERR, __FILE__, __LINE__); |
---|
[1] | 118 | return false; |
---|
| 119 | } |
---|
[42] | 120 | |
---|
[1] | 121 | $this->connected = true; |
---|
[116] | 122 | return true; |
---|
[1] | 123 | } |
---|
[42] | 124 | |
---|
[468] | 125 | public function beginTrans() |
---|
[1] | 126 | { |
---|
[279] | 127 | if (!$this->_connect()) { |
---|
| 128 | return false; |
---|
| 129 | } |
---|
[1] | 130 | $tid = MCVE_TransNew($this->conn); // Allocate memory for sending a transaction. |
---|
| 131 | $this->transParam($tid, MC_USERNAME, $this->username); |
---|
| 132 | $this->transParam($tid, MC_PASSWORD, $this->password); |
---|
| 133 | return $tid; |
---|
| 134 | } |
---|
[42] | 135 | |
---|
[468] | 136 | public function transParam($tid, $key, $var1, $var2=null) |
---|
[1] | 137 | { |
---|
| 138 | if (!isset($var2)) { |
---|
| 139 | return MCVE_TransParam($this->conn, $tid, $key, $var1); |
---|
| 140 | } else { |
---|
| 141 | return MCVE_TransParam($this->conn, $tid, $key, $var1, $var2); |
---|
| 142 | } |
---|
[42] | 143 | |
---|
[1] | 144 | } |
---|
[42] | 145 | |
---|
[468] | 146 | public function sendTrans($tid, $type='', $hide_msg=false) |
---|
[1] | 147 | { |
---|
[136] | 148 | $app =& App::getInstance(); |
---|
| 149 | |
---|
[1] | 150 | // Finalize structuring of to MCVE and ready it to be sent |
---|
| 151 | if (!MCVE_TransSend($this->conn, $tid)) { |
---|
[136] | 152 | $app->logMsg('Transaction improperly structured, possibly not enough info.', LOG_ERR, __FILE__, __LINE__); |
---|
[1] | 153 | return false; |
---|
| 154 | } |
---|
[42] | 155 | |
---|
| 156 | // Perform actual communication with MCVE engine. |
---|
[1] | 157 | // If blocking method is used, loop until transaction is complete |
---|
| 158 | if ($this->blocking != 1) { |
---|
| 159 | while (MCVE_CheckStatus($this->conn, $tid) != MCVE_DONE) { |
---|
| 160 | MCVE_Monitor($this->conn); // Perform communication on connection |
---|
| 161 | MCVE_uwait(10000); // Microsecond thread-safe sleep timer. Better than usleep() |
---|
| 162 | } |
---|
| 163 | } |
---|
[42] | 164 | |
---|
[1] | 165 | $ret_status = MCVE_ReturnStatus($this->conn, $tid); |
---|
| 166 | $ret_code = MCVE_ReturnCode($this->conn, $tid); |
---|
| 167 | $ret_text = mcve_text_code($ret_code); |
---|
| 168 | $verbiage = MCVE_TransactionText($this->conn, $tid); |
---|
[42] | 169 | |
---|
[1] | 170 | // Check to see if transaction was successful or not using a strict success/fail function |
---|
| 171 | if ($ret_status == MCVE_FAIL) { |
---|
[136] | 172 | $app->raiseMsg(sprintf('MCVE %s failure: %s %s', $type, $ret_text, ('' == $verbiage ? '' : '(' . trim($verbiage) . ')')), MSG_WARNING, __FILE__, __LINE__); |
---|
[1] | 173 | return false; |
---|
| 174 | } else if ($ret_status == MCVE_SUCCESS) { |
---|
| 175 | if (!$hide_msg) { |
---|
[136] | 176 | $app->raiseMsg(sprintf(_("MCVE %s success: %s %s"), $type, $ret_text, ('' == $verbiage ? '' : '(' . trim($verbiage) . ')')), MSG_SUCCESS, __FILE__, __LINE__); |
---|
[1] | 177 | } |
---|
[141] | 178 | $app->logMsg(sprintf('MCVE success details. Auth: %s; Batch: %s; Item: %s; TTID: %s; AVS: %s; CV: %s.', |
---|
[1] | 179 | MCVE_TransactionAuth($this->conn, $tid), |
---|
| 180 | MCVE_TransactionBatch($this->conn, $tid), |
---|
| 181 | MCVE_TransactionItem($this->conn, $tid), |
---|
| 182 | MCVE_TransactionID($this->conn, $tid), |
---|
| 183 | mcve_text_avs(MCVE_TransactionAVS($this->conn, $tid)), |
---|
| 184 | mcve_text_cv(MCVE_TransactionCV($this->conn, $tid)) |
---|
[201] | 185 | ), LOG_INFO, __FILE__, __LINE__); |
---|
[1] | 186 | return true; |
---|
| 187 | } else { |
---|
[136] | 188 | $app->logMsg(sprintf('Transaction failed. Unknown return code: %s', $ret_status), LOG_ERR, __FILE__, __LINE__); |
---|
[1] | 189 | return false; |
---|
| 190 | } |
---|
| 191 | } |
---|
[42] | 192 | |
---|
[468] | 193 | public function disconnect($tid) |
---|
[1] | 194 | { |
---|
| 195 | if ($this->connected) { |
---|
| 196 | // Optional, clean up memory by transaction ... This is done automatically by |
---|
| 197 | // MCVE_DestroyConn though |
---|
| 198 | MCVE_DeleteTrans($this->conn, $tid); |
---|
[42] | 199 | |
---|
[1] | 200 | // Clean up and close MCVE. |
---|
| 201 | MCVE_DestroyConn($this->conn); |
---|
| 202 | MCVE_DestroyEngine(); |
---|
[42] | 203 | |
---|
[1] | 204 | $this->connected = false; |
---|
| 205 | } |
---|
| 206 | } |
---|
| 207 | } |
---|
| 208 | // End of class. |
---|
| 209 | |
---|