Update Files

This commit is contained in:
2025-01-22 16:18:30 +01:00
parent ed4603cf95
commit a36294b518
16718 changed files with 2960346 additions and 0 deletions

View File

@ -0,0 +1,124 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys;
import php.*;
import haxe.io.Path;
private enum FileKind {
kdir;
kfile;
kother(k:String);
}
@:coreApi
class FileSystem {
public static inline function exists(path:String):Bool {
Global.clearstatcache(true, path);
return Global.file_exists(path);
}
public static inline function rename(path:String, newPath:String):Void {
Global.rename(path, newPath);
}
public static function stat(path:String):FileStat {
Global.clearstatcache(true, path);
var info = Global.stat(path);
if (info == false)
throw 'Unable to stat $path';
var info:NativeArray = info;
return {
gid: info['gid'],
uid: info['uid'],
atime: Date.fromTime(info['atime'] * 1000),
mtime: Date.fromTime(info['mtime'] * 1000),
ctime: Date.fromTime(info['ctime'] * 1000),
dev: info['dev'],
ino: info['ino'],
nlink: info['nlink'],
rdev: info['rdev'],
size: info['size'],
mode: info['mode']
};
}
public static inline function fullPath(relPath:String):String {
return Syntax.shortTernary(Global.realpath(relPath), null);
}
public static function absolutePath(relPath:String):String {
if (Path.isAbsolute(relPath))
return relPath;
return Path.join([Sys.getCwd(), relPath]);
}
static function kind(path:String):FileKind {
Global.clearstatcache(true, path);
var kind = Global.filetype(path);
if (kind == false)
throw 'Failed to check file type $path';
switch (kind) {
case "file":
return kfile;
case "dir":
return kdir;
default:
return kother(kind);
}
}
public static function isDirectory(path:String):Bool {
Global.clearstatcache(true, path);
return Global.is_dir(path);
}
public static function createDirectory(path:String):Void {
Global.clearstatcache(true, path);
if (!Global.is_dir(path))
Global.mkdir(path, 493, true);
}
public static inline function deleteFile(path:String):Void {
Global.unlink(path);
}
public static inline function deleteDirectory(path:String):Void {
Global.rmdir(path);
}
public static function readDirectory(path:String):Array<String> {
var list = [];
var dir = Global.opendir(path);
var file;
while ((file = Global.readdir(dir)) != false) {
if (file != '.' && file != '..') {
list.push(file);
}
}
Global.closedir(dir);
return list;
}
}

View File

