<?php 
/*#################################################################*\
|# Licence Number 0DT7-0122-0NW1-0210
|# -------------------------------------------------------------   #|
|# Copyright (c)2010 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
# ################################################################################
*/

require_once 'init.php';

//Load Snoopy class that simulates a browser
require_once 'libs/snoopy/Snoopy.class.php';

$error = 0;

//What output to make,
//form or import results
$showImport = 0;

$action = (!empty ($_REQUEST['action']) ? $_REQUEST['action'] : 'default');
$action = strtolower (trim ($action));

//Define Google languages
$langChoice = array ('lang_ar'     => 'Arabic', 'lang_bg'     => 'Bulgarian', 'lang_ca'     => 'Catalan', 'lang_zh-CN'  => 'Chinese (Simplified)',
                     'lang_zh-TW'  => 'Chinese (Traditional)', 'lang_hr'     => 'Croation', 'lang_cs'     => 'Czech', 'lang_da'     => 'Danish',
                     'lang_nl'     => 'Dutch', 'lang_en'      => 'English', 'lang_et'     => 'Estonian', 'lang_fi'     => 'Finnish',
                     'lang_fr'     => 'French', 'lang_de'     => 'German', 'lang_el'     => 'Greek', 'lang_iw'		=>'Hebrew',
					 'lang_hu'     =>'Hungarian', 'lang_is'   => 'Icelandic', 'lang_id' => 'Indonesian', 'lang_it' =>'Italian', 'lang_ja' => 'Japanese',
					 'lang_ko' => 'Korean', 'lang_lv' => 'Latvian', 'lang_lt' => 'Lithuanian', 'lang_no' => 'Norwegian',
					 'lang_pl' => 'Polish', 'lang_pt' => 'Portuguese', 'lang_ro' => 'Romanian', 'lang_ru' => 'Russian',
					 'lang_sr' => 'Serbian', 'lang_sk' => 'Slovak', 'lang_sl' => 'Slovenian', 'lang_es' => 'Spanish',
					 'lang_sv' => 'Swedish', 'lang_tr' => 'Turkish'
                  );
$tpl->assign('langChoice', $langChoice);

//Define results count
$googleResultsCount = array (
                        '10'  => '10 '._L('Results'),
                        '20'  => '20 '._L('Results'),
                        '30'  => '30 '._L('Results'),
                        '50'  => '50 '._L('Results'),
                        '100' => '100 '._L('Results')
                     );
$tpl->assign('googleResultsCount', $googleResultsCount);

//Define Occurrences
$occurrences = array (
                  'any'    => _L('anywhere in the page'),
                  'title'  => _L('in the title of the page'),
                  'body'   => _L('in the text of the page'),
                  'url'    => _L('in the URL of the page'),
                  'links'  => _L('in links to the page')
               );
$tpl->assign('occurrences', $occurrences);

$categID = (isset ($_POST['CATEGORY_ID']) && $_POST['CATEGORY_ID'] > 0 ? intval ($_POST['CATEGORY_ID']) : 0);
$tpl->assign('CATEGORY_ID', $categID);

