1<?php 2 3namespace dokuwiki\plugin\bez\mdl; 4 5use dokuwiki\plugin\bez\meta\PermissionDeniedException; 6 7class Authentication_tokenFactory extends Factory { 8 9 public function can_create_token() { 10 return $this->model->get_level() >= BEZ_AUTH_USER; 11 } 12 13 public function get_token($page_id) { 14 $r = $this->model->sqlite->query("SELECT token FROM {$this->get_table_name()} WHERE page_id=?", $page_id); 15 $token = $this->model->sqlite->res2single($r); 16 if (!$token) { 17 return false; 18 } 19 return $token; 20 } 21 22 public function create_token($page_id, $expire_date='') { 23 24 if (!$this->can_create_token()) { 25 throw new PermissionDeniedException(); 26 } 27 28 $token = $this->get_token($page_id); 29 if ($token) { 30 return $token; 31 } 32 33 if ($expire_date == '') { 34 $expire_date = date('c', strtotime('+10 years')); 35 } 36 37 $token = bin2hex(openssl_random_pseudo_bytes(16)); 38 $this->model->sqlite->storeEntry($this->get_table_name(), 39 array('page_id' => $page_id, 40 'token' => $token, 41 'generated_by' => $this->model->user_nick, 42 'generation_date' => date('c'), 43 'expire_date' => $expire_date)); 44 45 return $token; 46 } 47}