|
From: <ma...@sc...> - 2006-04-14 14:14:41
|
Update of /cvsroot/meshdb/www/db2 In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7355 Modified Files: google.php Added Files: zipfile.php Log Message: added optional zipping ability to google.pgp google earth export function (disabled by default) and a php class to do zipping, from a freely available source. does the zipping in memory, no temporary files. --- NEW FILE: zipfile.php --- <?php /* * (freely) Gotten from : http://www.zend.com/zend/spotlight/creating-zip-files2.php */ /* Zip file creation class makes zip files on the fly... use the functions add_dir() and add_file() to build the zip file; see example code below by Eric Mueller http://www.themepark.com v1.1 9-20-01 - added comments to example v1.0 2-5-01 initial version with: - class appearance - add_file() and file() methods - gzcompress() output hacking by Denis O.Philippov, web...@at..., http://www.atlant.ru */ // official ZIP file format: http://www. // pkware.com/appnote.txt class zipfile { var $datasec = array(); // array to store compressed data var $ctrl_dir = array(); // central directory var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00"; //end of Central directory record var $old_offset = 0; function add_dir($name) // adds "directory" to archive - do this before putting any files in directory! // $name - name of directory... like this: "path/" // ...then you can add files using add_file with names like "path/file.txt" { $name = str_replace("\\", "/", $name); $fr = "\x50\x4b\x03\x04"; $fr .= "\x0a\x00"; // ver needed to extract $fr .= "\x00\x00"; // gen purpose bit flag $fr .= "\x00\x00"; // compression method $fr .= "\x00\x00\x00\x00"; // last mod time and date $fr .= pack("V",0); // crc32 $fr .= pack("V",0); //compressed filesize $fr .= pack("V",0); //uncompressed filesize $fr .= pack("v", strlen($name) ); //length of pathname $fr .= pack("v", 0 ); //extra field length $fr .= $name; // end of "local file header" segment // no "file data" segment for path // "data descriptor" segment (optional but necessary if archive is not served as file) $fr .= pack("V",$crc); //crc32 $fr .= pack("V",$c_len); //compressed filesize $fr .= pack("V",$unc_len); //uncompressed filesize // add this entry to array $this -> datasec[] = $fr; $new_offset = strlen(implode("", $this->datasec)); // ext. file attributes mirrors MS-DOS directory attr byte, detailed // at http://support.microsoft.com/support/kb/articles/Q125/0/19.asp // now add to central record $cdrec = "\x50\x4b\x01\x02"; $cdrec .="\x00\x00"; // version made by $cdrec .="\x0a\x00"; // version needed to extract $cdrec .="\x00\x00"; // gen purpose bit flag $cdrec .="\x00\x00"; // compression method $cdrec .="\x00\x00\x00\x00"; // last mod time & date $cdrec .= pack("V",0); // crc32 $cdrec .= pack("V",0); //compressed filesize $cdrec .= pack("V",0); //uncompressed filesize $cdrec .= pack("v", strlen($name) ); //length of filename $cdrec .= pack("v", 0 ); //extra field length $cdrec .= pack("v", 0 ); //file comment length $cdrec .= pack("v", 0 ); //disk number start $cdrec .= pack("v", 0 ); //internal file attributes $ext = "\x00\x00\x10\x00"; $ext = "\xff\xff\xff\xff"; $cdrec .= pack("V", 16 ); //external file attributes - 'directory' bit set $cdrec .= pack("V", $this -> old_offset ); //relative offset of local header $this -> old_offset = $new_offset; $cdrec .= $name; // optional extra field, file comment goes here // save to array $this -> ctrl_dir[] = $cdrec; } function add_file($data, $name) // adds "file" to archive // $data - file contents // $name - name of file in archive. Add path if your want { $name = str_replace("\\", "/", $name); //$name = str_replace("\\", "\\\\", $name); $fr = "\x50\x4b\x03\x04"; $fr .= "\x14\x00"; // ver needed to extract $fr .= "\x00\x00"; // gen purpose bit flag $fr .= "\x08\x00"; // compression method $fr .= "\x00\x00\x00\x00"; // last mod time and date $unc_len = strlen($data); $crc = crc32($data); $zdata = gzcompress($data); $zdata = substr( substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug $c_len = strlen($zdata); $fr .= pack("V",$crc); // crc32 $fr .= pack("V",$c_len); //compressed filesize $fr .= pack("V",$unc_len); //uncompressed filesize $fr .= pack("v", strlen($name) ); //length of filename $fr .= pack("v", 0 ); //extra field length $fr .= $name; // end of "local file header" segment // "file data" segment $fr .= $zdata; // "data descriptor" segment (optional but necessary if archive is not served as file) $fr .= pack("V",$crc); //crc32 $fr .= pack("V",$c_len); //compressed filesize $fr .= pack("V",$unc_len); //uncompressed filesize // add this entry to array $this -> datasec[] = $fr; $new_offset = strlen(implode("", $this->datasec)); // now add to central directory record $cdrec = "\x50\x4b\x01\x02"; $cdrec .="\x00\x00"; // version made by $cdrec .="\x14\x00"; // version needed to extract $cdrec .="\x00\x00"; // gen purpose bit flag $cdrec .="\x08\x00"; // compression method $cdrec .="\x00\x00\x00\x00"; // last mod time & date $cdrec .= pack("V",$crc); // crc32 $cdrec .= pack("V",$c_len); //compressed filesize $cdrec .= pack("V",$unc_len); //uncompressed filesize $cdrec .= pack("v", strlen($name) ); //length of filename $cdrec .= pack("v", 0 ); //extra field length $cdrec .= pack("v", 0 ); //file comment length $cdrec .= pack("v", 0 ); //disk number start $cdrec .= pack("v", 0 ); //internal file attributes $cdrec .= pack("V", 32 ); //external file attributes - 'archive' bit set $cdrec .= pack("V", $this -> old_offset ); //relative offset of local header // &n // bsp; echo "old offset is ".$this->old_offset.", new offset is $new_offset<br>"; $this -> old_offset = $new_offset; $cdrec .= $name; // optional extra field, file comment goes here // save to central directory $this -> ctrl_dir[] = $cdrec; } function file() { // dump out file $data = implode("", $this -> datasec); $ctrldir = implode("", $this -> ctrl_dir); return $data. $ctrldir. $this -> eof_ctrl_dir. pack("v", sizeof($this -> ctrl_dir)). // total # of entries "on this disk" pack("v", sizeof($this -> ctrl_dir)). // total # of entries overall pack("V", strlen($ctrldir)). // size of central dir pack("V", strlen($data)). // offset to start of central dir "\x00\x00"; // .zip file comment length } } ?> Index: google.php =================================================================== RCS file: /cvsroot/meshdb/www/db2/google.php,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- google.php 14 Apr 2006 13:08:20 -0000 1.4 +++ google.php 14 Apr 2006 14:14:29 -0000 1.5 @@ -1,18 +1,22 @@ <? include("config.php"); include("util.php"); - - /* declare content type */ - header("Content-Type: application/keyhole"); - //header("Content-Type: text/plain"); /* TEST ONLY */ + include("zipfile.php"); function nice_die($error) { + + /* return xml header */ + header("application/vnd.google-earth.kml+xml"); + + /* return error message*/ echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; echo "<kml xmlns=\"http://earth.google.com/kml/2.0\">\n"; echo " <Document>\n"; echo " <Folder><name>error: $error</name></Folder>\n"; echo " </Document>\n"; echo "</kml>\n"; + + /* give up, leaving only the error */ exit(0); } @@ -33,20 +37,23 @@ $maxdays = $HTTP_GET_VARS['maxdays']; } + /* send as zip file */ + $zip = 0; + if (isset($HTTP_GET_VARS['zip'])) { + $zip = $HTTP_GET_VARS['zip']; + } + /* Connect */ $db = mysql_connect($MYSQLHOST, $MYSQLUSER, $MYSQLPASS); mysql_select_db($MYSQLDB, $db) or nice_die(mysql_error($db)); - /* Last modified header */ + /* for last modified header */ $result = mysql_query("SELECT max(unix_timestamp(updated)) from admin", $db) or nice_die(mysql_error($db)); $row = mysql_fetch_row($result); - $updated = intval($row[0]); + $gmt_updated = intval($row[0]); - /* comment this out if testing */ - header("Last-Modified: ".gmdate("D, d M Y H:i:s", $updated)." GMT"); - /* give back memory */ mysql_free_result($result); @@ -62,10 +69,13 @@ $result = mysql_query($query, $db) or nice_die(mysql_error($db)); + /* kml file as a string */ + $kml_text = ""; + /* xml header (no nice_die() after here) */ - echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; - echo "<kml xmlns=\"http://earth.google.com/kml/2.0\">\n"; - echo "<Document>\n"; + $kml_text .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; + $kml_text .= "<kml xmlns=\"http://earth.google.com/kml/2.0\">\n"; + $kml_text .= "<Document>\n"; /* 0 used to mark "not found" later */ $rows = 0; @@ -115,51 +125,51 @@ } /* generate the next icon style */ - echo " <Style id=\"$validStyles[$i]\">\n"; - echo " <IconStyle id=\"iconStyle_$validStyles[$i]\">\n"; - echo " <Icon>\n"; - echo " <href>$pallette</href>\n"; - echo " <x>$offsetx</x>\n"; - echo " <y>$offsety</y>\n"; - echo " <w>$sizex</w>\n"; - echo " <h>$sizey</h>\n"; - echo " </Icon>\n"; - echo " </IconStyle>\n"; + $kml_text .= " <Style id=\"$validStyles[$i]\">\n"; + $kml_text .= " <IconStyle id=\"iconStyle_$validStyles[$i]\">\n"; + $kml_text .= " <Icon>\n"; + $kml_text .= " <href>$pallette</href>\n"; + $kml_text .= " <x>$offsetx</x>\n"; + $kml_text .= " <y>$offsety</y>\n"; + $kml_text .= " <w>$sizex</w>\n"; + $kml_text .= " <h>$sizey</h>\n"; + $kml_text .= " </Icon>\n"; + $kml_text .= " </IconStyle>\n"; /* default label seems to be fine, so no change for now */ - //echo " <LabelStyle id=\"labelStyle_$validStyles[$i]\">\n"; - //echo " <color>7fffaaff</color>\n"; - //echo " <scale>1.5</scale>\n"; - //echo " </LabelStyle>\n"; + //$kml_text .= " <LabelStyle id=\"labelStyle_$validStyles[$i]\">\n"; + //$kml_text .= " <color>7fffaaff</color>\n"; + //$kml_text .= " <scale>1.5</scale>\n"; + //$kml_text .= " </LabelStyle>\n"; /* style for line extrused from ground to $alt */ - echo " <LineStyle id=\"lineStyle_$validStyles[$i]\">\n"; - echo " <color>ff0000ff</color>\n"; - echo " <width>3</width>\n"; - echo " </LineStyle>\n"; - echo " </Style>\n"; + $kml_text .= " <LineStyle id=\"lineStyle_$validStyles[$i]\">\n"; + $kml_text .= " <color>ff0000ff</color>\n"; + $kml_text .= " <width>3</width>\n"; + $kml_text .= " </LineStyle>\n"; + $kml_text .= " </Style>\n"; } /* style for intra node links */ - echo " <Style id=\"linkStyle\">\n"; - echo " <LineStyle id=\"lineStyle_linkStyle\">\n"; - echo " <color>7fff0000</color>\n"; - echo " <width>3</width>\n"; - echo " </LineStyle>\n"; - echo " <IconStyle id=\"iconStyle_linkStyle\">\n"; - echo " <Icon>\n"; - echo " <href>root://icons/palette-5.png</href>\n"; - echo " <x>96</x>\n"; - echo " <y>64</y>\n"; - echo " <w>32</w>\n"; - echo " <h>32</h>\n"; - echo " </Icon>\n"; - echo " </IconStyle>\n"; - echo " </Style>\n"; + $kml_text .= " <Style id=\"linkStyle\">\n"; + $kml_text .= " <LineStyle id=\"lineStyle_linkStyle\">\n"; + $kml_text .= " <color>7fff0000</color>\n"; + $kml_text .= " <width>3</width>\n"; + $kml_text .= " </LineStyle>\n"; + $kml_text .= " <IconStyle id=\"iconStyle_linkStyle\">\n"; + $kml_text .= " <Icon>\n"; + $kml_text .= " <href>root://icons/palette-5.png</href>\n"; + $kml_text .= " <x>96</x>\n"; + $kml_text .= " <y>64</y>\n"; + $kml_text .= " <w>32</w>\n"; + $kml_text .= " <h>32</h>\n"; + $kml_text .= " </Icon>\n"; + $kml_text .= " </IconStyle>\n"; + $kml_text .= " </Style>\n"; /* default enclosing folder */ - echo "<Folder><name>brismesh.org</name>\n"; + $kml_text .= "<Folder><name>brismesh.org</name>\n"; while (($row = mysql_fetch_assoc($result))) { @@ -194,27 +204,27 @@ } /* display the row */ - echo " <Placemark>\n"; - echo " <description><![CDATA[Contact's Name: " . htmlspecialchars($row['contactname']) . "<br>View <a href=\"http://www.itee.uq.edu.au/~mesh/db-srtm/view.php?nodeid=".$row['nodeid']."\">Node Details</a> on BrisMESH<br>Last Updated: ".$updated."]]></description>\n"; - echo " <name><![CDATA[" . $row['nodename'] . "]]></name>\n"; - echo " <styleUrl>$style</styleUrl>\n"; - echo " <LookAt>\n"; - echo " <longitude>$lon</longitude>\n"; - echo " <latitude>$lat</latitude>\n"; - echo " <range>" . 500 . "</range>\n"; - echo " <tilt>0</tilt>\n"; - echo " <heading>3</heading>\n"; - echo " </LookAt>\n"; + $kml_text .= " <Placemark>\n"; + $kml_text .= " <description><![CDATA[Contact's Name: " . htmlspecialchars($row['contactname']) . "<br>View <a href=\"http://www.itee.uq.edu.au/~mesh/db-srtm/view.php?nodeid=".$row['nodeid']."\">Node Details</a> on BrisMESH<br>Last Updated: ".$updated."]]></description>\n"; + $kml_text .= " <name><![CDATA[" . $row['nodename'] . "]]></name>\n"; + $kml_text .= " <styleUrl>$style</styleUrl>\n"; + $kml_text .= " <LookAt>\n"; + $kml_text .= " <longitude>$lon</longitude>\n"; + $kml_text .= " <latitude>$lat</latitude>\n"; + $kml_text .= " <range>" . 500 . "</range>\n"; + $kml_text .= " <tilt>0</tilt>\n"; + $kml_text .= " <heading>3</heading>\n"; + $kml_text .= " </LookAt>\n"; /* point on the map */ - echo " <Point>\n"; - echo " <extrude>1</extrude>\n"; - echo " <altitudeMode>relativeToGround</altitudeMode>\n"; - echo " <coordinates>$lon,$lat,$alt</coordinates>\n"; - echo " </Point>\n"; + $kml_text .= " <Point>\n"; + $kml_text .= " <extrude>1</extrude>\n"; + $kml_text .= " <altitudeMode>relativeToGround</altitudeMode>\n"; + $kml_text .= " <coordinates>$lon,$lat,$alt</coordinates>\n"; + $kml_text .= " </Point>\n"; /* finish placemark */ - echo " </Placemark>\n"; + $kml_text .= " </Placemark>\n"; /* add links as placemarks */ if ($links == 1) { @@ -247,17 +257,17 @@ } /* emit the link placemark */ - echo " <Placemark>\n"; - echo " <name>link_".$row2['nodeid']."_to_".$row2['peerid']."</name>\n"; - echo " <styleUrl>#linkStyle</styleUrl>\n"; - echo " <LineString>\n"; - echo " <altitudeMode>relativeToGround</altitudeMode>\n"; - echo " <coordinates>\n"; - echo " $lon1, $lat1, $alt1\n"; - echo " $lon2, $lat2, $alt2\n"; - echo " </coordinates>\n"; - echo " </LineString>\n"; - echo " </Placemark>\n"; + $kml_text .= " <Placemark>\n"; + $kml_text .= " <name>link_".$row2['nodeid']."_to_".$row2['peerid']."</name>\n"; + $kml_text .= " <styleUrl>#linkStyle</styleUrl>\n"; + $kml_text .= " <LineString>\n"; + $kml_text .= " <altitudeMode>relativeToGround</altitudeMode>\n"; + $kml_text .= " <coordinates>\n"; + $kml_text .= " $lon1, $lat1, $alt1\n"; + $kml_text .= " $lon2, $lat2, $alt2\n"; + $kml_text .= " </coordinates>\n"; + $kml_text .= " </LineString>\n"; + $kml_text .= " </Placemark>\n"; } @@ -277,14 +287,41 @@ /* if we got no rows, display an error */ if ($rows == 0) { if (isset($nodeid)) { - echo "<Folder><name>Node ".$nodeid." not valid.</name></Folder>"; + $kml_text .= "<Folder><name>Node ".$nodeid." not valid.</name></Folder>"; } else { - echo "<Folder><name>no results available</name></Folder>"; + $kml_text .= "<Folder><name>no results available</name></Folder>"; } } /* xml footer */ - echo "</Folder>\n"; - echo "</Document>\n"; - echo "</kml>\n"; + $kml_text .= "</Folder>\n"; + $kml_text .= "</Document>\n"; + $kml_text .= "</kml>\n"; + + /* cache control - comment out if testing */ + header("Last-Modified: ".gmdate("D, d M Y H:i:s", $gmt_updated)." GMT"); + + if ($zip == 0) { + + /* send headers */ + header("application/vnd.google-earth.kml+xml"); + + /* send plain text */ + echo $kml_text; + + } else { + + /* instantiate the zipfile */ + $theZip = new zipfile; + + /* add kml data */ + $theZip->add_file($kml_text, "doc.kml"); + + /* content type */ + header('Content-Type: application/vnd.google-earth.kmz'); + + /* send zip */ + print ($theZip->file); + + } ?> |