1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
inherit "roxenlib"; 
 
// add_campaign 
//    add an ad campaign to the database 
void add_campaign(mapping m, object db) 
{ 
  object result; 
  string query; 
  array row; 
 
  query = "INSERT INTO campaigns (name, password) VALUES " 
        "('" + db->quote(m->name) + "'," 
        "PASSWORD('" + db->quote(m->password) + "') )"; 
  db->query(query); 
} 
 
// get_campaigns 
//    get a listing of available campaigns 
array(mapping) get_campaigns(object db) 
{ 
  object result; 
  string query, ret; 
  array row, campaigns; 
 
  query = "SELECT id, name FROM campaigns ORDER BY name"; 
  result = db->big_query(query); 
 
  campaigns = ({}); 
  while(row = result->fetch_row()) 
    campaigns += ({ (["id":row[0],"name":row[1]]) }); 
 
  return campaigns; 
} 
 
// get_info 
//    get an ad campaign's data 
mapping get_info(int id, object db) 
{ 
  object result; 
  string query, ret; 
  array row; 
 
  query = "SELECT id, name, password FROM campaigns WHERE id="+id; 
  result = db->big_query(query); 
  row = result->fetch_row(); 
 
  return ([ "id" : row[0], "name" : row[1], "password" : row[2] ]); 
} 
 
// set_info 
//    set an ad campaign's data 
void set_info(mapping m, object db) 
{ 
  string query; 
 
  query = "UPDATE campaigns SET " 
          "name='" + db->quote(m->name) + "' "; 
 
  if (m->password && sizeof(m->password)) 
    query += ", password=PASSWORD('"+db->quote(m->password)+"') "; 
 
  query += "WHERE id="+m->id; 
  db->query(query); 
} 
 
// delete_campaign 
//    remove an ad campaign. this will delete any run associated with 
//    this campaign, and any impressions associated with those runs. 
void delete_campaign(int id, object db) 
{ 
  object result; 
  string query; 
  array row; 
 
  result = db->big_query("SELECT id FROM runs WHERE campaign="+id); 
  while(row = result->fetch_row()) 
    .Run.delete_run((int)row[0], db); 
  db->query("DELETE FROM campaigns WHERE id="+id); 
} 
 
// get_stats 
//    returns as html the statistics for this campaign 
string get_stats(int campaign, object db) 
{ 
  object result; 
  array row; 
  string ret, query; 
  int impressions, clickthroughs; 
 
  query = "SELECT FROM_DAYS(i.day) AS d, COUNT(*), COUNT(DISTINCT i.host), " 
          "COUNT(DISTINCT i.user), COUNT(IF(i.click = 'Y',1,NULL)) " 
          "FROM impressions AS i, runs AS r WHERE i.run=r.id AND r.campaign="+ 
          campaign+" GROUP BY i.day ORDER BY d"; 
 
  result = db->big_query(query); 
  ret = "<table cellpadding=1 cellspacing=0 border=0 bgcolor=#000000><tr><td>" 
        "<TABLE border=0 cellspacing=0 cellpadding=4>" 
        "<TR BGCOLOR=#113377><TH><FONT COLOR=#ffffff>Date</FONT></TH>" 
        "<TH><FONT COLOR=#ffffff>Impressions</FONT></TH>" 
        "<TH><FONT COLOR=#ffffff>Clickthroughs</FONT></TH>" 
        "<TH><FONT COLOR=#ffffff>Rate (%)</FONT></TH>" 
        "<TH><FONT COLOR=#ffffff>Hosts</FONT></TH>" 
        "<TH><FONT COLOR=#ffffff>Users</FONT></TH></TR>"; 
  impressions = 0; 
  clickthroughs = 0; 
 
  while(row = result->fetch_row()) 
  { 
    ret += "<TR ALIGN=RIGHT BGCOLOR=#FFFFFF>" 
           "<TD>"+row[0]+"</TD><TD>"+row[1]+"</TD><TD>"+row[4]+"</TD><TD>"+ 
           sprintf("%.2f",(((float)row[4]/(float)row[1])*100)) + "</TD><TD>" + row[2] + "</TD>" 
          "<TD>" + row[3] + "</TD></TR>"; 
    impressions += (int)row[1]; 
    clickthroughs += (int)row[4]; 
  } 
 
  if (impressions) 
    ret += "<TR BGCOLOR=#FFFFFF><TD COLSPAN=6><HR NOSHADE></TD></TR>" 
         "<TR ALIGN=RIGHT BGCOLOR=#FFFFFF>" 
         "<TD>&nbsp;</TD><TD>"+impressions+"</TD><TD>"+clickthroughs+"</TD>"  
        "<TD>"+sprintf("%.2f", ((float)clickthroughs/(float)impressions)*100) + 
        "</TD><TD>&nbsp;</TD><TD>&nbsp;</TD></TR>"; 
 
  ret += "</TABLE>" 
         "</td></tr></table>"; 
 
  return ret; 
}