/modules/phpjs.js
JavaScript | 2038 lines | 1755 code | 43 blank | 240 comment | 73 complexity | 059127a9be1c7e3f69eb917327c1f9af MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- /** @ignore */
- /**
- * @fileoverview
- *
- * <p>PHPJS - implementations of PHP core functions in JavaScript.</p>
- */
- function array_chunk (input, size, preserve_keys) {
- // http://kevin.vanzonneveld.net
- // + original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com)
- // + improved by: Brett Zamir (http://brett-zamir.me)
- // % note 1: Important note: Per the ECMAScript specification, objects may not always iterate in a predictable order
- // * example 1: array_chunk(['Kevin', 'van', 'Zonneveld'], 2);
- // * returns 1: [['Kevin', 'van'], ['Zonneveld']]
- // * example 2: array_chunk(['Kevin', 'van', 'Zonneveld'], 2, true);
- // * returns 2: [{0:'Kevin', 1:'van'}, {2: 'Zonneveld'}]
- // * example 3: array_chunk({1:'Kevin', 2:'van', 3:'Zonneveld'}, 2);
- // * returns 3: [['Kevin', 'van'], ['Zonneveld']]
- // * example 4: array_chunk({1:'Kevin', 2:'van', 3:'Zonneveld'}, 2, true);
- // * returns 4: [{1: 'Kevin', 2: 'van'}, {3: 'Zonneveld'}]
- var x, p = '', i = 0, c = -1, l = input.length || 0, n = [];
- if (size < 1) {
- return null;
- }
- if (Object.prototype.toString.call(input) === '[object Array]') {
- if (preserve_keys) {
- while (i < l) {
- (x = i % size) ? n[c][i] = input[i] : n[++c] = {}, n[c][i] = input[i];
- i++;
- }
- }
- else {
- while (i < l) {
- (x = i % size) ? n[c][x] = input[i] : n[++c] = [input[i]];
- i++;
- }
- }
- }
- else {
- if (preserve_keys) {
- for (p in input) {
- if (input.hasOwnProperty(p)) {
- (x = i % size) ? n[c][p] = input[p] : n[++c] = {}, n[c][p] = input[p];
- i++;
- }
- }
- }
- else {
- for (p in input) {
- if (input.hasOwnProperty(p)) {
- (x = i % size) ? n[c][x] = input[p] : n[++c] = [input[p]];
- i++;
- }
- }
- }
- }
- return n;
- }
- function array_intersect () {
- // Returns the entries of arr1 that have values which are present in all the other arguments
- //
- // version: 1006.1915
- // discuss at: http://phpjs.org/functions/array_intersect
- // + original by: Brett Zamir (http://brett-zamir.me)
- // % note 1: These only output associative arrays (would need to be
- // % note 1: all numeric and counting from zero to be numeric)
- // * example 1: $array1 = {'a' : 'green', 0:'red', 1: 'blue'};
- // * example 1: $array2 = {'b' : 'green', 0:'yellow', 1:'red'};
- // * example 1: $array3 = ['green', 'red'];
- // * example 1: $result = array_intersect($array1, $array2, $array3);
- // * returns 1: {0: 'red', a: 'green'}
- var arr1 = arguments[0], retArr = {};
- var k1 = '', arr = {}, i = 0, k = '';
-
- arr1keys:
- for (k1 in arr1) {
- arrs:
- for (i=1; i < arguments.length; i++) {
- arr = arguments[i];
- for (k in arr) {
- if (arr[k] === arr1[k1]) {
- if (i === arguments.length-1) {
- retArr[k1] = arr1[k1];
- }
- // If the innermost loop always leads at least once to an equal value, continue the loop until done
- continue arrs;
- }
- }
- // If it reaches here, it wasn't found in at least one array, so try next value
- continue arr1keys;
- }
- }
-
- return retArr;
- }
- function array_key_exists ( key, search ) {
- // Checks if the given key or index exists in the array
- //
- // version: 1004.2314
- // discuss at: http://phpjs.org/functions/array_key_exists
- // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + improved by: Felix Geisendoerfer (http://www.debuggable.com/felix)
- // * example 1: array_key_exists('kevin', {'kevin': 'van Zonneveld'});
- // * returns 1: true
- // input sanitation
- if (!search || (search.constructor !== Array && search.constructor !== Object)){
- return false;
- }
-
- return key in search;
- }
- function array_keys (input, search_value, argStrict) {
- // http://kevin.vanzonneveld.net
- // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + input by: Brett Zamir (http://brett-zamir.me)
- // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // * example 1: array_keys( {firstname: 'Kevin', surname: 'van Zonneveld'} );
- // * returns 1: {0: 'firstname', 1: 'surname'}
-
- var tmp_arr = {}, strict = !!argStrict, include = true, cnt = 0;
- var key = '';
-
- for (key in input) {
- include = true;
- if (search_value != undefined) {
- if (strict && input[key] !== search_value){
- include = false;
- } else if (input[key] != search_value){
- include = false;
- }
- }
-
- if (include) {
- tmp_arr[cnt] = key;
- cnt++;
- }
- }
-
- return tmp_arr;
- }
- function array_unique (inputArr) {
- // http://kevin.vanzonneveld.net
- // + original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com)
- // + input by: duncan
- // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + bugfixed by: Nate
- // + input by: Brett Zamir (http://brett-zamir.me)
- // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + improved by: Michael Grier
- // + bugfixed by: Brett Zamir (http://brett-zamir.me)
- // % note 1: The second argument, sort_flags is not implemented;
- // % note 1: also should be sorted (asort?) first according to docs
- // * example 1: array_unique(['Kevin','Kevin','van','Zonneveld','Kevin']);
- // * returns 1: {0: 'Kevin', 2: 'van', 3: 'Zonneveld'}
- // * example 2: array_unique({'a': 'green', 0: 'red', 'b': 'green', 1: 'blue', 2: 'red'});
- // * returns 2: {a: 'green', 0: 'red', 1: 'blue'}
- var key = '', tmp_arr2 = {}, val = '';
- var __array_search = function (needle, haystack) {
- var fkey = '';
- for (fkey in haystack) {
- if (haystack.hasOwnProperty(fkey)) {
- if ((haystack[fkey] + '') === (needle + '')) {
- return fkey;
- }
- }
- }
- return false;
- };
- for (key in inputArr) {
- if (inputArr.hasOwnProperty(key)) {
- val = inputArr[key];
- if (false === __array_search(val, tmp_arr2)) {
- tmp_arr2[key] = val;
- }
- }
- }
- return tmp_arr2;
- }
- function basename (path, suffix) {
- // Returns the filename component of the path
- //
- // version: 1004.2314
- // discuss at: http://phpjs.org/functions/basename
- // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + improved by: Ash Searle (http://hexmen.com/blog/)
- // + improved by: Lincoln Ramsay
- // + improved by: djmix
- // * example 1: basename('/www/site/home.htm', '.htm');
- // * returns 1: 'home'
- // * example 2: basename('ecra.php?p=1');
- // * returns 2: 'ecra.php?p=1'
- var b = path.replace(/^.*[\/\\]/g, '');
-
- if (typeof(suffix) == 'string' && b.substr(b.length-suffix.length) == suffix) {
- b = b.substr(0, b.length-suffix.length);
- }
-
- return b;
- }
- /**
- * count(o)
- *
- * Simulates PHP's count() function
- *
- * @param o {mixed} object, array, string, etc.
- * @returns {int} size
- *
- * size is number of members of an object, length of a string, length of an array, or 1 for others
- */
- function count(o) {
- if (Util.isArray(o) || Util.isString(o)) {
- return o.length;
- }
- else if (Util.isObject(o)) {
- var n = 0;
- forEach(o, function(oo, key) {
- if (o.hasOwnProperty(key)) {
- n++;
- }
- });
- return n;
- }
- else {
- return 1;
- }
- }
- function date (format, timestamp) {
- // http://kevin.vanzonneveld.net
- // + original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com)
- // + parts by: Peter-Paul Koch (http://www.quirksmode.org/js/beat.html)
- // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + improved by: MeEtc (http://yass.meetcweb.com)
- // + improved by: Brad Touesnard
- // + improved by: Tim Wiel
- // + improved by: Bryan Elliott
- //
- // + improved by: Brett Zamir (http://brett-zamir.me)
- // + improved by: David Randall
- // + input by: Brett Zamir (http://brett-zamir.me)
- // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + improved by: Brett Zamir (http://brett-zamir.me)
- // + improved by: Brett Zamir (http://brett-zamir.me)
- // + improved by: Theriault
- // + derived from: gettimeofday
- // + input by: majak
- // + bugfixed by: majak
- // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + input by: Alex
- // + bugfixed by: Brett Zamir (http://brett-zamir.me)
- // + improved by: Theriault
- // + improved by: Brett Zamir (http://brett-zamir.me)
- // + improved by: Theriault
- // + improved by: Thomas Beaucourt (http://www.webapp.fr)
- // + improved by: JT
- // + improved by: Theriault
- // + improved by: RafaĹ Kukawski (http://blog.kukawski.pl)
- // + bugfixed by: omid (http://phpjs.org/functions/380:380#comment_137122)
- // + input by: Martin
- // + input by: Alex Wilson
- // % note 1: Uses global: php_js to store the default timezone
- // % note 2: Although the function potentially allows timezone info (see notes), it currently does not set
- // % note 2: per a timezone specified by date_default_timezone_set(). Implementers might use
- // % note 2: this.php_js.currentTimezoneOffset and this.php_js.currentTimezoneDST set by that function
- // % note 2: in order to adjust the dates in this function (or our other date functions!) accordingly
- // * example 1: date('H:m:s \\m \\i\\s \\m\\o\\n\\t\\h', 1062402400);
- // * returns 1: '09:09:40 m is month'
- // * example 2: date('F j, Y, g:i a', 1062462400);
- // * returns 2: 'September 2, 2003, 2:26 am'
- // * example 3: date('Y W o', 1062462400);
- // * returns 3: '2003 36 2003'
- // * example 4: x = date('Y m d', (new Date()).getTime()/1000);
- // * example 4: (x+'').length == 10 // 2009 01 09
- // * returns 4: true
- // * example 5: date('W', 1104534000);
- // * returns 5: '53'
- // * example 6: date('B t', 1104534000);
- // * returns 6: '999 31'
- // * example 7: date('W U', 1293750000.82); // 2010-12-31
- // * returns 7: '52 1293750000'
- // * example 8: date('W', 1293836400); // 2011-01-01
- // * returns 8: '52'
- // * example 9: date('W Y-m-d', 1293974054); // 2011-01-02
- // * returns 9: '52 2011-01-02'
- var that = this,
- jsdate, f, formatChr = /\\?([a-z])/gi,
- formatChrCb,
- // Keep this here (works, but for code commented-out
- // below for file size reasons)
- //, tal= [],
- _pad = function (n, c) {
- if ((n = n + '').length < c) {
- return new Array((++c) - n.length).join('0') + n;
- }
- return n;
- },
- txt_words = ["Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
- formatChrCb = function (t, s) {
- return f[t] ? f[t]() : s;
- };
- f = {
- // Day
- d: function () { // Day of month w/leading 0; 01..31
- return _pad(f.j(), 2);
- },
- D: function () { // Shorthand day name; Mon...Sun
- return f.l().slice(0, 3);
- },
- j: function () { // Day of month; 1..31
- return jsdate.getDate();
- },
- l: function () { // Full day name; Monday...Sunday
- return txt_words[f.w()] + 'day';
- },
- N: function () { // ISO-8601 day of week; 1[Mon]..7[Sun]
- return f.w() || 7;
- },
- S: function () { // Ordinal suffix for day of month; st, nd, rd, th
- var j = f.j();
- return j < 4 | j > 20 && ['st', 'nd', 'rd'][j%10 - 1] || 'th';
- },
- w: function () { // Day of week; 0[Sun]..6[Sat]
- return jsdate.getDay();
- },
- z: function () { // Day of year; 0..365
- var a = new Date(f.Y(), f.n() - 1, f.j()),
- b = new Date(f.Y(), 0, 1);
- return Math.round((a - b) / 864e5) + 1;
- },
- // Week
- W: function () { // ISO-8601 week number
- var a = new Date(f.Y(), f.n() - 1, f.j() - f.N() + 3),
- b = new Date(a.getFullYear(), 0, 4);
- return _pad(1 + Math.round((a - b) / 864e5 / 7), 2);
- },
- // Month
- F: function () { // Full month name; January...December
- return txt_words[6 + f.n()];
- },
- m: function () { // Month w/leading 0; 01...12
- return _pad(f.n(), 2);
- },
- M: function () { // Shorthand month name; Jan...Dec
- return f.F().slice(0, 3);
- },
- n: function () { // Month; 1...12
- return jsdate.getMonth() + 1;
- },
- t: function () { // Days in month; 28...31
- return (new Date(f.Y(), f.n(), 0)).getDate();
- },
- // Year
- L: function () { // Is leap year?; 0 or 1
- var j = f.Y();
- return j%4==0 & j%100!=0 | j%400==0;
- },
- o: function () { // ISO-8601 year
- var n = f.n(),
- W = f.W(),
- Y = f.Y();
- return Y + (n === 12 && W < 9 ? -1 : n === 1 && W > 9);
- },
- Y: function () { // Full year; e.g. 1980...2010
- return jsdate.getFullYear();
- },
- y: function () { // Last two digits of year; 00...99
- return (f.Y() + "").slice(-2);
- },
- // Time
- a: function () { // am or pm
- return jsdate.getHours() > 11 ? "pm" : "am";
- },
- A: function () { // AM or PM
- return f.a().toUpperCase();
- },
- B: function () { // Swatch Internet time; 000..999
- var H = jsdate.getUTCHours() * 36e2,
- // Hours
- i = jsdate.getUTCMinutes() * 60,
- // Minutes
- s = jsdate.getUTCSeconds(); // Seconds
- return _pad(Math.floor((H + i + s + 36e2) / 86.4) % 1e3, 3);
- },
- g: function () { // 12-Hours; 1..12
- return f.G() % 12 || 12;
- },
- G: function () { // 24-Hours; 0..23
- return jsdate.getHours();
- },
- h: function () { // 12-Hours w/leading 0; 01..12
- return _pad(f.g(), 2);
- },
- H: function () { // 24-Hours w/leading 0; 00..23
- return _pad(f.G(), 2);
- },
- i: function () { // Minutes w/leading 0; 00..59
- return _pad(jsdate.getMinutes(), 2);
- },
- s: function () { // Seconds w/leading 0; 00..59
- return _pad(jsdate.getSeconds(), 2);
- },
- u: function () { // Microseconds; 000000-999000
- return _pad(jsdate.getMilliseconds() * 1000, 6);
- },
- // Timezone
- e: function () { // Timezone identifier; e.g. Atlantic/Azores, ...
- // The following works, but requires inclusion of the very large
- // timezone_abbreviations_list() function.
- /* return this.date_default_timezone_get();
- */
- throw 'Not supported (see source code of date() for timezone on how to add support)';
- },
- I: function () { // DST observed?; 0 or 1
- // Compares Jan 1 minus Jan 1 UTC to Jul 1 minus Jul 1 UTC.
- // If they are not equal, then DST is observed.
- var a = new Date(f.Y(), 0),
- // Jan 1
- c = Date.UTC(f.Y(), 0),
- // Jan 1 UTC
- b = new Date(f.Y(), 6),
- // Jul 1
- d = Date.UTC(f.Y(), 6); // Jul 1 UTC
- return 0 + ((a - c) !== (b - d));
- },
- O: function () { // Difference to GMT in hour format; e.g. +0200
- var tzo = jsdate.getTimezoneOffset(),
- a = Math.abs(tzo);
- return (tzo > 0 ? "-" : "+") + _pad(Math.floor(a / 60) * 100 + a % 60, 4);
- },
- P: function () { // Difference to GMT w/colon; e.g. +02:00
- var O = f.O();
- return (O.substr(0, 3) + ":" + O.substr(3, 2));
- },
- T: function () { // Timezone abbreviation; e.g. EST, MDT, ...
- // The following works, but requires inclusion of the very
- // large timezone_abbreviations_list() function.
- /* var abbr = '', i = 0, os = 0, default = 0;
- if (!tal.length) {
- tal = that.timezone_abbreviations_list();
- }
- if (that.php_js && that.php_js.default_timezone) {
- default = that.php_js.default_timezone;
- for (abbr in tal) {
- for (i=0; i < tal[abbr].length; i++) {
- if (tal[abbr][i].timezone_id === default) {
- return abbr.toUpperCase();
- }
- }
- }
- }
- for (abbr in tal) {
- for (i = 0; i < tal[abbr].length; i++) {
- os = -jsdate.getTimezoneOffset() * 60;
- if (tal[abbr][i].offset === os) {
- return abbr.toUpperCase();
- }
- }
- }
- */
- return 'UTC';
- },
- Z: function () { // Timezone offset in seconds (-43200...50400)
- return -jsdate.getTimezoneOffset() * 60;
- },
- // Full Date/Time
- c: function () { // ISO-8601 date.
- return 'Y-m-d\\TH:i:sP'.replace(formatChr, formatChrCb);
- },
- r: function () { // RFC 2822
- return 'D, d M Y H:i:s O'.replace(formatChr, formatChrCb);
- },
- U: function () { // Seconds since UNIX epoch
- return jsdate / 1000 | 0;
- }
- };
- this.date = function (format, timestamp) {
- that = this;
- jsdate = (timestamp == null ? new Date() : // Not provided
- (timestamp instanceof Date) ? new Date(timestamp) : // JS Date()
- new Date(timestamp * 1000) // UNIX timestamp (auto-convert to int)
- );
- return format.replace(formatChr, formatChrCb);
- };
- return this.date(format, timestamp);
- }
- function dirname (path) {
- // Returns the directory name component of the path
- //
- // version: 1004.2314
- // discuss at: http://phpjs.org/functions/dirname
- // + original by: Ozh
- // + improved by: XoraX (http://www.xorax.info)
- // * example 1: dirname('/etc/passwd');
- // * returns 1: '/etc'
- // * example 2: dirname('c:/Temp/x');
- // * returns 2: 'c:/Temp'
- // * example 3: dirname('/dir/test/');
- // * returns 3: '/dir'
-
- return path.replace(/\\/g,'/').replace(/\/[^\/]*\/?$/, '');
- }
- function echo() {
- var arg = '', argc = arguments.length, argv = arguments, i = 0;
- for (i=0; i<argc; i++) {
- arg = argv[i];
- res.write(arg);
- }
- }
- function empty (mixed_var) {
- // !No description available for empty. @php.js developers: Please update the function summary text file.
- //
- // version: 1004.2314
- // discuss at: http://phpjs.org/functions/empty
- // + original by: Philippe Baumann
- // + input by: Onno Marsman
- // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + input by: LH
- // + improved by: Onno Marsman
- // + improved by: Francesco
- // + improved by: Marc Jansen
- // + input by: Stoyan Kyosev (http://www.svest.org/)
- // * example 1: empty(null);
- // * returns 1: true
- // * example 2: empty(undefined);
- // * returns 2: true
- // * example 3: empty([]);
- // * returns 3: true
- // * example 4: empty({});
- // * returns 4: true
- // * example 5: empty({'aFunc' : function () { alert('humpty'); } });
- // * returns 5: false
-
- var key;
-
- if (mixed_var === "" ||
- mixed_var === 0 ||
- mixed_var === "0" ||
- mixed_var === null ||
- mixed_var === false ||
- typeof mixed_var === 'undefined'
- ){
- return true;
- }
-
- if (typeof mixed_var == 'object') {
- for (key in mixed_var) {
- if (mixed_var.hasOwnProperty(key)) {
- return false;
- }
- }
- return true;
- }
-
- return false;
- }
- function file_exists(path) {
- var fs = require('fs');
- return fs.exists(path);
- }
- function htmlspecialchars (string, quote_style, charset, double_encode) {
- // http://kevin.vanzonneveld.net
- // + original by: Mirek Slugen
- // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + bugfixed by: Nathan
- // + bugfixed by: Arno
- // + revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + bugfixed by: Brett Zamir (http://brett-zamir.me)
- // + input by: Ratheous
- // + input by: Mailfaker (http://www.weedem.fr/)
- // + reimplemented by: Brett Zamir (http://brett-zamir.me)
- // + input by: felix
- // + bugfixed by: Brett Zamir (http://brett-zamir.me)
- // % note 1: charset argument not supported
- // * example 1: htmlspecialchars("<a href='test'>Test</a>", 'ENT_QUOTES');
- // * returns 1: '<a href='test'>Test</a>'
- // * example 2: htmlspecialchars("ab\"c'd", ['ENT_NOQUOTES', 'ENT_QUOTES']);
- // * returns 2: 'ab"c'd'
- // * example 3: htmlspecialchars("my "&entity;" is still here", null, null, false);
- // * returns 3: 'my "&entity;" is still here'
- var optTemp = 0,
- i = 0,
- noquotes = false;
- if (typeof quote_style === 'undefined' || quote_style === null) {
- quote_style = 2;
- }
- string = string.toString();
- if (double_encode !== false) { // Put this first to avoid double-encoding
- string = string.replace(/&/g, '&');
- }
- string = string.replace(/</g, '<').replace(/>/g, '>');
- var OPTS = {
- 'ENT_NOQUOTES': 0,
- 'ENT_HTML_QUOTE_SINGLE': 1,
- 'ENT_HTML_QUOTE_DOUBLE': 2,
- 'ENT_COMPAT': 2,
- 'ENT_QUOTES': 3,
- 'ENT_IGNORE': 4
- };
- if (quote_style === 0) {
- noquotes = true;
- }
- if (typeof quote_style !== 'number') { // Allow for a single string or an array of string flags
- quote_style = [].concat(quote_style);
- for (i = 0; i < quote_style.length; i++) {
- // Resolve string input to bitwise e.g. 'ENT_IGNORE' becomes 4
- if (OPTS[quote_style[i]] === 0) {
- noquotes = true;
- }
- else if (OPTS[quote_style[i]]) {
- optTemp = optTemp | OPTS[quote_style[i]];
- }
- }
- quote_style = optTemp;
- }
- if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) {
- string = string.replace(/'/g, ''');
- }
- if (!noquotes) {
- string = string.replace(/"/g, '"');
- }
- return string;
- }
- function is_null (mixed_var) {
- // Returns true if variable is null
- //
- // version: 1004.2314
- // discuss at: http://phpjs.org/functions/is_null
- // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // * example 1: is_null('23');
- // * returns 1: false
- // * example 2: is_null(null);
- // * returns 2: true
- return ( mixed_var === null );
- }
- function is_numeric (mixed_var) {
- // Returns true if value is a number or a numeric string
- //
- // version: 1004.2314
- // discuss at: http://phpjs.org/functions/is_numeric
- // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + improved by: David
- // + improved by: taith
- // + bugfixed by: Tim de Koning
- // + bugfixed by: WebDevHobo (http://webdevhobo.blogspot.com/)
- // + bugfixed by: Brett Zamir (http://brett-zamir.me)
- // * example 1: is_numeric(186.31);
- // * returns 1: true
- // * example 2: is_numeric('Kevin van Zonneveld');
- // * returns 2: false
- // * example 3: is_numeric('+186.31e2');
- // * returns 3: true
- // * example 4: is_numeric('');
- // * returns 4: false
- // * example 4: is_numeric([]);
- // * returns 4: false
- return (typeof(mixed_var) === 'number' || typeof(mixed_var) === 'string') && mixed_var !== '' && !isNaN(mixed_var);
- }
- function isset () {
- // !No description available for isset. @php.js developers: Please update the function summary text file.
- //
- // version: 1004.2314
- // discuss at: http://phpjs.org/functions/isset
- // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + improved by: FremyCompany
- // + improved by: Onno Marsman
- // * example 1: isset( undefined, true);
- // * returns 1: false
- // * example 2: isset( 'Kevin van Zonneveld' );
- // * returns 2: true
-
- var a=arguments, l=a.length, i=0;
-
- if (l===0) {
- error('Empty isset');
- }
-
- while (i!==l) {
- if (typeof(a[i])=='undefined' || a[i]===null) {
- return false;
- } else {
- i++;
- }
- }
- return true;
- }
- function lcfirst (str) {
- // http://kevin.vanzonneveld.net
- // + original by: Brett Zamir (http://brett-zamir.me)
- // * example 1: lcfirst('Kevin Van Zonneveld');
- // * returns 1: 'kevin Van Zonneveld'
- str += '';
- var f = str.charAt(0).toLowerCase();
- return f + str.substr(1);
- }
- function ltrim ( str, charlist ) {
- // http://kevin.vanzonneveld.net
- // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + input by: Erkekjetter
- // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + bugfixed by: Onno Marsman
- // * example 1: ltrim(' Kevin van Zonneveld ');
- // * returns 1: 'Kevin van Zonneveld '
- charlist = !charlist ? ' \\s\u00A0' : (charlist+'').replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1');
- var re = new RegExp('^[' + charlist + ']+', 'g');
- return (str+'').replace(re, '');
- }
- function microtime (get_as_float) {
- // http://kevin.vanzonneveld.net
- // + original by: Paulo Freitas
- // * example 1: timeStamp = microtime(true);
- // * results 1: timeStamp > 1000000000 && timeStamp < 2000000000
- var now = new Date().getTime() / 1000;
- var s = parseInt(now, 10);
- return (get_as_float) ? now : (Math.round((now - s) * 1000) / 1000) + ' ' + s;
- }
- function number_format(number, decimals, dec_point, thousands_sep) {
- // http://kevin.vanzonneveld.net
- // + original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
- // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + bugfix by: Michael White (http://getsprink.com)
- // + bugfix by: Benjamin Lupton
- // + bugfix by: Allan Jensen (http://www.winternet.no)
- // + revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
- // + bugfix by: Howard Yeend
- // + revised by: Luke Smith (http://lucassmith.name)
- // + bugfix by: Diogo Resende
- // + bugfix by: Rival
- // + input by: Kheang Hok Chin (http://www.distantia.ca/)
- // + improved by: davook
- // + improved by: Brett Zamir (http://brett-zamir.me)
- // + input by: Jay Klehr
- // + improved by: Brett Zamir (http://brett-zamir.me)
- // + input by: Amir Habibi (http://www.residence-mixte.com/)
- // + bugfix by: Brett Zamir (http://brett-zamir.me)
- // + improved by: Theriault
- // * example 1: number_format(1234.56);
- // * returns 1: '1,235'
- // * example 2: number_format(1234.56, 2, ',', ' ');
- // * returns 2: '1 234,56'
- // * example 3: number_format(1234.5678, 2, '.', '');
- // * returns 3: '1234.57'
- // * example 4: number_format(67, 2, ',', '.');
- // * returns 4: '67,00'
- // * example 5: number_format(1000);
- // * returns 5: '1,000'
- // * example 6: number_format(67.311, 2);
- // * returns 6: '67.31'
- // * example 7: number_format(1000.55, 1);
- // * returns 7: '1,000.6'
- // * example 8: number_format(67000, 5, ',', '.');
- // * returns 8: '67.000,00000'
- // * example 9: number_format(0.9, 0);
- // * returns 9: '1'
- // * example 10: number_format('1.20', 2);
- // * returns 10: '1.20'
- // * example 11: number_format('1.20', 4);
- // * returns 11: '1.2000'
- // * example 12: number_format('1.2000', 3);
- // * returns 12: '1.200'
- var n = !isFinite(+number) ? 0 : +number,
- prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
- sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
- dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
- s = '',
- toFixedFix = function (n, prec) {
- var k = Math.pow(10, prec);
- return '' + Math.round(n * k) / k;
- };
- // Fix for IE parseFloat(0.55).toFixed(0) = 0;
- s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
- if (s[0].length > 3) {
- s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
- }
- if ((s[1] || '').length < prec) {
- s[1] = s[1] || '';
- s[1] += new Array(prec - s[1].length + 1).join('0');
- }
- return s.join(dec);
- }
- function parse_str (str, array) {
- // http://kevin.vanzonneveld.net
- // + original by: Cagri Ekin
- // + improved by: Michael White (http://getsprink.com)
- // + tweaked by: Jack
- // + bugfixed by: Onno Marsman
- // + reimplemented by: stag019
- // + bugfixed by: Brett Zamir (http://brett-zamir.me)
- // + bugfixed by: stag019
- // - depends on: urldecode
- // + input by: Dreamer
- // + bugfixed by: Brett Zamir (http://brett-zamir.me)
- // % note 1: When no argument is specified, will put variables in global scope.
- // + bugfixed by: MIO_KODUKI (http://mio-koduki.blogspot.com/)
- // % note 1: When a particular argument has been passed, and the returned value is different parse_str of PHP. For example, a=b=c&d====c
- // * example 1: var arr = {};
- // * example 1: parse_str('first=foo&second=bar', arr);
- // * results 1: arr == { first: 'foo', second: 'bar' }
- // * example 2: var arr = {};
- // * example 2: parse_str('str_a=Jack+and+Jill+didn%27t+see+the+well.', arr);
- // * results 2: arr == { str_a: "Jack and Jill didn't see the well." }
- var glue1 = '=',
- glue2 = '&',
- array2 = String(str).replace(/^&?([\s\S]*?)&?$/, '$1').split(glue2),
- i, j, chr, tmp, key, value, bracket, keys, evalStr, that = this,
- fixStr = function (str) {
- return that.urldecode(str).replace(/([\\"'])/g, '\\$1').replace(/\n/g, '\\n').replace(/\r/g, '\\r');
- };
- if (!array) {
- array = this.window;
- }
- for (i = 0; i < array2.length; i++) {
- tmp = array2[i].split(glue1);
- if (tmp.length < 2) {
- tmp = [tmp, ''];
- }
- key = fixStr(tmp.shift());
- value = fixStr(tmp.join(glue1));
- while (key.charAt(0) === ' ') {
- key = key.substr(1);
- }
- if (key.indexOf('\0') !== -1) {
- key = key.substr(0, key.indexOf('\0'));
- }
- if (key && key.charAt(0) !== '[') {
- keys = [];
- bracket = 0;
- for (j = 0; j < key.length; j++) {
- if (key.charAt(j) === '[' && !bracket) {
- bracket = j + 1;
- } else if (key.charAt(j) === ']') {
- if (bracket) {
- if (!keys.length) {
- keys.push(key.substr(0, bracket - 1));
- }
- keys.push(key.substr(bracket, j - bracket));
- bracket = 0;
- if (key.charAt(j + 1) !== '[') {
- break;
- }
- }
- }
- }
- if (!keys.length) {
- keys = [key];
- }
- for (j = 0; j < keys[0].length; j++) {
- chr = keys[0].charAt(j);
- if (chr === ' ' || chr === '.' || chr === '[') {
- keys[0] = keys[0].substr(0, j) + '_' + keys[0].substr(j + 1);
- }
- if (chr === '[') {
- break;
- }
- }
- evalStr = 'array';
- for (j = 0; j < keys.length; j++) {
- key = keys[j];
- if ((key !== '' && key !== ' ') || j === 0) {
- key = "'" + key + "'";
- } else {
- key = eval(evalStr + '.push([]);') - 1;
- }
- evalStr += '[' + key + ']';
- if (j !== keys.length - 1 && eval('typeof ' + evalStr) === 'undefined') {
- eval(evalStr + ' = [];');
- }
- }
- evalStr += " = '" + value + "';\n";
- eval(evalStr);
- }
- }
- }
- function pathinfo (path, options) {
- // http://kevin.vanzonneveld.net
- // + original by: Nate
- // + revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + improved by: Brett Zamir (http://brett-zamir.me)
- // % note 1: Inspired by actual PHP source: php5-5.2.6/ext/standard/string.c line #1559
- // % note 1: The way the bitwise arguments are handled allows for greater flexibility
- // % note 1: & compatability. We might even standardize this code and use a similar approach for
- // % note 1: other bitwise PHP functions
- // % note 2: php.js tries very hard to stay away from a core.js file with global dependencies, because we like
- // % note 2: that you can just take a couple of functions and be on your way.
- // % note 2: But by way we implemented this function, if you want you can still declare the PATHINFO_*
- // % note 2: yourself, and then you can use: pathinfo('/www/index.html', PATHINFO_BASENAME | PATHINFO_EXTENSION);
- // % note 2: which makes it fully compliant with PHP syntax.
- // - depends on: dirname
- // - depends on: basename
- // * example 1: pathinfo('/www/htdocs/index.html', 1);
- // * returns 1: '/www/htdocs'
- // * example 2: pathinfo('/www/htdocs/index.html', 'PATHINFO_BASENAME');
- // * returns 2: 'index.html'
- // * example 3: pathinfo('/www/htdocs/index.html', 'PATHINFO_EXTENSION');
- // * returns 3: 'html'
- // * example 4: pathinfo('/www/htdocs/index.html', 'PATHINFO_FILENAME');
- // * returns 4: 'index'
- // * example 5: pathinfo('/www/htdocs/index.html', 2 | 4);
- // * returns 5: {basename: 'index.html', extension: 'html'}
- // * example 6: pathinfo('/www/htdocs/index.html', 'PATHINFO_ALL');
- // * returns 6: {dirname: '/www/htdocs', basename: 'index.html', extension: 'html', filename: 'index'}
- // * example 7: pathinfo('/www/htdocs/index.html');
- // * returns 7: {dirname: '/www/htdocs', basename: 'index.html', extension: 'html', filename: 'index'}
- // Working vars
- var opt = '', optName = '', optTemp = 0, tmp_arr = {}, cnt = 0, i=0;
- var have_basename = false, have_extension = false, have_filename = false;
- // Input defaulting & sanitation
- if (!path) {
- return false;
- }
- if (!options) {
- options = 'PATHINFO_ALL';
- }
- // Initialize binary arguments. Both the string & integer (constant) input is
- // allowed
- var OPTS = {
- 'PATHINFO_DIRNAME': 1,
- 'PATHINFO_BASENAME': 2,
- 'PATHINFO_EXTENSION': 4,
- 'PATHINFO_FILENAME': 8,
- 'PATHINFO_ALL': 0
- };
- // PATHINFO_ALL sums up all previously defined PATHINFOs (could just pre-calculate)
- for (optName in OPTS) {
- OPTS.PATHINFO_ALL = OPTS.PATHINFO_ALL | OPTS[optName];
- }
- if (typeof options !== 'number') { // Allow for a single string or an array of string flags
- options = [].concat(options);
- for (i=0; i < options.length; i++) {
- // Resolve string input to bitwise e.g. 'PATHINFO_EXTENSION' becomes 4
- if (OPTS[options[i]]) {
- optTemp = optTemp | OPTS[options[i]];
- }
- }
- options = optTemp;
- }
- // Internal Functions
- var __getExt = function (path) {
- var str = path+'';
- var dotP = str.lastIndexOf('.')+1;
- return str.substr(dotP);
- };
- // Gather path infos
- if (options & OPTS.PATHINFO_DIRNAME) {
- tmp_arr.dirname = this.dirname(path);
- }
- if (options & OPTS.PATHINFO_BASENAME) {
- if (false === have_basename) {
- have_basename = this.basename(path);
- }
- tmp_arr.basename = have_basename;
- }
- if (options & OPTS.PATHINFO_EXTENSION) {
- if (false === have_basename) {
- have_basename = this.basename(path);
- }
- if (false === have_extension) {
- have_extension = __getExt(have_basename);
- }
- tmp_arr.extension = have_extension;
- }
- if (options & OPTS.PATHINFO_FILENAME) {
- if (false === have_basename) {
- have_basename = this.basename(path);
- }
- if (false === have_extension) {
- have_extension = __getExt(have_basename);
- }
- if (false === have_filename) {
- have_filename = have_basename.substr(0, (have_basename.length - have_extension.length)-1);
- }
- tmp_arr.filename = have_filename;
- }
- // If array contains only 1 element: return string
- cnt = 0;
- for (opt in tmp_arr){
- cnt++;
- }
- if (cnt == 1) {
- return tmp_arr[opt];
- }
- // Return full-blown array
- return tmp_arr;
- }
- function rtrim ( str, charlist ) {
- // http://kevin.vanzonneveld.net
- // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + input by: Erkekjetter
- // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + bugfixed by: Onno Marsman
- // + input by: rem
- // + bugfixed by: Brett Zamir (http://brett-zamir.me)
- // * example 1: rtrim(' Kevin van Zonneveld ');
- // * returns 1: ' Kevin van Zonneveld'
- charlist = !charlist ? ' \\s\u00A0' : (charlist+'').replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\\$1');
- var re = new RegExp('[' + charlist + ']+$', 'g');
- return (str+'').replace(re, '');
- }
- function serialize (mixed_value) {
- // http://kevin.vanzonneveld.net
- // + original by: Arpad Ray (mailto:arpad@php.net)
- // + improved by: Dino
- // + bugfixed by: Andrej Pavlovic
- // + bugfixed by: Garagoth
- // + input by: DtTvB (http://dt.in.th/2008-09-16.string-length-in-bytes.html)
- // + bugfixed by: Russell Walker (http://www.nbill.co.uk/)
- // + bugfixed by: Jamie Beck (http://www.terabit.ca/)
- // + input by: Martin (http://www.erlenwiese.de/)
- // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // - depends on: utf8_encode
- // % note: We feel the main purpose of this function should be to ease the transport of data between php & js
- // % note: Aiming for PHP-compatibility, we have to translate objects to arrays
- // * example 1: serialize(['Kevin', 'van', 'Zonneveld']);
- // * returns 1: 'a:3:{i:0;s:5:"Kevin";i:1;s:3:"van";i:2;s:9:"Zonneveld";}'
- // * example 2: serialize({firstName: 'Kevin', midName: 'van', surName: 'Zonneveld'});
- // * returns 2: 'a:3:{s:9:"firstName";s:5:"Kevin";s:7:"midName";s:3:"van";s:7:"surName";s:9:"Zonneveld";}'
- var _getType = function (inp) {
- var type = typeof inp, match;
- var key;
- if (type == 'object' && !inp) {
- return 'null';
- }
- if (type == "object") {
- if (!inp.constructor) {
- return 'object';
- }
- var cons = inp.constructor.toString();
- match = cons.match(/(\w+)\(/);
- if (match) {
- cons = match[1].toLowerCase();
- }
- var types = ["boolean", "number", "string", "array"];
- for (key in types) {
- if (cons == types[key]) {
- type = types[key];
- break;
- }
- }
- }
- return type;
- };
- var type = _getType(mixed_value);
- var val, ktype = '';
-
- switch (type) {
- case "function":
- val = "";
- break;
- case "boolean":
- val = "b:" + (mixed_value ? "1" : "0");
- break;
- case "number":
- val = (Math.round(mixed_value) == mixed_value ? "i" : "d") + ":" + mixed_value;
- break;
- case "string":
- mixed_value = this.utf8_encode(mixed_value);
- val = "s:" + encodeURIComponent(mixed_value).replace(/%../g, 'x').length + ":\"" + mixed_value + "\"";
- break;
- case "array":
- case "object":
- val = "a";
- /*
- if (type == "object") {
- var objname = mixed_value.constructor.toString().match(/(\w+)\(\)/);
- if (objname == undefined) {
- return;
- }
- objname[1] = this.serialize(objname[1]);
- val = "O" + objname[1].substring(1, objname[1].length - 1);
- }
- */
- var count = 0;
- var vals = "";
- var okey;
- var key;
- for (key in mixed_value) {
- ktype = _getType(mixed_value[key]);
- if (ktype == "function") {
- continue;
- }
-
- okey = (key.match(/^[0-9]+$/) ? parseInt(key, 10) : key);
- vals += this.serialize(okey) +
- this.serialize(mixed_value[key]);
- count++;
- }
- val += ":" + count + ":{" + vals + "}";
- break;
- case "undefined": // Fall-through
- default: // if the JS object has a property which contains a null value, the string cannot be unserialized by PHP
- val = "N";
- break;
- }
- if (type != "object" && type != "array") {
- val += ";";
- }
- return val;
- }
- function sprintf ( ) {
- // Return a formatted string
- //
- // version: 1004.2314
- // discuss at: http://phpjs.org/functions/sprintf
- // + original by: Ash Searle (http://hexmen.com/blog/)
- // + namespaced by: Michael White (http://getsprink.com)
- // + tweaked by: Jack
- // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + input by: Paulo Ricardo F. Santos
- // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + input by: Brett Zamir (http://brett-zamir.me)
- // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // * example 1: sprintf("%01.2f", 123.1);
- // * returns 1: 123.10
- // * example 2: sprintf("[%10s]", 'monkey');
- // * returns 2: '[ monkey]'
- // * example 3: sprintf("[%'#10s]", 'monkey');
- // * returns 3: '[####monkey]'
- var regex = /%%|%(\d+\$)?([-+\'#0 ]*)(\*\d+\$|\*|\d+)?(\.(\*\d+\$|\*|\d+))?([scboxXuidfegEG])/g;
- var a = arguments, i = 0, format = a[i++];
-
- // pad()
- var pad = function (str, len, chr, leftJustify) {
- if (!chr) {
- chr = ' ';
- }
- var padding = (str.length >= len) ? '' : Array(1 + len - str.length >>> 0).join(chr);
- return leftJustify ? str + padding : padding + str;
- };
-
- // justify()
- var justify = function (value, prefix, leftJustify, minWidth, zeroPad, customPadChar) {
- var diff = minWidth - value.length;
- if (diff > 0) {
- if (leftJustify || !zeroPad) {
- value = pad(value, minWidth, customPadChar, leftJustify);
- } else {
- value = value.slice(0, prefix.length) + pad('', diff, '0', true) + value.slice(prefix.length);
- }
- }
- return value;
- };
-
- // formatBaseX()
- var formatBaseX = function (value, base, prefix, leftJustify, minWidth, precision, zeroPad) {
- // Note: casts negative numbers to positive ones
- var number = value >>> 0;
- prefix = prefix && number && {
- '2': '0b',
- '8': '0',
- '16': '0x'
- }
- [base] || '';
- value = prefix + pad(number.toString(base), precision || 0, '0', false);
- return justify(value, prefix, leftJustify, minWidth, zeroPad);
- };
-
- // formatString()
- var formatString = function (value, leftJustify, minWidth, precision, zeroPad, customPadChar) {
- if (precision != null) {
- value = value.slice(0, precision);
- }
- return justify(value, '', leftJustify, minWidth, zeroPad, customPadChar);
- };
-
- // doFormat()
- var doFormat = function (substring, valueIndex, flags, minWidth, _, precision, type) {
- var number;
- var prefix;
- var method;
- var textTransform;
- var value;
-
- if (substring == '%%') {
- return '%';
- }
-
- // parse flags
- var leftJustify = false, positivePrefix = '', zeroPad = false, prefixBaseX = false, customPadChar = ' ';
- var flagsl = flags.length;
- for (var j = 0; flags && j < flagsl; j++) {
- switch (flags.charAt(j)) {
- case ' ':
- positivePrefix = ' ';
- break;
- case '+':
- positivePrefix = '+';
- break;
- case '-':
- leftJustify = true;
- break;
- case "'":
- customPadChar = flags.charAt(j+1);
- break;
- case '0':
- zeroPad = true;
- break;
- case '#':
- prefixBaseX = true;
- break;
- }
- }
-
- // parameters may be null, undefined, empty-string or real valued
- // we want to ignore null, undefined and empty-string values
- if (!minWidth) {
- minWidth = 0;
- } else if (minWidth == '*') {
- minWidth = +a[i++];
- } else if (minWidth.charAt(0) == '*') {
- minWidth = +a[minWidth.slice(1, -1)];
- } else {
- minWidth = +minWidth;
- }
-
- // Note: undocumented perl feature:
- if (minWidth < 0) {
- minWidth = -minWidth;
- leftJustify = true;
- }
-
- if (!isFinite(minWidth)) {
- error('sprintf: (minimum-)width must be finite');
- }
-
- if (!precision) {
- precision = 'fFeE'.indexOf(type) > -1 ? 6 : (type == 'd') ? 0 : undefined;
- } else if (precision == '*') {
- precision = +a[i++];
- } else if (precision.charAt(0) == '*') {
- precision = +a[precision.slice(1, -1)];
- } else {
- precision = +precision;
- }
-
- // grab value using valueIndex if required?
- value = valueIndex ? a[valueIndex.slice(0, -1)] : a[i++];
-
- switch (type) {
- case 's':
- return formatString(String(value), leftJustify, minWidth, precision, zeroPad, customPadChar);
- case 'c':
- return formatString(String.fromCharCode(+value), leftJustify, minWidth, precision, zeroPad);
- case 'b':
- return formatBaseX(value, 2, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
- case 'o':
- return formatBaseX(value, 8, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
- case 'x':
- return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
- case 'X':
- return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad).toUpperCase();
- case 'u':
- return formatBaseX(value, 10, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
- case 'i':
- case 'd':
- number = parseInt(+value, 10);
- prefix = number < 0 ? '-' : positivePrefix;
- value = prefix + pad(String(Math.abs(number)), precision, '0', false);
- return justify(value, prefix, leftJustify, minWidth, zeroPad);
- case 'e':
- case 'E':
- case 'f':
- case 'F':
- case 'g':
- case 'G':
- number = +value;
- prefix = number < 0 ? '-' : positivePrefix;
- method = ['toExponential', 'toFixed', 'toPrecision']['efg'.indexOf(type.toLowerCase())];
- textTransform = ['toString', 'toUpperCase']['eEfFgG'.indexOf(type) % 2];
- v…
Large files files are truncated, but you can click here to view the full file