-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathRum.php
More file actions
430 lines (379 loc) · 10.4 KB
/
Copy pathRum.php
File metadata and controls
430 lines (379 loc) · 10.4 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?php
namespace Stackify\Utils;
use Stackify\Exceptions\RumValidationException;
/**
* RUMV2 Support
*/
class Rum
{
protected $rumScriptUrl;
protected $rumKey;
protected $appName;
protected $environment;
protected $hasSetup;
protected $config = array();
const DEFAULT_RUM_SCRIPT_URL = 'https://stckjs.stackify.com/stckjs.js';
const DEFAULT_RUM_KEY = '';
/**
* This is our container for the one an only instance with get instance
* Reference: https://refactoring.guru/design-patterns/singleton/php/example
*/
private static $_instances = [];
/**
* Constructor
*/
public function __construct()
{
$this->rumScriptUrl = self::DEFAULT_RUM_SCRIPT_URL;
$ds = DIRECTORY_SEPARATOR;
$this->config['DebugLogPath'] = realpath(dirname(__FILE__) . "$ds..$ds..") . $ds . 'debug/log.log';
$this->hasSetup = false;
}
/**
* Get singleton instance
*
* @return this
*/
public static function getInstance()
{
$cls = static::class;
if (!isset(self::$_instances[$cls])) {
self::$_instances[$cls] = new static();
}
return self::$_instances[$cls];
}
/**
* Setup configuration for rum
*
* @param string $appName Application Name
* @param string $environment Environment
* @param string $rumKey RUM Key
* @param string $rumScriptUrl RUM Script URL
*
* @return this
*/
public function setupConfiguration($appName, $environment, $rumKey = null, $rumScriptUrl = null, $config = null)
{
try {
$this->appName = $this->validateAppName($appName);
$this->environment = $this->validateEnvironment($environment);
$this->rumScriptUrl = $this->checkRumScriptUrl($rumScriptUrl);
$this->rumKey = $this->checkRumKey($rumKey);
$this->hasSetup = true;
} catch (\Exception $e) {
$this->logError('Unable to setup RUM Configuration. Something went wrong. Message: %s', $e->getMessage());
// Reset state
$this->appName = null;
$this->environment = null;
$this->rumScriptUrl = self::DEFAULT_RUM_SCRIPT_URL;
$this->rumKey = null;
}
return $this;
}
/**
* Check RUM Script URL
*
* @param string $rumScriptUrl RUM Script URL
*
* @return string
*/
public function checkRumScriptUrl($rumScriptUrl)
{
$url = isset($_SERVER['RETRACE_RUM_SCRIPT_URL']) ? $_SERVER['RETRACE_RUM_SCRIPT_URL'] : null;
if (!$url) {
$url = $rumScriptUrl;
}
return $this->validateRumScriptUrl($url);
}
/**
* Check RUM Key
*
* @param string $rumKey RUM Key
*
* @return string
*/
public function checkRumKey($rumKey)
{
$key = isset($_SERVER['RETRACE_RUM_KEY']) ? $_SERVER['RETRACE_RUM_KEY'] : null;
if (!$key) {
$key = $rumKey;
}
return $this->validateRumKey($key);
}
/**
* Validate RUM Script URL
*
* @param string $rumScriptUrl RUM Script URL
*
* @return void
*/
public function validateRumScriptUrl($rumScriptUrl)
{
if (empty($rumScriptUrl)) {
return self::DEFAULT_RUM_SCRIPT_URL;
}
if (!preg_match('/^((((https?|ftps?|gopher|telnet|nntp):\/\/)|(mailto:|news:))(%[0-9A-Fa-f]{2}|[-()_.!~*\';\/\?:@&=+$,A-Za-z0-9])+)([).!\';\/\?:,][\[:blank:|:blank:\]])?$/', $rumScriptUrl)) {
throw new RumValidationException('RUM Script URL is in invalid format.');
}
return $rumScriptUrl;
}
/**
* Validate RUM Key
*
* @param string $rumKey RUM key
*
* @return void
*/
public function validateRumKey($rumKey)
{
if (empty($rumKey)) {
throw new RumValidationException('RUM Key is empty.');
}
if (!preg_match('/^[A-Za-z0-9_-]+$/', $rumKey)) {
throw new RumValidationException('RUM Key is in invalid format.');
}
return $rumKey;
}
/**
* Validate Application Name
*
* @param string $appName
*
* @return string
*/
public function validateAppName($appName)
{
if (empty($appName)) {
throw new RumValidationException('Application Name is empty.');
}
return $appName;
}
/**
* Validate Environment
*
* @param string $environment
*
* @return string
*/
public function validateEnvironment($environment)
{
if (empty($environment)) {
throw new RumValidationException('Environment is empty.');
}
return $environment;
}
/**
* Get insert rum script tag
*
* @return string|null
*/
public function insertRumScript()
{
try {
if ($this->isProfilerActive()) {
return $this->getProfilerInsertRumScript();
}
if (empty($this->getRumScriptUrl()) || empty($this->getRumKey())) {
return null;
}
$transactionId = $this->getTransactionId();
$reportingUrl = $this->getReportingUrl();
$applicationName = $this->getApplicationName();
$environment = $this->getEnvironment();
if (empty($transactionId) || empty($reportingUrl) || empty($applicationName) || empty($environment)) {
return null;
}
$rumSettings = array(
'ID' => $transactionId,
'Name' => base64_encode(utf8_encode($applicationName)),
'Env' => base64_encode(utf8_encode($environment)),
'Trans' => base64_encode(utf8_encode($reportingUrl))
);
return '<script type="text/javascript">(window.StackifySettings || (window.StackifySettings = '.json_encode($rumSettings).'))</script><script src="'.$this->getRumScriptUrl().'" data-key="'.$this->getRumKey().'" async></script>';
} catch (\Exception $e) {
$this->logError('Unable to insert RUM Script. Something went wrong. Message: %s', $e->getMessage());
}
}
/**
* Get transaction ID
*
* @return string|null
*/
public function getTransactionId()
{
return null;
}
/**
* Get reporting URL
*
* @return string|null
*/
public function getReportingUrl()
{
if ($this->getSapiName() == 'cli') {
$phpSelf = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF']: null;
if ($phpSelf) {
return basename($phpSelf, '.php');
}
return null;
}
$requestMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD']: null;
$requestUri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI']: null;
$reportingUrl = '/';
if ($requestUri && is_string($requestUri)) {
$reportingUrl = array_filter(explode('?', $requestUri))[0];
$reportingUrl = rtrim($reportingUrl, '/');
}
return ($requestMethod ? $requestMethod . '-' : '') . $reportingUrl;
}
/**
* Get server api name
*
* @return string
*/
public function getSapiName()
{
return php_sapi_name();
}
/**
* Checks if the profiler is active
*
* @return boolean
*/
protected function isProfilerActive()
{
return function_exists('stackify_transaction_id') && class_exists($this->getProfilerClass());
}
/**
* Get profiler insert rum script
*
* @return string
*/
public function getProfilerInsertRumScript()
{
if ($this->isProfilerActive() && method_exists($this->getProfilerClass(), 'insertRumScript')) {
$profilerClass = $this->getProfilerClass();
return $profilerClass::insertRumScript();
}
return null;
}
/**
* Get profiler class
*
* @return string
*/
public function getProfilerClass()
{
return 'Stackify\Profiler';
}
/**
* Get RUM Script URL
*
* @return string
*/
public function getRumScriptUrl()
{
return $this->rumScriptUrl;
}
/**
* Check if RUM Util is setup
*
* @return boolean
*/
public function isSetup()
{
return $this->hasSetup;
}
/**
* Get debug log path
*
* @return string
*/
public function getDebugLogPath()
{
return $this->config && isset($this->config['DebugLogPath']) ? $this->config['DebugLogPath'] : null;
}
/**
* Get Application Name
*
* @return string
*/
public function getApplicationName()
{
return $this->appName;
}
/**
* Get environment
*
* @return string
*/
public function getEnvironment()
{
return $this->environment;
}
/**
* Get RUM Key
*
* @return string
*/
public function getRumKey()
{
return $this->rumKey;
}
/**
* Log error message
*
* @param string $message Message
*
* @return void
*/
protected function logError($message)
{
$this->log($message, func_get_args(), false);
}
/**
* Log error messages
*
* @param string $message
* @param array $args
* @param boolean $success
*
* @return void
*/
protected function log($message, $args, $success = true)
{
$replacements = array_slice($args, 1);
$prefix = $success ? 'Stackify Log' : 'Stackify Error';
$template = "[$prefix][RUM] $message";
$formatted = preg_replace('/\r\n/', '', vsprintf($template, $replacements));
if ($this->getDebugLogPath()) {
$this->writeToFile($this->getDebugLogPath(), $formatted);
}
if (!$success) {
$this->writeToErrorLog($formatted);
}
}
/**
* Write message to file
*
* @param string $path
* @param string $string
*
* @return void
*/
protected function writeToFile($path, $string)
{
@file_put_contents($path, "$string\n", FILE_APPEND);
}
/**
* Write message to php built in error log
*
* @param string $string
*
* @return void
*/
protected function writeToErrorLog($string)
{
error_log($string);
}
}