Benutzer:Antonsusi/skyhack.php

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
<?PHP
ob_start("ob_gzhandler");
#error_reporting( E_ALL );
#ini_set("display_errors",1);

/**
 skyhack by user:kolossos@wikipedia  Released under GPL
based on geohack (c)  by
Magnus Manske (2006)
 Released under GPL
 geo_param.php is (c) 2005, Egil Kvaleberg <egil@kvaleberg.no>, also GPL
*/

#include "../common.php" ;
#ini_set('user_agent','MSIE 4\.0b2;'); # Fake user agent
ini_set('user_agent','skyhack'); # Fake user agent

#include "geo_param.php" ;
#include "mapsources.php" ;

$name=addslashes($_GET[name]);
$object=addslashes($_GET[object]);
if ($object=="") {$object=$name; }

$name_url=str_replace(" ","",$name);
$ra=addslashes($_GET[ra]);            #Rektaszension hh mm ss
$de=addslashes($_GET[de]);            #Deklination    +/-ddd mm ss

$lon=15*$ra - 180;
$lat=$de;

$size=addslashes($_GET[size]);       #App-dim in degrees   (beta)
if ( $size==''){$size=0.01;}

$size_minute=$size*60;
$beta=0.0087266*$size;
$range = 6378000 *(1.1917536 * sin($beta) - cos($beta) + 1); 
$z=floor(-log($size/90,2));

if ($size_minute < 10)
{   $radius = 12;}
else {
if ($size_minute > 50){$radius = 60;} else {$radius= 1.2 * $size_minute;}
}



function fix_language_code ( $lang , $default = "en" ) {
  $lang = trim ( strtolower ( $lang ) ) ;
  if ( preg_match ( "/^([\-a-z]+)/" , $lang , $l ) ) {
    $lang = $l[0] ;
  } else $lang = $default ; // Fallback
  return $lang ;
}

function get_request ( $key , $default = "" ) {
    global $prefilled_requests ;
    if ( isset ( $prefilled_requests[$key] ) ) return $prefilled_requests[$key] ;
    if ( isset ( $_REQUEST[$key] ) ) return str_replace ( "\'" , "'" , $_REQUEST[$key] ) ;
    return $default ;
}



set_time_limit ( 20 ) ; # 20 sec should be enough for everyone!

$lang = fix_language_code ( get_request ( 'language' , 'en' ) , '' ) ;
if ( $lang == '' ) {
    print "No language given!" ;
    exit ;
}

$test = isset ( $_REQUEST['test'] ) ;


# Read template
$pagename = "Template:SkyhackTool" ;
#$pagename = "User:Magnus_Manske/skyhack" ;
#$pagename = get_request ( "altpage" , "Template:GeoTemplate" ) ;
$page = @file_get_contents ( "http://meta.wikimedia.org/wiki/$pagename?action=render" ) ;

if ( false === $page || '' == $page ) {
    print "Failed to open template. Please try again in a moment." ;
    exit ;
}

$page = str_replace ( ' href="/w' , " href=\"http://{$lang}.wikipedia.org/w" , $page ) ;

# Separate content
#$page = array_pop ( explode ( '<!-- start skyhack -->' , $page , 2 ) ) ;
#$page = array_shift ( explode ( '<!-- end skyhack -->' , $page , 2 ) ) ;

# Remove edit links
do {
    $op = $page ;
    $p = explode ( '<span class="editsection"' , $page , 2 ) ;
    if ( count ( $p ) == 1 ) continue ;
    $page = array_shift ( $p ) ;
    $p = explode ( '</span>' , array_pop ( $p ) , 2 ) ;
    $page .= array_pop ( $p ) ;
} while ( $op != $page ) ;

# Replace text
#$md = new map_sources ( $theparams , "Some title" ) ;

for ( $a = 1 ; $a <= 5 ; $a++ ) {
        $k = 'viz' . $a ;
        $k2 = '{' . $k . '}' ;
        $k3 = '{' . $k . 'text}' ;
        if ( isset ( $_REQUEST[$k] ) ) {
                $page = str_replace ( $k2 , urlencode ( $_REQUEST[$k] ) , $page ) ;
                $page = str_replace ( $k3 , $_REQUEST[$k] , $page ) ;
        } else {
                $p = explode ( $k2 , $page , 2 ) ;
                if ( count ( $p ) != 2 ) continue ;
                $p1 = explode ( '<li>' , $p[0] ) ;
                $p2 = explode ( '</li>' , $p[1]) ;
                array_pop ( $p1 ) ;
                array_shift ( $p2 ) ;
                $page = implode ( '<li>' , $p1 ) ;
                $page .= implode ( '</li>' , $p2 ) ;
                $page = str_replace ( $k2 , urlencode ( $_REQUEST[$k] ) , $page ) ;
        }
}


