source: branches/2.0singleton/lib/MCVE.inc.php @ 132

Last change on this file since 132 was 128, checked in by scdev, 18 years ago

finished /lib folder

File size: 6.7 KB
Line 
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 */
8class 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;
21
22    function MCVE($username, $password)
23    {
24        $app =& App::getInstance();
25
26        // Ensure PHP was compiled with the MCVE functions.
27        if (!extension_loaded('mcve')) {
28            trigger_error('MCVE class instantiation failed: MCVE extension not available.', E_USER_ERROR);
29        }
30        if ('' == $username || '' == $password) {
31            $app->logMsg(sprintf('Empty username or password provided.', null), LOG_ERR, __FILE__, __LINE__);
32        }
33        $this->username = $username;
34        $this->password = $password;
35    }
36
37    function _connect()
38    {
39        $app =& App::getInstance();
40
41        if ($this->connected) {
42            return true;
43        }
44
45        // Initialize SSL structures and definitions.
46        MCVE_InitEngine($ca_bundle_file);
47
48        // Allocate Connection Structures
49        $this->conn = MCVE_InitConn();
50
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)) {
55                $app->logMsg('Could not set method to IP.', LOG_ERR, __FILE__, __LINE__);
56                return false;
57            }
58            break;
59        case 'ssl' :
60            if (!MCVE_SetSSL($this->conn, $this->host, $this->ssl_port)) {
61                $app->logMsg('Could not set method to IP.', LOG_ERR, __FILE__, __LINE__);
62                return false;
63            }
64            break;
65        case 'dropfile' :
66            if (!MCVE_SetDropFile($this->conn, '/var/mcve/trans')) {
67                $app->logMsg('Could not set method to IP.', LOG_ERR, __FILE__, __LINE__);
68                return false;
69            }
70            break;
71        default :
72            $app->logMsg('Connection method not defined.', LOG_ERR, __FILE__, __LINE__);
73            return false;
74        }
75
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)) {
81            $app->logMsg('Could not set blocking mode.', LOG_ERR, __FILE__, __LINE__);
82            return false;
83        }
84
85        // Maximum of 30s per transaction allowed. Timeout occurs on server-end, not client-end
86        if (!MCVE_SetTimeout($this->conn, $this->timeout)) {
87            $app->logMsg('Could not set timeout.', LOG_ERR, __FILE__, __LINE__);
88            return false;
89        }
90
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);
94            $app->logMsg("Connection failed: $error. Are you sure the MCVE engine is running?", LOG_ERR, __FILE__, __LINE__);
95            return false;
96        }
97
98        $this->connected = true;
99        return true;
100    }
101
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    }
110
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        }
118
119    }
120
121    function sendTrans($tid, $type='', $hide_msg=false)
122    {
123        $app =& App::getInstance();
124
125        // Finalize structuring of  to MCVE and ready it to be sent
126        if (!MCVE_TransSend($this->conn, $tid)) {
127            $app->logMsg('Transaction improperly structured, possibly not enough info.', LOG_ERR, __FILE__, __LINE__);
128            return false;
129        }
130
131        // Perform actual communication with MCVE engine.
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        }
139
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);
144
145        // Check to see if transaction was successful or not using a strict success/fail function
146        if ($ret_status == MCVE_FAIL) {
147            $app->raiseMsg(sprintf('MCVE %s failure: %s %s', $type, $ret_text, ('' == $verbiage ? '' : '(' . trim($verbiage) . ')')), MSG_WARNING, __FILE__, __LINE__);
148            return false;
149        } else if ($ret_status == MCVE_SUCCESS) {
150            if (!$hide_msg) {
151                $app->raiseMsg(sprintf(_("MCVE %s success: %s %s"), $type, $ret_text, ('' == $verbiage ? '' : '(' . trim($verbiage) . ')')), MSG_SUCCESS, __FILE__, __LINE__);
152            }
153            $app->logMsg(sprintf(_("MCVE success details. Auth: %s; Batch: %s; Item: %s; TTID: %s; AVS: %s; CV: %s."),
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 {
163            $app->logMsg(sprintf('Transaction failed. Unknown return code: %s', $ret_status), LOG_ERR, __FILE__, __LINE__);
164            return false;
165        }
166    }
167
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);
174
175            // Clean up and close MCVE.
176            MCVE_DestroyConn($this->conn);
177            MCVE_DestroyEngine();
178
179            $this->connected = false;
180        }
181    }
182}
183// End of class.
184
185?>
Note: See TracBrowser for help on using the repository browser.