00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
#ifdef WIN32
00020
#include <config-win.h>
00021
static int getpid() {
return 0; }
00022
#endif
00023
#include <stdio.h>
00024
#include <stdlib.h>
00025
#include <string.h>
00026
#include <mysql/mysql.h>
00027
#ifndef WIN32
00028
#include <unistd.h>
00029
#endif
00030
#include <libmysqlwrapped.h>
00031
#include <libfd.h>
00032
#include <string>
00033
#include <libcgi++.h>
00034
00035
#include "utils.h"
00036
#include "html.h"
00037
#include "forms.h"
00038
#include "Mime.h"
00039
#include "FinderHandler.h"
00040
00041
#include "dbd.h"
00042
00043
00044
00045
00046
00047
00048 static long searchtag = 0;
00049 static long searchcat = 0;
00050
00051 FILE *
out = stdout;
00052 ExecSocket *
g_pSocket = NULL;
00053 char tmp_path[1000];
00054 bool g_bSize =
false;
00055
00056
00057 void valflik(
int nr,
const std::string& title,
int flik)
00058 {
00059 fprintf(
out,
"<div style='border: 1px #c0c0c0 solid; background: #e0e0e0;"
00060
" margin-bottom: 5px;' align='center'>");
00061
if (flik == nr)
00062 fprintf(
out,
"<a style='font-weight: bold;' href='/cgi-bin/exec?flik=%d'>%s</a>", nr, title.c_str());
00063
else
00064 fprintf(
out,
"<a href='/cgi-bin/exec?flik=%d'>%s</a>", nr, title.c_str());
00065 fprintf(
out,
"</div>");
00066 }
00067
00068
00069 void run(Database& db,FILE *o,
const std::string& input_data)
00070 {
00071
#ifdef WIN32
00072
::GetTempPath(1000,
tmp_path);
00073 strcat(
tmp_path,
"\\");
00074
#else
00075
strcpy(
tmp_path,
"/tmp/");
00076
#endif
00077
00078
00079
out = o;
00080 Query q(db);
00081 Form *form = NULL;
00082 Cookies *cs;
00083
00084
char *q_s = getenv(
"QUERY_STRING");
00085
00086
char *r_m = getenv(
"REQUEST_METHOD");
00087
char *http_cookie = getenv(
"HTTP_COOKIE");
00088
char sql[1000];
00089
char slask[1000];
00090
00091
00092
00093
if (!r_m)
00094 {
00095 printf(
"This program is part of a cgi script, and should not be\n");
00096 printf(
"be called from the command line\n");
00097 exit(0);
00098 }
00099
00100
if (!strcasecmp(r_m,
"get"))
00101 {
00102
if (q_s)
00103 form =
new Form(q_s,strlen(q_s));
00104
else
00105 form =
new Form(
"", 0);
00106 }
00107
else
00108
if (input_data.size())
00109 {
00110 FILE *fil = fopen(input_data.c_str(),
"rb");
00111 form =
new Form(fil);
00112 fclose(fil);
00113 }
00114
else
00115 {
00116
if (!strcasecmp(r_m,
"post"))
00117 form =
new Form();
00118
else
00119
error(
"unknown request_method");
00120 }
00121
00122
00123
g_bSize =
false;
00124
00125
00126 cs =
new Cookies(http_cookie ? http_cookie : (
char *)
"");
00127
unpack_cookie(cs);
00128
00129 cs -> getvalue(
"flik",slask,20);
00130
int flik = atoi(slask);
00131
00132
00133
form_input(db, q, sql, form, cs, flik);
00134
if (form -> getvalue(
"flik",slask,20))
00135 {
00136 flik = atoi(slask);
00137 }
00138
searchtag =
searchcat = 0;
00139
if (form -> getvalue(
"tag",slask,20))
00140 {
00141
searchtag = atol(slask);
00142 }
00143
if (form -> getvalue(
"cat",slask,20))
00144 {
00145
searchcat = atol(slask);
00146 }
00147
long again = 0;
00148
if (form -> getvalue(
"again",slask,20) && *slask && atoi(slask))
00149 {
00150 again = atol(slask);
00151 flik = 2;
00152 }
00153
00154
00155
long get_file = 0;
00156
if (form -> getvalue(
"get_file",slask,20) && *slask && atoi(slask))
00157 {
00158 get_file = atol(slask);
00159 }
00160
if (get_file)
00161 {
00162
db::Item item(db, get_file);
00163
if (item.
num)
00164 {
00165 cgi::Base64 b(
'@');
00166 std::string mime = static_cast<FinderHandler&>(
g_pSocket->Handler()).GetMimeFromFile(item.
name);
00167 std::string data;
00168
for (std::vector<std::string>::iterator it = item.
data.begin(); it != item.
data.end(); it++)
00169 {
00170 data += *it;
00171 }
00172 fprintf(
out,
"Content-type: %s\r\n", mime.c_str());
00173 fprintf(
out,
"Content-length: %d\r\n", b.decode_length(data));
00174
00175 fprintf(
out,
"Content-disposition: filename=%s\r\n", item.
name.c_str());
00176 fprintf(
out,
"\r\n");
00177 b.decode_to_stdout(data,
out);
00178
return;
00179 }
00180 }
00181
00182
00183
set_cookie(cs,
"flik",flik);
00184 {
00185
pack_and_set_cookies(cs);
00186 fprintf(
out,
"Content-type: text/html; charset=ISO-8859-1\r\n");
00187
00188
00189 fprintf(
out,
"\r\n");
00190 fflush(
out);
00191 }
00192
00193
00194 fprintf(
out,
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd\">\n");
00195 fflush(
out);
00196
00197 fprintf(
out,
"<html><head><title>fd</title>");
00198 fprintf(
out,
"<link rel='SHORTCUT ICON' href='/favicon.ico'>");
00199 fprintf(
out,
00200
"<style type='text/css'>"
00201
"th.x { background: #c0c0c0; padding-left: 8px; padding-right: 8px; }\n"
00202
"td.x { background: #e0e0e0; padding-left: 8px; padding-right: 8px; }\n"
00203
"body { margin: 0; padding: 0; font-family: sans-serif; }\n"
00204
"</style>");
00205 fprintf(
out,
"</head>");
00206
00207
00208 fprintf(
out,
"<body>");
00209
00210
00211 fprintf(
out,
"<table cellspacing=5 cellpadding=5><tr><td valign='top'>"
00212
"<a href='http://www.alhem.net/duct/'>"
00213
"<img src='/ahl.png' border=0>"
00214
"</a><br>");
00215
valflik(1,
"Search",flik);
00216
valflik(2,
"Post",flik);
00217 fprintf(
out,
00218
"</td><td valign='top'>");
00219
00220
if (
g_bSize)
00221 {
00222 fprintf(
out,
"<h3 style='color: #800000'>Largest file size allowed is 400000 bytes</h3>");
00223 }
00224
00225
switch (flik)
00226 {
00227
case 1:
00228
search(db, q, sql, form, cs);
00229
break;
00230
case 2:
00231
if (again)
00232 {
00233
db::Item item(db, again);
00234
post_form(db, q, sql, &item);
00235 }
00236
else
00237 {
00238
post_form(db, q, sql);
00239 }
00240
break;
00241 }
00242
00243
00244 fprintf(
out,
"</body></html>\n");
00245
00246
00247
delete form;
00248
delete cs;
00249 }
00250
00251
00252 void post_form(Database& db,Query& q,
char *sql,
db::Item *p)
00253 {
00254 fprintf(
out,
"<form action='/cgi-bin/exec' method='post' enctype='multipart/form-data'>");
00255 fprintf(
out,
"<input type=hidden name=post_form value=1>");
00256
if (p)
00257 {
00258 fprintf(
out,
"<input type=hidden name=typ value='Again'>");
00259 fprintf(
out,
"<input type=hidden name=itemnr value=%ld>", p -> num);
00260 fprintf(
out,
"<p style='font-weight: bold;'>Send item again</p>");
00261 fprintf(
out,
"<p><u>%s</u>: %s</p>",p->
typ.c_str(),p->
name.c_str());
00262 }
00263
else
00264 {
00265 fprintf(
out,
"<p style='font-weight: bold;'>Item type</p>");
00266 fprintf(
out,
"<table cellspacing=5><tr>");
00267 fprintf(
out,
"<td><input type=radio name=typ value='File'>File</td>");
00268 fprintf(
out,
"<td><input type=file name=the_file></td>"
00269
"</tr><tr>");
00270 fprintf(
out,
"<td><input type=radio name=typ value='URL' checked>URL</td>");
00271 fprintf(
out,
"<td><input type=text size=40 name=the_url value='http://'></td>");
00272 fprintf(
out,
"</tr></table>");
00273 }
00274
00275 fprintf(
out,
"<p style='font-weight: bold;'>Category</p>");
00276
00277 q.get_result(
"select * from category order by name");
00278
if (q.num_rows())
00279 {
00280 fprintf(
out,
"<select name=category><option>");
00281
while (q.fetch_row())
00282 {
00283
db::Category tag(&db, &q);
00284 fprintf(
out,
"<option value=%ld%s>%s", tag.
num, (p && p->
category == tag.
num) ?
" selected" :
"",tag.
name.c_str() );
00285 }
00286 fprintf(
out,
"</select> or ");
00287 }
00288 q.free_result();
00289 fprintf(
out,
"new category <input type=text name=newcategory><br>");
00290
00291 fprintf(
out,
"<p style='font-weight: bold;'>Tags (optional)</p>");
00292
00293 q.get_result(
"select * from tag order by name");
00294
if (q.num_rows())
00295 {
00296 fprintf(
out,
"<select name=tag1><option>");
00297
while (q.fetch_row())
00298 {
00299
db::Tag tag(&db, &q);
00300 fprintf(
out,
"<option value=%ld>%s", tag.
num, tag.
name.c_str() );
00301 }
00302 fprintf(
out,
"</select> or ");
00303 }
00304 q.free_result();
00305 fprintf(
out,
"new tag <input type=text name=newtag1><br>");
00306
00307 q.get_result(
"select * from tag order by name");
00308
if (q.num_rows())
00309 {
00310 fprintf(
out,
"<select name=tag2><option>");
00311
while (q.fetch_row())
00312 {
00313
db::Tag tag(&db, &q);
00314 fprintf(
out,
"<option value=%ld>%s", tag.
num, tag.
name.c_str() );
00315 }
00316 fprintf(
out,
"</select> or ");
00317 }
00318 q.free_result();
00319 fprintf(
out,
"new tag <input type=text name=newtag2><br>");
00320
00321 q.get_result(
"select * from tag order by name");
00322
if (q.num_rows())
00323 {
00324 fprintf(
out,
"<select name=tag3><option>");
00325
while (q.fetch_row())
00326 {
00327
db::Tag tag(&db, &q);
00328 fprintf(
out,
"<option value=%ld>%s", tag.
num, tag.
name.c_str() );
00329 }
00330 fprintf(
out,
"</select> or ");
00331 }
00332 q.free_result();
00333 fprintf(
out,
"new tag <input type=text name=newtag3><br>");
00334
00335
if (p)
00336 {
00337 fprintf(
out,
"<p><input type=submit name=submit value=' Send again '></p>");
00338 }
00339
else
00340 {
00341 fprintf(
out,
"<p><input type=submit name=submit value=' Submit new item '></p>");
00342 }
00343
00344 fprintf(
out,
"</form>");
00345 }
00346
00347
00348 void search(Database& db,Query& q,
char *sql,Form *form,Cookies *cs)
00349 {
00350 Query q2(db);
00351
00352
if (
searchtag)
00353 {
00354 sprintf(sql,
"select item.* from item,linkitemtag where linkitemtag.tag=%ld and item.num=linkitemtag.item",
searchtag);
00355 q.get_result(sql);
00356 }
00357
else
00358
if (
searchcat)
00359 {
00360 sprintf(sql,
"select * from item where category=%ld",
searchcat);
00361 q.get_result(sql);
00362 }
00363
else
00364 {
00365 q.get_result(
"select * from item");
00366 }
00367 fprintf(
out,
"<table cellspacing=1>");
00368 fprintf(
out,
"<tr>");
00369 fprintf(
out,
"<th class='x'>Type</th>"
00370
"<th class='x'>Category</th>"
00371
"<th class='x'>Tags</th>"
00372
"<th class='x'>Link</th>"
00373
"<th class='x'>Size</th>");
00374
00375 fprintf(
out,
"</tr>");
00376
while (q.fetch_row())
00377 {
00378
db::Item item(&db, &q);
00379
db::Category cat(db, item.
category);
00380
00381 fprintf(
out,
"<tr>");
00382 fprintf(
out,
"<td class='x'>%s</td>", item.
typ.c_str());
00383 fprintf(
out,
"<td class='x'><a href='/cgi-bin/exec?cat=%ld'>%s</a></td>", cat.
num, cat.
name.c_str());
00384
00385 fprintf(
out,
"<td class='x'>");
00386 sprintf(sql,
"select * from linkitemtag where item=%ld", item.
num);
00387 q2.get_result(sql);
00388
while (q2.fetch_row())
00389 {
00390
db::Linkitemtag x(&db, &q2);
00391
db::Tag tag(db, x.
tag);
00392 fprintf(
out,
" <a href='/cgi-bin/exec?tag=%ld'>%s</a>",tag.
num,tag.
name.c_str());
00393 }
00394 q2.free_result();
00395 fprintf(
out,
"</td>");
00396
00397
if (item.
typ ==
"URL")
00398 {
00399 fprintf(
out,
"<td class='x'><a href='%s'>%s</a></td>", item.
name.c_str(), item.
name.c_str());
00400 }
00401
else
00402
if (item.
typ ==
"File")
00403 {
00404 fprintf(
out,
"<td class='x'><a href='/cgi-bin/exec?get_file=%ld'>%s</a></td>", item.
num, item.
name.c_str());
00405 }
00406 fprintf(
out,
"<td class='x' align='right'>%ld</td>",item.
sz);
00407
00408 fprintf(
out,
"<td><a href='/cgi-bin/exec?again=%ld'>send again</a></td>",item.
num);
00409 fprintf(
out,
"</tr>");
00410 }
00411 q.free_result();
00412 fprintf(
out,
"</table>");
00413 }
00414
00415