Exemple #1
0
parseTheEnv (struct QueryDataForamt *QueryData) {

	int res;

	//inaliserer
	QueryData->query[0] = '\0';
	

	// Initialize the CGI lib
	res = cgi_init();
  
	// Was there an error initializing the CGI???
	if (res != CGIERR_NONE) {
      		printf("Error # %d: %s<p>\n", res, cgi_strerror(res));
      		exit(0);
    	}

	if (cgi_getentrystr("query") == NULL) {
		perror("Did'n receive any query.");
	}
	else {
		//printf("query=%s<p>\n", cgi_getentrystr("query"));
		strncat(QueryData->query,cgi_getentrystr("query"),sizeof(QueryData->query));
	}

	// debugg
	//setter query
	//strncat(QueryData->query,"sex",3);
	///debug
}
int
main (int argc, char *argv[])
{
	char *host = NULL;
	char *prefix;
	char *user;
	char *collection = NULL;
	int ret;

	printf("Cache-control: no-cache\n");
	printf("Content-type: text/plain\n\n");
	if (getenv("QUERY_STRING")) {
		// We are in CGI mode
		if ((ret = cgi_init()) != CGIERR_NONE) {
			fprintf(stderr, "Error # %d: %s.\n", ret, cgi_strerror(ret));
			exit(0);
		}
		if ((prefix = cgi_getentrystr("q")) == NULL) {
			fprintf(stderr, "Did not find a prefix.\n");
			exit(0);
		}
#ifndef SUGGEST_OEM
		user = getenv("REMOTE_USER");
		if (user == NULL) {
			fprintf(stderr, "Not logged in.\n");
			exit(0);
		}
#else
		if ((user = cgi_getentrystr("user")) == NULL) {
			fprintf(stderr, "Did not find a user.\n");
			exit(0);
		}
#endif
		if ((collection = cgi_getentrystr("collection")) == NULL) {
			fprintf(stderr, "Did not find collection info.\n");
			collection = "";
		}

	}
	else {
		if (argc < 5) {
			printf("Usage: ./prog host prefix user collection\n");
			exit(1);
		}
		host = argv[1];
		prefix = argv[2];
		user = argv[3];
		collection = argv[4];
	}

	if (host == NULL)
		host = RPC_HOST;
	suggest_1 (host, prefix, user, collection);

	return 0;
}
Exemple #3
0
double cgi_getentrydouble(const char *field_name)
{
  double v;
  
  v = 0;
  
  if (cgi_getentrystr(field_name) != NULL)
    {
      if (sscanf(cgi_getentrystr(field_name), "%lf", &v) != 1)
	cgi_errno = CGIERR_NOT_DOUBLE;
    }
  else
    cgi_errno = CGIERR_NOT_DOUBLE;
  
  return(v);
}
Exemple #4
0
int cgi_getentryint(const char *field_name)
{
  int v;
  
  v = 0;
  
  if (cgi_getentrystr(field_name) != NULL)
    {
      if (sscanf(cgi_getentrystr(field_name), "%d", &v) != 1)
	cgi_errno = CGIERR_NOT_INTEGER;
    }
  else
    cgi_errno = CGIERR_NOT_INTEGER;
  
  return(v);
}
Exemple #5
0
void
cgi_get_string( char *name, char *value ) {
	char *s;
	s = name;
	while ( *s != ' ' || *(s+1) != ' ' ) s++;
	*s=0;
	s = cgi_getentrystr(name);
	if (s != NULL) {
		while (*s) {
			*value++ = *s++;
		}
	}
}
Exemple #6
0
int cgi_getentrybool(const char *field_name, int def)
{
  const char * temp;
  int v;
  
  
  /* Assume the default: */
  
  v = def;
  
  
  /* Get the value (if any): */
  
  temp = cgi_getentrystr(field_name);
  
  if (temp != NULL)
    {
      if (strcasecmp(temp, "yes") == 0 ||
	  strcasecmp(temp, "on") == 0 ||
	  strcasecmp(temp, "true") == 0)
	{
	  /* A "yes" or "on" is a 1: */
	  
	  v = 1;
	}
      else if (strcasecmp(temp, "no") == 0 ||
	       strcasecmp(temp, "off") == 0 ||
	       strcasecmp(temp, "false") == 0)
	{
	  /* A "no" or "off" is a 0: */
	  
	  v = 0;
	}
      else if (temp[0] != 0)
	{
	  /* We got something, but not "yes", "on", "no" or "off": */
	  
	  cgi_errno = CGIERR_NOT_BOOL;
	}
    }
  else
    cgi_errno = CGIERR_NOT_BOOL;
  
  
  return(v);
}
Exemple #7
0
    int main(int argc, char *argv[])
    {
        int sockfd, n;
	int i,y;
	char *strpointer;  
	int res;
        char buf[MAXDATASIZE];
        struct hostent *he;
        struct sockaddr_in their_addr; // connector's address information 
	FILE *LOGFILE;
	//char hostName[] = "localhost";
        //char hostName[] = "127.0.0.1";
	char hostName[] = "bbs-001.boitho.com";
	struct SiderFormat Sider[MaxsHits];
        struct SiderHederFormat SiderHeder;
	char buff[64]; //generell buffer
	struct in_addr ipaddr;
        struct QueryDataForamt QueryData;


        //send out an HTTP header:
        printf("Content-type: text/xml\n\n");



        //hvis vi har argumeneter er det første et query
        if (getenv("QUERY_STRING") == NULL) {
                if (argc < 2 ) {
                        printf("Error ingen query spesifisert.\n\nEksempel på bruk for å søke på boitho:\n\tsearchkernel boitho\n\n\n");
                }
                else {
                        QueryData.query[0] = '\0';
                        for(i=1;i<argc ;i++) {
                                sprintf(QueryData.query,"%s %s",QueryData.query,argv[i]);
                        }
                        //strcpy(QueryData.query,argv[1]);
                        //printf("argc :%i %s %s\n",argc,argv[1],argv[2]);
                        printf("query %s\n",QueryData.query);
                }
        }
        else {
		// Initialize the CGI lib
        	res = cgi_init();

		// Was there an error initializing the CGI???
	        if (res != CGIERR_NONE) {
        	        printf("Error # %d: %s<p>\n", res, cgi_strerror(res));
        	        exit(0);
        	}
		
		if (cgi_getentrystr("query") == NULL) {
                	perror("Did'n receive any query.");
        	}
		else {
        	        strncat(QueryData.query,cgi_getentrystr("query"),sizeof(QueryData.query));
	        }

        }

        if (strlen(QueryData.query) > MaxQueryLen -1) {
                printf("query to long\n");
                exit(1);
        }

        //gjør om til liten case
        for(i=0;i<strlen(QueryData.query);i++) {
                QueryData.query[i] = tolower(QueryData.query[i]);
        }


        if ((he=gethostbyname(hostName)) == NULL) {  // get the host info 
            perror("gethostbyname");
            exit(1);
        }

        if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
            perror("socket");
            exit(1);
        }

        their_addr.sin_family = AF_INET;    // host byte order 
        their_addr.sin_port = htons(PORT);  // short, network byte order 
        their_addr.sin_addr = *((struct in_addr *)he->h_addr);
        memset(&(their_addr.sin_zero), '\0', 8);  // zero the rest of the struct 

        if (connect(sockfd, (struct sockaddr *)&their_addr,
                                              sizeof(struct sockaddr)) == -1) {
            perror("connect");
            exit(1);
        }
	struct queryNodeHederFormat queryNodeHeder;

	//kopierer inn query	
	strncpy(queryNodeHeder.query,QueryData.query,sizeof(queryNodeHeder.query) -1);
	
	//sender forespørsel
	sendall(sockfd,queryNodeHeder.query,sizeof(queryNodeHeder));

	//motter hedderen for svaret
	if ((i=recv(sockfd, &SiderHeder, sizeof(SiderHeder),MSG_WAITALL)) == -1) {
                perror("recv");
        }

	//printf("TotaltTreff %i,showabal %i,filtered %i,total_usecs %f\n",SiderHeder.TotaltTreff,SiderHeder.showabal,SiderHeder.filtered,SiderHeder.total_usecs);

	for(i=0;i<SiderHeder.showabal;i++) {

		if ((n=recv(sockfd, &Sider[i], sizeof(struct SiderFormat),MSG_WAITALL)) == -1) {
	               	perror("recv");
        	}
		//printf("url: %s\n",Sider[i].DocumentIndex.Url);
	}


        close(sockfd);


        y=0;
        //fjerner tegn som er eskapet med \, eks \" blir til &quot;
        for(i=0;i<strlen(QueryData.query);i++) {
                if (QueryData.query[i] == '\\') {

                        switch(QueryData.query[++i]) {
                                case '"':
                                        //&quot;
                                        buff[y++] = '&'; buff[y++] = 'q'; buff[y++] = 'u'; buff[y++] = 'o'; buff[y++] = 't'; buff[y++] = ';';
                                break;


                        }
                        //else {
                        //      printf("error: found \\ but no case\n");
                        //}
                }
                else {
                        //printf("%c\n",QueryData.query[i]);
                        buff[y++] = QueryData.query[i];
                }
        }
        strncpy(QueryData.query,buff,sizeof(QueryData.query) -1);

        printf("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?> \n");
        printf("<!DOCTYPE family SYSTEM \"http://www.boitho.com/xml/search.dtd\"> \n");

        printf("<search>\n");   


        printf("<treff-info totalt=\"%i\" query=\"%s\" hilite=\"%s\" tid=\"%f\" filtered=\"%i\" showabal=\"%i\"/>\n",SiderHeder.TotaltTreff,QueryData.query,SiderHeder.hiliteQuery,SiderHeder.total_usecs,SiderHeder.filtered,SiderHeder.showabal);


	for(i=0;i<SiderHeder.showabal;i++) {

		if (!Sider[i].deletet) {

                //filtrerer ut tegn som ikke er lov i xml
                while ((strpointer = strchr(Sider[i].DocumentIndex.Url,'&')) != NULL) {
                        (*strpointer) = 'a';
                }
                //while ((strpointer = strchr(Sider[i].title,'&')) != NULL) {
                //        (*strpointer) = 'a';
                //}
                //while ((strpointer = strchr(Sider[i].description,'&')) != NULL) {
                //        (*strpointer) = 'a';
                //}


                printf("<treff>\n");

                printf("\t<DocID>%i-%i</DocID>\n",Sider[i].iindex.DocID,rLotForDOCid(Sider[i].iindex.DocID));
                printf("\t<POSISJON>%i</POSISJON>\n",i +1);

                //DocumentIndex
                printf("\t<Url>%s</Url>\n",Sider[i].DocumentIndex.Url);
                printf("\t<Title>%s</Title>\n",Sider[i].title);
                printf("\t<AdultWeight>%hu</AdultWeight>\n",Sider[i].DocumentIndex.AdultWeight);
                printf("\t<Sprok>%s</Sprok>\n",Sider[i].DocumentIndex.Sprok);
                //temp: blir rare tegn her              
		printf("\t<Dokumenttype>%s</Dokumenttype>\n",Sider[i].DocumentIndex.Dokumenttype);

                printf("\t<RepositorySize>%u</RepositorySize>\n",Sider[i].DocumentIndex.htmlSize);

                printf("\t<THUMBNALE>%s</THUMBNALE>\n",Sider[i].thumbnale);

                printf("\t<CACHE>%s</CACHE>\n",Sider[i].cacheLink);
                printf("\t<IMAGEWIDTH>100</IMAGEWIDTH>\n");
                printf("\t<IMAGEHEIGHT>100</IMAGEHEIGHT>\n");

                printf("\t<METADESCRIPTION></METADESCRIPTION>\n");
                printf("\t<CATEGORY></CATEGORY>\n");
                printf("\t<OFFENSIVE_CODE>FALSE</OFFENSIVE_CODE>\n");


                printf("\t<beskrivelse>%s</beskrivelse>\n",Sider[i].description);
                printf("\t<TermRank>%i</TermRank>\n",Sider[i].iindex.TermRank);
                printf("\t<PopRank>%i</PopRank>\n",Sider[i].iindex.PopRank);
                printf("\t<allrank>%i</allrank>\n",Sider[i].iindex.allrank);

		ipaddr.s_addr = Sider[i].DocumentIndex.IPAddress;

                printf("\t<IPAddress>%s</IPAddress>\n",inet_ntoa(ipaddr));

                printf("\t<RESPONSE>%hu</RESPONSE>\n",Sider[i].DocumentIndex.response);

                printf("\t<NrOfHits>%i</NrOfHits>\n",Sider[i].iindex.TermAntall);

                //printer ut hits (hvor i dokumenetet orde befinner seg ).
                printf("\t<hits>");
                for (y=0; (y < Sider[i].iindex.TermAntall) && (y < MaxTermHit); y++) {
                        printf("%hu ",Sider[i].iindex.hits[y]);
                }
                printf("</hits>\n");

                printf("</treff>\n");

		}
	}

	printf("</search>\n");

	//ToDo: må ha låsing her
	if ((LOGFILE = fopen("/home/boitho/config/query.log","a")) == NULL) {
		perror("logfile");
	}
        fprintf(LOGFILE,"%s %i\n",queryNodeHeder.query,SiderHeder.TotaltTreff);
        fclose(LOGFILE);


        return 0;
    } 
