<?php
/**
 * Wrapper class around the Minube (http://www.minube.com) API for PHP
 * @author Alberto Varela <alberto@berriart.com>
 * @author_url http://www.berriart.com
 * @class_url http://www.berriart.com/minube-php/
 * @version 0.1
 * 
 * Make what you want with this class, but if you redistribute it keep the credits please ;)
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY.
 */

class minube{
    
/**
     * minube API URL
     * @var string
     */
    
var $api_url "http://api.minube.com/";

    
/**
     * @var string
     */
    
var $api_key='';

    
/**
     * @var array
     */
    
var $responseInfo=array();

    
/**
     * @var boolean
     */
     
var $debug false;

    
/**
     * Constructor, sets the API key
     */
    
function minube$api_key )
    {
        
$this->api_key $api_key;
    }
    
    
/**
     * Get countries
     * $id = integer string Optional. Could be the ID or the name of a country
     * $order = string Optional. Order results by... [pois_count*, name, id, full_count, see_count, restaurant_count, blog_count, hotel_count]
     * $limit = integer Optional. Results limit
    * @return SimpleXMLElement Object
    **/
    
function getCountries$id=false$order=false$limit=false )
    {
        
$method 'locations/countries';
        
$parameters = array();
        
        if(
$id) {
            if(
is_numeric($id)) {
                
$parameters['id'] = $id;
            }
            else {
                
$parameters['name'] = $id;
            }
        }
        if(
$order) {
            
$parameters['order'] = $order;
        }
        if(
$limit) {
            
$parameters['limit'] = $limit;
        }
        
        if( empty(
$parameters) ) {
            
$parameters false;
        }

        return 
$this->process($method$parameters);
    }
    
    
/**
     * Get cities
     * $id = integer string Optional. Could be the ID or the name of a city
     * $country = integer string Optional. Could be the ID or the name of a country
     * $order = string Optional. Order results by... [pois_count*, name, id, full_count, see_count, restaurant_count, blog_count, hotel_count]
     * $limit = integer Optional. Results limit
    * @return SimpleXMLElement Object
    **/
    
function getCities$id=false$country=false$order=false$limit=false )
    {
        
$method 'locations/cities';
        
$parameters = array();
        
        if(
$id) {
            if(
is_numeric($id)) {
                
$parameters['id'] = $id;
            }
            else {
                
$parameters['name'] = $id;
            }
        }
        if(
$country) {
            
$parameters['country'] = $country;
        }
        if(
$order) {
            
$parameters['order'] = $order;
        }
        if(
$limit) {
            
$parameters['limit'] = $limit;
        }
        
        if( empty(
$parameters) ) {
            
$parameters false;
        }

        return 
$this->process($method$parameters);
    }
    
    
/**
     * Get categories
     * $id = integer string Optional. Could be the ID or the name of a category
     * $order = string Optional. Order results by... [name*, id, group]
     * $limit = integer Optional. Results limit
    * @return SimpleXMLElement Object
    **/
    
function getCategories$id=false$order=false$limit=false )
    {
        
$method 'places/categories';
        
$parameters = array();
        
        if(
$id) {
            if(
is_numeric($id)) {
                
$parameters['id'] = $id;
            }
            else {
                
$parameters['name'] = $id;
            }
        }
        if(
$order) {
            
$parameters['order'] = $order;
        }
        if(
$limit) {
            
$parameters['limit'] = $limit;
        }
        
        if( empty(
$parameters) ) {
            
$parameters false;
        }

        return 
$this->process($method$parameters);
    }

    
/**
     * Get rincones
     * $id = integer Optional. Could be the ID of a rincón
     * $city = integer string Optional. Could be the ID or the name of a city
     * $country = integer string Optional. Could be the ID or the name of a country
     * $category = integer string Optional. Could be the ID or the name of a category
     * $category_group = string Optional. Could be a category_group
     * $order = string Optional. Order results by... [pois_count*, name, id, full_count, see_count, restaurant_count, blog_count, hotel_count]
     * $limit = integer Optional. Results limit
    * @return SimpleXMLElement Object
    **/
    
function getPois$id=false$city=false$country=false$category=false$category_group=false$order=false$limit=false )
    {
        
$method 'places/pois';
        
$parameters = array();
        
        if(
$id) {
            
$parameters['id'] = $id;
        }
        if(
$city) {
            
$parameters['city'] = $city;
        }
        if(
$country) {
            
$parameters['country'] = $country;
        }
        if(
$category) {
            
$parameters['category'] = $category;
        }
        if(
$category_group) {
            
$parameters['category_group'] = $category_group;
        }
        if(
$order) {
            
$parameters['order'] = $order;
        }
        if(
$limit) {
            
$parameters['limit'] = $limit;
        }
        
        if( empty(
$parameters) ) {
            
$parameters false;
        }

        return 
$this->process($method$parameters);
    }    

    
