<?php 
/*#################################################################*\
|# Licence Number 0JKF-0621-0SE1-0112
|# -------------------------------------------------------------   #|
|# Copyright (c)2013 PHP Link Directory.                           #|
|# http://www.phplinkdirectory.com                                 #|
\*#################################################################*/
	
/**
# ################################################################################
# Project:   PHP Link Directory
#
# **********************************************************************
# Copyright (C) 2004-2009 NetCreated, Inc. (http://www.netcreated.com/)
#
# This software is for use only to those who have purchased a license.
# A license must be purchased for EACH installation of the software.
#
# By using the software you agree to the terms:
#
#    - You may not redistribute, sell or otherwise share this software
#      in whole or in part without the consent of the the ownership
#      of PHP Link Directory. Please contact david@david-duval.com
#      if you need more information.
#
#    - You agree to retain a link back to http://www.phplinkdirectory.com/
#      on all pages of your directory in you purchased the $25 version
#      of the software.
#
# License holders are entitled to upgrades to the 3.4 branch of the software
# as they are made available at http://www.phplinkdirectory.com/
#
# In some cases, license holders may be required to agree to changes
# in the software license before receiving updates to the software.
# **********************************************************************
#
# For questions, help, comments, discussion, etc., please join the
# PHP Link Directory Forum http://www.phplinkdirectory.com/forum/
#
# @link           http://www.phplinkdirectory.com/
# @copyright      2004-2009 NetCreated, Inc. (http://www.netcreated.com/)
# @projectManager David DuVal <david@david-duval.com>
# @package        PHPLinkDirectory
# @version        4.0.0
# ################################################################################
*/

/**
 * Reads the configuration data from the database and sets it as constants
 * @author dcb
 */
function read_config(&$db)
{
   global $tables;

   //Define settings array to export
   $exportSettings = array();

   $sql = "SELECT * FROM `{$tables['config']['name']}`";
   $db->SetFetchMode(ADODB_FETCH_ASSOC);
   $rs = $db->Execute($sql);

   while (!$rs->EOF)
   {
      define ($rs->Fields('ID'), $rs->Fields('VALUE'));
      $exportSettings[$rs->Fields('ID')] = $rs->Fields('VALUE');

      $rs->MoveNext();
   }
   set_default_recpr();

   return $exportSettings;
}

function set_default_recpr()
{
   $default_recpr_link_url   = (ENABLE_THREE_WAY == 1 ? THREE_WAY_LINK_URL   : SITE_URL );
   $default_recpr_link_title = (ENABLE_THREE_WAY == 1 ? THREE_WAY_LINK_TITLE : SITE_NAME);
   define ('DEFAULT_RECPR_URL'  , $default_recpr_link_url  );
   define ('DEFAULT_RECPR_TITLE', $default_recpr_link_title);
}

/**
 * Creates a Smarty object and sets default values
 * @author dcb
 */
function get_tpl($force_tpl='')
{
   if (!empty ($_SESSION['user_language']))
   {
      $language = $_SESSION['user_language'];
   }
   elseif (defined ('LANGUAGE'))
   {
      $language = LANGUAGE;
      if (empty ($language))
         $language = 'en';
   }
   else
      $language = 'en';

   $tpl = new IntSmarty($language);

   
   //Determine what template to use
   if (empty ($force_tpl))
      determine_template();
   else
      define ('USE_TEMPLATE', $force_tpl.'/');

   $tpl->template_dir = INSTALL_PATH.'templates/'.USE_TEMPLATE;
   $tpl->compile_dir  = INSTALL_PATH.'temp/templates';
   $tpl->cache_dir    = INSTALL_PATH.'temp/cache';
   
   return $tpl;
}

function get_widget_tpl($wid)
{
   if (!empty ($_SESSION['user_language'])) {
      $language = $_SESSION['user_language'];
   } elseif (defined ('LANGUAGE')) {
      $language = LANGUAGE;
      if (empty ($language))
         $language = 'en';
   } else
      $language = 'en';

	$t = new IntSmarty($language);
	
	$t->caching = 0;
	$t->compile_id = $t->compile_id . $wid; //compile_id is already used in intsmarty for language settings
	$t->template_dir = INSTALL_PATH.'widgets/'.$wid.'/templates';
	$t->compile_dir  = INSTALL_PATH.'temp/templates';
	$t->cache_dir    = INSTALL_PATH.'temp/cache';
	
	return $t;
}

/**
 * Get templates and read templates informations
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function get_templates($dirname='templates/', $exclude='Core', $info_file='readme.txt', $screenshot='screenshot.png')
{
   $pattern   = array ( '`[,]+`', '/\s/', '`[,]$`', '`^[,]`' );
   $replace   = array ( ','     , ''    , ''      , ''       );

   $dirname   = preg_replace ($pattern, $replace, $dirname);
   $exclude   = preg_replace ($pattern, $replace, $exclude);
   $exclude   = strtolower   ($exclude);
   $exclude  .= ',.,..';

   $exclude   = explode (',', $exclude);

   $template_list  = array ();

   if ($handle = @ opendir ($dirname))
   {
      $key = 0;
      while (false !== ($dir_content = @ readdir ($handle)))
      {
         if (!in_array (strtolower ($dir_content), $exclude) && !empty ($dir_content))
         {
            if (is_dir ($dirname.$dir_content))
            {
               $template_list[$key] = array ();
               $template_list[$key]['theme_path'] = $dir_content;
               $template_list[$key]['theme_full_path'] = $dirname.$dir_content;
               $template_list[$key]['theme_info_file'] = $template_list[$key]['theme_full_path'].'/'.$info_file;
               $template_list[$key]['theme_screenshot_file'] = (is_file ($template_list[$key]['theme_full_path'].'/'.$screenshot) ? $template_list[$key]['theme_full_path'].'/'.$screenshot : '');

               $info_file_content = implode ('', file ($template_list[$key]['theme_info_file']));

               preg_match ("|Theme Name:(.*)|i" , $info_file_content, $theme_name       );
               preg_match ("|Theme URI:(.*)|i"  , $info_file_content, $theme_uri        );
               preg_match ("|Description:(.*)|i", $info_file_content, $theme_description);
               preg_match ("|Version:(.*)|i"    , $info_file_content, $theme_version    );
               preg_match ("|Author:(.*)|i"     , $info_file_content, $theme_author     );
               preg_match ("|Author URI:(.*)|i" , $info_file_content, $theme_author_uri );

               $template_list[$key]['theme_name']        = clean_string($theme_name[1]       );
               $template_list[$key]['theme_uri']         = clean_string($theme_uri[1]        );
               $template_list[$key]['theme_description'] = clean_string($theme_description[1]);
               $template_list[$key]['theme_version']     = clean_string($theme_version[1]    );
               $template_list[$key]['theme_author']      = clean_string($theme_author[1]     );
               $template_list[$key]['theme_author_uri']  = clean_string($theme_author_uri[1] );

               //Empty memory
               unset ($theme_name, $theme_uri, $theme_description, $theme_version, $theme_author, $theme_author_uri, $info_file_content);

               $key++;
            }
         }
      }

      @ closedir ($handle);
   }
   unset ($dir_content, $key, $dirname, $exclude, $info_file, $pattern, $replace);
   return $template_list;
}

function determine_template()
{
   $available_templates  = get_templates();
   $error_not_found      = _L('Could not determine template!');
   $error_none_available = _L('No template(s) available!');

   if (!is_array ($available_templates) || empty ($available_templates))
      exit ($error_none_available);

   $get_template = (defined ('TEMPLATE') && TEMPLATE != '' ? TEMPLATE : 'Default');
   $get_template = trim ($get_template);

   $use_template = '';

   foreach ($available_templates as $key => $template)
   {
      if (strtolower ($template['theme_path']) == strtolower ($get_template))
         $use_template = trim ($template['theme_path']);
   }

   if (empty ($use_template))
      exit ($error_not_found);

   //Free memory
   unset ($error_not_found, $error_none_available, $get_template, $available_templates);

   //Make template path availble system wide
   define ('USE_TEMPLATE', $use_template);

   return $use_template;
}

/**
 * Shortcut function for string translations
 * @param string $str String to translate
 * @return string translated string
 * @author dcb
 */
function _L($str)
{
   global $tpl;
   if (method_exists ($tpl, 'translate'))
      return $tpl->translate($str);
   else
      return $str;
}

/**
 * Extracts from the global variable $_REQUEST only the values those keys correspond to table column names
 * @param string $table table name
 * @return array associative array
 * @author dcb
 */
function get_table_data($table)
{
   global $db, $tables;

   $data = array ();
   
   //Get table fields
   $dbTblFields = $db->MetaColumnNames($tables[$table]['name'], true);
   
   if (is_array ($dbTblFields) && !empty ($dbTblFields))
   {
      
       //New method, used because
      //phpLD can add now DB fields
      foreach ($dbTblFields as $key => $field)
      {
         if (isset ($_REQUEST[$field]))
            $data[$field] = $_REQUEST[$field];
      }
   }
   else
   {
      //Old method, just as backup if anything goes wrong
      foreach ($tables[$table]['fields'] as $col => $v)
      {
         if (isset ($_REQUEST[$col]))
            $data[$col] = $_REQUEST[$col];
      }
   }

   return $data;
}

function set_user_action ($action_value, $action_id, $user_id) {
    global $db, $tables;
    //if action exists, let's get it
    $sql = "SELECT * FROM `{$tables['user_actions']['name']}` WHERE `USER_ID`=".$db->qstr($user_id)." 
    			AND `ACTION_ID`=".$db->qstr($action_id);
    $data = $db->GetOne($sql);
    if ($data['ID']== '') {
    	$sql = "INSERT INTO `{$tables['user_actions']['name']}` VALUES ('', ".$db->qstr($user_id).", ".$db->qstr($action_id).", 
    		".$db->qstr($action_value).")";
        $db->Execute($sql);
    } else {
        $sql = "UPDATE `{$tables['user_actions']['name']}` SET `VALUE`=".$db->qstr($action_value)." 
        	WHERE `USER_ID`=".$db->qstr($user_id)." AND `ACTION_ID`=".$db->qstr($action_id)."";
        $db->Execute($sql);
    }
}

function remove_user_action($action_id, $user_id) {
    global $db, $tables;
    $sql = "DELETE FROM `{$tables['user_actions']['name']}` WHERE `USER_ID`=".$db->qstr($user_id)." AND `ACTION_ID`=".$db->qstr($action_id)."";
    $db->Execute($sql);
}

/**
 * ~DEPRECATED~
 * Extract saved configuration for the Captcha Image confirmation
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function get_captcha_info($option)
{
   global $db, $tables;
   $option = trim ($option);
   $sql = "SELECT `VALUE` FROM `{$tables['config']['name']}` WHERE `ID` = ".$db->qstr($option);

   return $db->GetOne($sql);
}

/**
 * Read language files, and get the language
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function select_lang($dirname = '../lang/')
{
   $lang  = array ();
   $files = array ();

   // Get language files of directory
   $extension = "php";
   $extension = str_replace (" ", "", $extension);
   $ext = explode (",", $extension);
   if ($handle = @ opendir ($dirname))
   {
      while (false !== ($file = readdir ($handle)))
         for ($i = 0; $i < sizeof ($ext);$i++)
            if (strstr ($file, ".".$ext[$i]) && stristr ($file, '~') === false && !empty ($ext[$i]))
               $files[] = $file;
      @ closedir ($handle);
   }

   //Select needed file info
   foreach ($files as $key => $f)
   {
      if (is_readable ($dirname.$f))
      {
         $lang_file_info = language_file_data($dirname.$f);
         $arr_key = substr ($f, 0, -4);
         if (isset ($lang_file_info['LANGUAGE']) && !empty ($lang_file_info['LANGUAGE']))
            $lang[$arr_key] = ucfirst ($lang_file_info['LANGUAGE']);
         else
            $lang[$arr_key] = ucfirst (substr ($f, 0, -4));
         unset ($f);
      }
   }

   unset ($file,$files, $dirname, $ext, $extension, $handle, $language, $i, $key, $arr_key, $f);
   natcasesort ($lang);

   return $lang;
}

/**
 * Make safe slashes
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function txt_stripslashes($string='')
{
   $string = stripslashes ($string);
   $string = preg_replace ("/\\\(?!&amp;#|\?#)/", "&#092;", $string);

   return $string;
}

/**
 * Removes special entities
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function un_html_specialchars($string='')
{
   return strtr ($string, array_flip (get_html_translation_table (HTML_SPECIALCHARS, ENT_QUOTES)) + array ('&#039;' => '\'', '&nbsp;' => ' '));
}

/**
 * Clean/handle white-space chars or given ressource (array|string)
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 *
 * @param  mixed [array|string]
 * @return mixed [array|string]
 */
function filter_white_space($ressource)
{
   if (!empty ($ressource))
   {
      if (is_array ($ressource))
      {
         foreach ($ressource as $key => $value)
         {
            if (is_string ($value))
               $ressource[$key] = clean_str_white_space($value);
         }

         return $ressource;
      }
      elseif (is_string ($ressource))
      {
         $ressource = clean_str_white_space($ressource);
         return $ressource;
      }
   }
   else
   {
      return false;
   }
}

/**
 * Clean a string of unneeded white-space chars
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function clean_str_white_space($string='')
{
   //Windows to *nix
   $string = str_replace ("\r\n"  , "\n"      , $string);
   //Mac to *nix
   $string = str_replace ("\r"    , "\n"      , $string);
   //TABs
   $string = str_replace ("\t"    , " "       , $string);
   //NULL BYTE
   $string = str_replace ("\0"    , ""        , $string);
   //Vertical TABs
   $string = str_replace ("\x0B"  , ""        , $string);
   //Multiple white-space chars
   //$string = preg_replace ('#[\s]+#i', ' '       , $string);
   $string = trim ($string);

   return $string;
}

/**
 * Clean a string of all white-space chars, except simple space only once
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 *
 * @param string Text to be processed
 * @param string String with which to replace white-space characters
 * @return string clean text
 */
function strip_white_space($string='', $replace=' ')
{
   //Remove all kind of white-space chars
   $search = array ( "\n",   //*NIX
                     "\r",   //Mac
                     "\r\n", //Windows
                     "\t",   //Tab
                     "\x0B", //Vertical Tab
                     "\0"    //NULL BYTE
                 );
   $string = str_replace ($search, '', $string);

   //Remove multiple white-spaces
   $string = preg_replace ('#[\s]+#i', $replace, $string);

   $string = trim ($string);
   return $string;
}

/**
 * Clean a string of unneeded chars and code
 * This is really a paranoic cleaner, but better to be paranoic than to cry later
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function clean_string($string='')
{
   $string = strip_tags ($string); /* No HTML and/or PHP tags */
   //$string = clean_javascript_tags ($string);
   $string = clean_str_white_space($string);
   $string = trim ($string);

   return $string;
}

/**
 * Remove bad and unneeded chars of a string
 * This is really a paranoic cleaner, but better to be paranoic than to cry later
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function clean_string_paranoia($string='')
{
   $string = clean_string ($string);
   $string = str_replace ("&nbsp; ", "" , $string);  /* TAB             */
   $string = str_replace ("\n"     , "" , $string);  /* *NIX            */
   $string = str_replace ("\r\n"   , "" , $string);  /* Windows to *nix */
   $string = str_replace ("\r"     , "" , $string);  /* Mac to *nix     */
   $string = trim ($string);

   return $string;
}

/**
 * Make clean javascript codes
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function safe_javascript_tags($string='')
{
   $string = preg_replace ("/javascript/i" , "j&#097;v&#097;script", $string);
   $string = preg_replace ("/alert/i"      , "&#097;lert"          , $string);
   $string = preg_replace ("/about:/i"     , "&#097;bout:"         , $string);
   $string = preg_replace ("/onmouseover/i", "&#111;nmouseover"    , $string);
   $string = preg_replace ("/onclick/i"    , "&#111;nclick"        , $string);
   $string = preg_replace ("/onload/i"     , "&#111;nload"         , $string);
   $string = preg_replace ("/onsubmit/i"   , "&#111;nsubmit"       , $string);
   $string = preg_replace ("/<body/i"      , "&lt;body"            , $string);
   $string = preg_replace ("/<html/i"      , "&lt;html"            , $string);
   $string = preg_replace ("/document\./i" , "&#100;ocument."      , $string);

   return $string;
}

/**
 * Remove javascript codes
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function clean_javascript_tags($string='')
{
   $string = preg_replace ("/javascript/i" , "", $string);
   $string = preg_replace ("/alert/i"      , "", $string);
   $string = preg_replace ("/about:/i"     , "", $string);
   $string = preg_replace ("/onmouseover/i", "", $string);
   $string = preg_replace ("/onclick/i"    , "", $string);
   $string = preg_replace ("/onload/i"     , "", $string);
   $string = preg_replace ("/onsubmit/i"   , "", $string);
   $string = preg_replace ("/<body/i"      , "", $string);
   $string = preg_replace ("/<html/i"      , "", $string);
   $string = preg_replace ("/document\./i" , "", $string);

   return $string;
}

/**
 * Clean search queries for all unneeded code and chars
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function clean_search_query($string='')
{
   $pattern = array (/*'`([^-+"\s\w])`',*/ '`[+]{2,}`', '`[-]{2,}`', '`["]{2,}`');
   $replace = array (/*''              ,*/ '+'        , '-'        , '"'        );

   $string = clean_string_paranoia ($string);
   $string = preg_replace ($pattern, $replace, $string); /* Remove unneded chars */
   $string = stripslashes ($string);

   return $string;
}

/**
 * Clean search queries for all unneeded code and chars
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function clean_search_location($string='')
{
   $pattern = array ('`([^+\w])`', '`[+]{2,}`');
   $replace = array (''          , '+'        );

   $string = clean_string_paranoia ($string);
   $string = str_replace  ("/\s"   , "+"     , $string);
   $string = preg_replace ($pattern, $replace, $string); /* Remove unneded chars */
   $string = stripslashes ($string);

   return $string;
}

