-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.TopicFormRefreshAjax.php
More file actions
355 lines (296 loc) · 12 KB
/
class.TopicFormRefreshAjax.php
File metadata and controls
355 lines (296 loc) · 12 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
require_once INCLUDE_DIR . 'class.topic.php';
require_once INCLUDE_DIR . 'class.dynamic_forms.php';
class TopicFormRefreshAjax extends AjaxController {
const GITHUB_REPO = 'ChesnoTech/ost-topic-form-refresh';
/**
* Proxy endpoint for loading help topic forms.
* Duplicates DynamicFormsAjaxAPI::getFormsForHelpTopic() logic
* but WITHOUT the HTTP_REFERER check that blocks AJAX on some servers.
*/
function getTopicForms($topic_id) {
global $thisstaff;
if (!$thisstaff)
Http::response(403, 'Login required');
if (!($topic = Topic::lookup($topic_id)))
Http::response(404, 'No such help topic');
if ($_GET || isset($_SESSION[':form-data'])) {
if (!is_array($_SESSION[':form-data']))
$_SESSION[':form-data'] = array();
$_SESSION[':form-data'] = array_merge(
$_SESSION[':form-data'],
Format::htmlchars($_GET)
);
}
$html = '';
$media = '';
foreach ($topic->getForms() as $form) {
if ($form->isDeleted() || !$form->hasAnyVisibleFields())
continue;
ob_start();
$form->getForm($_SESSION[':form-data'])->render(array(
'staff' => true,
'mode' => 'create'));
$html .= ob_get_clean();
ob_start();
print $form->getMedia();
$media .= ob_get_clean();
}
return $this->encode(array(
'media' => $media,
'html' => $html,
));
}
/* ── Self-Update ─────────────────────────────── */
function checkUpdate() {
global $thisstaff;
if (!$thisstaff || !$thisstaff->isAdmin())
Http::response(403, 'Admin access required');
$pluginInfo = include(dirname(__FILE__) . '/plugin.php');
$current = $pluginInfo['version'];
$releases = self::getAllReleases();
if (isset($releases['error']))
return $this->encode(array('error' => $releases['error']));
$updates = self::categorizeUpdates($releases, $current);
$updates['current'] = $current;
return $this->encode($updates);
}
function doUpdate() {
global $thisstaff;
if (!$thisstaff || !$thisstaff->isAdmin())
Http::response(403, 'Admin access required');
$pluginDir = dirname(__FILE__);
$pluginInfo = include($pluginDir . '/plugin.php');
$targetTag = isset($_POST['target_tag']) ? trim($_POST['target_tag']) : '';
if (!$targetTag)
return $this->encode(array(
'success' => false,
'error' => 'No target version specified'));
// Fetch the specific release by tag
$release = self::getReleaseByTag($targetTag);
if (isset($release['error']))
return $this->encode(array(
'success' => false, 'error' => $release['error']));
if (!version_compare($release['version'], $pluginInfo['version'], '>'))
return $this->encode(array(
'success' => false, 'error' => 'Already up to date'));
// Download zipball
$zipData = self::curlGet($release['zipball_url']);
if (!$zipData)
return $this->encode(array(
'success' => false,
'error' => 'Failed to download update package'));
$tempZip = tempnam(sys_get_temp_dir(), 'tfr') . '.zip';
$tempDir = sys_get_temp_dir() . '/tfr-' . uniqid();
file_put_contents($tempZip, $zipData);
// Extract
$zip = new ZipArchive();
if ($zip->open($tempZip) !== true) {
@unlink($tempZip);
return $this->encode(array(
'success' => false,
'error' => 'Failed to open update package'));
}
@mkdir($tempDir, 0755, true);
$zip->extractTo($tempDir);
$zip->close();
@unlink($tempZip);
// GitHub zips contain one root dir: Owner-Repo-hash/
$rootDir = null;
foreach (scandir($tempDir) as $e) {
if ($e === '.' || $e === '..') continue;
if (is_dir("$tempDir/$e")) {
$rootDir = "$tempDir/$e";
break;
}
}
if (!$rootDir || !file_exists("$rootDir/plugin.php")) {
self::rmdirRecursive($tempDir);
return $this->encode(array(
'success' => false,
'error' => 'Invalid update package structure'));
}
// Replace files (skip .git)
self::copyDir($rootDir, $pluginDir);
self::rmdirRecursive($tempDir);
// Clear opcache so PHP sees the new files
if (function_exists('opcache_invalidate')) {
$iter = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($pluginDir,
RecursiveDirectoryIterator::SKIP_DOTS));
foreach ($iter as $f) {
if (pathinfo($f, PATHINFO_EXTENSION) === 'php')
opcache_invalidate($f->getPathname(), true);
}
}
return $this->encode(array(
'success' => true,
'new_version' => $release['version'],
));
}
/* ── GitHub helpers ──────────────────────────── */
/**
* Fetch all published (non-draft, non-prerelease) releases from GitHub.
*/
private static function getAllReleases() {
$url = 'https://api.github.com/repos/'
. self::GITHUB_REPO . '/releases?per_page=50';
$body = self::curlGet($url, false);
if (!$body)
return array('error' =>
'Failed to reach GitHub — check server connectivity');
$data = json_decode($body, true);
if (!is_array($data))
return array('error' =>
'Invalid response from GitHub');
if (empty($data))
return array('error' =>
'No releases found — create a GitHub release first');
$releases = array();
foreach ($data as $rel) {
if (!empty($rel['draft']) || !empty($rel['prerelease']))
continue;
if (empty($rel['tag_name']))
continue;
$releases[] = array(
'version' => ltrim($rel['tag_name'], 'v'),
'tag' => $rel['tag_name'],
'body' => isset($rel['body']) ? $rel['body'] : '',
'zipball_url' => $rel['zipball_url'],
'published' => isset($rel['published_at'])
? $rel['published_at'] : '',
);
}
if (empty($releases))
return array('error' =>
'No published releases found');
return $releases;
}
/**
* Fetch a single release by its git tag.
*/
private static function getReleaseByTag($tag) {
$url = 'https://api.github.com/repos/'
. self::GITHUB_REPO . '/releases/tags/' . urlencode($tag);
$body = self::curlGet($url, false);
if (!$body)
return array('error' =>
'Failed to reach GitHub — release not found');
$data = json_decode($body, true);
if (!$data || empty($data['tag_name']))
return array('error' =>
'Release not found: ' . $tag);
return array(
'version' => ltrim($data['tag_name'], 'v'),
'tag' => $data['tag_name'],
'body' => isset($data['body']) ? $data['body'] : '',
'zipball_url' => $data['zipball_url'],
);
}
/**
* Split available releases into minor (same major) and major (new major)
* update paths relative to the current installed version.
*
* Returns: { minor_update: {...}|null, major_update: {...}|null }
*/
private static function categorizeUpdates($releases, $currentVersion) {
$curParts = explode('.', $currentVersion);
$curMajor = (int)$curParts[0];
$minorUpdate = null; // latest within same major version
$majorUpdate = null; // latest with a higher major version
$minorAll = array(); // all available minor/patch versions
$majorAll = array(); // all available major versions
foreach ($releases as $rel) {
$ver = $rel['version'];
if (!version_compare($ver, $currentVersion, '>'))
continue;
$parts = explode('.', $ver);
$major = (int)$parts[0];
if ($major === $curMajor) {
$minorAll[] = $rel;
if (!$minorUpdate
|| version_compare($ver, $minorUpdate['version'], '>'))
$minorUpdate = $rel;
} else {
$majorAll[] = $rel;
if (!$majorUpdate
|| version_compare($ver, $majorUpdate['version'], '>'))
$majorUpdate = $rel;
}
}
// Sort version lists descending for display
usort($minorAll, function($a, $b) {
return version_compare($b['version'], $a['version']);
});
usort($majorAll, function($a, $b) {
return version_compare($b['version'], $a['version']);
});
return array(
'minor_update' => $minorUpdate,
'minor_available' => $minorAll,
'major_update' => $majorUpdate,
'major_available' => $majorAll,
);
}
private static function curlGet($url, $followRedirects = true) {
$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERAGENT => 'osTicket-Plugin-Updater/1.0',
CURLOPT_TIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => $followRedirects,
CURLOPT_SSL_VERIFYPEER => true,
));
$resp = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return ($code === 200) ? $resp : false;
}
/* ── File-system helpers ─────────────────────── */
private static function copyDir($src, $dst) {
foreach (scandir($src) as $e) {
if ($e === '.' || $e === '..' || $e === '.git') continue;
$s = "$src/$e";
$d = "$dst/$e";
if (is_dir($s)) {
if (!is_dir($d)) @mkdir($d, 0755, true);
self::copyDir($s, $d);
} else {
@copy($s, $d);
}
}
}
private static function rmdirRecursive($dir) {
if (!is_dir($dir)) return;
foreach (scandir($dir) as $e) {
if ($e === '.' || $e === '..') continue;
$p = "$dir/$e";
is_dir($p) ? self::rmdirRecursive($p) : @unlink($p);
}
@rmdir($dir);
}
/* ── Asset serving ───────────────────────────── */
private function serveFile($file, $contentType) {
if (!file_exists($file))
Http::response(404, 'Not found');
$etag = '"tfr-' . md5($file) . '-' . filemtime($file) . '"';
header('Content-Type: ' . $contentType);
header('Cache-Control: public, max-age=86400');
header('ETag: ' . $etag);
if (isset($_SERVER['HTTP_IF_NONE_MATCH'])
&& trim($_SERVER['HTTP_IF_NONE_MATCH']) === $etag) {
Http::response(304, '');
exit;
}
readfile($file);
exit;
}
function serveJs() {
$this->serveFile(dirname(__FILE__) . '/assets/topic-form-refresh.js',
'application/javascript; charset=UTF-8');
}
function serveAdminJs() {
$this->serveFile(dirname(__FILE__) . '/assets/topic-form-refresh-admin.js',
'application/javascript; charset=UTF-8');
}
}