]> git.ipfire.org Git - thirdparty/sarg.git/commitdiff
Improve javascript inclusion in HTML files
authorFrédéric Marchal <fmarchal@users.sourceforge.net>
Sun, 21 Nov 2010 08:19:10 +0000 (08:19 +0000)
committerFrédéric Marchal <fmarchal@users.sourceforge.net>
Sun, 21 Nov 2010 08:19:10 +0000 (08:19 +0000)
The javascript to sort the table is not always included in every
report. It is now loaded only when necessary.

The path to the javascript to include in the HTML report may be
relative to the output_dir.

The code allows for the future use of more javascripts to be loaded on
demand by some pages.

19 files changed:
authfail.c
dansguardian_report.c
denied.c
documentation/util.txt
download.c
grepday.c
html.c
include/conf.h
include/defs.h
index.c
repday.c
report.c
sarg.conf
siteuser.c
squidguard_report.c
topsites.c
topuser.c
useragent.c
util.c

index 9f1ad289db570e6e0a156a5d6f6b2798dfae317d..be634698003061b12dde3e584c2a914748feeddd 100644 (file)
@@ -90,7 +90,7 @@ void authfail_report(void)
      exit(EXIT_FAILURE);
    }
 
-   write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Authentication Failures"));
+   write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Authentication Failures"),HTML_JS_NONE);
    fputs("<tr><td class=\"header_c\">",fp_ou);
    fprintf(fp_ou,_("Period: %s"),period.html);
    fputs("</td></tr>\n",fp_ou);
index 1662e48c7c3cd58805029b20d89db09beef073c5..e1e3b3f196eb6335e561a3c04cf67e3be77f9862 100644 (file)
@@ -69,7 +69,7 @@ void dansguardian_report(void)
      exit(EXIT_FAILURE);
    }
 
-   write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("DansGuardian"));
+   write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("DansGuardian"),HTML_JS_NONE);
    fputs("<tr><td class=\"header_c\">",fp_ou);
    fprintf(fp_ou,_("Period: %s"),period.html);
    fputs("</td></tr>\n",fp_ou);
index 36f35f4c356e8c55ab78496530d7ffa0d3bf0df7..553a62200bf9df5a17f5dcb1a5c5012f779258ab 100644 (file)
--- a/denied.c
+++ b/denied.c
@@ -81,7 +81,7 @@ void gen_denied_report(void)
      exit(EXIT_FAILURE);
    }
 
-   write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Denied"));
+   write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Denied"),HTML_JS_NONE);
    fputs("<tr><td class=\"header_c\">",fp_ou);
    fprintf(fp_ou,_("Period: %s"),period.html);
    fputs("</td></tr>\n",fp_ou);
index 41772b2d4877f85a219f854270a4ca5eec0b42be..70a5b8ca71ec82d325b5fdcab641ea17d6f33217 100644 (file)
@@ -725,13 +725,16 @@ Get the size, in human readable form and kibibytes, of the content of a director
 
 
 
-/*! \fn void write_html_head(FILE *fp_ou, const char *page_title)
+/*! \fn void write_html_head(FILE *fp_ou,int depth, const char *page_title,int javascript)
 Write the header of the HTML document. The DTD corresponds to a
 transitional HTML version 4.01. The title of the document is taken from
 the global variable ::Title.
 
 \param fp_ou The file to which the HTML header is written.
+\param depth How deep is the page in the directory tree. The path of the relative javascripts is adjusted accordingly.
 \param title The title of the page.
+\param javascript Which javascript to include in the page. Is a combination of the following bits:
+  \arg HTML_JS_SORTTABLE
 */
 
 
@@ -747,6 +750,8 @@ The header of the report must be closed by a call to close_html_header().
 \param fp_ou The file to which the HTML header is written.
 \param depth How deep is the page in the directory tree. The depth is passed to show_sarg().
 \param title The title of the page.
+\param javascript Which javascript to include in the page. Is a combination of bits.
+See \see write_html_header() for the possible values.
 */
 
 
index 60b4fe5af194ad5868bbcfe1884c2715a57dcdec..10739e9781a19aa5ef52b17ede0b167f1cf6af2c 100644 (file)
@@ -76,7 +76,7 @@ void download_report(void)
       exit(EXIT_FAILURE);
    }
 
-   write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Downloads"));
+   write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Downloads"),HTML_JS_NONE);
    fputs("<tr><td class=\"header_c\">",fp_ou);
    fprintf(fp_ou,_("Period: %s"),period.html);
    fputs("</td></tr>\n",fp_ou);
