Location: PHPKode > scripts > Maribol IMDB > maribol-imdb/maribol-imdb.php
<?php
/*
Plugin Name: Maribol IMDB
Plugin URI: http://www.mlabs.info/maribol-imdb
Description: Maribol IMDB is a wordpress plugin that helps you show info about a movie in your blog posts.
Author: Samuel Marian
Version: 0.3.1
Author URI: http://www.mlabs.info/
*/


	$dir = (defined(__DIR__)) ?  __DIR__ : dirname(__FILE__);
	define('CACHE_PATH', $dir.'/_cache/');
	define('POSTER_CACHE_PATH', $dir.'/_posters/');
	define('MOVIE_CACHE_LIST', CACHE_PATH.'_movie_cache.db');
	
	if(!file_exists(CACHE_PATH)){
		mkdir(CACHE_PATH, 0777);
	}
	
	function MaribolIMDB_doOpenMoviesCache(){
		return (array)@json_decode(@file_get_contents(MOVIE_CACHE_LIST), true);
	}
	
	if(is_admin()){
		include($dir.'/admin.php');
	}
	
	function MaribolIMDB_getMovieDetails($content, $imdb_id, $info){
		$cache_data = MaribolIMDB_check_cache($imdb_id);
		
		$info = (array)$info;
		foreach($info as $info_item):
			if($info_item == 'poster' && strpos($cache_data['poster'], 'http://') === false){
				$content = str_replace('[imdb:poster]', plugins_url().'/maribol-imdb/_posters/'.$cache_data['poster'], $content);
			
			}elseif($info_item == 'poster_small' && strpos($cache_data['poster_small'], 'http://') === false){
				$content = str_replace('[imdb:poster_small]', plugins_url().'/maribol-imdb/_posters/'.$cache_data['poster_small'], $content);
			
			}else{
				$content = str_replace('[imdb:'.$info_item.']', $cache_data[$info_item], $content);
			}
		endforeach;
		
		return $content;
	}
	
	function MaribolIMDB_create_postername($string){
		$url = preg_replace('([^a-z\-\_0-9])', '-', strtolower($string));
		$url = preg_replace('([-]+)', '-', $url);
		$url = trim($url, ' _-');
		$url = $url.time();
		return $url;
	}
	
	function MaribolIMDB_create_cache($imdb_id){
		
		$cache_location = CACHE_PATH.'tt'.$imdb_id.'.db';
		$servers = array(
			'http://www.mlabs.info/api/maribol-imdb/api.php?id=',
			'http://www.maribol.ro/api/maribol-imdb/api.php?id='
		);
		
		$false = true;
		foreach($servers as $id=>$server){
			$json = @file_get_contents($server.$imdb_id);
			if(strlen($json) > 20 ){
				$exist = true;
				break;
			}
		}
		
		if($exist == true){
			$fh = @fopen($cache_location, 'w');
			
			$maribol_imdb_copy_long_desc = get_option('maribol-imdb-copy_long_desc');
			$maribol_imdb_copy_poster = get_option('maribol-imdb-copy_poster');
			
			$open_cache = @json_decode($json, true);
						
			if($maribol_imdb_copy_long_desc == 1){
				unset($open_cache['description']);
			}	
			
			if($maribol_imdb_copy_poster == 1){
				if(!is_dir(POSTER_CACHE_PATH)){
					mkdir(POSTER_CACHE_PATH, 0777);
				}
				if(is_dir(POSTER_CACHE_PATH)){
					if(!is_writable(POSTER_CACHE_PATH)){
						chmod(POSTER_CACHE_PATH, 0777);
					}
					if(is_writable(POSTER_CACHE_PATH)){
						if($open_cache['poster'] != ''){
							$local_file_big = MaribolIMDB_create_postername($open_cache['title'].'-big-').'.'.end(explode('.', $open_cache['poster']));
							$poster_big_copied = copy($open_cache['poster'], POSTER_CACHE_PATH.$local_file_big);
							if($poster_big_copied){
								$open_cache['poster'] = $local_file_big;
							}
							
							$local_file_small = MaribolIMDB_create_postername($open_cache['title'].'-small-').'.'.end(explode('.', $open_cache['poster_small']));
							$poster_small_copied = copy($open_cache['poster_small'], POSTER_CACHE_PATH.$local_file_small);
							if($poster_small_copied){
								$open_cache['poster_small'] = $local_file_small;
							}
						}					
					}
					
				}
			}
			
			@fwrite($fh, json_encode($open_cache));
			@fclose($fh);
		
			$list_cache = array(
				'title' => $open_cache['title'],
				'year' => $open_cache['year'],
				'imdb_id' => $imdb_id,
				'last_cache' => time()
			);
			
			$movies = MaribolIMDB_doOpenMoviesCache();
			$exist = 0;
			foreach($movies as $key=>$movie){
				if($movie['imdb_id'] == $imdb_id){
					$exist = 1;
					$movies[$key] = $list_cache;
					break;
				}
			}
			
			if($exist == 0){
				$movies[] = $list_cache;
			}
			
			$fh = @fopen(MOVIE_CACHE_LIST, 'w');
			@fwrite($fh, @json_encode($movies));
			@fclose($fh);
			
		}
		
		return $json;
	}
	
	function MaribolIMDB_check_cache($imdb_id){
		$cache_location = CACHE_PATH.'tt'.$imdb_id.'.db';
		if(@file_exists($cache_location) && @is_file($cache_location)){
			return @json_decode(file_get_contents($cache_location), true);
		}else{
			return @json_decode(MaribolIMDB_create_cache($imdb_id), true);
		}
	}
	
	function MaribolIMDB_refresh_cache($imdb_id){
		$cache_location = CACHE_PATH.'tt'.$imdb_id.'.db';
		return @json_decode(MaribolIMDB_create_cache($imdb_id), true);
	}
	
	function MaribolIMDB_doCacheNonCachedMovies(){
		if(!file_exists(MOVIE_CACHE_LIST)){
			foreach(glob(CACHE_PATH.'*.db') as $cached_movie_url){
				$movie = @json_decode(@file_get_contents($cached_movie_url), true);
				$movies[] = array(
					'title' => $movie['title'],
					'year' => $movie['year'],
					'imdb_id' => basename($cached_movie_url),
					'last_cache' => time()
				);
			}
			$fh = @fopen(MOVIE_CACHE_LIST, 'w');
			@fwrite($fh, @json_encode($movies));
			@fclose($fh);
		}
	}

	function MaribolIMDB_check_if_imdb_id_exist($content) {
		@preg_match('/\[imdb\]tt([0-9]+)\[\/imdb\]/isU', $content, $imdb_id_exist);
		if(trim($imdb_id_exist[1]) != ''){
			$content = str_replace($imdb_id_exist[0], '', $content);
			@preg_match_all('/\[imdb\:(title|year|description|short_description|poster|poster_small|categories|duration|director|writers|cast|language|release_date|filming_locations|rating)\]/isU', $content, $info_matches);
			if(count($info_matches)>0){
				$content = MaribolIMDB_getMovieDetails($content, $imdb_id_exist[1], $info_matches[1]);
			}
		}
		return $content;
	}
	add_filter('the_content', 'MaribolIMDB_check_if_imdb_id_exist');
	
	// Adding credit for Maribol Labs
	function MaribolIMDB_powered() {
		echo '<span id="maribol_imdb" style="font-size:11px;color:#333;">Powered by <a style="font-size:11px;color:#333;" title="Maribol IMDB" href="http://www.mlabs.info/maribol-imdb">Maribol IMDB</a></span>';
	}
	add_action('wp_footer', 'MaribolIMDB_powered');

?>
Return current item: Maribol IMDB