00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <stdbool.h>
00033 #include <stdarg.h>
00034 #include <string.h>
00035 #include "qDecoder.h"
00036 #include "qInternal.h"
00037
00038 #ifndef DISABLE_CGI
00039
00082 bool qHtmlPrintf(FILE *stream, int mode, const char *format, ...) {
00083 char buf[1024*10];
00084 va_list arglist;
00085 int status;
00086
00087 va_start(arglist, format);
00088 status = vsnprintf(buf, sizeof(buf), format, arglist);
00089 va_end(arglist);
00090
00091 return qHtmlPuts(stream, mode, buf);
00092 }
00093
00105 bool qHtmlPuts(FILE *stream, int mode, char *buf) {
00106
00107 if (buf == NULL) return false;
00108
00109 if (mode == 0) fprintf(stream, "%s", buf);
00110 else if (mode == 10) {
00111 int i;
00112 for (i = 0; buf[i] != '\0'; i++) {
00113 switch (buf[i]) {
00114 case ' ' : {
00115 if ((i > 0) && (buf[i - 1] == ' ')) fprintf(stream, " ");
00116 else fprintf(stream, " ");
00117 break;
00118 }
00119 case '\r' : {
00120 break;
00121 }
00122 case '\n' : {
00123 fprintf(stream, "<br>\n");
00124 break;
00125 }
00126 default : {
00127 fprintf(stream, "%c", buf[i]);
00128 break;
00129 }
00130 }
00131 }
00132 } else {
00133 char *ptr, retstop, lastretstop, *target, *deftarget, *token;
00134 int printhtml, autolink, convert, linkflag, ignoreflag;
00135
00136
00137 printhtml = 1;
00138 autolink = 1;
00139 target = "_top";
00140 deftarget = "qnewwin";
00141 convert = 0;
00142
00143 switch (mode) {
00144 case 01 : {
00145 printhtml = 1, autolink = 0, target = "";
00146 convert = 0;
00147 break;
00148 }
00149 case 11 : {
00150 printhtml = 1, autolink = 0, target = "";
00151 convert = 1;
00152 break;
00153 }
00154
00155 case 02 : {
00156 printhtml = 1, autolink = 1, target = "";
00157 convert = 0;
00158 break;
00159 }
00160 case 12 : {
00161 printhtml = 1, autolink = 1, target = "";
00162 convert = 1;
00163 break;
00164 }
00165
00166 case 03 : {
00167 printhtml = 1, autolink = 1, target = "_top";
00168 convert = 0;
00169 break;
00170 }
00171 case 13 : {
00172 printhtml = 1, autolink = 1, target = "_top";
00173 convert = 1;
00174 break;
00175 }
00176 case 23 : {
00177 printhtml = 1, autolink = 1, target = deftarget;
00178 convert = 0;
00179 break;
00180 }
00181 case 33 : {
00182 printhtml = 1, autolink = 1, target = deftarget;
00183 convert = 1;
00184 break;
00185 }
00186
00187 case 04 : {
00188 printhtml = 0, autolink = 0, target = "";
00189 convert = 0;
00190 break;
00191 }
00192 case 14 : {
00193 printhtml = 0, autolink = 0, target = "";
00194 convert = 1;
00195 break;
00196 }
00197
00198 case 05 : {
00199 printhtml = 0, autolink = 1, target = "";
00200 convert = 0;
00201 break;
00202 }
00203 case 15 : {
00204 printhtml = 0, autolink = 1, target = "";
00205 convert = 1;
00206 break;
00207 }
00208
00209 case 06 : {
00210 printhtml = 0, autolink = 1, target = "_top";
00211 convert = 0;
00212 break;
00213 }
00214 case 16 : {
00215 printhtml = 0, autolink = 1, target = "_top";
00216 convert = 1;
00217 break;
00218 }
00219 case 26 : {
00220 printhtml = 0, autolink = 1, target = deftarget;
00221 convert = 0;
00222 break;
00223 }
00224 case 36 : {
00225 printhtml = 0, autolink = 1, target = deftarget;
00226 convert = 1;
00227 break;
00228 }
00229
00230 default: {
00231 DEBUG("_autolink(): Invalid Mode (%d).", mode);
00232 return false;
00233 }
00234 }
00235
00236 token = " `(){}[]<>\"',\r\n";
00237 lastretstop = '0';
00238 ptr = qStrTok(buf, token, &retstop);
00239
00240 for (linkflag = ignoreflag = 0; ptr != NULL;) {
00241
00242 if (ignoreflag == 0) {
00243 if (autolink == 1) {
00244 if (!strncmp(ptr, "http://", 7)) linkflag = 1;
00245 else if (!strncmp(ptr, "https://", 8)) linkflag = 1;
00246 else if (!strncmp(ptr, "ftp://", 6)) linkflag = 1;
00247 else if (!strncmp(ptr, "telnet://", 9)) linkflag = 1;
00248 else if (!strncmp(ptr, "news:", 5)) linkflag = 1;
00249 else if (!strncmp(ptr, "mailto:", 7)) linkflag = 1;
00250 else if (qHtmlIsEmail(ptr) == 1) linkflag = 2;
00251 else linkflag = 0;
00252 }
00253 if (linkflag == 1) fprintf(stream, "<a href=\"%s\" target=\"%s\">%s</a>", ptr, target, ptr);
00254 else if (linkflag == 2) fprintf(stream, "<a href=\"mailto:%s\">%s</a>", ptr, ptr);
00255 else fprintf(stream, "%s", ptr);
00256 }
00257
00258
00259 if (printhtml == 1) {
00260 if (retstop == '<') fprintf(stream, "<");
00261 else if (retstop == '>') fprintf(stream, ">");
00262 else if (retstop == '\"') fprintf(stream, """);
00263 else if (retstop == '&') fprintf(stream, "&");
00264
00265 else if (retstop == ' ' && convert == 1) {
00266 if (lastretstop == ' ' && strlen(ptr) == 0) fprintf(stream, " ");
00267 else fprintf(stream, " ");
00268 } else if (retstop == '\r' && convert == 1);
00269 else if (retstop == '\n' && convert == 1) fprintf(stream, "<br>\n");
00270
00271 else if (retstop != '\0') fprintf(stream, "%c", retstop);
00272 } else {
00273 if (retstop == '<') ignoreflag = 1;
00274 else if (retstop == '>') ignoreflag = 0;
00275
00276 else if (retstop == '\"' && ignoreflag == 0) fprintf(stream, """);
00277 else if (retstop == '&' && ignoreflag == 0) fprintf(stream, "&");
00278
00279 else if (retstop == ' ' && ignoreflag == 0 && convert == 1) {
00280 if (lastretstop == ' ' && strlen(ptr) == 0) fprintf(stream, " ");
00281 else fprintf(stream, " ");
00282 } else if (retstop == '\r' && ignoreflag == 0 && convert == 1);
00283 else if (retstop == '\n' && ignoreflag == 0 && convert == 1) fprintf(stream, "<br>\n");
00284
00285 else if (retstop != '\0' && ignoreflag == 0) fprintf(stream, "%c", retstop);
00286
00287 }
00288
00289 lastretstop = retstop;
00290 ptr = qStrTok(NULL, token, &retstop);
00291 }
00292 }
00293
00294 return true;
00295 }
00296
00304 bool qHtmlIsEmail(const char *email) {
00305 int i, alpa, dot, gol;
00306
00307 if (email == NULL) return false;
00308
00309 for (i = alpa = dot = gol = 0; email[i] != '\0'; i++) {
00310 switch (email[i]) {
00311 case '@' : {
00312 if (alpa == 0) return false;
00313 if (gol > 0) return false;
00314 gol++;
00315 break;
00316 }
00317 case '.' : {
00318 if ((i > 0) && (email[i - 1] == '@')) return false;
00319 if ((gol > 0) && (email[i - 1] == '.')) return false;
00320 dot++;
00321 break;
00322 }
00323 default : {
00324 alpa++;
00325 if ((email[i] >= '0') && (email[i] <= '9')) break;
00326 else if ((email[i] >= 'A') && (email[i] <= 'Z')) break;
00327 else if ((email[i] >= 'a') && (email[i] <= 'z')) break;
00328 else if ((email[i] == '-') || (email[i] == '_')) break;
00329 else return false;
00330 }
00331 }
00332 }
00333
00334 if ((alpa <= 3) || (gol == 0) || (dot == 0)) return false;
00335
00336 return true;
00337 }
00338
00346 bool qHtmlIsUrl(const char *url) {
00347 if (!strncmp(url, "http://", 7)) return true;
00348 else if (!strncmp(url, "ftp://", 6)) return true;
00349 else if (!strncmp(url, "telnet://", 9)) return true;
00350 else if (!strncmp(url, "mailto:", 7)) return true;
00351 else if (!strncmp(url, "news:", 5)) return true;
00352 return false;
00353 }
00354
00355 #endif