/**
 * Clean META keywords and truncate to maximum length allowed (=1024)
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function clean_meta_keywords($keywords='')
{
   global $phpldSettings;

   $maxLength = (isset ($phpldSettings['META_KEYWORDS_MAX_LENGTH']) ? intval ($phpldSettings['META_KEYWORDS_MAX_LENGTH']) : 1024);

   //Remove all kind of white-space chars
   $pattern = array ( "\n",   //*NIX
                     "\r",   //Mac
                     "\r\n", //Windows
                     "\t",   //Tab
                     "\x0B", //Vertical Tab
                     "\0"    //NULL BYTE
                 );
   $keywords = str_replace ($pattern, '', $keywords);

   $keywords = trim ($keywords);

   if (strlen ($keywords) > $maxLength) //Shorten if longer than allowed (1024 chars)
   {
      $keywords = substr ($keywords, 0 , $maxLength);
      $last_pos = strrpos ($keywords, ',');
      if ($last_pos > 0)
         $keywords = substr ($keywords, 0 , $last_pos);
   }

   //Clean multiple spaces, commas, etc
   $pattern  = array ( '/\s+/', '`[,]+[\s]*`', '`[,]+`', '`([,]*[\s]*)$`', '`^[,]*`', '`[,]+$`' );
   $replace  = array ( ' '    , ','          , ','     , ''              , ''       , ''        );
   $keywords = preg_replace ($pattern, $replace, $keywords);

   if (empty ($keywords))
      return '';

   //use lowercase chars only
   $keywords = strtolower ($keywords);

   //Build an array of keywords
   $returnKeywords = explode (',', $keywords);

   if (is_array ($returnKeywords) && !empty ($returnKeywords))
   {
      //Keep only unique keywords
      $returnKeywords = array_unique ($returnKeywords);

      //Join back array into a string
      $keywords = implode (',', $returnKeywords);
   }

   unset ($returnKeywords, $pattern, $replace, $maxLength);

   return trim ($keywords);
}

/**
 * Un-Escape a string
 * @param  string Escaped string
 * @return string Unescaped string
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function un_escape($string)
{
   $trans_tbl = get_html_translation_table (HTML_ENTITIES);
   $trans_tbl = array_flip ($trans_tbl);
   return strtr ($string, $trans_tbl);
}

/**
 * Get information of language filesize
 * Modified WordPress plugins-style
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function language_file_data($lang_file)
{
   $lang_file = implode ('', file ($lang_file));

   preg_match ("|Language:(.*)|i"                , $lang_file, $lang       );
   preg_match ("|Language File Author:(.*)|i"    , $lang_file, $author_name);
   preg_match ("|Language File Author URL:(.*)|i", $lang_file, $author_URL );

   /* Remove unused vars and clean the information we get */
   unset ($lang_file,$lang[0],$author_name[0],$author_URL[0]);
   $lang[1]        = clean_string_paranoia($lang[1]);
   $author_name[1] = clean_string_paranoia($author_name[1]);
   $author_URL[1]  = clean_string_paranoia($author_URL[1]);

   return array ('LANGUAGE' => $lang[1], 'AUTHOR_NAME' => $author_name[1], 'AUTHOR_URL' => $author_URL[1]);
}

/**
 * Encode a password using "sha1" or "md5" depending on PHP version
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function encrypt_password($password='')
{
   if (empty($password))
      return false;

   if (version_compare (phpversion(), "4.3.0", ">=") && function_exists ('sha1'))
      return "{sha1}".sha1($password);
   else
      return "{md5}".md5($password);
}

/**
 * Read content of file
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function filecontent($file)
{
   $handle = @ fopen ($file, "r");
   $contents = '';
   if (!$handle)
      return false;
   else
      while (!feof ($handle))
         $contents .= @ fread ($handle, 8192);

   @ fclose ($handle);

   return $contents;
}

/**
 * Open a remote URL
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function remote_fopen($url)
{
   if (ini_get ('allow_url_fopen'))
   {
      $fp = @ fopen ($url, 'r');
      if (!$fp)
         return false;

      $linea = '';
      while ($remote_read = @ fread ($fp, 4096))
         $linea .= $remote_read;

      @ fclose ($fp);
      return $linea;
   }
   elseif(function_exists('curl_init'))
   {
      $handle = @ curl_init();
      @ curl_setopt ($handle, CURLOPT_URL, $url);
      @ curl_setopt ($handle, CURLOPT_CONNECTTIMEOUT, 1);
      @ curl_setopt ($handle, CURLOPT_RETURNTRANSFER, 1);
      $buffer = @ curl_exec ($handle);
      @ curl_close ($handle);
      return $buffer;
   }
   else
      return false;
}

/**
 * Write string to file
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 * @param  string  Filename that is going to be written
 * @param  string  String that is going to be written to file
 * @param  boolean TRUE if filepointer is going to be put on first position
 * @param  boolean TRUE if write mode is append, FALSE if regular mode. Overrides the "truncate" parameter.
 */
function write_to_file($filename, $string='', $truncate=false, $append=false)
{
   if ($append == true)
      $truncate = false;

   $mode = ($append == true ? 'a' : 'w');

   // Check if file exists
   if (!file_exists ($filename))
      return false;

   // Check if file is writeable
   if (!is_writable ($filename))
      @ chmod ($filename, 0666); // Try to give writing permissions

   if (!is_writable ($filename))
      return false;

   // Open file
   if (!$f = @ fopen ($filename, $mode))
      return false;

   if ($truncate == true)
   {
      // Truncate file
      @ ftruncate ($f, '0');
   }

   // Write content to file
   if (@ fwrite ($f, $string) === false)
      return false;

   // Close file
   if (!@ fclose ($f))
      return false;

   clearstatcache();

   return true;
}

/**
 * Detect web server software (Apache or IIS)
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function detect_server_software()
{
   //Server detection
   define ('IS_APACHE', ( strstr($_SERVER['SERVER_SOFTWARE'], 'Apache') || strstr($_SERVER['SERVER_SOFTWARE'], 'LiteSpeed') ) ? 1 : 0);
   define ('IS_IIS'   , strstr ($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') ? 1 : 0);
}

/**
 * HTTP Redirect without message
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function http_custom_redirect($url=SITE_URL, $option='', $timeout=0, $cust_msg='', $status = 302)
{
   //No NULL chars in URL
   $url = preg_replace ('/\0+/', '', $url);
   $url = preg_replace ('/(\\\\0)+/', '', $url);

   //No whitespace in URL
   $url = strip_white_space($url);

   //More cleaning
   $url = trim ($url);
   $url = str_replace ('&amp;', '&', $url);

   $option = strtolower ($option);

   $cust_msg = trim ($cust_msg);

   if (defined ('IS_IIS'))
      $is_IIS = IS_IIS;
   else
      $is_IIS = (strstr ($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') ? 1 : 0);

   if ($is_IIS == 1)
   {
      //Seems like for Ms. IIS this works better
      $option = 'refresh';
   }

   if ($option == 'refresh')
   {
      @ header ("Refresh: {$timeout};url=".$url);
   }
   elseif ($option == 'html')
   {
      echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
      echo "\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n";
      echo "<head>\n";
      echo "<title>"._L('Redirecting...')."</title>\n";
      echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
      echo '<link rel="stylesheet" type="text/css" href="files/main.css" />';
      echo "\n<meta http-equiv='refresh' content='{$timeout}; url={$url}' />";
      echo "\n</head>\n<body>\n";
      echo "<div class=\"redirect\">{$cust_msg}</div>\n";
      echo "</body>\n</html>";

      exit();
   }
   elseif ($option == 'javascript')
   {
      echo javascript_redirect($url, $timeout);
   }
   else
   {
      if (php_sapi_name() != 'cgi-fcgi')
      {
         //IIS and sometimes FastCGI cause problems
         httpstatus($status);
      }
     // @ header ("Location: " . $url);
 @ header ("Location: " . $url, TRUE, $status);
   }

   exit();
}

/**
 * Make a JavaScript redirect with custom message
 * @param string  $url       URL where to redirect
 * @param integer $timeout   redirect timeout
 * @param string  $title_msg page and header title of redirect page
 * @param string  $cust_msg  custom message to be displayed on redirect page
 * @param integer $status    status of current work (-1 = Error; 0 = neutral/still working; 1 = OK/finished )
 *
 * @return string           full HTML source of redirect page
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function javascript_redirect($url='', $timeout=0, $title_msg='', $cust_msg='', $status=0)
{
   $url = trim ($url);
   $url = str_replace('&amp;', '&', $url);

   $title_msg = (!empty ($title_msg) ? trim ($title_msg) : _L('Redirecting...'));
   $cust_msg  = trim ($cust_msg);
   $status_css_class = ($status == 1 ? 'complete' : ($status == -1 ? 'failed' : 'regular'));

   echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
   echo "\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n";
   echo "<head>\n";
   echo "<title>".strip_tags ($title_msg)."</title>\n";
   echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
   echo '<link rel="stylesheet" type="text/css" href="'.TEMPLATE_ROOT.'/files/style.css" />';
   echo "\n</head>\n<body>\n<div id=\"wrap\" class=\"{$status_css_class}\">";
   echo "<div class=\"redirect\">";

   if ($timeout == 0)
   {
      echo '<h2 class="redirect-title">'.$title_msg.'</h2>';
      echo '<p class="redirect-url"><a href="'.$url.'" title="'._L('Click here if your browser does not automatically redirect you.').'">'._L('Click here if your browser does not automatically redirect you.').'</a></p>';
      echo "\n<script type=\"text/javascript\">/* <![CDATA[ */\n";
      echo "window.location=\"{$url}\";";
      echo "\n/* ]]> */</script>\n";
   }
   else
   {
      echo "\n<script type=\"text/javascript\">/* <![CDATA[ */\n";
      echo "timeout = ".($timeout * 10).";
      function do_refresh()
      {
         window.status=\"".$title_msg."\";
         timerID = setTimeout(\"do_refresh();\", 100);
         if (timeout > 0)
         {
            timeout -= 1;
         }
         else
         {
            clearTimeout(timerID);
            window.status=\"{$title_msg}\";
            window.location=\"{$url}\";
         }
      }
      do_refresh();";
      echo "\n/* ]]> */</script>\n";
      echo '<h2 class="redirect-title">'.$title_msg.'</h2>';
      echo '<p class="redirect-url"><a href="'.$url.'" title="'._L('Click here if your browser does not automatically redirect you.').'" onclick="javascript:clearTimeout(timerID);">'._L('Click here if your browser does not automatically redirect you.').'</a></p>';
   }

   echo "\n<div class=\"block\">{$cust_msg}</div>\n";
   echo "\n</div>";
   echo "\n</body>";
   echo "\n</html>";

   exit();
}


/**
 * Put each search keyword into an array element
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function make_search_keywords($keywords, $separator=" ")
{
   $keywords_array = array ($keywords);
   $keywords_array = array_merge ($keywords_array, explode (" ", $keywords));
   foreach ($keywords_array as $key => $keyw)
   {
      $keywords_array[$key] = trim ($keyw);
      if (empty ($keywords_array[$key]) || strlen ($keyw) < 3)
         unset ($keywords_array[$key]);
   }

   return array_unique ($keywords_array);
}

function get_regular_categs_tree($id=0)
{
   global $db, $tables;
   static $categs = array ("0" => "[Top]");
   static $level = 0;
   $level++;
   $rs = $db->CacheExecute("SELECT `ID`, `TITLE` FROM `{$tables['category']['name']}` WHERE `PARENT_ID` = ".$db->qstr($id)." AND `STATUS` = '2' AND `SYMBOLIC` <> '1' ORDER BY `TITLE`");

   while (!$rs->EOF)
   {
      $categs[$rs->Fields('ID')] = str_repeat ('|&nbsp;&nbsp;&nbsp;', $level -1).'|___'.$rs->Fields('TITLE');
      get_regular_categs_tree($rs->Fields('ID'));
      $rs->MoveNext();
   }
   $level--;

   return $categs;
}
function get_regular_categs_tree_opentoarticles($id=0)
{
   global $db, $tables;
   static $categs = array ("0" => array("val" => "[Top]", "closed" => 1) );
   static $level = 0;
   $level++;
   $rs = $db->CacheExecute("SELECT `ID`, `TITLE`, `CLOSED_TO_ARTICLES` FROM `{$tables['category']['name']}` WHERE `PARENT_ID` = ".$db->qstr($id)." AND `STATUS` = '2' AND `SYMBOLIC` <> '1' ORDER BY `TITLE`");

   while (!$rs->EOF)
   {
      $categs[$rs->Fields('ID')]['val'] = str_repeat ('|&nbsp;&nbsp;&nbsp;', $level -1).'|___'.$rs->Fields('TITLE');
      $categs[$rs->Fields('ID')]['closed'] = $rs->Fields('CLOSED_TO_ARTICLES');  
      
      get_regular_categs_tree_opentoarticles($rs->Fields('ID'));
      $rs->MoveNext();
   }
   $level--;

   return $categs;
}
function get_regular_categs_tree_opentolinks($id=0)
{
   global $db, $tables;
   static $categs = array ("0" => array("val" => "[Top]", "closed" => 1) );
   static $level = 0;
   $level++;
   $rs = $db->CacheExecute("SELECT `ID`, `TITLE`, `CLOSED_TO_LINKS` FROM `{$tables['category']['name']}` WHERE `PARENT_ID` = ".$db->qstr($id)." AND `STATUS` = '2' AND `SYMBOLIC` <> '1' ORDER BY `TITLE`");

   while (!$rs->EOF)
   {
      $categs[$rs->Fields('ID')]['val'] = str_repeat ('|&nbsp;&nbsp;&nbsp;', $level -1).'|___'.$rs->Fields('TITLE');
      $categs[$rs->Fields('ID')]['closed'] = $rs->Fields('CLOSED_TO_LINKS');  
   	
      get_regular_categs_tree_opentolinks($rs->Fields('ID'));
      $rs->MoveNext();
   }
   $level--;

   return $categs;
}
function get_categs_tree($id=0)
{
   global $db, $tables;

   static $categs = array();
   //Add TOP category only if editor is allowed
   if ($id == 0)
   {
//      if ($_SESSION['phpld']['adminpanel']['is_admin'] || in_array($id, $_SESSION['phpld']['adminpanel']['permission_array']))
         $categs[0] = "[Top]";
   }

   static $level = 0;
   $level++;
   $rs = $db->Execute("SELECT `ID`, `TITLE` FROM `{$tables['category']['name']}` WHERE `PARENT_ID` = ".$db->qstr($id)." AND `STATUS` = '2' AND `SYMBOLIC` <> '1' ORDER BY `TITLE`");

   while(!$rs->EOF)
   {
      if(empty($_SESSION['phpld']['adminpanel']['id']) || $_SESSION['phpld']['adminpanel']['is_admin'])
         $categs[$rs->Fields('ID')] = str_repeat('|&nbsp;&nbsp;&nbsp;', $level -1).'|___'.$rs->Fields('TITLE');
      else
//         if(in_array($rs->Fields('ID'), $_SESSION['phpld']['adminpanel']['permission_array']))
            $categs[$rs->Fields('ID')] = str_repeat('|&nbsp;&nbsp;&nbsp;', $level -1).'|___'.$rs->Fields('TITLE');
      get_categs_tree($rs->Fields('ID'));
      $rs->MoveNext();
   }
   $level--;
   
   return $categs;
}

function get_categs_tree_frontend_forlinks($id=0)
{
   global $db, $tables;

   static $categs = array();

   if ((isset($_SESSION['phpld']['user']['level'])
         && (($_SESSION['phpld']['user']['level'] == 1) || ($_SESSION['phpld']['user']['level'] == 3)))) {
        $opentolinks = "1 ";
   } else {
        $opentolinks = "`CLOSED_TO_LINKS` = 0 ";
   }
   
   static $level = 0;
   $level++;
   $rs = $db->Execute("SELECT `ID`, `TITLE` FROM `{$tables['category']['name']}` WHERE `PARENT_ID` = ".$db->qstr($id)." AND `STATUS` = '2' AND ".$opentolinks." AND `SYMBOLIC` <> '1' ORDER BY `TITLE`");

   while(!$rs->EOF)
   {
      if(empty($_SESSION['phpld']['adminpanel']['id']) || $_SESSION['phpld']['adminpanel']['is_admin'])
         if ((isset($_SESSION['phpld']['user']['level'])
             && (($_SESSION['phpld']['user']['level'] == 1) || ($_SESSION['phpld']['user']['level'] == 3)))
        || (isset($_SESSION['phpld']['user']['id']) &&
            has_rights_on_cat($_SESSION['phpld']['user']['id'], $rs->Fields('ID'))))  {
            $categs[$rs->Fields('ID')] = str_repeat('|&nbsp;&nbsp;&nbsp;', $level -1).'|___'.$rs->Fields('TITLE');
            }
      else
         if(in_array($rs->Fields('ID'), $_SESSION['phpld']['adminpanel']['permission_array']))
            $categs[$rs->Fields('ID')] = str_repeat('|&nbsp;&nbsp;&nbsp;', $level -1).'|___'.$rs->Fields('TITLE');
     
    if ((isset($_SESSION['phpld']['user']['level'])
         && (($_SESSION['phpld']['user']['level'] == 1) || ($_SESSION['phpld']['user']['level'] == 3)))
    || (isset($_SESSION['phpld']['user']['id']) &&
        has_rights_on_cat($_SESSION['phpld']['user']['id'], $rs->Fields('ID')))) {
        get_categs_tree_frontend_forlinks($rs->Fields('ID'));
    }
      
      $rs->MoveNext();
   }
   $level--;

   return $categs;
}

function get_categs_tree_frontend_forarts($id=0)
{
   global $db, $tables;

   static $categs = array();

   if ((isset($_SESSION['phpld']['user']['level'])
         && (($_SESSION['phpld']['user']['level'] == 1) || ($_SESSION['phpld']['user']['level'] == 3)))) {
        
   } else {
        //$opentoarts = "`CLOSED_TO_ARTICLES` = 0 ";
   }
   $opentoarts = "1 ";
   static $level = 0;
   $level++;
   $rs = $db->Execute("SELECT `ID`, `TITLE` FROM `{$tables['category']['name']}` WHERE `PARENT_ID` = ".$db->qstr($id)." AND `STATUS` = '2' AND ".$opentoarts." AND `SYMBOLIC` <> '1' ORDER BY `TITLE`");

   while(!$rs->EOF)
   {
      if(empty($_SESSION['phpld']['adminpanel']['id']) || $_SESSION['phpld']['adminpanel']['is_admin'])
         if ((isset($_SESSION['phpld']['user']['level'])
             && (($_SESSION['phpld']['user']['level'] == 1) || ($_SESSION['phpld']['user']['level'] == 3)))
        || (isset($_SESSION['phpld']['user']['id']) &&
            has_rights_on_cat($_SESSION['phpld']['user']['id'], $rs->Fields('ID'))))  {
            $categs[$rs->Fields('ID')] = str_repeat('|&nbsp;&nbsp;&nbsp;', $level -1).'|___'.$rs->Fields('TITLE');
            }
      else
         if(in_array($rs->Fields('ID'), $_SESSION['phpld']['adminpanel']['permission_array']))
            $categs[$rs->Fields('ID')] = str_repeat('|&nbsp;&nbsp;&nbsp;', $level -1).'|___'.$rs->Fields('TITLE');
     
    if ((isset($_SESSION['phpld']['user']['level'])
         && (($_SESSION['phpld']['user']['level'] == 1) || ($_SESSION['phpld']['user']['level'] == 3)))
    || (isset($_SESSION['phpld']['user']['id']) &&
        has_rights_on_cat($_SESSION['phpld']['user']['id'], $rs->Fields('ID')))) {
        get_categs_tree_frontend_forarts($rs->Fields('ID'));
    }
      
      $rs->MoveNext();
   }
   $level--;

   return $categs;
}


function get_categs_tree_frontend($id=0, $cur)
{
   global $db, $tables;
   
   static $categs = array();
   //Add TOP category only if editor is allowed
   if ($id == 0)
   {
         $categs[0] = "[Top]";
   }

   static $level = 0;
   $level++;
   $rs = $db->Execute("SELECT `ID`, `TITLE` FROM `{$tables['category']['name']}` WHERE `PARENT_ID` = ".$db->qstr($id)." AND `STATUS` = '2' AND `SYMBOLIC` !='1' AND `ID`!=".$db->qstr($cur)." ORDER BY `TITLE`");

   
   
   while(!$rs->EOF)
   {
   	 if(empty($_SESSION['phpld']['adminpanel']['id']) || $_SESSION['phpld']['adminpanel']['is_admin'])
         if ((isset($_SESSION['phpld']['user']['level'])
             && (($_SESSION['phpld']['user']['level'] == 1) || ($_SESSION['phpld']['user']['level'] == 3)))
        || (isset($_SESSION['phpld']['user']['id']) &&
            has_rights_on_cat($_SESSION['phpld']['user']['id'], $rs->Fields('ID'))))  {
            $categs[$rs->Fields('ID')] = str_repeat('|&nbsp;&nbsp;&nbsp;', $level -1).'|___'.$rs->Fields('TITLE');
            }
      else
         if(in_array($rs->Fields('ID'), $_SESSION['phpld']['adminpanel']['permission_array']))
            $categs[$rs->Fields('ID')] = str_repeat('|&nbsp;&nbsp;&nbsp;', $level -1).'|___'.$rs->Fields('TITLE');
      if ($cur != $rs->Fields('ID')) {
        if ((isset($_SESSION['phpld']['user']['level'])
             && (($_SESSION['phpld']['user']['level'] == 1) || ($_SESSION['phpld']['user']['level'] == 3)))
        || (isset($_SESSION['phpld']['user']['id']) &&
            has_rights_on_cat($_SESSION['phpld']['user']['id'], $rs->Fields('ID')))) {
            get_categs_tree_frontend($rs->Fields('ID'), $cur);
        }
      }
      $rs->MoveNext();
   }
   $level--;
   
   return $categs;
}


function get_categs_tree_backend($id=0, $cur)
{
   global $db, $tables;
   
   static $categs = array();
   //Add TOP category only if editor is allowed
   if ($id == 0)
   {
         $categs[0] = "[Top]";
   }

   static $level = 0;
   $level++;
   $rs = $db->Execute("SELECT `ID`, `TITLE` FROM `{$tables['category']['name']}` WHERE `PARENT_ID` = ".$db->qstr($id)." AND `STATUS` = '2' AND `SYMBOLIC` !='1' AND `ID`!=".$db->qstr($cur)." ORDER BY `TITLE`");
   
   while(!$rs->EOF)
   {

   	 if(empty($_SESSION['phpld']['adminpanel']['id']) || $_SESSION['phpld']['adminpanel']['is_admin'])
            $categs[$rs->Fields('ID')] = str_repeat('|&nbsp;&nbsp;&nbsp;', $level -1).'|___'.$rs->Fields('TITLE');
      else
         if(in_array($rs->Fields('ID'), $_SESSION['phpld']['adminpanel']['permission_array']))
            $categs[$rs->Fields('ID')] = str_repeat('|&nbsp;&nbsp;&nbsp;', $level -1).'|___'.$rs->Fields('TITLE');
      if ($cur != $rs->Fields('ID')) {
            get_categs_tree_backend($rs->Fields('ID'), $cur);
      }
      $rs->MoveNext();
   }
   $level--;
   
   return $categs;
}

function get_grant_categs_tree($id=0)
{
   global $db, $tables;

   static $categs = array();
   //Add TOP category only if editor is allowed
   if ($id == 0)
   {
      if ($_SESSION['phpld']['adminpanel']['is_admin'] || in_array($id, $_SESSION['phpld']['adminpanel']['permission_array']))
         $categs[0] = "[Top]";
   }

   static $level = 0;
   $level++;
   $rs = $db->Execute("SELECT `ID`, `TITLE` FROM `{$tables['category']['name']}` WHERE `PARENT_ID` = ".$db->qstr($id)." AND `SYMBOLIC` <> 1 ORDER BY `TITLE`");

   while (!$rs->EOF)
   {
      if ($_SESSION['phpld']['adminpanel']['is_admin'])
         $categs[$rs->Fields('ID')] = str_repeat ('|&nbsp;&nbsp;&nbsp;', $level -1).'|___'.$rs->Fields('TITLE');
      else
         if (in_array ($rs->Fields('ID'), $_SESSION['phpld']['adminpanel']['grant_permission_array']))
            $categs[$rs->Fields('ID')] = str_repeat ('|&nbsp;&nbsp;&nbsp;', $level -1).'|___'.$rs->Fields('TITLE');

      get_grant_categs_tree($rs->Fields('ID'));
      $rs->MoveNext();
   }
   $level--;

   return $categs;
}

/**
 * [DEPRECATED]
 * Use "$client_info['IP']" instead
 *
 * Get IP address of current visitor
 */