@ -0,0 +1,268 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.db;
import php.*;
import sys.db.*;
import php.db.*;
import php.db.Mysqli_result;
@:coreApi class Mysql {
public static function connect(params:{
host:String,
?port:Int,
user:String,
pass:String,
?socket:String,
?database:String
}):Connection {
return new MysqlConnection(params);
}
}
private class MysqlConnection implements Connection {
var db:Mysqli;
public function new(params:{
host:String,
?port:Int,
user:String,
pass:String,
?socket:String,
?database:String
}):Void {
if (params.port == null)
params.port = Std.parseInt(Global.ini_get('mysqli.default_port'));
if (params.socket == null)
params.socket = Global.ini_get('mysqli.default_socket');
if (params.database == null)
params.database = "";
db = new Mysqli(params.host, params.user, params.pass, params.database, params.port, params.socket);
}
public function request(s:String):ResultSet {
var result = db.query(s);
if (result == false)
throw 'Failed to perform db query: ' + db.error;
if (result == true) {
return new WriteMysqlResultSet(db.affected_rows);
}
return new MysqlResultSet(result);
}
public function close():Void {
db.close();
}
public function escape(s:String):String {
return db.escape_string(s);
}
public function quote(s:String):String {
if (s.indexOf("\000") >= 0)
return "x'" + Global.bin2hex(s) + "'";
return "'" + db.escape_string(s) + "'";
}
public function addValue(s:StringBuf, v:Dynamic):Void {
if (Global.is_int(v) || Global.is_null(v)) {
s.add(v);
} else if (Global.is_bool(v)) {
s.add(v ? 1 : 0);
} else {
s.add(quote(Std.string(v)));
}
}
public function lastInsertId():Int {
return db.insert_id;
}
public function dbName():String {
return 'MySQL';
}
public function startTransaction():Void {
var success = db.begin_transaction();
if (!success)
throw 'Failed to start transaction: ' + db.error;
}
public function commit():Void {
var success = db.commit();
if (!success)
throw 'Failed to commit transaction: ' + db.error;
}
public function rollback():Void {
var success = db.rollback();
if (!success)
throw 'Failed to rollback transaction: ' + db.error;
}
}
private class MysqlResultSet implements ResultSet {
static var hxAnonClassName = Boot.getHxAnon().phpClassName;
public var length(get, null):Int;
public var nfields(get, null):Int;
var result:Mysqli_result;
var fetchedRow:NativeAssocArray<Scalar>;
var fieldsInfo:NativeAssocArray<MysqliFieldInfo>;
public function new(result:Mysqli_result) {
this.result = result;
}
public function hasNext():Bool {
if (fetchedRow == null)
fetchNext();
return fetchedRow != null;
}
public function next():Dynamic {
if (fetchedRow == null)
fetchNext();
return withdrawFetched();
}
public function results():List<Dynamic> {
var list = new List();
result.data_seek(0);
var row = result.fetch_object(hxAnonClassName);
while (row != null) {
row = correctObjectTypes(row);
list.add(row);
row = result.fetch_object(hxAnonClassName);
}
return list;
}
public function getResult(n:Int):String {
if (fetchedRow == null)
fetchNext();
return Global.array_values(fetchedRow)[n];
}
public function getIntResult(n:Int):Int {
return Syntax.int(getResult(n));
}
public function getFloatResult(n:Int):Float {
return Syntax.float(getResult(n));
}
public function getFieldsNames():Null<Array<String>> {
var fields = result.fetch_fields();
return [for (field in fields) field.name];
}
function fetchNext() {
var row = result.fetch_assoc();
if (row != null)
fetchedRow = correctArrayTypes(row);
}
function withdrawFetched():Dynamic {
if (fetchedRow == null)
return null;
var row = fetchedRow;
fetchedRow = null;
return Boot.createAnon(row);
}
function correctArrayTypes(row:NativeAssocArray<String>):NativeAssocArray<Scalar> {
var fieldsInfo = getFieldsInfo();
Syntax.foreach(row, function(field:String, value:String) {
row[field] = correctType(value, fieldsInfo[field].type);
});
return cast row;
}
function correctObjectTypes(row:{}):{} {
var fieldsInfo = getFieldsInfo();
Syntax.foreach(row, function(field:String, value:String) {
value = correctType(value, fieldsInfo[field].type);
Syntax.setField(row, field, value);
});
return row;
}
inline function getFieldsInfo():NativeAssocArray<MysqliFieldInfo> {
if (fieldsInfo == null) {
fieldsInfo = cast Syntax.arrayDecl();
Syntax.foreach(result.fetch_fields(), function(_, info) {
fieldsInfo[info.name] = info;
});
}
return fieldsInfo;
}
function correctType(value:String, type:Int):Scalar {
if (value == null)
return null;
if (type == Const.MYSQLI_TYPE_BIT || type == Const.MYSQLI_TYPE_TINY || type == Const.MYSQLI_TYPE_SHORT || type == Const.MYSQLI_TYPE_LONG
|| type == Const.MYSQLI_TYPE_INT24 || type == Const.MYSQLI_TYPE_CHAR) {
return Syntax.int(value);
}
if (type == Const.MYSQLI_TYPE_DECIMAL
|| type == Const.MYSQLI_TYPE_NEWDECIMAL
|| type == Const.MYSQLI_TYPE_FLOAT
|| type == Const.MYSQLI_TYPE_DOUBLE) {
return Syntax.float(value);
}
return value;
}
function get_length()
return result.num_rows;
function get_nfields()
return result.field_count;
}
private class WriteMysqlResultSet extends MysqlResultSet {
var affectedRows:Int = 0;
public function new(affectedRows:Int) {
super(null);
this.affectedRows = affectedRows;
}
override public function hasNext():Bool {
return false;
}
override function fetchNext() {}
override function get_length()
return affectedRows;
override function get_nfields()
return 0;
}

