Beispiel #1
0
int WorldSocket::Update(void)
{
    if (closing_)
        return -1;

    if (m_OutActive || m_OutBuffer->length() == 0)
        return 0;

    return handle_output(get_handle());
}
Beispiel #2
0
/*---------------------------------------------------------------------------*/
static void
appcall(void *state)
{
  if(uip_closed() || uip_aborted() || uip_timedout()) {
    ctkmode();
  } else if(uip_connected()) {
  } else {
    handle_input();
    handle_output();   
  }
}
Beispiel #3
0
int WorldSocket::Update (void)
{
    if (closing_)
        return -1;

    if (m_OutActive || (m_OutBuffer->length() == 0 && msg_queue()->is_empty()))
        return 0;

    int ret;
    do
        ret = handle_output (get_handle());
    while (ret > 0);

    return ret;
}
Beispiel #4
0
int usb_vprintf(const char *format, va_list args)
{
	int ret;
	struct queue state;

	if (is_readonly)
		return EC_SUCCESS;

	ret = usb_wait_console();
	if (ret)
		return ret;

	state = tx_q;
	ret = vfnprintf(__tx_char, &state, format, args);

	if (queue_count(&state))
		handle_output();

	return ret;
}
Beispiel #5
0
//Hanlde "all" requests to/from server, pass off to child funcs in
// rcon-handlers.c for specific API hooking
int root_handler(struct mg_connection *conn, json_t* jreq, json_t* jrsp){
    //Get current "action" request
    json_t* action = json_object_get(jreq,"action");
    if (!action){
        //no action found, set error handling
        json_object_set_new(jrsp,"error",json_string("no 'action' key in request (please POST)"));
        return 400;
    }

    //test handler, echos back some basic data about the request
    if(cmpjstr(action,"version")){
        return handle_version(conn,jreq,jrsp);
    }
    else if(cmpjstr(action,"api/hotkey")){
        return handle_hotkey(jreq,jrsp);
    }
    else if(cmpjstr(action,"api/output")){
        return handle_output(jreq,jrsp);
    }
    else if(cmpjstr(action,"plugin")){
        for (size_t i=0; i < rcon_data.plugin_handlers.num; i++){
            struct rcon_handler hndl = rcon_data.plugin_handlers.array[i];
            if (cmpjstr(json_object_get(jreq,"plugin_action"),hndl.action)){
                return hndl.handle_func(conn,jreq,jrsp);
            }
        }
        json_object_set_new(jrsp,"error",json_string("no matching 'plugin_action' found for request."));
        return 400;
    }
    else {
        json_object_set_new(jrsp,"error",json_string("no matching 'action' found for request."));

        if (jreq){
            json_object_set(jrsp, "requestJSON", jreq);
        }
        else{
            json_object_set_new(jrsp,"requestJSON", json_null());
        }
        return 400;
    }
}
int WorldSocket::Update (void)
{
    if (closing_)
        return -1;

    if (m_OutActive)
        return 0;

    {
        ACE_GUARD_RETURN(LockType, Guard, m_OutBufferLock, 0);
        if (m_OutBuffer->length() == 0 && msg_queue()->is_empty())
            return 0;
    }

    int ret;
    do
    ret = handle_output(get_handle());
    while (ret > 0);

    return ret;
}
Beispiel #7
0
int usb_puts(const char *outstr)
{
	int ret;
	struct queue state;

	if (is_readonly)
		return EC_SUCCESS;

	ret  = usb_wait_console();
	if (ret)
		return ret;

	state = tx_q;
	while (*outstr)
		if (__tx_char(&state, *outstr++))
			break;

	if (queue_count(&state))
		handle_output();

	return *outstr ? EC_ERROR_OVERFLOW : EC_SUCCESS;
}
static void
jcrusher_handle_transform(TSCont contp)
{
  JCrusherData *data;
  int done;

  /* Get our data structure for this operation. The private data
     structure contains the output VIO and output buffer. If the
     private data structure pointer is NULL, then we'll create it
     and initialize its internals. */

  TSDebug("jcrusher", "Start of handle_transform()");
  data = TSContDataGet(contp);
  if (!data) {
    data = jcrusher_data_alloc();
    TSContDataSet(contp, data);
  }

  do {
    switch (data->state) {
    case STATE_BUFFER_DATA:
      TSDebug("jcrusher", "data->state is STATE_BUFFER_DATA");
      done = handle_buffering(contp, data);
      break;
    case STATE_OUTPUT_DATA:
      TSDebug("jcrusher", "data->state is STATE_OUTPUT_DATA");
      done = handle_output(contp, data);
      break;
    default:
      TSDebug("jcrusher", "data->state is UNKNOWN");
      done = 1;
      break;
    }
  } while (!done);

  TSDebug("jcrusher", "End of handle_transform()");
}
Beispiel #9
0
//分析http数据
static void handle_connection(struct httpd_state *s)
{
	handle_input(s);  //处理http输入数据
	if(s->state==STATE_OUTPUT)handle_output(s);//输出状态,处理输出数据 
}
Beispiel #10
0
void *
edcast_thread (void *arg)
{
	jack_thread_info_t *info = (jack_thread_info_t *) arg;
	static jack_nframes_t total_captured = 0;
	//jack_nframes_t samples_per_frame = info->channels * 2048;
	jack_nframes_t samples_per_frame = 2048;
	size_t bytes_per_frame = samples_per_frame * sample_size;
	float *framebuf_r = (float *)malloc (bytes_per_frame);
	float *framebuf_l = (float *)malloc (bytes_per_frame);
	float *framebuf = (float *)malloc (bytes_per_frame*2);

	pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
	pthread_mutex_lock (&edcast_thread_lock);

	info->status = 0;

	while (1) {

/*
		if (rb) {
			int x = jack_ringbuffer_read_space (rb[0]);
			printf("We've got %d in the ringbuffer\n", x);
		}
*/
		while (info->can_capture &&
		       (jack_ringbuffer_read_space (rb[0]) >= bytes_per_frame)) {

			jack_ringbuffer_read (rb[0], (char *)framebuf_r, bytes_per_frame);
			if (currentChannels == 2) {
				jack_ringbuffer_read (rb[1], (char *)framebuf_l, bytes_per_frame);
			}
			int samplecounter = 0;
			for (int i=0;i<samples_per_frame;i++) {
				framebuf[samplecounter] = framebuf_r[i];
				samplecounter++;
				if (currentChannels == 2) {
					framebuf[samplecounter] = framebuf_l[i];
					samplecounter++;
				}
			}
	
			handle_output(&g, (float *)framebuf, samples_per_frame, currentChannels, currentSamplerate);
			if (changeMetadata) {
				char buf[1024] = "";
				setCurrentSongTitleURL(&g, currentTitle);

				sprintf(buf, "Changing metadata to %s", currentTitle);
				serverStatusCallback(&g, (void *)buf);
				changeMetadata = 0;
			}
		}

		/* wait until process() signals more data */
		pthread_cond_wait (&data_ready, &edcast_thread_lock);
	}

 done:
	pthread_mutex_unlock (&edcast_thread_lock);
	free (framebuf);
	free (framebuf_l);
	free (framebuf_r);
	return 0;
}
Beispiel #11
0
/// Read data from the network
int RASocket::handle_input(ACE_HANDLE)
{
    DEBUG_LOG("RASocket::handle_input");
    if(closing_)
    {
        sLog.outError("Called RASocket::handle_input with closing_ = true");
        return -1;
    }

    size_t readBytes = peer().recv(inputBuffer+inputBufferLen, RA_BUFF_SIZE-inputBufferLen-1);

    if(readBytes <= 0)
    {
        DEBUG_LOG("read %u bytes in RASocket::handle_input", readBytes);
        return -1;
    }

    ///- Discard data after line break or line feed
    bool gotenter=false;
    for(; readBytes > 0 ; --readBytes)
    {
        char c = inputBuffer[inputBufferLen];
        if (c=='\r'|| c=='\n')
        {
            gotenter=true;
            break;
        }
        ++inputBufferLen;
    }

    if (gotenter)
    {
        inputBuffer[inputBufferLen]=0;
        inputBufferLen=0;
        switch(stage)
        {
            /// <ul> <li> If the input is '<username>'
            case NONE:
            {
                std::string szLogin=inputBuffer;

                accId = sAccountMgr.GetId(szLogin);

                ///- If the user is not found, deny access
                if(!accId)
                {
                    sendf("-No such user.\r\n");
                    sLog.out(LOG_RA, "User %s does not exist.",szLogin.c_str());
                    if(bSecure)
                    {
                        handle_output();
                        return -1;
                    }
                    sendf("\r\n");
                    sendf(sObjectMgr.GetMangosStringForDBCLocale(LANG_RA_USER));
                    break;
                }

                accAccessLevel = sAccountMgr.GetSecurity(accId);

                ///- if gmlevel is too low, deny access
                if (accAccessLevel < iMinLevel)
                {
                    sendf("-Not enough privileges.\r\n");
                    sLog.out(LOG_RA, "User %s has no privilege.",szLogin.c_str());
                    if(bSecure)
                    {
                        handle_output();
                        return -1;
                    }
                    sendf("\r\n");
                    sendf(sObjectMgr.GetMangosStringForDBCLocale(LANG_RA_USER));
                    break;
                }

                ///- allow by remotely connected admin use console level commands dependent from config setting
                if (accAccessLevel >= SEC_ADMINISTRATOR && !bStricted)
                    accAccessLevel = SEC_CONSOLE;

                stage=LG;
                sendf(sObjectMgr.GetMangosStringForDBCLocale(LANG_RA_PASS));
                break;
            }
            ///<li> If the input is '<password>' (and the user already gave his username)
            case LG:
            {                                               //login+pass ok
                std::string pw = inputBuffer;

                if (sAccountMgr.CheckPassword(accId, pw))
                {
                    stage=OK;

                    sendf("+Logged in.\r\n");
                    sLog.out(LOG_RA, "User account %u has logged in.", accId);
                    sendf("mangos>");
                }
                else
                {
                    ///- Else deny access
                    sendf("-Wrong pass.\r\n");
                    sLog.out(LOG_RA, "User account %u has failed to log in.", accId);
                    if(bSecure)
                    {
                        handle_output();
                        return -1;
                    }
                    sendf("\r\n");
                    sendf(sObjectMgr.GetMangosStringForDBCLocale(LANG_RA_PASS));
                }
                break;
            }
            ///<li> If user is logged, parse and execute the command
            case OK:
                if (strlen(inputBuffer))
                {
                    sLog.out(LOG_RA, "Got '%s' cmd.",inputBuffer);
                    if (strncmp(inputBuffer,"quit",4)==0)
                        return -1;
                    else
                    {
                        CliCommandHolder* cmd = new CliCommandHolder(accId, accAccessLevel, this, inputBuffer, &RASocket::zprint, &RASocket::commandFinished);
                        sWorld.QueueCliCommand(cmd);
                        pendingCommands.acquire();
                    }
                }
                else
                    sendf("mangos>");
                break;
            ///</ul>
        };

    }
    // no enter yet? wait for next input...
    return 0;
}
Beispiel #12
0
/*---------------------------------------------------------------------------*/
static void handle_connection(struct httpd_state *s) {
	handle_input(s);
	if(s->state == STATE_OUTPUT) {
		handle_output(s);
	}
}
int CCSocketHandler::Send(const char * buff, int len)
{
	if(len>0)
	{
		if (GetReconnectFlag()==true){
			Connect();
		}
		const char* sendbuff = buff;
		int sendlen = len;

		if(this->_w.data_len()==0)
		{
			int ret = ::send (netfd,buff, len, 0);
			log_debug("Send Fd[%d]", netfd);
			if(-1 == ret)
			{
				if(errno == EINTR || errno == EAGAIN || errno == EINPROGRESS)
				{
					log_boot("sending,INTR|EAGAIN|EINPROGRESS,errno:[%d]", errno);
					this->_w.append(sendbuff, sendlen);
					EnableOutput ();
					ApplyEvents ();
					_stage = CONN_DATA_SENDING;
					return 0;
				}
				else
				{
					log_boot ("sending package to client failed, ret[%d]",  ret);	
					_stage = CONN_FATAL_ERROR;
					this->OnClose();
					if(GetReconnectFlag()==false){//����Ҫ����
						delete this;
					}
					else{
						Reset();
					}
					return -1;
				}
			}
			else if(ret<len)
			{
				sendbuff += ret;
				sendlen -=  ret;
				this->_w.append(sendbuff, sendlen);
				EnableOutput ();
				ApplyEvents ();
				_stage = CONN_DATA_SENDING;
				log_debug("had sent part of data, send len=[%d]",ret);
				return ret;
			}
			else if(ret==len)
			{
				log_debug("send complete, send len=[%d]",len);
				_stage = CONN_SEND_DONE;
				return ret;
			}
		}
		else
		{
			this->_w.append(sendbuff, sendlen);
			if( handle_output() ==CONN_FATAL_ERROR )
			{
				this->OnClose();
				if(GetReconnectFlag()==false){//����Ҫ����
					delete this;
				}
				else{
					Reset();
				}
				return -1;
			}
			else
				return len;
		}
	}
	return len;
}
/* definesc functia de vizualizareaa a news */
int sql_new_view(int id)
{
	/* declar raspunsul */
	int ans;
	
	/* declar variabila care retine interogarea */
	char *query = NULL;

	/* declar variabila care retine conexiunea la serverul MySQL */
	MYSQL *conn = NULL;

	/* declar variabila care retine rezultatul interogarii */
	MYSQL_RES *result = NULL;

	/* declar variabila care retine randul interogarii */
	MYSQL_ROW row;	

	/* aloc memorie pentru query */
	if ((query = (char *)realloc(query, sizeof(char) * (128 + inlength))) == NULL)
		handle_error("newssql::sql_new_view::realloc(query, 128 + inlength)");

	/* initializez conexiunea la baza de date, CU TRATAREA ERORILOR */
	if ((conn = mysql_init(NULL)) == NULL)
		handle_error("newssql::sql_new_view::mysql_init(conn)");

	/* conectez la baza de date, CU TRATAREA ERORILOR */
	if (mysql_real_connect(conn, "localhost", "root", "root", "rcnews", 0, NULL, 0) == NULL)
		handle_error("newssql::sql_new_view::mysql_real_connect(conn, rcnews)");

	/* creez sirul de caractere pentru query SELECT */
	sprintf(query, "SELECT * FROM news WHERE id ='%d';", id);

	/* gasesc utilizatorul, CU TRATAREA ERORILOR */
	if (mysql_query(conn, query))
		handle_error("newssql::sql_new_view::mysql_query(conn, select)");

	/* retin rezultatul */
	if ((result = mysql_store_result(conn)) == NULL)
		handle_error("newssql::sql_new_view::mysql_store_result(conn, select)");	

	/* parcurg intrarile selectate din baza de date */
	if (row = mysql_fetch_row(result))
	{
		/* construiesc raspunsul */
		handle_output("success", 0);
		
		/* scriu bara in raspuns */
		handle_output("|", 1);

		/* scriu identificatorul stirii */
		handle_output(row[0], 1);

		/* scriu bara in raspuns */
		handle_output("|", 1);

		/* scriu numele stirii */
		handle_output(row[1], 1);

		/* scriu bara in raspuns */
		handle_output("|", 1);

		/* scriu textul stirii */
		handle_output(row[2], 1);

		/* scriu bara in raspuns */
		handle_output("|", 1);

		/* scriu momentul stirii */
		handle_output(row[3], 1);

		/* scriu bara in raspuns */
		handle_output("|", 1);

		/* scriu categoria parinte stirii */
		handle_output(row[4], 1);

		/* scriu bara in raspuns */
		handle_output("|", 1);

		/* scriu identificatorul propunatorului stirii */
		handle_output(row[5], 1);

		/* raspunsul este valid */
		ans = 1;
	}
	else
	{
		/* scriu eroare de id */
		handle_output(errors[1], 0);

		/* raspunsul este invalid */
		ans = 0;
	}

	/* eliberez rezultatul */
	mysql_free_result(result);

	/* inchid conexiunea la baza de date */
	mysql_close(conn);

	/* intorc starea */
	return ans;
}
/* definesc functia recursiva de vizualizare a stirilor */
void sql_news_view(int catid, int levmax, int lev, int limit)
{
	/* declar variabila care verifica validitatea categoriei */
	int user;

	/* declar variabila care retine identificatorul categoriei curente */
	int id;

	/* declar variabila care retine numarul nivelului stirii */
	char number[11];

	/* declar variabila care retine interogarea */
	char *query = NULL;

	/* declar variabila care retine conexiunea la serverul MySQL */
	MYSQL *conn = NULL;

	/* declar variabila care retine rezultatul interogarii */
	MYSQL_RES *result = NULL;

	/* declar variabila care retine randul interogarii */
	MYSQL_ROW row;

	/* verific sa nu fi depasit nivelul */
	if (levmax > 0 && lev >= levmax)
	{
		/* intorc */
		return;
	}

	/* initializez conexiunea la baza de date, CU TRATAREA ERORILOR */
	if ((conn = mysql_init(NULL)) == NULL)
		handle_error("newssql::sql_news_view::mysql_init(conn)");

	/* conectez la baza de date, CU TRATAREA ERORILOR */
	if (mysql_real_connect(conn, "localhost", "root", "root", "rcnews", 0, NULL, 0) == NULL)
		handle_error("newssql::sql_news_view::mysql_real_connect(conn, rcnews)");

	/* aloc memorie pentru query */
	if ((query = (char *)realloc(query, sizeof(char) * (128 + inlength))) == NULL)
		handle_error("newssql::sql_news_view::realloc(query, 128 + inlength)");

	/* verific validitatea categoriei */
	if ((user = sql_cat_user_exists(catid)) <= 0)
	{
		/* creez sirul de caractere pentru query SELECT */
		sprintf(query, "SELECT id FROM categories WHERE parent_id IS NULL;");

		/* gasesc categoriile, CU TRATAREA ERORILOR */
		if (mysql_query(conn, query))
			handle_error("newssql::sql_news_view::mysql_query(conn, select)");

		/* retin rezultatul */
		if ((result = mysql_store_result(conn)) == NULL)
			handle_error("newssql::sql_news_view::mysql_store_result(conn, select)");	

		/* parcurg intrarile selectate din baza de date */
		while (row = mysql_fetch_row(result))
		{
			/* retin valoarea identificatorului*/
			if ((id = atoi(row[0])) > 0)
			{
				/* reapelez cu categorie valida */
				sql_news_view(id, levmax, lev, limit); 
			}
	
			/* tratez si celalalt caz */
			else
			{
				/* trec la randul urmator extras */
				continue;
			}
		}
	}
	
	/* tratez cazul cand categoria e valida */
	else
	{
		/* creez sirul de caractere pentru query SELECT */
		if (limit > 0)		
			sprintf(query, "SELECT id, name, text, moment, category_id, user_id FROM news WHERE category_id = '%d' ORDER BY moment DESC LIMIT %d;", catid, limit);
		else
			sprintf(query, "SELECT id, name, text, moment, category_id, user_id FROM news WHERE category_id = '%d' ORDER BY moment DESC;", catid);

		/* gasesc stirile, CU TRATAREA ERORILOR */
		if (mysql_query(conn, query))
			handle_error("newssql::sql_news_view::mysql_query(conn, select)");

		/* retin rezultatul */
		if ((result = mysql_store_result(conn)) == NULL)
			handle_error("newssql::sql_news_view::mysql_store_result(conn, select)");	

		/* parcurg intrarile selectate din baza de date */
		while (row = mysql_fetch_row(result))
		{
			/* construiesc numarul */
			sprintf(number, "%d", lev);

			/* scriu bara in raspus */
			handle_output("|", 1);

			/* scriu nivelul in raspuns */
			handle_output(number, 1);

			/* scriu bara in raspus */
			handle_output("|", 1);

			/* scriu identificatorul stirii */
			handle_output(row[0], 1);
			
			/* scriu bara in raspus */
			handle_output("|", 1);

			/* scriu numele stirii */
			handle_output(row[1], 1);

			/* scriu bara in raspus */
			handle_output("|", 1);

			/* scriu textul stirii */
			handle_output(row[2], 1);

			/* scriu bara in raspus */
			handle_output("|", 1);

			/* scriu momentul stirii */
			handle_output(row[3], 1);

			/* scriu bara in raspus */
			handle_output("|", 1);

			/* scriu categoria stirii */
			handle_output(row[4], 1);

			/* scriu bara in raspus */
			handle_output("|", 1);

			/* scriu identificatorul propunatorului stirii */
			handle_output(row[5], 1);
		}

		/* creez sirul de caractere pentru query SELECT */
		sprintf(query, "SELECT id FROM categories WHERE parent_id = '%d';", catid);

		/* gasesc categoriile, CU TRATAREA ERORILOR */
		if (mysql_query(conn, query))
			handle_error("newssql::sql_news_view::mysql_query(conn, select)");

		/* retin rezultatul */
		if ((result = mysql_store_result(conn)) == NULL)
			handle_error("newssql::sql_news_view::mysql_store_result(conn, select)");	

		/* parcurg intrarile selectate din baza de date */
		while (row = mysql_fetch_row(result))
		{
			/* retin valoarea identificatorului*/
			if ((id = atoi(row[0])) > 0)
			{
				/* reapelez cu categorie valida */
				sql_news_view(id, levmax, lev + 1, limit); 
			}

			/* tratez si celalalt caz */
			else
			{
				/* trec la randul urmator extras */
				continue;
			}
		}
	}

	/* eliberez rezultatul */
	mysql_free_result(result);

	/* inchid conexiunea la baza de date */
	mysql_close(conn);
}
Beispiel #16
0
/* definesc functia de vizualizarea a utilizatorilor */
void sql_users_view()
{
	/* declar variabila care retine interogarea */
	char *query = NULL;

	/* declar variabila care retine conexiunea la serverul MySQL */
	MYSQL *conn = NULL;

	/* declar variabila care retine rezultatul interogarii */
	MYSQL_RES *result = NULL;

	/* declar variabila care retine randul interogarii */
	MYSQL_ROW row;	

	/* aloc memorie pentru query */
	if ((query = (char *)realloc(query, sizeof(char) * (128 + inlength))) == NULL)
		handle_error("userssql::sql_users_view::realloc(query, 128 + inlength)");

	/* initializez conexiunea la baza de date, CU TRATAREA ERORILOR */
	if ((conn = mysql_init(NULL)) == NULL)
		handle_error("userssql::sql_users_view::mysql_init(conn)");

	/* conectez la baza de date, CU TRATAREA ERORILOR */
	if (mysql_real_connect(conn, "localhost", "root", "root", "rcnews", 0, NULL, 0) == NULL)
		handle_error("userssql::sql_users_view::mysql_real_connect(conn, rcnews)");

	/* creez sirul de caractere pentru query SELECT */
	strcpy(query, "SELECT id, username, permission, moment FROM users ORDER BY username ASC;");

	/* gasesc utilizatorul, CU TRATAREA ERORILOR */
	if (mysql_query(conn, query))
		handle_error("userssql::sql_users_view::mysql_query(conn, select)");

	if ((result = mysql_store_result(conn)) == NULL)
		handle_error("userssql::sql_users_view::mysql_store_result(conn, select)");	

	/* construiesc raspunsul */
	handle_output("success", 0);

	/* parcurg intrarile selectate din baza de date */
	while (row = mysql_fetch_row(result))
	{
		/* adaug o bara incremental la raspuns*/
		handle_output("|", 1);

		/* adaug identificatorul utilizatorului curent */
		handle_output(row[0], 1);

		/* adaug o bara incremental la raspuns*/
		handle_output("|", 1);

		/* adaug numele utilizatorului curent */
		handle_output(row[1], 1);

		/* adaug o bara incremental la raspuns*/
		handle_output("|", 1);

		/* adaug momentul utilizatorului curent */
		handle_output(row[2], 1);
     
		/* adaug o bara incremental la raspuns*/
		handle_output("|", 1);

		/* adaug drepturile utilizatorului curent */
		handle_output(row[3], 1);
	}

	/* eliberez rezultatul */
	mysql_free_result(result);

	/* inchid conexiunea la baza de date */
	mysql_close(conn);
}
Beispiel #17
0
/* definesc functia de conectare a unui utilizator */
int sql_user_connect(char *username, char *password)
{
	/* declar si initializez (0) numarul de utilizatori rezultati */
	int ans;
	
	/* declar variabila care retine interogarea */
	char *query = NULL;

	/* declar variabila care retine conexiunea la serverul MySQL */
	MYSQL *conn = NULL;

	/* declar variabila care retine rezultatul interogarii */
	MYSQL_RES *result = NULL;

	/* declar variabila care retine randul interogarii */
	MYSQL_ROW row;	

	/* aloc memorie pentru query */
	if ((query = (char *)realloc(query, sizeof(char) * (128 + inlength))) == NULL)
		handle_error("userssql::sql_user_connect::realloc(query, 128 + inlength)");

	/* initializez conexiunea la baza de date, CU TRATAREA ERORILOR */
	if ((conn = mysql_init(NULL)) == NULL)
		handle_error("userssql::sql_user_connect::mysql_init(conn)");

	/* conectez la baza de date, CU TRATAREA ERORILOR */
	if (mysql_real_connect(conn, "localhost", "root", "root", "rcnews", 0, NULL, 0) == NULL)
		handle_error("userssql::sql_user_connect::mysql_real_connect(conn, rcnews)");

	/* creez sirul de caractere pentru query SELECT */
	sprintf(query, "SELECT id, permission FROM users WHERE username = '******' and password = '******';", username, password);

	/* gasesc utilizatorul, CU TRATAREA ERORILOR */
	if (mysql_query(conn, query))
		handle_error("userssql::sql_user_connect::mysql_query(conn, select)");

	if ((result = mysql_store_result(conn)) == NULL)
		handle_error("userssql::sql_user_connect::mysql_store_result(conn, select)");	

	/* verific valoarea raspunsului */
	if (row = mysql_fetch_row(result))
	{
		/* retin idul */
		connected = atoi(row[0]);

		/* retin permisiunea */
		permitted = atoi(row[1]); 

		/* construiesc raspunsul */
		handle_output("success|", 0);

		/* scriu identificatorul in raspuns */
		handle_output(row[0], 1);
		
		/* scriu o bara in raspuns */
		handle_output("|", 1);

		/* scriu numele in raspuns */
		handle_output(row[1], 1);

		/* consider valid raspunsul */
		ans = 1;
	}

	/* tratez cazul cand nu s-a putut face conectarea */
	else
	{
		/* consider invalid raspunsul */
		ans = 0;
	}

	/* eliberez rezultatul */
	mysql_free_result(result);

	/* inchid conexiunea la baza de date */
	mysql_close(conn);

	/* intorc starea */
	return ans;
}
Beispiel #18
0
/* definesc functia de vizualizarea a utilizatorilor */
int sql_user_view(int id)
{
	/* declar si initializez (0) numarul de utilizatori rezultati */
	int ans;
	
	/* declar variabila care retine interogarea */
	char *query = NULL;

	/* declar variabila care retine conexiunea la serverul MySQL */
	MYSQL *conn = NULL;

	/* declar variabila care retine rezultatul interogarii */
	MYSQL_RES *result = NULL;

	/* declar variabila care retine randul interogarii */
	MYSQL_ROW row;	

	/* aloc memorie pentru query */
	if ((tmp = (char *)realloc(query, sizeof(char) * (128 + inlength))) == NULL)
		handle_error("userssql::sql_user_view::realloc(query, 128 + inlength)");
	else
		query = tmp;

	/* initializez conexiunea la baza de date, CU TRATAREA ERORILOR */
	if ((conn = mysql_init(NULL)) == NULL)
		handle_error("userssql::sql_user_view::mysql_init(conn)");

	/* conectez la baza de date, CU TRATAREA ERORILOR */
	if (mysql_real_connect(conn, "localhost", "root", "root", "rcnews", 0, NULL, 0) == NULL)
		handle_error("userssql::sql_user_view::mysql_real_connect(conn, rcnews)");

	/* creez sirul de caractere pentru query SELECT */
	sprintf(query, "SELECT id, username, permission, moment FROM users WHERE id ='%d';", id);

	/* gasesc utilizatorul, CU TRATAREA ERORILOR */
	if (mysql_query(conn, query))
		handle_error("userssql::sql_user_view::mysql_query(conn, select)");

	if ((result = mysql_store_result(conn)) == NULL)
		handle_error("userssql::sql_user_view::mysql_store_result(conn, select)");	

	/* parcurg intrarile selectate din baza de date */
	if (row = mysql_fetch_row(result))
	{
		/* construiesc sirul de raspuns */
		handle_output("success|", 0);

		/* scriu in output identificatorul utilizatorului curent */
		handle_output(row[0], 1);

		/* adaug o bara incremental la raspuns*/
		handle_output("|", 1);

		/* adaug numele utilizatorului curent */
		handle_output(row[1], 1);

		/* adaug o bara incremental la raspuns*/
		handle_output("|", 1);

		/* adaug momentul utilizatorului curent */
		handle_output(row[2], 1);

		/* adaug o bara incremental la raspuns*/
		handle_output("|", 1);

		/* adaug permisiunile utilizatorului curent */
		handle_output(row[3], 1);

		/* consider raspuns valid */
		ans = 1;
	}
	
	/* tratez cazul cand nu am utilizator */
	else
	{
		/* consider raspuns invalid */
		ans = 0;
	}

	/* eliberez rezultatul */
	mysql_free_result(result);

	/* inchid conexiunea la baza de date */
	mysql_close(conn);

	/* intorc starea */
	return ans;
}