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
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
297
  
298
  
299
  
300
  
301
  
302
  
303
  
304
  
305
  
306
  
307
  
308
  
309
  
310
  
311
  
312
  
313
  
314
  
315
  
316
  
317
  
318
  
319
  
320
  
321
  
322
  
323
  
324
  
325
  
326
  
327
  
328
  
329
  
330
  
331
  
332
  
333
  
334
  
335
  
336
  
337
  
338
  
339
  
340
  
341
  
342
  
343
  
344
  
345
  
346
  
347
  
348
  
349
  
350
  
351
  
352
  
353
  
354
  
355
  
356
  
357
  
358
  
359
  
360
  
361
  
362
  
// This is a roxen module. Copyright © 1997-1999, Idonex AB. 
// 
// A module for Roxen Challenger, which gives the tags 
// <sqltable>, <sqlquery> and <sqloutput>. 
// 
// Henrik Grubbström 1997-01-12 
 
constant cvs_version="$Id: sqltag.pike,v 1.42 2000/01/11 19:14:16 mast Exp $"; 
constant thread_safe=1; 
#include <module.h> 
 
// Compatibility with old versions of the sqltag module. 
// #define SQL_TAG_COMPAT 
 
inherit "module"; 
inherit "roxenlib"; 
 
import Sql; 
 
Configuration conf; 
 
 
// Module interface functions 
 
array register_module() 
{ 
  return ({ MODULE_PARSER|MODULE_PROVIDER, 
            "SQL module", 
            "This module gives the three tags &lt;SQLQUERY&gt;, " 
            "&lt;SQLOUTPUT&gt;, and &lt;SQLTABLE&gt;.<br>\n", 
            0, 1 }); 
} 
 
TAGDOCUMENTATION 
#ifdef manual 
constant tagdoc=(["sqltable":"<desc tag>SQL table</desc>", 
  "sqlquery":"<desc tag>SQL query</desc>","sqloutput":"<desc cont>SQL output</desc>"]); 
/* 
             "Usage:<ul>\n" 
             "<table border=0>\n" 
             "<tr><td valign=top><b>&lt;sqloutput&gt;</b></td>" 
             "<td>Executes an SQL query, and " 
             "replaces #-quoted fieldnames with the results. # is " 
             "quoted as ##. The content between &lt;sqloutput&gt; and " 
             "&lt;/sqloutput&gt; is repeated once for every row in the " 
             "result.</td></tr>\n" 
             "<tr><td valign=top><b>&lt;sqlquery&gt;</b></td>\n" 
             "<td>Executes an SQL query, but " 
             "doesn't do anything with the result. This is useful if " 
             "you do queries like INSERT and CREATE.</td></tr>\n" 
             "<tr><td valign=top><b>&lt;sqltable&gt;</td>" 
             "<td>Executes an SQL query, and makes " 
             "an HTML table from the result.</td></tr>\n" 
             "</table></ul>\n" 
             "The following attributes are used by the above tags:<ul>\n" 
             "<table border=0>\n" 
             "<tr><td valign=top><b>query</b></td>" 
             "<td>The actual SQL query. (<b>REQUIRED</b>)</td></tr>\n" 
             "<tr><td valign=top><b>host<b></td>" 
             "<td>The hostname of the machine the SQL server runs on.<br>\n" 
             "This argument can also be used to specify which SQL server " 
             "to use by specifying an \"SQL URL\":<br><ul>\n" 
             "<pre>[<i>sqlserver</i>://][[<i>user</i>][:<i>password</i>]@]" 
             "[<i>host</i>[:<i>port</i>]]/<i>database</i></pre><br>\n" 
             "</ul>Valid values for \"sqlserver\" depend on which " 
             "SQL servers your pike has support for, but the following " 
             "might exist: msql, mysql, odbc, oracle, postgres.</td></tr>\n" 
             "<tr><td valign=top><b>database</b></td>" 
             "<td>The name of the database to use.</td></tr>\n" 
             "<tr><td valign=top><b>user</b></td>" 
             "<td>The name of the user to access the database with.</td></tr>\n" 
             "<tr><td valign=top><b>password</b></td>" 
             "<td>The password to access the database.</td></tr>\n" 
             "<tr><td valign=top><b>parse</b></td>" 
             "<td>If specified, the query will be parsed by the " 
             "RXML parser</td></tr>\n" 
             "<tr><td valign=top><b>quiet</b></td>" 
             "<td>If specified, SQL errors will be kept quiet.</td></tr>\n" 
             "</table></ul><p>\n" 
             "The &lt;sqltable&gt; tag has an additional attribute " 
             "<b>ascii</b>, which generates a tab separated table (usefull " 
             "with eg the &lt;diagram&gt; tag).<p>\n" 
             "\n" 
             "<b>NOTE</b>: Specifying passwords in the documents may prove " 
             "to be a security hole if the module is not loaded for some " 
             "reason.<br>\n" 
             "<b>SEE ALSO</b>: The &lt;FORMOUTPUT&gt; tag can be " 
             "useful to generate the queries.<br>\n" 
 */ 
