source: branches/1.1dev/lib/MCVE.inc.php

Last change on this file was 708, checked in by anonymous, 4 years ago

Update class constructor method names to construct

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