File: webReport.cpp

package info (click to toggle)
bzflag 2.4.2%2Bds1-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 28,016 kB
  • ctags: 18,545
  • sloc: cpp: 149,420; sh: 13,654; ansic: 3,831; makefile: 2,404; perl: 482; python: 260; objc: 251; php: 162; xml: 24
file content (188 lines) | stat: -rw-r--r-- 4,979 bytes parent folder | download
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
/* bzflag
 * Copyright (c) 1993-2012 Tim Riker
 *
 * This package is free software;  you can redistribute it and/or
 * modify it under the terms of the license found in the file
 * named COPYING that should have accompanied this file.
 *
 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

// webReport.cpp : Defines the entry point for the DLL application.
//

#include "bzfsAPI.h"
#include "bzfsHTTPAPI.h"

std::string templatesDir;

class WebReport : public bz_Plugin, public bzhttp_VDir, public bzhttp_TemplateCallback
{
public:
  WebReport();

  virtual const char* Name (){return "Web Reports";}
  virtual void Init(const char* config);
  virtual void Cleanup();

  void loadDefaultTemplates(void);

  virtual const char* GetTemplateKey(const char* /* key */);
  virtual bool GetTemplateLoop(const char* /* key */, const char* /*param*/);
  virtual bool GetTemplateIF(const char* /* key */, const char* /*param*/);

  std::string noAuthDefaultTeplate,reportDefaultTemplate;
  bool evenLine;
  bool valid;
  bz_APIStringList *reports;
  int report;

  virtual const char* VDirName(){return "WebReport";}
  virtual const char* VDirDescription(){return "View Reports On-line";}

  virtual bzhttp_ePageGenStatus GeneratePage ( const bzhttp_Request& request, bzhttp_Responce &responce );
  virtual bool GenerateNoAuthPage ( const bzhttp_Request& request, bzhttp_Responce &responce);

  virtual bool AllowResourceDownloads ( void );

  protected:
    std::string resourceDir;
};

BZ_PLUGIN(WebReport)

WebReport::WebReport() :bz_Plugin(), bzhttp_VDir(),bzhttp_TemplateCallback()
{
  evenLine = false;
  valid = false;
}

void WebReport::Init(const char *commandLine)
{
  bzhttp_RegisterVDir(this,this);
  this->RequiredAuthentiction = eBZID;
  this->CacheAuthentication = true;

  loadDefaultTemplates();

  if (commandLine)
    resourceDir = commandLine;
  if (resourceDir.size())
  {
    bzhttp_AddSearchPath("ReportTemplates",commandLine);
    this->ResourceDirs.push_back(std::string(commandLine));
  }
}

bool WebReport::AllowResourceDownloads ( void )
{
  return resourceDir.size() > 0;
}


void WebReport::Cleanup()
{
  bzhttp_RemoveAllVdirs(this);
  Flush();
}

const char* WebReport::GetTemplateKey(const char* _key)
{
  std::string key = _key;
  if (key == "evenodd")
    return evenLine ? "even" : "odd";
  else if (key =="report") {
    if (reports && report > 0 && report < (int)reports->size())
      return reports->get(report).c_str();
  }
  return "";
}

bool WebReport::GetTemplateLoop(const char* _key, const char* /*_param*/)
{
  std::string key = _key;
  if (key != "report")
    return false;

  if (!reports || !reports->size())
    return false;

  report++;
  if (report >= (int)reports->size())
    return false;

  evenLine = !evenLine;

  return true;
}

bool WebReport::GetTemplateIF(const char* _key, const char* /*_param*/)
{
  std::string key = _key;
  if (key == "valid")
    return valid;
  return false;
}

 bzhttp_ePageGenStatus WebReport::GeneratePage ( const bzhttp_Request& request, bzhttp_Responce &responce )
{
  evenLine = false;
  reports = NULL;

  if (!request.UserHasPerm(bz_perm_viewReports))
    return GenerateNoAuthPage(request,responce) ? ePageDone : eNoPage;

  responce.ReturnCode = e200OK;
  responce.DocumentType = eHTML;
 // unsigned int sessionID = request.Session->SessionID;

  std::string action = request.GetParamater("action");

  reports = bz_getReports();
  report = -1;

  // find the report file
  const char* file = bzhttp_FindFile("ReportTemplates","reports.tmpl");
  if (file)
    responce.AddBodyData(bzhttp_RenderTemplate(file,this).c_str());
  else
    responce.AddBodyData(bzhttp_RenderTemplateFromText(reportDefaultTemplate.c_str(),this).c_str());

  if (reports)
    bz_deleteStringList(reports);

  return ePageDone;
}

bool WebReport::GenerateNoAuthPage ( const bzhttp_Request& /*request*/, bzhttp_Responce &responce)
{
  const char* file = bzhttp_FindFile("ReportTemplates","report_noAuth.tmpl");
  if (file)
    responce.AddBodyData(bzhttp_RenderTemplate(file,this).c_str());
  else
  {
    file = bzhttp_FindFile("ReportTemplates","unauthorized.tmpl");
    if (file)
      responce.AddBodyData(bzhttp_RenderTemplate(file,this).c_str());
    else
      responce.AddBodyData(bzhttp_RenderTemplateFromText(noAuthDefaultTeplate.c_str(),this).c_str());
  }

  return true;
}

void WebReport::loadDefaultTemplates(void)
{
  reportDefaultTemplate = "<html><body>[?IF Valid][*START Reports][$Report]<br>[*END Reports]There are no reports, sorry[*EMPTY Reports][?ELSE Valid]Invalid Login, sorry[?END Valid]</body></html>";
  noAuthDefaultTeplate = "<html><body>Unauthorized User</body></html>";
}

// Local Variables: ***
// mode: C++ ***
// tab-width: 8 ***
// c-basic-offset: 2 ***
// indent-tabs-mode: t ***
// End: ***
// ex: shiftwidth=2 tabstop=8