index 8c39043803e9e82ed7155bc349bfbe109f9e1a6f..5d09f79bbafb86f11b7c50d6b8201ef6fbc553a6 100644 (file)
--- a/grepday.c
+++ b/grepday.c
@@ -723,7 +723,7 @@ void greport_day(const struct userinfostruct *uinfo)
      debuga(_("(grepday) Cannot open output file %s\n"),wdirname);
      exit(EXIT_FAILURE);
    }
-   write_html_head(fp_ou,_("Graph report"));
+   write_html_head(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Graph report"),HTML_JS_NONE);
 
    fputs("<table class=\"report\" cellpadding=\"0\" cellspacing=\"2\">\n", fp_ou);
    if((datetimeby & DATETIME_BYTE)!=0) {
diff --git a/html.c b/html.c
index b84093a40c8b9af25acfeef8f2f1178cbce020f7..1ee57510577ad72639e8316df33a1a7fc1d14edf 100644 (file)
--- a/html.c
+++ b/html.c
@@ -225,7 +225,7 @@ void htmlrel(void)
          exit(EXIT_FAILURE);
       }
 
-      write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("User report"));
+      write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("User report"),HTML_JS_SORTTABLE);
       fprintf(fp_ou,"<tr><td class=\"header_c\">%s:&nbsp;%s</td></tr>\n",_("Period"),period.html);
       fprintf(fp_ou,"<tr><td class=\"header_c\">%s:&nbsp;%s</td></tr>\n",_("User"),uinfo->label);
       fputs("<tr><td class=\"header_c\">",fp_ou);
index 01133649c41a9b90a621af0e3f6850c1a4b17c42..f3d4cb06e79a87da8c8cd861b8a4b0b99574f181 100755 (executable)
@@ -262,6 +262,11 @@ int mkstemps(char *template, int suffixlen);
 #define USER_SORT_CONNECT 0x0008UL
 #define USER_SORT_TIME    0x0010UL
 
+//! Value to exclude all the javascripts from the html page.
+#define HTML_JS_NONE 0x0000
+//! Bit to include sorttable.js in the html plage.
+#define HTML_JS_SORTTABLE 0x0001
+
 struct periodstruct
 {
    //! The first date of the period.
index ee237f9020150960ba986d73e421afe68a8e3c48..ef18bb7e65a819246dfb9d679b5581381c6ab80e 100755 (executable)
@@ -212,8 +212,8 @@ void fixendofline(char *str);
 void show_info(FILE *fp_ou);
 void show_sarg(FILE *fp_ou, int depth);
 void write_logo_image(FILE *fp_ou);
-void write_html_head(FILE *fp_ou, const char *page_title);
-void write_html_header(FILE *fp_ou, int depth, const char *title);
+void write_html_head(FILE *fp_ou, int depth, const char *page_title,int javascript);
+void write_html_header(FILE *fp_ou, int depth, const char *title,int javascript);
 void close_html_header(FILE *fp_ou);
 __attribute__((warn_unused_result)) int write_html_trailer(FILE *fp_ou);
 void output_html_string(FILE *fp_ou,const char *str,int maxlen);
diff --git a/index.c b/index.c
index 25daa19a4e68bd97ca20589e442ffb5b3b495113..2fc7235dd581049a72968c6a02e4f02839f989c4 100644 (file)
--- a/index.c
+++ b/index.c
@@ -138,7 +138,7 @@ static void make_date_index(void)
       debuga(_("(index) Cannot open file %s - %s\n"),yearindex,strerror(errno));
       exit(EXIT_FAILURE);
    }
-   write_html_header(fp_ou,0,ngettext("SARG report","SARG reports",nyears));
+   write_html_header(fp_ou,0,ngettext("SARG report","SARG reports",nyears),HTML_JS_NONE);
    close_html_header(fp_ou);
    fputs("<div class=\"index\"><table cellpadding=\"1\" cellspacing=\"2\">\n<tr><td></td><td></td></tr>\n",fp_ou);
    fprintf(fp_ou,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",_("YEAR"),_("SIZE"));
@@ -185,7 +185,7 @@ static void make_date_index(void)
          exit(EXIT_FAILURE);
       }
       snprintf(title,sizeof(title),ngettext("SARG: report for %04d","SARG: reports for %04d",nmonths),year);
-      write_html_header(fp_ou2,1,title);
+      write_html_header(fp_ou2,1,title,HTML_JS_NONE);
       close_html_header(fp_ou2);
       fputs("<div class=\"index\"><table cellpadding=\"1\" cellspacing=\"2\">\n<tr><td></td><td></td></tr>\n",fp_ou2);
       fprintf(fp_ou2,"<tr><th class=\"header_l\">%s/%s</th></tr>\n",_("YEAR"),_("MONTH"));
