Initial commit

Functional, without SSO
This commit is contained in:
Jimmy Monin
2016-09-18 11:03:26 +02:00
commit 57708e3169
253 changed files with 30787 additions and 0 deletions

View File

@ -0,0 +1,54 @@
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @package log4php
*/
/**
* This class encapsulates the information obtained when parsing
* formatting modifiers in conversion modifiers.
*
* @package log4php
* @subpackage helpers
* @since 0.3
*/
class LoggerFormattingInfo {
/**
* Minimal output length. If output is shorter than this value, it will be
* padded with spaces.
*/
public $min = 0;
/**
* Maximum output length. If output is longer than this value, it will be
* trimmed.
*/
public $max = PHP_INT_MAX;
/**
* Whether to pad the string from the left. If set to false, the string
* will be padded from the right.
*/
public $padLeft = true;
/**
* Whether to trim the string from the left. If set to false, the string
* will be trimmed from the right.
*/
public $trimLeft = false;
}

View File

@ -0,0 +1,226 @@
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @package log4php
*/
/**
* A convenience class to convert property values to specific types.
*
* @version $Revision: 1374617 $
* @package log4php
* @subpackage helpers
* @since 0.5
*/
class LoggerOptionConverter {
/** String values which are converted to boolean TRUE. */
private static $trueValues = array('1', 'true', 'yes', 'on');
/**
* String values which are converted to boolean FALSE.
*
* Note that an empty string must convert to false, because
* parse_ini_file() which is used for parsing configuration
* converts the value _false_ to an empty string.
*/
private static $falseValues = array('0', 'false', 'no', 'off', '');
/**
* Read a predefined var.
*
* It returns a value referenced by <var>$key</var> using this search criteria:
* - if <var>$key</var> is a constant then return it. Else
* - if <var>$key</var> is set in <var>$_ENV</var> then return it. Else
* - return <var>$def</var>.
*
* @param string $key The key to search for.
* @param string $def The default value to return.
* @return string the string value of the system property, or the default
* value if there is no property with that key.
*/
public static function getSystemProperty($key, $def) {
if(defined($key)) {
return (string)constant($key);
} else if(isset($_SERVER[$key])) {
return (string)$_SERVER[$key];
} else if(isset($_ENV[$key])) {
return (string)$_ENV[$key];
} else {
return $def;
}
}
/** Converts $value to boolean, or throws an exception if not possible. */
public static function toBooleanEx($value) {
if (isset($value)) {
if (is_bool($value)) {
return $value;
}
$value = strtolower(trim($value));
if (in_array($value, self::$trueValues)) {
return true;
}
if (in_array($value, self::$falseValues)) {
return false;
}
}
throw new LoggerException("Given value [" . var_export($value, true) . "] cannot be converted to boolean.");
}
/**
* Converts $value to integer, or throws an exception if not possible.
* Floats cannot be converted to integer.
*/
public static function toIntegerEx($value) {
if (is_integer($value)) {
return $value;
}
if (is_numeric($value) && ($value == (integer) $value)) {
return (integer) $value;
}
throw new LoggerException("Given value [" . var_export($value, true) . "] cannot be converted to integer.");
}
/**
* Converts $value to integer, or throws an exception if not possible.
* Floats cannot be converted to integer.
*/
public static function toPositiveIntegerEx($value) {
if (is_integer($value) && $value > 0) {
return $value;
}
if (is_numeric($value) && ($value == (integer) $value) && $value > 0) {
return (integer) $value;
}
throw new LoggerException("Given value [" . var_export($value, true) . "] cannot be converted to a positive integer.");
}
/** Converts the value to a level. Throws an exception if not possible. */
public static function toLevelEx($value) {
if ($value instanceof LoggerLevel) {
return $value;
}
$level = LoggerLevel::toLevel($value);
if ($level === null) {
throw new LoggerException("Given value [" . var_export($value, true) . "] cannot be converted to a logger level.");
}
return $level;
}
/**
* Converts a value to a valid file size (integer).
*
* Supports 'KB', 'MB' and 'GB' suffixes, where KB = 1024 B etc.
*
* The final value will be rounded to the nearest integer.
*
* Examples:
* - '100' => 100
* - '100.12' => 100
* - '100KB' => 102400
* - '1.5MB' => 1572864
*
* @param mixed $value File size (optionally with suffix).
* @return integer Parsed file size.
*/
public static function toFileSizeEx($value) {
if (empty($value)) {
throw new LoggerException("Empty value cannot be converted to a file size.");
}
if (is_numeric($value)) {
return (integer) $value;
}
if (!is_string($value)) {
throw new LoggerException("Given value [" . var_export($value, true) . "] cannot be converted to a file size.");
}
$str = strtoupper(trim($value));
$count = preg_match('/^([0-9.]+)(KB|MB|GB)?$/', $str, $matches);
if ($count > 0) {
$size = $matches[1];
$unit = $matches[2];
switch($unit) {
case 'KB': $size *= pow(1024, 1); break;
case 'MB': $size *= pow(1024, 2); break;
case 'GB': $size *= pow(1024, 3); break;
}
return (integer) $size;
}
throw new LoggerException("Given value [$value] cannot be converted to a file size.");
}
/**
* Converts a value to string, or throws an exception if not possible.
*
* Objects can be converted to string if they implement the magic
* __toString() method.
*
*/
public static function toStringEx($value) {
if (is_string($value)) {
return $value;
}
if (is_numeric($value)) {
return (string) $value;
}
if (is_object($value) && method_exists($value, '__toString')) {
return (string) $value;
}
throw new LoggerException("Given value [" . var_export($value, true) . "] cannot be converted to string.");
}
/**
* Performs value substitution for string options.
*
* An option can contain PHP constants delimited by '${' and '}'.
*
* E.g. for input string "some ${FOO} value", the method will attempt
* to substitute ${FOO} with the value of constant FOO if it exists.
*
* Therefore, if FOO is a constant, and it has value "bar", the resulting
* string will be "some bar value".
*
* If the constant is not defined, it will be replaced by an empty string,
* and the resulting string will be "some value".
*
* @param string $string String on which to perform substitution.
* @return string
*/
public static function substConstants($string) {
preg_match_all('/\${([^}]+)}/', $string, $matches);
foreach($matches[1] as $key => $match) {
$match = trim($match);
$search = $matches[0][$key];
$replacement = defined($match) ? constant($match) : '';
$string = str_replace($search, $replacement, $string);
}
return $string;
}
}

