static Template *template_load(gchar *filename, guint tmplid) { Template *tmpl; FILE *fp; gchar buf[BUFFSIZE]; if ((fp = g_fopen(filename, "rb")) == NULL) { FILE_OP_ERROR(filename, "fopen"); return NULL; } tmpl = g_new(Template, 1); tmpl->tmplid = tmplid; tmpl->name = NULL; tmpl->to = NULL; tmpl->cc = NULL; tmpl->bcc = NULL; tmpl->replyto = NULL; tmpl->subject = NULL; tmpl->value = NULL; while (fgets(buf, sizeof(buf), fp) != NULL) { if (buf[0] == '\n') break; else if (!g_ascii_strncasecmp(buf, "Name:", 5)) tmpl->name = g_strdup(g_strstrip(buf + 5)); else if (!g_ascii_strncasecmp(buf, "To:", 3)) tmpl->to = g_strdup(g_strstrip(buf + 3)); else if (!g_ascii_strncasecmp(buf, "Cc:", 3)) tmpl->cc = g_strdup(g_strstrip(buf + 3)); else if (!g_ascii_strncasecmp(buf, "Bcc:", 3)) tmpl->bcc = g_strdup(g_strstrip(buf + 4)); else if (!g_ascii_strncasecmp(buf, "Reply-To:", 9)) tmpl->replyto = g_strdup(g_strstrip(buf + 9)); else if (!g_ascii_strncasecmp(buf, "Subject:", 8)) tmpl->subject = g_strdup(g_strstrip(buf + 8)); } if (!tmpl->name) { g_warning("wrong template format\n"); template_free(tmpl); return NULL; } tmpl->value = file_read_stream_to_str(fp); if (!tmpl->value) { g_warning("cannot read template body\n"); template_free(tmpl); return NULL; } fclose(fp); return tmpl; }
void template_clear_config(GSList *tmpl_list) { GSList *cur; Template *tmpl; for (cur = tmpl_list; cur != NULL; cur = cur->next) { tmpl = (Template *)cur->data; template_free(tmpl); } g_slist_free(tmpl_list); }
int things_list(struct http_request *req) { int rc; char *zErrMsg = 0; char *query = "SELECT * FROM found ORDER BY last DESC"; template_t tmpl; attrlist_t attributes; struct timespec when; buf = kore_buf_create(mb); if(!thing) thing = hashmap_new(); // load template from assets template_load (asset_things_list_html, asset_len_things_list_html, &tmpl); // initialise attribute list attributes = attrinit(); if( ! parse_datetime(&when, "now", NULL) ) kore_log(LOG_ERR,"parse-datetime error"); else { struct tm *tt; tt = localtime (&when.tv_sec); mktime(tt); strftime(line, ml, "Dowse :: %d %m %Y - %H:%M:%S", tt); attrcat(attributes, "title", line); } sqlquery(query, things_list_cb, attributes); template_apply(&tmpl, attributes, buf); http_response(req, 200, buf->data, buf->offset); template_free(&tmpl); attrfree(attributes); kore_buf_free(buf); return (KORE_RESULT_OK); }
int thing_show(struct http_request *req) { int rc; template_t tmpl; attrlist_t attributes; char *zErrMsg = 0; char *macaddr; http_populate_get(req); // we shouldn't free the result in macaddr if (http_argument_get_string(req, "macaddr", &macaddr)) kore_log(LOG_DEBUG, "thing_show macaddr %s",macaddr); else kore_log(LOG_ERR,"thing_show get argument error"); // prepare query snprintf(line,ml,"SELECT * FROM found WHERE macaddr = '%s'",macaddr); // allocate output buffer buf = kore_buf_create(mb); // load template template_load (asset_thing_show_html, asset_len_thing_show_html, &tmpl); attributes = attrinit(); attrcat(attributes, "title", "Dowse information panel"); // SQL query sqlquery(line, thing_show_cb, attributes); template_apply(&tmpl,attributes,buf); http_response(req, 200, buf->data, buf->offset); template_free(&tmpl); attrfree(attributes); kore_buf_free(buf); return (KORE_RESULT_OK); }
int main(void) { unsigned int logged_in = 0; char *username, *password = { NULL }; cookie_t *cookies = cookies_init(); cookie_t *our_cookie = cookie(COOKIE_NAME); char *template_path = "templates/portal"; cookie_t *c = NULL; // Check for session cookie if (our_cookie) { // && cookie_valid(our_cookie)) { logged_in = 1; c = our_cookie; c->expires = 3600; } else { // New connection or expired... get_params(); username = param("username"); password = param("password"); logged_in = CheckUserCredentials(username, password); if (logged_in) { c = cookie_create(username, password); c->expires = 3600; } else WebBork("HERE"); } cookie_add(cookies, c); content_type_cookies("text/html", cookies); template_t **t = template_init(); template_input(t, "USERNAME", username); template_output(template_path, t); template_free(t); exit(EXIT_SUCCESS); }