<?php defined('SYSPATH') or die('No direct access allowed.');

/**
 * This class is for providing password from a password genarator
 *
 * @package    lnApp
 * @category   lnApp/Helpers
 * @author     Deon George
 * @copyright  (c) 2009-2013 Deon George
 * @license    http://dev.leenooks.net/license.html
 */
abstract class lnApp_PWgen {
	public static function get($pwonly=TRUE) {
		if (! Kohana::$config->load('pwgen')->host OR ! Kohana::$config->load('pwgen')->port)
			throw new Kohana_Exception('No configuration for host or port (:host/:port)',array(':host'=>Kohana::$config->load('pwgen')->host,':port'=>Kohana::$config->load('pwgen')->port));

		$ps = socket_create(AF_INET,SOCK_STREAM,0);
		if (! socket_connect($ps,Kohana::$config->load('pwgen')->host,Kohana::$config->load('pwgen')->port))
			throw new Kohana_Exception('Unable to connect to password server');

		// echo "Reading response:\n\n";
		$pw = '';
		while ($out = socket_read($ps,64))
			$pw .= rtrim($out);

		// echo "Closing socket...";
		socket_close ($ps);

		list($passwd,$passwdSay) = explode(' ',$pw);
		// print " Password [$passwd] ($passwdSay) [$pw] ".md5($passwd)."<BR>";

		return $pwonly ? $passwd : array('pw'=>$passwd,'say'=>$passwdSay);
	}
}
?>
