Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
proxy_authentication_config.inc
1 <?php
18 require_once SQ_LIB_PATH.'/config/config.inc';
19 require_once SQ_LIB_PATH.'/html_form/html_form.inc';
20 
33 {
34 
39  var $config_vars = Array(
40  // Proxy Options
41  'SQ_PA_ENABLED' => Array('editable' => 1, 'default' => FALSE),
42  'SQ_PA_HOSTNAME' => Array('editable' => 1, 'default' => ''),
43  'SQ_PA_PORT' => Array('editable' => 1, 'default' => ''),
44  'SQ_PA_USERNAME' => Array('editable' => 1, 'default' => ''),
45  'SQ_PA_PASSWORD' => Array('editable' => 1, 'default' => ''),
46  'SQ_PA_ALTERNATIVES' => Array('editable' => 1, 'default' => 'a:0:{}'),
47  'SQ_PROXY_URL_PORT_STRIP' => Array('editable' => 1, 'default' => FALSE),
48  // SSL Accelerator Options
49  'SQ_SSLA_ENABLED' => Array('editable' => 1, 'default' => FALSE),
50  'SQ_SSLA_MATCH' => Array('editable' => 1, 'default' => 'ip'),
51  'SQ_SSLA_IP' => Array('editable' => 1, 'default' => ''),
52  'SQ_SSLA_HEADER_NAME' => Array('editable' => 1, 'default' => ''),
53  'SQ_SSLA_HEADER_VALUE' => Array('editable' => 1, 'default' => ''),
54  'SQ_SSLA_FORCE_SECURE' => Array('editable' => 1, 'default' => FALSE),
55  );
56 
57 
62  function __construct()
63  {
64  parent::__construct();
65  $this->config_file = SQ_DATA_PATH.'/private/conf/proxy_authentication.inc';
66 
67  }//end constructor
68 
69 
82  function paintBackend(&$o)
83  {
84  parent::paintBackend($o);
85 
86  $write_access = $this->writeAccess();
87  $class = get_class_lower($this);
88 
89  $o->openSection('Primary Proxy Configuration');
90  $o->openField(translate('proxy_authentication_status'));
91  if (SQ_PA_ENABLED) {
92  require_once(SQ_INCLUDE_PATH.'/general_occasional.inc');
93  $socket = open_socket_connection('http://matrix.squiz.net', $errno, $errstr);
94  if ($socket !== FALSE && !is_null($socket)) {
95  $response = fread($socket, 128);
96  if (preg_match('|407 proxy authentication required|i', $response)) {
97  echo '<font color="red"><strong>'.translate('proxy_authentication_failed').'</strong></font>';
98  } else {
99  echo '<font color="green"><strong>'.translate('proxy_authentication_passed').'</strong></font>';
100  }
101  fclose($socket);
102  } else {
103  echo '<strong class="sq-backend-warning">'.translate('proxy_configuration_problem').'</font>';
104  }
105  } else {
106  echo '<strong>'.translate('proxy_authentication_disabled').'</font>';
107  }
108  $o->closeField();
109 
110  $o->openField(translate('proxy_authentication_enable'));
111  if ($write_access) {
112  combo_box($class.'[SQ_PA_ENABLED]', Array(translate('no'), translate('yes')), FALSE, SQ_PA_ENABLED);
113  } else {
114  echo (SQ_PA_ENABLED ? translate('yes') : translate('no'));
115  }
116  $o->closeField();
117  $o->openField(translate('hostname'));
118  if ($write_access) {
119  text_box($class.'[SQ_PA_HOSTNAME]', SQ_PA_HOSTNAME, 25);
120  } else {
121  echo SQ_PA_HOSTNAME;
122  }
123  $o->closeField();
124  $o->openField(translate('port'));
125  if ($write_access) {
126  text_box($class.'[SQ_PA_PORT]', SQ_PA_PORT, 5);
127  } else {
128  echo SQ_PA_PORT;
129  }
130  $o->closeField();
131  $o->openField(translate('username'));
132  if ($write_access) {
133  text_box($class.'[SQ_PA_USERNAME]', SQ_PA_USERNAME, 15);
134  } else {
135  echo SQ_PA_USERNAME;
136  }
137  $o->closeField();
138  $o->openField(translate('password'));
139  if ($write_access) {
140  password_box($class.'[SQ_PA_PASSWORD]', base64_decode(SQ_PA_PASSWORD), 15);
141  } else {
142  if (strlen(SQ_PA_PASSWORD)) {
143  echo translate('password_not_shown');
144  }
145  }
146  $o->closeField();
147 
148  $o->closeSection();
149 
150  $o->openSection(translate('proxy_url_manipulation'));
151 
152  $o->openField(translate('proxy_url_strip_port'));
153  if ($write_access) {
154  combo_box($class.'[SQ_PROXY_URL_PORT_STRIP]', Array(translate('no'), translate('yes')), FALSE, SQ_PROXY_URL_PORT_STRIP);
155  } else {
156  echo (SQ_PROXY_URL_PORT_STRIP ? translate('yes') : translate('no'));
157  }
158  $o->note(translate('proxy_url_strip_port_note'));
159  $o->closeField();
160 
161  $o->closeSection();
162 
163  $o->openSection('Alternative Proxy Configuration');
164  $o->openField('');
165  $this->paintAlternativeProxies($o);
166  $o->closeField();
167  $o->sectionNote('Alternative proxies will be used if they are set to be Active, and their URL pattern matches the URL being requested through the proxy. Proxies are matched from top to bottom; only the first proxy that matches will be used.');
168  $o->closeSection('');
169 
170  $o->openSection('SSL Accelerator Configuration');
171  $o->openField(translate('ssl_accelerator_handling_enabled'));
172  if ($write_access) {
173  combo_box($class.'[SQ_SSLA_ENABLED]', Array(translate('no'), translate('yes')), FALSE, SQ_SSLA_ENABLED);
174  } else {
175  echo (SQ_SSLA_ENABLED ? translate('yes') : translate('no'));
176  }
177  $o->note(translate('ssl_accelerator_handling_enabled_note'));
178  $o->closeField();
179 
180  $o->openField(translate('ssl_accelerator_match_method'));
181  $options = Array(
182  'ip' => translate('ssl_accelerator_match_method_ip'),
183  'header' => translate('ssl_accelerator_match_method_header'),
184  'both' => translate('ssl_accelerator_match_method_both'),
185  );
186  if ($write_access) {
187  combo_box($class.'[SQ_SSLA_MATCH]', $options, FALSE, SQ_SSLA_MATCH);
188  } else {
189  echo $options[SQ_SSLA_MATCH];
190  }
191  $o->note(translate('ssl_accelerator_match_method_note'));
192  $o->closeField();
193 
194  $o->openField(translate('ssl_accelerator_ip_address'));
195  if ($write_access) {
196  text_box($class.'[SQ_SSLA_IP]', SQ_SSLA_IP, 15);
197  } else {
198  echo SQ_SSLA_IP;
199  }
200  $o->note(translate('ssl_accelerator_ip_address_note'));
201  $o->closeField();
202 
203  $o->openField(translate('ssl_accelerator_header_name'));
204  if ($write_access) {
205  text_box($class.'[SQ_SSLA_HEADER_NAME]', SQ_SSLA_HEADER_NAME, 15);
206  } else {
207  echo SQ_SSLA_HEADER_NAME;
208  }
209  $o->note(translate('ssl_accelerator_header_name_note'));
210  $o->closeField();
211 
212  $o->openField(translate('ssl_accelerator_header_value'));
213  if ($write_access) {
214  text_box($class.'[SQ_SSLA_HEADER_VALUE]', SQ_SSLA_HEADER_VALUE, 15);
215  } else {
216  echo SQ_SSLA_HEADER_VALUE;
217  }
218  $o->note(translate('ssl_accelerator_header_value_note'));
219  $o->closeField();
220 
221  $o->openField(translate('ssl_accelerator_force_secure'));
222  if ($write_access) {
223  combo_box($class.'[SQ_SSLA_FORCE_SECURE]', Array(translate('no'), translate('yes')), FALSE, SQ_SSLA_FORCE_SECURE);
224  } else {
225  echo (SQ_SSLA_FORCE_SECURE ? translate('yes') : translate('no'));
226  }
227  $o->note(translate('ssl_accelerator_force_secure_note'));
228  $o->closeField();
229 
230  $o->sectionNote(translate('ssl_accelerator_section_note'));
231  $o->closeSection();
232 
233  if ($write_access) $o->commitButton('', TRUE);
234 
235  }//end paintBackend()
236 
237 
246  {
247  $all_proxies = SQ_PA_ALTERNATIVES;
248  if (trim($all_proxies) !== '') {
249  $all_proxies = unserialize(SQ_PA_ALTERNATIVES);
250  } else {
251  $all_proxies = Array();
252  }
253 
254  /*
255  * Pattern DOES/DOES NOT options, used for select field.
256  */
257  $pattern_not_options = Array(
258  '0' => 'does',
259  '1' => 'does not',
260  );
261 
262  /*
263  * Pattern type options to be used by the select field.
264  * This array also used for gramatically-correct pattern type
265  * descriptions when a DOES NOT is in force.
266  * eg. "URL DOES NOT begin with ..."
267  */
268  $pattern_type_options = Array(
269  'equals' => 'equal',
270  'begins' => 'begin with',
271  'ends' => 'end with',
272  'contains' => 'contain',
273  );
274 
275  /*
276  * Gramatically-correct pattern type descriptions for cases where a
277  * DOES NOT is not involved.
278  * eg. change "URL DOES equal ..." to "URL equals ...".
279  */
280  $pattern_type_desc = Array(
281  'equals' => 'equals',
282  'begins' => 'begins with',
283  'ends' => 'ends with',
284  'contains' => 'contains',
285  );
286 
287  $write_access = $this->writeAccess();
288  $class = get_class_lower($this);
289 
290  $i = 0;
291  foreach ($all_proxies as $proxy_key => $proxy_details) {
292  $i++;
293  $o->openSection('Alternative Proxy '.$i);
294  $o->openField('Hostname');
295  if ($write_access) {
296  echo text_box($class.'_alternative[proxy_url]['.$proxy_key.']', array_get_index($proxy_details, 'proxy_url', ''), 30);
297  $o->note(translate('proxy_leave_blank'));
298  } else {
299  echo (array_get_index($proxy_details, 'proxy_url', '') === '') ? '<i>' . translate('proxy_none_set') . '</i>' : array_get_index($proxy_details, 'proxy_url', '');
300  }
301  $o->closeField();
302  $o->openField('Port');
303  if ($write_access) {
304  echo int_text_box($class.'_alternative[proxy_port]['.$proxy_key.']', array_get_index($proxy_details, 'proxy_port', ''), FALSE, 7, 0, 65535);
305  } else {
306  echo array_get_index($proxy_details, 'proxy_port', '');
307  }
308  $o->closeField();
309  $o->openField('Username');
310  if ($write_access) {
311  echo text_box($class.'_alternative[username]['.$proxy_key.']', array_get_index($proxy_details, 'username', ''), 10);
312  } else {
313  echo array_get_index($proxy_details, 'username', '');
314  }
315  $o->closeField();
316  $o->openField('Password');
317  if ($write_access) {
318  echo password_box($class.'_alternative[password]['.$proxy_key.']', base64_decode(array_get_index($proxy_details, 'password', '')), 10);
319  } else {
320  if (trim(array_get_index($proxy_details, 'password', '')) !== '') {
321  ?><em>Password not shown</em><?php
322  }
323  }
324  $o->closeField();
325  $o->openField('Pattern');
326  if ($write_access) {
327  echo 'URL ';
328  echo combo_box($class.'_alternative[pattern_not]['.$proxy_key.']', $pattern_not_options, FALSE, array_get_index($proxy_details, 'pattern_not', '0'));
329  echo ' ';
330  echo combo_box($class.'_alternative[pattern_type]['.$proxy_key.']', $pattern_type_options, FALSE, array_get_index($proxy_details, 'pattern_type', 'equals'));
331  echo ' ';
332  text_box($class.'_alternative[pattern_url]['.$proxy_key.']', array_get_index($proxy_details, 'pattern_url', ''), 30);
333  } else {
334  echo translate('comparison'.(array_get_index($proxy_details, 'pattern_not', '0') ? '_not_' : '_').array_get_index($proxy_details, 'pattern_type', 'equals'), 'URL', '"'.array_get_index($proxy_details, 'pattern_url', '').'"');
335  }
336  $o->closeField();
337  $o->openField(translate('active_question'));
338  if ($write_access) {
339  echo check_box($class.'_alternative[active]['.$proxy_key.']', 1, array_get_index($proxy_details, 'active', TRUE));
340  } else {
341  echo array_get_index($proxy_details, 'active', TRUE);
342  }
343  $o->closeField();
344  if ($write_access) {
345  $o->openField(translate('delete_question'));
346  echo check_box($class.'_alternative[delete]['.$proxy_key.']', 1, FALSE);
347  $o->closeField();
348  }
349  $o->closeSection();
350  }
351 
352  if ($write_access) {
353  $o->openSection('Add New Proxy');
354  $o->openField('Add new proxy');
355  echo check_box($class.'_alternative[add]', 1, FALSE);
356  $o->closeField();
357  $o->closeSection();
358  }
359 
360  }//end paintAlternativeProxies()
361 
362 
363  public function processBackend(Backend_Outputter $o)
364  {
365  parent::processBackend($o);
366 
367  }//end processInterface();
368 
369 
370  public function processAlternativeProxies(&$vars)
371  {
372 
373 
374  if (defined('SQ_PA_ALTERNATIVES')) {
375  $all_proxies = SQ_PA_ALTERNATIVES;
376  if (trim($all_proxies) !== '') {
377  $all_proxies = unserialize(SQ_PA_ALTERNATIVES);
378  } else {
379  $all_proxies = Array();
380  }
381  } else {
382  $all_proxies = Array();
383  }
384 
385  $class = get_class_lower($this);
386 
387  $alt_post = array_get_index($_POST, $class.'_alternative', Array());
388 
389  if (!empty($alt_post)) {
390  $deletes = array_get_index($alt_post, 'delete', Array());
391  foreach ($deletes as $key => $value) {
392  unset($all_proxies[$key]);
393  }
394 
395  $actives = array_get_index($alt_post, 'active', Array());
396  foreach ($all_proxies as $key => $value) {
397  $all_proxies[$key]['active'] = isset($actives[$key]);
398 
399  $all_proxies[$key]['proxy_url'] = $alt_post['proxy_url'][$key];
400  $all_proxies[$key]['proxy_port'] = $alt_post['proxy_port'][$key];
401  $all_proxies[$key]['username'] = $alt_post['username'][$key];
402  $all_proxies[$key]['password'] = base64_encode($alt_post['password'][$key]);
403  $all_proxies[$key]['pattern_not'] = $alt_post['pattern_not'][$key];
404  $all_proxies[$key]['pattern_type'] = $alt_post['pattern_type'][$key];
405  $all_proxies[$key]['pattern_url'] = $alt_post['pattern_url'][$key];
406  }
407 
408  }
409 
410  $add = array_get_index($alt_post, 'add', 0);
411  if ($add) {
412  // stop Matrix from throwing PHP notice if the
413  // $all_proxies array is empty
414  if (!empty($all_proxies)) {
415  $new_key = max(array_keys($all_proxies)) + 1;
416  } else {
417  $new_key = 1;
418  }
419  $all_proxies[$new_key] = Array();
420  }
421 
422  $vars['SQ_PA_ALTERNATIVES'] = serialize($all_proxies);
423 
424  }//end processAlternativeProxies()
425 
426 
439  function save($vars, $backup_existing=FALSE)
440  {
441  // attempt to load the config file to make sure we get any current settings
442  if (file_exists($this->config_file)) {
443  require_once $this->config_file;
444  }
445 
446  $this->processAlternativeProxies($vars);
447 
448  // encode password
449  if (isset($vars['SQ_PA_PASSWORD']) && !empty($vars['SQ_PA_PASSWORD'])) {
450  $vars['SQ_PA_PASSWORD'] = base64_encode($vars['SQ_PA_PASSWORD']);
451  }
452 
453  return parent::save($vars, $backup_existing);
454 
455  }//end save()
456 
457 
458 }//end class
459 
460 ?>