public function process() { $params = $this->PWE->getURL()->getParamsAsArray(); $file_path = '/' . $this->dl_base . '/' . FilesystemHelper::protectAgainsRelativePaths(implode('/', $params)); $file = $this->PWE->getRootDirectory() . $file_path; if (!is_file($file)) { PWELogger::error("File not found: %s, referer: %s", $file_path, $_SERVER['HTTP_REFERER']); throw new HTTP4xxException("File not found", HTTP4xxException::NOT_FOUND); } $this->recordDownload($file); throw new HTTP3xxException($file_path); }
public function registerModule($name) { PWELogger::debug("Registering module %s", $name); $mod =& $this->getModuleNode($name); try { $modClass = new ReflectionClass($name); if ($modClass->isInstantiable()) { if ($modClass->implementsInterface('PWE\\Modules\\Setupable')) { PWELogger::debug("Setting up %s", $name); /** @noinspection PhpUndefinedMethodInspection */ $name::setup($this->PWE, $mod); } } $this->saveRegistry(); } catch (\ReflectionException $e) { PWELogger::error("Failed to register module %s: %s", $name, $e); } }
public function testError_exc() { PWELogger::setStdErr("php://stdout"); PWELogger::error("Errors are bad: %s", new ExceptionExpected()); PWELogger::debug(new ExceptionExpected()); }
require_once __DIR__ . '/PWE/Lib/Smarty/SmartyAssociative.php'; require_once __DIR__ . '/PWE/Modules/Outputable.php'; require_once __DIR__ . '/PWE/Core/PWEURL.php'; require_once __DIR__ . '/PWE/Auth/PWEUserAuthController.php'; $PWECore = new PWECore(); PWEAutoloader::setPWE($PWECore); $uri = $_SERVER['REDIRECT_URL'] ? $_SERVER['REDIRECT_URL'] : $_SERVER['REQUEST_URI']; $started = microtime(true); try { require_once dirname($_SERVER['SCRIPT_FILENAME']) . '/cfg.php'; echo $PWECore->process($uri); PWELogger::debug('Response headers: %s', headers_list()); } catch (\Exception $e) { try { if ($e->getCode() >= 500 || $e->getCode() <= 100) { PWELogger::error('Exception occured at page %s: %s', $uri, $e); } elseif ($e->getCode() >= 400) { PWELogger::info('Exception occured at page %s: %s', $uri, $e); } $PWECore->sendHTTPHeader("Content-Type: text/html"); $PWECore->sendHTTPStatusCode($e->getCode()); echo $PWECore->getErrorPage($e); } catch (\Exception $e2) { echo "<textarea cols='100' rows='25' readonly='readonly'>"; echo $e2->__toString(); echo "</textarea><br>"; echo "<p>Caused by:</p>"; echo "<textarea cols='100' rows='25' readonly='readonly'>"; echo $e->__toString(); echo "</textarea><br>"; die($e2->getMessage());
public function ArrayToFile(array &$a, $fname, $comment = false) { PWELogger::debug("Saving XML file: %s", $fname); if (!is_dir(dirname($fname))) { throw new InvalidArgumentException("Directory for saving not exists: " . dirname($fname)); } $bak_dir = $this->use_cache ? $this->cache_dir : dirname($fname); $bak_file = $bak_dir . '/' . basename($fname) . '.' . posix_geteuid() . ".bak"; if (is_file($bak_file)) { try { unlink($bak_file); } catch (PHPFatalException $e) { PWELogger::warn('Cannot remove old backup file: %s', $bak_file); } } if (is_file($fname)) { try { copy($fname, $bak_file); } catch (PHPFatalException $e) { PWELogger::warn("Unable to create backup file: %s", $bak_file); } } try { // записываем результат $f = fopen($fname, 'w+'); if ($f && flock($f, LOCK_EX)) { // открыли и заблокировались? fputs($f, "<?xml version='1.0' encoding='UTF-8'?>"); if ($comment) { fputs($f, "\n\n<!-- {$comment} -->\n"); } $this->generateNode($a, 0, $f); //обратимся к Дзену! fflush($f); flock($f, LOCK_UN); fclose($f); } else { throw new Exception("Unable to create new result file {$fname}"); } PWELogger::debug("Checking written file results"); $tmp_arr = array(); $this->FileToArray($fname, $tmp_arr); } catch (Exception $e) { PWELogger::error("Resulting xml file is broken: %s", $fname, $e); if (is_file($bak_file)) { copy($bak_file, $fname); } else { if (is_file($fname)) { unlink($fname); } } throw $e; } return true; }
/** * @param $args * @param $ext * @param $dir * @return string */ private function getRenderedPage($args, $ext, $dir) { if (pathinfo($args[0], PATHINFO_EXTENSION) != $ext) { $args[0] .= '.' . $ext; } else { throw new HTTP3xxException(substr($args[0], 0, strlen($args[0]) - strlen($ext) - 1)); } $file = $dir . '/' . str_replace(':', DIRECTORY_SEPARATOR, $args[0]); $real_file = realpath($file); $pos = strpos($real_file, realpath($dir) . DIRECTORY_SEPARATOR); PWELogger::debug("Test path for sanity: %s ; %s ; %s", $file, $real_file, $pos); if ($pos === false) { PWELogger::warn("Possible injection attempt: %s", $file); throw new HTTP4xxException("Wrong wiki page specified", HTTP4xxException::NOT_FOUND); } PWELogger::info("Wiki file to show: %s", $file); if (!is_file($file)) { PWELogger::error("Not found wiki file: %s", $file); throw new HTTP4xxException("Wiki page not found", HTTP4xxException::NOT_FOUND); } $contents = $this->renderPage($file); return $contents; }
public static function fsys_copydir($from, $to, $is_deep = true, $move = false) { if (!is_dir($from)) { PWELogger::warn("Source directory not exists for copy: %s", $from); return false; } PWELogger::debug("Copying %s to %s", $from, $to); if (strrchr($from, '/') != '/') { $from .= '/'; } if (strrchr($to, '/') != '/') { $to .= '/'; } $files = self::fsys_readdir($from, $is_deep); if (!is_dir($to)) { if (!self::fsys_mkdir($to)) { PWELogger::error("Unable to create destination directory: %s", $to); return false; } if (!is_dir(preg_replace('!/\\w+/$!', '/', $to))) { PWELogger::error("Unable to use destination directory: %s", $to); return false; } } if (!is_writable($to)) { PWELogger::error("Destination dir '%s' is not writable", $to); return false; } foreach ($files as $file => $is_dir) { if (!is_readable($from . $file)) { PWELogger::error("Source '%s.%s' is not readable", $from, $file); return false; } if ($move && !is_writable($from . $file)) { PWELogger::error("Source '%s.%s' is not writable for move", $from, $file); return false; } } $existed_files = self::fsys_readdir($to); $copied = true; foreach ($files as $path => $is_dir) { $newpath = $to . $path; $oldpath = $from . $path; if ($is_dir) { if ($is_deep && !is_dir($newpath)) { $copied = self::fsys_mkdir($newpath); if (!$copied) { break; } } } else { $copied = copy($oldpath, $newpath); PWELogger::debug("Copy file %s %s -> %s", $copied ? 'success' : 'FAILED', $oldpath, $newpath); if (!$copied && (!file_exists($newpath) || filesize($oldpath) > 0)) { break; } } } if (!$copied) { PWELogger::info("Rolling back..."); self::fsys_removedir($to, $is_deep, $existed_files); return false; } if ($move && !self::fsys_removedir($from, $is_deep)) { return false; } return true; }