Subversion Repositories wimsdev

Rev

Rev 3121 | Rev 3864 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
  2.  *
  3.  *  This program is free software; you can redistribute it and/or modify
  4.  *  it under the terms of the GNU General Public License as published by
  5.  *  the Free Software Foundation; either version 2 of the License, or
  6.  *  (at your option) any later version.
  7.  *
  8.  *  This program is distributed in the hope that it will be useful,
  9.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  *  GNU General Public License for more details.
  12.  *
  13.  *  You should have received a copy of the GNU General Public License
  14.  *  along with this program; if not, write to the Free Software
  15.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17.  
  18.       /* This file contains various configuration routines for wims. */
  19.  
  20.         /* Limitations are essentially for security reasons:
  21.          * to curb attacks by resource exhaustion of the server. */
  22.  
  23.         /* maximal length of any module's variable definition file. */
  24. int VAR_DEF_LENGTH_LIMIT=50000;
  25.         /* maximal nesting level of substitutions. */
  26. int SUBST_LIMIT=16;
  27.         /* We fix a limit for the number of goto's (or for jumps) executed,
  28.          * in order to avoid infinite loop or DOS attack. */
  29. int GOTO_LIMIT=500000;
  30.         /* Limit for instex, insplot, insPLOT. */
  31. int INS_LIMIT=500;
  32.         /* Limit for frames in an animation. */
  33. int ANIM_LIMIT=400;
  34.         /* module log file length */
  35. int MODULE_LOG_LIMIT=102400;
  36.         /* general log file length */
  37. int GEN_LOG_LIMIT=1024000;
  38.         /* number of old log files */
  39. int OLD_LOG_FILES=2;
  40.  
  41.         /* Resource limits */
  42. int threshold1=150;     /* First load shreshold */
  43. int threshold2=300;     /* Second load shreshold */
  44. int ispriority=0;       /* whether the connection is from priority sites */
  45.  
  46.         /* sessions not accessed more than these seconds will be erased. */
  47. int idle_time=5400;
  48.         /* non-class sessions not accessed more than these seconds will be erased. */
  49. int idle_time2=2400;
  50.         /* first-time sessions not accessed more than these seconds will be erased. */
  51. int idle_time3=900;
  52.  
  53. int rafalvl=10;         /* anti-rapidfire severity */
  54.  
  55.         /* Explanation: two real numbers a,b such that
  56.          * |a+b|>|a-b|*compare_precision
  57.          * will be considered equal in !ifvalue comparisons. */
  58. int DEFAULT_COMPARE_PRECISION=10000;
  59.         /* Precision of printing (hence of all evaluations). */
  60. int DEFAULT_PRINT_PRECISION=8;
  61.         /* Defaults for instex, insplot, etc. */
  62. char *DEFAULT_INS_FORMAT="gif";
  63. char *DEFAULT_ANIM_FORMAT="gif";
  64. char *DEFAULT_INS_DENSITY="100x100";
  65. char *DEFAULT_INSPLOT_FONT="small";
  66.  
  67.         /* gnuplot intermediate graphics format */
  68. char *gnuplot_format="gif";
  69.  
  70.   /* electronic address of site manager. */
  71. char *site_manager="wims@$httpd_HTTP_HOST";
  72.   /* Main configure file, this name cannot be configured! */
  73. char *config_file="../log/wims.conf";
  74. char PATH[512];  /* directory settings */
  75. #ifdef MACOSX
  76.  char *commonpath="/usr/local/bin:/usr/bin:/usr/games:/bin:/sw/bin:/usr/bin/X11:/usr/X11R6/bin";
  77. #else
  78.  char *commonpath="/usr/local/bin:/usr/bin:/usr/games:/bin:/usr/bin/X11:/usr/X11R6/bin:/opt/teTeX/bin";
  79. #endif
  80. char *gap_command="gap.sh -T";
  81. char *maxima_command="maxima";
  82. char *mupad_command="/usr/local/lib/mupad/share/bin/mupad";
  83. char *pari_command="gp -f -q -p 150000";
  84.  
  85.   /* default languages */
  86. char site_langbuf[]="en fr cn es it nl";
  87. char *site_languages=site_langbuf;
  88.   /* Statistics shows up? */
  89. char *show_stat="no";
  90.   /* Author names show up in search result? */
  91. char *show_author="no";
  92.   /* Show user IP adress to supervisor */
  93. char *show_ip="yes";
  94.   /* busy hours definition */
  95. char *busyhours="";
  96.   /* Presentation theme */
  97. char *theme="standard";
  98.   /* directory of session files */     
  99. char session_dir[MAX_FNAME+1], s2_dir[MAX_FNAME+1];
  100.   /* standardised header model for modules' output. Will be stored
  101.    * into the variable $wims_html_header. If the theme contains
  102.    * a file 'head.phtml', html_header will be defined by the address
  103.    * of this file  in subroutine define_html_header */
  104. char *html_header="html/header.phtml";
  105.   /* standardised about table for modules */
  106. char *module_about_file="html/module_about";
  107.   /* title definition page */
  108. char *title_page="title.phtml";
  109.   /* stardardised header menu bar, to be included in module pages
  110.    * via command !headmenu */
  111. char *header_menu="headmenu.phtml";
  112. char *header_menu_user="headmenu_user.phtml";
  113. char *header_menu_supervisor="headmenu_supervisor.phtml";
  114.   /* standardised referer to wims home, to be included in
  115.    * module pages via command !homeref */
  116. char *home_referer="homeref.phtml";
  117. char *home_referer_user="homeref_user.phtml";
  118. char *home_referer_supervisor="homeref_supervisor.phtml";
  119.   /* background color in standard reference tables */
  120. char *ref_bgcolor="#777777";
  121.   /* link color */
  122. char *link_color="#004077";
  123.   /* vlink color */
  124. char *vlink_color="#004077";
  125.   /* link color in standard reference tables */
  126. char *ref_menucolor="white";
  127.   /* background help button color */
  128. char *ref_button_help_bgcolor="green";
  129.   /* background help button color */
  130. char *ref_button_help_color="white";
  131.   /* button background color */
  132. char *ref_button_bgcolor="#666666";
  133.   /* button color */
  134. char *ref_button_color="white";
  135.   /* module log disabled by default */
  136. int modlog=0;
  137.   /* name of the variable definition file for every module.
  138.    * It is this file which defines the presence of the module.
  139.    * Better do not make it configurable. */
  140. char var_def_file[256];
  141.   /* name of variable init file. Processed only when cmd=new or renew. */
  142. char *var_init_file="var.init";
  143.   /* name of variable calculation file. Parsed at each call. */
  144. char *main_var_proc_file="var.proc";
  145.   /* name of the main (programmable) html file. */
  146. char *html_file="main.phtml";
  147.   /* Introduction page name */
  148. char *intro_file="intro.phtml";
  149.   /* Reference page */
  150. char *ref_file="wims_ref.phtml";
  151.   /* script to process different inserts.
  152.    * The two consecutive points in the name prohibits ordinary
  153.    * user execution of the script. */
  154. char *insdraw_processor="insdraw..processor";
  155. char *insplot_processor="insplot..processor";
  156. char *tex2gif="bin/tex..gif";
  157. char *priority="";              /* IPs of priority sites */
  158. char *texgif_fontdir="w/texgif";
  159. char *texgif_texheader="mathfonts/header";
  160. char *bgcolor="#E8E8E8";        /* page background color */
  161. char *bgimg="";                 /* page background image file */
  162. char *pagecss="-theme-";                /* style sheet file */
  163. char *theme_icon="default";             /* icon */
  164. char *usecookie="no";           /* whether to send cookies to anon requests */
  165. char *mail_opt="";              /* !mailto options */
  166. char nodeip[256]="";            /* ip of cluster node if relevant */
  167. char temp_log[MAX_FNAME+1];     /* temp log file name */
  168.         /* 0: low; 1: high; 2: MathML */
  169. int mathalign_base=0;           /* Not use <sup> for middle alignment */
  170. int backup_hour=-1;             /* Hour for daily backup; -1 means no backup. */
  171. int site_accounting=0;          /* whether accounting should be activated */
  172. int examlog_limit=7;            /* number of logged exam sessions for each student */
  173.  
  174.   /* Automatic module update */
  175. char *mu_auto="none";
  176. char *mu_cat="all";
  177. char *mu_delay="20";
  178. char *mu_site="http://wims.unice.fr/download/modules";
  179. char *mu_zone="all";
  180. char *mu_download_site="unice,wimsedu";
  181. char *mu_publish_site="unice";
  182.  
  183.   /* keywords destinated to robots */
  184. char *site_keywords="interactive mathematics, interactive math, server side interactivity";
  185. char *site_description="interactive exercises, online calculators and plotters, mathematical recreation and games";
  186.  
  187.   /* class and document authorization */
  188. int class_quota=100;
  189. int superclass_quota=100;
  190. int doc_quota=32;
  191. char *class_regpass="email,email,deny";
  192. char *doc_regpass="";
  193.  
  194.   /* Where to find programs; if your httpd sets /bin:/usr/bin in PATH,
  195.    * this should be OK. */
  196. char *sed_prog="sed";
  197. char *tr_prog="tr";
  198. char *awk_prog="awk";
  199.  
  200.   /* myname */
  201. char *cgi_name="wims.cgi";
  202. char *aliased_cgi="no";
  203. char *aliased_getfile="yes";
  204.  
  205.   /* Limits for classes and participants */
  206. int class_limit=200; /* limit of number of classes */
  207. int user_limit=5000; /* limit of total number of registered users */
  208. int class_user_limit=300; /* limit of users in one class */
  209. int forum_limit=100;  /* Limit of message boards */
  210.  
  211. typedef struct {
  212.     char *name;
  213.     int is_integer;
  214.     void  *address;
  215. } CONFIG_DATA;
  216.  
  217. CONFIG_DATA main_config[]={
  218.       {"PATH",                  0, &commonpath},
  219.       {"aliased_cgi",           0, &aliased_cgi},
  220.       {"aliased_getfile",       0, &aliased_getfile},
  221.       {"backup_hour",           1, &backup_hour},
  222.       {"busyhours",             0, &busyhours},
  223.       {"cgi_name",              0, &cgi_name},
  224.       {"class_limit",           1, &class_limit},
  225.       {"class_quota",           1, &class_quota},
  226.       {"class_regpass",         0, &class_regpass},
  227.       {"class_user_limit",      1, &class_user_limit},
  228.       {"css",                   0, &pagecss},
  229.       {"default_anim_format",   0, &DEFAULT_ANIM_FORMAT},
  230.       {"default_ins_format",    0, &DEFAULT_INS_FORMAT},
  231.       {"default_insplot_font",  0, &DEFAULT_INSPLOT_FONT},
  232.       {"default_texposition",   1, &mathalign_base},
  233.       {"devel_modules",         0, &devel_modules},
  234.       {"doc_quota",             1, &doc_quota},
  235.       {"doc_regpass",           0, &doc_regpass},
  236.       {"examlog_limit",         1, &examlog_limit},
  237.       {"forum_limit",           1, &forum_limit},
  238.       {"gap_command",           2, &gap_command},
  239.       {"general_log_limit",     1, &GEN_LOG_LIMIT},
  240.       {"gnuplot_format",        0, &gnuplot_format},
  241.       {"home_module",           0, &home_module},
  242.       {"idle_time",             1, &idle_time},
  243.       {"idle_time2",            1, &idle_time2},
  244.       {"idle_time3",            1, &idle_time3},
  245.       {"insdraw_processor",     0, &insdraw_processor},
  246.       {"link_color",            0, &link_color},
  247.       {"mail_opt",              0, &mail_opt},
  248.       {"manager_https",         1, &manager_https},
  249.       {"manager_site",          0, &manager_site},
  250.       {"maxima_command",        2, &maxima_command},
  251.       {"module_log",            1, &modlog},
  252.       {"module_log_limit",      1, &MODULE_LOG_LIMIT},
  253.       {"mu_auto",               0, &mu_auto},
  254.       {"mu_cat",                0, &mu_cat},
  255.       {"mu_delay",              0, &mu_delay},
  256.       {"mu_download_site",              0, &mu_download_site},
  257.       {"mu_publish_site",               0, &mu_publish_site},
  258.       {"mu_site",               0, &mu_site},
  259.       {"mu_zone",               0, &mu_zone},
  260.       {"mupad_command",         2, &mupad_command},
  261.       {"old_log_files",         1, &OLD_LOG_FILES},
  262.       {"page_bgcolor",          0, &bgcolor},
  263.       {"page_bgimg",            0, &bgimg},
  264.       {"pari_command",          2, &pari_command},
  265.       {"priority",              0, &priority},
  266.       {"rafale_level",          1, &rafalvl},
  267.       {"ref_bgcolor",           0, &ref_bgcolor},
  268.       {"ref_button_bgcolor",            0, &ref_button_bgcolor},
  269.       {"ref_button_color",              0, &ref_button_color},
  270.       {"ref_button_help_bgcolor",               0, &ref_button_help_bgcolor},
  271.       {"ref_button_help_color",         0, &ref_button_help_color},
  272.       {"ref_menucolor",         0, &ref_menucolor},
  273.       {"rlimit_cpu",            1, &rlimit_cpu},
  274.       {"show_author",           0, &show_author},
  275.       {"show_ip",               0, &show_ip},      
  276.       {"show_stat",             0, &show_stat},
  277.       {"site_accounting",       1, &site_accounting},
  278.       {"site_description",      0, &site_description},
  279.       {"site_keywords",         0, &site_keywords},
  280.       {"site_languages",        0, &site_languages},
  281.       {"site_manager",          0, &site_manager},
  282.       {"superclass_quota",      1, &superclass_quota},
  283.       {"texbasesize",           1, &texbasesize},
  284.       {"theme",                 0, &theme},
  285.       {"theme_icon",                    0, &theme_icon},
  286.       {"threshold1",            1, &threshold1},
  287.       {"threshold2",            1, &threshold2},
  288.       {"tmp_debug",             0, &tmp_debug},
  289.       {"trusted_modules",       0, &trusted_modules},
  290.       {"usecookie",             0, &usecookie},
  291.       {"user_limit",            1, &user_limit},
  292.       {"var_def_length_limit",  1, &VAR_DEF_LENGTH_LIMIT},
  293.       {"vlink_color",           0, &vlink_color},
  294.       {"workfile_limit",        1, &WORKFILE_LIMIT}
  295. };
  296. #define MAIN_CONFIG_NO (sizeof(main_config)/sizeof(main_config[0]))
  297.  
  298.         /* processes the list of languages supported on the site */
  299. void language_list(void)
  300. {
  301.     int i,k;
  302.     char *lbuf[MAX_LANGUAGES];
  303.     calc_tolower(site_languages); items2words(site_languages);
  304.     setvar("wims_site_languages",site_languages);
  305.     i=cutwords(site_languages,lbuf,MAX_LANGUAGES);
  306.     available_lang_no=i;
  307.     for(i=k=0;i<available_lang_no;i++) {
  308.         if(strlen(lbuf[i])==2) memmove(available_lang[k++],lbuf[i],3);
  309.     }
  310.     if(k>0) memmove(lang,available_lang[0],3);
  311. }
  312.  
  313. char hostname[256];
  314. int html_call=0;
  315.  
  316.         /* determine the http reference name of the server */
  317. void determine_ref_name(void)
  318. {
  319.     char *s1, *s2, *p, *pl;
  320.     char buf[MAX_LINELEN+1];
  321.  
  322.     hostname[0]=0;
  323.     s1=getvar("httpd_HTTP_HOST");s2=getvar("httpd_SCRIPT_NAME");
  324.     gethostname(hostname,sizeof(hostname));
  325.     if(hostname[0]==0 && s1 && *s1!=0) mystrncpy(hostname,s1,sizeof(hostname));
  326.     if(s1==NULL || *s1==0) s1=hostname;
  327.     setvar("wims_myhostname",hostname);
  328.     ref_base[0]=0;
  329.     if(s2!=NULL) mystrncpy(buf,s2,sizeof(buf));
  330.     else buf[0]=0;
  331.     if(s1!=NULL && *buf=='/') {
  332.         p=strrchr(buf,'/'); if(p==NULL) p=buf; else *p++=0;
  333.         if(strcmp(p,cgi_name)!=0 && strlen(p)>4 &&
  334.            p[2]=='_' && islower(p[0]) && islower(p[1])) {
  335.             pre_language[0]=p[0];pre_language[1]=p[1];pre_language[2]=0;
  336.             p[2]=0; setenv("HTTP_ACCEPT_LANGUAGE",p,1); pl=p; p+=3;
  337.             s2=strrchr(p,'.'); if(s2!=NULL) {
  338.                 *s2=0; if(strcasecmp(s2+1,"cgi")!=0) html_call=1;
  339.             }
  340.             s2=p; while((s2=strpbrk(s2,"@~"))!=NULL) *s2='/';
  341.             if(strncmp(p,"doc/",4)==0) {
  342.                 char *pd, *pb;
  343.                 pd=p+4;
  344.                 if(*pd) {
  345.                     pb=strchr(pd,'/');
  346.                     if(pb) *pb++=0; else pb="main";
  347.                     setvar("doc",pd); setvar("block",pb); setvar("job","read");
  348.                 }
  349.                 setvar(ro_name[ro_module],mkfname(NULL,"adm/doc.%s",pl));
  350.             }
  351.             else setvar(ro_name[ro_module],p);
  352.             module_defined=1;
  353.         }
  354.         p=strchr(buf,'_');
  355.         if(p!=NULL && p>buf+3 && myislower(*(p-1)) && myislower(*(p-2))
  356.            && *(p-3)=='/' && myisalpha(*(p+1))) {
  357.             char *pp;
  358.             for(pp=p+1; myisalnum(*pp); pp++); if(*pp=='/') *(p-3)=0;
  359.         }
  360.         snprintf(ref_name,sizeof(ref_name)-2,"http://%s%s/%s",s1,buf,cgi_name);
  361.         mystrncpy(ref_base,ref_name,sizeof(ref_base)-2);
  362.         p=strrchr(ref_base,'/');
  363.         if(p!=NULL) *(p+1)=0;
  364.     }
  365.     else mystrncpy(ref_name,cgi_name,sizeof(ref_name)-1);
  366.     setvar("wims_ref_name",ref_name);
  367. }
  368.  
  369.         /* Read and interprete wims configuration file. */
  370. void main_configure(void)
  371. {
  372.     int i;
  373.     char *conf_buf,*e,*p,*p2,*p3;
  374.     char buf[MAX_LINELEN+1];
  375.    
  376.     conf_buf=readfile(config_file,NULL,WORKFILE_LIMIT);
  377.     if(conf_buf==NULL) goto fileend;
  378.     e=conf_buf-1; while(e) {
  379.         p=e+1; e=strchr(p,'\n'); if(e) *e=0;
  380.         p=find_word_start(p);
  381.         if(*p==0 || *p==comment_prefix_char) continue; /* empty or comment line */
  382.         p2=strchr(p,'=');
  383.         if(p2==NULL) continue; /* syntax error */
  384.         *p2=0; *find_word_end(p)=0; p2=find_word_start(p2+1);
  385.         p3=p2+strlen(p2); while(myislspace(p3[-1])) p3--;
  386.         while(p3[-1]=='\\') p3--; *p3=0;
  387.         i=search_list(main_config,MAIN_CONFIG_NO,sizeof(main_config[0]),p);
  388.         if(i<0) continue; /* name non-defined */
  389.         if(main_config[i].is_integer==1) {
  390.             int *ip;
  391.             ip=main_config[i].address;
  392.             *ip=atoi(p2); if(*ip<0) *ip=0;
  393.         }
  394.         else {
  395.             char **cp;
  396.             cp=main_config[i].address;
  397.             *cp=p2;
  398.         }
  399.     }
  400.     fileend:
  401.     setenv("session_base_dir",session_dir,1);
  402.     determine_ref_name();
  403.     language_list();
  404.     for(i=0;i<MAIN_CONFIG_NO;i++) {
  405.         char **pp;
  406.         if(main_config[i].is_integer==2) {
  407.             pp=main_config[i].address;
  408.             setenv(main_config[i].name,*pp,1);
  409.         }
  410.     }
  411.         /* check priority */
  412.     if(priority[0] && checkhost(priority)>0) ispriority=1;
  413.     getcwd(cwdbuf,sizeof(cwdbuf)); setvar("httpd_PWD",cwdbuf);
  414.     setenv("wims_server_base",cwdbuf,1);
  415.     p=strrchr(cwdbuf,'/');
  416.     if(p!=NULL && strcmp(p,"/public_html")==0) {
  417.         *p=0; snprintf(PATH,sizeof(PATH),"%s/other/bin:%s",cwdbuf,commonpath);
  418.         force_setvar("wims_home",cwdbuf);
  419.         *p='/';
  420.     }
  421.     else {
  422.         snprintf(PATH,sizeof(PATH),"%s/../other/bin:%s",cwdbuf,commonpath);
  423.         force_setvar("wims_home",cwdbuf);
  424.     }
  425.     setenv("PATH",PATH,1);
  426.     p=getvar("httpd_SERVER_SOFTWARE");
  427.     if(p && (p2=strstr(p,"Apache"))!=NULL && strcmp(p2,"Apache/1.3")>=0
  428.        && strstr(aliased_getfile,"yes")!=NULL)
  429.       good_httpd=1;
  430.     accessfile(buf,"r","../tmp/log/myip");
  431.     *find_word_end(buf)=0;
  432.     if(buf[0]) {
  433.         snprintf(nodeip,sizeof(nodeip),"%s",buf);
  434.         snprintf(temp_log,sizeof(temp_log),"%s.%s",TEMP_LOG_FILE,nodeip);
  435.         force_setvar("wims_priv_nodeip",nodeip);
  436.     }
  437.     else {
  438.         nodeip[0]=0;
  439.         snprintf(temp_log,sizeof(temp_log),"%s",TEMP_LOG_FILE);
  440.     }
  441. }
  442.  
  443. struct {
  444.     int rname;
  445.     int *rval;
  446. } resource_table[]={
  447. #ifdef RLIMIT_CPU
  448.       {RLIMIT_CPU,      &rlimit_cpu},
  449. #endif
  450. #ifdef RLIMIT_FSIZE
  451.       {RLIMIT_FSIZE,    &rlimit_fsize},
  452. #endif
  453. #ifdef RLIMIT_AS
  454.       {RLIMIT_AS,       &rlimit_as},
  455. #endif
  456. #ifdef RLIMIT_DATA
  457.       {RLIMIT_DATA,     &rlimit_data},
  458. #endif
  459. #ifdef RLIMIT_STACK
  460.       {RLIMIT_STACK,    &rlimit_stack},
  461. #endif
  462. #ifdef RLIMIT_CORE
  463.       {RLIMIT_CORE,     &rlimit_core},
  464. #endif
  465. #ifdef RLIMIT_RSS
  466.       {RLIMIT_RSS,      &rlimit_rss},
  467. #endif
  468. #ifdef RLIMIT_NPROC
  469.       {RLIMIT_NPROC,    &rlimit_nproc},
  470. #endif
  471. #ifdef RLIMIT_NOFILE
  472.       {RLIMIT_NOFILE,   &rlimit_nofile},
  473. #endif
  474. #ifdef RLIMIT_MEMLOCK
  475.       {RLIMIT_MEMLOCK,  &rlimit_memlock}
  476. #endif
  477. };
  478. #define RESOURCE_NO (sizeof(resource_table)/sizeof(resource_table[0]))
  479.  
  480.         /* set system resource limits */
  481. void set_rlimits(void)
  482. {
  483.     int i;
  484.     struct rlimit rlim;
  485.    
  486.     for(i=0;i<RESOURCE_NO;i++) {
  487.         rlim.rlim_cur=rlim.rlim_max=*(resource_table[i].rval);
  488.         setrlimit(resource_table[i].rname,&rlim);
  489.         if(resource_table[i].rname==RLIMIT_CPU) {
  490.             setvar("wims_cpu_limit",int2str(rlim.rlim_max));
  491.             initalarm();
  492.         }
  493.     }
  494. }
  495.  
  496. struct {
  497.     char *name;
  498.     int is_integer;
  499.     void *default_value;
  500. } module_default[]={
  501.       {"anim_format",           0, &DEFAULT_ANIM_FORMAT},
  502.       {"gnuplot_format",        0, &gnuplot_format},
  503.       {"ins_anim_limit",        1, &ANIM_LIMIT},
  504.       {"ins_density",           0, &DEFAULT_INS_DENSITY},
  505.       {"ins_format",            0, &DEFAULT_INS_FORMAT},
  506.       {"insplot_font",          0, &DEFAULT_INSPLOT_FONT},
  507.       {"wims_busyhours",        0, &busyhours},
  508.       {"wims_class_limit",      1, &class_limit},
  509.       {"wims_class_quota",      1, &class_quota},
  510.       {"wims_class_regpass",    0, &class_regpass},
  511.       {"wims_class_user_limit", 1, &class_user_limit},
  512.       {"wims_compare_precision",1, &DEFAULT_COMPARE_PRECISION},
  513.       {"wims_devel_modules",    0, &devel_modules},
  514.       {"wims_doc_quota",        1, &doc_quota},
  515.       {"wims_doc_regpass",      0, &doc_regpass},
  516.       {"wims_examlog_limit",    1, &examlog_limit},
  517.       {"wims_forum_limit",      1, &forum_limit},
  518.       {"wims_bgcolor",          0, &bgcolor},
  519.       {"wims_bgimg",            0, &bgimg},
  520.       {"wims_css",              0, &pagecss},
  521.       {"wims_link_color",       0, &link_color},
  522.       {"wims_print_precision",  1, &DEFAULT_PRINT_PRECISION},
  523.       {"wims_ref_bgcolor",      0, &ref_bgcolor},
  524.       {"wims_ref_button_bgcolor",               0, &ref_button_bgcolor},
  525.       {"wims_ref_button_color",         0, &ref_button_color},
  526.       {"wims_ref_button_help_bgcolor",          0, &ref_button_help_bgcolor},
  527.       {"wims_ref_button_help_color",            0, &ref_button_help_color},
  528.       {"wims_ref_menucolor",            0, &ref_menucolor},
  529.       {"wims_ref_menucolor",    0, &ref_menucolor},
  530.       {"wims_show_author",      0, &show_author},
  531.       {"wims_show_ip",          0, &show_ip},
  532.       {"wims_show_stat",        0, &show_stat},
  533.       {"wims_site_description", 0, &site_description},
  534.       {"wims_site_keywords",    0, &site_keywords},
  535.       {"wims_site_manager",     0, &site_manager},
  536.       {"wims_texalign",         1, &mathalign_base},
  537.       {"wims_texbasesize",      1, &texbasesize},
  538.       {"wims_theme",            0, &theme},
  539.       {"wims_theme_icon",       0, &theme_icon},
  540.       {"wims_tmp_debug",        0, &tmp_debug},
  541.       {"wims_usecookie",        0, &usecookie},
  542.       {"wims_user_limit",       1, &user_limit},
  543.       {"wims_vlink_color",      0, &vlink_color},
  544.       {"wims_superclass_quota", 1, &superclass_quota}
  545. };
  546. #define MODULE_DEFAULT_NO (sizeof(module_default)/sizeof(module_default[0]))
  547.  
  548.         /* Set defaults for module, and set corresponding variables. */
  549. void module_configure(void)
  550. {
  551.     int i;
  552.     confset=1;
  553.     for(i=0;i<MODULE_DEFAULT_NO;i++) {
  554.         if(module_default[i].is_integer) {
  555.             int j, *jp;
  556.             jp=module_default[i].default_value;
  557.             j=*jp; if(j<0) j=0;
  558.             setvar(module_default[i].name,int2str(j));
  559.         }
  560.         else {
  561.             char **cp;
  562.             cp=module_default[i].default_value;
  563.             setvar(module_default[i].name,*cp);
  564.         }
  565.     }
  566.     setenv("texgif_fontdir",texgif_fontdir,1);
  567.     setenv("texgif_texheader",texgif_texheader,1);
  568.     setvar("wims_texsize","0");
  569.     confset=0;
  570. }
  571.  
  572. char *modindex[]={
  573.       "address",
  574.       "author",
  575.       "category",
  576.       "copyright",
  577.       "data",
  578.       "description",
  579.       "description_ca",
  580.       "description_cn",
  581.       "description_de",
  582.       "description_en",
  583.       "description_es",
  584.       "description_fr",
  585.       "description_it",
  586.       "description_nl",
  587.       "description_si",
  588.       "description_tw",
  589.       "domain",
  590.       "help",
  591.       "keywords",
  592.       "language",
  593.       "level",
  594.       "maintainer",
  595.       "maintainer_address",
  596.       "mode",
  597.       "require",
  598.       "scoring",
  599.       "title",
  600.       "title_ca",
  601.       "title_cn",
  602.       "title_de",
  603.       "title_en",
  604.       "title_es",
  605.       "title_fr",
  606.       "title_it",
  607.       "title_nl",
  608.       "title_si",
  609.       "title_tw",
  610.       "translator",
  611.       "translator_address",
  612.       "translator_address_ca",
  613.       "translator_address_cn",
  614.       "translator_address_de",
  615.       "translator_address_en",
  616.       "translator_address_es",
  617.       "translator_address_fr",
  618.       "translator_address_it",
  619.       "translator_address_nl",
  620.       "translator_address_si",
  621.       "translator_address_tw",
  622.       "translator_ca",
  623.       "translator_cn",
  624.       "translator_de",
  625.       "translator_en",
  626.       "translator_es",
  627.       "translator_fr",
  628.       "translator_it",
  629.       "translator_nl",
  630.       "translator_si",
  631.       "translator_tw",
  632.       "vardef",
  633.       "version",
  634.       "wims_version",
  635. };
  636. #define MODINDEX_NO (sizeof(modindex)/sizeof(modindex[0]))
  637. char *module_special_file[]={
  638.     "intro","help"
  639. };
  640. #define MODSPEC_NO (sizeof(module_special_file)/sizeof(module_special_file[0]))
  641.  
  642. #ifdef WEBMATH
  643.  
  644.         /* read and treat module's INDEX file */
  645. void module_index(void)
  646. {
  647.     char buf[MAX_NAMELEN+1], ind_buf[MAX_LINELEN+1];
  648.     char *e, *p, *p2, *ft;
  649.     double v1, v2;
  650.     int i,l;
  651.     long indf_len;
  652.    
  653.     memmove(var_def_file,default_var_def,strlen(default_var_def)+1);
  654.     ft=readfile(mkfname(NULL,"%s/INDEX",module_prefix),
  655.                 ind_buf,sizeof(ind_buf));
  656.     if(ft==NULL) ft=readfile(mkfname(NULL,"%s/index",module_prefix),
  657.                              ind_buf,sizeof(ind_buf));
  658.     if(ft==NULL) return;
  659.     e=ind_buf-1; indf_len=strlen(ind_buf);
  660.     for(l=0; e<ind_buf+indf_len && e!=NULL ;l++) {
  661.         p=e+1; e=strchr(p,'\n'); if(e!=NULL) *e=0;
  662.         strip_trailing_spaces(p); p=find_word_start(p);
  663.         if(*p==0 || *p==comment_prefix_char) continue; /* empty or comment line */
  664.         p2=strchr(p,'=');
  665.         if(p2==NULL) continue; /* syntax error */
  666.         *p2=0; *find_word_end(p)=0; p2=find_word_start(p2+1);
  667.         i=search_list(modindex,MODINDEX_NO,sizeof(modindex[0]),p);
  668.         if(i<0) continue; /* name not in list */
  669.         snprintf(buf,sizeof(buf),"module_%s",p);
  670.         setvar(buf,p2);
  671.     }
  672.     if(mode!=mode_default) {
  673.         char *s;
  674.         s=getvar("module_mode");
  675.         if(s==NULL) {
  676.             nomode: mode=mode_default; force_setvar("wims_mode","");
  677.         }
  678.         else switch(mode) {
  679.             case mode_popup: {
  680.                 if(strstr(s,"popup")==NULL) goto nomode;
  681.                 else break;
  682.             }
  683.             case mode_raw: {
  684.                 if(strstr(s,"raw")==NULL) goto nomode;
  685.                 else break;
  686.             }
  687.             default: break;
  688.         }
  689.     }
  690.         /* test for existence of some special files in module's directory */
  691.     for(i=0;i<MODSPEC_NO;i++) {
  692.         if(ftest(mkfname(NULL,"%s/%s.phtml",module_prefix,module_special_file[i]))
  693.            ==is_file) {
  694.             snprintf(buf,sizeof(buf),"module_has_%s",module_special_file[i]);
  695.             setvar(buf,"yes");
  696.         }
  697.     }
  698.     p=getvar("module_language"); if(p==NULL || *p==0) setvar("module_language","en");
  699.     setvar("module_has_about","yes"); /* now we have default about.phtml */
  700.         /* Set var_def */
  701.     p=getvar("module_vardef");
  702.     if(p!=NULL && *p!=0) mystrncpy(var_def_file,p,sizeof(var_def_file));
  703.     p=getvar("module_wims_version"); if(p!=NULL && *p!=0) {
  704.         v1=atof(p); v2=atof(wims_version);
  705.         if(finite(v1) && finite(v2) &&
  706.            (v1>v2 || (v1==v2 && strcmp(find_word_start(p),wims_version)>0))) {
  707.             setvar("module_wims_version",p);
  708.             setvar("wims_version",wims_version);
  709.             module_error("antidated_version");
  710.         }
  711.     }
  712.     if(isdevelmodule) {
  713.         p=getvar("module_scoring");
  714.         if(p==NULL || strcasecmp(p,"yes")!=0) isdevelmodule=0;
  715.     }
  716. }
  717.  
  718. #endif
  719.  
  720.         /* Set up a unique job identity as a 4-bytes integer */
  721. unsigned long create_job_ident(void)
  722. {
  723. /* unsigned long it; // 64-bit compatibility: take away _long_ */
  724.     unsigned long it;
  725.     int i;
  726.         /* Is this enough to guarantee uniqueness? */
  727.    
  728.     it=(nowtime<<16)+(getpid()&0xffff);
  729.         /* The encryption is very simple. */
  730.     it=it^0x5a3c9671;
  731.     for(i=0;i<MAX_SESRANDOM;i++) sesrandomtab[i]=random();
  732.     /* ensure that the result will be a 4-bytes integer gives random problem on 32 bits
  733.     return it;*/
  734.     return it & 0xffffffff;
  735. }
  736.  
  737.         /* Setup a job identifier */
  738. void set_job_ident(void)
  739. {
  740.     unsigned long l,r;
  741.    
  742.     l=create_job_ident();
  743.     /* the last 4 bits always make 0xA. A bug of glibc random()? */
  744.     r=random()>>4;
  745.     snprintf(job_identifier,sizeof(job_identifier),"%lX%08lX",r,l);
  746.     var_noexport=1;
  747.     setvar("job_identifier",job_identifier);
  748.     var_noexport=0;
  749. }
  750.  
  751.         /* define the variable $wims_html_header */
  752. void define_html_header(void)
  753. {
  754.   char *expir, *sp, *cp, *ladirection, *mp;
  755.     char buf[MAX_LINELEN+1],ebuf[128];
  756.     char *nocache="<meta http-equiv=\"Pragma\" content=\"no-cache\">\n\
  757. <meta http-equiv=\"Cache-Control\" content=\"no-cache\">\n";
  758.     int noc;
  759.     time_t t;
  760.  
  761.     noc=0;
  762.     cp=getvar("wims_expire"); if(cp!=NULL) goto css;
  763.     if(!robot_access && cmd_type==cmd_intro && isclassmodule) {
  764.         sp=getvar("special_parm"); if(sp==NULL) sp="";
  765.         if(strcmp(sp,".nocache.")==0) {
  766.             force_setvar("special_parm",""); noc=1;
  767.         }
  768.         if(!noc) {
  769.             mp=getvar(ro_name[ro_module]);
  770.             if(mp!=NULL && strncmp(mp,"devel/",strlen("devel/"))==0) noc=1;
  771.         }
  772.     }
  773.     if(mode==mode_default) {
  774.         if(!robot_access && (cmd_type!=cmd_intro || noc)) {
  775.             if(html_call) {
  776.                 /* expiration in 1 day for html call. */
  777.                 t=nowtime+(long) 24*3600; expir=ctime(&t);
  778.                 nocache="";
  779.             }
  780.             else expir="1 Jan 1990";
  781.         }
  782.         else {
  783.             /* expiration in 10 days for robot access or intro page. */
  784.             t=nowtime+(long) 10*24*3600; expir=ctime(&t); nocache="";
  785.         }
  786.         snprintf(ebuf,sizeof(ebuf),"%s",expir); strip_trailing_spaces(ebuf);
  787.         snprintf(buf,sizeof(buf),
  788.                  "<meta http-equiv=\"expires\" content=\"%s\">\n%s",ebuf,nocache);
  789.         setvar("wims_expire",buf);
  790.     }
  791.     css: setvar("wims_CSS","");
  792.     cp=getvar("wims_css");
  793.     ladirection=getvar("wims_main_dirn");
  794.     if (strcmp(ladirection,"ltr")==0) ladirection="";
  795.     if(!robot_access && cp!=NULL && *cp!=0 && strstr(cp,"---")==NULL) {
  796.         char *nbuf;
  797.         cp=find_word_start(cp);
  798.         if(strchr(cp,'/')==NULL) {
  799.             char *pc, *th, *ti; int st=0;
  800.             pc=getvar("wims_class");
  801.             if(pc!=NULL && *pc!=0 && strcmp(cp,"class")==0) {
  802.                  nbuf=mkfname(NULL,"%s/%s/css",class_base,pc);
  803.                  th=getvar("class_theme");ti=getvar("class_theme_icon") ;
  804.                  if(th==NULL || *th==0) th=getvar("wims_theme"); st=1;
  805.                  if(ti==NULL || *ti==0) ti=getvar("wims_theme_icon");
  806.             }
  807.             else {/* Il faut peut-être changer là aussi pour direction */
  808.                 nbuf=mkfname(NULL,"html/css/%s/%s.css",lang,cp);
  809.                 th=getvar("wims_theme");
  810.                 ti=getvar("wims_theme_icon");
  811.             }
  812.             if(strcmp(cp,"-theme-")==0 && strchr(th,'.')==NULL) {
  813.                   if(th==NULL || *th==0) th="default";
  814.                   nbuf=mkfname(NULL,"html/themes/%s/css%s.css",th,ladirection);
  815.             }
  816.             if(readfile(nbuf,tmplbuf,sizeof(tmplbuf))) {
  817.             }
  818.             else {
  819.              th=getvar("wims_theme");
  820.              nbuf=mkfname(NULL,"html/themes/%s/css%s.css",th,ladirection);
  821.                 }
  822.                 if(readfile(nbuf,tmplbuf,sizeof(tmplbuf))) {
  823.               if (st) {snprintf(buf,sizeof(buf),"<style type=\"text/css\"><!--\n\
  824. %s\n\
  825. --></style>",tmplbuf); }
  826.                   else {
  827.                   snprintf(buf,sizeof(buf),"<link href=\"%s\" rel=\"stylesheet\" type=\"text/css\">\n\
  828.                   <link href=\"gifs/themes/%s/icon.css\" rel=\"stylesheet\" type=\"text/css\">",nbuf,ti);
  829.                  }
  830.                 setvar("wims_CSS",buf);
  831.             }
  832.             else {
  833.            
  834.             }
  835.             if (th) {
  836.                nbuf=mkfname(NULL,"html/themes/%s/htmlheader.phtml",th) ;
  837.                if (readfile(nbuf,tmplbuf,sizeof(tmplbuf))) html_header=nbuf ;
  838.             }
  839.           }
  840.     }
  841.     if(readfile(html_header,buf,sizeof(buf))) setvar("wims_html_header",buf);
  842. }
  843.  
  844. void setsesdir(char *d)
  845. {
  846.     char buf[MAX_FNAME+1];
  847.     if(strstr(d,parent_dir_string)!=NULL) {
  848.         force_setvar("wims_session",robot_session);
  849.         mystrncpy(buf,robot_session,sizeof(buf));
  850.         mkfname(session_dir,"../tmp");
  851.         mkfname(s2_dir,"../tmp");
  852.     }
  853.     else {
  854.         force_setvar("wims_session",d);
  855.         snprintf(buf,sizeof(buf),"%s/%s",SESSION_BASE,d);
  856.         mkfname(session_dir,"../%s",SESSION_BASE);
  857.         /* mkfname(s2_dir,"../%s",S2_BASE); */
  858.     }
  859.     force_setvar("wims_sesdir",buf);
  860. }
  861.  
  862.