{ *********************************************************************** } { } { PHP2Delphi Library } { } { PHPÀÇ ÇÔ¼öÇü½ÄÀ» µ¨ÆÄÀÌ¿¡¼­ »ç¿ëÇϱâÀ§ÇÑ ¶óÀ̺귯¸® } { } { ÀÛ¼º : ¼­µ¿È¯(donghanee@gmail.com) http://d-3-b.com } { } { ÀÛ¼ºÀÏ : 2004-10-14 } { ¾÷µ¥ÀÌÆ® : 2006-02-24 } { *********************************************************************** } unit PHP; interface uses Windows, Messages, Dialogs, SysUtils, Types, Classes, StrUtils, Math, DateUtils, HTTPApp, md5; Const PHP_RAND_MAX=2147483647; function php_basename(path:String; suffix:String=''):String; function php_copy(source:String; dest:String):Boolean; function php_date(format:String;timestamp:Integer=0):String; function php_dirname(path : String):String; function php_exec(command:String):String; function php_explode(Const separator, str:String):TStringList;overload; procedure php_explode(Const separator, str:String;var Data:TStringList);overload; function php_file_exists(filename:String):Boolean; function php_floor(value : Extended):Integer; function php_filesize(filename:String):Int64; function php_implode(glue:String;pieces:TStrings):String; function php_intval(str:String):Integer; function php_is_dir(filename:String):Boolean; function php_join(glue:String;pieces:TStrings):String; function php_mkdir(pathname:String;mode:Integer=0):Boolean; function php_md5(str:String;raw_output:Boolean=False):String; function php_rand(Min:Integer=0;Max:Integer=0):Integer; function php_round(val: Extended; precision:Integer = 0):Integer; function php_server(name:String):String; function php_strlen(str:String):Integer; function php_strpos(haystack : String;needle : string;offset:Integer=0):Integer; function php_strrchr(haystack:String;needle:String):String; function php_strrev(Const str:String):String; function php_strrpos(haystack:String;needle:String;offset:Integer=0):Integer; function php_strtolower(str:String):String; function php_strstr(haystack, needle:String):String; function php_str_replace(Const search, replace, subject:string):string; function php_substr(str : string;start:Integer;plength:Integer=0):String; function php_time():Integer; function php_trim (str:String):String; function php_urldecode(str:String):String; function php_urlencode(str:String):String; function php_utf8_decode(data:String):String; function php_utf8_encode(data:String):String; function php_unlink(filename:String):Boolean; function php_ord(str:String):Integer; function php_pow(base, exp:Integer):Integer; function php_htmlspecialchars(str:String;quote_style:Integer=0;charset:String=''):String; implementation function php_strtolower(str:String):String; begin Result := LowerCase(str); end; function php_htmlspecialchars(str:String;quote_style:Integer=0;charset:String=''):String; var restr : String; begin restr := str; restr := php_str_replace('&', '&', restr); restr := php_str_replace('"', '"', restr); restr := php_str_replace('''', ''', restr); restr := php_str_replace('<', '<', restr); restr := php_str_replace('>', '>', restr); Result := restr; end; { // read only error function php_filesize(filename:String):Integer; var f : file of Byte; begin Result := 0; if not php_file_exists(filename) then Exit; AssignFile( F, fileName ); Reset( F ); try Result := FileSize( F ); finally CloseFile( F ); end; end; } { // size -1 function php_filesize(filename:String):Integer; var FileInfo:integer; begin Result := 0; if not php_file_exists(filename) then Exit; FileInfo := FileOpen(ExTractfilename(filename), fmOpenRead+fmShareDenyNone); Result := GetFileSize(FileInfo, nil); FileClose(FileInfo); end; } function php_filesize(filename:String):Int64; var Stream : TFileStream; begin Result := 0; if not php_file_exists(filename) then Exit; Stream := TFileStream.Create(filename, fmOpenRead or fmShareDenyNone); Result := Stream.Size; Stream.Free; end; { md5¸ðµâÀÌ ÇÊ¿äÇÏ´Ù 2006-04-15 } function php_md5(str:String;raw_output:Boolean=False):String; begin Result := MD5Print(MD5String(str)); end; function php_pow(base, exp:Integer):Integer; begin Result := Round(Power(base, exp)); end; function php_ord(str:String):Integer; var CharData : Char; begin CharData := str[1]; Result := Ord(CharData); end; function php_unlink(filename:String):Boolean; begin Result := DeleteFile(filename); end; { Write : 2005-08-08 PHP ¿Í´Â ´Ù¸£°Ô Æí¸®ÇÔÀ» À§ÇØ mode ´Â ÀÔ·ÂÇÏÁö ¾Ê¾Æµµ µÈ´Ù. ¸Þ´º¾ó¿£ ¿À·ù·Î int mkdir ( string pathname, int mode) ·Î Ç¥½ÃµÊ. } function php_mkdir(pathname:String;mode:Integer=0):Boolean; begin Result := CreateDir(pathname); end; function php_is_dir(filename:String):Boolean; begin Result := DirectoryExists(filename); end; { 2005-06-27 } function php_date(format:String;timestamp:Integer=0):String; var dformat : String; time : TDateTime; begin dformat := php_str_replace('y', 'yy', format); dformat := php_str_replace('Y', 'yyyy', dformat); dformat := php_str_replace('m', 'mm', dformat); dformat := php_str_replace('M', 'm', dformat); dformat := php_str_replace('n', 'mmm', dformat); dformat := php_str_replace('H', 'hh', dformat); dformat := php_str_replace('d', 'dd', dformat); dformat := php_str_replace('j', 'd', dformat); dformat := php_str_replace('i', 'nn', dformat); dformat := php_str_replace('s', 'ss', dformat); if timestamp = 0 then begin time := Now; end else begin timestamp := timestamp + 9*60*60; //GMT + 9 time := UnixToDateTime(timestamp); end; Result := FormatDateTime(dformat, time); end; { 2005-06-19 } function php_trim (str:String):String; begin Result := Trim(str); end; function php_utf8_decode(data:String):String; begin Result := UTF8Decode(data); end; function php_utf8_encode(data:String):String; begin Result := UTF8Encode(data); end; { 2005-06-09 } function php_exec(command:String):String; begin WinExec(PChar(command), SW_SHOW); Result := ''; //°á°ú¹° Ãâ·Â ±¸Çö¸øÇÔ. end; { 2005-05-30 } function php_strrchr(haystack:String;needle:String):String; var pos : Integer; begin pos := php_strrpos(haystack, needle); Result := php_substr(haystack, pos); end; function php_strrpos(haystack:String;needle:String;offset:Integer=0):Integer; var i : Integer; hLen, nLen : Integer; begin hLen := php_strlen(haystack)+1; nLen := php_strlen(needle); Result := -1; for i := hLen-nLen downto 0 do begin if(Copy(haystack, i, nLen) = needle) then begin Result := i-1; break; end; end; end; { 2005-05-21 } function php_urlencode(str:String):String; var i : Integer; begin Result := ''; if Length(str) > 0 then for I := 1 to Length(str) do begin if not (str[I] in ['0'..'9', 'a'..'z', 'A'..'Z', ' ']) then Result := Result + '%' + IntToHex(Ord(str[I]), 2) else if not (str[I] = ' ') then Result := Result + str[I] else begin Result := Result + '+'; end; end; end; function php_urldecode(str:String):String; begin Result := HTTPDecode(str); end; { 2005-03-16 Modift 2005-07-31 } function php_intval(str:String):Integer; var rint : Integer; begin Result := 0; if(str = '') then begin exit; end; if(TryStrToInt(str, rint)) then begin Result := rint; end; end; function php_join(glue:String;pieces:TStrings):String; begin Result := php_implode(glue, pieces); end; { 2005-03-14 } function php_implode(glue:String;pieces:TStrings):String; var i : Integer; rstr : String; begin for i := 0 to pieces.Count - 1 do begin rstr := rstr + pieces.Strings[i]; if(i <> pieces.Count - 1) then begin rstr := rstr + glue; end; end; Result := rstr; end; { 2005-03-05 } function php_strlen(str:String):Integer; begin Result := Length(str); end; { 2005-03-05 } function php_strstr(haystack, needle:String):String; var findpos : Integer; begin Result := ''; findpos := php_strpos(haystack, needle); if findpos = -1 then Exit; Result := php_substr(haystack, findpos - 1); //string °ú Boolean ¸¦ °°ÀÌ ¸®ÅÏÇÒ¼ø¾ø´Ù end; { Write 2005-03-03 } function php_file_exists(filename:String):Boolean; begin Result := FileExists(filename); end; { Write 2005-02-24 } function php_time():Integer; begin Result := DateTimeToUnix(Now); Result := Result - 9*60*60; //GMT -9 end; { Write 2005-02-25 } function php_substr(str : string;start:Integer;plength:Integer=0):String; begin Inc(start); if(plength=0) then begin plength := Length(str); end else begin //Inc(plength); end; if start < 0 then begin start := Length(str) + start; end; Result := Copy(str, start, plength); end; { Write 2005-02-25 PHP¿Í´Â ´Ù¸£°Ô ¸øãÀ¸¸é -1¸¦ ¸®ÅÏÇÑ´Ù } function php_strpos(haystack : String;needle : string;offset:Integer=0):Integer; begin Result := Pos(needle, haystack)-1; end; { Write 2005-02-25 } function php_rand(Min:Integer=0;Max:Integer=0):Integer; begin Randomize; if(Max = 0) then Max := PHP_RAND_MAX; Result := RandomRange(Min, Max); end; { Write 2005-02-08 PHP ÀÇ $_SERVER["????"] ±â´É Á» ¾ïÁöÀ̱äÇÏ´Ù-_-; } function php_server(name:string):String; var send : String; begin send := ''; if(name = 'PHP_SELF') or (name = 'SCRIPT_FILENAME') then begin send := ParamStr(0); end else if(name = 'DOCUMENT_ROOT') then begin send := php_dirname(ParamStr(0)); end; Result := send; end; function php_str_replace(Const search, replace, subject:string):string; begin Result := StringReplace(subject, search, replace, [rfReplaceAll]); end; function php_explode(Const separator, str:String):TStringList; var List : TStringList; begin List := TStringList.Create; List.Text := php_str_replace(separator, #13#10, str); Result := List; // List.Free; end; procedure php_explode(Const separator, str:String;var Data:TStringList); begin Data.Text := php_str_replace(separator, #13#10, str); end; function php_strrev(Const str:String):String; begin Result := ReverseString(str); end; { Write : 2004-12-28 Bug } function php_round(val: Extended; precision:Integer = 0):Integer; begin Result := Round(val); end; { Write : 2004-12-28 } function php_floor(value : Extended):Integer; begin Result := Trunc(value); end; { Write : 2004-12-28 } function php_basename(path:String; suffix:String=''):String; begin Result := ExtractFileName(path); // À¥°æ·Î / À϶§ if php_strpos(Result, '/') <> -1 then begin Result := php_strrchr(Result, '/'); Result := php_substr(Result, 1) end; end; { Write : 2005-02-06 } function php_copy(source:String; dest:String):Boolean; var chk : Boolean; begin if not FileExists(source) then begin Result := False; exit; end; chk := CopyFile(PChar(source), PChar(dest), False); Result := chk; end; { Write : 2005-02-06 } function php_dirname(path : String):String; begin Result := ExtractFilePath(path); end; end.