<?php

/**
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package fusionLib
 * @copyright Copyright (c) 2013 fusionLib. All rights reserved.
 * @link http://fusionlib.com
 */

/**
 * flApiKeyMgr Class
 *
 * Class to generate and manage API keys.
 *
 * @package fusionLib
 */
class flApiKeyMgr {

	/**
	 * The database object.
	 *
	 * @var flDb
	 */
	protected $db;

	/**
	 * Create the object.
	 *
	 * @param flDb $db The database object to use.
	 */
	function __construct($db) {
		$this->db = $db;
	}

	/**
	 * Get all the keys in the system and their descriptions.
	 *
	 * @return array Array of keys to descriptions.
	 */
	function getKeys() {
		$keys = array();
		$result = $this->db->query('SELECT * FROM fl_apikeys');
		while($row = $result->fetch())
			$keys[$row['apikey']] = $row['description'];
		return $keys;
	}

	/**
	 * Create a new API Key.
	 *
	 * @param string $desc The description for the key.
	 * @return bool|string The API key or false if failed.
	 */
	function createKey($desc) {
		$r = $this->db->insert('fl_apikeys', array(
			'apikey' => $k = sha1($desc . uniqid()),
			'description' => $desc
		));
		return $r ? $k : false;
	}

	/**
	 * Get the description of an API Key.
	 *
	 * @param string $key The key to get the description for.
	 * @return string The description.
	 */
	function getKeyDesc($key) {
		return $this->db->fetchOne('SELECT description FROM fl_apikeys WHERE apikey=?', $key);
	}

	/**
	 * Get the roles assigned to a key.
	 *
	 * @param string $key The key to get the roles for.
	 * @return array Array of assigned roles.
	 */
	function getKeyRoles($key) {
		$r = array();
		$result = $this->db->query("SELECT roleid,rolename FROM fl_apikeyroles LEFT JOIN fl_roles USING(roleid) WHERE apikey=?", $key);
		while($row = $result->fetch())
			$r[$row['roleid']] = $row['rolename'];
		return $r;
	}

	/**
	 * Set the roles assigned to a key.
	 *
	 * @param string $key The key to assign roles to.
	 * @param array $roles Array of role IDs to assign to the key.
	 */
	function setKeyRoles($key, $roles) {
		// Delete old roles
		$this->db->delete('fl_apikeyroles', 'apikey=?', $key);

		// Set new roles
		if(count($roles)) {
			$results = $this->db->query('SELECT roleid FROM fl_roles WHERE roleid IN(?' . str_repeat(',?', count($roles) - 1) . ')', $roles);
			while($row = $results->fetch())
				$this->db->insert('fl_apikeyroles', array('apikey' => $key, 'roleid' => $row['roleid']));
		}
	}

	/**
	 * Update the description of an API key.
	 *
	 * @param string $key The key to update.
	 * @param string $desc The new description for the key.
	 * @return bool true if updated; else false.
	 */
	function updateKeyDesc($key, $desc) {
		return $this->db->update('fl_apikeys', array('description' => $desc), 'apikey=?', $key);
	}

	/**
	 * Delete an API Key.
	 *
	 * @param string $key The key to delete.
	 * @return bool true if the key deleted; else false.
	 */
	function deleteKey($key) {
		return $this->db->delete('fl_apikeys', 'apikey=?', $key);
	}
}
