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

Last change on this file since 313 was 279, checked in by quinn, 17 years ago

Added better error response when mcve engine not running.

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