1 | <?php |
---|
2 | /** |
---|
3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
4 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
5 | * Copyright 2001-2010 Strangecode, LLC |
---|
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 | /** |
---|
24 | * MCVE.inc.php |
---|
25 | * |
---|
26 | * The MCVE class provides functions for communicating with a MCVE server. |
---|
27 | * |
---|
28 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
29 | * @version 1.0 |
---|
30 | */ |
---|
31 | class MCVE { |
---|
32 | |
---|
33 | var $username; |
---|
34 | var $password; |
---|
35 | var $host = 'localhost'; |
---|
36 | var $ip_port = 8333; |
---|
37 | var $ssl_port = 8444; |
---|
38 | var $ca_bundle_file = '/usr/share/ssl/certs/ca-bundle.crt'; |
---|
39 | var $connect_method = 'ip'; |
---|
40 | var $timeout = 10; |
---|
41 | var $blocking = 1; |
---|
42 | var $connected = false; |
---|
43 | var $conn; |
---|
44 | |
---|
45 | function MCVE($username, $password) |
---|
46 | { |
---|
47 | $app =& App::getInstance(); |
---|
48 | |
---|
49 | // Ensure PHP was compiled with the MCVE functions. |
---|
50 | if (!extension_loaded('mcve')) { |
---|
51 | trigger_error('MCVE class instantiation failed: MCVE extension not available.', E_USER_ERROR); |
---|
52 | } |
---|
53 | if ('' == $username || '' == $password) { |
---|
54 | $app->logMsg(sprintf('Empty username or password provided.', null), LOG_ERR, __FILE__, __LINE__); |
---|
55 | } |
---|
56 | $this->username = $username; |
---|
57 | $this->password = $password; |
---|
58 | } |
---|
59 | |
---|
60 | function _connect() |
---|
61 | { |
---|
62 | $app =& App::getInstance(); |
---|
63 | |
---|
64 | if ($this->connected) { |
---|
65 | return true; |
---|
66 | } |
---|
67 | |
---|
68 | // Initialize SSL structures and definitions. |
---|
69 | MCVE_InitEngine($this->ca_bundle_file); |
---|
70 | |
---|
71 | // Allocate Connection Structures |
---|
72 | $this->conn = MCVE_InitConn(); |
---|
73 | |
---|
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)) { |
---|
78 | $app->logMsg('Could not set method to IP.', LOG_ERR, __FILE__, __LINE__); |
---|
79 | return false; |
---|
80 | } |
---|
81 | break; |
---|
82 | case 'ssl' : |
---|
83 | if (!MCVE_SetSSL($this->conn, $this->host, $this->ssl_port)) { |
---|
84 | $app->logMsg('Could not set method to IP.', LOG_ERR, __FILE__, __LINE__); |
---|
85 | return false; |
---|
86 | } |
---|
87 | break; |
---|
88 | case 'dropfile' : |
---|
89 | if (!MCVE_SetDropFile($this->conn, '/var/mcve/trans')) { |
---|
90 | $app->logMsg('Could not set method to IP.', LOG_ERR, __FILE__, __LINE__); |
---|
91 | return false; |
---|
92 | } |
---|
93 | break; |
---|
94 | default : |
---|
95 | $app->logMsg('Connection method not defined.', LOG_ERR, __FILE__, __LINE__); |
---|
96 | return false; |
---|
97 | } |
---|
98 | |
---|
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)) { |
---|
104 | $app->logMsg('Could not set blocking mode.', LOG_ERR, __FILE__, __LINE__); |
---|
105 | return false; |
---|
106 | } |
---|
107 | |
---|
108 | // Maximum of 30s per transaction allowed. Timeout occurs on server-end, not client-end |
---|
109 | if (!MCVE_SetTimeout($this->conn, $this->timeout)) { |
---|
110 | $app->logMsg('Could not set timeout.', LOG_ERR, __FILE__, __LINE__); |
---|
111 | return false; |
---|
112 | } |
---|
113 | |
---|
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); |
---|
117 | $app->logMsg("$error. Are you sure the MCVE engine is running?", LOG_ERR, __FILE__, __LINE__); |
---|
118 | return false; |
---|
119 | } |
---|
120 | |
---|
121 | $this->connected = true; |
---|
122 | return true; |
---|
123 | } |
---|
124 | |
---|
125 | function beginTrans() |
---|
126 | { |
---|
127 | if (!$this->_connect()) { |
---|
128 | return false; |
---|
129 | } |
---|
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 | } |
---|
135 | |
---|
136 | function transParam($tid, $key, $var1, $var2=null) |
---|
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 | } |
---|
143 | |
---|
144 | } |
---|
145 | |
---|
146 | function sendTrans($tid, $type='', $hide_msg=false) |
---|
147 | { |
---|
148 | $app =& App::getInstance(); |
---|
149 | |
---|
150 | // Finalize structuring of to MCVE and ready it to be sent |
---|
151 | if (!MCVE_TransSend($this->conn, $tid)) { |
---|
152 | $app->logMsg('Transaction improperly structured, possibly not enough info.', LOG_ERR, __FILE__, __LINE__); |
---|
153 | return false; |
---|
154 | } |
---|
155 | |
---|
156 | // Perform actual communication with MCVE engine. |
---|
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 | } |
---|
164 | |
---|
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); |
---|
169 | |
---|
170 | // Check to see if transaction was successful or not using a strict success/fail function |
---|
171 | if ($ret_status == MCVE_FAIL) { |
---|
172 | $app->raiseMsg(sprintf('MCVE %s failure: %s %s', $type, $ret_text, ('' == $verbiage ? '' : '(' . trim($verbiage) . ')')), MSG_WARNING, __FILE__, __LINE__); |
---|
173 | return false; |
---|
174 | } else if ($ret_status == MCVE_SUCCESS) { |
---|
175 | if (!$hide_msg) { |
---|
176 | $app->raiseMsg(sprintf(_("MCVE %s success: %s %s"), $type, $ret_text, ('' == $verbiage ? '' : '(' . trim($verbiage) . ')')), MSG_SUCCESS, __FILE__, __LINE__); |
---|
177 | } |
---|
178 | $app->logMsg(sprintf('MCVE success details. Auth: %s; Batch: %s; Item: %s; TTID: %s; AVS: %s; CV: %s.', |
---|
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)) |
---|
185 | ), LOG_INFO, __FILE__, __LINE__); |
---|
186 | return true; |
---|
187 | } else { |
---|
188 | $app->logMsg(sprintf('Transaction failed. Unknown return code: %s', $ret_status), LOG_ERR, __FILE__, __LINE__); |
---|
189 | return false; |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | function disconnect($tid) |
---|
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); |
---|
199 | |
---|
200 | // Clean up and close MCVE. |
---|
201 | MCVE_DestroyConn($this->conn); |
---|
202 | MCVE_DestroyEngine(); |
---|
203 | |
---|
204 | $this->connected = false; |
---|
205 | } |
---|
206 | } |
---|
207 | } |
---|
208 | // End of class. |
---|
209 | |
---|
210 | ?> |
---|