Пример #1
0
static NEOERR * _meta_save (const char *path, HDF *meta)
{
  NEOERR *err;
  char ftmp[_POSIX_PATH_MAX];
  char fpath[_POSIX_PATH_MAX];

  snprintf (ftmp, sizeof(ftmp), "%s,log.tmp", path);
  snprintf (fpath, sizeof(fpath), "%s,log", path);

  err = hdf_write_file (meta, ftmp);
  if (err) return nerr_pass (err);
  if (rename (ftmp, fpath) == -1)
  {
    unlink (ftmp);
    return nerr_raise_errno (NERR_IO, "Unable to rename file %s", ftmp);
  }

  return STATUS_OK;
}
Пример #2
0
void Hdf::write(const char *filename) const {
  CheckNeoError(hdf_write_file(getRaw(), filename));
}
Пример #3
0
int main(int argc, char **argv, char **envp)
{
    CGI *cgi;
    NEOERR *err;
    int ret;

    HASH *dbh;
    HASH *tplh;
    session_t *session = NULL;
    char *requri, *jsoncb;

    int (*data_handler)(CGI *cgi, HASH *dbh, session_t *session);
    void *lib;

    //sleep(20);
    mconfig_parse_file(SITE_CONFIG, &g_cfg);
    mtc_init(TC_ROOT"viki");

    ret = ltpl_init(&tplh);
    if (ret != RET_RBTOP_OK) {
        mtc_err("init templates error");
        mutil_redirect("初始化模板失败", TGT_SELF, URL_CLOSE, true);
        return ret;
    }

    ret = ldb_init(&dbh);
    if (ret != RET_RBTOP_OK) {
        mtc_err("init db error");
        mutil_redirect("初始化数据库失败", TGT_SELF, URL_CLOSE, true);
        return ret;
    }

    lib = dlopen(NULL, RTLD_NOW|RTLD_GLOBAL);
    if (lib == NULL) {
        mtc_err("possible? %s", dlerror());
        mutil_redirect("初始化库函数失败", TGT_SELF, URL_CLOSE, true);
        return 1;
    }
    
#ifndef DROP_FCGI
    cgiwrap_init_emu(NULL, &read_cb, &printf_cb, &write_cb, NULL, NULL, NULL);
    while (FCGI_Accept() >= 0) {
#endif
        cgiwrap_init_std(argc, argv, environ);
        err = cgi_init(&cgi, NULL);
        JUMP_NOK_CGI(err, response);
        err = cgi_parse(cgi);
        JUMP_NOK_CGI(err, response);

#ifdef NCGI_MODE
        hdf_set_value(cgi->hdf, PRE_REQ_URI_RW, "/csc/hc");
        hdf_set_value(cgi->hdf, PRE_COOKIE".uin", "1001");
        hdf_set_value(cgi->hdf, PRE_COOKIE".uname", "bigml");
        hdf_set_value(cgi->hdf, PRE_COOKIE".musn", "8Y]u0|v=*MS]U3J");
#endif
        
        ret = session_init(cgi->hdf, dbh, &session);
        if (ret != RET_RBTOP_OK) {
            mtc_err("init session failure");
            goto response;
        }

        requri = hdf_get_value(cgi->hdf, PRE_REQ_URI_RW, "NULL");
        if (mutil_client_attack(cgi->hdf, requri, LMT_CLI_ATTACK,
                                PERIOD_CLI_ATTACK)) {
            goto response;
        }
        
        ret = lfile_access_rewrited(cgi, dbh, session);
        if (ret != RET_RBTOP_OK) {
            goto response;
        }

        data_handler = lutil_get_data_handler(lib, cgi);
        if (data_handler == NULL) {
            mtc_err("get handler failure");
            ret = RET_RBTOP_NEXIST;
            goto response;
        }

        ret = (*data_handler)(cgi, dbh, session);
        
    response:
        if (cgi != NULL && cgi->hdf != NULL) {
#ifdef DEBUG_HDF
            hdf_write_file(cgi->hdf, TC_ROOT"hdf.viki");
#endif
            switch (CGI_REQ_TYPE(cgi)) {
            case CGI_REQ_HTML:
                if (CGI_REQ_METHOD(cgi) != CGI_REQ_GET) {
                    goto resp_ajax;
                }
                if (ret != RET_RBTOP_OK && ret == RET_RBTOP_NEXIST) {
                    cgi_redirect(cgi, "/404.html");
                } else {
                    ret = ltpl_render(cgi, tplh, session);
                    if (ret != RET_RBTOP_OK) {
                        if (ret == RET_RBTOP_NEXIST)
                            cgi_redirect(cgi, "/404.html");
                        else
                            cgi_redirect(cgi, "/503.html");
                    }
                }
                break;
            case CGI_REQ_AJAX:
            resp_ajax:
                ldb_opfinish_json(ret, cgi->hdf, NULL, 0);
                jsoncb = hdf_get_value(cgi->hdf, PRE_REQ_AJAX_FN, NULL);
                if (jsoncb != NULL) {
                    mjson_execute_hdf(cgi->hdf, jsoncb, session->tm_cache_browser);
                } else {
                    mjson_output_hdf(cgi->hdf, session->tm_cache_browser);
                }
                break;
            default:
                cgi_redirect(cgi, "/503.html");
                break;
            }
            cgi_destroy(&cgi);
            session_destroy(&session);
        }
#ifndef DROP_FCGI
    }
#endif

    ldb_destroy(dbh);
    ltpl_destroy(tplh);
    mconfig_cleanup(&g_cfg);
    return 0;
}
Пример #4
0
int main(int argc, char **argv, char **envp)
{
	CGI *cgi = NULL;
	NEOERR *err;
	mdb_conn *conn = NULL;

	/* skey, user, pass, return jsoncallback*/
	char *s, *u, *p, *r, *jcbk;
	/* keeptime(hours)  */
	int t;
	char tm[LEN_TM_GMT];
	
	//sleep(20);
	mtc_init("login");
	mconfig_parse_file(SITE_CONFIG, &g_cfg);
	mutil_wrap_fcgi(argc, argv, envp);
	if (mdb_init(&conn, DB_DSN) != MDB_ERR_NONE) {
		mtc_err("init db error %s", mdb_get_errmsg(conn));
		printf("Content-Type: text/html; charset=UTF-8\r\n\r\n");
		printf("{errcode: %d}", SAM_ERR_INIT);
		return 1;
	}
	
#ifndef DROP_FCGI
	while (FCGI_Accept() >= 0) {
#endif

		/*
		 * cgi init 
		 */
		err = cgi_init(&cgi, NULL);
		if (err != STATUS_OK) {
			mtc_err("init cgi error");
			printf("Content-Type: text/html; charset=UTF-8\r\n\r\n");
			printf("{errcode: %d}", SAM_ERR_INIT);
			goto opfinish;
		}
		err = cgi_parse(cgi);
		if (err != STATUS_OK) {
			mtc_err("parse cgi error");
			hdf_set_int_value(cgi->hdf, PRE_OUTPUT".errcode", SAM_ERR_PARSE);
			goto opfinish;
		}
#if 0
		if (mutil_client_attack_cookie(cgi->hdf, "login", 30, 60)) {
			mtc_err("client attack");
			hdf_set_int_value(cgi->hdf, PRE_OUTPUT".errcode", SAM_ERR_NEEDREST);
			goto opfinish;
		}
#endif

		u = hdf_get_value(cgi->hdf, PRE_COOKIE".samuser", NULL);
		s = hdf_get_value(cgi->hdf, PRE_COOKIE".samkey", NULL);
		if (s && u) {
			if (user_has_login(conn, u, s)) {
				hdf_set_copy(cgi->hdf, PRE_OUTPUT".samuser", PRE_COOKIE".samuser");
				hdf_set_copy(cgi->hdf, PRE_OUTPUT".samkey", PRE_COOKIE".samkey");
				goto done;
			}
		}
		
		u = hdf_get_value(cgi->hdf, PRE_QUERY".u", NULL);
		p = hdf_get_value(cgi->hdf, PRE_QUERY".p", NULL);
		if (!u || !p) {
			mtc_err("parameter miss %s %s", u, p);
			hdf_set_int_value(cgi->hdf, PRE_OUTPUT".errcode", SAM_ERR_NEEDINPUT);
			goto opfinish;
		}
		s = user_login_auth(conn, u, p);
		if (!s) {
			mtc_err("login error %s %s", u, p);
			hdf_set_int_value(cgi->hdf, PRE_OUTPUT".errcode", SAM_ERR_PASSW);
			goto opfinish;
		}

		cgiwrap_write(P3P_HEADER, strlen(P3P_HEADER));
		cgi_cookie_set(cgi, "samuser", u, NULL, SITE_DOMAIN, NULL, 1, 0);
		cgi_cookie_set(cgi, "samkey", s, NULL, SITE_DOMAIN, NULL, 1, 0);
#if 0
		t = hdf_get_int_value(cgi->hdf, PRE_QUERY".t", 0);
		mmisc_getdatetime_gmt(tm, sizeof(tm), "%A, %d-%b-%Y %T GMT", 60*60*t);
		cgi_cookie_set(cgi, "samkey", s, NULL, SITE_DOMAIN, tm, 1, 0);
#endif
		
		hdf_set_value(cgi->hdf, PRE_OUTPUT".samuser", u);
		hdf_set_value(cgi->hdf, PRE_OUTPUT".samkey", s);
		free(s);

	done:
		/*
		 * TODO set samkey, samuser to app's domain
		 * DONE this is done by jsonp
		 */
		hdf_set_value(cgi->hdf, PRE_OUTPUT".success", "1");
	opfinish:
		if (cgi) {
			r = hdf_get_value(cgi->hdf, PRE_QUERY".r", NULL);
			if (r) {
				cgi_redirect(cgi, r);
			} else {
				jcbk = hdf_get_value(cgi->hdf, PRE_QUERY".jsoncallback", NULL);
				if (jcbk != NULL) {
					mjson_execute_hdf(cgi->hdf, jcbk, 0);
				} else {
					mjson_output_hdf(cgi->hdf, 0);
				}
			}
#ifdef DEBUG_HDF
			hdf_write_file(cgi->hdf, HF_LOG_PATH"hdf.login");
#endif
			cgi_destroy(&cgi);
		}
		
#ifndef DROP_FCGI
 	} /* FCGI_Accept() */
#endif

	mdb_destroy(conn);
	return 0;
}
Пример #5
0
static NEOERR* rend_blog_index(HASH *dbh, HASH *tplh, int pageid, int *pgttr)
{
    CSPARSE *cs = NULL;
    HDF *hdf, *dhdf;
    STRING str;
    NEOERR *err = STATUS_OK;
    char fname[_POSIX_PATH_MAX];
    
    if (!dbh || !tplh) return nerr_raise(NERR_ASSERT, "paramter null");
    
    cs = (CSPARSE*)hash_lookup(tplh, "blog_index");
    dhdf = (HDF*)hash_lookup(tplh, "blog_index_hdf");
    if (!cs || !dhdf) return nerr_raise(LERR_MISS_TPL, "blog_index not found");

    err = hdf_init(&hdf);
    if (err != STATUS_OK) return nerr_pass(err);

    hdf_copy(hdf, NULL, dhdf);

    ltpl_prepare_rend(hdf, "layout.html");

    hdf_set_int_value(hdf, PRE_QUERY".pageid", pageid);

    err = blog_index_static_get(hdf, dbh);
    if (err != STATUS_OK) goto done;

    int ntt = hdf_get_int_value(hdf, PRE_OUTPUT".ntt", 0);
    int pgtt = hdf_get_int_value(hdf, PRE_OUTPUT".pgtt", 1);
    if (pgttr) *pgttr = pgtt;
    if (pageid == 0) {
        if (pgtt > 1) {
            err = hdf_set_int_value(hdf, PRE_OUTPUT".pgprev", pgtt-1);
            TRACE_NOK(err);

            if (ntt % BLOG_NUM_PERPAGE == 1) {
                err = rend_blog_index(dbh, tplh, pgtt-1, NULL);
                TRACE_NOK(err);

                if (pgtt > 2) {
                    /* origin 1.html's nex is index.html, change them into 2.html */
                    err = rend_blog_index(dbh, tplh, pgtt-2, NULL);
                    TRACE_NOK(err);
                }
            }
        }
    } else {
        if (pageid > 1 && pgtt > 1)
            hdf_set_int_value(hdf, PRE_OUTPUT".pgprev", pageid-1);
        if (pgtt == pageid+1)
            hdf_set_value(hdf, PRE_OUTPUT".pgnext", "index");
        else if (pgtt > pageid)
            hdf_set_int_value(hdf, PRE_OUTPUT".pgnext", pageid+1);
    }
    
    cs->hdf = hdf;

    string_init(&str);
    err = cs_render(cs, &str, mcs_strcb);
    if (err != STATUS_OK) goto done;

    if (pageid == 0)
        snprintf(fname, sizeof(fname), "%sindex.html", PATH_BLOG);
    else
        snprintf(fname, sizeof(fname), "%s%d.html", PATH_BLOG, pageid);
    
    err = mutil_makesure_dir(fname);
    if (err != STATUS_OK) goto done;
    
    err = mcs_str2file(str, fname);
    if (err != STATUS_OK) goto done;

#ifdef DEBUG_HDF
    hdf_write_file(hdf, TC_ROOT"hdf.blg.index");
#endif

done:
    hdf_destroy(&hdf);
    cs->hdf = NULL;
    string_clear(&str);
    return nerr_pass(err);
}
Пример #6
0
int main(int argc, char **argv, char **envp)
{
    CGI *cgi;
    NEOERR *err = STATUS_OK;

    HASH *dbh, *tplh, *evth;
    session_t *session = NULL;
    char *temps;
    int http_max_upload;

    NEOERR* (*data_handler)(CGI *cgi, HASH *dbh, HASH *evth, session_t *session);
    void *lib;
    
    //sleep(20);
    mutil_makesure_coredump();
    mtc_init(TC_ROOT"viki");

    err = lerr_init();
    DIE_NOK_CGI(err);
    
    err = mcfg_parse_file(SITE_CONFIG, &g_cfg);
    DIE_NOK_CGI(err);

    err = mtpl_set_tplpath(PATH_TPL);
    DIE_NOK_CGI(err);

    err = mtpl_InConfigRend_init(PATH_TPL"/config/email", "email", &g_datah);
    DIE_NOK_CGI(err);

    err = mtpl_InConfigRend_init(PATH_TPL"/config/inbox", "inbox", &g_datah);
    DIE_NOK_CGI(err);

    err = ltpl_init(&tplh, NULL);
    DIE_NOK_CGI(err);

    err = hash_insert(g_datah, "runtime_templates", (void*)tplh);
    DIE_NOK_CGI(err);

    err = ldb_init(&dbh);
    DIE_NOK_CGI(err);

    err = levt_init(&evth);
    DIE_NOK_CGI(err);

    lib = dlopen(NULL, RTLD_NOW|RTLD_GLOBAL);
    if (!lib) {
        err = nerr_raise(NERR_SYSTEM, "dlopen %s", dlerror());
        DIE_NOK_CGI(err);
    }
    
#ifdef USE_FASTCGI
    cgiwrap_init_emu(NULL, &read_cb, &printf_cb, &write_cb, NULL, NULL, NULL);
    while (FCGI_Accept() >= 0) {
#endif
        cgiwrap_init_std(argc, argv, environ);
        err = cgi_init(&cgi, NULL);
        if (err != STATUS_OK) goto response;
        
        http_max_upload = hdf_get_int_value(g_cfg, PRE_CONFIG".http_max_upload", 0);
        if (http_max_upload > 0) {
            err = mcs_register_upload_parse_cb(cgi, &http_max_upload);
            if (err != STATUS_OK) goto response;
        }
        
        err = cgi_parse(cgi);
        if (err != STATUS_OK) goto response;

#ifdef NCGI_MODE
        hdf_set_value(cgi->hdf, PRE_REQ_URI_RW, "/image/member/pic");
        hdf_set_value(cgi->hdf, PRE_COOKIE".uin", "1001");
        hdf_set_value(cgi->hdf, PRE_COOKIE".uname", "bigml");
        hdf_set_value(cgi->hdf, PRE_COOKIE".musn", "8Y]u0|v=*MS]U3J");
        hdf_set_value(cgi->hdf, PRE_QUERY".ip", "222.247.56.14");
        hdf_set_value(cgi->hdf, PRE_QUERY".to", "cj_BXTSJ");
        hdf_set_value(cgi->hdf, PRE_QUERY".s", "koldddd");
        hdf_set_value(cgi->hdf, PRE_QUERY".JsonCallback", "Ape.transport.read");
        hdf_set_value(cgi->hdf, PRE_QUERY".type", "phone");
        hdf_set_value(cgi->hdf, PRE_QUERY".mid", "485010473");
#endif
        
        err = session_init(cgi, dbh, &session);
        if (err != STATUS_OK) goto response;

        if (lutil_client_attack(cgi->hdf, session, "lcs_uname")) {
            err = nerr_raise(LERR_ATTACK, "%s need a rest, babey!", session->dataer);
            goto response;
        }
        
        if ((data_handler = lutil_get_data_handler(lib, cgi, session)) == NULL) {
            err = nerr_raise(LERR_MISS_DATA, "dataer %s not found", session->dataer);
            goto response;
        }

        err = (*data_handler)(cgi, dbh, evth, session);
        
    response:
        if (cgi != NULL && cgi->hdf != NULL) {
            lerr_opfinish_json(err, cgi->hdf);

            if (!session) session = session_default();
            switch (session->reqtype) {
            case CGI_REQ_HTML:
                err = ltpl_render(cgi, tplh, session);
                if (err != STATUS_OK) {
                    SAFE_FREE(session->render);
                    if (nerr_match(err, LERR_MISS_TPL)) {
                        session->render = strdup("404");
                    } else {
                        session->render = strdup("503");
                    }
                    TRACE_NOK(err);
                    err = ltpl_render(cgi, tplh, session);
                    TRACE_NOK(err);
                }
                break;
            case CGI_REQ_AJAX:
            resp_ajax:
                temps = hdf_get_value(cgi->hdf, PRE_REQ_AJAX_FN, NULL);
                if (temps != NULL) {
                    mjson_execute_hdf(cgi->hdf, temps, session->tm_cache_browser);
                } else {
                    mjson_output_hdf(cgi->hdf, session->tm_cache_browser);
                }
                break;
            case CGI_REQ_IMAGE:
                temps = hdf_get_value(cgi->hdf, PRE_OUTPUT".302", NULL);
                if (temps) {
                    cgi_redirect_uri(cgi, temps);
                } else if (session->data) {
                    mimg_output(session->data);
                    session->data = NULL;
                } else goto resp_ajax;
                break;
            default:
                cgi_redirect(cgi, "/503.html");
                break;
            }
            
#ifdef DEBUG_HDF
            hdf_write_file(cgi->hdf, TC_ROOT"hdf.viki");
#endif
            
            cgi_destroy(&cgi);
            session_destroy(&session);
            cgi = NULL;
            session = NULL;
        }
#ifdef USE_FASTCGI
    }
#endif

    levt_destroy(evth);
    ldb_destroy(dbh);
    ltpl_destroy(tplh);
    mtpl_InConfigRend_destroy(g_datah);
    mcfg_cleanup(&g_cfg);

    return 0;
}
Пример #7
0
NEOERR* ltpl_parse_file(HASH *dbh, HASH *evth,
                        void *lib, char *dir, char *name, HASH *outhash)
{
    char *tp = NULL, *tpl = NULL, *val = NULL;
    HDF *node = NULL, *dhdf = NULL, *child = NULL, *thdf = NULL;
    CSPARSE *cs = NULL;
    STRING str;
    char fname[_POSIX_PATH_MAX], tok[64], *outfile;
    NEOERR* (*data_handler)(HDF *hdf, HASH *dbh, HASH *evth);
    NEOERR *err;
    
    memset(fname, 0x0, sizeof(fname));
    snprintf(fname, sizeof(fname), "%s/%s", dir, name);
    err = hdf_init(&node);
    if (err != STATUS_OK) return nerr_pass(err);
 
    err = hdf_read_file(node, fname);
    if (err != STATUS_OK) return nerr_pass(err);

    child = hdf_obj_child(node);
    while (child != NULL) {
        mtc_dbg("parse node %s", hdf_obj_name(child));
        string_init(&str);

        val = mcs_obj_attr(child, "merge");
        if (val) {
            ULIST *list;
            string_array_split(&list, val, ",", 10);
            ITERATE_MLIST(list) {
                snprintf(fname, sizeof(fname), "%s/%s",
                         dir, neos_strip((char*)list->items[t_rsv_i]));
                err = hdf_init(&dhdf);
                JUMP_NOK(err, wnext);
                err = hdf_read_file(dhdf, fname);
                JUMP_NOK(err, wnext);
                err = hdf_copy(child, NULL, dhdf);
                JUMP_NOK(err, wnext);
            }
            uListDestroy(&list, ULIST_FREE);
        }

        /*
         * can't use dataset directly, because we'll destroy the whole node
         */
        err = hdf_init(&dhdf);
        JUMP_NOK(err, wnext);
        err = hdf_get_node(child, PRE_CFG_DATASET, &thdf);
        JUMP_NOK(err, wnext);
        err = hdf_copy(dhdf, NULL, thdf);
        JUMP_NOK(err, wnext);
        
        err = cs_init(&cs, dhdf);
        JUMP_NOK(err, wnext);

        hdf_set_value(cs->hdf, "hdf.loadpaths.tpl", PATH_TPL);
        hdf_set_value(cs->hdf, "hdf.loadpaths.local", dir);

        err = cgi_register_strfuncs(cs);
        JUMP_NOK(err, wnext);
        err = mcs_register_bitop_functions(cs);
        JUMP_NOK(err, wnext);
        err = mcs_register_mkd_functions(cs);
        JUMP_NOK(err, wnext);
        err = mcs_register_string_uslice(cs);
        JUMP_NOK(err, wnext);

        tpl = hdf_get_value(child, PRE_CFG_LAYOUT, "null.html");
        snprintf(fname, sizeof(fname), "%s/%s", PATH_TPL, tpl);
        err = cs_parse_file(cs, fname);
        JUMP_NOK(err, wnext);

        if (outhash != NULL) {
            /*
             * store template for rend stage use
             */
            hdf_set_value(cs->hdf, PRE_RESERVE"."PRE_CFG_LAYOUT, tpl);
            
            /*
             * strdup the key, baby, because we'll free the hdf later
             */
            err = hash_insert(outhash, (void*)strdup(hdf_obj_name(child)), (void*)cs);
            JUMP_NOK(err, wnext);

            snprintf(tok, sizeof(tok), "%s_hdf", hdf_obj_name(child));
            err = hash_insert(outhash, (void*)strdup(tok), (void*)cs->hdf);
            JUMP_NOK(err, wnext);
        }

        if ((outfile = hdf_get_value(child, PRE_CFG_OUTPUT, NULL)) != NULL) {
            ltpl_prepare_rend(cs->hdf, tpl);
                
            /*
             * get_data
             */
            val = hdf_get_value(child, PRE_CFG_DATAER, NULL);
            if (val != NULL && lib) {
                data_handler = dlsym(lib, val);
                if( (tp = dlerror()) != NULL) {
                    mtc_err("%s", tp);
                    //continue;
                } else {
                    err = (*data_handler)(cs->hdf, dbh, evth);
                    TRACE_NOK(err);
                }
            }

            err = cs_render(cs, &str, mcs_strcb);
            JUMP_NOK(err, wnext);

            /*
             * produce output filename
             */
            val = mcs_hdf_attr(child, PRE_CFG_OUTPUT, "ftime");
            if (val) {
                char tm[LEN_TM];
                mutil_getdatetime(tm, sizeof(tm), val, 0);
                outfile = mstr_repstr(1, outfile, "$ftime$", tm);
            }
            snprintf(fname, sizeof(fname), PATH_DOC"%s", outfile);

            /*
             * output file
             */
            err = mfile_makesure_dir(fname);
            JUMP_NOK(err, wnext);

            err = mcs_str2file(str, fname);
            JUMP_NOK(err, wnext);
#ifdef DEBUG_HDF
            snprintf(fname, sizeof(fname), "%s/hdf.%s",
                     TC_ROOT, hdf_obj_name(child));
            hdf_write_file(child, fname);
#endif
        }

    wnext:
        if (cs != NULL && outhash == NULL)
            cs_destroy(&cs);
        string_clear(&str);
        child = hdf_obj_next(child);
    }
        
    if (node != NULL) hdf_destroy(&node);

    return STATUS_OK;
}
Пример #8
0
NEOERR* ltpl_parse_file(HASH *dbh, void *lib, char *dir, char *name, HASH *outhash)
{
    char *tp = NULL, *tpl = NULL, *val = NULL;
    HDF *node = NULL, *dhdf = NULL, *child = NULL;
    CSPARSE *cs = NULL;
    STRING str;
    char fname[_POSIX_PATH_MAX], tok[64];
    NEOERR* (*data_handler)(HDF *hdf, HASH *dbh);
    NEOERR *err;
    
    memset(fname, 0x0, sizeof(fname));
    snprintf(fname, sizeof(fname), "%s/%s", dir, name);
    err = hdf_init(&node);
    if (err != STATUS_OK) return nerr_pass(err);

    err = hdf_read_file(node, fname);
    if (err != STATUS_OK) return nerr_pass(err);

    child = hdf_obj_child(node);
    while (child != NULL) {
        mtc_dbg("parse node %s", hdf_obj_name(child));
        string_init(&str);

        val = mutil_obj_attr(child, "merge");
        if (val) {
            snprintf(fname, sizeof(fname), "%s/%s", dir, val);
            err = hdf_init(&dhdf);
            JUMP_NOK(err, wnext);
            err = hdf_read_file(dhdf, fname);
            JUMP_NOK(err, wnext);
            err = hdf_copy(child, NULL, dhdf);
            JUMP_NOK(err, wnext);
        }
        
        err = cs_init(&cs, hdf_get_obj(child, PRE_CFG_DATASET));
        JUMP_NOK(err, wnext);
            
        hdf_set_value(cs->hdf, "hdf.loadpaths.local", dir);

        err = cgi_register_strfuncs(cs);
        JUMP_NOK(err, wnext);
        err = mcs_register_bitop_functions(cs);
        JUMP_NOK(err, wnext);
        err = mcs_register_mkd_functions(cs);
        JUMP_NOK(err, wnext);

        tpl = hdf_get_value(child, PRE_CFG_LAYOUT, "null.html");
        snprintf(fname, sizeof(fname), "%s/%s", PATH_TPL, tpl);
        err = cs_parse_file(cs, fname);
        JUMP_NOK(err, wnext);

        if (outhash != NULL) {
            /*
             * strdup the key, baby, because we'll free the hdf later
             */
            err = hash_insert(outhash, (void*)strdup(hdf_obj_name(child)), (void*)cs);
            JUMP_NOK(err, wnext);
            if (hdf_get_obj(child, PRE_CFG_DATASET)) {
                err = hdf_init(&dhdf);
                JUMP_NOK(err, wnext);
                err = hdf_copy(dhdf, NULL, hdf_get_obj(child, PRE_CFG_DATASET));
                JUMP_NOK(err, wnext);
                snprintf(tok, sizeof(tok), "%s_hdf", hdf_obj_name(child));
                err = hash_insert(outhash, (void*)strdup(tok), (void*)dhdf);
                JUMP_NOK(err, wnext);
            }
        }
            
        if (hdf_get_value(child, PRE_CFG_OUTPUT, NULL) != NULL) {
            ltpl_prepare_rend(hdf_get_obj(child, PRE_CFG_DATASET), tpl);
                
            /*
             * get_data
             */
            val = hdf_get_value(child, PRE_CFG_DATAER, NULL);
            if (val != NULL && lib) {
                data_handler = dlsym(lib, val);
                if( (tp = dlerror()) != NULL) {
                    mtc_err("%s", tp);
                    //continue;
                } else {
                    err = (*data_handler)(hdf_get_obj(child, PRE_CFG_DATASET), dbh);
                    TRACE_NOK(err);
                }
            }
                
            err = cs_render(cs, &str, mcs_strcb);
            JUMP_NOK(err, wnext);
                
            snprintf(fname, sizeof(fname), PATH_DOC"%s",
                     hdf_get_value(child, PRE_CFG_OUTPUT, "null.html"));
            err = mutil_makesure_dir(fname);
            JUMP_NOK(err, wnext);

            err = mcs_str2file(str, fname);
            JUMP_NOK(err, wnext);
#ifdef DEBUG_HDF
            snprintf(fname, sizeof(fname), "%s/hdf.%s",
                     TC_ROOT, hdf_obj_name(child));
            hdf_write_file(child, fname);
#endif
        }

    wnext:
        if (cs != NULL && outhash == NULL)
            cs_destroy(&cs);
        string_clear(&str);
        child = hdf_obj_next(child);
    }
        
    if (node != NULL) hdf_destroy(&node);

    return STATUS_OK;
}
Пример #9
0
int main(int argc, char **argv, char **envp)
{
	CGI *cgi = NULL;
	NEOERR *err;
	mdb_conn *conn = NULL;

	/* skey, user, pass, return jsoncallback*/
	char *s, *u, *r, *jcbk;
	
	//sleep(20);
	mtc_init("logout");
	mconfig_parse_file(SITE_CONFIG, &g_cfg);
	mutil_wrap_fcgi(argc, argv, envp);
	if (mdb_init(&conn, DB_DSN) != MDB_ERR_NONE) {
		mtc_err("init db error %s", mdb_get_errmsg(conn));
		printf("Content-Type: text/html; charset=UTF-8\r\n\r\n");
		printf("{errcode: %d}", SAM_ERR_INIT);
		return 1;
	}
	
#ifndef DROP_FCGI
	while (FCGI_Accept() >= 0) {
#endif

		/*
		 * cgi init 
		 */
		err = cgi_init(&cgi, NULL);
		if (err != STATUS_OK) {
			mtc_err("init cgi error");
			printf("Content-Type: text/html; charset=UTF-8\r\n\r\n");
			printf("{errcode: %d}", SAM_ERR_INIT);
			goto opfinish;
		}
		err = cgi_parse(cgi);
		if (err != STATUS_OK) {
			mtc_err("parse cgi error");
			hdf_set_int_value(cgi->hdf, PRE_OUTPUT".errcode", SAM_ERR_PARSE);
			goto opfinish;
		}

		u = hdf_get_value(cgi->hdf, PRE_COOKIE".samuser", NULL);
		s = hdf_get_value(cgi->hdf, PRE_COOKIE".samkey", NULL);
		if (s && u) {
			if (user_has_login(conn, u, s)) {
				user_logout(conn, u);
				cgi_cookie_clear(cgi, "samuser", SITE_DOMAIN, NULL);
				cgi_cookie_clear(cgi, "samkey", SITE_DOMAIN, NULL);
				hdf_set_value(cgi->hdf, PRE_OUTPUT".success", "1");
				goto opfinish;
			}
		}
		hdf_set_int_value(cgi->hdf, PRE_OUTPUT".errcode", SAM_ERR_NOTLOGIN);
	opfinish:
		if (cgi) {
			r = hdf_get_value(cgi->hdf, PRE_QUERY".r", NULL);
			if (r) {
				cgi_redirect(cgi, r);
			} else {
				jcbk = hdf_get_value(cgi->hdf, PRE_QUERY".jsoncallback", NULL);
				if (jcbk != NULL) {
					mjson_execute_hdf(cgi->hdf, jcbk, 0);
				} else {
					mjson_output_hdf(cgi->hdf, 0);
				}
			}
#ifdef DEBUG_HDF
			hdf_write_file(cgi->hdf, HF_LOG_PATH"hdf.logout");
#endif
			cgi_destroy(&cgi);
		}
		
#ifndef DROP_FCGI
 	} /* FCGI_Accept() */
#endif

	mdb_destroy(conn);
	return 0;
}