This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/modules/ssl/classes/model/service/plugin/ssl.php

171 lines
4.1 KiB
PHP
Raw Normal View History

2011-12-16 23:31:35 +00:00
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class supports Services
*
* @package OSB
* @subpackage SSL
* @category Models
* @author Deon George
* @copyright (c) 2010 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Model_Service_Plugin_SSL extends Model_Service_Plugin {
protected $_table_name = 'service__ssl';
protected $_updated_column = FALSE;
// Relationships
protected $_belongs_to = array(
'service'=>array(),
);
protected $_has_one = array(
'ssl_ca'=>array('far_key'=>'ssl_ca_id','foreign_key'=>'id'),
);
protected $_display_filters = array(
'csr'=>array(
array('SSL::csrsubject',array(':value')),
),
2011-12-26 13:52:46 +00:00
'cert'=>array(
array('SSL::subject',array(':value')),
),
2011-12-16 23:31:35 +00:00
);
// Required abstract functions
public function service_view() {
return View::factory('service/user/plugin/ssl/view')
->set('so',$this);
}
public function name() {
if ($this->cert) {
return sprintf('%s:%s',$this->ssl_ca->subject(),$this->display('cert'));
} else
return $this->display('csr');
}
public function algorithm() {
return SSL::algorithm($this->cert);
}
public function dn() {
return SSL::dn($this->cert);
}
public function dnissuer() {
return SSL::dnissuer($this->cert);
}
public function issuer() {
return SSL::issuer($this->cert);
2011-12-16 23:31:35 +00:00
}
// @todo This needs to be validated for this model
public function product() {
if ($this->provided_adsl_plan_id)
return $this->adsl_plan;
else
return $this->service->product->plugin();
}
public function details() {
return SSL::details($this->cert);
}
2011-12-26 13:52:46 +00:00
public function valid_from($format=FALSE) {
return SSL::from($this->cert,$format);
2011-12-16 23:31:35 +00:00
}
2011-12-26 13:52:46 +00:00
public function valid_to($format=FALSE) {
return SSL::expire($this->cert,$format);
2011-12-16 23:31:35 +00:00
}
public function serial_num() {
return SSL::serial($this->cert);
}
public function hash() {
return SSL::hash($this->cert);
}
public function version() {
return SSL::version($this->cert);
}
/**
* Get specific service details for use in other modules
* For Example: Invoice
*
* @todo Make the rendered items configurable
* @todo Change this method name, now that it is public
*/
// @todo This needs to be validated for this model
public function _details($type) {
switch ($type) {
case 'invoice_detail_items':
2012-02-22 08:15:46 +00:00
return array();
2011-12-16 23:31:35 +00:00
break;
default:
return parent::$_details($type);
}
}
// @todo This needs to be validated for this model
public function admin_update() {
return View::factory('service/admin/plugin/ssl/update')
->set('mediapath',Route::get('default/media'))
->set('so',$this);
}
public function download_button() {
2011-12-28 04:12:35 +00:00
if (! $this->service->active OR ! preg_match('/client/',$this->service->product->plugin()->extensions) OR $this->valid_to() < time())
2011-12-16 23:31:35 +00:00
return '';
// @todo Do some password validation
$output = Form::open('user/ssl/download');
$output .= Form::hidden('sid',$this->service->id);
$output .= _('Choose a password').': '.Form::password('passwd','').'<br/><br/>';
$output .= Form::submit('download','Download',array('class'=>'form_button'));
return $output;
}
public function cacerts() {
$return = array();
$x = $this->ssl_ca_id;
while ($x) {
$sco = ORM::factory('ssl_ca',$x);
array_push($return,$sco->sign_cert);
$x = $sco->parent_ssl_ca_id;
}
return $return;
}
public function renew() {
$d = SSL::details($this->cert);
$ssl_conf = Kohana::config('ssl');
2012-02-22 08:15:46 +00:00
// @todo change this so an admin can force this.
$force = TRUE;
2011-12-16 23:31:35 +00:00
// If our certificate is not old enough skip
2012-02-22 08:15:46 +00:00
if ($d['validTo_time_t'] > time()+$ssl_conf['min_renew_days']*86400 AND ! $force)
2011-12-16 23:31:35 +00:00
return FALSE;
2012-02-22 08:15:46 +00:00
$res = openssl_csr_sign($this->csr,$this->ssl_ca->sign_cert,$this->ssl_ca->sign_pk,$this->service->product->plugin()->days,array(
2011-12-16 23:31:35 +00:00
'config'=>$ssl_conf['config'],
2012-02-22 08:15:46 +00:00
'x509_extensions'=>$this->service->product->plugin()->extensions,
'digest_alg'=>'sha1',
2011-12-16 23:31:35 +00:00
),time());
2012-02-22 08:15:46 +00:00
if ($res AND openssl_x509_export($res,$cert)) {
2011-12-16 23:31:35 +00:00
$this->cert = $cert;
$this->save();
return TRUE;
} else
throw new Kohana_Exception('Error Creating SSL Certificate :error',array(':error'=>openssl_error_string()));
}
}
?>