* Copyright 2001-2010 Strangecode, LLC * * This file is part of The Strangecode Codebase. * * The Strangecode Codebase is free software: you can redistribute it and/or * modify it under the terms of the GNU General Public License as published by the * Free Software Foundation, either version 3 of the License, or (at your option) * any later version. * * The Strangecode Codebase is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * The Strangecode Codebase. If not, see . */ /** * MCVE.inc.php * * The MCVE class provides functions for communicating with a MCVE server. * * @author Quinn Comendant * @version 1.0 */ class MCVE { var $username; var $password; var $host = 'localhost'; var $ip_port = 8333; var $ssl_port = 8444; var $ca_bundle_file = '/usr/share/ssl/certs/ca-bundle.crt'; var $connect_method = 'ip'; var $timeout = 10; var $blocking = 1; var $connected = false; var $conn; function MCVE($username, $password) { $app =& App::getInstance(); // Ensure PHP was compiled with the MCVE functions. if (!extension_loaded('mcve')) { trigger_error('MCVE class instantiation failed: MCVE extension not available.', E_USER_ERROR); } if ('' == $username || '' == $password) { $app->logMsg(sprintf('Empty username or password provided.', null), LOG_ERR, __FILE__, __LINE__); } $this->username = $username; $this->password = $password; } function _connect() { $app =& App::getInstance(); if ($this->connected) { return true; } // Initialize SSL structures and definitions. MCVE_InitEngine($this->ca_bundle_file); // Allocate Connection Structures $this->conn = MCVE_InitConn(); // Set Connection Method and Locations switch ($this->connect_method) { case 'ip' : if (!MCVE_SetIP($this->conn, $this->host, $this->ip_port)) { $app->logMsg('Could not set method to IP.', LOG_ERR, __FILE__, __LINE__); return false; } break; case 'ssl' : if (!MCVE_SetSSL($this->conn, $this->host, $this->ssl_port)) { $app->logMsg('Could not set method to IP.', LOG_ERR, __FILE__, __LINE__); return false; } break; case 'dropfile' : if (!MCVE_SetDropFile($this->conn, '/var/mcve/trans')) { $app->logMsg('Could not set method to IP.', LOG_ERR, __FILE__, __LINE__); return false; } break; default : $app->logMsg('Connection method not defined.', LOG_ERR, __FILE__, __LINE__); return false; } // Put connection into non-blocking mode, meaning that the client must // loop waiting for the transaction to complete. You should always specify // this function with your blocking preference as the default is currently // non-blocking, but future versions of php_mcve will default to blocking. if (!MCVE_SetBlocking($this->conn, $this->blocking)) { $app->logMsg('Could not set blocking mode.', LOG_ERR, __FILE__, __LINE__); return false; } // Maximum of 30s per transaction allowed. Timeout occurs on server-end, not client-end if (!MCVE_SetTimeout($this->conn, $this->timeout)) { $app->logMsg('Could not set timeout.', LOG_ERR, __FILE__, __LINE__); return false; } // Connect to MCVE, if there's an error, print the exact reason for connection failure if (!MCVE_Connect($this->conn)) { $error = MCVE_ConnectionError($this->conn); $app->logMsg("$error. Are you sure the MCVE engine is running?", LOG_ERR, __FILE__, __LINE__); return false; } $this->connected = true; return true; } function beginTrans() { if (!$this->_connect()) { return false; } $tid = MCVE_TransNew($this->conn); // Allocate memory for sending a transaction. $this->transParam($tid, MC_USERNAME, $this->username); $this->transParam($tid, MC_PASSWORD, $this->password); return $tid; } function transParam($tid, $key, $var1, $var2=null) { if (!isset($var2)) { return MCVE_TransParam($this->conn, $tid, $key, $var1); } else { return MCVE_TransParam($this->conn, $tid, $key, $var1, $var2); } } function sendTrans($tid, $type='', $hide_msg=false) { $app =& App::getInstance(); // Finalize structuring of to MCVE and ready it to be sent if (!MCVE_TransSend($this->conn, $tid)) { $app->logMsg('Transaction improperly structured, possibly not enough info.', LOG_ERR, __FILE__, __LINE__); return false; } // Perform actual communication with MCVE engine. // If blocking method is used, loop until transaction is complete if ($this->blocking != 1) { while (MCVE_CheckStatus($this->conn, $tid) != MCVE_DONE) { MCVE_Monitor($this->conn); // Perform communication on connection MCVE_uwait(10000); // Microsecond thread-safe sleep timer. Better than usleep() } } $ret_status = MCVE_ReturnStatus($this->conn, $tid); $ret_code = MCVE_ReturnCode($this->conn, $tid); $ret_text = mcve_text_code($ret_code); $verbiage = MCVE_TransactionText($this->conn, $tid); // Check to see if transaction was successful or not using a strict success/fail function if ($ret_status == MCVE_FAIL) { $app->raiseMsg(sprintf('MCVE %s failure: %s %s', $type, $ret_text, ('' == $verbiage ? '' : '(' . trim($verbiage) . ')')), MSG_WARNING, __FILE__, __LINE__); return false; } else if ($ret_status == MCVE_SUCCESS) { if (!$hide_msg) { $app->raiseMsg(sprintf(_("MCVE %s success: %s %s"), $type, $ret_text, ('' == $verbiage ? '' : '(' . trim($verbiage) . ')')), MSG_SUCCESS, __FILE__, __LINE__); } $app->logMsg(sprintf('MCVE success details. Auth: %s; Batch: %s; Item: %s; TTID: %s; AVS: %s; CV: %s.', MCVE_TransactionAuth($this->conn, $tid), MCVE_TransactionBatch($this->conn, $tid), MCVE_TransactionItem($this->conn, $tid), MCVE_TransactionID($this->conn, $tid), mcve_text_avs(MCVE_TransactionAVS($this->conn, $tid)), mcve_text_cv(MCVE_TransactionCV($this->conn, $tid)) ), LOG_INFO, __FILE__, __LINE__); return true; } else { $app->logMsg(sprintf('Transaction failed. Unknown return code: %s', $ret_status), LOG_ERR, __FILE__, __LINE__); return false; } } function disconnect($tid) { if ($this->connected) { // Optional, clean up memory by transaction ... This is done automatically by // MCVE_DestroyConn though MCVE_DeleteTrans($this->conn, $tid); // Clean up and close MCVE. MCVE_DestroyConn($this->conn); MCVE_DestroyEngine(); $this->connected = false; } } } // End of class. ?>