/****************************************************************************** * * * Function : This function will select a redis database * * Returns : 0 (success), 1 (failure) * * * ******************************************************************************/ int redis_select_database(AGENT_RESULT *result, int *ret, char *zbx_key, redisContext **redisCptr, char *database) { // Declare Variables redisReply *redisR; // Run redis command if (redis_command(result, zbx_key, *redisCptr, &redisR, "SELECT", database, REDIS_REPLY_STATUS)) {return 1;} // If the database has not been selected if (strcmp(redisR->str,"OK") != 0) { // Set the return zbx_ret_fail(result, ret, LOG_LEVEL_DEBUG, zbx_key, "Redis database does not exist", redisR); return 1; } // Free the reply freeReplyObject(redisR); return 0; }
/****************************************************************************** * * * Function : This function will check if a redis key exists * * Returns : 0 (success), 1 (failure) * * * ******************************************************************************/ int redis_key_check_exists(AGENT_RESULT *result, int *ret, char *zbx_key, redisContext **redisCptr, char *key) { // Declare Variables redisReply *redisR; // Run redis command if (redis_command(result, zbx_key, *redisCptr, &redisR, "EXISTS", key, REDIS_REPLY_INTEGER)) {return 1;} // If the key does not exist if (redisR->integer != 1) { // Set the return zbx_ret_fail(result, ret, LOG_LEVEL_DEBUG, zbx_key, "Redis key does not exist", redisR); return 1; } // Free the reply freeReplyObject(redisR); return 0; }
/****************************************************************************** * * * Function : This function will check if a redis key type matches * * Returns : 0 (success), 1 (failure) * * * ******************************************************************************/ int redis_key_check_type(AGENT_RESULT *result, int *ret, char *zbx_key, redisContext **redisCptr, char *key, char *type) { // Declare Variables redisReply *redisR; // Run redis command if (redis_command(result, zbx_key, *redisCptr, &redisR, "TYPE", key, REDIS_REPLY_INTEGER)) {return 1;} // If the key is not the correct type if (strcmp(redisR->str,type) != 0) { // Set the return zbx_ret_fail(result, ret, LOG_LEVEL_DEBUG, zbx_key, "Redis key type does not match", redisR); return 1; } // Free the reply freeReplyObject(redisR); return 0; }
/****************************************************************************** * * * Function : This function will check if a redis hash field exists * * Returns : 0 (success), 1 (failure) * * * ******************************************************************************/ int redis_hash_field_check_exists(AGENT_RESULT *result, int *ret, char *zbx_key, redisContext **redisCptr, char *hash, char *field) { // Declare Variables redisReply *redisR; char redisParams[MAX_LENGTH_STRING]; // Append the parameters strcat(redisParams,hash); strcat(redisParams," "); strcat(redisParams,field); // Run redis command if (redis_command(result, zbx_key, *redisCptr, &redisR, "HEXISTS", redisParams, REDIS_REPLY_INTEGER)) {return 1;} // If the hash field does not exist if (redisR->integer != 1) { // Set the return zbx_ret_fail(result, ret, LOG_LEVEL_DEBUG, zbx_key, "Redis hash field does not exist", redisR); return 1; } // Free the reply freeReplyObject(redisR); return 0; }
int main(int argc,char *argv[]) { redis_client *c = redis_connect("10.210.210.146","8001"); if(!c || c->fd < 0)die("connect error"); redis_response *resp = redis_command(c,"set key abc"); if(resp) free_response(resp); resp = redis_command(c,"get key"); if(resp) { printf("%s\n",resp->data); } free_response(resp); free_client(c); return 0; }
string profile_delete() { string fid = arg(3); if(isset( fid ) ) { if(DB_TYPE==1) { redis_command("SREM profile_fields %d", intval(fid) ); redis_command("DEL profile_fields:%d", intval(fid) ); } if(DB_TYPE==2) { db_querya("DELETE FROM profile_fields WHERE fid=%d", fid.c_str() ); } } redirect( url("admin/profile") ); return ""; }
void aggregator_remove( map <string, string> feed ) { if(DB_TYPE==1) { string iid; REDIS_RES * result = redis_query("SMEMBERS aggregator_item:fid:%d", intval(feed["fid"]) ); while( redis_fetch( result, iid ) ) { redis_command("DEL aggregator_item:%d", intval(iid) ); redis_command("SREM aggregator_item %d", intval(iid) ); } redis_command("DEL aggregator_item:fid:%d", intval(feed["fid"]) ); redis_command("HMSET aggregator_feed:%d checked %d etag %s modified %d", intval(feed["fid"]), 0, "", 0 ); } if(DB_TYPE==2) { db_querya("DELETE FROM aggregator_item WHERE fid = %d", feed["fid"].c_str() ); db_querya("UPDATE aggregator_feed SET checked = 0, etag = '', modified = 0 WHERE fid = %d", feed["fid"].c_str() ); } set_page_message("The news items from "+feed["title"]+" have been removed."); }
void translation_save() { map <string, string> node = cur_node; if(DB_TYPE==1) { redis_command("HSET node:%d language %s", intval(node["nid"]), node["language"].c_str() ); } if(DB_TYPE==2) { db_querya("UPDATE node SET language='%s' WHERE nid=%d", node["language"].c_str(), node["nid"].c_str() ); } }
string profile_edit_submit() { if (!isset( cur_form["fid"]["#value"] ) ) { if(DB_TYPE==1) { int fid = redis_int("INCR profile_fields:ids"); redis_command("SADD profile_fields %d", fid); redis_command("HMSET profile_fields:%d title %s name %s explanation %s", fid, cur_form["title"]["#value"].c_str(), cur_form["name"]["#value"].c_str(), cur_form["explanation"]["#value"].c_str() ); } if(DB_TYPE==2) { db_querya("INSERT INTO profile_fields (title, name, explanation) VALUES ('%s', '%s', '%s')", cur_form["title"]["#value"].c_str(), cur_form["name"]["#value"].c_str(), cur_form["explanation"]["#value"].c_str() ); } } else { if(DB_TYPE==1) { int fid = intval( cur_form["fid"]["#value"] ); redis_command("HMSET profile_fields:%d title %s name %s explanation %s", fid, cur_form["title"]["#value"].c_str(), cur_form["name"]["#value"].c_str(), cur_form["explanation"]["#value"].c_str() ); } if(DB_TYPE==2) { db_querya("UPDATE profile_fields SET title = '%s', name = '%s', explanation = '%s' WHERE fid = %d", cur_form["title"]["#value"].c_str(), cur_form["name"]["#value"].c_str(), cur_form["explanation"]["#value"].c_str(), cur_form["fid"]["#value"].c_str() ); } } redirect( url("admin/profile") ); return ""; }
void profile_save_value( string field, string value, string uid ) { if( !isset(uid) ) uid = user["uid"]; _profile_get_fields(); if(DB_TYPE==1) { redis_command("HSET profile_values:%d %s %s", intval(uid), profile_field[ field ].c_str(), value.c_str() ); } if(DB_TYPE==2) { db_querya("DELETE FROM profile_values WHERE fid = %d AND uid = %d", profile_field[ field ].c_str(), uid.c_str() ); db_querya("INSERT INTO profile_values (fid, uid, value) VALUES (%d, %d, '%s')", profile_field[ field ].c_str(), uid.c_str(), value.c_str() ); } }
int32_t ClusterRedis::readwrite() { _redisReply = (redisReply *)redis_command("readwrite"); if (_redisReply) { if (_redisReply->type == REDIS_REPLY_STATUS && !strcmp(_redisReply->str, "OK")) { readonly_ = false; this->FreeSources(); return 0; } } this->FreeSources(); return -2; }
void profile_save_profile(map <string, string> &account) { _profile_get_fields(); if(DB_TYPE==1) { for( map <string, string>::iterator i = profile_field.begin(), end = profile_field.end(); i != end; i++ ) { redis_command("HSET profile_values:%d %s %s", intval(account["uid"]), i->second.c_str(), account[ i->first ].c_str() ); } } if(DB_TYPE==2) { for( map <string, string>::iterator i = profile_field.begin(), end = profile_field.end(); i != end; i++ ) { db_querya("DELETE FROM profile_values WHERE fid = %d AND uid = %d", i->second.c_str(), account["uid"].c_str() ); db_querya("INSERT INTO profile_values (fid, uid, value) VALUES (%d, %d, '%s')", i->second.c_str(), account["uid"].c_str(), account[ i->first ].c_str() ); } } }
void aggregator_save_feed( map <string, string> &edit ) { if(DB_TYPE==1) { if (isset(edit["fid"]) && isset(edit["title"])) { redis_command("HMSET aggregator_feed:%d title %s url %s refresh %d", intval(edit["fid"]), edit["title"].c_str(), edit["url"].c_str(), intval(edit["refresh"]) ); } else if (isset( edit["fid"] )) { string iid; redis_command("DEL aggregator_feed:%d", intval(edit["fid"]) ); REDIS_RES * result = redis_query("SMEMBERS aggregator_item:fid:%d", intval(edit["fid"]) ); while( redis_fetch( result, iid ) ) { redis_command("DEL aggregator_item:%d", intval(iid) ); redis_command("SREM aggregator_item %d", intval(iid) ); } redis_command("DEL aggregator_item:fid:%d", intval(edit["fid"]) ); } else if ( isset(edit["title"]) ) { edit["fid"] = redis_str("INCR aggregator_feed:ids"); redis_command("SADD aggregator_feed %d", intval(edit["fid"]) ); redis_command("HMSET aggregator_feed:%d title %s url %s refresh %d block %d", intval(edit["fid"]), edit["title"].c_str(), edit["url"].c_str(), intval(edit["refresh"]), 5 ); } } if(DB_TYPE==2) { if (isset(edit["fid"]) && isset(edit["title"])) { db_querya("UPDATE aggregator_feed SET title = '%s', url = '%s', refresh = %d WHERE fid = %d", edit["title"].c_str(), edit["url"].c_str(), edit["refresh"].c_str(), edit["fid"].c_str() ); } else if (isset( edit["fid"] )) { db_querya("DELETE FROM aggregator_feed WHERE fid = %d", edit["fid"].c_str() ); db_querya("DELETE FROM aggregator_item WHERE fid = %d", edit["fid"].c_str() ); } else if ( isset(edit["title"]) ) { db_querya("INSERT INTO aggregator_feed (title, url, refresh, block, description, image) VALUES ('%s', '%s', %d, 5, '', '')", edit["title"].c_str(), edit["url"].c_str(), edit["refresh"].c_str() ); edit["fid"] = db_last_insert_id(); } } }
void aggregator_save_item(map <string, string> edit) { if(DB_TYPE==1) { if ( isset(edit["iid"]) && isset(edit["title"]) ) { redis_command("HMSET aggregator_item:%d fid %d title %s link %s author %s description %s timestamp %d guid %s", intval(edit["iid"]), intval(edit["fid"]), edit["title"].c_str(), edit["link"].c_str(), edit["author"].c_str(), edit["description"].c_str(), intval(edit["timestamp"]), edit["guid"].c_str() ); } else if ( isset(edit["iid"]) ) { redis_command("SREM aggregator_item:fid:%d %d", redis_int("HGET aggregator_item:%d fid", intval(edit["iid"]) ), intval(edit["iid"]) ); redis_command("SREM aggregator_item %d", intval(edit["iid"]) ); redis_command("DEL aggregator_item:%d", intval(edit["iid"]) ); } else if ( isset( edit["title"] ) && isset( edit["link"] ) ) { edit["iid"] = redis_str("INCR aggregator_item:ids"); redis_command("SADD aggregator_item %d", intval(edit["iid"]) ); redis_command("SADD aggregator_item:fid:%d %d", intval(edit["fid"]), intval(edit["iid"]) ); redis_command("HMSET aggregator_item:%d fid %d title %s link %s author %s description %s timestamp %d guid %s", intval(edit["iid"]), intval(edit["fid"]), edit["title"].c_str(), edit["link"].c_str(), edit["author"].c_str(), edit["description"].c_str(), intval(edit["timestamp"]), edit["guid"].c_str() ); } } if(DB_TYPE==2) { if ( isset(edit["iid"]) && isset(edit["title"]) ) { db_querya("UPDATE aggregator_item SET title = '%s', link = '%s', author = '%s', description = '%s', guid = '%s', timestamp = %d WHERE iid = %d", edit["title"].c_str(), edit["link"].c_str(), edit["author"].c_str(), edit["description"].c_str(), edit["guid"].c_str(), edit["timestamp"].c_str(), edit["iid"].c_str() ); } else if ( isset(edit["iid"]) ) { db_querya("DELETE FROM aggregator_item WHERE iid = %d", edit["iid"].c_str() ); } else if ( isset( edit["title"] ) && isset( edit["link"] ) ) { db_querya("INSERT INTO aggregator_item (fid, title, link, author, description, timestamp, guid) VALUES (%d, '%s', '%s', '%s', '%s', %d, '%s')", edit["fid"].c_str(), edit["title"].c_str(), edit["link"].c_str(), edit["author"].c_str(), edit["description"].c_str(), edit["timestamp"].c_str(), edit["guid"].c_str() ); } edit["iid"] = db_last_insert_id(); } }
void aggregator_refresh(map <string, string> &feed) { // Generate conditional GET headers. map <string, string> headers; if ( isset(feed["etag"]) ) { headers["If-None-Match"] = feed["etag"]; } if ( isset(feed["modified"]) ) { headers["If-Modified-Since"] = gmdate("D, d M Y H:i:s", feed["modified"]) + " GMT"; } map <string, string> result; // Request feed. http_request( result, feed["url"], headers ); // Process HTTP response code. switch ( intval( result["response_code"] ) ) { case 304: if(DB_TYPE==1) redis_command("HSET aggregator_feed:%d checked %d", intval(feed["fid"]), time() ); if(DB_TYPE==2) db_querya("UPDATE aggregator_feed SET checked = %d WHERE fid = %d", str( time() ).c_str(), feed["fid"].c_str() ); set_page_message( "There is no new syndicated content from " + feed["title"] ); break; case 301: feed["url"] = result["redirect_url"]; case 200: case 302: case 307: // Filter the input data: if ( aggregator_parse_feed( result["data"], feed ) ) { string modified = !isset2(result,"Last-Modified") ? "0" : strtotime( result["Last-Modified"] ); // Prepare the channel data. for( map <string, string>::iterator curr = channel.begin(), end = channel.end(); curr != end; curr++ ) { trim( curr->second ); channel[curr->first] = curr->second; } // Prepare the image data (if any). for( map <string, string>::iterator curr = image.begin(), end = image.end(); curr != end; curr++ ) { trim( curr->second ); image[curr->first] = curr->second; } string str_image; if (isset(image["LINK"]) && isset(image["URL"]) && isset(image["TITLE"]) ) { // Note, we should really use theme_image() here but that only works with local images it won't work with images fetched with a URL unless PHP version > 5 str_image = "<a href=\""+ check_url(image["LINK"]) +"\" class=\"feed-image\"><img src=\"" + check_url(image["URL"]) + "\" alt=\"" + check_plain(image["TITLE"]) +"\" /></a>"; } else { str_image = ""; } string etag = !isset(result["ETag"]) ? "" : result["ETag"]; // Update the feed data. if(DB_TYPE==1) { redis_command_fields(redis_arg("HMSET aggregator_feed:%d", intval(feed["fid"]) ), "", "url,checked,link,description,image,etag,modified", "%s,%d,%s,%s,%s,%s,%d", feed["url"].c_str(), time(), channel["LINK"].c_str(), channel["DESCRIPTION"].c_str(), str_image.c_str(), etag.c_str(), intval( modified ) ); } if(DB_TYPE==2) { db_querya("UPDATE aggregator_feed SET url = '%s', checked = %d, link = '%s', description = '%s', image = '%s', etag = '%s', modified = %d WHERE fid = %d", feed["url"].c_str(), str(time()).c_str(), channel["LINK"].c_str(), channel["DESCRIPTION"].c_str(), str_image.c_str(), etag.c_str(), modified.c_str(), feed["fid"].c_str() ); } set_page_message( "There is new syndicated content from " + feed["title"] ); break; } result["error"] = "feed not parseable"; default: set_page_message( "The feed from "+feed["title"]+" seems to be broken, because of error \""+result["response_code"]+"\". "+ result["error"] ); } }