Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
token.php
1<?php
9namespace Bitrix\Im\Bot;
10
11class Token
12{
13 const CACHE_TOKEN_TTL = 86400;
14 const CACHE_TOKEN_PATH = '/bx/im/token/';
15
16 public static function isActive($botId, $dialogId)
17 {
18 if ($botId == $dialogId)
19 return true;
20
21 $date = new \Bitrix\Main\Type\DateTime();
22
23 $result = self::getFromCache($botId);
24 return $result && $result[$dialogId] && $result[$dialogId]['DATE_EXPIRE'] >= $date->getTimestamp();
25 }
26
27 public static function add($botId, $dialogId)
28 {
29 return self::get($botId, $dialogId, true);
30 }
31
32 public static function get($botId, $dialogId, $prolong = false)
33 {
34 if ($botId == $dialogId)
35 return false;
36
37 $result = self::getFromCache($botId);
38
39 $date = new \Bitrix\Main\Type\DateTime();
40 if (!($result[$dialogId] ?? null) || $result[$dialogId]['DATE_EXPIRE'] < $date->getTimestamp())
41 {
42 $cache = \Bitrix\Main\Data\Cache::createInstance();
43 $cache->clean('token_'.$botId, self::CACHE_TOKEN_PATH);
44
45 $orm = \Bitrix\Im\Model\BotTokenTable::add(Array(
46 'DATE_EXPIRE' => $date->add('10 MINUTES'),
47 'BOT_ID' => $botId,
48 'DIALOG_ID' => $dialogId
49 ));
50 if ($orm->getId() <= 0)
51 {
52 return false;
53 }
54 $addResult = $orm->getData();
55
56 $result[$dialogId] = Array(
57 'ID' => $orm->getId(),
58 'TOKEN' => '',
59 'DIALOG_ID' => $addResult['DIALOG_ID'],
60 'DATE_EXPIRE' => $addResult['DATE_EXPIRE']->getTimestamp()
61 );
62 }
63 else if ($prolong)
64 {
65 $date = new \Bitrix\Main\Type\DateTime();
66 $orm = \Bitrix\Im\Model\BotTokenTable::update($result[$dialogId]['ID'], Array(
67 'DATE_EXPIRE' => $date->add('10 MINUTES')
68 ));
69 if ($orm->isSuccess())
70 {
71 $addResult = $orm->getData();
72 $result[$dialogId]['DATE_EXPIRE'] = $addResult['DATE_EXPIRE']->getTimestamp();
73
74 $cache = \Bitrix\Main\Data\Cache::createInstance();
75 $cache->initCache(self::CACHE_TOKEN_TTL, 'token_'.$botId, self::CACHE_TOKEN_PATH);
76 $cache->startDataCache();
77 $cache->endDataCache($result);
78 }
79 }
80
81 return $result[$dialogId];
82 }
83
84 private static function getFromCache($botId)
85 {
86 $cache = \Bitrix\Main\Data\Cache::createInstance();
87 if($cache->initCache(self::CACHE_TOKEN_TTL, 'token_'.$botId, self::CACHE_TOKEN_PATH))
88 {
89 $result = $cache->getVars();
90 }
91 else
92 {
93 $result = Array();
94 $orm = \Bitrix\Im\Model\BotTokenTable::getList(Array(
95 'filter' => array(
96 '>DATE_EXPIRE' => new \Bitrix\Main\Type\DateTime(),
97 '=BOT_ID' => $botId
98 ),
99 ));
100 while ($token = $orm->fetch())
101 {
102 $result[$token['DIALOG_ID']] = Array(
103 'ID' => $token['ID'],
104 'TOKEN' => $token['TOKEN'],
105 'DIALOG_ID' => $token['DIALOG_ID'],
106 'DATE_EXPIRE' => is_object($token['DATE_EXPIRE'])? $token['DATE_EXPIRE']->getTimestamp(): 0
107 );
108 if ($token['TOKEN'])
109 {
110 $result[$token['TOKEN']] = Array(
111 'ID' => $token['ID'],
112 'TOKEN' => $token['TOKEN'],
113 'DIALOG_ID' => $token['DIALOG_ID'],
114 'DATE_EXPIRE' => is_object($token['DATE_EXPIRE'])? $token['DATE_EXPIRE']->getTimestamp(): 0
115 );
116 }
117 }
118
119 $cache->startDataCache();
120 $cache->endDataCache($result);
121 }
122
123 return $result;
124 }
125}
const CACHE_TOKEN_TTL
Definition token.php:13
const CACHE_TOKEN_PATH
Definition token.php:14
static add($botId, $dialogId)
Definition token.php:27
static isActive($botId, $dialogId)
Definition token.php:16
static get($botId, $dialogId, $prolong=false)
Definition token.php:32