Help - Fsockopen Function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xtremeclan
Forum Newbie
Posts: 6
Joined: Mon Jun 13, 2005 2:53 pm

Help - Fsockopen Function

Post by xtremeclan »

On my website this line of php:
$fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);
is giving me this error:
Debug mode activated

PHP-Nuke Treasury mod

PayPal Instant Payment Notification script

See below for status:
----------------------------------------------------------------
Connection to db - OK!
Opening connection and validating request with PayPal...

Warning: Die Funktion fsockopen(), welche dein Script nutze, wurde aus Sicherheitsgründen deaktiviert und kann nicht genutzt werden. in /usr/export/www/hosting/xclan/modules/Donations/ipn/ipnppd.php on line 79
FAILED to connect to PayPAl

In other words the fsockopen function has been deactivated for security reasons, could someone please help me?
Or perhaps give me an alternative line of code.
Thanks
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post by Syranide »

serversettings for PHP, nothing you can do about it, unless it is your server, in which I don't see the point in what you've done.

so I am assuming you use a commerical server (or so). only thing you can do is try to rely on something, else Curl or file_get_contents (if that would do the trick, likely not), something like that.

I can't read german so I'm relying on your "translation", about it has been deactivated.
xtremeclan
Forum Newbie
Posts: 6
Joined: Mon Jun 13, 2005 2:53 pm

Post by xtremeclan »

I'm not quite sure what you mean by try to rely on something.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

I think he just missed placed the comma.
serversettings for PHP, nothing you can do about it, unless it is your server, in which I don't see the point in what you've done.

so I am assuming you use a commerical server (or so). only thing you can do is try to rely on something else, Curl or file_get_contents (if that would do the trick, likely not), something like that.

I can't read german so I'm relying on your "translation", about it has been deactivated.
as in try curl of file_get_contents
xtremeclan
Forum Newbie
Posts: 6
Joined: Mon Jun 13, 2005 2:53 pm

Post by xtremeclan »

