SSL, Google Chart updates, lnAPP improvements

This commit is contained in:
Deon George
2012-12-11 08:48:30 +11:00
parent c4e760fa21
commit 6588de4f7f
67 changed files with 940 additions and 539 deletions

View File

@@ -0,0 +1,220 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class extends Kohana's [ORM] class to create defaults for OSB.
*
* @package OSB
* @subpackage Core
* @category ORM
* @author Deon George
* @copyright (c) 2010 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
abstract class ORM_OSB extends ORM {
/**
* @var string Database to connect to
*/
protected $_db = 'default';
protected $_created_column = array('column'=>'date_orig','format'=>TRUE);
protected $_updated_column = array('column'=>'date_last','format'=>TRUE);
// Our attribute values that need to be stored as serialized
protected $_serialize_column = array();
// Our attributes used in forms.
protected $_form = array();
// Rules to assist with site ID and getting next record ID for inserts.
public function rules() {
return array(
'id'=>array(
array('ORM_OSB::get_next_id',array(':model',':field')),
),
'site_id'=>array(
array('ORM_OSB::set_site_id',array(':model',':field')),
),
);
}
/**
* This function will enhance the [Validate::filter], since it always passes
* the value as the first argument and sometimes functions need that to not
* be the first argument.
*
* Currently this implements:
* [date()][date-ref]
*
* [date-ref]: http://www.php.net/date
*
* This function will throw an exception if called without a function
* defined.
*
* @param mixed $val Value to be processed
* @param string $func Name of function to call
* @param string $arg Other arguments for the function
* @todo This has probably changed in KH 3.1
*/
final public static function _filters($val,$func,$arg) {
switch ($func) {
case 'date':
return date($arg,$val);
default:
throw new Exception(sprintf(_('Unknown function: %s (%s,%s)'),$func,$arg,$val));
}
}
final public static function form($table,$blank=FALSE) {
return ORM::factory($table)->formselect($blank);
}
/**
* Get Next record id
*
* @param array Validate object
* @param string Primary Key
*/
public static function get_next_id($model,$field) {
if (! is_null($model->$field))
return TRUE;
$model->_changed[$field] = $field;
$ido = ORM::factory('module')
->where('name','=',$model->_table_name)
->find();
if (! $ido->loaded())
throw new Kohana_Exception('Problem getting record_id for :table',array(':table'=>$model->_table_name));
$model->$field = $ido->record_id->next_id($ido->id);
return TRUE;
}
/**
* Set the site ID attribute for each row update
*/
public static function set_site_id($model,$field) {
if (! is_null($model->$field))
return TRUE;
$model->_changed[$field] = $field;
$model->$field = Config::siteid();
return TRUE;
}
public function __get($column) {
if (array_key_exists($column,$this->_table_columns)) {
// If the column is a blob, we'll decode it automatically
if (
$this->_table_columns[$column]['data_type'] == 'blob'
AND ! is_null($this->_object[$column])
AND ! isset($this->_changed[$column])
AND (! isset($this->_table_columns[$column]['auto_convert']) OR ! $this->_table_columns[$column]['auto_convert'])
) {
// In case our blob hasnt been saved as one.
try {
$this->_object[$column] = $this->blob($this->_object[$column]);
}
catch(Exception $e) {
// @todo Log this exception
echo Kohana_Exception::text($e), "\n";
echo debug_print_backtrace();
}
$this->_table_columns[$column]['auto_convert'] = TRUE;
}
// If the column is a serialized object, we'll unserialize it.
if (
in_array($column,$this->_serialize_column)
AND is_string($this->_object[$column])
AND ! is_null($this->_object[$column])
AND ! isset($this->_changed[$column])
AND (! isset($this->_table_columns[$column]['unserialized']) OR ! $this->_table_columns[$column]['unserialized'])
) {
// In case our object hasnt been saved as serialized.
try {
$this->_object[$column] = unserialize($this->_object[$column]);
}
catch(Exception $e) {
// @todo Log this exception
echo Kohana_Exception::text($e), "\n";
echo debug_print_backtrace();
}
$this->_table_columns[$column]['unserialized'] = TRUE;
}
}
return parent::__get($column);
}
public function formselect($blank) {
$result = array();
if ($blank)
$result[] = '';
foreach ($this->find_all() as $o)
$result[$o->{$this->_form['id']}] = $o->{$this->_form['value']};
return $result;
}
public function keyget($column,$key=NULL) {
if (is_null($key) OR ! is_array($this->$column))
return $this->$column;
else
return array_key_exists($key,$this->$column) ? $this->{$column}[$key] : NULL;
}
public function save(Validation $validation = NULL) {
// Find any fields that have changed, and process them.
if ($this->_changed)
foreach ($this->_changed as $c)
// Any fields that are blobs, and encode them.
if ($this->_table_columns[$c]['data_type'] == 'blob') {
$this->_object[$c] = $this->blob($this->_object[$c],TRUE);
// We need to reset our auto_convert flag
if (isset($this->_table_columns[$c]['auto_convert']))
$this->_table_columns[$c]['auto_convert'] = FALSE;
// Any fields that should be seriailzed, we'll do that.
} elseif (is_array($this->_object[$c]) AND in_array($c,$this->_serialize_column)) {
$this->_object[$c] = serialize($this->_object[$c]);
}
return parent::save($validation);
}
/**
* Retrieve and Store DB BLOB data.
*/
private function blob($data,$set=FALSE) {
return $set ? gzcompress(serialize($data)) : unserialize(gzuncompress($data));
}
public function config($key) {
$mc = Config::instance()->so->module_config($this->_object_name);
return empty($mc[$key]) ? '' : $mc[$key];
}
public function list_active() {
return $this->_where_active()->find_all();
}
public function list_count($active=TRUE) {
$a=($active ? $this->_where_active() : $this);
return $a->find_all()->count();
}
}
?>

