Author © Net_CZ
<?php

/**
 * 
 * @author Net_CZ
 * @date 11.1.2010
 * 
 * You can use this library freely, but keep my copy, please.
 * 
 * Usage:
 * ======
 * 
 * - first you must ask for your WebAPI key
 * - include this file to your project.
 * - set WSDL url, countryID and apiKey
 *
 */

class Aukro
{
	/*
	 * Variable to store SOAP to
	 */
    public $soap;
    
    /*
     * URL to WSDL
     * eg.: Czech Republic = 'http://webapi.aukro.cz/uploader.php?wsdl'
     */
    private $wsdl = 'http://webapi.aukro.cz/uploader.php?wsdl';
    
    /*
     * Country code
     * eg.: Czech Republic = '56'
     */
    private $countryID = 56;
    
    /*
     * API key
     */
    private $apiKey = '';
    
    /*
     * Default user name
     */
    private $userLogin = '';

    /*
     * Default user password
     */
    private $userPassword = '';
    
    /*
     * Library initialization
     */
    public function __construct()
    {
    	try
    	{
            $this->soap = new SoapClient($this->wsdl); // load WSDL
    	}
    	catch(Exception $e)
    	{
    		echo 'Unable to load WSDL or incorrect WSDL';
    	}
    }
    
    /**
     * Loads system status
     * 
     * @param $sysVar - which feature are you asking for (default - 1 - AllegroWebAPI)
     * @return array | exception message
     */
    public function doQuerySysStatus($sysVar = 1)
    {
        $params = array(
            'sysvar' => $sysVar,
            'country-id' => $this->countryID,
            'webapi-key' => $this->apiKey
        );
        
        try
        {
            return $this->soap->__soapCall('doQuerySysStatus', $params);
        }
        catch(Exception $e)
        {
            return $e->getMessage();
        }
    }

    /**
     * Loads user ID
     * 
     * @param $userLogin - which user ID are asking for
     * @return int | exception message
     */
    public function doGetUserID($userLogin = false)
    {
        $params = array(
            "country-id" => $this->countryID,
            "user-login" => ($userLogin) ? $userLogin : $this->userLogin,
            "webapi-key" => $this->apiKey
        );
        
        try
        {
            return $this->soap->__soapCall("doGetUserID", $params);
        }
        catch(Exception $e)
        {
            return $e->getMessage();
        }
    }
    
    /**
     * Converts string to friendly URL
     * 
     * /based on seo_string function by starenka/
     * 
     * @param $string - URL
     * @return string
     */
    private function __seo_string($string)
    {
    	if(function_exists('iconv'))
    	{
	        $string = preg_replace('~[^\\pL0-9_]+~u', '_', $string);
	        $string = trim($string, "-");
	        $string = iconv("utf-8", "us-ascii//TRANSLIT", $string);
	        $string = strtolower($string);
	        $string = preg_replace('~[^-a-z0-9_]+~', '', $string);
    	}
    	else
    	{
    		$bad = array('Á','Č','Ď','É','Ě','Í','Ň','Ó','Ř','Š','Ť','Ů','Ú','Ý','Ž','á','č','ď','é','ě','í','ň','ó','ř','š','ť','ů','ú','ý','ž');
	        $good = array ('A','C','D','E','E','I','N','O','R','S','T','U','U','Y','Z', 'a','c','d','e','e','i','n','o','r','s','t','u','u','y','z');
	        $string = preg_replace('~[^\\pL0-9_]+~u', '_', $string);
	        $string = trim($string, "-");
	        $string = str_replace($bad,$good,$string);
	        $string = strtolower($string);
	        $string = preg_replace('~[^-a-z0-9_]+~', '', $string);
    	}
	
	    return $string;
     }
    
