March 17, 2011

sqlite pdo write lock problem solved

When using PHP and PDO to access an sqlite dababase, and when you do a "select *" using the $handle->query($sqlcmd), you must use "foreach" to read all the select results before you can issue the next "query" or "exec" call. Otherwise, the database will be locked.

March 4, 2011

Win32 applications GUI vs Console

This web page explains well the difference between a WIN32 GUI app vs. Console app.

March 3, 2011

php header redirect IE issue

In web development, a HTTP POST is often processed on the server ending with a header("Location:page.php") call, so that user can refresh their page without re-posting. This usually works well.

The only issue is IE. After posting or file upload, using the PHP header() call won't work with IE, although it works with Firefox. You will have to use the HTML direct instead.

February 9, 2011

Windows CE ssh server and client

Windows CE development is not as fun as Linux because of the lack of open source tools. However, once I am able to make ssh server and client running on it, it becomes better. Here is how to get ssh server and client running on windows CE 5 (ARM based):

1. Download CESSH at http://www.codeplex.com/wikipage?projectname=CESSH
    Download sshd_config at http://www.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=CESSH&DownloadId=3561
    Download file http://www.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=CESSH&DownloadId=3562

2. copy the files to your windows CE device, suppose "\ssh"
3. mkdir "\Hard Disk"
4. copy sshd_config "\Hard Disk"
5. mkdir "\NandFlash"
6. mkdir "\NandFlash\ssh"
7. copy ssh_host_dsa_key to "\NandFlash\ssh"
8.  run "adduser.exe root rootpass" to add the user "root" with password "rootpass"
9. copy SocketToFile.DLL to \windows
10. run "sshd.exe" 

