static int get_query_value(struct shttpd_arg* arg, const char* name, char* value, int valueSize) { const char* queryString; value[0] = '\0'; queryString = shttpd_get_env(arg, "QUERY_STRING"); if (queryString == NULL) { return 0; } int result = shttpd_get_var(name, queryString, strlen(queryString), value, valueSize); if (result < 0 || result == valueSize) { return 0; } value[valueSize - 1] = '\0'; return 1; }
static int index_html (struct shttpd_arg_t *arg) { int n = 0, ret = 0; int *p = arg->user_data; /* integer passed to us */ const char *phone_num = NULL, *text = NULL; const char *ajax = NULL; int isajax = 0; if((ajax = shttpd_get_var(arg, "ajax")) != NULL) isajax = 1; if(!isajax) { n += snprintf (arg->buf + n, arg->buflen - n, "%s", "HTTP/1.1 200 OK\r\n" "Content-Type: text/html\r\n\r\n" "<html><head><title>Send SMS To Phone</title>" "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf8\"/>" "<meta name=\"viewport\" content=\"width=320, initial-scale=1.0\" />" "</head><body>"); } phone_num = shttpd_get_var (arg, "phone_num"); text = shttpd_get_var (arg, "text"); if(phone_num != NULL && text != NULL) { if ((ret = send_sms (phone_num, text)) < 0) { n += snprintf (arg->buf + n, arg->buflen - n, "<p>Could not Send SMS: %d</p>", ret); } else { char sql[1024]; char *sr; n += snprintf (arg->buf + n, arg->buflen - n, "<p>Sent Ok</p>"); sprintf(sql,"insert into message (address,text,date,flags) values('%s','%s','%d','3');", phone_num,text,time(NULL)); sr = sqlite_exec("/var/root/Library/SMS/sms.db",sql); free(sr); } if(isajax) { arg->last = 1; return (n); } } n += snprintf (arg->buf + n, arg->buflen - n, "<form method=\"post\">" "<p>To:" "<input style=\"font-size:18px\" type=\"text\" size=\"14\" maxlength=\"14\" name=\"phone_num\" value=\"%s\"/>" " <a href=\"/simpb.html\">PhoneBook</a></p>" "<textarea style=\"font-size:15px\" cols=30 rows=10 name=\"text\">%s</textarea><br/>" "<input style=\"font-size:15px\" value=\"Send\" type=\"submit\"/></form>", (phone_num != NULL) ? phone_num : "", (text != NULL) ? text : ""); n += snprintf (arg->buf + n, arg->buflen - n, "<div style=\"text-align: right\"><a href=\"/setting.html\">Setting</a> <a href=\"/sms_sim.html\">SMS(SIM)</a> <a href=\"/sms.html?box=inbox\">Inbox</a> <a href=\"/sms.html?box=outbox\">Outbox</a></div>"); n += snprintf (arg->buf + n, arg->buflen - n, "<br/><div><a href=\"/credit.html\">Credit</a>"); n += snprintf (arg->buf + n, arg->buflen - n, " <a href=\"/api.html\">API</a></dev>"); n += snprintf (arg->buf + n, arg->buflen - n, "</body></html>"); assert (n < (int) arg->buflen); arg->last = 1; return (n); }
static int setting_html(struct shttpd_arg_t *arg) { int n = 0, ret = 0; const char *username=NULL; const char *pass1=NULL; const char *pass2=NULL; n += snprintf (arg->buf + n, arg->buflen - n, "%s", "HTTP/1.1 200 OK\r\n" "Content-Type: text/html\r\n\r\n" "<html><head><title>Setting</title>" "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf8\"/>" "<meta name=\"viewport\" content=\"width=320, initial-scale=1.0\" />" "</head><body>"); if((username = shttpd_get_var(arg, "user")) != NULL) { if((pass1 = shttpd_get_var(arg, "pass1")) != NULL) { if((pass2 = shttpd_get_var(arg, "pass2")) != NULL) { fprintf(stderr,"user:%s\n",username); fprintf(stderr,"pass1:%s\n",pass1); fprintf(stderr,"pass2:%s\n",pass2); if(strcmp(pass1,pass2)) { n += snprintf (arg->buf + n, arg->buflen - n, "Password Mismatch"); n += snprintf (arg->buf + n, arg->buflen - n, "</body></html>"); arg->last = 1; return n; } char buf[128]; md5(buf,username,":","mydomain.com",":",pass1); fprintf(stderr,"md5:%s\n",buf); FILE *fp = fopen("/usr/local/etc/htpasswd","w+"); if(fp) { char htpasswd[128]; sprintf(htpasswd,"%s:%s:%s",username,"mydomain.com",buf); fprintf(fp,htpasswd); fclose(fp); n += snprintf (arg->buf + n, arg->buflen - n, "Reset Password Successfully."); n += snprintf (arg->buf + n, arg->buflen - n, "</body></html>"); }else{ n += snprintf (arg->buf + n, arg->buflen - n, "Reset Password Error."); n += snprintf (arg->buf + n, arg->buflen - n, "</body></html>"); } arg->last = 1; return n; } } } n += snprintf (arg->buf + n, arg->buflen - n, "<form method=\"post\">" "<p>Reset Username and Password:</p>" "<p>User Name:<input type=\"text\" maxlength=\"14\" name=\"user\" value=\"\"/></p>" "<p>Password:<input type=\"password\" maxlength=8 name=\"pass1\" value=\"\"/></p>" "<p>Confirm Password:<input type=\"password\" maxlength=8 name=\"pass2\" value=\"\"/></p>" "<input value=\"Generate\" type=\"submit\"/></form>"); n += snprintf (arg->buf + n, arg->buflen - n, "</body></html>"); arg->last = 1; return n; }
static int sms_html (struct shttpd_arg_t *arg) { int n = 0, ret = 0; char *line; int sr_len; char *sr,*sr1; char *box = NULL; char sql[256]; n += snprintf (arg->buf + n, arg->buflen - n, "%s", "HTTP/1.1 200 OK\r\n" "Content-Type: text/html\r\n\r\n" "<html><head><title>SMS in iPhone DataBase</title>" "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf8\"/>" "<meta name=\"viewport\" content=\"width=320, initial-scale=1.0\" />" "</head><body>"); sprintf(sql,"select address,text,date from message order by date desc"); box = shttpd_get_var(arg, "box"); if(box != NULL) { if(!strcmp(box,"inbox")) sprintf(sql,"select address,text,date from message where flags<3 order by date desc"); else if(!strcmp(box,"outbox")) sprintf(sql,"select address,text,date from message where flags=3 order by date desc"); } n += snprintf(arg->buf + n, arg->buflen - n, "<div>"); sr = sqlite_exec("/var/root/Library/SMS/sms.db",sql); sr_len = strlen(sr); for(line = sr; line < (sr + sr_len-1); line = strstr(line,"|\n")+2) { char addr[64]; char text[1024]; time_t t; char sql[128]; int id = -1; char *name; char *time_str; char encoded_url[1024]; sscanf(line,"%[^|]|%[^|]|%d",addr,text,&t); sprintf(sql,"select multivalue_id from ABPhoneLastFour where value=%s",&addr[strlen(addr)-4]); sr1 = sqlite_exec("/var/root/Library/AddressBook/AddressBook.sqlitedb",sql); sscanf(sr1,"%d",&id); free(sr1); sprintf(sql,"select record_id from ABMultiValue where UID=%d",id); sr1 = sqlite_exec("/var/root/Library/AddressBook/AddressBook.sqlitedb",sql); sscanf(sr1,"%d",&id); free(sr1); sprintf(sql,"select first from ABPerson where ROWID=%d",id); sr1 = sqlite_exec("/var/root/Library/AddressBook/AddressBook.sqlitedb",sql); fprintf(stderr,"sr1:%s",sr1); if(sr1[0] != 0) { sr1[strlen(sr1)-2] = 0; name = sr1; }else name = addr; time_str = ctime(&t); time_str[strlen(time_str)-2] = 0; //printf("%s",line); //printf("%s\n%s\n",time_str,text); urlencode(addr,encoded_url); n += snprintf(arg->buf + n, arg->buflen - n, "<p><a href=\"/?phone_num=%s\">%s</a> %s",encoded_url,name,ctime(&t)); n += snprintf(arg->buf + n, arg->buflen - n, "<span>%s</span>",text); //urlencode(text, encoded_text); n += snprintf(arg->buf + n, arg->buflen - n, " <a href=\"/?text=%s\">smsto</a> <a href=\"mailto:?body=%s\">mailto</a></p>",text,text); free(sr1); } free(sr); n += snprintf (arg->buf + n, arg->buflen - n, "</div></body></html>"); assert (n < (int) arg->buflen); arg->last = 1; return (n); }
static int index_html(struct shttpd_arg_t *arg) { int n = 0; int *p = arg->user_data; /* integer passed to us */ FILE *fp; const char *value, *fname, *data, *host; /* Change the value of integer variable */ if ((value = shttpd_get_var(arg, "name1")) != NULL) *p = atoi(value); n += snprintf(arg->buf + n, arg->buflen - n, "%s", "HTTP/1.1 200 OK\r\n" "Content-Type: text/html\r\n\r\n<html><body>"); n += snprintf(arg->buf + n, arg->buflen - n, "<h1>Welcome to embedded" " example of SHTTPD v. %s </h1>", shttpd_version()); n += snprintf(arg->buf + n, arg->buflen - n, "<h2>Internal int variable value: [%d]</h2>", *p); n += snprintf(arg->buf + n, arg->buflen - n, "%s", "<form method=\"POST\">" "Enter new value: " "<input type=\"text\" name=\"name1\"/>" "<input type=\"submit\">" "</form><ul>" "<li><a href=\"/secret\">Protected page</a> (guest:guest)" "<li><a href=\"/huge\">Output 1Mb of data</a>" "<li><a href=\"/myetc/\">Aliased /etc directory</a>" "<li><a href=\"/Makefile\">On-disk file (Makefile)</a>"); host = shttpd_get_header(arg, "Host"); n += snprintf(arg->buf + n, arg->buflen - n, "<li>'Host' header value: [%s]", host ? host : "NOT SET"); n += snprintf(arg->buf + n, arg->buflen - n, "<li>This example shows that it is possible to POST " "big volumes of data, and save it into a file"); /* Save POSTed data */ if ((fname = shttpd_get_var(arg, "filename")) != NULL) { if ((fp = fopen(fname, "w+")) != NULL) { if ((data = shttpd_get_var(arg, "data")) == NULL) { n += snprintf(arg->buf + n, arg->buflen - n, "<p>Could not get data</p>"); } else if (fwrite(data, strlen(data), 1, fp) != 1) { n += snprintf(arg->buf + n, arg->buflen - n, "<p>Could not write data: %s</p>", strerror(errno)); } (void) fclose(fp); } else { n += snprintf(arg->buf + n, arg->buflen - n, "<p>Error opening %s: %s</p>", fname, strerror(errno)); } } n += snprintf(arg->buf + n, arg->buflen - n, "<form method=\"post\">" "<p>Enter file name to store POSTed data: " "<input type=\"text\" name=\"filename\" value=\"%s\">" "<p>Data to be stored:<br>" "<textarea cols=68 rows=25 name=\"data\">hello</textarea><br>" "<input type=\"submit\"></form>", fname ? fname : ""); n += snprintf(arg->buf + n, arg->buflen - n, "</body></html>"); assert(n < (int)arg->buflen); arg->last = 1; return (n); }