JavaScript stream_get_line function

A JavaScript equivalent of PHP’s stream_get_line

function stream_get_line (handle, length, ending) {
  // http://kevin.vanzonneveld.net
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // *     example 1: fopen('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm', 'r');
  // *     example 1: stream_get_line(handle, 2);
  // *     returns 1: '<'
  var start = 0,
    fullline = '';

  if (!this.php_js || !this.php_js.resourceData || !this.php_js.resourceDataPointer || length !== undefined && !length) {
    return false;
  }

  start = this.php_js.resourceDataPointer[handle.id];

  if (start === undefined || !this.php_js.resourceData[handle.id][start]) {
    return false; // Resource was already closed or already reached the end of the file
  }

  // Fix: Should we also test for /\r\n?|\n/?
  fullline = this.php_js.resourceData[handle.id].slice(start, this.php_js.resourceData[handle.id].indexOf(ending, start) + 1);
  if (fullline === '') {
    fullline = this.php_js.resourceData[handle.id].slice(start); // Get to rest of the file
  }

  length = (length === undefined || fullline.length < length) ? fullline.length : Math.floor(length / 2) || 1; // two bytes per character (or surrogate), but ensure at least one
  this.php_js.resourceDataPointer[handle.id] += length;
  return this.php_js.resourceData[handle.id].substr(start, length - (fullline && ending && ending.length ? ending.length : 0));
}

Example 1
fopen('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm', 'r');
stream_get_line(handle, 2);

Should return
'<'

Добавлено: 27 Июля 2018 21:00:27 Добавил: Андрей Ковальчук

JavaScript stream_get_contents function

A JavaScript equivalent of PHP’s stream_get_contents

function stream_get_contents (handle, maxLength, offset) {
  // http://kevin.vanzonneveld.net
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // *     example 1: var stream = fopen('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm', 'r');
  // *     example 1: stream_get_contents(stream, 7, 2);
  // *     returns 1: 'DOCTYPE'
  if (!this.php_js || !this.php_js.resourceData || !this.php_js.resourceDataPointer || !handle || !handle.id) {
    return false;
  }
  offset = offset || 0;
  this.php_js.resourceDataPointer[handle.id] += offset;

  var chrs = this.php_js.resourceData[handle.id].slice(this.php_js.resourceDataPointer[handle.id]);
  chrs = maxLength >= 0 ? chrs.substr(0, maxLength) : chrs;

  this.echo(chrs);
  this.php_js.resourceDataPointer[handle.id] += chrs.length;

  return chrs;
}

Example 1
var stream = fopen('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm', 'r');
stream_get_contents(stream, 7, 2);

Should return
'DOCTYPE'

Добавлено: 27 Июля 2018 20:58:22 Добавил: Андрей Ковальчук

JavaScript stream_context_set_params function

A JavaScript equivalent of PHP’s stream_context_set_params

function stream_context_set_params (stream_or_context, params) {
  // http://kevin.vanzonneveld.net
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // *     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;
}

Example 1
var context = stream_context_create();
stream_context_set_params({notification:function (notification_code, severity, message, message_code, bytes_transferred, bytes_max) {}});

Should return
true

Добавлено: 27 Июля 2018 20:35:01 Добавил: Андрей Ковальчук

JavaScript stream_context_set_option function

A JavaScript equivalent of PHP’s stream_context_set_option

function stream_context_set_option (stream_or_context, optionsOrWrapper, option, value) {
  // http://kevin.vanzonneveld.net
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // *     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;
}

Example 1
var opts = {http:{ method:'GET', header: 'Accept-language: en\r\nCookie: foo=bar\r\n' } };
var context = stream_context_create(opts);
stream_context_set_option(context, opts);

Should return
true

Добавлено: 27 Июля 2018 20:33:24 Добавил: Андрей Ковальчук

JavaScript stream_context_get_params function

A JavaScript equivalent of PHP’s stream_context_get_params

function stream_context_get_params (stream_or_context) {
  // http://kevin.vanzonneveld.net
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // *     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;
}

Example 1
var params = {notification:function (notification_code, severity, message, message_code, bytes_transferred, bytes_max) {}};
var context = stream_context_create({}, params);
stream_context_get_params(context);

Should return
{notification:function (notification_code, severity, message, message_code, bytes_transferred, bytes_max) {}, options:{}}

Добавлено: 27 Июля 2018 20:32:47 Добавил: Андрей Ковальчук

JavaScript stream_context_set_default function

A JavaScript equivalent of PHP’s stream_context_set_default

function stream_context_set_default (options) {
  // http://kevin.vanzonneveld.net
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // -    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
  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;
}

Example 1
var opts = {http:{ method:'GET', header: 'Accept-language: en\r\nCookie: foo=bar\r\n' } };
var context = stream_context_set_default(opts);
get_resource_type(context);

Should return
'stream-context'

