Subversion Repositories wimsdev

Rev

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