Wednesday, August 31, 2011

php unicode string

Have you ever wanted to express unicode code points in a php string

$string = "Hello World \u4f60\u597d\u4e16\u754c";
(which is...)
$string = "Hello World 你好世界";
Try this:
$string = ustring("Hello World \u4f60\u597d\u4e16\u754c");
function ustring($string)
{
return preg_replace_callback("/\\\\[Uu]([0-9A-Fa-f]{4})/",'matcheduchar', $str)."\n";
}
function matcheduchar($matches)
{
$num = hexdec($matches[1]);
if($num<=0x7F) return chr($num); if($num<=0x7FF) return chr(($num>>6)+192).chr(($num&63)+128);
if(0xd800<=$num && $num<=0xdfff) return '';//invalid block of utf8 if($num<=0xFFFF) return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128);
if($num<=0x10FFFF) return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128).chr(($num&63)+128);
return '';
}

Friday, May 20, 2011

Geany Find in Files Dialog

I opened the geany Find in Files Dialog and it kept defaulting to ISO-8859-1. It kept bugging me cause I wanted it to default to UTF-8

Turns out... in the source code of Geany 0.20 says this:
/* set the encoding of the current file */
if (doc != NULL)
    enc_idx = encodings_get_idx_from_charset(doc->encoding);
gtk_combo_box_set_active(GTK_COMBO_BOX(fif_dlg.encoding_combo), enc_idx);

So my problem was I had an ISO-8859-1 file open... then when I went todo CTRL-SHIFT-F, find in files, it defaulted to the charset of the file that was already open. All I had to do was change the encoding of my file (that was already open) to utf8 and the find in files defaulted correctly.

Wednesday, May 11, 2011

hg style file on linux

http://hgbook.red-bean.com/read/customizing-the-output-of-mercurial.html#id417978
http://www.jaharmi.com/2008/11/30/list_changed_files_in_a_mercurial_repository_with_a_custom_output_style
http://mercurial.808500.n3.nabble.com/How-to-get-a-list-of-changed-files-td808109.html

save this file:

http://mercurial.808500.n3.nabble.com/attachment/808110/0/map-cmdline.gward
to this folder:
/usr/share/mercurial/templates/

now you can do:
hg log -r tip --style gward -v

or setup your ~/.hgrc
[ui]
style = gward
[defaults]
log = -v

now you can do:
hg log -r tip

Thursday, February 17, 2011

Java Servlet 2.5 API javadocs for Tomcat 6

Oh my goodness, I am starting to dislike oracle. When I google for "java servlet 2.5 api docs"

The first link is:
* http://www.oracle.com/technetwork/java/javaee/servlet/index.html

and when I go to the page:
* http://tomcat.apache.org/tomcat-6.0-doc/index.html

it has a link to
* Servlet API Javadocs - The Servlet 2.5 API Javadocs.

But the link is a dead link that redirects to
* http://www.oracle.com/technetwork/java/javaee/servlet/index.html

ug.

Well I finally found out that javaee 5 uses servlet api 2.5 so this might work
http://download.oracle.com/javaee/5/api/javax/servlet/package-summary.html

But so might this:
Java Servlet Javadoc API http://download.oracle.com/docs/cd/E17802_01/products/products/servlet/2.5/docs/servlet-2_5-mr2/

Java Servlet In Ubuntu with Tomcat 6

user@desktop:~/Servlet$ sudo apt-get install tomcat6
user@desktop:~/Servlet$ sudo service tomcat6 start
user@desktop:~/Servlet$ sudo service tomcat6 restart
user@desktop:~/Servlet$ sudo service tomcat6 stop
user@desktop:~/Servlet$ sudo service tomcat6 start
user@desktop:~/Servlet$ echo "* used tutorial http://content.hccfl.edu/pollock/ajava/war/myservletwar.htm"
* used tutorial http://content.hccfl.edu/pollock/ajava/war/myservletwar.htm
user@desktop:~/Servlet$ vim SampleServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SampleServlet extends HttpServlet
{
  public void doPost ( HttpServletRequest req, HttpServletResponse res ) throws IOException, ServletException
  {
    doGet( req, res );
  }

  public void doGet ( HttpServletRequest req, HttpServletResponse res ) throws IOException, ServletException
  {
    res.setContentType( "text/html" ); // Can also use "text/plain" or others.
    PrintWriter out = res.getWriter();

    String addr = req.getRemoteAddr();

    // Create output (the response):
    out.println( "<HTML><HEAD><TITLE>SampleServlet in     myServletWar</TITLE></HEAD>" );
    out.println( "<BODY><H1 ALIGN=\"CENTER\">" );
    out.println( "Hello " + addr + ", from SampleServlet in myServletWar!" );
    out.println( "</H1></BODY></HTML>" );
    out.close();
  }
}

