Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> RavenNuke(tm) v2.5x
Author Message
Doulos
Life Cycles Becoming CPU Cycles



Joined: Jun 06, 2005
Posts: 732

PostPosted: Sat Jun 08, 2019 7:05 pm Reply with quote

After using google to try updating the module for php7 the module and block are showing fine without errors- except IPN isn't working. I have run into a snag with the following error and code. Hoping someone can tell me what is wrong.
Quote:
PHP Warning: mysqli_fetch_assoc() expects exactly 1 parameter, 2 given in /home/php7clanfga/public_html/modules/Donations/ipn/ipnppd.php on line 234


Code:
<?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("../config.php");
$ERR = 0;
$log = "";
$loglvl = $tr_config[ipn_dbg_lvl];
define(_ERR, 1);
define(_INF, 2);

if( isset($_GET[dbg]) )
   $dbg = 1;
else
   $dbg = 0;

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

$ipnppd = mysqli_connect($hostname_ipnppd, $username_ipnppd, $password_ipnppd);

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

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

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

// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n";
$header .= "Host:www.paypal.com\r\n";
$header .= "Connection: close\r\n\r\n";

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

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

$fp = fsockopen ('tls://ipnpb.paypal.com', 443, $errno, $errstr, 30);

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

dprt("OK!", _INF);

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

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

$insertSQL = "";
// Look for duplicate txn_id's
if( $txn_id )
{
   $sql = "SELECT * FROM transactions WHERE txn_id = '$txn_id'";
   $Recordset1 = mysqli_query($ipnppd, $sql);
   $row_Recordset1 = mysqli_fetch_assoc($Recordset1);
   $NumDups = mysqli_num_rows($Recordset1);
}

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

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

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

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

         // We're cleared to add this record
         dprt($insertSQL, _INF);
         $Result1 = mysqli_query($ipnppd, $insertSQL);
         dprt("SQL result = " . $Result1, _INF);

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

if( $dbg )
{
   $sql = "SELECT * FROM transactions LIMIT 10";
   echo "Selecting database...";
   $res = mysqli_select_db($database_ipnppd, $ipnppd);
   if($res)
      echo "OK!<br>";
    else
      echo "<b>FAILED - err: $res</b><br>";
   echo "Executing test query...";
   $Result1 = mysqli_query($sql, $ipnppd);
   if($Result1)
      echo "PASSED!<br>";
   else
      echo "<b>FAILED</b><br>";
   echo "PayPal Receiver Email: $tr_config[receiver_email]" ;
}

if( $log )
{
   dprt("Logging events<br>\n", _INF);
   // Insert the log entry
   $sql = "INSERT INTO translog VALUES ('','" . strftime('%Y-%m-%d %H:%M:%S',time()) . "', '"
       . strftime('%Y-%m-%d %H:%M:%S',strtotime($_POST['payment_date'])) . "','" . addslashes($log) . "')";
   $Result1 = mysqli_query($ipnppd, $sql);

   // Clear out old log entries
   $sql = "SELECT id as lowid FROM translog ORDER BY id DESC LIMIT " . $tr_config[ipn_log_entries];
   $Result1 = mysqli_query($ipnppd, $sql);
   [b]while($recordSet = mysqli_fetch_assoc($Result1))[/b]
   {
      $lowid = $recordSet[lowid];
   }
   $sql =  "DELETE FROM translog WHERE id < '" . $lowid . "'";
   $Result1 = mysqli_query($ipnppd, $sql);
}

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

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

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


?>
 
View user's profile Send private message
neralex
Site Admin



Joined: Aug 22, 2007
Posts: 1774

PostPosted: Sun Jun 09, 2019 12:15 am Reply with quote

I updated many parts of this script here for RPS and I will send you the files for testing, maybe that will fix it. 😏

_________________
Github: RavenNuke 
View user's profile Send private message
neralex







PostPosted: Sun Jun 09, 2019 5:18 am Reply with quote

Edit: link removed

DO A BACKUP OF THIS FILE!

Please note: I renamed on my end some tables to simplify the identification of the Donations db-tables. So it could be your config-table for the Donations module is still called 'config' instead of '_treasury_config' as you can see it in the following query. If so, then edit this query from:

php Code:
$result = $db->sql_query('SELECT * FROM `' . $prefix . '_treasury_config` WHERE `subtype` = \'\'');


...to this:

php Code:
$result = $db->sql_query('SELECT * FROM `' . $prefix . 'config` WHERE `subtype` = \'\'');


If you are unsure, please COMPARE the table-name with the config.php of the Donations module and your database.

What I changed: I called the config.php and db.php from the RN-installation, to use the internal sql-layer without establishing a separate database-conntection and changed all sql-queries to let it work with this layer.

Good luck and let me know, if it works or not. If not, please post also the error-messages.


Last edited by neralex on Sun Jun 09, 2019 7:48 am; edited 1 time in total 
Doulos







PostPosted: Sun Jun 09, 2019 6:26 am Reply with quote

Thank you for doing that. Keep in mind that I don't know what I am talking about, but ...

Quote:
FATAL ERROR: Unable to connect to SQL database.


The treasury tables are in their own database, not part of RN database. I realize that the original code is probably bad, but I am only trying to get the IPN to work on PHP7 and not trying to, or trying to get someone else to update the whole module. (Though, of course, I wouldn't object to that, LOL). I've got the module and block working without errors - all except the IPN portion.
 
neralex







PostPosted: Sun Jun 09, 2019 7:16 am Reply with quote

Doulos wrote:
The treasury tables are in their own database, not part of RN database.

Make it sense? Anyway...


Doulos wrote:
I realize that the original code is probably bad, but I am only trying to get the IPN to work on PHP7 and not trying to, or trying to get someone else to update the whole module.

Well done! Where is the difference? If you have fixed this issue, the next is probably waiting on you... Wink


Last edited by neralex on Sun Jun 09, 2019 7:47 am; edited 1 time in total 
Doulos







PostPosted: Sun Jun 09, 2019 7:34 am Reply with quote

Quote:
MySQL Error
There seems to be a problem with the System Configuration Table - it's missing.
If you are the System Administrator and installing this for the first time,
did you remember to run the INSTALLATION/installSQL.php file?
 
neralex







PostPosted: Sun Jun 09, 2019 7:47 am Reply with quote

Ok was a try lol. I deleted both GISTs now.
 
neralex







PostPosted: Sun Jun 09, 2019 7:58 am Reply with quote

Maybe something like this:

php Code:
while($recordSet = mysqli_fetch_assoc($Result1))


to:

php Code:
while($recordSet = mysqli_fetch_assoc(mysqli_result $Result1))


or:

php Code:
while($recordSet = mysqli_fetch_assoc($Result1)['lowid'])


This script is so f***ing outdated...
 
Doulos







PostPosted: Sun Jun 09, 2019 10:09 am Reply with quote

Ya, I know, but it has worked for me great on php5 for 14 years.
 
neralex







PostPosted: Sun Jun 09, 2019 11:12 am Reply with quote

What speaks against to move the db-tables into the RN-database?
 
Doulos







PostPosted: Sun Jun 09, 2019 2:49 pm Reply with quote

Nothing except my lack of understanding.
 
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> RavenNuke(tm) v2.5x

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©