View File

@ -0,0 +1,218 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.db;
import php.*;
import php.Global.*;
import php.db.*;
import sys.db.*;
@:coreApi class Sqlite {
public static function open(file:String):Connection {
return new SQLiteConnection(file);
}
}
private class SQLiteConnection implements Connection {
var db:SQLite3;
public function new(file:String) {
db = new SQLite3(file);
db.enableExceptions(true);
}
public function request(s:String):ResultSet {
var result = db.query(s);
return new SQLiteResultSet(result);
}
public function close():Void {
db.close();
}
public function escape(s:String):String {
return SQLite3.escapeString(s);
}
public function quote(s:String):String {
if (s.indexOf("\000") >= 0)
return "x'" + Global.bin2hex(s) + "'";
return "'" + SQLite3.escapeString(s) + "'";
}
public function addValue(s:StringBuf, v:Dynamic):Void {
if (Global.is_int(v) || Global.is_null(v)) {
s.add(v);
} else if (Global.is_bool(v)) {
s.add(v ? 1 : 0);
} else {
s.add(quote(Std.string(v)));
}
}
public function lastInsertId():Int {
return Syntax.int(db.lastInsertRowID());
}
public function dbName():String {
return 'SQLite';
}
public function startTransaction():Void {
db.query('BEGIN TRANSACTION');
}
public function commit():Void {
db.query('COMMIT');
}
public function rollback():Void {
db.query('ROLLBACK');
}
}
private class SQLiteResultSet implements ResultSet {
public var length(get, null):Int;
public var nfields(get, null):Int;
var cache = new NativeIndexedArray<{}>();
var result:SQLite3Result;
var resultIsDepleted = false;
var fieldsInfo:NativeAssocArray<Int>;
public function new(result:SQLite3Result) {
this.result = result;
}
public function hasNext():Bool {
return switch next() {
case null: false;
case row:
array_unshift(cache, row);
row;
}
}
public function next():Dynamic {
return switch array_shift(cache) {
case null: fetchNext();
case row: row;
}
}
function fetchNext():Null<{}> {
return resultIsDepleted ? null : switch result.fetchArray(Const.SQLITE3_ASSOC) {
case false:
resultIsDepleted = true;
result.finalize();
null;
case row:
Boot.createAnon(correctArrayTypes(row));
}
}
public function cacheAll():NativeIndexedArray<{}> {
var row = fetchNext();
while(row != null) {
cache.push(row);
row = fetchNext();
}
return cache;
}
public function results():List<Dynamic> {
var list = new List();
for(row in cacheAll()) {
list.add(row);
}
return list;
}
function getColumn(n:Int):Any {
return array_values(Syntax.array(current()))[n];
}
public function getResult(n:Int):String {
return Syntax.string(getColumn(n));
}
public function getIntResult(n:Int):Int {
return Syntax.int(getColumn(n));
}
public function getFloatResult(n:Int):Float {
return Syntax.float(getColumn(n));
}
public function getFieldsNames():Null<Array<String>> {
var fieldsInfo = getFieldsInfo();
return Global.array_keys(fieldsInfo);
}
function current():Null<{}> {
return switch reset(cache) {
case false:
switch next() {
case null: null;
case row:
cache.push(row);
row;
}
case row: row;
}
}
function correctArrayTypes(row:NativeAssocArray<String>):NativeAssocArray<Scalar> {
var fieldsInfo = getFieldsInfo();
Syntax.foreach(row, function(field:String, value:String) {
row[field] = correctType(value, fieldsInfo[field]);
});
return cast row;
}
inline function getFieldsInfo():NativeAssocArray<Int> {
if (fieldsInfo == null) {
fieldsInfo = cast Syntax.arrayDecl();
for (i in 0...nfields) {
fieldsInfo[result.columnName(i)] = result.columnType(i);
}
}
return fieldsInfo;
}
function correctType(value:String, type:Int):Scalar {
if (value == null)
return null;
if (type == Const.SQLITE3_INTEGER)
return Syntax.int(value);
if (type == Const.SQLITE3_FLOAT)
return Syntax.float(value);
return value;
}
function get_length():Int
return count(cacheAll());
function get_nfields():Int
return result.numColumns();
}

