-
Notifications
You must be signed in to change notification settings - Fork 8
/
inc.php
288 lines (239 loc) · 7.53 KB
/
inc.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
use \Eventviva\ImageResize;
date_default_timezone_set('UTC');
require_once('vendor/autoload.php');
require_once('lib/Regex.php');
require_once('lib/php_calendar.php');
require_once('lib/format.php');
require_once('lib/config.php');
function mc() {
static $m;
if(!isset($m)) {
$m = new Memcached;
$m->addServer('127.0.0.1', 11211);
}
return $m;
}
class ImageProxy {
public static function url($url) {
$signature = hash_hmac('sha256', $url, Config::$secret);
if(preg_match('/^http/', $url))
$path = '?url=' . urlencode($url) . '&';
else
$path = '/' . $url . '?';
return Config::$base.'img.php'.$path.'sig=' . $signature;
}
public static function image($url, $sig) {
$expectedSignature = hash_hmac('sha256', $url, Config::$secret);
if($sig == $expectedSignature) {
$filename = './public/avatars/'.$sig.'.jpg';
if(preg_match('/^https?:\/\//', $url)) {
if(file_exists($filename)) {
header('Content-type: image/jpeg');
readfile($filename);
return;
}
$client = new GuzzleHttp\Client();
try {
$img = $client->request('GET', $url);
// Resize to 64px
$image = ImageResize::createFromString($img->getBody());
$image->resizeToBestFit(64, 64);
// Save to disk
$image->save($filename, IMAGETYPE_JPEG, 80);
$image->output(IMAGETYPE_JPEG, 80);
return;
} catch(GuzzleHttp\Exception\ClientException $e) {
}
}
}
header('Content-type: image/svg+xml');
readfile('./public/assets/user.svg');
}
}
function getViewerTimezone() {
try {
$tzname = array_key_exists('timezone_view', $_COOKIE) ? $_COOKIE['timezone_view'] : 'UTC';
$tz = new DateTimeZone($tzname);
} catch(Exception $e) {
$tzname = 'UTC';
$tz = new DateTimeZone('UTC');
}
return [$tzname, $tz];
}
function filterText($text, $channel) {
/*
for($i=0; $i<strlen($text); $i++) {
if(ord($text[$i]) < 32)
$text[$i] = '';
}
*/
$text = htmlspecialchars($text, ENT_SUBSTITUTE, 'UTF-8');
#$text = mb_encode_numericentity($text);
// Remove `/me ` from the beginning of lines
$text = preg_replace('/^\/me /', '', $text);
$text = preg_replace('/({.+})/', '<code>$1</code>', $text);
$text = preg_replace(Regex_URL::$expression, Regex_URL::$replacement, $text);
$text = preg_replace(Regex_Twitter::$expression, Regex_Twitter::$replacement, $text);
if($b=Config::wiki_base($channel)) {
$wikireplace = str_replace('{{wikibase}}', $b, Regex_WikiPage::$replacement);
$text = preg_replace(Regex_WikiPage::$expression, $wikireplace, $text);
}
// Expand Loqi memes
$text = preg_replace('/(?<!\")(http:\/\/meme\.loqi\.me\/m\/[a-zA-Z0-9_]+\.(jpg|gif|png))/', '$1<br><img src="$1" style="max-width: 200px; vertical-align: top; margin-left: 80px;">', $text);
return $text;
}
function isMeMessage($text) {
return preg_match('/^\/me /', $text);
}
function xmlEscapeText($text, $autolink=TRUE) {
# escape the source line of text
$text = str_replace(array('&','<','>','"'), array('&','<','>','"'), $text);
if($autolink) {
# add links for URLs and twitter names
$text = preg_replace(Regex_URL::$expression, Regex_URL::$replacement, $text);
$text = preg_replace(Regex_Twitter::$expression, Regex_Twitter::$replacement, $text);
}
return $text;
}
function stripIRCControlChars($text) {
$text = preg_replace('/\x03\d{1,2}/', '', $text);
$text = preg_replace('/\x03/', '', $text);
return $text;
}
function trimString($str, $length, $allow_word_break=false) {
// trims $str to $length characters
// if $str is too long, it puts … on the end
// if $allow_word_break is true, doesn't split a word in the middle
if( strlen($str) <= $length ) {
return $str;
} else {
if( $allow_word_break ) {
return trim(substr($str,0,$length-3))."...";
} else {
$newstr = substr($str,0,$length-3);
return substr($newstr, 0, strrpos($newstr, " "))."...";
}
}
}
function refreshUsers() {
$collections = [
['group'=>'indieweb', 'url'=>'https://indieweb.org/irc-people'],
['group'=>'w3c', 'url'=>'https://www.w3.org/wiki/IRC-people']
];
foreach($collections as $c) {
$parsed = file_get_contents('http://pin13.net/mf2/?url='.urlencode($c['url']));
if(trim($parsed)) {
$data = json_decode($parsed);
if($data && property_exists($data, 'items') && count($data->items) && count($data->items[0])) {
$item = $data->items[0];
if(isset($item->children))
$users = $item->children;
else
$users = $data->items;
$er = fopen('php://stderr', 'w');
fputs($er, 'found ' . count($users) . ' items'."\n");
fclose($er);
file_put_contents(dirname(__FILE__).'/data/'.$c['group'].'.json', json_encode($users,JSON_PRETTY_PRINT+JSON_UNESCAPED_SLASHES));
}
}
}
}
function join_with_and($array) {
if(count($array) == 0) return '';
if(count($array) == 1) return $array[0];
$last = array_pop($array);
return implode(', ', $array).' and '.$last;
}
/**
* Generate file that contains the date with the first message
* in each channel
*/
/* TODO: update to check files on disk and save as YYYY-MM-DD
function refreshFirst() {
$channels = Config::supported_channels();
$dates = new stdClass();
foreach ($channels as $channel) {
if ($channel == 'indieweb') {
$sqlChannels = '"#indieweb","#indiewebcamp"';
} else {
$sqlChannels = '"' . Config::irc_channel_for_slug($channel) . '"';
}
$sql = 'SELECT timestamp FROM irclog'
. ' WHERE channel IN (' . $sqlChannels . ')'
. ' ORDER BY timestamp ASC'
. ' LIMIT 1';
$stmt = db()->query($sql);
$row = $stmt->fetchObject();
if (is_object($row)) {
$dates->{'#' . $channel} = strtotime(date('Y-m-d', ((int) $row->timestamp / 1000))) * 1000;
}
}
file_put_contents(__DIR__ . '/data/first.json', json_encode($dates));
}
*/
function isAfterFirst($channel, $date)
{
$data = json_decode(file_get_contents(__DIR__ . '/data/first.json'));
if (!isset($data->$channel)) {
return false;
}
return strtotime($date) >= strtotime($data->$channel);
}
$users = array();
function loadUsers($channel) {
global $users;
if($group=Config::group_for_channel($channel)) {
$filename = dirname(__FILE__).'/data/'.$group.'.json';
if(file_exists($filename)){
$data = json_decode(file_get_contents($filename));
foreach($data as $item) {
if(in_array('h-card', $item->type)) {
$users[] = $item;
}
}
}
}
}
function loadTimezones() {
global $users;
$timezones = [];
foreach($users as $u) {
if(property_exists($u->properties, 'tz')) {
$t = $u->properties->tz[0];
if(!in_array($t, $timezones)) {
try {
new DateTimeZone($t);
$timezones[] = $t;
} catch(Exception $e) {}
}
}
}
sort($timezones);
return $timezones;
}
function userForNick($nick) {
global $users;
$nick = strtolower(trim($nick,'_[]'));
foreach($users as $u) {
if(@strtolower($u->properties->nickname[0]) == $nick) {
return $u;
}
}
return null;
}
function userForHost($host) {
global $users;
foreach($users as $u) {
$userHost = property_exists($u->properties, 'url') ? preg_replace('/https?:\/\//','', strtolower($u->properties->url[0])) : false;
if($userHost && $userHost == strtolower($host)) {
return $u;
}
}
return null;
}
function debug($thing) {
if($_SERVER['REMOTE_ADDR'] == '24.21.213.88') {
var_dump($thing);
}
}