public function onRequest($request, $response) { $filename = ZPHP::getRootPath() . DS . 'webroot' . $request->server['path_info']; if (is_file($filename)) { //解析静态文件 $response->header("Content-Type", $this->getMime($filename) . '; charset=utf-8'); $response->end(file_get_contents($filename)); return; } $param = []; if (!empty($request->get)) { $param = $request->get; } if (!empty($request->post)) { $param += $request->post; } $_SERVER['HTTP_USER_AGENT'] = $request->header['user-agent']; Request::parse($param); Request::setViewMode('Php'); Request::setHttpServer(1); Response::setResponse($response); try { $result = ZRoute::route(); } catch (\Exception $e) { $model = Formater::exception($e); $model['_view_mode'] = 'Json'; $result = Response::display($model); } $response->end($result); Request::setViewMode(ZConfig::getField('project', 'view_mode', 'Json')); Request::setHttpServer(0); }
function testFormater() { // BytesToString $res = Formater::Bytes2String(23552); $this->assertEqual($res, "23 KB", "BytesToString"); //Date2Fuzzy $res = Formater::Date2Fuzzy("2006-05-04 00:00:01"); $this->assertEqual($res, "May 4 at 12:00am", "Date2Fuzzy"); //CompareDates $res = Formater::CompareDates(date("Y-m-d"), date("Y-m-d", time() - 102240)); $this->assertEqual($res, 1, "CompareDates"); //TimeStringToStamp $res = Formater::TimeString2Stamp(date("Y-m-d")); $this->assertEqual($res, mktime(0, 0, 0, date("m"), date("d"), date("Y")), "TimeStringToStamp"); //TimeStampToString $res = Formater::TimeStamp2String(time(), "Y-m-d"); $this->assertEqual($res, date("Y-m-d"), "TimeStampToString"); //TimeFormat $res = Formater::TimeFormat(time(), "Y-m-d"); $this->assertEqual($res, date("Y-m-d"), "TimeFormat"); //TimeToHumanReadable $res = Formater::Time2HumanReadable(86400); $this->assertTrue(strcasecmp($res, '1 day') == 0, "TimeToHumanReadable"); //FuzzyTimeString $res = Formater::FuzzyTimeString(time()); $this->assertTrue(stristr($res, "today"), "FuzzyTimeString"); // time ago $time = time() - 58 - 60 * 2; $res = Formater::TimeAgo($time); $this->assertEqual($res, "2 mins 58 seconds ago", "TimeAgo"); }
public function getViews() { $files = parent::getViews(); $sName = array(); $TDName = array(); $TRName = array(); foreach ($this->getFields() as $field) { if ($field['visible']) { $sName[] = '{ "sName": "' . $field['varname'] . '" }'; if ($field['type'] == "datetime") { $TDName[] = "<td>{{ entity." . $field['varname'] . "|date('d/m/Y H:i:s') }}</td>"; } else { $TDName[] = "<td>{{ entity." . $field['varname'] . " }}</td>"; } $TRName[] = "<th>" . $field['name'] . "</th>"; } } foreach ($files as $k => $body) { $body = str_replace('%s_fields%', implode(',', $sName), $body); $body = str_replace('%sname_fields%', implode(',', $sName), $body); $body = str_replace('%sortable_entity_fields%', implode('', $TRName), $body); $body = str_replace('%sortable_entity_values%', implode('', $TDName), $body); $files[$k] = $body; } return $files; }
public function onTask($server, $taskId, $fromId, $data) { Request::parse($data); Request::addParams('taskId', $taskId . '_' . $fromId); try { ZRoute::route(); } catch (\Exception $e) { $model = Formater::exception($e); ZLog::info('exception', $model); } }
public function getViews() { $files = parent::getViews(); $sName = array(); $TRName = array(); foreach ($this->getFields() as $field) { if ($field['visible']) { $TRName[] = "<th>" . $field['name'] . "</th>"; $sName[] = '{ "sName": "' . $field['varname'] . '" }'; } } foreach ($files as $k => $body) { $body = str_replace('%sname_fields%', implode(',', $sName), $body); $body = str_replace('%datagrid_entity_fields%', implode('', $TRName), $body); $body = str_replace('%sname_fields%', implode(',', $sName), $body); $files[$k] = $body; } return $files; }
public function xListServersAction() { $this->request->defineParams(array('roleId' => array('type' => 'int'), 'farmId' => array('type' => 'int'), 'farmRoleId' => array('type' => 'int'), 'serverId', 'hideTerminated' => array('type' => 'bool'), 'sort' => array('type' => 'json', 'default' => array('property' => 'id', 'direction' => 'ASC')))); $sql = "SELECT servers.*, farms.name AS farm_name, roles.name AS role_name FROM servers LEFT JOIN farms ON servers.farm_id = farms.id\n\t\t\t\tLEFT JOIN roles ON roles.id = servers.role_id WHERE servers.env_id='{$this->getEnvironmentId()}'"; if ($this->getParam('farmId')) { $sql .= " AND farm_id='{$this->getParam('farmId')}'"; } if ($this->getParam('farmRoleId')) { $sql .= " AND farm_roleid='{$this->getParam('farmRoleId')}'"; } if ($this->getParam('roleId')) { $sql .= " AND role_id='{$this->getParam('roleId')}'"; } if ($this->getParam('serverId')) { $sql .= " AND server_id={$this->db->qstr($this->getParam('serverId'))}"; } if ($this->getParam('hideTerminated')) { $sql .= " AND servers.status != '" . SERVER_STATUS::TERMINATED . "'"; } $response = $this->buildResponseFromSql($sql, array("server_id", "farm_id", "`farms`.`name`", "remote_ip", "local_ip", "`servers`.`status`"), NULL, false); foreach ($response["data"] as &$row) { try { $dbServer = DBServer::LoadByID($row['server_id']); $row['cloud_server_id'] = $dbServer->GetCloudServerID(); $row['ismaster'] = $dbServer->GetProperty(SERVER_PROPERTIES::DB_MYSQL_MASTER) || $dbServer->GetProperty(Scalr_Db_Msr::REPLICATION_MASTER); $row['cloud_location'] = $dbServer->GetCloudLocation(); if ($dbServer->platform == SERVER_PLATFORMS::EC2) { $loc = $dbServer->GetProperty(EC2_SERVER_PROPERTIES::AVAIL_ZONE); if ($loc && $loc != 'x-scalr-diff') { $row['cloud_location'] .= "/" . substr($loc, -1, 1); } } if ($dbServer->platform == SERVER_PLATFORMS::EC2) { $row['has_eip'] = $this->db->GetOne("SELECT id FROM elastic_ips WHERE server_id = ?", array($dbServer->serverId)); } if ($dbServer->GetFarmRoleObject()->GetRoleObject()->hasBehavior(ROLE_BEHAVIORS::MONGODB)) { $shardIndex = $dbServer->GetProperty(Scalr_Role_Behavior_MongoDB::SERVER_SHARD_INDEX); $replicaSetIndex = $dbServer->GetProperty(Scalr_Role_Behavior_MongoDB::SERVER_REPLICA_SET_INDEX); $row['cluster_position'] = "{$shardIndex}-{$replicaSetIndex}"; } } catch (Exception $e) { } $row['isrebooting'] = $this->db->GetOne("SELECT value FROM server_properties WHERE server_id=? AND `name`=?", array($row['server_id'], SERVER_PROPERTIES::REBOOTING)); $row['is_szr'] = $dbServer->IsSupported("0.5"); $row['initDetailsSupported'] = $dbServer->IsSupported("0.7.181"); if ($dbServer->GetProperty(SERVER_PROPERTIES::SZR_IS_INIT_FAILED) && in_array($dbServer->status, array(SERVER_STATUS::INIT, SERVER_STATUS::PENDING))) { $row['isInitFailed'] = 1; } $launchError = $dbServer->GetProperty(SERVER_PROPERTIES::LAUNCH_ERROR); if ($launchError) { $row['launch_error'] = "1"; } $row['agent_version'] = $dbServer->GetProperty(SERVER_PROPERTIES::SZR_VESION); $row['agent_update_needed'] = $dbServer->IsSupported("0.7") && !$dbServer->IsSupported("0.7.189"); $row['agent_update_manual'] = !$dbServer->IsSupported("0.5"); $row['os_family'] = $dbServer->GetOsFamily(); $row['flavor'] = $dbServer->GetFlavor(); if (!$row['flavor']) { $row['flavor'] = ''; } if ($dbServer->status == SERVER_STATUS::RUNNING) { $tm = (int) $dbServer->GetProperty(SERVER_PROPERTIES::INITIALIZED_TIME); if (!$tm) { $tm = (int) strtotime($row['dtadded']); } if ($tm > 0) { $row['uptime'] = Formater::Time2HumanReadable(time() - $tm, false); } } else { $row['uptime'] = ''; } $r_dns = $this->db->GetOne("SELECT value FROM farm_role_settings WHERE farm_roleid=? AND `name`=?", array($row['farm_roleid'], DBFarmRole::SETTING_EXCLUDE_FROM_DNS)); $row['excluded_from_dns'] = !$dbServer->GetProperty(SERVER_PROPERTIES::EXCLUDE_FROM_DNS) && !$r_dns ? false : true; } $this->response->data($response); }
AND ( severity='".E_ERROR."' OR severity='".E_CORE_ERROR."' OR severity='".E_USER_ERROR."' ) "); $warns = $db->GetOne(" SELECT COUNT(*) FROM syslog WHERE transactionid='{$row['transactionid']}' AND ( severity='".E_WARNING."' OR severity='".E_USER_WARNING."' OR severity='".E_CORE_WARNING."' ) "); $dtadded = Formater::FuzzyTimeString(strtotime($row["dtadded"])); $firstEntry = htmlentities($row["message"], ENT_QUOTES, "UTF-8"); $response["data"][] = array ( "id" => $row["id"], "dtadded" => $dtadded, "warns" => $warns, "errors" => $errors, "firstEntry" => $firstEntry, "transactionid" => $row["transactionid"] ); } print json_encode($response);
public function xGetTerminationDetailsAction() { $dbFarm = DBFarm::LoadByID($this->getParam('farmId')); $this->user->getPermissions()->validate($dbFarm); $outdatedFarmRoles = $this->db->GetAll("SELECT id FROM farm_roles WHERE farmid=?", array($dbFarm->ID)); $data = array(); $isMongoDbClusterRunning = false; $isMysql = false; foreach ($outdatedFarmRoles as $farmRole) { $dbFarmRole = DBFarmRole::LoadByID($farmRole['id']); if (!$isMongoDbClusterRunning) { $isMongoDbClusterRunning = $dbFarmRole->GetRoleObject()->hasBehavior(ROLE_BEHAVIORS::MONGODB) && $dbFarmRole->GetSetting(Scalr_Role_Behavior_MongoDB::ROLE_CLUSTER_STATUS) != Scalr_Role_Behavior_MongoDB::STATUS_TERMINATED; } if (!$isMysql) { $isMysql = $dbFarmRole->GetRoleObject()->hasBehavior(ROLE_BEHAVIORS::MYSQL); } $row = array('dtLastSync' => strtotime($dbFarmRole->dtLastSync) ? Formater::FuzzyTimeString(strtotime($dbFarmRole->dtLastSync), false) : "Never", 'name' => $dbFarmRole->GetRoleObject()->name, 'id' => $dbFarmRole->ID, 'isBundleRunning' => $this->db->GetOne("SELECT id FROM bundle_tasks WHERE status NOT IN ('success','failed') AND role_id=? AND farm_id IN (SELECT id FROM farms WHERE client_id=?)", array($dbFarmRole->RoleID, $dbFarm->ClientID))); foreach ($dbFarmRole->GetServersByFilter(array('status' => SERVER_STATUS::RUNNING)) as $dbServer) { $row['servers'][] = array('server_id' => $dbServer->serverId, 'remoteIp' => $dbServer->remoteIp); } $data[] = $row; } $this->response->data(array('roles' => $data, 'isMongoDbClusterRunning' => $isMongoDbClusterRunning, 'isMysqlRunning' => $isMysql, 'farmId' => $dbFarm->ID)); }