source: subversion/sites/tile.openstreetmap.org/cat_tile.rb @ 4078

Last change on this file since 4078 was 3633, checked in by jonb, 13 years ago

cat_tile.rb: Make blocked tiles return a 403 error with a message explaining what has happened

  • Property svn:executable set to *
File size: 2.9 KB
Line 
1require 'cgi'
2
3cgi = CGI.new
4
5module Foo
6  require 'mysql'
7  require 'date'
8  require 'time'
9  require 'singleton'
10
11  class Bar
12    def call_local_sql
13      dbh = nil
14      begin
15        dbh = Mysql.real_connect('localhost', 'tile', 'tile', 'tile')
16        sql = yield
17        res = dbh.query(sql)
18        return res #        if res.nil? then return true else return res end
19      rescue MysqlError =>ex
20        puts ex
21      ensure
22        dbh.close unless dbh.nil?
23      end
24      nil
25    end
26  end
27end
28
29#first check number of accesses
30
31include Apache
32ip = Apache.request.connection.remote_ip
33fb = Foo::Bar.new
34
35res = fb.call_local_sql { "select hits from access where ip='#{ip}'" }
36if res.nil?
37  exit
38end
39
40if res.num_rows == 0
41  fb.call_local_sql { "insert into access (ip,hits) values('#{ip}',1) ON DUPLICATE KEY UPDATE hits=hits+1;" }
42else
43  hits = 0 
44  res.each_hash do |row|
45    hits = row['hits'].to_i
46  end
47  hits += 1
48
49  # User gets an error once they hit upper limit (and not even counted any more)
50  if hits > 75_000
51    sleep 3
52    cgi.header("status"=>"403 Your IP has downloaded too many tiles today. Please try again tomorrow or use planet.osm and render them yourself!")
53   exit
54  end
55
56  fb.call_local_sql { "update access set hits=#{hits} where ip='#{ip}'" }
57  if hits > 50_000
58    print IO.read("/home/www/tile/images/limit.png")
59    exit
60  end
61end
62
63#now send the tile
64x = cgi['x'].to_i
65y = cgi['y'].to_i
66z = cgi['z'].to_i
67
68if z and (z > 18 or z < 0)
69  exit
70end
71
72# valid x/y for tiles are 0 ... 2^zoom-1
73limit = (2 ** z) - 1
74
75if x and (x < 0 or x > limit)
76  print IO.read("/home/www/tile/images/blank-000000.png")
77  exit
78end
79
80if y and (y < 0 or y > limit)
81  print IO.read("/home/www/tile/images/blank-000000.png")
82  exit
83end
84
85
86res = fb.call_local_sql { "select data, dirty_t, created_at from tiles where x = #{x} and y=#{y} and z=#{z} limit 1" }
87if res.nil?
88  exit
89end
90
91if res.num_rows == 0
92  fb.call_local_sql { "insert into tiles (x,y,z,dirty_t, created_at) values (#{x},#{y},#{z},'true',NOW())" }
93
94  res = fb.call_local_sql { "select count(dirty_t) as dirty from tiles where dirty_t='true'" }
95  if res.nil?
96    exit
97  end
98  res.each_hash do |row|
99    if row['dirty'].to_i < 128
100      render = IO.popen("/home/jburgess/live/render_from_list.py > /dev/null", "w+")
101      render.puts "#{x} #{y} #{z}"
102      render.close
103    else
104      exit
105    end
106  end
107end
108
109res = fb.call_local_sql { "select data, dirty_t, created_at from tiles where x = #{x} and y=#{y} and z=#{z} limit 1" }
110if res.nil?
111  exit
112end
113if res.num_rows == 0
114  fb.call_local_sql { "insert into tiles (x,y,z,dirty_t, created_at) values (#{x},#{y},#{z},'true',NOW())" }
115  exit
116else
117  res.each_hash do |row|
118    print row['data']
119    if row['dirty_t'] == 'false' and Time.parse(row['created_at']) < (Time.now - (60*60*24*3))
120      fb.call_local_sql { "update tiles set dirty_t = 'true' where x = #{x} and y=#{y} and z=#{z}" }
121    end
122  end
123end
124
Note: See TracBrowser for help on using the repository browser.