View File

@@ -1,4 +1,138 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides login capability
*
* @package OSB
* @subpackage Page/Login
* @category Controllers
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
* @also [logout]
*/
class Controller_Login extends lnApp_Controller_Login {}
class Controller_Login extends lnApp_Controller_Login {
public function action_register() {
// If user already signed-in
if (Auth::instance()->logged_in()!= 0) {
// Redirect to the user account
HTTP::redirect('welcome/index');
}
// Instantiate a new user
$account = ORM::factory('account');
// If there is a post and $_POST is not empty
if ($_POST) {
// Check Auth
$status = $account->values($_POST)->check();
if (! $status) {
foreach ($account->validation()->errors('form/register') as $f => $r) {
// $r[0] has our reason for validation failure
switch ($r[0]) {
// Generic validation reason
default:
SystemMessage::add(array(
'title'=>_('Validation failed'),
'type'=>'error',
'body'=>sprintf(_('The defaults on your submission were not valid for field %s (%s).'),$f,$r)
));
}
}
}
$ido = ORM::factory('module')
->where('name','=','account')
->find();
$account->id = $ido->record_id->next_id($ido->id);
// Save the user details
if ($account->save()) {}
}
SystemMessage::add(array(
'title'=>_('Already have an account?'),
'type'=>'info',
'body'=>_('If you already have an account, please login..')
));
Block::add(array(
'title'=>_('Register'),
'body'=>View::factory('register')
->set('account',$account)
->set('errors',$account->validation()->errors('form/register')),
));
$this->template->left = HTML::anchor('login','Login').'...';
}
/**
* Enable user password reset
*/
public function action_reset() {
// Minutes to keep our token
$token_expire = 15;
// If user already signed-in
if (Auth::instance()->logged_in()!= 0) {
// Redirect to the user account
HTTP::redirect('welcome/index');
}
// If the user posted their details to reset their password
if ($_POST) {
// If the username is correct, create a method token
if (! empty($_POST['username']) AND ($ao=ORM::factory('account',array('username'=>$_POST['username']))) AND $ao->loaded()) {
$mmto = ORM::factory('module_method_token')
->method(array('account','user_resetpassword'))
->account($ao)
->uses(2)
->expire(time()+$token_expire*60);
if ($mmto->generate()) {
// Send our email with the token
// @todo Need to provide an option if Email_Template is not installed/activited.
// @todo Need to provide an option if account_reset_password template doesnt exist.
$et = Email_Template::instance('account_reset_password');
$et->to = array('account'=>array($mmto->account_id));
$et->variables = array(
'SITE'=>URL::base(TRUE,TRUE),
'SITE_ADMIN'=>Config::sitename(),
'SITE_NAME'=>Config::sitename(),
'TOKEN'=>$mmto->token,
'TOKEN_EXPIRE_MIN'=>$token_expire,
'USER_NAME'=>sprintf('%s %s',$mmto->account->first_name,$mmto->account->last_name),
);
$et->send();
// Log the password reset
$ao->log('Password reset token sent');
}
// Redirect to our password reset, the Auth will validate the token.
} elseif (! empty($_REQUEST['token'])) {
HTTP::redirect(sprintf('user/account/resetpassword?token=%s',$_REQUEST['token']));
}
// Show our token screen even if the email was invalid.
if (isset($_POST['username']))
Block::add(array(
'title'=>_('Reset your password'),
'body'=>View::factory('login_reset_sent'),
'style'=>array('css/login.css'=>'screen'),
));
else
HTTP::redirect('login');
} else {
Block::add(array(
'title'=>_('Reset your password'),
'body'=>View::factory('login_reset'),
'style'=>array('css/login.css'=>'screen'),
));
}
}
}
?>

