Monday, 27 January 2014

Read Word Documents Text Using php

<?php

/*
* @Author : Naveen
* @UpdatedON: 27-1-2014
* @Email : naveenvoruganti29@gmail.com
*/
class DocReaders {

protected $content = '';

function get($filename){
//echo $filename;exit;
//check the file is exist or not
if(file_exists($filename)){
$content =  $this->getContent($filename);
list($s) =  explode("<br />",$content);
$s= preg_replace('/[^a-zA-Z0-9\'\":.,$&\/| ]/', '', $s);
return trim($s);
}
else
//throw new Exception("File not Exist");
return false;

}

function getContent($filePath){

//get the File name first from the path
$filename = basename($filePath);
//check the type of file
list($file,$type) = explode(".",$filename);

if(strtolower($type)=='doc'){

$content = nl2br($this->parseWord($filePath));

}else if(strtolower($type)=='docx'){ //For 2007 Docx Format

$content = $this->parseDocx($filePath);

} else if(strtolower($type)=='pdf'){ //FOr Pdf files

$content = $this->pdf2text($filePath);
}

return $content;
}

//for doc format only 2003
function parseWord($filename){

$fileHandle = fopen($filename, "r");

$line = @fread($fileHandle, filesize($filename));

$lines = explode(chr(0x0D),$line);

$outtext = "";

foreach($lines as $thisline){

$pos = strpos($thisline, chr(0x00));

if (($pos !== FALSE)||(strlen($thisline)==0)){

} else {

$outtext .= $thisline." ";

 }

 }

$outtext = preg_replace("/[^a-zA-Z0-9\s\,\.\-\n\r\t@\/\_\(\)]/","",$outtext);

return $outtext;


}

//for docx format 2007
function parseDocx($filename){

$striped_content = '';

$content = '';

if(!$filename || !file_exists($filename)) return false;

$zip = zip_open($filename); if (!$zip || is_numeric($zip)) return false;

while ($zip_entry = zip_read($zip)) {

if (zip_entry_open($zip, $zip_entry) == FALSE) continue;

if (zip_entry_name($zip_entry) != "word/document.xml") continue;

$content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));

zip_entry_close($zip_entry);

}// end while

zip_close($zip); //echo $content; //echo "<hr>"; //file_put_contents('1.xml', $content);

$content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content);

$content = str_replace('</w:r></w:p>', "\r\n", $content);

$striped_content = strip_tags($content);

return $striped_content;

}

/****** Start To Read PDF Files ********/

function decodeAsciiHex($input) {
$output = "";

$isOdd = true;
$isComment = false;

for($i = 0, $codeHigh = -1; $i < strlen($input) && $input[$i] != '>'; $i++) {
$c = $input[$i];

if($isComment) {
if ($c == '\r' || $c == '\n')
$isComment = false;
continue;
}

switch($c) {
case '\0': case '\t': case '\r': case '\f': case '\n': case ' ': break;
case '%':
$isComment = true;
break;

default:
$code = hexdec($c);
if($code === 0 && $c != '0')
return "";

if($isOdd)
$codeHigh = $code;
else
$output .= chr($codeHigh * 16 + $code);

$isOdd = !$isOdd;
break;
}
}

if($input[$i] != '>')
return "";

if($isOdd)
$output .= chr($codeHigh * 16);

return $output;
}
function decodeAscii85($input) {
$output = "";

$isComment = false;
$ords = array();

for($i = 0, $state = 0; $i < strlen($input) && $input[$i] != '~'; $i++) {
$c = $input[$i];

if($isComment) {
if ($c == '\r' || $c == '\n')
$isComment = false;
continue;
}

if ($c == '\0' || $c == '\t' || $c == '\r' || $c == '\f' || $c == '\n' || $c == ' ')
continue;
if ($c == '%') {
$isComment = true;
continue;
}
if ($c == 'z' && $state === 0) {
$output .= str_repeat(chr(0), 4);
continue;
}
if ($c < '!' || $c > 'u')
return "";

$code = ord($input[$i]) & 0xff;
$ords[$state++] = $code - ord('!');

if ($state == 5) {
$state = 0;
for ($sum = 0, $j = 0; $j < 5; $j++)
$sum = $sum * 85 + $ords[$j];
for ($j = 3; $j >= 0; $j--)
$output .= chr($sum >> ($j * 8));
}
}
if ($state === 1)
return "";
elseif ($state > 1) {
for ($i = 0, $sum = 0; $i < $state; $i++)
$sum += ($ords[$i] + ($i == $state - 1)) * pow(85, 4 - $i);
for ($i = 0; $i < $state - 1; $i++)
$ouput .= chr($sum >> ((3 - $i) * 8));
}

return $output;
}
function decodeFlate($input) {
return @gzuncompress($input);
}

