Added Config class
authorIgor Scheller <igor.scheller@igorshp.de>
Fri, 1 Dec 2017 12:27:49 +0000 (13:27 +0100)
committerIgor Scheller <igor.scheller@igorshp.de>
Fri, 1 Dec 2017 12:27:49 +0000 (13:27 +0100)
13 files changed:
config.sample.php
include/cryptography.php
include/database.php
include/directdebit.php
include/earnings.php
include/events.php
include/expenses.php
include/fees.php
include/finances.php
include/members.php
index.php
js/sepa-CORE.xml
src/Config/Config.php [new file with mode: 0644]

index 63cce1b..6d3f34a 100644 (file)
@@ -1,24 +1,23 @@
 <?php
 
-define('IS_INSTALLED', 0);
-define('DB_USER', 'cccffm');
-define('DB_PASSWORD', 'geheim');
-define('DB_NAME', 'cccffm');
-define('DB_HOST', 'localhost');
-define('DB_CHARSET', 'utf8');
-define('DB_PREFIX', 'memberdb_');
-
-define('FOUNDING_DATE', strtotime('02.10.2009'));
-define('DIRECTDEBIT_DATE', strtotime('01.07.2010'));
-
-define('DIRECTDEBIT_DAY_OF_MONTH', 15);
-
-define('ACCOUNT_HOLDER', 'CCCFFM e.V.');
-define('ACCOUNT_NUMBER', '1234567890');
-define('BANK_CODE', '0987654321');
-
-define('CREDITOR_IDENTIFIER', 'DE00ZZZ00000000000');
-define('IBAN', 'DE89370400440532013000');
-define('BIC', 'TESTDEX0XXX');
-# Mandatory until 2014-01-31 (DE), 2016-01-31 (EU)
-define('USE_BIC', 1);
+return [
+    'db'            => [
+        'user'     => 'memberdb',
+        'password' => 'password',
+        'database' => 'memberdb',
+        'host'     => 'localhost',
+    ],
+    'founding_date' => new DateTime('2009-10-02'),
+    'direct_debit'  => [
+        'date' => new DateTime('2010-07-01'),
+        'day'  => 15,
+    ],
+    'bank'          => [
+        'holder'      => 'CCCFFM e.V.',
+        'number'      => '1234567890',
+        'code'        => '0987654321',
+        'creditor_id' => 'DE00ZZZ00000000000',
+        'iban'        => 'DE89370400440532013000',
+        'bic'         => 'TESTDEX0XXX',
+    ],
+];
index 311df5b..74ded63 100644 (file)
@@ -62,7 +62,7 @@ $SQL_CREATE_CRYPTO = sprintf('
       `created_at` DATETIME NOT NULL,
       `modified_at` DATETIME NOT NULL
     ) ENGINE=InnoDB DEFAULT CHARSET=%2$s
-    ', DB_TABLE_CRYPTO, DB_CHARSET);
+    ', DB_TABLE_CRYPTO, 'utf8');
 
 /* DB functions {{{ */
 function db_get_crypto_keys()
index 3c5c1ab..7c9fe80 100644 (file)
@@ -1,11 +1,14 @@
 <?php
 
 /* DATABASE TABLE NAMES {{{ */
-define('DB_TABLE_CRYPTO', DB_PREFIX . 'crypto');
-define('DB_TABLE_EVENTS', DB_PREFIX . 'events');
-define('DB_TABLE_MEMBERS', DB_PREFIX . 'members');
-define('DB_TABLE_EARNINGS', DB_PREFIX . 'earnings');
-define('DB_TABLE_EXPENSES', DB_PREFIX . 'expenses');
+
+use MemberDB\Config\Config;
+
+define('DB_TABLE_CRYPTO', 'memberdb_crypto');
+define('DB_TABLE_EVENTS', 'memberdb_events');
+define('DB_TABLE_MEMBERS', 'memberdb_members');
+define('DB_TABLE_EARNINGS', 'memberdb_earnings');
+define('DB_TABLE_EXPENSES', 'memberdb_expenses');
 /* }}} */
 
 
