Skip to content
This repository has been archived by the owner on Jun 9, 2018. It is now read-only.

Commit

Permalink
store meta values in cookies on client
Browse files Browse the repository at this point in the history
  • Loading branch information
MartijnR committed Mar 12, 2014
1 parent adf6a86 commit f045832
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Code_Igniter/application/controllers/webform.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function __construct()
parent::__construct();
$this->load->helper(array('subdomain','url', 'form'));
$this->load->model('Survey_model','',TRUE);
$this->load->library('encrypt');
$this->load->library(array('encrypt', 'meta'));
$sub = get_subdomain();
$suf = $this->Survey_model->ONLINE_SUBDOMAIN_SUFFIX;
$this->subdomain = ($this->Survey_model->has_offline_launch_enabled())
Expand All @@ -39,7 +39,7 @@ function __construct()
$this->xsl_version_prev = (isset($form_props['xsl_version'])) ? $form_props['xsl_version'] : NULL;
}
$this->iframe = ( $this->input->get('iframe', TRUE) == 'true' );

if ($this->config->item('auth_support')) {
$this->load->add_package_path(APPPATH.'third_party/form_auth');
}
Expand All @@ -48,6 +48,7 @@ function __construct()
$this->load->add_package_path(APPPATH.'third_party/account');
}
$this->load->library('account');

log_message('debug', 'Webform Controller Initialized');
}

Expand Down Expand Up @@ -295,6 +296,9 @@ private function _get_form()
$this->credentials = $this->form_auth->get_credentials($s);
$this->Form_model->setup($this->server_url, $this->form_id, $this->credentials, $this->form_hash_prev, $this->xsl_version_prev, $this->media_hash_prev);

$uid = ($this->credentials) ? $this->credentials['username'] : NULL;
$this->meta->setMeta($uid);

if($this->Form_model->requires_auth()) {
log_message('debug', "AUTHENTICATION REQUIRED");
$form = new stdClass();
Expand Down
72 changes: 72 additions & 0 deletions Code_Igniter/application/libraries/Meta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Meta Class
*
* Deals with Meta values that are best generated at server
*
* @author Martijn van de Rijdt
* @license see link
* @link https://github.com/MartijnR/enketo
*/
class Meta {

private $CI;
private $cookie_prefix = '__enketo_meta_';

public function __construct()
{
$this->CI =& get_instance();
$this->CI->load->helper('url');
$this->domain = $this->get_domain();
log_message('debug', 'Meta library initialized');
}

public function setMeta($username = NULL) {

log_message('debug', 'setting meta with username:'.$username);
if ($username && !$this->getCookie('uid')) {
$this->setCookie( 'uid', $this->domain.':'.$username);
}

if (!$this->getCookie('deviceid')) {
$this->setCookie('deviceid', $this->domain.':'.$this->generate_deviceid(), TRUE);
}

}

private function setCookie($name, $value, $expire_as_late_as_possible = FALSE )
{
if (empty($name) || empty($value)) {
return;
}
$cookie = array(
'name' => $this->cookie_prefix . $name,
'value' => $value,
'expire' => ($expire_as_late_as_possible) ? 10 * 365 * 24 * 60 * 60 : 7 * 24 * 60 *60,
'domain' => $this->CI->config->item('cookie_domain'),
'path' => $this->CI->config->item('cookie_path'),
'prefix' => $this->CI->config->item('cookie_prefix'),
'secure' => $this->CI->config->item('cookie_secure')
);
$this->CI->input->set_cookie($cookie);
}

private function getCookie($name)
{
return $this->CI->input->cookie($this->cookie_prefix . $name, TRUE);
}

private function generate_deviceid()
{
$this->CI->load->helper('string');
return random_string('alnum', 16);
}

private function get_domain()
{
$base_url = base_url();
return substr( $base_url, strpos($base_url, '://') + 3 );
}
}

/* End of file Meta.php */

0 comments on commit f045832

Please sign in to comment.