Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
apache_config.inc
1 <?php
17 require_once SQ_LIB_PATH.'/config/config.inc';
18 
19 define('SQ_APACHE_CONF_CUSTOM_KEYWORD_KEY', 'sq_custom_keyword_key');
20 
32 class Apache_Config extends Config
33 {
34 
35 
43  var $config_vars = Array(
44  'request_notes' => Array('editable' => 1, 'default' => Array()),
45  );
46 
47 
54  var $request_notes = Array();
55 
56 
61  function __construct()
62  {
63  parent::__construct();
64  $this->config_file = SQ_DATA_PATH.'/private/conf/apache.inc';
65 
66  if (!file_exists($this->config_file)) return;
67 
68  // load request notes
69  require_once $this->config_file;
70  if (!empty($request_notes)) {
71  $this->request_notes = $request_notes;
72  }
73 
74  }//end constructor
75 
76 
88  function save($vars, $backup_existing=FALSE, $send_message=TRUE)
89  {
90  if ($GLOBALS['SQ_SYSTEM']->runLevelEnables(SQ_SECURITY_PERMISSIONS)) {
91  $is_root = $GLOBALS['SQ_SYSTEM']->userRoot();
92  $is_admin = ($is_root || $GLOBALS['SQ_SYSTEM']->userSystemAdmin());
93 
94  // if we dont have access, go away
95  if (!$is_admin) {
96  trigger_localised_error('SYS0267', E_USER_WARNING, $this->name);
97  return FALSE;
98  }
99  }
100  if (!$this->writeAccess()) {
101  trigger_localised_error('SYS0026', E_USER_WARNING, $this->name);
102  return FALSE;
103  }
104  // attempt to load the config file to make sure we get any current settings
105  if (file_exists($this->config_file)) {
106  require $this->config_file;
107  }
108 
109  $changed_vars = Array();
110  ob_start();
111  foreach ($this->config_vars as $var_name => $data) {
112 
113  $current_value = (isset($$var_name)) ? $$var_name : $data['default'];
114  if ($data['editable'] && isset($vars[$var_name]) && $current_value !== $vars[$var_name]) {
115 
116  $value = $vars[$var_name];
117 
118  $changed_vars[$var_name] = Array('old' => $current_value, 'new' => $value);
119  } else {
120  $value = $current_value;
121  }
122 
123  if (!$this->_paintConfigVar($var_name, $value)) {
124  ob_end_clean();
125  return FALSE;
126  }
127 
128  }//end foreach
129 
130  $str = '<'."?php\n".ob_get_contents().'?'.">\n";
131 
132  ob_end_clean();
133  if ($backup_existing && file_exists($this->config_file)) {
134 
135  $i = 0;
136  do {
137  $i++;
138  $old_version = $this->config_file.'.'.$i;
139  } while (file_exists($old_version));
140 
141  if (!copy($this->config_file, $old_version)) {
142  return FALSE;
143  }
144 
145  }// endif
146 
147  if (SQ_PHP_CLI) {
148  echo "----------------------------------\n".($str)."----------------------------------\n";
149  }
150  require_once SQ_FUDGE_PATH.'/general/file_system.inc';
151  // make sure the directory exists
152  if (!create_directory(dirname($this->config_file))) {
153  return FALSE;
154  }
155 
156  // and save the file
157  if (!string_to_file($str, $this->config_file)) {
158  return FALSE;
159  }
160 
161  // Check if a message should be sent.
162  if ($changed_vars && $send_message) {
163  $ms = $GLOBALS['SQ_SYSTEM']->getMessagingService();
164  $ms->openQueue();
165  foreach ($changed_vars as $var_name => $values) {
166  $message_body = $var_name.' changed from '.var_export($values['old'], TRUE).' to '.var_export($values['new'], TRUE)."\n";
167  $config_name = ucwords(str_replace('_', ' ', get_class($this)));
168  $msg_reps = Array(
169  'setting' => $var_name,
170  'old_value' => var_export($values['old'], TRUE),
171  'new_value' => var_export($values['new'], TRUE),
172  );
173  $message = $ms->newMessage(Array(), 'config.'.get_class($this), $msg_reps);
174  $ms->enqueueMessage($message);
175  }
176  $ms->closeQueue();
177 
178  }// end if
179 
180  return TRUE;
181 
182 
183  }//end save()
184 
185 
193  function canAcquireLock()
194  {
195  // need to be root or a sys admin
196  return ($GLOBALS['SQ_SYSTEM']->userRoot() || $GLOBALS['SQ_SYSTEM']->userSystemAdmin());
197 
198  }//end canAcquireLock()
199 
200 
207  function writeAccess()
208  {
209  if (!parent::writeAccess()) return FALSE;
210  if (!$GLOBALS['SQ_SYSTEM']->runLevelEnables(SQ_SECURITY_PERMISSIONS)) {
211  return TRUE;
212  }
213 
214  // need to be root or a sys admin
215  return ($GLOBALS['SQ_SYSTEM']->userRoot() || $GLOBALS['SQ_SYSTEM']->userSystemAdmin());
216 
217  }//end writeAccess()
218 
219 
232  function paintBackend(&$o)
233  {
234  parent::paintBackend($o);
235 
236  $write_access = $this->writeAccess();
237 
238  $is_root = $GLOBALS['SQ_SYSTEM']->userRoot();
239 
240  $class = get_class($this);
241 
242  $o->openSection(translate('apache_request_notes'));
243 
244  if (!empty($this->request_notes)) {
245  $o->openField('');
246 
247  ?><table class="sq-backend-table">
248  <tr>
249  <th><?php echo translate('apache_note_name'); ?></th>
250  <th><?php echo translate('source'); ?></th>
251  <th><?php echo translate('value'); ?></th>
252  <th><?php echo translate('apache_example_usage'); ?></th>
253  <?php
254  if ($write_access) {
255  ?><th style="text-align: center;"><?php echo translate('remove_question'); ?></th><?php
256  }
257  ?>
258  </tr>
259  <?php
260  foreach (array_reverse($this->request_notes) as $note_name => $note_info) {
261  ?><tr>
262  <td><?php echo $note_name; ?></td>
263  <td><?php echo $note_info['type']; ?></td>
264  <td> <?php echo $note_info['keyword']; ?></td>
265  <td>
266  <?php
267  echo '%{'.$note_name.'}n';
268  ?>
269  </td>
270  <?php
271  if ($write_access) {
272  ?><td style="text-align: center;"><?php echo check_box('remove['.base64_encode($note_name).']'); ?></td><?php
273  }
274  ?>
275  </tr><?php
276  }
277  ?></table><?php
278 
279  $o->closeField();
280  }//end if no request notes
281 
282  if ($write_access) {
283  $o->openField(translate('new_question'));
284 
285  $elements = Array();
286 
287  ob_start();
288  $keyword_option = Array(
289  'sq_keyword_select_none' => '--',
290  'sq_keyword_select_user' => translate('apache_asset_type_user'),
291  'sq_keyword_select_asset' => translate('apache_asset_type_asset'),
292  );
293  // select what asset to list keywords for
294  combo_box('sq_keyword_type_option', $keyword_option, FALSE, 'none', 0, 'id="sq_asset_type_select" onchange="sqKeywordSelectUpdate(this)"');
295  echo '<br />';
296  $asset_type_selector = ob_get_clean();
297 
298  ?>
299  <script language="javascript">
300  function sqKeywordSelectUpdate(typeSelector)
301  {
302  /* hide all select input */
303  for (optionNum = 0; optionNum < typeSelector.options.length; optionNum ++) {
304  optionValue = typeSelector.options[optionNum].value;
305  if (optionValue != 'sq_keyword_select_none') {
306  document.getElementById(optionValue).style.display = 'none';
307  }
308  }
309 
310  /* display selected input */
311  if (typeSelector.value != 'sq_keyword_select_none') {
312  document.getElementById(typeSelector.value).style.display = '';
313 
314  document.getElementById(typeSelector.value).parentNode.parentNode.style.display = '';
315  document.getElementById('sq_request_note_new').parentNode.parentNode.style.display = '';
316  } else {
317  document.getElementById(typeSelector.value).parentNode.parentNode.style.display = 'none';
318  document.getElementById('sq_request_note_new').parentNode.parentNode.style.display = 'none';
319  }
320 
321  sqKeywordValueSelectUpdate(document.getElementById(typeSelector.value));
322  }
323 
324  function sqKeywordValueSelectUpdate(keywordSelector)
325  {
326  /* display or hide the custom keyword input box*/
327  document.getElementById('sq_custom_keyword').parentNode.parentNode.style.display = (keywordSelector.value == 'sq_custom_keyword_key') ? '' : 'none';
328 
329  }
330  </script>
331  <?php
332 
333  // standard and user asset keywords
334  foreach (Array('asset', 'user') as $asset_type) {
335  ob_start();
336  $asset = new $asset_type();
337  $options = $asset->getAvailableKeywords();
338  $options[SQ_APACHE_CONF_CUSTOM_KEYWORD_KEY] = translate('custom_keyword');
339  combo_box('sq_keyword_select_'.$asset_type, $options, FALSE, '', 0, 'id="sq_keyword_select_'.$asset_type.'" onchange="sqKeywordValueSelectUpdate(this)"');
340  $elements[] = ob_get_clean();
341  }
342 
343  ob_start();
344  hidden_field('sq_keyword_select_none', '');
345  $elements[] = ob_get_clean();
346 
347  // input box for the custom keyword
348  ob_start();
349  text_box('sq_custom_keyword', '', '', '', FALSE, 'id="sq_custom_keyword"');
350  $custom_keyword_input = ob_get_clean();
351 
352  // input box for new request notes
353  ob_start();
354  text_box('sq_request_note_new', '', '', '', FALSE, 'id="sq_request_note_new"');
355  $note_name_input = ob_get_clean();
356 
357  ?>
358  <table class="sq-backend-table">
359  <tr>
360  <th><?php echo translate('source'); ?></th>
361  <td><?php echo $asset_type_selector; ?></td>
362  </tr>
363  <tr>
364  <th><?php echo translate('value'); ?></th>
365  <td><?php echo implode(' ', $elements); ?></td>
366  </tr>
367  <tr>
368  <th><?php echo translate('keyword_name'); ?></th>
369  <td><?php echo $custom_keyword_input; ?></td>
370  </tr>
371  <tr>
372  <th><?php echo translate('apache_note_name'); ?></th>
373  <td><?php echo $note_name_input; ?></td>
374  </tr>
375  </table>
376  <script language="javascript">
377  sqKeywordSelectUpdate(document.getElementById('sq_asset_type_select'));
378  </script>
379  <?php
380 
381  $o->closeField();
382  }//end if write access
383  $o->sectionNote(translate('apache_note_example'));
384 
385  $o->closeSection();
386  if ($write_access) $o->commitButton('', TRUE);
387 
388  }//end paintBackend()
389 
390 
400  function processBackend(&$o)
401  {
402  $class = get_class_lower($this);
403 
404  // wipe the values of any checked entries
405  if (isset($_POST['remove']) && !empty($_POST['remove'])) {
406  $remove_notes = array_keys($_POST['remove']);
407  foreach ($remove_notes as $note_name) {
408  unset($this->request_notes[base64_decode($note_name)]);
409  }//end foreach
410  }
411 
412  // process any new request note
413  if ($new_request_note = $this->_getNewRequestNote($_POST)) {
414  $this->request_notes = array_merge($new_request_note, $this->request_notes);
415  }
416  $_POST[$class]['request_notes'] = $this->request_notes;
417 
418  return parent::processBackend($o);
419 
420  }//end processBackend()
421 
422 
435  function _paintConfigVar($var_name, $value, $var_export=TRUE)
436  {
437  // we're not going to save this as a constant, since it's an array
438  if ($var_name == 'request_notes') {
439  echo '$request_notes = '.var_export($value, 1).';'."\n";
440  } else {
441  parent::_paintConfigVar($var_name, $value, $var_export);
442  }
443  return TRUE;
444 
445  }//end _paintConfigVar()
446 
447 
456  function _getNewRequestNote($data)
457  {
458  // make sure we're really going to get a new request note
459  if (!isset($data['sq_keyword_type_option'])) {
460  return FALSE;
461  }
462  if ($data['sq_keyword_type_option'] == 'sq_keyword_select_none') {
463  return FALSE;
464  }
465 
466  $new_req_note = Array();
467 
468  // collect all the info we can
469  $req_note_name = $data['sq_request_note_new'];
470  $new_req_note['type'] = $data['sq_keyword_type_option'];
471  $new_req_note['keyword'] = $data[$new_req_note['type']];
472  if ($new_req_note['keyword'] == SQ_APACHE_CONF_CUSTOM_KEYWORD_KEY) {
473  $new_req_note['keyword'] = trim(strtolower($data['sq_custom_keyword']));
474  }
475 
476  // make our type an asset type rather
477  $new_req_note['type'] = str_replace('sq_keyword_select_', '', $new_req_note['type']);
478 
479  // need to have a name to create a new request note
480  if (empty($req_note_name)) return FALSE;
481 
482  // need to have a name to create a new request note
483  if (empty($new_req_note['keyword'])) return FALSE;
484 
485  // double check name
486  if (in_array($req_note_name, array_keys($this->request_notes))) {
487  trigger_localised_error('SYS0314', E_USER_NOTICE);
488  return Array();
489  }
490 
491  return Array($req_note_name => $new_req_note);
492 
493  }//end _getNewRequestNote()
494 
495 
496 }//end class
497 
498 ?>