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

Last change on this file since 151 was 141, checked in by scdev, 18 years ago

Q - Changed <strong> to <em> in raiseMsg() calls.

File size: 6.8 KB
RevLine 
[1]1<?php
2/**
[136]3 * MCVE.inc.php
4 * code by strangecode :: www.strangecode.com :: this document contains copyrighted information
[1]5 *
[136]6 * The MCVE class provides functions for communicating with a MCVE server.
7 *
[1]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;
[42]24
[1]25    function MCVE($username, $password)
26    {
[136]27        $app =& App::getInstance();
28
[1]29        // Ensure PHP was compiled with the MCVE functions.
30        if (!extension_loaded('mcve')) {
[116]31            trigger_error('MCVE class instantiation failed: MCVE extension not available.', E_USER_ERROR);
[1]32        }
[38]33        if ('' == $username || '' == $password) {
[136]34            $app->logMsg(sprintf('Empty username or password provided.', null), LOG_ERR, __FILE__, __LINE__);
[38]35        }
[1]36        $this->username = $username;
37        $this->password = $password;
38    }
39
40    function _connect()
41    {
[136]42        $app =& App::getInstance();
43
[1]44        if ($this->connected) {
45            return true;
46        }
[42]47
[1]48        // Initialize SSL structures and definitions.
49        MCVE_InitEngine($ca_bundle_file);
[42]50
[1]51        // Allocate Connection Structures
52        $this->conn = MCVE_InitConn();
[42]53
[1]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)) {
[136]58                $app->logMsg('Could not set method to IP.', LOG_ERR, __FILE__, __LINE__);
[1]59                return false;
60            }
61            break;
62        case 'ssl' :
63            if (!MCVE_SetSSL($this->conn, $this->host, $this->ssl_port)) {
[136]64                $app->logMsg('Could not set method to IP.', LOG_ERR, __FILE__, __LINE__);
[1]65                return false;
66            }
67            break;
68        case 'dropfile' :
69            if (!MCVE_SetDropFile($this->conn, '/var/mcve/trans')) {
[136]70                $app->logMsg('Could not set method to IP.', LOG_ERR, __FILE__, __LINE__);
[1]71                return false;
72            }
73            break;
74        default :
[136]75            $app->logMsg('Connection method not defined.', LOG_ERR, __FILE__, __LINE__);
[116]76            return false;
[1]77        }
[42]78
[1]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)) {
[136]84            $app->logMsg('Could not set blocking mode.', LOG_ERR, __FILE__, __LINE__);
[1]85            return false;
86        }
[42]87
[1]88        // Maximum of 30s per transaction allowed. Timeout occurs on server-end, not client-end
89        if (!MCVE_SetTimeout($this->conn, $this->timeout)) {
[136]90            $app->logMsg('Could not set timeout.', LOG_ERR, __FILE__, __LINE__);
[1]91            return false;
92        }
[42]93
[1]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);
[136]97            $app->logMsg("Connection failed: $error. Are you sure the MCVE engine is running?", LOG_ERR, __FILE__, __LINE__);
[1]98            return false;
99        }
[42]100
[1]101        $this->connected = true;
[116]102        return true;
[1]103    }
[42]104
[1]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    }
[42]113
[1]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        }
[42]121
[1]122    }
[42]123
[1]124    function sendTrans($tid, $type='', $hide_msg=false)
125    {
[136]126        $app =& App::getInstance();
127
[1]128        // Finalize structuring of  to MCVE and ready it to be sent
129        if (!MCVE_TransSend($this->conn, $tid)) {
[136]130            $app->logMsg('Transaction improperly structured, possibly not enough info.', LOG_ERR, __FILE__, __LINE__);
[1]131            return false;
132        }
[42]133
134        // Perform actual communication with MCVE engine.
[1]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        }
[42]142
[1]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);
[42]147
[1]148        // Check to see if transaction was successful or not using a strict success/fail function
149        if ($ret_status == MCVE_FAIL) {
[136]150            $app->raiseMsg(sprintf('MCVE %s failure: %s %s', $type, $ret_text, ('' == $verbiage ? '' : '(' . trim($verbiage) . ')')), MSG_WARNING, __FILE__, __LINE__);
[1]151            return false;
152        } else if ($ret_status == MCVE_SUCCESS) {
153            if (!$hide_msg) {
[136]154                $app->raiseMsg(sprintf(_("MCVE %s success: %s %s"), $type, $ret_text, ('' == $verbiage ? '' : '(' . trim($verbiage) . ')')), MSG_SUCCESS, __FILE__, __LINE__);
[1]155            }
[141]156            $app->logMsg(sprintf('MCVE success details. Auth: %s; Batch: %s; Item: %s; TTID: %s; AVS: %s; CV: %s.',
[1]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_DEBUG, __FILE__, __LINE__);
164            return true;
165        } else {
[136]166            $app->logMsg(sprintf('Transaction failed. Unknown return code: %s', $ret_status), LOG_ERR, __FILE__, __LINE__);
[1]167            return false;
168        }
169    }
[42]170
[1]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);
[42]177
[1]178            // Clean up and close MCVE.
179            MCVE_DestroyConn($this->conn);
180            MCVE_DestroyEngine();
[42]181
[1]182            $this->connected = false;
183        }
184    }
185}
186// End of class.
187
188?>
Note: See TracBrowser for help on using the repository browser.