I'm not experienced in PHP so i'm not sure what you mean at all :(
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

PHP-Nuke Treasury mod
if your using php-nuke you might want to check there boards to see if anyone there has come across this problem and has a solution or mod to fix it.

just a thought :idea:
xtremeclan
Forum Newbie
Posts: 6
Joined: Mon Jun 13, 2005 2:53 pm

Post by xtremeclan »

I have but i got no help so i thought there may be a solution here because it is a problem with the actual code.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

here is a nice article that might help you on your way.
http://www.softcoded.com/paypal/secure_paypal_ipn.php
xtremeclan
Forum Newbie
Posts: 6
Joined: Mon Jun 13, 2005 2:53 pm

Post by xtremeclan »

OK I've replaced Fsockopen with Fopen (Line 79) But now i get this error:

Debug mode activated

PHP-Nuke Treasury mod

PayPal Instant Payment Notification script

See below for status:
----------------------------------------------------------------
Connection to db - OK!
Opening connection and validating request with PayPal...

Warning: fopen() expects at most 4 parameters, 5 given in /usr/export/www/hosting/xclan/modules/Donations/ipn/ipnppd.php on line 79
FAILED to connect to PayPAl

Here is the IPN Script Code

Code: Select all

<?php
/************************************************************************/
/* NukeTreasury - Financial management for PHP-Nuke                      */
/* Copyright (c) 2004 by Dave Lawrence AKA Thrash                       */
/*                       thrash@fragnastika.com                         */
/*                       thrashn8r@hotmail.com                          */
/*                                                                      */
/* This program 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 2 of the License.       */
/*                                                                      */
/* This program 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 this program; if not, write to the Free Software          */
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  */
/* USA                                                                  */
/************************************************************************/

/* NOTE: This file is accessed by PayPal directly, and not through PHP-Nuke */

include(&quote;../config.php&quote;);
$ERR = 0;
$log = &quote;&quote;;
$loglvl = $tr_config&#1111;ipn_dbg_lvl];
define(_ERR, 1);
define(_INF, 2);

if( isset($_GET&#1111;dbg]) )
	$dbg = 1;
else
	$dbg = 0;

if( $dbg )
{
	dprt(&quote;Debug mode activated&quote;, _INF);
	echo &quote;<br>PHP-Nuke Treasury mod<br><br>PayPal Instant Payment Notification script<br><br>See below for status:<br>&quote;;
	echo &quote;----------------------------------------------------------------<br>&quote;;
	$receiver_email = $tr_config&#1111;'receiver_email'];
}

$ipnppd = mysql_pconnect($hostname_ipnppd, $username_ipnppd, $password_ipnppd) or die(mysql_error());

if( $ipnppd )
	dprt(&quote;Connection to db - OK!&quote;, _INF);
else
	dprt(&quote;Connection to db - **FAILED**&quote;, _ERR);

// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';

foreach ($_POST as $key => $value) 
{
	$value = urlencode(stripslashes($value));
	$req .= &quote;&$key=$value&quote;;
}

// post back to PayPal system to validate
$header .= &quote;POST /cgi-bin/webscr HTTP/1.0\r\n&quote;;
$header .= &quote;Content-Type: application/x-www-form-urlencoded\r\n&quote;;
$header .= &quote;Content-Length: &quote; . strlen($req) . &quote;\r\n\r\n&quote;;

// assign posted variables to local variables
$item_name = $_POST&#1111;'item_name'];
$item_number = $_POST&#1111;'item_number'];
$payment_status = $_POST&#1111;'payment_status'];
$payment_amount = $_POST&#1111;'mc_gross'];
$payment_currency = $_POST&#1111;'mc_currency'];
$txn_id = $_POST&#1111;'txn_id'];
$txn_type = $_POST&#1111;'txn_type'];
$receiver_email = $_POST&#1111;'receiver_email'];
$payer_email = $_POST&#1111;'payer_email'];

dprt(&quote;Opening connection and validating request with PayPal...&quote;, _INF);

$fp = fopen ('www.paypal.com', 80, $errno, $errstr, 30);

if (!$fp) {
	// HTTP ERROR
	dprt(&quote;FAILED to connect to PayPAl&quote;, _ERR);
	die();
}

dprt(&quote;OK!&quote;, _INF);

fputs ($fp, $header . $req);

// Perform PayPal email account verification
if( !$dbg && strcasecmp( $_POST&#1111;'business'], $tr_config&#1111;'receiver_email']) != 0)
{
	dprt(&quote;Incorrect receiver email: $receiver_email , aborting&quote;, _ERR) ;
	$ERR = 1;
}

$insertSQL = &quote;&quote;;
// Look for duplicate txn_id's
if( $txn_id )
{
	$sql = &quote;SELECT * FROM transactions WHERE txn_id = '$txn_id'&quote;;
	$Recordset1 = mysql_query($sql, $ipnppd) or die(mysql_error());
	$row_Recordset1 = mysql_fetch_assoc($Recordset1); 
	$NumDups = mysql_num_rows($Recordset1);
}

while (!$dbg && !$ERR && !feof($fp)) 
{
	$res = fgets ($fp, 1024);
	if (strcmp ($res, &quote;VERIFIED&quote;) == 0)
	{
		dprt(&quote;PayPal Verified&quote;, _INF);
		// Ok, PayPal has told us we have a valid IPN here

		// Check for a reversal for a refund
		if( strcmp($payment_status, &quote;Refunded&quote;) == 0)
		{
			// Verify the reversal
			dprt(&quote;Transaction is a Refund&quote;, _INF);
			if( ($NumDups == 0) || strcmp($row_Recordset1&#1111;payment_status], &quote;Completed&quote;) || 
				(strcmp($row_Recordset1&#1111;txn_type], &quote;web_accept&quote;) != 0 && strcmp($row_Recordset1&#1111;txn_type], &quote;send_money&quote;) != 0) )
			{
				// This is an error.  A reversal implies a pre-existing completed transaction
				dprt(&quote;IPN Error: Received refund but missing prior completed transaction&quote;, _ERR);
				foreach( $_POST as $key => $val )
				{
					dprt(&quote;$key => $val&quote;, $_ERR);
				}
				break;
			}
			if( $NumDups != 1 )
			{
				dprt(&quote;IPN Error: Received refund but multiple prior txn_id's encountered, aborting&quote;, _ERR);
				foreach( $_POST as $key => $val )
				{
					dprt(&quote;$key => $val&quote;, $_ERR);
				}
				break;
			}
			
			// We flip the sign of these amount so refunds can be handled correctly
			$mc_gross = -$_POST&#1111;'mc_gross'];
			$mc_fee = -$_POST&#1111;'mc_fee'];
			$insertSQL = sprintf(&quote;INSERT INTO transactions (`txn_id`,`business`,`item_name`, `item_number`, `quantity`, `invoice`, `custom`, `memo`, `tax`, `option_name1`, `option_selection1`, `option_name2`, `option_selection2`, `payment_status`, `payment_date`, `txn_type`, `mc_gross`, `mc_fee`, `mc_currency`, `settle_amount`, `exchange_rate`, `first_name`, `last_name`, `address_street`, `address_city`, `address_state`, `address_zip`, `address_country`, `address_status`, `payer_email`, `payer_status`) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')&quote;, 
				$_POST&#1111;'txn_id'],$_POST&#1111;'business'],$_POST&#1111;'item_name'],$_POST&#1111;'item_number'],$_POST&#1111;'quantity'],$_POST&#1111;'invoice'],$_POST&#1111;'custom'],$_POST&#1111;'memo'],$_POST&#1111;'tax'],$_POST&#1111;'option_name1'],$_POST&#1111;'option_selection1'],$_POST&#1111;'option_name2'],$_POST&#1111;'option_selection2'],$_POST&#1111;'payment_status'],strftime('%Y-%m-%d %H:%M:%S',strtotime($_POST&#1111;'payment_date'])),$_POST&#1111;'txn_type'],$mc_gross,$mc_fee,$_POST&#1111;'mc_currency'],$_POST&#1111;'settle_amount'],$_POST&#1111;'exchange_rate'],$_POST&#1111;'first_name'],$_POST&#1111;'last_name'],$_POST&#1111;'address_street'],$_POST&#1111;'address_city'],$_POST&#1111;'address_state'],$_POST&#1111;'address_zip'],$_POST&#1111;'address_country'],$_POST&#1111;'address_status'],$_POST&#1111;'payer_email'],$_POST&#1111;'payer_status']);

			// We're cleared to add this record
			dprt($insertSQL, _INF);
			$Result1 = mysql_query($insertSQL, $ipnppd) or die(mysql_error());
			dprt(&quote;SQL result = &quote; . $Result1, _INF);
	
			break;
		} else // Look for anormal payment
		if( (strcmp($payment_status, &quote;Completed&quote;) == 0) && ((strcmp($txn_type, &quote;web_accept&quote;)== 0) || (strcmp($txn_type, &quote;send_money&quote;)== 0)) )
		{
			dprt(&quote;Normal transaction&quote;, _INF);
			if( $lp ) fputs($lp, $payer_email . &quote; &quote; . $payment_status . &quote; &quote; . $_POST&#1111;'payment_date'] . &quote;\n&quote;);

			// Check for a duplicate txn_id
			if( $NumDups != 0 )
			{
				dprt(&quote;Valid IPN, but DUPLICATE txn_id! aborting&quote;, _ERR);
				foreach( $_POST as $key => $val )
				{
					dprt(&quote;$key => $val&quote;, $_ERR);
				}
				break;
			}
			
			$insertSQL = sprintf(&quote;INSERT INTO transactions (`txn_id`,`business`,`item_name`, `item_number`, `quantity`, `invoice`, `custom`, `memo`, `tax`, `option_name1`, `option_selection1`, `option_name2`, `option_selection2`, `payment_status`, `payment_date`, `txn_type`, `mc_gross`, `mc_fee`, `mc_currency`, `settle_amount`, `exchange_rate`, `first_name`, `last_name`, `address_street`, `address_city`, `address_state`, `address_zip`, `address_country`, `address_status`, `payer_email`, `payer_status`) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')&quote;, 
				$_POST&#1111;'txn_id'],$_POST&#1111;'business'],$_POST&#1111;'item_name'],$_POST&#1111;'item_number'],$_POST&#1111;'quantity'],$_POST&#1111;'invoice'],$_POST&#1111;'custom'],$_POST&#1111;'memo'],$_POST&#1111;'tax'],$_POST&#1111;'option_name1'],$_POST&#1111;'option_selection1'],$_POST&#1111;'option_name2'],$_POST&#1111;'option_selection2'],$_POST&#1111;'payment_status'],strftime('%Y-%m-%d %H:%M:%S',strtotime($_POST&#1111;'payment_date'])),$_POST&#1111;'txn_type'],$_POST&#1111;'mc_gross'],$_POST&#1111;'mc_fee'],$_POST&#1111;'mc_currency'],$_POST&#1111;'settle_amount'],$_POST&#1111;'exchange_rate'],$_POST&#1111;'first_name'],$_POST&#1111;'last_name'],$_POST&#1111;'address_street'],$_POST&#1111;'address_city'],$_POST&#1111;'address_state'],$_POST&#1111;'address_zip'],$_POST&#1111;'address_country'],$_POST&#1111;'address_status'],$_POST&#1111;'payer_email'],$_POST&#1111;'payer_status']);

			// We're cleared to add this record
			dprt($insertSQL, _INF);
			$Result1 = mysql_query($insertSQL, $ipnppd) or die(mysql_error());
			dprt(&quote;SQL result = &quote; . $Result1, _INF);

			break;
		} else // We're not interested in this transaction, so we're done
		{
			dprt(&quote;Valid IPN, but not interested in this transaction&quote;, _ERR);
			foreach( $_POST as $key => $val )
			{
				dprt(&quote;$key => $val&quote;, $_ERR);
			}
			break;
		}
	}
	else if (strcmp ($res, &quote;INVALID&quote;) == 0) 
	{
		// log for manual investigation
		dprt(&quote;Invalid IPN transaction, this is an abnormal condition&quote;, _ERR);
		foreach( $_POST as $key => $val )
		{
			dprt(&quote;$key => $val&quote;, $_ERR);
		}
		break;
	}
}

if( $dbg )
{
	$sql = &quote;SELECT * FROM transactions LIMIT 10&quote;;
	echo &quote;Selecting database...&quote;;
	$res = mysql_select_db($database_ipnppd, $ipnppd);
	if($res)
		echo &quote;OK!<br>&quote;; 
	 else
		echo &quote;<b>FAILED - err: $res</b><br>&quote;;
	echo &quote;Executing test query...&quote;;
	$Result1 = mysql_query($sql, $ipnppd) or die(mysql_error());
	if($Result1)
		echo &quote;PASSED!<br>&quote;;
	else
		echo &quote;<b>FAILED</b><br>&quote;;
	echo &quote;PayPal Receiver Email: $tr_config&#1111;receiver_email]&quote; ;
}

if( $log )
{
	dprt(&quote;Logging events<br>\n&quote;, _INF);
	// Insert the log entry
	$sql = &quote;INSERT INTO translog VALUES ('','&quote; . strftime('%Y-%m-%d %H:%M:%S',mktime()) . &quote;', '&quote; 
		 . strftime('%Y-%m-%d %H:%M:%S',strtotime($_POST&#1111;'payment_date'])) . &quote;','&quote; . addslashes($log) . &quote;')&quote;;
	$Result1 = mysql_query($sql, $ipnppd) or die(mysql_error());

	// Clear out old log entries
	$sql = &quote;SELECT id as lowid FROM translog ORDER BY id DESC LIMIT &quote; . $tr_config&#1111;ipn_log_entries];
	$Result1 = mysql_query($sql, $ipnppd) or die(mysql_error());
	while($recordSet = mysql_fetch_assoc($Result1))
	{
		$lowid = $recordSet&#1111;lowid];
	}
	$sql =  &quote;DELETE FROM translog WHERE id < '&quote; . $lowid . &quote;'&quote;;
	$Result1 = mysql_query($sql, $ipnppd) or die(mysql_error());
}

fclose ($fp);
if( $lp ) fputs($lp,&quote;Exiting\n&quote;);
if( $lp ) fclose ($lp);

if( $dbg)
{
	echo &quote;<br>----------------------------------------------------------------<br>&quote;;
	echo &quote;If you don't see any error messages, you should be good to go!<br>&quote;;
}
	
function dprt($str, $clvl)
{
	global $dbg, $ipnppd, $lp, $log, $loglvl;

	if( $lp ) fputs($lp, $str . &quote;\n&quote;);
	if( $dbg ) echo $str . &quote;<br>&quote;;
	if( $clvl <= $loglvl )
		$log .= $str . &quote;\n&quote;;
}


?>
Could someone please help me edit it so that it works..
xtremeclan
Forum Newbie
Posts: 6
Joined: Mon Jun 13, 2005 2:53 pm

Post by xtremeclan »

bump
Post Reply