/*

  DBOX Module
  Copyright (C) 1994-2000 Daniel Kroening <kroening@handshake.de>

  Purpose: 

*/

#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <dirent.h>
#include <getopt.h>

#include <dboxpath.h>
#include <logf.h>
#include <folder.h>
#include <flags.h>
#include <charsettools.h>
#include <dboxversion.h>
#include <libdboxversion.h>
#include <account.h>
#include <portinfo.h>
#include <logins.h>
#include "../online/userlog.h"
#include "ostatus.h"
#include "webtools.h"

typedef struct _ext_table_entry
 {
  char *name;
  char *mime_type;
  char *mime_encoding;
  char *icon;
 } ext_table_entry;

#include "mime_table.h"

internetaddresst peer;

void auth_err(const string &realm)
 {
  cout << "HTTP/1.0 401 Keep cool\r\n"
          "Server: DBOX-WWW Interface\r\n"
          "WWW-Authenticate: Basic realm=\"" << realm << "\"\r\n"
          "\r\n";

  cout << "<HTML>\n"
          "<HEAD>\n"
          "<TITLE>Access denied!</TITLE>\n"
          "<BODY BGCOLOR=\"" << config.webonline_bgcolor << "\">\n";

  cout << "<h1>Access denied!</h1><P>\n"
          "<p>\n";

  showcopyright();
  endpage();
  return;
 }

bool req_auth(httpt &http, const string &realm)
 {
  char password[TEXTLEN];

  http.basic_auth(status.username, password, TEXTLEN-1);

  dnstring(status.username);

  if(status.username[0]==0)
   {
    auth_err(realm);
    return TRUE;
   }

  strmaxcpy(status.userprofile.name, status.username, TEXTLEN-1);
  if(status.userprofile.read())
   {
    auth_err(realm);
    logf.printf("webonline", "Falscher Username \"%s\"", status.username);
    return TRUE;
   }

  snprintf(status.userlogfile, DIRLEN, "%slogfile",
    status.userprofile.getpath());

  if(verifylogin(&status.userprofile, password))
   {
    auth_err(realm);
    userlogprintf1(status, "webonline", "Falsches Passwort");
    return TRUE;
   }

  userlogprintf1(status, "webonline", "Erfolgreiche Authentikation");

  userpt up2;
  get_profiles(status.userprofile, up2, status.gruppe);

  if(!get_internalflag(status.userprofile, up2, status.gruppe, LOGIN_WEB))
   {
    startpage("DBOX WWW-Interface");
    cout << "<h1>Access denied!</h1><P>\n"
            "Sie haben keine Berechtigung, "
            "das Web-Interface zu benutzen!<p>\n";

    userlogprintf1(status, "webonline", "access denied");

    showcopyright();
    endpage();
    return TRUE;
   }

  return FALSE;
 }

void makebutton(char *text, char *link, char *target)
 {
  char linktext[DIRLEN], *tptr, *tptr2, *tptr3;

  strmaxcpy(linktext, link, DIRLEN-1);
  tptr=strchr(linktext, '?');
  if(tptr!=NULL) *(tptr++)=0;

  cout << "<FORM ACTION=\"" << linktext << "\"";

  if(target!=NULL && target[0]!=0)
    cout << " target=\"" << target << "\"";

  cout << ">\n";

  if(tptr!=NULL)
   {
   schleife:
    tptr2=strchr(tptr, '&');
    if(tptr2!=NULL) *(tptr2++)=0;
    tptr3=strchr(tptr, '=');
    if(tptr3!=NULL)
     {
      *(tptr3++)=0;
      cout << "<input name=\"" << tptr 
           << "\" value=\"" << tptr3 << "\" type=\"HIDDEN\">\n";

      if(tptr2!=NULL) 
       {
        tptr=tptr2;
        goto schleife;
       }
     }
   }

  cout << "<INPUT TYPE=submit value=\"" << text 
       << "\"></form>\n";
 }

char *http_time(time_t t)
 {
  struct tm *gmt;
  time_t when;
  static char tbuf[128];

  when = t ? t : time(NULL);
  gmt = gmtime(&when);
  strftime(tbuf, 128, "%a, %d %b %Y %H:%M:%S GMT", gmt);
  return tbuf;
 }

void print_http_header(ostream &out)
 {
  time_t t;

  time(&t);

  out << "HTTP/1.0 200 Keep cool\r\n"
         "MIME-Version: 1.0\r\n"
         "Server: DBOX-WWW Interface\r\n"
         "Expires: " << 
         http_time(t-60*60*24) << "\r\n";

  out << "Last-modified: " << http_time(t) << "\r\n"
         "Content-Type: text/html\r\n"
         "Content-Encoding: binary\r\n"
         "\r\n";
 }

void startpage(ostream &out, const string &title)
 {
  print_http_header(out);

  out << 
    "<HTML>\n"
    "<HEAD>\n"
    "<TITLE>" << title << "</TITLE>\n"
    "<BODY BGCOLOR=\"" << config.webonline_bgcolor << "\">\n";
 }

void startpage(const char *title)
 {
  startpage(cout, title);
 }

void endpage(ostream &out)
 {
  out << "</BODY>\n"
         "</HTML>\n";
 }

void endpage()
 {
  endpage(cout);
 }

void showcopyright(ostream &out)
 {
  out << "<h6>DBOX-WWW Interface, &copy; 1995-2001 "
         "<a href=\"http://www.handshake.de/user/kroening/\">"
         "Daniel Kr&ouml;ning</a></h6><p>\n";
 }

void showcopyright()
 {
  showcopyright(cout);
 }

void mkgifbutton(const char *gifname, const char *alt,
                 const char *link)
 {
  printf("<a href=\"%s\" target=\"_top\">"
         "<img src=\"/html/webonline/%s.gif\" "
         "alt=\"%s\" border=0></a>\n", 
         link, gifname, alt);
 }

const char *get_mime_type(const char *filename)
 {
  int i;
  const char *extention=strrchr(filename, '.');
  if(extention==NULL) return NULL;

  extention++;

  for(i=0; i<EXT_TABLE_LEN; i++)
    if(strcasecmp(extention, ext_mime_table[i].name)==0)
      return ext_mime_table[i].mime_type;

  return NULL;
 }

void redirect(ostream &out, const string &url)
 {
  out << "HTTP/1.0 302 Keep cool\r\n"
         "MIME-Version: 1.0\r\n"
         "Server: DBOX-WWW Interface\r\n"
         "Content-Type: text/html\r\n"
         "Location: " << url << "\r\n"
         "Content-Encoding: binary\r\n"
         "\r\n";

  out << 
    "<HTML>\n"
    "<HEAD>\n"
    "<TITLE>Redirection</TITLE>\n"
    "<BODY BGCOLOR=\"" << config.webonline_bgcolor << "\">\n";

  out << "<a href=\"" << url << "\">click here</a>\n";

  endpage(out);
 }




syntax highlighted by Code2HTML, v. 0.9.1