Skip to content
This repository has been archived by the owner on Dec 10, 2022. It is now read-only.

Commit

Permalink
Merge develop into database-improvements-#21
Browse files Browse the repository at this point in the history
  • Loading branch information
tdmalone committed Jun 21, 2017
2 parents 7b71145 + 68f074a commit 50394f5
Show file tree
Hide file tree
Showing 20 changed files with 171 additions and 110 deletions.
9 changes: 1 addition & 8 deletions .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@ coverage:
default: false # Disable the default status that measures entire project, it seems to have trouble finding commits to compare against

notify:

# Slack
slack:
default:
url: secret:oCrBaxCGOkkOw2lS8/j3rPngZhcRXh9kN9XjSbMDM8zfskRv9qc8nLHsPE4nQ2IDMtCwU79dp/0olPDYjP8n3PaekRE0bq7jtZOyoyn2yRdojOLWGjfA4gyPI0bYApdTQPvFCeetsDg/7KKp/7lUcwryhtVoj2CwwvPi+rftVVQ=

# Gitter
gitter:
default:
url: secret:aSildbzaxigSAte00r9azncZLwEIV7p2EO84VSBsfAlwMvJwML0bzZn7CvEsHpYQm5MB1nYX4bYBJRHSlVQ9lo2XReDo2nilR5RVklbKC6caorVgOd4UzrIOnIghF1e6
url: secret:KUlue5nEd+KPP27k+9VunKrC5dPIIl6BZcLRMy0zeoYp9Fa0oGZTynRPiebbYvxfQwHvLC9tZhY88Pkimjgs9WTH+31inFKwAxCQXNid7xe8aUlTgPdn6fyNar3ZLGiKZZiW1EOJPal4ivV0rcnskn91JpS+Cw56rmvLfYZhSvs=

31 changes: 18 additions & 13 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

# Development config options example for Slackemon
# Development config options example for Slackémon
# Alessandro Pezzè / Tim Malone / May 2017

# Set these as environment variables, or, if running in development environment, copy this file to .env
# and set them here. You will need to set APP_ENV as an environment variable to 'development' for this
# to be read.
# Set these as environment variables, or, if running in a development environment, copy this file to .env
# and set them here. If you're not using Docker Compose, you will need to set APP_ENV as an environment
# variable to 'development' for this file to be read by PHP dotenv.

###################### REQUIRED VARIABLES ######################

Expand All @@ -25,7 +25,8 @@ SLACKEMON_INBOUND_URL=https://example.com/slackemon/

# Set a random token that cron.php will check for before running.
# REQUIRED: When running cron over the web eg. `cron.php?token=XXXXX` - protects from being invoked by someone else.
# NOT REQUIRED: When you run cron.php from within your environment eg. `/usr/local/bin/php /var/www/html/cron.php`.
# NOT REQUIRED: When you run cron.php from within your environment eg. `/usr/local/bin/php /var/www/html/cron.php` or
# over the web on a local network (eg. in our Docker Compose set up).
SLACKEMON_CRON_TOKEN=XXXXXXXXXXXXXXXXXXXX

###################### OPTIONAL VARIABLES ######################
Expand All @@ -35,12 +36,12 @@ SLACKEMON_OPENWEATHERMAP_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

# Configure the data cache/store.
# Local is generally recommended, unless of course you're hosting on a service with a ephemeral filesystem.
# The data cache cannot be disabled - it is required to run Slackemon.
# The data cache cannot be disabled - it is required to run Slackémon.

# 'aws', 'local' or 'postgres' (AWS is not recommended)
# 'aws', 'local' or 'postgres' (AWS is not recommended for this option; S3 is not best for regularly changing data)
SLACKEMON_DATA_STORE_METHOD=local

# 'aws' or 'local'
# 'aws' or 'local' (when AWS is used, a backup local cache will be kept to reduce costs - great for ephemeral filesystems)
SLACKEMON_DATA_CACHE_METHOD=local

# Local data folder for use if 'local' is set for either of the above options
Expand All @@ -52,9 +53,9 @@ SLACKEMON_DATA_FOLDER=.data
SLACKEMON_DATA_BUCKET=xxxxxxxxxxxxxxx