@@ -24,11 +27,13 @@ if (function_exists('mysql_set_charset') === false) {
 function db_connect()
 {
     global $dbh;
-    if (!($dbh = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD, true))) {
+    $config = Config::getInstance();
+    $db = $config->get('db');
+    if (!($dbh = mysql_connect($db['host'], $db['user'], $db['password'], true))) {
         throw new Exception('<html><body>Konnte keine Verbindung zur Datenbank herstellen.</body></html>');
     }
-    mysql_set_charset(DB_CHARSET, $dbh);
-    if (!mysql_select_db(DB_NAME)) {
+    mysql_set_charset('utf8', $dbh);
+    if (!mysql_select_db($db['database'])) {
         throw new Exception('<html><body>Eine Verbindung zur Datenbank konnte hergestellt werden, aber die angegebene Datenbank konnte nicht ausgewählt werden.</body></html>');
     }
 }
index bf07f17..a00e79a 100644 (file)
@@ -1,9 +1,14 @@
 <?php
 
 
+use MemberDB\Config\Config;
+
 function action_directdebit()
 {
 
+    $config = Config::getInstance();
+    $bank = $config['bank'];
+
     $debittype = array(
         1 => 'DTAUS',
         2 => 'FRST',
@@ -220,7 +225,7 @@ function action_directdebit()
 
                 switch ($('#directdebittype').val()) {
                     case "dtaus":
-                        if (!DTAUS.setAccountFileSender('<?=dtaus_string(ACCOUNT_HOLDER)?>', '<?=BANK_CODE?>', '<?=ACCOUNT_NUMBER?>')) {
+                        if (!DTAUS.setAccountFileSender('<?php echo dtaus_string($bank['holder']); ?>', '<?php echo $bank['code']; ?>', '<?php echo $bank['number']; ?>')) {
                             $('#directdebit_content').val(DTAUS.errormsg);
                             return;
                         }
@@ -252,16 +257,14 @@ function action_directdebit()
 
                     case "pain":
 
-                    <?php $bic = (USE_BIC == 1) ? '\'' . BIC . '\'' : 'null'; ?>
-
                         // datejs.com MAGIC
                         var jetzt = new Date.today();
-                        var collectiondate = new Date.parse('<?=DIRECTDEBIT_DAY_OF_MONTH?>');
-                        if (jetzt.getDate() > <?=DIRECTDEBIT_DAY_OF_MONTH?>) {
+                        var collectiondate = new Date.parse('<?php echo $config->get('direct_debit')['day']; ?>');
+                        if (jetzt.getDate() > <?php echo $config->get('direct_debit')['day']; ?>) {
                             collectiondate = collectiondate.add(1).month();
                         }
 
-                        if (!SEPACORE.init(collectiondate, '<?=CREDITOR_IDENTIFIER?>', '<?=ACCOUNT_HOLDER?>', '<?=IBAN?>', <?=$bic?>)) {
+                        if (!SEPACORE.init(collectiondate, '<?php echo $bank['creditor_id']; ?>', '<?php echo $bank['holder']; ?>', '<?php echo $bank['iban']; ?>', '<?php echo $bank['bic']; ?>'')) {
                             $('#directdebit_content').val(DTAUS.errormsg);
                             return;
                         }
@@ -402,7 +405,6 @@ function dtaus_string($string)
         'Ĥ' => 'H',
         'Ħ' => 'H',
         'ì' => 'i',
-        'ì' => 'i',
         'î' => 'i',
         'ï' => 'i',
         'ĩ' => 'i',
index 7f06f28..9744751 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+use MemberDB\Config\Config;
+
 $SQL_CREATE_EARNINGS = sprintf('
     CREATE TABLE IF NOT EXISTS `%1$s` (
       `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
@@ -16,7 +18,7 @@ $SQL_CREATE_EARNINGS = sprintf('
       INDEX `idx_member_id` (`member_id`),
       INDEX `idx_date` (`date`)
     ) ENGINE=InnoDB DEFAULT CHARSET=%2$s
-    ', DB_TABLE_EARNINGS, DB_CHARSET);
+    ', DB_TABLE_EARNINGS, 'utf8');
 
 $EARNING_TYPES = array(
     'fee'              => 'Mitgliedsbeitrag',
@@ -24,7 +26,7 @@ $EARNING_TYPES = array(
     'interest'         => 'Zinsen',
     'room'             => 'Raumeinnahme',
     'account_transfer' => 'Umbuchung',
-    'old_fee'          => 'Mitgliedsbeiträge vor dem ' . format_date(DIRECTDEBIT_DATE),
+    'old_fee'          => 'Mitgliedsbeiträge vor dem ' . format_date(Config::getInstance()->get('direct_debit')['date']->getTimestamp()),
     'catering'         => 'Raum - Getraenke & Snacks'
 );
 
index 44dd3ee..0122345 100644 (file)
@@ -14,7 +14,7 @@ $SQL_CREATE_EVENTS = sprintf('
       PRIMARY KEY  (`id`),
       UNIQUE KEY `member_id_event_date` (`member_id`, `event_date`)
     ) ENGINE=InnoDB DEFAULT CHARSET=%2$s
-    ', DB_TABLE_EVENTS, DB_CHARSET);
+    ', DB_TABLE_EVENTS, 'utf8');
 
 $EVENT_TYPES = array(
     'joined'  => 'Eintritt',
index 3e5af93..5aed50f 100644 (file)
@@ -16,7 +16,7 @@ $SQL_CREATE_EXPENSES = sprintf('
       INDEX `idx_member_id` (`member_id`),
       INDEX `idx_date` (`date`)
     ) ENGINE=InnoDB DEFAULT CHARSET=%2$s
-    ', DB_TABLE_EXPENSES, DB_CHARSET);
+    ', DB_TABLE_EXPENSES, 'utf8');
 
 $EXPENSE_TYPES = array(
     'invoice'          => 'Rechnung',
index b826a7b..7b89f7c 100644 (file)
@@ -4,6 +4,8 @@
 /* HELPER functions FOR FEES {{{ */
 
 // build an empty structure for holding monthly information
+use MemberDB\Config\Config;
+
 function _fees_build_month_array($start_date, $end_date, $preset = array())
 {
     $start_info = getdate($start_date);
@@ -42,13 +44,14 @@ function _fees_build_month_array($start_date, $end_date, $preset = array())
 
 function _fees_apply_event_information(&$info, $events)
 {
+    $config = Config::getInstance();
     if (!empty($events)) {
 
         foreach ($events as $event) {
 
             $timestamp = db_date2unixtime($event['event_date']);
-            if ($timestamp < FOUNDING_DATE) {
-                $timestamp = FOUNDING_DATE;
+            if ($timestamp < $config->get('founding_date')) {
+                $timestamp = $config->get('founding_date');
             }
             $date_info = getdate($timestamp);
 
@@ -113,6 +116,7 @@ function fees_get_list_for_member($member_id, $end_date)
 {
 
     static $cache = array();
+    $config = Config::getInstance();
 
     $end_date = mktime(0, 0, 0, date('m', $end_date) + 1, 0, date('Y', $end_date)); // last day of given month
 
@@ -131,7 +135,7 @@ function fees_get_list_for_member($member_id, $end_date)
         }
     }
 
-    $ret = _fees_build_month_array(FOUNDING_DATE, $end_date);
+    $ret = _fees_build_month_array($config->get('founding_date'), $end_date);
     if (empty($ret)) {
         return;
     }
@@ -221,15 +225,18 @@ function fees_get_list_for_month($year, $month)
 function fee_next_directdebit_for_member($member_id, $max_date = null)
 {
 
+    $config = Config::getInstance();
     $member = db_get_member_with_id($member_id);
     if (!$member['directdebit']) {
         return null;
     }
 
-    $sum_old_fees = fees_sum_for_member($member_id, DIRECTDEBIT_DATE - 86400);
+    /** @var DateTime $direct_debit_date */
+    $direct_debit_date = $config->get('direct_debit')['date'];
+    $sum_old_fees = fees_sum_for_member($member_id, $direct_debit_date->getTimestamp() - 86400);
     $sum_new_paid = finance_get_paid_fees_for_member($member_id);
-    $year = date('Y', DIRECTDEBIT_DATE);
-    $month = date('n', DIRECTDEBIT_DATE);
+    $year = $direct_debit_date->format('Y');
+    $month = $direct_debit_date->format('n');
     $day = 1;
 
 
@@ -325,6 +332,7 @@ function action_fees()
 function render_fees_by_member()
 {/*{{{*/
     $members = db_get_members();
+    $config = Config::getInstance();
     ?>
     <h2>Mitgliedsbeitr&auml;ge nach Mitglied</h2>
     <table>
@@ -343,7 +351,9 @@ function render_fees_by_member()
             $current_fee = fees_for_member_at_date($member['id'], time());
 
             $sum_fees = fees_sum_for_member($member['id'], time());
-            $sum_old_fees = fees_sum_for_member($member['id'], DIRECTDEBIT_DATE - 86400);
+            /** @var DateTime $direct_debit */
+            $direct_debit = $config->get('direct_debit')['date'];
+            $sum_old_fees = fees_sum_for_member($member['id'], $direct_debit->getTimestamp() - 86400);
             $sum_old_paid = finance_get_paid_fees_for_member($member['id'], true);
             $sum_new_paid = finance_get_paid_fees_for_member($member['id']);
             $sum_new_fees = bcsub($sum_fees, $sum_old_fees);
@@ -499,6 +509,9 @@ function render_next_direct_debit()
 function render_fees_for_member($member_id)
 {/*{{{*/
     global $MEMBER_TYPES, $EARNING_TYPES, $EXPENSE_TYPES;
+    $config = Config::getInstance();
+    /** @var DateTime $direct_debit */
+    $direct_debit = $config->get('direct_debit')['date'];
 
     $member = db_get_member_with_id($member_id);
     if (!isset($member)) {
@@ -512,7 +525,7 @@ function render_fees_for_member($member_id)
 
     $sum_new_paid = finance_get_paid_fees_for_member($member_id);
     $sum_old_paid = finance_get_paid_fees_for_member($member_id, true);
-    $sum_old_fees = fees_sum_for_member($member_id, DIRECTDEBIT_DATE - 86400);
+    $sum_old_fees = fees_sum_for_member($member_id, $direct_debit->getTimestamp() - 86400);
     $sum_fees = fees_sum_for_member($member_id, time());
     $sum_new_fees = bcsub($sum_fees, $sum_old_fees);
 
@@ -530,7 +543,7 @@ function render_fees_for_member($member_id)
         $state = sprintf('Es sind noch %1$s Mitgliedsbeitrag offen, davon %2$s für die Zeit vor dem %3$s und %4$s für danach.',
             format_money(bcadd(bcsub($sum_old_fees, $sum_old_paid), bcsub($sum_new_fees, $sum_new_paid))),
             format_money(bcsub($sum_old_fees, $sum_old_paid)),
-            format_date(DIRECTDEBIT_DATE),
+            format_date($direct_debit->getTimestamp()),
             format_money(bcsub($sum_new_fees, $sum_new_paid))
         );
     } elseif ($new_open) {
@@ -538,7 +551,7 @@ function render_fees_for_member($member_id)
             format_money(bcsub($sum_new_fees, $sum_new_paid)));
     } elseif ($old_open) {
         $state = sprintf('Für die Zeit vor dem %1$s sind noch %2$s Mitgliedsbeitrag offen.',
-            format_date(DIRECTDEBIT_DATE), format_money(bcsub($sum_old_fees, $sum_old_paid)));
+            format_date($direct_debit->getTimestamp()), format_money(bcsub($sum_old_fees, $sum_old_paid)));
     }
 
     $next_debit = fee_next_directdebit_for_member($member_id);
index ec1ef79..bd8a677 100644 (file)
@@ -1,12 +1,14 @@
 <?php
 
 
+use MemberDB\Config\Config;
+
 function finance_get_total_paid_fees()
 {
-    $paid = db_select_single(sprintf('SELECT SUM(value) as paid_fees FROM %1$s WHERE type IN(\'old_fee\', \'fee\') AND status=\'paid\'',
+    $paid = db_select_single(sprintf('SELECT SUM(value) AS paid_fees FROM %1$s WHERE type IN(\'old_fee\', \'fee\') AND `status`=\'paid\'',
         DB_TABLE_EARNINGS
     ));
-    $refunded = db_select_single(sprintf('SELECT SUM(value) as refunded_fees FROM %1$s WHERE type IN(\'member_fee\') AND status=\'paid\'',
+    $refunded = db_select_single(sprintf('SELECT SUM(value) AS refunded_fees FROM %1$s WHERE type IN(\'member_fee\') AND `status`=\'paid\'',
         DB_TABLE_EXPENSES
     ));
 
@@ -19,7 +21,7 @@ function finance_get_paid_fees_for_member($member_id, $old_fees = false)
     // $old_fees: Beiträge die nicht durch Lastschrift eingezogen werden.
     //            Falls zu viel bezahlt, müssen diese auf die per Lastschrift
     //            eingezogenen Beiträge angerechnet werden.
-    $row = db_select_single(sprintf('SELECT SUM(value) as paid_fees FROM %1$s WHERE member_id=\'%2$d\' AND type=\'%3$s\' AND status=\'paid\'',
+    $row = db_select_single(sprintf('SELECT SUM(value) AS paid_fees FROM %1$s WHERE member_id=\'%2$d\' AND type=\'%3$s\' AND `status`=\'paid\'',
         DB_TABLE_EARNINGS,
         db_escape($member_id),
         $old_fees ? 'old_fee' : 'fee'
@@ -31,14 +33,16 @@ function finance_get_paid_fees_for_member($member_id, $old_fees = false)
         return $paid_fees;
     }
 
-    $invoiced_fees = fees_sum_for_member($member_id, DIRECTDEBIT_DATE - 86400);
+    /** @var DateTime $direct_debit */
+    $direct_debit = Config::getInstance()->get('direct_debit')['date']
+    $invoiced_fees = fees_sum_for_member($member_id, $direct_debit->getTimestamp() - 86400);
     $paid_invoiced_fees = finance_get_paid_fees_for_member($member_id, true);
     if (bccomp($paid_invoiced_fees, $invoiced_fees) == 1) {
         $paid_fees = bcadd($paid_fees, bcsub($paid_invoiced_fees, $invoiced_fees));
     }
 
 
-    $refunded = db_select_single(sprintf('SELECT SUM(value) as refunded_fees FROM %1$s WHERE member_id=\'%2$d\' AND type IN(\'member_fee\') AND status=\'paid\'',
+    $refunded = db_select_single(sprintf('SELECT SUM(value) AS refunded_fees FROM %1$s WHERE member_id=\'%2$d\' AND type IN(\'member_fee\') AND `status`=\'paid\'',
         DB_TABLE_EXPENSES,
         db_escape($member_id)
     ));
index 484d18b..af9d571 100644 (file)
@@ -34,7 +34,7 @@ $SQL_CREATE_MEMBERS = sprintf('
       PRIMARY KEY  (`id`),
       UNIQUE KEY `number` (`number`)
     ) ENGINE=InnoDB DEFAULT CHARSET=%2$s
-    ', DB_TABLE_MEMBERS, DB_CHARSET);
+    ', DB_TABLE_MEMBERS, 'utf8');
 
 $DIRECTDEBIT_STATES = array(
     '' => '- (Barzahler)',
@@ -366,7 +366,7 @@ function action_list_members()
     </table>
 <?php else : ?>
     <p>Bisher gibt es noch keine Mitglieder.</p>
-    <?php
+<?php
 endif;
     $count = $regular + $honorary + $patron;
     ?>
index 889a6f1..03fca91 100644 (file)
--- a/index.php
+++ b/index.php
@@ -1,5 +1,7 @@
 <?php
 
+use MemberDB\Config\Config;
+
 error_reporting(E_ALL);
 ini_set('display_errors', 1);
 
@@ -18,16 +20,13 @@ if (!file_exists(__DIR__ . '/config.php')) {
     die('Not configured');
 }
 
-require_once('config.php');
+$config = Config::getInstance();
+$config->set(require(__DIR__ . '/config.php'));
 require_once(__DIR__ . '/include/includes.php');
 
 
 db_connect();
 
-if (!defined('IS_INSTALLED') || !IS_INSTALLED) {
-    install_database();
-}
-
 $action = 'list_members';
 if (isset($_GET['action'])) {
     $action = $_GET['action'];
index 2e71118..71f5ba9 100644 (file)
@@ -7,7 +7,7 @@
       <MsgId>Message-ID</MsgId> <!-- TBD -->
       <CreDtTm>2010-11-21T09:30:47.000Z</CreDtTm>
       <NbOfTxs><!-- TBD --></NbOfTxs> 
-      <InitgPty><Nm><!-- ACCOUNT_HOLDER --></Nm></InitgPty>
+      <InitgPty><Nm><!-- bank.holder --></Nm></InitgPty>
     </GrpHdr>
     <!-- </getGroupHeaderBlock> -->
     
         <LclInstrm><Cd>CORE</Cd></LclInstrm>
        <SeqTp><!-- FRST / RCUR --></SeqTp>
       </PmtTpInf>
-      <ReqdColltnDt> <!-- DIRECTDEBIT_DAY_OF_MONTH. des Monats e.g. 2014-01-15--></ReqdColltnDt>
+      <ReqdColltnDt> <!-- direct_debit.day des Monats e.g. 2014-01-15--></ReqdColltnDt>
       <!-- </getPaymentInformationHeaderBlock> -->
       
       <!-- <getCreditorBlock> -->
-      <Cdtr><Nm><!-- ACCOUNT_HOLDER --></Nm> </Cdtr>
-      <CdtrAcct><Id><IBAN><!-- IBAN --></IBAN></Id></CdtrAcct>
-      <CdtrAgt><FinInstnId><BIC><!-- BIC --></BIC></FinInstnId></CdtrAgt><!-- USE_BIC? -->
+      <Cdtr><Nm><!-- bank.holder --></Nm> </Cdtr>
+      <CdtrAcct><Id><IBAN><!-- bank.iban --></IBAN></Id></CdtrAcct>
+      <CdtrAgt><FinInstnId><BIC><!-- bank.bic --></BIC></FinInstnId></CdtrAgt>
       <ChrgBr>SLEV</ChrgBr>
       <CdtrSchmeId><Id><PrvtId><Othr>
-        <Id><!-- CREDITOR_IDENTIFIER --></Id>
+        <Id><!-- bank.creditor_id --></Id>
         <SchmeNm><Prtry>SEPA</Prtry></SchmeNm>
       </Othr></PrvtId></Id></CdtrSchmeId>
       <!-- </getCreditorBlock> -->
diff --git a/src/Config/Config.php b/src/Config/Config.php
new file mode 100644 (file)
index 0000000..a9fcb63
--- /dev/null
@@ -0,0 +1,117 @@
+<?php
+
+namespace MemberDB\Config;
+
+class Config
+{
+    /**
+     * The config values
+     *
+     * @var array
+     */
+    protected $data = [];
+
+    /**
+     * @var self
+     */
+    protected static $instance = null;
+
+    /**
+     * @param string|null $key
+     * @param mixed       $default
+     * @return mixed
+     */
+    public function get($key, $default = null)
+    {
+        if (is_null($key)) {
+            return $this->data;
+        }
+
+        if ($this->has($key)) {
+            return $this->data[$key];
+        }
+
+        return $default;
+    }
+
+    /**
+     * @param string|array $key
+     * @param mixed        $value
+     */
+    public function set($key, $value = null)
+    {
+        if (is_array($key)) {
+            foreach ($key as $configKey => $configValue) {
+                $this->set($configKey, $configValue);
+            }
+
+            return;
+        }
+
+        $this->data[$key] = $value;
+    }
+
+    /**
+     * @param string $key
+     * @return bool
+     */
+    public function has($key)
+    {
+        return isset($this->data[$key]);
+    }
+
+    /**
+     * @param string $key
+     */
+    public function remove($key)
+    {
+        unset($this->data[$key]);
+    }
+
+    /**
+     * @param string $key
+     * @return mixed
+     */
+    public function __get($key)
+    {
+        return $this->get($key);
+    }
+
+    /**
+     * @param string $key
+     * @param mixed  $value
+     */
+    public function __set($key, $value)
+    {
+        $this->set($key, $value);
+    }
+
+    /**
+     * @param string $key
+     * @return bool
+     */
+    public function __isset($key)
+    {
+        return $this->has($key);
+    }
+
+    /**
+     * @param string $key
+     */
+    public function __unset($key)
+    {
+        $this->remove($key);
+    }
+
+    /**
+     * @return Config
+     */
+    public static function getInstance()
+    {
+        if (!self::$instance instanceof self) {
+            self::$instance = new self();
+        }
+
+        return self::$instance;
+    }
+}