function getObjectOptions($object) {
$options = array();
if (preg_match("#<<(.*)>>#ismU", $object, $options)) {
$options = explode("/", $options[1]);
@array_shift($options);

$o = array();
for ($j = 0; $j < @count($options); $j++) {
$options[$j] = preg_replace("#\s+#", " ", trim($options[$j]));
if (strpos($options[$j], " ") !== false) {
$parts = explode(" ", $options[$j]);
$o[$parts[0]] = $parts[1];
} else
$o[$options[$j]] = true;
}
$options = $o;
unset($o);
}

return $options;
}
function getDecodedStream($stream, $options) {
$data = "";
if (empty($options["Filter"]))
$data = $stream;
else {
$length = !empty($options["Length"]) ? $options["Length"] : strlen($stream);
$_stream = substr($stream, 0, $length);

foreach ($options as $key => $value) {
if ($key == "ASCIIHexDecode")
$_stream = $this->decodeAsciiHex($_stream);
if ($key == "ASCII85Decode")
$_stream = $this->decodeAscii85($_stream);
if ($key == "FlateDecode")
$_stream = $this->decodeFlate($_stream);
}
$data = $_stream;
}
return $data;
}
function getDirtyTexts(&$texts, $textContainers) {
for ($j = 0; $j < count($textContainers); $j++) {
if (preg_match_all("#\[(.*)\]\s*TJ#ismU", $textContainers[$j], $parts))
$texts = array_merge($texts, @$parts[1]);
elseif(preg_match_all("#Td\s*(\(.*\))\s*Tj#ismU", $textContainers[$j], $parts))
$texts = array_merge($texts, @$parts[1]);
}
}
function getCharTransformations(&$transformations, $stream) {
preg_match_all("#([0-9]+)\s+beginbfchar(.*)endbfchar#ismU", $stream, $chars, PREG_SET_ORDER);
preg_match_all("#([0-9]+)\s+beginbfrange(.*)endbfrange#ismU", $stream, $ranges, PREG_SET_ORDER);

for ($j = 0; $j < count($chars); $j++) {
$count = $chars[$j][1];
$current = explode("\n", trim($chars[$j][2]));
for ($k = 0; $k < $count && $k < count($current); $k++) {
if (preg_match("#<([0-9a-f]{2,4})>\s+<([0-9a-f]{4,512})>#is", trim($current[$k]), $map))
$transformations[str_pad($map[1], 4, "0")] = $map[2];
}
}
for ($j = 0; $j < count($ranges); $j++) {
$count = $ranges[$j][1];
$current = explode("\n", trim($ranges[$j][2]));
for ($k = 0; $k < $count && $k < count($current); $k++) {
if (preg_match("#<([0-9a-f]{4})>\s+<([0-9a-f]{4})>\s+<([0-9a-f]{4})>#is", trim($current[$k]), $map)) {
$from = hexdec($map[1]);
$to = hexdec($map[2]);
$_from = hexdec($map[3]);

for ($m = $from, $n = 0; $m <= $to; $m++, $n++)
$transformations[sprintf("%04X", $m)] = sprintf("%04X", $_from + $n);
} elseif (preg_match("#<([0-9a-f]{4})>\s+<([0-9a-f]{4})>\s+\[(.*)\]#ismU", trim($current[$k]), $map)) {
$from = hexdec($map[1]);
$to = hexdec($map[2]);
$parts = preg_split("#\s+#", trim($map[3]));

for ($m = $from, $n = 0; $m <= $to && $n < count($parts); $m++, $n++)
$transformations[sprintf("%04X", $m)] = sprintf("%04X", hexdec($parts[$n]));
}
}
}
}
function getTextUsingTransformations($texts, $transformations) {
$document = "";
for ($i = 0; $i < count($texts); $i++) {
$isHex = false;
$isPlain = false;

$hex = "";
$plain = "";
for ($j = 0; $j < strlen($texts[$i]); $j++) {
$c = $texts[$i][$j];
switch($c) {
case "<":
$hex = "";
$isHex = true;
break;
case ">":
$hexs = str_split($hex, 4);
for ($k = 0; $k < count($hexs); $k++) {
$chex = str_pad($hexs[$k], 4, "0");
if (isset($transformations[$chex]))
$chex = $transformations[$chex];
$document .= html_entity_decode("&#x".$chex.";");
}
$isHex = false;
break;
case "(":
$plain = "";
$isPlain = true;
break;
case ")":
$document .= $plain;
$isPlain = false;
break;
case "\\":
$c2 = $texts[$i][$j + 1];
if (in_array($c2, array("\\", "(", ")"))) $plain .= $c2;
elseif ($c2 == "n") $plain .= '\n';
elseif ($c2 == "r") $plain .= '\r';
elseif ($c2 == "t") $plain .= '\t';
elseif ($c2 == "b") $plain .= '\b';
elseif ($c2 == "f") $plain .= '\f';
elseif ($c2 >= '0' && $c2 <= '9') {
$oct = preg_replace("#[^0-9]#", "", substr($texts[$i], $j + 1, 3));
$j += strlen($oct) - 1;
$plain .= html_entity_decode("&#".octdec($oct).";");
}
$j++;
break;

default:
if ($isHex)
$hex .= $c;
if ($isPlain)
$plain .= $c;

break;
}
}
$document .= "\n";
}

return $document;
}

