]> projects.mako.cc - selectricity-live/blob - vendor/plugins/sitealizer/lib/app/controllers/sitealizer_controller.rb
changed instance of hyperchad to selectricity
[selectricity-live] / vendor / plugins / sitealizer / lib / app / controllers / sitealizer_controller.rb
1 require 'digest/sha1'
2 SA_CONFIG = YAML::load_file(File.dirname(__FILE__)+'/../../config.yml')
3
4 class SitealizerController < ActionController::Base
5   
6   before_filter :check_access_type, :except => [:login, :logout]
7   layout 'sitealizer'
8   
9   def self.template_root
10     "#{RAILS_ROOT}/vendor/plugins/sitealizer/lib/app/views"
11   end
12   
13   def login
14     return unless request.post?
15     if params[:username] == SA_CONFIG['sitealizer']['username'].to_s && params[:password] == SA_CONFIG['sitealizer']['password'].to_s
16       cookies[:sitealizer] = {:value => Digest::SHA1.hexdigest("--#{Time.now.to_i}--#{params[:username]}--")}
17       redirect_to :action => 'index'
18     else
19       flash[:notice] = "Username/Password Invalid"
20     end
21   end
22   
23   def logout
24     cookies.delete :sitealizer
25     redirect_to :action => 'login'
26   end
27   
28   def index
29     if !params[:month] && !params[:year]
30       params[:month] = sprintf("%02d",Time.now.month)
31       params[:year]  = Time.now.year
32     end
33   end
34   
35   def menu
36     months = []
37     years  = []
38     f = SiteTracker.find(:all, :limit => 1, :order => 'created_on ASC')
39     l = SiteTracker.find(:all, :limit => 1, :order => 'created_on DESC')
40     first = !f.empty? ? f.first.created_on : Date.today
41     last  = !l.empty? ? l.first.created_on : Date.today
42     (first.to_date..last.to_date).each{|d| months << d.month unless months.include?(d.month)}
43     (first.to_date..last.to_date).each{|d| years << d.year unless years.include?(d.year)}
44     @months = months.sort.map{|m| 
45       m=sprintf('%02d',m); 
46       "<option #{"selected" if m.to_i == params[:month].to_i  } value=#{m}>#{m}</option>"
47     }
48     @years  = years.sort.map{|y| 
49       "<option #{"selected" if y == params[:year].to_i} value=#{y}>#{y}</option>"
50     }
51     @cookie = cookies[:sitealizer]
52   end
53   
54   def summary
55     @year = nil
56     @month = nil
57     begin
58       if "#{params[:year]}-#{params[:month]}-1".to_time
59         @year = params[:year]
60         @month = params[:month]
61       end
62     rescue
63       @year = Time.now.year
64       @month = sprintf("%02d",Time.now.month)
65     end
66     @total_hits  = SiteTracker.count_hits(:year => @year, :month => @month)
67     @unique_hits = SiteTracker.count_hits(:count => 'DISTINCT ip', :year => @year, :month => @month)
68     @daily_hits  = SiteTracker.find_daily_hits(:year => @year, :month => @month)
69     @month_hits  = SiteTracker.find_monthly_hits(:year => @year)
70     @year_hits   = SiteTracker.count_hits(:date => "#{@year}-%")
71     @year_uniq   = SiteTracker.count_hits(:date => "#{@year}-%", :count => "DISTINCT ip")
72     file = File.dirname(__FILE__)+'/../../last_update'
73     @last_update = File.exists?(file) ? open(file,'r'){|f| f.read.to_time.to_s(:long)} : "Not updated yet"
74   end
75   
76   def hourly_stats
77     @year  = params[:year] || Time.now.year
78     @month = params[:month] || sprintf("%02d",Time.now.month)
79     @total_hits  = SiteTracker.count_hits(:year => @year, :month => @month)
80     @hourly_hits = SiteTracker.find_hourly_hits(:year => @year, :month => @month)
81   end
82   
83   def search_stats
84     @year  = params[:year] || Time.now.year
85     @month = params[:month] || sprintf("%02d",Time.now.month)
86     @keywords = SiteTracker.find_search_terms(:year => @year, :month => @month)
87     @engines  = SiteTracker.find_domains(:year => @year, :month => @month, :host => request.host)
88     @robots   = SiteTracker.find_robots(:year => @year, :month => @month)    
89   end
90   
91   def visitor_info
92     @year  = params[:year] || Time.now.year
93     @month = params[:month] || sprintf("%02d",Time.now.month) 
94     @total_hits  = SiteTracker.count_hits(:year => @year, :month => @month)
95     @browsers    = SiteTracker.find_browsers(:year => @year, :month => @month)
96     @platforms   = SiteTracker.find_platforms(:year => @year, :month => @month)
97     @hosts       = SiteTracker.find_hosts(:year => @year, :month => @month)
98     @languages   = SiteTracker.find_languages(:year => @year, :month => @month)
99   end
100   
101   def referrer_stats
102     @year  = params[:year] || Time.now.year
103     @month = params[:month] || sprintf("%02d",Time.now.month)  
104     @total_hits  = SiteTracker.count_hits(:year => @year, :month => @month)
105     @referers    = SiteTracker.find_referers(:year => @year, :month => @month)
106     @page_urls   = SiteTracker.find_page_urls(:year => @year, :month => @month)
107   end
108   
109   protected
110   def check_access_type
111     if SA_CONFIG['sitealizer']['access_type'] == 'private'
112       unless cookies[:sitealizer]
113         redirect_to :action => 'login'
114       end
115     end
116   end
117
118 end

Benjamin Mako Hill || Want to submit a patch?