public static function updateConfig($key, $value) { /** @var XenForo_Application $app */ $app = XenForo_Application::getInstance(); $path = $app->getRootDir() . '/library/config.php'; $originalContents = file_get_contents($path); $varNamePattern = '#(\\n|^)(?<varName>\\$config'; foreach (explode('.', $key) as $i => $keyPart) { // try to match the quote $varNamePattern .= '\\[([\'"]?)' . preg_quote($keyPart, '#') . '\\' . ($i + 3) . '\\]'; } $varNamePattern .= ').+(\\n|$)#'; $candidates = array(); $offset = 0; while (true) { if (!preg_match($varNamePattern, $originalContents, $matches, PREG_OFFSET_CAPTURE, $offset)) { break; } $offset = $matches[0][1] + strlen($matches[0][0]); $candidates[] = $matches; } if (count($candidates) !== 1) { XenForo_Helper_File::log(__METHOD__, sprintf('count($candidates) = %d', count($candidates))); return; } $matches = reset($candidates); $replacement = $matches[1][0] . $matches['varName'][0] . ' = ' . var_export($value, true) . ';' . $matches[5][0]; $contents = substr_replace($originalContents, $replacement, $matches[0][1], strlen($matches[0][0])); DevHelper_Generator_File::writeFile($path, $contents, true, false); }
public static function deleteAllCached() { foreach (self::$_cached as $url => $tempFile) { if (XenForo_Application::debugMode()) { $fileSize = @filesize($tempFile); } $deleted = @unlink($tempFile); if (XenForo_Application::debugMode()) { XenForo_Helper_File::log(__CLASS__, call_user_func_array('sprintf', array('delete %s -> %s, %s, %d bytes', $url, $tempFile, $deleted ? 'succeeded' : 'failed', !empty($fileSize) ? $fileSize : 0))); } } self::$_cached = array(); }
public static function assertNoErrors(XenForo_DataWriter $dw, $checkMethod, $dataType) { switch (strtolower($checkMethod)) { case 'delete': case 'predelete': $checkMethod = 'preDelete'; break; case 'save': case 'presave': default: $checkMethod = 'preSave'; break; } $dw->{$checkMethod}(); if ($errors = $dw->getErrors()) { $errorString = implode("\n\t", $errors); XenForo_Helper_File::log('webdav-error', sprintf("%s:\n\t%s\n\t%s", $dataType, $dw->get('title'), $errorString), false); // Note that in order to have Dreamweaver actually show an error, we have to use 'Forbidden'. throw new Sabre_DAV_Exception_Forbidden($errorString); } }
public function _makeRequest($method, $path = '', $params = null, $headers = array(), $data = null) { if (empty($params)) { $params = array(); } if (!is_array($headers)) { $headers = array($headers); } if (isset($headers['Content-MD5'])) { unset($headers['Content-MD5']); } if (is_resource($data)) { throw new Zend_Service_Amazon_S3_Exception("No support for stream data"); } $data = strval($data); $headers['x-amz-content-sha256'] = Zend_Crypt::hash('sha256', $data); $headers['x-amz-date'] = sprintf('%sT%sZ', gmdate('Ymd', XenForo_Application::$time), gmdate('His', XenForo_Application::$time)); $headers['Host'] = parse_url($this->_endpoint, PHP_URL_HOST); $retryCount = 0; // build the end point (with path) $endpoint = clone $this->_endpoint; $path = '/' . $path; $endpoint->setPath($path); $this->addSignatureAws4($method, $path, $params, $headers); $client = self::getHttpClient(); $client->resetParameters(); $client->setUri($endpoint); $client->setAuth(false); $client->setHeaders($headers); if (is_array($params)) { foreach ($params as $name => $value) { $client->setParameterGet($name, $value); } } if ($method == 'PUT' && $data !== null) { if (!isset($headers['Content-type'])) { $headers['Content-type'] = self::getMimeType($path); } $client->setRawData($data, $headers['Content-type']); } do { $retry = false; $response = $client->request($method); $responseCode = $response->getStatus(); if (XenForo_Application::debugMode() && $responseCode != 200) { XenForo_Helper_File::log(__METHOD__, sprintf("%s %s -> %d %s\n\n", $method, var_export($headers, true), $responseCode, $response->getBody())); } // some 5xx errors are expected, so retry automatically if ($responseCode >= 500 && $responseCode < 600 && $retryCount <= 5) { $retry = true; $retryCount++; sleep($retryCount / 4 * $retryCount); } elseif ($responseCode == 307) { // need to redirect, new S3 endpoint given // this should never happen as Zend_Http_Client will redirect automatically } elseif ($responseCode == 100) { // 'OK to Continue'; } } while ($retry); return $response; }
private static function _saveConfig(array $config) { $configOptionId = self::_getConfigOptionId($config['apiUrl']); /** @var XenForo_DataWriter_Option $optionDw */ $optionDw = XenForo_DataWriter::create('XenForo_DataWriter_Option'); $optionDw->setExistingData($configOptionId); $optionDw->set('option_value', $config); if ($optionDw->save()) { XenForo_Application::getOptions()->set($configOptionId, $config); if (XenForo_Application::debugMode()) { XenForo_Helper_File::log(__CLASS__, sprintf('%s($apiUrl=%s)', __METHOD__, $config['apiUrl'])); } return true; } return false; }
public function assertNoDwErrors(XenForo_DataWriter $dw, $checkMethod, $dataType) { switch (strtolower($checkMethod)) { case 'delete': case 'predelete': $checkMethod = 'preDelete'; break; case 'save': case 'presave': default: $checkMethod = 'preSave'; break; } $dw->{$checkMethod}(); if ($errors = $dw->getErrors()) { $filePath = $this->getDirectory($dw->getMergedData()) . self::$s . $this->getFileName($dw->getMergedData()); if (count($errors) == 1) { $error = htmlspecialchars_decode($errors[key($errors)]); // Yes Nathan, I could use regex but fuu if (strpos($error, 'Line ') === 0) { $pos = strpos($error, ':'); $line = substr($error, 5, $pos - 5); $error = trim(substr($error, $pos + 1)); } $errorString = 'E: ' . $dataType . ' Error: ' . $error . " in " . $filePath; if (isset($line)) { $errorString .= ", line {$line} \n\n"; } else { $errorString .= "\n\n"; } } else { foreach ($errors as &$err) { $err = htmlspecialchars_decode($err); } $errorString = 'E: ' . $dataType . " Error: \nin " . $filePath . "\n"; $errorString .= implode("\n", $errors) . "\n\n"; } XenForo_Helper_File::log('devtools-error', str_replace("\n", ' ', $errorString)); $this->printDebugInfo($errorString); die; // throwing wasn't working with console so just doing this throw new XenForo_Exception($errorString); } }