While creating Jmail I needed to input a multiline argument into a javascript function. It seems it can't be done!
I had to replace chr(13) + chr(10) with '$nl$' (any code would do) so the argument did not include line breaks, and then replace '$nl$' with chr(13) + chr(10) in the javascript function.
For the first bit I used
str_replace(chr(13).chr(10), '$nl$', $row['body']);
in php. If you want to see the code for the last bit, just go to the link above and look at the source.
Thursday, October 04, 2007
Tuesday, June 26, 2007
Comparing thousand of file names
This week I am moving all my files from one server to another. I use Filezilla to download them to my machine, and will later upload them to the new server. Unfortunately, the connection gets broken from time to time and I do not remember which folders have been downloaded and where I broke off. What to do?
First I looked for existing solutions and found Beyond Compare. I spent half an hour trying to figure it out, but gave up. It was probably too advanced for me, I just wanted to check if all the files had been downloaded. I couldn't care less about file size, date stamp, crc verification, ...
So, I wrote my own php script. Found code that goes through all the files in all sub folders of a given folder and adds up their sizes. Not what I wanted, but easy enough to edit to my needs. The code used stack (array) and not recursion to get to all the folders. It used array_shift(array) that pulls an element off the stack.
I ran it on the server and saved the paths to a mysql table. Then I exported the table, imported it into my local mysql and edited the code to fill a table with the downloaded paths. Finally I ran a query to show which files were missing either locally on the server.
The server table had 8190 records and the local one 8090. After having indexed the path fields the query ran fast and displayed 297 records:
server local
dirsize.php NULL
pmachinefree/1.jpg NULL
pmachinefree/2.jpg NULL
pmachinefree/3.jpg NULL
pmachinefree/4.jpg NULL
---
NULL pmachinefree/images/uploads/påske.jpg
NULL pmachinefree/images/uploads/påske22.jpg
First I looked for existing solutions and found Beyond Compare. I spent half an hour trying to figure it out, but gave up. It was probably too advanced for me, I just wanted to check if all the files had been downloaded. I couldn't care less about file size, date stamp, crc verification, ...
So, I wrote my own php script. Found code that goes through all the files in all sub folders of a given folder and adds up their sizes. Not what I wanted, but easy enough to edit to my needs. The code used stack (array) and not recursion to get to all the folders. It used array_shift(array) that pulls an element off the stack.
I ran it on the server and saved the paths to a mysql table. Then I exported the table, imported it into my local mysql and edited the code to fill a table with the downloaded paths. Finally I ran a query to show which files were missing either locally on the server.
The server table had 8190 records and the local one 8090. After having indexed the path fields the query ran fast and displayed 297 records:
server local
dirsize.php NULL
pmachinefree/1.jpg NULL
pmachinefree/2.jpg NULL
pmachinefree/3.jpg NULL
pmachinefree/4.jpg NULL
---
NULL pmachinefree/images/uploads/påske.jpg
NULL pmachinefree/images/uploads/påske22.jpg
Sunday, May 06, 2007
vbscript and javascript
vbscript is bad since it will only run in Internet Explorer. vbscript is good because it runs from the command prompt with csript progra.vbs.
javascript is good since it runs in any browser. javascript is bad since it won't run from the command prompt.
The last two days I have been working on feeding pgn files to a chess viewer. See here and here.
I first wrote a program for converting Yahoo Games chess game recording to pgn chess recording using vbscript. Why? Since I wanted to refresh my vbscript programming skills and because I wanted to create a modify a file something I did a bit of a year ago when I worked as a developer in a bank.
When I had published the vbscript program on the web page I thought it might be kind of fun to see the translation live using two text boxes and javascript.
So, the last few hours I have translated from vbscript to javascript. Quite a fun exercise!
If you are interested in checking out my translation, here are the two files: vbscript, javascript.
Who came first and why are they so different? Wikipedia reveals little on the matter: vbscript or javascript. By the way, the new guy in town is called Ajax.
This is what I thought happened:
Q: We have this meeting to come up with a scripting language. On my left a Netscape representative and on my right a Microsoft representative. Let's start with something simple. What is the smallest natural number?
N: That must be 0.
M: I hate to say this, but you are wrong. Everyone knows that 1 is the smallest natural number.
Q: So in an array called arr what is arr(1)?
M: The first element in the array.
N: The second element in the array. And it should be written arr[1] as it is not a function.
Q: OK. Now most people believe that 4 is different from 5. How should this be expressed?
M: 4 <> 5. If 4 is less than 5 or greater than 5, it is obviously different from 5.
N: 4 != 5.
Q: Let's move on. A block of code, how should that be indicated?
N: "{" at the start and "}" at the end.
M: Depends on the block. One example: "if" at the start and "end if" at the end.
Q: How do I find the third character in a string called board?
M: mid(board, 3, 1).
N: board.substring(2, 3).
Q: Quite! And the ascii value of that character?
M: asc(mid(board, 3, 1).
N: board.substring(2, 3).charCodeAt(0).
Q: And if that character is "s"?
M: instr(mid(board, 3, 1), "s") > 0.
N: board.substring(2, 3).indexOf("s") != -1
...
Here is a page that does some code comparison between the two languages.
javascript is good since it runs in any browser. javascript is bad since it won't run from the command prompt.
The last two days I have been working on feeding pgn files to a chess viewer. See here and here.
I first wrote a program for converting Yahoo Games chess game recording to pgn chess recording using vbscript. Why? Since I wanted to refresh my vbscript programming skills and because I wanted to create a modify a file something I did a bit of a year ago when I worked as a developer in a bank.
When I had published the vbscript program on the web page I thought it might be kind of fun to see the translation live using two text boxes and javascript.
So, the last few hours I have translated from vbscript to javascript. Quite a fun exercise!
If you are interested in checking out my translation, here are the two files: vbscript, javascript.
Who came first and why are they so different? Wikipedia reveals little on the matter: vbscript or javascript. By the way, the new guy in town is called Ajax.
This is what I thought happened:
Q: We have this meeting to come up with a scripting language. On my left a Netscape representative and on my right a Microsoft representative. Let's start with something simple. What is the smallest natural number?
N: That must be 0.
M: I hate to say this, but you are wrong. Everyone knows that 1 is the smallest natural number.
Q: So in an array called arr what is arr(1)?
M: The first element in the array.
N: The second element in the array. And it should be written arr[1] as it is not a function.
Q: OK. Now most people believe that 4 is different from 5. How should this be expressed?
M: 4 <> 5. If 4 is less than 5 or greater than 5, it is obviously different from 5.
N: 4 != 5.
Q: Let's move on. A block of code, how should that be indicated?
N: "{" at the start and "}" at the end.
M: Depends on the block. One example: "if" at the start and "end if" at the end.
Q: How do I find the third character in a string called board?
M: mid(board, 3, 1).
N: board.substring(2, 3).
Q: Quite! And the ascii value of that character?
M: asc(mid(board, 3, 1).
N: board.substring(2, 3).charCodeAt(0).
Q: And if that character is "s"?
M: instr(mid(board, 3, 1), "s") > 0.
N: board.substring(2, 3).indexOf("s") != -1
...
Here is a page that does some code comparison between the two languages.
Friday, April 27, 2007
Local date and time
When I store the present date and time on the server what is stored is the server's (in Hong Kong, California, ...) local date and time.
However, I want the user to see the local date and time where he is located when the data is extracted from the server's database.
With javascript one may find the local offset. To feed that information back into a php file can be done as shown below.
A page is called with $arg arguments, if the local offset has not been already calculated (if (!isset($p_offset))) then write a javascript that will run and call the page itself (location.href = ...) with the arguments as before plus one more argument called offset.

To try the code, click http://iwantyourquestion.com/test.php?abc=2&def=cat
The seond part takes place when you extract datetime information from the server stored in its local time.

To try the code, click http://iwantyourquestion.com/test2.php
However, I want the user to see the local date and time where he is located when the data is extracted from the server's database.
With javascript one may find the local offset. To feed that information back into a php file can be done as shown below.
A page is called with $arg arguments, if the local offset has not been already calculated (if (!isset($p_offset))) then write a javascript that will run and call the page itself (location.href = ...) with the arguments as before plus one more argument called offset.

To try the code, click http://iwantyourquestion.com/test.php?abc=2&def=cat
The seond part takes place when you extract datetime information from the server stored in its local time.

To try the code, click http://iwantyourquestion.com/test2.php
Wednesday, April 25, 2007
Cool tooltips
The day started with J-Walk pointing to A Periodic Table which led to overLIB as I wanted to know how the tooltips were made.
flickr.com has a different approach it seems.

I posted a question to the overLIB group.
Untill that is answered, I will follow a simplified version of the flickr approach. It looks like this:
flickr.com has a different approach it seems.

I posted a question to the overLIB group.
Untill that is answered, I will follow a simplified version of the flickr approach. It looks like this:
Monday, April 16, 2007
How naiive I am!
I wanted to title this piece "How naiive I have been!", but I rather play it safe. :)
I am creating a web site using mysql, php, and javascript at http://iwantyourquestion.com.
For easy and fast validation I require the user to have javascript turned on, and testing seemed to indicate that everything was well.
Then a thought struck me. What if the user decided to be mean?
Let me explain. On a page I have a link like .../ask_main.php?id=24&statusid=3 where id is the question id and statusid indicated if the question has been asked, answered, or closed. Instead of clicking the link, the user may copy it to the address bar and edit the parameters!
What if he changed the id to 25? Well, he might then see a question that was not his. What if he changed the statusid to 2. Then he might reply to an answer that has not been given creating nonsense.
The remedy. When the user goes to the page, in the intended or unintended fashion, I test if the question id belongs to the current user. If it does not I say 'Access denied.' I removed the statusid parameter and read it from the tables instead after the user clicked. That way it can not be manipulated.
That was a problem with a direct link with parameters. Now let's look at what happens when forms are submitted. Well, if they are submitted the normal way, by clicking the submit button, my javascript code does the validation. But what if the user types in the address bar a call to the page given in the code action=... in the form tag?
Remedy. I did this server validation:
// check that all parameters are sent
if (!isset($p_subject) or !isset($p_question) or !isset($p_why) or !isset($p_tried) or !isset($p_timetriedid)) {die('Access denied.');}
// check that all parameters are not empty
if ($p_subject=='' or $p_question=='' or $p_why=='' or $p_tried=='' or $p_timetriedid=='') {die('Access denied.');}
That way, at least the mysql queries won't produce errors. I could have taken the step to send the user back to the form page, in case his Javascript is off, but for now I request it to be on and tell the user if it is not.
As a further example of my ignorance I have to add that I did not know that php had error catching facilities and that not using them might produce a security risk.
I used the function is_int() in my code, but forgot the fist part of the function name and wrote only int(). That produced this runtime error:
Fatal error: Call to undefined function: int() in /xxx/yyy/zzz-... on line 15
It reveals where the home folder is, my username on the server, and the domain. All food for hackers.
http://www.devpapers.com/article/270 and http://www.phpbuilder.com/columns/starkey20020930.php3?print_mode=1 gave me some advice I am following now.
Conclusion: If it doesn't work the first time, you must be a programmer.
I am creating a web site using mysql, php, and javascript at http://iwantyourquestion.com.
For easy and fast validation I require the user to have javascript turned on, and testing seemed to indicate that everything was well.
Then a thought struck me. What if the user decided to be mean?
Let me explain. On a page I have a link like .../ask_main.php?id=24&statusid=3 where id is the question id and statusid indicated if the question has been asked, answered, or closed. Instead of clicking the link, the user may copy it to the address bar and edit the parameters!
What if he changed the id to 25? Well, he might then see a question that was not his. What if he changed the statusid to 2. Then he might reply to an answer that has not been given creating nonsense.
The remedy. When the user goes to the page, in the intended or unintended fashion, I test if the question id belongs to the current user. If it does not I say 'Access denied.' I removed the statusid parameter and read it from the tables instead after the user clicked. That way it can not be manipulated.
That was a problem with a direct link with parameters. Now let's look at what happens when forms are submitted. Well, if they are submitted the normal way, by clicking the submit button, my javascript code does the validation. But what if the user types in the address bar a call to the page given in the code action=... in the form tag?
Remedy. I did this server validation:
// check that all parameters are sent
if (!isset($p_subject) or !isset($p_question) or !isset($p_why) or !isset($p_tried) or !isset($p_timetriedid)) {die('Access denied.');}
// check that all parameters are not empty
if ($p_subject=='' or $p_question=='' or $p_why=='' or $p_tried=='' or $p_timetriedid=='') {die('Access denied.');}
That way, at least the mysql queries won't produce errors. I could have taken the step to send the user back to the form page, in case his Javascript is off, but for now I request it to be on and tell the user if it is not.
As a further example of my ignorance I have to add that I did not know that php had error catching facilities and that not using them might produce a security risk.
I used the function is_int() in my code, but forgot the fist part of the function name and wrote only int(). That produced this runtime error:
Fatal error: Call to undefined function: int() in /xxx/yyy/zzz-... on line 15
It reveals where the home folder is, my username on the server, and the domain. All food for hackers.
http://www.devpapers.com/article/270 and http://www.phpbuilder.com/columns/starkey20020930.php3?print_mode=1 gave me some advice I am following now.
Conclusion: If it doesn't work the first time, you must be a programmer.
Wednesday, April 11, 2007
Borders that are not borders are the borders I like
The other day I was surprised to see that the border that looked nice in Firefox 2, looked awful in IE6, and created havoc in IE7.
Firefox:
IE6:
IE7: (The Add buttons are partially hidden.)
What to do? Create different css tags for different browsers? No way!
I found some nice borders at Sure Support:
It took me quite a while to find out how they created the border. It looked nice in Firefox and in IE!
The border around every cell is created in a sneaky way:
1. Set the table background to gray.
2. Set every cell background to white.
3. Set table cell spacing to "1" and border to "0"
Code:
Firefox:
IE6:
IE7: (The Add buttons are partially hidden.)
What to do? Create different css tags for different browsers? No way!I found some nice borders at Sure Support:
It took me quite a while to find out how they created the border. It looked nice in Firefox and in IE!The border around every cell is created in a sneaky way:
1. Set the table background to gray.
2. Set every cell background to white.
3. Set table cell spacing to "1" and border to "0"
Code:
Saturday, March 24, 2007
Elseif
I like to optimise code after it is working to understand it better and therefore being able to maintain it more easily.
Yesterday, I copied some php code from PayPal's site. I noticed that in an if elseif endif there were statements in common in the if part and the elseif part. Naturally, I moved the statements to after the endif.
The result: thousands of emails from the program PayPal calls on my server when I make a payment (sandbox or otherwise)!
Why? Because the statements, that send an email, now were executed even when the if and the elseif conditions failed. :)
Conclusion: don't treat if elseif endif as if else endif!
Yesterday, I copied some php code from PayPal's site. I noticed that in an if elseif endif there were statements in common in the if part and the elseif part. Naturally, I moved the statements to after the endif.
The result: thousands of emails from the program PayPal calls on my server when I make a payment (sandbox or otherwise)!
Why? Because the statements, that send an email, now were executed even when the if and the elseif conditions failed. :)
Conclusion: don't treat if elseif endif as if else endif!
Subscribe to:
Posts (Atom)