function pdf2text($filename) {
$infile = @file_get_contents($filename, FILE_BINARY);
if (empty($infile))
return "";

$transformations = array();
$texts = array();

preg_match_all("#obj(.*)endobj#ismU", $infile, $objects);
$objects = @$objects[1];

for ($i = 0; $i < count($objects); $i++) {
$currentObject = $objects[$i];

if (preg_match("#stream(.*)endstream#ismU", $currentObject, $stream)) {
$stream = ltrim($stream[1]);

$options = $this->getObjectOptions($currentObject);
if (!(empty($options["Length1"]) && empty($options["Type"]) && empty($options["Subtype"])))
continue;

$data = $this->getDecodedStream($stream, $options);
if (strlen($data)) {
if (preg_match_all("#BT(.*)ET#ismU", $data, $textContainers)) {
$textContainers = @$textContainers[1];
$this->getDirtyTexts($texts, $textContainers);
} else
$this->getCharTransformations($transformations, $data);
}
}
}

return $this->getTextUsingTransformations($texts, $transformations);
}

/*******End to Read Pdf Files****/

}

c = new DocReaders ();
$result = c->getContent("filepath with filename");
?>

Thursday, 25 April 2013

How to validate the Multiple Emails Using Jquery validation


$.validator.addMethod(
    "multiemail",
     function(value, element) {
         if (this.optional(element)) // return true on optional element
             return true;
         var emails = value.split(/[,]+/); // split element by , and ;
         valid = true;
         for (var i in emails) {
             value = emails[i];
             valid = valid &&
                     jQuery.validator.methods.email.call(this, $.trim(value), element);
         }
         return valid;
     },

    jQuery.validator.messages.email
);

