Skip to content

Commit

Permalink
Merge branch 'MDL-60291-master' of git://github.com/ankitagarwal/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Oct 11, 2017
2 parents ae2ae5f + 4e23b6b commit 5fab66a
Show file tree
Hide file tree
Showing 29 changed files with 361 additions and 312 deletions.
145 changes: 91 additions & 54 deletions lib/maxmind/GeoIp2/Database/Reader.php
Expand Up @@ -5,6 +5,7 @@
use GeoIp2\Exception\AddressNotFoundException;
use GeoIp2\ProviderInterface;
use MaxMind\Db\Reader as DbReader;
use MaxMind\Db\Reader\InvalidDatabaseException;

/**
* Instances of this class provide a reader for the GeoIP2 database format.
Expand All @@ -29,7 +30,6 @@
* SPL {@link \InvalidArgumentException} will be thrown. If the database is
* corrupt or invalid, a {@link \MaxMind\Db\Reader\InvalidDatabaseException}
* will be thrown.
*
*/
class Reader implements ProviderInterface
{
Expand All @@ -39,15 +39,16 @@ class Reader implements ProviderInterface
/**
* Constructor.
*
* @param string $filename The path to the GeoIP2 database file.
* @param array $locales List of locale codes to use in name property
* from most preferred to least preferred.
* @param string $filename the path to the GeoIP2 database file
* @param array $locales list of locale codes to use in name property
* from most preferred to least preferred
*
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
* is corrupt or invalid
*/
public function __construct(
$filename,
$locales = array('en')
$locales = ['en']
) {
$this->dbReader = new DbReader($filename);
$this->locales = $locales;
Expand All @@ -56,14 +57,14 @@ public function __construct(
/**
* This method returns a GeoIP2 City model.
*
* @param string $ipAddress IPv4 or IPv6 address as a string.
*
* @return \GeoIp2\Model\City
* @param string $ipAddress an IPv4 or IPv6 address as a string
*
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database.
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
* is corrupt or invalid
*
* @return \GeoIp2\Model\City
*/
public function city($ipAddress)
{
Expand All @@ -73,14 +74,14 @@ public function city($ipAddress)
/**
* This method returns a GeoIP2 Country model.
*
* @param string $ipAddress IPv4 or IPv6 address as a string.
* @param string $ipAddress an IPv4 or IPv6 address as a string
*
* @return \GeoIp2\Model\Country
*
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database.
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
* is corrupt or invalid
*
* @return \GeoIp2\Model\Country
*/
public function country($ipAddress)
{
Expand All @@ -90,14 +91,14 @@ public function country($ipAddress)
/**
* This method returns a GeoIP2 Anonymous IP model.
*
* @param string $ipAddress IPv4 or IPv6 address as a string.
*
* @return \GeoIp2\Model\AnonymousIp
* @param string $ipAddress an IPv4 or IPv6 address as a string
*
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database.
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
* is corrupt or invalid
*
* @return \GeoIp2\Model\AnonymousIp
*/
public function anonymousIp($ipAddress)
{
Expand All @@ -109,16 +110,37 @@ public function anonymousIp($ipAddress)
}

/**
* This method returns a GeoIP2 Connection Type model.
* This method returns a GeoLite2 ASN model.
*
* @param string $ipAddress IPv4 or IPv6 address as a string.
* @param string $ipAddress an IPv4 or IPv6 address as a string
*
* @return \GeoIp2\Model\ConnectionType
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*
* @return \GeoIp2\Model\Asn
*/
public function asn($ipAddress)
{
return $this->flatModelFor(
'Asn',
'GeoLite2-ASN',
$ipAddress
);
}

/**
* This method returns a GeoIP2 Connection Type model.
*
* @param string $ipAddress an IPv4 or IPv6 address as a string
*
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database.
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
* is corrupt or invalid
*
* @return \GeoIp2\Model\ConnectionType
*/
public function connectionType($ipAddress)
{
Expand All @@ -132,14 +154,14 @@ public function connectionType($ipAddress)
/**
* This method returns a GeoIP2 Domain model.
*
* @param string $ipAddress IPv4 or IPv6 address as a string.
*
* @return \GeoIp2\Model\Domain
* @param string $ipAddress an IPv4 or IPv6 address as a string
*
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database.
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
* is corrupt or invalid
*
* @return \GeoIp2\Model\Domain
*/
public function domain($ipAddress)
{
Expand All @@ -153,14 +175,14 @@ public function domain($ipAddress)
/**
* This method returns a GeoIP2 Enterprise model.
*
* @param string $ipAddress IPv4 or IPv6 address as a string.
* @param string $ipAddress an IPv4 or IPv6 address as a string
*
* @return \GeoIp2\Model\Enterprise
*
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database.
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
* is corrupt or invalid
*
* @return \GeoIp2\Model\Enterprise
*/
public function enterprise($ipAddress)
{
Expand All @@ -170,14 +192,14 @@ public function enterprise($ipAddress)
/**
* This method returns a GeoIP2 ISP model.
*
* @param string $ipAddress IPv4 or IPv6 address as a string.
* @param string $ipAddress an IPv4 or IPv6 address as a string
*
* @return \GeoIp2\Model\Isp
*
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database.
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
* not in the database
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
* is corrupt or invalid
*
* @return \GeoIp2\Model\Isp
*/
public function isp($ipAddress)
{
Expand All @@ -193,7 +215,7 @@ private function modelFor($class, $type, $ipAddress)
$record = $this->getRecord($class, $type, $ipAddress);

$record['traits']['ip_address'] = $ipAddress;
$class = "GeoIp2\\Model\\" . $class;
$class = 'GeoIp2\\Model\\' . $class;

return new $class($record, $this->locales);
}
Expand All @@ -203,7 +225,7 @@ private function flatModelFor($class, $type, $ipAddress)
$record = $this->getRecord($class, $type, $ipAddress);

$record['ip_address'] = $ipAddress;
$class = "GeoIp2\\Model\\" . $class;
$class = 'GeoIp2\\Model\\' . $class;

return new $class($record);
}
Expand All @@ -214,7 +236,7 @@ private function getRecord($class, $type, $ipAddress)
$method = lcfirst($class);
throw new \BadMethodCallException(
"The $method method cannot be used to open a "
. $this->metadata()->databaseType . " database"
. $this->metadata()->databaseType . ' database'
);
}
$record = $this->dbReader->get($ipAddress);
Expand All @@ -223,13 +245,28 @@ private function getRecord($class, $type, $ipAddress)
"The address $ipAddress is not in the database."
);
}
if (!is_array($record)) {
// This can happen on corrupt databases. Generally,
// MaxMind\Db\Reader will throw a
// MaxMind\Db\Reader\InvalidDatabaseException, but occasionally
// the lookup may result in a record that looks valid but is not
// an array. This mostly happens when the user is ignoring all
// exceptions and the more frequent InvalidDatabaseException
// exceptions go unnoticed.
throw new InvalidDatabaseException(
"Expected an array when looking up $ipAddress but received: "
. gettype($record)
);
}

return $record;
}

/**
* @throws \InvalidArgumentException if arguments are passed to the method.
* @throws \BadMethodCallException if the database has been closed.
* @return \MaxMind\Db\Reader\Metadata object for the database.
* @throws \InvalidArgumentException if arguments are passed to the method
* @throws \BadMethodCallException if the database has been closed
*
* @return \MaxMind\Db\Reader\Metadata object for the database
*/
public function metadata()
{
Expand Down
3 changes: 1 addition & 2 deletions lib/maxmind/GeoIp2/Exception/HttpException.php
Expand Up @@ -5,11 +5,10 @@
/**
* This class represents an HTTP transport error.
*/

class HttpException extends GeoIp2Exception
{
/**
* The URI queried
* The URI queried.
*/
public $uri;

Expand Down
2 changes: 1 addition & 1 deletion lib/maxmind/GeoIp2/Exception/InvalidRequestException.php
Expand Up @@ -9,7 +9,7 @@
class InvalidRequestException extends HttpException
{
/**
* The code returned by the MaxMind web service
* The code returned by the MaxMind web service.
*/
public $error;

Expand Down
27 changes: 16 additions & 11 deletions lib/maxmind/GeoIp2/Model/AbstractModel.php
Expand Up @@ -2,17 +2,17 @@

namespace GeoIp2\Model;

use GeoIp2\Compat\JsonSerializable;

/**
* @ignore
*/
abstract class AbstractModel implements JsonSerializable
abstract class AbstractModel implements \JsonSerializable
{
protected $raw;

/**
* @ignore
*
* @param mixed $raw
*/
public function __construct($raw)
{
Expand All @@ -21,26 +21,29 @@ public function __construct($raw)

/**
* @ignore
*
* @param mixed $field
*/
protected function get($field)
{
if (isset($this->raw[$field])) {
return $this->raw[$field];
} else {
if (preg_match('/^is_/', $field)) {
return false;
} else {
return null;
}
}
if (preg_match('/^is_/', $field)) {
return false;
}

return null;
}

/**
* @ignore
*
* @param mixed $attr
*/
public function __get($attr)
{
if ($attr != "instance" && property_exists($this, $attr)) {
if ($attr !== 'instance' && property_exists($this, $attr)) {
return $this->$attr;
}

Expand All @@ -49,10 +52,12 @@ public function __get($attr)

/**
* @ignore
*
* @param mixed $attr
*/
public function __isset($attr)
{
return $attr != "instance" && isset($this->$attr);
return $attr !== 'instance' && isset($this->$attr);
}

public function jsonSerialize()
Expand Down
18 changes: 7 additions & 11 deletions lib/maxmind/GeoIp2/Model/AnonymousIp.php
Expand Up @@ -5,24 +5,18 @@
/**
* This class provides the GeoIP2 Anonymous IP model.
*
* @property-read boolean $isAnonymous This is true if the IP address belongs to
* @property-read bool $isAnonymous This is true if the IP address belongs to
* any sort of anonymous network.
*
* @property-read boolean $isAnonymousVpn This is true if the IP address belongs to
* @property-read bool $isAnonymousVpn This is true if the IP address belongs to
* an anonymous VPN system.
*
* @property-read boolean $isHostingProvider This is true if the IP address belongs
* @property-read bool $isHostingProvider This is true if the IP address belongs
* to a hosting provider.
*
* @property-read boolean $isPublicProxy This is true if the IP address belongs to
* @property-read bool $isPublicProxy This is true if the IP address belongs to
* a public proxy.
*
* @property-read boolean $isTorExitNode This is true if the IP address is a Tor
* @property-read bool $isTorExitNode This is true if the IP address is a Tor
* exit node.
*
* @property-read string $ipAddress The IP address that the data in the model is
* for.
*
*/
class AnonymousIp extends AbstractModel
{
Expand All @@ -35,6 +29,8 @@ class AnonymousIp extends AbstractModel

/**
* @ignore
*
* @param mixed $raw
*/
public function __construct($raw)
{
Expand Down

0 comments on commit 5fab66a

Please sign in to comment.