Location: PHPKode > scripts > FreecoNet API > freeconet-api/freeconetapi.class.php
<?php


/**
 * Author: Pawel Banasiak {@link http://banasiak.net}
 *
 * YOU USE THIS SCRIPT AT YOUR OWN RESPONSIBILITY AND AT YOUR OWN RISK
 *
 * See {@link https://api.freeconet.pl/documentation/FreeconetRestAPI_1.0.pdf}
 *
 * @package freeconetAPI
 * @author Pawel Banasiak <hide@address.com>
 * @copyright Pawel Banasiak 2008
 * @license BSD
 * @version 0.0.1
 */

class freeconetAPI {
	private $connectionTimeout;
	private $connectionType = 'curl';
	private $userName;
	private $userDomain;
	private $password;
	private $authType;
	private $token = false;
	private $validPeriod;
	private $url = '/RestAPI/V1/execute';
	private $server = 'api.freeconet.pl';
	private $EOL = "\r\n";
	private $errors = '';
	private $isXMLError;
	/**
	 * Constructor.
	 *
	 * @param string $username
	 * @param string $password
	 * @param string $userDomain default: sip.freeconet.pl
	 * @param string $authType default: token
	 * @param int $connectionTimeout default: 15
	 * @param string $connectionType "curl" or "fsockopen" (not safety!) default: curl
	 * @param int $validPeriod default: 60
	 */
	public function __construct() {
		$args = func_num_args();
		if($args == 1)
			$this->reInit(func_get_arg(0));
		elseif($args >= 2 && $args <= 7) {
			$cmd = '$this->init(';
			$params = Array();
			for($i=0;$i<$args;$i++) {
				$params[$i] = func_get_arg($i);
				$cmd .= '$params['.$i.'], ';
			}
			$cmd = substr($cmd, 0, -2);
			$cmd .= ');';
			eval($cmd);
		}
	}
	private function init($userName, $password, $userDomain = 'sip.freeconet.pl', $authType = 'token', $connectionTimeout = 15, $connectionType = 'curl', $validPeriod = 60) {
		try {
			$this->userName = $userName;
			$this->password = $password;
			$this->userDomain = $userDomain;
			$this->authType = 'token';
			$this->connectionTimeout = (int)$connectionTimeout;
			$this->connectionType = ($connectionType == 'fsockopen' ? 'fsockopen' : 'curl');
			$this->validPeriod = (int)$validPeriod;
			$this->getNewToken();
		}
		catch(Exception $e) {
			unset($this);
		}
	}
	private function reInit($token) {
		$this->token = $token;
		if(!$this->checkToken()) {
			$this->token = false;
			$this->getNewToken();
		}
	}
	private function connect($function, $params, $isToken = true, $isSig = true) {
		if($this->token === false && $isToken === true)
			return false;
		$queryString = 'fun=' . urlencode($function);
		if(is_array($params)) {
			foreach($params as $key => $val) {
				$queryString .= '&' . urlencode($key) . '=' . urlencode($val);
			}
		}
		if($isToken) {
			if($this->token)
				$queryString .= '&tokenStr=' . urlencode($this->token);
		}
		if($isSig) {
			$sig = md5(str_replace(array('=', '&'), '', $queryString) . md5($this->password));
			$queryString .= '&sig=' . $sig;

		}
		switch ($this->connectionType) {
			case 'curl' :
				try {
					$ch = curl_init();
					if($ch === false)
						return false;
					curl_setopt($ch, CURLOPT_URL, 'https://' . $this->server . $this->url . '?' . $queryString);
					curl_setopt($ch, CURLOPT_HEADER, false);
					curl_setopt($ch, CURLOPT_TIMEOUT, $this->connectionTimeout);
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
					curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
					$return = curl_exec($ch);
					if(curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200)
						return false;
					return $this->toXML($return);
				}
				catch (Exception $e) {
					return false;
				}
				break;
			case 'fsockopen' :
				try {
					$fp = fsockopen('ssl://' . $this->server, 443, $errno, $errstr, $this->connectionTimeout);
					if($fp === false)
						return false;
					$out = 'GET ' . $this->url . '?' . $queryString . ' HTTP/1.0' . $this->EOL;
					$out .= 'Host: ' . $this->server . $this->EOL;
					$out .= 'Content-Type: application/x-www-form-urlencoded' . $this->EOL;
					$out .= 'Connection: Close' . $this->EOL . $this->EOL;
					$response = '';
					fwrite($fp, $out);
					while (!feof($fp) and $fp !== false) {
						$response .= fgets($fp, 128);
					}
					fclose($fp);
					return $this->toXML(substr($response, strpos($response, $this->EOL . $this->EOL) + strlen($this->EOL . $this->EOL)));
				}
				catch (Exception $e) {
					return false;
				}
				break;
		}
		return false;
	}
	/**
	 * DO NOT USE THIS FUNCTION!
	 *
	 */
	public function xmlError() {
		$this->isXMLError = true;
	}
	private function toXML($xml) {
		try {
			$doc = new DOMDocument();
			$doc->validateOnParse = true;
			$doc->strictErrorChecking = true;
			set_error_handler(Array($this, 'xmlError'));
			$this->isXMLError = false;
			$doc->loadXML($xml);
			if($this->isXMLError)
				return false;
			restore_error_handler();
			return $doc;
		}
		catch(Exception $e) {
			return false;
		}
	}
	/**
	 * Return array with last errors or false on non errors.
	 *
	 * @return array|bool
	 */
	public function getLastErrors() {
		try {
			$els = $this->errors;
			if(!is_object($els))
				return false;
			$return = Array();
			for($i=0;$i<$els->length;$i++) {
				$el = $els->item($i);
				$els2 = $el->getElementsByTagName('*');
				for($i2=0;$i2<$els2->length;$i2++) {
					$el2 = $els2->item($i2);
					if($el2->nodeName == 'code' || $el2->nodeName == 'msg')
						$return[$el2->nodeName] = $el2->nodeValue;
				}
			}
			return $return;
		}
		catch(Exception $e) {
			return false;
		}
	}
	private function isErrors($xml) {
		try {
			$this->errors = '';
			if(!is_object($xml))
				return true;
			$els = $xml->getElementsByTagName('errors');
			if($els->length == 0)
				return false;
			$this->errors = $els;
			return true;
		}
		catch(Exception $e) {
			return true;
		}
	}
	/**
	 *
	 *
	 * @return bool
	 */
	public function checkToken() {
		try {
			$result = $this->connect('checkToken', NULL, true, false);
			if($this->isErrors($result))
				return false;
			return true;
		}
		catch(Exception $e) {
			return false;
		}
	}
	/**
	 * Return actual token or false on error.
	 *
	 * @return string|bool actual token (string) or false on error (bool)
	 */
	public function getToken() {
		try {
			return $this->token;
		}
		catch(Exception $e) {
			return false;
		}
	}
	private function getNewToken() {
		try {
			$params = array(
			'validPeriod' => $this->validPeriod,
			'userName' => $this->userName,
			'password' => $this->password,
			'userDomain' => $this->userDomain,
			'noAttempts' => -1
			);
			$result = $this->connect('getToken', $params, false, false);
			if($this->isErrors($result))
				return false;
			$els = $result->getElementsByTagName('param');
			for($i=0;$i<$els->length;$i++) {
				$el = $els->item($i);
				if($el->getAttribute('name') == 'tokenStr') {
					$this->token = $el->getAttribute('value');
					return true;
				}
			}
			return false;
		}
		catch(Exception $e) {
			return false;
		}
	}
	/**
	 *
	 *
	 * @return array|bool false on error
	 */
	public function getGroupFinAccountInfo() {
		try {
			$result = $this->connect('getGroupFinAccountInfo', NULL);
			if($this->isErrors($result))
				return false;
			$els = $result->getElementsByTagName('param');
			$return = Array();
			for($i=0;$i<$els->length;$i++) {
				$el = $els->item($i);
				$return[$el->getAttribute('name')] = $el->getAttribute('value');
			}
			return $return;
		}
		catch(Exception $e) {
			return false;
		}
	}
	/**
	 *
	 *
	 * @param int $idService
	 * @param string $from
	 * @param string $fromType
	 * @param string $to
	 * @param string $toType
	 * @return array|bool false on error
	 */
	public function makeCall($idService, $from, $fromType, $to, $toType) {
		try {
			$params = Array(
			'idService' => $idService,
			'from' => $from,
			'fromType' => $fromType,
			'to' => $to,
			'toType' => $toType
			);
			$result = $this->connect('makeCall', $params);
			if($this->isErrors($result))
				return false;
			$els = $result->getElementsByTagName('param');
			$return = Array();
			for($i=0;$i<$els->length;$i++) {
				$el = $els->item($i);
				$return[$el->getAttribute('name')] = $el->getAttribute('value');
			}
			return $return;
		}
		catch(Exception $e) {
			return false;
		}
	}
	/**
	 * Return encrypted text or false on error
	 *
	 * @param unknown_type $text
	 * @return string|bol encrypted text (string) or false on error (bool)
	 */
	public function encryptPGP($text) {
		try {
			$params = Array(
			'inputText' => $text
			);
			$result = $this->connect('encryptPGP', $params, false, false);
			if($this->isErrors($result))
				return false;
			$els = $result->getElementsByTagName('param');
			for($i=0;$i<$els->length;$i++) {
				$el = $els->item($i);
				if($el->getAttribute('name') == 'outputStr')
					return $el->getAttribute('value');
			}
			return false;
		}
		catch(Exception $e) {
			return false;
		}
	}
}
?>
Return current item: FreecoNet API