This repository has been archived by the owner on Jun 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
store meta values in cookies on client
#290, https://github.com/MartijnR/enketo-core/issues/159, https://github.com/MartijnR/enketo-core/issues/35
- Loading branch information
Showing
2 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |