Skip to content

Commit 64af96c

Browse files
committed
Merge branch 'master' of github.com:kvz/phpjs
2 parents bbd5921 + 362956f commit 64af96c

File tree

124 files changed

+5585
-2560
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+5585
-2560
lines changed

_experimental/array/list.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,30 @@ function list () {
44
// % note 1: Only works in global context and deviates (by necessity) from
55
// % note 1: PHP version by adding the array (which in PHP is an rvalue
66
// % note 1: separate from the list() lvalue) as the last argument
7+
// * example 1: var drink, color, power;
78
// * example 1: list('drink', 'color', 'power', ['coffee', 'brown', 'caffeine']);
9+
// * example 1: drink +' is '+color+' and '+power+' makes it special.\n';
810
// * returns 1: 'coffee is brown and caffeine makes it special.\n'
911

10-
var i=0, arr=[];
12+
var i = 0, arr = [];
1113

1214
arr = arguments[arguments.length-1];
13-
for (i = 0; i < arr.length; i++) {
14-
this[arguments[i]] = arr[i];
15+
16+
if (arr && typeof arr === 'object' && arr.change_key_case) { // Duck-type check for our own array()-created PHPJS_Array
17+
return arr.list.apply(arr, Array.prototype.slice.call(arguments, 0, -1));
18+
}
19+
if (arr && typeof arr === 'object' && arr.length && !arr.propertyIsEnumerable('length')) {
20+
for (i = 0; i < arr.length; i++) {
21+
this.window[arguments[i]] = arr[i];
22+
}
23+
}
24+
else {
25+
for (i in arr) {
26+
if (i.length === parseInt(i).toString().length && parseInt(i) < arguments.length-1) {
27+
this.window[arguments[i]] = arr[i];
28+
}
29+
}
1530
}
1631

32+
return arr;
1733
}

_experimental/language/$_COOKIE.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function $_COOKIE (name) {
2+
// http://kevin.vanzonneveld.net
3+
// + original by: http://www.quirksmode.org/js/cookies.html
4+
// + improved by: Steve
5+
// + improved by: Brett Zamir (http://brett-zamir.me)
6+
// * example 1: document.cookie = 'test=hello';
7+
// * example 1: var $myVar = $_COOKIE('test'); // Note the round brackets!
8+
// * returns 1: 'hello'
9+
10+
var i = 0, c = '', nameEQ = name + '=',
11+
ca = document.cookie.split(';'),
12+
cal = ca.length;
13+
for (i = 0; i < cal; i++) {
14+
c = ca[i].replace(/^ */, '');
15+
if (c.indexOf(nameEQ) === 0) {
16+
return decodeURIComponent(c.slice(nameEQ.length).replace(/\+/g, '%20'));
17+
}
18+
}
19+
return null;
20+
}

_experimental/language/$_GET.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function $_GET (name) {
2+
// http://kevin.vanzonneveld.net
3+
// + original by: Brett Zamir (http://brett-zamir.me)
4+
// % note 1: See import_request_variables() for building a $_GET object which allows the PHP-like bracket access.
5+
// * example 1: document.cookie = 'test=hello';
6+
// * example 1: var $myVar = $_COOKIE('test'); // Note the round brackets!
7+
// * returns 1: 'hello'
8+
9+
var nameEQ = name + '=',
10+
url = window.location.href,
11+
pos = url.indexOf('?'),
12+
url = url.slice(pos + 1),
13+
arr = url.split('&'),
14+
i = 0,
15+
pair = '',
16+
arrl = arr.length;
17+
for (i = 0; i < arrl; i++) {
18+
var pair = arr[i];
19+
if (pair.indexOf(nameEQ) === 0) {
20+
return decodeURIComponent(pair.slice(nameEQ.length).replace(/\+/g, '%20'));
21+
}
22+
}
23+
return null;
24+
}

_experimental/language/foreach.js

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,40 @@ function foreach (arr, handler) {
55
// * returns 1: undefined
66
// * example 2: foreach (['a', 'b'], function (key, val) {alert(key+'::'+val);});
77
// * returns 2: undefined
8-
var k;
9-
if (handler.length === 1) {
10-
for (k in arr) {
11-
handler(arr[k]); // only pass the value
12-
}
13-
}
14-
else {
15-
for (k in arr) {
16-
handler(k, arr[k]);
17-
}
18-
}
8+
// * example 1: foreach ({key: 'value'}, function (val) {alert(val);});
9+
// * returns 1: undefined
10+
// * example 2: foreach ({key: 'value'}, function (key, val) {alert(key+'::'+val);});
11+
// * returns 2: undefined
12+
13+
var k, it, pair;
14+
if (arr && typeof arr === 'object' && arr.change_key_case) { // Duck-type check for our own array()-created PHPJS_Array
15+
return arr.foreach(handler);
16+
}
17+
if (typeof this.Iterator !== 'undefined') {
18+
var it = this.Iterator(arr); // Does not add prototype items
19+
if (handler.length === 1) {
20+
for (pair in it) {
21+
handler(pair[1]); // only pass the value
22+
}
23+
}
24+
else {
25+
for (pair in it) {
26+
handler(pair[0], pair[1]);
27+
}
28+
}
29+
}
30+
else if (handler.length === 1) {
31+
for (k in arr) {
32+
if (arr.hasOwnProperty(k)) {
33+
handler(arr[k]); // only pass the value
34+
}
35+
}
36+
}
37+
else {
38+
for (k in arr) {
39+
if (arr.hasOwnProperty(k)) {
40+
handler(k, arr[k]);
41+
}
42+
}
43+
}
1944
}

_experimental/ldap/ldap_bind.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function ldap_bind (link_id, bind_rdn, bind_pass, successCb, errorCb) {
2+
// http://kevin.vanzonneveld.net
3+
// + original by: Brett Zamir (http://brett-zamir.me)
4+
// % note 1: For CommonJS/Node (SSJS) only: Meant to work with https://github.com/joewalnes/node-ldapauth or https://github.com/jeremycx/node-LDAP, but not yet finalized
5+
// % note 2: We break somewhat with the PHP API by necessity in order to allow
6+
// % note 2: asynchronous callbacks, having the last two arguments being a
7+
// % note 2: successful callback and an error callback respectively, with the
8+
// % note 2: latter being passed the error and result and the former being
9+
// % note 2: passed the result only.
10+
// * example 1: var ldapconn = ldap_connect ('localhost');
11+
// * example 1: var ldapbind = ldap_bind(ldapconn, 'brettz', 'dontyouwish', function success (result) {res.redirect('/res_need_login');}, function error (err, result) {res.send("auth error");});
12+
// * returns 1: true
13+
14+
bind_rdn= bind_rdn || null;
15+
bind_pass = bind_pass || null;
16+
if (typeof link_id !== 'object' || !link_id.get_resource_type) { // Duck type for PHPJS_Resource
17+
return false;
18+
}
19+
link_id.ldapauth.authenticate(link_id.hostname, link_id.port, bind_rdn, bind_pass,
20+
function (err, result) {
21+
if (err) {
22+
errorCb(err, result);
23+
return;
24+
}
25+
successCb(result);
26+
}
27+
);
28+
return true;
29+
}

_experimental/ldap/ldap_connect.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
function ldap_connect (hostname, port) {
2+
// http://kevin.vanzonneveld.net
3+
// + original by: Brett Zamir (http://brett-zamir.me)
4+
// % note 1: For CommonJS/Node (SSJS) only: Meant to work with https://github.com/joewalnes/node-ldapauth or https://github.com/jeremycx/node-LDAP, but not yet finalized
5+
// % note 2: Creates an LDAP link (PHPJS_Resource)
6+
// * example 1: var ldapconn = ldap_connect ('localhost');
7+
// * returns 1: 'Resource id #1'
8+
9+
var resource;
10+
11+
hostname = hostname || null;
12+
var portPos = hostname && hostname.indexOf(':');
13+
port = port || (portPos > 0 ? hostname.slice(portPos+1) : 389);
14+
15+
// BEGIN REDUNDANT
16+
this.php_js = this.php_js || {};
17+
this.php_js.resourceIdCounter = this.php_js.resourceIdCounter || 0;
18+
// END REDUNDANT
19+
20+
// The following class represents a PHP resource type, which LDAP in PHP requires.
21+
22+
// BEGIN STATIC
23+
function PHPJS_Resource(type, id, opener) { // Can reuse the following for other resources, just changing the instantiation
24+
// See http://php.net/manual/en/resource.php for types
25+
this.type = type;
26+
this.id = id;
27+
this.opener = opener;
28+
}
29+
PHPJS_Resource.prototype.toString = function () {
30+
return 'Resource id #' + this.id;
31+
};
32+
PHPJS_Resource.prototype.get_resource_type = function () {
33+
return this.type;
34+
};
35+
PHPJS_Resource.prototype.var_dump = function () {
36+
return 'resource(' + this.id + ') of type (' + this.type + ')';
37+
};
38+
// END STATIC
39+
40+
this.php_js.resourceIdCounter++;
41+
resource = new PHPJS_Resource('ldap link', this.php_js.resourceIdCounter, 'ldap_connect');
42+
43+
// Attaching LDAP-specific properties
44+
resource.hostname = hostname;
45+
resource.port = port;
46+
resource.ldapauth = require("ldapauth");
47+
48+
return resource;
49+
}

_workbench/datetime/DateInterval.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
function DateInterval (interval_spec) { // string
2+
// http://kevin.vanzonneveld.net
3+
// + original by: Brett Zamir (http://brett-zamir.me)
4+
// - depends on: date_interval_create_from_date_string
5+
// * example 1: var di = new DateInterval('P2Y4DT6H8M');
6+
// * example 1: di.d === 4;
7+
// * returns 1: true
8+
9+
var that = this,
10+
matches, weeks = false,
11+
dec = '(?:(\\d+(?:[.,]\\d*)?)', // Must decimal be followed by number?
12+
_pad = function (n, c) {
13+
if ((n = n + '').length < c ) {
14+
return new Array(++c - n.length).join('0') + n;
15+
}
16+
return n;
17+
};
18+
if (!this.DateInterval.prototype.format) { // We need to place here for compilation reasons
19+
var DateInterval = this.DateInterval;
20+
DateInterval.prototype = {
21+
constructor: DateInterval,
22+
format : function (format) {
23+
return format.replace(/%([%YyMmDdaHhIiSsRr])/, function (n0, n1) {
24+
switch (n1) {
25+
case '%':
26+
return '%';
27+
case 'Y': case 'M': case 'D': case 'H': case 'I': case 'S':
28+
var l = n1.toLowerCase();
29+
return _pad(that[l], 2);
30+
case 'y': case 'm': case 'd': case 'h': case 'i': case 's':
31+
return that[n1];
32+
case 'a':
33+
return that.days;
34+
case 'R':
35+
return that.invert ? '-' : '+';
36+
case 'r':
37+
return that.invert ? '-' : '';
38+
default:
39+
throw 'Unexpected character in DateInterval.format replace';
40+
}
41+
});
42+
}
43+
};
44+
DateInterval.createFromDateString = function (time) { // string (date string with relative parts)
45+
return that.date_interval_create_from_date_string(time);
46+
};
47+
}
48+
49+
try {
50+
if ((matches = interval_spec.match(/^P(\d+)W$/))) {
51+
this.d = 7 * matches[1];
52+
weeks = true;
53+
}
54+
else if ((matches = interval_spec.match(new RegExp(
55+
'^P' + dec + 'Y)?' + dec + 'M)?' + dec + 'D)?(?:T' + dec + 'H)?' + dec + 'M)?' + dec + 'S)?)?$'
56+
)))) {
57+
var mj = matches.join('');
58+
if (
59+
mj.match(/[.,]\d+../) || // Decimal used in non-lowest position
60+
mj.replace(/[.,]/g, '').length < 3 // Only P and/or T
61+
) {
62+
throw 'Handle-below';
63+
}
64+
}
65+
else if (!(matches = interval_spec.match(
66+
/^P(\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1])T([0-1]\d|2[0-3]):([0-5]\d):([0-5]\d)$/
67+
))) {
68+
throw 'Handle-below';
69+
}
70+
}
71+
catch (e) {
72+
throw 'Malformed DateInterval'; // Throw exception from single place
73+
}
74+
if (!weeks) {
75+
this.y = matches[1] || 0;
76+
this.m = matches[2] || 0;
77+
this.d = matches[3] || 0;
78+
this.h = matches[4] || 0;
79+
this.i = matches[5] || 0;
80+
this.s = matches[6] || 0;
81+
}
82+
this.invert = 0; // or 1; Fix: Must be changed directly to work?
83+
this.days = this.d || false; // Fix: When will it not be determinable (to be false); how to determine conversion from months?
84+
}

