static inline BYTE *readFormFromSocket( Socket_t *s, ULONG *length ) { BYTE *retData = NULL; char buffer[ MAX_SIZE ]; int readSize; readSize = SocketRead( s, buffer, MAX_SIZE, 0 ); DataForm *ldf = (DataForm *)buffer; // its our packet if( ldf->df_ID == ID_FCRE ) { // message is bigger then buffer // we must read it all if( ldf->df_Size > MAX_SIZE ) { unsigned int readedBytes = readSize; BufString *bs = BufStringNew(); BufStringAddSize( bs, buffer, readSize ); // reading bytes till end while( readedBytes <= ldf->df_Size ) { readSize = SocketRead( s, buffer, MAX_SIZE, 0 ); BufStringAddSize( bs, buffer, readSize ); readedBytes += readSize; } FFree( bs ); *length = bs->bs_Size; return (BYTE *)bs->bs_Buffer; } // message fits buffer else { if( ( retData = FCalloc( readSize, sizeof(BYTE) ) ) != NULL ) { memcpy( retData, buffer, readSize ); *length = readSize; } else { ERROR("Cannot allocate memory for message buffer\n"); } } } return retData; }
int FileWrite( struct File *f, char *buffer, int wsize ) { int result = -1; SpecialData *sd = (SpecialData *)f->f_SpecialData; if( sd ) { DEBUG("File write %s\n", buffer ); result = BufStringAddSize( sd->fp->nf_Data, buffer, wsize ); sd->fp->nf_Offset += result; } return wsize; }
void AppSessionThread( void *args ) { struct FThread *ft = (FThread *)args; AppSession *as = (AppSession *)ft->t_Data; struct timeval timeout; fd_set fds; while( ft->t_Quit != TRUE ) { FD_ZERO( &fds ); FD_SET( as->as_WritePipe, &fds ); timeout.tv_sec = 5000; timeout.tv_usec = 0; int err = select( as->as_WritePipe+1, &fds, NULL, NULL, &timeout ); if( err < 0 ) { FERROR("Problem\n"); } else if( err == 0 ) { DEBUG("Timeout\n"); } else { #define BUFFER_SIZE 2048 char buffer[ BUFFER_SIZE ]; BufString *bs = BufStringNew(); int size = -1; while( size != 0 )//!feof( (FILE *) as->as_WritePipe ) ) { // Make a new buffer and read size = read( as->as_WritePipe, buffer, BUFFER_SIZE ); //int size = fread( buffer, sizeof(char), BUFFER_SIZE, (FILE *)as->as_WritePipe ); BufStringAddSize( bs, buffer, size ); } BufStringDelete( bs ); } } ft->t_Launched = FALSE; }
BufString *Dir( File *s, const char *path ) { char *comm = NULL; if( ( comm = calloc( strlen( path ) +512, sizeof(char) ) ) != NULL ) { strcpy( comm, s->f_Name ); strcat( comm, ":" ); if( path != NULL ) { strcat( comm, path ); } if( s != NULL ) { SpecialData *sd = (SpecialData *)s->f_SpecialData; char command[ 1024 ]; // maybe we should count that... sprintf( command, "php \"modules/system/module.php\" \"type=%s&module=files&args=false&command=directory&authkey=false&sessionid=%s&path=%s&subPath=\";", sd->type, s->f_SessionID, comm//path ); int answerLength; BufString *bs = NULL; ListString *result = PHPCall( command, &answerLength ); if( result != NULL ) { bs =BufStringNewSize( result->ls_Size ); if( bs != NULL ) { BufStringAddSize( bs, result->ls_Data, result->ls_Size ); } ListStringDelete( result ); } // we should parse result to get information about success free( comm ); return bs; } free( comm ); } return NULL; }
BufString *Info( File *s, const char *path ) { DEBUG("[INRAM] Info!\n"); BufString *bs = BufStringNew(); BufStringAdd( bs, "ok<!--separate-->"); DEBUG("Info!\n"); // user is trying to get access to not his directory DEBUG("Check access for path '%s' in root path '%s' name '%s'\n", path, s->f_Path, s->f_Name ); int error = 0; SpecialData *srd = (SpecialData *) s->f_SpecialData; INRAMFile *dir =INRAMFileGetLastPath( srd->root, path, &error ); if( dir != NULL ) { FillStat( bs, dir, s, path ); } else { DEBUG("[INRAM] file stat FAIL %s\n", path ); SpecialData *locsd = (SpecialData *)s->f_SpecialData; SystemBase *l = (SystemBase *)locsd->sb; char buffer[ 256 ]; int size = snprintf( buffer, sizeof(buffer), "{ \"response\": \"%s\", \"code\":\"%d\" }", l->sl_Dictionary->d_Msg[DICT_FILE_OR_DIRECTORY_DO_NOT_EXIST] , DICT_FILE_OR_DIRECTORY_DO_NOT_EXIST ); BufStringAddSize( bs, buffer, size ); //BufStringAdd( bs, "{ \"response\": \"File or directory do not exist\"}" ); } DEBUG("[INRAM] Info END\n"); return bs; }
Image *ImageRead( struct ImageLibrary *im, File *rootDev, const char *path ) { Image *img = NULL; FHandler *fh = rootDev->f_FSys; File *rfp = (File *)fh->FileOpen( rootDev, path, "rb" ); if( rfp != NULL ) { BufString *bs = BufStringNew( ); char buffer[ 20048 ]; int len = 0; while( ( len = fh->FileRead( rfp, buffer, 20048 ) ) > 0 ) { BufStringAddSize( bs, buffer, len ); } ExceptionInfo *ei=AcquireExceptionInfo(); ImageInfo *ii=CloneImageInfo((ImageInfo *) NULL); img = BlobToImage( ii, bs->bs_Buffer, bs->bs_Size, ei ); if( img == NULL ) { ERROR("Cannot convert file data to image\n"); } DestroyExceptionInfo( ei ); DestroyImageInfo( ii ); BufStringDelete( bs ); fh->FileClose( rootDev, rfp ); } else { ERROR("Cannot open file: %s to read\n", path ); } }
BufString *Dir( File *s, const char *path ) { if( s != NULL ) { SpecialData *sd = (SpecialData *)s->f_SpecialData; char command[ 1024 ]; // maybe we should count that... sprintf( command, "php \"modules/system/module.php\" \"type=%s&module=files&args=false&command=directory&authkey=false&sessionid=%s&path=%s&subPath=\";", sd->type, s->f_SessionID, path ); int answerLength; ListString *result = PHPCall( command, &answerLength ); BufString *bs =BufStringNewSize( result->ls_Size ); BufStringAddSize( bs, result->ls_Data, result->ls_Size ); ListStringDelete( result ); // we should parse result to get information about success return bs; } return NULL; }
BufString *AppSessionRemUserByNames( AppSession *as, UserSession *loggedSession, char *userlist ) { BufString *bs = BufStringNew(); if( userlist == NULL ) { DEBUG("Userlist is empty!\n"); BufStringAdd( bs, " {\"removed\": [ ] }" ); return bs; } BufStringAdd( bs, " {\"removed\": [" ); unsigned int i = 0; unsigned int usersi = 0; char *upositions[ 128 ]; memset( upositions, 0, sizeof( upositions ) ); if( as != NULL ) { // remove spaces and 'weird' chars from entry unsigned int i, j=0; DEBUG("[AppSession] user list: %s\n", userlist ); if( userlist != NULL ) { unsigned int listsize = strlen( userlist ); for( i=0 ; i < listsize ; i++ ) { if( userlist[ i ] == '[' || userlist[ i ] == ']' || userlist[ i ] == ' ' || userlist[ i ] == '"' ) { if( userlist[ i ] == ']' ) { userlist[ i ] = 0; DEBUG("End\n"); break; } } else { userlist[ j ] = userlist[ i ]; j++; } } userlist[ j ] = 0; upositions[ 0 ] = userlist; usersi = 1; listsize = strlen( userlist ); for( i = 1 ; i < listsize; i++ ) { if( userlist[ i ] == ',' ) { userlist[ i ] = 0; upositions[ usersi++ ] = &(userlist[ i+1 ]); } } //userlistadded = FCalloc( usersi * 512, sizeof(char) ); } } else { DEBUG("[AppSession] as = NULL\n" ); } // find user sessions by username // and send message pthread_mutex_lock( &as->as_SessionsMut ); UserSession *adminSession = NULL; char tmp[ 1024 ]; int msgsndsize = 0; SASUList *asul = as->as_UserSessionList; SASUList **rementr = NULL; unsigned int rementrnum = 0; int returnEntry = 0; DEBUG("[AppSession] Number of entries in SAS %d\n", as->as_UserNumber ); if( as->as_UserNumber > 0 ) { rementr = FCalloc( as->as_UserNumber+100, sizeof(SASUList *) ); User *assidAdmin = NULL; while( asul != NULL ) { for( i = 0 ; i < usersi ; i++ ) { if( asul->usersession->us_User != NULL ) { DEBUG("[AppSession] Checking user '%s'\n", upositions[ i ] ); if( strcmp( upositions[ i ], asul->usersession->us_User->u_Name ) == 0 ) { char locbuf[ 128 ]; int size = 0; if( returnEntry == 0 ) { size = snprintf( locbuf, sizeof(locbuf), "%s", asul->usersession->us_User->u_Name ); } else { size = snprintf( locbuf, sizeof(locbuf), ",%s", asul->usersession->us_User->u_Name ); } BufStringAddSize( bs, locbuf, size ); returnEntry++; if( asul->usersession == as->as_UserSessionList->usersession ) { DEBUG("[AppSession] Admin will be removed\n"); adminSession = asul->usersession; } rementr[ rementrnum++ ] = asul; } } } asul = (SASUList *) asul->node.mln_Succ; } } pthread_mutex_unlock( &as->as_SessionsMut ); // // we want to remove admin // if( rementr != NULL ) { if( adminSession != NULL ) { asul = as->as_UserSessionList; while( asul != NULL ) { int len = sprintf( tmp, "{\"type\":\"msg\",\"data\": { \"type\":\"%s\", \"data\":{\"type\":\"%lu\", \"data\":{ \"identity\":{\"username\":\"%s\"},\"data\": {\"type\":\"sasid-close\",\"data\":\"%s\"}}}}}", asul->authid, as->as_SASID, loggedSession->us_User->u_Name, asul->usersession->us_User->u_Name ); msgsndsize += WebSocketSendMessageInt( asul->usersession, tmp, len ); asul = (SASUList *) asul->node.mln_Succ; } } else { for( i=0 ; i < rementrnum ; i++ ) { DEBUG("[AppSession] authid %s sasid %lu userptr %p usersessptr %p usersessuser ptr %p\n", rementr[ i ]->authid, as->as_SASID, loggedSession->us_User, rementr[ i ]->usersession, rementr[ i ]->usersession->us_User ); int len = sprintf( tmp, "{\"type\":\"msg\",\"data\": { \"type\":\"%s\", \"data\":{\"type\":\"%lu\", \"data\":{ \"identity\":{\"username\":\"%s\"},\"data\": {\"type\":\"sasid-close\",\"data\":\"%s\"}}}}}", rementr[ i ]->authid, as->as_SASID, loggedSession->us_User->u_Name, rementr[ i ]->usersession->us_User->u_Name ); msgsndsize += WebSocketSendMessageInt( rementr[ i ]->usersession, tmp, len ); AppSessionRemUsersession( as, rementr[ i ]->usersession ); } } FFree( rementr ); } BufStringAdd( bs, "] }" ); return bs; }
gdImagePtr ImageRead( struct ImageLibrary *im, File *rootDev, const char *path ) { gdImagePtr img = NULL; FHandler *fh = rootDev->f_FSys; File *rfp = (File *)fh->FileOpen( rootDev, path, "rb" ); if( rfp != NULL ) { BufString *bs = BufStringNew( ); char buffer[ 20048 ]; int len = 0; while( ( len = fh->FileRead( rfp, buffer, 20048 ) ) > 0 ) { BufStringAddSize( bs, buffer, len ); } img = gdImageCreateFromJpegPtr( bs->bs_Size, (void *)bs->bs_Buffer ) ; if( img == NULL ) { if( img == NULL ) { img = gdImageCreateFromBmpPtr( bs->bs_Size, (void *)bs->bs_Buffer ) ; if( img == NULL ) { img = gdImageCreateFromGifPtr( bs->bs_Size, (void *)bs->bs_Buffer ) ; if( img == NULL ) { img = gdImageCreateFromPngPtr( bs->bs_Size, (void *)bs->bs_Buffer ) ; if( img == NULL ) { img = gdImageCreateFromTgaPtr( bs->bs_Size, (void *)bs->bs_Buffer ) ; if( img == NULL ) { img = gdImageCreateFromTiffPtr( bs->bs_Size, (void *)bs->bs_Buffer ) ; if( img == NULL ) { img = gdImageCreateFromWBMPPtr( bs->bs_Size, (void *)bs->bs_Buffer ) ; if( img == NULL ) { img = gdImageCreateFromWebpPtr( bs->bs_Size, (void *)bs->bs_Buffer ) ; } } } } } } } } if( img == NULL ) { ERROR("Graphics format not recognized\n"); } BufStringDelete( bs ); fh->FileClose( rootDev, rfp ); } else { ERROR("Cannot open file: %s to read\n", path ); } return img; }