<?php
/*
copyright: copyright 2004 Sam Yapp. All Rights Reserved. You may modify this file for use on your
					site running a licenced copy of the Php Image Host scripts.
					You must not distribute this file or derivations of it.
Nullified By CrAcKlOvE
*/

class paypalpayments{

	var $orderbys = array('username'=>'Username', 'email'=>'User Email', 'paypal_email'=>'PayPal Email',
												'amount'=>'Amount', 'txn_id'=>'Transaction Id#', 'dated'=>'Date');

	function paypalpayments(&$ace){
		$this->ace =& $ace;
	}

	function addpayment($txnid,$subid, $userid, $paypalemail, $amount, $status, $paypal_data){
		$paypal_data = addslashes(serialize($paypal_data));
		$paypalemail = addslashes($paypalemail);
		$amount = addslashes($amount);
		$status = addslashes($status);
		$txnid = addslashes($txnid);
		$subid = addslashes($subid);

		$sql = "INSERT INTO paypalpayments ";
		$sql .="(txn_id, sub_id, user_id, paypal_email, amount, paypal_data, dated ) ";
		$sql .="VALUES ('$txnid', '$subid', $userid, '$paypalemail', '$amount', '$paypal_data', now()) ";
		$res = @mysql_query($sql);
		if( !$res ){
			return 0;
		}
		return mysql_insert_id();
	}

	function getpayments($criteria, $orderby = 'dated', $orderdir = 'desc', $first = 0, $limit = -1){
		$wheres = array('u.user_id=p.user_id ');
		foreach( $criteria as $c=>$v ){
			switch( $c ){
				case 'username': $wheres[] = "u.username='".addslashes($v)."' "; break;
				case 'userid': settype($v,'integer'); $wheres[] = "p.user_id=$v "; break;
				case 'email': $wheres[] = "u.email='".addslashes($v)."' "; break;
				case 'paypalemail': $wheres[] = "p.paypal_email='".addslashes($v)."' "; break;
				case 'minamount': settype($v, 'double'); $wheres[] = "p.amount>='$v' "; break;
				case 'maxamount': settype($v, 'double'); $wheres[] = "p.amount<='$v' "; break;
				case 'to': $wheres[] = "p.dated<='".addslashes($v)."' "; break;
				case 'from': $wheres[] = "p.dated>='".addslashes($v)."' "; break;
				case 'txnid': $wheres[] = "p.txn_id='".addslashes($v)."' "; break;
				case 'id': settype($id, 'integer'); $wheres[] = "p.payment_id=$v "; break;
				case 'ids': $ids = $this->ace->getids($v); $wheres[] = "p.payment_id IN (".join(",",$ids).") "; break;
				case 'subid': $wheres[] = "p.sub_id='".addslashes($v)."' "; break;
			}
		}
		$counting = isset($criteria['count']) ? $criteria['count'] : false;
		if( $counting ){
			$sql = "SELECT COUNT(*) ";
		}else{
			$sql = "SELECT p.*, u.* ";
		}
		$sql .="FROM paypalpayments p, users u ";
		$sql .="WHERE ".join(" AND ", $wheres)." ";

		if( $counting ){

			$res = $this->ace->query($sql, 'Count Payments');
			return mysql_result($res,0,0);
		}else{
			$ob = 'p.dated';
			switch( $orderby ){
				case 'username':
				case 'email':
					$ob = "u.$orderby ";
					break;
				case 'dated':
				case 'paypal_email':
				case 'txn_id':
				case 'amount':
					$ob = "p.$orderby ";
					break;
			}
			$sql .="ORDER BY $ob $orderdir ";
			if( $limit != -1 ) $sql .= "LIMIT $first,$limit ";
			$res = $this->ace->query($sql, 'Get Payments');
			$ps = array();
			while($p = mysql_fetch_object($res)) $ps[] = $p;
			return $ps;
		}
		return 0;
	}

	function deletepayments($ids){
		$ids = $this->ace->getids($ids);
		if( count($ids) > 0 ){
			$sql = "DELETE FROM paypalpayments WHERE payment_id IN (".join(",",$ids).") ";
			$this->ace->query($sql, 'Delete Payments');
			return mysql_affected_rows();
		}
	}

}

?>