View File

@ -0,0 +1,237 @@
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @package log4php
*/
/**
* Most of the work of the {@link LoggerPatternLayout} class
* is delegated to the {@link LoggerPatternParser} class.
*
* <p>It is this class that parses conversion patterns and creates
* a chained list of {@link LoggerPatternConverter} converters.</p>
*
* @version $Revision: 1395467 $
* @package log4php
* @subpackage helpers
*
* @since 0.3
*/
class LoggerPatternParser {
/** Escape character for conversion words in the conversion pattern. */
const ESCAPE_CHAR = '%';
/** Maps conversion words to relevant converters. */
private $converterMap;
/** Conversion pattern used in layout. */
private $pattern;
/** Regex pattern used for parsing the conversion pattern. */
private $regex;
/**
* First converter in the chain.
* @var LoggerPatternConverter
*/
private $head;
/** Last converter in the chain. */
private $tail;
public function __construct($pattern, $converterMap) {
$this->pattern = $pattern;
$this->converterMap = $converterMap;
// Construct the regex pattern
$this->regex =
'/' . // Starting regex pattern delimiter
self::ESCAPE_CHAR . // Character which marks the start of the conversion pattern
'(?P<modifiers>[0-9.-]*)' . // Format modifiers (optional)
'(?P<word>[a-zA-Z]+)' . // The conversion word
'(?P<option>{[^}]*})?' . // Conversion option in braces (optional)
'/'; // Ending regex pattern delimiter
}
/**
* Parses the conversion pattern string, converts it to a chain of pattern
* converters and returns the first converter in the chain.
*
* @return LoggerPatternConverter
*/
public function parse() {
// Skip parsing if the pattern is empty
if (empty($this->pattern)) {
$this->addLiteral('');
return $this->head;
}
// Find all conversion words in the conversion pattern
$count = preg_match_all($this->regex, $this->pattern, $matches, PREG_OFFSET_CAPTURE);
if ($count === false) {
$error = error_get_last();
throw new LoggerException("Failed parsing layotut pattern: {$error['message']}");
}
$prevEnd = 0;
foreach($matches[0] as $key => $item) {
// Locate where the conversion command starts and ends
$length = strlen($item[0]);
$start = $item[1];
$end = $item[1] + $length;
// Find any literal expressions between matched commands
if ($start > $prevEnd) {
$literal = substr($this->pattern, $prevEnd, $start - $prevEnd);
$this->addLiteral($literal);
}
// Extract the data from the matched command
$word = !empty($matches['word'][$key]) ? $matches['word'][$key][0] : null;
$modifiers = !empty($matches['modifiers'][$key]) ? $matches['modifiers'][$key][0] : null;
$option = !empty($matches['option'][$key]) ? $matches['option'][$key][0] : null;
// Create a converter and add it to the chain
$this->addConverter($word, $modifiers, $option);
$prevEnd = $end;
}
// Add any trailing literals
if ($end < strlen($this->pattern)) {
$literal = substr($this->pattern, $end);
$this->addLiteral($literal);
}
return $this->head;
}
/**
* Adds a literal converter to the converter chain.
* @param string $string The string for the literal converter.
*/
private function addLiteral($string) {
$converter = new LoggerPatternConverterLiteral($string);
$this->addToChain($converter);
}
/**
* Adds a non-literal converter to the converter chain.
*
* @param string $word The conversion word, used to determine which
* converter will be used.
* @param string $modifiers Formatting modifiers.
* @param string $option Option to pass to the converter.
*/
private function addConverter($word, $modifiers, $option) {
$formattingInfo = $this->parseModifiers($modifiers);
$option = trim($option, "{} ");
if (isset($this->converterMap[$word])) {
$converter = $this->getConverter($word, $formattingInfo, $option);
$this->addToChain($converter);
} else {
trigger_error("log4php: Invalid keyword '%$word' in converison pattern. Ignoring keyword.", E_USER_WARNING);
}
}
/**
* Determines which converter to use based on the conversion word. Creates
* an instance of the converter using the provided formatting info and
* option and returns it.
*
* @param string $word The conversion word.
* @param LoggerFormattingInfo $info Formatting info.
* @param string $option Converter option.
*
* @throws LoggerException
*
* @return LoggerPatternConverter
*/
private function getConverter($word, $info, $option) {
if (!isset($this->converterMap[$word])) {
throw new LoggerException("Invalid keyword '%$word' in converison pattern. Ignoring keyword.");
}
$converterClass = $this->converterMap[$word];
if(!class_exists($converterClass)) {
throw new LoggerException("Class '$converterClass' does not exist.");
}
$converter = new $converterClass($info, $option);
if(!($converter instanceof LoggerPatternConverter)) {
throw new LoggerException("Class '$converterClass' is not an instance of LoggerPatternConverter.");
}
return $converter;
}
/** Adds a converter to the chain and updates $head and $tail pointers. */
private function addToChain(LoggerPatternConverter $converter) {
if (!isset($this->head)) {
$this->head = $converter;
$this->tail = $this->head;
} else {
$this->tail->next = $converter;
$this->tail = $this->tail->next;
}
}
/**
* Parses the formatting modifiers and produces the corresponding
* LoggerFormattingInfo object.
*
* @param string $modifier
* @return LoggerFormattingInfo
* @throws LoggerException
*/
private function parseModifiers($modifiers) {
$info = new LoggerFormattingInfo();
// If no modifiers are given, return default values
if (empty($modifiers)) {
return $info;
}
// Validate
$pattern = '/^(-?[0-9]+)?\.?-?[0-9]+$/';
if (!preg_match($pattern, $modifiers)) {
trigger_error("log4php: Invalid modifier in conversion pattern: [$modifiers]. Ignoring modifier.", E_USER_WARNING);
return $info;
}
$parts = explode('.', $modifiers);
if (!empty($parts[0])) {
$minPart = (integer) $parts[0];
$info->min = abs($minPart);
$info->padLeft = ($minPart > 0);
}
if (!empty($parts[1])) {
$maxPart = (integer) $parts[1];
$info->max = abs($maxPart);
$info->trimLeft = ($maxPart < 0);
}
return $info;
}
}