# Configure the image cache - two methods are supported; either local, or via an AWS S3 bucket (or you can disable).
# You should use the AWS option unless you are hosting Slackemon on a suitably fast server/connection.
# You should use the AWS option unless you are hosting Slackémon on a suitably fast server/connection.
# Please do NOT disable the image cache unless doing so for a short time for testing - otherwise it will cause unfair
# load for the external resources that Slackemon uses.
# load for the external media resources that Slackémon uses.

# 'aws', 'local', or 'disabled'
SLACKEMON_IMAGE_CACHE_METHOD=local
Expand All @@ -68,12 +69,16 @@ SLACKEMON_IMAGE_CACHE_BUCKET=xxxxxxxxxxxxxxx

# Postgres access details.
# Only required if using 'postgres' for the data store above.
# The format is postgres://username:password@host:port/database_name
SLACKEMON_DATABASE_URL=postgres://postgres:password@172.17.0.2:5432/slackemon
# Docker Compose format:
POSTGRES_USER=slackemon_user
POSTGRES_PASSWORD=slackemon_password
POSTGRES_DB=slackemon_db
# Alternative Postgres URL format if not using Docker Compose (uncomment to use; overrides the above):
#SLACKEMON_DATABASE_URL=postgres://username:password@host:5432/database_name

# AWS access details.
# Only required if using 'aws' for either the data cache or image cache above.
# 'us-east-1' is recommended for the region, as it is the same region Slack uses!
# 'us-east-1' is recommended for the region, as it is the same region Slack uses and they'll be the ones requesting!
SLACKEMON_AWS_ID=XXXXXXXXXXXXXXXXXXXX
SLACKEMON_AWS_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
SLACKEMON_AWS_REGION=us-east-1
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
/tests/logs
error_log
phpcs.xml
phpunit.xml
phpunit.xml
docker-compose.override.yml
9 changes: 1 addition & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,14 @@ after_success:
- travis_retry php vendor/bin/codacycoverage clover tests/logs/clover.xml

notifications:

