Location: PHPKode > scripts > IMDb_Fetch > imdb_fetch/IMDb_Fetch.class.php
<?php
/**
 * ************************************************************** *
 *                                                                *
 * ************************************************************** *
 * ************************************************************** *
 *                  Copyright notice and creds					  *
 * ************************************************************** *
 * ************************************************************** *
 *                                                                *
 * A class to retrive information out from IMDb about movies.     *
 * Select what information to fetch for yourself. 				  *
 * Add as many movies as you like.								  *
 *                                                                *
 * Protected by Norwegian law                                     *
 *  Mikael Brevik @ http://mikaelb.net/                           *
 *                                                                *
 *                                                                *
 * ************************************************************** *
 *                             Usage                              *
 * ************************************************************** *
 * 
 * If you are using MySQL caching you must connect to MySQL and 
 * select DB before running any processing methods in the class.
 * 
 * Also if you deside to use UTF-8, run these codes:
 * 
 * // UTF-8 Fix
 * header('Content-Type: text/html; charset=utf-8');
 *
 *	if($imdb->activate_mysql_cache) {
 *		mysql_query("SET NAMES utf8");
 *	}
 * 
 * You also might want to add a notice to the user that non-cached
 * movies will take some time to load.
 * 
 * ************************************************************** *
 * 
 * Example of getting:
 *  - all info out of one movie (or more):
 * 
 * $imdb = new IMDb_Fetch ('The Boondock Saints');
 * $imdb->all_details = true;
 * 
 * print_r($imdb->get_result_array());
 * 
 * ************************************************************** *
 * 
 * Example of getting:
 * 
 *  - rating, name, 4 of the cast and year of production 
 * from X movies:
 * 
 * $imdb = new IMDb_Fetch ();
 * $imdb->from_title = array ('tt0123456', 'The Boondock Saints');
 * $imdb->add_movie('Disturbia');
 * $imdb->show_year = true;
 * $imdb->show_cast = 4;
 * $imdb->show_rating = true;
 * 
 * print_r($imdb->get_result_array());
 * 
 * 
 * Example of getting:
 *  - 10 videos from your Vote History with all details
 * 
 * $imdb = new IMDb_Fetch ();
 * $imdb->add_movie($imdb->get_votehistory('USERID', 10));
 * $imdb->all_details = true
 * 
 * print_r($imdb->get_result_array());
 * 
 * (Vote history must be public!)
 * 
 * ************************************************************** *
 * ************************************************************** *
 * 
 * The formating of the results is something you must take 
 * care of on your own.
 * 
 * ************************************************************** *
 * ************************************************************** *
 * ************************************************************** *
 * Use the class with care and please put a link to my site		  *
 * from the site that uses the script. My blog is at        	  *
 * http://mikaelb.net                                       	  *
 * ************************************************************** *
 */

class IMDb_Fetch {

	// Language settings for when results are not found
	public $lang_unknown = 'Unknown';
	public $default_poster = 'No poster';
	
	
	
	
	// Setting system variables
	public $from_title = false;
	
	public $image_path = '';

	public $all_details = false;

	public $show_year = false;
	public $show_director = false;
	public $show_writer = false;
	public $show_genre = false;
	public $show_tagline = false;
	public $show_plot = false;
	public $show_keywords = false;
	public $show_cast = false;
	public $show_runtime = false;
	public $show_country = false;
	public $show_rating = false;
	public $show_poster = false;
	public $show_votes = false;
	public $show_language = false;

	// System variable - Storing cache-settings
	public $activate_mysql_cache = false;


	/**
	 * Cunstructor. Makes it possible to start the
	 * movielist and add titles to the list. 
	 *
	 * @param mixed $movies
	 */
	public function __construct ($movies = false) {
		if ($movies) {
			$this->add_movie($movies);
		}
	}

