php code error? [Answered]RSS

1 reply

Last post Aug 25, 2008 11:10 AM by Zhao Ji Ma - MSFT

  • php code error?

    Aug 20, 2008 03:54 PM|patfee|LINK

    Hi,

    could anyone please see if the following scrip should normally run on IIS6?

    I have a setup similar as explained in http://www.simongibson.com/intranet/php2003/

    <?php
    // process the post message from ShipPlotter share
    // this script is an example of how you might deal with the post from ShipPlotter

    // Most settings are located in shipinfo.inc.php.


    include 'shipinfo.inc.php';


    if ($filterwindow) {
        $LatN=$_POST['LatN'];
        $LatS=$_POST['LatS'];
        $LonE=$_POST['LonE'];
        $LonW=$_POST['LonW']; // used to window the data sent back
        // plus a 50% margin around the window
        $LatN += 0.5*($LatN-$LatS);
        $LatS -= 0.333*($LatN-$LatS); // 0.333 because we already scaled up LatN
        $LonE += 0.5*($LonE-$LonW);
        $LonW -= 0.333*($LonE-$LonW); // ditto
        if ($LatN > 90) $LatN = 90;
        if ($LatS < -90) $LatS = -90;
        if ($LonE > 180) $LonE = 180;
        if ($LonW < -180) $LonW = -180;
        $windowcond = "lat > '$LatS' AND lat < '$LatN' AND lon > '$LonW' AND lon < '$LonE'";
    }

    $Lines = 0;
    $Lines=$_POST['Lines'];
    $Reg=$_POST['Reg'];
    $Uponly = 0;
    $Uponly = $_POST['Uponly'];
    $Extracode=chr($_POST['Extracode']);
    $lastconn = 0;


    // echo "Connecting...";
    @mysql_connect($dbserver,$username,$password) or die('Could not connect to database: ' . mysql_error());
    @mysql_select_db($database) or die( "Could not select database: ".mysql_error());

    // Access control
    if ($accesscontrol) {
        include 'access_db.php';
        $access = allow_access($emailadr);
        // echo 'Access = ' . $access . ' Lastconn = ' . $lastconn;
        if ($access < 1) {
            die("Access denied");
        }
    }

    // Setting up output buffering
    ob_start("ob_gzhandler");

    // read in the given number of lines
    for ($i = 1; $i <= $Lines; ++$i)
    {
        $lhead = "line".sprintf("%04d",$i); // the line header of the ith line
        $ldata = $_POST[$lhead]; // the content

        $search = array ('/\$/','/\\\\/','/{/','/}/','/"/','/\'/');
        $replace= array ('-'   ,'/'     ,'('  ,')'  ,' '  ,' '   );
        $ldata = preg_replace($search, $replace, $ldata);

        if (preg_match("/^[\040-\172\300-\377]+$/", $ldata))
        //A more restricted version:
        //if (preg_match("/^[-*:&)(\.\w\[\] 0-9]+$/", $ldata))
        {
          // unpack the variables
          $r_mmsi = (int)0;
          $r_mtime = (int)0;
          $r_status = (int)0;
          $r_type = (int)0;
          $r_lat = (float)0;
          $r_lon = (float)0;
          $r_speed = (float)0;
          $r_course = (float)0;
          $r_heading = (int)0;
          $r_draft = (float)0;
          $r_length = (int)0;
          $r_width = (int)0;
          $r_imo = (int)0;
          $r_l1 = (int)0;
          $r_w1 = (int)0;
          sscanf($ldata,"%d %d %d %d %f %f %f %f %d %f %d %d",$r_mmsi,$r_mtime,$r_status,$r_type,$r_lat,$r_lon,$r_speed,$r_course,$r_heading,$r_draft,$r_length,$r_width);
          if (($r_mmsi < 999999999) &&
              ($r_mmsi > 0) &&
              ($r_lat > -90) &&
              ($r_lon > -180) &&
              ($r_lat < 90) &&
              ($r_lon < 180))  // ignore obviously false data
          {
            $nameoffset = 78; // this is a kludge to fix a shift problem with navaids (type no is 3 digits instead of 2)
            if ($r_type > 99)
               $nameoffset = 79;
            $r_name = substr($ldata,$nameoffset,20);
            // need to defend against leading spaces in the name
            $r_name = trim($r_name);
            $r_name = substr($r_name."                    ",0,20);
            $r_call = substr($ldata,$nameoffset+21,7); // +21
            $r_dest = substr($ldata,$nameoffset+29,20); // +29
            $r_eta  = substr($ldata,$nameoffset+50,11); // +50
            // the imo number and the extra dimension terms are after all the strings for upward compatibility
            $ldata2 = substr($ldata,$nameoffset+62);

            sscanf($ldata2,"%d %d %d",$r_imo,$r_l1,$r_w1);

            if($r_imo > 9999999)
            {
                 $r_imo /= 100; // deal with incorrectly entered IMO numbers
            }
            // now look for that ship in our database indexed by mmsi
            $query="SELECT * FROM shipinfo WHERE mmsi='$r_mmsi'";
            $result=mysql_query($query);
            $num = 0;
            if (!$result)
            {
              //mysql_close();
              die("DB query failed.");
            }
            else
            {
              $num=mysql_num_rows($result);
            }
            if ($num == 0)
            {
               // this is a new ship to us
               $sql = "INSERT INTO `shipinfo` (`mmsi`,`mtime`,`status`,`type`,`lat`,`lon`,`speed`,`course`,`heading`,`draft`,`length`,`width`,`name`,`call`,`dest`,`eta`,`reg`,`imo`,`l1`,`w1`,`extracode`) VALUES ('$r_mmsi','$r_mtime','$r_status','$r_type','$r_lat','$r_lon','$r_speed','$r_course','$r_heading','$r_draft','$r_length','$r_width','$r_name','$r_call','$r_dest','$r_eta','$Reg','$r_imo','$r_l1','$r_w1','$Extracode');";
               $result=mysql_query($sql);
            }
            if ($num > 0)
            {
              // we know this ship.  How do the times compare?
              $mtime=mysql_result($result,0,"mtime");
              if ($mtime > $r_mtime)
              {
                // ours is more recent than theirs
              }
              else
              {
                // theirs is more recent than ours
                $sql = "UPDATE shipinfo SET mmsi = '$r_mmsi', mtime = '$r_mtime', status = '$r_status', type = '$r_type', lat = '$r_lat', lon = '$r_lon', speed = '$r_speed', course = '$r_course', heading = '$r_heading', draft = '$r_draft', length = '$r_length', width = '$r_width', name = '$r_name', call = '$r_call', dest = '$r_dest', eta = '$r_eta', reg = '$Reg', imo = '$r_imo' , l1 = '$r_l1' , w1 = '$r_w1', extracode = '$Extracode' WHERE mmsi = '$r_mmsi'";
                $result=mysql_query($sql);
              }
            }
          }
       }
    }

    // now we have read all the arguments and updated the database
    // now we must delete any stale ships
    $mintime = 300;
    $maxtime = 300;
    $sql="DELETE FROM `shipinfo` WHERE mtime < (UNIX_TIMESTAMP()-$mintime) OR mtime > (UNIX_TIMESTAMP()+$maxtime)";
    $result=mysql_query($sql);

    // now we must output the database if not Uponly

    $query="SELECT * FROM shipinfo WHERE reg != '$Reg'";

    if ($filterwindow)
        $query=$query . " AND " . $windowcond;

    if ($filterlastconn)
        $query=$query . " AND timestamp >= '$lastconn'";

    $num = 0;
    if (($Uponly == 0) && ($Reg != 0) && ($Reg != -1))
    {
        $result=mysql_query($query);
        if (!$result)
        {
            //mysql_close();
            die("DB query failed.");
        }
        else
        {
            $num=mysql_num_rows($result);
        }
    }

    //echo "Variables = $num\n";
    for ($i = 0 ; $i < $num ; $i++)
       {
           $r_mmsi=mysql_result($result,$i,"mmsi");
           $r_mtime=mysql_result($result,$i,"mtime");
           $r_status=mysql_result($result,$i,"status");
           $r_type=mysql_result($result,$i,"type");
           $r_lat=mysql_result($result,$i,"lat");
           $r_lon=mysql_result($result,$i,"lon");
           $r_speed=mysql_result($result,$i,"speed");
           $r_course=mysql_result($result,$i,"course");
           $r_heading=mysql_result($result,$i,"heading");
           $r_draft=mysql_result($result,$i,"draft");
           $r_length=mysql_result($result,$i,"length");
           $r_width=mysql_result($result,$i,"width");
           $r_name=mysql_result($result,$i,"name");
           $r_call=mysql_result($result,$i,"call");
           $r_dest=mysql_result($result,$i,"dest");
           $r_eta=mysql_result($result,$i,"eta");
           $r_reg=mysql_result($result,$i,"reg");
           $r_imo=mysql_result($result,$i,"imo");
           $r_l1=mysql_result($result,$i,"l1");
           $r_w1=mysql_result($result,$i,"w1");
           $r_extracode=mysql_result($result,$i,"extracode");

           // to identify the sharing source, we will hash the reg number into a letter and put it at the end of the destination
           $hash = 0;
           $t = $r_reg % 16;
           $hash ^= $t;
           $r_reg /= 16;
           $t = $r_reg % 16;
           $hash ^= $t;
           $r_reg /= 16;
           $t = $r_reg % 16;
           $hash ^= $t;
           $r_reg /= 16;
           $t = $r_reg % 16;
           $hash ^= $t;
           $hash += 97; // to make a lower case letter starting with a
           if ($hash > 122) $hash = 122; // just in case

           //You need this in somewhere to avoid a null character in the response string
           if ($r_extracode == "\0")
               $r_extracode = " ";

           if (phpversion() < "4.3.10.10") {
               // For elder PHP versions
               printf("%09d %010d %02d %02d %3.6lf %4.6lf %03.1lf %03.1lf %03d %02.1lf %03d %02d %-20s %-7s %-20s %-11s %-1s %07d %03d %02d %-1s\n",$r_mmsi,$r_mtime,$r_status,$r_type,$r_lat,$r_lon,$r_speed,$r_course,$r_heading,$r_draft,$r_length,$r_width,$r_name,$r_call,$r_dest,$r_eta,chr($hash),$r_imo,$r_l1,$r_w1,$r_extracode);
                      // Important note:
                      // Some versions of PHP treat the double field specifiers differently.
                      // As shown above, %3.6lf means 3 digits before and 6 digits after the decimal point.
                      // This is contrary to the PHP spec but conforms to the way earlier versions of PHP behave.
                      // The version below (commented out) uses %10.6 to mean the same thing
                      // that is 10 digits altogether and six after the decimal point.
                      // If the version above fails, try the version below.
           } else {
               // For recent PHP versions
               printf("%09d %010d %02d %02d %10.6lf %11.6lf %05.1lf %05.1lf %03d %04.1lf %03d %02d %-20s %-7s %-20s %-11s %-1s %07d %03d %02d %-1s\n",$r_mmsi,$r_mtime,$r_status,$r_type,$r_lat,$r_lon,$r_speed,$r_course,$r_heading,$r_draft,$r_length,$r_width,$r_name,$r_call,$r_dest,$r_eta,chr($hash),$r_imo,$r_l1,$r_w1,$r_extracode);
           }
        }



    // Updating user statistics

    if ($accesscontrol) {
        update_user_stats($Reg, $Extracode, $Lines, $num);
        //update_user_stats($Reg, $Lines, $num);
    }


    ob_end_flush();


    // Some diagnostics when viewed from a browser (access control disabled)
    if (!strstr($_SERVER['HTTP_USER_AGENT'], 'ShipPlotter')) {
        echo "The information can not be viewed with a browser.<p>";
        $query="SELECT COUNT(*) FROM `shipinfo` WHERE 1";
        $result=mysql_query($query);
        if (!$result) {
            $error = mysql_error();
            echo "The shipinfo table can not be accessed: $error<p>";
        } else {
            $total=mysql_result($result,0,"COUNT(*)");
            echo "There are $total entries in the shipinfo table.<p> The script seems to be working.<p>";
        }
    }


    mysql_close();
    ?>
     

     

  • Re: php code error?

    Aug 25, 2008 11:10 AM|Zhao Ji Ma - MSFT|LINK

    Hi, 

    If you meant to run the same script, you can surely run it on IIS6. Do you have specific error message?

    Zhao Ji Ma
    Sincerely,
    Microsoft Online Community Support