-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib_utility.php
138 lines (116 loc) · 3.21 KB
/
lib_utility.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* InfoAppl Bulletin Board Bot
* ===================
* UWiClab, University of Urbino
* ===================
* Support library. Don't change a thing here.
*/
/**
* Checks whether a text string starts with another.
* Performs a case-insensitive check.
* @param $text String to search in.
* @param $substring String to search for.
* @return bool True if $text starts with $substring.
*/
function starts_with($text = '', $substring = '') {
return (strpos(mb_strtolower($text), mb_strtolower($substring)) === 0);
}
/**
* Extracts the first command from a given text string.
*/
function extract_command($text = '') {
$matches = array();
if(preg_match("/^\/([a-zA-Z0-9_]*)( |$)/", $text, $matches) !== 1) {
return null;
}
if(sizeof($matches) < 2) {
return null;
}
return $matches[1];
}
/**
* Extracts the command payload from a string.
* @param $text String to search in.
* @return string Command payload, if any, or empty string.
*/
function extract_command_payload($text = '') {
return mb_ereg_replace("^\/([a-zA-Z0-9_]*)( |$)", '', $text);
}
/**
* Extracts a cleaned-up response from the user.
*/
function extract_response($text) {
if(!$text) {
return '';
}
$lower_response = mb_strtolower(trim_response($text));
return escape_accents($lower_response);
}
function trim_response($text) {
return trim($text, ' /,.!?;:\'"');
}
/**
* Serches for the last occurence of the = char.
*
* @param string $text The string from which the guid needs to be extracted.
* @return string the GUID extracted.
*/
function extract_guid_number($text = '') {
return mb_substr($text,mb_strrpos($text, '=')+1);
}
/**
* Hydrates a string value using a map of key/values.
*/
function hydrate($text, $map = null) {
if(!$map || !is_array($map)) {
$map = array();
}
foreach($map as $from => $to) {
$text = str_replace($from, escape_markdown($to), $text);
}
return $text;
}
/**
* Unite two arrays, even if they are null.
* Always returns a valid array.
*/
function unite_arrays($a, $b) {
if(!$a || !is_array($a)) {
$a = array();
}
if($b && is_array($b)) {
$a = array_merge($a, $b);
}
return $a;
}
/**
* Escapes Markdown reserved characters so non-Markdown text can be
* embedded in a Markdown message without issues.
*/
function escape_markdown($text) {
return mb_ereg_replace('([_*\[\]\(\)])', '\\\1', $text);
}
/**
* Escapes accents.
*/
function escape_accents($text)
{
return preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml|caron);~i', '$1', htmlentities($text, ENT_QUOTES, 'UTF-8'));
}
/**
* Checks whether $text is a valid guid or not.
* @param $text String the string to be checked.
* @return boolean
*/
function is_guid($text) {
return preg_match('/^\{?[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}\}?$/', $text) > 0;
}
function generate_qr_code_url($code) {
$deeplink_base_url = rawurlencode(TELEGRAM_DEEP_LINK_URI_BASE);
return "https://chart.googleapis.com/chart?chs=500x500&cht=qr&chl=$deeplink_base_url$code&choe=UTF-8";
}
function get_live_channel_id(){
return DEBUG ? DEBUG_LIVE_CHANNEL_ID: LIVE_CHANNEL_ID;
}
?>