<?php
/**
 * Installatron Server Integration API backend.php
 *
 * The example backend.php assumes each user as seen by Installatron
 * also matches a system user. In a case where a "nobody" user envokes
 * Installatron the initSession() method would need to be defined to
 * set itron::$session["user"] to the current user.
 *
 * To install an application, the Installatron GUI can be used, or
 * a command such as this can be executed without the GUI:
 *    /usr/local/installatron/installatron --install --api=json --user user123 --applicaton wordpress --url http://bob.com/somedir --email user@domain.com --db manual --db-host localhost --db-name user123_db01 --db-user user123_db01 --db-pass 938ualkjdk 
 *
 * For further command documentation:
 *  http://installatron.com/developer/integration
 *  http://installatron.com/developer/automation/commandline
 *
 * @modified 2011-08-26
 * @support support@installatron.com
 * @support http://installatron.com/tickets
 */
class i_custom extends i_panel
{
	/**
	 * The control panel name.
	 *
	 * @return  string   Control panel name
	 */
    public function getName()
	{
		return "Your Control Panel";
	}

	/**
	 * The control panel version.
	 *
	 * @return  string   Control panel version
	 */
	public function getVersion()
	{
		return "4.2.1";
	}

	/**
	 * Initialize session for the current user.
	 *
	 * In this example the system user that executes Installatron is also the current user,
	 * so we can omit this method entirely. In a case where a "nobody" user envokes Installatron,
	 * this method would need to be defined to set itron::$session["user"] to the current user.
	 *
	 * @return  bool    true if successful; false otherwise
	 */
#    public function initSession()
#    {
#        if (isset($_SERVER["USER"]))
#        {
#            itron::$session["user"] = $_SERVER["USER"];
#            return true;
#        }
#        return false;
#    }

	/**
	 * Information about a control panel user.
	 *
	 * @param   string  Username
	 * @return  array   User information array
	 */
	public function getUser($u)
	{
		// if it's the admin user set in /usr/local/installatron/etc/settings.ini, just return.
		if ( $u === itron::$ini["admin"] )
		{
			return array(
				"type" => "admin",
				"email" => "admin@host.com"
			);
		}

		//@notes
		// - This method will need to be able to gather information for any system user that can use Installatron.
		// - If each domain owned by this user has its own home directory, "sub_users" will need to be defined for each domain.
		//   See http://installatron.com/developer/integration#4.4
		
		// get POSIX information for given user
		$userinfo = posix_getpwnam($u);
		if ( $userinfo === false )
		{
			return false;
		}
		
		return array(
			"type" => "user",
			"parent" => itron::$ini["admin"],
			"package" => "",
			"system_user" => intval($userinfo["uid"]),
			"system_group" => intval($userinfo["gid"]),

			//@todo replace with this user's email address:
			"email" => "user@domain.com",

			//@todo replace with the path to system account's home directory:
			"path_home" => $userinfo["dir"],

			//@todo replace each with correct values for this user:
			"vhosts" => array(
				"http://bob.com" => "domains/bob.com/httpdocs",
				"http://www.bob.com" => "domains/bob.com/httpdocs",
				"http://sub.bob.com" => "domains/bob.com/httpdocs/sub",
				"http://seconddomain.com" => "domains/seconddomain.com/httpdocs",
				"http://www.seconddomain.com" => "domains/seconddomain.com/httpdocs"
			)

		);

	}

	/**
	 * Databases owned by the effective user.
	 *
	 * @param   string  Database type
	 * @return  array   User's databases
	 */
	public function getDBs($type)
	{
		$u = itron::$session["effective_user"];

		switch($type)
		{
		case "mysql":
			$r = array();
			// YOUR CODE HERE
		}
		return $r;
	}

	/**
	 * Create a database
	 *
	 * + If your panel requires each database name and username
	 * start with the control panel user's username, note that the
	 * incoming $name value does not have the username prepended.
	 *
	 * @param   string  New database name +
	 * @param   string  New database username +
	 * @param   string  New database username's password
	 * @param   string  Database type (must be "mysql")
	 * @param   string  New database host (as originally returned by getUser(effective_user).mysql_host).
	 * @return  bool    true if sucessful; false otherwise
	 */
	public function createDB($name, $user, $pass, $type, $host)
	{
		switch($type)
		{
		case "mysql":
			// YOUR CODE HERE
			return true;
		}
		return false;
	}

	/**
	 * Remove a database.
	 *
	 * If database usernamess are tied to database names,
	 * the database username can also be removed.
	 *
	 * @param   string  Name of database to be removed
	 * @param   string  Database type
	 * @param   string  New database host (as originally returned by getUser(effective_user).mysql_host).
	 * @return  bool    true if sucessful; false otherwise
	 */
	public function removeDB($name, $type, $host)
	{
		switch($type)
		{
		case "mysql":
			// YOUR CODE HERE
			return true;
		}
		return false;
	}

}
?>