<commit_before><?php

namespace phpListRestapi;

defined('PHPLISTINIT') || die;

class Subscribers
{
    /**
     * Get all the Subscribers in the system.
     * 
     * <p><strong>Parameters:</strong><br/>
     * [order_by] {string} name of column to sort, default "id".<br/>
     * [order] {string} sort order asc or desc, default: asc.<br/>
     * [limit] {integer} limit the result, default 100 (max 100)<br/>
     * [offset] {integer} offset of the result, default 0.<br/>
     * </p>
     * <p><strong>Returns:</strong><br/>
     * List of Subscribers.
     * </p>
     */
    public static function subscribersGet($order_by = 'id', $order = 'asc', $limit = 100, $offset = 0)
    {

        if (isset($_REQUEST['order_by']) && !empty($_REQUEST['order_by'])) {
            $order_by = $_REQUEST['order_by'];
        }
        if (isset($_REQUEST['order']) && !empty($_REQUEST['order'])) {
            $order = $_REQUEST['order'];
        }
        if (isset($_REQUEST['limit']) && !empty($_REQUEST['limit'])) {
            $limit = intval($_REQUEST['limit']);
        }
        if (isset($_REQUEST['offset']) && !empty($_REQUEST['offset'])) {
            $offset = intval($_REQUEST['offset']);
        }
        if ($limit > 100) {
            $limit = 100;
        }
       
        $params = array (
            'order_by' => array($order_by,PDO::PARAM_STR),
            'order' => array($order,PDO::PARAM_STR),
            'limit' => array($limit,PDO::PARAM_INT),
            'offset' => array($offset,PDO::PARAM_INT),
        );

        Common::select('Subscribers', 'SELECT * FROM '.$GLOBALS['tables']['user']." ORDER BY :order_by :order LIMIT :limit OFFSET :offset;",$params);
    }

    /**
     * Get the total of Subscribers in the system.
     * 
     * <p><strong>Parameters:</strong><br/>
     * none
     * </p>
     * <p><strong>Returns:</strong><br/>
     * Number of subscribers.
     * </p>
     */
    public static function subscribersCount()
    {
        Common::select('Subscribers', 'SELECT count(id) as total FROM '.$GLOBALS['tables']['user'],array(),true);
    }

    /**
     * Get one Subscriber by ID.
     * 
     * <p><strong>Parameters:</strong><br/>
     * [*id] {integer} the ID of the Subscriber.<br/>
     * </p>
     * <p><strong>Returns:</strong><br/>
     * One Subscriber only.
     * </p>
     */
    public static function subscriberGet($id = 0)
    {
        if ($id == 0) {
            $id = sprintf('%d',$_REQUEST['id']);
        }
        if (!is_numeric($id) || empty($id)) {
            Response::outputErrorMessage('invalid call');
        }
        
        $params = array(
            'id' => array($id,PDO::PARAM_INT),
        );
        Common::select('Subscriber', 'SELECT * FROM '.$GLOBALS['tables']['user']." WHERE id = :id;",$params, true);
    }

    /**
     * Get one Subscriber by email address.
     * 
     * <p><strong>Parameters:</strong><br/>
     * [*email] {string} the email address of the Subscriber.<br/>
     * </p>
     * <p><strong>Returns:</strong><br/>
     * One Subscriber only.
     * </p>
     */
    public static function subscriberGetByEmail($email = '')
    {
        if (empty($email)) {
            $email = $_REQUEST['email'];
        }
        $params = array(
            'email' => array($email,PDO::PARAM_STR)
        );
        Common::select('Subscriber', 'SELECT * FROM '.$GLOBALS['tables']['user']." WHERE email = :email;",$params, true);
    }

    /**
     * Get one Subscriber by foreign key.
     * 
     * <p><strong>Parameters:</strong><br/>
     * [*foreignkey] {string} the foreign key of the Subscriber.<br/>
     * </p>
     * <p><strong>Returns:</strong><br/>
     * One Subscriber only.
     * </p>
     */
    public static function subscriberGetByForeignkey($foreignkey = '')
    {
        if (empty($foreignkey)) {
            $foreignkey = $_REQUEST['foreignkey'];
        }
        $params = array(
            'foreignkey' => array($foreignkey,PDO::PARAM_STR)
        );
        Common::select('Subscriber', 'SELECT * FROM '.$GLOBALS['tables']['user']." WHERE foreignkey = :foreignkey;",$params, true);
    }

