EAI Newsletter Archives - India Renewable Energy Consulting – Solar, Biomass, Wind, Cleantech
Select Page

EAI Newsletter Archives

[raw]

[/raw]
‘;
exit();
}

$items = FetchXmlNode(‘item’, $content, true);
$numbershown = 1;
$mycontent = ‘

‘;
foreach ($items as $itempos => $item) {
if ($numbershown > NUMBER_OF_ENTRIES_TO_SHOW) {
break;
}

$title = FetchXmlNode(‘title’, $item);
$link = FetchXmlNode(‘link’, $item);
$author = FetchXmlNode(‘author’, $item);
$postdate = FetchXmlNode(‘pubdate’, $item);
$postdate = date(‘d-m-Y’, strtotime($postdate ));;

$postcontent = urldecode(FetchXmlNode(‘content’, $item));

$post = ”;
preg_match(‘%<\!\[cdata\[(.*?)\]\]>%is’, $postcontent, $match);
if (!empty($match)) {
$post = $match[1];
}
$shortpost = GetShortPost($post);
$mycontent .= ‘

‘ . $postdate . ‘ – ‘ . $title . ‘

‘;
$numbershown++;
}
$mycontent .= ‘

‘;
echo $mycontent;

/**
* GetShortPost
* Based on the content passed in, it returns a shortened/trimmed down version of that content.
* If the content is shorter than the length specified at the top, nothing is trimmed down.
* If it is longer than the length specified, then it’s trimmed to a max of that length.
*
* @param String $content The content to trim down to a particular length.
*
* @see MAX_POST_LENGTH
*
* @return String Returns the trimmed down version of the content.
*/
function GetShortPost($content=”)
{
$content = trim($content);
$post = str_replace(“\r”, “\n”, $content);
$post = str_replace(“\n”, “”, $post);
if (strlen($post) > MAX_POST_LENGTH) {
$shortpost = substr($post, 0, MAX_POST_LENGTH);
if (substr($shortpost, -3, 3) != ‘…’) {
$shortpost .= ‘…’;
}
} else {
$shortpost = $post;
}
return $shortpost;
}

/**
* TimeDifference
* Works out the relative difference between when an item was posted and now.
* Eg turns 125 seconds into 2 minutes, 5 seconds.
* If the difference is longer than a week, it’s just returned as a properly formatted date (d-M-Y)
*
* @param Int $posttime The time the item was posted
* @param Int $timenow The time now
*
* @return String Returns the relative time difference as a string (eg ‘2 minutes, 5 seconds’).
*/
function TimeDifference($posttime, $timenow)
{
$difference = $timenow – $posttime;

if ($difference < ONE_MINUTE) { $timechange = number_format($difference, 0) . ' second'; if ($difference > 1) {
$timechange .= ‘s’;
}
}
if ($difference > ONE_MINUTE && $difference < ONE_HOUR) { $num_mins = floor($difference / ONE_MINUTE); $timechange = number_format($num_mins, 0) . ' minute'; if ($num_mins > 1) {
$timechange .= ‘s’;
}
}

if ($difference >= ONE_HOUR && $difference < ONE_DAY) { $hours = floor($difference/ONE_HOUR); $mins = floor($difference % ONE_HOUR) / ONE_MINUTE; $timechange = number_format($hours, 0) . ' hour'; if ($hours > 1) {
$timechange .= ‘s’;
}

$timechange .= ‘, ‘ . number_format($mins, 0) . ‘ minute’;
if ($mins > 1) {
$timechange .= ‘s’;
}
}

if ($difference >= ONE_DAY && $difference < ONE_WEEK) { $days = floor($difference / ONE_DAY); $timechange = number_format($days, 0) . ' day'; if ($days > 1) {
$timechange .= ‘s’;
}
}

if ($difference >= ONE_WEEK) {
return date(‘d-M-Y’, $posttime);
}

$timechange .= ‘ ago’;
return $timechange;
}

/**
* FetchXmlNode
* Looks for a node based on the name of the node you’re looking for and the xml passed in.
* Either returns a particular node (if you’re looking for one) or all of those nodes if you’re looking for all.
*
* @param String $node The name of the ‘node’ you’re looking for.
* @param String $xml The xml you’re looking for the node in.
* @param Boolean $all Whether you are looking for all nodes in a tree or just one particular node.
*
* @return False|Array Returns false if the node is not found in the xml, otherwise returns the array of items for that node.
*/
function FetchXmlNode($node=”, $xml=”, $all=false)
{
if ($node == ”) {
return false;
}
if ($all) {
preg_match_all(‘%<('.$node.'[^>]*)>(.*?)%is’, $xml, $matches);
} else {
preg_match(‘%<('.$node.'[^>]*)>(.*?)%is’, $xml, $matches);
}

if (!isset($matches[2])) {
return false;
}

return $matches[2];
}

/**
* GetContent
* Gets the content from the XML_URL defined at the top and returns it as a string.
* Tries to use curl (if it’s available) or fopen (if curl isn’t available).
* If neither are available, echos an error message and then dies.
*
* @see XML_URL
* @see NUMBER_OF_ENTRIES_TO_SHOW
*
* @return String|Void Returns the page content if it can fetch it, otherwise shows an error and dies.
*/
function GetContent()
{
$url = XML_URL . ‘?Fetch=’ . NUMBER_OF_ENTRIES_TO_SHOW;

if (function_exists(‘curl_init’)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

$pagedata = curl_exec($ch);

if (!$pagedata) {
$error = curl_error($ch);
}
curl_close($ch);

if (!$pagedata) {
echo “Error: ” . $error . “
“;
}
$pagedata = trim($pagedata);
return $pagedata;
}

if (ini_get(‘allow_url_fopen’)) {
if (!$fp = fopen($url, ‘r’)) {
echo ‘Unable to open url, aborting.
‘;
exit();
}
$pagedata = ”;
while (!feof($fp)) {
$pagedata .= fread($fp, 1024);
}
fclose($fp);
$pagedata = trim($pagedata);
return $pagedata;
}

echo ‘Your server does not support curl or have allow_url_fopen switched on. Unfortunately we cannot get content without either of these options being available. Please speak to your administrator.
‘;
exit();
}
?>

Copyright © 2024 EAI. All rights reserved.