	/**
	 * Fetch movies from vote history. Vote history must be public!
	 *
	 * @param int $user
	 * @return mixed
	 */
	public function get_votehistory ($user, $max = 0) {
		$user = (int) $user;
		$url = 'http://imdb.com/mymovies/list?l='.$user;

		if (!$stream = fopen($url, 'r')) {
			return '';
		}

		$source = stream_get_contents($stream);
		fclose($stream);

		$matches = array ();
		$regex = '/<a href="\/title\/(t{2}\d{7})\/">([^>]*)<\/a> \((\d{4})(?:.*)\)<\/td>/';
		preg_match_all ($regex, $source, $matches);

		if(count($matches[1])) {
			if($max !== 0 && is_numeric($max) && $max < count($matches[1])) {
				return array_slice($matches[1], 0, $max);
			}
			
			return $matches[1];
		}

		return '';
	}


	/**
	 * Add a movie or movies to the list. 
	 * Add through array, multiple arguments or one single title.
	 * 
	 * return bool
	 */
	public function add_movie(/* args */) {
		$args = func_get_args();
		$movie = $args[0];
		unset($args[0]);

		if(!is_array ($this->from_title)) {
			if(is_string($this->from_title)) {
				$this->from_title = array($this->from_title);
			} else {
				$this->from_title = array();
			}
		}
		if(is_array($movie)) {
			$this->from_title = array_merge($this->from_title, $movie);
		} else {
			$this->from_title[] = $movie;
		}

		if(isset($args[1])) {
			foreach ($args as $movie) {
				$this->add_movie($movie);
			}
		}

		return true;
	}

	/**
	 * Fetch poster/movie image from the ID given in the argument.
	 *
	 * @param string $id
	 * @return string
	 */
	private function get_poster ($id) {

		if(!empty($this->image_path)) {
			if(substr($this->image_path, -1) != '/') {
				$this->image_path .= '/';
			}
			$path = $this->image_path . $id . '.jpg';

			if(file_exists ($path)) {
				return $path;
			}
		}


		$matches = array ();
		$source = @file_get_contents ('http://imdb.com/title/' . $id);
		$regex = '/<a name="poster"(?:[^>]*)><img(?:[^>]*)src="(?P<poster>[^>]*)" height="(?:[^>]*)><\/a>/';
		preg_match ($regex, $source, $matches);

		if(isset($matches[1])) {
			if(!empty($this->image_path)) {
				if (copy ($matches[1], $path)) {
					return $path;
				}
			} else {
				return $matches[1];
			}
		}

		return $this->default_poster;
	}

	/**
	 * Get information from $source through given $regex. $type sets name of the information. 
	 * Results are added to $match
	 *
	 * @param string $regex
	 * @param string $type
	 * @param string $source
	 * @param array $match
	 * @return bool
	 */
	private function get_from_regex ($regex, $type, $source, &$match) {
		$matcher = array ();

		if(empty($regex) || empty($type) || empty($source)) {
			$match['votes'] = $this->lang_unknown;
			return false;
		}

		preg_match_all($regex, $source, $matcher);

		if(!count($matcher[1])) {
			$match[$type] = $this->lang_unknown;
			return false;
		}

		$match[$type] = $matcher[1][0];
		return true;
	}

