<commit_before><?php

/**
 * AuditTrail
 *
 * PHP version 5
 *
 * @category  Library
 * @package   PyritePHP
 * @author    <NAME> <<EMAIL>>
 * @copyright 2016 <NAME>
 * @license   https://opensource.org/licenses/MIT  MIT
 * @link      https://github.com/vphantom/pyrite-php
 */

namespace Pyrite;

/**
 * AuditTrail class
 *
 * @category  Library
 * @package   PyritePHP
 * @author    <NAME> <<EMAIL>>
 * @copyright 2016 <NAME>
 * @license   https://opensource.org/licenses/MIT  MIT
 * @link      https://github.com/vphantom/pyrite-php
 */
class AuditTrail
{
    /**
     * Bootstrap: define event handlers
     *
     * @return null
     */
    public static function bootstrap()
    {
        on('install',   'Pyrite\AuditTrail::install');
        on('log',       'Pyrite\AuditTrail::add');
        on('history',   'Pyrite\AuditTrail::get');
        on('user_seen', 'Pyrite\AuditTrail::getLastLogin');
    }

    /**
     * Create database tables if necessary
     *
     * @return null
     */
    public static function install()
    {
        global $PPHP;
        $db = $PPHP['db'];
        echo "    Installing log... ";

        $db->begin();
        $db->exec(
            "
            CREATE TABLE IF NOT EXISTS 'transactions' (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
                userId INTEGER NOT NULL DEFAULT '0',
                ip VARCHAR(16) NOT NULL DEFAULT '127.0.0.1',
                objectType VARCHAR(64) DEFAULT NULL,
                objectId INTEGER DEFAULT NULL,
                action VARCHAR(64) NOT NULL DEFAULT '',
                fieldName VARCHAR(64) DEFAULT NULL,
                oldValue VARCHAR(255) DEFAULT NULL,
                newValue VARCHAR(255) DEFAULT NULL,
                content TEXT NOT NULL DEFAULT ''
            )
            "
        );
        $db->commit();
        self::add(null, null, 'installed');
        echo "    done!\n";
    }