Добавлено: 27 Июля 2018 20:32:03 Добавил: Андрей Ковальчук

JavaScript stream_context_get_options function

A JavaScript equivalent of PHP’s stream_context_get_options

function stream_context_get_options (stream_or_context) {
  // http://kevin.vanzonneveld.net
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // *     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;
}

Example 1
var opts = {http:{method:'GET', header: 'Accept-language: en\r\nCookie: foo=bar\r\n'}};
var context = stream_context_create(opts);
stream_context_get_options(context);

Should return
{http:{ method:'GET', header: 'Accept-language: en\r\nCookie: foo=bar\r\n' }}

Добавлено: 27 Июля 2018 20:31:23 Добавил: Андрей Ковальчук

JavaScript stream_context_get_default function

A JavaScript equivalent of PHP’s stream_context_get_default

function stream_context_get_default (options) {
  // http://kevin.vanzonneveld.net
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // -    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
  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;
}

Example 1
var context = stream_context_get_default();
get_resource_type(context);

Should return
'stream-context'

Добавлено: 27 Июля 2018 20:30:39 Добавил: Андрей Ковальчук

JavaScript stream_context_create function

A JavaScript equivalent of PHP’s stream_context_create

function stream_context_create (options, params) {
  // http://kevin.vanzonneveld.net
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // %          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
  this.php_js = this.php_js || {};
  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;
}

Example 1
var opts = {http:{ method:'GET', header: 'Accept-language: en\r\nCookie: foo=bar\r\n' } };
var context = stream_context_create(opts);
get_resource_type(context);

Should return
'stream-context'

Добавлено: 27 Июля 2018 20:29:49 Добавил: Андрей Ковальчук

JavaScript runkit_superglobals function

A JavaScript equivalent of PHP’s runkit_superglobals

function runkit_superglobals () {
  // http://kevin.vanzonneveld.net
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // %          note 1: You must manually define the superglobals to be able to use them.
  // %          note 2: Another alternative (though you can't reflect on them with this function) is to use import_request_variables()
  // *     example 1: runkit_superglobals();
  // *     returns 1: []
  var superglobal = {},
    p = '',
    arr = [];
  var superglobals = ['$_GET', '$_POST', '$_REQUEST', '$_COOKIE', '$_SESSION', '$_SERVER', '$_ENV', '$_FILES'];
  for (var i = 0; i < superglobals.length; i++) {
    superglobal = this.window[superglobals[i]];
    for (p in superglobal) {
      arr.push(superglobal[p]);
    }
  }
  return arr;
}

Example 1
runkit_superglobals();

Should return
[]

Добавлено: 27 Июля 2018 20:29:05 Добавил: Андрей Ковальчук

JavaScript runkit_method_rename function

A JavaScript equivalent of PHP’s runkit_method_rename

