Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
43 changed files
with
454 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
function stream_context_create (options, params) { | ||
// http://kevin.vanzonneveld.net | ||
// + original by: Brett Zamir (http://brettz9.blogspot.com) | ||
// % note 1: Can be made to work as a wrapper for proprietary contexts as well | ||
// * example 1: var opts = {http:{ method:'GET', header: 'Accept-language: en\r\nCookie: foo=bar\r\n' } }; | ||
// * example 1: var context = stream_context_create(opts); | ||
// * example 1: get_resource_type(context); | ||
// * returns 1: 'stream-context' | ||
|
||
var resource = {}; | ||
options = options || {}; | ||
// params.notification is only property currently in PHP for params | ||
|
||
// BEGIN REDUNDANT | ||
if (!this.php_js) { | ||
this.php_js = {}; | ||
} | ||
if (!this.php_js.resourceIdCounter) { | ||
this.php_js.resourceIdCounter = 0; | ||
} | ||
|
||
function PHPJS_Resource (type, id, opener) { // Can reuse the following for other resources, just changing the instantiation | ||
// See http://php.net/manual/en/resource.php for types | ||
this.type = type; | ||
this.id = id; | ||
this.opener = opener; | ||
} | ||
PHPJS_Resource.prototype.toString = function () { | ||
return 'Resource id #'+this.id; | ||
}; | ||
PHPJS_Resource.prototype.get_resource_type = function () { | ||
return this.type; | ||
}; | ||
PHPJS_Resource.prototype.var_dump = function () { | ||
return 'resource('+this.id+') of type ('+this.type+')'; | ||
// return 'resource('+this.id+'), '+this.type+')'; another format | ||
}; | ||
// END REDUNDANT | ||
|
||
this.php_js.resourceIdCounter++; | ||
|
||
resource = new PHPJS_Resource('stream-context', this.php_js.resourceIdCounter, 'stream_context_create'); | ||
resource.stream_options = options; | ||
resource.stream_params = params; | ||
|
||
return resource; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
function stream_context_get_default (options) { | ||
// http://kevin.vanzonneveld.net | ||
// + original by: Brett Zamir (http://brettz9.blogspot.com) | ||
// - depends on: stream_context_create | ||
// % note 1: Although for historical reasons in PHP, this function can be used with | ||
// % note 1: its options argument to set the default, it is no doubt best to use | ||
// % note 1: stream_context_set_default() to do so | ||
// * example 1: var context = stream_context_get_default(); | ||
// * example 1: get_resource_type(context); | ||
// * returns 1: 'stream-context' | ||
|
||
// BEGIN REDUNDANT | ||
if (!this.php_js) { | ||
this.php_js = {}; | ||
} | ||
// END REDUNDANT | ||
|
||
if (!this.php_js.default_streams_context) { | ||
this.php_js.default_streams_context = this.stream_context_create(options); | ||
} | ||
if (options) { | ||
this.php_js.default_streams_context.stream_options = options; | ||
} | ||
|
||
return this.php_js.default_streams_context; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
function stream_context_get_options (stream_or_context) { | ||
// http://kevin.vanzonneveld.net | ||
// + original by: Brett Zamir (http://brettz9.blogspot.com) | ||
// * example 1: var opts = {http:{method:'GET', header: 'Accept-language: en\r\nCookie: foo=bar\r\n'}}; | ||
// * example 1: var context = stream_context_create(opts); | ||
// * example 1: stream_context_get_options(context); | ||
// * returns 1: {http:{ method:'GET', header: 'Accept-language: en\r\nCookie: foo=bar\r\n' }} | ||
|
||
return stream_or_context.stream_options; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
function stream_context_get_params (stream_or_context) { | ||
// http://kevin.vanzonneveld.net | ||
// + original by: Brett Zamir (http://brettz9.blogspot.com) | ||
// * example 1: var params = {notification:function (notification_code, severity, message, message_code, bytes_transferred, bytes_max) {}}; | ||
// * example 1: var context = stream_context_create({}, params); | ||
// * example 1: stream_context_get_params(context); | ||
// * returns 1: {notification:function (notification_code, severity, message, message_code, bytes_transferred, bytes_max) {}, options:{}} | ||
|
||
return stream_or_context.stream_params; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
function stream_context_set_default (options) { | ||
// http://kevin.vanzonneveld.net | ||
// + original by: Brett Zamir (http://brettz9.blogspot.com) | ||
// - depends on: stream_context_create | ||
// * example 1: var opts = {http:{ method:'GET', header: 'Accept-language: en\r\nCookie: foo=bar\r\n' } }; | ||
// * example 1: var context = stream_context_set_default(opts); | ||
// * example 1: get_resource_type(context); | ||
// * returns 1: 'stream-context' | ||
|
||
// BEGIN REDUNDANT | ||
if (!this.php_js) { | ||
this.php_js = {}; | ||
} | ||
// END REDUNDANT | ||
if (!this.php_js.default_streams_context) { | ||
this.php_js.default_streams_context = this.stream_context_create(options); | ||
} | ||
this.php_js.default_streams_context.stream_options = options; | ||
|
||
return this.php_js.default_streams_context; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
function stream_context_set_option (stream_or_context, optionsOrWrapper, option, value) { | ||
// http://kevin.vanzonneveld.net | ||
// + original by: Brett Zamir (http://brettz9.blogspot.com) | ||
// * example 1: var opts = {http:{ method:'GET', header: 'Accept-language: en\r\nCookie: foo=bar\r\n' } }; | ||
// * example 1: var context = stream_context_create(opts); | ||
// * example 1: stream_context_set_option(context, opts); | ||
// * returns 1: true | ||
|
||
if (option) { | ||
if (!stream_or_context.stream_options[optionsOrWrapper]) { // Don't overwrite all? | ||
stream_or_context.stream_options[optionsOrWrapper] = {}; | ||
} | ||
stream_or_context.stream_options[optionsOrWrapper][option] = value; | ||
} | ||
else { | ||
stream_or_context.stream_options = optionsOrWrapper; | ||
} | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
function stream_context_set_params (stream_or_context, params) { | ||
// http://kevin.vanzonneveld.net | ||
// + original by: Brett Zamir (http://brettz9.blogspot.com) | ||
// * example 1: var context = stream_context_create(); | ||
// * example 1: stream_context_set_params({notification:function (notification_code, severity, message, message_code, bytes_transferred, bytes_max) {}}); | ||
// * returns 1: true | ||
|
||
var param = ''; | ||
|
||
// Docs also allow for "options" as a parameter here (i.e., setting options instead of parameters) and source seems to allow encoding, input_encoding, output_encoding, and default_mode | ||
for (param in params) { // Overwrites all, or just supplied? Seems like just supplied | ||
if (param === 'options') { | ||
stream_or_context.stream_options = params[param]; // Overwrite all? | ||
} | ||
else { | ||
stream_or_context.stream_params[param] = params[param]; | ||
} | ||
} | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
function stream_get_wrappers () { | ||
// http://kevin.vanzonneveld.net | ||
// + original by: Brett Zamir (http://brettz9.blogspot.com) | ||
// * example 1: stream_get_wrappers(); | ||
// * returns 1: ['php', 'file', 'http'] | ||
|
||
var p = '', retArr = []; | ||
|
||
// BEGIN REDUNDANT | ||
if (!this.php_js) { | ||
this.php_js = {}; | ||
} | ||
if (!this.php_js.stream_wrappers) { | ||
this.php_js.stream_wrappers = {}; | ||
} | ||
if (!this.php_js.default_stream_wrappers) { | ||
this.php_js.default_stream_wrappers = {}; | ||
} | ||
// END REDUNDANT | ||
|
||
for (p in this.php_js.default_stream_wrappers) { | ||
retArr.push(p); | ||
} | ||
for (p in this.php_js.stream_wrappers) { | ||
retArr.push(p); | ||
} | ||
return retArr; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
function stream_is_local (stream_or_url) { | ||
// http://kevin.vanzonneveld.net | ||
// + original by: Brett Zamir (http://brettz9.blogspot.com) | ||
// * example 1: stream_is_local('/etc'); | ||
// * returns 1: true | ||
|
||
if (typeof stream_or_url === 'string') { | ||
return (/^(https?|ftps?|ssl|tls):/.test(stream_or_url)) ? false : true; // Need a better check than this | ||
} | ||
return stream_or_url.is_local ? true : false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
function stream_register_wrapper (protocol, classname, flags) { | ||
// http://kevin.vanzonneveld.net | ||
// + original by: Brett Zamir (http://brettz9.blogspot.com) | ||
// - depends on: stream_wrapper_register | ||
// % note 1: In addition to accepting a global classname, we'll also allow a class constructor to be passed in | ||
// * example 1: function VariableStream () {} | ||
// * example 1: VariableStream.prototype = {stream_open: function () {}, stream_read : function () {}, stream_write : function () {}, stream_tell : function () {}, stream_eof : function () {}, stream_seek : function () {}}; | ||
// * example 1: stream_wrapper_register('var', 'VariableStream'); | ||
// * returns 1: true | ||
|
||
return this.stream_wrapper_register(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
function stream_wrapper_register (protocol, classname, flags) { | ||
// http://kevin.vanzonneveld.net | ||
// + original by: Brett Zamir (http://brettz9.blogspot.com) | ||
// % note 1: In addition to accepting a global classname, we'll also allow a class constructor to be passed in | ||
// * example 1: function VariableStream () {} | ||
// * example 1: VariableStream.prototype = {stream_open: function () {}, stream_read : function () {}, stream_write : function () {}, stream_tell : function () {}, stream_eof : function () {}, stream_seek : function () {}}; | ||
// * example 1: stream_wrapper_register('var', 'VariableStream'); | ||
// * returns 1: true | ||
|
||
var opts = 0, i = 0; | ||
|
||
// BEGIN REDUNDANT | ||
if (!this.php_js) { | ||
this.php_js = {}; | ||
} | ||
if (!this.php_js.stream_wrappers) { | ||
this.php_js.stream_wrappers = {}; | ||
} | ||
// END REDUNDANT | ||
|
||
if (this.php_js.stream_wrappers[protocol]) { | ||
return false; // must call stream_wrapper_unregister() before trying to assign | ||
} | ||
|
||
if (typeof classname === 'string') { | ||
var win = window; // make configurable by custom phpjs ini later | ||
classname = win[classname]; | ||
} | ||
if (!flags) {flags = 0;} | ||
var OPTS = { | ||
STREAM_IS_URL : 1, | ||
}; | ||
if (typeof options === 'number') { | ||
opts = flags; | ||
} | ||
else { // Allow for a single string or an array of string flags (currently only one possible though) | ||
flags = [].concat(flags); | ||
for (i=0; i < flags.length; i++) { | ||
// Resolve string input to bitwise | ||
if (OPTS[flags[i]]) { | ||
opts = opts | OPTS[flags[i]]; | ||
} | ||
} | ||
} | ||
|
||
this.php_js.stream_wrappers[protocol] = {is_url: opts & OPTS.STREAM_IS_URL, clss: classname}; | ||
|
||
/* | ||
// The stream wrapper prototype "class" (all properties and methods below are public); more like an interface, but not all methods are required | ||
streamWrapper { | ||
// Properties | ||
resource context ; // $context in PHP | ||
// Methods | ||
__construct ( void ) | ||
bool dir_closedir ( void ) | ||
bool dir_opendir ( string $path , int $options ) | ||
string dir_readdir ( void ) | ||
bool dir_rewinddir ( void ) | ||
bool mkdir ( string $path , int $mode , int $options ) | ||
bool rename ( string $path_from , string $path_to ) | ||
bool rmdir ( string $path , int $options ) | ||
resource stream_cast ( int $cast_as ) | ||
void stream_close ( void ) | ||
bool stream_eof ( void ) | ||
bool stream_flush ( void ) | ||
bool stream_lock ( mode $operation ) | ||
bool stream_open ( string $path , string $mode , int $options , string &$opened_path ) | ||
string stream_read ( int $count ) | ||
bool stream_seek ( int $offset , int $whence = SEEK_SET ) | ||
bool stream_set_option ( int $option , int $arg1 , int $arg2 ) | ||
array stream_stat ( void ) | ||
int stream_tell ( void ) | ||
int stream_write ( string $data ) | ||
bool unlink ( string $path ) | ||
array url_stat ( string $path , int $flags ) | ||
} | ||
*/ | ||
|
||
// COPY-AND-PASTE | ||
/* | ||
function streamWrapper () { | ||
this.context; | ||
} | ||
streamWrapper.prototype = { | ||
constructor : streamWrapper, | ||
dir_closedir : function () {}, | ||
dir_opendir : function (path, options) {}, | ||
dir_readdir : function () {}, | ||
dir_rewinddir : function () {}, | ||
mkdir : function (path, mode, options) {}, | ||
rename : function (path_from, path_to) {}, | ||
rmdir : function (path, options) {}, | ||
stream_cast : function (cast_as) {}, | ||
stream_close : function () {}, | ||
stream_eof : function () {}, | ||
stream_flush : function () {}, | ||
stream_lock : function (mode, operation) {}, | ||
stream_open : function (path, mode, options, opened_path) {}, | ||
stream_read : function (count) {}, | ||
stream_seek : function (offset, whence) { | ||
if (!whence) { | ||
whence = 'SEEK_SET'; | ||
} | ||
}, | ||
stream_set_option : function (option, arg1, arg2) {}, | ||
stream_stat : function () {}, | ||
stream_tell : function () {}, | ||
stream_write : function (data) {}, | ||
unlink : function (path) {}, | ||
url_stat : function (path, flags) {} | ||
}; | ||
*/ | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
function stream_wrapper_restore (protocol) { | ||
// http://kevin.vanzonneveld.net | ||
// + original by: Brett Zamir (http://brettz9.blogspot.com) | ||
// * example 1: stream_wrapper_restore('http'); | ||
// * returns 1: true | ||
|
||
if (!this.php_js || !this.php_js.default_stream_wrappers || !this.php_js.default_stream_wrappers[protocol] || | ||
!this.php_js.default_stream_wrappers[protocol].disabled | ||
) { | ||
return false; | ||
} | ||
this.php_js.default_stream_wrappers[protocol].disabled = false; | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
function stream_wrapper_unregister (protocol) { | ||
// http://kevin.vanzonneveld.net | ||
// + original by: Brett Zamir (http://brettz9.blogspot.com) | ||
// * example 1: stream_wrapper_unregister('var'); | ||
// * returns 1: true | ||
|
||
// BEGIN REDUNDANT | ||
if (!this.php_js) { | ||
this.php_js = {}; | ||
} | ||
if (!this.php_js.stream_wrappers) { | ||
this.php_js.stream_wrappers = {}; | ||
} | ||
if (!this.php_js.default_stream_wrappers) { | ||
this.php_js.default_stream_wrappers = {}; | ||
} | ||
// END REDUNDANT | ||
|
||
if (this.php_js.stream_wrappers[protocol]) { | ||
delete this.php_js.stream_wrappers[protocol]; // in case there is a user-registered one | ||
} | ||
else { | ||
if (!this.php_js.default_stream_wrappers[protocol]) { | ||
this.php_js.default_stream_wrappers[protocol] = {}; | ||
} | ||
this.php_js.default_stream_wrappers[protocol].disabled = true; | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
function stream_socket_accept () { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
function stream_socket_client () { | ||
|
||
} |
Oops, something went wrong.