Sunday, August 28, 2011

image test to local site


Reading a php page with only an image is a method a hacker could use to get your cookie info if they could get that image tag onto a secure site or blog. <br /> <img alt="no image" src="http://stokescomp.com/sample/test.php" style="display: none;" />

Monday, August 22, 2011

Error when trying to use JSON and jquery libraries together

For the fix for this just use json2.js instead of json.js:
https://github.com/douglascrockford/JSON-js
The problem was that he was replacing the json function in the object.prototype and this breaks jQuery since this is not a very good idea. The new json2.js is much better. Below is the troubles I was having and how I solved it with the json.js but later I ran into other things that was broken in jQuery because of json.js. So just use json2.js instead.
I got this error when I had both json.js and jquery scripts in the same html file:
JSON.parse: unexpected character So I tried updating to the newest jquery 1.6.2 and I got this error: c.replace is not a function
I spent a couple hours trying to track it down.
I noticed that when I leave out the json.js file there are no errors when running any jquery code.
First I looked in json.js and found the lines that made it fail.
In json.js it has this code at the end to actually make the parseJSON function if it doesn't exist:

if(!Object.prototype.toJSONString){
Object.prototype.toJSONString=function(filter){return JSON.stringify(this,filter);};
Object.prototype.parseJSON=function(filter){return JSON.parse(this,filter);};
}}());


Now I tracked this down to the exact part of the jquery code that was causeing the problem.
There is a function called by(a,c) in the jquery file. The variable c had these values:
paddingLeft
paddingRight
opacity
function (filter) { "use strict"; return JSON.stringify(this, filter); }
When it got to the function here it fails since thats not a string but a function that was created using JSON's Object.prototype.toJSONString=function(filter)
I don't know why the function is in this list of css types. To fix it I replaced:
c=c.replace(bp,"-$1").toLowerCase();
with:
c=c.toString().replace(bp,"-$1").toLowerCase();
the toString changes the function to a string.
Then when I run the code it gets to the json function and tries to pass an object to json so I fixed it like this:
Object.prototype.parseJSON=function(filter){if((typeof filter) == 'object') return;return JSON.parse(this,filter);};
This checks if its an object instead of the expected string and returns nothing if thats the case.
I don't want to spend more time trying to figure out the root problem but this hack will make it work for now.

Neat asynchronous uploads

I am letting people upload multiple documents at a time to be added as revisions to existing documents. If the file name matches it shows them the revs that exist.
While they enter a description for the files the upload moves the files to a temp folder:
I move the files asynchronously using a hidden iFrame and a target on the form pointing to the name of the iFrame. The php thats echoed there includes js that will change a button from loading to Open for each file so they can open pdfs from the files temp location.
Without refreshing I upload the document and allow them to open it while its in a temp folder on the server. Then when they have finished adding a description for it then the file is moved to the real folder where all the other files are. The database has a hierarchical table that shows where in the virtual folders for these files are even though they are in the same folder in the physical server.

Sunday, August 14, 2011

XMP tags

I found out recently about a tag in html I didn't know about called XMP. They make it so all the code that you put in them is shown and the browser won't interprit them as html. Here is an example:

<p>testing a paragraph</p>

PHP Daemon runs out of file resources

http://gnuvince.wordpress.com/2008/10/28/php-wrong-for-long-running-processes-wrong-for-america/

<?php
$fd = fopen('/etc/passwd', 'r');
echo "$fdn";
fclose($fd);

$fd = fopen('/etc/fstab', 'r');
echo "$fdn";
fclose($fd);
?>


And this shows the usual:
$ php fds.php
Resource id #5
Resource id #6

Python doesn't have this problem though.

PHP is reusing the file descriptors but counting up its file resource. When it gets to 2^31-1 files and then goes into negative numbers and when it gets to 0 it crashes. How can the process use that many file handles in a few weeks time?

How do you reuse old file descriptors or resources after they are closed so you can run the php script forever?

Tuesday, August 2, 2011

Clearing multiple setTimeouts with js

http://garbageburrito.com/blog/entry/555/slideshow-clearing-all-javascript-timers
I was making timers and was having trouble clearing them since if you use the same variable to save each timer and you use clearTimeout(timer); it only clears the last timer and not all of them. You can't just call clearTimeout 3 times to clear them. So I found this col code to put setTimeouts into an array and clear them before you set more.

Make many timers and clear them any time.
<script>
timers = new Array();

function resetTimeouts() {
timers = new Array();
}
function clearTimeouts() {
for (var i= 0;i < timers.length; i++) {
clearTimeout(timers[i]);
}
//resetTimeouts();
timers = new Array();
}
//stop any timer
clearTimeouts();
timers.push(setTimeout("alert('timer here');", 2000))
</script>


Then if you make many timers and then you make more you want to clear all the old ones each time you make more so the old ones aren't triggered after new ones are made.

HDs prices have dropped

3TB Hard drives are now $130 on newegg.com
Thats the cheapest I have seen. This is 4.3 cents a GB and the cheapest I knew about before was 5cents a GB.