To test it, get pocketputty
 (http://www.pocketputty.net/download.html)


If make this work over ActiveSync, use the remote port tunnel in putty.

Another great tool to have is netcat for CE: http://www.dr-bischoff.de/wince/Andreas_WINCE_stuff.html#netcat

Hope this helps.

February 4, 2011

Slick board and XML/SWF charts

SlickBoard and XML/SWF charts seem like very great products. Let me know if you have used it.

And also the google visualization tools which give you access to google finance like charts (http://code.google.com/apis/visualization/documentation/gallery/annotatedtimeline.html)

January 28, 2011

Free php web stat analyzer

1. TraceWatch
2. BBClone

Both GPL and Free.

January 27, 2011

git, cgit and mini_httpd

 git allows very easy local installation. We need a easy way to view the local git repository. mini_httpd is a great local web server to use, and git web app can either be "gitweb" or "cgit".

I tried "gitweb" but couldn't get it to see my projects. But I got "cgit" to work well. Here is how:

1. download and compile cgit. very simple, just follow the README file.
2. change cgit Makefile, "CGIT_CONFIG = /yourpath/cgitrc", then "touch cgit.c" and "make" again. We do this because by default cgitrc is located at /etc/ and we want it local.
3. Create your cgitrc file from the example file. I use something like this:

cache-size=0
css=/cgit.css
enable-index-links=1
enable-log-filecount=1
enable-log-linecount=1
max-stats=quarter
root-title=My git repository
root-desc=Tracking my project development
root-readme=about.html
snapshots=tar.gz
mimetype.git=image/git
mimetype.html=text/html
mimetype.jpg=image/jpeg
mimetype.jpeg=image/jpeg
mimetype.pdf=application/pdf
mimetype.png=image/png
mimetype.svg=image/svg+xml
repo.url=linux
repo.path=/opt/gittest/.git
repo.desc=My project
repo.owner=me@me.com
repo.readme=info/web/about.html
repo.snapshots=tar.gz
repo.enable-log-linecount=0
repo.max-stats=month

4. start minihttpd and your are in business. :-)

January 26, 2011

Vim non greedy regular expression search

* (0 or more) greedy matching
\+ (1 or more) greedy matching
\{-} (0 or more) non-greedy matching
\{-n,} (at least n) non-greedy matching
 
http://blog.vinceliu.com/2008/02/non-greedy-regular-expression-matching.html 

CSS Positioning explained

http://www.barelyfitz.com/screencast/html-training/css/positioning/

Simple truth:

1. Use positon:relative in the outer box and then positoin:absolute in the inner box. This is very effective.

2. When using position:absolute with background image, make sure you explicitly specify "width" and "height", otherwise it won't work.

CSS and box shadow (blurred shadow)

All major browsers except IE (IE9 may support it) supports the CSS3 box shadow. So you need the javascript to do some tricks for IE. Here is the link. http://www.hintzmann.dk/testcenter/js/jquery/boxshadow/

January 24, 2011

Use Linux to control power outlet via USB

Much of information on this page is based the web page at here

1. buy EcoStrip , and Linksys USB2HUB4 USB 2.0 hub. This hub supports power control. Internally it uses NEC chipset.

2. Download hub-ctrl.c, OR download it at this site

3. Use lsusb utilities with -v option, you can inspect 'Hub Descriptor', such like:
Hub Descriptor:
   [...]
     wHubCharacteristic 0x0089
       Per-port power switching
       Per-port overcurrent protection
       Port indicators
   [...]

4.compile hub-ctrl.c with -lusb (you need to install libusb-dev if you don't have it)

5. run "sudo ./hub-ctrl" to list the possible hub that supports power control
Bus 002 Device 003: ID 0409:0058 NEC Corp. HighSpeed Hub
Bus 002 Device 002: ID 8087:0020
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 8087:0020
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub 
 
6.From above, you can see that my Linksys hub is located at Bus 2 Device 3. So I use the following command to turn the ecoStrip (which is plugged into the Linksys Hub port 1) power off:

sudo ./hub-ctrl -b 2 -d 3 -P 1 
 
And the following command to turn it on

sudo ./hub-ctrl -b 2 -d 3 -P 1 -p 1

January 21, 2011

transfer or populate sqlite table to another table

INSERT INTO TABLE2 (COL1, COL2, COL3) SELECT COL1, COL4, COL7 FROM TABLE1

January 19, 2011

Use PHP to receive email

I use "sendmail". Other MTA should be similar.

1. Supposed you want php to receive emails destined to "support@example.com". Go to /etc/aliases and add a line:
     support: "|/usr/local/bin/getemail.php"

2. restart sendmail
3. go to /etc/smrsh, and create two symbolic link files:

 ln -s /usr/bin/php php
 ln -s /usr/local/bin/getemail.php getemail.php

These symbolic links basically tells sendmail you know these programs are safe for it to run

4. create your php script. A simple php script looks like this. Of course you should add your logic to it.
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
         $email .= fread($fd, 1024);
}
fclose($fd);

file_put_contents("/tmp/email.txt",$email);
die();

January 18, 2011

See which file is preventing you from mount read-only

In embedded systems, you can make your entire root file system read only (therefore preventing flash corruption) by doing:

mount -o remount,ro /

But sometimes this fails, because some files are being open as "writable". We need to find a way to identify these processes. lsof comes to rescue.
 
lsof +D / | awk $4~/w$/{print}

This commands list all open files recursively then uses an one-line awk script to find the lines that have files open for write. 

lsof is a really good friend. :-)

January 11, 2011

PHP PDF import and modification

There is a php library that allows importing a page from an existing PDF file, and then write data/graph on top of it. This essentially allows one to modify a pdf file.

