Esempio n. 1
0
int 
Server_react(Server *server, void *socket, zmq_msg_t *msg)
{
	size_t cmdlen, msglen, datalen;
	char *eol, *message, *command;
	TCLIST *args;
	Command *cmd;
	void *data = NULL;
	
	message = zmq_msg_data(msg);
	msglen = zmq_msg_size(msg);
	zmq_msg_close(msg);
	
	eol = strchr(message, '\n');
	
	if (eol) {
		cmdlen = 1 + (eol - message);
		command = (char *)malloc((cmdlen + 1) * sizeof(char));
		memset(command, 0, cmdlen + 1);
		memcpy(command, message, cmdlen);
		args = tcstrsplit(tcstrsqzspc(command), " \t");
		cmd = lookup_command(tclistval2(args, 0));
		if (cmd == NULL) {
			ERROR("protocol error: %s", command);
			send(socket, "-INVALID_COMMAND");
			return 0;
		}
		if (msglen > cmdlen) {
			/* data size will always be last arg */
			datalen = tcatoi((char *)tclistval2(args, tclistnum(args) - 1));
			if ((datalen + cmdlen) != msglen) {
				ERROR("error receiving data or invalid data size");
				send(socket, "-INCORRECT_DATA_SIZE");
				return 0;
			}
			data = malloc(sizeof(char) * (datalen + 1));
			if (data == NULL) {
				ERROR("out of memory");
				send(socket, "-OUT_OF_MEMORY");
				return 0;
			}
			memset(data + datalen, 0, 1);
			memcpy(data, message + cmdlen, datalen);
			DEBUG("data received: %s", data);
		}
		cmd->fn(server, socket, args, data);
		return 1;
	}
	else {
		ERROR("request missing eol");
		send(socket, "-MISSING_EOL");
	}

	return 0;
}
Esempio n. 2
0
void parse_conf()
{
	FILE   *fp;
	TCLIST *data;
	TCMAP  *map; 
	
	char       *key, *val, buf[256];
	const char *value;
		
	fp = fopen("./conf/blog.conf", "r");
	if(!fp) exit(0) ;
	
	map = tcmapnew();
	memset(buf, 0, sizeof(buf));

	while ((fgets(buf, 255, fp)) != NULL)
	{
		trim(buf);
		if(strlen(buf) == 0) continue;
		if(buf[0] == '#') continue;
		
		data = explode("=", buf);
		if(tclistnum(data) == 2)
		{
			key = strdup(tclistval2(data, 0));
			val = strdup(tclistval2(data, 1));
			
			trim(key);
			trim(val);

			tcmapput(map, key, strlen(key), val, strlen(val));
			safe_free(key);
			safe_free(val);
		}
		
		tclistdel(data);
		memset(buf, 0, sizeof(buf));
	}
	fclose(fp);
	
	VALIDATE_INT("blog_page_size", value, conf.page.blog)
	VALIDATE_INT("admin_page_size", value, conf.page.admin)
	VALIDATE_INT("comment_page_size", value, conf.page.comment)

	VALIDATE_STRINT("db", value, conf.path)
	VALIDATE_STRINT("username", value, conf.username)
	VALIDATE_STRINT("password", value, conf.password)


	tcmapdel(map);
}
Esempio n. 3
0
void panel_article_delete(struct env_t *_SERVER)
{
	int        rc;
	char       *addr;
	const char *id;
	
	sqlite3_stmt *stmt;

	id = tclistval2(_SERVER->_GET, 3);
	if(id == NULL || !is_digit(id))
	{
		puts("Content-type: text/html\r\n\r\n<script>alert('请您选择要删除的文章');window.location.href='/panel-article-list-all.html';</script>");
		return ;
	}
	
	//tcmapout2(maps, id);
	
	rc = sqlite3_prepare(db, "DELETE FROM article WHERE art_id = ?;", -1, &stmt, NULL); 

	sqlite3_bind_int(stmt, 1, atoi(id));
	
	sqlite3_step(stmt); 
	sqlite3_finalize(stmt); 
	
	addr = getenv("HTTP_REFERER");
	if(addr)
		printf("Content-type: text/html\r\n\r\n<script>alert('删除已完成');window.location.href='%s';</script>", addr);
	else
		puts("Content-type: text/html\r\n\r\n<script>alert('删除已完成');window.location.href='/panel-article-list-all.html';</script>");

}
Esempio n. 4
0
File: sandbox.c Progetto: titita/npa
static int sandbox_listen2(TCLIST *event_list, NOTIFY_HANDLER handler, void *listener)
{
	int idx;
	const char *type;

	if (!event_list || !handler)
		return -1;

	idx = 0;
	while (idx < tclistnum(event_list)) {
		type = tclistval2(event_list, idx);
		sandbox_listen(type, handler, listener);
		idx++;
	}

	return 0;
}
Esempio n. 5
0
static void rangeout_test(void *db, const char *command, int num, int vsiz,
			int batch, unsigned int seed)
{
	TCADB *adb = db;
	struct keygen keygen;
	TCLIST *args = tclistnew();
	char start_key[KEYGEN_PREFIX_SIZE + 1];
	char max[100];
	char end_key[KEYGEN_PREFIX_SIZE + 1];
	char binc[2];

	keygen_init(&keygen, seed);

	keygen_prefix(&keygen, start_key);
	sprintf(max, "%d", batch);
	keygen_prefix(&keygen, end_key);
	end_key[KEYGEN_PREFIX_SIZE - 1] = '-' + 1;
	sprintf(binc, "0");

	tclistpush2(args, start_key);
	tclistpush2(args, max);
	tclistpush2(args, end_key);
	tclistpush2(args, binc);

	while (1) {
		TCLIST *recs;

		recs = do_tcadbmisc(adb, command, args);
		if (tclistnum(recs) == 0)
			break;

		if (debug) {
			const char *num_recs = tclistval2(recs, 0);
			num -= atoi(num_recs);
			if (num != 0 && atoi(num_recs) != batch)
				die("Unexpected number of records are deleted");
		}

		tclistdel(recs);
	}
	if (debug && num != 0)
		die("Unexpected number of records are deleted");

	tclistdel(args);
}
Esempio n. 6
0
static void range_atomic_test(void *db, int num, int vsiz, int batch,
			unsigned int seed)
{
	TCADB *adb = db;
	struct keygen keygen;
	TCLIST *args = tclistnew();
	char start_key[KEYGEN_PREFIX_SIZE + 1];
	char max[100];
	char end_key[KEYGEN_PREFIX_SIZE + 1];
	char binc[2];

	keygen_init(&keygen, seed);

	keygen_prefix(&keygen, start_key);
	sprintf(max, "%d", batch);
	keygen_prefix(&keygen, end_key);
	end_key[KEYGEN_PREFIX_SIZE - 1] = '-' + 1;
	sprintf(binc, "0");

	tclistpush2(args, start_key);
	tclistpush2(args, max);
	tclistpush2(args, end_key);
	tclistpush2(args, binc);

	while (1) {
		TCLIST *recs;
		int num_recs;

		recs = do_tcadbmisc(adb, "range_atomic", args);
		num_recs = tclistnum(recs) / 2;
		if (!num_recs)
			break;

		check_records(recs, &keygen, vsiz, num < batch ? num : batch);
		tclistover2(args, 0, tclistval2(recs, 2 * (num_recs - 1)));
		tclistdel(recs);
		num -= num_recs;
	}
	if (debug && num)
		die("Unexpected record num: %d", num);

	tclistdel(args);
}
Esempio n. 7
0
void panel_article_upload(struct env_t *_SERVER)
{
	const char *path, *file;

	if(tclistnum(_SERVER->_FILES) == 0)
	{
		puts("Content-type: text/html\r\n\r\n{\"error\":1, \"message\":\"文件上传失败\"}");
		return ;
	}
	
	file = tclistval2(_SERVER->_FILES, 0);
	path = tcmapget2(_SERVER->_COOKIE, "file");
	if(path == NULL)
	{
		printf("Set-Cookie: file=%s;\r\nContent-type: text/html\r\n\r\n{\"error\":0, \"url\":\"/files/%s\"}", file, file);
		return ;
	}

	printf("Set-Cookie: file=%s|%s;\r\nContent-type: text/html\r\n\r\n{\"error\":0, \"url\":\"/files/%s\"}", path, file, file);
}
Esempio n. 8
0
/* kwicimpl */
JNIEXPORT jobjectArray JNICALL Java_tokyocabinet_TDBQRY_kwicimpl
(JNIEnv *env, jobject self, jobjectArray cols, jstring name, jint width, jint opts){
  if(!cols){
    throwillarg(env);
    return NULL;
  }
  jclass clsstring = name ?
    (*env)->GetObjectClass(env, name) : (*env)->FindClass(env, CLSSTRING);
  TDBQRY *qry = (TDBQRY *)(intptr_t)(*env)->GetLongField(env, self, tdbqry_fid_ptr);
  jsize cnum = (*env)->GetArrayLength(env, cols);
  TCMAP *tcols = tcmapnew2(cnum + 1);
  jboolean icc = false;
  const char *cbuf = NULL;
  int csiz = 0;
  if(name){
    cbuf = (*env)->GetStringUTFChars(env, name, &icc);
    if(!cbuf){
      throwoutmem(env);
      return NULL;
    }
    csiz = strlen(cbuf);
  }
  cnum--;
  for(int i = 0; i < cnum; i += 2){
    name = (*env)->GetObjectArrayElement(env, cols, i);
    jboolean icn;
    jbyte *nbuf = (*env)->GetByteArrayElements(env, name, &icn);
    if(!nbuf){
      throwoutmem(env);
      return NULL;
    }
    int nsiz = (*env)->GetArrayLength(env, name);
    jobject val = (*env)->GetObjectArrayElement(env, cols, i + 1);
    jboolean icv;
    jbyte *vbuf = (*env)->GetByteArrayElements(env, val, &icv);
    if(!vbuf){
      throwoutmem(env);
      return false;
    }
    int vsiz = (*env)->GetArrayLength(env, val);
    if(!cbuf || (nsiz == csiz && !memcmp(nbuf, cbuf, nsiz)))
      tcmapputkeep(tcols, nbuf, nsiz, vbuf, vsiz);
    if(icv) (*env)->ReleaseByteArrayElements(env, val, vbuf, JNI_ABORT);
    if(icn) (*env)->ReleaseByteArrayElements(env, name, nbuf, JNI_ABORT);
    (*env)->DeleteLocalRef(env, val);
    (*env)->DeleteLocalRef(env, name);
  }
  if(width < 0){
    width = 1 << 30;
    opts |= TCKWNOOVER | TCKWPULEAD;
  }
  TCLIST *texts = tctdbqrykwic(qry, tcols, cbuf, width, opts);
  int tnum = tclistnum(texts);
  jobjectArray ary = (*env)->NewObjectArray(env, tnum, clsstring, NULL);
  for(int i = 0; i < tnum; i++){
    jobject text = (*env)->NewStringUTF(env, tclistval2(texts, i));
    (*env)->SetObjectArrayElement(env, ary, i, text);
    (*env)->DeleteLocalRef(env, text);
  }
  tclistdel(texts);
  if(icc) (*env)->ReleaseStringUTFChars(env, name, cbuf);
  tcmapdel(tcols);
  return ary;
}
Esempio n. 9
0
const char* 
xtc_listval(void* list, int index)
{
        return tclistval2(list, index);
}
Esempio n. 10
0
/* perform search command */
static int procsearch(const char *path, TCLIST *conds, const char *oname, const char *otype,
                      int omode, int max, int skip, bool pv, bool px, bool kw, bool ph, int bt,
                      bool rm, const char *mtype){
  TCTDB *tdb = tctdbnew();
  if(g_dbgfd != INVALID_HANDLE_VALUE) tctdbsetdbgfd(tdb, g_dbgfd);
  if(!tctdbsetcodecfunc(tdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(tdb);
  if(!tctdbopen(tdb, path, (rm ? TDBOWRITER : TDBOREADER) | omode)){
    printerr(tdb);
    tctdbdel(tdb);
    return 1;
  }
  bool err = false;
  TDBQRY *qry = tctdbqrynew(tdb);
  int cnum = tclistnum(conds);
  for(int i = 0; i < cnum - 2; i += 3){
    const char *name = tclistval2(conds, i);
    const char *opstr = tclistval2(conds, i + 1);
    const char *expr  = tclistval2(conds, i + 2);
    int op = tctdbqrystrtocondop(opstr);
    if(op >= 0) tctdbqryaddcond(qry, name, op, expr);
  }
  if(oname){
    int type = tctdbqrystrtoordertype(otype);
    if(type >= 0) tctdbqrysetorder(qry, oname, type);
  }
  tctdbqrysetlimit(qry, max, skip);
  if(rm){
    double stime = tctime();
    if(!tctdbqrysearchout(qry)){
      printerr(tdb);
      err = true;
    }
    double etime = tctime();
    if(ph){
      TCLIST *hints = tcstrsplit(tctdbqryhint(qry), "\n");
      int hnum = tclistnum(hints);
      for(int i = 0; i < hnum; i++){
        const char *hint = tclistval2(hints, i);
        if(*hint == '\0') continue;
        printf("\t:::: %s\n", hint);
      }
      tclistdel(hints);
      printf("\t:::: number of records: %d\n", tctdbqrycount(qry));
      printf("\t:::: elapsed time: %.5f\n", etime - stime);
    }
  } else if(bt > 0){
    double sum = 0;
    for(int i = 1; i <= bt; i++){
      double stime = tctime();
      TCLIST *res = tctdbqrysearch(qry);
      double etime = tctime();
      tclistdel(res);
      printf("%d: %.5f sec.\n", i, etime - stime);
      sum += etime - stime;
    }
    printf("----\n");
    printf("total: %.5f sec. (%.5f s/q = %.5f q/s)\n", sum, sum / bt, bt / sum);
  } else {
    double stime = tctime();
    TCLIST *res;
    TCLIST *hints;
    int count;
    int mtnum = mtype ? tctdbmetastrtosettype(mtype) : -1;
    if(mtnum >= 0){
      TDBQRY *qrys[cnum/3+1];
      int qnum = 0;
      for(int i = 0; i < cnum - 2; i += 3){
        const char *name = tclistval2(conds, i);
        const char *opstr = tclistval2(conds, i + 1);
        const char *expr  = tclistval2(conds, i + 2);
        int op = tctdbqrystrtocondop(opstr);
        if(op >= 0){
          qrys[qnum] = tctdbqrynew(tdb);
          tctdbqryaddcond(qrys[qnum], name, op, expr);
          if(oname){
            int type = tctdbqrystrtoordertype(otype);
            if(type >= 0) tctdbqrysetorder(qrys[qnum], oname, type);
          }
          tctdbqrysetlimit(qrys[qnum], max, skip);
          qnum++;
        }
      }
      res = tctdbmetasearch(qrys, qnum, mtnum);
      hints = qnum > 0 ? tcstrsplit(tctdbqryhint(qrys[0]), "\n") : tclistnew2(1);
      count = qnum > 0 ? tctdbqrycount(qrys[0]) : 0;
      for(int i = 0; i < qnum; i++){
        tctdbqrydel(qrys[i]);
      }
    } else {
      res = tctdbqrysearch(qry);
      hints = tcstrsplit(tctdbqryhint(qry), "\n");
      count = tctdbqrycount(qry);
    }
    double etime = tctime();
    if(max < 0) max = INT_MAX;
    int rnum = tclistnum(res);
    for(int i = 0; i < rnum && max > 0; i++){
      int pksiz;
      const char *pkbuf = tclistval(res, i, &pksiz);
      if(kw){
        TCMAP *cols = tctdbget(tdb, pkbuf, pksiz);
        if(cols){
          TCLIST *texts = tctdbqrykwic(qry, cols, NULL, 16, TCKWMUTAB);
          int tnum = tclistnum(texts);
          for(int j = 0; j < tnum && max > 0; j++){
            int tsiz;
            const char *text = tclistval(texts, j, &tsiz);
            printdata(pkbuf, pksiz, px);
            putchar('\t');
            printdata(text, tsiz, px);
            putchar('\n');
            max--;
          }
          tclistdel(texts);
          tcmapdel(cols);
        }
      } else {
        printdata(pkbuf, pksiz, px);
        if(pv){
          TCMAP *cols = tctdbget(tdb, pkbuf, pksiz);
          if(cols){
            tcmapiterinit(cols);
            const char *kbuf;
            int ksiz;
            while((kbuf = tcmapiternext(cols, &ksiz)) != NULL){
              int vsiz;
              const char *vbuf = tcmapiterval(kbuf, &vsiz);
              putchar('\t');
              printdata(kbuf, ksiz, px);
              putchar('\t');
              printdata(vbuf, vsiz, px);
            }
            tcmapdel(cols);
          }
        }
        putchar('\n');
        max--;
      }
    }
    if(ph){
      int hnum = tclistnum(hints);
      for(int i = 0; i < hnum; i++){
        const char *hint = tclistval2(hints, i);
        if(*hint == '\0') continue;
        printf("\t:::: %s\n", hint);
      }
      printf("\t:::: number of records: %d\n", count);
      printf("\t:::: elapsed time: %.5f\n", etime - stime);
    }
    tclistdel(hints);
    tclistdel(res);
  }
  tctdbqrydel(qry);
  if(!tctdbclose(tdb)){
    if(!err) printerr(tdb);
    err = true;
  }
  tctdbdel(tdb);
  return err ? 1 : 0;
}
Esempio n. 11
0
/* parse arguments of put command */
static int runput(int argc, char **argv){
  char *path = NULL;
  char *pkey = NULL;
  TCLIST *vals = tcmpoollistnew(tcmpoolglobal());
  int omode = 0;
  int dmode = 0;
  bool sx = false;
  for(int i = 2; i < argc; i++){
    if(!path && argv[i][0] == '-'){
      if(!strcmp(argv[i], "-nl")){
        omode |= TDBONOLCK;
      } else if(!strcmp(argv[i], "-nb")){
        omode |= TDBOLCKNB;
      } else if(!strcmp(argv[i], "-dk")){
        dmode = -1;
      } else if(!strcmp(argv[i], "-dc")){
        dmode = 1;
      } else if(!strcmp(argv[i], "-dai")){
        dmode = 10;
      } else if(!strcmp(argv[i], "-dad")){
        dmode = 11;
      } else if(!strcmp(argv[i], "-sx")){
        sx = true;
      } else {
        usage();
      }
    } else if(!path){
      path = argv[i];
    } else if(!pkey){
      pkey = argv[i];
    } else {
      tclistpush2(vals, argv[i]);
    }
  }
  if(!path || !pkey) usage();
  TCMAP *cols = tcmapnew();
  char *pkbuf;
  int pksiz;
  if(sx){
    pkbuf = tchexdecode(pkey, &pksiz);
    for(int i = 0; i < tclistnum(vals) - 1; i += 2){
      const char *name = tclistval2(vals, i);
      const char *value = tclistval2(vals, i + 1);
      int nsiz;
      char *nbuf = tchexdecode(name, &nsiz);
      int vsiz;
      char *vbuf = tchexdecode(value, &vsiz);
      tcmapput(cols, nbuf, nsiz, vbuf, vsiz);
      tcfree(vbuf);
      tcfree(nbuf);
    }
  } else {
    pksiz = strlen(pkey);
    pkbuf = tcmemdup(pkey, pksiz);
    for(int i = 0; i < tclistnum(vals) - 1; i += 2){
      const char *name = tclistval2(vals, i);
      const char *value = tclistval2(vals, i + 1);
      tcmapput2(cols, name, value);
    }
  }
  int rv = procput(path, pkbuf, pksiz, cols, omode, dmode);
  tcmapdel(cols);
  tcfree(pkbuf);
  return rv;
}
Esempio n. 12
0
void panel_article_update(struct env_t *_SERVER)
{
	tpl_t        *tpl;
	TCLIST       *arr;
	char         *html, path[128], buf[128];

	sqlite3_stmt *stmt;
	time_t       visit;
	int          rc, i, type, pos, len, n;
	const char   *id, *title, *sortlevel, *content, *recommend, *keyword, *filename, *file, *post_time;


	if(tcmapget2(_SERVER->_POST, "Article_Update"))
	{
		file      = tcmapget2(_SERVER->_COOKIE, "file");
		id        = tcmapget2(_SERVER->_POST, "id");
		title     = tcmapget2(_SERVER->_POST, "title");
		content   = tcmapget2(_SERVER->_POST, "content");
		keyword   = tcmapget2(_SERVER->_POST, "keyword");
		filename  = tcmapget2(_SERVER->_POST, "filename");
		sortlevel = tcmapget2(_SERVER->_POST, "sortlevel");
		recommend = tcmapget2(_SERVER->_POST, "recommend");
		post_time = tcmapget2(_SERVER->_POST, "post_time");

		pos = strpos(content, "<!-- idx -->");
		pos = (pos == -1) ? strlen(content) : pos;
		rc = sqlite3_prepare(db, "UPDATE article SET title = ?, content = ?, catid = ?, keyword = ?, filename = ?, recommend = ?, position = ? WHERE art_id = ?;", -1, &stmt, NULL); 
		
		sqlite3_bind_text(stmt, 1, title,     -1, SQLITE_STATIC);
		sqlite3_bind_text(stmt, 2, content,   -1, SQLITE_STATIC);
		sqlite3_bind_text(stmt, 3, sortlevel, -1, SQLITE_STATIC);
		sqlite3_bind_text(stmt, 4, keyword,   -1, SQLITE_STATIC);
		sqlite3_bind_text(stmt, 5, filename,  -1, SQLITE_STATIC);

		sqlite3_bind_int(stmt, 6, atoi(recommend));
		sqlite3_bind_int(stmt, 7, pos);
		sqlite3_bind_int(stmt, 8, atoi(id));
		

		sqlite3_step(stmt); 
		sqlite3_finalize(stmt); 

		visit = atoi(post_time);

		if(file)
		{
			memset(buf,  0, sizeof(buf));
			memset(path, 0, sizeof(path));
			
			strftime(buf, sizeof(buf), "%Y/%m/%d", localtime(&visit));

			//如果有上传的文件,就把上传的文件,从临时目录移动目标目录
			len = snprintf(path, sizeof(path), "./attachment/%s/%llu/", buf, (long long unsigned int)id);
			
			if(!is_dir(path)) mkpath(_SERVER->mp, path, 0777);
			if(!strchr(file, '|'))
			{
				memset(buf,  0, sizeof(buf));
				snprintf(buf, sizeof(buf), "./files/%s", file);
				strcat(path, file);
				
			//	log_debug("panel_article_insert buf = %s\tpath = %s\r\n", buf, path);
				if(file_exists(buf)) rename(buf, path);
			}
			else
			{
				arr = explode("|", (char *)file);
				n   = tclistnum(arr);
				for(i=0; i<n; i++)
				{
					memset(buf,  0, sizeof(buf));
					filename = tclistval2(arr, i);
					snprintf(buf, sizeof(buf), "./files/%s", filename);

					path[len] = '\0';
					strcat(path, filename);

		//			log_debug("panel_article_insert explode buf = %s\tpath = %s\r\n", buf, path);

					if(file_exists(buf)) rename(buf, path);
				}
				tclistdel(arr);
			}
			
			path[len] = '\0';
			

			//更新图片附件的 路径
			rc = sqlite3_prepare(db, "UPDATE article SET content = replace(content, '/files/', ?) WHERE art_id = ?;", -1, &stmt, NULL);
			
			sqlite3_bind_text(stmt, 1, path+1,   -1, SQLITE_STATIC);
			sqlite3_bind_int(stmt, 2, atoi(id));
			sqlite3_step(stmt); 
			sqlite3_finalize(stmt); 
			
		}

#ifdef _BUILD_HTML
		memset(path, 0, sizeof(path));
		snprintf(path, sizeof(path), "./html/article/%s/%s.htm", sortlevel, id);
		if(file_exists(path))
			remove(path);

		memset(path, 0, sizeof(path));
		snprintf(path, sizeof(path), "./html/list/%s", sortlevel);
		ftw(path, fn, 500);
#endif
		printf("Content-type: text/html\r\n\r\n<script>alert('编辑成功!');window.location.href='/panel-article-list-%s.html';</script>", sortlevel);

		return ;
	}
	
	id = tclistval2(_SERVER->_GET, 3);
	if(id == NULL || !is_digit(id))
	{
		puts("Content-type: text/html\r\n\r\n<script>alert('请您选择要更新的文章');window.location.href='/panel-article-list-all.html';</script>");
		return ;
	}

	tpl = tpl_alloc();
	if (tpl_load(tpl, "./templets/panel/article_update.html") != TPL_OK)
    {
		printf("Content-type: text/html\r\n\r\n./templets/panel/article_update.html Error loading template file!");
        tpl_free(tpl);
		return ;
    }
	
	//加载分类的数据
	rc = sqlite3_prepare(db, "SELECT sortname, sortdir FROM category", -1, &stmt, NULL);
	
	tpl_select_section(tpl, "classic");
	while((rc = sqlite3_step(stmt)) == SQLITE_ROW)
	{
		for(i=0; i<sqlite3_column_count(stmt); i++)
		{
			tpl_set_field(tpl, sqlite3_column_name(stmt,i) ,sqlite3_column_text(stmt,i), strlen(sqlite3_column_text(stmt,i)));
		}
		tpl_append_section(tpl);
	}

	tpl_deselect_section(tpl);
	sqlite3_finalize(stmt);
	
	//加载需要编辑的数据
	rc = sqlite3_prepare(db, "SELECT * FROM article WHERE art_id = ?", -1, &stmt, NULL);
	sqlite3_bind_int(stmt, 1, atoi(id));

	while((rc = sqlite3_step(stmt)) == SQLITE_ROW)
	{
		for(i=0; i<sqlite3_column_count(stmt); i++)
		{
			type = sqlite3_column_type(stmt, i);
			switch(type)
			{
				case SQLITE_INTEGER:
					tpl_set_field_uint_global(tpl, sqlite3_column_name(stmt,i), sqlite3_column_int(stmt, i));
					break;
				case SQLITE_TEXT:
					tpl_set_field_global(tpl, sqlite3_column_name(stmt,i) ,sqlite3_column_text(stmt,i), strlen(sqlite3_column_text(stmt,i)));
					break;
			}
		}
	}

	sqlite3_finalize(stmt);


	html = mspace_malloc(_SERVER->mp, tpl_length(tpl) + 1);
    tpl_get_content(tpl, html);

	printf("Content-type: text/html\r\n\r\n%s", html);
	
	tpl_free(tpl);
	mspace_free(_SERVER->mp, html);
	
	tpl  = NULL;
}
Esempio n. 13
0
void panel_article_insert(struct env_t *_SERVER)
{
	time_t       visit;
	tpl_t        *tpl;
	TCLIST       *arr;
	char         *html, path[128], buf[128];
	
	sqlite3_int64   id;
	sqlite3_stmt    *stmt;
	int             rc, i, pos, n, len;
	const char      *title, *sortlevel, *content, *recommend, *keyword, *filename, *file;

	if(tcmapget2(_SERVER->_POST, "Article_Insert"))
	{
		visit     = time((time_t*)0);
		file      = tcmapget2(_SERVER->_COOKIE, "file");
		title     = tcmapget2(_SERVER->_POST,   "title");
		content   = tcmapget2(_SERVER->_POST,   "content");
		keyword   = tcmapget2(_SERVER->_POST,   "keyword");
		filename  = tcmapget2(_SERVER->_POST,   "filename");
		sortlevel = tcmapget2(_SERVER->_POST,   "sortlevel");
		recommend = tcmapget2(_SERVER->_POST,   "recommend");
		
		pos = strpos(content, "<!-- idx -->");
		pos = (pos == -1) ? strlen(content) : pos;
		rc  = sqlite3_prepare(db, "INSERT INTO article(title, content, catid, keyword, filename, recommend, post_time, position, hit, comment_num) VALUES(?, ?, ?, ?, ?, ?, ?, ?, 0, 0);", -1, &stmt, NULL); 
		
		sqlite3_bind_text(stmt, 1, title,     -1, SQLITE_STATIC);
		sqlite3_bind_text(stmt, 2, content,   -1, SQLITE_STATIC);
		sqlite3_bind_text(stmt, 3, sortlevel, -1, SQLITE_STATIC);
		sqlite3_bind_text(stmt, 4, keyword,   -1, SQLITE_STATIC);
		sqlite3_bind_text(stmt, 5, filename,  -1, SQLITE_STATIC);

		sqlite3_bind_int(stmt, 6, atoi(recommend));
		sqlite3_bind_int(stmt, 7, visit);
		sqlite3_bind_int(stmt, 8, pos);

		
		sqlite3_step(stmt); 
		sqlite3_finalize(stmt); 
		
		id = sqlite3_last_insert_rowid(db);
				
		if(file)
		{
			memset(buf,  0, sizeof(buf));
			memset(path, 0, sizeof(path));
			
			strftime(buf, sizeof(buf), "%Y/%m/%d", localtime(&visit));

			//如果有上传的文件,就把上传的文件,从临时目录移动目标目录
			len = snprintf(path, sizeof(path), "./attachment/%s/%llu/", buf, id);
			
			if(!is_dir(path)) mkpath(_SERVER->mp, path, 0777);
			if(!strchr(file, '|'))
			{
				memset(buf,  0, sizeof(buf));
				snprintf(buf, sizeof(buf), "./files/%s", file);
				strcat(path, file);
				
			//	log_debug("panel_article_insert buf = %s\tpath = %s\r\n", buf, path);
				if(file_exists(buf)) rename(buf, path);
			}
			else
			{
				arr = explode("|", (char *)file);
				n   = tclistnum(arr);
				for(i=0; i<n; i++)
				{
					memset(buf,  0, sizeof(buf));
					filename = tclistval2(arr, i);
					snprintf(buf, sizeof(buf), "./files/%s", filename);

					path[len] = '\0';
					strcat(path, filename);

		//			log_debug("panel_article_insert explode buf = %s\tpath = %s\r\n", buf, path);

					if(file_exists(buf)) rename(buf, path);
				}
				tclistdel(arr);
			}
			
			path[len] = '\0';
			

			//更新图片附件的 路径
			rc = sqlite3_prepare(db, "UPDATE article SET content = replace(content, '/files/', ?) WHERE art_id = ?;", -1, &stmt, NULL);
			
			sqlite3_bind_text(stmt,  1, path+1, -1, SQLITE_STATIC);
			sqlite3_bind_int64(stmt, 2, id);
			sqlite3_step(stmt); 
			sqlite3_finalize(stmt); 
			
		}
		

#ifdef _BUILD_HTML
		memset(path, 0, sizeof(path));
		snprintf(path, sizeof(path), "./html/list/%s", sortlevel);
		ftw(path, fn, 500);
#endif
		make_article_index(_SERVER);
		printf("Content-type: text/html\r\n\r\n<script>alert('添加成功!');window.location.href='/panel-article-list-%s.html';</script>", sortlevel);

		return ;
	}
	

	tpl = tpl_alloc();
	if (tpl_load(tpl, "./templets/panel/article_insert.html") != TPL_OK)
    {
		puts("Content-type: text/html\r\n\r\n./templets/panel/article_insert.html Error loading template file!");
        tpl_free(tpl);
		return ;
    }

	rc = sqlite3_prepare(db, "SELECT sortname, sortdir FROM category", -1, &stmt, NULL);
	
	tpl_select_section(tpl, "classic");
	while((rc = sqlite3_step(stmt)) == SQLITE_ROW)
	{
		for(i=0; i<sqlite3_column_count(stmt); i++)
		{
			tpl_set_field(tpl, sqlite3_column_name(stmt,i) ,sqlite3_column_text(stmt,i), strlen(sqlite3_column_text(stmt,i)));
		}

		tpl_append_section(tpl);
	}

	tpl_deselect_section(tpl);
	sqlite3_finalize(stmt);


	html = mspace_malloc(_SERVER->mp, tpl_length(tpl) + 1);
    tpl_get_content(tpl, html);

	printf("Content-type: text/html\r\n\r\n%s", html);
	
	tpl_free(tpl);
	mspace_free(_SERVER->mp, html);
	
	tpl  = NULL;
}
Esempio n. 14
0
void panel_article_list(struct env_t *_SERVER)
{
	tpl_t  *tpl;
	char   url[128];
	char   *html, *sql;
	int    rc, i, type, total;
	

	uint limit[2], cur;
	sqlite3_stmt   *stmt;
	const char     *sortlevel, *page;
	
	
	tpl = tpl_alloc();
	if (tpl_load(tpl, "./templets/panel/article_list.html") != TPL_OK)
    {
		printf("Content-type: text/html\r\n\r\n./templets/panel/article_list.html Error loading template file!");
		tpl_free(tpl);
		return ;
    }

	total     = 0;
	sortlevel = tclistval2(_SERVER->_GET, 3);
	page      = tclistval2(_SERVER->_GET, 4);

	memset(&url, 0, sizeof(url));
	if((sortlevel) && (strcmp(sortlevel, "all") == 0))
	{
		strcpy(url, "/panel-article-list-all");
		rc   = sqlite3_prepare(db, "SELECT COUNT(*) AS c FROM article;", -1, &stmt, NULL);
	}
	else if((sortlevel) && (is_alpha(sortlevel)) && (strcmp(sortlevel, "html") != 0))
	{
		rc   = sqlite3_prepare(db, "SELECT COUNT(*) AS c FROM article WHERE catid = ?;", -1, &stmt, NULL);
		sqlite3_bind_text(stmt, 1, sortlevel, -1, SQLITE_STATIC);
		
		snprintf(url, sizeof(url), "/panel-article-list-%s", sortlevel);
	}
	else
	{
		strcpy(url, "/panel-article-list-all");
		rc   = sqlite3_prepare(db, "SELECT COUNT(*) AS c FROM article;", -1, &stmt, NULL);
	}

	rc = sqlite3_step(stmt);
	if(rc == SQLITE_ROW)
	{
		total = sqlite3_column_int(stmt, 0);
	}
	sqlite3_finalize(stmt);
	
	
	cur = (page) ? atoi(page) : 1;

	//分页
	pager(tpl, total, conf.page.admin, cur, url, limit);

	tpl_set_field_int_global(tpl, "total", total);

	if((sortlevel) && (strcmp(sortlevel, "all") == 0))
	{
		rc = sqlite3_prepare(db, "SELECT art.comment_num, art.hit, art.art_id, art.title, art.content, datetime(art.post_time, 'unixepoch') AS dt, cat.sortname, cat.sortdir FROM article AS art LEFT JOIN category AS cat ON cat.sortdir = art.catid ORDER BY art.art_id DESC LIMIT ?, ?;", -1, &stmt, NULL);
		
		sqlite3_bind_int(stmt, 1, limit[0]);
		sqlite3_bind_int(stmt, 2, limit[1]);
	}
	else if((sortlevel) && (is_alpha(sortlevel)) && (strcmp(sortlevel, "html") != 0))
	{
		rc = sqlite3_prepare(db, "SELECT art.comment_num, art.hit, art.art_id, art.title, art.content, datetime(art.post_time, 'unixepoch') AS dt, cat.sortname, cat.sortdir FROM article AS art LEFT JOIN category AS cat ON cat.sortdir = art.catid WHERE art.catid = ? ORDER BY art.art_id DESC LIMIT ?, ?;", -1, &stmt, NULL);
		
		sqlite3_bind_text(stmt, 1, sortlevel, -1, SQLITE_STATIC);
		sqlite3_bind_int(stmt,  2, limit[0]);
		sqlite3_bind_int(stmt,  3, limit[1]);
	}
	else
	{
		rc = sqlite3_prepare(db, "SELECT art.comment_num, art.hit, art.art_id, art.title, art.content, datetime(art.post_time, 'unixepoch') AS dt, cat.sortname, cat.sortdir FROM article AS art LEFT JOIN category AS cat ON cat.sortdir = art.catid ORDER BY art.art_id DESC LIMIT ?, ?;", -1, &stmt, NULL);
		
		sqlite3_bind_int(stmt, 1, limit[0]);
		sqlite3_bind_int(stmt, 2, limit[1]);
	}
	
	
	
	tpl_select_section(tpl, "data");
	while((rc = sqlite3_step(stmt)) == SQLITE_ROW)
	{
		for(i=0; i<sqlite3_column_count(stmt); i++)
		{
			type = sqlite3_column_type(stmt, i);
			switch(type)
			{
				case SQLITE_INTEGER:
					tpl_set_field_uint(tpl, sqlite3_column_name(stmt,i), sqlite3_column_int(stmt, i));
					break;
				case SQLITE_TEXT:
					tpl_set_field(tpl, sqlite3_column_name(stmt,i) ,sqlite3_column_text(stmt,i), strlen(sqlite3_column_text(stmt,i)));
					break;
			}
		}
		tpl_append_section(tpl);
	}

	tpl_deselect_section(tpl);
	sqlite3_finalize(stmt);
	

	html = mspace_malloc(_SERVER->mp, tpl_length(tpl) + 1);
    tpl_get_content(tpl, html);
	
	printf("Content-type: text/html\r\n\r\n%s", html);

	tpl_free(tpl);
	mspace_free(_SERVER->mp, html);
	
	tpl  = NULL;
	
}
Esempio n. 15
0
/* perform import command */
static int procimport(const char *dbpath, TCLIST *files, TCLIST *sufs){
  TCTDB *tdb = tctdbnew();
  if(!tctdbtune(tdb, TUNEBNUM, TUNEAPOW, TUNEFPOW, 0)){
    printdberr(tdb);
    tctdbdel(tdb);
    return 1;
  }
  if(!tctdbopen(tdb, dbpath, TDBOWRITER | TDBOCREAT)){
    printdberr(tdb);
    tctdbdel(tdb);
    return 1;
  }
  bool err = false;
  if(!tctdbsetindex(tdb, "name", TDBITLEXICAL | TDBITKEEP) && tctdbecode(tdb) != TCEKEEP){
    printdberr(tdb);
    err = true;
  }
  if(!tctdbsetindex(tdb, "cdate", TDBITDECIMAL | TDBITKEEP) && tctdbecode(tdb) != TCEKEEP){
    printdberr(tdb);
    err = true;
  }
  if(!tctdbsetindex(tdb, "mdate", TDBITDECIMAL | TDBITKEEP) && tctdbecode(tdb) != TCEKEEP){
    printdberr(tdb);
    err = true;
  }
  if(!tctdbsetindex(tdb, "xdate", TDBITDECIMAL | TDBITKEEP) && tctdbecode(tdb) != TCEKEEP){
    printdberr(tdb);
    err = true;
  }
  tclistinvert(files);
  char *fpath;
  while((fpath = tclistpop2(files)) != NULL){
    TCLIST *cfiles = tcreaddir(fpath);
    if(cfiles){
      tclistsort(cfiles);
      for(int i = tclistnum(cfiles) - 1; i >= 0; i--){
        const char *cfile = tclistval2(cfiles, i);
        bool hit = false;
        for(int j = 0; j < tclistnum(sufs); j++){
          if(tcstribwm(cfile, tclistval2(sufs, j))){
            hit = true;
            break;
          }
        }
        if(!hit) continue;
        char *lpath = tcsprintf("%s/%s", fpath, cfile);
        tclistpush2(files, lpath);
        tcfree(lpath);
      }
      tclistdel(cfiles);
    } else {
      int isiz;
      char *ibuf = tcreadfile(fpath, IOMAXSIZ, &isiz);
      if(ibuf){
        TCMAP *cols = tcmapnew2(TINYBNUM);
        wikiload(cols, ibuf);
        const char *name = tcmapget2(cols, "name");
        if(name && *name != '\0'){
          int64_t id = tcatoi(tcmapget4(cols, "id", ""));
          if(dbputart(tdb, id, cols)){
            id = tcatoi(tcmapget4(cols, "id", ""));
            printf("%s: imported: id=%lld name=%s\n", fpath, (long long)id, name);
          } else {
            printdberr(tdb);
            err = true;
          }
        } else {
          printf("%s: ignored because there is no name\n", fpath);
        }
        tcmapdel(cols);
        tcfree(ibuf);
      }
    }
    tcfree(fpath);
  }
  if(!tctdbclose(tdb)){
    printdberr(tdb);
    err = true;
  }
  tctdbdel(tdb);
  return err ? 1 : 0;
}