$page = str_replace ( '{ra}' ,$ra, $page ) ;
$page = str_replace ( '{de}' ,$de, $page ) ;
$page = str_replace ( '{latdegdec}' ,$lat, $page ) ;
$page = str_replace ( '{londegdec}' ,$lon, $page ) ;
$page = str_replace ( '{name}' ,$name, $page ) ;
$page = str_replace ( '{name_url}' ,$name_url, $page ) ;
$page = str_replace ( '{title}' ,$name, $page ) ;
$page = str_replace ( '{object}' ,$object, $page ) ;

$page = str_replace ( '{size}' ,$size, $page ) ;
$page = str_replace ( '{size_minute}' ,$size_minute, $page ) ;
$page = str_replace ( '{z}' ,$z, $page ) ;
$page = str_replace ( '{range}' ,$range, $page ) ;
$page = str_replace ( '{radius}' ,$radius, $page ) ;


# Output
print "<html><head><title>Mapsources for the sky</title>" ;
print "<meta http-equiv='content-type' content='text/html; charset=utf-8'>" ;
print '<link rel="stylesheet" type="text/css" media="screen" href="./main.css" >' ;
print $stylesheets ;
print '</head><body style="margin-left:5px; margin-right:5px;">' ;
#print get_common_header ( "geohack.php" ) ;
print '

<div style="float:right">
 <a href="http://tools.wmflabs.org">
 <img style="vertical-align:top"  height="40px" border="0" alt="Powered by WMF Labs" src="//wikitech.wikimedia.org/w/images/c/cf/Labslogo_thumb.png" />
 </a>
</div>

<span style="left:5px;top:2px;position:relative;border:2px solid red;display:inline;float:left;padding:2px;font-size:150%;background-color:white"><a target="_blank" href="http://wikimediafoundation.org/wiki/Fundraising">Donate to Wikimedia!</a></span>

<center style="width:100%; border-bottom:1px solid #AAAAAA;margin-bottom:3px;padding:2px;background-color:#AAFFAA">
This is based on the <i>mapsources</i> extension by <a href="http://en.wikipedia.org/wiki/User:Egil">en:User:Egil</a>, extended by <a href="http://en.wikipedia.org/wiki/User:Magnus_Manske">Magnus Manske</a> and <a href="http://de.wikipedia.org/wiki/Benutzer:Kolossos">User:Kolossos</a>.
<br/>
For the source of this script, see
<a href="./skyhack-source.php">skyhack-source.php</a>.
</center>
' ;

print '
<div id="globalWrapper">
<div>

<div style="float:left">
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/0/07/Orion_Nebula_-_Hubble_2006_mosaic.jpg/140px-Orion_Nebula_-_Hubble_2006_mosaic.jpg" />
</div>

<div id="content" style="margin-top:0px;margin-left:145px">
<div id="bodyContent">
<h1>Map sources/SkyHack</h1>
' ;

print $page ;

print '
</div>
</div>
</div>
</div>
</body>
</html>
' ;

?>
<?PHP


/**
 *   Parse geo parameters
 */
class geo_param {
        var $latdeg;
        var $londeg;

        var $latdeg_min;
        var $londeg_min;
        var $latdeg_max;
        var $londeg_max;

        var $pieces;
        var $error;
        var $coor;
        var $title;

        /**
         *   Constructor:
         *   Read coordinates, and if there is a range, read the range
         */
        function geo_param( $param )
        {
                $this->pieces = explode(" ", str_replace ( ' O' , ' E' , str_replace( '_', ' ', $param )));
                $this->get_coor( );

                $this->latdeg_min = $this->latdeg_max = $this->latdeg;
                $this->londeg_min = $this->londeg_max = $this->londeg;
                if ( isset( $this->pieces[0] ) && $this->pieces[0] == "to") {
                        array_shift($this->pieces);
                        $this->get_coor();
                        if ($this->latdeg < $this->latdeg_max) {
                                $this->latdeg_min = $this->latdeg;
                        } else {
                                $this->latdeg_max = $this->latdeg;
                        }
                        if ($this->londeg < $this->londeg_max) {
                                $this->londeg_min = $this->londeg;
                        } else {
                                $this->londeg_max = $this->londeg;
                        }
                        $this->latdeg = ($this->latdeg_max+$this->latdeg_min) / 2;
                        $this->londeg = ($this->londeg_max+$this->londeg_min) / 2;
                        $this->coor = array();
                }
        }

