// Connect to the DB
function connectDb($host,$user,$pass,$dbase) {
if (!($link = @mysql_connect($host,$user,$pass)))
die("Temporarily Unavailable. Please try again later.");
else {
mysql_query("SET NAMES 'utf8'");
@mysql_select_db($dbase,$link);
}
return $link;
}
// Parses the URL and returns the page name
function parseUrl($url) {
$url = mysql_real_escape_string(trim(substr($url,1,strlen($url)))); // remove '/'
$url = (strstr($url,'?')) ? substr($url,0,strpos($url,'?')) : $url;
$page = (empty($url)) ? 'index.shtml' : $url;
return $page;
}
// 404 Page Not Found
function _404() {
global $content;
header("HTTP/1.0 404 Not Found");
$content['title'] = 'Page Not Found';
$html = return_template($content,'template/404.tpl');
echo $html;
exit();
}
// 404 Page Not Found
function _503() {
global $content;
$content['title'] = 'Page Temporarily Unavailable';
$html = return_template($content,'template/503.tpl');
echo $html;
exit();
}
// Returns the template with embedded content
function return_template($local_array, $template_file) {
$fp = @fopen ($template_file, "r");
if ($fp) {
$template_block = fread ($fp, filesize ($template_file));
$template_block = ereg_replace("\"", "\\\"", $template_block);
$template_block = ereg_replace('}}}', '"]."', $template_block);
$template_block = ereg_replace('\{\{\{', '".$local_array["', $template_block);
@eval("\$eval_template_block = \"$template_block\";");
}
else
echo "Error opening template: {$template_file}";
@fclose($fp);
return $eval_template_block;
}
// Get the main categories into an array
function getSectionName($id) {
$query = "SELECT nav_title, name FROM content WHERE id = {$id}";
if (!($result = @mysql_query($query)))
die("Fatal Error: [getSectionName] " . $query);
else
$row = mysql_fetch_assoc($result);
return ucfirst($row['nav_title']);
}
// Top Navigation
function top_nav($main_id) {
// Get the ID of the last item
$query="SELECT id FROM content WHERE p_id = 0 AND stat ORDER BY nav_order desc LIMIT 1";
if (!($result=@mysql_query($query)))
die("Fatal Error: [top_nav] " . mysql_error());
$row = @mysql_fetch_assoc($result);
$lastID = $row['id'];
$query="SELECT id ,nav_title, name FROM content WHERE p_id = 0 AND stat ORDER BY nav_order";
if (!($result=@mysql_query($query)))
die("Fatal Error: [top_nav] " . mysql_error());
$out = "
\n";
while ($row=@mysql_fetch_assoc($result)) {
$active = ($row['id'] == $main_id) ? "style=\"text-decoration: underline;\"" : "";
$last = ($row['id'] == $lastID) ? " style=\"border-right:none\"" : "";
$out .= "- {$row['nav_title']}
\n";
}
$out .= "
\n";
mysql_free_result($result);
return $out;
}
// Top Sub Navigation
function top_sub_nav($main_id) {
$zones['source'] = getSource($main_id);
$query="SELECT id , nav_title, name FROM content WHERE p_id = 0 AND stat ORDER BY nav_order";
if (!($result=@mysql_query($query)))
die("Fatal Error: [top_sub_nav] " . mysql_error());
$out = "\n";
while ($row=@mysql_fetch_assoc($result)) {
$last = ($row['id'] == $lastID) ? " style=\"border-right:none\"" : "";
if ($row['id'] == $main_id) {
$out .= "\n
\n";
$out .= sections($row['id'],1);
// Top Nav Text Link 1
$out .= "- " . return_template($zones,'advertisement/zones/top-nav-text-link1.tpl') . "
\n";
// Top Nav Text Link 2
$out .= "- " . return_template($zones,'advertisement/zones/top-nav-text-link2.tpl') . "
\n";
$out .= "
\n";
}
}
$out .= "
\n";
mysql_free_result($result);
return $out;
}
// Left Navigation
function left_nav($main_id) {
$query="SELECT id ,nav_title, name FROM content WHERE p_id = 0 AND stat ORDER BY nav_order";
if (!($result=@mysql_query($query)))
die("Fatal Error: " . mysql_error());
$out = "\n";
$out .= "- Home Office Guide";
$out .= "
- Home Office Blog";
while ($row=@mysql_fetch_assoc($result)) {
$active = ($row['id'] == $main_id) ? " style=\"font-weight: bold;\"" : "";
$out .= "
- {$row['nav_title']}";
if ($row['id'] == $main_id) {
$out .= "\n
\n";
$out .= sections($row['id'],0);
$out .= "
\n";
}
$out .= " \n";
}
$out .= "
\n";
mysql_free_result($result);
return $out;
}
function sections($parent, $deep) {
global $content;
$query="SELECT id, nav_title, name FROM content WHERE p_id='{$parent}' AND stat ORDER BY nav_order";
if (($deep < 2) && ($result=@mysql_query($query))) {
while ($row=mysql_fetch_assoc($result)) {
$active = (($row['id'] == $content['p_id']) || ($row['id'] == $content['id'])) ? " style=\"font-weight: bold; text-decoration: underline;\"" : "";
if ($deep == 1)
$out .= "" . $row['nav_title'] . "\n";
else {
$out .= "" . $row['nav_title'] . "\n";
$out .= "\n";
}
if (($deep == 0 && $content['id'] == $row['id']) || ($deep == 0 && $content['p_id'] == $row['id']))
$out .= sections($row['id'], $deep+1);
if ($deep == 0)
$out .= "
\n\n";
}
mysql_free_result($result);
}
return $out;
}
function orphan($p_id) {
return ($p_id == -1) ? true : false;
}
function get_articles($parent) {
global $content;
$query="SELECT id, nav_title, name FROM content WHERE p_id = {$parent} AND stat ORDER BY nav_order";
if (!($result=@mysql_query($query)))
die("Fatal Error: [get_articles] Please contact the administrator.");
while ($row=@mysql_fetch_assoc($result)) {
$active = ($row['id'] == $content['id']) ? ' style="background-color:#de5362"' : '';
$out .= "{$row[nav_title]}\n";
}
mysql_free_result($result);
return $out;
}
// Wrap content with google box ads
function wrap($content) {
$google_article_top = file_get_contents('google/google_article_top.txt');
$google_article_bot = file_get_contents('google/google_article_bot.txt');
// Parse heading to insert NetSeer ad below it.
$n = strpos($content,'')+6;
$npre = substr($content,0,$n);
$npost = substr($content,$n);
$content = $npre.$npost;
$s = strpos($content,'',(strpos($content,'
')+1));
$pre = substr($content,0,$s);
$post = substr($content,$s);
// Parse content to allow chitika ad to display below the upper google adsense
$i = strpos($post,'
',(strpos($post,'
')+1));
$mid = substr($post,0,$i);
$end = substr($post,$i);
$content = $pre.$google_article_top.$mid.$end.$google_article_bot;
return $content;
}
// Get the main section id of the article
function mainID($p_id) {
$query = "SELECT id, p_id FROM content WHERE id = {$p_id} AND stat";
if (!($result=@mysql_query($query))) die("Fatal Error: [mainID] Please contact the administrator.");
if (($row = mysql_fetch_assoc($result)) && $row['p_id'] != 0) {
mysql_free_result($result);
return(mainID($row['p_id']));
}
mysql_free_result($result);
return ($row['id']) ? $row['id'] : 0;
}
// Custom Form Validator
function validateForm($data) {
foreach($data as $key => $field) {
list($type,$value) = split(':',$field);
switch($type) {
case 'required':
if (empty($value))
return $key . ' is a required field';
break;
case 'email':
if(!is_email($value))
return 'You entered an invalid email address.';
break;
}
}
}
// Get source string for banners
function getSource($id) {
$sources = array(
// Source Names
);
return ($sources[$id]) ? $sources[$id] : 'womenshealth';
}
// News
function news($table,$threads,$limit) {
$news = array();
$query = "SELECT * FROM {$table} WHERE stat ORDER BY `date` desc LIMIT {$threads}, {$limit}";
if (!($result = mysql_query($query)) || (mysql_num_rows($result) < 1))
_404();
while($row = mysql_fetch_assoc($result)) {
$i = $row['id'];
$news[$i]['id'] = $row['id'];
$news[$i]['title'] = $row['url_title'];
$news[$i]['content'] = $row['content'];
$news[$i]['date'] = $row['date'];
}
return $news;
}
// Return Total Items in a news table
function totalNewsItems($table) {
$query = "SELECT COUNT(*) AS `totalitems` FROM {$table} WHERE stat";
$result = mysql_query($query);
if($record=mysql_fetch_object($result))
return $record->totalitems;
else
return 0;
}
// Home page News
function homePageNews() {
$query = "SELECT * FROM gynob_news WHERE stat ORDER BY `date` desc LIMIT 6";
if (!($result = mysql_query($query)) || (mysql_num_rows($result) < 1))
_404();
$out .= "
\n";
while ($row = mysql_fetch_assoc($result)) {
$row['title'] = ucwords($row['title']);
$row['date'] = date("M d, Y",$row['date']);
$out .= " | {$row[url_title]} | {$row[date]} |
\n";
}
$out .= "
\n";
return $out;
}
// Forum Topics
function getForumTopics() {
$query="SELECT id, topic_title AS name, topic_url AS url, descr AS descr FROM forum_topics";
if(!($result=mysql_query($query)))
_404();
else {
$out = '';
while ($record=mysql_fetch_object($result)) {
//$posts = getTopicTotalPosts($record->id);
$out .= "- url}/\">{$record->name}
\n";
}
$out .= '
';
}
return $out;
}
// Get the number of posts in a topic
function getTopicTotalPosts($topicID) {
$query = "SELECT COUNT(*) AS total FROM forum_topics topics, forum_threads threads, forum_posts posts WHERE posts.thread_id = threads.id AND threads.topic_id = topics.id AND topics.id = {$topicID} GROUP BY topics.id";
$result=mysql_query($query);
if (!($row = mysql_fetch_assoc($result)))
return 0;
else
return $row['total'];
}
// List of pages that uses a generic template
// Returns true if the page exists
function isGenericPage($page) {
$genericPages = array(
'searchresults.html',
'demographics.html',
);
return (in_array($page,$genericPages)) ? true : false;
}
// Return the salt for crypting
function salt() {
return 'suite212preg_info004';
}
// Return Top 7 Recent Articles
function getRecentArticles() {
$query="SELECT name, nav_title FROM content WHERE p_id != -1 AND stat ORDER BY id DESC LIMIT 7";
if(!($result=mysql_query($query)))
_404();
else
while ($record=mysql_fetch_object($result))
$out .= "name}\">{$record->nav_title}
\n";
return $out;
}
// Pagination
function getNewsPaginationString($page = 1, $totalitems, $limit = 15, $targetpage = '/', $pagestring)
{
// DEFAULTS
$adjacents = 8;
// VARS
$prev = $page - 1;
$next = $page + 1;
$firstpage = 1;
$lastpage = ceil($totalitems / $limit);
$fiop = ($limit * $page) - $limit + 1; //# of first item displayed in current page
$liop = min($limit * $page, $totalitems); //# of last item displayed in current page
/*
DRAW PAGINATOR
*/
//$pagination = "\n";
if($lastpage > 1) // Paginator page selection is drawn only if more than 1 pages are there
{
$pagination .= "Page $page of $lastpage \n";
// First page selector
if ($page > 2)
//$pagination .= "- First
";
// Previous page selector
if ($page > 1)
if ($prev == 1)
$pagination .= "< Prev\n";
else
$pagination .= "< Prev\n";
// Page selectors
if ($page < 8) //not enough pages to bother
{
for ($counter = 1; $counter <= min($adjacents, $lastpage); $counter++)
{
if ($counter == 1) {
if ($counter == $page)
$pagination .= "$counter\n";
else
$pagination .= "$counter\n";
}
else {
if ($counter == $page)
$pagination .= "$counter\n";
else
$pagination .= "$counter\n";
}
}
// Append the last 3 pages if possible
if ($lastpage > $adjacents) {
if (($page + 2 < $lastpage - 3) && $lastpage - 3 > $adjacents) {
$pagination .= "...\n";
for ($i = $lastpage - 2; $i <= $lastpage; $i++) {
$pagination .= "$i\n";
}
}
else {
$pagination .= "...\n";
$pagination .= "$lastpage\n";
}
}
}
elseif ($page > $lastpage - 8) // End of the list
{
// Append the first 3 pages if possible
if ($page - 2 > $firstpage + 3) {
for ($i = 1; $i <= $firstpage + 2; $i++) {
if ($i == 1)
$pagination .= "$i\n";
else
$pagination .= "$i\n";
}
$pagination .= "...\n";
}
for($counter = ($lastpage - min($adjacents, $lastpage) < 4) ? 6 : $lastpage - min($adjacents, $lastpage); $counter <= $lastpage; $counter++)
{
if ($counter) {
if ($counter == 1)
$pagination .= "$counter\n";
else {
if ($counter == $page)
$pagination .= "$counter\n";
else
$pagination .= "$counter\n";
}
}
}
}
else // Middle of the list
{
// Append the first 3 pages if possible
if ($page - 2 > $firstpage + 2) {
for ($i = 1; $i <= $firstpage + 2; $i++) {
if ($i == 1)
$pagination .= "$i\n";
else
$pagination .= "$i\n";
}
$pagination .= "...\n";
}
for($counter = $page - 2; $counter <= $page + 2; $counter++)
{
if ($counter) {
if ($counter == 1)
$pagination .= "$counter\n";
else {
if ($counter == $page)
$pagination .= "$counter\n";
else
$pagination .= "$counter\n";
}
}
}
// Append the last 3 pages if possible
if ($lastpage > $adjacents) {
if (($page + 2 < $lastpage - 3) && $lastpage - 3 != $adjacents) {
$pagination .= "...\n";
for ($i = $lastpage - 2; $i <= $lastpage; $i++) {
$pagination .= "$i\n";
}
}
else {
$pagination .= "- ...
\n";
$pagination .= "$lastpage\n";
}
}
}
//next button
if ($page < $lastpage)
$pagination .= "Next >\n";
//last button
//if ($page < $lastpage - 1)
//$pagination .= "- Last
";
//$pagination .= "
\n";
}
$pagination = "\n";
return $pagination;
}
// Insert email to `emails` table.
function addEmail($email) {
$query = "SELECT * FROM emails WHERE email = '{$email}'";
$result = mysql_query($query);
if ($row = mysql_fetch_assoc($result))
return false;
else {
$IP = getenv('REMOTE_ADDR');
$query = "INSERT INTO(email,ip,submitdate) VALUES('{$email}','{$IP}',unix_timestamp())";
$result = mysql_query($query);
return true;
}
}
?>