UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

295 lines (235 loc) 9.64 kB
<!-- Generated by Rakefile:build --> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2012-06-16 04:42:20 <br /> @AngelFcM: there is a dependency listed; you have to add the utf8_encode() function---but I should warn you that this function does not seem to behave properly, at least like in PHP, or at least for non-ASCII. http://pajhome.org.uk/crypt/md5/ claims to do this, so I'd like to see if we could test to confirm. <hr /> <strong> <a href="localhost" rel="nofollow">AngelFcM</a> </strong> on 2012-05-19 20:09:47 <br /> I tried use it in the console web of Firefox but returns me the next error. [13:07:48.759] TypeError: this.utf8_encode is not a function ??? <hr /> <strong> <a href="http://an3m1.com/" rel="nofollow">???? ????</a> </strong> on 2012-04-18 10:19:40 <br /> Write more, that’s all I have to say. Literally, it seems as though you relied on the video to make your point. You clearly know what you’re talking about, why waste your intelligence on just posting videos to your blog when you could be giving us something enlightening to read <hr /> <strong> Steve </strong> on 2011-06-22 14:02:51 <br /> This js will not work with long string and will not handle string containing &quot;&amp;&quot; symbols. <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2010-12-22 05:58:09 <br /> @Rob: Hi Rob, thanks for your input. Someone claimed that unlike ours, http://pajhome.org.uk/crypt/md5/ (under a BSD license) works correctly with non-ASCII characters. Do you have the inclination to test that for us? I'd hate to make patches if we should be working on another code base instead. <hr /> <strong> Rob </strong> on 2010-12-20 23:57:48 <br /> There's a minor bug in convertToWordArray() that causes the word array input to the MD5 function to have null values. JavaScript does the right thing anyway, but it may not be desired behavior. Here's one fix: <pre><code> var convertToWordArray = function (str) { var lWordCount; var lMessageLength = str.length; var lNumberOfWords_temp1=lMessageLength + 8; var lNumberOfWords_temp2=(lNumberOfWords_temp1-(lNumberOfWords_temp1 % 64))/64; var lNumberOfWords = (lNumberOfWords_temp2+1)*16; var lWordArray=new Array(lNumberOfWords-1); /* Initialize lWordArray */ var i = lWordArray.length; while ( i-- ) { lWordArray[i] = 0; } var lBytePosition = 0; var lByteCount = 0; while ( lByteCount &lt; lMessageLength ) { lWordCount = (lByteCount-(lByteCount % 4))/4; lBytePosition = (lByteCount % 4)*8; lWordArray[lWordCount] = (lWordArray[lWordCount] | (str.charCodeAt(lByteCount)&lt;&lt;lBytePosition)); lByteCount++; } lWordCount = (lByteCount-(lByteCount % 4))/4; lBytePosition = (lByteCount % 4)*8; lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80&lt;&lt;lBytePosition); lWordArray[lNumberOfWords-2] = lMessageLength&lt;&lt;3; lWordArray[lNumberOfWords-1] = lMessageLength&gt;&gt;&gt;29; return lWordArray; }; </code></pre> Also, addUnsigned can be shortened up a lot if you're willing to bet on JavaScript not changing its behavior. Since JavaScript currently has 53 bits of precision for its number type, you can change addUnsigned to: <pre><code> var addUnsigned = function (lX,lY) { var x = lX + lY; return x &lt; 0xFFFFFFFF ? x : x - 0x100000000; }; </code></pre> Since addUnsigned is an innermost function, that might or might not be a desirable change. I've tested it with known MD5 values on several browsers and they've all handled it correctly. <hr /> <strong> ion </strong> on 2010-11-25 07:24:19 <br /> you can take a look at this site: http://md5hashdecrypter.com/decrypt.php <hr /> <strong> <a href="NONE" rel="nofollow">pilus</a> </strong> on 2009-06-10 20:28:26 <br /> Sorry if I sound kinda lame coz I am, but what about the second argument to php's md5() that returns raw binary data ? how could I do that in JS ? Anyone know ? <hr /> <strong> Rob </strong> on 2009-06-07 19:26:20 <br /> Should the dependency be used even if the page encoding is already set to utf-8 through the Content-Type meta tag? <hr /> <strong> <a href="http://www.in-wetter.at" rel="nofollow">Wetter</a> </strong> on 2008-11-15 17:14:49 <br /> It is very useful article - exactly this I find long time. Thank you. <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-02-07 00:12:29 <br /> @ Evil Angelica: Guess I haven't.. Do you have any idea what hacking is? <hr /> <strong> Evil Angelica </strong> on 2008-02-06 20:32:17 <br /> <pre><code>alert:(You Have Been H4x0r3D! By Evil Angelica <hr /> <strong> Philip Andrew </strong> on 2008-01-30 08:04:45 <br /> This is a good idea if you want to combine it with Aptana JAXER then it makes JAXER more like PHP. http://www.aptana.com/jaxer/ <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-01-29 21:00:09 <br /> @ Eric: Interesting thought. But that would mean maintaining multiple versions of the same function, just to be able to do a 'solid' benchmark (by eliminating as many variables such as platform, browser, processor, etc. between your workstation and mine). And storing and maintaining different versions for the sole purpose of a micro benchmark would slow the project down in this phase, I think. So for now, I think I'll leave it up to the community to suggest speed improvements instead. But thanks for your input, very much appreciated! <hr /> <strong> Eric </strong> on 2008-01-28 02:34:39 <br /> I think it'd be fantastic to see these functions coupled with some performance measurements, both so we can see areas need optimization and to help people understand which operations are unrealistically expensive. <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-01-18 11:13:03 <br /> @ Ates Goral: I've added it to my queue. You can check out RSS to see when it gets added here. Thank you for that masterpiece! <hr /> <strong> Ates Goral </strong> on 2008-01-18 05:53:21 <br /> A while back, I had published a function called phpSerialize() (that does what the PHP serialize() function does) at http://magnetiq.com/2006/07/30/php-style-serialization-of-javascript-objects/ Here's the code copied here: <pre><code> /* Returns the class name of the argument or undefined if it's not a valid JavaScript object. */ function getObjectClass(obj) { if (obj &amp;amp;&amp;amp; obj.constructor &amp;amp;&amp;amp; obj.constructor.toString) { var arr = obj.constructor.toString().match( /function\s*(\w+)/); if (arr &amp;amp;&amp;amp; arr.length == 2) { return arr[1]; } } return undefined; } /* Serializes the given argument, PHP-style. The type mapping is as follows: JavaScript Type PHP Type --------------- -------- Number Integer or Decimal String String Boolean Boolean Array Array Object Object undefined Null The special JavaScript object null also becomes PHP Null. This function may not handle associative arrays or array objects with additional properties well. */ function phpSerialize(val) { switch (typeof(val)) { case &amp;quot;number&amp;quot;: return (Math.floor(val) == val ? &amp;quot;i&amp;quot; : &amp;quot;d&amp;quot;) + &amp;quot;:&amp;quot; + val + &amp;quot;;&amp;quot;; case &amp;quot;string&amp;quot;: return &amp;quot;s:&amp;quot; + val.length + &amp;quot;:\&amp;quot;&amp;quot; + val + &amp;quot;\&amp;quot;;&amp;quot;; case &amp;quot;boolean&amp;quot;: return &amp;quot;b:&amp;quot; + (val ? &amp;quot;1&amp;quot; : &amp;quot;0&amp;quot;) + &amp;quot;;&amp;quot;; case &amp;quot;object&amp;quot;: if (val == null) { return &amp;quot;N;&amp;quot;; } else if (&amp;quot;length&amp;quot; in val) { var idxobj = { idx: -1 }; return &amp;quot;a:&amp;quot; + val.length + &amp;quot;:{&amp;quot; + val.map( function (item) { this.idx++; var ser = phpSerialize(item); return ser ? phpSerialize(this.idx) + ser : false; }, idxobj).filter( function (item) { return item; }).join(&amp;quot;&amp;quot;) + &amp;quot;}&amp;quot;; } else { var class_name = getObjectClass(val); if (class_name == undefined) { return false; } var props = new Array(); for (var prop in val) { var ser = phpSerialize(val[prop]); if (ser) { props.push(phpSerialize(prop) + ser); } } return &amp;quot;O:&amp;quot; + class_name.length + &amp;quot;:\&amp;quot;&amp;quot; + class_name + &amp;quot;\&amp;quot;:&amp;quot; + props.length + &amp;quot;:{&amp;quot; + props.join(&amp;quot;&amp;quot;) + &amp;quot;}&amp;quot;; } case &amp;quot;undefined&amp;quot;: return &amp;quot;N;&amp;quot;; } return false; } </code></pre> <hr />