Thursday, March 12, 2009

A simple Pagination function

Following function can be used for pagination blocks. You need to call this function, and print the return string. The return string format is << <> >>
i.e. first page link, previous page link, page numbers link, next page link, last page link.
You need to add javascript function called paginate(page_no) which takes page no as the argument & in turns do AJAX call for fetching the next records.
Argument list for pagination function:
  1. $current_page: current page number
  2. $cnt: total records
  3. $limit: No of records to be displayed

function pagination($current_page,$cnt,$limit){
$last_page = ceil($cnt/$limit);
$str = "";
$first_pg = "";
$prev_pg = "";
$next = "";
$last = "";

if($last_page == 1){
$first_pg = " << ";
$prev_pg = " < ";
}else{
if($current_page > 1){
$first_pg = "<a href=\"javascript:void(0)\" onClick=\"paginate(1)\"> << </a>";
$prev_pg = "
<a href=\"javascript:void(0)\" onClick=\"paginate($current_page-1)\"> < </a>";
}
for($i=1;$i<=$last_page;$i++){
if($i == $current_page)
$str .= $i." | ";
else
$str .= "<a href=\"javascript:void(0)\" onClick=\"paginate($i)\"><u>".$i."</u></a> | ";
}
$str = substr($str,0,strlen($str)-2);


if($current_page != $last_page){
$next = "<a href=\"javascript:void(0)\" onClick=\"paginate($current_page+1)\"> > </a>";
$last = "
<a href=\"javascript:void(0)\" onClick=\"paginate($last_page)\"> >> </a>";
}
if($current_page == $last_page){
$next = " > ";
$last = " >> ";
}

}
$page_string = $first_pg.$prev_pg.$str.$next.$last;
return $page_string;
}



Truncating the text

In many places the description or text needs to be truncated. Following function can be used to truncate the text in such a way that the words are not half chopped up.

Argument List:
  1. $mystring: The text
  2. $limit: length of the string upto which string is to be truncate.
  3. $pad: The string which shows continuation. Default it takes 3 dots (...) .


function truncate_text($mystring, $limit = 153,$pad ="..."){
if(strlen($mystring) < $limit)
return $mystring;
$rstring = substr($mystring,0,$limit);
$no_of_chars = strrpos($rstring," ");
if($no_of_chars < $limit)
$rstring = substr($mystring,0,$no_of_chars).$pad;
return $rstring;
}

Find the Age?

In php, you can use the following script to find the age.

$age = floor((time() - strtotime($dob))/31556926);

where $dob is date in any format.

Tuesday, January 27, 2009

User defined xml reponse in drupal 6 webservices

XMLRPC server gives xml reponse in following way.

<methodResponse>
  <fault>
   <value>
    <struct>
     <member>
      <name></name>
      <value><int></int></value>
     </member>
     <member>
      <name></name>
      <value><string></string></value>
     </member>
    </struct>
   </value>
  </fault>
</methodResponse>



But If you want to use your own xml format for response, then edit includes/xmlrpcs.inc

Comment following code at line number 77
$xml = xmlrpc_value_get_xml($r);

and while returning the value, return response in xml format. For example

function myservices_test($name){
  $response = "<root>
    <name>".$name."</name>
</root>";


  return $response;
}

Request webservices in drupal 6



1. Following is the xml format for sending the request

$xmlrequest= "<?xml version='1.0'?>
<methodCall>
  <methodName>myservices.test</methodName>
    <params>
      <param>
        <value>
          <string></string>
          <int></int>
        </value>
      </param>
    </params>
  </methodCall>";

$ch = curl_init('http://localhost/drupal/services/xmlrpc');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $xmlrequest);
$result = curl_exec ($ch);
echo $result;
curl_close ($ch);

Save the file and execute in the web browser.
Note:
2. methodname will be the name of method in your service.
3. If you want to pass parameters then send it in following format

You should always maintain the order, as the values will be read in same order at the server side.

Creating your own Webservices with drupal 6

