Igor Gariev (gariev) wrote in changelog,
Igor Gariev
gariev
changelog

[livejournal] r17451: LJSUP-6862: Add ability to insert maps (...

Committer: gariev
LJSUP-6862: Add ability to insert maps (Google&Yandex) into the posts

A   trunk/cgi-bin/LJ/Maps.pm
U   trunk/cgi-bin/cleanhtml.pl
U   trunk/htdocs/tools/embedcontent.bml
Added: trunk/cgi-bin/LJ/Maps.pm
===================================================================
--- trunk/cgi-bin/LJ/Maps.pm	                        (rev 0)
+++ trunk/cgi-bin/LJ/Maps.pm	2010-09-27 09:17:49 UTC (rev 17451)
@@ -0,0 +1,107 @@
+package LJ::Maps;
+use strict;
+use URI;
+
+##
+## input: attribute hash for <lj-map /> tag (url, width, height)
+## outpus: html code
+## method is called from html cleaner
+##
+sub expand_ljmap_tag {
+    my $class = shift;
+    my $attr = shift;
+
+    if (my $url = $attr->{'url'}) {
+        my $uri = URI->new($url);
+        if ($uri->can('host')) {
+            my $host = $uri->host;
+            my %url_params = $uri->query_form;
+            my $iframe_url;
+            my $width = LJ::ehtml($attr->{'width'}) || 500;
+            my $height = LJ::ehtml($attr->{'height'}) || 350;
+            if ($host eq 'maps.google.com') {
+                $iframe_url = URI->new("http://maps.google.com/");
+                $iframe_url->query_form(
+                    ll      => $url_params{'ll'},
+                    spn     => $url_params{'spn'},
+                    z       => $url_params{'z'},
+                    output  => "embed"
+                );
+            } elsif ($host eq 'maps.yandex.ru') {
+                $iframe_url = 
+                    "http://$LJ::EMBED_MODULE_DOMAIN?mode=lj-map&url=" . LJ::eurl($url) 
+                    . "&width=" . LJ::eurl($width) . "&height=" . LJ::eurl($height);
+            }
+            
+            if ($iframe_url) {
+                return "<iframe src='$iframe_url' width='$width' height='$height' frameborder='0' style='border: 0;'></iframe>";
+            }
+        }
+    }
+
+    return "[error: invalid lj-map tag]";
+}
+
+##
+## input: hash of options (url, width, height)
+## output: html code for <iframe> with Yandex Map
+## method is called from htdocs/tools/embedcontent.bml
+## 
+sub get_iframe_source {
+    my $class = shift;
+    my %opts = @_;
+    
+    ## http://maps.yandex.ru/?ll=37.580238%2C55.749544&spn=0.010504%2C0.00311&z=17&l=map
+    my $uri = URI->new($opts{'url'});
+    if ($uri->can('host') && $uri->host eq 'maps.yandex.ru') {
+        my $key;
+        {
+            my @domains = split /\./, LJ::Request->header_in("Host");
+            if (@domains>=2) {
+                my $subdomain = "$domains[-2].$domains[-1]";
+                warn $subdomain;
+                $key = $LJ::YANDEX_MAPS_API_KEYS{$subdomain};
+            }
+        }
+        return "[error: no Yandex Map API key found]"
+            unless $key;
+
+        my %url_params = $uri->query_form;
+        my ($x, $y) = split /,/, $url_params{'ll'};
+        my $zoom    = $url_params{'z'};
+        $x =~ s/[^\d\.\-\+]//g; $y =~ s/[^\d\.\-\+]//g;
+        $zoom =~ s/[^\d\.\-\+]//g;
+        my $width   = LJ::ehtml($opts{'width'});
+        my $height  = LJ::ehtml($opts{'height'});
+        #my $width   = LJ::ehtml($opts{'width'} - 20);
+        #my $height  = LJ::ehtml($opts{'height'} - 20);
+        
+        return <<"HTML";
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+    <script src="http://api-maps.yandex.ru/1.1/index.xml?key=$key" type="text/javascript"></script>
+        <script type="text/javascript">
+            YMaps.jQuery(function () {
+                var map = new YMaps.Map(YMaps.jQuery("#YMapsID")[0]);
+                map.setCenter(new YMaps.GeoPoint($x, $y), $zoom);
+                map.addControl(new YMaps.ToolBar());
+                map.addControl(new YMaps.Zoom());
+                map.addControl(new YMaps.ScaleLine());
+            });
+        </script>
+<style type="text/css">
+* {padding: 0; margin: 0;  }
+</style>
+</head>
+<body >
+    <div id="YMapsID" style="width:${width}px;height:${height}px"></div>
+</body>
+</html>
+HTML
+     }
+    return "[error: invalid Yandex Map url]";
+}
+
+1;
+

Modified: trunk/cgi-bin/cleanhtml.pl
===================================================================
--- trunk/cgi-bin/cleanhtml.pl	2010-09-27 06:17:36 UTC (rev 17450)
+++ trunk/cgi-bin/cleanhtml.pl	2010-09-27 09:17:49 UTC (rev 17451)
@@ -8,6 +8,7 @@
                       HTML::TokeParser
                       LJ::EmbedModule
                       LJ::Config
+                      LJ::Maps
                       );
 
 LJ::Config->load;
