|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the TelegramBot package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Longman\TelegramBot; |
|
12
|
|
|
|
|
13
|
|
|
use Exception; |
|
14
|
|
|
use Longman\TelegramBot\Exception\TelegramException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class BotanDB |
|
18
|
|
|
*/ |
|
19
|
|
|
class BotanDB extends DB |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Initialize botan shortener table |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function initializeBotanDb() |
|
25
|
|
|
{ |
|
26
|
|
|
if (!defined('TB_BOTAN_SHORTENER')) { |
|
27
|
|
|
define('TB_BOTAN_SHORTENER', self::$table_prefix . 'botan_shortener'); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Select cached shortened URL from the database |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $url |
|
35
|
|
|
* @param string $user_id |
|
36
|
|
|
* |
|
37
|
|
|
* @return array|bool |
|
38
|
|
|
* @throws TelegramException |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function selectShortUrl($url, $user_id) |
|
41
|
|
|
{ |
|
42
|
|
|
if (!self::isDbConnected()) { |
|
43
|
|
|
return false; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
try { |
|
47
|
|
|
$sth = self::$pdo->prepare(' |
|
48
|
|
|
SELECT `short_url` |
|
49
|
|
|
FROM `' . TB_BOTAN_SHORTENER . '` |
|
50
|
|
|
WHERE `user_id` = :user_id |
|
51
|
|
|
AND `url` = :url |
|
52
|
|
|
ORDER BY `created_at` DESC |
|
53
|
|
|
LIMIT 1 |
|
54
|
|
|
'); |
|
55
|
|
|
|
|
56
|
|
|
$sth->bindValue(':user_id', $user_id); |
|
57
|
|
|
$sth->bindValue(':url', $url); |
|
58
|
|
|
$sth->execute(); |
|
59
|
|
|
|
|
60
|
|
|
return $sth->fetchColumn(); |
|
61
|
|
|
} catch (Exception $e) { |
|
62
|
|
|
throw new TelegramException($e->getMessage()); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Insert shortened URL into the database |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $url |
|
70
|
|
|
* @param string $user_id |
|
71
|
|
|
* @param string $short_url |
|
72
|
|
|
* |
|
73
|
|
|
* @return bool |
|
74
|
|
|
* @throws TelegramException |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function insertShortUrl($url, $user_id, $short_url) |
|
77
|
|
|
{ |
|
78
|
|
|
if (!self::isDbConnected()) { |
|
79
|
|
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
try { |
|
83
|
|
|
$sth = self::$pdo->prepare(' |
|
84
|
|
|
INSERT INTO `' . TB_BOTAN_SHORTENER . '` |
|
85
|
|
|
(`user_id`, `url`, `short_url`, `created_at`) |
|
86
|
|
|
VALUES |
|
87
|
|
|
(:user_id, :url, :short_url, :created_at) |
|
88
|
|
|
'); |
|
89
|
|
|
|
|
90
|
|
|
$sth->bindValue(':user_id', $user_id); |
|
91
|
|
|
$sth->bindValue(':url', $url); |
|
92
|
|
|
$sth->bindValue(':short_url', $short_url); |
|
93
|
|
|
$sth->bindValue(':created_at', self::getTimestamp()); |
|
94
|
|
|
|
|
95
|
|
|
return $sth->execute(); |
|
96
|
|
|
} catch (Exception $e) { |
|
97
|
|
|
throw new TelegramException($e->getMessage()); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|