	/**
	 * Get selected information out of a IMDb-ID on a movie through IMDB-site.
	 *
	 * @param string $id
	 * @return array
	 */
	private function get_from_id ($id) {

		if($this->activate_mysql_cache) {
			$details = $this->all_details;
			$this->all_details = true;
			$show_cast = $this->show_cast;
		}

		// Get the source.
		$source = @file_get_contents ('http://imdb.com/title/'.$id.'/');

		// Now fetch the other info.
		$imdb_info = array ();
		$regex_info = '/<title>([^>]*) \((\d{4})(?:.*)\)<\/title>/';
		preg_match ($regex_info, $source, $imdb_info);



		$match = array ('id' => $id,
		'name' => $imdb_info[1]);
		
		
		
		// All different settings...
		if($this->show_year || $this->all_details) {
			$match['year'] = $imdb_info[2];
		}

		if($this->show_rating || $this->all_details) {
			$rating_regex = '/<b>User Rating:<\/b>\s*<b>(\d{1,2}\.\d)\/10<\/b>/';
			$this->get_from_regex ($rating_regex, 'rating', $source, $match);
		}

		if($this->show_votes || $this->all_details) {
			$votes_regex = '/<small>\(<a href="ratings">([^>]*) votes<\/a>\)<\/small>/';
			$this->get_from_regex ($votes_regex, 'votes', $source, $match);
		}

		if($this->show_poster || $this->all_details) {
			$match['poster'] = $this->get_poster($id);
		}

		if($this->show_director || $this->all_details) {
			$director = array ();
			$director_regex = '/<h5>Director:<\/h5>\n<a href="\/name\/(nm\d{7})\/">([^>]*)<\/a>/';
			preg_match($director_regex, $source, $director);

			if(isset($director[1])) {
				$match['director'] = array ('id' => $director[1], 'name' => $director[2]);
			} else {
				$match['director'][] = $this->lang_unknown;
			}
		}

		if($this->show_writer || $this->all_details) {
			$writer = array ();
			$writer_regex = '/<h5>Writers?(?: <a href="\/wga">\(WGA\)<\/a>)?:<\/h5>\n<a href="\/name\/(nm\d{7})\/">([^>]*)<\/a>\s*(\([\w ]*\))?(?:<br\/><a href="\/name\/(nm\d{7})\/">([^>]*)<\/a> \(([\w ]*)\))?/';
			preg_match_all($writer_regex, $source, $writer);


			$writer_full = array();
			$writer_full[] = array ('id' => $writer[1][0],
			'name' => $writer[2][0],
			'role' => $writer[3][0]);

			if(empty($writer[4])) {
				$writer_sec = array ('id' => $writer[4][0],
				'name' => $writer[5][0],
				'role' => $writer[6][0]);
				$writer_full[] = $writer_sec;
			}
			if(!count($writer[1])) {
				$writer_full = array();
				$writer_full[] = $this->lang_unknown;
			}
			$match['writer'] = $writer_full;
		}


		if($this->show_genre || $this->all_details) {
			$genre = array ();
			$genre_regex = '/<a href="\/Sections\/Genres\/([\w ]*)\/">/';
			preg_match_all($genre_regex, $source, $genre);

			if(count($genre[1])) {
				$match['genre'] = $genre[1];
			} else {
				$match['genre'][] = $this->lang_unknown;
			}
		}

		if($this->show_tagline || $this->all_details) {
			$tagline_regex = '/<h5>Tagline:<\/h5>\n([^>]*)(?:<a[^>]*>more<\/a>\n)?<\/div>/';
			$this->get_from_regex ($tagline_regex, 'tagline', $source, $match);
		}


		if($this->show_plot || $this->all_details) {
			$plot_regex = '/<h5>Plot (?:Outline|Summary):<\/h5>\s*([^>]*)(?:\s*<a[^>]*>more<\/a>)?\n<\/div>/';
			$this->get_from_regex ($plot_regex, 'plot', $source, $match);
		}

		if($this->show_keywords || $this->all_details) {
			$keywords = array ();
			$keywords_regex = '/<a href="\/keyword\/[^>]*\/">([^>]*)<\/a>/';
			preg_match_all($keywords_regex, $source, $keywords);


			if(count($keywords[1])) {
				$match['keywords'] = $keywords[1];
			} else {
				$match['keywords'][] = $this->lang_unknown;
			}
		}

		if($this->show_cast || $this->all_details) {

			$cast = array ();
			$cast_regex = '/<td class="nm"><a href="\/name\/(nm\d{7})\/">([^>]*)<\/a><\/td><td class="ddd"> ... <\/td><td class="char">([^>]*)<\/td>/';
			preg_match_all($cast_regex, $source, $cast);

			if($this->show_cast === true || ($this->show_cast === false && $this->all_details)) {
				$this->show_cast = count($cast[1]);
			}

			if(!count($cast[1])) {
				$match['cast'][] = $this->lang_unknown;
			} else {

				$ret_cast = array ();
				for ($i = 0; $i < $this->show_cast; $i++) {
					if(isset($cast[1][$i])) {
						$ret_cast[] = array ('id' => $cast[1][$i],
						'name' => $cast[2][$i],
						'role' => $cast[3][$i]);
					}
				}

				$match['cast'] = $ret_cast;
			}
		}


		if($this->show_runtime || $this->all_details) {
			$runtime_regex = '/<h5>Runtime:<\/h5>\s*(\d{2,5})/';
			$this->get_from_regex ($runtime_regex, 'runtime', $source, $match);
		}

		if($this->show_country || $this->all_details) {
			$country = array ();
			$country_regex = '/<a href="\/Sections\/Countries\/([^>]*)\/">/';
			preg_match_all($country_regex, $source, $country);


			if(count($country[1])) {
				$match['country'] = $country[1];
			} else {
				$match['country'][] = $this->lang_unknown;
			}
		}

		if($this->show_language || $this->all_details) {
			$language = array ();
			$language_regex = '/<a href="\/Sections\/Languages\/([^>]*)\/">/';
			preg_match_all($language_regex, $source, $language);


			if(count($language[1])) {
				$match['language'] = $language[1];
			} else {
				$match['language'][] = $this->lang_unknown;
			}
		}



		if($this->activate_mysql_cache) {

			if(!$this->save_info($match)) {
				return 'Error saving information';
			}

			$this->all_details = $details;
			$this->show_cast = $show_cast;
			return $this->mysql_movie_info ($id);
		}


		return $match;
	}

	
	/**
	 * Save the settings from given array containing movie information
	 *
	 * @param array $movie_info
	 * @return boolean
	 */
	private function save_info ($movie_info) {

		// Movie-information
		$title_info = 'INSERT INTO `imdb_title` (`title_imdb`, `name`, `year`, `runtime`, `tagline`, `plot`, `poster`)
		VALUES (%s, %s, %d, %d, %s, %s, %s)';
		$title_query = sprintf($title_info,
		$this->quote_smart($movie_info['id']),
		$this->quote_smart($movie_info['name']),
		$this->quote_smart($movie_info['year']),
		$this->quote_smart($movie_info['runtime']),
		$this->quote_smart($movie_info['tagline']),
		$this->quote_smart($movie_info['plot']),
		$this->quote_smart($movie_info['poster'])
		);

		$title_mysql = mysql_query($title_query);
		if(!$title_mysql) {
			return false;
		}

		$movie_id = (int) mysql_insert_id();

		$dir_imdb = $movie_info['director']['id'];
		if($movie_info['director'][0] == $this->lang_unknown) {
			$dir_imdb = 'none';
			$movie_info['director']['name'] = $this->lang_unknown;
		}


		// Director add
		if(!$this->thing_exsist('imdb_director', 'director_imdb', $dir_imdb)) {


			$director_query = 'INSERT INTO imdb_director (director_imdb, name) VALUES (%s, %s)';
			$director_query = sprintf($director_query, $this->quote_smart($dir_imdb), $this->quote_smart($movie_info['director']['name']));

			$director_sql = mysql_query ($director_query);
			if(!$director_sql) {
				return false;
			}

			$director_id = (int) mysql_insert_id();
			$director_title_sql = mysql_query ("INSERT INTO imdb_director_title (director_id, movie_id) VALUES ($director_id, $movie_id)");

			if(!$director_title_sql) {
				return false;
			}
		} else {
			if(!$this->insert_connection('imdb_director', 'director_imdb', $dir_imdb, 'imdb_director_title', 'director_id', $movie_id)) {
				return false;
			}
		}

		// Cast add
		if(!$this->mysql_from_array_extended ('cast', $movie_info['cast'], $movie_id)) {
			return false;
		}

		// Writer add
		if(!$this->mysql_from_array_extended ('writer', $movie_info['writer'], $movie_id)) {
			return false;
		}

		if(!$this->mysql_from_array('country', $movie_info['country'], $movie_id)) {
			return false;
		}

		if(!$this->mysql_from_array('language', $movie_info['language'], $movie_id)) {
			return false;
		}

		if(!$this->mysql_from_array('genre', $movie_info['genre'], $movie_id)) {
			return false;
		}

		if(!$this->mysql_from_array('keyword', $movie_info['keywords'], $movie_id)) {
			return false;
		}

		return true;
	}
	
	
	/**
	 * Insert information into table with suffix $type.
	 * Method used in the method above. 
	 *
	 * @param string $type
	 * @param array $movie_info
	 * @param string $movie_id
	 * @return boolean
	 */
	private function mysql_from_array ($type, $movie_info, $movie_id) {

		// Country information
		$count = count($movie_info);
		$query_temp = 'INSERT INTO imdb_'.$type.' (name) VALUES (%s)';

		for($i=0; $i<$count; $i++) {
			if(!$this->thing_exsist('imdb_'.$type, 'name', $movie_info[$i])) {

				$query = sprintf($query_temp, $this->quote_smart($movie_info[$i]));
				$sql = mysql_query ($query);
				if(!$sql) {
					return false;
				}

				$gotten_id = (int) mysql_insert_id();
				$type_title_sql = mysql_query ("INSERT INTO imdb_{$type}_title ({$type}_id, movie_id) VALUES ($gotten_id, $movie_id)");

				if(!$type_title_sql) {
					return false;
				}
			} else {
				if(!$this->insert_connection('imdb_'.$type, 'name', $movie_info[$i], 'imdb_'.$type.'_title', $type.'_id', $movie_id)) {
					return false;
				}
			}
		}

		return true;
	}
	
