// ----------------------------------------------------------------------------
// Copyright (C) 2014
// David Freese, W1HKJ
//
// This file is part of flmsg
//
// flrig is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// flrig is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
// ----------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "mongoose.h"
#include "config.h"
#include "flmsg.h"
#include "templates.h"
#include "debug.h"
#include "util.h"
#include "gettext.h"
#include "flmsg_dialog.h"
#include "flmsg_config.h"
#include "flinput2.h"
#include "date.h"
#include "calendar.h"
#include "icons.h"
#include "fileselect.h"
#include "wrap.h"
#include "status.h"
#include "parse_xml.h"
#include "icons.h"
#include "threads.h"
#include "ext_string.h"
#ifdef WIN32
# include "flmsgrc.h"
# include "compat.h"
# define dirent fl_dirent_no_thanks
#endif
#include
#include "dirent-check.h"
#include
#include
#include
using namespace std;
extern int custom_select;
extern struct mg_server *server;
static string action_str = "\n name_values;
string html_form;
string edit_txt;
void escape(string &s)
{
size_t p;
p = 0;
while ((p = s.find("\r", p)) != string::npos)
s.erase(p,1);
p = 0;
while ((p = s.find("\n", p)) != string::npos) {
s.replace(p, 1, "\\n");
p += 2;
}
p = 0;
while ((p = s.find("\"", p)) != string::npos) {
if (p == 0 || s[p-1] != '\\') {
s.replace(p, 1, "\\\"");
p += 2;
} else p++;
}
}
void unescape(string &s)
{
size_t p;
p = 0;
while ((p = s.find("\\n", p)) != string::npos) {
s.replace(p,2,"\n");
p++;
}
p = 0;
while ((p = s.find("\\\"", p)) != string::npos) {
s.replace(p,2,"\"");
p++;
}
}
int convert_case(string &s)
{
std::string s1, s3;
extstring s2;//s1;
s2.assign(s);
size_t p1 = s2.ufind("", ptype);
pname = html_form.find(name_str, pstart);
if (pname == string::npos || pname > pend) {
ptype = html_form.find(input_types[i].txt, ptype + 1);
continue;
}
pname += name_str.length();
p1 = html_form.find("\"", pname);
field_name = html_form.substr(pname, p1 - pname);
switch (input_types[i].id) {
case T_TEXT: case T_PASSWORD: case T_NUMBER:
case T_DATE: case T_DTIME: case T_DTIME_LOCAL:
case T_TIME: case T_WEEK: case T_MONTH: case T_TEL: case T_EMAIL:
pvalue = html_form.find(value_str, pstart);
if (pvalue == string::npos || pvalue > pend) break;
pvalue += value_str.length();
p2 = html_form.find("\"", pvalue);
val = html_form.substr(pvalue, p2 - pvalue);
p3 = val.find(""");
while (p3 != string::npos)
val.replace(p3, 6, "\"");
escape(val);
field_value = val;
break;
case T_RADIO:
pvalue = html_form.find(value_str, pstart);
if (pvalue == string::npos || pvalue > pend) break;
pvalue += value_str.length();
p1 = html_form.find("\"", pvalue);
if (p1 < pend) {
field_name.append(".")
.append(html_form.substr(pvalue, p1 - pvalue));
field_value.clear();
p2 = html_form.find(checkedeq, pstart);
if (p2 < pend) {
field_value = "Y";//checked;
html_form.erase(p2 - 1, checkedeq.length() + 1);
pend = html_form.find(">", pstart);
} else {
p2 = html_form.find(checked, pstart);
if (p2 < pend) {
field_value = "Y";//checked;
html_form.erase(p2 - 1, checked.length() + 1);
pend = html_form.find(">", pstart);
}
}
}
break;
case T_CHECKBOX:
pvalue = html_form.find(value_str, pstart);
if (pvalue != string::npos && pvalue < pend) {
pvalue += value_str.length();
p1 = html_form.find("\"", pvalue);
if (p1 < pend) {
field_name.append(".")
.append(html_form.substr(pvalue, p1 - pvalue));
}
}
field_value.clear();
p2 = html_form.find(checkedeq, pstart);
if (p2 < pend) {
field_value = "Y";//"ON";
html_form.erase(p2 - 1, checkedeq.length() + 1);
pend = html_form.find(">", pstart);
} else {
p2 = html_form.find(checked, pstart);
if (p2 < pend) {
field_value = "Y";//"ON";
html_form.erase(p2 - 1, checked.length() + 1);
pend = html_form.find(">", pstart);
} else {
p2 = html_form.find(" ON", pstart);
if (p2 < pend) {
field_value = "Y";//"ON";
html_form.erase(p2, 3);
pend = html_form.find(">", pstart);
}
}
}
break;
case T_AREA: //extract
pvalue = pend + 1;
p1 = html_form.find(textend_str, pvalue);
if (p1 == string::npos) break;
if (p1 > pvalue)
val = html_form.substr(pvalue, p1 - pvalue);
else
val.clear();
html_form.erase(pvalue, val.length());
escape(val);
field_value.assign(val);
break;
case T_SELECT:
p3 = html_form.find(end_sel_str, pstart);
if (p3 == string::npos) break;
p2 = html_form.find(selected, pstart);
if (p2 != string::npos && p2 < p3) {
pvalue = html_form.rfind(value_str, p2);
if (pvalue != string::npos) {
pvalue += value_str.length();
p1 = html_form.find("\"", pvalue);
if (p1 < p2)
field_value = html_form.substr(pvalue, p1 - pvalue);
}
}
break;
default:
break;
}
edit_txt.append(field_name).append(",");
edit_txt.append(field_value).append("\n");
name_values.push_back(NAME_VALUE(input_types[i].id, field_name, field_value));
ptype = html_form.find(input_types[i].txt, ptype+1);
}
}
}
void refresh_txt_custom_msg(void *)
{
txt_custom_msg->clear();
txt_custom_msg->add(edit_txt.c_str());
}
// called by web server thread
void get_html_vars(struct mg_connection *conn)
{
size_t p;
if (custom_select < 0) return;
edit_txt.assign("CUSTOM_FORM,")
.append(custom_pairs[custom_select].file_name)
.append("\n");
string field, line, val;
for (size_t n = 0; n < name_values.size(); n++)
name_values[n].value.clear();
for (size_t n = 0; n < name_values.size(); n++) {
field = name_values[n].name;
if (name_values[n].id == T_CHECKBOX ||
name_values[n].id == T_RADIO) {
if ((p = field.find(".")) != string::npos)
field.erase(p);
}
int fld_len = mg_get_var_len(conn, field.c_str());
char buff[fld_len + 1];
memset(buff, 0, sizeof(buff));
mg_get_var(conn, field.c_str(), buff, sizeof(buff));
switch (name_values[n].id) {
case T_RADIO :
p = name_values[n].name.find(".");
if (name_values[n].name.substr(p+1) == buff)
name_values[n].value = "Y";//checked;
line.assign(name_values[n].name)
.append(",")
.append(name_values[n].value);
break;
case T_CHECKBOX :
if (strstr(buff, "on") == buff || strstr(buff, "ON") == buff)
name_values[n].value = "Y";//"ON";
line.assign(name_values[n].name)
.append(",")
.append(name_values[n].value);
break;
case T_AREA://get html vars
val = buff;
escape(val);
name_values[n].value = val;
line.assign(name_values[n].name).append(",").append(val);
break;
// T_TEXT, T_PASSWORD, T_SELECT, T_DATE, T_DATETIME ...
default :
name_values[n].value = buff;
line.assign(name_values[n].name)
.append(",")
.append(name_values[n].value);
}
edit_txt.append(line).append("\n");
}
Fl::awake(refresh_txt_custom_msg);
}
// modify the html form with the value strings
void assign_values(string &html)
{
string nm, val, s1, s2, temp;
size_t p, p0, p1, p2, p3, pbeg, pend, pval, pnm;
for (size_t n = 0; n < name_values.size(); n++) {
switch (name_values[n].id) {
case T_TEXT : case T_PASSWORD : case T_NUMBER :
case T_DATE : case T_DTIME : case T_DTIME_LOCAL :
case T_TIME : case T_WEEK : case T_MONTH : case T_TEL : case T_EMAIL:
nm.assign("NAME=\"").append(name_values[n].name).append("\"");
pnm = html.find(nm);
if (pnm != string::npos) {
pnm += nm.length();
p1 = html.find(value_str, pnm);
p2 = html.find(">", pnm);
val = name_values[n].value;
unescape(val);
p3 = val.find("\"");
while (p3 != string::npos) {
val.replace(p3,1, """);
p3 = val.find("\"");
}
if (p1 < p2) {
p1 += value_str.length();
p2 = html.find("\"", p1);
html.replace(p1, p2 - p1, val);
} else {
temp.assign(" VALUE=\"").append(val).append("\"");
html.insert(p2, temp);
}
}
break;
case T_RADIO :
temp = name_values[n].name;
p = temp.find(".");
s1.assign(temp.substr(0, p));
s2.assign(temp.substr(p+1));
nm.assign(name_str).append(s1).append("\"");
val.assign(value_str).append(s2).append("\"");
pnm = html.find(nm);
while (pnm != string::npos) {
pbeg = html.rfind("<", pnm); // beginning of tag specifier
pend = html.find(">", pbeg); // end of tag specifier
pval = html.find(val, pbeg);
if (pval == string::npos || pval > pend) {
pnm = html.find(nm, pend);
continue;
}
// found name and value pair
if (name_values[n].value == checked ||
name_values[n].value == "Y")
html.insert(pend, string(" ").append(checked));
pend = html.find(">", pbeg);
pnm = html.find(nm, pend);
}
break;
case T_CHECKBOX :
nm.assign("NAME=\"").append(name_values[n].name).append("\"");
pnm = html.find(nm);
if (pnm != string::npos) {
pbeg = html.rfind("<", pnm);
pend = html.find(">", pbeg);
if (name_values[n].value == "ON" ||
name_values[n].value == "Y") {
html.insert(pend, string(" ").append(checked));
pend = html.find(">", pbeg);
}
}
break;
case T_AREA : //assign values
nm.assign("NAME=\"").append(name_values[n].name).append("\"");
pnm = html.find(nm);
if (pnm != string::npos) {
p1 = html.find(textend_str, pnm);
if (p1 == string::npos)
break;
p0 = html.rfind(">", p1) + 1;
val = name_values[n].value;
unescape(val);
if (p0 < p1) {
html.replace(p0, p1 - p0, val);
} else {
html.insert(p1, val);
}
}
break;
case T_SELECT :
nm.assign("NAME=\"").append(name_values[n].name).append("\"");
pnm = html.find(nm);
if (pnm != string::npos) {
p2 = html.find("", p1);
if (html.substr(p0, p1 - p0) == name_values[n].value) {
html.replace(p1+1, p3 - p1 - 1, " SELECTED");
} else
html.replace(p1+1, p3 - p1 - 1, "");
}
p0 = html.find(value_str, p0);
}
}
break;
default :
break;
}
}
}
void custom_editor(struct mg_connection *conn)
{
string html_edit = html_form;
size_t p = html_edit.find("