/** BEGIN COPYRIGHT BLOCK * Copyright 2001 Sun Microsystems, Inc. * Portions copyright 1999, 2001-2003 Netscape Communications Corporation. * All rights reserved. * END COPYRIGHT BLOCK **/ /* * template.c: The actual HTML templates in a static variable * * All blame to Mike McCool */ #include #include #include #include "template.h" #include "libadmin/libadmin.h" #include "libadmin/dbtlibadmin.h" #include "base/util.h" /* If you add something to this structure, don't forget to document it * in templates.h, and increase MAXTEMPLATE! * * Also, save yourself a lot of grief and put a space after the name. */ static struct template_s templates[MAXTEMPLATE] = { {"IF ", "FUNC conditional"}, {"ELSE ", "FUNC conditional"}, {"ENDIF ", "FUNC conditional"}, {"TITLE ", "%s\n" "\n"}, {"PAGEHEADER ", "FUNC pageheader"}, {"DOCSWITCHER ", ""}, {"COPYRIGHT ", ""}, {"RESOURCEPICKER ", "FUNC respicker"}, {"BOOKTRACK ", "FUNC booktrack"}, {"BEGININFO ", "\n" "" #if 0 "" "
" "%s
" "\"*\""\n"}, #endif "\n"}, {"ADDINFO ", "
"}, {"ENDINFO ", "
\n
\n"}, {"SUBMIT ", "FUNC submit\n"}, {"DOCUMENTROOT ", "FUNC docroot"}, {"BEGINELEM ", "
"},
/*  {"ELEM ", "
%s"}, */ {"ELEM ", "\n%s"}, /* {"ENDELEM ", "
\n"}, */ {"ENDELEM ", "\n"}, {"ELEMADD ", "%s"}, /* {"ELEMDIV ", "
"}, */ {"ELEMDIV ", "\n"}, {"REFERER ", "FUNC link_referer"}, {"INDEX ", "%s\n"}, {"SERVERROOT ", "FUNC serverroot"}, {"RESTART ", "%s\n"}, {"ACCESS ", "FUNC makeurl"}, {"COMMIT ", "%s\n"}, {"BACKOUT ", "
If you don't want to %s, you can " "return to the server manager.
\n"}, {"CURSERVNAME", "FUNC curservname"}, {"VERIFY ", "FUNC verify"}, {"HELPBUTTON", "FUNC helpbutton"}, {"DIALOGSUBMIT", "FUNC dialogsubmit"}, {"HELPJSFN", "FUNC helpjsfn"} }; int get_directive(char *string); void conditional(char *input, char **vars, int index); void respicker(char **config); void currentres(char **config); void prevres(char **config); void booktrack(char *input, char **vars); void docswitcher(char *input); void docroot(char **vars); void link_referer(char **input, char **vars); void serverroot(char **vars); char **get_vars(char *string); static void output(char *string); void makeurl(char **vars); void curservname(void); void pageheader(char **vars, char **config); void submit(int verify, char **vars); void helpbutton(char *topic); void dialogsubmit(char *topic); static int status = -1; /* Filter a page. Takes the page to filter as an argument. Uses above * filters to process. */ NSAPI_PUBLIC int parse_line(char *line_input, char **input) { register int index; char *position; int dirlen = strlen(DIRECTIVE_START); char **vars; if(!strncmp(line_input, DIRECTIVE_START, dirlen)) { position = (char *) (line_input + dirlen); index = get_directive(position); /* did we get one? */ if(index != -1) { /* if so, get the vars. */ position += strlen(templates[index].name); vars = get_vars(position); /* Dispatch the correct function (done for readability, * although I'm starting to wonder if I should bother) */ if(!strncmp(templates[index].format, "FUNC ", 5)) { if(!strncmp(templates[index].format+5, "conditional", 11)) conditional(input[0], vars, index); else if(!strncmp(templates[index].format+5, "respicker", 9)) respicker(input); else if(!strncmp(templates[index].format+5, "booktrack", 9)) booktrack(input[0], vars); else if(!strncmp(templates[index].format+5, "docswitcher", 11)) docswitcher(input[0]); else if(!strncmp(templates[index].format+5, "docroot", 7)) docroot(vars); else if(!strncmp(templates[index].format+5, "link_referer",12)) link_referer(input, vars); else if(!strncmp(templates[index].format+5, "serverroot",10)) serverroot(vars); else if(!strncmp(templates[index].format+5, "makeurl",7)) makeurl(vars); else if(!strncmp(templates[index].format+5, "curservname",11)) curservname(); else if(!strncmp(templates[index].format+5, "pageheader",10)) pageheader(vars, input); else if(!strncmp(templates[index].format+5, "submit",6)) submit(0, vars); else if(!strncmp(templates[index].format+5, "verify",6)) submit(1, vars); else if(!strncmp(templates[index].format+5, "helpbutton",10)) helpbutton(vars[0]); else if(!strncmp(templates[index].format+5, "dialogsubmit",12)) dialogsubmit(vars[0]); /* We don't know what this template is. Send it back. */ else return -1; } else { /* I just can't believe there's no easy way to create * a va_list. */ char line[BIG_LINE]; sprintf(line, templates[index].format, (vars[0] != NULL) ? vars[0]: "", (vars[1] != NULL) ? vars[1]: "", (vars[2] != NULL) ? vars[2]: "", (vars[3] != NULL) ? vars[3]: ""); output(line); } } else { /* We found a directive, but we can't identify it. Send it back.*/ /* Check status first; if we're not supposed to be outputing */ /* because of an "IF" block, don't tell the program to */ /* try and cope with it. */ if(status) return -1; else return 0; } } else /* We found no directive. The line is normal. */ output(line_input); /* If we're here, we either handled it correctly or the line was benign.*/ return 0; } void conditional(char *input, char **vars, int index) { if((!strncmp(templates[index].name, "IF", 2)) && (vars[0] != NULL)) { status = input[atoi(vars[0])] - '0'; } else if((!strncmp(templates[index].name, "ELSE", 4)) && (status != -1)) { status ^= 1; } else if(!strncmp(templates[index].name, "ENDIF", 5)) status = -1; } void respicker(char **config) { output("
\n"); output("
\n"); prevres(config); output("

\n"); output("
\n"); } void currentres(char **config) { int l; char line[BIG_LINE]; char *resname, *restype; resname = get_current_resource(config); restype = get_current_typestr(config); if(!strcmp(restype, NAME)) { if(!strcmp(resname, "default")) sprintf(line, "Modifying: " "the entire server."); else sprintf(line, "Modifying: " "the object named %s.", resname); } else if(!strcmp(restype, FILE_OR_DIR)) { l = strlen(resname) - 1; if(resname[l] == '*') { sprintf(line, "Modifying: the directory " "%s", resname); } else { sprintf(line, "Modifying: %s %s", (strchr(resname, '*')) ? "files matching" : "the file", resname); } } else if(!strcmp(restype, TEMPLATE)) { sprintf(line, "Modifying: the template %s", resname); } else if(!strcmp(restype, WILDCARD)) { #ifdef MCC_PROXY sprintf(line, "Modifying: URLs matching RE %s" #else sprintf(line, "Modifying: files matching %s" #endif "", resname); } output(line); } void prevres(char **config) { #ifndef MCC_NEWS char *res = get_current_resource(config); int rtype = get_current_restype(config); if(status) { char **options = NULL; register int x=0; int found=0; int option_cnt = total_object_count(); fprintf(stdout, "\n"); #ifdef MCC_PROXY fprintf(stdout, "\n"); fprintf(stdout, "\n"); fprintf(stdout, "\n\n\n
Editing:\n", option_cnt <=20 ? "" : "size=5"); #endif #ifdef MCC_HTTPD /* template->styles nightmare */ if((rtype==PB_NAME) && (strcmp(res, "default"))) { /* enter: STYLES MODE */ fprintf(stdout, ""); fprintf(stdout, ""); #ifdef MCC_PROXY fprintf(stdout, "
\n"); #endif fputs("", stdout); /* output("\n"); */ } #endif } void booktrack(char *input, char **vars) { char line[BIG_LINE]; if((vars[0] != NULL) && (vars[1] != NULL)) { sprintf(line, "" "\"\"", (input[0] - '0') ? vars[0] : vars[1], (vars[2] != NULL) ? vars[2] : "none"); output(line); } } void docswitcher(char *input) { char line[BIG_LINE]; char *whichimg, *whatmode; #ifdef USE_ADMSERV char *qs = getenv("QUERY_STRING"); char *sname = getenv("SCRIPT_NAME"); char *mtmp; char *tmp = getenv("SERVER_NAMES"); char *servers = NULL; if(tmp) servers = STRDUP(tmp); #endif if(!(input[0] - '0')) { whichimg = "b-clsd.gif"; whatmode = "Express mode"; } else { whichimg = "b-open.gif"; whatmode = "Full docs"; } mtmp = (char *) MALLOC( (sname? strlen(sname) : 0) + (qs? strlen(qs) : 0) + (strlen(whichimg) + strlen(whatmode)) + 1024); sprintf(mtmp, "
\n" "\n", (qs ? "?0" : sname), whichimg, whatmode); output(mtmp); #ifdef USE_ADMSERV if(!servers) { sprintf(line, "\n", whatmode); output(line); } else if(servers[0] == '(') { sprintf(line, "\n"); } else { sprintf(line, "\n"); } #endif sprintf(mtmp, "\n", (qs? "?0" : sname), whichimg); output(mtmp); #ifdef USE_ADMSERV if(servers) { sprintf(line, "\n"); output(line); } #endif sprintf(line, "
" "" "%sCurrent servers:
\n"); output(line); output(""); tmp=strtok(++servers, "|)"); while(tmp) { char *tmp2; output(""); tmp2=strchr(tmp, '-'); tmp2++; output(tmp2); tmp=strtok(NULL, "|)"); if(tmp) output(","); output("\n"); } output("
Current server: "); output(line); output(""); tmp = strchr(servers, '-'); *tmp++ = '\0'; output(tmp); output(""); output("" "
" "" "Choose a new server or set of servers
\n"); output(line); output("
\n"); } void docroot(char **vars) { #ifndef MCC_NEWS char line[BIG_LINE]; pblock *pb = grab_pblock(PB_NAME, "default", "NameTrans", "document-root", NULL, NULL); char *docroot = ""; if(pb) docroot = pblock_findval("root", pb); sprintf(line, "%s%s\n", docroot, (vars[0] != NULL) ? vars[0] : ""); output(line); #endif } void serverroot(char **vars) { char line[BIG_LINE]; #ifdef USE_ADMSERV char *sroot = getenv("NETSITE_ROOT"); #else char *sroot = get_mag_var("#ServerRoot"); #endif sprintf(line, "%s%s", (sroot) ? sroot : "", (vars[0]) ? vars[0] : ""); output(line); } void makeurl(char **vars) { char line[BIG_LINE]; sprintf(line,"%s\n", get_serv_url(), vars[0] ? vars[0] : ""); output(line); } void curservname(void) { output(get_srvname(0)); } NSAPI_PUBLIC void pageheader(char **vars, char **config) { char line[BIG_LINE]; #if 0 /* MLM - put in to have non-working Back button */ char *ref=get_referer(config); char *t; #endif output("
\n"); util_snprintf(line, BIG_LINE, ""); output(line); util_snprintf(line, BIG_LINE, "", vars[2]); output(line); output("
"); output(line); util_snprintf(line, BIG_LINE, "
"); output(line); #if 0 /* MLM - put in to have non-working Back button */ t=strrchr(ref, '/'); *t++='\0'; util_snprintf(line, BIG_LINE, "", ref, t); output(line); util_snprintf(line, BIG_LINE, "\n"); output(line); #endif util_snprintf(line, BIG_LINE, "%s" "
" "
\n"); } char *_get_help_button(char *topic) { char line[BIG_LINE]; util_snprintf( line, BIG_LINE, "", XP_GetAdminStr(DBT_help_), topic ? helpJavaScriptForTopic( topic ) : helpJavaScript() ); return(STRDUP(line)); } NSAPI_PUBLIC char *helpJavaScriptForTopic( char *topic ) { char *tmp; char line[BIG_LINE]; char *server=get_srvname(0); char *type; int typeLen; /* Get the server type, without the instance name into type */ tmp = strchr( server, '-' ); typeLen = tmp - server; type = (char *)MALLOC( typeLen + 1 ); type[typeLen] = '\0'; while ( typeLen-- ) { type[typeLen] = server[typeLen]; } util_snprintf( line, BIG_LINE, "if ( top.helpwin ) {" " top.helpwin.focus();" " top.helpwin.infotopic.location='%s/%s/admin/tutor?!%s';" "} else {" " window.open('%s/%s/admin/tutor?%s', '" INFO_IDX_NAME"_%s', " HELP_WIN_OPTIONS");}", getenv("SERVER_URL"), server, topic, getenv("SERVER_URL"), server, topic, type ); return(STRDUP(line)); } NSAPI_PUBLIC char *helpJavaScript() { char *tmp, *sn; tmp=STRDUP(getenv("SCRIPT_NAME")); if(strlen(tmp) > (unsigned)BIG_LINE) tmp[BIG_LINE-2]='\0'; sn=strrchr(tmp, '/'); if( sn ) *sn++='\0'; return helpJavaScriptForTopic( sn ); } void submit(int verify, char **vars) { char line[BIG_LINE]; char outline[BIG_LINE]; if(verify) { util_snprintf(line, BIG_LINE, "\n", vars[0]); output(line); } output("
"); if(!verify) { util_snprintf(outline, BIG_LINE, "%s%s%s%s%s", "\n"); } else { util_snprintf(outline, BIG_LINE, "%s%s%s%s%s%s", "\n"); } output(outline); util_snprintf(outline, BIG_LINE, "%s%s%s%s", "\n"); output(outline); util_snprintf(line, BIG_LINE, "\n", _get_help_button( vars[0] )); output(line); output("
", "", "", "", "", "%s
\n"); output("\n"); output("\n"); } void helpbutton(char *topic) { output("