        /**
         *  Private:
         *  Get a set of coordinates from parameters
         */
        function get_coor( ) {
                if ($i = strpos($this->pieces[0],';')) {
                        /* two values seperated by a semicolon */
                        $this->coor = array(
                                $this->latdeg = substr($this->pieces[0],0,$i),
                                $this->londeg = substr($this->pieces[0],$i+1));
                        array_shift($this->pieces);
                        $latNS = 'N';
                        $lonEW = 'E';
                        $latmin = $lonmin = $latsec = $lonsec = 0;
                } elseif ($this->is_coor($this->pieces[1],$this->pieces[3])) {
                        $this->coor = array(
                                $this->latdeg = array_shift($this->pieces),
                                $latNS        = array_shift($this->pieces),
                                $this->londeg = array_shift($this->pieces),
                                $lonEW        = array_shift($this->pieces));
                        $latmin = $lonmin = $latsec = $lonsec = 0;
                } elseif ($this->is_coor($this->pieces[2],$this->pieces[5])) {
                        $this->coor = array(
                                $this->latdeg = array_shift($this->pieces),
                                $latmin       = array_shift($this->pieces),
                                $latNS        = array_shift($this->pieces),
                                $this->londeg = array_shift($this->pieces),
                                $lonmin       = array_shift($this->pieces),
                                $lonEW        = array_shift($this->pieces));
                        $latsec = $lonsec = 0;
                } elseif ($this->is_coor($this->pieces[3],$this->pieces[7])) {
                        $this->coor = array(
                                $this->latdeg = array_shift($this->pieces),
                                $latmin       = array_shift($this->pieces),
                                $latsec       = array_shift($this->pieces),
                                $latNS        = array_shift($this->pieces),
                                $this->londeg = array_shift($this->pieces),
                                $lonmin       = array_shift($this->pieces),
                                $lonsec       = array_shift($this->pieces),
                                $lonEW        = array_shift($this->pieces));
                } else {
                        # support decimal, signed lat, lon
                        $this->error = "Unrecognized format";
                        print $this->error ;
                }


                if ($this->latdeg >  90 or $this->latdeg <  -90
                 # FIXME: Other planetary bodies allow for latitudes greater than 180
                 or $this->londeg > 360 or $this->londeg < -360
                 or $latmin       >  60 or $latmin       <    0
                 or $lonmin       >  60 or $lonmin       <    0
                 or $latsec       >  60 or $latsec       <    0
                 or $lonsec       >  60 or $lonsec       <    0) {
                        $this->error = "Out of range";
                }

                $latfactor = 1.0 ;
                $lonfactor = 1.0 ;
                if (strtoupper($latNS) == "S") {
                        $latfactor = -1.0 ;
                        #$this->latdeg = -$this->latdeg;
                }

                if (strtoupper($lonEW) == "W") {
                        $lonfactor = -1.0 ;
                        #$this->londeg = -$this->londeg;
                }

                # Make decimal degree, if not already
                $latmin += $latsec/60.0;
                $lonmin += $lonsec/60.0;
                if ($this->latdeg < 0) {
                        $this->latdeg -= $latmin/60.0;
                } else {
                        $this->latdeg += $latmin/60.0;
                }
                if ($this->londeg < 0) {
                        $this->londeg -= $lonmin/60.0;
                } else {
                        $this->londeg += $lonmin/60.0;
                }
                $this->latdeg *= $latfactor ;
                $this->londeg *= $lonfactor ;
        }

        /**
         *   Given decimal degrees, convert to 
         *   minutes, seconds and direction
         */
        function make_minsec( $deg )
        {
                if ( $deg >= 0) {
                        $NS = "N";
                        $EW = "E";
                } else {
                        $NS = "S";
                        $EW = "W";
                }
                # Round to a suitable number of digits
                # FIXME: should reflect precision
                $deg = round($deg, 6);
                $min = 60.0 * (abs($deg) - intval(abs($deg))); 
                $min = round($min, 4);
                $sec = 60.0 * ($min - intval($min));
                $sec = round($sec, 2);

                return array(
                        'deg'   => $deg,
                        'min'   => $min,
                        'sec'   => $sec,
                        'NS'    => $NS,
                        'EW'    => $EW);
        }

