<commit_before><?php
/**
 * @title            Security Model Class
 *
 * @author           <NAME> <<EMAIL>>
 * @copyright        (c) 2012-2018, <NAME>. All Rights Reserved.
 * @license          GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.
 * @package          PH7 / Framework / Mvc / Model
 * @version          1.3
 */

namespace PH7\Framework\Mvc\Model;

defined('PH7') or exit('Restricted access');

use DateInterval;
use DateTime;
use PDO;
use PH7\DbTableName;
use PH7\Framework\Date\CDateTime;
use PH7\Framework\Ip\Ip;
use PH7\Framework\Layout\Tpl\Engine\PH7Tpl\PH7Tpl;
use PH7\Framework\Mvc\Model\Engine\Db;
use PH7\Framework\Mvc\Model\Engine\Util\Various;
use PH7\Security as SecurityCore;

class Security
{
    /** @var string */
    private $sIp;

    /** @var string */
    private $sCurrentTime;

    public function __construct()
    {
        $this->sIp = Ip::get();
        $this->sCurrentTime = (new CDateTime)->get()->dateTime('Y-m-d H:i:s');
    }

    /**
     * Block user IP
     *
     * @param string $sIp IP address.
     * @param int $iExpir Expiration in seconds. Default 86400
     *
     * @return bool Returns TRUE if no IP has been found (and the new IP has been added to the block list), otherwise FALSE.
     */
    public function blockIp($sIp, $iExpir = 86400)
    {
        $iExpir = time() + (int)$iExpir;
        $rStmt = Db::getInstance()->prepare('SELECT ip FROM' . Db::prefix(DbTableName::BLOCK_IP) . 'WHERE ip = :ip LIMIT 1');
        $rStmt->bindValue(':ip', $sIp, PDO::PARAM_STR);
        $rStmt->execute();

        // If IP is not found
        if ($rStmt->rowCount() == 0) {
            $rStmt = Db::getInstance()->prepare('INSERT INTO' . Db::prefix(DbTableName::BLOCK_IP) . 'VALUES (:ip, :expiration)');
            $rStmt->bindValue(':ip', $sIp, PDO::PARAM_STR);
            $rStmt->bindValue(':expiration', $iExpir, PDO::PARAM_INT);
            $rStmt->execute();
            return true;
        }

        return false;
    }