    /**
     * Add one Subscriber.
     * 
     * <p><strong>Parameters:</strong><br/>
     * [*email] {string} the email address of the Subscriber.<br/>
     * [*confirmed] {integer} 1=confirmed, 0=unconfirmed.<br/>
     * [*htmlemail] {integer} 1=html emails, 0=no html emails.<br/>
     * [*foreignkey] {string} Foreign key.<br/>
     * [*subscribepage] {integer} subscribe page to sign up to.<br/>
     * [*password] {string} The password for this Subscriber.<br/>
     * [*disabled] {integer} 1=disabled, 0=enabled<br/>
     * </p>
     * <p><strong>Returns:</strong><br/>
     * The added Subscriber.
     * </p>
     */
    public static function subscriberAdd()
    {
        $sql = 'INSERT INTO '.$GLOBALS['tables']['user'].'
          (email, confirmed, foreignkey, htmlemail, password, passwordchanged, subscribepage, disabled, entered, uniqid) 
          VALUES (:email, :confirmed, :foreignkey, :htmlemail, :password, now(), :subscribepage, :disabled, now(), :uniqid);';

        $encPwd = Common::encryptPassword($_REQUEST['password']);
        $uniqueID = Common::createUniqId();
        if (!validateEmail($_REQUEST['email'])) {
            Response::outputErrorMessage('invalid email address');
        }
        
        try {
            $db = PDO::getConnection();
            $stmt = $db->prepare($sql);
            $stmt->bindParam('email', $_REQUEST['email'], PDO::PARAM_STR);
            $stmt->bindParam('confirmed', $_REQUEST['confirmed'], PDO::PARAM_BOOL);
            $stmt->bindParam('htmlemail', $_REQUEST['htmlemail'], PDO::PARAM_BOOL);
            /* @@todo ensure uniqueness of FK */
            $stmt->bindParam('foreignkey', $_REQUEST['foreignkey'], PDO::PARAM_STR);
            $stmt->bindParam('password', $encPwd, PDO::PARAM_STR);
            $stmt->bindParam('subscribepage', $_REQUEST['subscribepage'], PDO::PARAM_INT);
            $stmt->bindParam('disabled', $_REQUEST['disabled'], PDO::PARAM_BOOL);
            $stmt->bindParam('uniqid', $uniqueID, PDO::PARAM_STR);
            $stmt->execute();
            $id = $db->lastInsertId();
            $db = null;
            self::SubscriberGet($id);
        } catch (\Exception $e) {
            Response::outputError($e);
        }
    }


