Subversion Repositories wimsdev

Rev

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