Saturday, 20 April 2013

How to get the Youtube video information using id in php


<?php
    // function to parse a video <entry>
    function parseVideoEntry($entry) {    
      $obj= new stdClass;
   
      // get nodes in media: namespace for media information
      $media = $entry->children('http://search.yahoo.com/mrss/');
      $obj->title = $media->group->title;
      $obj->description = $media->group->description;
   
      // get video player URL
      $attrs = $media->group->player->attributes();
      $obj->watchURL = $attrs['url'];
   
      // get video thumbnail
      $attrs = $media->group->thumbnail[0]->attributes();
      $obj->thumbnailURL = $attrs['url'];
         
      // get <yt:duration> node for video length
      $yt = $media->children('http://gdata.youtube.com/schemas/2007');
      $attrs = $yt->duration->attributes();
      $obj->length = $attrs['seconds'];
   
      // get <yt:stats> node for viewer statistics
      $yt = $entry->children('http://gdata.youtube.com/schemas/2007');
      $attrs = $yt->statistics->attributes();
      $obj->viewCount = $attrs['viewCount'];
   
      // get <gd:rating> node for video ratings
      $gd = $entry->children('http://schemas.google.com/g/2005');
      if ($gd->rating) {
        $attrs = $gd->rating->attributes();
        $obj->rating = $attrs['average'];
      } else {
        $obj->rating = 0;      
      }
     
      // get <gd:comments> node for video comments
      $gd = $entry->children('http://schemas.google.com/g/2005');
      if ($gd->comments->feedLink) {
        $attrs = $gd->comments->feedLink->attributes();
        $obj->commentsURL = $attrs['href'];
        $obj->commentsCount = $attrs['countHint'];
      }
   
      // get feed URL for video responses
      $entry->registerXPathNamespace('feed', 'http://www.w3.org/2005/Atom');
      $nodeset = $entry->xpath("feed:link[@rel='http://gdata.youtube.com/schemas/
      2007#video.responses']");
      if (count($nodeset) > 0) {
        $obj->responsesURL = $nodeset[0]['href'];    
      }
       
      // get feed URL for related videos
      $entry->registerXPathNamespace('feed', 'http://www.w3.org/2005/Atom');
      $nodeset = $entry->xpath("feed:link[@rel='http://gdata.youtube.com/schemas/
      2007#video.related']");
      if (count($nodeset) > 0) {
        $obj->relatedURL = $nodeset[0]['href'];    
      }
 
      // return object to caller
      return $obj;    
    }
    $vid='VIDEO ID';
    // set video data feed URL
    $feedURL = 'http://gdata.youtube.com/feeds/api/videos/' . $vid;

    // read feed into SimpleXML object
    $entry = simplexml_load_file($feedURL);
 
    // parse video entry
    $video = parseVideoEntry($entry);
     
    // display main video record
    echo "<table>\n";
    echo "<tr>\n";
    echo "<td><a href=\"{$video->watchURL}\">
    <img src=\"$video->thumbnailURL\"/></a></td>\n";
    echo "<td><a href=\"{$video->watchURL}\">{$video->title}</a>
    <br/>\n";
    echo sprintf("%0.2f", $video->length/60) . " min. | {$video->rating}
    user rating | {$video->viewCount} views<br/>\n";
echo 'desc';
    echo $video->description . " end </td>\n";
    echo "</tr>\n";
 
    // read 'video comments' feed into SimpleXML object
    // parse and display each comment
    if ($video->commentsURL && $video->commentsCount > 0) {
      $commentsFeed = simplexml_load_file($video->commentsURL);  
      echo "<tr><td colspan=\"2\"><h3>" . $commentsFeed->title .
      "</h3></td></tr>\n";
      echo "<tr><td colspan=\"2\"><ol>\n";
      foreach ($commentsFeed->entry as $comment) {
        echo "<li>" . $comment->content . "</li>\n";
      }
      echo "</ol></td></tr>\n";
    }
 
    // read 'video responses' feed into SimpleXML object
    // parse and display each video entry
    if ($video->responsesURL) {
      $responseFeed = simplexml_load_file($video->responsesURL);  
      echo "<tr><td colspan=\"2\"><h3>" .
      $responseFeed->title . "</h3></td></tr>\n";
      foreach ($responseFeed->entry as $response) {
        $responseVideo = parseVideoEntry($response);
        echo "<tr>\n";
        echo "<td><a href=\"{$responseVideo->watchURL}\">
        <img src=\"$responseVideo->thumbnailURL\"/></a></td>\n";
        echo "<td><a href=\"{$responseVideo->watchURL}\"
        >{$responseVideo->title}</a><br/>\n";
        echo sprintf("%0.2f", $responseVideo->length/60) . " min. |
        {$responseVideo->rating} user rating | {$responseVideo->viewCount}
        views<br/>\n";
        echo $responseVideo->description . "</td>\n";
        echo "</tr>\n";    
      }
    }
 
    // read 'related videos' feed into SimpleXML object
    // parse and display each video entry
    if ($video->relatedURL) {
      $relatedFeed = simplexml_load_file($video->relatedURL);  
      echo "<tr><td colspan=\"2\"><h3>" .
      $relatedFeed->title . "</h3></td></tr>\n";
      foreach ($relatedFeed->entry as $related) {
        $relatedVideo = parseVideoEntry($related);
        echo "<tr>\n";
        echo "<td><a href=\"{$relatedVideo->watchURL}\">
        <img src=\"$relatedVideo->thumbnailURL\"/></a></td>\n";
        echo "<td><a href=\"{$relatedVideo->watchURL}\">
        {$relatedVideo->title}</a><br/>\n";
        echo sprintf("%0.2f", $relatedVideo->length/60) . " min. |
        {$relatedVideo->rating} user rating | {$relatedVideo->viewCount}
        views<br/>\n";
        echo $relatedVideo->description . "</td>\n";
        echo "</tr>\n";    
      }
    }
    echo "</table>\n";  
    ?>

Get the Gmail Contacts using PHP



error_reporting (E_ALL);

$user = 'YOUR EMAIL'
$password ='YOUR PASSWORD';


// step 1: login
$login_url = "https://www.google.com/accounts/ClientLogin";
$fields = array (
'Email' => $user,
'Passwd' => $password,
'service' => 'cp', // <== contact list service code
'source' => 'test-google-contact-grabber',
'accountType' => 'GOOGLE',
);

$curl = curl_init ();
curl_setopt ($curl, CURLOPT_URL,$login_url);
curl_setopt ($curl, CURLOPT_POST, 1);
curl_setopt ($curl, CURLOPT_POSTFIELDS,$fields);
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($curl);

$returns = array ();

foreach (explode ("\n",$result) as $line)
{
$line = trim ($line);
if (!$line) continue;
list ($k,$v) = explode ("=",$line,2);

$returns[$k] = $v;
}

curl_close ($curl);

// step 2: grab the contact list
$feed_url = "http://www.google.com/m8/feeds/contacts/$user/full?alt=json&max-results=250";

$header = array (
'Authorization: GoogleLogin auth=' . @$returns['Auth'],
);

$curl = curl_init ();
curl_setopt ($curl, CURLOPT_URL, $feed_url);
curl_setopt ($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec ($curl);
curl_close ($curl);

$data = json_decode ($result);

$contacts = array ();

//NOW CHECK IF THE ENTRY IS EXIST OR NOT
if(isset($data->feed->entry)){
foreach ($data->feed->entry as $entry)
{
$contact = new stdClass();
//$contact->title = $entry->title->{'$t'};
if(isset($entry->{'gd$email'}[0]->address)){
$contact->email = $entry->{'gd$email'}[0]->address;

$contacts[] = $contact;}
}
     echo '<pre>';
 print_r($contacts);
 echo '</pre>';exit;

 }else
 //show the error
print_r($data->feed);