user@desktop:~/Servlet$ mkdir myServletWar
user@desktop:~/Servlet$ mkdir myServletWar/META-INF
user@desktop:~/Servlet$ mkdir myServletWar/WEB-INF
user@desktop:~/Servlet$ mkdir myServletWar/WEB-INF/classes
user@desktop:~/Servlet$ vim myServletWar/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<display-name>myServletWar, a first Web Application</display-name>
<description>
This is a simple web application containing a single servlet
of the "Hello, World" variety.
</description>
<servlet>
<servlet-name>myHello</servlet-name>
<servlet-class>SampleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myHello</servlet-name>
<url-pattern>/sample</url-pattern>
</servlet-mapping>
</web-app>

user@desktop:~/Servlet$ javac -cp ".:/usr/share/java/servlet-api-2.5.jar" -d myServletWar/WEB-INF/classes/ SampleServlet.java
user@desktop:~/Servlet$ find
.
./myServletWar
./myServletWar/WEB-INF
./myServletWar/WEB-INF/classes
./myServletWar/WEB-INF/classes/SampleServlet.class
./myServletWar/WEB-INF/web.xml
./myServletWar/META-INF
./SampleServlet.java
user@desktop:~/Servlet$ rm -f myServletWar.war
user@desktop:~/Servlet$ jar -cvf myServletWar.war -C myServletWar/ .
user@desktop:~/Servlet$ sudo cp myServletWar.war /var/lib/tomcat6/webapps/
user@desktop:~/Servlet$ curl http://127.0.0.1:8080/myServletWar/sample

Wednesday, February 16, 2011

javac classpath - semicolon delimited

after installing tomcat 6 for example:
javac -cp ".:/usr/share/java/servlet-api-2.5.jar" SampleServlet.java

vim turn off indenting


:set noai
:set ai



:set paste
:set nopaste

Wednesday, January 19, 2011

install php from source on ubuntu 10.10

http://www.php.net/manual/en/install.unix.apache2.php
http://httpd.apache.org/download.cgi
http://us2.php.net/downloads.php
sudo apt-get install g++
sudo apt-get install libssl-dev
tar -xjvf httpd-2.2.17.tar.bz2
cd httpd-2.2.17/
./configure --enable-so
sudo make
sudo make install
sudo /usr/local/apache2/bin/apachectl start
cd ..
tar -xjvf php-5.3.5.tar.bz2
cd php-5.3.5/
sudo apt-get install libxml2-dev
sudo apt-get install libmysqlclient-dev
./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql
sudo make
sudo make install
cd ..

Wednesday, January 12, 2011

Fetch SSL Certificate - Public Key Details

$url = 'www.digicert.com'; // For example
$context = stream_context_create();
$res = stream_context_set_option($context, 'ssl', 'capture_peer_cert', true);
$res = stream_context_set_option($context, 'ssl', 'verify_host', true);
if ($socket = stream_socket_client("ssl://$url:443/", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context)) {
if ($options = stream_context_get_options($context)) {
if (isset($options['ssl']) && isset($options['ssl']['peer_certificate'])) {
$x509_resource = $options['ssl']['peer_certificate'];
$cert_arr = openssl_x509_parse($x509_resource);
openssl_x509_export($x509_resource,$x509_string);

$public_key_res = openssl_pkey_get_public($x509_string);
$public_key_arr = openssl_pkey_get_details($public_key_res);

print_r($public_key_arr);
}
}
}

Monday, January 10, 2011

preg_match reference links

http://us.php.net/manual/en/reference.pcre.pattern.modifiers.php
http://us.php.net/manual/en/regexp.reference.meta.php
http://us.php.net/manual/en/regexp.reference.escape.php
http://us.php.net/manual/en/regexp.reference.unicode.php

