Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Checking if IP address is vaild works

  • Loading branch information...
commit fd6d7076bab689af8e2e652e76b9014ddb985ce8 1 parent 4c0ad22
@keverw authored
Showing with 287 additions and 0 deletions.
  1. +271 −0 php.js
  2. +13 −0 range_check.js
  3. +3 −0  test.js
View
271 php.js
@@ -0,0 +1,271 @@
+/*
+ * More info at: http://phpjs.org
+ *
+ * This is version: 3.26
+ * php.js is copyright 2012 Kevin van Zonneveld.
+ *
+ * Portions copyright Brett Zamir (http://brett-zamir.me), Kevin van Zonneveld
+ * (http://kevin.vanzonneveld.net), Onno Marsman, Theriault, Michael White
+ * (http://getsprink.com), Waldo Malqui Silva, Paulo Freitas, Jack, Jonas
+ * Raoni Soares Silva (http://www.jsfromhell.com), Philip Peterson, Legaev
+ * Andrey, Ates Goral (http://magnetiq.com), Alex, Ratheous, Martijn Wieringa,
+ * Rafał Kukawski (http://blog.kukawski.pl), lmeyrick
+ * (https://sourceforge.net/projects/bcmath-js/), Nate, Philippe Baumann,
+ * Enrique Gonzalez, Webtoolkit.info (http://www.webtoolkit.info/), Carlos R.
+ * L. Rodrigues (http://www.jsfromhell.com), Ash Searle
+ * (http://hexmen.com/blog/), Jani Hartikainen, travc, Ole Vrijenhoek,
+ * Erkekjetter, Michael Grier, Rafał Kukawski (http://kukawski.pl), Johnny
+ * Mast (http://www.phpvrouwen.nl), T.Wild, d3x,
+ * http://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hex-in-javascript,
+ * Rafał Kukawski (http://blog.kukawski.pl/), stag019, pilus, WebDevHobo
+ * (http://webdevhobo.blogspot.com/), marrtins, GeekFG
+ * (http://geekfg.blogspot.com), Andrea Giammarchi
+ * (http://webreflection.blogspot.com), Arpad Ray (mailto:arpad@php.net),
+ * gorthaur, Paul Smith, Tim de Koning (http://www.kingsquare.nl), Joris, Oleg
+ * Eremeev, Steve Hilder, majak, gettimeofday, KELAN, Josh Fraser
+ * (http://onlineaspect.com/2007/06/08/auto-detect-a-time-zone-with-javascript/),
+ * Marc Palau, Kevin van Zonneveld (http://kevin.vanzonneveld.net/), Martin
+ * (http://www.erlenwiese.de/), Breaking Par Consulting Inc
+ * (http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256CFB006C45F7),
+ * Chris, Mirek Slugen, saulius, Alfonso Jimenez
+ * (http://www.alfonsojimenez.com), Diplom@t (http://difane.com/), felix,
+ * Mailfaker (http://www.weedem.fr/), Tyler Akins (http://rumkin.com), Caio
+ * Ariede (http://caioariede.com), Robin, Kankrelune
+ * (http://www.webfaktory.info/), Karol Kowalski, Imgen Tata
+ * (http://www.myipdf.com/), mdsjack (http://www.mdsjack.bo.it), Dreamer,
+ * Felix Geisendoerfer (http://www.debuggable.com/felix), Lars Fischer, AJ,
+ * David, Aman Gupta, Michael White, Public Domain
+ * (http://www.json.org/json2.js), Steven Levithan
+ * (http://blog.stevenlevithan.com), Sakimori, Pellentesque Malesuada,
+ * Thunder.m, Dj (http://phpjs.org/functions/htmlentities:425#comment_134018),
+ * Steve Clay, David James, Francois, class_exists, nobbler, T. Wild, Itsacon
+ * (http://www.itsacon.net/), date, Ole Vrijenhoek (http://www.nervous.nl/),
+ * Fox, Raphael (Ao RUDLER), Marco, noname, Mateusz "loonquawl" Zalega, Frank
+ * Forte, Arno, ger, mktime, john (http://www.jd-tech.net), Nick Kolosov
+ * (http://sammy.ru), marc andreu, Scott Cariss, Douglas Crockford
+ * (http://javascript.crockford.com), madipta, Slawomir Kaniecki,
+ * ReverseSyntax, Nathan, Alex Wilson, kenneth, Bayron Guevara, Adam Wallner
+ * (http://web2.bitbaro.hu/), paulo kuong, jmweb, Lincoln Ramsay, djmix,
+ * Pyerre, Jon Hohle, Thiago Mata (http://thiagomata.blog.com), lmeyrick
+ * (https://sourceforge.net/projects/bcmath-js/this.), Linuxworld, duncan,
+ * Gilbert, Sanjoy Roy, Shingo, sankai, Oskar Larsson Högfeldt
+ * (http://oskar-lh.name/), Denny Wardhana, 0m3r, Everlasto, Subhasis Deb,
+ * josh, jd, Pier Paolo Ramon (http://www.mastersoup.com/), P, merabi, Soren
+ * Hansen, Eugene Bulkin (http://doubleaw.com/), Der Simon
+ * (http://innerdom.sourceforge.net/), echo is bad, Ozh, XoraX
+ * (http://www.xorax.info), EdorFaus, JB, J A R, Marc Jansen, Francesco, LH,
+ * Stoyan Kyosev (http://www.svest.org/), nord_ua, omid
+ * (http://phpjs.org/functions/380:380#comment_137122), Brad Touesnard, MeEtc
+ * (http://yass.meetcweb.com), Peter-Paul Koch
+ * (http://www.quirksmode.org/js/beat.html), Olivier Louvignes
+ * (http://mg-crea.com/), T0bsn, Tim Wiel, Bryan Elliott, Jalal Berrami,
+ * Martin, JT, David Randall, Thomas Beaucourt (http://www.webapp.fr), taith,
+ * vlado houba, Pierre-Luc Paour, Kristof Coomans (SCK-CEN Belgian Nucleair
+ * Research Centre), Martin Pool, Kirk Strobeck, Rick Waldron, Brant Messenger
+ * (http://www.brantmessenger.com/), Devan Penner-Woelk, Saulo Vallory, Wagner
+ * B. Soares, Artur Tchernychev, Valentina De Rosa, Jason Wong
+ * (http://carrot.org/), Christoph, Daniel Esteban, strftime, Mick@el, rezna,
+ * Simon Willison (http://simonwillison.net), Anton Ongson, Gabriel Paderni,
+ * Marco van Oort, penutbutterjelly, Philipp Lenssen, Bjorn Roesbeke
+ * (http://www.bjornroesbeke.be/), Bug?, Eric Nagel, Tomasz Wesolowski,
+ * Evertjan Garretsen, Bobby Drake, Blues (http://tech.bluesmoon.info/), Luke
+ * Godfrey, Pul, uestla, Alan C, Ulrich, Rafal Kukawski, Yves Sucaet,
+ * sowberry, Norman "zEh" Fuchs, hitwork, Zahlii, johnrembo, Nick Callen,
+ * Steven Levithan (stevenlevithan.com), ejsanders, Scott Baker, Brian Tafoya
+ * (http://www.premasolutions.com/), Philippe Jausions
+ * (http://pear.php.net/user/jausions), Aidan Lister
+ * (http://aidanlister.com/), Rob, e-mike, HKM, ChaosNo1, metjay, strcasecmp,
+ * strcmp, Taras Bogach, jpfle, Alexander Ermolaev
+ * (http://snippets.dzone.com/user/AlexanderErmolaev), DxGx, kilops, Orlando,
+ * dptr1988, Le Torbi, James (http://www.james-bell.co.uk/), Pedro Tainha
+ * (http://www.pedrotainha.com), James, Arnout Kazemier
+ * (http://www.3rd-Eden.com), Chris McMacken, gabriel paderni, Yannoo,
+ * FGFEmperor, baris ozdil, Tod Gentille, Greg Frazier, jakes, 3D-GRAF, Allan
+ * Jensen (http://www.winternet.no), Howard Yeend, Benjamin Lupton, davook,
+ * daniel airton wermann (http://wermann.com.br), Atli Þór, Maximusya, Ryan
+ * W Tenney (http://ryan.10e.us), Alexander M Beedie, fearphage
+ * (http://http/my.opera.com/fearphage/), Nathan Sepulveda, Victor, Matteo,
+ * Billy, stensi, Cord, Manish, T.J. Leahy, Riddler
+ * (http://www.frontierwebdev.com/), Rafał Kukawski, FremyCompany, Matt
+ * Bradley, Tim de Koning, Luis Salazar (http://www.freaky-media.com/), Diogo
+ * Resende, Rival, Andrej Pavlovic, Garagoth, Le Torbi
+ * (http://www.letorbi.de/), Dino, Josep Sanz (http://www.ws3.es/), rem,
+ * Russell Walker (http://www.nbill.co.uk/), Jamie Beck
+ * (http://www.terabit.ca/), setcookie, Michael, YUI Library:
+ * http://developer.yahoo.com/yui/docs/YAHOO.util.DateLocale.html, Blues at
+ * http://hacks.bluesmoon.info/strftime/strftime.js, Ben
+ * (http://benblume.co.uk/), DtTvB
+ * (http://dt.in.th/2008-09-16.string-length-in-bytes.html), Andreas, William,
+ * meo, incidence, Cagri Ekin, Amirouche, Amir Habibi
+ * (http://www.residence-mixte.com/), Luke Smith (http://lucassmith.name),
+ * Kheang Hok Chin (http://www.distantia.ca/), Jay Klehr, Lorenzo Pisani,
+ * Tony, Yen-Wei Liu, Greenseed, mk.keck, Leslie Hoare, dude, booeyOH, Ben
+ * Bryan
+ *
+ * Dual licensed under the MIT (MIT-LICENSE.txt)
+ * and GPL (GPL-LICENSE.txt) licenses.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL KEVIN VAN ZONNEVELD BE LIABLE FOR ANY CLAIM, DAMAGES
+ * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+
+// jslint.com configuration options. See: http://wiki.github.com/kvz/phpjs/jslint-options
+/* global window */
+/* jslint adsafe: false, bitwise: false, browser: false, cap: false, css: false, debug: false, devel: false, eqeqeq: true, evil: false, forin: false, fragment: false, immed: true, indent: 4, laxbreak: false, maxerr: 100, maxlen: 80, newcap: true, nomen: false, on: true, onevar: false, passfail: false, plusplus: false, regexp: false, rhino: false, safe: false, sidebar: false, strict: false, sub: false, undef: true, white: false, widget: false */
+
+// Our idea with CommonJS is that you can do the following:
+// var php = require('php');
+// php.md5('test');
+
+exports.inet_ntop = function (a) {
+ // Converts a packed inet address to a human readable IP address string
+ //
+ // version: 1109.2015
+ // discuss at: http://phpjs.org/functions/inet_ntop
+ // + original by: Theriault
+ // * example 1: \php.inet_ntop('\x7F\x00\x00\x01');
+ // * returns 1: '127.0.0.1'
+ // * example 2: \php.inet_ntop('\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1');
+ // * returns 2: '::1'
+ var i = 0,
+ m = '',
+ c = [];
+ a += '';
+ if (a.length === 4) { // IPv4
+ return [
+ a.charCodeAt(0), a.charCodeAt(1), a.charCodeAt(2), a.charCodeAt(3)].join('.');
+ } else if (a.length === 16) { // IPv6
+ for (i = 0; i < 16; i++) {
+ c.push(((a.charCodeAt(i++) << 8) + a.charCodeAt(i)).toString(16));
+ }
+ return c.join(':').replace(/((^|:)0(?=:|$))+:?/g, function (t) {
+ m = (t.length > m.length) ? t : m;
+ return t;
+ }).replace(m || ' ', '::');
+ } else { // Invalid length
+ return false;
+ }
+};
+
+exports.inet_pton = function (a) {
+ // Converts a human readable IP address to a packed binary string
+ //
+ // version: 1109.2015
+ // discuss at: http://phpjs.org/functions/inet_pton
+ // + original by: Theriault
+ // * example 1: \php.inet_pton('::');
+ // * returns 1: '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0' (binary)
+ // * example 2: \php.inet_pton('127.0.0.1');
+ // * returns 2: '\x7F\x00\x00\x01' (binary)
+ var r, m, x, i, j, f = String.fromCharCode;
+ m = a.match(/^(?:\d{1,3}(?:\.|$)){4}/); // IPv4
+ if (m) {
+ m = m[0].split('.');
+ m = f(m[0]) + f(m[1]) + f(m[2]) + f(m[3]);
+ // Return if 4 bytes, otherwise false.
+ return m.length === 4 ? m : false;
+ }
+ r = /^((?:[\da-f]{1,4}(?::|)){0,8})(::)?((?:[\da-f]{1,4}(?::|)){0,8})$/;
+ m = a.match(r); // IPv6
+ if (m) {
+ // Translate each hexadecimal value.
+ for (j = 1; j < 4; j++) {
+ // Indice 2 is :: and if no length, continue.
+ if (j === 2 || m[j].length === 0) {
+ continue;
+ }
+ m[j] = m[j].split(':');
+ for (i = 0; i < m[j].length; i++) {
+ m[j][i] = parseInt(m[j][i], 16);
+ // Would be NaN if it was blank, return false.
+ if (isNaN(m[j][i])) {
+ return false; // Invalid IP.
+ }
+ m[j][i] = f(m[j][i] >> 8) + f(m[j][i] & 0xFF);
+ }
+ m[j] = m[j].join('');
+ }
+ x = m[1].length + m[3].length;
+ if (x === 16) {
+ return m[1] + m[3];
+ } else if (x < 16 && m[2].length > 0) {
+ return m[1] + (new Array(16 - x + 1)).join('\x00') + m[3];
+ }
+ }
+ return false; // Invalid IP.
+};
+
+exports.ip2long = function (IP) {
+ // Converts a string containing an (IPv4) Internet Protocol dotted address into a proper address
+ //
+ // version: 1109.2015
+ // discuss at: http://phpjs.org/functions/ip2long
+ // + original by: Waldo Malqui Silva
+ // + improved by: Victor
+ // + revised by: fearphage (http://http/my.opera.com/fearphage/)
+ // + revised by: Theriault
+ // * example 1: \php.ip2long('192.0.34.166');
+ // * returns 1: 3221234342
+ // * example 2: \php.ip2long('0.0xABCDEF');
+ // * returns 2: 11259375
+ // * example 3: \php.ip2long('255.255.255.256');
+ // * returns 3: false
+ var i = 0;
+ // PHP allows decimal, octal, and hexadecimal IP components.
+ // PHP allows between 1 (e.g. 127) to 4 (e.g 127.0.0.1) components.
+ IP = IP.match(/^([1-9]\d*|0[0-7]*|0x[\da-f]+)(?:\.([1-9]\d*|0[0-7]*|0x[\da-f]+))?(?:\.([1-9]\d*|0[0-7]*|0x[\da-f]+))?(?:\.([1-9]\d*|0[0-7]*|0x[\da-f]+))?$/i); // Verify IP format.
+ if (!IP) {
+ return false; // Invalid format.
+ }
+ // Reuse IP variable for component counter.
+ IP[0] = 0;
+ for (i = 1; i < 5; i += 1) {
+ IP[0] += !! ((IP[i] || '').length);
+ IP[i] = parseInt(IP[i]) || 0;
+ }
+ // Continue to use IP for overflow values.
+ // PHP does not allow any component to overflow.
+ IP.push(256, 256, 256, 256);
+ // Recalculate overflow of last component supplied to make up for missing components.
+ IP[4 + IP[0]] *= Math.pow(256, 4 - IP[0]);
+ if (IP[1] >= IP[5] || IP[2] >= IP[6] || IP[3] >= IP[7] || IP[4] >= IP[8]) {
+ return false;
+ }
+ return IP[1] * (IP[0] === 1 || 16777216) + IP[2] * (IP[0] <= 2 || 65536) + IP[3] * (IP[0] <= 3 || 256) + IP[4] * 1;
+};
+
+exports.long2ip = function (proper_address) {
+ // Converts an (IPv4) Internet network address into a string in Internet standard dotted format
+ //
+ // version: 1109.2015
+ // discuss at: http://phpjs.org/functions/long2ip
+ // + original by: Waldo Malqui Silva
+ // * example 1: \php.long2ip( 3221234342 );
+ // * returns 1: '192.0.34.166'
+ var output = false;
+
+ if (!isNaN(proper_address) && (proper_address >= 0 || proper_address <= 4294967295)) {
+ output = Math.floor(proper_address / Math.pow(256, 3)) + '.' + Math.floor((proper_address % Math.pow(256, 3)) / Math.pow(256, 2)) + '.' + Math.floor(((proper_address % Math.pow(256, 3)) % Math.pow(256, 2)) / Math.pow(256, 1)) + '.' + Math.floor((((proper_address % Math.pow(256, 3)) % Math.pow(256, 2)) % Math.pow(256, 1)) / Math.pow(256, 0));
+ }
+
+ return output;
+};
+
+
View
13 range_check.js
@@ -0,0 +1,13 @@
+(function ()
+{
+ function vaild_ip(addr)
+ {
+ var php = require('./php.js');
+ return php.inet_pton(addr) !== false;
+ }
+
+ // Export public API
+ var range_check = {};
+ range_check.vaild_ip = vaild_ip;
+ module.exports = range_check;
+}());
View
3  test.js
@@ -0,0 +1,3 @@
+var range_check = require('./range_check.js'); //use just diskspace if installed via NPM
+
+console.log(range_check.vaild_ip('10.0.1.5'));
Please sign in to comment.
Something went wrong with that request. Please try again.