@@ -246,7 +246,7 @@ static void make_date_index(void)
             exit(EXIT_FAILURE);
          }
          snprintf(title,sizeof(title),ngettext("SARG: report for %04d/%02d","SARG: reports for %04d/%02d",ndays),year,month);
-         write_html_header(fp_ou3,2,title);
+         write_html_header(fp_ou3,2,title,HTML_JS_NONE);
          close_html_header(fp_ou3);
          fputs("<div class=\"index\"><table cellpadding=\"1\" cellspacing=\"2\">\n<tr><td></td><td></td></tr>\n",fp_ou3);
          fprintf(fp_ou3,"<tr><th class=\"header_l\">%s/%s/%s</th></tr>\n",_("YEAR"),_("MONTH"),_("DAYS"));
@@ -410,7 +410,7 @@ static void make_file_index(void)
       debuga(_("(index) Cannot open file %s\n"),wdir);
       exit(EXIT_FAILURE);
    }
-   write_html_header(fp_ou,0,ngettext("SARG report","SARG reports",nsort));
+   write_html_header(fp_ou,0,ngettext("SARG report","SARG reports",nsort),HTML_JS_SORTTABLE);
    close_html_header(fp_ou);
    fputs("<div class=\"index\"><table cellpadding=\"1\" cellspacing=\"2\"",fp_ou);
    if (SortTableJs[0]) fputs(" class=\"sortable\"",fp_ou);
index 4a610f1943fd9b8582142a565d43ac6db8e65d98..2b587d0031eaf947c3be5c8a705055d7cf04716e 100644 (file)
--- a/repday.c
+++ b/repday.c
@@ -124,7 +124,7 @@ void report_day(const struct userinfostruct *uinfo)
      exit(EXIT_FAILURE);
    }
 
-   write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Day report"));
+   write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Day report"),HTML_JS_NONE);
    fprintf(fp_ou,"<tr><td class=\"header_c\" colspan=\"2\">%s:&nbsp;%s</td></tr>\n",_("Period"),period.html);
    fprintf(fp_ou,"<tr><th class=\"header_c\" colspan=\"2\">%s:&nbsp;%s</th></tr>\n",_("User"),uinfo->label);
    close_html_header(fp_ou);
index 814c27800e1c231b2ef47398187127786ab18008..6f49e6d8217ea93014897ce34fdf0f3c13b98d60 100644 (file)
--- a/report.c
+++ b/report.c
@@ -278,7 +278,7 @@ void gerarel(void)
                   sprintf(httplink,"<font size=%s><a href=\"http://%s\">%s</a>",FontSize,accurl,accurl);
                */
 
-               write_html_header(fp_tt,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Site access report"));
+               write_html_header(fp_tt,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Site access report"),HTML_JS_NONE);
                fprintf(fp_tt,"<tr><td class=\"header_c\">%s:&nbsp;%s</td></tr>\n",_("Period"),period.html);
                fprintf(fp_tt,"<tr><td class=\"header_c\">%s:&nbsp;%s</td></tr>\n",_("User"),uinfo->label);
                fputs("<tr><td class=\"header_c\">",fp_tt);
index 7bee12607a7266e0cb92c96b74cd40ba9c8a422d..1a2877b056aa9c1d4976aa2709d9241f36c11037 100644 (file)
--- a/sarg.conf
+++ b/sarg.conf
 # TAG: sorttable path
 #      The full path to a javascript script to dynamically sort the tables.
 #      The path is the path a browser must follow to find the script.
+#
+#      If the path starts with "../" then it is assumed to be a relative
+#      path and sarg adds as many "../" as necessary to locate the js script from
+#      the output directory. Therefore, ../../sorttable.js searches for the javascript
+#      one level above output_dir.
+#
 #      If this entry is set, each sortable table will have the "sortable" class set.
 #      You may have a look at http://www.kryogenix.org/code/browser/sorttable/
-#      for such as implementation.
+#      for the implementation on which sarg is based.
 #
 # sorttable /sorttable.js
index e1e6c284b434e5d1afc2e379faf6ee65fa0db92a..48bf8762f1f5ccf42911927d37140b3608e2da45 100644 (file)
@@ -79,7 +79,7 @@ void siteuser(void)
      exit(EXIT_FAILURE);
    }
 
-   write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Sites & Users"));
+   write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Sites & Users"),HTML_JS_SORTTABLE);
    fputs("<tr><td class=\"header_c\">",fp_ou);
    fprintf(fp_ou,_("Period: %s"),period.html);
    fputs("</td></tr>\n",fp_ou);
index 36b1f305b0de7ff9f332b63dc3f71434055a9ba5..31aadb17f2e0f371a49d2b9bb937a0701a0b962f 100644 (file)
@@ -80,7 +80,7 @@ void squidguard_report(void)
       exit(EXIT_FAILURE);
    }
 
