update Piwik to version 2.16 (fixes #91)

This commit is contained in:
oliver 2016-04-10 18:55:57 +02:00
commit d885a4baa9
5833 changed files with 418860 additions and 226988 deletions

View file

@ -0,0 +1,74 @@
# Piwik/Ini
Read and write INI configurations.
[![Build Status](https://img.shields.io/travis/piwik/component-ini.svg?style=flat-square)](https://travis-ci.org/piwik/component-ini)
[![Coverage Status](https://img.shields.io/coveralls/piwik/component-ini/master.svg?style=flat-square)](https://coveralls.io/r/piwik/component-ini?branch=master)
[![Latest Version](https://img.shields.io/github/release/piwik/component-ini.svg?style=flat-square)](https://packagist.org/packages/piwik/component-ini)
[![](https://img.shields.io/packagist/dm/piwik/ini.svg?style=flat-square)](https://packagist.org/packages/piwik/ini)
## Installation
```json
composer require piwik/ini
```
## Why?
PHP provides a `parse_ini_file()` function to read INI files.
This component provides the following benefits over the built-in function:
- allows to write INI files
- classes can be used with dependency injection and mocked in unit tests
- throws exceptions instead of PHP errors
- better type supports:
- parses boolean values (`true`/`false`, `on`/`off`, `yes`/`no`) to real PHP booleans ([instead of strings `"1"` and `""`](http://3v4l.org/JuvOT))
- parses null to PHP `null` ([instead of an empty string](http://3v4l.org/KSoj2))
- works even if `parse_ini_file()` or `parse_ini_string()` is disabled in `php.ini` by falling back on an alternate implementation (can happen on some shared hosts)
- fixes [a PHP 5.3.3 bug](http://3v4l.org/jD1Lh)
- fixes [a parsing bug](http://3v4l.org/m24cT) present in PHP <= 5.4.4
## Usage
### Read
```php
$reader = new IniReader();
// Read a string
$array = $reader->readString($string);
// Read a file
$array = $reader->readFile('config.ini');
```
### Write
```php
$writer = new IniWriter();
// Write to a string
$string = $writer->writeToString($array);
// Write to a file
$writer->writeToFile('config.ini', $array);
```
## License
The Ini component is released under the [LGPL v3.0](http://choosealicense.com/licenses/lgpl-3.0/).
## Contributing
To run the unit tests:
```
vendor/bin/phpunit
```
To run the performance tests:
```
php -n vendor/bin/athletic -p tests/PerformanceTest
```

View file

@ -0,0 +1,22 @@
{
"name": "piwik/ini",
"type": "library",
"license": "LGPL-3.0",
"autoload": {
"psr-4": {
"Piwik\\Ini\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Piwik\\Tests\\Ini\\": "tests/"
}
},
"require": {
"php": ">=5.3.3"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
"athletic/athletic": "0.1.*"
}
}

View file

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="./vendor/autoload.php">
<testsuites>
<testsuite name="Test suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>

View file

@ -0,0 +1,390 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Ini;
/**
* Reads INI configuration.
*/
class IniReader
{
/**
* @var bool
*/
private $useNativeFunction;
public function __construct()
{
$this->useNativeFunction = function_exists('parse_ini_string');
}
/**
* Reads a INI configuration file and returns it as an array.
*
* The array returned is multidimensional, indexed by section names:
*
* ```
* array(
* 'Section 1' => array(
* 'value1' => 'hello',
* 'value2' => 'world',
* ),
* 'Section 2' => array(
* 'value3' => 'foo',
* )
* );
* ```
*
* @param string $filename The file to read.
* @throws IniReadingException
* @return array
*/
public function readFile($filename)
{
$ini = $this->getContentOfIniFile($filename);
return $this->readString($ini);
}
/**
* Reads a INI configuration string and returns it as an array.
*
* The array returned is multidimensional, indexed by section names:
*
* ```
* array(
* 'Section 1' => array(
* 'value1' => 'hello',
* 'value2' => 'world',
* ),
* 'Section 2' => array(
* 'value3' => 'foo',
* )
* );
* ```
*
* @param string $ini String containing INI configuration.
* @throws IniReadingException
* @return array
*/
public function readString($ini)
{
// On PHP 5.3.3 an empty line return is needed at the end
// See http://3v4l.org/jD1Lh
$ini .= "\n";
if ($this->useNativeFunction) {
$array = $this->readWithNativeFunction($ini);
} else {
$array = $this->readWithAlternativeImplementation($ini);
}
return $array;
}
/**
* @param string $ini
* @throws IniReadingException
* @return array
*/
private function readWithNativeFunction($ini)
{
$array = @parse_ini_string($ini, true);
if ($array === false) {
$e = error_get_last();
throw new IniReadingException('Syntax error in INI configuration: ' . $e['message']);
}
// We cannot use INI_SCANNER_RAW by default because it is buggy under PHP 5.3.14 and 5.4.4
// http://3v4l.org/m24cT
$rawValues = @parse_ini_string($ini, true, INI_SCANNER_RAW);
$array = $this->decode($array, $rawValues);
return $array;
}
private function getContentOfIniFile($filename)
{
if (!file_exists($filename) || !is_readable($filename)) {
throw new IniReadingException(sprintf("The file %s doesn't exist or is not readable", $filename));
}
$content = $this->getFileContent($filename);
if ($content === false) {
throw new IniReadingException(sprintf('Impossible to read the file %s', $filename));
}
return $content;
}
/**
* Reads ini comments for each key.
*
* The array returned is multidimensional, indexed by section names:
*
* ```
* array(
* 'Section 1' => array(
* 'key1' => 'comment 1',
* 'key2' => 'comment 2',
* ),
* 'Section 2' => array(
* 'key3' => 'comment 3',
* )
* );
* ```
*
* @param string $filename The path to a file.
* @throws IniReadingException
* @return array
*/
public function readComments($filename)
{
$ini = $this->getContentOfIniFile($filename);
$ini = $this->splitIniContentIntoLines($ini);
$descriptions = array();
$section = '';
$lastComment = '';
foreach ($ini as $line) {
$line = trim($line);
if (strpos($line, '[') === 0) {
$tmp = explode(']', $line);
$section = trim(substr($tmp[0], 1));
$descriptions[$section] = array();
$lastComment = '';
continue;
}
if (!preg_match('/^[a-zA-Z0-9[]/', $line)) {
if (strpos($line, ';') === 0) {
$line = trim(substr($line, 1));
}
// comment
$lastComment .= $line . "\n";
continue;
}
list($key, $value) = explode('=', $line, 2);
$key = trim($key);
if (strpos($key, '[]') === strlen($key) - 2) {
$key = substr($key, 0, -2);
}
if (empty($descriptions[$section][$key])) {
$descriptions[$section][$key] = $lastComment;
}
$lastComment = '';
}
return $descriptions;
}
private function splitIniContentIntoLines($ini)
{
if (is_string($ini)) {
$ini = explode("\n", str_replace("\r", "\n", $ini));
}
return $ini;
}
/**
* Reimplementation in case `parse_ini_file()` is disabled.
*
* @author Andrew Sohn <asohn (at) aircanopy (dot) net>
* @author anthon (dot) pang (at) gmail (dot) com
*
* @param string $ini
* @return array
*/
private function readWithAlternativeImplementation($ini)
{
$ini = $this->splitIniContentIntoLines($ini);
if (count($ini) == 0) {
return array();
}
$sections = array();
$values = array();
$result = array();
$globals = array();
$i = 0;
foreach ($ini as $line) {
$line = trim($line);
$line = str_replace("\t", " ", $line);
// Comments
if (!preg_match('/^[a-zA-Z0-9[]/', $line)) {
continue;
}
// Sections
if ($line{0} == '[') {
$tmp = explode(']', $line);
$sections[] = trim(substr($tmp[0], 1));
$i++;
continue;
}
// Key-value pair
list($key, $value) = explode('=', $line, 2);
$key = trim($key);
$value = trim($value);
if (strstr($value, ";")) {
$tmp = explode(';', $value);
if (count($tmp) == 2) {
if ((($value{0} != '"') && ($value{0} != "'")) ||
preg_match('/^".*"\s*;/', $value) || preg_match('/^".*;[^"]*$/', $value) ||
preg_match("/^'.*'\s*;/", $value) || preg_match("/^'.*;[^']*$/", $value)
) {
$value = $tmp[0];
}
} else {
if ($value{0} == '"') {
$value = preg_replace('/^"(.*)".*/', '$1', $value);
} elseif ($value{0} == "'") {
$value = preg_replace("/^'(.*)'.*/", '$1', $value);
} else {
$value = $tmp[0];
}
}
}
$value = trim($value);
// Special keywords
if ($value === 'true' || $value === 'yes' || $value === 'on') {
$value = true;
} elseif ($value === 'false' || $value === 'no' || $value === 'off') {
$value = false;
} elseif ($value === '' || $value === 'null') {
$value = null;
}
if (is_string($value)) {
$value = trim($value, "'\"");
}
if ($i == 0) {
if (substr($key, -2) == '[]') {
$globals[substr($key, 0, -2)][] = $value;
} else {
$globals[$key] = $value;
}
} else {
if (substr($key, -2) == '[]') {
$values[$i - 1][substr($key, 0, -2)][] = $value;
} else {
$values[$i - 1][$key] = $value;
}
}
}
for ($j = 0; $j < $i; $j++) {
if (isset($values[$j])) {
$result[$sections[$j]] = $values[$j];
} else {
$result[$sections[$j]] = array();
}
}
$finalResult = $result + $globals;
return $this->decode($finalResult, $finalResult);
}
/**
* @param string $filename
* @return bool|string Returns false if failure.
*/
private function getFileContent($filename)
{
if (function_exists('file_get_contents')) {
return file_get_contents($filename);
} elseif (function_exists('file')) {
$ini = file($filename);
if ($ini !== false) {
return implode("\n", $ini);
}
} elseif (function_exists('fopen') && function_exists('fread')) {
$handle = fopen($filename, 'r');
if (!$handle) {
return false;
}
$ini = fread($handle, filesize($filename));
fclose($handle);
return $ini;
}
return false;
}
/**
* We have to decode values manually because parse_ini_file() has a poor implementation.
*
* @param mixed $value The array decoded by `parse_ini_file`
* @param mixed $rawValue The same array but with raw strings, so that we can re-decode manually
* and override the poor job of `parse_ini_file`
* @return mixed
*/
private function decode($value, $rawValue)
{
if (is_array($value)) {
foreach ($value as $i => &$subValue) {
$subValue = $this->decode($subValue, $rawValue[$i]);
}
return $value;
}
if (! is_string($value)) {
return $value;
}
$value = $this->decodeBoolean($value, $rawValue);
$value = $this->decodeNull($value, $rawValue);
if (is_numeric($value) && $this->noLossWhenCastToInt($value)) {
return $value + 0;
}
return $value;
}
private function decodeBoolean($value, $rawValue)
{
if ($value === '1' && ($rawValue === 'true' || $rawValue === 'yes' || $rawValue === 'on')) {
return true;
}
if ($value === '' && ($rawValue === 'false' || $rawValue === 'no' || $rawValue === 'off')) {
return false;
}
return $value;
}
private function decodeNull($value, $rawValue)
{
if ($value === '' && $rawValue === 'null') {
return null;
}
return $value;
}
private function noLossWhenCastToInt($value)
{
return (string) ($value + 0) === $value;
}
}

View file

@ -0,0 +1,16 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Ini;
/**
* Exception when reading a INI configuration.
*/
class IniReadingException extends \Exception
{
}

View file

@ -0,0 +1,120 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Ini;
/**
* Writes INI configuration.
*/
class IniWriter
{
/**
* Writes an array configuration to a INI file.
*
* The array provided must be multidimensional, indexed by section names:
*
* ```
* array(
* 'Section 1' => array(
* 'value1' => 'hello',
* 'value2' => 'world',
* ),
* 'Section 2' => array(
* 'value3' => 'foo',
* )
* );
* ```
*
* @param string $filename
* @param array $config
* @param string $header Optional header to insert at the top of the file.
* @throws IniWritingException
*/
public function writeToFile($filename, array $config, $header = '')
{
$ini = $this->writeToString($config, $header);
if (!file_put_contents($filename, $ini)) {
throw new IniWritingException(sprintf('Impossible to write to file %s', $filename));
}
}
/**
* Writes an array configuration to a INI string and returns it.
*
* The array provided must be multidimensional, indexed by section names:
*
* ```
* array(
* 'Section 1' => array(
* 'value1' => 'hello',
* 'value2' => 'world',
* ),
* 'Section 2' => array(
* 'value3' => 'foo',
* )
* );
* ```
*
* @param array $config
* @param string $header Optional header to insert at the top of the file.
* @return string
* @throws IniWritingException
*/
public function writeToString(array $config, $header = '')
{
$ini = $header;
$sectionNames = array_keys($config);
foreach ($sectionNames as $sectionName) {
$section = $config[$sectionName];
// no point in writing empty sections
if (empty($section)) {
continue;
}
if (! is_array($section)) {
throw new IniWritingException(sprintf("Section \"%s\" doesn't contain an array of values", $sectionName));
}
$ini .= "[$sectionName]\n";
foreach ($section as $option => $value) {
if (is_numeric($option)) {
$option = $sectionName;
$value = array($value);
}
if (is_array($value)) {
foreach ($value as $currentValue) {
$ini .= $option . '[] = ' . $this->encodeValue($currentValue) . "\n";
}
} else {
$ini .= $option . ' = ' . $this->encodeValue($value) . "\n";
}
}
$ini .= "\n";
}
return $ini;
}
private function encodeValue($value)
{
if (is_bool($value)) {
return (int) $value;
}
if (is_string($value)) {
return "\"$value\"";
}
return $value;
}
}

View file

@ -0,0 +1,16 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Ini;
/**
* Exception when writing a INI configuration.
*/
class IniWritingException extends \Exception
{
}