	/**
	 * Save information for Cast and such multidimentional arrays, and same as the above.
	 * Also used in the same method as the mysql_from_array method.
	 *
	 * @param string $type
	 * @param array $movie_info
	 * @param string $movie_id
	 * @return boolean
	 */
	private function mysql_from_array_extended ($type, $movie_info, $movie_id) {

		// Country information
		$cast_count = count($movie_info);
		$cast_info = "INSERT INTO imdb_$type ({$type}_imdb, name, role) VALUES (%s, %s, %s)";

		for($i=0; $i<$cast_count; $i++) {

			if(!$this->thing_exsist('imdb_'.$type, $type.'_imdb', $movie_info[$i]['id'])) {

				$cast_query = sprintf($cast_info,
				$this->quote_smart($movie_info[$i]['id']),
				$this->quote_smart($movie_info[$i]['name']),
				$this->quote_smart($movie_info[$i]['role']));

				$cast_sql = mysql_query ($cast_query);
				if(!$cast_sql) {
					return false;
				}
				$cast_id = (int) mysql_insert_id();
				$cast_title_sql = mysql_query ("INSERT INTO imdb_{$type}_title ({$type}_id, movie_id) VALUES ($cast_id, $movie_id)");

				if(!$cast_title_sql) {
					return false;
				}
			} else {
				if(!$this->insert_connection('imdb_'.$type, $type.'_imdb', $movie_info[$i]['id'], 'imdb_'.$type.'_title', $type.'_id', $movie_id)) {
					return false;
				}
			}
		}

		return true;
	}

	
	/**
	 * Checks if the $id exsists in the $field of the $table
	 *
	 * @param string $table
	 * @param string $field
	 * @param mixed $id
	 * @return boolean
	 */
	private function thing_exsist ($table, $field, $id) {
		$id = $this->quote_smart($id);
		$sql = mysql_query("SELECT COUNT(1) AS numb FROM $table WHERE $field = $id");

		return mysql_result($sql, 0, 'numb');
	}

	/**
	 * Insert a connection between $table and $connection_table.
	 * Check for $id_field in $table, where $field = $id. And insert into $connection_table set $id_field and movie_id 
	 *
	 * @param string $table
	 * @param string $field
	 * @param string $id
	 * @param string $connection_table
	 * @param string $id_field
	 * @param int $movie_id
	 * @return bool
	 */
	private function insert_connection ($table, $field, $id, $connection_table, $id_field, $movie_id) {
		$id = $this->quote_smart($id);
		$query = "SELECT $id_field FROM $table WHERE $field = $id";
		$sql = mysql_query($query);
		if(!$sql) {
			return false;
		}
		$id_connection = mysql_result($sql, 0);

		$query = mysql_query("INSERT INTO $connection_table ($id_field, movie_id) VALUES ($id_connection, $movie_id)");
		if($query) {
			return true;
		}

		return false;
	}



	/**
	 * Get selected information out of a IMDb-ID on a movie through Cached MySQL.
	 *
	 * @param string $id
	 * @return array
	 */
	private function mysql_movie_info ($id) {

		if(strpos($id, "\n") !== false) {
			return 'Error fetching information';
		}

		$id = $this->quote_smart($id);

		$query = 'SELECT movie_id, title_imdb, name, year, runtime, tagline, plot, poster FROM imdb_title
		WHERE title_imdb = '.$id . ' OR name = '. $id;
		$mysql_query = mysql_query ($query);

		$imdb_info = array();
		while($row = mysql_fetch_assoc($mysql_query)) {
			$imdb_info = $row;
		}

		// Fetch All information.
		// Start by adding ID (imdb-id) and Name
		$match = array ('id' => $imdb_info['title_imdb'], 'name' => $imdb_info['name']);

		if($this->show_year || $this->all_details) {
			// Show Year

			$match['year'] = $imdb_info['year'];
		}


		if($this->show_poster || $this->all_details) {
			$match['poster'] = $imdb_info['poster'];
		}

		if($this->show_director || $this->all_details) {
			$input = $this->get_sub_cat($imdb_info['title_imdb'], 'director', 'a.director_imdb, a.name');
			$match['director'] = array('id' => $input[0]['director_imdb'], 'name' => $input[0]['name']);
		}

		if($this->show_writer || $this->all_details) {
			$input = $this->get_sub_cat($imdb_info['title_imdb'], 'writer', 'a.writer_imdb, a.name, a.role');
			$writer_out = array();
			foreach($input as $writer) {
				$writer_out[] = array('id' => $writer['writer_imdb'], 'name' => $writer['name'], 'role' => $writer['role']);
			}

			$match['writer'] = $writer_out;
		}

		if($this->show_genre || $this->all_details) {
			$match['genre'] = $this->get_sub_cat($imdb_info['title_imdb'], 'genre', 'a.name', false);
		}

		if($this->show_tagline || $this->all_details) {
			$match['tagline'] = $imdb_info['tagline'];
		}

		if($this->show_plot || $this->all_details) {
			$match['plot'] = $imdb_info['plot'];
		}

		if($this->show_keywords || $this->all_details) {
			$match['keywords'] = $this->get_sub_cat($imdb_info['title_imdb'], 'keyword', 'a.name', false);
		}

		if($this->show_cast || $this->all_details) {

			$cast_sql = $this->get_sub_cat($imdb_info['title_imdb'], 'cast', 'a.cast_imdb, a.name, a.role');

			$cast_out = array();
			if($this->show_cast === true || ($this->show_cast === false && $this->all_details)) {
				$this->show_cast = count($cast_sql);
			}

			for($i=0; $i<$this->show_cast; $i++) {
				$cast_out[] = array('id' => $cast_sql[$i]['cast_imdb'], 'name' => $cast_sql[$i]['name'], 'role' => $cast_sql[$i]['role']);
			}

			$match['cast'] = $cast_out;
		}

		if($this->show_runtime || $this->all_details) {
			$match['runtime'] = $imdb_info['runtime'];
		}

		if($this->show_country || $this->all_details) {
			$match['country'] = $this->get_sub_cat($imdb_info['title_imdb'], 'country', 'a.name', false);
		}

		if($this->show_language || $this->all_details) {
			// Show Language
			$match['language'] = $this->get_sub_cat($imdb_info['title_imdb'], 'language', 'a.name', false);
		}


		return $match;
	}

	
	/**
	 * Get information from the tables with relations from the main table
	 *
	 * @param string $id
	 * @param string $type
	 * @param string $added_field
	 * @param string $extra
	 * @return array
	 */
	private function get_sub_cat ($id, $type, $added_field = '', $extra = true) {
		$id = $this->quote_smart($id);
		$query = "SELECT {$added_field}
		FROM imdb_{$type} a 
		INNER JOIN imdb_{$type}_title b ON b.{$type}_id = a.{$type}_id 
		INNER JOIN imdb_title c ON c.movie_id = b.movie_id 
		WHERE c.title_imdb = {$id}";

		$sql = mysql_query ($query);
		if(mysql_num_rows($sql) < 1) {
			return array('Error fetching from DB');
		}
		$return = array();
		while($row = mysql_fetch_assoc($sql)) {
			if(!$extra) {
				$return[] = $row['name'];
			} else {
				$return[] = $row;
			}
		}


		return $return;
	}
	
