[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

6.0

fgets and fclose errors

Asked by FirstBorn in PHP Scripting Language

Tags: errors

Hi,

Thanks for your help.

Here is the issue.
I am able to use this script and it works for Me.
My partner is testing it and gets an error when clicking on the button to make a purchase.

The Error and the Code are listed below.

Thank You VERY Much for helping to resolve this.

Respectfully,

FirstBorn

-----------------------------
Environment:
-----------------------------
Apache version        1.3.37 (Unix)
PHP version        4.3.11

-----------------------------
Here are the Errors:
-----------------------------
Warning: fgets(): supplied argument is not a valid stream resource in /home/blah/public_html/blah/blah/index.php on line 880

Warning: fclose(): supplied argument is not a valid stream resource in /home/blah/public_html/blah/blah/index.php on line 883

----------------------------
Here is the code:
----------------------------
<?php
include("settings.php");

function searchKeywords($url){
  $parts = parse_url($url);
  $host = str_replace("www.", "", $parts["host"]);
  $keywords = "";
  parse_str($parts["query"], $vars);
  if(strpos(" $host", "google")){
    $keywords = urldecode($vars["q"]);
  }
  elseif(strpos(" $host", "yahoo")){
    $keywords = urldecode($vars["p"]);
  }
  elseif(strpos(" $host", "live")){
    $keywords = urldecode($vars["q"]);
  }
  elseif($vars["keywords"]){
    $keywords = urldecode($vars["keywords"]);
  }
  elseif($vars["query"]){
    $keywords = urldecode($vars["query"]);
  }
  else{
    $keywords = urldecode($vars["q"]);
  }
  return $keywords;
}

$action = $_REQUEST["action"];

if(substr($action, 0, 5)=="admin" && $action != "admin" && $action != "adminlogin" && !$_COOKIE["admin"]){
  # Not logged in.  Redirect to login.
  header("Location: $_SERVER[PHP_SELF]?action=admin");
  exit;
}

# Redirectin case PayPal goofs and fails to do so.
if($_COOKIE["first"]>0 && $_SERVER["REQUEST_METHOD"]=="POST" && $action=="" && $_SERVER["HTTP_REFERER"]==""){
  if(isset($_COOKIE["oto"])){
    setcookie("otodl", time(), time()+(3600*$sys_expire_hours), "/", "." . $sys_domain);
    $url = "index.php?action=downloadoto";
  }
  elseif(isset($_COOKIE["ordered"])){
    $url = "index.php?action=squeeze";
  }
  else{
    # No cookie--no purchase.
    $url = "/";
  }
  setcookie("first", 0, time()-3600, "/", "." . $sys_domain);
  #setcookie("firstdl", time(), time()+(3600*$sys_expire_hours), "/", "." . $sys_domain);
  echo "
    <html>
    <head>
    <title>Thank you!</title>
    </head>
    <body>
    <form action='$url' method=post>
      <p><b>Thank you for your purchase!</b></p>
      <p>
        Click the button below to continue.
      </p>
      <input type=submit value='Please Click Here To Continue'>
    </form>
    </body>
    </html>
    ";
  exit;
}

if(strpos(" $_SERVER[QUERY_STRING]", "Return+To+Merchant") | strpos(" $_SERVER[QUERY_STRING]", "form_charset=UTF-8") | strpos(" $_SERVER[HTTP_REFERER]", "paypal")){
  # Redirect user to squeeze page.  For some weird reason,
  # if the user views their printable receipt or does
  # ANYTHING other than click "Return to Merchant",
  # PayPal will send people back to the home page.
  # We have to anticipate that and redirect them.
  if(isset($_COOKIE["oto"])){
    $url = "index.php?action=downloadoto";
  }
  elseif(isset($_COOKIE["ordered"])){
    $url = "index.php?action=squeeze";
  }
  else{
    # No cookie--no purchase.
    $url = "/";
  }
  echo "
    <html>
    <head>
    <title>Please wait...</title>
    <meta http-equiv='refresh' content='0;url=$url'>
    </head>
    <body>
    <i>Please wait...</i>
    </body>
    </html>
    ";
  exit;
}

if(isset($_GET["e"])){
  # Make sure this account hasn't been blocked.
  if(!strpos(" $sys_blocked", $_GET["e"])){
    # Set cookie and redirect visitor if it's through an affiliate link.
    # Set privacy policy for IE6/WinXP users.
    # If you don't do this, a lot of IE browsers wont accept the cookie.
    header('P3P: CP="IDC DSP COR CURa ADMa OUR IND PHY ONL COM STA"');
    setcookie("ref", $_SERVER["HTTP_REFERER"], time()+31536000, "/", "." . $sys_domain);
    setcookie("aff", $_GET["e"], time()+31536000, "/", "." . $sys_domain);
    header("Location: $sys_script_folder");
    exit;
  }
  else{
    # Inform visitor that this customers link has been blocked
    # for fradulent activity.
    $filename = $sys_template_folder . "fraud.html";
    $fh = @fopen($filename, "r");
    while($s = fgets($fh)){
      echo $s;
    }
    @fclose($fh);
    exit;
  }
}

# Keep paypal email on hand at all times.
if(isset($_COOKIE["aff"])){
  $email = $_COOKIE["aff"];
}
else{
  $email = $sys_default_email;
}

if($action==""){
  # Save referrer.
  setcookie("ref", $_SERVER["HTTP_REFERER"], time()+31536000, "/", "." . $sys_domain);

  # Show sales letter.
  $filename = $sys_template_folder . "salesletter.html";
 
  # Set test cookie to make sure this will work for user.
  setcookie("cookietest", true, time()+31536000, "/", "." . $sys_domain);
}
elseif($action=="order"){
  # An order. Great! :)
 
  # First, make sure cookies work for this user (since they are required).
  if(isset($_COOKIE["cookietest"])){
    # Yup.  Send them through the order process.
    if(isset($_GET["oto"])){
      # Buying OTO.
      if(!isset($_COOKIE["aff"])){
        # No affiliate. Use primary email address.
        $email = $sys_default_email;
      }
      else{
        srand();
        $r = rand(1, 100);
        if($r<=$sys_oto_percent){
          # Send OTO to affiliate.
          $email = $_COOKIE["aff"];
        }
        else{
          # Use primary.
          $email = $sys_default_email;
        }
      }
      $item_name = $sys_oto_name;
      $item_number = $sys_oto_number;
      $item_cost = $sys_oto_cost;
      $item_download_url = "http://$sys_domain" . $sys_script_folder . "?action=downloadoto";
      #$item_download_url = "http://$sys_domain" . $sys_script_folder;
      $item_cancel_url = "http://$sys_domain" . $sys_script_folder;
      $item_ipn_url = "http://$sys_domain" . $sys_script_folder . "ipn.php";
      setcookie("oto", time(), time()+($sys_expire_hours * 3600), "/", "." . $sys_domain);
    }
    else{
      # Regular purchase.
      $item_name = $sys_item_name;
      $item_number = $sys_item_number;
      $item_cost = $sys_item_cost;
      $item_download_url = "http://$sys_domain" . $sys_script_folder . "?action=squeeze";
      #$item_download_url = "http://$sys_domain" . $sys_script_folder;
      $item_cancel_url = "http://$sys_domain" . $sys_script_folder;
      $item_ipn_url = "http://$sys_domain" . $sys_script_folder . "ipn.php";
    }
    setcookie("first", time(), time()+600, "/", "." . $sys_domain);

    # Set ordered cookie.
    setcookie("ordered", time(), time()+($sys_expire_hours * 3600), "/", "." . $sys_domain);
   
    $paypal = "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=$email&item_name=$item_name&item_number=$item_number&amount=$item_cost&no_shipping=2&return=$item_download_url&cancel_return=$item_cancel_url&no_note=1&currency_code=USD&lc=US&bn=PP%2dBuyNowBF&charset=UTF%2d8";

    # Use meta-refresh instead of header() redirect.
    # header() seems to cause session issues with PayPal.
    echo "
    <html>
    <head>
    <title>Please wait...</title>
    </head>
    <body>
    <p align=center>
    <table width=100% height=100%><tr><td align=center valign=center>
      <table width=420px cellpadding=5 border=1><tr><td>
        <font face=verdana>
        <p>You will be redirected to the PayPal checkout in 5 seconds.</p>
        <p>Remember to click <b>RETURN TO MERCHANT</b> after checkout or you will not receive your download.</p>
        </font>
      </td></tr></table>
    </table>
    </p>
    <form action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\" id=paymentform>
    <input type=\"hidden\" name=\"cmd\" value=\"_xclick\">
    <input type=\"hidden\" name=\"business\" value=\"$email\">
    <input type=\"hidden\" name=\"item_name\" value=\"$item_name\">
    <input type=\"hidden\" name=\"item_number\" value=\"$item_number\">
    <input type=\"hidden\" name=\"amount\" value=\"$item_cost\">
    <input type=\"hidden\" name=\"no_shipping\" value=\"1\">
    <input type=\"hidden\" name=\"return\" value=\"$item_download_url\">
    <input type=\"hidden\" name=\"cancel_return\" value=\"$item_cancel_url\">
    <input type=\"hidden\" name=\"no_note\" value=\"1\">
    <input type=\"hidden\" name=\"shipping\" value=\"0.00\">
    <input type=\"hidden\" name=\"currency_code\" value=\"USD\">
    <input type=\"hidden\" name=\"lc\" value=\"US\">
    <input type=\"hidden\" name=\"bn\" value=\"PP-BuyNowBF\">
    <input type=\"hidden\" name=\"rm\" value=\"2\">
    <input type=\"hidden\" name=\"notify_url\" value=\"$item_ipn_url\">
    <input type=\"hidden\" name=\"custom\" value=\"$_COOKIE[ref]\">
    </form>
    <script language=javascript>
      setTimeout('redir()', 5000);
      function redir(){
        document.getElementById('paymentform').submit();
      }
    </script>
    </body>
    </html>
    ";
    exit;
  }
}
elseif($action=="download" && isset($_REQUEST["id"])){
  # Check that: 1) ID is valid and 2) download has not timed out.
  $id = $_REQUEST["id"];

  $fh = @fopen($sys_template_folder . "ipn.txt", "r");
  $invalid = true;
  $records = array();
  $changes = false;
  while($rec = @fgets($fh)){
    $rec = str_replace("\n", "", $rec);
    if(trim($rec)){
      $record = explode("|", $rec);
      if($record[0]==$id){
        $invalid = false;
        # Valid ID.  Check timeout.
        if(time()<=$record[9]){
          # Still active.  Set cookies and show download.
          setcookie("ordered", time(), $record[9], "/", "." . $sys_domain);
          if($record[2]==$sys_oto_number){
            # One-time offer download.  Set cookie and show download.
            setcookie("oto", time(), $record[9], "/", "." . $sys_domain);
            $filename = $sys_template_folder . "downloadoto.html";
          }
          else{
            #if($record[10]==0){
            #  # Show squeeze page.
            #  $filename = $sys_template_folder . "squeeze.html";
            #  $record[10] = 1;
            #  $changes = true;
            #}
            #else{
              # Already squeezed, show download page.
              $filename = $sys_template_folder . "download.html";
            #}
          }
        }
        else{
          # Expired.
          $filename = $sys_template_folder . "downloadexpired.html";
        }
        $rec = "";
        for($i=0;$i<count($record);$i++){
          if($i<count($record)-1){
            $rec .= $record[$i] . "|";
          }
          else{
            $rec .= $record[$i];
          }
        }
        $records[] = $rec;
      }
      else{
        $records[] = $rec;
      }
    }
  }
  if($invalid){
    # Invalid ID.
    $filename = $sys_template_folder . "invalid.html";
  }

  if($changes){
    # Update IPN file.
    $done = false;
    $fh = @fopen($sys_template_folder . "ipn.txt", "w+");
    while(!$done){
      $fl = flock($fh, LOCK_EX);
      if($fl){
        foreach($records as $record){
          fwrite($fh, $record . "\n");
        }
        $done = true;
        flock($fh, LOCK_UN);
      }
    }
    @fclose($fh);
  }
}

# All other actions require that user has ordered and
# cookie is set.  If it's not, then it's expired.
elseif(isset($_COOKIE["ordered"])){
  # Kill "first" cookie
  if($action=="oto"){
    # Show oto page.
    $filename = $sys_template_folder . "oto.html";
  }
  elseif($action=="squeeze" && $_SERVER["REQUEST_METHOD"]=="POST"){
    # Show name squeeze page.
    $filename = $sys_template_folder . "squeeze.html";
  }
  elseif($action=="download"){
    $safe = true;
    if($sys_secure_dl && $_SERVER["REQUEST_METHOD"]!="POST"){
      $safe = false;
    }
    if($safe){
      if($sys_oto && !isset($_GET["dl"])){
        # Show OTO page.
        $filename = $sys_template_folder . "oto.html";
      }
      else{
        # Show download page.
        $filename = $sys_template_folder . "download.html";
      }
    }
    else{
      # Not safe, show expired.
      $filename = $sys_template_folder . "downloadexpired.html";
    }
  }
  elseif($action=="downloadoto" && isset($_COOKIE["oto"]) && $_SERVER["REQUEST_METHOD"]=="POST"){
    # Show OTO download page.
    $filename = $sys_template_folder . "downloadoto.html";
  }
  elseif($action=="downloadoto"){
    # Show expired since visitor doesn't have oto cookie.
    $filename = $sys_template_folder . "downloadexpired.html";
  }
  elseif($action=="thankyou"){
    # Show thank you page.
    $filename = $sys_template_folder . "thankyou.html";
  }
  elseif($action=="thankyouoto"){
    # Show OTO thank you page.
    $filename = $sys_template_folder . "thankyouoto.html";
  }
  else{
    # Expired.
    $filename = $sys_template_folder . "downloadexpired.html";
  }
}
elseif($action=="order" && !isset($_COOKIE["cookietest"])){
  # No cookies. Show error.
  $filename = $sys_template_folder . "nocookies.html";
}
elseif($action){
  # Cookie's gone.  Download has expired.
  $filename = $sys_template_folder . "downloadexpired.html";
}

if($action=="admin"){
  # Get username/password for admin area.
  echo "
    <font face=arial size=2>
    <form action='$_SERVER[PHP_SELF]' method=post>
    <b>Admin Login</b>
    <p>
    <table>
      <tr><td>username</td><td><input type=text name=username size=20></td></tr>
      <tr><td>password</td><td><input type=password name=password size=20></td></tr>
    </table>
    <input type=submit value='Login'>
    <input type=hidden name=action value=adminlogin>
    </p>
    </form>
    </font>
  ";
  exit;
}
elseif($action=="adminlogin"){
  # Verify admin username/password.
  if($_POST["username"] == $sys_admin_username && $_POST["password"] == $sys_admin_password){
    # Valid.  Cookie and redirect.
    setcookie("admin", true);
    header("Location: $_SERVER[PHP_SELF]?action=adminmenu");
    exit;
  }
  else{
    echo "<font face=arial color=red><b>Invalid username or password.</b></font>";
    exit;
  }
}
if($_COOKIE["admin"]){
  $adminheader = "<html><head><title>Admin Panel</title><head><body>";
  $adminfooter = "</body></html>";
  # Admin functions.
  if($action=="adminmenu"){
    echo "
    $adminheader
    <p>
    <b>Admin Panel</b>
    </p>
    <ol>
      <li> <a href='$_SERVER[PHP_SELF]?action=adminbuys'>Show All Sales</a>
      <li> <a href='$_SERVER[PHP_SELF]?action=adminbuys&count=50'>Show Last 50 Sales</a>
      <li> <a href='$_SERVER[PHP_SELF]?action=adminbuys&count=100'>Show Last 100 Sales</a>
      <li> <a href='$_SERVER[PHP_SELF]?action=adminmine'>Show My Sales</a>
      <li> <a href='$_SERVER[PHP_SELF]?action=adminrefs'>Referrer Report</a>
      <li> <a href='$_SERVER[PHP_SELF]?action=adminrefdomains'>Referrer Domains Report</a>
      <li> <a href='$_SERVER[PHP_SELF]?action=adminrefsmine'>My Sales Referrer Report</a>
      <li> <a href='$_SERVER[PHP_SELF]?action=adminrefdomainsmine'>My Sales Referrer Domains Report</a>
      <li> <a href='$_SERVER[PHP_SELF]?action=adminaffiliates'>Affiliates Report</a>
      <li> <a href='$_SERVER[PHP_SELF]?action=adminexport'>Export Purchases to CSV</a>
      <li> <a href='$_SERVER[PHP_SELF]?action=adminlogout'>Logout</a>
    </ol>
    $adminfooter
    ";
    exit;
  }
  elseif($action=="adminbuys"|$action=="adminmine"){
    # Show all customers.
    $records = @file($sys_template_folder . "ipn.txt");
    $count = 0;
    $count = $_REQUEST["count"];
    $rcount = count($records);
    if($count){
      $showing = "last $count";
    }
    else{
      $showing = "all $rcount";
    }
    if($action=="adminmine"){
      $showing = "only sales paid to you";
    }
    echo "
      $adminheader
      <a href='$_SERVER[PHP_SELF]?action=adminmenu'>Back to Menu</a>
      <p>
      <i>Clicking the EXPIRES link will add another $sys_expire_hours hours to the customer's download link expiration.</i>
      </p>
      <p>
      $rcount total records.  Showing $showing. Your sales are <span style='background-color: #ffffcc;'>highlighted</span>.
      <table border=1 cellspacing=0 cellpadding=0><tr>
        <td><font size=1 face=arial><b>PayPal Receipt ID</b></font></td>
        <td><font size=1 face=arial><b>Item Number</b></font></td>
        <td><font size=1 face=arial><b>Affiliate Email</b></font></td>
        <td><font size=1 face=arial><b>Customer Email</b></font></td>
        <td><font size=1 face=arial><b>First Name</b></font></td>
        <td><font size=1 face=arial><b>Last Name</b></font></td>
        <td><font size=1 face=arial><b>Payment</b></font></td>
        <td><font size=1 face=arial><b>Bought</b></font></td>
        <td><font size=1 face=arial><b>Expires</b></font></td>
        </tr>
    ";
    $bgcolor = "#ffffff";
    $c = $rcount;
    foreach($records as $rec){
      $c--;
      if(!$count | $c<=$count){
        $rec = str_replace("\n", "", $rec);
        if(trim($rec)){
          $record = explode("|", $rec);
          $date = "";
          if($record[11]){
            $date = date("Y-m-d H:i:s", $record[11]);
          }
          $expires = date("Y-m-d H:i:s", $record[9]);
          if($record[3]==$sys_default_email){
            # Our sale.  Yay! :)
            $bgcolor = "#ffffcc";
          }
          $show = true;
          if($action=="adminmine"){
            if($record[3]!=$sys_default_email){
              $show = false;
            }
          }
          if($show){
            echo "
              <tr>
                <td bgcolor=$bgcolor><font size=1 face=arial><a href='index.php?action=download&id=$record[0]' target=_blank>$record[0]</a></font></td>
                <td bgcolor=$bgcolor><font size=1 face=arial>$record[2]</font></td>
                <td bgcolor=$bgcolor><font size=1 face=arial>$record[3]</font></td>
                <td bgcolor=$bgcolor><font size=1 face=arial>$record[4]</font></td>
                <td bgcolor=$bgcolor><font size=1 face=arial>$record[5]</font></td>
                <td bgcolor=$bgcolor><font size=1 face=arial>$record[6]</font></td>
                <td bgcolor=$bgcolor><font size=1 face=arial>$record[8]</font></td>
                <td bgcolor=$bgcolor><font size=1 face=arial>$date</font></td>
                <td bgcolor=$bgcolor><font size=1 face=arial><a href='$_SERVER[PHP_SELF]?action=adminextend&id=$record[0]'>$expires</a></font></td>
              </tr>
            ";
          }
          if($bgcolor=="#ffffff"){
            $bgcolor = "#eeeeee";
          }
          else{
            $bgcolor = "#ffffff";
          }
        }
      }
    }
    echo "</table>$adminfooter";
    exit;
  }
  elseif($action=="adminexport"){
    # Export purchase records to CSV.
    $first = true;
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=export.csv");
    header("Content-Transfer-Encoding: binary");
    $records = @file($sys_template_folder . "ipn.txt");
    foreach($records as $rec){
      $rec = str_replace("\n", "", $rec);
      if(trim($rec)){
        if($first){
          # Output headers.
          echo '"PayPal Receipt ID","Item Name","Item Number","Affiliate Email","Customer Email","First Name","Last Name","Company","Payment","Bought","Expires"' . "\r\n";
          $first = false;
        }
        $record = explode("|", $rec);
        $date = "";
        if($record[11]){
          $date = date("Y-m-d H:i:s", $record[11]);
        }
        $expires = date("Y-m-d H:i:s", $record[9]);
        echo "\"$record[0]\",\"$record[1]\",\"$record[2]\",\"$record[3]\",\"$record[4]\",\"$record[5]\",\"$record[6]\",\"$record[7]\",\"$record[8]\",\"$date\",\"$expires\"\r\n";
      }
    }
    exit;
  }
  elseif($action=="adminrefs"|$action=="adminrefsmine"){
    # Show referrer report.
    $records = @file($sys_template_folder . "ipn.txt");
    $count = 0;
    $count = $_REQUEST["count"];
    $rcount = count($records);
    if($count){
      $showing = "last $count";
    }
    else{
      $showing = "all $rcount";
    }
    if($action=="adminrefsmine"){
      $showing = "only sales paid to you";
    }
    echo "
      $adminheader
      <a href='$_SERVER[PHP_SELF]?action=adminmenu'>Back to Menu</a>
      <p>
      $rcount total records.  Showing $showing. Your sales are <span style='background-color: #ffffcc;'>highlighted</span>.
      <table border=1><tr>
        <td><font size=1 face=arial><b>Referrer</b></font></td>
        <td><font size=1 face=arial><b>Domain</b></font></td>
        <td><font size=1 face=arial><b>Keywords</b></font></td>
        <td><font size=1 face=arial align=right><b>Sales</b></font></td>
        </tr>
    ";
    $bgcolor = "#ffffff";
    $c = $rcount;
    $refs = array();
    foreach($records as $rec){
      $c--;
      if(!$count | $c<=$count){
        $rec = str_replace("\n", "", $rec);
        if(trim($rec)){
          $record = explode("|", $rec);
          $date = "";
          if($record[11]){
            $date = date("Y-m-d H:i:s", $record[11]);
          }
          $expires = date("Y-m-d H:i:s", $record[9]);
          if($record[3]==$sys_default_email){
            # Our sale.  Yay! :)
            $bgcolor = "#ffffcc";
          }
          $show = true;
          if($action=="adminrefsmine"){
            if($record[3]!=$sys_default_email){
              $show = false;
            }
          }
          if($show){
            if(strlen($record[12])>100){
              $ref = substr($record[12], 0, 98) . "..";
            }
            else{
              $ref = $record[12];
            }
            if(isset($refs[$record[12]])){
              $refs[$record[12]]++;
            }
            else{
              $refs[$record[12]]=1;
            }
          }
        }
      }
    }
    arsort($refs);
    foreach($refs as $ref => $sales){
      $parts = parse_url($ref);
      $host = str_replace("www.", "", $parts["host"]);
      $keywords = searchKeywords($ref);
      if(strlen($ref)>100){
        $sref = substr($ref, 0, 98) . "..";
      }
      else{
        $sref = $ref;
      }
      echo "
        <tr>
          <td bgcolor=$bgcolor><font size=1 face=arial><a href='$ref' target=_blank>$sref</a></font></td>
          <td bgcolor=$bgcolor><font size=1 face=arial><a href='http://$host' target=_blank>$host</a></font></td>
          <td bgcolor=$bgcolor><font size=1 face=arial>$keywords</font></td>
          <td bgcolor=$bgcolor align=right><font size=1 face=arial>$sales</font></td>
        </tr>
      ";
      if($bgcolor=="#ffffff"){
        $bgcolor = "#eeeeee";
      }
      else{
        $bgcolor = "#ffffff";
      }
    }
    echo "</table>$adminfooter";
    exit;
  }
  elseif($action=="adminrefdomains"|$action=="adminrefdomainsmine"){
    # Show referrer report.
    $records = @file($sys_template_folder . "ipn.txt");
    $count = 0;
    $count = $_REQUEST["count"];
    $rcount = count($records);
    if($count){
      $showing = "last $count";
    }
    else{
      $showing = "all $rcount";
    }
    if($action=="adminrefdomainsmine"){
      $showing = "only sales paid to you";
    }
    echo "
      $adminheader
      <a href='$_SERVER[PHP_SELF]?action=adminmenu'>Back to Menu</a>
      <p>
      $rcount total records.  Showing $showing. Your sales are <span style='background-color: #ffffcc;'>highlighted</span>.
      <table border=1><tr>
        <td><font size=1 face=arial><b>Domain</b></font></td>
        <td><font size=1 face=arial align=right><b>Sales</b></font></td>
        </tr>
    ";
    $bgcolor = "#ffffff";
    $c = $rcount;
    $refs = array();
    foreach($records as $rec){
      $c--;
      if(!$count | $c<=$count){
        $rec = str_replace("\n", "", $rec);
        if(trim($rec)){
          $record = explode("|", $rec);
          $date = "";
          if($record[11]){
            $date = date("Y-m-d H:i:s", $record[11]);
          }
          $expires = date("Y-m-d H:i:s", $record[9]);
          if($record[3]==$sys_default_email){
            # Our sale.  Yay! :)
            $bgcolor = "#ffffcc";
          }
          $show = true;
          if($action=="adminrefdomainsmine"){
            if($record[3]!=$sys_default_email){
              $show = false;
            }
          }
          if($show){
            $parts = parse_url($record[12]);
            $host = str_replace("www.", "", $parts["host"]);
            if(isset($refs[$host])){
              $refs[$host]++;
            }
            else{
              $refs[$host]=1;
            }
          }
        }
      }
    }
    arsort($refs);
    foreach($refs as $ref => $sales){
      echo "
        <tr>
          <td bgcolor=$bgcolor><font size=1 face=arial><a href='http://$ref' target=_blank>$ref</a></font></td>
          <td bgcolor=$bgcolor align=right><font size=1 face=arial>$sales</font></td>
        </tr>
      ";
      if($bgcolor=="#ffffff"){
        $bgcolor = "#eeeeee";
      }
      else{
        $bgcolor = "#ffffff";
      }
    }
    echo "</table>$adminfooter";
    exit;
  }
  elseif($action=="adminaffiliates"){
    # Show referrer report.
    $records = @file($sys_template_folder . "ipn.txt");
    $count = 0;
    $count = $_REQUEST["count"];
    $rcount = count($records);
    if($count){
      $showing = "last $count";
    }
    else{
      $showing = "all $rcount";
    }
    echo "
      $adminheader
      <a href='$_SERVER[PHP_SELF]?action=adminmenu'>Back to Menu</a>
      <p>
      $rcount total records.  Showing $showing. Your sales are <span style='background-color: #ffffcc;'>highlighted</span>.
      <table border=1><tr>
        <td><font size=1 face=arial><b>Affiliate</b></font></td>
        <td><font size=1 face=arial align=right><b>Sales</b></font></td>
        </tr>
    ";
    $bgcolor = "#ffffff";
    $c = $rcount;
    $refs = array();
    foreach($records as $rec){
      $c--;
      if(!$count | $c<=$count){
        $rec = str_replace("\n", "", $rec);
        if(trim($rec)){
          $record = explode("|", $rec);
          $date = "";
          if($record[11]){
            $date = date("Y-m-d H:i:s", $record[11]);
          }
          $expires = date("Y-m-d H:i:s", $record[9]);
          if($record[3]==$sys_default_email){
            # Our sale.  Yay! :)
            $bgcolor = "#ffffcc";
          }
          if(isset($refs[$record[3]])){
            $refs[$record[3]]++;
          }
          else{
            $refs[$record[3]]=1;
          }
        }
      }
    }
    arsort($refs);
    foreach($refs as $ref => $sales){
      echo "
        <tr>
          <td bgcolor=$bgcolor><font size=1 face=arial><a href='mailto:$ref'>$ref</a></font></td>
          <td bgcolor=$bgcolor><font size=1 face=arial>$sales</td>
        </tr>
      ";
      if($bgcolor=="#ffffff"){
        $bgcolor = "#eeeeee";
      }
      else{
        $bgcolor = "#ffffff";
      }
    }
    echo "</table>$adminfooter";
    exit;
  }
  elseif($action=="adminextend"){
    # Extend duration of purchase download link.
    $id = $_REQUEST["id"];
    $records = @file($sys_template_folder . "ipn.txt");
    $recs = array();
    foreach($records as $rec){
      $rec = str_replace("\n", "", $rec);
      if(trim($rec)){
        $record = explode("|", $rec);
        if($record[0]==$id){
          # Extend this record.
          $changes = true;
          $record[9] += (3600 * $sys_expire_hours);
          $rec = "";
          for($i=0;$i<count($record);$i++){
            if($i<count($record)-1){
              $rec .= $record[$i] . "|";
            }
            else{
              $rec .= $record[$i];
            }
          }
          $recs[] = $rec;
        }
        else{
          $recs[] = $rec;
        }
      }
    }
    if($changes){
      # Update IPN file.
      $done = false;
      $fh = @fopen($sys_template_folder . "ipn.txt", "w+");
      while(!$done){
        $fl = flock($fh, LOCK_EX);
        if($fl){
          foreach($recs as $record){
            fwrite($fh, $record . "\n");
          }
          $done = true;
          flock($fh, LOCK_UN);
        }
      }
      @fclose($fh);
      echo "
        $adminheader
        <a href='$_SERVER[PHP_SELF]?action=adminmenu'>Back to Menu</a>
          <p>
          Link expiration has been extended by $sys_expire_hours hours.
          </p>
          <p>
          Give customer this link to download:
          </p>
          <p>
          http://$sys_domain" . $sys_script_folder . "?action=download&id=$id
          </p>
        $adminfooter
      ";
    }
    else{
      echo "
      <a href='$_SERVER[PHP_SELF]?action=adminmenu'>Back to Menu</a>
        <p>
        No matching purchase record found.
        </p>
      ";
    }
    exit;
  }
  elseif($action=="adminlogout"){
    # Logout.
    setcookie("admin", "", time() - 3600);
    header("Location: $_SERVER[PHP_SELF]");
    exit;
  }
}

# If we made it here, this needs to be cleared.
setcookie("first", 0, time()-3600, "/", "." . $sys_domain);

# Show appropriate page.
$fh = fopen($filename, "r");
while($s = fgets($fh)){
  echo $s;
}
fclose($fh);
?>
 
Related Solutions
Keywords: fgets and fclose errors
 
Loading Advertisement...
 
[+][-]06/03/07 05:40 PM, ID: 19205429Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: PHP Scripting Language
Tags: errors
Sign Up Now!
Solution Provided By: Vee_Mod
Participating Experts: 3
Solution Grade: B
 
[+][-]02/15/07 07:19 PM, ID: 18545871Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/15/07 07:21 PM, ID: 18545880Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/15/07 07:40 PM, ID: 18545939Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02/15/07 09:05 PM, ID: 18546278Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/18/07 07:49 AM, ID: 18558718Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02/23/07 04:07 PM, ID: 18600274Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02/23/07 05:45 PM, ID: 18600588Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/23/07 05:57 PM, ID: 18600635Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05/30/07 10:19 PM, ID: 19185645Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]06/02/07 09:29 AM, ID: 19201053Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92