public function actionOperations($id = 0) { if (isset($_POST['daemon_id'])) { $id = $_POST['daemon_id']; } $all = $id === 'all'; $did = (int) $id; $action = false; $params = array(); $acts = array('active_start', 'active_stop', 'active_restart', 'active_suspend', 'suspended_resume', 'run_chat', 'run_stop', 'run_restart', 'run_console', 'global_clean_players', 'global_clear_cmdcache'); foreach ($_POST as $k => $v) { if (in_array($k, $acts)) { $action = $k; break; } $params[$k] = $v; } if (($did || $all) && preg_match('/^run_/', $action)) { $cmd = substr($action, 4); if ($action == 'run_console') { if (!strlen(@$params['command'])) { Yii::app()->user->setFlash('operations', Yii::t('admin', 'No command to send.')); $this->redirect(array('operations', 'id' => $id)); } $cmd = 'run_s:' . $params['command']; } else { if ($action == 'run_chat') { if (!strlen(@$params['message'])) { Yii::app()->user->setFlash('operations', Yii::t('admin', 'No message to send.')); $this->redirect(array('operations', 'id' => $id)); } $from = strlen(@$params['from']) ? $params['from'] : Yii::app()->user->name; $cmd = 'mc:say <' . $from . '> ' . $params['message']; } } if ($all) { $res = McBridge::get()->globalCmd('server running:' . $cmd); } else { $res = array($did => McBridge::get()->cmd($did, 'server running:' . $cmd)); } $msg = ''; $runIds = array(); $failIds = array(); foreach ($res as $i => $r) { if (@$r['success']) { $runIds[] = $i; } else { $failIds[] = $i . ': ' . CHtml::encode(@$r['error']); } } $msg = ''; if (count($runIds)) { $msg .= Yii::t('admin', 'Action run for daemons:') . '<br/>' . implode(', ', $runIds); } if (count($failIds)) { $msg .= (count($runIds) ? '<br/><br/>' : '') . Yii::t('admin', 'Action failed for daemons:') . '<br/>' . implode(', ', $failIds); } if (!strlen($msg)) { $msg = Yii::t('admin', 'No daemons affected'); } Yii::app()->user->setFlash('operations', $msg); $this->redirect(array('operations', 'id' => $id) + $params); } else { if (($did || $all) && preg_match('/^(active_|suspended_)/', $action)) { $active = false; if (preg_match('/^active_/', $action)) { $active = true; } $cond = array('suspended' => $active ? 0 : 1); if (!$all) { $cond['daemon_id'] = $did; } $svs = Server::model()->findAllByAttributes($cond); $runIds = array(); $failIds = array(); foreach ($svs as $sv) { $res = $this->runServerAction($sv, $action, $params); if (!$res[0]) { $failIds[] = $sv->id . ': ' . $res[1]; } else { $runIds[] = $sv->id; } } $msg = ''; if (count($runIds)) { $msg .= Yii::t('admin', 'Action run for servers:') . '<br/>' . implode(', ', $runIds); } if (count($failIds)) { $msg .= (count($runIds) ? '<br/><br/>' : '') . Yii::t('admin', 'Action failed for servers:') . '<br/>' . implode(', ', $failIds); } if (!strlen($msg)) { $msg = Yii::t('admin', 'No servers affected'); } Yii::app()->user->setFlash('operations', $msg); $this->redirect(array('operations', 'id' => $id) + $params); } else { if (preg_match('/^global_/', $action)) { if ($action == 'global_clean_players') { $sql = 'delete from `player` where `level`=1 or `level`=(select `default_level`' . ' from `server` where `id`=`server_id`)'; $cmd = Yii::app()->bridgeDb->createCommand($sql); $del = $cmd->execute(); Yii::log('Player table cleanup: Deleted ' . $del . ' player entries'); Yii::app()->user->setFlash('operations', Yii::t('admin', 'Deleted {del} player entries.', array('{del}' => $del))); $this->redirect(array('operations', 'id' => $id)); } else { if ($action == 'global_clear_cmdcache') { CommandCache::clear(); Yii::log('Cleared command cache table'); Yii::app()->user->setFlash('operations', Yii::t('admin', 'Cleared command cache table.')); $this->redirect(array('operations', 'id' => $id)); } } } } } $this->render('operations', array('daemon_id' => $id)); }