View File

@ -0,0 +1,123 @@
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @package log4php
*/
/**
* Contains various helper methods.
*
* @package log4php
* @subpackage helpers
* @since 2.3
*/
class LoggerUtils {
/**
* Splits a fully qualified class name into fragments delimited by the
* namespace separator (\).
*
* For backward compatibility, a dot (.) can be used as a delimiter as
* well.
*
* @param string $name
*
* @return array Class name split into fragments.
*/
public static function tokenizeClassName($name) {
$name = str_replace('.', '\\', $name);
$name = trim($name, ' \\');
$fragments = explode('\\', $name);
foreach($fragments as $key => $fragment) {
if (trim($fragment) === '') {
unset($fragments[$key]);
}
}
return $fragments;
}
/**
* Attempts to shorten the given class name to the desired length.
*
* This is done by separating the class name into fragments (delimited
* by \ or .) and trimming individual fragments, starting with the left,
* until desired length has been reached.
*
* The final fragment (i.e. class name) will never be shortened so the
* result may still be longer than given length.
*
* @param string $name The (qualified) class name.
* @param integer $length The length to shorten to. If null or 0 is given,
* the name will be returned without shortening.
*/
public static function shortenClassName($name, $length) {
if ($length === null || $length < 0) {
return $name;
}
$name = str_replace('.', '\\', $name);
$name = trim($name, ' \\');
// Check if any shortening is required
$currentLength = strlen($name);
if ($currentLength <= $length) {
return $name;
}
// Split name into fragments
$fragments = explode('\\', $name);
// If zero length is specified, return only last fragment
if ($length == 0) {
return array_pop($fragments);
}
// If the name splits to only one fragment, then it cannot be shortened
$count = count($fragments);
if ($count == 1) {
return $name;
}
foreach($fragments as $key => &$fragment) {
// Never shorten last fragment
if ($key == $count - 1) {
break;
}
// Check for empty fragments (shouldn't happen but it's possible)
$fragLen = strlen($fragment);
if ($fragLen <= 1) {
continue;
}
// Shorten fragment to one character and check if total length satisfactory
$fragment = substr($fragment, 0, 1);
$currentLength = $currentLength - $fragLen + 1;
if ($currentLength <= $length) {
break;
}
}
unset($fragment);
return implode('\\', $fragments);
}
}