OXID eShop CE  4.10.6
 All Classes Namespaces Files Functions Variables Pages
oxserversmanager.php
Go to the documentation of this file.
1 <?php
2 
10 {
11 
16 
21 
25  const CONFIG_NAME_FOR_SERVER_INFO = 'aServersData_';
26 
34  public function getServer($sServerId)
35  {
36  $aServerData = $this->getServerDataFromDb($sServerId);
37 
38  return $this->createServer($sServerId, $aServerData);
39  }
40 
46  public function saveServer($oServer)
47  {
48  $aServerData = array(
49  'id' => $oServer->getId(),
50  'timestamp' => $oServer->getTimestamp(),
51  'ip' => $oServer->getIp(),
52  'lastFrontendUsage' => $oServer->getLastFrontendUsage(),
53  'lastAdminUsage' => $oServer->getLastAdminUsage(),
54  'isValid' => $oServer->isValid()
55  );
56  $this->saveToDb($oServer->getId(), $aServerData);
57  }
58 
67  protected function createServer($sServerId, $aData = array())
68  {
70  $oAppServer = oxNew('oxApplicationServer');
71 
72  $oAppServer->setId($sServerId);
73  $oAppServer->setTimestamp($this->getServerParameter($aData, 'timestamp'));
74  $oAppServer->setIp($this->getServerParameter($aData, 'serverIp'));
75  $oAppServer->setLastFrontendUsage($this->getServerParameter($aData, 'lastFrontendUsage'));
76  $oAppServer->setLastAdminUsage($this->getServerParameter($aData, 'lastAdminUsage'));
77  $oAppServer->setIsValid($this->getServerParameter($aData, 'isValid'));
78 
79  return $oAppServer;
80  }
81 
90  protected function getServerParameter($aData, $sName)
91  {
92  return array_key_exists($sName, $aData) ? $aData[$sName] : null;
93  }
94 
100  public function getServers()
101  {
102  $aServersData = $this->getServersData();
103  $aServersData = $this->markInActiveServers($aServersData);
104  $aServersData = $this->deleteInActiveServers($aServersData);
105 
106  $aValidServers = array();
107 
108  foreach ($aServersData as $aServer) {
109  if ($aServer['isValid']) {
110  unset($aServer['isValid']);
111  unset($aServer['timestamp']);
112  $aValidServers[] = $aServer;
113  }
114  }
115 
116  return $aValidServers;
117  }
118 
124  public function deleteServer($sServerId)
125  {
126  $oConfig = oxRegistry::getConfig();
127  $sShopId = $oConfig->getBaseShopId();
128  $sVarName = self::CONFIG_NAME_FOR_SERVER_INFO.$sServerId;
129  $oDb = oxDb::getDb();
130  $sQ = "DELETE FROM oxconfig WHERE oxvarname = ? and oxshopid = ?";
131  $oDb->execute($sQ, array($sVarName, $sShopId));
132  }
133 
141  public function markInActiveServers($aServersData = null)
142  {
143  foreach ($aServersData as $sServerId => $aServerData) {
144  if ($aServerData['timestamp'] < oxRegistry::get("oxUtilsDate")->getTime() - self::NODE_AVAILABILITY_CHECK_PERIOD) {
145  $oServer = $this->getServer($sServerId);
146  $oServer->setIsValid(false);
147  $this->saveServer($oServer);
148  $aServersData[$sServerId]['isValid'] = false;
149  }
150  }
151  return $aServersData;
152  }
153 
161  public function deleteInActiveServers($aServersData)
162  {
163  foreach ($aServersData as $sServerId => $aServerData) {
164  if ($aServerData['timestamp'] < oxRegistry::get("oxUtilsDate")->getTime() - self::INACTIVE_NODE_STORAGE_PERIOD) {
165  $this->deleteServer($sServerId);
166  unset($aServersData[$sServerId]);
167  }
168  }
169  return $aServersData;
170  }
171 
177  public function getServersData()
178  {
179  $aServersData = array();
180  $result = $this->getAllServersDataConfigsFromDb();
181  if ($result != false && $result->recordCount() > 0) {
182  while (!$result->EOF) {
183  $sServerId = $this->parseServerIdFromConfig($result->fields['oxvarname']);
184  $aServersData[$sServerId] = (array)unserialize($result->fields['oxvarvalue']);
185  $result->moveNext();
186  }
187  }
188  return $aServersData;
189  }
190 
198  private function parseServerIdFromConfig($sVarName)
199  {
200  $iConstNameLength = strlen(self::CONFIG_NAME_FOR_SERVER_INFO);
201  $sId = substr($sVarName, $iConstNameLength);
202  return $sId;
203  }
204 
210  protected function getAllServersDataConfigsFromDb()
211  {
213  $oConfig = oxRegistry::getConfig();
214 
215  $sConfigsQuery = "SELECT oxvarname, " . $oConfig->getDecodeValueQuery() .
216  " as oxvarvalue FROM oxconfig WHERE oxvarname like ? AND oxshopid = ?";
217 
218  return $oDb->select($sConfigsQuery, array(self::CONFIG_NAME_FOR_SERVER_INFO."%", $oConfig->getBaseShopId()));
219  }
220 
228  protected function getServerDataFromDb($sServerId)
229  {
230  $aServerData = array();
231  $sData = $this->getConfigValueFromDB(self::CONFIG_NAME_FOR_SERVER_INFO.$sServerId);
232 
233  if ($sData != false ) {
234  $aServerData = (array)unserialize($sData);
235  }
236  return $aServerData;
237  }
238 
246  private function getConfigValueFromDB($sVarName)
247  {
248  $oConfig = oxRegistry::getConfig();
249  $oDb = oxDb::getDb();
250 
251  $sConfigsQuery = "SELECT " . $oConfig->getDecodeValueQuery() .
252  " as oxvarvalue FROM oxconfig WHERE oxvarname = ? AND oxshopid = ?";
253 
254  $sResult = $oDb->getOne($sConfigsQuery, array($sVarName, $oConfig->getBaseShopId()), false);
255 
256  return $sResult;
257  }
258 
265  protected function saveToDb($sServerId, $aServerData)
266  {
267  $oConfig = oxRegistry::getConfig();
268  $sVarName = self::CONFIG_NAME_FOR_SERVER_INFO.$sServerId;
269  $sConfigKey = $oConfig->getConfigParam('sConfigKey');
270  $sValue = serialize($aServerData);
271  $sVarType = 'arr';
272  $sShopId = $oConfig->getBaseShopId();
273  $oDb = oxDb::getDb();
274  if ($this->getConfigValueFromDB($sVarName) !== false) {
275  $sQ = "UPDATE oxconfig SET oxvarvalue=ENCODE( ?, ?) WHERE oxvarname = ? and oxshopid = ?";
276  $oDb->execute($sQ, array($sValue, $sConfigKey, $sVarName, $sShopId));
277  } else {
278  $sOxid = oxUtilsObject::getInstance()->generateUID();
279 
280  $sQ = "insert into oxconfig (oxid, oxshopid, oxmodule, oxvarname, oxvartype, oxvarvalue)
281  values(?, ?, '', ?, ?, ENCODE( ?, ?) )";
282  $oDb->execute($sQ, array($sOxid, $sShopId, $sVarName, $sVarType, $sValue, $sConfigKey));
283  }
284  }
285 }