source: trunk/lib/MCVE.inc.php @ 279

Last change on this file since 279 was 279, checked in by quinn, 17 years ago

Added better error response when mcve engine not running.

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