-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibwebview.php
373 lines (295 loc) · 9.69 KB
/
libwebview.php
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
//TAB=4
if (
! function_exists( 'popup_info' )
&&
! function_exists( 'popup_warning' )
&&
! function_exists( 'popup_error' )
&&
! function_exists( 'popup_question' )
)
{
define( 'POPUP_INFO' , 0 );
define( 'POPUP_WARNING' , 1 );
define( 'POPUP_ERROR' , 2 );
define( 'POPUP_QUESTION' , 3 );
function popup_warning ( string $MESSAGE , string $TITLE = NULL ) : bool { return popup_info( $MESSAGE , $TITLE , POPUP_WARNING ); }
function popup_error ( string $MESSAGE , string $TITLE = NULL ) : bool { return popup_info( $MESSAGE , $TITLE , POPUP_ERROR ); }
function popup_question( string $MESSAGE , string $TITLE = NULL ) : bool { return popup_info( $MESSAGE , $TITLE , POPUP_QUESTION ); }
switch( PHP_OS_FAMILY )
{
case 'Windows':
if ( ! extension_loaded( 'FFI' ) )
{
ini_set( 'extension_dir' , dirname( PHP_BINARY ).'\\ext' );
dl( dirname( PHP_BINARY ).'\\ext\\php_ffi.dll');
}
if ( extension_loaded( 'FFI' ) )
{
function popup_info( string $MESSAGE , string $TITLE = NULL , string $TYPE = POPUP_INFO ) : bool
{
static $FFI = null ;
if ( is_null( $FFI ) )
{
$CDEF = <<<CDEF
enum{
MB_OK = 0x00000000L,
MB_ABORTRETRYIGNORE = 0x00000002L ,
MB_CANCELTRYCONTINUE = 0x00000006L ,
MB_OKCANCEL = 0x00000001L ,
MB_RETRYCANCEL = 0x00000005L ,
MB_YESNO = 0x00000004L ,
MB_YESNOCANCEL = 0x00000003L ,
MB_HELP = 0x00004000L ,
MB_ICONEXCLAMATION = 0x00000030L ,
MB_ICONWARNING = 0x00000030L ,
MB_ICONINFORMATION = 0x00000040L ,
MB_ICONASTERISK = 0x00000040L ,
MB_ICONQUESTION = 0x00000020L ,
MB_ICONSTOP = 0x00000010L ,
MB_ICONERROR = 0x00000010L ,
MB_ICONHAND = 0x00000010L ,
};
typedef void* HANDLE;
typedef HANDLE HWND;
typedef const char* LPCSTR;
typedef unsigned UINT;
int MessageBoxW(void*, void*, void*, unsigned);
CDEF;
$FFI = FFI::cdef( $CDEF , 'user32.dll' );
}
$TITLE ??= $_SERVER['PHP_SELF'] ;
switch( $TYPE )
{
case POPUP_ERROR : $FLAGS = 0x10 ; break ; // OK
case POPUP_WARNING : $FLAGS = 0x30 ; break ; // OK
case POPUP_QUESTION: $FLAGS = 0x24 ; break ; // YES/NO
default: $FLAGS = 0x40 ; break ; // OK
}
if ( function_exists( 'iconv' ) )
{
$TITLE = iconv( "UTF-8", "UTF-16LE//TRANSLIT", $TITLE."\0\0\0" );
$MESSAGE = iconv( "UTF-8", "UTF-16LE//TRANSLIT", $MESSAGE."\0\0\0" );
}
$PRESSED = $FFI->MessageBoxW( NULL , $MESSAGE , $TITLE , $FLAGS );
return $PRESSED != 7;
}
return;
}
else
if ( function_exists( 'readline' ) )
{
goto popup_fallback_readline;
}
break;
case 'Linux' :
case 'BSD':
default:
if ( ! empty( exec( "which zenity" ) ) ) // GNOME
{
function popup_info( string $MESSAGE , string $TITLE = NULL , int $TYPE = POPUP_INFO ) : bool
{
$TITLE ??= $_SERVER['PHP_SELF'] ;
switch( $TYPE )
{
case POPUP_ERROR: $TYPE = 'error' ; break ;
case POPUP_QUESTION: $TYPE = 'question' ; break ;
case POPUP_WARNING: $TYPE = 'warning' ; break ;
default: $TYPE = 'info' ; break ;
}
$PRESSED = exec( "zenity --$TYPE --title=\"$TITLE\" --text=\"$MESSAGE\" ; echo $?" );
return $PRESSED == 0 ;
}
return;
}
else
if ( ! empty( exec( "which kdialog" ) ) ) // KDE
{
function popup_info( string $MESSAGE , string $TITLE = NULL , int $TYPE = POPUP_INFO ) : bool
{
$TITLE ??= $_SERVER['PHP_SELF'] ;
switch( $TYPE )
{
case POPUP_ERROR: $TYPE = 'error' ; break ;
case POPUP_QUESTION: $TYPE = 'yesno' ; break ;
case POPUP_WARNING: $TYPE = 'sorry' ; break ;
default: $TYPE = 'msgbox' ; break ;
}
$PRESSED = exec( "kdialog --title \"$TITLE\" --$TYPE \"$MESSAGE\" ; echo $?" );
return $PRESSED == 0 ;
}
return;
}
else
if ( ! empty( exec( "which xmessage" ) ) ) // X11
{
function popup_info( string $MESSAGE , string $TITLE = NULL , int $TYPE = POPUP_INFO ) : bool
{
$TITLE ??= $_SERVER['PHP_SELF'] ;
$BTNS = $TYPE == POPUP_QUESTION ? 'NO,YES' : 'OK' ;
switch( $TYPE )
{
case POPUP_ERROR : $ICO = 'X' ; break ;
case POPUP_QUESTION : $ICO = '?' ; break ;
case POPUP_WARNING : $ICO = '!' ; break ;
default: $ICO = 'i' ; break ;
}
if ( function_exists( 'iconv' ) )
{
$TITLE = iconv( "UTF-8", "ASCII//TRANSLIT", $TITLE );
$MESSAGE = iconv( "UTF-8", "ASCII//TRANSLIT", $MESSAGE );
}
$TITLE = addslashes( $TITLE );
$MESSAGE = addslashes( $MESSAGE );
$PRESSED = exec( "echo \"[$ICO] ___ $TITLE ___ [$ICO]\n\n$MESSAGE\n\" | xmessage -buttons $BTNS -center -print -file -" );
return $PRESSED != 'NO' ;
}
return;
}
else
if ( function_exists( 'readline' ) )
{
popup_fallback_readline:
function popup_info( string $MESSAGE , string $TITLE = NULL , int $TYPE = POPUP_INFO ) : bool
{
$TITLE ??= $_SERVER['PHP_SELF'] ;
switch( $TYPE )
{
case POPUP_ERROR : $ICO = '[ ERROR ]' ; break ;
case POPUP_QUESTION : $ICO = '[ QUESTION ]' ; break ;
case POPUP_WARNING : $ICO = '[ WARNING ]' ; break ;
default: $ICO = '[ INFO ]' ; break ;
}
echo "$ICO $TITLE : $MESSAGE" ;
if ( $TYPE == POPUP_QUESTION )
{
$PRESSED = strtolower( readline( " ( Yes / No ) : " ) )[0] ?? 'y' ;
return $PRESSED != 'n' ;
}
readline( "( Press Enter to continue ... )" );
return true ;
}
return;
}
else // read
{
function popup_info( string $MESSAGE , string $TITLE = NULL , int $TYPE = POPUP_INFO ) : bool
{
$TITLE ??= $_SERVER['PHP_SELF'] ;
switch( $TYPE )
{
case POPUP_ERROR : $ICO = '[ ERROR ]' ; break ;
case POPUP_QUESTION : $ICO = '[ QUESTION ]' ; break ;
case POPUP_WARNING : $ICO = '[ WARNING ]' ; break ;
default: $ICO = '[ INFO ]' ; break ;
}
echo "$ICO $TITLE : $MESSAGE" ;
if ( $TYPE == POPUP_QUESTION )
{
$PRESSED = strtolower( exec( 'read -p " ( Yes / No ) : " answer ; echo $answer' ) )[0] ?? 'y' ;
return $PRESSED != 'n' ;
}
readline( "( Press Enter to continue ... )" );
return true ;
}
return;
}
break;
} //endswitch
}//endif function_exists()
// -------------------------------------------
class WebView
{
const HINT_NONE = 0 ; // Width and height are default size
const HINT_MIN = 1 ; // Width and height are minimum bounds
const HINT_MAX = 2 ; // Width and height are maximum bounds
const HINT_FIXED = 3 ; // Window size can not be changed by a user
static object $FFI ;
static object $FFI_null ;
static function _init() : bool
{
if ( PHP_OS_FAMILY != 'Windows' )
{
// TODO : check if required libs are installed, and display messagebox if not
// Debian : "apt install libgtk-3-0 libwebkit2gtk-4.0-37"
// Fedora : "dnf install gtk3 webkit2gtk4.0"
}
if ( isset( self::$FFI ) ) return true ;
self::$FFI_null = FFI::cast('void*',null);
$header_dirs = [ './' , './webview/' , './lib/' , './include/' ];
$header_file = 'libwebview.h' ;
foreach( $header_dirs as $dir )
{
if ( file_exists( $dir.$header_file ) )
{
$header_file = $dir.$header_file ;
break ;
}
}
if ( ! file_exists( $header_file ) )
{
popup_error( "`libwebview.h` not found." );
return false ;
}
$lib_dirs = [ './' , './webview/' , './lib/' , './include/' ];
$lib_ext = PHP_OS_FAMILY == 'Windows' ? '.dll' : '.so' ;
$lib_file = 'libwebview'.$lib_ext ;
foreach( $lib_dirs as $dir )
{
if ( file_exists( $dir.$lib_file ) )
{
$lib_file = $dir.$lib_file ;
break ;
}
}
if ( ! file_exists( $lib_file ) )
{
popup_error( "`libwebview$lib_ext` not found." );
return false ;
}
try
{
self::$FFI = FFI::cdef( file_get_contents( $header_file ) , $lib_file );
}
catch( Error $E )
{
popup_error( $E->getMessage() , 'Exception' );
return false ;
}
return true ;
}
// ---------------------------
private object $webview ;
function __construct( bool $debug = false )
{
self::_init();
$this->webview = $this->webview = self::$FFI->webview_create( $debug , null );
}
function __destruct()
{
self::$FFI->webview_destroy( $this->webview );
}
function run() { self::$FFI->webview_run( $this->webview ); }
function terminate() { self::$FFI->webview_terminate( $this->webview ); }
function dispatch( callable $fn ) { self::$FFI->webview_dispatch( $this->webview , $fn , self::$FFI_null ); }
function set_title( string $title ) { self::$FFI->webview_set_title( $this->webview , $title ); }
function set_size( int $width , int $height , int $hints = 0 ) { self::$FFI->webview_set_size( $this->webview , $width , $height , $hints ); }
function navigate( string $url ) { self::$FFI->webview_navigate( $this->webview , $url ); }
function set_html( string $html ) { self::$FFI->webview_set_html( $this->webview , $html ); }
function init( string $js ) { self::$FFI->webview_init( $this->webview , $js ); } // executed before every window.onload
function eval( string $js ) { self::$FFI->webview_eval( $this->webview , $js ); }
function bind( string $name , callable $fn )
{
$cb = function( $seq , $req ) use ( $fn )
{
$args = json_decode( $req );
$results = $fn( $args );
$results = is_null( $results ) ? '' : json_encode( $results ) ;
self::$FFI->webview_return( $this->webview , $seq , 0 , $results );
};
self::$FFI->webview_bind( $this->webview , $name , $cb , self::$FFI_null );
}
function unbind( string $name ) { self::$FFI->webview_unbind( $this->webview , $name ); }
}
//EOF