if (isset ($_POST['import']) && isset ($_POST['toimport'])) {
   //Show import page
   $showImport = 1;

   $importErrors  = 0;
   $importSuccess = 0;
   $importSkipped = 0;

   //Was a category selected?
   if ($categID > 0) {
      if (ENABLE_PAGERANK == 1) {
         require_once 'include/pagerank.php';
      }

      $currentDate = gmdate ('Y-m-d H:i:s');

      foreach ($_POST['toimport'] as $key) {
         $data = array();
         $url = $_POST['URL'][$key];

         //Correct URL
         if (strlen (trim ($url)) > 0 && !preg_match ('#^http[s]?:\/\/#i', $url))
            $url = "http://".$url;

         //Check first if URL is already available
         if (defined ('ALLOW_MULTIPLE') && ALLOW_MULTIPLE == 1) {
            $isUnique = checkUrlUnique('link', 'URL', $url, NULL, 'CATEGORY_ID', $categID);
         } else {
            $isUnique = checkUrlUnique('link', 'URL', $url);
         }

         if ($isUnique == 1) {
            //Get a new ID
            $id = $db->GenID($tables['link']['name'].'_SEQ');
            $data['ID'] = (!empty ($id) ? intval ($id) : '');

            //Get imported infos
            $data['TITLE'] = $_POST['TITLE'][$key];
            $data['URL']   = $url; //Already clean
            $data['DESCRIPTION'] = $_POST['DESCRIPTION'][$key];
            $data['OWNER_NAME'] = $_POST['OWNER_NAME'][$key];
            $data['OWNER_EMAIL'] = $_POST['OWNER_EMAIL'][$key];

            //Add more options
            $data['CATEGORY_ID'] = $categID;
            $data['STATUS'] = 2;
            $data['VALID']  = 1;
            $data['HITS']   = 0;
            $data['LAST_CHECKED']   = $currentDate;
            $data['DATE_ADDED']     = $currentDate;
            $data['DATE_MODIFIED']  = $currentDate;

            //Calculate PageRank
            if (ENABLE_PAGERANK == 1) {
               $data['PAGERANK'] = get_page_rank($data['URL']);
            }
            $data['PAGERANK'] = (empty ($data['PAGERANK']) || $data['PAGERANK'] < -1 ? -1 : intval ($data['PAGERANK']));
            $data['PAGERANK'] = (empty ($data['PAGERANK']) || $data['PAGERANK'] > 10 ? 10 : intval ($data['PAGERANK']));

            $saveStatus = $db->Replace($tables['link']['name'], $data, 'ID', true);

            if (!$saveStatus)
               $importErrors++;
            else
               $importSuccess++;
         } else {
            $importSkipped++;
         }
		
         //empty link_to_import table
         $db->Execute("DELETE FROM {$tables['link_to_import']['name']}");
         
         //Free memory
         unset ($data, $isUnique, $url);
      }

      $tpl->assign('importErrors'  , $importErrors);
      $tpl->assign('importSuccess' , $importSuccess);
      $tpl->assign('importSkipped' , $importSkipped);
   } else {
      //No or wrong category selected
      $error++;
      $errorMsg = _L('Please select a category');
      
      if (AJAX_CAT_SELECTION_METHOD == 0) {
         $categs = get_categs_tree();
         $tpl->assign('categs', $categs);
	  }
	
	  $importResults  = $db->GetAll("SELECT * FROM {$tables['link_to_import']['name']}");
	
	  $tpl->assign('importResults', $importResults);
   }
} else {
	if ($error > 0 && $_REQUEST['start'] > 0) {
	   $start = (isset ($_REQUEST['prevStart']) && preg_match ('`^[\d]+$`', $_REQUEST['prevStart']) ? intval ($_REQUEST['prevStart']) : 0);
	} else {
	   $start = (isset ($_REQUEST['start']) && preg_match ('`^[\d]+$`', $_REQUEST['start']) ? intval ($_REQUEST['start']) : 0);
	}
	$tpl->assign('start', $start);
	
	switch ($action) {
	   case 'google'  :
	   	  if (isset($_REQUEST['num']) && ($_SESSION['imported']!=1)) {	
	      	if ($errorMsg == '') {
	      		// reffers to lines 171 - 174
	      		$tpl->assign("show_me", 1);
	      	}
	      	$_SESSION['imported'] = 1;
	   	  } else {
	   	  	$_SESSION['imported'] = 0;
	   	  }
	      $showImport = 1;
	
	      if (AJAX_CAT_SELECTION_METHOD == 0) {
	         $categs = get_categs_tree();
	         $tpl->assign('categs', $categs);
	      }
	
	      $importResults  = $db->GetAll("SELECT * FROM {$tables['link_to_import']['name']}");
	
		  $tpl->assign('importResults', $importResults);

	
	      break;
	
	   //Import from DMOZ
	   case 'dmoz'  :
	      $showImport = 1;
	      if (AJAX_CAT_SELECTION_METHOD == 0) {
	         $categs = get_categs_tree();
	         $tpl->assign('categs', $categs);
	      }
	
	      $importResults = array();
	      //Get import settings
	      $ddata = array();
		  $ddata['impdomain']= '0';
	      $ddata['impdomain'] = (!empty ($_REQUEST['impdomain']) ? 1 : 0);
	      $nextStart = $start + $gdata['num'];
	      $tpl->assign('nextStart', $nextStart);
	
	      //Build Dmoz search URL
	      $dmozAddress = (isset ($_REQUEST['dmozurl']) ? trim ($_REQUEST['dmozurl']) : '');
	      $tpl->assign('dmozAddress', $dmozAddress);
	
	      //Get Google results
	      if (class_exists ('Snoopy')) {
	         //Initialize Snoopy class
	         $snoopy = new Snoopy;
	
	         //Define User Agent we masquerade as
	         $snoopy->agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0';
	
	         //Define HTTP accept types
	         $snoopy->accept = 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,image/jpeg,image/gif,*/*;q=0.5';
	
	         //Fetch content of the web page
	         $fetchResults = $snoopy->fetch($dmozAddress);
	
	         if ($fetchResults === false) {
	            //Could not fetch URL
	            //Possible library missing or an error occured
	            //Reciprocal link validation failed
	            $responseContent = '';
	         }
	
	         //Get Content
	         $responseContent = (is_string ($snoopy->results) && !empty ($snoopy->results) ? $snoopy->results : '');
	      } else {
	         //Use simple results fetching
	         $handle = @ fopen ($dmozAddress, 'r');
	         $responseContent = '';
	
	         while (!feof ($handle)) {
	            $responseContent .= @ fread ($handle, 8192);
	         }
	
	         @ fclose ($handle);
	      }

	      if (empty ($responseContent)) {
	         //Error while fetching results page
	         $error++;
	         $errorMsg = _L('Could not fetch Dmoz results page').'!';
	      } else {
	         $cleanContent = explode ("<hr>\n<ul>", $responseContent);
	         $resultParts  = explode ("<li>", $cleanContent[1]);
	         //Remove junk
	         if (isset ($resultParts[0]))
	            unset ($resultParts[0]);
	
	         $i = 0;
	         foreach ($resultParts as $key => $result) {
	            //Get URL
	            $u1 = explode ('<a href="', $result);
	            $u2 = explode ('">', $u1[1]);
	            $importResults[$i]['URL'] = un_escape(clean_string($u2[0]));
	
	            $domainName = parseDomain($importResults[$i]['URL']);
	
	            $importResults[$i]['URL'] = ($ddata['impdomain'] == 1 ? parseDomain($importResults[$i]['URL'], 1) : $importResults[$i]['URL']);
	
	            //Correct URL
	            if (strlen (trim ($importResults[$i]['URL'])) > 0 && !preg_match ('#^http[s]?:\/\/#i', $importResults[$i]['URL']))
	               $importResults[$i]['URL'] = "http://".$importResults[$i]['URL'];
	
	            //Get title
	            $t1 = explode ('">', $result);
	            $t2 = explode ('</a>', $t1[1]);
	            $importResults[$i]['TITLE'] = un_escape(clean_string($t2[0]));
	
	            //Get description
	            $d1 = explode ('</a> - ', $result);
	            $d2 = explode ('</ul>', $d1[1]);
	            $importResults[$i]['DESCRIPTION'] = un_escape(clean_string($d2[0]));
	
	            //Build author name [domain name??]
	            $importResults[$i]['OWNER_NAME'] = $domainName;
	
	            //Build author email [webmaster@domain]
	            $importResults[$i]['OWNER_EMAIL'] = 'webmaster@'.$domainName;
	
	            $i++;
	         }
	         $tpl->assign('importResults', $importResults);
	         $tpl->assign('for_dmoz', 1);
	      }
	      break;
	
	   case 'default' :
	   default        :
	
	   if (empty ($_POST['gsubmit'])) {
	   	  //empty link_to_import table
	      $db->Execute("DELETE FROM {$tables['link_to_import']['name']}");
	      
	   	  //Define default crawl values
	      $gdata = array();
	      $gdata['lr']       = 'lang_en';
	      
	      $gdata['num']        = 10;
	      $gdata['as_occt']    = 'any';
	      $gdata['firstlevel'] = 1;
	      $gdata['impdomain']  = 1;
	      $gdata['safesearch'] = 1;
	
	      SmartyValidate :: connect($tpl);
	      SmartyValidate :: register_form('spider-google', true);
	      SmartyValidate :: register_validator('v_as_q', 'as_q', 'notEmpty', false, false, 'trim', 'spider-google');
	   } else {
	      SmartyValidate :: connect($tpl);
	
	      $gdata = array();
	      $gdata['lr']       = (isset ($_POST['lr']) && array_key_exists ($_POST['lr'], $langChoice) ? $_POST['lr'] : 'lang_en');
	      $gdata['as_q']       = (!empty ($_POST['as_q']) ? clean_string($_POST['as_q']) : '');
	      $gdata['as_eq']      = (!empty ($_POST['as_eq']) ? clean_string($_POST['as_eq']) : '');
	      $gdata['num']        = (array_key_exists ($_POST['num'], $googleResultsCount) ? $_POST['num'] : 10);
	      $gdata['as_occt']    = (array_key_exists ($_POST['as_occt'], $occurrences) ? $_POST['as_occt'] : 'any');
	      $gdata['firstlevel'] = (isset ($_POST['firstlevel']) ? 1 : 0);
	      $gdata['impdomain']  = (isset ($_POST['impdomain']) ? 1 : 0);
	      $gdata['safesearch'] = (isset ($_POST['safesearch']) ? 1 : 0);
	
	      //Clean multiple white spaces
	      $gdata['as_q'] = preg_replace ('#[\s]+#i', ' ', $gdata['as_q']);
	      $gdata['as_q'] = clean_string($_REQUEST['as_q']);
	      $gdata['as_eq'] = preg_replace ('#[\s]+#i', ' ', $gdata['as_eq']);
	
	      if (SmartyValidate :: is_valid($gdata, 'spider-google') && !empty ($_POST['gsubmit'])) {
	         $redirectURL = DOC_ROOT.'/spider.php?action=google&num='.$gdata['num'].'&as_q='.$gdata['as_q'].'&lr='.$gdata['lr'];
	//
	//         //Build URL variables [encoded]
	////         foreach ($gdata as $key => $value)
	////            $redirectURL .= '&amp;'.$key.'='.urlencode ($value);
	//	
			 $_SESSION['imported'] = 0;
	         http_custom_redirect($redirectURL);
	      }
	   } 
	   
	   if (empty ($_POST['dsubmit'])) {
	      //Define default crawl values
	      $ddata = array();
	      $ddata['dmozurl'] = '';
	      $ddata['impdomain']  = 1;
	
	      SmartyValidate :: connect($tpl);
	      SmartyValidate :: register_form('spider-dmoz', true);
	      SmartyValidate :: register_validator('v_dmozurl', 'dmozurl', 'notEmpty', false, false, 'trim', 'spider-dmoz');
	   } else {
	      SmartyValidate :: connect($tpl);
	
	      $ddata = array();
	      $ddata['dmozurl']   = (!empty ($_POST['dmozurl']) ? $_POST['dmozurl'] : '');
	      $ddata['impdomain'] = (isset ($_POST['impdomain']) ? 1 : 0);
	
	      if (SmartyValidate :: is_valid($ddata, 'spider-dmoz') && !empty ($_POST['dsubmit'])) {
	         $redirectURL = DOC_ROOT.'/spider.php?action=dmoz';
	
	         //Build URL variables [encoded]
	         foreach ($ddata as $key => $value)
	            $redirectURL .= '&amp;'.$key.'='.urlencode ($value);
	
	         http_custom_redirect($redirectURL);
	      }
	   }
	   $tpl->assign('gdata', $gdata);
	   $tpl->assign('ddata', $ddata);
	   break;
	}
}

$tpl->assign('action', $action);
$tpl->assign('error', $error);
if (!empty ($errorMsg))
   $tpl->assign('errorMsg', $errorMsg);

$tpl->assign('showImport', $showImport);

$content = $tpl->fetch(ADMIN_TEMPLATE.'/spider.tpl');

$tpl->assign('content', $content);

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

//Make output
echo $tpl->fetch(ADMIN_TEMPLATE.'/main.tpl');

if (!function_exists ('un_escape')) {
   /**
    * Un-Escape a string
    * @param  string Escaped string
    * @return string Unescaped string
    */
   function un_escape($string='') {
      $trans_tbl = get_html_translation_table (HTML_ENTITIES);
      $trans_tbl = array_flip ($trans_tbl);
      return strtr ($string, $trans_tbl);
   }
}
?>