    /**
     * Add a new transaction to the audit trail
     *
     * Suggested minimum set of actions:
     *
     *     created
     *     modified
     *     deleted
     *
     * You can either use these positional arguments or specify a single
     * associative array argument with only the keys you need defined.
     *
     * At least an action should be specified (i.e. 'rebooted', perhaps) and
     * typically also objectType and objectId.  The rest is accessory.
     *
     * @param array|string    $objectType Class of object this applies to (*or args, see above)
     * @param string|int|null $objectId   Specific instance acted upon
     * @param string          $action     Type of action performed
     * @param string|null     $fieldName  Specific field affected
     * @param string|int|null $oldValue   Previous value for affected field
     * @param string|int|null $newValue   New value for affected field
     * @param string|int|null $userId     Over-ride session userId with this one
     * @param string|null     $content    Additional text to store in the entry
     *
     * @return null
     */
    public static function add($objectType, $objectId = null, $action = null, $fieldName = null, $oldValue = null, $newValue = null, $userId = 0, $content = '')
    {
        global $PPHP;
        $db = $PPHP['db'];

        $ip = '127.0.0.1';
        $req = grab('request');
        if (isset($req['remote_addr'])) {
            $ip = $req['remote_addr'];
        };

        // First argument could contain named arguments
        if (is_array($objectType)) {
            if (isset($objectType['objectId']))  $objectId  = $objectType['objectId'];
            if (isset($objectType['action']))    $action    = $objectType['action'];
            if (isset($objectType['fieldName'])) $fieldName = $objectType['fieldName'];
            if (isset($objectType['oldValue']))  $oldValue  = $objectType['oldValue'];
            if (isset($objectType['newValue']))  $newValue  = $objectType['newValue'];
            if (isset($objectType['userId']))    $userId    = $objectType['userId'];
            if (isset($objectType['content']))   $content   = $objectType['content'];
            if (isset($objectType['objectType'])) {
                $objectType = $objectType['objectType'];
            } else {
                $objectType = null;
            };
        };

        if ($userId === 0  &&  isset($_SESSION['user']['id'])) {
            $userId = $_SESSION['user']['id'];
        };

        $db->exec(
            "
            INSERT INTO transactions
            (userId, ip, objectType, objectId, action, fieldName, oldValue, newValue, content)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
            ",<commit_msg>Enforce size limit on transaction fields<commit_after><?php

/**
 * AuditTrail
 *
 * PHP version 5
 *
 * @category  Library
 * @package   PyritePHP
 * @author    <NAME> <<EMAIL>>
 * @copyright 2016 <NAME>
 * @license   https://opensource.org/licenses/MIT  MIT
 * @link      https://github.com/vphantom/pyrite-php
 */

namespace Pyrite;

/**
 * AuditTrail class
 *
 * @category  Library
 * @package   PyritePHP
 * @author    <NAME> <<EMAIL>>
 * @copyright 2016 <NAME>
 * @license   https://opensource.org/licenses/MIT  MIT
 * @link      https://github.com/vphantom/pyrite-php
 */
class AuditTrail
{
    /**
     * Bootstrap: define event handlers
     *
     * @return null
     */
    public static function bootstrap()
    {
        on('install',   'Pyrite\AuditTrail::install');
        on('log',       'Pyrite\AuditTrail::add');
        on('history',   'Pyrite\AuditTrail::get');
        on('user_seen', 'Pyrite\AuditTrail::getLastLogin');
    }

    /**
     * Create database tables if necessary
     *
     * @return null
     */
    public static function install()
    {
        global $PPHP;
        $db = $PPHP['db'];
        echo "    Installing log... ";

        $db->begin();
        $db->exec(
            "
            CREATE TABLE IF NOT EXISTS 'transactions' (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
                userId INTEGER NOT NULL DEFAULT '0',
                ip VARCHAR(16) NOT NULL DEFAULT '127.0.0.1',
                objectType VARCHAR(64) DEFAULT NULL,
                objectId INTEGER DEFAULT NULL,
                action VARCHAR(64) NOT NULL DEFAULT '',
                fieldName VARCHAR(64) DEFAULT NULL,
                oldValue VARCHAR(255) DEFAULT NULL,
                newValue VARCHAR(255) DEFAULT NULL,
                content TEXT NOT NULL DEFAULT ''
            )
            "
        );
        $db->commit();
        self::add(null, null, 'installed');
        echo "    done!\n";
    }

    /**
     * Add a new transaction to the audit trail
     *
     * Suggested minimum set of actions:
     *
     *     created
     *     modified
     *     deleted
     *
     * You can either use these positional arguments or specify a single
     * associative array argument with only the keys you need defined.
     *
     * At least an action should be specified (i.e. 'rebooted', perhaps) and
     * typically also objectType and objectId.  The rest is accessory.
     *
     * @param array|string    $objectType Class of object this applies to (*or args, see above)
     * @param string|int|null $objectId   Specific instance acted upon
     * @param string          $action     Type of action performed
     * @param string|null     $fieldName  Specific field affected
     * @param string|int|null $oldValue   Previous value for affected field
     * @param string|int|null $newValue   New value for affected field
     * @param string|int|null $userId     Over-ride session userId with this one
     * @param string|null     $content    Additional text to store in the entry
     *
     * @return null
     */
    public static function add($objectType, $objectId = null, $action = null, $fieldName = null, $oldValue = null, $newValue = null, $userId = 0, $content = '')
    {
        global $PPHP;
        $db = $PPHP['db'];

        $ip = '127.0.0.1';
        $req = grab('request');
        if (isset($req['remote_addr'])) {
            $ip = $req['remote_addr'];
        };

        // First argument could contain named arguments
        if (is_array($objectType)) {
            if (isset($objectType['objectId']))  $objectId  = $objectType['objectId'];
            if (isset($objectType['action']))    $action    = $objectType['action'];
            if (isset($objectType['fieldName'])) $fieldName = $objectType['fieldName'];
            if (isset($objectType['oldValue']))  $oldValue  = $objectType['oldValue'];
            if (isset($objectType['newValue']))  $newValue  = $objectType['newValue'];
            if (isset($objectType['userId']))    $userId    = $objectType['userId'];
            if (isset($objectType['content']))   $content   = $objectType['content'];
            if (isset($objectType['objectType'])) {
                $objectType = $objectType['objectType'];
            } else {
                $objectType = null;
            };
        };

        if ($userId === 0  &&  isset($_SESSION['user']['id'])) {
            $userId = $_SESSION['user']['id'];
        };

        // Enforce size limit on field values
        $oldValue = substr($oldValue, 0, 250);
        $newValue = substr($newValue, 0, 250);

        $db->exec(
            "
            INSERT INTO transactions
            (userId, ip, objectType, objectId, action, fieldName, oldValue, newValue, content)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
            ",
{'fraction_non_alphanumeric': 0.12985663827134844, 'fraction_numerical': 0.008726366091834614, 'mean_word_length': 2.038825757575758, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 27, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3796740', 'n_tokens_mistral': 2808, 'n_tokens_neox': 2672, 'n_words': 745}