PageRenderTime 341ms CodeModel.GetById 56ms app.highlight 183ms RepoModel.GetById 60ms app.codeStats 1ms

/pi.xmlgrab.php

https://bitbucket.org/ajweaver/xmlgrab
PHP | 1195 lines | 834 code | 227 blank | 134 comment | 211 complexity | 2364c4ca22dc74d46adee8705e859c5c MD5 | raw file
   1<?php
   2
   3/* VERSIONS 
   4
   5NOYTEs changeg is_unique fn return value, added else to unique test, did update code, changed catgeory ceation sql, fixed mb_oncdoing bug
   6
   7
   8	0.8			Add support to update entries
   9	0.7.4		Fixed blank date parsing bug
  10	0.7.3 		MSM support
  11	0.7.2		Now supports xml attributes, repeated elements, uk date stamps, merge multiple categories
  12
  13	Based on codebase of FeedGrab 0.7.1
  14
  15	*/
  16
  17$plugin_info = array(
  18	'pi_name'           => 'XMLGrab',
  19	'pi_version'        => '0.8',
  20	'pi_author'         => 'Andrew Weaver',
  21	'pi_author_url'     => 'http://www.brandnewbox.co.uk/docs/',
  22	'pi_description'    => 'Allows you to extract data from an XML feed and insert it into a weblog',
  23	'pi_usage'          => XMLGrab::usage()
  24);
  25
  26
  27class XMLGrab {
  28
  29	var $debug = true;
  30	var $runsql = true;
  31	var $uk_dates = true;  // convert MM/DD/YYYY to DD/MM/YYYY
  32
  33	var $page_url = '';
  34	var $title = 'title';
  35	var $publish_date = 'dc:date';
  36	var $author_id = 1;
  37	var $feed_weblog_id = '';
  38	var $feed_fields = '';
  39	var $weblog_fields = '';
  40	var $cats = '';
  41	var $cat_group = 0;
  42	var $cat_field = '';
  43	var $cat_delimiter = ',';
  44	var $cat_group_2 = 0;
  45	var $cat_field_2 = '';
  46	var $cat_delimiter_2 = ',';
  47	var $unique = 'title,date';
  48	var $itempath = '';
  49	var $site_id = '';
  50	var $do_update = false;
  51	var $status = '';
  52
  53	var $return_data = '';
  54
  55	var $time_offset = 0;
  56
  57	function XMLGrab() {
  58		global $TMPL, $LOC, $FNS, $PREFS, $SESS, $REGX, $DB;
  59
  60		// Check parameters
  61
  62		$this->page_url = ( ! $TMPL->fetch_param('url') ) ? '' : str_replace('&#47;', '/',trim($TMPL->fetch_param('url')));
  63		$this->feed_weblog_id = ( ! $TMPL->fetch_param('weblog') ) ? '' : $TMPL->fetch_param('weblog');
  64		$this->author_id = ( ! $TMPL->fetch_param('author') ) ? 1 : $TMPL->fetch_param('author');
  65		$this->title = ( ! $TMPL->fetch_param('title') ) ? '' : $TMPL->fetch_param('title');
  66		$this->publish_date = ( ! $TMPL->fetch_param('date') ) ? '' : $TMPL->fetch_param('date');
  67		$this->feed_fields = ( ! $TMPL->fetch_param('use') ) ? '' : $TMPL->fetch_param('use');
  68		$this->weblog_fields = ( ! $TMPL->fetch_param('fields') ) ? '' : $TMPL->fetch_param('fields');
  69		$this->time_offset = ( ! $TMPL->fetch_param('offset') ) ? 0 : $TMPL->fetch_param('offset');
  70		$this->cats = ( ! $TMPL->fetch_param('category') ) ? '' : $TMPL->fetch_param('category');
  71		$this->cat_group = ( ! $TMPL->fetch_param('category_group') ) ? 0 : $TMPL->fetch_param('category_group');
  72		$this->cat_field = ( ! $TMPL->fetch_param('category_field') ) ? '' : $TMPL->fetch_param('category_field');
  73		$this->cat_delimiter = ( ! $TMPL->fetch_param('category_delimiter') ) ? ',' : $TMPL->fetch_param('category_delimiter');
  74		$this->cat_group_2 = ( ! $TMPL->fetch_param('category_group_2') ) ? 0 : $TMPL->fetch_param('category_group_2');
  75		$this->cat_field_2 = ( ! $TMPL->fetch_param('category_field_2') ) ? '' : $TMPL->fetch_param('category_field_2');
  76		$this->cat_delimiter_2 = ( ! $TMPL->fetch_param('category_delimiter_2') ) ? ',' : $TMPL->fetch_param('category_delimiter_2');
  77		$this->unique = ( ! $TMPL->fetch_param('unique') ) ? 'title,date' : $TMPL->fetch_param('unique');
  78		$this->itempath = ( ! $TMPL->fetch_param('itempath') ) ? '/xml/item' : $TMPL->fetch_param('itempath');
  79		$this->site_id = ( ! $TMPL->fetch_param('site_id') ) ? '' : $TMPL->fetch_param('site_id');
  80		$this->do_update = ( $TMPL->fetch_param('update') ) ? true : false;
  81		$this->status = ( ! $TMPL->fetch_param('status') ) ? '' : $TMPL->fetch_param('status');
  82
  83		$this->debug = ( ! $TMPL->fetch_param('debug') ) ? true : false;
  84
  85		if ($this->page_url == '') {
  86			return $this->return_data;
  87		}
  88
  89		if ($this->feed_weblog_id == '') {
  90			return $this->return_data;
  91		}
  92
  93		if ($this->cat_delimiter == 'TAB') {
  94			$this->cat_delimiter = "\t";
  95		}    
  96		if ($this->cat_delimiter == 'SPACE') {
  97			$this->cat_delimiter = " ";
  98		}
  99		if ($this->cat_delimiter_2 == 'TAB') {
 100			$this->cat_delimiter_2 = "\t";
 101		}    
 102		if ($this->cat_delimiter_2 == 'SPACE') {
 103			$this->cat_delimiter_2 = " ";
 104		}
 105
 106		if ($this->feed_fields == '') {
 107			return $this->return_data;
 108		} else {
 109			$this->feed_fields = str_replace(SLASH, '/', $this->feed_fields);
 110			$fieldsArray = explode( "|", $this->feed_fields );
 111		}
 112		if ($this->weblog_fields == '') {
 113			return $this->return_data;
 114		} else {
 115			$this->weblog_fields = str_replace(SLASH, '/', $this->weblog_fields);
 116			$weblogArray = explode( "|", $this->weblog_fields );
 117		}
 118		// Check number of input and weblog fields match
 119		if ( count($fieldsArray) != count($weblogArray) ) {
 120			return $this->return_data;
 121		}
 122
 123		$weblog_to_feed[ 'title' ] = Array();
 124		$weblog_to_feed[ 'title' ][ 'is_custom' ] = 0;
 125		$weblog_to_feed[ 'title' ][ 'field' ] = $this->title;
 126		$weblog_to_feed[ 'date' ] = Array();
 127		$weblog_to_feed[ 'date' ][ 'is_custom' ] = 0;
 128		$weblog_to_feed[ 'date' ][ 'field' ] = $this->publish_date;
 129
 130		// Use fields array to map feed fields to weblog fields
 131		for( $i=0; $i<count( $weblogArray ); $i++ ) {
 132			$weblog_to_feed[ $weblogArray[ $i ] ] = Array();
 133			$weblog_to_feed[ $weblogArray[ $i ] ][ 'is_custom' ] = 1;
 134			$weblog_to_feed[ $weblogArray[ $i ] ][ 'field' ] = $fieldsArray[ $i ];
 135		} 
 136
 137		// Get custom fields from database
 138		$customfieldsArray = $DB->query("SELECT exp_weblog_fields.field_id, 
 139								exp_weblog_fields.field_name, 
 140								exp_weblog_fields.field_label, exp_weblog_fields.field_fmt,
 141								exp_weblog_fields.field_type, exp_weblog_fields.field_related_id
 142							 	FROM exp_weblogs, exp_weblog_fields
 143							 	WHERE exp_weblogs.field_group = exp_weblog_fields.group_id
 144								AND exp_weblogs.weblog_id = '".$this->feed_weblog_id."'");
 145		$field_ids = '';
 146		
 147		// Map weblog custom fields to id's
 148		foreach ( $customfieldsArray->result as $row ) {
 149			$weblog_to_feed[ $row[ "field_name" ] ][ 'id' ] = $row[ "field_id" ];
 150			$weblog_to_feed[ $row[ "field_name" ] ][ 'format' ] = $row[ "field_fmt" ];
 151			$weblog_to_feed[ $row[ "field_name" ] ][ 'type' ] = $row[ "field_type" ];
 152			if ( $row[ "field_type" ] == "rel" ) {
 153				$weblog_to_feed[ $row[ "field_name" ] ][ 'related_id' ] = $row[ "field_related_id" ];
 154			}
 155			if ( substr($row[ "field_type" ], 0, 8) == "ftype_id" ) {
 156
 157				// Is the field a FF type?
 158				$ftype_id = substr( $row[ "field_type" ], 9 );
 159
 160				// Find out what type
 161				$sql = "SELECT class FROM exp_ff_fieldtypes WHERE fieldtype_id = " . $ftype_id;
 162				$results = $DB->query($sql);
 163				$weblog_to_feed[ $row[ "field_name" ] ][ 'type' ] = $results->row["class"];
 164				
 165				// If it is matrix, look for a matching parameter and store it
 166				if( $results->row["class"] == "matrix" ) {
 167					$weblog_to_feed[ $row[ "field_name" ] ][ 'matrix' ] = $TMPL->fetch_param( $row[ "field_name" ] );
 168				}
 169
 170				// Store any FF settings for this type
 171				$sql = "SELECT ff_settings FROM exp_weblog_fields WHERE field_id = " . $row[ "field_id" ];
 172				$field_settings = $DB->query($sql);
 173				$weblog_to_feed[ $row[ "field_name" ] ][ 'field_settings' ] = unserialize($field_settings->row["ff_settings"] );
 174
 175			}
 176		}
 177
 178		// Retrieve xml
 179
 180		if ( $this->debug ) { print "Fetching: " . $this->page_url . "<br/>\n"; }
 181
 182		if ( function_exists('curl_init'))
 183		{
 184			$xml = $this->_curl_fetch( $this->page_url ); 
 185		}
 186		else
 187		{
 188			$xml = $this->_fsockopen_fetch( $this->page_url );
 189		}
 190
 191
 192		// Parse XML using EE's builtin functions
 193
 194		include_once( PATH_CORE . "core.xmlparser.php" );
 195		$XML = new EE_XMLparser;
 196		$xml_obj = $XML->parse_xml( $xml );
 197
 198		// EE replaces slashes with entities earlier in process, convert back here
 199		$this->itempath = str_replace(SLASH, '/', $this->itempath);
 200
 201		$items = null; // initialise array to store output
 202
 203		// Recurse through XML structure looking for nodes that match $this->itempath
 204
 205		$this->fetch_xml( $xml_obj, $this->itempath, $items );
 206
 207		$entries_added = 0;
 208
 209		// Loop over all feed items
 210		foreach ( $items as $item ) {
 211
 212			$this->post = null;
 213
 214			// Get title using title parameter, generate url
 215			$this->post[ "title" ] = $this->get_namespaced_field( 
 216				$item, 
 217				$weblog_to_feed[ 'title' ][ 'field' ] 
 218				);
 219			// Check for XSS
 220			$this->post['title'] = $REGX->xss_clean( $this->post['title'] );
 221
 222			// Get unique url
 223			$source_encoding = ( isset( $this->RSS->source_encoding ) ? $this->RSS->source_encoding : 'UTF-8' );
 224
 225			// Clean title
 226			if (function_exists('mb_convert_encoding'))
 227			{
 228				$encoded_title = mb_convert_encoding($this->post[ "title" ], strtoupper('UTF-8'), $source_encoding );
 229			}
 230			elseif(function_exists('iconv') AND ($iconvstr = @iconv($source_encoding, 'UTF-8', $this->post["title"])) !== FALSE)
 231			{
 232				$encoded_title = $iconvstr;
 233			}
 234			else
 235			{
 236				$encoded_title = utf8_encode($this->post["title"]);
 237			}
 238			$this->post[ "url_title" ] = $REGX->create_url_title( $encoded_title );
 239			$sql = "SELECT count(*) AS count FROM exp_weblog_titles WHERE url_title = '".$DB->escape_str( $this->post[ "url_title" ] )."' AND weblog_id = '$this->feed_weblog_id'";
 240			$results = $DB->query($sql);
 241			if ($results->row['count'] > 0) {
 242				// Duplicate exists, add a counter on the end
 243				$sql = "SELECT count(*) AS count FROM exp_weblog_titles WHERE url_title LIKE '".$DB->escape_str( $this->post[ "url_title" ] )."%' AND weblog_id = '$this->feed_weblog_id'";
 244				$results = $DB->query($sql);
 245				$this->post[ "url_title" ] .= $results->row['count']+1;
 246			}
 247
 248			// Get date field using date parameter
 249			$this->post[ "date" ] = time();
 250			if( isset( $item[ $weblog_to_feed[ 'date' ][ 'field' ] ] ) ) {
 251				$this->post[ "date" ] = $this->parse_date( 
 252					$item[ $weblog_to_feed[ 'date' ][ 'field' ] ]
 253				);
 254			}
 255
 256			// Load this->post array with data
 257			foreach ( $weblog_to_feed as $field => $data ) {
 258
 259				if ( isset( $data[ "is_custom" ] ) && $data[ "is_custom" ] ) {
 260					if( !isset($data["type"]) || $data["type"] != "matrix" ) {
 261						$this->post[ $field ] = $this->get_namespaced_field( $item, $data[ "field" ] );
 262					} else {
 263						// Handle Pixel & Tonic Matrix field
 264						$no_subrows = $this->get_namespaced_field( $item, $data[ "field" ] . '#' );
 265						// loop over fields adding to matrix array
 266						$matrix_data = array();
 267						$matrix_fields = explode( "|", $data["matrix"] );
 268						for ( $i=0; $i<$no_subrows; $i++ ) {
 269							$matrix_data[ $i ] = array();
 270							foreach( $matrix_fields as $j => $matrix_field ) {
 271								$matrix_data[ $i ][ 'col_id_'. $data["field_settings"]["col_ids"][ $j ]  ] = $this->get_namespaced_field( 
 272									$item, 
 273									$data[ "field" ] . '/' . $matrix_field . ( $i > 0 ? '#'. ($i+1) : '' )
 274									);
 275							}
 276						}
 277						// Store array of data to insert into exp_matrix_data
 278						$this->post[ $field ] = $matrix_data;
 279					}
 280				}
 281			}
 282
 283			if ( $this->debug ) { print "  Checking: " . $this->post['title'] . "<br />\n"; }
 284
 285			// Look for duplicate entries
 286			$entry_id = $this->is_entry_unique( $this->post, $this->unique, $weblog_to_feed );
 287			if (  $entry_id == 0 ) {
 288
 289				if ( $this->debug ) { 
 290					print "  Found new entry: " . $this->post[ "title" ] . "<br/>\n"; 
 291				}
 292
 293				// Get weblog details for default values
 294				$query = $DB->query("SELECT blog_title, blog_url, rss_url, 
 295					ping_return_url, deft_comments, deft_trackbacks, 
 296					deft_status, cat_group, field_group 
 297					FROM exp_weblogs 
 298					WHERE weblog_id = '$this->feed_weblog_id'"); 
 299
 300				if ($query->num_rows == 0) {
 301					return false;
 302				}
 303
 304				// Check status
 305				$status = $query->row['deft_status'];
 306				if( $this->status != '' ) {
 307					$status = $this->get_namespaced_field( $item, $this->status );
 308				}
 309
 310				// Insert into weblog_titles
 311				$data = array(  
 312					'entry_id'          => '',
 313					'weblog_id'         => $this->feed_weblog_id,
 314					'author_id'         => $this->author_id,
 315					'title'             => $this->post['title'],
 316					'url_title'         => $this->post['url_title'],
 317					'ip_address'        => '127.0.0.1',
 318					'entry_date'        => ($this->post['date'] - $this->time_offset),
 319					'year'              => gmdate('Y', $this->post['date'] - $this->time_offset),
 320					'month'             => gmdate('m', $this->post['date'] - $this->time_offset),
 321					'day'               => gmdate('d', $this->post['date'] - $this->time_offset),
 322					'sticky'            => 'n',
 323					'status'            => $status,
 324					'allow_comments'    => $query->row['deft_comments'],
 325					'allow_trackbacks'  => $query->row['deft_trackbacks']
 326					);
 327				if ( $this->site_id != '' ) {
 328					$data[ 'site_id' ] = $this->site_id;
 329				}
 330
 331				$sql = $DB->insert_string('exp_weblog_titles', $data);
 332				if ( $this->runsql ) {
 333					$DB->query($sql);
 334					$entry_id = $DB->insert_id;
 335				} else {
 336					$entry_id = 99999;
 337					if ( $this->debug ) { print $sql . "<br/>\n"; }
 338				}
 339				
 340				// Insert into weblog_data
 341				unset( $this->post['title'] );
 342				unset( $this->post['url_title'] );
 343				unset( $this->post['date'] );
 344
 345				$data = array();
 346				$data["entry_id"] = $entry_id;
 347				$data["weblog_id"] = $this->feed_weblog_id;
 348				$data["entry_id"] = $entry_id;
 349				if ( $this->site_id != '' ) {
 350					$data[ 'site_id' ] = $this->site_id;
 351				}
 352
 353				// Load custom field data into data array
 354				foreach ( $this->post as $custom_field => $custom_data ) {
 355					
 356					if( !isset($weblog_to_feed[ $custom_field ][ "type" ]) || $weblog_to_feed[ $custom_field ][ "type" ] == "date" ) {
 357					
 358						$date = $REGX->xss_clean( trim( $custom_data ) );
 359						$pat = "/(\d{4})-(\d{1,2})-(\d{1,2})/";
 360
 361						// Date field
 362						if ( preg_match( $pat, $date, $match ) ) {
 363
 364							list( $year, $month, $day) = array( $match[1], $match[2], $match[3]);
 365							$epoch = gmmktime( 0, 0, 0, $month, $day, $year);
 366							$epoch += $LOC->set_localized_offset();
 367						}
 368
 369						$data['field_id_' . $weblog_to_feed[ $custom_field ][ "id" ] ] = $epoch;
 370					
 371					} elseif ( $weblog_to_feed[ $custom_field ][ "type" ] == "matrix" ) {
 372						
 373						// Insert Matrix data
 374						$row = 0;
 375						foreach( $custom_data as $matrix_row ) {
 376							// Loop through data rows and insert record into exp_matrix_data
 377							if ( $this->site_id != '' ) {
 378								$matrix_row["site_id"] = $this->site_id;
 379							}
 380							$matrix_row["entry_id"] = $entry_id;
 381							$matrix_row["field_id"] = $weblog_to_feed[ $custom_field ][ "id" ];
 382							$matrix_row["row_order"] = $row++;
 383							$sql = $DB->insert_string('exp_matrix_data', $matrix_row);
 384							$DB->query($sql);
 385						}
 386						// Update $this->post to set value to 1 to indcate matrix data exists
 387						$data['field_id_' . $weblog_to_feed[ $custom_field ][ "id" ] ] = 1;
 388					
 389					} else {
 390						
 391						$data['field_id_' . $weblog_to_feed[ $custom_field ][ "id" ] ] = $REGX->xss_clean( trim( $custom_data ) );
 392						
 393					}
 394					
 395					$data['field_ft_' . $weblog_to_feed[ $custom_field ][ "id" ] ] = $weblog_to_feed[ $custom_field ][ "format" ];
 396					
 397				}
 398
 399				$sql = $DB->insert_string('exp_weblog_data', $data);
 400				if ( $this->runsql ) {
 401					$DB->query($sql);
 402				} else {
 403					if ( $this->debug ) { print $sql . "<br/>\n"; }
 404				}
 405
 406				// Insert category details
 407
 408				// Default category for all entries
 409
 410				if (isset($this->cats) && $this->cats != '') 
 411				{         	
 412					$cats = explode(',',$this->cats);
 413					$cats = array_unique($cats);
 414
 415					$results = $DB->query("SELECT cat_id, parent_id FROM exp_categories 
 416						WHERE (cat_id IN ('".implode("','",$cats)."') OR cat_name IN ('".implode("','",$cats)."'))
 417						AND group_id = '".$query->row['cat_group']."'");
 418
 419					if ($results->num_rows > 0)
 420					{
 421						foreach($results->result as $row)
 422						{
 423							if ($PREFS->ini('auto_assign_cat_parents') == 'y' && $row['parent_id'] != '0')
 424							{
 425								$DB->query("INSERT INTO exp_category_posts (entry_id, cat_id) VALUES ('$entry_id', '".$row['parent_id']."')");
 426							}
 427							$DB->query("INSERT INTO exp_category_posts (entry_id, cat_id) VALUES ('$entry_id', '".$row['cat_id']."')");
 428						}
 429					}
 430				}
 431
 432				// Specific category by feed field
 433				$cat_ids = array();
 434
 435				// Category group 1
 436				if ( $this->cat_field != '' ) {
 437
 438					$this->cat_field = str_replace(SLASH, '/', $this->cat_field);
 439
 440					// Find category item
 441					$nameList = $this->get_namespaced_field( $item, $this->cat_field );
 442
 443					// Does entry have multiple category items
 444					$num_cats = $this->get_namespaced_field( $item, $this->cat_field . "#" );
 445					if ( $num_cats > 1 ) {
 446						for( $i=2; $i<=$num_cats; $i++ ) {
 447							$nameList .= $this->cat_delimiter . " " . $this->get_namespaced_field( $item, $this->cat_field . "#" . $i );
 448						}
 449					}
 450
 451					$names = explode( $this->cat_delimiter, $nameList );
 452					foreach ( $names as $name ) {
 453						$name = trim( $name );
 454						$query =$DB->query("SELECT cat_id
 455							FROM exp_categories
 456							WHERE exp_categories.cat_name = '".$DB->escape_str($name)."'
 457							AND exp_categories.parent_id = '0'
 458							AND exp_categories.group_id = '".$DB->escape_str($this->cat_group)."'");
 459
 460						if ($query->num_rows == 0) {	
 461							// Create primary category
 462							$insert_array = array(
 463								'group_id'  	=> $this->cat_group,
 464								'site_id' => ( $this->site_id != '' ? $this->site_id : 1 ),
 465								'cat_name' 	=> $name,
 466								'cat_url_title' => $REGX->create_url_title( $name ),
 467								'cat_image' 	=> '',
 468								'parent_id'   => '0'
 469								);
 470						
 471							print "<p>Try to add category " . $name . "</p>";
 472
 473							if ($this->runsql) { 
 474								$DB->query($DB->insert_string('exp_categories', $insert_array));       
 475								$cat_ids[] = $DB->insert_id;
 476							} else {
 477								print($DB->insert_string('exp_categories', $insert_array));
 478								$cat_ids[] = 0;
 479							}
 480							
 481							 $insert_array = array(
 482								'cat_id'  	=> $DB->insert_id,
 483								'site_id' 	=> ( $this->site_id != '' ? $this->site_id : 1 ),
 484								'group_id' 	=> $this->cat_group
 485							);
 486							$DB->query($DB->insert_string('exp_category_field_data', $insert_array));
 487							
 488						} else {
 489							print "<p>Category " . $name . " exists</p>";
 490							$cat_ids[] = $query->row['cat_id'];
 491						}
 492					} 
 493				} 
 494
 495				// Insert  category details
 496
 497				if ( isset($cat_ids) && count($cat_ids) > 0 ) 
 498				{         	
 499					$cats = array_unique($cat_ids);
 500
 501					$results = $DB->query("SELECT cat_id, parent_id FROM exp_categories 
 502					WHERE (cat_id IN ('".implode("','",$cats)."') OR cat_name IN ('".implode("','",$cats)."'))
 503					AND group_id = '".$this->cat_group."'");
 504
 505					if ($results->num_rows > 0)
 506					{
 507						foreach($results->result as $row)
 508						{
 509							if ($PREFS->ini('auto_assign_cat_parents') == 'y' && $row['parent_id'] != '0')
 510							{
 511								if ($this->runsql) { 
 512									$DB->query("INSERT INTO exp_category_posts (entry_id, cat_id) VALUES ('$entry_id', '".$row['parent_id']."')");
 513								}
 514							}
 515							if ($this->runsql) {
 516								$DB->query("INSERT INTO exp_category_posts (entry_id, cat_id) VALUES ('$entry_id', '".$row['cat_id']."')");
 517							}
 518						}
 519					}
 520				}
 521				
 522				// Category group 2
 523
 524				$cat_ids = array();
 525
 526				if ( $this->cat_field_2 != '' ) {
 527
 528					$this->cat_field_2 = str_replace(SLASH, '/', $this->cat_field_2);
 529
 530					// Find category item
 531					$nameList = $this->get_namespaced_field( $item, $this->cat_field_2 );
 532
 533					// Does entry have multiple category items
 534					$num_cats = $this->get_namespaced_field( $item, $this->cat_field_2 . "#" );
 535					if ( $num_cats > 1 ) {
 536						for( $i=2; $i<=$num_cats; $i++ ) {
 537							$nameList .= $this->cat_delimiter_2 . " " . $this->get_namespaced_field( $item, $this->cat_field_2 . "#" . $i );
 538						}
 539					}
 540
 541					$names = explode( $this->cat_delimiter_2, $nameList );
 542					foreach ( $names as $name ) {
 543						$name = trim( $name );
 544						$query =$DB->query("SELECT cat_id
 545							FROM exp_categories
 546							WHERE exp_categories.cat_name = '".$DB->escape_str($name)."'
 547							AND exp_categories.parent_id = '0'
 548							AND exp_categories.group_id = '".$DB->escape_str($this->cat_group_2)."'");
 549
 550						if ($query->num_rows == 0) {	
 551							// Create primary category
 552							$insert_array = array(
 553								'group_id'  	=> $this->cat_group_2,
 554								'site_id' => ( $this->site_id != '' ? $this->site_id : 1 ),
 555								'cat_name' 	=> $name,
 556								'cat_url_title' => $REGX->create_url_title( $name ),
 557								'cat_image' 	=> '',
 558								'parent_id'   => '0'
 559								);
 560						
 561							print "<p>Try to add category " . $name . "</p>";
 562
 563							if ($this->runsql) { 
 564								$DB->query($DB->insert_string('exp_categories', $insert_array));       
 565								$cat_ids[] = $DB->insert_id;
 566							} else {
 567								print($DB->insert_string('exp_categories', $insert_array));
 568								$cat_ids[] = 0;
 569							}
 570							
 571							 $insert_array = array(
 572								'cat_id'  	=> $DB->insert_id,
 573								'site_id' 	=> ( $this->site_id != '' ? $this->site_id : 1 ),
 574								'group_id' 	=> $this->cat_group_2
 575							);
 576							$DB->query($DB->insert_string('exp_category_field_data', $insert_array));
 577							
 578						} else {
 579							print "<p>Category " . $name . " exists</p>";
 580							$cat_ids[] = $query->row['cat_id'];
 581						}
 582					} 
 583				}
 584
 585				// Insert  category details
 586				if ( isset($cat_ids) && count($cat_ids) > 0 ) 
 587				{         	
 588					$cats = array_unique($cat_ids);
 589
 590					$results = $DB->query("SELECT cat_id, parent_id FROM exp_categories 
 591					WHERE (cat_id IN ('".implode("','",$cats)."') OR cat_name IN ('".implode("','",$cats)."'))
 592					AND group_id = '".$this->cat_group_2."'");
 593
 594					if ($results->num_rows > 0)
 595					{
 596						foreach($results->result as $row)
 597						{
 598							if ($PREFS->ini('auto_assign_cat_parents') == 'y' && $row['parent_id'] != '0')
 599							{
 600								if ($this->runsql) { 
 601									$DB->query("INSERT INTO exp_category_posts (entry_id, cat_id) VALUES ('$entry_id', '".$row['parent_id']."')");
 602								}
 603							}
 604							if ($this->runsql) {
 605								$DB->query("INSERT INTO exp_category_posts (entry_id, cat_id) VALUES ('$entry_id', '".$row['cat_id']."')");
 606							}
 607						}
 608					}
 609				}
 610		
 611			$entries_added++;
 612
 613		} else {
 614
 615			// DUPLICATE ENTRY
 616			
 617			if ( $this->debug ) { 
 618				print "  Found existing entry: " . $this->post[ "title" ] . "<br/>\n"; 
 619			}
 620
 621			if ( $this->do_update ) {
 622
 623			// Update weblog_titles
 624			
 625			$data = array(  
 626				'title'             => $this->post['title'],
 627				'entry_date'        => ($this->post['date'] - $this->time_offset),
 628				'year'              => gmdate('Y', $this->post['date'] - $this->time_offset),
 629				'month'             => gmdate('m', $this->post['date'] - $this->time_offset),
 630				'day'               => gmdate('d', $this->post['date'] - $this->time_offset),
 631				);
 632
 633			$sql = $DB->update_string('exp_weblog_titles', $data, "entry_id = '".$entry_id."'");
 634			$DB->query($sql);
 635			
 636			// Update custom fields
 637			
 638			$data = array();
 639			
 640			unset( $this->post['title'] );
 641			unset( $this->post['url_title'] );
 642			unset( $this->post['date'] );
 643			
 644			// Load custom field data into data array
 645			foreach ( $this->post as $custom_field => $custom_data ) {
 646				if( $weblog_to_feed[ $custom_field ][ "type" ] == "date" ) {
 647				
 648					$date = $REGX->xss_clean( trim( $custom_data ) );
 649					$pat = "/(\d{4})-(\d{1,2})-(\d{1,2})/";
 650
 651					// Date field
 652					if ( preg_match( $pat, $date, $match ) ) {
 653
 654						list( $year, $month, $day) = array( $match[1], $match[2], $match[3]);
 655						$epoch = gmmktime( 0, 0, 0, $month, $day, $year);
 656						$epoch += $LOC->set_localized_offset();
 657					}
 658
 659					$data['field_id_' . $weblog_to_feed[ $custom_field ][ "id" ] ] = $epoch;
 660				
 661					} elseif ( $weblog_to_feed[ $custom_field ][ "type" ] == "matrix" ) {
 662						
 663						$sql = "DELETE FROM exp_matrix_data WHERE entry_id = " . $entry_id . " AND field_id = " . $weblog_to_feed[ $custom_field ][ "id" ];
 664						$DB->query($sql);
 665						
 666						// Insert Matrix data
 667						$row = 0;
 668						foreach( $custom_data as $matrix_row ) {
 669							// Loop through data rows and insert record into exp_matrix_data
 670							if ( $this->site_id != '' ) {
 671								$matrix_row["site_id"] = $this->site_id;
 672							}
 673							$matrix_row["entry_id"] = $entry_id;
 674							$matrix_row["field_id"] = $weblog_to_feed[ $custom_field ][ "id" ];
 675							$matrix_row["row_order"] = $row++;
 676							$sql = $DB->insert_string('exp_matrix_data', $matrix_row);
 677							$DB->query($sql);
 678						}
 679						// Update $this->post to set value to 1 to indcate matrix data exists
 680						$data['field_id_' . $weblog_to_feed[ $custom_field ][ "id" ] ] = 1;
 681
 682				} else {
 683					
 684					$data['field_id_' . $weblog_to_feed[ $custom_field ][ "id" ] ] = $REGX->xss_clean( trim( $custom_data ) );
 685
 686				}
 687				$data['field_ft_' . $weblog_to_feed[ $custom_field ][ "id" ] ] = $weblog_to_feed[ $custom_field ][ "format" ];
 688			}
 689
 690			$sql = $DB->update_string('exp_weblog_data', $data, "entry_id = '".$entry_id."'");
 691			$DB->query($sql);
 692			
 693			// Update categories
 694			
 695			if (isset($this->cats) && $this->cats != '') 
 696			{         	
 697				$cats = explode(',',$this->cats);
 698				$cats = array_unique($cats);
 699
 700				$results = $DB->query("SELECT cat_id, parent_id FROM exp_categories 
 701					WHERE (cat_id IN ('".implode("','",$cats)."') OR cat_name IN ('".implode("','",$cats)."'))
 702					AND group_id = '".$query->row['cat_group']."'");
 703
 704				if ($results->num_rows > 0)
 705				{
 706					foreach($results->result as $row)
 707					{
 708						if ($PREFS->ini('auto_assign_cat_parents') == 'y' && $row['parent_id'] != '0')
 709						{
 710							$DB->query("INSERT INTO exp_category_posts (entry_id, cat_id) VALUES ('$entry_id', '".$row['parent_id']."')");
 711						}
 712						$DB->query("INSERT INTO exp_category_posts (entry_id, cat_id) VALUES ('$entry_id', '".$row['cat_id']."')");
 713					}
 714				}
 715			}
 716
 717			// Category group 1
 718			
 719			$query = $DB->query("DELETE  p FROM exp_category_posts p LEFT JOIN exp_categories c USING (cat_id) WHERE entry_id = " . $DB->escape_str($entry_id) . " AND c.group_id = " . $DB->escape_str($this->cat_group) );
 720		
 721			if ( $this->cat_field != '' ) {
 722
 723				$this->cat_field = str_replace(SLASH, '/', $this->cat_field);
 724
 725				// Find category item
 726				$nameList = $this->get_namespaced_field( $item, $this->cat_field );
 727
 728				// Does entry have multiple category items
 729				$num_cats = $this->get_namespaced_field( $item, $this->cat_field . "#" );
 730				if ( $num_cats > 1 ) {
 731					for( $i=2; $i<=$num_cats; $i++ ) {
 732						$nameList .= $this->cat_delimiter . " " . $this->get_namespaced_field( $item, $this->cat_field . "#" . $i );
 733					}
 734				}
 735
 736				$cat_ids = array();
 737
 738				$names = explode( $this->cat_delimiter, $nameList );
 739				foreach ( $names as $name ) {
 740					$name = trim( $name );
 741					$query =$DB->query("SELECT cat_id
 742						FROM exp_categories
 743						WHERE exp_categories.cat_name = '".$DB->escape_str($name)."'
 744						AND exp_categories.parent_id = '0'
 745						AND exp_categories.group_id = '".$DB->escape_str($this->cat_group)."'");
 746
 747					if ($query->num_rows == 0) {	
 748
 749						// Create primary category
 750						$insert_array = array(
 751							'group_id'  	=> $this->cat_group,
 752							'site_id' => ( $this->site_id != '' ? $this->site_id : 1 ),
 753							'cat_name' 	=> $name,
 754							'cat_url_title' => $REGX->create_url_title( $name ),
 755							'cat_image' => '',
 756							'parent_id'   => '0'
 757							);
 758					
 759						print "<p>Try to add category " . $name . "</p>";
 760
 761						$DB->query($DB->insert_string('exp_categories', $insert_array));       
 762						$cat_ids[] = $DB->insert_id;
 763
 764						$insert_array = array(
 765							'cat_id'  	=> $DB->insert_id,
 766							'site_id' 	=> ( $this->site_id != '' ? $this->site_id : 1 ),
 767							'group_id' 	=> $this->cat_group
 768						);
 769						$DB->query($DB->insert_string('exp_category_field_data', $insert_array));       
 770
 771
 772					} else {
 773						print "<p>Category " . $name . " exists</p>";
 774						$cat_ids[] = $query->row['cat_id'];
 775					}
 776				} 
 777			} 
 778
 779			// Insert  category details
 780
 781			if ( isset($cat_ids) && count($cat_ids) > 0 ) 
 782			{         	
 783				$cats = array_unique($cat_ids);
 784
 785				$results = $DB->query("SELECT cat_id, parent_id FROM exp_categories 
 786					WHERE (cat_id IN ('".implode("','",$cats)."') OR cat_name IN ('".implode("','",$cats)."'))
 787					AND group_id = '".$this->cat_group."'");
 788
 789				if ($results->num_rows > 0)
 790				{
 791					foreach($results->result as $row)
 792					{
 793						if ($PREFS->ini('auto_assign_cat_parents') == 'y' && $row['parent_id'] != '0')
 794						{
 795							if ($this->runsql) { 
 796								$DB->query("INSERT INTO exp_category_posts (entry_id, cat_id) VALUES ('$entry_id', '".$row['parent_id']."')");
 797							}
 798						}
 799						if ($this->runsql) {
 800							$DB->query("INSERT INTO exp_category_posts (entry_id, cat_id) VALUES ('$entry_id', '".$row['cat_id']."')");
 801						}
 802					}
 803				}
 804			}
 805			
 806			// Category group 2
 807
 808			$query = $DB->query("DELETE  p FROM exp_category_posts p LEFT JOIN exp_categories c USING (cat_id) WHERE entry_id = " . $DB->escape_str($entry_id) . " AND c.group_id = " . $DB->escape_str($this->cat_group_2) );
 809		
 810			if ( $this->cat_field_2 != '' ) {
 811
 812				$this->cat_field_2 = str_replace(SLASH, '/', $this->cat_field_2);
 813
 814				// Find category item
 815				$nameList = $this->get_namespaced_field( $item, $this->cat_field_2 );
 816
 817				// Does entry have multiple category items
 818				$num_cats = $this->get_namespaced_field( $item, $this->cat_field_2 . "#" );
 819				if ( $num_cats > 1 ) {
 820					for( $i=2; $i<=$num_cats; $i++ ) {
 821						$nameList .= $this->cat_delimiter . " " . $this->get_namespaced_field( $item, $this->cat_field_2. "#" . $i );
 822					}
 823				}
 824
 825				$cat_ids = array();
 826
 827				$names = explode( $this->cat_delimiter_2, $nameList );
 828				foreach ( $names as $name ) {
 829					$name = trim( $name );
 830					$query =$DB->query("SELECT cat_id
 831						FROM exp_categories
 832						WHERE exp_categories.cat_name = '".$DB->escape_str($name)."'
 833						AND exp_categories.parent_id = '0'
 834						AND exp_categories.group_id = '".$DB->escape_str($this->cat_group_2)."'");
 835
 836					if ($query->num_rows == 0) {	
 837
 838						// Create primary category
 839						$insert_array = array(
 840							'group_id'  	=> $this->cat_group_2,
 841							'site_id' => ( $this->site_id != '' ? $this->site_id : 1 ),
 842							'cat_name' 	=> $name,
 843							'cat_url_title' => $REGX->create_url_title( $name ),
 844							'cat_image' => '',
 845							'parent_id'   => '0'
 846							);
 847					
 848						print "<p>Try to add category " . $name . "</p>";
 849
 850						$DB->query($DB->insert_string('exp_categories', $insert_array));       
 851						$cat_ids[] = $DB->insert_id;
 852
 853						$insert_array = array(
 854							'cat_id'  	=> $DB->insert_id,
 855							'site_id' 	=> ( $this->site_id != '' ? $this->site_id : 1 ),
 856							'group_id' 	=> $this->cat_group_2
 857						);
 858						$DB->query($DB->insert_string('exp_category_field_data', $insert_array));       
 859
 860
 861					} else {
 862						print "<p>Category " . $name . " exists</p>";
 863						$cat_ids[] = $query->row['cat_id'];
 864					}
 865				} 
 866			} 
 867
 868			// Insert  category details
 869
 870			if ( isset($cat_ids) && count($cat_ids) > 0 ) 
 871			{         	
 872				$cats = array_unique($cat_ids);
 873
 874				$results = $DB->query("SELECT cat_id, parent_id FROM exp_categories 
 875					WHERE (cat_id IN ('".implode("','",$cats)."') OR cat_name IN ('".implode("','",$cats)."'))
 876					AND group_id = '".$this->cat_group_2."'");
 877
 878				if ($results->num_rows > 0)
 879				{
 880					foreach($results->result as $row)
 881					{
 882						if ($PREFS->ini('auto_assign_cat_parents') == 'y' && $row['parent_id'] != '0')
 883						{
 884							if ($this->runsql) { 
 885								$DB->query("INSERT INTO exp_category_posts (entry_id, cat_id) VALUES ('$entry_id', '".$row['parent_id']."')");
 886							}
 887						}
 888						if ($this->runsql) {
 889							$DB->query("INSERT INTO exp_category_posts (entry_id, cat_id) VALUES ('$entry_id', '".$row['cat_id']."')");
 890						}
 891					}
 892				}
 893			}
 894
 895		}
 896		}
 897	}
 898
 899	if ($entries_added > 0) {
 900		if ( $this->debug ) { 
 901			print "<br/>\nNew entries: " . $entries_added . "<br/>\n"; 
 902		}
 903		
 904		if ($PREFS->ini('new_posts_clear_caches') == 'y') {
 905			$FNS->clear_caching('all');
 906		} else {
 907			$FNS->clear_caching('sql_cache');
 908		}
 909	}
 910}
 911
 912public static function usage() {
 913	ob_start(); 
 914
 915	?>
 916		XMLGrab allows you to extract data from an XML feed and insert it into a weblog. 
 917
 918		See: http://www.brandnewbox.co.uk/docs/
 919
 920	<?php
 921
 922	$buffer = ob_get_contents();	
 923	ob_end_clean(); 
 924	return $buffer;
 925}
 926
 927function get_namespaced_field( $item, $field ) {	
 928	// $field = strtolower( $field );
 929	if ( strpos( $field, ':' ) ) {
 930		$subfieldArray = explode( ":", $field );
 931		if ( isset( $item[ $subfieldArray[0] ][ $subfieldArray[1] ] ) ) {
 932			return( $item[ $subfieldArray[0] ][ $subfieldArray[1] ] );
 933		}
 934	} else {
 935		if ( isset( $item[ $field ] ) ) {
 936			return( $item[ $field ] );
 937		}
 938	}
 939}
 940
 941function parse_date( $datestr ) {
 942	print "<p>$datestr</p>";
 943/*
 944	if ( $this->uk_dates ) { 
 945		$datestr = $this->reformat_to_dd_mm_yyyy( $datestr );
 946	}
 947	*/
 948	$date = strtotime( $datestr );
 949	/*
 950	if ( $date == -1 ) {
 951		$date = $this->parse_w3cdtf( $datestr );
 952	}
 953	if ( $date == -1 ) {
 954		$date = $this->parse_twitter_created_at( $datestr );
 955	}
 956	*/
 957	if ( $date == -1 || $date == "" ) {
 958		$date = time();
 959	}
 960	return( $date );
 961}
 962
 963function parse_twitter_created_at( $datestr ) {
 964	// Format: Wed Apr 18 13:17:34 +0000 2007
 965	$parts = explode( " ", $datestr );
 966	$newdatestr = $parts[2] . " " . $parts[1] . " " . $parts[5] . " " . $parts[3] . " " . $parts[4]; 
 967	return strtotime( $newdatestr );
 968}
 969
 970function reformat_to_dd_mm_yyyy( $datestr ) {
 971	// reformats: MM/DD/YYYY to DD/MM/YYYY
 972	// should pass through if does not match regex
 973	$newdatestr = preg_replace("/^\s*([0-9]{1,2})[\/\. -]+([0-9]{1,2})[\/\. -]+([0-9]{2,4})/", "\\2/\\1/\\3", $datestr);
 974	return $newdatestr;
 975}
 976
 977
 978function parse_w3cdtf ( $date_str ) {
 979
 980	# regex to match wc3dtf
 981	$pat = "/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):((\d{2}))?(?:([-+])(\d{2}):?(\d{2})|(Z))?/";
 982
 983	if ( preg_match( $pat, $date_str, $match ) ) {
 984		list( $year, $month, $day, $hours, $minutes, $seconds) = 
 985			array( $match[1], $match[2], $match[3], $match[4], $match[5], $match[6]);
 986
 987		# calc epoch for current date assuming GMT
 988		$epoch = gmmktime( $hours, $minutes, $seconds, $month, $day, $year);
 989
 990		$offset = 0;
 991		if ( $match[10] == 'Z' ) {
 992			# zulu time, aka GMT
 993		}
 994		else {
 995			list( $tz_mod, $tz_hour, $tz_min ) =
 996				array( $match[8], $match[9], $match[10]);
 997
 998			# zero out the variables
 999			if ( ! $tz_hour ) { $tz_hour = 0; }
1000			if ( ! $tz_min ) { $tz_min = 0; }
1001
1002			$offset_secs = (($tz_hour*60)+$tz_min)*60;
1003
1004			# is timezone ahead of GMT?  then subtract offset
1005			#
1006			if ( $tz_mod == '+' ) {
1007				$offset_secs = $offset_secs * -1;
1008			}
1009
1010			$offset = $offset_secs; 
1011		}
1012		$epoch = $epoch + $offset;
1013		return $epoch;
1014	}
1015	else {
1016		return -1;
1017	}
1018}
1019
1020function is_entry_unique( $post, $unique, $weblog_to_feed ) {
1021	global $DB;
1022
1023	// Default for backwards compatibility
1024	if ( $unique == "title,date" || $unique == "" ) {
1025
1026		$query = $DB->query("SELECT * FROM exp_weblog_titles WHERE LEFT(title,100) = LEFT('".$DB->escape_str($post[ "title" ])."',100) AND entry_date = '".$DB->escape_str($post[ "date" ])."'");
1027
1028	} else {
1029
1030		/* Build custom query */
1031
1032		$sql = "SELECT * FROM exp_weblog_titles t, exp_weblog_data d WHERE t.entry_id = d.entry_id";
1033
1034		$uniqueArray = explode(",", $unique);
1035		foreach ( $uniqueArray as $value ) {
1036			switch ( $value ) {
1037				case 'title': {
1038					$sql .= " AND " . $value . "=\"" . $DB->escape_str( $post[ $value ] ) . "\"";
1039					break;
1040				}
1041				case 'date': {
1042					$sql .= " AND entry_date=\"" . $DB->escape_str( $post[ $value ] ) . "\"";
1043					break;
1044				}
1045				default: {
1046					if ( $weblog_to_feed[ $value ][ "is_custom" ] ) {
1047						$name = "field_id_" . $weblog_to_feed[ $value ][ "id" ];
1048						$sql .= " AND " . $name . "=\"" . $DB->escape_str( $post[ $value ] ) . "\"";
1049					}
1050				}
1051			}
1052		}
1053
1054		$query = $DB->query( $sql );
1055
1056	}
1057
1058	if ( $query->num_rows > 0) {
1059		return $query->row['entry_id'];
1060	}
1061	return 0;
1062}
1063
1064function fetch_xml( $x, $search, &$items, $path="", $element=0, $in_element=false, $subpath="" ) {
1065
1066	$path = $path . "/" . $x->tag ;
1067
1068	// print "@" . $search . "@ v @" . $path . "@<br/>";
1069
1070	if ( $path == $search ) {
1071		// Path matches exactly our search element - we are in a new item
1072		$element++;
1073		$items[ $element ] = array();
1074		$subpath = "";
1075		$in_element = true;
1076	} elseif ( $str = strstr( $path, $search ) ) {
1077		// We are within an existing item  - get xpath of subcomponent
1078		$subpath = substr( $str, strlen( $search )+1 );
1079		if ( ! isset( $items[ $element ][ $subpath . "#" ] ) ) {
1080			$items[ $element ][ $subpath . "#" ] = 0;
1081		}
1082		$count = $items[ $element ][ $subpath . "#" ]++;
1083		if ( isset( $items[ $element ][ $subpath ] ) ) {
1084			$subpath .= "#" . ( $count + 1);
1085		}
1086	} else {
1087		$in_element = false;
1088	}
1089
1090	if ( count( $x->children ) == 0 ) {
1091
1092		// Element has children ie, is not a parent element
1093		if ( $in_element ) {
1094			// If within an item, add to its array
1095			$items[ $element ][ $subpath ] = $x->value;
1096		}
1097	} else {
1098		// Loop over all child elements...        
1099		foreach ( $x->children as $key => $value ) {
1100			// ...and recurse through xml structure
1101			$element = $this->fetch_xml( $value, $search, $items, $path, $element, $in_element, $subpath );
1102		}
1103	}
1104
1105	// Add attributes
1106	if( $in_element ) {
1107		if ( is_array( $x->attributes ) ) {
1108			foreach ( $x->attributes as $attr_key => $attr_value ) {
1109				$items[ $element ][ $subpath . "@" . $attr_key ] = $attr_value;
1110			}
1111		}
1112	}
1113
1114
1115	return $element;
1116}
1117
1118// --------------------------------------------------------------------
1119
1120/**
1121	* curl Fetch
1122	*
1123	* From pi.twitter_timeline.php
1124	*
1125	* @access	public
1126	* @param	string
1127	* @return	string
1128	*/
1129function _curl_fetch($url)
1130{
1131	$ch = curl_init(); 
1132	curl_setopt($ch, CURLOPT_URL, $url); 
1133	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
1134	curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
1135	// curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->password}");
1136
1137	$data = curl_exec($ch);
1138
1139	curl_close($ch);
1140
1141	return $data;
1142}
1143
1144// --------------------------------------------------------------------
1145
1146/**
1147	* fsockopen Fetch
1148	*
1149	* From pi.twitter_timeline.php
1150	*
1151	* @access	public
1152	* @param	string
1153	* @return	string
1154	*/
1155function _fsockopen_fetch($url)
1156{
1157	$target = parse_url($url);
1158
1159	$data = '';
1160
1161	$fp = fsockopen($target['host'], 80, $error_num, $error_str, 8); 
1162
1163	if (is_resource($fp))
1164	{
1165		fputs($fp, "GET {$url} HTTP/1.0\r\n");
1166		fputs($fp, "Host: {$target['host']}\r\n");
1167		// fputs($fp, "Authorization: Basic ".base64_encode("$this->user:$this->password")."\r\n");
1168		fputs($fp, "User-Agent: EE/xmlgrab PHP/" . phpversion() . "\r\n\r\n");
1169
1170		$headers = TRUE;
1171
1172		while( ! feof($fp))
1173		{
1174			$line = fgets($fp, 4096);
1175
1176			if ($headers === FALSE)
1177			{
1178				$data .= $line;
1179			}
1180			elseif (trim($line) == '')
1181			{
1182				$headers = FALSE;
1183			}
1184		}
1185
1186		fclose($fp); 
1187	}
1188
1189	return $data;
1190}
1191
1192
1193} // end class XMLGrab
1194
1195?>