Subversion Repositories wimsdev

Rev

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