View File

@@ -3,7 +3,7 @@
/**
* This class provides the default template controller for rendering pages.
*
* @package lnApp
* @package OSB
* @subpackage Page/Template
* @category Controllers
* @author Deon George

View File

@@ -1,9 +1,9 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* lnApp Main home page
* OSB Main home page
*
* @package lnApp
* @package OSB
* @subpackage Page/Home
* @category Controllers
* @author Deon George

View File

@@ -64,7 +64,6 @@ $(function () {
id = data.rslt.obj.attr(\'id\').substr(a+1);
if (href = $("#N_"+id).attr("href")) {
if (! use_ajax) {
window.location = href;
return;

View File

@@ -40,6 +40,9 @@ abstract class lnApp_Script extends HTMLRender {
case 'file':
$foutput .= HTML::script($mediapath->uri(array('file'=>$value['data'])));
break;
case 'src':
$foutput .= HTML::script($value['data']);
break;
case 'stdin':
$soutput .= sprintf("<script type=\"text/javascript\">//<![CDATA[\n%s\n//]]></script>",$value['data']);
break;

View File

@@ -10,7 +10,7 @@
* @copyright (c) 2010 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Model_Account_Log extends ORMOSB {
class Model_Account_Log extends ORM_OSB {
protected $_belongs_to = array(
'account'=>array(),
);

View File

@@ -1,7 +1,7 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* @package lnApp
* @package OSB
* @subpackage Auth
* @category Models
* @author Deon George

View File

@@ -10,7 +10,7 @@
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Model_Country extends ORMOSB {
class Model_Country extends ORM_OSB {
public function currency() {
return ORM::factory('currency')->where('country_id','=',$this->id)->find();
}

View File

@@ -10,6 +10,6 @@
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Model_Currency extends ORMOSB {
class Model_Currency extends ORM_OSB {
}
?>

View File

@@ -10,7 +10,7 @@
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Model_Group_Method extends ORMOSB {
class Model_Group_Method extends ORM_OSB {
// Relationships
protected $_has_one = array(
'record_id'=>array(),

View File

@@ -10,7 +10,7 @@
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Model_Language extends ORMOSB {
class Model_Language extends ORM_OSB {
protected $_form = array('id'=>'id','value'=>'name');
}
?>

View File

@@ -13,7 +13,7 @@
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Model_Module extends ORMOSB {
class Model_Module extends ORM_OSB {
// Relationships
protected $_has_many = array(
'module_method'=>array('far_key'=>'id'),

View File

@@ -10,7 +10,7 @@
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Model_Module_Method extends ORMOSB {
class Model_Module_Method extends ORM_OSB {
// Relationships
protected $_belongs_to = array(
'module'=>array(),

View File

@@ -10,7 +10,7 @@
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Model_Module_Method_Token extends ORMOSB {
class Model_Module_Method_Token extends ORM_OSB {
// Relationships
protected $_belongs_to = array(
'account'=>array(),

View File

@@ -8,7 +8,7 @@
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Model_Record_Id extends ORMOSB {
class Model_Record_Id extends ORM_OSB {
protected $_primary_key = 'module_id';
// This module doesnt keep track of column updates automatically

View File

@@ -13,7 +13,7 @@
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Model_Setup extends ORMOSB {
class Model_Setup extends ORM_OSB {
// Setup doesnt use the update column
protected $_updated_column = FALSE;

View File

@@ -10,9 +10,9 @@
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class ORM extends Kohana_ORM {
protected $_table_names_plural = false;
protected $_model_names_plural = false;
abstract class ORM extends Kohana_ORM {
protected $_table_names_plural = FALSE;
protected $_model_names_plural = FALSE;
private $_object_formated = array();
private $_formated = FALSE;
// Our filters used to display values in a friendly format