_workbench/datetime/DatePeriod.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function DatePeriod (start, interval, recurrences, options) { // DateTime, DateInterval, int, int
2+
// http://kevin.vanzonneveld.net
3+
// + original by: Brett Zamir (http://brett-zamir.me)
4+
// * example 1: new DatePeriod();
5+
// * returns 1: {}
6+
7+
// Finish example above
8+
9+
// Is iteratable with foreach in PHP; this class is just put here for convenience,
10+
// as there is no PHP function that returns it; it is just placed here as a related class,
11+
// see http://en.wikipedia.org/wiki/ISO_8601#Durations
12+
13+
if (!this.DatePeriod.EXCLUDE_START_DATE) { // We need to place here for compilation reasons
14+
var DatePeriod = this.DatePeriod;
15+
// See https://developer.mozilla.org/en/New_in_JavaScript_1.7#Iterators
16+
DatePeriod.prototype.__iterator__ = function () { // Mozilla/Chrome/Safari only; will allow DatePeriod to be iterated with for...in or for each
17+
return Iterator(this.data, false); // Allow both key and value to be passed back? (otherwise, 2nd arg is true);
18+
// need destructuring to get individually; might also use our implementation of foreach to do without this
19+
// Mozilla-only approach, but it will require use of (and adaptation of) our foreach()
20+
};
21+
DatePeriod.EXCLUDE_START_DATE = 1;
22+
}
23+
24+
// or start, interval, end, options
25+
// or isostr, options
26+
if (options) { // the only optional part in all 3 constructor forms
27+
}
28+
}

0 commit comments

Comments
 (0)