    /**
     * Add / Updates multiples Subscribers.
     *
     * <p><strong>Parameters:</strong><br/>
     * [*subscribers] {json}  array of object with this properties (  email, confirmed, htmlemail, foreignkey, subscribepage, password, disabled )
     * </p>
     * <p><strong>Returns:</strong><br/>
     * Array of Subscriber Objects
     * </p>
     */
    public static function subscriberBulkAdd()
    {
        $subscribers =  json_decode( stripslashes($_REQUEST['subscribers'] ), true );
        if(!isset($_REQUEST['subscribers'])){
            return Response::outputError(new \Exception("subscribers variable not set"));
        }

        if( !is_array($subscribers ) ){
            return Response::outputError(new \Exception("subscribers variable is invalid"));
        }

        $sqlCount = 'SELECT id FROM '.$GLOBALS['tables']['user'].'
                        WHERE email = :email';

        $sqlInsert = 'INSERT INTO '.$GLOBALS['tables']['user'].'
          (email, confirmed, foreignkey, htmlemail, password, passwordchanged, subscribepage, disabled, entered, uniqid)
          VALUES (:email, :confirmed, :foreignkey, :htmlemail, :password, now(), :subscribepage, :disabled, now(), :uniqid);';

        $sqlUpdate = 'UPDATE '.$GLOBALS['tables']['user'].'
                        SET email = :email,
                            confirmed=:confirmed,
                            foreignkey=:foreignkey,
                            htmlemail=:htmlemail,
                            password= <PASSWORD>,
                            passwordchanged:=now(),
                            subscribepage=:subscribepage,
                            disabled=:disabled
                        WHERE id = :id ';
        $db = PDO::getConnection();
        try {
            $db->beginTransaction();
            $stmtCount  = $db->prepare($sqlCount);
            $stmtInsert = $db->prepare($sqlInsert);
            $stmtUpdate = $db->prepare($sqlUpdate);
            $objs = [];
            foreach ($subscribers as $subscriber) {
                if (!validateEmail($subscriber['email'])) {
                    Response::outputErrorMessage('invalid email address');
                }
            }
            foreach ($subscribers as $subscriber) {
                $stmtCount->bindParam('email', $subscriber['email'], PDO::PARAM_STR);

                $stmtCount->execute();

                $result = $stmtCount->fetchAll();
                if ( isset($result[0])) {
                    $id = $result[0]['id'];
                }else{
                    $id = null;<commit_msg>fix 5.3 compatibility (subscriberBulkAdd)<commit_after><?php

namespace phpListRestapi;

defined('PHPLISTINIT') || die;

class Subscribers
{
    /**
     * Get all the Subscribers in the system.
     * 
     * <p><strong>Parameters:</strong><br/>
     * [order_by] {string} name of column to sort, default "id".<br/>
     * [order] {string} sort order asc or desc, default: asc.<br/>
     * [limit] {integer} limit the result, default 100 (max 100)<br/>
     * [offset] {integer} offset of the result, default 0.<br/>
     * </p>
     * <p><strong>Returns:</strong><br/>
     * List of Subscribers.
     * </p>
     */
    public static function subscribersGet($order_by = 'id', $order = 'asc', $limit = 100, $offset = 0)
    {

        if (isset($_REQUEST['order_by']) && !empty($_REQUEST['order_by'])) {
            $order_by = $_REQUEST['order_by'];
        }
        if (isset($_REQUEST['order']) && !empty($_REQUEST['order'])) {
            $order = $_REQUEST['order'];
        }
        if (isset($_REQUEST['limit']) && !empty($_REQUEST['limit'])) {
            $limit = intval($_REQUEST['limit']);
        }
        if (isset($_REQUEST['offset']) && !empty($_REQUEST['offset'])) {
            $offset = intval($_REQUEST['offset']);
        }
        if ($limit > 100) {
            $limit = 100;
        }
       
        $params = array (
            'order_by' => array($order_by,PDO::PARAM_STR),
            'order' => array($order,PDO::PARAM_STR),
            'limit' => array($limit,PDO::PARAM_INT),
            'offset' => array($offset,PDO::PARAM_INT),
        );

        Common::select('Subscribers', 'SELECT * FROM '.$GLOBALS['tables']['user']." ORDER BY :order_by :order LIMIT :limit OFFSET :offset;",$params);
    }

    /**
     * Get the total of Subscribers in the system.
     * 
     * <p><strong>Parameters:</strong><br/>
     * none
     * </p>
     * <p><strong>Returns:</strong><br/>
     * Number of subscribers.
     * </p>
     */
    public static function subscribersCount()
    {
        Common::select('Subscribers', 'SELECT count(id) as total FROM '.$GLOBALS['tables']['user'],array(),true);
    }

    /**
     * Get one Subscriber by ID.
     * 
     * <p><strong>Parameters:</strong><br/>
     * [*id] {integer} the ID of the Subscriber.<br/>
     * </p>
     * <p><strong>Returns:</strong><br/>
     * One Subscriber only.
     * </p>
     */
    public static function subscriberGet($id = 0)
    {
        if ($id == 0) {
            $id = sprintf('%d',$_REQUEST['id']);
        }
        if (!is_numeric($id) || empty($id)) {
            Response::outputErrorMessage('invalid call');
        }
        
        $params = array(
            'id' => array($id,PDO::PARAM_INT),
        );
        Common::select('Subscriber', 'SELECT * FROM '.$GLOBALS['tables']['user']." WHERE id = :id;",$params, true);
    }

    /**
     * Get one Subscriber by email address.
     * 
     * <p><strong>Parameters:</strong><br/>
     * [*email] {string} the email address of the Subscriber.<br/>
     * </p>
     * <p><strong>Returns:</strong><br/>
     * One Subscriber only.
     * </p>
     */
    public static function subscriberGetByEmail($email = '')
    {
        if (empty($email)) {
            $email = $_REQUEST['email'];
        }
        $params = array(
            'email' => array($email,PDO::PARAM_STR)
        );
        Common::select('Subscriber', 'SELECT * FROM '.$GLOBALS['tables']['user']." WHERE email = :email;",$params, true);
    }

    /**
     * Get one Subscriber by foreign key.
     * 
     * <p><strong>Parameters:</strong><br/>
     * [*foreignkey] {string} the foreign key of the Subscriber.<br/>
     * </p>
     * <p><strong>Returns:</strong><br/>
     * One Subscriber only.
     * </p>
     */
    public static function subscriberGetByForeignkey($foreignkey = '')
    {
        if (empty($foreignkey)) {
            $foreignkey = $_REQUEST['foreignkey'];
        }
        $params = array(
            'foreignkey' => array($foreignkey,PDO::PARAM_STR)
        );
        Common::select('Subscriber', 'SELECT * FROM '.$GLOBALS['tables']['user']." WHERE foreignkey = :foreignkey;",$params, true);
    }

    /**
     * Add one Subscriber.
     * 
     * <p><strong>Parameters:</strong><br/>
     * [*email] {string} the email address of the Subscriber.<br/>
     * [*confirmed] {integer} 1=confirmed, 0=unconfirmed.<br/>
     * [*htmlemail] {integer} 1=html emails, 0=no html emails.<br/>
     * [*foreignkey] {string} Foreign key.<br/>
     * [*subscribepage] {integer} subscribe page to sign up to.<br/>
     * [*password] {string} The password for this Subscriber.<br/>
     * [*disabled] {integer} 1=disabled, 0=enabled<br/>
     * </p>
     * <p><strong>Returns:</strong><br/>
     * The added Subscriber.
     * </p>
     */
    public static function subscriberAdd()
    {
        $sql = 'INSERT INTO '.$GLOBALS['tables']['user'].'
          (email, confirmed, foreignkey, htmlemail, password, <PASSWORD>, subscribepage, disabled, entered, uniqid) 
          VALUES (:email, :confirmed, :foreignkey, :htmlemail, :password, now(), :subscribepage, :disabled, now(), :uniqid);';

        $encPwd = Common::encryptPassword($_REQUEST['password']);
        $uniqueID = Common::createUniqId();
        if (!validateEmail($_REQUEST['email'])) {
            Response::outputErrorMessage('invalid email address');
        }
        
        try {
            $db = PDO::getConnection();
            $stmt = $db->prepare($sql);
            $stmt->bindParam('email', $_REQUEST['email'], PDO::PARAM_STR);
            $stmt->bindParam('confirmed', $_REQUEST['confirmed'], PDO::PARAM_BOOL);
            $stmt->bindParam('htmlemail', $_REQUEST['htmlemail'], PDO::PARAM_BOOL);
            /* @@todo ensure uniqueness of FK */
            $stmt->bindParam('foreignkey', $_REQUEST['foreignkey'], PDO::PARAM_STR);
            $stmt->bindParam('password', $enc<PASSWORD>, PDO::PARAM_STR);
            $stmt->bindParam('subscribepage', $_REQUEST['subscribepage'], PDO::PARAM_INT);
            $stmt->bindParam('disabled', $_REQUEST['disabled'], PDO::PARAM_BOOL);
            $stmt->bindParam('uniqid', $uniqueID, PDO::PARAM_STR);
            $stmt->execute();
            $id = $db->lastInsertId();
            $db = null;
            self::SubscriberGet($id);
        } catch (\Exception $e) {
            Response::outputError($e);
        }
    }


    /**
     * Add / Updates multiples Subscribers.
     *
     * <p><strong>Parameters:</strong><br/>
     * [*subscribers] {json}  array of object with this properties (  email, confirmed, htmlemail, foreignkey, subscribepage, password, disabled )
     * </p>
     * <p><strong>Returns:</strong><br/>
     * Array of Subscriber Objects
     * </p>
     */
    public static function subscriberBulkAdd()
    {
        $subscribers =  json_decode( stripslashes($_REQUEST['subscribers'] ), true );
        if(!isset($_REQUEST['subscribers'])){
            return Response::outputError(new \Exception("subscribers variable not set"));
        }

        if( !is_array($subscribers ) ){
            return Response::outputError(new \Exception("subscribers variable is invalid"));
        }

        $sqlCount = 'SELECT id FROM '.$GLOBALS['tables']['user'].'
                        WHERE email = :email';

        $sqlInsert = 'INSERT INTO '.$GLOBALS['tables']['user'].'
          (email, confirmed, foreignkey, htmlemail, password, passwordchanged, subscribepage, disabled, entered, uniqid)
          VALUES (:email, :confirmed, :foreignkey, :htmlemail, :password, now(), :subscribepage, :disabled, now(), :uniqid);';

        $sqlUpdate = 'UPDATE '.$GLOBALS['tables']['user'].'
                        SET email = :email,
                            confirmed=:confirmed,
                            foreignkey=:foreignkey,
                            htmlemail=:htmlemail,
                            password= :password,
                            passwordchanged:=now(),
                            subscribepage=:subscribepage,
                            disabled=:disabled
                        WHERE id = :id ';
        $db = PDO::getConnection();
        try {
            $db->beginTransaction();
            $stmtCount  = $db->prepare($sqlCount);
            $stmtInsert = $db->prepare($sqlInsert);
            $stmtUpdate = $db->prepare($sqlUpdate);
            $objs = array(); # <5.3 compatibility
            foreach ($subscribers as $subscriber) {
                if (!validateEmail($subscriber['email'])) {
                    Response::outputErrorMessage('invalid email address');
                }
            }
            foreach ($subscribers as $subscriber) {
                $stmtCount->bindParam('email', $subscriber['email'], PDO::PARAM_STR);

                $stmtCount->execute();

                $result = $stmtCount->fetchAll();
                if ( isset($result[0])) {
                    $id = $result[0]['id'];
                }else{
                    $id = null;
{'fraction_non_alphanumeric': 0.17308227953874436, 'fraction_numerical': 0.0032310177705977385, 'mean_word_length': 2.206286836935167, 'pattern_counts': {'":': 0, '<': 177, '<?xml version=': 0, '>': 226, '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': '399137', 'n_tokens_mistral': 5383, 'n_tokens_neox': 5129, 'n_words': 1085}