Location: PHPKode > scripts > e-Gold > e-gold/e-gold.class.php
<?php


/**
 * Class to manage e-Gold Account 
 * 
 * Author: Pawel Banasiak {@link http://banasiak.net}
 * 
 * Thanks to wlk ({@link http://krzeslo.net})
 * 
 * YOU USE THIS SCRIPT AT YOUR OWN RESPONSIBILITY AND AT YOUR OWN RISK
 *  
 * @package e-gold
 * @author Pawel Banasiak <hide@address.com>
 * @copyright Pawel Banasiak 2007
 * @license BSD
 * @version 1.0.0
 */

class egold {
	/**
	 * @var int Connection timeout
	 */
	public static $connection_timeout = 15;
	/**
	 * @var string Connection type - 'curl' or 'fsockopen' (fsockopen is not safety!)
	 */
	public static $connection_type = 'curl';
	private static $url_history = '/acct/historycsv.asp';
	private static $url_balance = '/acct/balance.asp';
	private static $url_verify = '/acct/verify.asp';
	private static $url_spend = '/acct/confirm.asp';
	private static $url_metaldata = '/unsecure/metaldata.asp';
	private static $urlserver = 'www.e-gold.com';
	private static $history_datas_default = array (
		'paymentsmade' => 1,
		'paymentsreceived' => 1,
		'redemptions' => 1,
		'fees' => 1
	);
	private static $EOL = "\r\n";
	private static function connect($type, $params, $secure = true) {
		$urlvar = 'url_' . $type;
		$params2 = '';
		if (is_array($params)) {
			foreach ($params as $key => $val) {
				$params2 .= $key . '=' . $val . '&';
			}
			$params2 = substr($params2, 0, strlen($params2) - 1);
		} else
			$params2 = $params;
		switch (self :: $connection_type) {
			case 'curl' :
				try {
					$ch = curl_init();
					if ($ch === false)
						return false;
					curl_setopt($ch, CURLOPT_URL, ($secure === true ? 'https://' : 'http://') . self :: $urlserver . self :: $$urlvar);
					curl_setopt($ch, CURLOPT_HEADER, false);
					curl_setopt($ch, CURLOPT_POST, true);
					curl_setopt($ch, CURLOPT_POSTFIELDS, $params2);
					curl_setopt($ch, CURLOPT_TIMEOUT, self :: $connection_timeout);
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
					if ($secure === true) {
						curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
						curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
					}
					$return = curl_exec($ch);
					if (curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200)
						return false;
					return $return;
				} catch (Exception $e) {
					return false;
				}
				break;
			case 'fsockopen' :
				try {
					$fp = fsockopen(($secure === true ? 'ssl://' : '') . self :: $urlserver, ($secure === true ? 443 : 80), $errno, $errstr, self :: $connection_timeout);
					if ($fp === false)
						return false;
					$out = 'POST ' . self :: $$urlvar . ' HTTP/1.0' . self :: $EOL;
					$out .= 'Host: ' . self :: $urlserver . self :: $EOL;
					$out .= 'Content-Type: application/x-www-form-urlencoded' . self :: $EOL;
					$out .= 'Content-Length: ' . strlen($params2) . self :: $EOL . self :: $EOL;
					$out .= $params2 . self :: $EOL;
					$out .= 'Connection: Close' . self :: $EOL . self :: $EOL;
					$response = '';
					fwrite($fp, $out);
					while (!feof($fp) and $fp !== false) {
						$response .= fgets($fp, 128);
					}
					fclose($fp);
					return substr($response, strpos($response, self :: $EOL . self :: $EOL) + strlen(self :: $EOL . self :: $EOL));
				} catch (Exception $e) {
					return false;
				}
				break;
		}
		return false;
	}
	/**
	 * Shows history of account transactions
	 * 
	 * @param int $account Number of e-Gold Account
	 * @param string $password Password to e-Gold Acount
	 * @param string $start Start date - must be in GNU format (see: {@link http://www.gnu.org/software/tar/manual/html_node/tar_113.html})
	 * @param string $end End date - must be in GNU format (see: {@link http://www.gnu.org/software/tar/manual/html_node/tar_113.html})
	 * @param array $datas see page 11 from {@link http://www.e-gold.com/docs/e-gold_automation.pdf} default: paymentsmade = 1, paymentsreceived = 1, redemptions = 1, fees = 1
	 * @return int|string|csv Returns negative on script error, string on e-Gold error or csv data on success
	 */
	public static function history($account, $password, $start = '-1 year', $end = 'now', $datas = null) {
		if ($datas === null)
			$datas = self :: $history_datas_default;
		$startdate = strtotime($start);
		if ($startdate === false or $startdate == -1)
			return -1;
		$enddate = strtotime($end);
		if ($enddate === false or $enddate == -1)
			return -2;
		$params = array ();
		$params['AccountID'] = $account;
		$params['PassPhrase'] = $password;
		$params['startday'] = date('j', $startdate);
		$params['startmonth'] = date('n', $startdate);
		$params['startyear'] = date('Y', $startdate);
		$params['endday'] = date('j', $enddate);
		$params['endmonth'] = date('n', $enddate);
		$params['endyear'] = date('Y', $enddate);
		if (is_array($datas)) {
			foreach ($datas as $key => $val) {
				$params[$key] = $val;
			}
		}
		$return = self :: connect('history', $params);
		if ($return === false)
			return -3;
		else
			return $return;
	}
	/**
	 * Shows current account balance
	 * 
	 * @param int $account Number of e-Gold Account
	 * @param string $password Password to e-Gold Acount
	 * @param string $cur Currency of balance (see page 17 from {@link http://www.e-gold.com/docs/e-gold_automation.pdf}) default: USD
	 * @return int|array Returns negative on error or array on success
	 */
	public static function balance($account, $password, $cur = 'USD') {
		$params = array (
			'AccountID' => $account,
			'PassPhrase' => $password
		);
		$return = self :: connect('balance', $params);
		if ($return === false)
			return -1;
		if (stristr($return, '<input type=hidden name=ERROR value=') !== false)
			return -2;
		$out = array ();
		$vars = array (
			'Gold_Ounces',
			'Gold_Grams',
			'Silver_Ounces',
			'Silver_Grams',
			'Platinum_Ounces',
			'Platinum_Grams',
			'Palladium_Ounces',
			'Palladium_Grams'
		);
		foreach ($vars as $val) {
			if (preg_match('/<input type="?hidden"? name="?' . $val . '"? value="?([0-9\.]*)"?>/i', $return, $match) === false)
				return -3;
			if (isset ($match[1]))
				$out[$val] = (float) $match[1];
			else
				$out[$val] = false;
		}
		$return = self :: metaldata(null, null, $cur);
		$out['Gold_Amount'] = $return[0][2] * $out['Gold_Ounces'];
		$out['Silver_Amount'] = $return[0][3] * $out['Silver_Ounces'];
		$out['Platinum_Amount'] = $return[0][4] * $out['Platinum_Ounces'];
		$out['Palladium_Amount'] = $return[0][5] * $out['Palladium_Ounces'];
		return $out;
	}
	/**
	 * Shows exchange rates of metals
	 * 
	 * @param string $start Start date - must be in GNU format (see: {@link http://www.gnu.org/software/tar/manual/html_node/tar_113.html}) If not specified, you receive latest exchange rates
	 * @param string $end End date - must be in GNU format (see: {@link http://www.gnu.org/software/tar/manual/html_node/tar_113.html})
	 * @param string $cur Currency (see page 17 from {@link http://www.e-gold.com/docs/e-gold_automation.pdf}) default: USD
	 * @return int|array Returns negative on error or array on success
	 */
	public static function metaldata($start = null, $end = null, $cur = 'USD') {
		if ($start !== null and $end !== null) {
			$startdate = strtotime($start);
			if ($startdate === false or $startdate == -1)
				return -1;
			$enddate = strtotime($end);
			if ($enddate === false or $enddate == -1)
				return -2;
			$params = array (
				'StartDate' => $startdate,
				'EndDate' => $enddate,
				'CUR' => $cur
			);
		} else
			$params = array (
				'LATEST' => 1,
				'CUR' => $cur
			);
		$return = self :: connect('metaldata', $params, false);
		if ($return === false)
			return -3;
		if (preg_match_all('#([0-9]+/[0-9]+/[0-9]{4}\s*[0-9]+:[0-9]+:[0-9]+\s*(?:PM|AM)),\s*([0-9\.]+)\s*,\s*([0-9\.]+)\s*,\s*([0-9\.]+)\s*,\s*([0-9\.]+)\s*#i', $return, $matches, PREG_SET_ORDER) === false)
			return -4;
		return $matches;
	}
	/**
	 * Verifies if transaction is possible
	 * 
	 * @param int $account Number of e-Gold Account
	 * @param string $password Password to e-Gold Acount
	 * @param int $payee Payee Account Number
	 * @param float $amount Amount of transaction
	 * @param string $worthof see page 9 from {@link http://www.e-gold.com/docs/e-gold_automation.pdf}
	 * @param int $payin see page 9 from {@link http://www.e-gold.com/docs/e-gold_automation.pdf}
	 * @param string $memo see page 9 from {@link http://www.e-gold.com/docs/e-gold_automation.pdf}
	 * @param string $paymentid see page 9 from {@link http://www.e-gold.com/docs/e-gold_automation.pdf}
	 * @return int|string|array Return negative on script error, string on e-Gold error or array on success
	 */
	public static function verify($account, $password, $payee, $amount, $worthof = "Gold", $payin = 1, $memo = null, $paymentid = null) {
		$params = array (
			'AccountID' => $account,
			'PassPhrase' => $password,
			'Payee_Account' => $payee,
			'Amount' => $amount,
			'PAY_IN' => $payin,
			'WORTH_OF' => $worthof,
			'Memo' => $memo,
			'PAYMENT_ID' => $paymentid
		);
		$return = self :: connect('verify', $params);
		if ($return === false)
			return -1;
		if (preg_match('/<input type="?hidden"? name="?ERROR"? value="?([^">]*)"?>/i', $return, $match) === false)
			return -2;
		if (isset ($match[1]))
			return $match[1];
		$out = array ();
		$vars = array (
			'Payee_Account',
			'ACTUAL_PAYMENT_OUNCES',
			'PAYMENT_AMOUNT',
			'PAYMENT_UNITS',
			'PAYMENT_METAL_ID',
			'PAYER_ACCOUNT',
			'USD_PER_OUNCE',
			'PAYMENT_ID'
		);
		foreach ($vars as $val) {
			if (preg_match('/<input type="?hidden"? name="?' . $val . '"? value="?([^">]*)"?>/i', $return, $match) === false)
				return -3;
			if (isset ($match[1]))
				$out[$val] = $match[1];
			else
				$out[$val] = false;
		}
		return $out;
	}
	/**
	 * Make transaction
	 * 
	 * @param int $account Number of e-Gold Account
	 * @param string $password Password to e-Gold Acount
	 * @param int $payee Payee Account Number
	 * @param float $amount Amount of transaction
	 * @param string $memo see page 7 from {@link http://www.e-gold.com/docs/e-gold_automation.pdf}
	 * @param int $payin see page 7 from {@link http://www.e-gold.com/docs/e-gold_automation.pdf}
	 * @param string $worthof see page 7 from {@link http://www.e-gold.com/docs/e-gold_automation.pdf}
	 * @param string $ounces see page 7 from {@link http://www.e-gold.com/docs/e-gold_automation.pdf}
	 * @param string $ignoreratechange see page 7 from {@link http://www.e-gold.com/docs/e-gold_automation.pdf}
	 * @param string $paymentid see page 7 from {@link http://www.e-gold.com/docs/e-gold_automation.pdf}
	 * @return int|string|array Returns negative on script error, string on e-Gold error or array on success
	 */
	public static function spend($account, $password, $payee, $amount, $memo, $payin = 1, $worthof = 'Gold', $ounces = null, $ignoreratechange = 'y', $paymentid = null) {
		$params = array (
			'AccountID' => $account,
			'PassPhrase' => $password,
			'Payee_Account' => $payee,
			'Amount' => $amount,
			'PAY_IN' => $payin,
			'WORTH_OF' => $worthof,
			'Memo' => $memo,
			'ACTUAL_PAYMENT_OUNCES' => $ounces,
			'IGNORE_RATE_CHANGE' => $ignoreratechange,
			'PAYMENT_ID' => $paymentid
		);
		$return = self :: connect('spend', $params);
		if ($return === false)
			return -1;
		if (preg_match('/<input type="?hidden"? name="?ERROR"? value="?([^">]*)"?>/i', $return, $match) === false)
			return -2;
		if (isset ($match[1]))
			return $match[1];
		$out = array ();
		$vars = array (
			'Payee_Account',
			'ACTUAL_PAYMENT_OUNCES',
			'PAYMENT_FEE_OUNCES',
			'PAYMENT_AMOUNT',
			'PAYMENT_UNITS',
			'PAYMENT_METAL_ID',
			'PAYER_ACCOUNT',
			'USD_PER_OUNCE',
			'PAYMENT_BATCH_NUM',
			'PAYMENT_ID'
		);
		foreach ($vars as $val) {
			if (preg_match('/<input type="?hidden"? name="?' . $val . '"? value="?([^">]*)"?>/i', $return, $match) === false)
				return -3;
			if (isset ($match[1]))
				$out[$val] = $match[1];
			else
				$out[$val] = false;
		}
		return $out;
	}
	/**
	 * Verifies payments
	 * 
	 * For mor information see {@link http://www.e-gold.com/docs/e-gold_sci.pdf}
	 * 
	 * @param string $alternatepassword Alternate password to e-Gold Account
	 * @param bool $apinmd5 Set TRUE if in $alternatepassword you specified md5 hash of Alternate password
	 * @param array $datas Specify only if payment informations is not in $_POST
	 * @param string $ip Specify only if IP address are not in $_SERVER['REMOTE_ADDR']
	 * @return int Return negative on error (-1 on bad ip address -2 on bad hash) or 1 on success
	 */
	public static function sci($alternatepassword = null, $apinmd5 = false, $datas = null, $ip = null) {
		if ($ip === null) {
			if (substr($_SERVER['REMOTE_ADDR'], 0, 11) != '63.240.230.')
				return -1;
		} else {
			if (substr($ip, 0, 11) != '63.240.230.')
				return -1;
		}
		if (is_array($datas))
			$v = & $datas;
		else
			$v = & $_POST;
		if ($alternatepassword != null) {
			$hash = strtoupper(md5($v['PAYMENT_ID'] . ':' . $v['PAYEE_ACCOUNT'] . ':' . $v['PAYMENT_AMOUNT'] . ':' . $v['PAYMENT_UNITS'] . ':' . $v['PAYMENT_METAL_ID'] . ':' . $v['PAYMENT_BATCH_NUM'] . ':' . $v['PAYER_ACCOUNT'] . ':' . strtoupper(($apinmd5 === false ? md5($alternatepassword) : $alternatepassword)) . ':' . $v['ACTUAL_PAYMENT_OUNCES'] . ':' . $v['USD_PER_OUNCE'] . ':' . $v['FEEWEIGHT'] . ':' . $v['TIMESTAMPGMT']));
			if ($v['V2_HASH'] != $hash)
				return -2;
		}
		return 1;
	}
}
?>
Return current item: e-Gold