-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.php
105 lines (84 loc) · 3.81 KB
/
config.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
<?php
require_once('polyfill.php');
require_once INCLUDE_DIR . 'class.plugin.php';
class CustomCodePluginConfig extends PluginConfig {
// Provide compatibility function for versions of osTicket prior to
// translation support (v1.9.4)
function translate() {
if (!method_exists('Plugin', 'translate')) {
return array(
function($x) { return $x; },
function($x, $y, $n) { return $n != 1 ? $y : $x; },
);
}
return Plugin::translate('customcode');
}
function getOptions() {
list($__, $_N) = self::translate();
return array(
'customcodeHeading' => new SectionBreakField(array(
'label' => $__('Enter your custom code below')
)),
'custom-code-css' => new TextareaField(array(
'label' => $__('Custom Client CSS'),
'configuration' => array('rows'=>10, 'cols'=>80, 'html'=>false),
)),
'custom-code-js' => new TextareaField(array(
'label' => $__('Custom Client JS'),
'configuration' => array('rows'=>10, 'cols'=>80, 'html'=>false),
)),
'custom-staff-code-css' => new TextareaField(array(
'label' => $__('Custom Staff CSS'),
'configuration' => array('rows'=>10, 'cols'=>80, 'html'=>false),
)),
'custom-staff-code-js' => new TextareaField(array(
'label' => $__('Custom Staff JS'),
'configuration' => array('rows'=>10, 'cols'=>80, 'html'=>false),
)),
);
}
function pre_save(&$config, &$errors) {
try {
$filepath = INCLUDE_DIR . "client/header.inc.php";
$find = "</head>";
$tag_start = "<!-- start custom code -->";
$tag_end = "<!-- end custom code-->";
$contents = file_get_contents($filepath);
//clean contents up
$contents = preg_replace("#" . $tag_start ."(.*?)" . $tag_end . "#s", "", $contents);
$contents = str_replace($tag_start, "", $contents);
$contents = str_replace($tag_end, "", $contents);
$css = $config['custom-code-css'];
$js = $config['custom-code-js'];
$replace = $tag_start;
$replace .= "<style>" . $css . "</style>";
$replace .= "<script>" . $js . "</script>";
$replace .= $tag_end;
$replace .= "</head>";
$contents = str_replace($find, $replace, $contents);
file_put_contents($filepath, $contents);
//staff code
$filepath = INCLUDE_DIR . "staff/header.inc.php";
$find = "</head>";
$tag_start = "<!-- start custom code -->";
$tag_end = "<!-- end custom code-->";
$contents = file_get_contents($filepath);
//clean contents up
$contents = preg_replace("#" . $tag_start ."(.*?)" . $tag_end . "#s", "", $contents);
$contents = str_replace($tag_start, "", $contents);
$contents = str_replace($tag_end, "", $contents);
$css = $config['custom-staff-code-css'];
$js = $config['custom-staff-code-js'];
$replace = $tag_start;
$replace .= "<style>" . $css . "</style>";
$replace .= "<script>" . $js . "</script>";
$replace .= $tag_end;
$replace .= "</head>";
$contents = str_replace($find, $replace, $contents);
file_put_contents($filepath, $contents);
} catch(Exception $e) {
error_log($e->getMessage());
}
return true;
}
}