/** * send data and process response * * @param array $data data to send * @return mixed response */ protected function command($data) { $this->connect(); // enter multibyte workaround mode if (ini_get('mbstring.func_overload') & 2) { if ($this->mbenc === null) { $this->mbenc = mb_internal_encoding(); } mb_internal_encoding('latin1'); } // build request $request = $this->serialize($data); // send request try { $this->TcpClient->write($request); } catch (SC_TcpClient_Exception $e) { throw new SC_Pushup_Exception($e->getMessage()); } // read response try { $response = $this->TcpClient->read(2048); if (empty($response)) { throw new SC_Pushup_Exception('empty response'); } // get response length $header = unpack('Nsize', substr($response, 0, 4)); // invalid response if ($header === false) { throw new SC_Pushup_Exception('invalid response, cannot unpack header data'); } // read remaining response data $size = $header['size'] + 4; while (strlen($response) < $size) { $response .= $this->TcpClient->read($size - strlen($response)); } } catch (SC_TcpClient_Exception $e) { $this->disconnect(); SC_Log::get('pushup')->error('network error: ' . $e->getMessage()); throw new SC_Pushup_Exception('network error'); } // process response $response = $this->deserialize($response); // leave multibyte workaround mode if ($this->mbenc !== null) { mb_internal_encoding($this->mbenc); $this->mbenc = null; } // check for errors if (!is_array($response) || empty($response)) { throw new SC_Pushup_Exception('command response error'); } if (($responseState = array_shift($response)) == self::COMMAND_ERROR) { throw new SC_Pushup_Exception(array_shift($response)); } // return result if (empty($response)) { switch ($responseState) { case self::COMMAND_OK: return true; case self::LOGIN_OK: return true; case self::LOGIN_FAILED: throw new SC_Pushup_Exception('login failed'); } } return $response; }
/** * config factory * * @param string registry index * @return mixed registry entry * @throws Exception if no entry is registerd for given index */ public static function get($index) { // check if config is already loaded if (isset(self::$data[$index])) { return self::$data[$index]; } // try magic autoloading switch ($index) { case 'cfg': require 'configs/base.php'; // set defaults if (empty($cfg['layout'])) { $cfg['layout'] = 'set1'; } $cfg['urlImg'] = $cfg['image_baseurl']; $cfg['urlIcons'] = $cfg['urlImg'] . $cfg['layout'] . '/ic/'; $cfg['urlGfx'] = $cfg['urlImg'] . $cfg['layout'] . '/gfx/'; $cfg['urlCSS'] = $cfg['urlImg'] . $cfg['layout'] . '/css/'; self::setConfig('cfg', $cfg); break; case 'lvs': require 'configs/LVSConfig.php'; self::setConfig('lvs', $cfg); break; default: $includePath = get_include_path(); $includePath = explode(':', $includePath); foreach ($includePath as $path) { if (substr($path, -1, 1) != DIRECTORY_SEPARATOR) { $path .= DIRECTORY_SEPARATOR; } $file = $path . 'configs' . DIRECTORY_SEPARATOR . $index . '.php'; if (is_readable($file)) { // prepare regEx if (null === self::$cacheConfigRegEx) { self::$cacheConfigRegEx = '~^(' . implode('|', self::$cacheConfigs) . ')~'; } // check if config is cachable if (!preg_match(self::$cacheConfigRegEx, $index)) { require $file; if (isset($cfg)) { self::setConfig($index, $cfg); } else { error_log('cant´t find variable $cfg in configfile ' . $file); } break 2; } // prepare and cache config $cacheFile = $path . self::$cacheDir . Localizer::getLanguage() . DIRECTORY_SEPARATOR . $index . '.php'; // load cached config if (!KWICK_DEV && is_readable($cacheFile)) { require $cacheFile; self::setConfig($index, $cfg); break 2; } // process $data = file_get_contents($file); Preprocessor::process($data, $index, Translatr::TYPE_CONFIG, $path); // save and load cache file try { File::put($cacheFile, $data); } catch (Exception $e) { SC_Log::get()->exception($e); } require $cacheFile; self::setConfig($index, $cfg); break 2; } } throw new Exception('No entry is registered and autoloading has failed for key "' . $index . '"'); } if (isset(self::$data[$index])) { return self::$data[$index]; } else { return false; } }