int mutt_query_complete(char *buf, size_t buflen) { QUERY *results = NULL; ADDRESS *tmpa; if (!QueryCmd) { mutt_error _("Query command not defined."); return 0; } results = run_query(buf, 1); if (results) { /* only one response? */ if (results->next == NULL) { tmpa = result_to_addr(results); mutt_addrlist_to_local(tmpa); buf[0] = '\0'; rfc822_write_address(buf, buflen, tmpa, 0); rfc822_free_address(&tmpa); free_query(&results); mutt_clear_error(); return 0; } /* multiple results, choose from query menu */ query_menu(buf, buflen, results, 1); } return 0; }
/* * Query table for specified rows * _h: structure representing database connection * _k: key names * _op: operators * _v: values of the keys that must match * _c: column names to return * _n: nmber of key=values pairs to compare * _nc: number of columns to return * _o: order by the specified column */ int db_query(db_con_t* _h, db_key_t* _k, db_op_t* _op, db_val_t* _v, db_key_t* _c, int _n, int _nc, db_key_t _o, db_res_t** _r) { int off, rv; if (!_c) { off = snprintf(sql_buf, SQL_BUF_LEN, "select * from %s ", CON_TABLE(_h)); } else { off = snprintf(sql_buf, SQL_BUF_LEN, "select "); off += print_columns(sql_buf + off, SQL_BUF_LEN - off, _c, _nc); off += snprintf(sql_buf + off, SQL_BUF_LEN - off, "from %s ", CON_TABLE(_h)); } if (_n) { off += snprintf(sql_buf + off, SQL_BUF_LEN - off, "where "); off += print_where(sql_buf + off, SQL_BUF_LEN - off, _k, _op, _v, _n); } if (_o) { off += snprintf(sql_buf + off, SQL_BUF_LEN - off, "order by %s", _o); } if(begin_transaction(_h, sql_buf)) return(-1); if (submit_query(_h, sql_buf) < 0) { LOG(L_ERR, "db_query(): Error while submitting query\n"); return -2; } rv = get_result(_h, _r); free_query(_h); commit_transaction(_h); return(rv); }
int db_free_query(db_con_t* _h, db_res_t* _r) { free_query(_h); free_result(_r); return 0; }
int test_fetch_role() { MYSQL_RES *res; MYSQL_ROW row; char sql[256]; int len; char *p; uint64_t effect; len = sprintf(sql, "insert into DBRoleInfo set id = 8686, profession = 1, level = 1, name = "); p = sql + len; *p++ = '\''; p += escape_string(p, "jacktang", 8); *p++ = '\''; query(sql, 1, &effect); sprintf(sql, "select id, name, profession, level, attribute, bag from DBRoleInfo where id = %lu", 8686l); res = query(sql, 1, NULL); row = fetch_row(res); row[0] = row[0]; free_query(res); return (0); }
int db_postgres_free_result(db_con_t* _con, db_res_t* _r) { free_query(_con); if (_r) db_free_result(_r); _r = 0; return 0; }
int main(void) { int len; char *lenstr,poststr[512]; char *player_id, *server_id; int can_share = 1; init_db((char *)"127.0.0.1", 3306, (char *)"pay", (char *)"root", (char *)"123456"); printf("Content-Type:text/html\n\n"); lenstr=getenv("CONTENT_LENGTH"); if(lenstr == NULL) { printf("<DIV STYLE=\"COLOR:RED\">Errorarameters should be entered!</DIV>\n"); return (0); } len=atoi(lenstr) + 1; if (len >= 512) return (0); fgets(poststr,len,stdin); parse_post_data(poststr, len); server_id = get_value((char *)"server_id"); player_id = get_value((char *)"player_id"); if (!server_id || !player_id) return (0); MYSQL_RES *res = NULL; MYSQL_ROW row; char sql[256]; sprintf(sql, "select last_share_time, share_times, pay_times from share where server_id = %s and player_id = %s", server_id, player_id); res = query(sql, 1, NULL); if (!res) { send_no_record(); goto done; } row = fetch_row(res); if (!row) { send_no_record(); goto done; } if (!check_can_share(row)) { can_share = 0; } printf("[%d, %s, %s]", can_share, row[1], row[2]); done: if (res) free_query(res); fflush(stdout); close_db(); return 0; }
/* * Frees memory associated with an allocated (or partially allocated) * pgsnmpd_query structure, and all such structures following it in the * query list */ void free_query(pgsnmpd_query *query) { if (query == NULL) return; if (query->table_name != NULL) free(query->table_name); if (query->query_text != NULL) free(query->query_text); if (query->table_oid != NULL) free(query->table_oid); if (query->types != NULL) free(query->types); if (query->result != NULL) PQclear(query->result); if (query->next != NULL) free_query(query->next); free(query); }
int fetch_role_handle(PROTO_HEAD *head, uint16_t fd, int len) { // int i; MYSQL_RES *res; MYSQL_ROW row; char sql[256]; FETCH_ROLE_DATA_RESPONSE *resp; char sendbuf[sizeof(PROTO_HEAD) + sizeof(*resp)]; FETCH_ROLE_DATA_REQUEST *request = (FETCH_ROLE_DATA_REQUEST *)head->data; resp = (FETCH_ROLE_DATA_RESPONSE *)(&sendbuf[sizeof(PROTO_HEAD)]); head = (PROTO_HEAD *)(&sendbuf[0]); resp->info.name[0] = '\0'; if (len != sizeof(PROTO_HEAD) + sizeof(FETCH_ROLE_DATA_REQUEST)) { log4c_category_log(mycat, LOG4C_PRIORITY_INFO, "%s: len[%d] with msg[%u] server[?] fail\n", __FUNCTION__, head->len, head->msg_id); return (-1); } sprintf(sql, "select id, name, profession, level, scene_id, pos_x, pos_y from DBRoleInfo where id = %lu", request->id); res = query(sql, 1, NULL); if (!res) { log4c_category_log(mycat, LOG4C_PRIORITY_INFO, "%s: can not fetch role %lu\n", __FUNCTION__, request->id); resp->result = -1; goto done; } row = fetch_row(res); if (!row) { log4c_category_log(mycat, LOG4C_PRIORITY_INFO, "%s: can not fetch role %lu\n", __FUNCTION__, request->id); resp->result = -1; goto done; } resp->result = 0; strcpy(resp->info.name, row[1]); resp->info.profession = atoi(row[2]); resp->info.level = atoi(row[3]); resp->info.scene_id = atoi(row[4]); resp->info.pos.pos_x = atoi(row[5]); resp->info.pos.pos_y = atoi(row[6]); // resp->bag.max_thing_num = 10; done: if (res) free_query(res); resp->id = request->id; head->len = htons(sizeof(sendbuf)); head->msg_id = htons(SERVER_PROTO_FETCH_ROLE_RESPONSE); send_one_msg(fd, head); // send(fd, sendbuf, sizeof(sendbuf), 0); return (0); }
/* Allocates a pgsnmpd_query struct */ pgsnmpd_query * alloc_custom_query(char *table_name, char *query_text, oid *table_oid, int oid_len) { pgsnmpd_query *query; int i; query = malloc(sizeof(pgsnmpd_query)); if (query == NULL) return NULL; query->result = NULL; query->next = NULL; query->last_refresh = 0; query->cache_timeout = -1; query->my_handler = NULL; query->typeslen = 0; i = strnlen(table_name, CQ_MAX_LEN); query->table_name = strndup(table_name, i); if (query->table_name == NULL) { free_query(query); return NULL; } i = strnlen(query_text, CQ_MAX_LEN); query->query_text = strndup(query_text, i); if (query->query_text == NULL) { free_query(query); return NULL; } query->table_oid = malloc(sizeof(oid) * oid_len); if (query->table_oid == NULL) { free_query(query); return NULL; } for (i = 0; i < oid_len; i++) { query->table_oid[i] = table_oid[i]; } query->oid_len = oid_len; return query; }
/* * Execute a raw SQL query */ int db_raw_query(db_con_t* _h, char* _s, db_res_t** _r) { int rv; if(begin_transaction(_h, sql_buf)) return(-1); if (submit_query(_h, _s) < 0) { LOG(L_ERR, "db_raw_query(): Error while submitting query\n"); return -2; } rv = get_result(_h, _r); free_query(_h); commit_transaction(_h); return(rv); }
/* * Delete a row from the specified table * _h: structure representing database connection * _k: key names * _o: operators * _v: values of the keys that must match * _n: number of key=value pairs */ int db_delete(db_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v, int _n) { int off; off = snprintf(sql_buf, SQL_BUF_LEN, "delete from %s", CON_TABLE(_h)); if (_n) { off += snprintf(sql_buf + off, SQL_BUF_LEN - off, " where "); off += print_where(sql_buf + off, SQL_BUF_LEN - off, _k, _o, _v, _n); } if(begin_transaction(_h, sql_buf)) return(-1); if (submit_query(_h, sql_buf) < 0) { LOG(L_ERR, "db_delete(): Error while deleting\n"); return -2; } free_query(_h); commit_transaction(_h); return(0); }
/* * Insert a row into specified table * _h: structure representing database connection * _k: key names * _v: values of the keys * _n: number of key=value pairs */ int db_insert(db_con_t* _h, db_key_t* _k, db_val_t* _v, int _n) { int off; off = snprintf(sql_buf, SQL_BUF_LEN, "insert into %s (", CON_TABLE(_h)); off += print_columns(sql_buf + off, SQL_BUF_LEN - off, _k, _n); off += snprintf(sql_buf + off, SQL_BUF_LEN - off, ") values ("); off += print_values(sql_buf + off, SQL_BUF_LEN - off, _v, _n); *(sql_buf + off++) = ')'; *(sql_buf + off) = '\0'; if(begin_transaction(_h, sql_buf)) return(-1); if (submit_query(_h, sql_buf) < 0) { LOG(L_ERR, "db_insert(): Error while inserting\n"); return -2; } free_query(_h); commit_transaction(_h); return(0); }
/* * Update some rows in the specified table * _h: structure representing database connection * _k: key names * _o: operators * _v: values of the keys that must match * _uk: updated columns * _uv: updated values of the columns * _n: number of key=value pairs * _un: number of columns to update */ int db_update(db_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v, db_key_t* _uk, db_val_t* _uv, int _n, int _un) { int off; off = snprintf(sql_buf, SQL_BUF_LEN, "update %s set ", CON_TABLE(_h)); off += print_set(sql_buf + off, SQL_BUF_LEN - off, _uk, _uv, _un); if (_n) { off += snprintf(sql_buf + off, SQL_BUF_LEN - off, " where "); off += print_where(sql_buf + off, SQL_BUF_LEN - off, _k, _o, _v, _n); *(sql_buf + off) = '\0'; } if(begin_transaction(_h, sql_buf)) return(-1); if (submit_query(_h, sql_buf) < 0) { LOG(L_ERR, "db_update(): Error while updating\n"); return -2; } free_query(_h); commit_transaction(_h); return(0); }
static int db_postgres_submit_query(const db_con_t* _con, const str* _s) { int i; ExecStatusType result; PGresult *res = NULL; if(! _con || !_s || !_s->s) { LM_ERR("invalid parameter value\n"); return(-1); } submit_func_called = 1; /* this bit of nonsense in case our connection get screwed up */ switch(PQstatus(CON_CONNECTION(_con))) { case CONNECTION_OK: break; case CONNECTION_BAD: LM_DBG("connection reset\n"); PQreset(CON_CONNECTION(_con)); break; case CONNECTION_STARTED: case CONNECTION_MADE: case CONNECTION_AWAITING_RESPONSE: case CONNECTION_AUTH_OK: case CONNECTION_SETENV: case CONNECTION_SSL_STARTUP: case CONNECTION_NEEDED: default: LM_ERR("%p PQstatus(%s) invalid: %.*s\n", _con, PQerrorMessage(CON_CONNECTION(_con)), _s->len, _s->s); return -1; } for (i=0;i<2;i++) { /* free any previous query that is laying about */ if(CON_RESULT(_con)) { free_query(_con); } /* exec the query */ if (PQsendQuery(CON_CONNECTION(_con), _s->s)) { LM_DBG("%p PQsendQuery(%.*s)\n", _con, _s->len, _s->s); while (1) { if ((res = PQgetResult(CON_CONNECTION(_con)))) { CON_RESULT(_con) = res; } else { break; } } result = PQresultStatus(CON_RESULT(_con)); if(result==PGRES_FATAL_ERROR) goto reconnect; else return 0; } else { reconnect: /* reconnection attempt - if this is the case */ LM_DBG("%p PQsendQuery failed: %s Query: %.*s\n", _con, PQerrorMessage(CON_CONNECTION(_con)), _s->len, _s->s); if(PQstatus(CON_CONNECTION(_con))!=CONNECTION_OK) { LM_DBG("connection reset\n"); PQreset(CON_CONNECTION(_con)); } else { /* failure not due to connection loss - no point in retrying */ if(CON_RESULT(_con)) { free_query(_con); } break; } } } LM_ERR("%p PQsendQuery Error: %s Query: %.*s\n", _con, PQerrorMessage(CON_CONNECTION(_con)), _s->len, _s->s); return 0; }
static int db_postgres_submit_async_query(const db_con_t* _con, const str* _s) { int i,ret=0; struct timeval start; if(! _con || !_s || !_s->s) { LM_ERR("invalid parameter value\n"); return(-1); } submit_func_called = 1; /* this bit of nonsense in case our connection get screwed up */ switch(PQstatus(CON_CONNECTION(_con))) { case CONNECTION_OK: break; case CONNECTION_BAD: LM_DBG("connection reset\n"); PQreset(CON_CONNECTION(_con)); break; case CONNECTION_STARTED: case CONNECTION_MADE: case CONNECTION_AWAITING_RESPONSE: case CONNECTION_AUTH_OK: case CONNECTION_SETENV: case CONNECTION_SSL_STARTUP: case CONNECTION_NEEDED: default: LM_ERR("%p PQstatus(%s) invalid: %.*s\n", _con, PQerrorMessage(CON_CONNECTION(_con)), _s->len, _s->s); return -1; } for (i=0;i<max_db_queries;i++) { /* free any previous query that is laying about */ if(CON_RESULT(_con)) { free_query(_con); } start_expire_timer(start,db_postgres_exec_query_threshold); ret = PQsendQuery(CON_CONNECTION(_con), _s->s); _stop_expire_timer(start, db_postgres_exec_query_threshold, "pgsql query", _s->s, _s->len, 0, sql_slow_queries, sql_total_queries); /* exec the query */ if (ret) { LM_DBG("%p PQsendQuery(%.*s)\n", _con, _s->len, _s->s); return 0; } else { LM_DBG("%p PQsendQuery failed: %s Query: %.*s\n", _con, PQerrorMessage(CON_CONNECTION(_con)), _s->len, _s->s); if(PQstatus(CON_CONNECTION(_con))!=CONNECTION_OK) { LM_DBG("connection reset\n"); PQreset(CON_CONNECTION(_con)); } else { /* failure not due to connection loss - no point in retrying */ if(CON_RESULT(_con)) { free_query(_con); } break; } } } LM_ERR("%p PQsendQuery Error: %s Query: %.*s\n", _con, PQerrorMessage(CON_CONNECTION(_con)), _s->len, _s->s); return -1; }
int main(int argc, char *argv[]) { int len; char *lenstr,poststr[512]; int server_id; int player_id; char *content; const int page_size = 20; //每页20条 int total_page; //页数 int cur_page; //请求的页数 int total_count; //总共的记录数 int start_pos = 0; server_id = atoi(basename(argv[0])); /* len = 0; for (;;) { if (len != 0) break; else sleep(1); } */ init_db((char *)"127.0.0.1", 3306, (char *)"sanguo", (char *)"root", (char *)"123456"); printf("Content-Type:text/html\n\n"); lenstr=getenv("CONTENT_LENGTH"); if(lenstr == NULL) { printf("<DIV STYLE=\"COLOR:RED\">Errorarameters should be entered!</DIV>\n"); return (0); } len=atoi(lenstr) + 1; if (len >= 512) return (0); fgets(poststr,len,stdin); parse_post_data(poststr, len); player_id = atoi(get_value((char *)"userid")); cur_page = atoi(get_value((char *)"page")) - 1; if (cur_page < 0) cur_page = 0; start_pos = cur_page * page_size; // server_id = 15; // player_id = 36; char sql[512]; MYSQL_RES *res = NULL; MYSQL_ROW row; sprintf(sql, "select * from question where server_id = %d and player_id = %d", server_id, player_id); res = stored_query(sql); total_count = mysql_num_rows(res); total_page = total_count / page_size + 1; printf("[{\"tp\":%d,\"pn\":%d}", total_page, cur_page + 1); mysql_data_seek(res, start_pos); for (int i = start_pos; i < total_count; ++i) { row = fetch_row(res); printf(",{\"qc\":\"%s\",\"rc\":\"%s\",\"qs\":%s,\"qt\":\"%s\",\"rt\":\"%s\",\"rn\":\"%s\",\"qtp\":%s}", row[2], row[6] ? row[6] : "", row[5], row[3], row[7] ? row[7] : "", row[8] ? row[8] : "", row[4]); if (!row) break; } printf("]"); free_query(res); done: fflush(stdout); close_db(); return 0; }
static int submit_query(db_con_t* _h, const char* _s) { int rv; /* ** this bit of nonsense in case our connection get screwed up */ switch(rv = PQstatus(CON_CONNECTION(_h))) { case CONNECTION_OK: break; case CONNECTION_BAD: PLOG("submit_query", "connection reset"); PQreset(CON_CONNECTION(_h)); break; } /* ** free any previous query that is laying about */ if(CON_RESULT(_h)) { free_query(_h); } /* ** exec the query */ CON_RESULT(_h) = PQexec(CON_CONNECTION(_h), _s); rv = 0; if(PQresultStatus(CON_RESULT(_h)) == 0) { PLOG("submit_query", "initial failure, FATAL"); /* ** terrible error?? */ rv = -3; } else { /* ** the query ran, get the status */ switch(PQresultStatus(CON_RESULT(_h))) { case PGRES_EMPTY_QUERY: rv = -9; break; case PGRES_COMMAND_OK: rv = 0; break; case PGRES_TUPLES_OK: rv = 0; break; case PGRES_COPY_OUT: rv = -4; break; case PGRES_COPY_IN: rv = -5; break; case PGRES_BAD_RESPONSE: rv = -6; break; case PGRES_NONFATAL_ERROR: rv = -7; break; case PGRES_FATAL_ERROR: rv = -8; break; default: rv = -2; break; } } if(rv < 0) { /* ** log the error */ char buf[256]; sprintf(buf, "query '%s', result '%s'\n", _s, PQerrorMessage(CON_CONNECTION(_h))); PLOG("submit_query", buf); } return(rv); }
int main(int argc, char *argv[]) { const char *s, *query_args; time_t if_modified_since = 0; int i; umask(007); load_default_config(); if (parse_configfile(rssrollrc, config_cb) == -1) { render_error("error: cannot open config file: %s", rssrollrc); goto done; } /*if (chdir("/tmp")) { printf("error main: chdir: /tmp: %s", strerror(errno)); render_error("chdir: /tmp: %s", strerror(errno)); goto done; } */ if (sqlite3_open(rssroll->dbpath, &db) != SQLITE_OK) { render_error("cannot load database: %s", rssroll->dbpath); goto done; } // default feeds snprintf(query_category, sizeof(query_string), "%d", rssroll->defcat); snprintf(query_limit, sizeof(query_limit), "0"); if ((q = get_query()) == NULL) { render_error("get_query"); printf("error main: get_query() NULL"); goto done; } if ((s = getenv("QUERY_STRING")) != NULL) { if (strlen(s) > 64) { printf("Status: 400\r\n\r\n You are trying to send very long query!\n"); fflush(stdout); return (0); } else if (strstr(s, "&") != NULL) { printf("warning main: escaped query '%s', user agent '%s', " "referer '%s'", s, q->user_agent ? q->user_agent : "(null)", q->referer ? q->referer : "(null)"); printf("Status: 400\r\n\r\nHTML escaped ampersand in cgi " "query string \"%s\"\n" "This might be a problem in your client \"%s\",\n" "or in the referring document \"%s\"\n" "See http://www.htmlhelp.org/tools/validator/problems.html" "#amp\n", s, q->user_agent ? q->user_agent : "", q->referer ? q->referer : ""); fflush(stdout); return (0); } else { for (i = 0; i < strlen(s); i++) { /* sanity check of the query string, accepts only alpha */ if (!isdigit(s[i])) { if(s[i] != '/') { printf("Status: 400\r\n\r\nYou are trying to send wrong query!\n"); fflush(stdout); return (0); } } } snprintf(query_string, sizeof(query_string), "%s", s); query_args = strtok(query_string, "/"); snprintf(query_category, sizeof(query_category), "%s", query_args); query_args = strtok(NULL, "/"); if (query_args != NULL) snprintf(query_limit, sizeof(query_limit), "%s", query_args); } } if ((q->referer != NULL && strstr(q->referer, "morisit")) || (s != NULL && strstr(s, "http://"))) { printf("Status: 503\r\n\r\nWe are not redirecting, " "nice try.\n"); fflush(stdout); return (0); } if (q->user_agent != NULL && !strncmp(q->user_agent, "Googlebot", 9)) { printf("Status: 503\r\n\r\nGooglebot you are not.\n"); fflush(stdout); return (0); } if ((s = getenv("IF_MODIFIED_SINCE")) != NULL) { if_modified_since = convert_rfc822_time(s); if (!if_modified_since) if_modified_since = (time_t)strtoul(s, NULL, 10); if (!if_modified_since) printf("warning main: invalid IF_MODIFIED_SINCE '%s'", s); } if ((s = getenv("HTTP_ACCEPT_ENCODING")) != NULL) { char *p = strstr(s, "gzip"); if (p != NULL && (strncmp(p, "gzip;q=0", 8) || atoi(p + 7) > 0.0)) { gz = gzdopen(fileno(stdout), "wb9"); if (gz == NULL) printf("error main: gzdopen"); else printf("Content-Encoding: gzip\r\n"); } } char fn[1024]; printf("%s\r\n\r\n", rssroll->ct_html); fflush(stdout); snprintf(fn, sizeof(fn), "%s/main.html", rssroll->htmldir); render_html(fn, &render_front, NULL); done: if (gz != NULL) { if (gzclose(gz) != Z_OK) printf("error main: gzclose"); gz = NULL; } else fflush(stdout); if (q != NULL) free_query(q); sqlite3_close(db); return (0); }
int db_postgres_store_result(const db_con_t* _con, db_res_t** _r) { ExecStatusType pqresult; int rc = 0; *_r = db_new_result(); if (*_r==NULL) { LM_ERR("failed to init new result\n"); rc = -1; goto done; } pqresult = PQresultStatus(CON_RESULT(_con)); LM_DBG("%p PQresultStatus(%s) PQgetResult(%p)\n", _con, PQresStatus(pqresult), CON_RESULT(_con)); switch(pqresult) { case PGRES_COMMAND_OK: /* Successful completion of a command returning no data * (such as INSERT or UPDATE). */ rc = 0; break; case PGRES_TUPLES_OK: /* Successful completion of a command returning data * (such as a SELECT or SHOW). */ if (db_postgres_convert_result(_con, *_r) < 0) { LM_ERR("%p Error returned from convert_result()\n", _con); db_free_result(*_r); *_r = 0; rc = -4; break; } rc = 0; break; /* query failed */ case PGRES_FATAL_ERROR: LM_ERR("%p - invalid query, execution aborted\n", _con); LM_ERR("%p: %s\n", _con, PQresStatus(pqresult)); LM_ERR("%p: %s\n", _con, PQresultErrorMessage(CON_RESULT(_con))); db_free_result(*_r); *_r = 0; rc = -3; break; case PGRES_EMPTY_QUERY: /* notice or warning */ case PGRES_NONFATAL_ERROR: /* status for COPY command, not used */ case PGRES_COPY_OUT: case PGRES_COPY_IN: /* unexpected response */ case PGRES_BAD_RESPONSE: default: LM_ERR("%p Probable invalid query\n", _con); LM_ERR("%p: %s\n", _con, PQresStatus(pqresult)); LM_ERR("%p: %s\n", _con, PQresultErrorMessage(CON_RESULT(_con))); db_free_result(*_r); *_r = 0; rc = -4; break; } done: free_query(_con); return (rc); }
int main(void) { int len; char *lenstr,poststr[512]; char *player_id, *server_id, *token; setenv("http_proxy", "127.0.0.1:8087", 1); setenv("https_proxy", "127.0.0.1:8087", 1); init_db((char *)"127.0.0.1", 3306, (char *)"pay", (char *)"root", (char *)"123456"); printf("Content-Type:text/html\n\n"); /* lenstr=getenv("CONTENT_LENGTH"); if(lenstr == NULL) { printf("<DIV STYLE=\"COLOR:RED\">Errorarameters should be entered!</DIV>\n"); return (0); } len=atoi(lenstr) + 1; if (len >= 512) return (0); fgets(poststr,len,stdin); parse_post_data(poststr, len); len = 10; for (; len == 10;) sleep(2); */ lenstr = getenv("QUERY_STRING"); if(lenstr == NULL) { printf("<DIV STYLE=\"COLOR:RED\">Errorarameters should be entered!</DIV>\n"); return (0); } len = strlen(lenstr) + 1; parse_post_data(lenstr, len); server_id = get_value((char *)"server_id"); player_id = get_value((char *)"player_id"); token = get_value((char *)"token"); if (!server_id || !player_id || !token) return (0); // server_id = "15"; // player_id = "15"; // token = "AAACEdEose0cBAKedvUNeBVZAlpbsjUo4r2LHmR3K56gGFbJ4Gwp6PWVEp5wte4X4IdLK5YHziYabuXHCZCoXhzqmSZCKrsiZCLgOb1yjddnI2MyiZBmFK"; g_data.server_id = server_id; g_data.player_id = player_id; g_data.times = 0; MYSQL_RES *res = NULL; MYSQL_ROW row; char sql[256]; sprintf(sql, "select last_share_time, share_times from share where server_id = \"%s\" and player_id = \"%s\"", server_id, player_id); res = query(sql, 1, NULL); if (!res) { send_facebook_share(token, share_msg[0], &g_data); goto done; } row = fetch_row(res); if (!row) { send_facebook_share(token, share_msg[0], &g_data); goto done; } if (!check_can_share(row)) { send_fail(); goto done; } g_data.times = atoi(row[1]); free_query(res); res = NULL; send_facebook_share(token, share_msg[g_data.times], &g_data); // ret = atoi(row[0]); done: if (res) free_query(res); fflush(stdout); close_db(); return 0; }
static void query_menu(char *buf, size_t buflen, QUERY *results, int retbuf) { MUTTMENU *menu; HEADER *msg = NULL; ENTRY *QueryTable = NULL; QUERY *queryp = NULL; int i, done = 0; int op; char helpstr[LONG_STRING]; char title[STRING]; snprintf(title, sizeof(title), _("Query")); /* FIXME */ menu = mutt_new_menu(MENU_QUERY); menu->make_entry = query_entry; menu->search = query_search; menu->tag = query_tag; menu->title = title; menu->help = mutt_compile_help(helpstr, sizeof(helpstr), MENU_QUERY, QueryHelp); if (results == NULL) { /* Prompt for Query */ if ((mutt_get_field(_("Query: "), buf, buflen, 0) == 0) && buf[0]) { results = run_query(buf, 0); } } if (results) { snprintf(title, sizeof(title), _("Query '%s'"), buf); /* count the number of results */ for (queryp = results; queryp; queryp = queryp->next) menu->max++; menu->data = QueryTable = (ENTRY *)safe_calloc(menu->max, sizeof(ENTRY)); for (i = 0, queryp = results; queryp; queryp = queryp->next, i++) QueryTable[i].data = queryp; while (!done) { switch ((op = mutt_menuLoop(menu))) { case OP_QUERY_APPEND: case OP_QUERY: if ((mutt_get_field(_("Query: "), buf, buflen, 0) == 0) && buf[0]) { QUERY *newresults = NULL; newresults = run_query(buf, 0); menu->redraw = REDRAW_FULL; if (newresults) { snprintf(title, sizeof(title), _("Query '%s'"), buf); if (op == OP_QUERY) { free_query(&results); results = newresults; safe_free(&QueryTable); } else { /* append */ for (queryp = results; queryp->next; queryp = queryp->next) ; queryp->next = newresults; } menu->current = 0; mutt_menuDestroy(&menu); menu = mutt_new_menu(MENU_QUERY); menu->make_entry = query_entry; menu->search = query_search; menu->tag = query_tag; menu->title = title; menu->help = mutt_compile_help(helpstr, sizeof(helpstr), MENU_QUERY, QueryHelp); /* count the number of results */ for (queryp = results; queryp; queryp = queryp->next) menu->max++; if (op == OP_QUERY) { menu->data = QueryTable = (ENTRY *)safe_calloc(menu->max, sizeof(ENTRY)); for (i = 0, queryp = results; queryp; queryp = queryp->next, i++) QueryTable[i].data = queryp; } else { int clear = 0; /* append */ safe_realloc(&QueryTable, menu->max * sizeof(ENTRY)); menu->data = QueryTable; for (i = 0, queryp = results; queryp; queryp = queryp->next, i++) { /* once we hit new entries, clear/init the tag */ if (queryp == newresults) clear = 1; QueryTable[i].data = queryp; if (clear) QueryTable[i].tagged = 0; } } } } break; case OP_CREATE_ALIAS: if (menu->tagprefix) { ADDRESS *naddr = NULL; for (i = 0; i < menu->max; i++) if (QueryTable[i].tagged) { ADDRESS *a = result_to_addr(QueryTable[i].data); rfc822_append(&naddr, a, 0); rfc822_free_address(&a); } mutt_create_alias(NULL, naddr); } else { ADDRESS *a = result_to_addr(QueryTable[menu->current].data); mutt_create_alias(NULL, a); rfc822_free_address(&a); } break; case OP_GENERIC_SELECT_ENTRY: if (retbuf) { done = 2; break; } /* fall through to OP_MAIL */ case OP_MAIL: msg = mutt_new_header(); msg->env = mutt_new_envelope(); if (!menu->tagprefix) { msg->env->to = result_to_addr(QueryTable[menu->current].data); } else { for (i = 0; i < menu->max; i++) if (QueryTable[i].tagged) { ADDRESS *a = result_to_addr(QueryTable[i].data); rfc822_append(&msg->env->to, a, 0); rfc822_free_address(&a); } } ci_send_message(0, msg, NULL, Context, NULL); menu->redraw = REDRAW_FULL; break; case OP_EXIT: done = 1; break; } } /* if we need to return the selected entries */ if (retbuf && (done == 2)) { int tagged = 0; size_t curpos = 0; memset(buf, 0, buflen); /* check for tagged entries */ for (i = 0; i < menu->max; i++) { if (QueryTable[i].tagged) { if (curpos == 0) { ADDRESS *tmpa = result_to_addr(QueryTable[i].data); mutt_addrlist_to_local(tmpa); tagged = 1; rfc822_write_address(buf, buflen, tmpa, 0); curpos = mutt_strlen(buf); rfc822_free_address(&tmpa); } else if (curpos + 2 < buflen) { ADDRESS *tmpa = result_to_addr(QueryTable[i].data); mutt_addrlist_to_local(tmpa); strcat(buf, ", "); /* __STRCAT_CHECKED__ */ rfc822_write_address((char *)buf + curpos + 1, buflen - curpos - 1, tmpa, 0); curpos = mutt_strlen(buf); rfc822_free_address(&tmpa); } } } /* then enter current message */ if (!tagged) { ADDRESS *tmpa = result_to_addr(QueryTable[menu->current].data); mutt_addrlist_to_local(tmpa); rfc822_write_address(buf, buflen, tmpa, 0); rfc822_free_address(&tmpa); } } free_query(&results); safe_free(&QueryTable); /* tell whoever called me to redraw the screen when I return */ globals.set_option(OPTNEEDREDRAW); } mutt_menuDestroy(&menu); }
int main(void) { int len; char *lenstr,poststr[512]; char *player_id, *server_id, *pay_id; int n_pay_id; int n_pay_times; int can_share = 1; uint64_t effect = 0; MYSQL_RES *res = NULL; MYSQL_ROW row; char sql[256]; /* len = 10; for (;;) { if (len != 10) break; sleep(2); } */ init_db((char *)"127.0.0.1", 3306, (char *)"pay", (char *)"root", (char *)"123456"); printf("Content-Type:text/html\n\n"); lenstr=getenv("CONTENT_LENGTH"); if(lenstr == NULL) { printf("<DIV STYLE=\"COLOR:RED\">Errorarameters should be entered!</DIV>\n"); return (0); } len=atoi(lenstr) + 1; if (len >= 512) return (0); fgets(poststr,len,stdin); parse_post_data(poststr, len); server_id = get_value((char *)"server_id"); player_id = get_value((char *)"player_id"); pay_id = get_value((char *)"pay_id"); if (!server_id || !player_id || !pay_id) return (0); sprintf(sql, "select share_times, pay_times from share where server_id = %s and player_id = %s", server_id, player_id); res = query(sql, 1, NULL); if (!res) { send_fail(); goto done; } row = fetch_row(res); if (!row) { send_fail(); goto done; } n_pay_id = atoi(pay_id); if (!check_can_get_share_pay(row, n_pay_id)) { send_fail(); goto done; } n_pay_times = atoi(row[1]); n_pay_times |= (1 << n_pay_id); free_query(res); res = NULL; sprintf(sql, "update `share` set `pay_times` = %d where `server_id` = %s and `player_id` = %s", n_pay_times, server_id, player_id); query(sql, 1, &effect); if (effect != 1) { send_fail(); goto done; } send_success(n_pay_id); send_charge_gold_req(atoi(player_id), get_gold[n_pay_id], 0, (char *)"share reward", (char *)"127.0.0.1", (char *)"3008"); done: if (res) free_query(res); fflush(stdout); close_db(); return 0; }