#include #include #include #include #include #include #include #include #include "globals.h" #define VERSION "asp2php 0.76.26 - April 11, 2006\n" \ "Copyright 1998-2006 By Michael Kohn (mike@mikekohn.net)\n" \ "For info and latest: http://asp2php.naken.cc/\n" \ "This program falls under the GPL License (http://www.gnu.org/)\n" #include "functions.h" /* ASP to PHP converter */ /* Copyright 1998-2004 by Michael Kohn */ /* DataBase Types: 1: MySQL 2: ODBC 3: Oracle 8 4: Sybase 5: Postgres Object Types: -1: Nothing 1: ADODB.Connection 2: ADODB.RecordSet 3: ADODB.Command 5: FileSystem Object 6: JMail 7: CDONTS.NewMail */ char tokenpush[TOKENSIZE]={ 0 }; int tokentypepush=0; char tokenpush2[TOKENSIZE]={ 0 }; int tokentypepush2=0; FILE *in,*out; int pushback=-1; int indent=0,line=1,column=0; char objects[80][50]; char objectstype[80][50]; int objectsptr=0; int database_type=0; int setvar=0; int add_isnum=0,add_session=0,BOF=0; int infunct=0,functptr=0,currdeffunct=-1; char funct[FUNCTHEAP]; char classes[FUNCTHEAP]; char global_uid[70]; char global_passwd[70]; char global_database[70]; char global_address[70]; char global_dsn[70]; char sessionpool[256][200]; int sessionptr=0; int classesptr=0; int pngs=0,y2k=1,casesense=0,php=0; int html=0,spacer=0; char aspextensions[20][10]; int aspextensions_ptr=0; int includes=0; int javascript=0; int loopcount=0; int global_asa=0; int gif2png=2; int language=0; int inbody=0; int fixwin95paths=0; int supress=0; int inclass=0; int flags=0; void convert(char *infile, char *outfile); int copyfiles(char *sourcedir, char *targetdir) { DIR *dp_asp; DIR *dp_php; struct dirent *direntp; struct stat statbuf; char newdir[2048]; char filename_asp[1024]; char filename_php[1024]; int t; dp_asp=opendir(sourcedir); while ((direntp=readdir(dp_asp))!=NULL) { if (strcmp(direntp->d_name,".")==0) continue; if (strcmp(direntp->d_name,"..")==0) continue; sprintf(filename_asp,"%s/%s",sourcedir,direntp->d_name); printf("working on file: %s\n",filename_asp); if (stat(filename_asp,&statbuf)==-1) { printf("Error: Couldn't stat file: %s\n\n",sourcedir); return 1; } if (statbuf.st_mode & S_IFDIR) { sprintf(filename_php,"%s/%s",targetdir,direntp->d_name); sprintf(filename_asp,"%s/%s",sourcedir,direntp->d_name); dp_php=opendir(filename_php); if (dp_php==0) { /* sprintf(newdir,"mkdir -p \"%s\"",filename_php); */ /* sprintf(newdir,"mkdir -p \"%s\"",filename_php); if (system(newdir)!=0) */ #ifdef __MINGW32__ if (mkdir(filename_php)!=0) #else if (mkdir(filename_php,511)!=0) #endif { printf("Error: Couldn't create dir: %s\n\n",filename_php); return 1; } } else { closedir(dp_php); } if (copyfiles(filename_asp,filename_php)==1) return 1; } else { sprintf(filename_php,"%s/%s",targetdir,direntp->d_name); sprintf(filename_asp,"%s/%s",sourcedir,direntp->d_name); t=strlen(filename_php); if (is_asp_filename(filename_asp)) { if (php==3) { change_extension(filename_php,".php3"); } else if (php==4) { change_extension(filename_php,".php4"); } else { change_extension(filename_php,".php"); } convert(filename_asp,filename_php); /* strcat(newdir,arguments); */ } else if (extcmp(filename_php,".htm")==1 || extcmp(filename_php,".html")==1) { if (html==1 && extcmp(filename_php,".htm")==1) { change_extension(filename_php,".html"); } convert(filename_asp,filename_php); } else { if (pngs==1 && extcmp(filename_php,".gif")==1 && gif2png!=0) { if (gif2png==2) { change_extension(filename_php,".png"); sprintf(newdir,"convert \"%s\" \"%s\"",filename_asp,filename_php); if (system(newdir)!=0) { printf("Error: Couldn't copy file: %s\n\n",filename_asp); return 1; } } else if (gif2png==1) { sprintf(newdir,"cp \"%s\" \"%s\" ; gif2png -d -O \"%s\"",filename_asp,filename_php,filename_php); if (system(newdir)!=0) { printf("Error: Couldn't copy file: %s\n\n",filename_asp); return 1; } } } else { if (copy_file(filename_asp,filename_php)!=0) { printf("Error: Couldn't copy file: %s\n\n",filename_asp); return 1; } /* sprintf(newdir,"cp \"%s\" \"%s\"",filename_asp,filename_php); */ } } } } closedir(dp_asp); return 0; } void start (char *infile, char *outfile) { char sourcedir[2048]; char targetdir[2048]; /* char command[2048]; */ DIR *dp_asp; DIR *dp_php; int t; /* if something looks redundant it's cause I took this code from GTKap2php and adapted it to work here :) */ strcpy(sourcedir,infile); strcpy(targetdir,outfile); t=strlen(sourcedir); while (t>0 && sourcedir[t-1]=='/') { sourcedir[t-1]=0; } t=strlen(targetdir); while (t>0 && targetdir[t-1]=='/') { targetdir[t-1]=0; } dp_asp=opendir(sourcedir); if (dp_asp==0) { printf("Error: ASP Source Dir %s doesn't exist.\n\n",sourcedir); return; } dp_php=opendir(targetdir); if (dp_php==0) { printf("Creating PHP target dir: %s\n\n",targetdir); /* man i really *HATE* Microsoft windows.. i used system with mkdir here cause it made life quite a bit easier with the -p and all and setting up dir permissions automatically, but i guess this doesn't work under DOS. sprintf(command,"mkdir -p \"%s\"",targetdir); t=system(command); if (t!=0) */ #ifdef __MINGW32__ if (mkdir(targetdir)!=0) #else if (mkdir(targetdir,511)!=0) #endif { printf("Error: Couldn't create PHP target dir: %s\n\n",targetdir); return; } dp_php=opendir(targetdir); if (dp_php==0) { printf("Error: Couldn't create PHP target dir\n"); return; } } closedir(dp_asp); closedir(dp_php); printf("Source Dir: %s\n",sourcedir); printf("Target Dir: %s\n\n",targetdir); fflush(stdout); /* printf("copying files\n"); */ if (copyfiles(sourcedir,targetdir)==1) { printf("Problem with sourcedir %s and targetdir %s\n",sourcedir,targetdir); } return; } void preparse(char *infile) { FILE *old_in; char token[TOKENSIZE]; char temp[TOKENSIZE]; int t,tokentype,ch,lastchar; old_in=in; in=fopen(infile,"rb"); if (in==NULL) { printf("Preparse: Error reading file: %s\n",infile); in=old_in; return; } while((ch=getc(in))!=EOF) { if (lastchar=='<' && ch=='%') { while((tokentype=gettoken(token))!=0) { if (tokentype==6) break; if (strcasecmp(token,"isnumeric")==0) add_isnum=1; else if (strcasecmp(token,"bof")==0) BOF=1; else if (strcasecmp(token,"function")==0) { tokentype=gettoken(token); add_funct(token); } else if (strcasecmp(token,"sub")==0) { tokentype=gettoken(token); add_funct(token); } else if (strcasecmp(token,"end")==0) { tokentype=gettoken(token); } else if (strcasecmp(token,"exit")==0) { tokentype=gettoken(token); } if (strcasecmp(token,"session")==0) { add_session=1; tokentype=gettoken(token); if (strcmp(token,"(")==0) { tokentype=gettoken(token); if (tokentype==10 || tokentype==11) { t=gettoken(temp); if (strcmp(temp,")")==0) { for (t=0; t0) t--; if (t!=0) outfile[t]=0; if (php==0) { strcat(outfile,".php"); } else if (php==3) { strcat(outfile,".php3"); } else if (php==4) { strcat(outfile,".php4"); } } if (strcmp(outfile,"-")!=0) { out=fopen(outfile,"w"); if (out==NULL) { printf("Error (convert()) opening output file: %s\n",outfile); return; } printf("Opening file: %s\n",infile); printf("Creating file: %s\n\n",outfile); } else { out=fdopen(STDOUT_FILENO,"w"); } preparse(infile); for (t=0; t<80; t++) { objects[t][0]=0; } in=fopen(infile,"rb"); if (in==NULL) { printf("Error reading file: %s\n",infile); exit(1); } if (add_session==1) insert_session(); if (global_asa==1) { fprintf(out,"\n"); } line=1; copyhtml(); if (supress==0) { putc('\n',out); printf("Done.\n"); } fclose(in); fclose(out); } int main(int argc, char *argv[]) { char infile[2048]; char outfile[2048]; int dirr=0,t; if (argc<2) { printf("Usage: asp2php { options } \n"); printf(" -o \n"); printf(" -dir (infile/outfile are dirs [READ DOCS!!])\n"); printf(" -mysql -odbc -oracle -sybase -postgres (to select database)\n"); printf(" -uid \n"); printf(" -passwd \n"); printf(" -database \n"); printf(" -address \n"); printf(" -dsn \n"); printf(" -pngs (convert references to gifs into pngs)\n"); printf(" -y2k (if your asp came from a non-y2k compliant NT Box)\n"); printf(" -php3 or -php4 (change extension to .php3 or .php4 instead of .php)\n"); printf(" -toupper (change all variables to uppercase)\n"); printf(" -tolower (change all variables to lowercase)\n"); printf(" -html (change all references from htm to html)\n"); printf(" -includes (change #inludes to php requires())\n"); printf(" -addextension .xxx (asp2php convert files with .xxx extensions)\n"); printf(" -spacer (space things out more)\n"); printf(" -gif2png (use gif2png instead of ImageMagick [-pngs mode only])\n"); printf(" -nomagick (don't convert gif files to png [-pngs mode only])\n"); printf(" -global_asa (include global.php in all pages)\n"); printf(" -fixwinpaths (fix windows paths [ '\\' becomes '/' ])\n"); printf(" -longexternvars (use, for example, $HTTP_POST_VARS instead of $_POST)\n"); printf(" -fulltags (asp2php will use %s\n",aspextensions[t]); } if (outfile[0]==0) { printf("You must set the output dir with the -o option.\n\n"); } else { start(infile,outfile); } } else { convert(infile,outfile); } return 0; }