View File

@ -0,0 +1,69 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.io;
import php.Global.*;
import php.Global;
@:coreApi class File {
public static function getContent(path:String):String {
return file_get_contents(path);
}
public static function getBytes(path:String):haxe.io.Bytes {
return haxe.io.Bytes.ofString(file_get_contents(path));
}
public static function saveContent(path:String, content:String):Void {
file_put_contents(path, content);
}
public static function saveBytes(path:String, bytes:haxe.io.Bytes):Void {
var f = write(path);
f.write(bytes);
f.close();
}
public static function read(path:String, binary:Bool = true):FileInput {
return @:privateAccess new FileInput(fopen(path, binary ? "rb" : "r"));
}
public static function write(path:String, binary:Bool = true):FileOutput {
return untyped new FileOutput(fopen(path, binary ? "wb" : "w"));
}
public static function append(path:String, binary:Bool = true):FileOutput {
return untyped new FileOutput(fopen(path, binary ? "ab" : "a"));
}
public static function update(path:String, binary:Bool = true):FileOutput {
if (!FileSystem.exists(path)) {
write(path).close();
}
return untyped new FileOutput(fopen(path, binary ? "rb+" : "r+"));
}
public static function copy(srcPath:String, dstPath:String):Void {
Global.copy(srcPath, dstPath);
}
}

View File

@ -0,0 +1,100 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.io;
import haxe.io.Eof;
import haxe.io.Error;
import haxe.io.Bytes;
import php.*;
import php.Global.*;
import php.Const.*;
@:coreApi
class FileInput extends haxe.io.Input {
private var __f:Resource;
function new(f:Resource):Void {
__f = f;
}
public override function readByte():Int {
var r = fread(__f, 1);
if (feof(__f))
throw new Eof();
if (r == false)
throw Custom('An error occurred');
return ord(r);
}
public override function readBytes(s:Bytes, p:Int, l:Int):Int {
if (feof(__f))
throw new Eof();
var r = fread(__f, l);
if (r == false)
throw Custom('An error occurred');
if (strlen(r) == 0)
throw new Eof();
var b = Bytes.ofString(r);
s.blit(p, b, 0, strlen(r));
return strlen(r);
}
public override function close():Void {
super.close();
if (__f != null)
fclose(__f);
}
public function seek(p:Int, pos:FileSeek):Void {
var w;
switch (pos) {
case SeekBegin:
w = SEEK_SET;
case SeekCur:
w = SEEK_CUR;
case SeekEnd:
w = SEEK_END;
}
var r = fseek(__f, p, w);
if (r == -1)
throw Custom('An error occurred');
}
public function tell():Int {
var r = ftell(__f);
if (r == false)
throw Custom('An error occurred');
return cast r;
}
public function eof():Bool {
return feof(__f);
}
override function readLine():String {
var r = fgets(__f);
if (false == r)
throw new Eof();
return rtrim(r, "\r\n");
}
}

View File