@@ -327,7 +328,13 @@
 
                 next TOKEN;
             }
+            
+            if ($tag eq 'lj-map') {
+                $newdata .= LJ::Maps->expand_ljmap_tag($attr);
+                next TOKEN;
+            }
 
+
             # lj-repost tag adds button that allows easily post text in remote user's blog.
             #
             # Usage:

Modified: trunk/htdocs/tools/embedcontent.bml
===================================================================
--- trunk/htdocs/tools/embedcontent.bml	2010-09-27 06:17:36 UTC (rev 17450)
+++ trunk/htdocs/tools/embedcontent.bml	2010-09-27 09:17:49 UTC (rev 17451)
@@ -4,29 +4,36 @@
     use vars qw(%GET %POST $title $headextra @errors @warnings);
     use LJ::Auth;
     use LJ::EmbedModule;
+    use LJ::Maps;
 
     # this can only be accessed from the embed module subdomain
     return "This page cannot be viewed from $LJ::DOMAIN"
         unless LJ::Request->header_in("Host") =~ /.*$LJ::EMBED_MODULE_DOMAIN$/i;
 
-    # we should have three GET params: journalid, moduleid, auth_token
-    my $journalid = $GET{journalid}+0 or return "No journalid specified";
-    my $moduleid  = $GET{moduleid};
-    return "No module id specified" unless defined $moduleid;
-    $moduleid += 0;
-    my $preview = $GET{preview};
-    # check auth_token
-    return "Invalid auth string" unless
-        LJ::Auth->check_sessionless_auth_token('embedcontent', %GET);
+    my $mode = $GET{'mode'} || 'lj-embed';
+    if ($mode eq 'lj-map') {
+        return LJ::Maps->get_iframe_source(%GET);
+    } elsif ($mode eq 'lj-embed') {
+        # we should have three GET params: journalid, moduleid, auth_token
+        my $journalid = $GET{journalid}+0 or return "No journalid specified";
+        my $moduleid  = $GET{moduleid};
+        return "No module id specified" unless defined $moduleid;
+        $moduleid += 0;
+        my $preview = $GET{preview};
+        # check auth_token
+        return "Invalid auth string" unless
+            LJ::Auth->check_sessionless_auth_token('embedcontent', %GET);
 
-    # ok we're cool, return content
-    my $content = LJ::EmbedModule->module_content(
-        journalid => $journalid,
-        moduleid  => $moduleid,
-        preview => $preview,
-    );
-    LJ::run_hooks('modify_embed_content', \$content, \%GET);
+        # ok we're cool, return content
+        my $content = LJ::EmbedModule->module_content(
+            journalid => $journalid,
+            moduleid  => $moduleid,
+            preview => $preview,
+        );
+        LJ::run_hooks('modify_embed_content', \$content, \%GET);
 
-    return qq {<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title>iframe</title><style type="text/css">HTML, BODY { background:transparent; padding:0; margin:0; border:0; overflow:hidden; }</style></head><body>$content</body></html>};
-}
+        return qq {<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title>iframe</title><style type="text/css">HTML, BODY { background:transparent; padding:0; margin:0; border:0; overflow:hidden; }</style></head><body>$content</body></html>};
+    }
+} 
 _code?>
+

Tags: bml, gariev, livejournal, pl, pm
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments