PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/php.js

https://github.com/ccarlin/range_check
JavaScript | 271 lines | 79 code | 11 blank | 181 comment | 31 complexity | d5a5cc9a54674adf972f6d8f973ef799 MD5 | raw file
  1. /*
  2. * More info at: http://phpjs.org
  3. *
  4. * This is version: 3.26
  5. * php.js is copyright 2012 Kevin van Zonneveld.
  6. *
  7. * Portions copyright Brett Zamir (http://brett-zamir.me), Kevin van Zonneveld
  8. * (http://kevin.vanzonneveld.net), Onno Marsman, Theriault, Michael White
  9. * (http://getsprink.com), Waldo Malqui Silva, Paulo Freitas, Jack, Jonas
  10. * Raoni Soares Silva (http://www.jsfromhell.com), Philip Peterson, Legaev
  11. * Andrey, Ates Goral (http://magnetiq.com), Alex, Ratheous, Martijn Wieringa,
  12. * Rafał Kukawski (http://blog.kukawski.pl), lmeyrick
  13. * (https://sourceforge.net/projects/bcmath-js/), Nate, Philippe Baumann,
  14. * Enrique Gonzalez, Webtoolkit.info (http://www.webtoolkit.info/), Carlos R.
  15. * L. Rodrigues (http://www.jsfromhell.com), Ash Searle
  16. * (http://hexmen.com/blog/), Jani Hartikainen, travc, Ole Vrijenhoek,
  17. * Erkekjetter, Michael Grier, Rafał Kukawski (http://kukawski.pl), Johnny
  18. * Mast (http://www.phpvrouwen.nl), T.Wild, d3x,
  19. * http://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hex-in-javascript,
  20. * Rafał Kukawski (http://blog.kukawski.pl/), stag019, pilus, WebDevHobo
  21. * (http://webdevhobo.blogspot.com/), marrtins, GeekFG
  22. * (http://geekfg.blogspot.com), Andrea Giammarchi
  23. * (http://webreflection.blogspot.com), Arpad Ray (mailto:arpad@php.net),
  24. * gorthaur, Paul Smith, Tim de Koning (http://www.kingsquare.nl), Joris, Oleg
  25. * Eremeev, Steve Hilder, majak, gettimeofday, KELAN, Josh Fraser
  26. * (http://onlineaspect.com/2007/06/08/auto-detect-a-time-zone-with-javascript/),
  27. * Marc Palau, Kevin van Zonneveld (http://kevin.vanzonneveld.net/), Martin
  28. * (http://www.erlenwiese.de/), Breaking Par Consulting Inc
  29. * (http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256CFB006C45F7),
  30. * Chris, Mirek Slugen, saulius, Alfonso Jimenez
  31. * (http://www.alfonsojimenez.com), Diplom@t (http://difane.com/), felix,
  32. * Mailfaker (http://www.weedem.fr/), Tyler Akins (http://rumkin.com), Caio
  33. * Ariede (http://caioariede.com), Robin, Kankrelune
  34. * (http://www.webfaktory.info/), Karol Kowalski, Imgen Tata
  35. * (http://www.myipdf.com/), mdsjack (http://www.mdsjack.bo.it), Dreamer,
  36. * Felix Geisendoerfer (http://www.debuggable.com/felix), Lars Fischer, AJ,
  37. * David, Aman Gupta, Michael White, Public Domain
  38. * (http://www.json.org/json2.js), Steven Levithan
  39. * (http://blog.stevenlevithan.com), Sakimori, Pellentesque Malesuada,
  40. * Thunder.m, Dj (http://phpjs.org/functions/htmlentities:425#comment_134018),
  41. * Steve Clay, David James, Francois, class_exists, nobbler, T. Wild, Itsacon
  42. * (http://www.itsacon.net/), date, Ole Vrijenhoek (http://www.nervous.nl/),
  43. * Fox, Raphael (Ao RUDLER), Marco, noname, Mateusz "loonquawl" Zalega, Frank
  44. * Forte, Arno, ger, mktime, john (http://www.jd-tech.net), Nick Kolosov
  45. * (http://sammy.ru), marc andreu, Scott Cariss, Douglas Crockford
  46. * (http://javascript.crockford.com), madipta, Slawomir Kaniecki,
  47. * ReverseSyntax, Nathan, Alex Wilson, kenneth, Bayron Guevara, Adam Wallner
  48. * (http://web2.bitbaro.hu/), paulo kuong, jmweb, Lincoln Ramsay, djmix,
  49. * Pyerre, Jon Hohle, Thiago Mata (http://thiagomata.blog.com), lmeyrick
  50. * (https://sourceforge.net/projects/bcmath-js/this.), Linuxworld, duncan,
  51. * Gilbert, Sanjoy Roy, Shingo, sankai, Oskar Larsson Högfeldt
  52. * (http://oskar-lh.name/), Denny Wardhana, 0m3r, Everlasto, Subhasis Deb,
  53. * josh, jd, Pier Paolo Ramon (http://www.mastersoup.com/), P, merabi, Soren
  54. * Hansen, Eugene Bulkin (http://doubleaw.com/), Der Simon
  55. * (http://innerdom.sourceforge.net/), echo is bad, Ozh, XoraX
  56. * (http://www.xorax.info), EdorFaus, JB, J A R, Marc Jansen, Francesco, LH,
  57. * Stoyan Kyosev (http://www.svest.org/), nord_ua, omid
  58. * (http://phpjs.org/functions/380:380#comment_137122), Brad Touesnard, MeEtc
  59. * (http://yass.meetcweb.com), Peter-Paul Koch
  60. * (http://www.quirksmode.org/js/beat.html), Olivier Louvignes
  61. * (http://mg-crea.com/), T0bsn, Tim Wiel, Bryan Elliott, Jalal Berrami,
  62. * Martin, JT, David Randall, Thomas Beaucourt (http://www.webapp.fr), taith,
  63. * vlado houba, Pierre-Luc Paour, Kristof Coomans (SCK-CEN Belgian Nucleair
  64. * Research Centre), Martin Pool, Kirk Strobeck, Rick Waldron, Brant Messenger
  65. * (http://www.brantmessenger.com/), Devan Penner-Woelk, Saulo Vallory, Wagner
  66. * B. Soares, Artur Tchernychev, Valentina De Rosa, Jason Wong
  67. * (http://carrot.org/), Christoph, Daniel Esteban, strftime, Mick@el, rezna,
  68. * Simon Willison (http://simonwillison.net), Anton Ongson, Gabriel Paderni,
  69. * Marco van Oort, penutbutterjelly, Philipp Lenssen, Bjorn Roesbeke
  70. * (http://www.bjornroesbeke.be/), Bug?, Eric Nagel, Tomasz Wesolowski,
  71. * Evertjan Garretsen, Bobby Drake, Blues (http://tech.bluesmoon.info/), Luke
  72. * Godfrey, Pul, uestla, Alan C, Ulrich, Rafal Kukawski, Yves Sucaet,
  73. * sowberry, Norman "zEh" Fuchs, hitwork, Zahlii, johnrembo, Nick Callen,
  74. * Steven Levithan (stevenlevithan.com), ejsanders, Scott Baker, Brian Tafoya
  75. * (http://www.premasolutions.com/), Philippe Jausions
  76. * (http://pear.php.net/user/jausions), Aidan Lister
  77. * (http://aidanlister.com/), Rob, e-mike, HKM, ChaosNo1, metjay, strcasecmp,
  78. * strcmp, Taras Bogach, jpfle, Alexander Ermolaev
  79. * (http://snippets.dzone.com/user/AlexanderErmolaev), DxGx, kilops, Orlando,
  80. * dptr1988, Le Torbi, James (http://www.james-bell.co.uk/), Pedro Tainha
  81. * (http://www.pedrotainha.com), James, Arnout Kazemier
  82. * (http://www.3rd-Eden.com), Chris McMacken, gabriel paderni, Yannoo,
  83. * FGFEmperor, baris ozdil, Tod Gentille, Greg Frazier, jakes, 3D-GRAF, Allan
  84. * Jensen (http://www.winternet.no), Howard Yeend, Benjamin Lupton, davook,
  85. * daniel airton wermann (http://wermann.com.br), Atli Þór, Maximusya, Ryan
  86. * W Tenney (http://ryan.10e.us), Alexander M Beedie, fearphage
  87. * (http://http/my.opera.com/fearphage/), Nathan Sepulveda, Victor, Matteo,
  88. * Billy, stensi, Cord, Manish, T.J. Leahy, Riddler
  89. * (http://www.frontierwebdev.com/), Rafał Kukawski, FremyCompany, Matt
  90. * Bradley, Tim de Koning, Luis Salazar (http://www.freaky-media.com/), Diogo
  91. * Resende, Rival, Andrej Pavlovic, Garagoth, Le Torbi
  92. * (http://www.letorbi.de/), Dino, Josep Sanz (http://www.ws3.es/), rem,
  93. * Russell Walker (http://www.nbill.co.uk/), Jamie Beck
  94. * (http://www.terabit.ca/), setcookie, Michael, YUI Library:
  95. * http://developer.yahoo.com/yui/docs/YAHOO.util.DateLocale.html, Blues at
  96. * http://hacks.bluesmoon.info/strftime/strftime.js, Ben
  97. * (http://benblume.co.uk/), DtTvB
  98. * (http://dt.in.th/2008-09-16.string-length-in-bytes.html), Andreas, William,
  99. * meo, incidence, Cagri Ekin, Amirouche, Amir Habibi
  100. * (http://www.residence-mixte.com/), Luke Smith (http://lucassmith.name),
  101. * Kheang Hok Chin (http://www.distantia.ca/), Jay Klehr, Lorenzo Pisani,
  102. * Tony, Yen-Wei Liu, Greenseed, mk.keck, Leslie Hoare, dude, booeyOH, Ben
  103. * Bryan
  104. *
  105. * Dual licensed under the MIT (MIT-LICENSE.txt)
  106. * and GPL (GPL-LICENSE.txt) licenses.
  107. *
  108. * Permission is hereby granted, free of charge, to any person obtaining a
  109. * copy of this software and associated documentation files (the
  110. * "Software"), to deal in the Software without restriction, including
  111. * without limitation the rights to use, copy, modify, merge, publish,
  112. * distribute, sublicense, and/or sell copies of the Software, and to
  113. * permit persons to whom the Software is furnished to do so, subject to
  114. * the following conditions:
  115. *
  116. * The above copyright notice and this permission notice shall be included
  117. * in all copies or substantial portions of the Software.
  118. *
  119. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  120. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  121. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  122. * IN NO EVENT SHALL KEVIN VAN ZONNEVELD BE LIABLE FOR ANY CLAIM, DAMAGES
  123. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  124. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  125. * OTHER DEALINGS IN THE SOFTWARE.
  126. */
  127. // jslint.com configuration options. See: http://wiki.github.com/kvz/phpjs/jslint-options
  128. /* global window */
  129. /* 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 */
  130. // Our idea with CommonJS is that you can do the following:
  131. // var php = require('php');
  132. // php.md5('test');
  133. exports.inet_ntop = function (a) {
  134. // Converts a packed inet address to a human readable IP address string
  135. //
  136. // version: 1109.2015
  137. // discuss at: http://phpjs.org/functions/inet_ntop
  138. // + original by: Theriault
  139. // * example 1: \php.inet_ntop('\x7F\x00\x00\x01');
  140. // * returns 1: '127.0.0.1'
  141. // * example 2: \php.inet_ntop('\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1');
  142. // * returns 2: '::1'
  143. var i = 0,
  144. m = '',
  145. c = [];
  146. a += '';
  147. if (a.length === 4) { // IPv4
  148. return [
  149. a.charCodeAt(0), a.charCodeAt(1), a.charCodeAt(2), a.charCodeAt(3)].join('.');
  150. } else if (a.length === 16) { // IPv6
  151. for (i = 0; i < 16; i++) {
  152. c.push(((a.charCodeAt(i++) << 8) + a.charCodeAt(i)).toString(16));
  153. }
  154. return c.join(':').replace(/((^|:)0(?=:|$))+:?/g, function (t) {
  155. m = (t.length > m.length) ? t : m;
  156. return t;
  157. }).replace(m || ' ', '::');
  158. } else { // Invalid length
  159. return false;
  160. }
  161. };
  162. exports.inet_pton = function (a) {
  163. // Converts a human readable IP address to a packed binary string
  164. //
  165. // version: 1109.2015
  166. // discuss at: http://phpjs.org/functions/inet_pton
  167. // + original by: Theriault
  168. // * example 1: \php.inet_pton('::');
  169. // * returns 1: '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0' (binary)
  170. // * example 2: \php.inet_pton('127.0.0.1');
  171. // * returns 2: '\x7F\x00\x00\x01' (binary)
  172. var r, m, x, i, j, f = String.fromCharCode;
  173. m = a.match(/^(?:\d{1,3}(?:\.|$)){4}/); // IPv4
  174. if (m) {
  175. m = m[0].split('.');
  176. m = f(m[0]) + f(m[1]) + f(m[2]) + f(m[3]);
  177. // Return if 4 bytes, otherwise false.
  178. return m.length === 4 ? m : false;
  179. }
  180. r = /^((?:[\da-f]{1,4}(?::|)){0,8})(::)?((?:[\da-f]{1,4}(?::|)){0,8})$/;
  181. m = a.match(r); // IPv6
  182. if (m) {
  183. // Translate each hexadecimal value.
  184. for (j = 1; j < 4; j++) {
  185. // Indice 2 is :: and if no length, continue.
  186. if (j === 2 || m[j].length === 0) {
  187. continue;
  188. }
  189. m[j] = m[j].split(':');
  190. for (i = 0; i < m[j].length; i++) {
  191. m[j][i] = parseInt(m[j][i], 16);
  192. // Would be NaN if it was blank, return false.
  193. if (isNaN(m[j][i])) {
  194. return false; // Invalid IP.
  195. }
  196. m[j][i] = f(m[j][i] >> 8) + f(m[j][i] & 0xFF);
  197. }
  198. m[j] = m[j].join('');
  199. }
  200. x = m[1].length + m[3].length;
  201. if (x === 16) {
  202. return m[1] + m[3];
  203. } else if (x < 16 && m[2].length > 0) {
  204. return m[1] + (new Array(16 - x + 1)).join('\x00') + m[3];
  205. }
  206. }
  207. return false; // Invalid IP.
  208. };
  209. exports.ip2long = function (IP) {
  210. // Converts a string containing an (IPv4) Internet Protocol dotted address into a proper address
  211. //
  212. // version: 1109.2015
  213. // discuss at: http://phpjs.org/functions/ip2long
  214. // + original by: Waldo Malqui Silva
  215. // + improved by: Victor
  216. // + revised by: fearphage (http://http/my.opera.com/fearphage/)
  217. // + revised by: Theriault
  218. // * example 1: \php.ip2long('192.0.34.166');
  219. // * returns 1: 3221234342
  220. // * example 2: \php.ip2long('0.0xABCDEF');
  221. // * returns 2: 11259375
  222. // * example 3: \php.ip2long('255.255.255.256');
  223. // * returns 3: false
  224. var i = 0;
  225. // PHP allows decimal, octal, and hexadecimal IP components.
  226. // PHP allows between 1 (e.g. 127) to 4 (e.g 127.0.0.1) components.
  227. 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.
  228. if (!IP) {
  229. return false; // Invalid format.
  230. }
  231. // Reuse IP variable for component counter.
  232. IP[0] = 0;
  233. for (i = 1; i < 5; i += 1) {
  234. IP[0] += !! ((IP[i] || '').length);
  235. IP[i] = parseInt(IP[i]) || 0;
  236. }
  237. // Continue to use IP for overflow values.
  238. // PHP does not allow any component to overflow.
  239. IP.push(256, 256, 256, 256);
  240. // Recalculate overflow of last component supplied to make up for missing components.
  241. IP[4 + IP[0]] *= Math.pow(256, 4 - IP[0]);
  242. if (IP[1] >= IP[5] || IP[2] >= IP[6] || IP[3] >= IP[7] || IP[4] >= IP[8]) {
  243. return false;
  244. }
  245. return IP[1] * (IP[0] === 1 || 16777216) + IP[2] * (IP[0] <= 2 || 65536) + IP[3] * (IP[0] <= 3 || 256) + IP[4] * 1;
  246. };
  247. exports.long2ip = function (proper_address) {
  248. // Converts an (IPv4) Internet network address into a string in Internet standard dotted format
  249. //
  250. // version: 1109.2015
  251. // discuss at: http://phpjs.org/functions/long2ip
  252. // + original by: Waldo Malqui Silva
  253. // * example 1: \php.long2ip( 3221234342 );
  254. // * returns 1: '192.0.34.166'
  255. var output = false;
  256. if (!isNaN(proper_address) && (proper_address >= 0 || proper_address <= 4294967295)) {
  257. 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));
  258. }
  259. return output;
  260. };