Thursday, December 30, 2010

Calculate sha1 thumbprint of ssl certificate

<?php
function sha1_thumbprint_pem($pem_file_contents)
{
    $file $pem_file_contents;
    $file preg_replace('/\-+BEGIN CERTIFICATE\-+/','',$file);
    $file preg_replace('/\-+END CERTIFICATE\-+/','',$file);
    $file trim($file);
    $file str_replacearray("\n\r","\n","\r"), ''$file);
    $bin base64_decode($file);
    return sha1($bin);
}
?>

Tuesday, December 14, 2010

preg_replace/preg_match utf8

preg_replace('/([^a-z\x{00C0}-\x{02AF}\x{0380}-\x{FFFF}\.\, ]|[\.\,][\.\,]+)/iu', '', $str);//allows most utf8

Friday, October 15, 2010

How to show the list of CA Root Certificates in Google Chrome in Ubuntu Linux

sudo apt-get install libnss3-tools
certutil -d sql:$HOME/.pki/nssdb -L -h "Builtin Object Token"

Monday, September 27, 2010

PHP CURLOPT_CERTINFO

sample php code using CURLOPT_CERTINFO, the php equivalent of CURLINFO_CERTINFO which is only available in php >=5.3.2 .
<?php
if($fp = tmpfile())
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"https://www.digicert.com/");
    curl_setopt($ch, CURLOPT_STDERR, $fp);
    curl_setopt($ch, CURLOPT_CERTINFO, 1);//certinfo goes to STDERR when CURLOPT_VERBOSE is set
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
    curl_setopt($ch, CURLOPT_SSLVERSION,3);
    $result = curl_exec($ch);
    curl_errno($ch)==0 or die("Error:" . curl_errno($ch) . " Message:" . curl_error($ch));
    fseek($fp, 0);//rewind
    $str='';
    while(strlen($str.=fread($fp,8192))==8192);
    echo $str;
    fclose($fp);
}
?>

Wednesday, August 25, 2010

nice sanitized mysql insert sample php

$row = array();
$row["firstname"]="Billy";
$row["lastname"]="O'Leary";
$row["create_date"]=date("Y-m-d H:i:s");
$row["status"]="1";
$escaped = array_map(create_function('$a', 'return is_null($a) ? NULL : mysql_real_escape_string($a);'),$row);
$quoted = array_map(create_function('$a', 'return is_null($a) ? "NULL" : "\x27".$a."\x27";'),$escaped);
echo "INSERT INTO `employees`(`".implode("`,`",array_keys($escaped))."`) VALUES(".implode(",",array_values($quoted)).");"."\n";

it generates the sanitized insert query
INSERT INTO `employees`(`firstname`,`lastname`,`create_date`,`status`) VALUES('Billy','O\'Leary','2010-08-25 15:32:37','1');

Sunday, July 18, 2010

sed strip unicode out of file

e2 80 8b is the hex utf8 for unicode code point U+200b
sed -e "s/\xe2\x80\x8b//g" input.u8 >output.u8

Tuesday, July 13, 2010

mysql union

mysql> (select 'a') union (select 'a');
+---+
| a |
+---+
| a |
+---+
1 row in set (0.00 sec)

mysql> (select 'a') union all (select 'a');
+---+
| a |
+---+
| a |
| a |
+---+
2 rows in set (0.00 sec)

mysql> (select 'a') union distinct (select 'a');
+---+
| a |
+---+
| a |
+---+
1 rows in set (0.00 sec)

Wednesday, June 30, 2010

maximum upload size in php


<?php
function max_upload_size()
{
    preg_match('/^([0-9]+)([PTGMK]?)$/i'strtoupperini_get('post_max_size') ), $p );
    preg_match('/^([0-9]+)([PTGMK]?)$/i'strtoupperini_get('upload_max_filesize') ), $u );
    $arr array('P'=>50,'T'=>40,'G'=>30,'M'=>20,'K'=>10);
    return min(  $p[1] * pow(2,$arr[$p[2]]), $u[1] * pow(2,$arr[$u[2]]) )/(1024*1024)."MB";
}
?>