function runkit_method_rename (classname, methodname, newname) {
  // http://kevin.vanzonneveld.net
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // *     example 1: runkit_method_rename('someClass', 'someMethod', 'newMethod');
  // *     returns 1: true
  var getFuncName = function (fn) {
    var name = (/\W*function\s+([\w\$]+)\s*\(/).exec(fn);
    if (!name) {
      return '(Anonymous)';
    }
    return name[1];
  };

  if (typeof classname === 'string') {
    classname = this.window[classname];
  }

/*
  var method = classname[methodname]; // Static
  classname[newname] = method;
  delete classname[methodname];
  */

  if (getFuncName(classname) !== 'PHP_JS' || // By default, don't allow overriding of PHP functions
  (this.php_js && this.php_js.ini && this.php_js.ini['runkit.internal_override'] && (this.php_js.ini['runkit.internal_override'].local_value === true || this.php_js.ini['runkit.internal_override'].local_value === 1 || this.php_js.ini['runkit.internal_override'].local_value === '1' || this.php_js.ini['runkit.internal_override'].local_value === 'true'))) {
    var method = classname.prototype[methodname];
    classname.prototype[newname] = method;
    delete classname.prototype[methodname];
    return true;
  }
  return false;
}

Example 1
runkit_method_rename('someClass', 'someMethod', 'newMethod');

Should return
true

Добавлено: 27 Июля 2018 20:28:23 Добавил: Андрей Ковальчук

JavaScript runkit_method_remove function

A JavaScript equivalent of PHP’s runkit_method_remove

function runkit_method_remove (classname, methodname) {
  // http://kevin.vanzonneveld.net
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // *     example 1: runkit_method_remove('someClass', 'someMethod');
  // *     returns 1: true
  if (typeof classname === 'string') {
    classname = this.window[classname];
  }
  if (getFuncName(classname) !== 'PHP_JS' || // By default, don't allow overriding of PHP functions
  (this.php_js && this.php_js.ini && this.php_js.ini['runkit.internal_override'] && (this.php_js.ini['runkit.internal_override'].local_value === true || this.php_js.ini['runkit.internal_override'].local_value === 1 || this.php_js.ini['runkit.internal_override'].local_value === '1' || this.php_js.ini['runkit.internal_override'].local_value === 'true'))) {
    delete classname.prototype[methodname]; // Delete any on prototype
    return true;
  }
  // delete classname[methodname]; // Delete any as static class method
  return false;
}

Example 1
runkit_method_remove('someClass', 'someMethod');

Should return
true

Добавлено: 27 Июля 2018 20:26:45 Добавил: Андрей Ковальчук

JavaScript runkit_method_redefine function

A JavaScript equivalent of PHP’s runkit_method_redefine

function runkit_method_redefine (classname, methodname, args, code, flags) {
  // http://kevin.vanzonneveld.net
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // *     example 1: runkit_method_redefine('someClass', 'someMethod', 'a,b', 'return a+b');
  // *     returns 1: true
  // In JavaScript, this is identical to runkit_method_add
  var argmnts = [],
    func;
  var getFuncName = function (fn) {
    var name = (/\W*function\s+([\w\$]+)\s*\(/).exec(fn);
    if (!name) {
      return '(Anonymous)';
    }
    return name[1];
  };

  switch (flags) {
  case 'RUNKIT_ACC_PROTECTED':
    throw 'Protected not supported';
  case 'RUNKIT_ACC_PRIVATE':
    throw 'Private not supported';
  case 'RUNKIT_ACC_PUBLIC':
  default:
    break;
  }

  argmnts = args.split(/,\s*/);

  if (typeof classname === 'string') {
    classname = this.window[classname];
  }

  if (getFuncName(classname) !== 'PHP_JS' || // By default, don't allow overriding of PHP functions
  (this.php_js && this.php_js.ini && this.php_js.ini['runkit.internal_override'] && (this.php_js.ini['runkit.internal_override'].local_value === true || this.php_js.ini['runkit.internal_override'].local_value === 1 || this.php_js.ini['runkit.internal_override'].local_value === '1' || this.php_js.ini['runkit.internal_override'].local_value === 'true'))) {
    // Could use the following to add as a static method to the class
    //        func = Function.apply(null, argmnts.concat(code));
    //            classname[methodname] = func;
    func = Function.apply(null, argmnts.concat(code));
    classname.prototype[methodname] = func;
    return true;
  }
  return false;
}

Example 1
runkit_method_redefine('someClass', 'someMethod', 'a,b', 'return a+b');

Should return
true

Добавлено: 27 Июля 2018 20:26:06 Добавил: Андрей Ковальчук

JavaScript runkit_method_copy function

A JavaScript equivalent of PHP’s runkit_method_copy

function runkit_method_copy (dClass, dMethod, sClass, sMethod) {
  // http://kevin.vanzonneveld.net
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // *     example 1: runkit_method_copy('newClass', 'newMethod', 'someClass', 'someMethod');
  // *     returns 1: true
/*
  function A(){}
  function C(){}
  C.d = function () {alert('inside d');}
  runkit_method_copy('A', 'b', 'C', 'd');
  A.b(); // 'inside d'
  */
  sMethod = sMethod || dMethod;

  if (typeof dClass === 'string') {
    dClass = this.window[dClass];
  }
  if (typeof sClass === 'string') {
    sClass = this.window[sClass];
  }

  //dClass[dMethod] = sClass[sMethod]; // Copy from static to static method (as per PHP example)
  dClass.prototype[dMethod] = sClass.prototype[sMethod]; // To copy from instance to instance
  return true;
}

Example 1
runkit_method_copy('newClass', 'newMethod', 'someClass', 'someMethod');

Should return
true

Добавлено: 27 Июля 2018 20:25:18 Добавил: Андрей Ковальчук

JavaScript runkit_import function

A JavaScript equivalent of PHP’s runkit_import

function runkit_import (file, flags) {
  // http://kevin.vanzonneveld.net
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // -    depends on: file_get_contents
  // %        note 1: does not return an associative array as in PHP and will evaluate all variables, not only those in a function or class
  // %        note 2: Implement instead with include?
  // *     example 1: runkit_import('http://example.com/somefile.js');
  // *     returns 1: undefined
  if (flags) {
    // RUNKIT_IMPORT_FUNCTIONS, RUNKIT_IMPORT_CLASS_METHODS, RUNKIT_IMPORT_CLASS_CONSTS,
    // RUNKIT_IMPORT_CLASS_PROPS, RUNKIT_IMPORT_CLASSES, RUNKIT_IMPORT_OVERRIDE
    // CLASSKIT_AGGREGATE_OVERRIDE ?
    throw 'Flags not supported for runkit_import';
  }

  eval(this.file_get_contents(file));
}

Example 1
runkit_import('http://example.com/somefile.js');

Should return
undefined

Добавлено: 27 Июля 2018 20:24:40 Добавил: Андрей Ковальчук