/**
     * Get comments
     * $id = integer Optional. Could be the ID off a comment
     * $user = integer string Optional. Could be the ID or the name of a user
     * $poi = integer Optional. Could be the ID of a rincon
     * $category = integer string Optional. Could be the ID or the name of a category
     * $category_group = string Optional. Could be a category_group
     * $country = integer string Optional. Could be the ID or the name of a country
     * $city = integer string Optional. Could be the ID or the name of a city
     * $order = string Optional. Order results by... [date*, id]
     * $limit = integer Optional. Results limit
    * @return SimpleXMLElement Object
    **/
    
function getComments$id=false$user=false$poi=false$category=false$category_group=false$country=false$city=false$order=false$limit=false )
    {
        
$method 'places/comments';
        
$parameters = array();
        
        if(
$id) {
            
$parameters['id'] = $id;
        }
        if(
$user) {
            
$parameters['user'] = $user;
        }
        if(
$poi) {
            
$parameters['poi'] = $poi;
        }
        if(
$city) {
            
$parameters['city'] = $city;
        }
        if(
$country) {
            
$parameters['country'] = $country;
        }
        if(
$category) {
            
$parameters['category'] = $category;
        }
        if(
$category_group) {
            
$parameters['category_group'] = $category_group;
        }
        if(
$order) {
            
$parameters['order'] = $order;
        }
        if(
$limit) {
            
$parameters['limit'] = $limit;
        }
        
        if( empty(
$parameters) ) {
            
$parameters false;
        }

        return 
$this->process($method$parameters);
    }
    
    
/**
     * Get nearby
     * $id = integer. Must be the ID of a rincon
     * $category = integer string Optional. Could be the ID or the name of a category
     * $category_group = string Optional. Could be a category_grou
     * $order = string Optional. Order results by... [views*, id, date, comments, distance]
     * $limit = integer Optional. Results limit
    * @return SimpleXMLElement Object
    **/
    
function getNearby$id,  $category=false$category_group=false$order=false$limit=false )
    {
        
$method 'places/nearby';
        
$parameters = array();
        
        if(
$id) {
            
$parameters['id'] = $id;
        }
        if(
$category) {
            
$parameters['category'] = $category;
        }
        if(
$category_group) {
            
$parameters['category_group'] = $category_group;
        }
        if(
$order) {
            
$parameters['order'] = $order;
        }
        if(
$limit) {
            
$parameters['limit'] = $limit;
        }
        
        if( empty(
$parameters) ) {
            
$parameters false;
        }

        return 
$this->process($method$parameters);
    }
    
    
/**
     * Get pictures
     * $id = integer Optional. Could be the ID off a picture
     * $poi = integer Optional. Could be the ID of a rincon
     * $country = integer string Optional. Could be the ID or the name of a country
     * $city = integer string Optional. Could be the ID or the name of a city
     * $category = integer string Optional. Could be the ID or the name of a category
     * $order = string Optional. Order results by... [date*, id, views, comments]
     * $limit = integer Optional. Results limit
    * @return SimpleXMLElement Object
    **/
    
function getPictures$id=false$poi=false$country=false$city=false$category=false$order=false$limit=false )
    {
        
$method 'media/pictures';
        
$parameters = array();
        
        if(
$id) {
            
$parameters['id'] = $id;
        }
        if(
$poi) {
            
$parameters['poi'] = $poi;
        }
        if(
$city) {
            
$parameters['city'] = $city;
        }
        if(
$country) {
            
$parameters['country'] = $country;
        }
        if(
$category) {
            
$parameters['category'] = $category;
        }
        if(
$order) {
            
$parameters['order'] = $order;
        }
        if(
$limit) {
            
$parameters['limit'] = $limit;
        }
        
        if( empty(
$parameters) ) {
            
$parameters false;
        }

        return 
$this->process($method$parameters);
    }    
    
    
/**
     * Get picture comments
     * $id = integer Optional. Could be the ID off a comment
     * $picture_id = integer Optional. Could be the ID off a picture
     * $order = string Optional. Order results by... [date*, id, picture_id]
     * $limit = integer Optional. Results limit
    * @return SimpleXMLElement Object
    **/
    
function getPictureComments$id=false,  $picture_id=false$order=false$limit=false )
    {
        
$method 'media/picture_comments';
        
$parameters = array();
        
        if(
$id) {
            
$parameters['id'] = $id;
        }
        if(
$picture_id) {
            
$parameters['picture_id'] = $picture_id;
        }
        if(
$order) {
            
$parameters['order'] = $order;
        }
        if(
$limit) {
            
$parameters['limit'] = $limit;
        }
        
        if( empty(
$parameters) ) {
            
$parameters false;
        }

        return 
$this->process($method$parameters);
    }    
    
    
/**
     * Get videos
     * $id = integer Optional. Could be the ID off a picture
     * $poi = integer Optional. Could be the ID of a rincon
     * $country = integer string Optional. Could be the ID or the name of a country
     * $city = integer string Optional. Could be the ID or the name of a city
     * $category = integer string Optional. Could be the ID or the name of a category
     * $order = string Optional. Order results by... [date*, id, views]
     * $limit = integer Optional. Results limit
    * @return SimpleXMLElement Object
    **/
    
