<?php

#########################################################################################################
# Project:				PHPLinkDirectory: Link exchange directory @ http://www.phplinkdirectory.com/	#
# Mod:					[Article Importer] @ http://www.monster-submit.com/phpld/						#
# Version:				1.00200906251																	#
# Notice:				Maintain this section															#
# Author:				Virtual Solutions																#
# Idea by and																							#
# contributed to by:	Matt @ http://www.thecanad1an.com												#
# License:				Copyright (c) 2008 - 2009 Virtual Solutions										#
#																										#
# Any redistribution of phpLD_Vault mods without the expressed written consent of Virtual Solutions is	#
# strictly prohibited. Copying any of the code contained within any phpLD_Vault mod and claiming it as	#
# your own is also prohibited. You may not remove any header notices from any mods. By using these		#
# phpLD_Vault mods, you agree to indemnify Virtual Solutions from any liability that might arise from	#
# their use.																							#
#																										#
# PHPLD_VAULT MODS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING	#
# BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND			#
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES	#
# OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN	#
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.							#
#########################################################################################################

##########################################################################
#  Do NOT change or alter the code below!
##########################################################################

$ap = "www.webmasterinfoandcontent.com";
$ex = "http://www.webmasterinfoandcontent.com/rss.php?rss=270 (download multiple articles)<br/>http://www.webmasterinfoandcontent.com/Art/35541/270/Buying-a-Hybrid-The-Case-Against.html(download single article)";