The library is called FPDI (http://www.setasign.de/products/pdf-php-solutions/fpdi/about/). It uses the basic PDF library FPDF, or TCPDF (which is a fork of FPDF).

Seems useful. haven't tried it yet.

More: tried it yesterday and it works beautifully!! Use GSview32 to find the exact x/y of the place where you want to insert text/graph.

Another gem found.

January 10, 2011

https, TLS, SSL and multiple hosts

http://en.wikipedia.org/wiki/Server_Name_Indication

Unfortunately Windows XP with IE does not support this.

December 28, 2010

Use sqlite3 with codeigniter

Codeigniter is a small, fast, and well-documented web framework. However, currently (as of version 1.7.3), only sqlite2 is supported. I made codeigniter 1.7.3 work with sqlite3, including the scaffolding feature, which I really like.

Here is how to make codeigniter 1.7.3 work with sqlite3:

1. Download the php-based sqlite3 PDO driver for codeigniter. This driver is based on the driver in the codeigniter wiki, but fixed up so it works with v1.7.3 and with scaffolding.

Download the sqlite3 PDO driver (based on the wiki driver 0.2)

2. Edit system/database/DB_driver.php,  around line 831, add the 4 lines below starting with '+'.


foreach($query->result_array() as $row)
{
if (isset($row['COLUMN_NAME']))
{
$retval[] = $row['COLUMN_NAME'];
}
+ else if ($this->platform()=="pdo")
+ {
+ $retval[] = next($row);
+ }
else
{
$retval[] = current($row);
}
}


3. Create directory /pdo in /database/drivers and copy to this directory
  driver *.php files
4. Create SQLite3 database file, and put it to any directory.
  My database file is [APPPATH]/db/base.db
5. In application database config [APPPATH]/config/database.php  set next settings:
 
$db['default']['hostname'''; 
$db['default']['username''';
$db['default']['password''';
$db['default']['database''sqlite:'.APPPATH.'db/base.db';
$db['default']['dbdriver''pdo'

Enjoy.

December 27, 2010

vimdiff ignoring white space

I had been searching for a way to ignore white spaces while using vimdiff. Unfortunately, vimdiff -h yields the generic Vim help. I finally found that including the following line in vimrc solves the problem.
set diffopt+=iwhite
From the command line:
vimdiff -c 'set diffopt+=iwhite' ...
 

December 22, 2010

codeignitor removes index.php

Ok, try
<IfModule mod_rewrite.c>
    
RewriteEngine On
    RewriteBase 
/

    
RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond 
%{REQUEST_FILENAME} !-d

    RewriteRule 
^(.*)$ /index.php?/$1 [L] <-- do not forget the after index.php!
</
IfModule>

<
IfModule !mod_rewrite.c>
    
# If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.

    
ErrorDocument 404 /index.php</IfModule
in config.php set
$config['index_page'"";$config['uri_protocol']    "AUTO"
With this htaccess index.php-hiding works for me with PHP4 and PHP5 running PHP as a CGI

Corss Compile tcpdump for linux/mips

1. download libpcap (I use version 1.1.1 at the time of post) and tcpdump (v4.1.1 as of time of post)

2. unzip both directory under the same directory, such as download/libpcap-1.1.1 and download/tcpdump-4.1.1


Build libpcap:

3. cd libpcap-1.1.1 ;

4. vi configure; search for "linux version", and remove the entire section of "case" under "linux)" until "do we have the wireless extensions". This is so that ./configure does not try to detect linux (and fail). Our linux is fine.

5. CC=/YOUR-CROSS-COMPILER-PATH/mips-openwrt-linux-gcc ./configure --host=mips-linux  --with-pcap=linux
6.  make. after make is successful, you'll have a libpcap.a

Build tcpdump:

7. cd ../tcpdump-4.1.1

8 .vi configure; earch for "linux version", and remove the entire section of "case" under "linux*)" until ";; \n *)".

9. CC=/YOUR-CROSS-COMPILER-PATH/mips-openwrt-linux-gcc ./configure --host=mips-linux  --with-pcap=linux


10. vi Makefile; search for /usr/include and remove them; the Makefile by mistake include including files in the host system. Remove them.





11. make. you can strip the final tcpdump if you like. all done.