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

Last change on this file since 260 was 201, checked in by scdev, 18 years ago

Q - increased some LOG_DEBUG messages to LOG_INFO so we can run with debugging off and still get the important ones.

File size: 6.8 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($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("Connection failed: $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        $this->_connect();
108        $tid = MCVE_TransNew($this->conn); // Allocate memory for sending a transaction.
109        $this->transParam($tid, MC_USERNAME, $this->username);
110        $this->transParam($tid, MC_PASSWORD, $this->password);
111        return $tid;
112    }
113
114    function transParam($tid, $key, $var1, $var2=null)
115    {
116        if (!isset($var2)) {
117            return MCVE_TransParam($this->conn, $tid, $key, $var1);
118        } else {
119            return MCVE_TransParam($this->conn, $tid, $key, $var1, $var2);
120        }
121
122    }
123
124    function sendTrans($tid, $type='', $hide_msg=false)
125    {
126        $app =& App::getInstance();
127
128        // Finalize structuring of  to MCVE and ready it to be sent
129        if (!MCVE_TransSend($this->conn, $tid)) {
130            $app->logMsg('Transaction improperly structured, possibly not enough info.', LOG_ERR, __FILE__, __LINE__);
131            return false;
132        }
133
134        // Perform actual communication with MCVE engine.
135        // If blocking method is used, loop until transaction is complete
136        if ($this->blocking != 1) {
137            while (MCVE_CheckStatus($this->conn, $tid) != MCVE_DONE) {
138                MCVE_Monitor($this->conn); // Perform communication on connection
139                MCVE_uwait(10000); // Microsecond thread-safe sleep timer. Better than usleep()
140            }
141        }
142
143        $ret_status = MCVE_ReturnStatus($this->conn, $tid);
144        $ret_code = MCVE_ReturnCode($this->conn, $tid);
145        $ret_text = mcve_text_code($ret_code);
146        $verbiage = MCVE_TransactionText($this->conn, $tid);
147
148        // Check to see if transaction was successful or not using a strict success/fail function
149        if ($ret_status == MCVE_FAIL) {
150            $app->raiseMsg(sprintf('MCVE %s failure: %s %s', $type, $ret_text, ('' == $verbiage ? '' : '(' . trim($verbiage) . ')')), MSG_WARNING, __FILE__, __LINE__);
151            return false;
152        } else if ($ret_status == MCVE_SUCCESS) {
153            if (!$hide_msg) {
154                $app->raiseMsg(sprintf(_("MCVE %s success: %s %s"), $type, $ret_text, ('' == $verbiage ? '' : '(' . trim($verbiage) . ')')), MSG_SUCCESS, __FILE__, __LINE__);
155            }
156            $app->logMsg(sprintf('MCVE success details. Auth: %s; Batch: %s; Item: %s; TTID: %s; AVS: %s; CV: %s.',
157                MCVE_TransactionAuth($this->conn, $tid),
158                MCVE_TransactionBatch($this->conn, $tid),
159                MCVE_TransactionItem($this->conn, $tid),
160                MCVE_TransactionID($this->conn, $tid),
161                mcve_text_avs(MCVE_TransactionAVS($this->conn, $tid)),
162                mcve_text_cv(MCVE_TransactionCV($this->conn, $tid))
163            ), LOG_INFO, __FILE__, __LINE__);
164            return true;
165        } else {
166            $app->logMsg(sprintf('Transaction failed. Unknown return code: %s', $ret_status), LOG_ERR, __FILE__, __LINE__);
167            return false;
168        }
169    }
170
171    function disconnect($tid)
172    {
173        if ($this->connected) {
174            // Optional, clean up memory by transaction ... This is done automatically by
175            // MCVE_DestroyConn though
176            MCVE_DeleteTrans($this->conn, $tid);
177
178            // Clean up and close MCVE.
179            MCVE_DestroyConn($this->conn);
180            MCVE_DestroyEngine();
181
182            $this->connected = false;
183        }
184    }
185}
186// End of class.
187
188?>
Note: See TracBrowser for help on using the repository browser.