Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
config.inc
1 <?php
29 class Config extends MySource_Object
30 {
31 
32 
38  public $config_vars = Array();
39 
44  public $config_file = '';
45 
50  public $name = '';
51 
52 
57  function __construct()
58  {
59  parent::__construct();
60  $this->name = ucwords(str_replace('_', ' ', get_class($this)));
61 
62  }//end constructor
63 
64 
76  public function save($vars, $backup_existing=FALSE, $send_message=TRUE)
77  {
78  if (!$this->writeAccess()) {
79  trigger_localised_error('SYS0026', E_USER_WARNING, $this->name);
80  return FALSE;
81  }
82 
83  // attempt to load the config file to make sure we get any current settings
84  if (file_exists($this->config_file)) {
85  require_once $this->config_file;
86  }
87 
88  $changed_vars = Array();
89  ob_start();
90  foreach ($this->config_vars as $var_name => $data) {
91 
92  $current_value = (defined($var_name)) ? constant($var_name) : $data['default'];
93  if ($data['editable'] && isset($vars[$var_name]) && $current_value !== $vars[$var_name]) {
94 
95  // OK to make sure that everything is fine, we are going to force the issue of setting the type
96  // of the vars to that of the default
97  $default_type = gettype($data['default']);
98 
99  $value = $vars[$var_name];
100 
101  $changed_vars[$var_name] = Array('old' => $current_value, 'new' => $value);
102 
103  } else {
104  $value = $current_value;
105  }
106 
107  if (!$this->_paintConfigVar($var_name, $value)) {
108  ob_end_clean();
109  return FALSE;
110  }
111 
112  }//end foreach
113 
114  $str = '<'."?php\n".ob_get_contents().'?'.">\n";
115 
116  ob_end_clean();
117 
118  if ($backup_existing && file_exists($this->config_file)) {
119 
120  $i = 0;
121  do {
122  $i++;
123  $old_version = $this->config_file.'.'.$i;
124  } while (file_exists($old_version));
125 
126  if (!copy($this->config_file, $old_version)) {
127  return FALSE;
128  }
129 
130  }// endif
131 
132  if (SQ_PHP_CLI) {
133  echo "----------------------------------\n".($str)."----------------------------------\n";
134  }
135  require_once SQ_FUDGE_PATH.'/general/file_system.inc';
136  // make sure the directory exists
137  if (!create_directory(dirname($this->config_file))) {
138  return FALSE;
139  }
140 
141  // and save the file
142  if (!string_to_file($str, $this->config_file)) {
143  return FALSE;
144  }
145 
146  // Check if a message should be sent.
147  if ($changed_vars && $send_message) {
148  $ms = $GLOBALS['SQ_SYSTEM']->getMessagingService();
149  $ms->openQueue();
150  foreach ($changed_vars as $var_name => $values) {
151  $message_body = $var_name.' changed from '.var_export($values['old'], true).' to '.var_export($values['new'], true)."\n";
152  $config_name = ucwords(str_replace('_', ' ', get_class($this)));
153  $msg_reps = Array(
154  'setting' => $var_name,
155  'old_value' => var_export($values['old'], true),
156  'new_value' => var_export($values['new'], true),
157  );
158  $message = $ms->newMessage(Array(), 'config.'.get_class_lower($this), $msg_reps);
159  $ms->enqueueMessage($message);
160  }
161  $ms->closeQueue();
162 
163  }// end if
164 
165  return TRUE;
166 
167  }//end save()
168 
169 
181  protected function _paintConfigVar($var_name, $value, $var_export=TRUE)
182  {
183  echo "define('", $var_name, "', ";
184  if ($var_export) {
185  var_export($value);
186  } else {
187  echo $value;
188  }
189 
190  echo ");\n";
191 
192  return TRUE;
193 
194  }//end _paintConfigVar()
195 
196 
205  public function acquireLock()
206  {
207  if (!$this->canAcquireLock()) {
208  return 'You are not allowed to acquire the lock on '.$this->name;
209  }
210  return $GLOBALS['SQ_SYSTEM']->acquireLock(get_class_lower($this));
211 
212  }//end acquireLock()
213 
214 
221  public function canAcquireLock()
222  {
223  return TRUE;
224 
225  }//end canAcquireLock()
226 
227 
236  public function releaseLock()
237  {
238  return $GLOBALS['SQ_SYSTEM']->releaseLock(get_class_lower($this));
239 
240  }//end releaseLock()
241 
242 
251  public function updateLock()
252  {
253  return $GLOBALS['SQ_SYSTEM']->updateLock(get_class_lower($this));
254 
255  }//end updateLock()
256 
257 
265  public function getLockInfo()
266  {
267  return $GLOBALS['SQ_SYSTEM']->getLockInfo(get_class_lower($this));
268 
269  }//end getLockInfo()
270 
271 
278  public function writeAccess()
279  {
280  if (!isset($GLOBALS['SQ_SYSTEM'])) return TRUE;
281  if (!$GLOBALS['SQ_SYSTEM']->runLevelEnables(SQ_SECURITY_PERMISSIONS)) {
282  return TRUE;
283  }
284 
285  $lock_info = $this->getLockInfo();
286  if (!empty($lock_info) && $lock_info['userid'] == $GLOBALS['SQ_SYSTEM']->currentUserId()) {
287  return TRUE;
288  }
289  return FALSE;
290 
291  }//end writeAccess()
292 
293 
306  public function paintBackend(&$o)
307  {
308  if (is_null($this->config_file) === FALSE) {
309  require_once $this->config_file;
310  }
311 
312  $map_array = Array (
313  'system_config' => 'system_configuration',
314  'squiz_server_config' => 'squiz_server_configuration',
315  'apache_config' => 'apache_configuration',
316  'hipo_config' => 'hipo_configuration',
317  'external_tools_config' => 'external_tools_configuration',
318  'password_rules_config' => 'password_rules_configuration',
319  'messaging_service_config' => 'messaging_service_configuration',
320  'proxy_authentication_config' => 'proxy_configuration',
321  'context_config' => 'context_configuration',
322  );
323 
324  if ($o->_persistant_get_vars['config_screen'] = $map_array[$o->_persistant_get_vars['config_screen']]) {
325  $config_screen = $o->_persistant_get_vars['config_screen'];
326  }
327 
328  $lock = $this->getLockInfo();
329  $have_lock = (!empty($lock) && $GLOBALS['SQ_SYSTEM']->currentUserId() == $lock['userid']);
330 
331  if (!empty($lock) || $this->canAcquireLock()) {
332 
333  $o->openSection(translate('locking_/_editing'));
334  $o->openRaw();
335  ?>
336  <table border="0" cellspacing="3" cellpadding="1">
337  <tr>
338  <td>
339  <?php sq_print_icon(sq_web_path('lib').'/web/images/icons/'.((empty($lock)) ? 'un' : '').'locked.png', 16, 16); ?>
340  </td>
341  <td>
342  <p class="sq-lock-message">
343  <?php
344  if (!empty($lock)) {
345  // this asset is currently locked
346  // so display message to the user
347  $user = $GLOBALS['SQ_SYSTEM']->am->getAsset($lock['userid']);
348 
349  $now = time();
350 
351  require_once SQ_FUDGE_PATH.'/general/datetime.inc';
352  $expires_in = easy_time_total(($lock['expires'] - $now), TRUE);
353  if (!$expires_in) $expires_in = '1 second';
354  $expires_in = translate('due_to_expire', $expires_in);
355  if ($have_lock) {
356  echo translate('release_lock', translate('release_lock_button'), translate($config_screen));
357  echo '<br />';
358  }
359  echo $expires_in;
360 
361  } else {
362  echo translate('acquire_lock', translate('acquire_lock_button'), translate($config_screen));
363  }
364  echo '</p>';
365  if (!empty($lock)) {
366  if ($have_lock) {
367  submit_button('sq_lock_release_manual', translate('release_lock_button'), 'set_hidden_field("process_form", "0");', 'accesskey="r"');
368  }
369  } else {
370  submit_button('sq_lock_acquire', translate('acquire_lock_button'), 'set_hidden_field("process_form", "0");', 'accesskey="a"');
371  }
372  ?>
373  </td>
374  </tr>
375  </table>
376  <?php
377  $o->closeRaw();
378  $o->closeSection();
379 
380  }//end if (!empty($lock) || $this->canAcquireLock())
381 
382  }//end paintBackend()
383 
384 
394  public function processBackend(&$o)
395  {
396  $saved = FALSE;
397  if (!empty($_POST['process_form'])) {
398 
399  if (!empty($_POST[get_class_lower($this)])) {
400  $saved = $this->save($_POST[get_class_lower($this)]);
401  }
402  }
403 
404  if (!empty($_POST['sq_lock_release']) || !empty($_POST['sq_lock_release_manual'])) {
405  $this->releaseLock();
406  }
407 
408  // if there is no lock currently and we want it
409  if (!empty($_POST['sq_lock_acquire'])) {
410  $this->acquireLock();
411  }
412 
413  // if the save was successful, we need to redirect because the constants
414  // that are already defined cannot be redefined in this script execution
415  if ($saved) $o->setRedirect($o->getCurrentLocation());
416 
417  return $saved;
418 
419  }//end processBackend()
420 
421 
422 }//end class
423 
424 ?>