/branches/3.2/src/manhat-lib/shared_survey_page.c

# · C · 314 lines · 234 code · 77 blank · 3 comment · 39 complexity · 5dde4af2e1e9816dd50bbc6b840feec7 MD5 · raw file

  1. #include <stdio.h>
  2. #include "../global.h"
  3. #include "shared_survey_util.h"
  4. #include "shared_survey_page.h"
  5. #define MAX_TEMP_NAME 50
  6. #define MAX_HTML_TAG 32
  7. #define MAX_HTML_TAG_WITH_ATT 3
  8. char *valid_html[MAX_HTML_TAG] = {
  9. "<b>", "</b>", "<i>", "</i>", "<table>", "</table>", "<tr>", "</tr>",
  10. "<td>", "</td>", "<th>", "</th>","<h2>", "</h2>", "<H2>", "</H2>",
  11. "<B>", "</B>", "<I>", "</I>", "<TABLE>", "</TABLE>", "<TR>", "</TR>",
  12. "<TD>", "</TD>", "<TH>", "</TH>", "<H3>", "</H3>", "<h3>", "</h3>"
  13. };
  14. char *valid_html_with_att[MAX_HTML_TAG_WITH_ATT] = {"<img", "<a href=", "<table"};
  15. char *replace_disallowed_html_code(char *str)
  16. {
  17. char *ptr, *ptr2, *new_str = 0;
  18. int new_len, i, found, len;
  19. len = strlen(str);
  20. new_len = len *2;
  21. new_str = (char*) malloc(sizeof(char)* new_len +1);
  22. if(!new_str)
  23. cs_critical_error(ERR_MALLOC_FAILED, "replace_disalollowed_html_code");
  24. memset(new_str, '\0', new_len+1);
  25. for(ptr = str; *ptr; ptr++)
  26. {
  27. found = 0;
  28. if(*ptr == '<')
  29. {
  30. for(i = 0; i< MAX_HTML_TAG && !found; i++)
  31. {
  32. ptr2 = strstr(ptr, valid_html[i]);
  33. if(ptr2 && ptr2 == ptr)
  34. {
  35. len = strlen(valid_html[i]) + strlen(new_str);
  36. if(len >= new_len)
  37. {
  38. new_str = realloc(new_str, sizeof(char)*new_len *2 +1);
  39. new_len = new_len *2;
  40. }
  41. strncat(new_str, valid_html[i], strlen(valid_html[i]));
  42. ptr = ptr + strlen(valid_html[i]) -1;
  43. found =1;
  44. }
  45. }
  46. if(!found)
  47. {
  48. for(i = 0; i< MAX_HTML_TAG_WITH_ATT && !found; i++)
  49. {
  50. ptr2 = strstr(ptr, valid_html_with_att[i]);
  51. if(ptr2 && ptr2 == ptr)
  52. {
  53. ptr2 = strchr(ptr, '>');
  54. if(ptr2)
  55. {
  56. len = strlen(new_str) + ptr2 - ptr +1;
  57. if(len >= new_len)
  58. {
  59. new_str = realloc(new_str, sizeof(char)*new_len *2 +1);
  60. new_len = new_len *2;
  61. }
  62. strncat(new_str, ptr, (ptr2 - ptr ) +1);
  63. ptr = ptr2;
  64. found = 1;
  65. }
  66. }
  67. }
  68. if(!found)
  69. {
  70. len = strlen("&lt;") + strlen(new_str);
  71. if(len >= new_len)
  72. {
  73. new_str = realloc(new_str, sizeof(char)*new_len *2 +1);
  74. new_len = new_len *2;
  75. }
  76. strncat(new_str, "&lt;", strlen("&lt;"));
  77. }
  78. }
  79. }
  80. else if(*ptr == '>')
  81. {
  82. len = strlen("&gt;") + strlen(new_str);
  83. if(len >= new_len)
  84. {
  85. new_str = realloc(new_str, sizeof(char)*new_len *2 +1);
  86. new_len = new_len *2;
  87. }
  88. strncat(new_str, "&gt;", strlen("&gt;"));
  89. }
  90. else
  91. {
  92. len = 1 + strlen(new_str);
  93. if(len >= new_len)
  94. {
  95. new_str = realloc(new_str, sizeof(char)*new_len *2 +1);
  96. new_len = new_len *2;
  97. }
  98. strncat(new_str, ptr, 1);
  99. }
  100. }
  101. return new_str;
  102. }
  103. static void
  104. set_common_property(int i, char *question_type, char *action, SURVEY_DATA *data)
  105. {
  106. char name[MAX_TEMP_NAME];
  107. //char *str;
  108. snprintf(name, MAX_TEMP_NAME, "item.%d.type", i);
  109. cs_set_value(name, question_type);
  110. snprintf(name, MAX_TEMP_NAME, "item.%d.modify_program", i);
  111. cs_set_value(name, action);
  112. snprintf(name, MAX_TEMP_NAME, "item.%d.id", i);
  113. cs_set_int_value(name, data->id);
  114. snprintf(name, MAX_TEMP_NAME, "item.%d.content", i);
  115. // str = replace_disallowed_html_code(data->caption);
  116. cs_set_value(name, data->caption);
  117. //free(str);
  118. }
  119. static void
  120. set_title_data(SURVEY_DATA *data, int i)
  121. {
  122. char name[MAX_TEMP_NAME];
  123. set_common_property(i, HEADING_TYPE, "survey_title_modify_form", data);
  124. snprintf(name, MAX_TEMP_NAME, "item.%d.chosen_bgcolor", i);
  125. cs_set_value(name, data->steps_bgcolor);
  126. cs_set_value("bgcolor", data->steps_bgcolor);
  127. }
  128. static void
  129. set_custom_data(SURVEY_DATA *data, int i)
  130. {
  131. set_common_property(i, CUSTOM_TYPE, "survey_custom_modify_form", data);
  132. }
  133. static void
  134. set_likert_data(SURVEY_DATA *data, int i, int order)
  135. {
  136. char name[MAX_TEMP_NAME];
  137. snprintf(name, MAX_TEMP_NAME, "item.%d.order", i);
  138. cs_set_int_value(name, order);
  139. set_common_property(i, LIKERT_TYPE, "survey_likert_modify_form", data);
  140. snprintf(name, MAX_TEMP_NAME, "item.%d.left_value", i);
  141. cs_set_value(name, data->left_cols);
  142. snprintf(name, MAX_TEMP_NAME, "item.%d.right_value", i);
  143. cs_set_value(name, data->right_rows);
  144. snprintf(name, MAX_TEMP_NAME, "item.%d.steps", i);
  145. cs_set_int_value(name, atoi(data->steps_bgcolor));
  146. }
  147. static void
  148. set_memo_data(SURVEY_DATA *data, int i, int order)
  149. {
  150. char name[MAX_TEMP_NAME];
  151. snprintf(name, MAX_TEMP_NAME, "item.%d.order", i);
  152. cs_set_int_value(name, order);
  153. set_common_property(i, MEMO_TYPE, "survey_memo_modify_form", data);
  154. snprintf(name, MAX_TEMP_NAME, "item.%d.cols", i);
  155. cs_set_value(name, data->left_cols);
  156. snprintf(name, MAX_TEMP_NAME, "item.%d.rows", i);
  157. cs_set_value(name, data->right_rows);
  158. }
  159. static void
  160. set_choice_data(SURVEY_DATA *data, int i, int order)
  161. {
  162. char name[MAX_TEMP_NAME];
  163. int ele_count = 0;
  164. char *str;
  165. snprintf(name, MAX_TEMP_NAME, "item.%d.order", i);
  166. cs_set_int_value(name, order);
  167. set_common_property(i, MC_TYPE, "survey_choice_modify_form", data);
  168. snprintf(name, MAX_TEMP_NAME, "item.%d.multi", i);
  169. cs_set_value(name, data->steps_bgcolor);
  170. if(data->head)
  171. {
  172. data->current = data->head;
  173. while(data->current)
  174. {
  175. snprintf(name, MAX_TEMP_NAME, "item.%d.choice.%d", i, ele_count);
  176. str = replace_disallowed_html_code(data->current->choice);
  177. cs_set_value(name, str);
  178. free(str);
  179. data->current = data->current->next;
  180. ele_count++;
  181. }
  182. }
  183. if(ele_count)
  184. {
  185. snprintf(name, MAX_TEMP_NAME, "item.%d.steps", i);
  186. cs_set_int_value(name, ele_count +1);
  187. }
  188. }
  189. static void
  190. set_text_data(SURVEY_DATA *data, int i, int order)
  191. {
  192. char name[MAX_TEMP_NAME];
  193. set_common_property(i, SHORT_ANSWER_TYPE, "survey_text_modify_form", data);
  194. snprintf(name, MAX_TEMP_NAME, "item.%d.order", i);
  195. cs_set_int_value(name, order);
  196. }
  197. void
  198. set_survey_content(SURVEY_LIST *list )
  199. {
  200. int i =0;
  201. SURVEY_DATA *ptr;
  202. int order = 1;
  203. if(list)
  204. {
  205. list->current = list->head;
  206. while(list->current)
  207. {
  208. ptr = list->current;
  209. if(strcmp(ptr->tag_name, "title") ==0)
  210. set_title_data(ptr, i);
  211. else if(is_likert_tag(ptr->tag_name)) /* shared_survey_util.c */
  212. {
  213. set_likert_data(ptr, i, order);
  214. order++;
  215. }
  216. else if(!strcmp(ptr->tag_name, "choice"))
  217. {
  218. set_choice_data(ptr, i, order);
  219. order++;
  220. }
  221. else if(!strcmp(ptr->tag_name, "text"))
  222. {
  223. set_text_data(ptr, i, order);
  224. order++;
  225. }
  226. else if(!strcmp(ptr->tag_name, "memo"))
  227. {
  228. set_memo_data(ptr, i, order);
  229. order++;
  230. }
  231. else
  232. set_custom_data(ptr, i);
  233. i++;
  234. list->current = list->current->next;
  235. }
  236. }
  237. }