-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathdsUtils.pas
224 lines (186 loc) · 5.25 KB
/
dsUtils.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
unit dsUtils;
{$ifdef fpc}
{$mode delphi}{$H+}
{$endif}
interface
uses
Classes, SysUtils, Dialogs, Controls, Forms, ShellAPI, ClipBrd, Windows, ShlObj,
Graphics, GifImage, Jpeg, PNGImage, exemod,
{$ifdef fpc}
LCLType,
{$endif}
zendTypes,
ZENDAPI,
phpTypes,
PHPAPI,
phpUtils,
php4delphi;
procedure InitializeDsUtils(PHPEngine: TPHPEngine);
procedure clipboard_setFiles(ht : integer; return_value: pzval; return_value_ptr: pzval; this_ptr : pzval;
return_value_used : integer; TSRMLS_DC : pointer); cdecl;
procedure clipboard_getFiles(ht : integer; return_value: pzval; return_value_ptr: pzval; this_ptr : pzval;
return_value_used : integer; TSRMLS_DC : pointer); cdecl;
procedure clipboard_assign(ht : integer; return_value: pzval; return_value_ptr: pzval; this_ptr : pzval;
return_value_used : integer; TSRMLS_DC : pointer); cdecl;
procedure clipboard_get(ht : integer; return_value: pzval; return_value_ptr: pzval; this_ptr : pzval;
return_value_used : integer; TSRMLS_DC : pointer); cdecl;
implementation
procedure CopyFilesToClipboard( FileList: string );
var
DropFiles: PDropFiles;
hGlobal: THandle;
iLen: Integer;
begin
iLen := Length( FileList ) + 2;
FileList := FileList + #0#0;
hGlobal := GlobalAlloc( GMEM_SHARE or GMEM_MOVEABLE or GMEM_ZEROINIT, SizeOf( TDropFiles ) + iLen );
if ( hGlobal = 0 ) then raise Exception.Create( 'Could not allocate memory.' );
begin
DropFiles := GlobalLock( hGlobal );
DropFiles^.pFiles := SizeOf( TDropFiles );
Move( FileList[1], ( PChar( DropFiles ) + SizeOf( TDropFiles ) )^, iLen );
GlobalUnlock( hGlobal);
Clipboard.SetAsHandle( CF_HDROP, hGlobal );
end;
end;
function GetFilesFromClipboard(): String;
var
f: THandle;
Buffer: array [0..1024] of Char;
i, numFiles: Integer;
begin
Clipboard.Open;
try
f := Clipboard.GetAsHandle( CF_HDROP ) ;
if f <> 0 then
begin
numFiles := DragQueryFile( f, $FFFFFFFF, nil, 0 ) ;
for i := 0 to numFiles-1 do
begin
Buffer[0] := #0;
DragQueryFile( f, i, Buffer, SizeOf( Buffer ) ) ;
if i <> 0 then
Result := Result + #13#10;
Result := Result + Buffer;
end;
end;
finally
Clipboard.Close;
end;
end;
procedure clipboard_setFiles;
var p: pzval_array;
FileList: String;
arr: PHashTable;
tmp: ^ppzval;
i, cnt: integer;
begin
if ht < 1 then begin zend_wrong_param_count(TSRMLS_DC); Exit; end;
zend_get_parameters_ex(ht, p);
FileList := '';
if p[0]^._type = IS_ARRAY then
begin
arr := p[0]^.value.ht;
cnt := zend_hash_num_elements(arr);
New(tmp);
for i := 0 to cnt - 1 do
begin
zend_hash_index_find(arr, i, tmp);
if Z_STRLEN(tmp^^) = 0 then
Continue;
if i = 0 then
FileList := FileList + (Z_STRVAL(tmp^^))
else
FileList := FileList + #0 + (Z_STRVAL(tmp^^));
end;
Dispose(tmp);
CopyFilesToClipboard( FileList );
end else
CopyFilesToClipboard( Z_STRVAL(p[0]^) );
dispose_pzval_array(p);
end;
procedure clipboard_getFiles;
var
FileList: TStrings;
i: integer;
begin
FileList := TStringList.Create;
FileList.Text := GetFilesFromClipboard;
_array_init( return_value, nil, 0 );
for i := 0 to FileList.Count - 1 do
add_index_stringl(return_value, i, PAnsiChar(FileList[i]), Length(FileList[i]), 1);
FileList.Free;
end;
procedure clipboard_assign;
var p: pzval_array;
format: String;
M: TMemoryStream;
PNG: TPNGObject;
JPG: TJPEGImage;
GIF: TGIFImage;
Pic: TPicture;
begin
if ht < 1 then begin zend_wrong_param_count(TSRMLS_DC); Exit; end;
zend_get_parameters_ex(ht, p);
if (ht > 1) and (p[1]^._type <> IS_NULL) then
begin
Pic := TPicture.Create;
M := TMemoryStream.Create;
format := LowerCase(Z_STRVAL(p[1]^));
try
String2Stream( Z_STRVAL(p[0]^), M );
if ( format = 'png' ) then
begin
PNG := TPNGObject.Create;
with PNG do
begin
LoadFromStream( M );
Pic.Assign(PNG);
end;
PNG.Free;
end
else if ( (format = 'jpeg') or (format = 'jpg') ) then
begin
JPG := TJPEGImage.Create;
with JPG do
begin
LoadFromStream( M );
Pic.Assign(JPG);
end;
JPG.Free;
end
else if ( format = 'gif' ) then
begin
GIF := TGIFImage.Create;
with GIF do
begin
LoadFromStream( M );
Pic.Assign(GIF);
end;
GIF.Free;
end
else if ( format = 'ico' ) then
Pic.Icon.LoadFromStream( M )
else if ( format = 'bmp' ) then
Pic.Bitmap.LoadFromStream( M );
Clipboard.Assign( Pic );
finally
M.Free;
Pic.Free;
end;
end else
Clipboard.Assign( TObject(Z_LVAL(p[0]^)) as TPersistent );
dispose_pzval_array(p);
end;
procedure clipboard_get;
begin
ZVAL_LONG(return_value, Integer(Clipboard));
end;
procedure InitializeDsUtils(PHPEngine: TPHPEngine);
begin
PHPEngine.AddFunction('clipboard_setFiles', @clipboard_setFiles);
PHPEngine.AddFunction('clipboard_getFiles', @clipboard_getFiles);
PHPEngine.AddFunction('clipboard_assign', @clipboard_assign);
PHPEngine.AddFunction('clipboard_get', @clipboard_get);
end;
end.