Subversion Repositories wimsdev

Rev

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