    /**
     * Add Login Log.
     *
     * @param string $sEmail
     * @param string $sUsername
     * @param string $sPassword
     * @param int $sStatus
     * @param string $sTable Default DbTableName::MEMBER
     *
     * @return void
     */
    public function addLoginLog($sEmail, $sUsername, $sPassword, $sStatus, $sTable = DbTableName::MEMBER)
    {
        Various::checkModelTable($sTable);

        $rStmt = Db::getInstance()->prepare('INSERT INTO' . Db::prefix($sTable . 'LogLogin') . '(email, username, password, status, ip)
        VALUES (:email, :username, :password, :status, :ip)');
        $rStmt->bindValue(':email', $sEmail, PDO::PARAM_STR);
        $rStmt->bindValue(':username', $sUsername, PDO::PARAM_STR);
        $rStmt->bindValue(':password', $sPassword, PDO::PARAM_STR);
        $rStmt->bindValue(':status', $sStatus, PDO::PARAM_STR);
        $rStmt->bindValue(':ip', $this->sIp, PDO::PARAM_STR);
        $rStmt->execute();
        Db::free($rStmt);
    }

    /**
     * Blocking access to the login page after exceeded login attempts.
     *
     * @param int $iMaxAttempts
     * @param int $iAttemptTime
     * @param string $sEmail Email address of member.
     * @param PH7Tpl $oView
     * @param string $sTable Default DbTableName::MEMBER
     *
     * @return bool Returns TRUE if attempts are allowed, FALSE otherwise.<commit_msg>Fix wrong LogLogin table name suffix<commit_after><?php
/**
 * @title            Security Model Class
 *
 * @author           <NAME> <<EMAIL>>
 * @copyright        (c) 2012-2018, <NAME>. All Rights Reserved.
 * @license          GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.
 * @package          PH7 / Framework / Mvc / Model
 * @version          1.3
 */

namespace PH7\Framework\Mvc\Model;

defined('PH7') or exit('Restricted access');

use DateInterval;
use DateTime;
use PDO;
use PH7\DbTableName;
use PH7\Framework\Date\CDateTime;
use PH7\Framework\Ip\Ip;
use PH7\Framework\Layout\Tpl\Engine\PH7Tpl\PH7Tpl;
use PH7\Framework\Mvc\Model\Engine\Db;
use PH7\Framework\Mvc\Model\Engine\Util\Various;
use PH7\Security as SecurityCore;

class Security
{
    /** @var string */
    private $sIp;

    /** @var string */
    private $sCurrentTime;

    public function __construct()
    {
        $this->sIp = Ip::get();
        $this->sCurrentTime = (new CDateTime)->get()->dateTime('Y-m-d H:i:s');
    }

    /**
     * Block user IP
     *
     * @param string $sIp IP address.
     * @param int $iExpir Expiration in seconds. Default 86400
     *
     * @return bool Returns TRUE if no IP has been found (and the new IP has been added to the block list), otherwise FALSE.
     */
    public function blockIp($sIp, $iExpir = 86400)
    {
        $iExpir = time() + (int)$iExpir;
        $rStmt = Db::getInstance()->prepare('SELECT ip FROM' . Db::prefix(DbTableName::BLOCK_IP) . 'WHERE ip = :ip LIMIT 1');
        $rStmt->bindValue(':ip', $sIp, PDO::PARAM_STR);
        $rStmt->execute();

        // If IP is not found
        if ($rStmt->rowCount() == 0) {
            $rStmt = Db::getInstance()->prepare('INSERT INTO' . Db::prefix(DbTableName::BLOCK_IP) . 'VALUES (:ip, :expiration)');
            $rStmt->bindValue(':ip', $sIp, PDO::PARAM_STR);
            $rStmt->bindValue(':expiration', $iExpir, PDO::PARAM_INT);
            $rStmt->execute();
            return true;
        }

        return false;
    }

    /**
     * Add Login Log.
     *
     * @param string $sEmail
     * @param string $sUsername
     * @param string $sPassword
     * @param int $sStatus
     * @param string $sTable Default DbTableName::MEMBER
     *
     * @return void
     */
    public function addLoginLog($sEmail, $sUsername, $sPassword, $sStatus, $sTable = DbTableName::MEMBER)
    {
        Various::checkModelTable($sTable);

        $rStmt = Db::getInstance()->prepare('INSERT INTO' . Db::prefix($sTable . 'log_login') . '(email, username, password, status, ip)
        VALUES (:email, :username, :password, :status, :ip)');
        $rStmt->bindValue(':email', $sEmail, PDO::PARAM_STR);
        $rStmt->bindValue(':username', $sUsername, PDO::PARAM_STR);
        $rStmt->bindValue(':password', $sPassword, PDO::PARAM_STR);
        $rStmt->bindValue(':status', $sStatus, PDO::PARAM_STR);
        $rStmt->bindValue(':ip', $this->sIp, PDO::PARAM_STR);
        $rStmt->execute();
        Db::free($rStmt);
    }

    /**
     * Blocking access to the login page after exceeded login attempts.
     *
     * @param int $iMaxAttempts
     * @param int $iAttemptTime
     * @param string $sEmail Email address of member.
     * @param PH7Tpl $oView
     * @param string $sTable Default DbTableName::MEMBER
     *
     * @return bool Returns TRUE if attempts are allowed, FALSE otherwise.
{'fraction_non_alphanumeric': 0.15027883768711475, 'fraction_numerical': 0.010859994129732903, 'mean_word_length': 2.6758360302049624, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '3100700', 'n_tokens_mistral': 2265, 'n_tokens_neox': 2218, 'n_words': 552}