<?php
/**
 * SeekQuarry/Yioop --
 * Open Source Pure PHP Search Engine, Crawler, and Indexer
 *
 * Copyright (C) 2009 - 2026  Chris Pollett chris@pollett.org
 *
 * LICENSE:
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * END LICENSE
 *
 * @author Chris Pollett
 * @license https://www.gnu.org/licenses/ GPL3
 * @link https://www.seekquarry.com/
 * @copyright 2009 - 2026
 * @filesource
 */
namespace seekquarry\yioop\views;

use seekquarry\yioop\configs as C;

/**
 * Draws the page that allows a user to register for an account
 *
 * @author Mallika Perepa (creator), Chris Pollett, Akash Patel
 */
class RegisterView extends View
{
    /** This view is drawn on a web layout
     * @var string
     */
    public $layout = "web";
    /**
     * An array of triples, each triple consisting of a question of the form
     * Which is the most..? followed by one of the form Which is the least ..?
     * followed by a string which is a comma separated list of possibilities
     * arranged from least to most. The values for these triples are determined
     * via the translate function tl. So can be set under Manage Locales
     * by editing their values for the desired locale. You can also
     * change them in the Security element
     * @var array
     */
    public $captchas_qa;
    /**
     * Besides calling the constructor for the base class this constructor
     * sets up the captchas_qa array so it can be localized. The reason for
     * putting this array in a view is so that multiple controllers and
     * components can see and manipulate it.
     * @param object $controller_object that is using this view
     */
    public function __construct($controller_object = null)
    {
        parent::__construct($controller_object);
    }
    /**
     * Draws the create account web page.
     *
     * @param array $data  contains the anti CSRF token
     *     the view, data for captcha and recover dropdowns
     */
    public function renderView($data)
    {
        $logged_in = (isset($data['ADMIN']) && $data['ADMIN']);
        $append_url = ($logged_in && isset($data[C\p('CSRF_TOKEN')]))
                ? C\p('CSRF_TOKEN') . "=" . $data[C\p('CSRF_TOKEN')] : "";
        $logo_field = "LOGO_LARGE";
        $logo = C\SHORT_BASE_URL . C\p('LOGO_LARGE');
        if ($_SERVER["MOBILE"]) {
            $logo_field = "LOGO_SMALL";
            $logo = C\SHORT_BASE_URL . C\p('LOGO_SMALL');
        }
        $logo = $this->helper("logo")->render($data,
            $logo_field, $logo);
        $missing = [];
        if (isset($data['MISSING'])) {
            $missing = $data['MISSING'];
        }
        ?>
        <div class="landing non-search">
        <div class="small-top">
            <h1 class="logo"><a href="<?=C\SHORT_BASE_URL ?>?<?=
                $append_url ?>"><img src="<?= $logo ?>"
                alt="<?= $this->logo_alt_text ?>" ></a><span> - <?=
                tl('register_view_create_account') ?></span></h1>
            <form method="post">
            <input type="hidden" name="c" value="register" >
            <input type="hidden" name="a" value="processAccountData" >
            <input type="hidden" name="cookieconsent" value="true" >
            <?php
            if (isset($_SESSION["random_string"])) {
            ?>
                <input type='hidden' name='nonce_for_string'
                    id='nonce_for_string' >
                <input type='hidden' name='random_string' id='random_string'
                    value='<?= $_SESSION["random_string"] ?>' >
                <input type='hidden' name='time' id='time'
                    value='<?= $_SESSION["request_time"] ?>' >
                <input type='hidden' name='level' id='level'
                    value='<?= $_SESSION["level"] ?>' >
                <?php
            } ?>
            <div class="register">
                <table>
                    <tr>
                        <th class="table-label">
                            <label for="firstname"><?=
                                tl('register_view_firstname')
                            ?></label>
                        </th>
                        <td class="table-input">
                            <input id="firstname" type="text"
                                class="narrow-field" maxlength="<?=
                                C\NAME_LEN ?>"
                                name="first" autocomplete="off"
                                value = "<?= $data['FIRST'] ?>" ><span
                                class="required-star none"
                                id="firstname-star">*</span></td>
                    </tr>
                    <tr>
                        <th class="table-label">
                            <label for="lastname"><?=
                                tl('register_view_lastname')
                            ?></label>
                        </th>
                        <td class="table-input">
                            <input id="lastname" type="text"
                                class="narrow-field" maxlength="<?=
                                C\NAME_LEN ?>"
                                name="last" autocomplete="off"
                                value = "<?= $data['LAST']  ?>" ><span
                                class="required-star none"
                                id="lastname-star">*</span></td>
                    </tr>
                    <tr>
                        <th class="table-label"><label for="username">
                            <?= tl('register_view_username')
                            ?></label>
                        </th>
                        <td class="table-input">
                            <input id="username" type="text"
                                class="narrow-field" maxlength="<?=
                                C\NAME_LEN ?>"
                                name="user" autocomplete="off"
                                value = "<?= $data['USER'] ?>" ><span
                                class="required-star none"
                                id="username-star">*</span></td>
                    </tr>
                    <tr>
                        <th class="table-label"><label for="email"><?=
                            tl('register_view_email')
                            ?></label>
                        </th>
                        <td class="table-input">
                            <input id="email" type="text"
                                class="narrow-field" maxlength="<?=
                                C\LONG_NAME_LEN ?>"
                                name="email" autocomplete="off"
                                value = "<?= $data['EMAIL'] ?>" ><span
                                class="required-star none"
                                id="email-star">*</span></td>
                    </tr>
                    <tr>
                        <th class="table-label">
                            <label for="pass-word"><?=
                            tl('register_view_password')
                            ?></label>
                        </th>
                        <td class="table-input">
                            <input id="pass-word" type="password"
                                class="narrow-field" maxlength="<?=
                                C\LONG_NAME_LEN?>"
                                name="password" value="<?=
                                $data['PASSWORD'] ?>" ><span
                                class="required-star none"
                                id="pass-word-star">*</span><?php
                            $this->helper("passwordrequirements")->render(
                                "pass-word", false); ?></td>
                    </tr>
                    <tr>
                        <th class="table-label">
                            <label for="retype-password"><?=
                                 tl('register_view_retypepassword')
                            ?></label>
                        </th>
                        <td class="table-input">
                            <input id="retype-password" type="password"
                                class="narrow-field" maxlength="<?=
                                C\LONG_NAME_LEN ?>"
                                name="repassword" value="<?=
                                $data['REPASSWORD'] ?>" ><span
                                class="required-star none"
                                id="retype-password-star">*</span>
                            <span id="pass-match"></span></td>
                    </tr>
                    <?php
                    if (C\p('RECOVERY_MODE') == C\QUESTIONS_RECOVERY) {
                        $recovery_question =
                            $data['RECOVERY_QUESTION'] ?? "";
                        $recovery_answer = $data['RECOVERY_ANSWER'] ?? "";
                        ?>
                        <tr>
                            <th class="table-label"><label
                                for="recovery-question"><?=
                                tl('register_view_recovery_question')
                                ?></label></th>
                            <td class="table-input border-top">
                                <input id="recovery-question" type="text"
                                    class="narrow-field" maxlength="<?=
                                    C\RECOVERY_QUESTION_LEN ?>"
                                    name="recovery_question" value="<?=
                                    $recovery_question ?>" ><?php
                                e(in_array("recovery_question", $missing)
                                    ? '<span class="red">*</span>' : '');
                                ?></td>
                        </tr>
                        <tr>
                            <th class="table-label"><label
                                for="recovery-answer"><?=
                                tl('register_view_recovery_answer')
                                ?></label></th>
                            <td class="table-input">
                                <input id="recovery-answer" type="text"
                                    class="narrow-field" maxlength="<?=
                                    C\LONG_NAME_LEN ?>"
                                    name="recovery_answer" value="<?=
                                    $recovery_answer ?>" ><?php
                                e(in_array("recovery_answer", $missing)
                                    ? '<span class="red">*</span>' : '');
                                ?></td>
                        </tr>
                        <?php
                    }
                    ?>
                    <tr>
                        <td></td>
                        <td class="table-input border-top narrow-field" ><?=
                            tl('register_view_privacy_i_agree') ?>
                        <a href="<?php e(C\SHORT_BASE_URL);
                            ?>terms.php"><?= tl('register_view_terms')
                            ?></a>
                        <?php e(tl('register_view_and')); ?>
                        <a href="<?php e(C\SHORT_BASE_URL);
                            ?>privacy.php"><?= tl('register_view_privacy')
                            ?></a><?= tl('register_view_period') ?>
                        </td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td class="table-input border-top">
                        <input type="hidden"
                            name="<?php e(C\p('CSRF_TOKEN'));?>"
                            value="<?php e($data[C\p('CSRF_TOKEN')]); ?>" >
                            <button id="submit" type="submit"><?=
                            tl('register_view_create_account')
                            ?></button>
                        </td>
                    </tr>
                </table>
            </div>
            </form>
            <div class="signin-exit">
                <ul>
                <li><a href="."><?= tl('register_view_return') ?></a></li>
                </ul>
            </div>
        </div>
        </div>
        <div class='tall-landing-spacer'></div>
        <?php
        if (isset($_SESSION["random_string"])) { ?>
            <script>
                document.addEventListener('DOMContentLoaded', function() {
                var body = tag(body);
                body.onload = findNonce('nonce_for_string', 'random_string'
                    , 'time', 'level');
                }, false);
            </script>
            <?php
        }
      }
    }
    ?>