        /**
         *   Given decimal degrees latitude and longitude, convert to
         *   string
         */
        function make_position( $lat, $lon )
        {
                $latdms = geo_param::make_minsec( $lat );
                $londms = geo_param::make_minsec( $lon );
                $outlat = intval(abs($latdms['deg'])) . "&deg;&nbsp;";
                $outlon = intval(abs($londms['deg'])) . "&deg;&nbsp;";
                if ($latdms['min'] != 0 or $londms['min'] != 0
                 or $latdms['sec'] != 0 or $londms['sec'] != 0) {
                        $outlat .= intval($latdms['min']) . "&prime;&nbsp;";
                        $outlon .= intval($londms['min']) . "&prime;&nbsp;";
                        if ($latdms['sec'] != 0 or $londms['sec'] != 0) {
                                $outlat .= $latdms['sec']. "&Prime;&nbsp;";
                                $outlon .= $londms['sec']. "&Prime;&nbsp;";
                        }
                }
                return $outlat . $latdms['NS'] . " " . $outlon . $londms['EW'];
        }

        /**
         *  Get the additional attributes in an associative array
         */
        function get_attr()
        {
                $a = array();
                while (($s = array_shift($this->pieces))) {
                        if (($i = strpos($s,":")) >= 1) {
                                $attr = substr($s,0,$i);
                                $val = substr($s,$i+1);
                                if (($j = strpos($val,"("))
                                 && ($k = strpos($val,")"))
                                 && ($k > $j)) {
                                        $a["arg:".$attr] = substr($val,$j+1,$k-($j+1));
                                        $val = substr($val,0,$j);
                                }
                                $a[$attr] = $val;
                        } elseif (intval($s) > 0) {
                            if ( isset($a['$scale']) and $a['$scale'] != "")
                                $a['scale'] = intval($s);
                        }
                }
                return $a;
        }

        function is_coor( $ns,$ew )
        {
                $ns = strtoupper($ns);
                $ew = strtoupper($ew);
                return (($ns=="N" or $ns=="S") and
                        ($ew=="E" or $ew=="W"));
        }

        function frac( $f)
        {
                return abs($f) - abs(intval($f));
        }

        /**
         *  Get composite position in RFC2045 format
         */
        function get_position( )
        {
                return $this->latdeg.";".$this->londeg;
        }

        /**
         *  Get error message that applies, or "" of all is well
         */
        function get_error()
        {
                if ($this->error != "") {
                        return "Error:".$this->error;
                }
                return "";
        }

        /**
         *  Produce markup suitable for use in page
         *  Use original content as much as possible
         */
        function get_markup() 
        {
                $n = count($this->coor);

                if ($n == 0) {
                        # Range is special case
                        return $this->make_position( $this->latdeg_min, 
                                                     $this->londeg_min )
                             . " to "
                             . $this->make_position( $this->latdeg_max,
                                                     $this->londeg_max );
                } elseif ($n == 2) {
                        return $this->coor[0].';'.
                               $this->coor[1];

                } elseif ($n == 4) {
                        return $this->coor[0].'&deg;&nbsp;'.
                               $this->coor[1].' '.
                               $this->coor[2].'&deg;&nbsp;'.
                               $this->coor[3];

                } elseif ($n == 6) {
                        return $this->coor[0].'&deg;'.
                               $this->coor[1].'&prime;&nbsp;'.
                               $this->coor[2].' '.
                               $this->coor[3].'&deg;'.
                               $this->coor[4].'&prime;&nbsp;'.
                               $this->coor[5];

                } elseif ($n == 8) {
                        return $this->coor[0].'&deg;'.
                               $this->coor[1].'&prime;'.
                               $this->coor[2].'&Prime;&nbsp;'.
                               $this->coor[3].' '.
                               $this->coor[4].'&deg;'.
                               $this->coor[5].'&prime;'.
                               $this->coor[6].'&Prime;&nbsp;'.
                               $this->coor[7];
                } else {
                        return $this->get_error();
                }
        }
}

?>