Exemple #8
0
int main (int argc, char *argv[]) {

    struct DocumentIndexFormat DocumentIndexPost;
    int PopRankextern;
    int PopRankintern;
    int PopRanknoc;
    int PopRanindex;
    char ShortRank;
    FILE *FH;
    struct popl popextern;
    struct popl popintern;
    struct popl popnoc;
    struct popl popindex;
    uLong htmlBufferSize = 0;
    char *htmlBuffer = NULL;
    char *acl_allowbuffer = NULL;
    char *acl_deniedbuffer = NULL;

    char timebuf[26];

    int optShowhtml = 0;
    int optShowWords = 0;
    int optSummary = 0;
    int optAnchor = 0;
    int optResource = 0;
    int optPopRank = 0;
    int optDelete = 0;
    int optAdult = 0;

    unsigned int DocID;
    char *subname;

    if (getenv("QUERY_STRING") == NULL) {

        extern char *optarg;
        extern int optind, opterr, optopt;
        char c;
        while ((c=getopt(argc,argv,"hwsarpdu"))!=-1) {
            switch (c) {
            case 'h':
                optShowhtml = 1;
                break;
            case 'u':
                optAdult = 1;
                break;
            case 'w':
                optShowWords = 1;
                break;
            case 's':
                optSummary = 1;
                break;
            case 'a':
                optAnchor = 1;
                break;
            case 'p':
                optPopRank = 1;
                break;
            case 'r':
                optResource = 1;
                break;
            case 'd':
                optDelete = 1;
                break;
            default:
                exit(1);
            }
        }
        --optind;

#ifdef DEBUG
        printf("argc %i, optind %i\n",argc,optind);
#endif

        if ((argc - optind)!= 3) {
            printf("Dette programet gir info om en DocID\n\n\tUsage PageInfo DocID collection\n");
            exit(1);
        }


        DocID = atol(argv[1 +optind]);
        subname = argv[2 +optind];


    }
    else {
        printf("Content-type: text/plain\n\n");
        int res;
        // Initialize the CGI lib
        res = cgi_init();

        // Was there an error initializing the CGI???
        if (res != CGIERR_NONE) {
            printf("Error # %d: %s<p>\n", res, cgilib_strerror(res));
            fprintf(stderr,"Cgi-lib error.");
            return -1;
        }

        if (cgi_getentrystr("subname") == NULL) {
            fprintf(stderr,"Didn't recieve any subname.");
            return -1;
        }
        else {
            subname = cgi_getentrystr("subname");
        }

        if (cgi_getentrystr("DocID") == NULL) {
            fprintf(stderr,"Didn't recieve any DocID.");
            return -1;
        }
        else {
            DocID = atol( cgi_getentrystr("DocID") );
        }

    }

    html_parser_init();

    printf("Showing data for Collection \"%s\", DocID %u\n\n",subname,DocID);


    printf("Lot: %i\n",rLotForDOCid(DocID));

    if (optDelete) {
        memset(&DocumentIndexPost,'\0',sizeof(DocumentIndexPost));
        DIWrite(&DocumentIndexPost,DocID,subname,NULL);

        return 0;
    }

    if (DIRead_fmode(&DocumentIndexPost,DocID,subname,'s')) {

        printf("Url: \"%s\"\nLanguage: %s (id: %s)\nOffensive code: %hu\nDocument type: %s\nTime tested sins last good crawl: %hu\nAdult weight: %hu\nResource size: %u\nIP Address: %u\nHtml size: %i\nImage size: %i\nUser ID: %i\nCrawler version: %f\nRepository pointer: %u\n",

               DocumentIndexPost.Url,
               getLangCode2(atoi(DocumentIndexPost.Sprok)),
               DocumentIndexPost.Sprok,
               DocumentIndexPost.Offensive_code,
               DocumentIndexPost.Dokumenttype,
               DocumentIndexPost.AntallFeiledeCrawl,
               DocumentIndexPost.AdultWeight,
               DocumentIndexPost.ResourceSize,
               DocumentIndexPost.IPAddress,
               DocumentIndexPost.htmlSize2,
               DocumentIndexPost.imageSize,
               DocumentIndexPost.userID,
               DocumentIndexPost.clientVersion,
               DocumentIndexPost.RepositoryPointer);

        if (DocumentIndexPost.response == 200) {
            printf("HTTP response: %hu\n",DocumentIndexPost.response);
        }
        else {
            printf("HTTP response: \033[1;31m%hu\033[0m\n",DocumentIndexPost.response);

        }


        ctime_r((time_t *)&DocumentIndexPost.CrawleDato,timebuf);
        timebuf[24] = '\0';


        printf("Last crawled time: %u\n",DocumentIndexPost.CrawleDato);
        printf("Last crawled time ISO: %s\n",timebuf);

        printf("crc32: %u\n",DocumentIndexPost.crc32);

#ifdef BLACK_BOX
        printf("Last seen Unix: %u\n",DocumentIndexPost.lastSeen);
        printf("Last seen ISO: %s", ctime(&DocumentIndexPost.lastSeen));
#endif

        printf("Nr of out links: %u\n",(unsigned int)DocumentIndexPost.nrOfOutLinks);


        char *metadesc, *title, *body;
        if (DocumentIndexPost.SummarySize == 0) {
            printf("Summary: Don't have pre-parsed summery (summary size is 0)\n");

        }
        else if (rReadSummary(DocID,&metadesc, &title, &body,DocumentIndexPost.SummaryPointer,DocumentIndexPost.SummarySize,subname)) {
            printf("\nSummary:\n");
            printf("\tSummary pointer: %u\n\tSummary size: %hu\n",DocumentIndexPost.SummaryPointer,DocumentIndexPost.SummarySize);

            printf("\tTitle from summary:  \"%s\"\n\tMeta description from summary: \"%s\"\n",title,metadesc);
            if (optSummary) {
                printf("Summary body\n*******************\n%s\n*******************\n\n",body);
            }
        }
        else {
            printf("Don't have pre-parsed summery\n");
        }




        struct ReposetoryHeaderFormat ReposetoryHeader;
        char *url, *attributes;

        if (!rReadHtml(&htmlBuffer,&htmlBufferSize,DocumentIndexPost.RepositoryPointer,DocumentIndexPost.htmlSize2,DocID,subname,&ReposetoryHeader,&acl_allowbuffer,&acl_deniedbuffer,DocumentIndexPost.imageSize, &url, &attributes)) {
            printf("rReadHtml: did not returne true!\n");
            return;
        }
        printf("Entire url: %s\n", url);

#ifdef BLACK_BOX
        printf("acl allow raw: \"%s\"\n",acl_allowbuffer);
        printf("acl denied raw: \"%s\"\n",acl_deniedbuffer);

        printf("acl allow resolved: \"%s\"\n",aclResolv(acl_allowbuffer));
        printf("acl denied resolved: \"%s\"\n",aclResolv(acl_deniedbuffer));

        printf("PopRank: %d\n", ReposetoryHeader.PopRank);
#endif

        if (optShowhtml) {

            printf("html uncompresed size %i\n",htmlBufferSize);
            printf("html buff:\n*******************************\n");
            fwrite(htmlBuffer,htmlBufferSize,1,stdout);
            printf("\n*******************************\n\n");


        }
        if (optShowWords) {
            printf("words:\n");
            //run_html_parser( DocumentIndexPost.Url, htmlBuffer, htmlBufferSize, fn );
            char *title, *body;
            html_parser_run(url,htmlBuffer, htmlBufferSize,&title, &body,fn,NULL );
        }
        if (optResource) {
            char buf[500000];
            printf("Resource:\n");
            printf("Ptr: 0x%x Len: %x\n", DocumentIndexPost.ResourcePointer, DocumentIndexPost.ResourceSize);
            if (getResource(rLotForDOCid(DocID), subname, DocID, buf, sizeof(buf)) == 0) {
                printf("\tDid not get any resource\n");
                warn("");
            } else {
                printf("%s\n", buf);
            }
        }

        printf("attributes:\"%s\"\n", attributes);

        free(url);
        free(attributes);
        free(acl_allowbuffer);
        free(acl_deniedbuffer);
    }
    else {
        printf("Cant read post\n");
    }

#ifndef BLACK_BOX

    if (optAdult) {
        int httpResponsCodes[nrOfHttpResponsCodes];
        //char *title;
        //char *body;
        struct adultFormat *adult;
        struct pagewordsFormat *pagewords = malloc(sizeof(struct pagewordsFormat));
        int AdultWeight;
        unsigned char langnr;
        if ((adult = malloc(sizeof(struct adultFormat))) == NULL) {
            perror("malloc argstruct.adult");
            exit(1);
        }

        wordsInit(pagewords);
        langdetectInit();
        adultLoad(adult);

        AdultWeight -1;

        handelPage(pagewords,&ReposetoryHeader,htmlBuffer,htmlBufferSize,&title,&body);

        wordsMakeRevIndex(pagewords,adult,&AdultWeight,&langnr);

        printf("adult %i\n",AdultWeight);
    }

    if (optAnchor) {
        int anchorBufferSize;
        char *anchorBuffer;

        anchorBufferSize = anchorRead(rLotForDOCid(DocID),subname,DocID,NULL,-1);
        anchorBufferSize += 1;
        anchorBuffer = malloc(anchorBufferSize);
        anchorRead(rLotForDOCid(DocID),subname,DocID,anchorBuffer,anchorBufferSize);

        printf("#######################################\nanchors:\n%s\n#######################################\n",anchorBuffer);

        free(anchorBuffer);
    }



    if (optPopRank) {
        popopen (&popindex,"/home/boitho/config/popindex");
        PopRanindex = popRankForDocID(&popindex,DocID);
        popclose(&popindex);
        printf("popindex %i\n",PopRanindex);

        if (popopen (&popextern,"/home/boitho/config/popextern")) {
            PopRankextern =  popRankForDocID(&popextern,DocID);
            printf("PopRankextern: %i\n",PopRankextern);
            popclose(&popextern);
        }
        if (popopen (&popintern,"/home/boitho/config/popintern")) {
            PopRankintern =  popRankForDocID(&popintern,DocID);
            printf("PopRankintern %i\n",PopRankintern);
            popclose(&popintern);
        }
        if (popopen (&popnoc,"/home/boitho/config/popnoc")) {
            PopRanknoc =  popRankForDocID(&popnoc,DocID);
            printf("PopRanknoc %i\n",PopRanknoc);
            popclose(&popnoc);
        }
        if (popopen (&popindex,"/home/boitho/config/popindex")) {
            PopRanindex = popRankForDocID(&popindex,DocID);
            printf("popindex %i\n",PopRanindex);
            popclose(&popindex);
        }



        printf("PopRankextern: %i\nPopRankintern %i\nPopRanknoc %i\n",PopRankextern,PopRankintern,PopRanknoc);


        int brank;
        popopenMemArray_oneLot(subname,rLotForDOCid(DocID));
        brank = popRankForDocIDMemArray(DocID);
        printf("brank %i\n",brank);
        //short rank
        if ( (FH = fopen(SHORTPOPFILE,"rb")) == NULL ) {
            perror("open");
        }
        else {
            if ((fseek(FH,DocID* sizeof(ShortRank),SEEK_SET) == 0) && (fread(&ShortRank,sizeof(ShortRank),1,FH) != 0)) {

                printf("Short rank %u\n",(unsigned char)ShortRank);
            }
            else {
                printf("no hort rank avalibal\n");
            };

            fclose(FH);
        }
    } // if optPopRank
#endif


}
Exemple #9
0
int main(int argc, char **argv, char **envp)
{
	int postsize;
	char *xmldata, *data;
	xmlDocPtr doc;
        xmlNodePtr cur, anode;
	int bbdnsock;
	int bbdnport;
	char *status = NULL;
	struct config_t maincfg;


	/* Read in config file */
	maincfg = maincfgopen();
	bbdnport = maincfg_get_int(&maincfg, "BLDPORT");
	maincfgclose(&maincfg);
	key_get(systemkey);
	char *request_method;

	if (!bbdn_conect(&bbdnsock, "", bbdnport))
		cgi_error(500, bstrerror());


	request_method = getenv("HTTP_REQUEST_METHOD")!=NULL ? strdup(getenv("HTTP_REQUEST_METHOD")) : strdup(getenv("REQUEST_METHOD"));
	// We will handel the post stuf our self. Set REQUEST_METHOD to GET so cgi-util ignores it.
	setenv("REQUEST_METHOD", "GET", 1);

	if (cgi_init() != CGIERR_NONE) {
		cgi_error(500, "Can't init cgi-util");		
	}


	/*
	 * Either called from command line, and then we want a file.
	 * or a http get/put
	 * Or we are handling a web request, and getting the data from stdin.
	 */
	if ((cgi_getentrystr("method") != NULL) && (strcmp(cgi_getentrystr("method"),"rest") == 0)) {

		char api[100], coll[100], url[512];
		char *requrle;

		if (getenv("REQUEST_URI") == NULL) {
			cgi_error(500, "Can't read REQUEST_URI");
		}

		requrle = strdup(getenv("REQUEST_URI"));
		unescape_url(requrle);
		sscanf(requrle,"/%[a-z]/%[a-zA-Z0-9_-]/%[^?]", api, coll, url);

		#ifdef DEBUG
			fprintf(stderr, "api: \"%s\"\n",api);
			fprintf(stderr, "coll: \"%s\"\n",coll);
			fprintf(stderr, "url: \"%s\"\n",url);
			fprintf(stderr, "request_method: \"%s\"\n",request_method);
			fprintf(stderr, "reques url \"%s\"\n",getenv("REQUEST_URI"));
			fprintf(stderr, "reques url unescaped \"%s\"\n",requrle);
		#endif

		free(requrle);

		if (strcmp(request_method,"POST") == 0 || strcmp(request_method,"ADDDELAYED") == 0 || strcmp(request_method,"PUT") == 0) {

			if (getenv("CONTENT_LENGTH") == NULL) {
				cgi_error(500, "Can't read CONTENT_LENGTH");
			}

			
	               // Get data length
	                postsize = atoi(getenv("CONTENT_LENGTH"));
	                data = malloc(postsize + 1);
			if (data == NULL) {
				cgi_error(500, "Can't allocate data.");
			}
	                // Read data
	                fread(data, 1, postsize, stdin);
	                data[postsize] = '\0';

			// add in to repo
	        	if (bbdn_docadd(bbdnsock, 
					coll, 											// collection name
					url, 											// url
					cgi_getentrystr("documenttype"), 							// document type
					data,											// data
					postsize, 										// data size
					0, 											// lastmodified
					cgi_getentrystr("acl_allow")!=NULL ? cgi_getentrystr("acl_allow") : "Everyone", 	// acl allow
					cgi_getentrystr("acl_denied"), 								// acl denied
					cgi_getentrystr("title"), 								// title
					cgi_getentrystr("documentformat"), 							// document format
					cgi_getentrystr("attributes"),								// attributes
					NULL,											// image
					0											// image size
				) != 1) {
				cgi_error(500, "bbdn_docadd() failed. Can't add document.");
			}

			
			if (strcmp(request_method,"ADDDELAYED") != 0) {
	                	// close it
	                	sd_close(bbdnsock, coll);
			}

			asprintf(&status,"Added %s to %s\n",url,coll);

		}
		else if (strcmp(request_method,"DELETE") == 0) {

			if (url[0] == '\0') {

				if (sd_deletecollection(bbdnsock, coll) != 1) {
					cgi_error(500, "Can't delete collection");
				}

				asprintf(&status,"Deleted collection %s\n",coll);
			}
			else {
				if (bbdn_deleteuri(bbdnsock, coll, url) != 1) {
					cgi_error(500, "Can't delete document");
				}

				asprintf(&status,"Deleted url %s in %s\n",url,coll);
			}

		}
		else if (strcmp(request_method,"CLOSE") == 0) {
			sd_close(bbdnsock, coll);

			asprintf(&status,"Closed %s\n",coll);

		}
		else {
			cgi_error(500, "Unknown request method \"%s\"", request_method );
		}

		#ifdef DEBUG
			// Print the envirement so we can better see what is going on.
	                char** env;
	                for (env = envp; *env != 0; env++) {
	                    char* thisEnv = *env;
	                    fprintf(stderr, "%s\n", thisEnv);
	                }
		#endif


	}
	else if ((cgi_getentrystr("do") != NULL) && (strcmp(cgi_getentrystr("do"),"add") == 0)) {

		char *data;
		int datasize;
		int n;

		const char *url = getenv("HTTP_X_FILENAME") ? getenv("HTTP_X_FILENAME") : cgi_getentrystr("url");
		const char *coll = cgi_getentrystr("collection");

		if (url == NULL) {
			cgi_error(500, "No url specified. Either set http header HTTP_X_FILENAME or get parameter 'url'.\n");
		}
		if (coll == NULL) {
			cgi_error(500, "No collection specified\n");
		}


		char *tmpname;
		FILE *fh;
		asprintf(&tmpname,"/tmp/%s",url);
	
		fh = fopen(tmpname,"wb");
		if (fh == NULL) {
			cgi_error(500, "Can't open file %s",tmpname);
		}

		if ((data = malloc( atoi(getenv("CONTENT_LENGTH")) )) == NULL) {
			cgi_error(500, "Can't malloc data");
		}

		datasize = 0;
		while ((n = fread ((unsigned char *)(data + datasize),1,1024,stdin)) > 0) {
			datasize += n;
		}

		fwrite(data,1,datasize,fh);

		fclose(fh);
		free(tmpname);


		//        bbdn_docadd(bbdnsock, xmldoc.collection, uri, xmldoc.documenttype, xmldoc.body, xmldoc.bodysize,
		//            xmldoc.lastmodified, xmldoc.aclallow, xmldoc.acldeny, xmldoc.title, xmldoc.documentformat, xmldoc.attributes, image, image_size);
        	bbdn_docadd(bbdnsock, coll, url, "", data, datasize,
      		    0, "Everyone", "", "omp1", "", "", NULL, 0);


		// close it
		sd_close(bbdnsock, coll);
	}
	else if ((cgi_getentrystr("do") != NULL) && (strcmp(cgi_getentrystr("do"),"delete") == 0)) {


		const char *url = getenv("HTTP_X_FILENAME") ? getenv("HTTP_X_FILENAME") : cgi_getentrystr("url");
		const char *coll = cgi_getentrystr("collection");

		if (url == NULL) {
			cgi_error(500, "No url specified. Either set http header HTTP_X_FILENAME or get parameter 'url'.\n");
		}
		if (coll == NULL) {
			cgi_error(500, "No collection specified\n");
		}

		bbdn_deleteuri(bbdnsock, coll, url);

		asprintf(&status,"%s deleted.\n", url);
	}
	else if (getenv("CONTENT_LENGTH") != NULL) {
		// Get data length
		postsize = atoi(getenv("CONTENT_LENGTH"));
		xmldata = malloc(postsize + 1);	
		// Read data
		fread(xmldata, 1, postsize, stdin);
		xmldata[postsize] = '\0';


		//fprintf(stderr, "Received %i bytes.\n", postsize);
		//fprintf(stderr, "Got document:\n%s\n", xmldata);

		//parsing xml
        	doc = xmlParseDoc((xmlChar*)xmldata);

        	if (doc == NULL)
		cgi_error(500, "Unable to parse document");

        	cur = xmlDocGetRootElement(doc);

        	if (cur == NULL) {
        	        xmlFreeDoc(doc);
        	        cgi_error(500, "empty document");
        	}

		// Some document checking
        	if (xmlStrcmp(cur->name, (const xmlChar *)ROOT_NODE_NAME)) {
        	        xmlFreeDoc(doc);
        	        cgi_error(500, "document of the wrong type, root node != %s, but %s\n", ROOT_NODE_NAME, cur->name);
        	}

		if ((anode = xml_find_child(cur, "key")) != NULL) {
			char *p;
		
			p = (char *)xmlNodeListGetString(doc, anode->xmlChildrenNode, 1);
			if (p == NULL)
				cgi_error(500, "No key data");

			if ((systemkey[0] != '\0') && (!key_equal(systemkey, p))) {
				cgi_error(500, "Keys does not match:  Got \"%s\" but wanted \"%s\"\n",p,systemkey);
			}
		} else {
			cgi_error(500, "Did not receive a key");
		}
		if ((anode = xml_find_child(cur, "version")) != NULL) {
			xmlChar *p;

			p = xmlNodeListGetString(doc, anode->xmlChildrenNode, 1);
			version = atoi((char*)p);

			xmlFree(p);
		} else {
			cgi_error(500, "Did not receive a version number");
		}

		for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
			if ((!xmlStrcmp(cur->name, (const xmlChar *) "key"))){
				// Ignore
			} else if ((!xmlStrcmp(cur->name, (const xmlChar *) "version"))){
				// Ignore
			} else if ((!xmlStrcmp(cur->name, (const xmlChar *) "add"))){
				xml_add(bbdnsock, doc, cur);
			} else if ((!xmlStrcmp(cur->name, (const xmlChar *) "delete"))){
				xml_delete(bbdnsock, doc, cur);
			} else if ((!xmlStrcmp(cur->name, (const xmlChar *) "close"))) {
				xml_close(bbdnsock, doc, cur);
			} else if ((!xmlStrcmp(cur->name, (const xmlChar *) "create"))) {
				xml_create(bbdnsock, doc, cur);
			} else if ((!xmlStrcmp(cur->name, (const xmlChar *) "users"))) {
				xml_users(doc, cur);
			} else if ((!xmlStrcmp(cur->name, (const xmlChar *) "gcwhispers"))) {
				xml_gcwhispers(bbdnsock, doc, cur);
			} else if ((!xmlStrcmp(cur->name, (const xmlChar *) "error"))) {
				xml_errormsg(bbdnsock, doc, cur);
			} else if ((!xmlStrcmp(cur->name, (xmlChar*)"text"))) {
				//fprintf(stderr, "Got text: %s\n", xmlNodeListGetString(doc, cur, 1));
				// Ignore for now
			} else {
				warnx("Unknown xml node '%s'", cur->name);
			}
		}

	} else {
		cgi_error(500, "Didn't receive any command or data.");
	}

	if (status != NULL) {
		printf("Content-type: text/plain\n\n");
		printf(status);
	}
	else {
		cgi_error(500, "Reached end of program without status.");
	}

	return 0;
}
Exemple #10
0
int main(int argc, char *argv[]) {

        static MYSQL demo_db;
        char mysql_query [2048];
        MYSQL_RES *mysqlres; /* To be used to fetch information into */
        MYSQL_ROW mysqlrow;
	int res;
	struct issuedaddFormat issuedadd;
	char clickStatus[33];

	unsigned int addid;
	char addurl[251];
	char remote_addr[32];

	#ifdef DEBUG
	printf("Content-type: text/html\n\n");
	#endif

	
	if (getenv("QUERY_STRING") == NULL) {
		if (argc != 4 ) {
			fprintf(stderr,"no QUERY_STRING and no command lin input.\n\n\tUsage addout.cgi addid http://www.test.com\n");
			exit(1);
		}
		else {
			addid = strtoul(argv[1], (char **)NULL, 10);
			strcpy(addurl,argv[2]);	
			strncpy(remote_addr,argv[3],sizeof(remote_addr) -1);
		}

	}
	else {
		//leser inn cgi variabler
		// Initialize the CGI lib
        	res = cgi_init();

        	// Was there an error initializing the CGI???
        	if (res != CGIERR_NONE) {
        	        fprintf(stderr,"Error # %d: %s\n", res, cgi_strerror(res));
        		exit(1);
        	}

	
		if (cgi_getentrystr("addurl") == NULL) {
        		fprintf(stderr,"Did'n receive any addurl.\n");
			exit(1);
        	}
        	else {
        		strncpy(addurl,cgi_getentrystr("addurl"),sizeof(addurl) -1);
        	}

		if (cgi_getentrystr("addid") == NULL) {
        		fprintf(stderr,"Did'n receive any id.\n");
			exit(1);
        	}
        	else {
        		addid = strtoul(cgi_getentrystr("addid"), (char **)NULL, 10);
        	}

		if (getenv("REMOTE_ADDR") == NULL) {
        		fprintf(stderr,"Did'n receive any REMOTE_ADDR.\n");
			exit(1);
        	}
        	else {
        		strncpy(remote_addr,getenv("REMOTE_ADDR"),sizeof(remote_addr) -1);
        	}
		

	}
	
        mysql_init(&demo_db);

        //if(!mysql_real_connect(&demo_db, "www2.boitho.com", "boitho_remote", "G7J7v5L5Y7", "boitho", 3306, NULL, 0)){
        if(!mysql_real_connect(&demo_db, "localhost", "boitho", "G7J7v5L5Y7", "boithoweb", 3306, NULL, 0)){
                fprintf(stderr,mysql_error(&demo_db));
                exit(1);
        }

	#ifdef DEBUG
	printf("add id %u\n",addid);
	#endif
        sprintf(mysql_query, "select keyword,bid,uri,clickfrequency,ppcuser,affuser,ipadress,UNIX_TIMESTAMP(issuetime),UNIX_TIMESTAMP(NOW()),HTTP_ACCEPT_LANGUAGE,HTTP_USER_AGENT,HTTP_REFERER,betaler_keyword_id,betaler_side_id from issuedadds where id='%u'",addid); 

        if(mysql_real_query(&demo_db, mysql_query, strlen(mysql_query))){ /* Make query */
             fprintf(stderr,mysql_error(&demo_db));
             exit(1);
        }


        //henter svaret
        mysqlres=mysql_store_result(&demo_db); /* Download result from server */
        if  ((mysqlrow=mysql_fetch_row(mysqlres)) == NULL) { /* Get a row from the results */
		fprintf(stderr,"cnat't fint the add in db.\n");
		strcpy(clickStatus,"CANT_FIND_ADD");
	}
	else {
                
                #ifdef DEBUG
		printf("%s<br>\n%s<br>\n%s<br>\n%s<br>\n%s<br>\n%s<br>\n%s<br>\n%s<br>\n%s<br>\n%s<br>\n%s<br>\n%s<br>\n",mysqlrow[0],mysqlrow[1],mysqlrow[2],mysqlrow[3],mysqlrow[4],mysqlrow[5],mysqlrow[6],mysqlrow[7],mysqlrow[8],mysqlrow[9],mysqlrow[10],mysqlrow[11]);
		#endif
		strncpy(issuedadd.query,mysqlrow[0],sizeof(issuedadd.query)-1);
		issuedadd.bid = atof(mysqlrow[1]);
		strncpy(issuedadd.uri,mysqlrow[2],sizeof(issuedadd.uri) -1);
		issuedadd.clickfrequency = atoi(mysqlrow[3]);
		strncpy(issuedadd.ppcuser,mysqlrow[4],sizeof(issuedadd.ppcuser)-1);
		strncpy(issuedadd.affuser,mysqlrow[5],sizeof(issuedadd.ppcuser)-1);
		strncpy(issuedadd.ipadress,mysqlrow[6],sizeof(issuedadd.ipadress)-1);
		issuedadd.issuetime = atoi(mysqlrow[7]);
		issuedadd.nowtime = atoi(mysqlrow[8]);
        	strncpy(issuedadd.HTTP_REFERER,mysqlrow[11],sizeof(issuedadd.HTTP_REFERER)-1);
		issuedadd.betaler_keyword_id = atoi(mysqlrow[12]);
		issuedadd.DocID = strtoul(mysqlrow[13], (char **)NULL, 10);

		
		//sjekker om vi har noe klikk på denne ipen til samme side fra før
		
	        sprintf(mysql_query, "select * from short_out_logg where betaler_side_id='%u' AND ip_adresse='%s' AND tid > (NOW() - INTERVAL 24 HOUR)",issuedadd.DocID,issuedadd.ipadress); 

	        if(mysql_real_query(&demo_db, mysql_query, strlen(mysql_query))){ /* Make query */
        	     	printf(mysql_error(&demo_db));
             		exit(1);
        	}

		int limit24hour;;
		mysqlres=mysql_store_result(&demo_db); /* Download result from server */
        	if  ((mysqlrow=mysql_fetch_row(mysqlres)) != NULL) { /* Get a row from the results */
			//printf("we have a record in short_out_logg\n");
			//bør kansje oppdatere klikk frekvens her
			limit24hour = 1;
		}
		else {
			//printf("we dont have a record in short_out_logg.\n %s\n",mysql_query);
			//legger den inn
			sprintf(mysql_query, "insert into short_out_logg values('%u','%s','%i',%s)",
				issuedadd.DocID,
				issuedadd.ipadress,
				0,
				"NOW()");
			if(mysql_real_query(&demo_db, mysql_query, strlen(mysql_query))){ /* Make query */
                        	printf(mysql_error(&demo_db));
                        	exit(1);
                	}		
			limit24hour = 0;
		}

		//sjekker om dette er et gyldig klikk 
		if (strcmp(remote_addr,issuedadd.ipadress) != 0){
			strcpy(clickStatus,"IP_MISS_MATCH");
		}
		//Sjekker om annonsen er ung nokk. 
		// bruker now time fra mysql server isteden for time() da vi ikke nødvendivis kjører på 
		//samme server, og klokkene ikke er synkronisert
		else if (issuedadd.nowtime > (issuedadd.issuetime + 3600)) { 

			strcpy(clickStatus,"ADD_TO_OLD");
		} 
		else if (issuedadd.clickfrequency > 0) {
			strcpy(clickStatus,"MORE_THEN_ONE_CLICK");
		}
		else if (limit24hour) {
			strcpy(clickStatus,"24_HOUR_LIMIT");
		}
		else {
			//vi har en ok status
			strcpy(clickStatus,"OK");
			
		}

		//redirekter brukeren
		if (strcmp(clickStatus,"OK") == 0) {
			printf("Location:%s\n\n",issuedadd.uri);			
		}
		else {
			printf("Location:%s\n\n",addurl);
		}

		//kan vel egentlig stenge ned standat utputt nå?

		//oppdaterer klikk frekvens
		sprintf(mysql_query, "update issuedadds set clickfrequency = clickfrequency+1 where id='%u'",addid);

        	if(mysql_real_query(&demo_db, mysql_query, strlen(mysql_query))){ /* Make query */
             		fprintf(stderr,mysql_error(&demo_db));
             		exit(1);
        	}

		//trekker penger hvis vi har ok status
		if (strcmp(clickStatus,"OK") == 0) {

			//trekker penger fra ppc brukene
			sprintf(mysql_query, "update brukere set penger=penger - %f where bruker_navn='%s'",issuedadd.bid,issuedadd.ppcuser);

			if(mysql_real_query(&demo_db, mysql_query, strlen(mysql_query))){ /* Make query */
        	                fprintf(stderr,mysql_error(&demo_db));
        	                exit(1);
        	        }

			//betaler search/aff brukeren
			sprintf(mysql_query, "update brukere set penger=penger + %f where bruker_navn='%s'",issuedadd.bid,issuedadd.affuser);
		
			if(mysql_real_query(&demo_db, mysql_query, strlen(mysql_query))){ /* Make query */
        	                fprintf(stderr,mysql_error(&demo_db));
        	                exit(1);
	                }
		}



		//logger
		sprintf(mysql_query, "insert DELAYED into out_logg values(%s,%s,'%s','%s','%s','%i','%s','%s','%f','%s')",
			"NULL",
			"NOW()",
			issuedadd.query,
			issuedadd.affuser,
			issuedadd.ppcuser,
			issuedadd.betaler_keyword_id,
			issuedadd.ipadress,
			issuedadd.HTTP_REFERER,
			issuedadd.bid,
			clickStatus);
	  
		if(mysql_real_query(&demo_db, mysql_query, strlen(mysql_query))){ /* Make query */
                        fprintf(stderr,mysql_error(&demo_db));
                        exit(1);
                }


	}


        mysql_free_result(mysqlres);
        mysql_close(&demo_db);


}