Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
[privoxy.git] / debian / patches / 01_local_usermanual.dpatch
1 #! /bin/sh /usr/share/dpatch/dpatch-run
2 ## 01_local_usermanual.dpatch by Roland Rosenfeld <roland@debian.org>
3 ##
4 ## All lines beginning with `## DP:' are a description of the patch.
5 ## DP: Add new "cgi" user-manual, which sends the user manual to the client
6 ## DP: and activate it in config file (Closes: #148128).
7
8 @DPATCH@
9 diff -urNad privoxy~/cgi.c privoxy/cgi.c
10 --- privoxy~/cgi.c      2006-02-11 23:44:26.000000000 +0100
11 +++ privoxy/cgi.c       2006-02-11 23:44:34.000000000 +0100
12 @@ -609,6 +609,9 @@
13     { "t",
14           cgi_transparent_image, 
15           NULL, TRUE /* Send a transparent image (short name) */ },
16 +   { "user-manual",
17 +         cgi_send_user_manual,
18 +         NULL, TRUE /* Send user-manual */ },
19     { NULL, /* NULL Indicates end of list and default page */
20           cgi_error_404,
21           NULL, TRUE /* Unknown CGI page */ }
22 @@ -821,21 +824,28 @@
23     {
24        return cgi_error_memory();
25     }
26 -
27     query_args_start = path_copy;
28 -   while (*query_args_start && *query_args_start != '?')
29 +   while (*query_args_start && *query_args_start != '?' && *query_args_start != '/')
30     {
31        query_args_start++;
32     }
33 -   if (*query_args_start == '?')
34 +   if (*query_args_start == '/') 
35     {
36        *query_args_start++ = '\0';
37 -   }
38 -
39 -   if (NULL == (param_list = parse_cgi_parameters(query_args_start)))
40 -   {
41 -      free(path_copy);
42 -      return cgi_error_memory();
43 +      if ((param_list = new_map()))
44 +      {
45 +         map(param_list, "file", 1, url_decode(query_args_start), 0);
46 +      }
47 +   } else {
48 +      if (*query_args_start == '?')
49 +      {
50 +         *query_args_start++ = '\0';
51 +      }
52 +      if (NULL == (param_list = parse_cgi_parameters(query_args_start)))
53 +      {
54 +         free(path_copy);
55 +         return cgi_error_memory();
56 +      }
57     }
58  
59     /*
60 @@ -1597,7 +1607,13 @@
61     if (!item) return NULL;
62  
63     result = strdup("<a href=\"");
64 -   string_append(&result, config->usermanual);
65 +   if (config->usermanual[0] == '/') {
66 +      string_append(&result, "http://");
67 +      string_append(&result, CGI_SITE_2_HOST);
68 +      string_append(&result, "/user-manual/");
69 +   } else {
70 +      string_append(&result, config->usermanual);
71 +   }
72     string_append(&result, ACTIONS_HELP_PREFIX);
73     string_join  (&result, string_toupper(item));
74     string_append(&result, "\">");
75 @@ -2171,7 +2187,11 @@
76     if (!err) err = map(exports, "default-cgi",   1, html_encode(CGI_PREFIX), 0);
77     if (!err) err = map(exports, "menu",          1, make_menu(caller), 0);
78     if (!err) err = map(exports, "code-status",   1, CODE_STATUS, 1);
79 -   if (!err) err = map(exports, "user-manual",   1, csp->config->usermanual ,1);
80 +   if (csp->config->usermanual[0] == '/') {
81 +      if (!err) err = map(exports, "user-manual",   1, "http://"CGI_SITE_2_HOST"/user-manual/" ,1);
82 +   } else {
83 +      if (!err) err = map(exports, "user-manual",   1, csp->config->usermanual ,1);
84 +   }
85     if (!err) err = map(exports, "actions-help-prefix", 1, ACTIONS_HELP_PREFIX ,1);
86  #ifdef FEATURE_TOGGLE
87     if (!err) err = map_conditional(exports, "enabled-display", global_toggle_state);
88 diff -urNad privoxy~/cgisimple.c privoxy/cgisimple.c
89 --- privoxy~/cgisimple.c        2006-02-11 23:44:26.000000000 +0100
90 +++ privoxy/cgisimple.c 2006-02-11 23:44:34.000000000 +0100
91 @@ -642,6 +642,89 @@
92     return JB_ERR_OK;
93  
94  }
95 +/*********************************************************************
96 + *
97 + * Function    :  cgi_send_user_manual
98 + *
99 + * Description :  CGI function that sends a user manual HTML file
100 + *
101 + * Parameters  :
102 + *          1  :  csp = Current client state (buffers, headers, etc...)
103 + *          2  :  rsp = http_response data structure for output
104 + *          3  :  parameters = map of cgi parameters
105 + *
106 + * CGI Parameters : file=name.html, the mane of the HTML file
107 + *                  (relative to user-manual from config)
108 + *
109 + * Returns     :  JB_ERR_OK on success
110 + *                JB_ERR_MEMORY on out-of-memory error.  
111 + *
112 + *********************************************************************/
113 +jb_err cgi_send_user_manual(struct client_state *csp,
114 +                            struct http_response *rsp,
115 +                            const struct map *parameters)
116 +{
117 +   const char * filename;
118 +   char *full_path;
119 +   FILE *fp;
120 +   char buf[BUFFER_SIZE];
121 +
122 +   assert(csp);
123 +   assert(rsp);
124 +   assert(parameters);
125 +
126 +   get_string_param(parameters, "file", &filename);
127 +   /* Check paramter for hack attempts */
128 +   if (filename && strchr(filename, '/')) {
129 +      return JB_ERR_CGI_PARAMS;
130 +   }
131 +   if (filename && strstr(filename, "..")) {
132 +      return JB_ERR_CGI_PARAMS;
133 +   }
134 +
135 +   full_path = make_path(csp->config->usermanual, 
136 +                         filename?filename:"index.html");
137 +   if (full_path == NULL)
138 +   {
139 +      return JB_ERR_MEMORY;
140 +   }
141 +
142 +   /* Allocate buffer */
143 +   rsp->body = strdup("");
144 +   if (rsp->body == NULL)
145 +   {
146 +      free(full_path);
147 +      return JB_ERR_MEMORY;
148 +   }
149 +
150 +   /* Open user-manual file */
151 +   if (NULL == (fp = fopen(full_path, "r")))
152 +   {
153 +      log_error(LOG_LEVEL_ERROR, "Cannot open user-manual file %s: %E", full_path);
154 +      free(full_path);
155 +      free(rsp->body);
156 +      return JB_ERR_FILE;
157 +   }
158 +   free(full_path);
159 +
160 +   /* Read file and write it out */
161 +   while (fgets(buf, BUFFER_SIZE, fp))
162 +   {
163 +      if (string_append(&rsp->body, buf))
164 +      {
165 +         fclose(fp);
166 +         return JB_ERR_MEMORY;
167 +      }
168 +   }
169 +   fclose(fp);
170 +
171 +   if (enlist(rsp->headers, "Content-Type: text/html"))
172 +   {
173 +      return JB_ERR_MEMORY;
174 +   }
175 +
176 +   return JB_ERR_OK;
177 +}
178  
179  
180  /*********************************************************************
181 diff -urNad privoxy~/cgisimple.h privoxy/cgisimple.h
182 --- privoxy~/cgisimple.h        2006-02-11 23:44:26.000000000 +0100
183 +++ privoxy/cgisimple.h 2006-02-11 23:44:34.000000000 +0100
184 @@ -128,6 +128,9 @@
185  extern jb_err cgi_send_stylesheet(struct client_state *csp,
186                                    struct http_response *rsp,
187                                    const struct map *parameters);
188 +extern jb_err cgi_send_user_manual(struct client_state *csp,
189 +                                   struct http_response *rsp,
190 +                                   const struct map *parameters);
191  
192  #ifdef FEATURE_GRACEFUL_TERMINATION
193  extern jb_err cgi_die (struct client_state *csp,
194 diff -urNad privoxy~/loadcfg.c privoxy/loadcfg.c
195 --- privoxy~/loadcfg.c  2006-02-11 23:44:26.000000000 +0100
196 +++ privoxy/loadcfg.c   2006-02-11 23:44:34.000000000 +0100
197 @@ -1579,7 +1579,13 @@
198      * link to it's section in the user-manual
199      */
200     buf = strdup("\n<br><a href=\"");
201 -   string_append(&buf, config->usermanual);
202 +   if (config->usermanual[0] == '/') {
203 +      string_append(&buf, "http://");
204 +      string_append(&buf, CGI_SITE_2_HOST);
205 +      string_append(&buf, "/user-manual/");
206 +   } else {
207 +      string_append(&buf, config->usermanual);
208 +   }
209     string_append(&buf, CONFIG_HELP_PREFIX);
210     string_join  (&buf, string_toupper(command));
211     string_append(&buf, "\">");