public function loadModel($id) { $model = ConfigFile::model()->findByPk((int) $id); if ($model === null) { throw new CHttpException(404, Yii::t('mc', 'The requested page does not exist.')); } return $model; }
public function actionEditConfig($id, $config, $ro, $file, $dir) { Yii::app()->user->can($id, 'edit configs', true); $cfg = ConfigFile::model()->findByPk($config); if (!$cfg || !preg_match('/' . $cfg->file . '/', $file)) { throw new CHttpException(404, Yii::t('mc', 'Config file not found')); } $rules = CJSON::decode($cfg->options); $ro = $ro == Yii::t('mc', 'True') ? true : false; $error = false; $name = $this->replData($cfg->name, $file, $dir); if (!$ro && @$_POST['save'] === 'true') { $data = ''; if ($cfg->type == 'properties') { $acceptAll = @$rules['*']['visible'] ? true : false; if (@count($_POST['option'])) { foreach ($_POST['option'] as $opt => $val) { if ($acceptAll || !isset($rules[$opt]['visible']) || $rules[$opt]['visible']) { $data .= $opt . '=' . $val . "\n"; } } } } else { $data = $_POST['list']; } $data = $data ? str_replace(array('\\', ' ;'), array('\\\\', ' \\;'), $data) : ''; $data = preg_replace('/\\n\\r?/', ' ;', $data); $d = null; if (!McBridge::get()->serverCmd($id, 'cfgfile set' . $cfg->type . ':' . $file . ':' . $dir . ':' . $data, $d)) { $error = McBridge::get()->lastError(); } else { if (@$d[0]['accepted'] != 'True') { $error = isset($d[0]['message']) ? $d[0]['message'] : Yii::t('mc', 'Error updating config file!'); } else { Yii::app()->user->setFlash('server', Yii::t('mc', 'Config File saved.')); $this->redirect(array('configs', 'id' => $id)); } } } $data = null; if (!McBridge::get()->serverCmd($id, 'cfgfile get' . $cfg->type . ':' . $file . ':' . $dir . ':', $data)) { $error = McBridge::get()->lastError(); } $opts = array(); $list = ''; if ($data && $cfg->type == 'properties') { if (@is_array($rules)) { foreach ($rules as $match => $info) { if (isset($info['select']) && $info['select'] === 'bool') { $info['select'] = array('true' => Yii::t('mc', 'Enabled'), 'false' => Yii::t('mc', 'Disabled')); } $found = false; foreach ($data as $k => $v) { $opt = array(); $opt['name'] = $v['option']; $opt['value'] = $v['value']; $opt['visible'] = true; if (strlen($opt['name']) && $match != '*' && $match != $opt['name']) { continue; } $found = true; if (isset($info['visible'])) { $opt['visible'] = $info['visible']; } if (isset($info['name'])) { $opt['name'] = $info['name']; } if (isset($info['select'])) { $opt['select'] = $info['select']; } if (isset($info['default']) && !strlen($opt['value'])) { $opt['value'] = $info['default']; } if ($opt['visible']) { $opts[$v['option']] = $opt; } if ($match != '*') { unset($data[$k]); break; } } if (!$found && $match != '*' && !@$info['nocreate'] && (!isset($info['visible']) || $info['visible'])) { $opts[$match] = $info; if (!@$opts[$match]['name']) { $opts[$match]['name'] = $match; } $opts[$match]['value'] = @$info['default']; } } } } else { if ($data) { foreach ($data as $line) { if (isset($line['line'])) { $list .= $line['line'] . "\n"; } } } } $this->render('editConfig', array('dataProvider' => new CArrayDataProvider($data, array('sort' => array('attributes' => array('name')), 'pagination' => array('pageSize' => 10))), 'type' => $cfg->type, 'list' => $list, 'options' => $opts, 'name' => $name, 'model' => $this->loadModel($id), 'error' => $error, 'ro' => $ro)); }