Skip to content

Commit

Permalink
Merge pull request #83 from Tjoosten/dotenv
Browse files Browse the repository at this point in the history
DotEnv for better configuration
  • Loading branch information
Pieter Colpaert committed Jul 11, 2015
2 parents c601416 + f8b5ebb commit cffe10c
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 77 deletions.
28 changes: 28 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
--- Database credentials
---
apiHost=localhost
apiUser=irail
apiTable=apilog
apiPassword=passwd
apiDatabase=irail

---
--- Database columns
---

column1=id
column2=time
column3=useragent
column4=fromstation
column5=tostation
column6=errors
column7=ip
column8=server

---
--- API servername
---

// API server name
apiServerName=0
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ api/push.php
.vagrant
.idea/
/vendor/
.env
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ Native applications using the iRail API and created or supported by the iRail te

All information can be found on [Project iRail](http://project.irail.be/).

### Configuration

For the configuration we used a package named dotenv.
There is a `.env example` located in the base folder from the project.
`composer install` wil write a nex .env file for your confiration.
The only thing you need to do is change the variables so it fits to your credentails.

Some interesting links:

* Source: <http://github.com/iRail/iRail>
Expand Down
22 changes: 16 additions & 6 deletions api/APICall.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*
* @author Pieter Colpaert
*/

use Dotenv\Dotenv;

ini_set("include_path", ".:data");
include_once("data/DataRoot.php");
include_once("data/structs.php");
Expand Down Expand Up @@ -124,8 +127,13 @@ protected function writeLog($ua, $from, $to, $err, $ip) {
$ua = mysql_real_escape_string($ua);
// insert in db
try {
include("../includes/dbConfig.php");
$query = "INSERT INTO $api_table ($api_c2, $api_c3, $api_c4, $api_c5, $api_c6, $api_c7, $api_c8) VALUES('$now', '$ua', '$from', '$to', '$err', '$ip', '$api_server_name')";
$dotenv = new Dotenv(dirname(__DIR__));
$dotenv->load();

$query = "
INSERT INTO $api_table ($api_c2, $api_c3, $api_c4, $api_c5, $api_c6, $api_c7, $api_c8)
VALUES('$now', '$ua', '$from', '$to', '$err', '$ip', '". $_ENV['apiServerName'] ."')";

$result = mysql_query($query);
}
catch (Exception $e) {
Expand All @@ -135,10 +143,12 @@ protected function writeLog($ua, $from, $to, $err, $ip) {


public static function connectToDB(){
try {
include("../includes/dbConfig.php");
mysql_pconnect($api_host, $api_user, $api_password);
mysql_select_db($api_database);
try {
$dotenv = new Dotenv(dirname(__DIR__));
$dotenv->load();

mysql_pconnect($_ENV['apiHost'], $_ENV['apiUser'], $_ENV['apiPassword']);
mysql_select_db($_ENV['apiDatabase']);
}
catch (Exception $e) {
throw new Exception("Error connecting to the database.", 3);
Expand Down
17 changes: 12 additions & 5 deletions api/stats/api-stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@
Source available at http://github.com/Tuinslak/iRail
*/

use Dotenv\Dotenv;

// "Public" API stats page
// to prevent giving MySQL access to *

// vars
$limit = 250;

// include vars
include("../../includes/dbConfig.php");
$dotenv = new Dotenv(dirname(__DIR__));
$dotenv->load();

$s = $_REQUEST["s"];

Expand All @@ -44,13 +46,18 @@
$count = 1 + $s;

try {
mysql_pconnect($api_host, $api_user, $api_password);
mysql_pconnect($_ENV['apiHost'], $_ENV['apiUser'], $_ENV['apiPassword']);
mysql_select_db($api_database);
$query = "SELECT COUNT($api_c1) FROM $api_table";
$query = "SELECT COUNT(". $_ENV['column1'] .") FROM ". $_ENV['apiTable'] ."";
$result = mysql_query($query);
$numrows = mysql_result($result, 0);

$query = "SELECT $api_c1, $api_c2, $api_c3, $api_c4, $api_c5, $api_c6, $api_c7, $api_c8 FROM $api_table ORDER BY $api_c1 DESC LIMIT $s,$limit";
$dbColumns = [
$_ENV['column1'], $_ENV['column2'], $_ENV['column3'], $_ENV['column4'],
$_ENV['column5'], $_ENV['column6'], $_ENV['column7'], $_ENV['column8']
];

$query = "SELECT $dbColumns FROM $api_table ORDER BY $api_c1 DESC LIMIT $s,$limit";
$result = mysql_query($query);
}
catch (Exception $e) {
Expand Down
28 changes: 24 additions & 4 deletions api/stats/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,39 @@
// "Public" API stats page
// Gives report about all days report
// include vars
include("../../includes/dbConfig.php");
use Dotenv\Dotenv;

$dotenv = new Dotenv(dirname(__DIR__));
$dotenv->load();

$filter = "";
if (isset($_GET['filter'])) {
$filter = mysql_escape_string($_GET['filter']);
}
try {
mysql_pconnect($api_host, $api_user, $api_password);
mysql_pconnect($_ENV['apiHost'], $_ENV['apiUser'], $_ENV['apiPassword']);
mysql_select_db($api_database);

$api_c2 = $_ENV['column2'];
$api_table = $_ENV['apiTable'];

if ($filter != "") {
$query = "SELECT DATE_FORMAT(STR_TO_DATE($api_c2,'%a, %d %b %Y %T'), '%d %m %Y') day, count(id) visitors FROM $api_table WHERE $api_c3 LIKE '%$filter%' GROUP BY DATE_FORMAT(STR_TO_DATE($api_c2,'%a, %d %b %Y %T'), '%d %b %Y') ORDER BY DATE_FORMAT(STR_TO_DATE($api_c2,'%a, %d %b %Y %T'), '%Y') desc, DATE_FORMAT(STR_TO_DATE($api_c2,'%a, %d %b %Y %T'), '%m') desc, DATE_FORMAT(STR_TO_DATE($api_c2,'%a, %d %b %Y %T'), '%d') desc LIMIT 1000";
$query = "
SELECT DATE_FORMAT(STR_TO_DATE($api_c2,'%a, %d %b %Y %T'), '%d %m %Y') day, count(id) visitors
FROM $api_table
WHERE $api_c3 LIKE '%$filter%'
GROUP BY DATE_FORMAT(STR_TO_DATE($api_c2,'%a, %d %b %Y %T'), '%d %b %Y')
ORDER BY DATE_FORMAT(STR_TO_DATE($api_c2,'%a, %d %b %Y %T'), '%Y') desc,
DATE_FORMAT(STR_TO_DATE($api_c2,'%a, %d %b %Y %T'), '%m') desc,
DATE_FORMAT(STR_TO_DATE($api_c2,'%a, %d %b %Y %T'), '%d') desc LIMIT 1000";
} else {
$query = "SELECT DATE_FORMAT(STR_TO_DATE($api_c2,'%a, %d %b %Y %T'), '%d %b %Y') day, count(id) visitors FROM $api_table GROUP BY DATE_FORMAT(STR_TO_DATE($api_c2,'%a, %d %b %Y %T'), '%d %b %Y') ORDER BY DATE_FORMAT(STR_TO_DATE($api_c2,'%a, %d %b %Y %T'), '%Y') desc, DATE_FORMAT(STR_TO_DATE($api_c2,'%a, %d %b %Y %T'), '%m') desc, DATE_FORMAT(STR_TO_DATE($api_c2,'%a, %d %b %Y %T'), '%d') desc LIMIT 1000";
$query = "
SELECT DATE_FORMAT(STR_TO_DATE($api_c2,'%a, %d %b %Y %T'), '%d %b %Y') day, count(id) visitors
FROM $api_table
GROUP BY DATE_FORMAT(STR_TO_DATE($api_c2,'%a, %d %b %Y %T'), '%d %b %Y')
RDER BY DATE_FORMAT(STR_TO_DATE($api_c2,'%a, %d %b %Y %T'), '%Y') desc,
DATE_FORMAT(STR_TO_DATE($api_c2,'%a, %d %b %Y %T'), '%m') desc,
DATE_FORMAT(STR_TO_DATE($api_c2,'%a, %d %b %Y %T'), '%d') desc LIMIT 1000";
}
$result = mysql_query($query);
$rows;
Expand Down
32 changes: 20 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
{
"name": "irail/irail",
"description": "The iRail API",
"license": "AGPLv3",
"authors": [
{
"name": "Pieter Colpaert",
"email": "pieter.colpaert@okfn.org"
}
],
"require": {

"name": "irail/irail",
"description": "The iRail API",
"license": "AGPLv3",
"authors": [
{
"name": "Pieter Colpaert",
"email": "pieter.colpaert@okfn.org"
}
}
],
"require": {
"vlucas/phpdotenv": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "4.7.*"
},
"scripts": {
"post-install-cmd": [
"php -r \"copy('.env.example', '.env');\""
]
}
}
50 changes: 0 additions & 50 deletions includes/dbConfig.example.php

This file was deleted.

26 changes: 26 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="./tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
syntaxCheck="true"
strict="true"
verbose="true"
>
<testsuites>
<testsuite name="PHP Dotenv Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
16 changes: 16 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
error_reporting(-1);
date_default_timezone_set('UTC');
/*
* Path trickery ensures test suite will always run, standalone or within
* another composer package. Designed to find composer autoloader and require
*/
$vendorPos = strpos(__DIR__, 'vendor/vlucas/phpdotenv');
if ($vendorPos !== false) {
// Package has been cloned within another composer package, resolve path to autoloader
$vendorDir = substr(__DIR__, 0, $vendorPos).'vendor/';
$loader = require $vendorDir.'autoload.php';
} else {
// Package itself (cloned standalone)
$loader = require __DIR__.'/../vendor/autoload.php';
}
29 changes: 29 additions & 0 deletions tests/configurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
use Dotenv\Dotenv;
class configurationTest extends \PHPUnit_Framework_TestCase
{
public function testDotEnvVars()
{
$dotenv = new Dotenv(dirname(__DIR__));
$dotenv->load();

// API Database credentials
$this->assertEquals($_ENV['apiHost'], 'localhost');
$this->assertEquals($_ENV['apiUser'], 'irail');
$this->assertEquals($_ENV['apiTable'], 'apilog');
$this->assertEquals($_ENV['apiPassword'], 'passwd');

// API database Columns
$this->assertEquals($_ENV['column1'], 'id');
$this->assertEquals($_ENV['column2'], 'time');
$this->assertEquals($_ENV['column3'], 'useragent');
$this->assertEquals($_ENV['column4'], 'fromstation');
$this->assertEquals($_ENV['column5'], 'tostation');
$this->assertEquals($_ENV['column6'], 'errors');
$this->assertEquals($_ENV['column7'], 'ip');
$this->assertEquals($_ENV['column8'], 'server');

// API nameserver
$this->assertEquals($_ENV['apiServerName'], 0);
}
}

0 comments on commit cffe10c

Please sign in to comment.