@ -0,0 +1,86 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.io;
import php.*;
import php.Global.*;
import php.Const.*;
@:coreApi
class FileOutput extends haxe.io.Output {
private var __f:Resource;
function new(f:Resource):Void {
__f = f;
}
public override function writeByte(c:Int):Void {
var r = fwrite(__f, chr(c));
if (r == false)
throw haxe.io.Error.Custom('An error occurred');
}
public override function writeBytes(b:haxe.io.Bytes, p:Int, l:Int):Int {
var s = b.getString(p, l);
if (feof(__f))
throw new haxe.io.Eof();
var r = fwrite(__f, s, l);
if (r == false)
throw haxe.io.Error.Custom('An error occurred');
return r;
}
public override function flush():Void {
var r = fflush(__f);
if (r == false)
throw haxe.io.Error.Custom('An error occurred');
}
public override function close():Void {
super.close();
if (__f != null)
fclose(__f);
}
public function seek(p:Int, pos:FileSeek):Void {
var w;
switch (pos) {
case SeekBegin:
w = SEEK_SET;
case SeekCur:
w = SEEK_CUR;
case SeekEnd:
w = SEEK_END;
}
var r = fseek(__f, p, w);
if (r == -1)
throw haxe.io.Error.Custom('An error occurred');
}
public function tell():Int {
var r = ftell(__f);
if (r == false)
throw haxe.io.Error.Custom('An error occurred');
return r;
}
}

View File

@ -0,0 +1,200 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.io;
import php.*;
import haxe.io.*;
import haxe.SysTools;
using StringTools;
using php.Global;
@:forward(iterator)
private abstract ProcessPipes(NativeIndexedArray<Resource>) from NativeIndexedArray<Resource> to NativeIndexedArray<Resource> {
public var stdin(get, never):Resource;
public var stdout(get, never):Resource;
public var stderr(get, never):Resource;
inline function get_stdin()
return this[0];
inline function get_stdout()
return this[1];
inline function get_stderr()
return this[2];
}
private class ReadablePipe extends Input {
var pipe:Resource;
var tmpBytes:Bytes;
public function new(pipe:Resource) {
this.pipe = pipe;
tmpBytes = Bytes.alloc(1);
}
override public function close():Void {
pipe.fclose();
}
override public function readByte():Int {
if (readBytes(tmpBytes, 0, 1) == 0)
throw Error.Blocked;
return tmpBytes.get(0);
}
override public function readBytes(s:Bytes, pos:Int, len:Int):Int {
if (pipe.feof())
throw new Eof();
var result = pipe.fread(len);
if (result == "")
throw new Eof();
if (result == false)
return throw Error.Custom('Failed to read process output');
var result:String = result;
var bytes = Bytes.ofString(result);
s.blit(pos, bytes, 0, result.strlen());
return result.strlen();
}
}
private class WritablePipe extends Output {
var pipe:Resource;
var tmpBytes:Bytes;
public function new(pipe:Resource) {
this.pipe = pipe;
tmpBytes = Bytes.alloc(1);
}
override public function close():Void {
pipe.fclose();
}
override public function writeByte(c:Int):Void {
tmpBytes.set(0, c);
writeBytes(tmpBytes, 0, 1);
}
override public function writeBytes(b:Bytes, pos:Int, l:Int):Int {
var s = b.getString(pos, l);
if (pipe.feof())
throw new Eof();
var result = Global.fwrite(pipe, s, l);
if (result == false)
throw Error.Custom('Failed to write to process input');
return result;
}
}
class Process {
public var stdout(default, null):Input;
public var stderr(default, null):Input;
public var stdin(default, null):Output;
var process:Resource;
var pipes:ProcessPipes;
var pid:Int = -1;
var running:Bool = true;
var _exitCode:Int = -1;
public function new(cmd:String, ?args:Array<String>, ?detached:Bool):Void {
if (detached)
throw "Detached process is not supported on this platform";
var descriptors = Syntax.arrayDecl(Syntax.arrayDecl('pipe', 'r'), Syntax.arrayDecl('pipe', 'w'), Syntax.arrayDecl('pipe', 'w'));
var result = buildCmd(cmd, args).proc_open(descriptors, pipes);
if (result == false)
throw Error.Custom('Failed to start process: $cmd');
process = result;
updateStatus();
stdin = new WritablePipe(pipes.stdin);
stdout = new ReadablePipe(pipes.stdout);
stderr = new ReadablePipe(pipes.stderr);
}
public function getPid():Int {
return pid;
}
public function exitCode(block:Bool = true):Null<Int> {
if (!block) {
updateStatus();
return (running ? null : _exitCode);
}
while (running) {
var arr = Syntax.arrayDecl(process);
try {
Syntax.suppress(Global.stream_select(arr, arr, arr, null));
} catch(_) {}
updateStatus();
}
return _exitCode;
}
public function close():Void {
if (!running)
return;
for (pipe in pipes)
Global.fclose(pipe);
process.proc_close();
}
public function kill():Void {
process.proc_terminate();
}
function buildCmd(cmd:String, ?args:Array<String>):String {
if (args == null)
return cmd;
return switch (Sys.systemName()) {
case "Windows":
[cmd.replace("/", "\\")].concat(args).map(SysTools.quoteWinArg.bind(_, true)).join(" ");
case _:
[cmd].concat(args).map(SysTools.quoteUnixArg).join(" ");
}
}
function updateStatus():Void {
if (!running)
return;
var status = process.proc_get_status();
if (status == false)
throw Error.Custom('Failed to obtain process status');
var status:NativeAssocArray<Scalar> = status;
pid = status['pid'];
running = status['running'];
_exitCode = status['exitcode'];
}
}