# Slack
slack:
rooms:
- secure: m2inJu16LhMQbAu8Z3gBwxuNU6emCDzIHObtmtoPS8ZevlJRfQj5Qi9EQSB4/AN1277aKg6kGi5B7cobaxWla5kWe6NCGKxFcpo099mZ3gB6sTPttz1E8XZbaEzast2oN+kRuiylJ7/XApiUfthrERpoeV9nBoGuuzTpfTNAzBT3ocC4wmNLDuT/UycBuN8uTgbU92CsKf41LP7m4IsOgCF7sC/tt2RogQNwfHoSjbsSSdn2FZdkA4n+hJRxz689z+Re4RCKzaZxJSoIFTn1ViNXyTOQF10brHrFBPKnqDfH0bC0rIC7RIaZ6s3rFrT/kb+AcScaHDbfS6tnC4Tl2TQtY4dUArIEstFIAgt9932TBRKsRdWO4m3nsRXbpKhLj6eVpC+ggVzsDFZ6QC6RBoO6v/XJyTyjwCHXOXWWXGrBqmKq3vz+71QO4j+CG2zGx/BPAVS45qx2nit/kXvCKifU66CuRvmejDz9aDpyRAZhnB/S7WJvXj9iCa6pgj+xreUm3Nif/ZGgyDEzdJCRQzwEP+nB0vxHX3yzraW25AUWahYLkAH1wKh39bc9YBFWzHsvcHX7sJZfJq9ZFpnbF33g4KdtRVqtRj//D32MTs9vzYFUNnqyJK5P5F94Kf78ZsGqIVt3q1Rw8jUCNVbDSHnBY1F0+JotT9eOU1ijadk=
- secure: p3dH7L5CFpk6lrmb7aiHVKwEl8Qpua+Mp3n09rYTE5LhTMaLHczzjqjatNrcyt4GMKSzpJI4axxZ3QXe8KCnl3rr6JwfEHR4jvS4TZbchKFuRfgwF54RMXbUtHfy9fK78LeAo6eQoBliEuDo9Qgk1GTOMtmBv9gOoYRgo3o5sgAAPhF8soD4daNJ70vIwUgZ3bGN7/KTMPweDuNfERJM9w/OrLtYuNKwFPNGSHHkONR5Y5H2lGZtxw9/kZxtvounAFQHwMZfuZpyPSqrqfK8xxJLumTdTrmb14JNm1o4MWelErBHXLmDcTJvJ03mRvEswxbFIePMSZjA913pFZkdR7uWzFo0Z5RRAi3LG9P5s+7UnQDnUxJIaXWYli9j8ikTuPOHXneiYmn4aGvHATvOA+MJ6EDNA1H1dwJ1PSfsFCeNWtvQ1Lt4hfA04GKQxIKoinakj23OmcUKK8eOW7zL3lu6pt1q8J61t33z+Oz/0Vf9i1ebFPNaKTv9M2sMHDLlFstx44WgGihUXhrhudT+1Kc580cYKiXumj8BQeMf9sjfZcn92iDyCk6cYHXdaLNvUqZzQKHAmFEUp/PL7mHd6+EKPz+AzPahfPhfQ3Wainy4woDjLagsODYGOlGicdoAe3SO2LvQO8fiLA7Ew3qGidxQFYOVJoGJgNjewRn9+ZY=
template:
- "Build <%{build_url}|#%{build_number}> (<%{compare_url}|%{commit}>) of %{repository}:%{branch} by %{author}"
- "%{result} in %{duration}"
- "(%{commit_message})"
# Gitter
webhooks:
urls:
- secure: bao71OyTzuN3rKR9XG+shHqwlSnjTKE7ZlAHspvUa3wMdiL0ttotpgday5OVfiYFT8DSvirmy+nMC2vCUZLuSM8umq5XJDqMjdjW3Cv+BQ+11/4pxGri7d3XHpbVUItdivCbldU3ma22iTh1Q7sTw3FhwQBDyezXUqvhwbwEVrMks3vMETr9vb99AbRfXn3PGcHYz7F1Ki3JnKnV18ZyH81qmt/pFZvpcqfsePTcuObON3iYSXbUHdOws/qUKFDCurg3NpZLRbwKdxfUnrbMV11OuZTTPtR8poVHFeSf4RpR1vrbS+QKFc5KXOuWe7CkDa7KEgZPRnprtfZWI7ageoS1EBLdbJEndRHbqpJpuYo1X1s9/hvh55cnSzC/zJCz4hMwMS5I0iMx2PGainZswXuER+RdTC3iOnzXdS9JoshsOqL1s9hBwMw5b5Ozj44vr6ySrHKN9cltFf6FhPZAwapKZK97hnOaGiJHHmcHQHSC6SJyS4m0nm8rRYRg2wTKsyKKnpRt6DA8xb+5f7D+7N3kI0PfHpLK+wYgr+HULf13Uz66A5gh+ScKsvyjxQOnLeHOwNOOwyD4SPrxzriGTyTN/hCfnILSH1fEF3r/mqPjGr/O6CvGT/RrHgUpfhghTf9S5u5HkUft+d+U+aim5RXpF9WP8IuGPRXzNCPT1ZI=
env:
global:
# Codacy code coverage
Expand Down
40 changes: 11 additions & 29 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,44 +1,26 @@
FROM php:7.0-apache
FROM php:7.0-fpm-alpine

LABEL maintainer "tdmalone@gmail.com"

COPY . /var/www/html
WORKDIR /var/www/html
ENV PROJECT_ROOT /slackemon

# Create a user
RUN useradd -ms /bin/bash slackemon
COPY . $PROJECT_ROOT
WORKDIR $PROJECT_ROOT

# Assign it to www-data
RUN usermod -a -G www-data slackemon

# Assign public_html to www-data
RUN chown -R www-data:www-data /var/www/html

# Change permission to only user and group www-data
RUN chmod -R 774 /var/www/html
RUN addgroup -g 1000 slackemon && \
adduser -u 1000 -G slackemon -s /bin/sh -D slackemon && \
chown -R slackemon:slackemon $PROJECT_ROOT && \
chmod -R 774 $PROJECT_ROOT