-   write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Redirector report"));
+   write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Redirector report"),HTML_JS_NONE);
    fputs("<tr><td class=\"header_c\">",fp_ou);
    fprintf(fp_ou,_("Period: %s"),period.html);
    fputs("</td></tr>\n",fp_ou);
index 0cea0a5a00842634a83923537950a0b903dcccb4..98d76ae99709591f0e4999b91d86902ba9e0e638 100644 (file)
@@ -185,7 +185,7 @@ void topsites(void)
      exit(EXIT_FAILURE);
    }
 
-   write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Top sites"));
+   write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Top sites"),HTML_JS_SORTTABLE);
    fputs("<tr><td class=\"header_c\">",fp_ou);
    fprintf(fp_ou,_("Period: %s"),period.html);
    fputs("</td></tr>\n",fp_ou);
index d4245f13f75667f6306b6d1e362b5a4bf9d99dbb..2ecb87c253f9e4d192cf8cac00314ffde2e16c45 100644 (file)
--- a/topuser.c
+++ b/topuser.c
@@ -178,7 +178,7 @@ void topuser(void)
    }
 
    snprintf(title,sizeof(title),_("SARG report for %s"),period.text);
-   write_html_header(fp_top3,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,title);
+   write_html_header(fp_top3,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,title,HTML_JS_SORTTABLE);
    fputs("<tr><td class=\"header_c\">",fp_top3);
    fprintf(fp_top3,_("Period: %s"),period.html);
    fputs("</td></tr>\n",fp_top3);
index a26bce5fa841f2aee45e5979c57f822e1ad30ae3..a0f967ce6540c1dad667f80bf338685b92b6255b 100644 (file)
@@ -157,7 +157,7 @@ void useragent(void)
    if(debug)
       debuga(_("Making Useragent report\n"));
 
-   write_html_header(fp_ht,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Squid Useragent's Report"));
+   write_html_header(fp_ht,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Squid Useragent's Report"),HTML_JS_NONE);
    fprintf(fp_ht,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Squid Useragent's Report"));
    fprintf(fp_ht,"<tr><td class=\"header_c\">%s: %s - %s</td></tr>\n",_("Period"),idate,fdate);
    close_html_header(fp_ht);
diff --git a/util.c b/util.c
index a0bb63ccafa927d63ea9d3dfedc09e1fdc375833..84c85dba921ffefd6090a9c09e439d9b08167b1d 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1600,20 +1600,28 @@ void write_logo_image(FILE *fp_ou)
       fprintf(fp_ou, "<div class=\"logo\"><img src=\"%s\" width=\"%s\" height=\"%s\" alt=\"Logo\">&nbsp;%s</div>\n",LogoImage,Width,Height,LogoText);
 }
 
-void write_html_head(FILE *fp_ou, const char *page_title)
+void write_html_head(FILE *fp_ou, int depth, const char *page_title,int javascript)
 {
+   int i;
+
    fputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n<html>\n",fp_ou);
    fprintf(fp_ou, "<head>\n  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet);
    if (page_title) fprintf(fp_ou,"<title>%s</title>\n",page_title);
    css(fp_ou);
-   if (SortTableJs[0])
-      fprintf(fp_ou,"<script type=\"text/javascript\" src=\"%s\"></script>\n",SortTableJs);
+   if ((javascript & HTML_JS_SORTTABLE)!=0 && SortTableJs[0]) {
+      fputs("<script type=\"text/javascript\" src=\"",fp_ou);
+      if (strncmp(SortTableJs,"../",3)==0) {
+         for (i=0 ; i<depth ; i++) fputs("../",fp_ou);
+      }
+      fputs(SortTableJs,fp_ou);
+      fputs("\"></script>\n",fp_ou);
+   }
    fputs("</head>\n<body>\n",fp_ou);
 }
 
-void write_html_header(FILE *fp_ou, int depth, const char *page_title)
+void write_html_header(FILE *fp_ou, int depth, const char *page_title,int javascript)
 {
-   write_html_head(fp_ou,page_title);
+   write_html_head(fp_ou,depth,page_title,javascript);
    write_logo_image(fp_ou);
    show_sarg(fp_ou, depth);
    fprintf(fp_ou,"<div class=\"title\"><table cellpadding=\"0\" cellspacing=\"0\">\n<tr><th class=\"title_c\">%s</th></tr>\n",Title);