function get_client_ip()
{
   $ipAddress = (!empty ($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : ((!empty ($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : @ getenv ('REMOTE_ADDR'));

   return $ipAddress;
}

function get_page($list_total)
{
   if (!isset ($_SESSION['p']))
      $_SESSION['p'] = array ();

   $page = isset ($_REQUEST['p']) ? $_REQUEST['p'] : (isset ($_SESSION[SCRIPT_NAME]['p']) ? $_SESSION[SCRIPT_NAME]['p'] : 1);
   if(($page -1) > ($list_total / LINKS_PER_PAGE))
      $page = floor ($list_total / LINKS_PER_PAGE) + 1;

   $_SESSION[SCRIPT_NAME]['p'] = $page;
   $_REQUEST['p'] = $page;
   return $page;
}

function request_uri()
{
   if ($_SERVER['REQUEST_URI'])
      return $_SERVER['REQUEST_URI'];

   // IIS with ISAPI_REWRITE
   if ($_SERVER['HTTP_X_REWRITE_URL'])
      return $_SERVER['HTTP_X_REWRITE_URL'];
   $p = $_SERVER['SCRIPT_NAME'];
   if ($_SERVER['QUERY_STRING'])
      $p .= '?'.$_SERVER['QUERY_STRING'];
   return $p;
}

/**
 * Check if current page is valid or invalid and return HTTP status code to send header
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function request_status()
{
   global $db, $tables;
   $status = true;

   $output_status_valid   = (FORCE_HTTP_STATUS_OK == 1         ? 200 : 0  );
   $output_status_invalid = (FORCE_INVALID_HTTP_STATUS_OK == 1 ? 200 : 404);

   if (isset ($_REQUEST['c']))
      $cid = $_REQUEST['c'];

   $path = request_uri();
   $path = preg_replace ('#page-(\d+)\.htm[l](.*)?$#', '', $path);

   if (ENABLE_REWRITE && !isset ($cid))
   {
      $path = substr ($path, strlen (DOC_ROOT) + 1);
      $qp   = strpos ($path, '?');
      if ($qp !== false)
         $path = substr ($path, 0, $qp);

      $path = explode ('/', $path);
      foreach ($path as $key => $cat)
      {
         $cat = trim ($cat);
         if (empty ($cat) || $cat == 'index.php')
            unset ($path[$key]);
      }

      //Search for latest/top links, articles
      $listArrayRegexp = array ('latest(-|_)links\.htm[l]?', 'top(-|_)hits\.htm[l]?', 'top(-|_)links\.htm[l]?', 'latest(-|_)articles\.htm[l]?', 'top(-|_)articles\.htm[l]?');
      foreach ($path as $key)
      {
         foreach ($listArrayRegexp as $regex)
         {
            if (preg_match ('#'.$regex.'#i', $key))
            {
               return $output_status_valid;
            }
         }
      }

      $id = 0;
      foreach ($path as $cat)
      {
         $sql = "SELECT `ID` FROM `{$tables['category']['name']}` WHERE `STATUS` = '2' AND `TITLE_URL` = ".$db->qstr($cat)." AND `PARENT_ID` = ".$db->qstr($id);
         $id = $db->GetOne($sql);
         if (empty ($id))
            $status = false;
      }
   }
   elseif (preg_match ('`^[\d]+$`', $cid) && isset ($cid))
   {
      if ($cid != 0)
      {
         $id = $db->GetOne("SELECT `ID` FROM `{$tables['category']['name']}` WHERE `STATUS` = '2' AND `ID` = ".$db->qstr($cid));
         if (empty ($id))
            $status = false;
      }
   }


   return ($status == true ? $output_status_valid : $output_status_invalid);
}

function get_category($uri = NULL)
{
   global $db, $tables;
   
   if (isset($_SESSION['phpld']['user']['id'])) {
       if (get_user_level($_SESSION['phpld']['user']['id']) == 1 || get_user_level($_SESSION['phpld']['user']['id']) == 3 ) {
              $permission_links_arts = "1 ";
              $permission_cats = "1 ";
              $permission_cats_parents = "1 ";
       } else {
           $permissions = get_user_permissions($_SESSION['phpld']['user']['id']);
           $permission_links_arts = "`CATEGORY_ID` IN (";
           $permission_cats = "`ID` IN (";
           $permission_cats_parents = "`PARENT_ID` IN (";
           if (count($permissions) >0) {
               for ($i=0; $i<count($permissions); $i++) {
                   if ($i<count($permissions)-1) {
                        $permission_links_arts .= "'".$permissions[$i]['CATEGORY_ID']."', ";
                        $permission_cats .= "'".$permissions[$i]['CATEGORY_ID']."', ";
                        $permission_cats_parents .= "'".$permissions[$i]['CATEGORY_ID']."', ";
                   } else {
                        $permission_links_arts .= "'".$permissions[$i]['CATEGORY_ID']."') ";
                        $permission_cats .= "'".$permissions[$i]['CATEGORY_ID']."') ";
                        $permission_cats_parents .= "'".$permissions[$i]['CATEGORY_ID']."') ";
                   }
               }
           } else {
               $permission_links_arts = "0 ";
               $permission_cats = "0 ";
               $permission_cats_parents = "0 ";
           }
       }
    } else {
       $permission_links_arts = "0 ";
       $permission_cats = "0 ";
       $permission_cats_parents = "0 ";
    }
   
   if ($uri != NULL)
   {
      $uri = parse_url ($uri);
      $query = $uri['query'];
      $path = $uri['path'];
      parse_str ($query, $vars);
      if (isset ($vars['c']))
         $cid = $vars['c'];
   }
   else
   {
      if (isset ($_REQUEST['c']))
         $cid = $_REQUEST['c'];
      $path = request_uri();
   }
   $id = 0;
   if (ENABLE_REWRITE && !isset ($cid))
   {
      $path = substr ($path, strlen (DOC_ROOT) + 1);
      $qp   = strpos ($path, '?');
      if ($qp !== false)
      {
         $path = substr ($path, 0, $qp);
      }
      $path = preg_replace ('`page-(\d+)\.htm[l]([^\/]*)?$`', '', $path);

      $path = explode ('/', $path);

      $id = 0;
      foreach($path as $cat)
      {
         if (!empty ($cat))
        
            $id = $db->GetOne("SELECT `ID` FROM `{$tables['category']['name']}` WHERE (`STATUS` = '2' OR {$permission_cats} OR {$permission_cats_parents}) AND `TITLE_URL` = ".$db->qstr($cat)." AND `PARENT_ID` = ".$db->qstr($id));
      }

   }
   elseif (preg_match ('`^[\d]+$`', $cid))
   
      $id = $db->GetOne("SELECT `ID` FROM `{$tables['category']['name']}` WHERE (`STATUS` = '2' OR {$permission_cats} OR {$permission_cats_parents}) AND `ID` = ".$db->qstr($cid));

   return ($id ? $id : '0');
}

function get_path($id)
{
   global $db, $tables;

   $path = array ();
   $i = 0;
   while ($id != 0)
   {
      $row = $db->GetRow("SELECT * FROM `{$tables['category']['name']}` WHERE `ID` = ".$db->qstr($id));
      $id = $row['PARENT_ID'];
      $path[] = $row;
      $i++;
   }
   $path[] = array ('ID' => '0', 'TITLE' => _L(SITE_NAME), 'TITLE_URL' => DOC_ROOT, 'DESCRIPTION' => SITE_DESC);

   return array_reverse ($path);
}

define ('URL_RESPONSE', 0);
define ('URL_HEADERS' , 1);
define ('URL_CONTENT' , 2);

function get_url($url, $what = 0, $referer = "", $cookies = array (), $useragent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)")
{
   static $redirect_count = 0;
   $ret = array ();
   $ret['status'] = false;
   $timeout = 10;
   $urlArray = parse_url ($url);
   if (!$urlArray['port'])
   {
      if ($urlArray['scheme'] == 'http')
         $urlArray['port'] = 80;
      elseif ($urlArray['scheme'] == 'https')
         $urlArray['port'] = 443;
      elseif ($urlArray['scheme'] == 'ftp')
         $urlArray['port'] = 21;
   }
   if (!$urlArray['path'])
      $urlArray['path'] = '/';

   $errno = "";
   $errstr = "";
   $fp = @ fsockopen ($urlArray['host'].'.', $urlArray['port'], $errno, $errstr, $timeout);
   if ($fp)
   {
      $request = "GET {$urlArray['path']}";
      if (!empty ($urlArray['query']))
         $request .= "?".$urlArray['query'];

      $request .= " HTTP/1.1\r\n"."Host: {$urlArray['host']}\r\n"."User-Agent: {$useragent}\r\n";
      if (!empty ($referer))
      {
         $request .= "Referer: $referer\r\n";
      }
      $request .= "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1\r\n"."Accept-Language: en-us, en;q=0.50\r\n".
      #"Accept-Encoding: gzip, deflate, compress;q=0.9\r\n".
      //"Accept-Charset: ISO-8859-1, utf-8;q=0.66, *;q=0.66\r\n".
      #"Keep-Alive: 300\r\n".
      "Connection: close\r\n"."Cache-Control: max-age=0\r\n";
      foreach ($cookies as $k => $v)
         $request .= "Cookie: {$k}={$v}\r\n";

      $request .= "\r\n";
      @ fputs ($fp, $request);
      $ret['response'] = fgets($fp);
      if (preg_match ("`HTTP/1\.. (.*) (.*)`U", $ret['response'], $parts)) {
         $ret['status'] = $parts[1][0] == '2' || $parts[1][0] == '3';
         $ret['code'] = $parts[1];
         if ($what == URL_RESPONSE || !$ret['status'])
         {
            @ fclose ($fp);
            return $ret;
         }
         $ret['headers'] = array ();
         $ret['cookies'] = array ();
         while (!feof ($fp))
         {
            $header = @ fgets ($fp, 2048);
            if ($header == "\r\n" || $header == "\n" || $header == "\n\l")
               break;
            list ($key, $value) = explode(':', $header, 2);
            if (trim ($key) == 'Set-Cookie')
            {
               $value = trim ($value);
               $p1 = strpos ($value, '=');
               $p2 = strpos ($value, ';');
               $key = substr ($value, 0, $p1);
               $val = substr ($value, $p1 + 1, $p2 - $p1 - 1);
               $ret['cookies'][$key] = $val;
            }
            else
               $ret['headers'][trim ($key)] = trim ($value);
         }
         if (($ret['code'] == '301' || $ret['code'] == '302') && !empty ($ret['headers']['Location']) && $redirect_count < 20)
         {
            $redirect_count ++;
            @ fclose ($fp);
            if (strpos ($ret['headers']['Location'], 'http://') === 0 || strpos ($ret['headers']['Location'], 'http://'))
               $redir_url = $ret['headers']['Location'];
            elseif (strpos ($ret['headers']['Location'], '/') === 0)
               $redir_url = $urlArray['scheme']."://".$urlArray[host].$ret['headers']['Location'];
            else
               $redir_url = $urlArray['scheme']."://".$urlArray[host].$urlArray[path].$ret['headers']['Location'];

            return get_url($redir_url, $what, $url, $ret['cookies']);
         }
         $redirect_count = 0;
         if($what == URL_HEADERS)
         {
            @ fclose ($fp);
            return $ret;
         }
         $chunked = isset ($ret['headers']['Transfer-Encoding']) && ('chunked' == $ret['headers']['Transfer-Encoding']);
         while (!feof ($fp))
         {
            $data = '';
            if ($chunked)
            {
               $line = @ fgets ($fp, 128);
               if (preg_match ('/^([0-9a-f]+)/i', $line, $matches))
               {
                  $len = hexdec ($matches[1]);
                  if (0 == $len)
                     while (!feof ($fp))
                        @ fread ($fp, 4096);
                  else
                     $data = @ fread ($fp, $len);
               }
            }
            else
               $data = @ fread ($fp, 4096);

            $ret['content'] .= $data;
         }
      }
      else
         $errstr = "Bad Communication";

      @ fclose ($fp);
   }
   else { // Occurs when if ($fp) returns false
   }
   $ret['error'] = $errstr;

   return $ret;
}

function parse_news($str)
{
   $str = str_replace ("\r\n", "\n", $str);
   $str = str_replace ("\r", "\n", $str);
   $str = explode ("\n", $str);
   $news = array ();
   $len = count ($str);
   $i = 0;
   while ($i < $len)
   {
      if ($str[$i] == '')
      {
         $i ++;
         continue;
      }
      $n = array ();
      $n['title'] = $str[$i ++];
      $n['date'] = $str[$i ++];
      while ($i < $len &&$str[$i] != '')
         $n['body'] .= $str[$i ++]."\n";
      $news[] = $n;
      $i ++;
   }
   return $news;
}

function parse_version($val)
{
   preg_match ('`(\d+)\.(\d+)\.(\d+)\s*((RC)(\d+))?`', $val, $match);
   $ver = sf ("%02d%02d%02d%02d", $match[1], $match[2], $match[3], $match[6]);

   return $ver;
}

if (!function_exists ('file_get_contents'))
{
   function file_get_contents ($fn)
   {
      $len = filesize ($fn);
      if(!$len)
         return false;

      $fp = @ fopen ($fn, 'r');
      if ($fp)
         return @ fread ($fp, $len);
      else
         return false;
   }
}

function set_log($file)
{
   if (!ini_get ('safe_mode'))
   {
      @ ini_set ('display_errors', 0);
      @ ini_set ('log_errors', 1);
      @ ini_set ('error_log', INSTALL_PATH.'temp/'.$file);
      @ error_reporting(E_ALL^E_NOTICE);
   }
}

function replace_email_vars($text, $data, $type = 1)
{
   if ($type == 1)
      $prefix = 'EMAIL_';
   elseif ($type == 2)
      $prefix = 'LINK_';
   elseif ($type == 3)
      $prefix = 'PAYMENT_';
   elseif ($type == 5)
      $prefix = 'USER_';
    elseif ($type == 7)
      $prefix = 'ARTICLE_';

   $paymentURLArt = (substr (SITE_URL, -1, 1) == '/' ? SITE_URL : SITE_URL.'/') . 'article_payment.php?id='.intval ($data['ID']);
   $text = str_replace ('{ARTICLE_PAYMENT_URL}', $paymentURLArt, $text);   
      
   if ($type == 2)
   {
      if (!empty ($data['ID']))
      {
         if (ENABLE_REWRITE == 1)
            $detailPageUrl = "detail/link-{$data['ID']}.html";
         else
            $detailPageUrl = "detail.php?id={$data['ID']}";

			$detailPageUrl = seo_url($data['ID'], $data['TITLE'], REWRITE_TYPE, REWRITE_EXT, 2);
         //Build detail page URL
         $detailPageUrl = (substr (SITE_URL, -1, 1) == '/' ? SITE_URL.$detailPageUrl : SITE_URL.'/'.$detailPageUrl);

         $text = str_replace ('{LINK_DETAIL_PAGE}', $detailPageUrl, $text);

         unset ($detailPageUrl);
      }
      if (!empty ($data['CATEGORY_ID']))
      {
         $categData = getCategoryByID($data['CATEGORY_ID']);
         $categData['CACHE_URL']   = (!empty ($categData['CACHE_URL'])   ? $categData['CACHE_URL']   : buildCategUrl($data['CATEGORY_ID']));
         $categData['CACHE_TITLE'] = (!empty ($categData['CACHE_TITLE']) ? $categData['CACHE_TITLE'] : buildCategUrlTitle($data['CATEGORY_ID']));
         $categData['CACHE_URL']   = (substr (SITE_URL, -1, 1) == '/' ? SITE_URL.$categData['CACHE_URL'] : SITE_URL.'/'.$categData['CACHE_URL']);

         $text = str_replace ('{LINK_CATEGORY_URL}'   , $categData['CACHE_URL']    , $text);
         $text = str_replace ('{LINK_CATEGORY_TITLE}' , $categData['CACHE_TITLE']  , $text);

         unset ($categData);
      }
      if (!empty ($data['ID']))
      {
         $paymentURL = (substr (SITE_URL, -1, 1) == '/' ? SITE_URL : SITE_URL.'/') . 'payment.php?id='.intval ($data['ID']);
         $text = str_replace ('{LINK_PAYMENT_URL}', $paymentURL, $text);
         
      }
   }
   elseif ($type == 5)
   {
      $loginURL = (substr (SITE_URL, -1, 1) == '/' ? SITE_URL : SITE_URL.'/') . 'login.php';
      $text = str_replace ('{USER_LOGIN_PAGE}', $loginURL, $text);
   }
elseif ($type == 7)
   {
      if (!empty ($data['ID']))
      {
         if (ENABLE_REWRITE == 1)
            $articlePageUrl = "articles/article-{$data['ID']}.html";
         else
            $articlePageUrl = "article.php?id={$data['ID']}";

			$articlePageUrl = seo_url($data['ID'], $data['TITLE'], REWRITE_TYPE, REWRITE_EXT, 1);              
            
            
         //Build ARTICLE page URL
         $articlePageUrl = (substr (SITE_URL, -1, 1) == '/' ? SITE_URL.$articlePageUrl : SITE_URL.'/'.$articlePageUrl);

         $text = str_replace ('{ARTICLE_PAGE}', $articlePageUrl, $text);

         unset ($articlePageUrl);
      }
      if (!empty ($data['CATEGORY_ID']))
      {
         $categData = getCategoryByID($data['CATEGORY_ID']);
         $categData['CACHE_URL']   = (!empty ($categData['CACHE_URL'])   ? $categData['CACHE_URL']   : buildCategUrl($data['CATEGORY_ID']));
         $categData['CACHE_TITLE'] = (!empty ($categData['CACHE_TITLE']) ? $categData['CACHE_TITLE'] : buildCategUrlTitle($data['CATEGORY_ID']));
         $categData['CACHE_URL']   = (substr (SITE_URL, -1, 1) == '/' ? SITE_URL.$categData['CACHE_URL'] : SITE_URL.'/'.$categData['CACHE_URL']);

         $text = str_replace ('{ARTICLE_CATEGORY_URL}'   , $categData['CACHE_URL']    , $text);
         $text = str_replace ('{ARTICLE_CATEGORY_TITLE}' , $categData['CACHE_TITLE']  , $text);

         unset ($categData);
      }
   }
   $text = str_replace ('{MY_SITE_NAME}'        , SITE_NAME          , $text);
   $text = str_replace ('{MY_SITE_URL}'         , SITE_URL           , $text);
   $text = str_replace ('{MY_SITE_DESC}'        , SITE_DESC          , $text);
   $text = str_replace ('{LINK_RECIPR_URL}'     , DEFAULT_RECPR_URL  , $text);
   $text = str_replace ('{LINK_RECIPR_TITLE}'   , DEFAULT_RECPR_TITLE, $text);

   foreach ($data as $k => $v)
   {
      $text = str_replace ("{".$prefix.$k."}", $v, $text);
   }

   return $text;
}

function get_emailer()
{
   global $db, $tables;

   if (!empty ($_SESSION['phpld']['adminpanel']['id']))
      $where = "`ID` = ".$db->qstr($_SESSION['phpld']['adminpanel']['id']);
   elseif (!empty ($_SESSION['phpld']['user']['id']))
      $where = "`ID` = ".$db->qstr($_SESSION['phpld']['user']['id']);
   else
      $where = "`LEVEL` = '1'";

   $sql = "SELECT `NAME`, `EMAIL` FROM `".TABLE_PREFIX."USER` WHERE {$where} LIMIT 1";

   $user_info = $db->GetRow($sql);

   require_once 'libs/phpmailer/class.phpmailer.php';

   $mail            = new PHPMailer();
   $mail->PluginDir = 'libs/phpmailer/';
   $mail->From      = $user_info['EMAIL'];
   $mail->FromName  = $user_info['NAME'];
   $mail->Mailer    = EMAIL_METHOD;

   unset ($user_info, $sql);

   switch (EMAIL_METHOD)
   {
      case 'smtp' :
         $mail->Host = EMAIL_SERVER;
         if (strlen (EMAIL_USER) > 0)
         {
            $mail->SMTPAuth = true;
            $mail->Username = EMAIL_USER;
            $mail->Password = EMAIL_PASS;
         }
         break;
      case 'sendmail' :
         $mail->Sendmail = EMAIL_SENDMAIL;
         break;
   }
   return $mail;
}

function get_emailer_admin()
{
   global $tables, $db;

   $where = '';
   if (!empty ($_SESSION['phpld']['adminpanel']['id']))
      $where .= " AND `ID` = ".$db->qstr($_SESSION['phpld']['adminpanel']['id']);

   $sql = "SELECT `NAME`, `EMAIL` FROM `".TABLE_PREFIX."USER` WHERE `LEVEL` = '1' {$where} ORDER BY `REGISTRATION_DATE` ASC LIMIT 1";

   $user_info = $db->GetRow($sql);

   require_once 'libs/phpmailer/class.phpmailer.php';

   $mail            = new PHPMailer();
   $mail->PluginDir = 'libs/phpmailer/';
   $mail->From      = $user_info['EMAIL'];
   $mail->FromName  = $user_info['NAME'];
   $mail->Sender    = $user_info['EMAIL'];
   $mail->Mailer    = EMAIL_METHOD;

   unset ($user_info, $sql);

   switch (EMAIL_METHOD)
   {
      case 'smtp':
         $mail->Host = EMAIL_SERVER;
         if (strlen (EMAIL_USER) > 0)
         {
            $mail->SMTPAuth = true;
            $mail->Username = EMAIL_USER;
            $mail->Password = EMAIL_PASS;
         }
         break;
      case 'sendmail':
         $mail->Sendmail = EMAIL_SENDMAIL;
         break;
   }
   return $mail;
}

/**
 * Get email template (title, subject, body) from DB
 * If it does not exists get it "hardcoded" from email templates file
 *
 * @param string Email tpl name, simmilar to ID field from PLD_CONFIG DB table
 * @return array Email template (boolean FALSE on error)
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function get_email_template($tpl)
{
   global $tables, $db;
   require_once 'include/email_templates.php';

   //Get email template ID
   $tplID = $db->GetOne("SELECT `VALUE` FROM `{$tables['config']['name']}` WHERE `ID` = ".$db->qstr($tpl));

   if (!$tplID)
   {
      //No email template ID found in config DB table, try default email templates
      if (isset ($constant_email_tpl[$tpl]) && is_array($constant_email_tpl[$tpl]))
      {
         return $constant_email_tpl[$tpl];
      }
   }
   else
   {
      //Email template ID found, get email template
      $tplSql     = "SELECT `TITLE`, `SUBJECT`, `BODY` FROM `{$tables['email_tpl']['name']}` WHERE `ID` = ".$db->qstr($tplID);
      $dbEmailTpl = $db->GetRow($tplSql);

      if (is_array ($dbEmailTpl) && !empty ($dbEmailTpl))
      {
         //Email template found, return it
         return $dbEmailTpl;
      }
      elseif (isset ($constant_email_tpl[$tpl]) && is_array($constant_email_tpl[$tpl]))
      {
         //No email template found in DB, return default
         return $constant_email_tpl[$tpl];
      }
   }

   //No email template found, not in DB nor in default email templates
   return false;
}

function format_email($address, $name)
{
   $name    = trim ($name);
   $address = trim ($address);
   if ($name)
      return "{$name} <{$address}>";

   return $address;
}

function db_replace($table, $data, $keyCol)
{
   global $tables, $db;
   foreach ($data as $key => $val)
   {
      if (substr ($tables[$table]['fields'][$key], 0, 1) == 'T')
         $data[$key] = $db->DBDate($val);
      else
         $data[$key] = $db->qstr($val);
   }

   return $db->Replace($tables[$table]['name'], $data, $keyCol, false);
}

/**
 * Send submit/reject notifications
 * @param mixed $data if type is array it is considered a link associative array; otherwise it is considered a link id
 * @param boolean $update if change owner notification type
 * @param boolean $RegularLink if notification is for a regular link or a reviewed link
 * @author dcb
 */
function send_status_notifications($data, $update = true, $RegularLink = true, $CustomReject = false)
{
   global $db, $tables;
   if (DEMO)
      return;

   if (is_array ($data))
      $id = $data['ID'];
   else
   {
      $id = $data;
      $data = $db->GetRow("SELECT * FROM `{$tables['link']['name']}` WHERE `ID` = ".$db->qstr($id));
   }

   if ($data['OWNER_NOTIF'] >= 2 && $RegularLink == true)
      return;
   if ($data['STATUS'] == 0 || $CustomReject == true)
      $tid = ($RegularLink === false ? NTF_REVIEW_REJECT_TPL  : NTF_REJECT_TPL);
   elseif ($data['STATUS'] == 2)
      $tid = ($RegularLink === false ? NTF_REVIEW_APPROVE_TPL : NTF_APPROVE_TPL);
   else
      return;

   $sql = "SELECT `SUBJECT`, `BODY` FROM `{$tables['email_tpl']['name']}` WHERE `ID` = ".$db->qstr($tid);
   $tmpl = $db->GetRow($sql);
   if ($tmpl)
   {
      $mail = get_emailer();
      $mail->Body    = replace_email_vars($tmpl['BODY']   , $data, 2);
      $mail->Subject = replace_email_vars($tmpl['SUBJECT'], $data, 2);
      if ($data['OWNER_EMAIL'])
      {
         $mail->AddAddress($data['OWNER_EMAIL'], $data['OWNER_NAME']);
         $sent = $mail->Send();
         if ($update)
            $db->Execute("UPDATE `{$tables['link']['name']}` SET `OWNER_NOTIF` = '2' WHERE `ID` = ".$db->qstr($id));
      }
   }
}
/**
 * Send submit/reject notifications
 * @param mixed $data if type is array it is considered a link associative array; otherwise it is considered a link id
 * @param boolean $update if change owner notification type
 * @param boolean $RegularLink if notification is for a regular link or a reviewed link
 * @author dcb
 */
function send_status_notificationsa($data, $update = true, $RegularArticle = true, $CustomReject = false)
{
   global $db, $tables;
   if (DEMO)
      return;

   if (is_array ($data))
      $id = $data['ID'];
   else
   {
      $id = $data;
      $data = $db->GetRow("SELECT * FROM `{$tables['article']['name']}` WHERE `ID` = ".$db->qstr($id));
   }

   if ($data['OWNER_NOTIF'] >= 2 && $RegularArticle == true)
      return;
   if ($data['STATUS'] == 0 || $CustomReject == true)
      $tid = ($RegularArticle === false ? NTF_REVIEWA_REJECT_TPL  : NTF_REJECTA_TPL);
   elseif ($data['STATUS'] == 2)
      $tid = ($RegularArticle === false ? NTF_REVIEWA_APPROVE_TPL : NTF_APPROVEA_TPL);
   else
      return;
   
   $sql = "SELECT `SUBJECT`, `BODY` FROM `{$tables['email_tpl']['name']}` WHERE `ID` = ".$db->qstr($tid);
   $tmpl = $db->GetRow($sql);

   if ($tmpl)
   {
      $mail = get_emailer();
      $mail->Body    = replace_email_vars($tmpl['BODY']   , $data, 7);
      $mail->Subject = replace_email_vars($tmpl['SUBJECT'], $data, 7);
      if ($data['OWNER_EMAIL'])
      {
         $mail->AddAddress($data['OWNER_EMAIL'], $data['OWNER_NAME']);
         $sent = $mail->Send();
         if ($update)
            $db->Execute("UPDATE `{$tables['article']['name']}` SET `OWNER_NOTIF` = '2' WHERE `ID` = ".$db->qstr($id));
      }
   }
}
/**
 * Send reciprocal link expiration notifications
 * @param mixed $data if type is array it is considered a link associative array; otherwise it is considered a link id
 * @author dcb
 */
function send_expired_notifications($data)
{
   global $db, $tables;

   if (DEMO)
      return;

   if (is_array ($data))
      $id = $data['ID'];
   else
   {
      $id = $data;
      $data = $db->GetRow("SELECT * FROM `{$tables['link']['name']}` WHERE `ID` = ".$db->qstr($id));
   }

   $tid = NTF_EXPIRED_TPL;

   $sql = "SELECT `SUBJECT`, `BODY` FROM `{$tables['email_tpl']['name']}` WHERE `ID` = ".$db->qstr($tid);
   $tmpl = $db->GetRow($sql);
   if ($tmpl)
   {
      $mail = get_emailer();
      $mail->Body    = replace_email_vars($tmpl['BODY']   , $data, 2);
      $mail->Subject = replace_email_vars($tmpl['SUBJECT'], $data, 2);
      if ($data['OWNER_EMAIL'])
      {
         $mail->AddAddress($data['OWNER_EMAIL'], $data['OWNER_NAME']);
         $sent = $mail->Send();
      }
   }
}

/**
 * Send submit notifications
 * @param mixed $data if type is array it is considered a link associative array; otherwise it is considered a link id
 * @param boolean $update if change owner notification type
 * @param boolean $RegularLink if notification is for a regular link or a reviewed link
*/
function send_submit_notifications($data, $RegularLink = true)
{
   global $db, $tables, $notif_msg;

   if (DEMO)
      return false;

   $EmailTemplate = ($RegularLink === false ? NTF_REVIEW_SUBMIT_TPL : NTF_SUBMIT_TPL);

   $sql = "SELECT `SUBJECT`, `BODY` FROM `{$tables['email_tpl']['name']}` WHERE `ID` = ".$db->qstr($EmailTemplate);
   $tmpl = $db->GetRow($sql);

   if ($tmpl)
   {
      $mail          = get_emailer_admin();
      $mail->Body    = replace_email_vars($tmpl['BODY']   , $data, 2);
      $mail->Subject = replace_email_vars($tmpl['SUBJECT'], $data, 2);
      $mail->AddAddress($data['OWNER_EMAIL'], $data['OWNER_NAME']);
      $sent = $mail->Send();
   }

   $tmpl = $notif_msg['submit'];

   $admin = $db->GetRow("SELECT `ID`, `NAME`, `EMAIL` FROM `{$tables['user']['name']}` WHERE `LEVEL` = '1' AND `ACTIVE` = '1' AND `SUBMIT_NOTIF` = '1' LIMIT 1");

   $where = (!empty ($admin['ID']) ? 'AND `ID` != '.$db->qstr($admin['ID']) : '');

   $users = $db->GetAll("SELECT `NAME`, `EMAIL` FROM `{$tables['user']['name']}` WHERE `ACTIVE` = '1' AND `SUBMIT_NOTIF` = '1' {$where}");

   $mail          = get_emailer_admin();
   $mail->Body    = replace_email_vars($tmpl['BODY']   , $data, 2);
   $mail->Subject = replace_email_vars($tmpl['SUBJECT'], $data, 2);

   if (!empty ($admin['EMAIL']))
      $mail->AddAddress($admin['EMAIL'], $admin['NAME']);

   if (is_array ($users) && !empty ($users))
      foreach ($users as $user)
         $mail->AddBCC($user['EMAIL'], $user['NAME']);

   $sent = $mail->Send();
}
function send_submit_notificationsa($data, $RegularArticle = true)
{
   global $db, $tables, $notif_msg;

   if (DEMO)
      return false;

   $EmailTemplate = ($RegularArticle === false ? NTF_REVIEWA_SUBMIT_TPL : NTF_SUBMITA_TPL);

   $sql = "SELECT `SUBJECT`, `BODY` FROM `{$tables['email_tpl']['name']}` WHERE `ID` = ".$db->qstr($EmailTemplate);
   $tmpl = $db->GetRow($sql);

   if ($tmpl)
   {
      $mail          = get_emailer_admin();
      $mail->Body    = replace_email_vars($tmpl['BODY']   , $data, 7);
      $mail->Subject = replace_email_vars($tmpl['SUBJECT'], $data, 7);
      $mail->AddAddress($data['OWNER_EMAIL'], $data['OWNER_NAME']);
      $sent = $mail->Send();
   }

   $tmpl = $notif_msg['submitarticle'];

   $admin = $db->GetRow("SELECT `ID`, `NAME`, `EMAIL` FROM `{$tables['user']['name']}` WHERE `LEVEL` = '1' AND `ACTIVE` = '1' AND `SUBMIT_NOTIF` = '1' LIMIT 1");

   $where = (!empty ($admin['ID']) ? 'AND `ID` != '.$db->qstr($admin['ID']) : '');

   $users = $db->GetAll("SELECT `NAME`, `EMAIL` FROM `{$tables['user']['name']}` WHERE `ACTIVE` = '1' AND `SUBMIT_NOTIF` = '1' {$where}");

   $mail          = get_emailer_admin();
   $mail->Body    = replace_email_vars($tmpl['BODY']   , $data, 7);
   $mail->Subject = replace_email_vars($tmpl['SUBJECT'], $data, 7);

   if (!empty ($admin['EMAIL']))
      $mail->AddAddress($admin['EMAIL'], $admin['NAME']);

   if (is_array ($users) && !empty ($users))
      foreach ($users as $user)
         $mail->AddBCC($user['EMAIL'], $user['NAME']);

   $sent = $mail->Send();
}

function send_payment_notifications($pdata, $ldata)
{
   global $db, $tables, $notif_msg;

   if (DEMO)
      return false;

   $pdata['SUCCESS'] = $pdata['CONFIRMED'] ? 'successful' : 'failed';

   $sql = "SELECT `SUBJECT`, `BODY` FROM `{$tables['email_tpl']['name']}` WHERE `ID` = ".$db->qstr(NTF_PAYMENT_TPL);
   $tmpl = $db->GetRow($sql);
   if ($tmpl)
   {
      $mail          = get_emailer_admin();
      $body          = replace_email_vars($tmpl['BODY']   , $ldata, 2);
      $subject       = replace_email_vars($tmpl['SUBJECT'], $ldata, 2);
      $mail->Body    = replace_email_vars($body           , $pdata, 3);
      $mail->Subject = replace_email_vars($subject        , $pdata, 3);
      $mail->AddAddress($ldata['OWNER_EMAIL'], $ldata['OWNER_NAME']);
      $sent = $mail->Send();
   }

   if ($ldata['URL'] == '') {
   		$tmpl = $notif_msg['payment_article'];
   } else {
   		$tmpl = $notif_msg['payment'];
   }

   $admin = $db->GetRow("SELECT `ID`, `NAME`, `EMAIL` FROM `{$tables['user']['name']}` WHERE `LEVEL` = '1' AND `ACTIVE` = '1' AND `SUBMIT_NOTIF` = '1' LIMIT 1");

   $where = (!empty ($admin['ID']) ? 'AND `ID` != '.$db->qstr($admin['ID']) : '');

   $users = $db->GetAll("SELECT `NAME`, `EMAIL` FROM `{$tables['user']['name']}` WHERE `ACTIVE` = '1' AND `PAYMENT_NOTIF` = '1' {$where}");

   $mail          = get_emailer();
   $body          = replace_email_vars($tmpl['BODY']   , $ldata, 2);
   $subject       = replace_email_vars($tmpl['SUBJECT'], $ldata, 2);
      
   $mail->Body    = replace_email_vars($body           , $pdata, 3);
  
   $mail->Subject = replace_email_vars($subject        , $pdata, 3);

   
   
   
   if (!empty ($admin['EMAIL']))
      $mail->AddAddress($admin['EMAIL'], $admin['NAME']);

   if (is_array ($users) && !empty ($users))
      foreach ($users as $user)
         $mail->AddBCC($user['EMAIL'], $user['NAME']);

   $sent = $mail->Send();
}

function date_addition($timestamp, $months)
{
   $d       = getdate ($timestamp);
   $mon     = $months % 12;
   $years   = $months / 12;
   $mytime  = mktime ($d['hours'], $d['minutes'], $d['seconds'], $d['mon'] + $mon, 1, $d['year'] + $years);
   $days    = min ($d['mday'], date ('t', $mytime));
   $mytime += ($days -1) * 86400;

   return $mytime;
}

function calculate_expiry_date($start, $units, $um)
{
   switch ($um) {
      case 1 :
         $mul = 1;
         break;
      case 2 :
         $mul = 3;
         break;
      case 3 :
         $mul = 6;
         break;
      case 4 :
         $mul = 12;
         break;
      default :
         $mul = 0;
         break;
   }
   if ($mul != 0)
      return date_addition($start, $units * $mul);

   return 0;
}

function update_link_payment($pid, $data, $success, $raw)
{
   global $db, $tables;
   $pdata = $db->GetRow("SELECT * FROM `{$tables['payment']['name']}` WHERE `ID` = ".$db->qstr($pid));
   if (!$pdata['ID'])
      return false;
   $pdata['NAME']           = $data['name'];
   $pdata['EMAIL']          = $data['email'];
   if($data['total'] != '')
   $pdata['PAYED_TOTAL']    =  $data['total'];
   if($data['quantity'] != '')
   $pdata['PAYED_QUANTITY'] =  $data['quantity'];
   $pdata['CONFIRMED']      = $success;
   $pdata['CONFIRM_DATE']   = gmdate ('Y-m-d H:i:s');
   $pdata['RAW_LOG']        = $raw;
   if($data['payment_type'] != '')
   $pdata['PAYMENT_TYPE']    = $data['payment_type'];#if echeck or instant
   if($data['payment_status'] != '')
   $pdata['PAYMENT_STATUS']    = $data['payment_status']; #check for status as echeck does not clear
   $pdata['TOTAL']  = $pdata['TOTAL'];
   
   if ($pdata['PAYED_TOTAL'] <  $pdata['TOTAL'])
   {
      $pdata['CONFIRMED'] = -1;
   }
if (preg_match("/Pending/i", $data['payment_status']) )
	{
$pdata['CONFIRMED'] = 0;
	}


   db_replace('payment', $pdata, 'ID');
   $ldata = $db->GetRow("SELECT * FROM `{$tables['link']['name']}` WHERE `ID` = ".$db->qstr($pdata['LINK_ID']));
   send_payment_notifications($pdata, $ldata);
   //Take no action if link not found
   if (!$ldata['ID'])
      return false;
   else
   {
      $ldata['PAYED'] = '1';
	     // set the expiry_date
      $exp_date = calculate_expiry_date(time(), $pdata['QUANTITY'], $pdata['UM']);
         if ($exp_date != 0)
            $ldata['EXPIRY_DATE'] = gmdate ('Y-m-d H:i:s', $exp_date);
      if (PAY_AUTO_ACCEPT)
      {
         $ldata['STATUS'] = 2;
      }
   }
   db_replace('link', $ldata, 'ID');

}

function sprint_r($val)
{
   ob_start ();
   print_r ($val);
   $ret = ob_get_contents ();
   ob_end_clean ();

   return $ret;
}

function read_echo($val)
{
   ob_start ();
   echo $val;
   $ret = ob_get_contents ();
   ob_end_clean ();

   return $ret;
}

function numeric_entify_utf8($utf8_string)
{
   $out = "";
   $ns = strlen ($utf8_string);
   for ($nn = 0; $nn < $ns; $nn ++)
   {
      $ch = $utf8_string[$nn];
      $ii = ord ($ch);
      //1 7 0bbbbbbb (127)
      if ($ii < 128)
         $out .= $ch;
      //2 11 110bbbbb 10bbbbbb (2047)
      else
         if ($ii >> 5 == 6)
         {
            $b1 = ($ii & 31);
            $nn ++;
            $ch = $utf8_string[$nn];
            $ii = ord ($ch);
            $b2 = ($ii & 63);
            $ii = ($b1 * 64) + $b2;
            $ent = sprintf ("&#%d;", $ii);
            $out .= $ent;
         }
      //3 16 1110bbbb 10bbbbbb 10bbbbbb
      else
         if ($ii >> 4 == 14)
         {
            $b1 = ($ii & 31);
            $nn ++;
            $ch = $utf8_string[$nn];
            $ii = ord ($ch);
            $b2 = ($ii & 63);
            $nn ++;
            $ch = $utf8_string[$nn];
            $ii = ord ($ch);
            $b3 = ($ii & 63);
            $ii = ((($b1 * 64) + $b2) * 64) + $b3;
            $ent = sprintf ("&#%d;", $ii);
            $out .= $ent;
         }
      //4 21 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
      else
         if ($ii >> 3 == 30)
         {
            $b1 = ($ii & 31);
            $nn ++;
            $ch = $utf8_string[$nn];
            $ii = ord ($ch);
            $b2 = ($ii & 63);
            $nn ++;
            $ch = $utf8_string[$nn];
            $ii = ord ($ch);
            $b3 = ($ii & 63);
            $nn ++;
            $ch = $utf8_string[$nn];
            $ii = ord ($ch);
            $b4 = ($ii & 63);
            $ii = ((((($b1 * 64) + $b2) * 64) + $b3) * 64) + $b4;
            $ent = sprintf ("&#%d;", $ii);
            $out .= $ent;
         }
   }

   return $out;
}

function xml_utf8_encode($str)
{
   return numeric_entify_utf8(htmlspecialchars ($str));
}

/**
 * Add slashes before "'" and "\" characters
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function sql_addslashes($string = '')
{
   $string = str_replace('\\', '\\\\', $string);
   $string = str_replace('\'', '\'\'', $string);

   return $string;
}

function find_child_categories()
{
   global $tables, $db, $data, $u;

   $child_count = 0;

   $rs = $db->Execute("SELECT `CATEGORY_ID` FROM `{$tables['user_permission']['name']}` WHERE `USER_ID` = ".$db->qstr($u));

   while (!$rs->EOF)
   {
      $row = $rs->FetchRow();

      $category = $row['CATEGORY_ID'];

      while ($category != 0)
      {
         $sql = "SELECT `PARENT_ID` FROM `{$tables['category']['name']}` WHERE `ID` = ".$db->qstr($category);
         $category = $db->GetOne($sql);
         if ($category == $data['CATEGORY_ID'])
         {
            $child_count++;
            break;
         }
      }
   }
   $rs->Close();

   return $child_count;
}

function delete_child_categories()
{
   global $tables, $db, $id, $u;

   $child_count = 0;

   $rs = $db->Execute("SELECT `ID`, `CATEGORY_ID` FROM `{$tables['user_permission']['name']}` WHERE `USER_ID` = ".$db->qstr($u));

   while (!$rs->EOF)
   {
      $row = $rs->FetchRow();

      $category = $row['CATEGORY_ID'];

      while ($category != 0)
      {
         $sql = "SELECT `PARENT_ID` FROM `{$tables['category']['name']}` WHERE `ID` = ".$db->qstr($category);
         $category = $db->GetOne($sql);
         if ($category == $id)
         {
            $db->Execute("DELETE FROM `{$tables['user_permission']['name']}` WHERE `ID` = ".$db->qstr($row['ID']));
            break;
         }
      }
   }
   $rs->Close();
}

/**
 * Build URL of a category
 * @param  integer  ID of category to build it's URL
 * @return string   Partial URL to be added to document root ("DOC_ROOT/")
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function buildCategUrl($id, $url='')
{
   global $db, $tables;

   //Check if category is not symbolic
   $categInfo = $db->GetRow("SELECT `SYMBOLIC`, `SYMBOLIC_ID` FROM `{$tables['category']['name']}` WHERE `ID` = ".$db->qstr($id));
   if ($categInfo['SYMBOLIC'] == 1 && !empty ($categInfo['SYMBOLIC_ID']))
   {
      //Category is symbolic, use it's symbolic id instead
      $id = intval ($categInfo['SYMBOLIC_ID']);
   }

   if (ENABLE_REWRITE == 0)
   {
      //Build regular URL
      $url = 'index.php?c='.intval ($id);
   }
   else
   {
      //Build friendly URL for rewriting
      $category = $db->GetRow("SELECT `PARENT_ID`, `TITLE_URL` FROM `{$tables['category']['name']}` WHERE `ID` = ".$db->qstr($id));

      if (is_array ($category) && !empty ($category))
      {
         $nextUrl = $category['TITLE_URL'] . '/' . $url;
         $url     = buildCategUrl($category['PARENT_ID'], $nextUrl);
      }
   }
   return trim ($url);
}

/**
 * Build cache title of a category
 * @param  integer  ID of category to build it's cache title
 * @return string   Category title to be added into category cache title
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function buildCategUrlTitle($id='0', $urlTitle='')
{
   global $db, $tables;

   $sql = "SELECT `PARENT_ID`, `TITLE` FROM `{$tables['category']['name']}` WHERE `ID` = ".$db->qstr($id);
   $category = $db->GetRow($sql);

   if (is_array ($category) && !empty ($category))
   {
      $nextUrlTitle = $category['TITLE'].': '.$urlTitle;
      $urlTitle     = buildCategUrlTitle($category['PARENT_ID'], $nextUrlTitle);
   }

   //Clean URL title
   $urlTitle = preg_replace ('`:[\s]*$`', '', $urlTitle);

   return trim ($urlTitle);
}

function get_editor_permission($user_id)
{
   global $tables, $db, $user_grant_permission, $user_permission, $user_grant_permission_array, $user_permission_array;

   $all_first_iteration = true;
   $first_iteration     = true;

   $rs = $db->Execute("SELECT `CATEGORY_ID` FROM `{$tables['user_permission']['name']}` WHERE `USER_ID` = ".$db->qstr($user_id));

   while (!$rs->EOF)
   {
      $row = $rs->FetchRow();
      if ($all_first_iteration)
      {
         $user_permission = "ID = ".$db->qstr($row['CATEGORY_ID']);
         $all_first_iteration = false;
      }
      else
         $user_permission .= " OR ID = ".$db->qstr($row['CATEGORY_ID']);

      $user_permission_array[] = $row['CATEGORY_ID'];

      $new_sub_categories = get_sub_categories($row['CATEGORY_ID']);

      foreach ($new_sub_categories as $category_id)
      {
         if ($first_iteration)
         {
            $user_grant_permission = "ID = ".$db->qstr($category_id);
            $user_permission .= " OR ID = ".$db->qstr($category_id);
            $first_iteration = false;
         }
         else
         {
            $user_grant_permission .= " || ID = ".$db->qstr($category_id);
            $user_permission .= " OR ID = ".$db->qstr($category_id);
         }
         $user_permission_array[] = $category_id;
         $user_grant_permission_array[] = $category_id;
      }

   }
   $rs->Close();
}

function get_sub_categories($id)
{
   global $db, $tables;

   $categs = array ();
   $rs = $db->Execute("SELECT `ID`, `TITLE` FROM `{$tables['category']['name']}` WHERE `PARENT_ID` = ".$db->qstr($id)." ORDER BY `TITLE`");
   while (!$rs->EOF)
   {
      $categs[] = $rs->Fields('ID');
      $categs = array_merge ($categs, get_sub_categories($rs->Fields('ID')));
      $rs->MoveNext();
   }

   return $categs;
}

/**
 * Determine link type
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function determine_link_type($type = 0)
{
   if (!preg_match ('`^[\d]+$`', $type))
      return false;

   $type = ($type < 0 || $type > 8 ? 0 : intval ($type));

   switch ($type) {
      case 8  :
         $return = 'featured_lt';
         break;
      case 7  :
         $return = 'normal_lt';
         break;

	  case 6  :
         $return = 'featured_dl';
         break;
      case 5  :
         $return = 'normal_dl';
         break;
      case 4  :
         $return = 'featured';
         break;
      case 3  :
         $return = 'reciprocal';
         break;
      case 2  :
         $return = 'normal';
         break;
      case 1  :
         $return = 'free';
         break;
      case 0  :
      default :
         $return = 'none';
         break;
   }

   return (!empty ($return) ? $return : false);
}

/**
 * Update email address and name for all links
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function update_link_owner($user_id = 0)
{
   global $db, $tables;

   if (empty ($user_id))
      return 0;
   else
   {
      $data = $db->GetRow("SELECT `NAME` AS `OWNER_NAME`, `EMAIL` AS `OWNER_EMAIL` FROM `{$tables['user']['name']}` WHERE `ID` = ".$db->qstr($user_id));

      $where = " `OWNER_ID` = ".$db->qstr($user_id);
      if (!$db->AutoExecute($tables['link']['name'], $data, 'UPDATE', $where))
         return 0;
      else
      {
         if (!$db->AutoExecute($tables['link_review']['name'], $data, 'UPDATE', $where))
            return 0;
         else
          {
               $where = " `USER_ID` = ".$db->qstr($user_id);
               if (!$db->AutoExecute($tables['linkcomment']['name'], $data, 'UPDATE', $where))
                  return 0;
          }
      }
   }
   return 1;
}

/**
 * Update email address and name for all articles
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function update_article_owner($user_id = 0)
{
	global $db, $tables;

	if (empty ($user_id))
		return 0;
	else
	{
		$data = $db->GetRow("SELECT `NAME` AS `OWNER_NAME`, `EMAIL` AS `OWNER_EMAIL` FROM `{$tables['user']['name']}` WHERE `ID` = ".$db->qstr($user_id));

		$where = " `OWNER_ID` = ".$db->qstr($user_id);
		if (!$db->AutoExecute($tables['article']['name'], $data, 'UPDATE', $where))
			return 0;
		else
		{
			if (!$db->AutoExecute($tables['article_review']['name'], $data, 'UPDATE', $where))
				return 0;
			else
			{
				$where = " `USER_ID` = ".$db->qstr($user_id);
				if (!$db->AutoExecute($tables['articlecomment']['name'], $data, 'UPDATE', $where))
					return 0;
			}
		}
	}
	return 1;
}
/**
 * Create password
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function create_password($length=6)
{
   $phrase = "";
   $chars = array (
                  "1","2","3","4","5","6","7","8","9","0",
                  "a","A","b","B","c","C","d","D","e","E","f","F","g","G","h","H","i","I","j","J",
                  "k","K","l","L","m","M","n","N","o","O","p","P","q","Q","r","R","s","S","t","T",
                  "u","U","v","V","w","W","x","X","y","Y","z","Z"
                  );

   $count = count ($chars) - 1;

   srand ((double) microtime() * 1234567);

   for ($i = 0; $i < $length; $i++)
      $phrase .= $chars[rand (0, $count)];

   return $phrase;
}

function construct_mod_rewrite_path($cat_id)
{
   global $tables, $db;

   $category        = $cat_id;
   $first_iteration = true;

   while ($category != 0)
   {
      //Get category title and add into URL
      $parent_row = $db->GetRow("SELECT `TITLE`, `TITLE_URL` FROM `{$tables['category']['name']}` WHERE `ID` = ". $db->qstr($category));
      if (empty ($parent_row['TITLE_URL']))
      {
         $parent_row['TITLE_URL'] = preg_replace ('`[^\w_-]`', '_', $parent_row['TITLE']);
         $parent_row['TITLE_URL'] = str_replace ('__', '_', $parent_row['TITLE_URL']);
      }
      if($first_iteration)
      {
         $mod_rewrite_url = $parent_row['TITLE_URL'];
         $first_iteration = false;
      }
      else
         $mod_rewrite_url = $parent_row['TITLE_URL']."/".$mod_rewrite_url;

      $category = $db->GetOne("SELECT `PARENT_ID` FROM `{$tables['category']['name']}` WHERE `ID` = ".$db->qstr($category));
   }

   $mod_rewrite_url = DOC_ROOT."/{$mod_rewrite_url}";

   return $mod_rewrite_url;
}

/**
 * Disable any caching by the browser
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 * http://www.faqs.org/rfcs/rfc2616
 */
function disable_browser_cache()
{
   @ header ('Expires: Mon, 14 Oct 2002 05:00:00 GMT');               // Date in the past
   @ header ('Last-Modified: ' . gmdate ("D, d M Y H:i:s") . ' GMT'); // Always modified
   @ header ('Cache-Control: no-store, no-cache, must-revalidate');   // HTTP 1.1
   @ header ('Cache-Control: post-check=0, pre-check=0', false);
   @ header ('Pragma: no-cache');                                     // HTTP 1.0
}

/**
 * Send HTTP/1.1 Status header
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 * http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1.1
 */
function httpstatus($status_code)
{
   static $http = array (
       100 => "HTTP/1.1 100 Continue",
       101 => "HTTP/1.1 101 Switching Protocols",
       200 => "HTTP/1.1 200 OK",
       201 => "HTTP/1.1 201 Created",
       202 => "HTTP/1.1 202 Accepted",
       203 => "HTTP/1.1 203 Non-Authoritative Information",
       204 => "HTTP/1.1 204 No Content",
       205 => "HTTP/1.1 205 Reset Content",
       206 => "HTTP/1.1 206 Partial Content",
       300 => "HTTP/1.1 300 Multiple Choices",
       301 => "HTTP/1.1 301 Moved Permanently",
       302 => "HTTP/1.1 302 Found",
       303 => "HTTP/1.1 303 See Other",
       304 => "HTTP/1.1 304 Not Modified",
       305 => "HTTP/1.1 305 Use Proxy",
       307 => "HTTP/1.1 307 Temporary Redirect",
       400 => "HTTP/1.1 400 Bad Request",
       401 => "HTTP/1.1 401 Unauthorized",
       402 => "HTTP/1.1 402 Payment Required",
       403 => "HTTP/1.1 403 Forbidden",
       404 => "HTTP/1.1 404 Not Found",
       405 => "HTTP/1.1 405 Method Not Allowed",
       406 => "HTTP/1.1 406 Not Acceptable",
       407 => "HTTP/1.1 407 Proxy Authentication Required",
       408 => "HTTP/1.1 408 Request Time-out",
       409 => "HTTP/1.1 409 Conflict",
       410 => "HTTP/1.1 410 Gone",
       411 => "HTTP/1.1 411 Length Required",
       412 => "HTTP/1.1 412 Precondition Failed",
       413 => "HTTP/1.1 413 Request Entity Too Large",
       414 => "HTTP/1.1 414 Request-URI Too Large",
       415 => "HTTP/1.1 415 Unsupported Media Type",
       416 => "HTTP/1.1 416 Requested range not satisfiable",
       417 => "HTTP/1.1 417 Expectation Failed",
       500 => "HTTP/1.1 500 Internal Server Error",
       501 => "HTTP/1.1 501 Not Implemented",
       502 => "HTTP/1.1 502 Bad Gateway",
       503 => "HTTP/1.1 503 Service Unavailable",
       504 => "HTTP/1.1 504 Gateway Time-out"
   );

   if (!empty ($status_code) && array_key_exists ($status_code, $http))
   {
      if (version_compare (phpversion(), '4.3.0', '>='))
      {
         @ header($http[$status_code], true, $status_code);
      }
      else
      {
         @ header($http[$status_code], true, $status_code);
      }
   }
}

/**
 * Tweak memory limit of PHP, usually to increase it for havy operations like backups or cache building on huge databases
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function tweak_memory_limit($memory_limit=16)
{
   // Tweak memory limit only if PHP is not in safe mode
   if (!ini_get ('safe_mode') && function_exists('ini_set'))
   {
      $current_memory_limit = preg_replace ('`([^\d])`', '', ini_get ('memory_limit'));
      $memory_limit = preg_replace ('`([^\d])`', '', $memory_limit);
      $memory_limit = (preg_match ('`[\d]+`', $memory_limit) && $memory_limit > 4 ? intval ($memory_limit) : intval ($current_memory_limit));

      @ ini_set('memory_limit', $memory_limit."M");

      unset ($memory_limit, $current_memory_limit);
   }
}

/**
 * Tweak maximum execution time of a script, usually to increase it for time taking operations
 * like backups or cache building on huge databases
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function tweak_time_limit($time_limit=90) //zero == unlimited
{
   // Tweak max execution time only if PHP is not in safe mode
   if (!ini_get ('safe_mode') && function_exists('set_time_limit'))
   {
      $time_limit = preg_replace ('`([^\d])`', '', $time_limit);
      $time_limit = (preg_match ('`[\d]+`', $time_limit) && $time_limit >= 10 ? intval ($time_limit) : ini_get ('max_execution_time'));

      @ set_time_limit($time_limit);

      unset ($time_limit);
   }
}

/**
 * Calculate difference (in seconds) of two given dates
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function dateTimeDifference($greaterdate, $smallerdate)
{
   $greaterdate = str_replace ('-', '/', $greaterdate);
   $smallerdate = str_replace ('-', '/', $smallerdate);

   $diff = (strtotime ($greaterdate) - strtotime ($smallerdate));

   return $diff;
}

/**
 * Calculate minutes from seconds
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function second2min($seconds)
{
   $minutes = ceil ($seconds / 60);

   return $minutes;
}

/**
 * Calculate hours from seconds
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function second2hour($seconds)
{
   $hours = ceil (($seconds / 60) / 60);

   return $hours;
}

/**
 * Calculate days from seconds
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function second2day($seconds)
{
   //1 day = 86400 secs
   $hours = ceil ($seconds / 86400);

   return $hours;
}

/**
 * Calculate weeks from seconds
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function second2week($seconds)
{
   //1 week = 604800 secs
   $hours = ceil ($seconds / 604800);

   return $hours;
}

/**
 * A simple array serializer to send array values through URLs
 * @param array The array to be serialized
 * @param string String encoding (no value = no encoding, other values are: "gzip" and "base64")
 * @return string The serialized array as string
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function simple_serialize($array=array(), $encoding='')
{
   if (!is_array ($array))
      return false;

   $string = serialize ($array);
   switch ($encoding)
   {
      case 'base64' :
         $string = base64_encode ($string);
         break;

      case 'gzip':
         $string = gzcompress ($string, 6);
         break;
      default:
         break;
   }

   return $string;
}

/**
 * A simple string unserializer to read array values sent through URLs
 * @param string The string to be unserialized
 * @param string String encoding (no value = no encoding, other values are: "gzip" and "base64")
 * @return array The unserialized string as array
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function simple_unserialize($string='', $encoding='')
{
   if (empty ($string))
      return false;

   switch ($encoding)
   {
      case 'base64' :
         $string = base64_decode ($string);
         break;

      case 'gzip':
         $string = gzuncompress ($string);
         break;
      default:
         break;
   }
   $array = unserialize ($string);

   return $array;
}

/**
 * Parse URL and return its components
 * @param string The URL to be parsed
 * @return array Submitted URL components
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function parseURL($url)
{
   $url = trim ($url);

   if (empty ($url))
      return false;

   return parse_url ($url);
}

/**
 * Print sizes in human readable format (e.g. 1Kb, 234Mb, 5Gb)
 * @param  integer Ugly, not formatted size
 * @return string  Nice, human readable size format
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function nicesize($size='')
{
   //Define storage units
   $units = array ('B', 'kB', 'MB', 'GB', 'TB', 'PB');

   $pos = 0;

   while ($size >= 1024)
   {
      $size /= 1024;
      $pos++;
   }

   if($size == 0)
   {
      //is empty
      return '-';
   }
   else
   {
      //Round up
      return round ($size, 2) . ' ' . $units[$pos];
   }
}

/**
 * Parse URL and return its domain name
 * @param string  The URL to be parsed
 * @param boolean/integer Keep the www. prefix ()
 * @return string Domain name of URL
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function parseDomain($url,$keepWWW=0)
{
   $url = trim ($url);

   if (empty ($url))
      return false;

   $output = parseURL($url);

   if (!isset ($output['host']))
      return false;

   $pattern = array();
   //Remove http: and https: prefix
   $pattern[] = '`^http[s]?:`';
   //Remove ftp: prefix
   $pattern[] = '`^ftp:`';
   //Remove mailto: prefix
   $pattern[] = '`^mailto:`';

   if (!$keepWWW)
   {
      //Optional remove www. prefix
      $pattern[] = '`^www.`';
   }

   //Remove any dots as prefix
   $pattern[] = '`^\.`';
   //Remove any dots as suffix
   $pattern[] = '`\.$`';
   //Remove any unaccepted character
   $pattern[] = '`[^\w\d-\.]`';

   $output['host'] = preg_replace ($pattern, '', $output['host']);

   return $output['host'];
}

/**
 * Draw unauthorized page
 * @param string Reasong of unauthorization
 * @param string Template file to load
 * @param bool   If unauthorized page content is returned or printed on screen
 * @return none/string  Page content or nothing because page is printed and script stopped
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function gotoUnauthorized($reason, $tplFile='unauthorized.tpl', $returnVal=false)
{
   global $tpl;

   //Remove request variables
   unset ($_POST, $_GET, $_REQUEST);

   //Provide a reason why access was unautorised
   $tpl->assign('unauthorizedReason', $reason);

   //Clean whitespace
   $tpl->load_filter('output', 'trimwhitespace');

   //Compress output for faster loading
   if (COMPRESS_OUTPUT == 1)
      $tpl->load_filter('output', 'CompressOutput');

   $output = $tpl->fetch($tplFile);

   if ($returnVal)
   {
      return $output;
   }
   else
   {
      echo $output;
      exit();
   }
}

/**
 * Decode a gzip encoded message (when Content-encoding = gzip)
 * Currently requires PHP with zlib support
 * For details on the DEFLATE compression algorithm see (RFC 1951):
 * http://www.faqs.org/rfcs/rfc1951
 *
 * @author Aaron G.
 * @link   http://www.php.net/manual/en/function.gzencode.php#44470
 *
 * @param  string String to be decoded
 * @return string Decoded string
 */
function gz_decode($data)
{
   if (!function_exists ('gzinflate'))
   {
      //"gzinflate" function does not exist
      return false;
   }

   $len = strlen ($data);

   if ($len < 18 || strcmp (substr ($data, 0, 2), "\x1f\x8b"))
   {
      //Not GZIP format (See RFC 1952)
      return null;
   }

   //Compression method
   $method = ord (substr ($data, 2, 1));
   //Flags
   $flags  = ord (substr ($data, 3, 1));

   if ($flags & 31 != $flags)
   {
      //Reserved bits are set -- NOT ALLOWED by RFC 1952
      return null;
   }

   //NOTE: $mtime may be negative (PHP integer limitations)
   $mtime = unpack ("V", substr ($data, 4, 4));
   $mtime = $mtime[1];
   $xfl   = substr ($data,8,1);
   $os    = substr ($data,8,1);
   $headerlen = 10;
   $extralen  = 0;
   $extra     = "";

   if ($flags & 4)
   {
      //2-byte length prefixed EXTRA data in header
      if ($len - $headerlen - 2 < 8)
      {
         //Invalid format
         return false;
      }

      $extralen = unpack ("v", substr ($data, 8, 2));
      $extralen = $extralen[1];

      if ($len - $headerlen - 2 - $extralen < 8)
      {
         //Invalid format
         return false;
      }

      $extra = substr ($data, 10, $extralen);
      $headerlen += 2 + $extralen;
   }

   $filenamelen = 0;
   $filename    = "";

   if ($flags & 8)
   {
      //C-style string file NAME data in header
      if ($len - $headerlen - 1 < 8)
      {
         //Invalid format
         return false;
      }

      $filenamelen = strpos (substr ($data, 8 + $extralen), chr(0));

      if ($filenamelen === false || $len - $headerlen - $filenamelen - 1 < 8)
      {
         //Invalid format
         return false;
      }

      $filename = substr ($data, $headerlen, $filenamelen);
      $headerlen += $filenamelen + 1;
   }

   $commentlen = 0;
   $comment    = "";

   if ($flags & 16)
   {
      //C-style string COMMENT data in header
      if ($len - $headerlen - 1 < 8)
      {
         //Invalid format
         return false;
      }

      $commentlen = strpos (substr ($data, 8 + $extralen + $filenamelen), chr(0));

      if ($commentlen === false || $len - $headerlen - $commentlen - 1 < 8)
      {
         //Invalid header format
         return false;
      }

      $comment = substr ($data, $headerlen, $commentlen);
      $headerlen += $commentlen + 1;
   }

   $headercrc = "";

   if ($flags & 1)
   {
      //2-bytes (lowest order) of CRC32 on header present
      if ($len - $headerlen - 2 < 8)
      {
         //Invalid format
         return false;
      }

      $calccrc   = crc32 (substr ($data, 0, $headerlen)) & 0xffff;
      $headercrc = unpack ("v", substr($data, $headerlen, 2));
      $headercrc = $headercrc[1];

      if ($headercrc != $calccrc)
      {
         //Bad header CRC
         return false;
      }

      $headerlen += 2;
   }

   //GZIP FOOTER - These be negative due to PHPs limitations
   $datacrc = unpack("V", substr ($data, -8, 4));
   $datacrc = $datacrc[1];
   $isize   = unpack("V", substr ($data, -4));
   $isize   = $isize[1];

   //Perform the decompression:
   $bodylen = $len - $headerlen - 8;

   if ($bodylen < 1)
   {
      //This should never happen - IMPLEMENTATION BUG!
      return null;
   }

   $body = substr ($data, $headerlen, $bodylen);
   $data = "";

   if ($bodylen > 0)
   {
      switch ($method)
      {
         case 8:
            //Currently the only supported compression method:
            $data = gzinflate ($body);
            break;
         default:
            //Unknown compression method
            return false;
      }
   }
   else
   {
      //I'm not sure if zero-byte body content is allowed.
      //Allow it for now...  Do nothing...
   }

   // Verifiy decompressed size and CRC32:
   // NOTE: This may fail with large data sizes depending on how
   //      PHPs integer limitations affect strlen() since $isize
   //      may be negative for large sizes.
   if ($isize != strlen ($data) || crc32 ($data) != $datacrc)
   {
      //Bad format! Length or CRC doesnt match!
      return false;
   }

   return trim ($data);
}

/**
 * Decode a gzip encoded message (when Content-encoding = gzip)
 * Currently requires PHP with zlib support
 * For details on the DEFLATE compression algorithm see (RFC 1951):
 * http://www.faqs.org/rfcs/rfc1951
 *
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 *
 * @param  string String to be decoded
 * @return string Decoded string
 */
function decode_gzip($body)
{
   if (function_exists ('gzinflate'))
   {
      $body = substr ($body, 10);
      //Inflate a deflated string
      return trim (gzinflate ($body));
   }
   else
   {
      //Function does not exist
      return false;
   }
}

/**
 * Decode a zlib deflated message (when Content-encoding = deflate)
 * Currently requires PHP with zlib support
 * For details on the ZLIB compression algorithm see (RFC 1950):
 * http://www.faqs.org/rfcs/rfc1950
 *
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 *
 * @param  string String to be decoded
 * @return string Decoded string
 */
function decode_deflate($body)
{
   if (function_exists ('gzuncompress'))
   {
      //Uncompress the zlib compressed string
      return trim (gzuncompress ($body));
   }
   else
   {
      //Function does not exist
      return false;
   }
}

/**
 * [DEBUG] Print an array
 * @author Constantin Bejenaru / Boby <constantin_bejenaru@frozenminds.com> (http://www.frozenminds.com)
 */
function print_array($array)
{
   echo "<div style=\"text-align:left;\"><pre>";
   print_r ($array);
   echo "</pre></div>";
}
function send_status_notificationse($data, $update = true, $RegularLink = true, $CustomReject = false)
{
global $db, $tables;
if (DEMO)
return;
if (is_array ($data))
$id = $data['ID'];
else
{
$id = $data;
$data = $db->GetRow("SELECT * FROM `{$tables['link']['name']}` WHERE `ID` = ".$db->qstr($id));
}
if ($data['STATUS'] == 2)
$tid = ($RegularLink === false ? NTF_REVIEW_APPROVE_TPL : NTF_APPROVE_TPL);
else
return;

$sql = "SELECT `SUBJECT`, `BODY` FROM `{$tables['email_tpl']['name']}` WHERE `ID` = ".$db->qstr($tid);
$tmpl = $db->GetRow($sql);
if ($tmpl)
{
$mail = get_emailer();
$mail->Body = replace_email_vars($tmpl['BODY'] , $data, 2);
$mail->Subject = replace_email_vars($tmpl['SUBJECT'], $data, 2);
if ($data['OWNER_EMAIL'])
{
$mail->AddAddress($data['OWNER_EMAIL'], $data['OWNER_NAME']);
$sent = $mail->Send();
if ($update)
$db->Execute("UPDATE `{$tables['link']['name']}` SET `OWNER_NOTIF` = '2' WHERE `ID` = ".$db->qstr($id));
}
}
}

function subscribe_to_comments($id)
{
   global $db, $tables;

   $data['USER_ID']    = $_SESSION['phpld']['user']['id'];
   $data['COMMENT_ID'] = $db->GetOne("SELECT `ID` FROM `{$tables['articlecomment']['name']}` WHERE `ARTICLE_ID` = '$id' ORDER BY `ID` DESC LIMIT 1");
   $data['ARTICLE_ID'] = $id;

   $n = $db->GetOne("SELECT COUNT(*) FROM `{$tables['article_subscribe']['name']}` WHERE `USER_ID` = '{$_SESSION['phpld']['user']['id']}' AND `ARTICLE_ID` = ".$db->qstr($id));

   if($n < 1)
      db_replace('article_subscribe', $data, 'ID');

}

function send_mail_to_subscribers($id)
{
   global $db, $tables;

   $article = $db->GetRow("SELECT * FROM `{$tables['article']['name']}` WHERE `ID` = ".$db->qstr($id));
   $comment = $db->GetRow("SELECT * FROM `{$tables['articlecomment']['name']}` WHERE `ARTICLE_ID` = ".$db->qstr($id)." ORDER BY `ID` DESC LIMIT 1");
   $users   = $db->GetAll("SELECT * FROM `{$tables['article_subscribe']['name']}` WHERE `ARTICLE_ID` = ".$db->qstr($id)." AND `USER_ID` <> ".$db->qstr($comment['USER_ID']));

   foreach($users as $k => $v)
   {
      $udata = $db->GetRow("SELECT * FROM `{$tables['user']['name']}` WHERE `ID` = ".$db->qstr($v['USER_ID']));

      $mail = get_emailer();
      $mail->Body    = 'You get this mail because you are subscribed for receiving answers on '. $article['TITLE']
                      ."\n\n"
                      ."-----------------------\n"
                      . $comment['COMMENT'] . "\n"
                      .'-----------------------'
                      ."\n\nArticle: " . str_replace('//', '/', SITE_URL . "/article.php?id=") . $id
                      ."\n\n"
                      ."Regards,\n" . SITE_NAME;

      $mail->Subject = 'New answers on "' . $article['TITLE'] . '"';
      $mail->isHTML  = true;

      if (!empty($udata['EMAIL']))
      {
         $mail->AddAddress($udata['EMAIL'], $udata['EMAIL']);
         $sent = $mail->Send();
      }
   }
}

function unsubscribe($id)
{
   global $db, $tables;

   $user_id = $_SESSION['phpld']['user']['id'];

   $db->Execute("DELETE FROM `{$tables['article_subscribe']['name']}` WHERE `ARTICLE_ID` = ".$db->qstr($id)." AND `USER_ID` = ".$db->qstr($user_id)."");

}


function determine_article_type($type = 0)
{
   if (!preg_match ('`^[\d]+$`', $type))
      return false;

   $type = ($type < 0 || $type > 4 ? 0 : intval ($type));

   switch ($type) {
      case 4  :
         $return = 'featured';
         break;
      case 3  :
         $return = 'reciprocal';
         break;
      case 2  :
         $return = 'normal';
         break;
      case 1  :
         $return = 'free';
         break;
      case 0  :
      default :
         $return = 'none';
         break;
   }

   return (!empty ($return) ? $return : false);
}

function update_article_payment($pid, $data, $success, $raw)
{
   global $db, $tables;
   $pdata = $db->GetRow("SELECT * FROM `{$tables['payment']['name']}` WHERE `ID` = ".$db->qstr($pid));
   if (!$pdata['ID'])
      return false;
   $pdata['NAME']           = $data['name'];
   $pdata['EMAIL']          = $data['email'];
   if($data['total'] != '')
   $pdata['PAYED_TOTAL']    =  $data['total'];
   if($data['quantity'] != '')
   $pdata['PAYED_QUANTITY'] =  $data['quantity'];
   $pdata['CONFIRMED']      = $success;
   $pdata['CONFIRM_DATE']   = gmdate ('Y-m-d H:i:s');
   $pdata['RAW_LOG']        = $raw;
   if($data['payment_type'] != '')
   $pdata['PAYMENT_TYPE']    = $data['payment_type'];#if echeck or instant
   if($data['payment_status'] != '')
   $pdata['PAYMENT_STATUS']    = $data['payment_status']; #check for status as echeck does not clear
   $pdata['TOTAL']  = $pdata['TOTAL'];

   if ($pdata['PAYED_TOTAL'] <  $pdata['TOTAL'])
   {
      $pdata['CONFIRMED'] = -1;
   }
if (preg_match("/Pending/i", $data['payment_status']) )
        {
$pdata['CONFIRMED'] = 0;
        }


   db_replace('payment', $pdata, 'ID');
   $ldata = $db->GetRow("SELECT * FROM `{$tables['article']['name']}` WHERE `ID` = ".$db->qstr($pdata['ARTICLE_ID']));
   send_payment_notifications($pdata, $ldata);
   //Take no action if link not found
   if (!$ldata['ID'])
      return false;
   else
   {
      $ldata['PAYED'] = '1';
             // set the expiry_date
      $exp_date = calculate_expiry_date(time(), $pdata['QUANTITY'], $pdata['UM']);
         if ($exp_date != 0)
            $ldata['EXPIRY_DATE'] = gmdate ('Y-m-d H:i:s', $exp_date);
      if (PAY_AUTO_ACCEPT)
      {
         $ldata['STATUS'] = 2;
      }
   }
   db_replace('article', $ldata, 'ID');

}

function getHeader($header)
{
        // Try to get it from the $_SERVER array first
        $temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
        if (!empty($_SERVER[$temp])) {
            return $_SERVER[$temp];
        }

        // This seems to be the only way to get the Authorization header on
        // Apache
        if (function_exists('apache_request_headers')) {
            $headers = apache_request_headers();
            if (!empty($headers[$header])) {
                return $headers[$header];
            }
        }

        return false;
}

function isXmlHttpRequest()
{
	return (getHeader('X_REQUESTED_WITH') == 'XMLHttpRequest');

}


function get_user_level($uid) {
    global $db, $tables;
    
    $data = $db->cacheGetAll("SELECT * FROM `{$tables['user']['name']}` WHERE `ID`=".$db->qstr($uid));
    
    return $data[0]['LEVEL'];
}

function get_user_permissions($uid) {
    global $db, $tables;
    $data = $db->cacheGetAll("SELECT `CATEGORY_ID` FROM `{$tables['user_permission']['name']}` WHERE `USER_ID`=".$db->qstr($uid));
    return $data;
}

function get_user($id) {
    global $db, $tables;
    
    $data = $db->cacheGetAll("SELECT * FROM `{$tables['user']['name']}` WHERE `ID`=".$db->qstr($id));
    
    return $data;
}

function has_rights_on_cat($uid, $cid) {
    global $db, $tables;
    $cat = getCategoryByID($cid);
  
    $data = $db->getOne("SELECT COUNT(`ID`) AS c FROM `{$tables['user_permission']['name']}` WHERE `USER_ID`=".$db->qstr($uid)." AND `CATEGORY_ID` = ".$db->qstr($cid));
     
    
    if ($data['c']>0){
        return 1;
    } else
        if ($cat['PARENT_ID']>0)
            return has_rights_on_cat($uid, $cat['PARENT_ID']);
        else {
            return 0;
        }
}


function has_rights_on_all_cats($level) {
    if ($level == 3 || $level == 1) {
        return true;
    } else {
        return false;
    }
}

function link_has_payment($id) {
    global $db, $tables;
    
    $c = $db->GetOne("SELECT COUNT(*) FROM `{$tables['payment']['name']}` WHERE `LINK_ID` = ".$db->qstr($id));
    
    return $c;
}

function article_has_payment($id) {
    global $db, $tables;
    
    $c = $db->GetOne("SELECT COUNT(*) FROM `{$tables['payment']['name']}` WHERE `ARTICLE_ID` = ".$db->qstr($id));
    
    return $c;
}

function user_needs_approval($uid, $cid) {
    global $db, $tables;
    $level = get_user_level($uid);
    
    $category = getCategoryByID($cid);

    
    
   //default actions from db
    $r = $db->CacheGetAll("SELECT * FROM `{$tables['user_default_actions']['name']}` ORDER BY `LEVEL_ID` ASC");
    $default = array();
    
    
    for ($i=0; $i<count($r); $i++) {
        $default[$r[$i]['ID']] = $r[$i];
    }

    
    //actual user actions
    $r = $db->CacheGetAll("SELECT *
                            FROM `{$tables['user_default_actions']['name']}` AS `default`
                                INNER JOIN `{$tables['user_actions']['name']}` AS `actual`
                                ON `default`.`ID` = `actual`.`ACTION_ID`
                            WHERE `actual`.`USER_ID` = ".$db->qstr($uid)."
                            ORDER BY `default`.`LEVEL_ID` ASC");
//                                INNER JOIN `{$tables['user_permission']['name']}` AS `perm`
//                                ON `perm`.`USER_ID` = `actual`.`USER_ID`
    
                            $actual = array();
          
                     
    for ($i=0; $i<count($r); $i++) {
        $actual[$r[$i]['ACTION_ID']] = $r[$i]['VALUE'];
    }
    $rights = array();
    

    
    if ((has_rights_on_cat($uid, $cid) > 0) || (has_rights_on_all_cats($level))) {

    	//add link rights go here: has id 4;
        if ($level != 1 && $level != 3){
            $rights = rights_for_editor($actual, $default);
        } elseif ($level == 3) {
            $rights = rights_for_supereditor($actual, $default);
        } elseif ($level == 1) {
            
            $rights = rights_for_admin($actual, $default);
        }
    } 
    return $rights;
}

function rights_for_editor($actual, $default) {
    if ($actual[4] != '') {
        $rights['addLink'] = $actual[4];
    } else
        $rights['addLink'] = $default[4]['VALUE'];
    if ($actual[5] != '') {
        $rights['editLink'] = $actual[5];
    } else
        $rights['editLink'] = $default[5]['VALUE'];
    if ($actual[6] != '') {
        $rights['delLink'] = $actual[6];
    } else
        $rights['delLink'] = $default[6]['VALUE'];
    if ($actual[7] != '') {
        $rights['addCat'] = $actual[10];
    } else
        $rights['addCat'] = $default[10]['VALUE'];
    //edit category is 11
    if ($actual[11] != '') {
        $rights['editCat'] = $actual[11];
    } else
        $rights['editCat'] = $default[11]['VALUE'];
    if ($actual[12] != '') {
        $rights['delCat'] = $actual[12];
    } else
        $rights['delCat'] = $default[12]['VALUE'];
    //artilcle related
    if ($actual[16] != '') {
        $rights['addArt'] = $actual[16];
    } else
        $rights['addArt'] = $default[16]['VALUE'];
    if ($actual[17] != '') {
        $rights['editArt'] = $actual[17];
    } else
        $rights['editArt'] = $default[17]['VALUE'];
    if ($actual[18] != '') {
        $rights['delArt'] = $actual[18];
    } else
        $rights['delArt'] = $default[18]['VALUE'];
    return $rights;
}

function rights_for_supereditor($actual, $default) {
    if ($actual[19] != '') {
        $rights['addLink'] = $actual[19];
    } else
        $rights['addLink'] = $default[19]['VALUE'];
    if ($actual[20] != '') {
        $rights['editLink'] = $actual[20];
    } else
        $rights['editLink'] = $default[20]['VALUE'];
    if ($actual[21] != '') {
        $rights['delLink'] = $actual[21];
    } else
        $rights['delLink'] = $default[21]['VALUE'];
    if ($actual[22] != '') {
        $rights['addCat'] = $actual[22];
    } else
        $rights['addCat'] = $default[22]['VALUE'];
    if ($actual[23] != '') {
        $rights['editCat'] = $actual[23];
    } else
        $rights['editCat'] = $default[23]['VALUE'];
    if ($actual[24] != '') {
        $rights['delCat'] = $actual[24];
    } else
        $rights['delCat'] = $default[24]['VALUE'];
    //artilcle related
    if ($actual[25] != '') {
        $rights['addArt'] = $actual[25];
    } else
        $rights['addArt'] = $default[25]['VALUE'];
    if ($actual[26] != '') {
        $rights['editArt'] = $actual[26];
    } else
        $rights['editArt'] = $default[26]['VALUE'];
    if ($actual[27] != '') {
        $rights['delArt'] = $actual[27];
    } else
        $rights['delArt'] = $default[27]['VALUE'];
    //page related
    if ($actual[28] != '') {
        $rights['addPage'] = $actual[28];
    } else
        $rights['addPage'] = $default[28]['VALUE'];
    if ($actual[29] != '') {
        $rights['editPage'] = $actual[29];
    } else
        $rights['editPage'] = $default[29]['VALUE'];
    if ($actual[30] != '') {
        $rights['delPage'] = $actual[30];
    } else
        $rights['delPage'] = $default[30]['VALUE'];
    return $rights;
}

function rights_for_admin($actual, $default) {
    
    
    if ($actual[37] != '') {
        $rights['addLink'] = $actual[37];
    } else
        $rights['addLink'] = $default[37]['VALUE'];
    if ($actual[38] != '') {
        $rights['editLink'] = $actual[38];
    } else
        $rights['editLink'] = $default[38]['VALUE'];
    if ($actual[39] != '') {
        $rights['delLink'] = $actual[39];
    } else
        $rights['delLink'] = $default[39]['VALUE'];
    if ($actual[40] != '') {
        $rights['addCat'] = $actual[40];
    } else
        $rights['addCat'] = $default[40]['VALUE'];
    if ($actual[41] != '') {
        $rights['editCat'] = $actual[41];
    } else
        $rights['editCat'] = $default[41]['VALUE'];
    if ($actual[42] != '') {
        $rights['delCat'] = $actual[42];
    } else
        $rights['delCat'] = $default[42]['VALUE'];
    //artilcle related
    if ($actual[43] != '') {
        $rights['addArt'] = $actual[43];
    } else
        $rights['addArt'] = $default[43]['VALUE'];
    if ($actual[44] != '') {
        $rights['editArt'] = $actual[44];
    } else
        $rights['editArt'] = $default[44]['VALUE'];
    if ($actual[45] != '') {
        $rights['delArt'] = $actual[45];
    } else
        $rights['delArt'] = $default[45]['VALUE'];
    //page related
    if ($actual[46] != '') {
        $rights['addPage'] = $actual[46];
    } else
        $rights['addPage'] = $default[46]['VALUE'];
    if ($actual[47] != '') {
        $rights['editPage'] = $actual[47];
    } else
        $rights['editPage'] = $default[47]['VALUE'];
    if ($actual[48] != '') {
        $rights['delPage'] = $actual[48];
    } else
        $rights['delPage'] = $default[48]['VALUE'];
    return $rights;
}

function seo_url($id, $title, $type, $ext, $link_type)
{
   switch($type)
   {
      case 0 :
         //$url = 'article-' . $id;
         if($link_type == 1)
      		$url = 'article-' . $id;
   	     elseif($link_type == 2)
      		$url = 'link-' . $id; 
      break;

      case 1 :
  $url = makeUrlAlias($title);
         $url = seo_rewrite($url) . '-' . $id;
      break;
   }

   switch($ext)
   {
      case 0 :
         $url .= '.htm';
      break;

      case 1 :
         $url .= '.html';
      break;

      case 2 :
         $url .= '.php';
      break;

      case 3 :
         $url .= '/';
      break;
   }

   if($link_type == 1)
      $url = 'articles/' . $url;
   elseif($link_type == 2)
      $url = 'detail/' . $url;

   return $url;
}




function seo_rewrite($text)
{
   $t = str_replace(')', '', $text);
   $t = preg_replace ('`[^\w_-]`', '-', $t);
   $t = str_replace ('__', '-', $t);
 $t = str_replace ('--', '-', $t);
	 $t = str_replace ('---', '-', $t);

   return $t;
}

function subscribe_to_commentslc($id)
{
   global $db, $tables;

   $data['USER_ID']    = $_SESSION['phpld']['user']['id'];
   $data['COMMENT_ID'] = $db->GetOne("SELECT `ID` FROM `{$tables['linkcomment']['name']}` WHERE `LINK_ID` = ".$db->qstr($id)." ORDER BY `ID` DESC LIMIT 1");
   $data['LINK_ID'] = $id;

   $n = $db->GetOne("SELECT COUNT(*) FROM `{$tables['link_subscribe']['name']}` WHERE `USER_ID` = '{$_SESSION['phpld']['user']['id']}' AND `LINK_ID` = ".$db->qstr($id));

   if($n < 1)
      db_replace('link_subscribe', $data, 'ID');

}

function send_mail_to_subscriberslc($id)
{
   global $db, $tables;

   $article = $db->GetRow("SELECT * FROM `{$tables['link']['name']}` WHERE `ID` = ".$db->qstr($id));
   $comment = $db->GetRow("SELECT * FROM `{$tables['linkcomment']['name']}` WHERE `LINK_ID` = ".$db->qstr($id)." ORDER BY `ID` DESC LIMIT 1");
   $users   = $db->GetAll("SELECT * FROM `{$tables['link_subscribe']['name']}` WHERE `LINK_ID` = ".$db->qstr($id)." AND `USER_ID` <> ".$db->qstr($comment['USER_ID']));

   foreach($users as $k => $v)
   {
      $udata = $db->GetRow("SELECT * FROM `{$tables['user']['name']}` WHERE `ID` = ".$db->qstr($v['USER_ID']));

      $mail = get_emailer();
      $mail->Body    = 'You get this mail because you are subscribed for receiving reviews on '. $link['TITLE']
                      ."\n\n"
                      ."-----------------------\n"
                      . $comment['COMMENT'] . "\n"
                      .'-----------------------'
                      ."\n\nlink: " . str_replace('//', '/', SITE_URL . "/detail.php?id=") . $id
                      ."\n\n"
                      ."Regards,\n" . SITE_NAME;

      $mail->Subject = 'New answers on "' . $article['TITLE'] . '"';
      $mail->isHTML  = true;

      if (!empty($udata['EMAIL']))
      {
         $mail->AddAddress($udata['EMAIL'], $udata['EMAIL']);
         $sent = $mail->Send();
      }
   }
}

function unsubscribelc($id)
{
   global $db, $tables;

   $user_id = $_SESSION['phpld']['user']['id'];

   $db->Execute("DELETE FROM `{$tables['link_subscribe']['name']}` WHERE `LINK_ID` = ".$db->qstr($id)." AND `USER_ID` = ".$db->qstr($user_id)."");

}

function has_confirmed_email($uid) {
    global $db, $tables;
    
    $data = $db->GetAll("SELECT * FROM `{$tables['user']['name']}` WHERE `ID`=".$db->qstr($uid));
  
    if ($data[0]['EMAIL_CONFIRMED']== 1) {
        return true;
    } else {
        return false;
    }
}

function email_is_confirmed($email) {
    global $db, $tables;
    
    //check user table first
    $result = $db->cacheGetAll("SELECT * FROM `{$tables['user']['name']}` WHERE `EMAIL`=".$db->qstr($email));
  
    $conf1 = $result[0]['EMAIL_CONFIRMED'];
    
    //check link table
    $result = $db->cacheGetAll("SELECT * FROM `{$tables['link']['name']}` WHERE `OWNER_EMAIL`=".$db->qstr($email));
  
    $conf2 = $result[0]['OWNER_EMAIL_CONFIRMED'];
    
    //check article table
    $result = $db->cacheGetAll("SELECT * FROM `{$tables['article']['name']}` WHERE `OWNER_EMAIL`=".$db->qstr($email));
  
    $conf3 = $result[0]['OWNER_EMAIL_CONFIRMED'];
    
    if (($conf1== 1) || ($conf2== 1) || ($conf3== 1)) {
        return true;
    } else {
        return false;
    }
}

function getRSS_feeds($url)
{
	require_once '../libs/rss/simplepie.inc';
	
	
	$feed = new SimplePie();
	
	$feed->strip_ads(true);
	
	$feed->feed_url($url);
	$feed->init();
	$items = $feed->get_items();
	$feeds = array();
	$i = -1;
	foreach($items as $item) {
		$i++;
		$feeds[$i]['title'] = $item->get_title();
		$feeds[$i]['description'] = $item->get_description();
		$feeds[$i]['url'] = $item->get_permalink();
		$feeds[$i]['date'] = $item->get_date("Y-m-d H:i:s");
	}
//	print_r($feeds);
	return $feeds;
}

function saveRSS_feeds($categoryId = '')
{
	global $db, $tables;
	$b = true;
	if ($categoryId == '') {
		$categories = $db->GetAll("SELECT ID FROM {$tables['category']['name']}");
		for ($i = 0; $i < count($categories); $i++) {
			$id = $categories[$i]['ID'];
			if(!saveCategoryFeeds($id)) {
				$b = false;
			}
		}
	} else {
		return saveCategoryFeeds($categoryId);
	}
	return $b;
}

function saveCategoryFeeds($categoryId)
{
	global $db, $tables;
	$b = true;
	$url = $db->GetOne("SELECT RSS_URL
							FROM {$tables['category']['name']}
							WHERE ID=".$db->qstr($categoryId));
	if ($url != NULL) {
		$db->Execute("DELETE FROM {$tables['news']['name']} WHERE CATEGORY_ID=".$db->qstr($categoryId));
		$feeds = getRSS_feeds($url);
		for ($i = 0; $i < count($feeds); $i++) {
			$title = $feeds[$i]['title'];
			$description = $feeds[$i]['description'];
			$url = $feeds[$i]['url'];
			$date = $feeds[$i]['date'];
			
			if (is_null($db->Execute("INSERT
						INTO {$tables['news']['name']}(TITLE,URL,DESCRIPTION,CATEGORY_ID, DATE_ADDED)
						VALUES(".$db->qstr($title).",".$db->qstr($url).",".$db->qstr($description).",".$db->qstr($categoryId).", ".$db->qstr($date).")
			"))) {
				$b = false;
			}
		}
	}
	return $b;
}


function curPageURL() {
    $isHTTPS = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on");
    $port = (isset($_SERVER["SERVER_PORT"]) && ((!$isHTTPS && $_SERVER["SERVER_PORT"] != "80") || ($isHTTPS && $_SERVER["SERVER_PORT"] != "443")));
    $port = ($port) ? ':'.$_SERVER["SERVER_PORT"] : '';
    $url = ($isHTTPS ? 'https://' : 'http://').$_SERVER["SERVER_NAME"].$port.$_SERVER["REQUEST_URI"];
    return $url;
}


function recacheCategory($id) {
	global $db, $tables;
	
	$cat['CACHE_TITLE'] = mysql_escape_string(trim (buildCategUrlTitle($id)));
    $cat['CACHE_URL']   = mysql_escape_string(trim (buildCategUrl($id)));
    
    $sql = $db->Execute("UPDATE ".$tables['category']['name']." SET `CACHE_TITLE`=".$db->qstr($cat['CACHE_TITLE']).", 
    `CACHE_URL`=".$db->qstr($cat['CACHE_URL'])." WHERE `ID`=".$db->qstr($id)."");
    
	$scats = get_sub_categories($id);
	
	for ($i=0; $i<count($scats); $i++) {
		recacheCategory($scats[$i]);
	}
}
function makeUrlAlias ($url, $length = 150)
{
   $url = (string) $url;
   if (empty ($url))
   {
      //nothing to do
      return '';
   }

   $length = (int) $length;
   $length = ($length > 0 ? $length : 1);

   //Define word separator
   $separator = '_';

   //Remove white-space chars
   $search = array (

      "\r\n" ,  //Windows
      "\n" ,  //*NIX
      "\r" ,  //Mac
      "\t" ,  //Tab
      "\x0B" ,  //Vertical Tab
      "\0" //NULL BYTE
   );
   $url = str_replace ($search, $separator, $url);

   //List of special chars and their replacements
   $replace = array (

      '&' => 'and' ,
      '\'' => '' ,
      'À' => 'A' ,
      'À' => 'A' ,
      'Á' => 'A' ,
      'Â' => 'A' ,
      'Ã' => 'A' ,
      'Ä' => 'AE' ,
      'Å' => 'A' ,
      'Å' => 'A' ,
      'Æ' => 'AE' ,
      'Ā' => 'A' ,
      'Ą' => 'A' ,
      'Ă' => 'A' ,
      'Ç' => 'C' ,
      'Ć' => 'C' ,
      'Č' => 'C' ,
      'Ĉ' => 'C' ,
      'Ċ' => 'C' ,
      'Ď' => 'D' ,
      'Đ' => 'D' ,
      'È' => 'E' ,
      'É' => 'E' ,
      'Ê' => 'E' ,
      'Ë' => 'E' ,
      'Ē' => 'E' ,
      'Ę' => 'E' ,
      'Ĕ' => 'E' ,
      'Ė' => 'E' ,
      'Ĝ' => 'G' ,
      'Ğ' => 'G' ,
      'Ġ' => 'G' ,
      'Ģ' => 'G' ,
      'Ĥ' => 'H' ,
      'Ħ' => 'H' ,
      'Ì' => 'I' ,
      'Í' => 'I' ,
      'Î' => 'I' ,
      'Î' => 'I' ,
      'Ï' => 'I' ,
      'Ī' => 'I' ,
      'Ĩ' => 'I' ,
      'Ĭ' => 'I' ,
      'Į' => 'I' ,
      'İ' => 'I' ,
      'Ĳ' => 'J' ,
      'Ĵ' => 'J' ,
      'Ķ' => 'K' ,
      'Ľ' => 'K' ,
      'Ĺ' => 'K' ,
      'Ļ' => 'K' ,
      'Ŀ' => 'K' ,
      'Ñ' => 'N' ,
      'Ń' => 'N' ,
      'Ň' => 'N' ,
      'Ņ' => 'N' ,
      'Ŋ' => 'N' ,
      'Ò' => 'O' ,
      'Ó' => 'O' ,
      'Ô' => 'O' ,
      'Õ' => 'O' ,
      'Ö' => 'OE' ,
      'Ø' => 'O' ,
      'Ō' => 'O' ,
      'Ő' => 'O' ,
      'Ŏ' => 'O' ,
      'Œ' => 'OE' ,
      'Ŕ' => 'R' ,
      'Ř' => 'R' ,
      'Ŗ' => 'R' ,
      'Ś' => 'S' ,
      'Ş' => 'S' ,
      'Ŝ' => 'S' ,
      'Ș' => 'S' ,
      'Ť' => 'T' ,
      'Ţ' => 'T' ,
      'Ŧ' => 'T' ,
      'Ț' => 'T' ,
      'Ù' => 'U' ,
      'Ù' => 'U' ,
      'Ú' => 'U' ,
      'Ú' => 'U' ,
      'Û' => 'U' ,
      'Ü' => 'UE' ,
      'Ū' => 'U' ,
      'Ů' => 'U' ,
      'Ű' => 'U' ,
      'Ŭ' => 'U' ,
      'Ũ' => 'U' ,
      'Ų' => 'U' ,
      'Ŵ' => 'W' ,
      'Ŷ' => 'Y' ,
      'Ÿ' => 'Y' ,
      'Ź' => 'Z' ,
      'Ż' => 'Z' ,
      'à' => 'a' ,
      'á' => 'a' ,
      'â' => 'a' ,
      'ã' => 'a' ,
      'ä' => 'ae' ,
      'å' => 'a' ,
      'ā' => 'a' ,
      'ą' => 'a' ,
      'ă' => 'a' ,
      'å' => 'a' ,
      'æ' => 'ae' ,
      'ç' => 'c' ,
      'ć' => 'c' ,
      'č' => 'c' ,
      'ĉ' => 'c' ,
      'ċ' => 'c' ,
      'ď' => 'd' ,
      'đ' => 'd' ,
      'è' => 'e' ,
      'é' => 'e' ,
      'ê' => 'e' ,
      'ë' => 'e' ,
      'ē' => 'e' ,
      'ę' => 'e' ,
      'ĕ' => 'e' ,
      'ė' => 'e' ,
      'ƒ' => 'f' ,
      'ĝ' => 'g' ,
      'ğ' => 'g' ,
      'ġ' => 'g' ,
      'ģ' => 'g' ,
      'ĥ' => 'h' ,
      'ħ' => 'h' ,
      'ì' => 'i' ,
      'í' => 'i' ,
      'î' => 'i' ,
      'ï' => 'i' ,
      'ī' => 'i' ,
      'ĩ' => 'i' ,
      'ĭ' => 'i' ,
      'į' => 'i' ,
      'ı' => 'i' ,
      'ĳ' => 'j' ,
      'ĵ' => 'j' ,
      'ķ' => 'k' ,
      'ĸ' => 'k' ,
      'ł' => 'l' ,
      'ľ' => 'l' ,
      'ĺ' => 'l' ,
      'ļ' => 'l' ,
      'ŀ' => 'l' ,
      'ñ' => 'n' ,
      'ń' => 'n' ,
      'ň' => 'n' ,
      'ņ' => 'n' ,
      'ŉ' => 'n' ,
      'ŋ' => 'n' ,
      'ò' => 'o' ,
      'ó' => 'o' ,
      'ô' => 'o' ,
      'õ' => 'o' ,
      'ö' => 'oe' ,
      'ø' => 'o' ,
      'ō' => 'o' ,
      'ő' => 'o' ,
      'ŏ' => 'o' ,
      'œ' => 'oe' ,
      'ŕ' => 'r' ,
      'ř' => 'r' ,
      'ŗ' => 'r' ,
      'ù' => 'u' ,
      'ú' => 'u' ,
      'û' => 'u' ,
      'ü' => 'ue' ,
      'ū' => 'u' ,
      'ů' => 'u' ,
      'ű' => 'u' ,
      'ŭ' => 'u' ,
      'ũ' => 'u' ,
      'ų' => 'u' ,
      'ŵ' => 'w' ,
      'ÿ' => 'y' ,
      'ŷ' => 'y' ,
      'ż' => 'z' ,
      'ź' => 'z' ,
      'ß' => 'ss' ,
      'ſ' => 's' ,
      'Α' => 'A' ,
      'Ά' => 'A' ,
      'Β' => 'B' ,
      'Γ' => 'G' ,
      'Δ' => 'D' ,
      'Ε' => 'E' ,
      'Έ' => 'E' ,
      'Ζ' => 'Z' ,
      'Η' => 'I' ,
      'Ή' => 'I' ,
      'Θ' => 'TH' ,
      'Ι' => 'I' ,
      'Ί' => 'I' ,
      'Ϊ' => 'I' ,
      'Κ' => 'K' ,
      'Λ' => 'L' ,
      'Μ' => 'M' ,
      'Ν' => 'N' ,
      'Ξ' => 'KS' ,
      'Ο' => 'O' ,
      'Ό' => 'O' ,
      'Π' => 'P' ,
      'Ρ' => 'R' ,
      'Σ' => 'S' ,
      'Τ' => 'T' ,
      'Υ' => 'Y' ,
      'Ύ' => 'Y' ,
      'Ϋ' => 'Y' ,
      'Φ' => 'F' ,
      'Χ' => 'X' ,
      'Ψ' => 'PS' ,
      'Ω' => 'O' ,
      'Ώ' => 'O' ,
      'α' => 'a' ,
      'ά' => 'a' ,
      'β' => 'b' ,
      'γ' => 'g' ,
      'δ' => 'd' ,
      'ε' => 'e' ,
      'έ' => 'e' ,
      'ζ' => 'z' ,
      'η' => 'i' ,
      'ή' => 'i' ,
      'θ' => 'th' ,
      'ι' => 'i' ,
      'ί' => 'i' ,
      'ϊ' => 'i' ,
      'ΐ' => 'i' ,
      'κ' => 'k' ,
      'λ' => 'l' ,
      'μ' => 'm' ,
      'ν' => 'n' ,
      'ξ' => 'ks' ,
      'ο' => 'o' ,
      'ό' => 'o' ,
      'π' => 'p' ,
      'ρ' => 'r' ,
      'σ' => 's' ,
      'τ' => 't' ,
      'υ' => 'y' ,
      'ύ' => 'y' ,
      'ϋ' => 'y' ,
      'ΰ' => 'y' ,
      'φ' => 'f' ,
      'χ' => 'x' ,
      'ψ' => 'ps' ,
      'ω' => 'o' ,
      'ώ' => 'o'
   );
   $url = strtr ($url, $replace);

   //Remove PHP and HTML tags
   $url = strip_tags ($url);

   //Remove chars except letters, numbers
//   $url = preg_replace ('#[^A-Za-z0-9]#', $separator, $url);

   //Remove multiple occurences of defined separator
   $url = preg_replace ('#[\\' . $separator . ']+#', $separator, $url);

   //Make lowercase
   //$url = mb_strtolower ($url);
   //does not work for < php 4.3.0, replaced with strtolower as below: 
   
   $url = strtolower ($url);

   $url = trim ($url);

   //Keep only max length
   $url = mb_substr ($url, 0, $length);

   //Trim replacement chars from start & end
   $search = array (

      '#^[^A-Za-z0-9]*#' ,
      '#[^A-Za-z0-9]*$#'
   );
   $url = preg_replace ($search, '', $url);

   return (string) $url;
}

function get_widgets() {
	global $db, $tables;
	$w = array();
	$wid = array();
	$i=0;
	if (is_dir(INSTALL_PATH."/widgets/")) {
	    if ($dh = opendir(INSTALL_PATH."/widgets/")) {
	        while (($file = readdir($dh)) !== false) {
	            if ($file != "." && $file != ".." && file_exists(INSTALL_PATH."/widgets/".$file."/config.xml")) {
	            	$w[$i] = $file;
	            	$i++;
	            }
	        }
	        closedir($dh);
	    }
	    asort($w);
//	    $i = 0;
	    foreach($w as $k=>$v) {
	    	$xml = get_widget_xml($v);
	    	$ind = count($wid[$xml['ZONETYPE']]);
	    	$wid[$xml['ZONETYPE']][$ind] = $xml;
	    	$wid[$xml['ZONETYPE']][$ind]['NAME'] = $v;
			$wids = $db->GetAll("SELECT * FROM `{$tables['widget']['name']}` WHERE `NAME` = ".$db->qstr($wid[$xml['ZONETYPE']][$ind]['NAME']));
			if (count($wids)>0) {
				$wid[$xml['ZONETYPE']][$ind]['INSTALLED'] = 1;
			} else {
				$wid[$xml['ZONETYPE']][$ind]['INSTALLED'] = 0;
			}
//	        $i++;
	    }
    
	    return $wid;
	}
}

function get_active_widgets() {
	global $db, $tables;
	
	$zones = get_widget_front_zones();
	for ($i=0; $i<count($zones); $i++) {
		if (strpos($zones[$i]['NAME'], "MAIN") === false && strpos($zones[$i]['NAME'], "DETAIL") === false) {
			$wid[$zones[$i]['NAME']] = $db->GetAll("SELECT * FROM `{$tables['widget_activated']['name']}` WHERE `ZONE` = ".$db->qstr($zones[$i]['NAME'])." ORDER BY `ORDER_ID` ASC");
			
			foreach ($wid[$zones[$i]['NAME']] as $k=>$v) {
				if ($v['NAME'] != '') {
					if (file_exists(INSTALL_PATH."widgets/".$v['NAME']."/".$v['NAME'].".php")) {
					 
						require_once INSTALL_PATH."widgets/".$v['NAME']."/".$v['NAME'].".php";
						if (class_exists($v['NAME'])) {
						    
							$w = new $v['NAME']($v['NAME'],'','');
							$wid[$zones[$i]['NAME']][$k]['CONTENT'] = $w->getContent();
							$wid[$zones[$i]['NAME']][$k]['SETTINGS'] = $w->getFrontSettings();
		
						}
					}
				}
			}
			
		} else {
			 $main = $db->GetRow("SELECT * FROM `{$tables['widget_activated']['name']}` WHERE `ZONE` = ".$db->qstr($zones[$i]['NAME'])." AND `NAME` = 'MainContent' ORDER BY `ORDER_ID` ASC");
		
			 $wid[$zones[$i]['NAME']]['TOP'] = $db->GetAll("SELECT * FROM `{$tables['widget_activated']['name']}` WHERE `ZONE` = ".$db->qstr($zones[$i]['NAME'])." AND `ORDER_ID` < ".$main['ORDER_ID']." ORDER BY `ORDER_ID` ASC");
			 
			foreach ($wid[$zones[$i]['NAME']]['TOP'] as $k=>$v) {
				if ($v['NAME'] != '') {
					if (file_exists(INSTALL_PATH."widgets/".$v['NAME']."/".$v['NAME'].".php")) {
						require_once INSTALL_PATH."widgets/".$v['NAME']."/".$v['NAME'].".php";
						if (class_exists($v['NAME']) ) {
							$w = new $v['NAME']($v['NAME'],'','');
							$wid[$zones[$i]['NAME']]['TOP'][$k]['CONTENT'] = $w->getContent();
							$wid[$zones[$i]['NAME']]['TOP'][$k]['SETTINGS'] = $w->getFrontSettings();
						}
					}
				}
			}
			$wid[$zones[$i]['NAME']]['BOT'] = $db->GetAll("SELECT * FROM `{$tables['widget_activated']['name']}` WHERE `ZONE` = ".$db->qstr($zones[$i]['NAME'])." AND `ORDER_ID` > ".$main['ORDER_ID']." ORDER BY `ORDER_ID` ASC");
			 
			foreach ($wid[$zones[$i]['NAME']]['BOT'] as $k=>$v) {
				if ($v['NAME'] != '') {
					if (file_exists(INSTALL_PATH."widgets/".$v['NAME']."/".$v['NAME'].".php")) {
						require_once INSTALL_PATH."widgets/".$v['NAME']."/".$v['NAME'].".php";
						if (class_exists($v['NAME'])) {
							$w = new $v['NAME']($v['NAME'],'','');
							$wid[$zones[$i]['NAME']]['BOT'][$k]['CONTENT'] = $w->getContent();
							$wid[$zones[$i]['NAME']]['BOT'][$k]['SETTINGS'] = $w->getFrontSettings();
						}
					}
				}
			}
		}
	}
	
	return $wid;
}

function pick_widget_zones($name) {
	global $db, $tables;
	$w = $db->GetRow("SELECT * FROM `{$tables['widget']['name']}` WHERE `NAME`=".$db->qstr($name));

	$zones = $db->GetAll("SELECT * FROM `{$tables['widget_zones']['name']}` WHERE `TYPE` =".$db->qstr($w['TYPE']));
	for ($i=0; $i<count($zones); $i++) {
		$wid = $db->GetAll("SELECT * FROM `{$tables['widget_activated']['name']}` WHERE `ZONE` = ".$db->qstr($zones[$i]['NAME'])." AND `NAME`=".$db->qstr($name));
		if (count($wid) > 0) {
			$zones[$i]['ACTIVE'] = 1;	
		}
	}
	
	return $zones;
}

function get_widget_zones() {
	global $db, $tables;
	$w = $db->GetAll("SELECT * FROM `{$tables['widget_zones']['name']}`");
	
	$wid = array();
	for ($i=0; $i<count($w); $i++) {
		$wid[$w[$i]['TYPE']][] = $w[$i];	
	}
	
	return $wid;
}

function get_widget_front_zones() {
	global $db, $tables;
	$w = $db->GetAll("SELECT * FROM `{$tables['widget_zones']['name']}`");
	
	return $w;
}

function get_widgets_per_zone($zone, $type) {
	global $db, $tables;
	
	$wid = $db->GetAll("SELECT w.*, wa.`ZONE`, IFNULL(wa.`ORDER_ID`, 1000) AS `ORDER_ID`  
						FROM `{$tables['widget']['name']}` w 
							LEFT JOIN `{$tables['widget_activated']['name']}` wa
							ON (w.`NAME` = wa.`NAME` AND (wa.ZONE = ".$db->qstr($zone)." OR wa.ZONE ='') ) 
						WHERE w.`TYPE` = ".$db->qstr($type)." 
						ORDER BY `ORDER_ID` ASC
						");
	return $wid;
}

function get_widget_xml($name) {
	$wid = array();
	if (($xml = file_get_contents(INSTALL_PATH."/widgets/".$name."/config.xml")) !== false) {
		$p = xml_parser_create();
		xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
		xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
		xml_parse_into_struct($p, $xml, $vals, $index);
		xml_parser_free($p);
		
		foreach ($index as $k=>$v) {
			if (strtoupper($k) != 'NAME' && strtoupper($k) != 'VALUE' && strtoupper($k) != 'INFO' && strtoupper($k) != 'IDENTIFIER' && strtoupper($k) != 'ALLOWED') {
				$wid[strtoupper($k)] = $vals[$v[0]]['value'];
			} else {
				for ($i=0; $i<count($index[$k]); $i++) {
					$wid['SETTINGS'][$i][strtoupper($k)] = $vals[$index[$k][$i]]['value'];
				} 	
			}
		}
	}
	return $wid;
}

?>