" "
"); output(_get_help_button(topic)); output("
\n"); output("\n"); } void dialogsubmit(char *topic) { char line[BIG_LINE]; char outline[BIG_LINE]; output("
"); util_snprintf(outline, BIG_LINE, "%s%s%s%s%s", "\n"); output(outline); util_snprintf(outline, BIG_LINE, "%s%s%s%s%s", "\n"); output(outline); util_snprintf(line, BIG_LINE, "\n", _get_help_button(topic)); output(line); output("
", "", "", "%s
\n"); output("\n"); output("\n"); } void helpjsfn(void) { char *tmp; char line[BIG_LINE]; char *server=get_srvname(0); char *type; int typeLen; /* Get the server type, without the instance name into type */ tmp = strchr( server, '-' ); typeLen = tmp - server; type = (char *)MALLOC( typeLen + 1 ); type[typeLen] = '\0'; while ( typeLen-- ) { type[typeLen] = server[typeLen]; } output("function displayHelpTopic(topic)\n"); output("{\n"); util_snprintf(line, BIG_LINE, " if (top.helpwin) {\n" " top.helpwin.focus();\n" " top.helpwin.infotopic.location='%s/%s/admin/tutor?!' + topic;\n" " } else {\n" " window.open('%s/%s/admin/tutor?' + topic, '" INFO_IDX_NAME"_%s', " HELP_WIN_OPTIONS");\n" " }\n" "}\n", getenv("SERVER_URL"), server, getenv("SERVER_URL"), server, type ); output(line); } void link_referer(char **input, char **vars) { char line[BIG_LINE]; sprintf( line, "\n", ( vars[0] ? vars[0] : getenv( "SCRIPT_NAME" ) ), cookieValue( "adminReferer", NULL ) ); output( line ); } int get_directive(char *string) { int index = -1; register int x; for(x=0; x < MAXTEMPLATE; x++) { if(!strncmp(string, templates[x].name, strlen(templates[x].name))) { index = x; break; } } return index; } NSAPI_PUBLIC int directive_is(char *target, char *directive) { char *position = (target + strlen(DIRECTIVE_START)); return(!(strncmp(directive, position, strlen(directive)))); } char **get_vars(char *string) { char **vars; register int x; int isvar; char scratch[BIG_LINE]; char lastchar; /* Initialize the vars array. */ vars = (char **) MALLOC((MAXVARS)*(sizeof(char *))); for(x=0; x< MAXVARS; x++) vars[x] = NULL; isvar = -1; x = 0; scratch[0] = '\0'; lastchar = ' '; while(*string != '\0') { if((*string == '\"') && (lastchar != '\\')) if(isvar != -1) { vars[x++] = (char *)STRDUP(scratch); isvar = -1; if(x == MAXVARS) break; } else isvar = 0; else if(isvar != -1) { scratch[isvar++] = *string; scratch[isvar] = '\0'; } else if(*string == DIRECTIVE_END) break; lastchar = *string; string++; } return vars; } static void output(char *line) { if(status) fputs(line, stdout); }