|
| 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