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

Commit f045832

Browse files
committed
1 parent adf6a86 commit f045832

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

Code_Igniter/application/controllers/webform.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function __construct()
2525
parent::__construct();
2626
$this->load->helper(array('subdomain','url', 'form'));
2727
$this->load->model('Survey_model','',TRUE);
28-
$this->load->library('encrypt');
28+
$this->load->library(array('encrypt', 'meta'));
2929
$sub = get_subdomain();
3030
$suf = $this->Survey_model->ONLINE_SUBDOMAIN_SUFFIX;
3131
$this->subdomain = ($this->Survey_model->has_offline_launch_enabled())
@@ -39,7 +39,7 @@ function __construct()
3939
$this->xsl_version_prev = (isset($form_props['xsl_version'])) ? $form_props['xsl_version'] : NULL;
4040
}
4141
$this->iframe = ( $this->input->get('iframe', TRUE) == 'true' );
42-
42+
4343
if ($this->config->item('auth_support')) {
4444
$this->load->add_package_path(APPPATH.'third_party/form_auth');
4545
}
@@ -48,6 +48,7 @@ function __construct()
4848
$this->load->add_package_path(APPPATH.'third_party/account');
4949
}
5050
$this->load->library('account');
51+
5152
log_message('debug', 'Webform Controller Initialized');
5253
}
5354

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

299+
$uid = ($this->credentials) ? $this->credentials['username'] : NULL;
300+
$this->meta->setMeta($uid);
301+
298302
if($this->Form_model->requires_auth()) {
299303
log_message('debug', "AUTHENTICATION REQUIRED");
300304
$form = new stdClass();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2+
/**
3+
* Meta Class
4+
*
5+
* Deals with Meta values that are best generated at server
6+
*
7+
* @author Martijn van de Rijdt
8+
* @license see link
9+
* @link https://github.com/MartijnR/enketo
10+
*/
11+
class Meta {
12+
13+
private $CI;
14+
private $cookie_prefix = '__enketo_meta_';
15+
16+
public function __construct()
17+
{
18+
$this->CI =& get_instance();
19+
$this->CI->load->helper('url');
20+
$this->domain = $this->get_domain();
21+
log_message('debug', 'Meta library initialized');
22+
}
23+
24+
public function setMeta($username = NULL) {
25+
26+
log_message('debug', 'setting meta with username:'.$username);
27+
if ($username && !$this->getCookie('uid')) {
28+
$this->setCookie( 'uid', $this->domain.':'.$username);
29+
}
30+
31+
if (!$this->getCookie('deviceid')) {
32+
$this->setCookie('deviceid', $this->domain.':'.$this->generate_deviceid(), TRUE);
33+
}
34+
35+
}
36+
37+
private function setCookie($name, $value, $expire_as_late_as_possible = FALSE )
38+
{
39+
if (empty($name) || empty($value)) {
40+
return;
41+
}
42+
$cookie = array(
43+
'name' => $this->cookie_prefix . $name,
44+
'value' => $value,
45+
'expire' => ($expire_as_late_as_possible) ? 10 * 365 * 24 * 60 * 60 : 7 * 24 * 60 *60,
46+
'domain' => $this->CI->config->item('cookie_domain'),
47+
'path' => $this->CI->config->item('cookie_path'),
48+
'prefix' => $this->CI->config->item('cookie_prefix'),
49+
'secure' => $this->CI->config->item('cookie_secure')
50+
);
51+
$this->CI->input->set_cookie($cookie);
52+
}
53+
54+
private function getCookie($name)
55+
{
56+
return $this->CI->input->cookie($this->cookie_prefix . $name, TRUE);
57+
}
58+
59+
private function generate_deviceid()
60+
{
61+
$this->CI->load->helper('string');
62+
return random_string('alnum', 16);
63+
}
64+
65+
private function get_domain()
66+
{
67+
$base_url = base_url();
68+
return substr( $base_url, strpos($base_url, '://') + 3 );
69+
}
70+
}
71+
72+
/* End of file Meta.php */

0 commit comments

Comments
 (0)