#endif 
 
array|string|object do_sql_query(string tag, mapping args, RequestID id) 
{ 
  if (!args->query) 
    return rxml_error(tag, "No query.", id); 
 
  if (args->parse) 
    args->query = parse_rxml(args->query, id); 
 
  string host = query("hostname"); 
  object(sql) con; 
  array(mapping(string:mixed)) result; 
  function sql_connect = id->conf->sql_connect; 
  mixed error; 
 
#ifdef SQL_TAG_COMPAT 
  string database = query("database"); 
  string user = query("user"); 
  string password = query("password"); 
 
  if (args->host) { 
    host = args->host; 
    user = ""; 
    password = ""; 
  } 
  if (args->database) { 
    database = args->database; 
    user = ""; 
    password = ""; 
    sql_connect = 0; 
  } 
  if (args->user) { 
    user = args->user; 
    sql_connect = 0; 
  } 
  if (args->password) { 
    password = args->password; 
    sql_connect = 0; 
  } 
 
  if (sql_connect) 
    error = catch(con = sql_connect(host)); 
  else { 
    host = (lower_case(host) == "localhost")?"":host; 
    error = catch(con = sql(host, database, user, password)); 
  } 
#else 
  if (args->host) 
    host=args->host; 
 
  if(sql_connect) 
    error = catch(con = sql_connect(host)); 
  else 
    error = catch(con = sql(lower_case(host)=="localhost"?"":host)); 
#endif 
 
  if (error) { 
    if (!args->quiet) { 
      if (args->log_error && QUERY(log_error)) { 
        report_error(sprintf("SQLTAG: Couldn't connect to SQL server:\n" 
                       "%s\n", describe_backtrace(error))); 
      } 
      return "<h3>Couldn't connect to SQL server</h3><br>\n" + 
              html_encode_string(error[0]) + "<false>"; 
    } 
    return rxml_error(tag, "Couldn't connect to SQL server. "+html_encode_string(error[0]), id); 
  } 
 
  if (error = catch(result = tag=="sqltable"?con->big_query(args->query):con->query(args->query))) { 
    error = html_encode_string(sprintf("Query %O failed. %s", args->query, 
                                       con->error()||"")); 
    if (!args->quiet) { 
      if (args->log_error && QUERY(log_error)) { 
        report_error(sprintf("SQLTAG: Query %O failed:\n" 
               "%s\n", 
               args->query, describe_backtrace(error))); 
      } 
      return "<h3>"+error+"</h3>\n<false>"; 
    } 
    return rxml_error(tag, error, id); 
  } 
 
  if(tag=="sqlquery") args["dbobj"]=con; 
  return result; 
} 
 
 
// -------------------------------- Tag handlers ------------------------------------ 
 
array|string container_sqloutput(string tag, mapping args, string contents, 
                    RequestID id) 
{ 
  if(args->help) return register_module()[2]; // FIXME 
  NOCACHE(); 
 
  string|array res=do_sql_query(tag, args, id); 
  if(stringp(res)) return res; 
 
  if (res && sizeof(res)) { 
    array ret = ({ do_output_tag(args, res, contents, id) }); 
    id->misc->defines[" _ok"] = 1; // The effect of <true>, since res isn't parsed. 
 
    if( args["rowinfo"] ) 
           id->variables[args->rowinfo]=sizeof(res); 
 
    return ret; 
  } 
 
  if (args["do-once"]) 
    return do_output_tag( args, ({([])}), contents, id )+ "<true>"; 
 
  return rxml_error(tag, "No SQL return values.", id); 
} 
 
string tag_sqlquery(string tag, mapping args, RequestID id) 
{ 
  if(args->help) return register_module()[2]; // FIXME 
  NOCACHE(); 
 
  string|array res=do_sql_query(tag, args, id); 
  if(stringp(res)) return res; 
 
  if(args["mysql-insert-id"]) 
    if(args->dbobj && args->dbobj->master_sql) 
      id->variables[args["mysql-insert-id"]] = args->dbobj->master_sql->insert_id(); 
    else 
      return rxml_error(tag, "No insert_id present.", id); 
 
  return "<true>"; 
} 
 