function getVideos$id=false$poi=false$country=false$city=false$category=false$order=false$limit=false )
    {
        
$method 'media/videos';
        
$parameters = array();
        
        if(
$id) {
            
$parameters['id'] = $id;
        }
        if(
$poi) {
            
$parameters['poi'] = $poi;
        }
        if(
$city) {
            
$parameters['city'] = $city;
        }
        if(
$country) {
            
$parameters['country'] = $country;
        }
        if(
$category) {
            
$parameters['category'] = $category;
        }
        if(
$order) {
            
$parameters['order'] = $order;
        }
        if(
$limit) {
            
$parameters['limit'] = $limit;
        }
        
        if( empty(
$parameters) ) {
            
$parameters false;
        }

        return 
$this->process($method$parameters);
    }    
    
    
/**
     * Get video comments
     * $id = integer Optional. Could be the ID off a comment
     * $video_id = integer Optional. Could be the ID off a video
     * $order = string Optional. Order results by... [date*, id, video_id]
     * $limit = integer Optional. Results limit
    * @return SimpleXMLElement Object
    **/
    
function getVideoComments$id=false,  $video_id=false$order=false$limit=false )
    {
        
$method 'media/video_comments';
        
$parameters = array();
        
        if(
$id) {
            
$parameters['id'] = $id;
        }
        if(
$video_id) {
            
$parameters['video_id'] = $video_id;
        }
        if(
$order) {
            
$parameters['order'] = $order;
        }
        if(
$limit) {
            
$parameters['limit'] = $limit;
        }
        
        if( empty(
$parameters) ) {
            
$parameters false;
        }

        return 
$this->process($method$parameters);
    }
    
    
/**
     * Get buzz flights 
     * $source_city = integer string Optional. Could be the ID or the name of a city
     * $source_country = integer string Optional. Could be the ID or the name of a country
     * $destination_city = integer string Optional. Could be the ID or the name of a city
     * $destination_country = integer string Optional. Could be the ID or the name of a country
     * $order = string Optional. Order results by... [best_price*, depart_date, return_date]
     * $limit = integer Optional. Results limit
    * @return SimpleXMLElement Object
    **/
    
function getBuzzFlights$source_city=false$source_country=false$destination_city=false$destination_country=false$order=false$limit=false )
    {
        
$method 'buzz/flights';
        
$parameters = array();
        
        if(
$source_city) {
            
$parameters['source_city'] = $source_city;
        }
        if(
$source_country) {
            
$parameters['source_country'] = $source_country;
        }
        if(
$destination_city) {
            
$parameters['destination_city'] = $destination_city;
        }
        if(
$destination_country) {
            
$parameters['destination_country'] = $destination_country;
        }
        if(
$order) {
            
$parameters['order'] = $order;
        }
        if(
$limit) {
            
$parameters['limit'] = $limit;
        }
        
        if( empty(
$parameters) ) {
            
$parameters false;
        }

        return 
$this->process($method$parameters);
    }

    
/**
     * Make a request manually, choose de method and pass the parameters
     * $method = string. Must be the request method
     * $parameters = array Optional. You could pass the parameters as an array (key=>value)
     * @return SimpleXMLElement Object
     */    
    
function request($method$parameters=false) {
        if( empty(
$parameters) ) {
            
$parameters false;
        }
        return 
$this->process($method$parameters);
    }
    
    
/**
     * Internal function where all the juicy curl fun takes place
     * this should not be called by anything external unless you are
     * doing something else completely then knock youself out.
     * @access private
     * @param string $url Required. API URL to request
     */
    
function process($method$parameters false)
    {
        
$url $this->api_url $method '.xml?api_key=' urlencode($this->api_key);
        
        if( 
is_array($parameters) ) {
            foreach(
$parameters as $key=>$value) {
                
$url .= '&' $key '=' urlencode($value);
            }
        }
    
        
$ch curl_init($url);

            
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
            
curl_setopt($chCURLOPT_VERBOSE1);
            
curl_setopt($chCURLOPT_NOBODY0);
            if( 
$this->debug ) {
                
curl_setopt($chCURLOPT_HEADERtrue);
            }
            else {
                
curl_setopt($chCURLOPT_HEADERfalse);
            }
            @
curl_setopt($chCURLOPT_FOLLOWLOCATION,1);

            
$response curl_exec($ch);
            
            
$this->responseInfo=curl_getinfo($ch);
            
curl_close($ch);
            
            if( 
$this->debug ) {
                
$debug preg_split("#\n\s*\n|\r\n\s*\r\n#m"$response);
                echo
'<pre>' $debug[0] . '</pre>'
                exit;
            }
            
            if( 
intval$this->responseInfo['http_code'] ) == 200 ) {
            if( 
function_exists('simplexml_load_string') ) {
                
$obj simplexml_load_string$response );
                return 
$obj;                
            }
            else {
                return 
$response;
            }
        }
            else {
            return 
false;
        }
    }

}

?>