cgi_func.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #ifdef WIN32
  5. #include "ds.h"
  6. #else
  7. #include <ds.h>
  8. #endif
  9. #include "chtbl.h"
  10. #include "hashpjw.h"
  11. #include "cgi_theme_gest.h"
  12. #define FALSE 0
  13. #define TRUE 1
  14. //#define DEBUG
  15. extern char cgi_BaseName[256];
  16. int cgi_started = FALSE;
  17. int cgi_paramparsed = FALSE;
  18. typedef struct cgiparam_
  19. {
  20. char nom[25];
  21. char value[100];
  22. } cgiparam;
  23. CHTbl Parametres;
  24. void GererParam(char *param);
  25. int TestEgalite(const void *val1, const void *val2)
  26. {
  27. cgiparam *val_1 = (cgiparam *)val1 , *val_2 = (cgiparam *)val2;
  28. int a = strcmp(val_1->nom,val_2->nom);
  29. if ( a == 0)
  30. a = 1;
  31. else
  32. a = 0;
  33. return a;
  34. }
  35. int hashage(const void *cle)
  36. {
  37. return hashpjw((void*) ( ((cgiparam*) cle)->nom));
  38. }
  39. void cgi_start()
  40. {
  41. if ( cgi_started == FALSE )
  42. printf("Content-type: text/html\nCache-Control: no-cache\nPragma: no-cache\n\n");
  43. cgi_started = TRUE;
  44. }
  45. int cgi_printfile(char *filename)
  46. {
  47. FILE *fp;
  48. char strline[255];
  49. if( (fp = fopen(filename,"rt") ) == NULL )
  50. return -1;
  51. do
  52. {
  53. fgets(strline,255,fp);
  54. printf("%s",strline);
  55. }
  56. while(!feof(fp));
  57. fclose(fp);
  58. return 0;
  59. }
  60. int cgi_parseparam()
  61. {
  62. #ifdef DEBUG
  63. char params[1001];
  64. #else
  65. char *params;
  66. #endif
  67. char temp[126];
  68. int OuQu_onest;
  69. int LenParams,index,LenParam,index2,index3;
  70. cgiparam *Param;
  71. if ( cgi_paramparsed == TRUE )
  72. return 1;
  73. chtbl_init(&Parametres,1000,hashage,TestEgalite,free);
  74. #ifndef DEBUG
  75. params = getenv("QUERY_STRING");
  76. #else
  77. strcpy(params,"erreur=consulter");
  78. #endif
  79. if (params == NULL)
  80. return -1;
  81. LenParams = strlen(params);
  82. memset(temp,0,126);
  83. LenParam=0;
  84. for(index=0;index<(LenParams+1);index++)
  85. {
  86. if ( (params[index] == '&') || (params[index] == 0) )
  87. {
  88. if ( (Param = (cgiparam*) malloc(sizeof(cgiparam))) == NULL)
  89. return -1;
  90. memset(Param,0,sizeof(cgiparam));
  91. OuQu_onest = 0;
  92. index3=0;
  93. for(index2=0;index2<LenParam;index2++)
  94. {
  95. if(temp[index2] == '=')
  96. {
  97. OuQu_onest = 1;
  98. index3 = 0;
  99. }
  100. else
  101. {
  102. if (OuQu_onest == 0)
  103. {
  104. Param->nom[index3++] = temp[index2];
  105. }
  106. else
  107. {
  108. Param->value[index3++] = temp[index2];
  109. }
  110. }
  111. }
  112. chtbl_insert(&Parametres,(void*)Param);
  113. memset(temp,0,126);
  114. LenParam = 0;
  115. }
  116. else
  117. {
  118. temp[LenParam++] = params[index];
  119. }
  120. }
  121. cgi_paramparsed = TRUE;
  122. return 0;
  123. }
  124. char *cgi_getparam(char *ParamName)
  125. {
  126. cgiparam Temp,*Param;
  127. char *Retour = NULL;
  128. if ( cgi_paramparsed == FALSE )
  129. return NULL;
  130. /* Initialisation de la structure */
  131. memset(&Temp,0,sizeof(cgiparam));
  132. /* On cree la structure qui va etre utilisé pour chercher dans la table */
  133. strcpy(Temp.nom,ParamName);
  134. /* On fait pointer Param vers la ou il faut */
  135. Param = &Temp;
  136. if ( chtbl_lookup(&Parametres,(void *)&Param) == 0)
  137. Retour = Param->value;
  138. return Retour;
  139. }
  140. int cgi_parsefile(char *filename)
  141. {
  142. /*
  143. Définition Attention ya une var :
  144. <!--#-->
  145. A mettre en début de ligne
  146. Df Var
  147. <!--TEST-->
  148. ou test est le nom de la var
  149. */
  150. FILE *fp;
  151. char strline[500];
  152. char strtest[61];
  153. int index,index2,LenLine;
  154. if( (fp = fopen(filename,"rt") ) == NULL )
  155. return -1;
  156. do
  157. {
  158. fgets(strline,500,fp);
  159. memset(strtest,0,61);
  160. memcpy(strtest,strline,8);
  161. if(strcmp(strtest,"<!--#-->") == 0)
  162. { /* On a une variable dans la ligne, on découpe... */
  163. LenLine = strlen(strline);
  164. for(index=8;index<LenLine;index++)
  165. {
  166. memset(strtest,0,9);
  167. memcpy(strtest,strline + index,4);
  168. if (strcmp(strtest,"<!--") == 0)
  169. {
  170. index = index + 4;
  171. index2 = 0;
  172. while(strline[ index + index2 ] != '-')
  173. index2++;
  174. memcpy(strtest,strline + index,index2);
  175. GererParam(strtest);
  176. index = index + index2 + 2;
  177. }
  178. else
  179. {
  180. printf("%c",strline[index]);
  181. }
  182. }
  183. }
  184. else
  185. { /* Sinon on affiche normalement */
  186. printf("%s",strline);
  187. }
  188. }
  189. while(!feof(fp));
  190. fclose(fp);
  191. return 0;
  192. }
  193. void cgi_cleanup()
  194. {
  195. if ( cgi_paramparsed == TRUE )
  196. chtbl_destroy(&Parametres);
  197. cgi_paramparsed = FALSE;
  198. }
  199. /*
  200. Cette fonction a pour but de reagir en fonction des paramètres passé en Include dans le fichier qui est lu
  201. ( par ex pour <!--THEME--> le parametre est "THEME" )
  202. */
  203. void GererParam(char *param)
  204. {
  205. ds_t ds_desc;
  206. char temp[512];
  207. if (strcmp(param,"THEME")==0)
  208. { /* On envois le theme courant*/
  209. printf("%s",get_act_theme());
  210. }
  211. else if(strcmp(param,"GET_THEME")==0)
  212. { /* On envois la liste de theme */
  213. cgi_parsefile("./theme/listtheme.file");
  214. }
  215. else if(strcmp(param,"CGI_BASENAME")==0)
  216. { /* On envois le nom de base du cgi */
  217. printf("%s",cgi_BaseName);
  218. }
  219. else
  220. { /* Sinon on va chercher la valeur de la variable "qnx" grace a ds */
  221. ds_desc=ds_register();
  222. memset(temp,0,512);
  223. if ( ds_get(ds_desc,param,temp,511) > 0)
  224. {
  225. printf("%s",temp);
  226. }
  227. ds_deregister(ds_desc);
  228. }
  229. }