	/**
	 * Quote-smart. Method for security messures. Prevent SQL-injections
	 *
	 * @param mixed $value
	 * @return mixed
	 */
	private function quote_smart ($value) {

		if( is_array($value) ) {
			return array_map(array($this, "quote_smart"), $value);
		}
		if( get_magic_quotes_gpc() ) {
			$value = stripslashes($value);
		}
		if( $value == '' ) {
			$value = 'NULL';
		} elseif ( !is_numeric($value) || $value[0] == '0' ) {
			$value = "'".mysql_real_escape_string($value)."'";
		}


		return $value;
	}

	/**
	 * Checks of the movie by $title (or id) is cached/saved in MySQL
	 *
	 * @param string $title
	 * @return bool
	 */
	private function saved_movie($title) {

		if(empty($title)) {
			return true;
		}

		$title = $this->quote_smart ($title);
		$query = sprintf ('SELECT COUNT(1) AS numb FROM imdb_title WHERE title_imdb = %1$s OR name = %1$s', $title);
		$mysql = mysql_query ($query);

		if (mysql_result($mysql, 0, 'numb')) {
			return true;
		}

		return false;
	}

	/**
	 * Get the IMDb-ID from title.
	 *
	 * @return array
	 */
	private function get_from_title () {
		$return = array ();


		foreach ($this->from_title as $key => $movie) {

			if(preg_match ('/^tt\d{7}\z/', $movie)) {
				$return[] = $this->get_from_id($movie);
				continue;
			}

			$movie = urlencode(strtolower($movie));
			// Get the source.
			$url = 'http://imdb.com/find?s=tt&q='.$movie;
			if (!$stream = fopen($url, 'r')) {
				$return[] = 'Specify your movie title. Too many results were given.';
				continue;
			}

			$source = stream_get_contents($stream);
			fclose($stream);


			// First try to find results in the search-page (if it's the one we're at)
			$match = array();
			$regex = '/<a href="\/title\/(tt\d{7})\/">([^>]*)<\/a> \((\d{4})(?:.*)\)<\/td><\/tr><\/table>/';
			preg_match ($regex, $source, $match);

			if(isset($match[1])) {

				if($this->activate_mysql_cache) {
					if($this->saved_movie($match[1])) {

						$return[] = $this->mysql_movie_info ($match[1]);
						continue;
					}
				}

				$return[] = $this->get_from_id($match[1]);
				continue;;
			}

			// There is not found any results by the pattern of the search-page.
			// Ergo, we have been sent to the title-page.
			// Fetch the ID. There is no ID near the other info, so it's fetched
			// seperatly...
			$regex_id = '/<a href="\/rg\/title-tease\/recommendations\/title\/(tt\d{7})\/recommendations">Show more recommendations<\/a>/';
			$imdb_id = array ();
			preg_match ($regex_id, $source, $imdb_id);

			if(!count($imdb_id)) {
				$return[] = 'Specify your movie title. Too many results were given.';
				continue;
			}

			if($this->activate_mysql_cache) {
				if($this->saved_movie($imdb_id[1])) {
					$return[] = $this->mysql_movie_info ($imdb_id[1]);
					continue;
				}
			}

			$return[] = $this->get_from_id($imdb_id[1]);
		}

		return $return;
	}

	/**
	 * Process the results etc etc
	 *
	 * @return unknown
	 */
	public function get_result_array () {


		if(!$this->from_title) {
			return 'No results to fetch. Set source options.';
		}

		$results = $this->get_from_title();
		return $results;
	}

}


?>
Return current item: IMDb_Fetch