Archive for June, 2009|Monthly archive page

Regular Expressions (REX)

‘#<title>(.*?)</title>#’
‘|<title>([^<]*?)</title>|is’
‘#<body[^>]*>(.*)</body>#siU’
‘#(\.bmp|\.gif|\.jpg|\.jpeg|\.png)$#i’
‘#\.swf$#i’
‘/xcf|odg|gif|jpg|png|bmp/i’
‘/{([A-Za-z\-_]+)}/’
‘/<a[^>]+href=”([^"]*)”[^>]*>([^<]*)<\/a>/ui’
‘/; (120×160|240×280|240×320|320×320)\)/’

‘/opera/i’
‘/<category>(.+?)<\/category>/is’
“#href=\”(.*?)\”#s”
‘|[a-z0-9_.-]+@[a-z0-9_.-]+(?!.*<)|i’
“/youtube\.com\/watch/i”
“/username=\”([^\"]+)\”/i”
‘/^[a-zA-Z0-9]+$/’
“‘<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"']?\d+;[\s]+URL[\s]*=[\s]*([^\"\']*?)[\"\']?>’i”

“‘<\/?(FORM|INPUT|SELECT|TEXTAREA|(OPTION))[^<>]*>(?(2)(.*(?=<\/?(option|select)[^<>]*>[\r\n]*)|(?=[\r\n]*))|(?=[\r\n]*))’Usi”

‘/(\b)GLOBALS|_REQUEST|_SERVER|_ENV|_COOKIE|_GET|_POST|_FILES|_SESSION(\b)/i’
‘/<input type\=”hidden” name\=”([^"]+)”.*?value\=”([^"]*)”[^>]*>/si’
‘#[?&](p|page_id|attachment_id)=(\d+)#’
‘/<!–more(.*?)?–>/’

preg_match_all html tag

preg_match_all — Perform a global regular expression match
Searches subject for all matches to the regular expression given in pattern and puts them in matches in the order specified by flags.
After the first match is found, the subsequent searches are continued on from end of the last match.

$str = '<img src="glf.jpg" alt="sandeep" title="verma" />';
$str1 = '<input onclick="oAuth.logout()" type="button" value="Logout" />';

$alt = preg_match_all('/(src|alt|title)=("[^"]*")/i', $str, $matches);

print_r($matches);

$alt = preg_match_all('/(onclick|type|value)=("[^"]*")/i', $str1, $matches);

print_r($matches);

print_r() to file

function print_r_to_file($var, $file) {
    // writing response to external file
    $f = fopen($file, 'w');
    ob_start();
    print_r($var);
    $return = ob_get_contents();
    ob_end_clean();
    fwrite($f, $return);
    fclose($f);
}

IE6 position fixed

The css position on IE6 DIV tag could not fixed by “position: fixed”, the div flowing with page scroll…….

<style type="text/css">
<!--
#nav {
position:fixed;
border:#000 solid 2px;
list-style:none;
width:100%;
top:0px;
left:0px;
background:#FFFFFF;
padding:10px;
z-index:1;
}
-->
</style> 

<!--[if IE 6]>
<style type="text/css">
#nav {
position:absolute; /* position fixed for IE6 */
top:expression(0+((e=document.documentElement.scrollTop)?e:document.body.scrollTop)+'px');
left:expression(0+((e=document.documentElement.scrollLeft)?e:document.body.scrollLeft)+'px');
}
</style>
<![endif]-->

We can use this css with IE6 and All to fix a div position:

<div class="Light11 Red" id="nav"><strong>Javascript is disabled in your browser, please enable javascript to browse site properly...</strong></div>

XML – SVG

Why XML?
*XML is a standardized format
* Can be read and written by most languages
* XML is human-readable (if written well)
* XML is flexible
* Can be altered with style sheets
* Transformations between different types of XML
* e.g. HTML table to chart
* Supports records, lists, and trees
* Plain text; platform independent

Why XML … Not
* Verbose and redundant
* Can be difficult to read
* Inefficient to parse, store, and transmit
* Parsers must deal with arbitrary levels of nesting and errors
* No concept of data types
* Hierarchical not relational

SVG: Scalable Vector Graphics
#A vector-based graphics format in XML
#Graphics consist of lines, shapes, colors (not pixels)
#Can be viewed by most modern browsers
#XML format, like HTML
#Can use JavaScript for animation / interactivity
#CSS for style

SVG Example

SVG example
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg">
<circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="red"/>
<circle cx="200" cy="50" r="40" stroke="black" stroke-width="2" fill="green"/>
<circle cx="300" cy="50" r="40" stroke="black" stroke-width="2" fill="yellow"/>
</svg>

Output here:
traffic

PHP vs. Java/JSP

Java is…
* Strictly object oriented
* Strongly typed
* More clearly structured
* More complex
* JSP requires special server (Tomcat)

PHP is…
*Procedural (like C), with some objects
* Weakly typed, generally more flexible
* Generally faster to develop in
* Available on most web servers
* Easier to make messy code
* No namespaces
* Vague function definitions

GET vs POST

Form submission methods
#HTTP supports two methods for submitting form data#

GET
> Parameters are passed in URL string
> Parameters visible to user in browser window
> Easy to debug

POST
> Parameters included in body of HTTP request
> Not visible to user
> Can handle larger data items

Example:

GET and POST requests
GET /index.php?user=sandeepverma&message=Hello HTTP/1.1
Host: www.sv.com
User-Agent: Mozilla/4.0

POST /index.php HTTP/1.1
Host: www.sv.com
User-Agent: Mozilla/4.0
Content-Length: 24
Content-Type: application/x-www-form-urlencoded
user=sandeepverma&message=Hello

Protect file uploads via PHP

Check server-side MIME type of uploaded files

The PHP form variable $_FILES['file']['type'] does not return correct mime type, so we have to use some php functions like…….
> finfo_file()
> getimagesize()
> exif_imagetype()


if(version_compare(substr(PHP_VERSION,0,1),5) == -1)
{
$san	= finfo_open(FILEINFO_MIME);
$mime	= finfo_file($san,$FileName);
$tmpvar = explode(";", $mime);
finfo_close($san);
if($mime == "image/jpeg")
{
echo "this is jpeg";
}
else
{
echo "this is not jpeg";
}
}

One can use getID3 [http://getid3.sourceforge.net/] classes to get mime type other than images….

Don’t upload to a web accessible directory

Protect your directory to execute perticular file type by putting a .htacces file in directory

AddType text/plain .php .js .cgi

more help here:
http://www.askapache.com/htaccess/mod_rewrite-tips-and-tricks.html

SOAP vs REST

A set of functions that can be called remotely using HTTP
> Used by other programs and programmers
> Define functions and arguments
> Return data rather than a Web page

SOAP
REST
> Requires WSDL files to publish resources

> XML in request and response

> Developer needs to know the XML syntax for the service

> Uses Remote Procedure Calls (RPCs) over HTTP

> XML-wrapped RPC difficult to “sniff”

> Uses a URI to locate objects

> Passes method calls as GET parameters

> Uses well-known calls (GET, POST, PUT, DELETE)

> Uses plain HTTP

> Calls can be secured by the firewall or via certificates