Monday, July 9, 2012

Auto highlight text inside pre tags using jQuery

We use pre tags to display the embed codes and short URLs. We use pre tags because it formats the text inside the tags as we insert it, which is ideal for displaying code in particular. The thing is, people like to copy this code and they have to go to the awful bother of selecting all the text manually if they wish to copy the code or URL.

jQuery hack to auto select all the text inside the pre tags. The only complication with this appears to be a cross platform solution as each browser appears to have their own way of selecting text.

Internet Explorer uses createTextRange.
Opera and Firefox use createRange.
Safari uses DOMSelection.
So with a pre tag like (just click anywhere in the code)…


jQuery( document ).ready(function() {	
	jQuery( 'pre.code' ).click( function() {
		var refNode = $( this )[0];
		if ( $.browser.msie ) {
			var range = document.body.createTextRange();
			range.moveToElementText( refNode );
			range.select();
		} else if ( $.browser.mozilla || $.browser.opera ) {
			var selection = window.getSelection();
			var range = document.createRange();
			range.selectNodeContents( refNode );
			selection.removeAllRanges();
			selection.addRange( range );
		} else if ( $.browser.safari ) {
			var selection = window.getSelection();
			selection.setBaseAndExtent( refNode, 0, refNode, 1 );
		}
	} );
 	} );


here goes the code :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script>
jQuery( document ).ready(function() { jQuery( 'pre.code' ).click( function() {
var refNode = $( this )[0];
if ( $.browser.msie ) {
var range = document.body.createTextRange();
range.moveToElementText( refNode );
range.select();
} else if ( $.browser.mozilla || $.browser.opera ) {
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents( refNode );
selection.removeAllRanges();
selection.addRange( range );
} else if ( $.browser.safari ) {
var selection = window.getSelection();
selection.setBaseAndExtent( refNode, 0, refNode, 1 );
}
} );
  } );
</script>
</head>
<body>
<pre class='code'>jQuery( document ).ready(function() { jQuery( 'pre.code' ).click( function() {
var refNode = $( this )[0];
if ( $.browser.msie ) {
var range = document.body.createTextRange();
range.moveToElementText( refNode );
range.select();
} else if ( $.browser.mozilla || $.browser.opera ) {
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents( refNode );
selection.removeAllRanges();
selection.addRange( range );
} else if ( $.browser.safari ) {
var selection = window.getSelection();
selection.setBaseAndExtent( refNode, 0, refNode, 1 );
}
} );
  } );
</pre>
</body>
</html>

Wednesday, July 4, 2012

post a file using PHP to remote server without ftp


Recently I was working for a product where i have to post a file to a remote server where remote server dont allow outbout FTP.
And in this process i found a couple of methods to post file to server;

my required file was in Xml and have to post that xml to remote server where I can fetch that data
Its quite simple and easy method.

here are my findings

local file name : postfile.php
remote file name : receivexml.php

code for postfile.php


$thistext="<?xml version="1.0"?>
        <SERVICES>
          <serv1234>
                <short>1234</short>
                <isuser>0</isuser>
                <keyword>keyword</keyword>
                <table>wltable</table>
                <expiry>
                        <expiry_status>1</expiry_status>
                        <expiry_startdate>04-JUL-12</expiry_startdate>
                        <expiry_enddate>27-JUL-12</expiry_enddate>
                        <expiry_message>Expiry Message</expiry_message>
                </expiry>
             
          </serv1234>
        </SERVICES>";



$ch = curl_init('http://rem.ote.server.ip/receivexml.php');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $thistext);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/xml',
'Content-Length: ' . strlen($thistext)) );                                                                                                                  
$result = curl_exec($ch);
print_r($result);
curl_close($ch);

code for  receivexml.php


echo "test xml";
if ( $_SERVER['REQUEST_METHOD'] === 'POST' ){
    $postText = file_get_contents('php://input');
}
//print_r($postText);
$shortcode='';
$xml = new SimpleXMLElement($postText);
$result = $xml->xpath('/SERVICES/ serv1234/short');
while(list( , $node) = each($result)) {
    echo $short=$node;
}
$myFile = "/directory/path/to/xml/".$short.".xml";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData=$postText;
fwrite($fh, $stringData);
fclose($fh);
?>

in receivexml.php
  1. received the file and stored in  $postText
  2. parsed the xml data with xpath
  3. set the file name to a tag value in this case "short"
  4. write data to file.