# Install git and zip, used by Composer; cron, nano, vim and finally postgres functions for PHP7
RUN apt-get update && apt-get install git zlib1g-dev cron nano vim libpq-dev -y && \
RUN apk --no-cache add git zlib-dev nano vim postgresql-dev && \
docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql && \
docker-php-ext-install zip pdo pdo_pgsql pgsql

# Install the Composer package manager
# Install Composer package manager
RUN curl -s http://getcomposer.org/installer | php

USER slackemon

# Install dependencies as non-root
# Install dependencies
RUN php composer.phar install

# Separate container vendor from local vendor
VOLUME /var/www/html/vendor

# Use root to start the server
USER root

# Set up the cron job every minute, with the correct environment
# then, start apache2 in foreground (https://github.com/docker-library/php/blob/76a1c5ca161f1ed6aafb2c2d26f83ec17360bc68/7.1/apache/Dockerfile#L205)
CMD printenv > /etc/environment && \
echo "* * * * * /usr/local/bin/php /var/www/html/cron.php" | crontab - && \
cron && \
apache2-foreground
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# Slackémon
[![Follow Slackémon on Twitter!](https://img.shields.io/twitter/follow/slackemon.svg?style=social&label=Follow)](https://twitter.com/slackemon)

[![Latest Release](https://img.shields.io/github/release/tdmalone/slackemon/all.svg)](https://github.com/tdmalone/slackemon/releases)
[![Join the chat at https://gitter.im/slackemon/Lobby](https://badges.gitter.im/slackemon/Lobby.svg)](https://gitter.im/slackemon/Lobby)
[![License](https://poser.pugx.org/tdmalone/slackemon/license)](https://github.com/tdmalone/slackemon/blob/master/LICENSE)
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bhttps%3A%2F%2Fgithub.com%2Ftdmalone%2Fslackemon.svg?type=shield)](https://app.fossa.io/projects/git%2Bhttps%3A%2F%2Fgithub.com%2Ftdmalone%2Fslackemon?ref=badge_shield)
[![Join us on Slack!](https://img.shields.io/badge/chat%2fplay-on%20slack-e01563.svg)](https://join.slack.com/playslackemon/shared_invite/MTk4Nzc5MTc0MDk2LTE0OTc3NTAwODgtNmU4MDZkZDU0MA)
[![Linux build status](https://travis-ci.org/tdmalone/slackemon.svg?branch=master)](https://travis-ci.org/tdmalone/slackemon)
[![Windows build status](https://img.shields.io/appveyor/ci/TimMalone/slackemon.svg)](https://ci.appveyor.com/project/TimMalone/slackemon)
[![Docker build status](https://img.shields.io/docker/build/tdmalone/slackemon.svg)](https://hub.docker.com/r/tdmalone/slackemon/builds/)
Expand Down Expand Up @@ -54,8 +53,8 @@ Scroll down for more setup options
Setup of Slackémon is _not_ quick. This may be worked on further in the future. For now, there are quite a few steps!

1. Download/clone/etc. the contents of this repository, and put it on a web server somewhere that runs PHP7.
* [Download ZIP](https://github.com/tdmalone/slackemon/archive/master.zip)
* Clone with Git: `git clone https://github.com/tdmalone/slackemon.git`
* [Download ZIP](https://github.com/tdmalone/slackemon/archive/master.zip) or clone with Git: `git clone https://github.com/tdmalone/slackemon.git`
* You may need to install depdendencies as well by running `composer install`. If you don't have Composer, [get it here](https://getcomposer.org/download/) first. If you're not going to be developing Slackémon, you can speed up the install of depdendencies by instead running `composer install --no-dev`.
* Install with Composer: `composer require tdmalone/slackemon`
* [Deploy with Docker](https://github.com/tdmalone/slackemon/wiki/Installing-with-Docker)
* [Deploy with Heroku](https://github.com/tdmalone/slackemon/wiki/Installing-on-Heroku)
Expand Down Expand Up @@ -114,6 +113,9 @@ Last but not least, this project would never have happened without the extensive

## License

[![License](https://poser.pugx.org/tdmalone/slackemon/license)](https://github.com/tdmalone/slackemon/blob/master/LICENSE)
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bhttps%3A%2F%2Fgithub.com%2Ftdmalone%2Fslackemon.svg?type=shield)](https://app.fossa.io/projects/git%2Bhttps%3A%2F%2Fgithub.com%2Ftdmalone%2Fslackemon?ref=badge_shield)

Slackémon - Catch and battle Pokémon with your teammates on Slack
Copyright (C) 2016-2017, [Tim Malone](https://github.com/tdmalone) and contributors.

Expand Down
11 changes: 1 addition & 10 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,7 @@ notifications:
# Slack
- provider: Slack
incoming_webhook:
secure: Z/sBH2RXNief0BVgo2cazy1KoJMgpA0dZup/E2NMKM9Z40bWyoIK0+yRRJ8HFRxZCNGS+Kri7epYUWDb4QZ8QZHOBKyREESninj1ZM2YT3U=
secure: Z/sBH2RXNief0BVgo2cazy1KoJMgpA0dZup/E2NMKM+cf3UbAFbkr+xk36cbCO5b0FLCE0QYdZFT6zCvGut/yftF6Ja6yEIkWqF6RNdzqyg=
on_build_success: true
on_build_failure: true
on_build_status_changed: true

# Gitter
# Currently configured in AppVeyor interface, as secure vars for webhook URLs are not supported yet
#- provider: Webhook
# url:
# secure: /2tAaEmW+de0UbUmtpuQTWfzeiLuKfsUVjzDDMK0kKNkUlFSmH6Ik1/mRpHrhBiUn8SvsDlYSehnSHEFEbN4HQ==
# on_build_success: true
# on_build_failure: true
# on_build_status_changed: true
13 changes: 12 additions & 1 deletion config.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,18 @@
define( 'SLACKEMON_IMAGE_CACHE_FOLDER', getenv( 'SLACKEMON_IMAGE_CACHE_FOLDER' ) ?: '.image-cache' );
define( 'SLACKEMON_IMAGE_CACHE_BUCKET', getenv( 'SLACKEMON_IMAGE_CACHE_BUCKET' ) );

define( 'SLACKEMON_DATABASE_URL', getenv( 'SLACKEMON_DATABASE_URL' ) ?: getenv( 'DATABASE_URL' ) );
// Prefer SLACKEMON_DATABASE_URL, fallback to DATABASE_URL (Heroku) and then to POSTGRES_USER/PASSWORD/DB (Docker Compose)
if ( getenv( 'SLACKEMON_DATABASE_URL' ) ) {
define( 'SLACKEMON_DATABASE_URL', getenv( 'SLACKEMON_DATABASE_URL' ) );
} else if ( getenv( 'DATABASE_URL' ) ) {
define( 'SLACKEMON_DATABASE_URL', getenv( 'DATABASE_URL' ) );
} else if ( getenv( 'POSTGRES_USER' ) && getenv( 'POSTGRES_PASSWORD' ) && getenv( 'POSTGRES_DB' ) ) {
define(
'SLACKEMON_DATABASE_URL',
'postgres://' . getenv( 'POSTGRES_USER' ) . ':' . getenv( 'POSTGRES_PASSWORD' ) .
'@database:5432/' . getenv( 'POSTGRES_DB' )
);
}

define( 'SLACKEMON_AWS_ID', getenv( 'SLACKEMON_AWS_ID' ) );
define( 'SLACKEMON_AWS_SECRET', getenv( 'SLACKEMON_AWS_SECRET' ) );
Expand Down
3 changes: 2 additions & 1 deletion cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
define( 'SKIP_AUTH', true );
require_once( __DIR__ . '/lib/init.php' );

// AUTH: Check if the cron token was set if running over the web.
// AUTH: Check if the cron token was set - if running over the web from a non-local IP address.
if (
'cli' !== php_sapi_name() &&
! slackemon_is_ip_private( $_SERVER['REMOTE_ADDR'] ) &&
( ! isset( $_REQUEST['token'] ) || SLACKEMON_CRON_TOKEN !== $_REQUEST['token'] )
) {
http_response_code( 403 );
Expand Down
42 changes: 42 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
version: "2"
services:
php:
image: tdmalone/slackemon
restart: always
volumes:
- ./:/slackemon
env_file:
- .env

webserver:
image: nginx:alpine
restart: always
ports:
- "80:80"
volumes:
- ./:/slackemon
- ./etc/nginx/default.conf:/etc/nginx/conf.d/default.conf

database:
image: postgres:alpine
restart: always
expose:
- "5432"
volumes:
- db_data:/var/lib/postgresql/data
environment:
- PGDATA=/var/lib/postgresql/data/pgdata
env_file:
- .env

cronjob:
image: appropriate/curl
restart: always
volumes:
- ./etc/crond/root:/var/spool/cron/crontabs/root
command: crond -f

# Local volume where to store data
# in a persistent way
volumes:
db_data:
1 change: 1 addition & 0 deletions etc/crond/root
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* * * * * curl webserver/cron.php
22 changes: 22 additions & 0 deletions etc/nginx/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
server {
listen 80;
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /slackemon;

location ~ /\. {
deny all;
}

location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
36 changes: 34 additions & 2 deletions lib/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function check_subcommands( $allowed_subcommands = [], $welcome_message = '' ) {
function slackemon_run_background_command( $path, $args, $additional_fields = [], $additional_fields_as_json = false ) {

// Build command URL
$command_url = 'http://' . $_SERVER['SERVER_NAME'];
$command_url = 'http://' . $_SERVER['HTTP_HOST'];
$command_url .= 80 != $_SERVER['SERVER_PORT'] && 443 != $_SERVER['SERVER_PORT'] ? ':' . $_SERVER['SERVER_PORT'] : '';
$command_url .= str_replace( basename( $_SERVER['SCRIPT_NAME'] ), '', $_SERVER['SCRIPT_NAME'] );
$command_url .= $path;
Expand Down Expand Up @@ -153,7 +153,7 @@ function slackemon_run_background_command( $path, $args, $additional_fields = []
function slackemon_run_background_action( $path, $action, $callback_id ) {

// Build action URL
$action_url = 'http://' . $_SERVER['SERVER_NAME'];
$action_url = 'http://' . $_SERVER['HTTP_HOST'];
$action_url .= 80 != $_SERVER['SERVER_PORT'] && 443 != $_SERVER['SERVER_PORT'] ? ':' . $_SERVER['SERVER_PORT'] : '';
$action_url .= str_replace( basename( $_SERVER['SCRIPT_NAME'] ), '', $_SERVER['SCRIPT_NAME'] );
$action_url .= $path;
Expand Down Expand Up @@ -221,4 +221,36 @@ function strtotitle( $title ) {

} // Function strtotitle

/**
* Checks if an IP address is from a recognised private range.
*
* @link https://stackoverflow.com/a/13818126/1982136
*/
function slackemon_is_ip_private( $ip ) {

$private_ranges = [
'10.0.0.0|10.255.255.255', // Single class A network
'172.16.0.0|172.31.255.255', // 16 contiguous class B network
'192.168.0.0|192.168.255.255', // 256 contiguous class C network
'169.254.0.0|169.254.255.255', // Link-local address aka Automatic Private IP Addressing
'127.0.0.0|127.255.255.255' // Localhost
];

$long_ip = ip2long( $ip );

if ( $long_ip && -1 !== $long_ip ) {

foreach ( $private_ranges as $range ) {
list ( $start, $end ) = explode( '|', $range );

if ( $long_ip >= ip2long( $start ) && $long_ip <= ip2long( $end ) ) {
return true;
}
}
}

return false;

} // Function slackemon_is_ip_private

// The end!
File renamed without changes
File renamed without changes
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<env name="SLACKEMON_INBOUND_URL" value="https://example.com/" />

<!-- Define the minimum server vars we expect to be set. -->
<server name="SERVER_NAME" value="slackemon-unit-tests" />
<server name="HTTP_HOST" value="slackemon-unit-tests" />
<server name="SERVER_PORT" value="8080" />

<!-- Define the post vars we will expect from Slack during a slash command invocation. -->
Expand Down
Loading

0 comments on commit 50394f5

Please sign in to comment.