From: Frédéric Marchal Date: Sat, 18 Sep 2010 12:36:16 +0000 (+0000) Subject: Don't use string comparison to sort the top users, top sites and user lists. Allow... X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=49767767467df649155c1593e2c837fa316a8bc7;p=thirdparty%2Fsarg.git Don't use string comparison to sort the top users, top sites and user lists. Allow for the translation of the sort criterion and order of the top users, top sites and user lists. --- diff --git a/documentation/sort.txt b/documentation/sort.txt index 8da855e..c72d19b 100644 --- a/documentation/sort.txt +++ b/documentation/sort.txt @@ -32,3 +32,15 @@ date, the time and the URL. In both cases, the sorted files are written in files with the extension \c log and the name of the unsorted file without the \c unsort extension. The unsorted file is deleted just after the sorting. */ + + + + + +/*! \fn void sort_labels(const char **label,const char **order) +Get the internationalized text to display when reporting the sort criterion and order +of a user list. + +\param label A pointer to set to the string of the sort criterion name. +\param order A pointer to set to the string of the sort order name +*/ diff --git a/getconf.c b/getconf.c index dd3ae27..a502978 100644 --- a/getconf.c +++ b/getconf.c @@ -41,6 +41,14 @@ struct param_list unsigned long int exclude; }; +struct sort_list +{ + //! The name of the value of the parameter. + const char *name; + //! The bit to set if the value is found. + unsigned long int value; +}; + static struct param_list report_type_values[]= { {"users_sites",REPORT_TYPE_USERS_SITES,0}, @@ -148,6 +156,28 @@ static struct param_list realtime_unauth_values[]= {"ignore",REALTIME_UNAUTH_REC_IGNORE,~REALTIME_UNAUTH_REC_IGNORE}, }; +struct sort_list topuser_sort[]= +{ + {"BYTES",TOPUSER_SORT_BYTES}, + {"USER",TOPUSER_SORT_USER}, + {"CONNECT",TOPUSER_SORT_CONNECT}, + {"TIME",TOPUSER_SORT_TIME}, +}; + +struct sort_list topsite_sort[]= +{ + {"BYTES",TOPSITE_SORT_BYTES}, + {"CONNECT",TOPSITE_SORT_CONNECT}, +}; + +struct sort_list user_sort[]= +{ + {"BYTES",USER_SORT_BYTES}, + {"SITE",USER_SORT_SITE}, + {"CONNECT",USER_SORT_CONNECT}, + {"TIME",USER_SORT_TIME}, +}; + static int is_param(const char *param,const char *buf) { int plen; @@ -325,6 +355,52 @@ static int getparam_list(const char *param,struct param_list *options,int noptio return(1); } +static int getparam_sort(const char *param,struct sort_list *options,int noptions,char *buf,unsigned long int *value) +{ + int plen; + char *str, *order; + int i; + + plen=strlen(param); + if (strncmp(buf,param,plen) != 0) return(0); + buf+=plen; + if ((unsigned char)*buf>' ') return(0); + while (*buf && (unsigned char)*buf<=' ') buf++; + + str=buf; + order=NULL; + while (*str && (unsigned char)*str>' ') str++; + if (*str) { + *str++='\0'; + while (*str && (unsigned char)*str<=' ') str++; + order=str; + } + for (i=0 ; i=noptions) { + debuga(_("Unknown sort criterion \"%s\" for parameter \"%s\"\n"),buf,param); + exit(EXIT_FAILURE); + } + *value|=options[i].value; + + if (order) { + str=order; + while (*str && (unsigned char)*str>' ') str++; + if (*str) { + *str++='\0'; + while (*str && (unsigned char)*str<=' ') str++; + } + if (strcasecmp(order,"reverse")==0 || strcasecmp(order,"D")==0) { + *value|=SORT_REVERSE; + } else if (strcasecmp(order,"normal")!=0 && strcasecmp(order,"A")!=0) { + debuga(_("Unknown sort order \"%s\" for parameter \"%s\"\n"),order,param); + exit(EXIT_FAILURE); + } + } + + buf=str; + return(1); +} + static void parmtest(char *buf) { char wbuf[2048]; @@ -407,9 +483,9 @@ static void parmtest(char *buf) return; } - if (getparam_2words("topuser_sort_field",buf,TopuserSortField,sizeof(TopuserSortField),TopuserSortOrder,sizeof(TopuserSortOrder))>0) return; + if (getparam_sort("topuser_sort_field",SET_LIST(topuser_sort),buf,&TopuserSort)>0) return; - if (getparam_2words("user_sort_field",buf,UserSortField,sizeof(UserSortField),UserSortOrder,sizeof(UserSortOrder))>0) return; + if (getparam_sort("user_sort_field",SET_LIST(user_sort),buf,&UserSort)>0) return; if (is_param("access_log",buf)>0) { if (AccessLogFromCmdLine==0) { @@ -484,7 +560,7 @@ static void parmtest(char *buf) if (getparam_string("index_sort_order",buf,IndexSortOrder,sizeof(IndexSortOrder))>0) return; - if (getparam_2words("topsites_sort_order",buf,TopsitesSortField,sizeof(TopsitesSortField),TopsitesSortType,sizeof(TopsitesSortType))>0) return; + if (getparam_sort("topsites_sort_order",SET_LIST(topsite_sort),buf,&TopsitesSort)>0) return; if (getparam_bool("long_url",buf,&LongUrl)>0) return; diff --git a/html.c b/html.c index ac108f5..4611ebc 100644 --- a/html.c +++ b/html.c @@ -55,6 +55,8 @@ void htmlrel(void) int count; int cstatus; const char txtext[]=".txt"; + const char *sort_field; + const char *sort_order; int dlen; char siteind[MAX_TRUNCATED_URL]; struct getwordstruct gwarea; @@ -108,6 +110,8 @@ void htmlrel(void) greport_prepare(); + sort_labels(&sort_field,&sort_order); + if ((dirp = opendir(tmp)) == NULL) { debuga(_("Failed to open directory %s - %s\n"),tmp,strerror(errno)); exit(EXIT_FAILURE); @@ -224,7 +228,9 @@ void htmlrel(void) write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("User report")); fprintf(fp_ou,"%s: %s\n",_("Period"),period.html); fprintf(fp_ou,"%s: %s\n",_("User"),uinfo->label); - fprintf(fp_ou,"%s: %s, %s\n",_("Sort"),UserSortField,UserSortOrder); + fputs("",fp_ou); + fprintf(fp_ou,_("Sort: %s, %s"),sort_field,sort_order); + fputs("\n",fp_ou); fprintf(fp_ou,"%s\n",_("User report")); close_html_header(fp_ou); diff --git a/include/conf.h b/include/conf.h index efbc40c..96b0c9d 100755 --- a/include/conf.h +++ b/include/conf.h @@ -244,6 +244,24 @@ int mkstemps(char *template, int suffixlen); #define REALTIME_UNAUTH_REC_SHOW 0x0001UL #define REALTIME_UNAUTH_REC_IGNORE 0x0002UL +#define SORT_REVERSE 0x0001 + +#define TOPUSER_SORT_REVERSE SORT_REVERSE +#define TOPUSER_SORT_BYTES 0x0002UL +#define TOPUSER_SORT_USER 0x0004UL +#define TOPUSER_SORT_CONNECT 0x0008UL +#define TOPUSER_SORT_TIME 0x0010UL + +#define TOPSITE_SORT_REVERSE SORT_REVERSE +#define TOPSITE_SORT_BYTES 0x0002UL +#define TOPSITE_SORT_CONNECT 0x0004UL + +#define USER_SORT_REVERSE SORT_REVERSE +#define USER_SORT_BYTES 0x0002UL +#define USER_SORT_SITE 0x0004UL +#define USER_SORT_CONNECT 0x0008UL +#define USER_SORT_TIME 0x0010UL + struct periodstruct { //! The first date of the period. @@ -278,8 +296,7 @@ char MailUtility[PATH_MAX]; int TopSitesNum; int TopUsersNum; char ExcludeCodes[256]; -char TopsitesSortField[15]; -char TopsitesSortType[20]; +unsigned long int TopsitesSort; unsigned long int ReportType; char UserTabFile[255]; char warea[MAXLEN]; @@ -308,10 +325,8 @@ char PasswdFile[MAXLEN]; char TempDir[MAXLEN]; char OutputDir[MAXLEN]; char OutputEmail[MAXLEN]; -char TopuserSortField[30]; -char UserSortField[30]; -char TopuserSortOrder[10]; -char UserSortOrder[10]; +unsigned long int TopuserSort; +unsigned long int UserSort; char UserAgentLog[255]; char module[255]; char ExcludeHosts[255]; diff --git a/include/defs.h b/include/defs.h index 4922bc7..6bfe656 100755 --- a/include/defs.h +++ b/include/defs.h @@ -147,6 +147,7 @@ void smartfilter_report(void); // sort.c void sort_users_log(const char *tmp, int debug); void tmpsort(void); +void sort_labels(const char **label,const char **order); // splitlog.c void splitlog(const char *arq, char *df, int dfrom, int duntil, int convert); diff --git a/log.c b/log.c index eecb272..41299ff 100644 --- a/log.c +++ b/log.c @@ -233,12 +233,9 @@ int main(int argc,char *argv[]) TopSitesNum=100; TopUsersNum=0; UserIp=0; - strcpy(TopuserSortField,"BYTES"); - strcpy(UserSortField,"BYTES"); - strcpy(TopuserSortOrder,"reverse"); - strcpy(UserSortOrder,"reverse"); - strcpy(TopsitesSortField,"CONNECT"); - strcpy(TopsitesSortType,"D"); + TopuserSort=TOPUSER_SORT_BYTES | TOPUSER_SORT_REVERSE; + UserSort=USER_SORT_BYTES | USER_SORT_REVERSE; + TopsitesSort=TOPSITE_SORT_CONNECT | TOPSITE_SORT_REVERSE; LongUrl=0; strcpy(FontFace,"Verdana,Tahoma,Arial"); datetimeby=DATETIME_BYTE; diff --git a/report.c b/report.c index aab9312..814c278 100644 --- a/report.c +++ b/report.c @@ -70,6 +70,8 @@ void gerarel(void) DIR *dirp; struct dirent *direntp; const char logext[]=".log"; + const char *sort_field; + const char *sort_order; int dlen; int url_len; int ourl_size=0; @@ -147,6 +149,7 @@ void gerarel(void) maketmp(user,tmp,debug,indexonly); maketmp_hour(user,tmp,indexonly); + sort_labels(&sort_field,&sort_order); ttopen=0; oldurltt=NULL; @@ -278,7 +281,9 @@ void gerarel(void) write_html_header(fp_tt,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Site access report")); fprintf(fp_tt,"%s: %s\n",_("Period"),period.html); fprintf(fp_tt,"%s: %s\n",_("User"),uinfo->label); - fprintf(fp_tt,"%s: %s, %s\n",_("Sort"),UserSortField,UserSortOrder); + fputs("",fp_tt); + fprintf(fp_tt,_("Sort: %s, %s"),sort_field,sort_order); + fputs("\n",fp_tt); fprintf(fp_tt,"%s\n",_("User")); close_html_header(fp_tt); diff --git a/sort.c b/sort.c index ad7a784..c2867e6 100644 --- a/sort.c +++ b/sort.c @@ -40,29 +40,29 @@ void tmpsort(void) const char *field1="2,2"; const char *field2="1,1"; const char *field3="3,3"; - const char *order="-r"; + const char *order; if(indexonly) return; if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return; - strup(UserSortField); - if(strcmp(UserSortField,"CONNECT") == 0) { + if((UserSort & USER_SORT_CONNECT) != 0) { field1="1,1"; field2="2,2"; field3="3,3"; - } else if(strcmp(UserSortField,"SITE") == 0) { + } else if((UserSort & USER_SORT_SITE) != 0) { field1="3,3"; field2="2,2"; field3="1,1"; - } else if(strcmp(UserSortField,"TIME") == 0) { + } else if((UserSort & USER_SORT_TIME) != 0) { field1="5,5"; field2="2,2"; field3="1,1"; } - strlow(UserSortOrder); - if(strcmp(UserSortOrder,"normal") == 0) + if((UserSort & USER_SORT_REVERSE) == 0) order=""; + else + order="-r"; if ((dirp = opendir(tmp)) == NULL) { debuga(_("Failed to open directory %s - %s\n"),tmp,strerror(errno)); @@ -170,3 +170,21 @@ void sort_users_log(const char *tmp, int debug) return; } + +void sort_labels(const char **label,const char **order) +{ + if((UserSort & USER_SORT_CONNECT) != 0) { + *label=_("connect"); + } else if((UserSort & USER_SORT_SITE) != 0) { + *label=_("site"); + } else if((UserSort & USER_SORT_TIME) != 0) { + *label=_("time"); + } else { + *label=_("bytes"); + } + + if((UserSort & USER_SORT_REVERSE) == 0) + *order=_("normal"); + else + *order=_("reverse"); +} diff --git a/topsites.c b/topsites.c index 7e1c202..7207b4d 100644 --- a/topsites.c +++ b/topsites.c @@ -153,21 +153,16 @@ void topsites(void) fclose(fp_ou); - strlow(TopsitesSortField); - strlow(TopsitesSortType); - - if(strcmp(TopsitesSortField,"connect") == 0) + if((TopsitesSort & TOPSITE_SORT_CONNECT) != 0) { sortf="-k 1,1 -k 2,2"; - else if(strcmp(TopsitesSortField,"bytes") == 0) + } else { sortf="-k 2,2 -k 1,1"; - else - sortf=""; - if(strcmp(TopsitesSortType,"a") == 0) - sortt=""; - else if(strcmp(TopsitesSortType,"d") == 0) + } + if((TopsitesSort & TOPSITE_SORT_REVERSE) != 0) { sortt="-r"; - else + } else { sortt=""; + } sprintf(csort,"sort %s -n %s -o \"%s\" \"%s\"",sortt,sortf,sites,general3); cstatus=system(csort); diff --git a/topuser.c b/topuser.c index b7d85a4..f5aa4fe 100644 --- a/topuser.c +++ b/topuser.c @@ -46,7 +46,9 @@ void topuser(void) char user[MAX_USER_LEN], tusr[MAXLEN]; char ipantes[MAXLEN], nameantes[MAXLEN]; const char *sfield="-n -k 2,2"; - const char *order="-r"; + const char *order; + const char *sort_field; + const char *sort_order; char title[80]; char *warea; int totuser=0; @@ -132,20 +134,26 @@ void topuser(void) } fclose(fp_top2); - strup(TopuserSortField); - strlow(TopuserSortOrder); - - if(strcmp(TopuserSortField,"USER") == 0) + if((TopuserSort & TOPUSER_SORT_USER) != 0) { sfield="-k 1,1"; - - if(strcmp(TopuserSortField,"CONNECT") == 0) + sort_field=_("user"); + } else if((TopuserSort & TOPUSER_SORT_CONNECT) != 0) { sfield="-n -k 3,3"; - - if(strcmp(TopuserSortField,"TIME") == 0) + sort_field=_("connect"); + } else if((TopuserSort & TOPUSER_SORT_TIME) != 0) { sfield="-n -k 4,4"; + sort_field=_("time"); + } else { + sort_field=_("bytes"); + } - if(strcmp(TopuserSortOrder,"normal") == 0) + if((TopuserSort & TOPUSER_SORT_REVERSE) == 0) { order=""; + sort_order=_("normal"); + } else { + order="-r"; + sort_order=_("reverse"); + } snprintf(top1,sizeof(top1),"%s/top",outdirname); sprintf(csort,"sort -T \"%s\" %s %s -o \"%s\" \"%s\"", tmp, order, sfield, top1, top2); @@ -174,7 +182,9 @@ void topuser(void) fputs("",fp_top3); fprintf(fp_top3,_("Period: %s"),period.html); fputs("\n",fp_top3); - fprintf(fp_top3,"%s: %s, %s\n",_("Sort"),TopuserSortField,TopuserSortOrder); + fputs("",fp_top3); + fprintf(fp_top3,_("Sort: %s, %s"),sort_field,sort_order); + fputs("\n",fp_top3); fprintf(fp_top3,"%s\n",_("Top users")); close_html_header(fp_top3);