http_processing.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Copyright (C) 2001 Florian Sander
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. /* Don't know a better place for these defines... */
  19. #define SLE_USERNOTFOUND 1
  20. #define SLE_NOUSERPASS 2
  21. #define SLE_WRONGPASS 3
  22. /* send_webseen():
  23. * take the URL string, split the parameters off,
  24. * calculate seen-results if necessary, and finally
  25. * send a template to the client
  26. */
  27. static void process_get_request(int idx)
  28. {
  29. char *url, urlbuf[512], *newurl, *s_timerange, *s_sorting;
  30. char *chan, *cmd, *user, *pass, *lchan, *lang, *str_skin;
  31. char *email, *homepage, *icqnr, *newpass, *addhosts, *list;
  32. char *newpass_confirmation, *nostats, *s_start, *s_end;
  33. struct stats_userlist *u;
  34. struct llist_1string *langlist;
  35. Context;
  36. // init all global vars
  37. reset_global_vars();
  38. if (!http_connection(idx)->path) {
  39. debug1("%s: no request. Dropping connection.", dcc[idx].host);
  40. return;
  41. }
  42. // copy the url into a buffer, so we can work on it without messing it up
  43. strncpy(urlbuf, http_connection(idx)->path, 512);
  44. urlbuf[511] = 0;
  45. url = urlbuf;
  46. // make sure there is a '/' at the end of the URL, or most links will
  47. // be broken.
  48. if (url[strlen(url) - 1] != '/') {
  49. newurl = nmalloc(strlen(url) + 1 + 1);
  50. strcpy(newurl, url);
  51. strcat(newurl, "/");
  52. dprintf(idx, "HTTP/1.1 301 Moved Permanently\nServer: EggdropMiniHTTPd/%s\n", HTTPD_VERSION);
  53. dprintf(idx, "Location: %s\nConnection: close\nContent-Type: text/html\n\n", newurl);
  54. dprintf(idx, "<HTML><body>The concluding \"/\" is important!<br><center>");
  55. dprintf(idx, "<a href=\"%s\">%s</a></center><br>", newurl, newurl);
  56. http_connection(idx)->code = 301;
  57. nfree(newurl);
  58. return;
  59. }
  60. // try to get skin and lang settings from the parameter list
  61. // If the parameter is specified, write it into a cookie. If it
  62. // is not specified, try to get it from a cookie first, and use the default
  63. // if it isn't even defined in a cookie
  64. if ((str_skin = get_param_value(idx, "skin")))
  65. set_cookie(idx, "skin", str_skin);
  66. else if (!(str_skin = get_cookie_value(idx, "skin")))
  67. str_skin = default_skin;
  68. if (!(glob_skin = templates_skin_find(skins, str_skin))) {
  69. if (!(glob_skin = templates_skin_find(skins, default_skin))) {
  70. send_http_header(idx, 500);
  71. dprintf(idx, "<HTML><BODY><H1>Internal Server Error: No skin found!</H1></BODY></HTML>");
  72. return;
  73. }
  74. }
  75. if ((lang = get_param_value(idx, "lang")))
  76. set_cookie(idx, "lang", lang);
  77. else if (!(lang = get_cookie_value(idx, "lang"))) {
  78. langlist = http_connection(idx)->langs;
  79. while (langlist) {
  80. if (slang_valid(glob_skin->slang, langlist->s1)) {
  81. lang = langlist->s1;
  82. break;
  83. }
  84. langlist = langlist->next;
  85. }
  86. if (!lang)
  87. lang = default_slang;
  88. }
  89. if (!(glob_slang = slang_find(glob_skin->slang, lang))) {
  90. if (!(glob_slang = slang_find(glob_skin->slang, default_slang))) {
  91. send_http_header(idx, 500);
  92. dprintf(idx, "<HTML><BODY><H1>Internal Server Error: No language found!</H1></BODY></HTML>");
  93. return;
  94. }
  95. }
  96. // now it's time to choose what to do
  97. if (!strcmp(url, "/")) {
  98. // user accessed the server root? ok, send the root template...
  99. send_http_header(idx, 200);
  100. template_send(glob_skin, "root", idx);
  101. return;
  102. } else if (!strcasecmp(url, "/cgi-bin/usersettings/")) {
  103. user = get_param_value(idx, "username");
  104. if (!user) {
  105. send_http_header(idx, 200);
  106. template_send(glob_skin, "userlogin", idx);
  107. return;
  108. }
  109. u = findsuser_by_name(user);
  110. if (!u) {
  111. glob_loginerror = SLE_USERNOTFOUND;
  112. send_http_header(idx, 200);
  113. template_send(glob_skin, "login_error", idx);
  114. return;
  115. }
  116. glob_user = u;
  117. if (get_param_value(idx, "sendpass")) {
  118. user_email_password(u);
  119. send_http_header(idx, 200);
  120. template_send(glob_skin, "password_emailed", idx);
  121. return;
  122. }
  123. pass = get_param_value(idx, "password");
  124. if (!pass) {
  125. send_http_header(idx, 200);
  126. template_send(glob_skin, "userlogin", idx);
  127. return;
  128. }
  129. if (!u->password) {
  130. glob_loginerror = SLE_NOUSERPASS;
  131. send_http_header(idx, 200);
  132. template_send(glob_skin, "login_error", idx);
  133. return;
  134. }
  135. if (!(!strcmp(u->password, pass))) {
  136. glob_loginerror = SLE_WRONGPASS;
  137. send_http_header(idx, 200);
  138. template_send(glob_skin, "login_error", idx);
  139. return;
  140. }
  141. icqnr = get_param_value(idx, "icqnr");
  142. if (icqnr)
  143. u->icqnr = atoi(icqnr);
  144. email = get_param_value(idx, "email");
  145. if (email) {
  146. setemail(u, email);
  147. }
  148. homepage = get_param_value(idx, "homepage");
  149. if (homepage) {
  150. sethomepage(u, homepage);
  151. }
  152. newpass = get_param_value(idx, "newpassword");
  153. newpass_confirmation = get_param_value(idx, "newpass_confirmation");
  154. if (newpass && newpass[0] && (newpass[0] != ' ')
  155. && newpass_confirmation && !strcmp(newpass_confirmation, newpass)) {
  156. u->password = nrealloc(u->password, strlen(newpass) + 1);
  157. strcpy(u->password, newpass);
  158. }
  159. list = get_param_value(idx, "list");
  160. if (list) {
  161. if (atoi(list))
  162. suser_setflag(u, S_LIST);
  163. else
  164. suser_delflag(u, S_LIST);
  165. }
  166. addhosts = get_param_value(idx, "addhosts");
  167. if (addhosts) {
  168. if (atoi(addhosts))
  169. suser_setflag(u, S_ADDHOSTS);
  170. else
  171. suser_delflag(u, S_ADDHOSTS);
  172. }
  173. nostats = get_param_value(idx, "nostats");
  174. if (nostats) {
  175. if (atoi(nostats)) {
  176. suser_setflag(u, S_NOSTATS);
  177. suser_delflag(u, S_LIST);
  178. } else
  179. suser_delflag(u, S_NOSTATS);
  180. }
  181. send_http_header(idx, 200);
  182. template_send(glob_skin, "usersettings", idx);
  183. return;
  184. } else {
  185. // strip the leading '/'
  186. url++;
  187. // and split the channel from the URL
  188. chan = decode_url(csplit(&url, '/'));
  189. glob_globstats = findglobstats(chan);
  190. if (!glob_globstats) {
  191. lchan = nmalloc(strlen(chan) + 1 + 1);
  192. lchan[0] = '#';
  193. strcpy(lchan + 1, chan);
  194. glob_globstats = findglobstats(lchan);
  195. nfree(lchan);
  196. if (!glob_globstats) {
  197. send_http_header(idx, 404);
  198. template_send(glob_skin, "404", idx);
  199. return;
  200. }
  201. }
  202. cmd = csplit(&url, '/');
  203. if (!strcasecmp(cmd, "")) {
  204. send_http_header(idx, 200);
  205. template_send(glob_skin, "chan", idx);
  206. return;
  207. } else if (!strcasecmp(cmd, "misc")) {
  208. send_http_header(idx, 200);
  209. template_send(glob_skin, "misc", idx);
  210. return;
  211. } else if (!strcasecmp(cmd, "top")) {
  212. s_timerange = csplit(&url, '/');
  213. s_sorting = csplit(&url, '/');
  214. if (!s_sorting[0] && !strcasecmp(s_timerange, "custom")) {
  215. // custom top talker list
  216. s_timerange = get_param_value(idx, "timerange");
  217. s_sorting = get_param_value(idx, "sorting");
  218. s_start = get_param_value(idx, "start");
  219. s_end = get_param_value(idx, "end");
  220. if (s_timerange)
  221. glob_timerange = get_timerange(s_timerange);
  222. else
  223. glob_timerange = S_TOTAL;
  224. if (s_sorting)
  225. glob_sorting = typetoi(s_sorting);
  226. else
  227. glob_sorting = T_WORDS;
  228. if (s_start)
  229. glob_top_start = atoi(s_start);
  230. if (s_end)
  231. glob_top_end = atoi(s_end);
  232. if (!glob_top_start)
  233. glob_top_start = 1;
  234. if (glob_top_end <= glob_top_start)
  235. glob_top_end = glob_top_start + webnr;
  236. if (glob_sorting == T_ERROR) {
  237. debug1("Invalid sorting '%s'. Defaulting to 'words'.", s_sorting);
  238. glob_sorting = T_WORDS;
  239. }
  240. if (glob_timerange == T_ERROR)
  241. glob_sorting = S_TOTAL;
  242. glob_toptype = itotype(glob_sorting);
  243. sortstats(glob_globstats, glob_sorting, glob_timerange);
  244. debug2("sorting: %s (%d)", s_sorting, glob_sorting);
  245. send_http_header(idx, 200);
  246. template_send(glob_skin, "custom_top", idx);
  247. return;
  248. }
  249. if (!s_sorting[0] || !s_timerange[0]) {
  250. // redirect client to full URL if it skipped anything
  251. chan = encode_url(glob_globstats->chan);
  252. newurl = nmalloc(strlen(chan) + 18 + 1);
  253. sprintf(newurl, "/%s/top/total/words/", chan);
  254. dprintf(idx, "HTTP/1.1 301 Moved Permanently\nServer: EggdropMiniHTTPd/%s\n", HTTPD_VERSION);
  255. dprintf(idx, "Location: %s\nConnection: close\nContent-Type: text/html\n\n", newurl);
  256. dprintf(idx, "<HTML><body>The concluding \"/\" is important!<br><center>");
  257. dprintf(idx, "<a href=\"%s\">%s</a></center><br>", newurl, newurl);
  258. http_connection(idx)->code = 301;
  259. nfree(newurl);
  260. return;
  261. }
  262. if (!strcasecmp(s_timerange, "total"))
  263. glob_timerange = S_TOTAL;
  264. else if (!strcasecmp(s_timerange, "today"))
  265. glob_timerange = S_TODAY;
  266. else if (!strcasecmp(s_timerange, "weekly"))
  267. glob_timerange = S_WEEKLY;
  268. else if (!strcasecmp(s_timerange, "monthly"))
  269. glob_timerange = S_MONTHLY;
  270. else if (!strcasecmp(s_timerange, "daily"))
  271. glob_timerange = S_DAILY;
  272. else {
  273. send_http_header(idx, 404);
  274. template_send(glob_skin, "404", idx);
  275. return;
  276. }
  277. Assert(glob_globstats);
  278. if (!strcasecmp(s_sorting, "graphs")) {
  279. send_http_header(idx, 200);
  280. template_send(glob_skin, "graphs", idx);
  281. return;
  282. }
  283. glob_sorting = slangtypetoi(s_sorting);
  284. if ((glob_timerange == T_ERROR) || (glob_sorting == T_ERROR)) {
  285. debug2("invalid top-parameter \"%s\" or \"%s\"", s_sorting, s_timerange);
  286. send_http_header(idx, 404);
  287. template_send(glob_skin, "404", idx);
  288. return;
  289. }
  290. glob_top_start = 1;
  291. glob_top_end = webnr;
  292. sortstats(glob_globstats, glob_sorting, glob_timerange);
  293. send_http_header(idx, 200);
  294. template_send(glob_skin, "top", idx);
  295. return;
  296. } else if (!strcasecmp(cmd, "users")) {
  297. user = decode_url(csplit(&url, '/'));
  298. if (!user[0]) {
  299. send_http_header(idx, 200);
  300. template_send(glob_skin, "userlist", idx);
  301. return;
  302. }
  303. glob_locstats = findlocstats(glob_globstats->chan, user);
  304. if (!glob_locstats) {
  305. send_http_header(idx, 404);
  306. template_send(glob_skin, "404", idx);
  307. return;
  308. }
  309. if (!glob_locstats->u)
  310. glob_locstats->u = findsuser_by_name(glob_locstats->user);
  311. glob_user = glob_locstats->u;
  312. if (glob_user && suser_nostats(glob_user)) {
  313. // don't let anyone access "private" stats
  314. send_http_header(idx, 404);
  315. template_send(glob_skin, "404", idx);
  316. return;
  317. }
  318. send_http_header(idx, 200);
  319. template_send(glob_skin, "user", idx);
  320. return;
  321. } else if (!strcasecmp(cmd, "onchan")) {
  322. send_http_header(idx, 200);
  323. template_send(glob_skin, "onchan", idx);
  324. return;
  325. }
  326. }
  327. send_http_header(idx, 404);
  328. template_send(glob_skin, "404", idx);
  329. }