View File

@ -0,0 +1,62 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.net;
import php.Global.*;
import php.SuperGlobal.*;
@:coreApi
class Host {
public var host(default, null):String;
private var _ip:String;
public var ip(default, null):Int;
public function new(name:String):Void {
host = name;
if (~/^(\d{1,3}\.){3}\d{1,3}$/.match(name)) {
_ip = name;
} else {
_ip = gethostbyname(name);
if (_ip == name) {
ip = 0;
return;
}
}
var p = _ip.split('.');
ip = intval(sprintf('%02X%02X%02X%02X', p[3], p[2], p[1], p[0]), 16);
}
public function toString():String {
return _ip;
}
public function reverse():String {
return gethostbyaddr(_ip);
}
public static function localhost():String {
return php.Syntax.coalesce(_SERVER['HTTP_HOST'], "localhost");
}
}

View File

@ -0,0 +1,200 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.net;
import php.*;
import php.Global.*;
import php.Const.*;
import sys.io.FileInput;
import sys.io.FileOutput;
@:coreApi
class Socket {
private var __s:Resource;
private var stream:Resource;
public var input(default, null):haxe.io.Input;
public var output(default, null):haxe.io.Output;
public var custom:Dynamic;
var protocol:String;
public function new():Void {
input = @:privateAccess new sys.io.FileInput(null);
output = @:privateAccess new sys.io.FileOutput(null);
initSocket();
protocol = "tcp";
}
private function initSocket():Void {
__s = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
}
private function assignHandler():Void {
stream = socket_export_stream(__s);
@:privateAccess (cast input : FileInput).__f = stream;
@:privateAccess (cast output : FileOutput).__f = stream;
}
public function close():Void {
socket_close(__s);
@:privateAccess (cast input : FileInput).__f = null;
@:privateAccess (cast output : FileOutput).__f = null;
input.close();
output.close();
}
public function read():String {
var b = '';
while (!feof(stream))
b += fgets(stream);
return b;
}
public function write(content:String):Void {
fwrite(stream, content);
}
public function connect(host:Host, port:Int):Void {
var r = socket_connect(__s, host.host, port);
checkError(r, 0, 'Unable to connect');
assignHandler();
}
public function listen(connections:Int):Void {
var r = socket_listen(__s, connections);
checkError(r, 0, 'Unable to listen on socket');
assignHandler();
}
public function shutdown(read:Bool, write:Bool):Void {
var rw = read && write ? 2 : (write ? 1 : (read ? 0 : 2));
var r = socket_shutdown(__s, rw);
checkError(r, 0, 'Unable to shutdown');
}
public function bind(host:Host, port:Int):Void {
var r = socket_bind(__s, host.host, port);
checkError(r, 0, "Unable to bind socket");
}
public function accept():Socket {
var r = socket_accept(__s);
checkError(r, 0, 'Unable to accept connections on socket');
var s = new Socket();
// FIXME: wasted resource here
s.__s = r;
s.assignHandler();
return s;
}
public function peer():{host:Host, port:Int} {
var host:String = "", port:Int = 0;
var r = socket_getpeername(__s, host, port);
checkError(r, 0, 'Unable to retrieve the peer name');
return {host: new Host(host), port: port};
}
public function host():{host:Host, port:Int} {
var host:String = "", port:Int = 0;
var r = socket_getsockname(__s, host, port);
checkError(r, 0, 'Unable to retrieve the host name');
return {host: new Host(host), port: port};
}
public function setTimeout(timeout:Float):Void {
var s = Std.int(timeout);
var ms = Std.int((timeout - s) * 1000000);
var timeOut:NativeStructArray<{sec:Int, usec:Int}> = {sec: s, usec: ms};
var r = socket_set_option(__s, SOL_SOCKET, SO_RCVTIMEO, timeOut);
checkError(r, 0, 'Unable to set receive timeout');
r = socket_set_option(__s, SOL_SOCKET, SO_SNDTIMEO, timeOut);
checkError(r, 0, 'Unable to set send timeout');
}
public function setBlocking(b:Bool):Void {
var r = b ? socket_set_block(__s) : socket_set_nonblock(__s);
checkError(r, 0, 'Unable to set blocking');
}
public function setFastSend(b:Bool):Void {
var r = socket_set_option(__s, SOL_TCP, TCP_NODELAY, true);
checkError(r, 0, "Unable to set TCP_NODELAY on socket");
}
public function waitForRead():Void {
select([this], null, null);
}
private static function checkError(r:Bool, code:Int, msg:String):Void {
if (r != false)
return;
throw haxe.io.Error.Custom('Error [$code]: $msg');
}
/**
Since PHP 8 sockets are represented as instances of class \Socket
TODO:
rewrite without `cast` after resolving https://github.com/HaxeFoundation/haxe/issues/9964
*/
static inline function getSocketId(s:Resource):Int {
return PHP_VERSION_ID < 80000 ? Syntax.int(s) : spl_object_id(cast s);
}
public static function select(read:Array<Socket>, write:Array<Socket>, others:Array<Socket>,
?timeout:Float):{read:Array<Socket>, write:Array<Socket>, others:Array<Socket>} {
var map:Map<Int, Socket> = new Map();
inline function addSockets(sockets:Array<Socket>) {
if (sockets != null)
for (s in sockets)
map[getSocketId(s.__s)] = s;
}
inline function getRaw(sockets:Array<Socket>):Array<Resource> {
return sockets == null ? [] : [for (s in sockets) s.__s];
}
inline function getOriginal(result:Array<Resource>) {
return [for (r in result) map[getSocketId(r)]];
}
addSockets(read);
addSockets(write);
addSockets(others);
// unwrap Sockets into Resources
var rawRead:NativeIndexedArray<Resource> = getRaw(read),
rawWrite:NativeIndexedArray<Resource> = getRaw(write),
rawOthers:NativeIndexedArray<Resource> = getRaw(others);
var sec = timeout == null ? null : Std.int(timeout);
var usec = timeout == null ? 0 : Std.int((timeout % 1) * 1000000);
var result = socket_select(rawRead, rawWrite, rawOthers, sec, usec);
checkError(result, 0, "Error during select call");
// convert raw resources back to Socket objects
return {
read: getOriginal(rawRead),
write: getOriginal(rawWrite),
others: getOriginal(rawOthers),
}
}
}