Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fisharebest committed Dec 18, 2015
0 parents commit 5b3e0df
Show file tree
Hide file tree
Showing 13 changed files with 1,154 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
src_dir: src

coverage_clover: tests/clover.xml

json_path: tests/coverage-upload.json
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea/
composer.lock
vendor/
27 changes: 27 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
language: php

sudo: false

cache:
directories:
- $HOME/.composer/cache

php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm

before_install:
- composer self-update;

install:
- composer install --no-interaction

script:
- phpunit --coverage-clover tests/clover.xml

after_script:
- php vendor/bin/coveralls -v
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CHANGE LOG
==========

## 1.0.0 (2015-12-18)
- Disable bug_compat_42
- Undo the effects of get_magic_quotes_gpc()
- http_response_code()
- gzopen()
- gzseek()
- gztell()
677 changes: 677 additions & 0 deletions LICENSE-GPLv3.md

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions LICENSE-MIT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2015 Greg Roach

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
73 changes: 73 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
[![Build Status](https://travis-ci.org/fisharebest/php-polyfill.svg?branch=master)](https://travis-ci.org/fisharebest/php-polyfill)
[![Coverage Status](https://coveralls.io/repos/fisharebest/php-polyfill/badge.svg?branch=master&service=github)](https://coveralls.io/github/fisharebest/php-polyfill?branch=master)

PHP Polyfill
============

This project is a complement to [symfony/polyfill](https://github.com/symfony/polyfill)
and provides some additional functions and workarounds.

The symfony project will only accept contributions that are 100% compatible
with the native PHP implementation. In many cases, this isn't actually
possible - it’s often why the functions were added to PHP in the first place!

So, this project provides implementations that are often “good enough” to get
your application working on old servers. Restrictions and limitations are
described below.

Any functions that are subsequently added to symfony/polyfill will be removed
from here.

Usage
=====

Add the dependency to your `composer.json` and allow autoloading magic to do the rest.

```json
{
"require": {
"fisharebest/php-polyfill": "~1.0",
},
}
```

The following polyfill libraries will be loaded automatically:

- `symfony/polyfill` - the core polyfills
- `symfony/intl` - polyfills for intl library functions
- `ircmaxell/password_compat` - polyfills for password functions
- `paragonie/random_compat` - polfills for random number functions
- `fisharebest/ext-calendar` - polyfills for the calendar library functions

PHP 5.3
=======

- If the server has enabled “magic quotes”, these are removed.
- If the server has enabled "bug_compat_42", this is disabled.


PHP 5.4
=======

- `http_response_code()` - The native function allows you to get the current
status code, even if it was set using another function, such as `header()`.
This implementation can only get the current status code if it was also set by
`http_response_code()`.

PHP 5.5.9 (Ubuntu 14.04 32 bit)
===============================

- `gzopen()` - Wrongly implemented as `gzopen64()`.
- `gzseek()` - Wrongly implemented as `gzseek64()`.
- `gztell()` - Wrongly implemented as `gztell64()`.

Contributions
=============

Please follow the existing code style and write unit-tests where you can.

License
=======

This package is dual licensed under both the [MIT](LICENSE-MIT.md) and
[GPLv3](LICENSE-GPLv3.md) licenses. Use whichever makes you happiest.
36 changes: 36 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "fisharebest/php-polyfill",
"type": "library",
"description": "Polyfills for PHP 5.3 onwards",
"keywords": [
"php",
"polyfill",
"shim",
"compat",
"compatibility"
],
"homepage": "https://github.com/fisharebest/php-polyfill",
"license": "MIT and GPL-3.0+",
"authors": [
{
"name": "Greg Roach",
"email": "fisharebest@gmail.com"
}
],
"require": {
"fisharebest/ext-calendar": "~2.2",
"symfony/polyfill": "~1.0"
},
"require-dev": {
"phpunit/phpunit": "*",
"satooshi/php-coveralls": "dev-master"
},
"autoload": {
"psr-4": {
"Fisharebest\\PhpPolyfill\\": "src/"
},
"files": [
"src/bootstrap.php"
]
}
}
25 changes: 25 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
beStrictAboutOutputDuringTests="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutTestSize="true"
bootstrap="vendor/autoload.php"
colors="true"
xmlns=""
>
<php>
<ini name="error_reporting" value="-1"/>
</php>

<filter>
<whitelist>
<directory suffix=".php">src</directory>
</whitelist>
</filter>

<testsuites>
<testsuite name="Fisharebest / PHP Polyfill">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
115 changes: 115 additions & 0 deletions src/Php54.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

/**
* PHP Polyfill
*
* @author Greg Roach <fisharebest@gmail.com>
* @copyright (c) 2015 Greg Roach
* @license MIT or GPLv3+
*/

namespace Fisharebest\PhpPolyfill;

/**
* Class Php54 - polyfills for functions introduced in PHP5.4
*/
class Php54 {
/**
* @link https://php.net/http_response_code
*
* @param string|null $response_code
*
* @return int
*/
public static function httpResponseCode($response_code) {
static $current_code = 200;

$messages = array(
100 => 'Continue',
101 => 'Switching Protocols',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Moved Temporarily',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
307 => 'Temporary Redirect',
308 => 'Permanent Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Time-out',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Large',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Time-out',
505 => 'HTTP Version not supported',
);

$previous_code = $current_code;

if (is_numeric($response_code)) {
$current_code = (int)$response_code;
$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
$message = isset($messages[$response_code]) ? $messages[$response_code] : 'Unknown Status Code';
header($protocol . ' ' . $response_code . ' ' . $message);
} elseif (null !== $response_code) {
$type = gettype($response_code);
trigger_error('http_response_code() expects parameter 1 to be long, ' . $type . ' given', E_USER_WARNING);
}

return $previous_code;
}

/**
* @link https://php.net/manual/en/security.magicquotes.disabling.php
*
* @param mixed[] $old
*
* @return mixed[]
*/
public static function removeMagicQuotesFromArray(array $old) {
$new = array();
foreach ($old as $key => $value) {
if (is_array($value)) {
$new[stripslashes($key)] = self::removeMagicQuotesFromArray($value);
} else {
$new[stripslashes($key)] = stripslashes($value);
}
}

return $new;
}

/**
* @link https://php.net/manual/en/security.magicquotes.disabling.php
*/
public static function removeMagicQuotes() {
$_GET = self::removeMagicQuotesFromArray($_GET);
$_POST = self::removeMagicQuotesFromArray($_POST);
$_COOKIE = self::removeMagicQuotesFromArray($_COOKIE);
$_REQUEST = self::removeMagicQuotesFromArray($_REQUEST);
}
}
45 changes: 45 additions & 0 deletions src/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* PHP Polyfill
*
* @author Greg Roach <fisharebest@gmail.com>
* @copyright (c) 2015 Greg Roach
* @license MIT or GPLv3+
*/

use Fisharebest\PhpPolyfill\Php54;

if (PHP_VERSION_ID < 50600) {
// Add features that were introduced in PHP 5.5

if (PHP_VERSION_ID === 50509 && PHP_INT_SIZE === 4) {
// Missing functions in PHP 5.5.9 - affects 32 bit builds of Ubuntu 14.04LTS
// See https://bugs.launchpad.net/ubuntu/+source/php5/+bug/1315888
if (!function_exists('gzopen') && function_exists('gzopen64')) {
function gzopen($filename, $mode, $use_include_path = 0) { return gzopen64($filename, $mode, $use_include_path); }
}
if (!function_exists('gzseek') && function_exists('gzseek64')) {
function gzseek($zp, $offset, $whence = SEEK_SET) { return gzseek64($zp, $offset, $whence); }
}
if (!function_exists('gztell') && function_exists('gztell64')) {
function gztell($zp) { return gztell64($zp); }
}
}

if (PHP_VERSION_ID < 50400) {
// Magic quotes were deprecated in PHP5.3 and removed in PHP5.4
if (get_magic_quotes_gpc()) {
Php54::removeMagicQuotes();
}
// The global session variable bug/feature was removed in PHP5.4
if (ini_get('session.bug_compat_42')) {
ini_set('session.bug_compat_42', '0');
}

// Add features that were introduced in PHP 5.4
if (!function_exists('http_response_code')) {
function http_response_code($reponse_code = null) { return Php54::httpResponseCode($reponse_code); }
}
}
}
1 change: 1 addition & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
clover.xml

0 comments on commit 5b3e0df

Please sign in to comment.