1. Download services module from http://drupal.org/project/Services

2. Unzip the service module directory and copy inside sites/all/modules directory

3. Now Enable the module, for that you need to open web browser => navigate to your drupal website => Goto Site building => Modules => List => Enable services and XMLRPC Server => save configuration

4. Goto Site Building => Services => Setings => Uncheck Use keys & Use sessid boxes => save configuration

All settings are done. Now time to write your own services.

1. Create a directory for your service inside sites/all/modules directory, say myservices.

2. Here you need to create 3 files:
i) myservices.inc
ii) myservices.module
iii) myservices.info


3. Open myservices.info and write following lines
; $Id: myservices.info,v 1.5 2009/01/20 18:20:21 sheetal Exp $
name = My services
description = Expose basic services.
package = Services - service
version = VERSION
dependencies[] = services
core = 6.x

; Information added by drupal.org packaging script on 2008-12-11
version = "6.8"
project = "drupal"
datestamp = "1229018427"


4. Now open myservices.module
You should know about drupal module development for writing the webservices.
For example, create a function myservices_service().
function myservices_service(){
return array(
array(
'#method' => 'myservices.test',
'#callback' => 'myservices_test',
'#file' => array('file' => 'inc', 'module' => 'myservices'),
'#args' => array(
array(
'#name' => 'your_name',
'#type' => 'string',
'#description' => t('A valid name.'),
)
)
};



5. Now open myservices.inc. This file will contain the actual code
function myservices_test($name){
$response = "Welcome " . $name;
//your code will come here
return $response;
}


6. Everything is completed. Test your webservice by going to Site Building => Services => Browse
Here you will see the list of services which you have created, i.e. myservices.test
You will also see the XMLRPC server.

7. Now click on the services, you will see the output.

Saturday, January 17, 2009

Validate remote url using javascript

1. Read remote url.
var url ='<!--#echo var="REMOTE_HOST"-->';

2. Enter list of additional url
var additional_url = ["http://www.abc.com"];

3. Create a regular expression string.
var handleurl = additional_url.join("|");
handleurl = new RegExp(handleurl, "i");

4. Check condition whether remote url is in list
if (url.search(handleurl)!=-1)
alert("done")
else
alert("Unauthorized access");

Validate user IP address using javascript

1. Read user IP address.
var ip = '<!--#echo var="REMOTE_ADDR"-->'

2. Enter list of allowed ips
var allowed_ips = ["11.11.11.11","22.22.22.22"]

3. Create a regular expression string.
var handleips = allowed_ips.join("|")
handleips = new RegExp(handleips, "i")

4. Check condition whether user IP address is in allowed list
if (ip.search(handleips)!=-1)
    alert("done")
else
    alert("Unauthorized access");

Wednesday, January 14, 2009

Send a mail using SMTP in PHP

1. First create a instance of smtp server.
$smtp_server = fsockopen("smtp.yourdomain.com", 25, $errno, $errstr, 30);

2. Check for errors while connecting to mail server.
if(!$smtp_server)
{
// We have an error, do something
exit;
}

3. Now write the headers in following way

fwrite($smtp_server, "HELLO\r\n");
fwrite($smtp_server, "MAIL FROM:\r\n");
fwrite($smtp_server, "RCPT TO:\r\n");
fwrite($smtp_server, "DATA\r\n");
fwrite($smtp_server, "Received: from yourdomain.com by anydomain.com ;\r\n");
fwrite($smtp_server, "Date: Thu, 03 Jan 2006 12:33:22 -0700\r\n");
fwrite($smtp_server, "From: Me \r\n");
fwrite($smtp_server, "Subject: Good Morning\r\n");
fwrite($smtp_server, "To: to@anydomain.com\r\n");
fwrite($smtp_server, "\r\nHi Dear:\r\nHow are you ?\r\n\r\n Me.\r\n");

4. Dont forget to write QUIT at the end.
fwrite($smtp_server, ".\r\nQUIT\r\n");

5. Close the connection.
fclose($smtp_server);