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

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