if (! preg_match('/\.html$/', $url, $matches)) {
	if (function_exists('curl_init')) {
		$ch		= curl_init($url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
		$file	= curl_exec($ch);
	}
	else { $file = file_get_contents($url); }
	if (! $file) { error("No data found at URL"); }
	if (preg_match('/rss.php/', $url)) {
		$res			= preg_match_all("/<link>(.*)<\/link>/i", $file, $matches);
		foreach ($matches[1] as $key=>$val) {
			if ($val != "http://$ap/") {
				$urls[]	= $val;
			}
		}
	}
	else {
		$res			= preg_match_all("/<li><a href=\"(.*)\" title=\"(.*)\">(.*)<\/a>(.*)<em>/i", $file, $matches);
		foreach ($matches[1] as $key=>$val) {
			if ($val != "http://$ap/") {
				$urls[]	= $val;
			}
		}
	}
}
else { $urls[0] = $url; }
if (! $urls) { error("No URLs found"); }
if (function_exists('curl_init')) {
	$ch			= curl_init("http://www.monster-submit.com/phpld/data/webmasterinfoandcontent.dat");
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
	curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
	$datfile	= curl_exec($ch);
}
else { $datfile = file_get_contents("http://www.monster-submit.com/phpld/data/webmasterinfoandcontent.dat"); }
if (! $datfile) { error("Could not download http://www.monster-submit.com/phpld/data/webmasterinfoandcontent.dat file"); }
else { $dat = explode("\n", $datfile); }
if (! $dat) { error("Downloaded http://www.monster-submit.com/phpld/data/webmasterinfoandcontent.dat file empty"); }
foreach ($urls as $key=>$val) {
	if ($count == $maxlinks) { break 1; }
	echo "Fetching article :: $val ";
	if (function_exists('curl_init')) {
		$ch		= curl_init($val);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
		$file	= curl_exec($ch);
	}
	else { $file = file_get_contents($val); }
	preg_match('/<head><title>(.*)<\/title>/', $file, $matches);
	$data['TITLE']			= $matches[1];
	$data['TITLE']			= preg_replace('/Webmasterinfoandcontent.com \| /', '', $data['TITLE']);
	echo $data['TITLE'] . ". . .";
	$start					= $dat[0];
	$finish					= $dat[2];
	$start_loc				= strpos($file, $start) + $dat[1];
	$finish_loc				= strpos($file, $finish) - $dat[3];
	$length					= $finish_loc - $start_loc;
	$data['ARTICLE']		= substr($file, $start_loc, $length);
	$data['ARTICLE']		= preg_replace('/<!DOCTYPE(.*)dtd\'>;/', '', $data['ARTICLE']);
	$data['ARTICLE']		= preg_replace('/<html>/', '', $data['ARTICLE']);
	$data['ARTICLE']		= preg_replace('/<head><title>(.*)<\/title>/', '', $data['ARTICLE']);
	$data['ARTICLE']		= preg_replace('/<body>/', '', $data['ARTICLE']);
	$data['ARTICLE']		= preg_replace('/<\/head>/', '', $data['ARTICLE']);
	$data['ARTICLE']		= preg_replace('/<\/body>/', '', $data['ARTICLE']);
	$data['ARTICLE']		= nl2br(preg_replace('/<\/html>/', '', $data['ARTICLE']));
	if ($format == "text") { $data['ARTICLE'] = strip_tags($data['ARTICLE']); }
	if ($link_attribute == 1) { $data['ARTICLE'] = preg_replace('/<a/', '<a rel="nofollow"', $data['ARTICLE']); }
	$start					= $dat[4];
	$finish					= $dat[6];
	$start_loc				= strpos($file, $start) + $dat[5];
	$finish_loc				= strpos($file, $finish) - $dat[7];
	$length					= $finish_loc - $start_loc;
	$data['DESCRIPTION']	= substr($file, $start_loc, $length) . " ";
	$maxlength				= (defined(ARTICLE_DESCRIPTION_MAX_LENGTH) ? ARTICLE_DESCRIPTION_MAX_LENGTH : 250);
	$data['DESCRIPTION']	= preg_replace('/Article from Webmasterinfoandcontent.com and entitled /', '', $data['TITLE']);
	$data['DESCRIPTION']	= substr($data['DESCRIPTION'], 0, $maxlength);
	$data['DESCRIPTION']	= substr($data['DESCRIPTION'], 0, strrpos($data['DESCRIPTION'],' '));
	$data['DESCRIPTION']	= $data['DESCRIPTION'] . "...";
	$data['ID']	= $db->GenID($tables['article']['name'] . '_SEQ');
	$data['STATUS']	= $status;
	$data['CATEGORY_ID']	= $category_id;
	$data['DATE_ADDED']		= gmdate("Y\-m\-d H\:i\:s");
	$data['DATE_MODIFIED']	= gmdate('Y-m-d H:i:s');
	$row					= $db->GetRow("SELECT `OWNER_ID` FROM `{$tables['article']['name']}` LIMIT 0");
	if (is_array($row)) {
		$data['OWNER_ID']		= $owner_id;
	}
	$row					= $db->GetRow("SELECT `ARTICLE_TYPE` FROM `{$tables['article']['name']}` LIMIT 0");
	if (is_array($row)) {
		$data['ARTICLE_TYPE']	= $article_type;
	}
	$data['IPADDRESS']      = $client_info['IP'];
	$row					= $db->GetRow("SELECT `FEATURED` FROM `{$tables['article']['name']}` LIMIT 0");
	if (is_array($row)) {
		if (FTR_ENABLE == 1) { $data['FEATURED'] = ($_POST['ARTICLE_TYPE'] == '4' ? '1' : '0'); }
	}
	$row					= $db->GetRow("SELECT `OWNER_ID` FROM `{$tables['article']['name']}` LIMIT 0");
	if (is_array($row)) {
		if ($data['OWNER_ID'] > 0) {
			$user_details = $db->GetRow("SELECT `LOGIN`, `NAME`, `EMAIL` FROM `{$tables['user']['name']}` WHERE `ID` = " . $db->qstr($data['OWNER_ID']));
			if (! empty($user_details)) {
				$data['OWNER_NAME']		= $user_details['NAME'];
				$data['OWNER_EMAIL']	= $user_details['EMAIL'];
			}
			unset($user_details);
		}
	}
	$aid					= $db->GetOne("SELECT `ID` FROM `{$tables['article']['name']}` WHERE UCASE(`TITLE`) = UCASE('" . $data['TITLE'] . "')");
	if(! $aid) {
		if ($db->Replace($tables['article']['name'], $data, 'ID', true) > 0) { 	$count++; echo " Imported<br/>\n"; }
		else { echo " Not imported :: " . $db->ErrorMsg() . "<br/>\n"; }
	}
	else { echo " Not imported :: Article already exists in database<br/>\n"; }
}

?>