string tag_sqltable(string tag, mapping args, RequestID id) 
{ 
  if(args->help) return register_module()[2]; // FIXME 
  NOCACHE(); 
 
  string|object res=do_sql_query(tag, args, id); 
  if(stringp(res)) return res; 
 
  int ascii=!!args->ascii; 
  string ret=""; 
 
  if (res) { 
    string nullvalue=args->nullvalue||""; 
    array(mixed) row; 
 
    if (!ascii) { 
      ret="<tr>"; 
      foreach(map(res->fetch_fields(), lambda (mapping m) { 
                                              return m->name; 
                                            } ), string name) 
        ret += "<th>"+name+"</th>"; 
      ret += "</tr>\n"; 
    } 
 
    if( args["rowinfo"] ) 
        id->variables[args->rowinfo]=res->num_rows(); 
 
    while(arrayp(row=res->fetch_row())) { 
      if (ascii) 
        ret += row * "\t" + "\n"; 
      else { 
        ret += "<tr>"; 
        foreach(row, mixed value) 
          ret += "<td>"+(value==""?nullvalue:value)+"</td>"; 
        ret += "</tr>\n"; 
      } 
    } 
 
    if (!ascii) 
      ret=make_container("table", args-(["host":"", "database":"", "user":"", "password":"", 
                                         "query":"", "nullvalue":""]), ret); 
 
    return ret+"<true>"; 
  } 
 
  return rxml_error(tag, "No SQL return values.", id); 
} 
 
 
// ------------------- Callback functions ------------------------- 
 
object(sql) sql_object(void|string host) 
{ 
  string host = stringp(host)?host:query("hostname"); 
  object(sql) con; 
  function sql_connect = conf->sql_connect; 
  mixed error; 
  /* Is this really a good idea? /mast 
  error = catch(con = sql_connect(host)); 
  if(error) 
    return 0; 
  return con; 
  */ 
  return sql_connect(host); 
} 
 
string query_provides() 
{ 
  return "sql"; 
} 
 
 
// ------------------------ Setting the defaults ------------------------- 
 
void create() 
{ 
  defvar("hostname", "localhost", "Default SQL database host", 
         TYPE_STRING, "Specifies the default host to use for SQL queries.\n" 
         "This argument can also be used to specify which SQL server to " 
         "use by specifying an \"SQL URL\":<ul>\n" 
         "<pre>[<i>sqlserver</i>://][[<i>user</i>][:<i>password</i>]@]" 
         "[<i>host</i>[:<i>port</i>]]/<i>database</i></pre></ul><br>\n" 
         "Valid values for \"sqlserver\" depend on which " 
         "SQL servers your pike has support for, but the following " 
         "might exist: msql, mysql, odbc, oracle, postgres.\n"); 
 
  defvar("log_error", 0, "Log errors to the event log", 
         TYPE_FLAG, "Enable this to log database connection and SQL " 
         "errors to the event log.\n"); 
 
#ifdef SQL_TAG_COMPAT 
  defvar("database", "", "Default SQL database (deprecated)", 
         TYPE_STRING, 
         "Specifies the name of the default SQL database.\n"); 
  defvar("user", "", "Default username (deprecated)", 
         TYPE_STRING, 
         "Specifies the default username to use for access.\n"); 
  defvar("password", "", "Default password (deprecated)", 
         TYPE_STRING, 
         "Specifies the default password to use for access.\n"); 
#endif // SQL_TAG_COMPAT 
} 
 
 
// --------------------- More interface functions -------------------------- 
 
void start(int level, object _conf) 
{ 
  if (_conf) { 
    conf = _conf; 
  } 
//add_api_function("sql_query", api_sql_query, ({ "string", 0,"int" })); 
} 
 
void stop() 
{ 
} 
 
string status() 
{ 
  if (mixed err = catch { 
    object o; 
    if (conf->sql_connect) 
      o = conf->sql_connect(QUERY(hostname)); 
    else 
      o = Sql.sql(QUERY(hostname) 
#ifdef SQL_TAG_COMPAT 
                , QUERY(database), QUERY(user), QUERY(password) 
#endif // SQL_TAG_COMPAT 
               ); 
    return(sprintf("Connected to %s server on %s<br>\n", 
                   o->server_info(), o->host_info())); 
  }) 
    return 
      "<font color=red>Not connected:</font> " + 
      replace (html_encode_string (describe_error(err)), "\n", "<br>\n") + 
      "<br>\n"; 
}