/
config.php
190 lines (165 loc) · 6.85 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
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
<?php
$CI =& get_instance();
$plugin = OpenVBX::$currentPlugin;
$plugin = $plugin->getInfo();
$plugin_url = base_url().'plugins/'.$plugin['dir_name'];
$op = @$_REQUEST['op'];
$zendesk_user = PluginData::get('zendesk_user');
if(!function_exists('json_encode')) {
include($plugin['plugin_path'].'/vendors/json.php');
}
if($op == 'test_credentials')
{ // {{{
try {
$email = @$_REQUEST['email'];
$password = @$_REQUEST['password'];
$url = @$_REQUEST['url'];
$timezone = (int) @$_REQUEST['timezone'];
$errors = array();
if(empty($email)) $errors[] = array( 'msg'=>'Email is required.', 'name'=>'zendesk_email' );
if(empty($password)) $errors[] = array( 'msg'=>'Password is required.', 'name'=>'zendesk_password' );
if(empty($url)) $errors[] = array( 'msg'=>'URL to your Zendesk is required.', 'name'=>'zendesk_url' );
else if(strpos($url, 'zendesk') === false) $errors[] = array( 'msg'=>'This is an invalid Zendesk URL.', 'name'=>'zendesk_url' );
if(!empty($errors)) throw new Exception('FORM_VALIDATION_ERROR');
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url.'/users.json',
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_HEADER => FALSE, // Helps differeniate from http and https
CURLOPT_FOLLOWLOCATION => FALSE,
CURLOPT_USERPWD => "$email:$password",
CURLOPT_RETURNTRANSFER => TRUE
));
$results = curl_exec($ch);
$ch_info = curl_getinfo($ch);
if(curl_errno($ch)) {
error_log('CURL failed due to '.curl_error());
return FALSE;
} else {
if($ch_info['http_code'] >= 200 && $ch_info['http_code'] < 300) {
throw new Exception('SUCCESS');
} else {
throw new Exception('INVALID_CREDENTIALS');
}
}
throw new Exception('EXCEPTION');
} catch(Exception $e) {
switch($e->getMessage()) {
case 'CANNOT_CONNECT_TO_HOST':
$results = array(
'msg' => "Cannot connect to $url.",
'key' => 'CANNOT_CONNECT_TO_HOST',
'type' => 'error',
'data' => array(
'url' => $url,
'errors' => array(
'name' => 'zendesk_url',
'msg' => 'Cannot connect to this url.'
)
)
);
break;
case 'FORM_VALIDATION_ERROR':
$results = array(
'msg' => 'There are errors on the form. Please fix and try again.',
'key' => 'FORM_VALIDATION_ERROR',
'type' => 'error',
'data' => array(
'errors' => $errors
)
);
break;
case 'INVALID_CREDENTIALS':
$results = array(
'msg' => 'The credentials you entered are invalid.',
'key' => 'INVALID_CREDENTIALS',
'type' => 'error'
);
break;
case 'OP_REQUIRED':
$results = array(
'msg' => 'No operation selected.',
'key' => 'OP_REQUIRED',
'type' => 'error'
);
break;
case 'SUCCESS':
// If credentials are valid, store it to plugin store for this user
PluginData::set('zendesk_user', array(
'url' => $url,
'email' => $email,
'password' => $password,
'timezone' => $timezone
));
$results = array(
'msg' => 'Awesome! Your credentials are valid.',
'key' => 'SUCCESS',
'type' => 'success'
);
break;
default:
case 'EXCEPTION':
$results = array(
'msg' => 'An unexpected error occurred.',
'key' => 'EXCEPTION',
'type' => 'error'
);
break;
}
}
echo '<JSON_DATA>'.json_encode($results).'</JSON_DATA>';
} // }}}
elseif($op == 'delete_credentials')
{ // {{{
PluginData::set('zendesk_user', '');
$results = array(
'msg' => 'Zendesk credentials erased.',
'key' => 'SUCCESS',
'type' => 'success'
);
echo '<JSON_DATA>'.json_encode($results).'</JSON_DATA>';
} // }}}
?>
<?php if(empty($op)): ?>
<style>
span[class$="_err"] { color:red; }
a.ajax_loader { background:url(<?php echo base_url() ?>assets/i/ajax-loader.gif); display:inline-block; width:16px; height:11px; vertical-align:middle; }
div.system_msg { display:inline-block; line-height:30px; vertical-align:center; }
div.system_msg > * { vertical-align:middle; }
</style>
<div class="vbx-applet">
<div id="zendesk_api_access" class="section">
<h2>API Access Credentials</h2>
<p>Please enter your access info so we can update Zendesk with incoming messages.</p>
<div class="vbx-input-container input" style="margin-bottom:10px;">
<label>Zendesk URL - the URL to your Zendesk which is something like http or https://yoursite.zendesk.com.</label>
<input name="zendesk_url" class="medium" type="text" value="<?php echo @$zendesk_user->url ?>" />
<span class="zendesk_url_err"></span>
</div>
<div class="vbx-input-container input" style="margin-bottom:10px;">
<label>Email - your email used to login to Zendesk</label>
<input name="zendesk_email" class="medium" type="text" value="<?php echo @$zendesk_user->email ?>" />
<span class="zendesk_email_err"></span>
</div>
<div class="vbx-input-container input" style="margin-bottom:5px;">
<label>Password - your password used to login to Zendesk</label>
<input name="zendesk_password" class="medium" type="password" value="<?php echo @$zendesk_user->password ?>" />
<span class="zendesk_password_err"></span>
</div>
<div style="line-height:30px;">
<button id="save_cred_btn" class="inline-button submit-button" style="margin-top:5px; vertical-align:center;">
<span>Save</span>
</button>
<a class="delete_creds_btn" href="#">Delete</a>
<div class="system_msg"></div>
</div>
<div style="clear:both;"></div>
</div><!-- #zendesk_api_access -->
</div>
<script>
var base_url = '<?php echo base_url() ?>';
var plugin_url = '<?php echo $plugin_url ?>';
var plugin_dir = '<?php echo $plugin['dir_name'] ?>';
</script>
<?php OpenVBX::addJS('config.js') ?>
<?php endif; ?>