    /**
     * Loads, converts to array and make some improvements do user items
     * 
     * @param $userID - user ID
     * @return array | exception message
     */
    private function __doGetUserItems($userID)
    {
    	$output = array();
    	
        $params = array(
            "user-id" => $userID,
            "webapi-key" => $this->apiKey,
            "country-id" => $this->countryID
        );
        
        try
        {
            $items = $this->soap->__soapCall("doGetUserItems", $params);
            
            $output["user-item-list"] = array();
            $output["user-item-count"] = $items["user-item-count"];
            
            foreach($items["user-item-list"] as $item)
            {
            	$item = get_object_vars($item);
            	$itHref = $this->__seo_string($item["it-name"]);
            	$item["it-href"] = 'http://www.aukro.cz/item' . $item["it-id"] . '_' . $itHref . '.html';
            	$output["user-item-list"][] = $item;
            }
            
            return $output;
        }
        catch(Exception $e)
        {
            return $e->getMessage();
        }
    }
    
    /**
     * Loads user items
     * 
     * @param $userID - which user items are asking for
     * @return array | exception message
     */
    public function doGetUserItems($userID)
    {
        return $this->__doGetUserItems($userID);
    }
    
    /**
     * Login user
     * 
     * @param $userLogin - user login name
     * @param $userPassword - user password
     * @param $localVersion - API local version
     * @param $encrypted - use encrypted connection
     * @return array | exception message
     */
    public function doLogin($localVersion, $userLogin = false, $userPassword = false, $encrypted = true)
    {
        if(function_exists('hash'))
        {
            //hash used in PHP since version 5.1.3
            $userPassword = hash('sha256', ($userPassword) ? $userPassword : $this->userPassword, true);
        }
        else
        {
            //in older version we should use mhash
            $userPassword = mhash(MHASH_SHA256, $userPassword);
        }
          
        //base64 for encoded binary password transfer
        $userPassword = base64_encode($userPassword);
         
        $params = array(
            "user-login" => ($userLogin) ? $userLogin : $this->userLogin,
            "user-password" => $userPassword,
            "country-code" => $this->countryID,
            "webapi-key" => $this->apiKey,
            "local-version" => $localVersion
        );
        
        try
        {
        	if($encrypted) return $this->soap->__soapCall("doLoginEnc", $params);
        	else return $this->soap->__soapCall("doLogin", $params);
        }
        catch(Exception $e)
        {
            return $e->getMessage();
        }
    }
    
    /**
     * Loads waiting feedbacks
     * 
     * @param $sessionHandle - session handle
     * @param $offset - items offset
     * @param $packageSize - items package size
     * @return array | exception message
     */
    public function doGetWaitingFeedbacks($sessionHandle, $offset = 0, $packageSize = 25)
    {
        $params = array(
            "session-handle" => $sessionHandle,
            "offset" => $offset,
            "package-size" => $packageSize
        );
        
        try
        {
        	return $this->soap->__soapCall("doGetWaitingFeedbacks", $params);
        }
        catch(Exception $e)
        {
            return $e->getMessage();
        }
    }
    
    /**
     * Loads user account data
     * 
     * @param $sessionHandle - session handle
     * @return array | exception message
     */
    public function doGetMyData($sessionHandle)
    {
        $params = array(
            "session-handle" => $sessionHandle
        );
        
        try
        {
            return $this->$this->soap->__soapCall("doGetMyData", $params);
        }
        catch(Exception $e)
        {
            return $e->getMessage();
        }
    }
    
    /**
     * Loads user billing information
     * 
     * @param $sessionHandle - session handle
     * @return array | exception message
     */
    public function doMyBilling($sessionHandle)
    {
        $params = array(
            "session-handle" => $sessionHandle
        );
        
        try
        {
            return $this->soap->__soapCall("doMyBilling", $params);
        }
        catch(Exception $e)
        {
            return $e->getMessage();
        }
    }
    
    /**
     * Loads user page
     * 
     * @param $userID - user ID
     * @return string | exception message
     */
    public function doShowUserPage($userID)
    {
        $params = array(
            "webapi-key" => $this->apiKey,
            "country-id" => $this->countryID,
            "user-id" => $userID
        );
        
        try
        {
            return $this->soap->__soapCall("doShowUserPage", $params);
        }
        catch(Exception $e)
        {
            return $e->getMessage();
        }
    }
}