<?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;

/**
 * This View is responsible for drawing the
 * screen for recovering a forgotten password
 *
 * @author Chris Pollett
 */
class RecoverView extends View
{
    /** This view is drawn on a web layout
     * @var string
     */
    public $layout = "web";
    /**
     * Draws the recover password web page and the page one get after
     * following the recover password email
     *
     * @param array $data  contains the anti CSRF token
     *     the view, data for captcha and recover dropdowns
     */
    public function renderView($data)
    {
        $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'];
        }
        $activity = (isset($data["RECOVER_COMPLETE"])) ?
            "recoverComplete" : "processRecoverData";
        ?>
        <div class="landing non-search">
        <div class="small-top">
            <h1 class="logo"><a href="<?=C\SHORT_BASE_URL ?>?<?=
                C\p('CSRF_TOKEN')."=".$data[C\p('CSRF_TOKEN')] ?>"><img
                src="<?= $logo ?>" alt="<?=
                $this->logo_alt_text ?>" ></a><span> - <?=
                tl('recover_view_recover_password') ?></span></h1>
            <form method="post">
            <input type="hidden" name="c" value="register" >
            <input type="hidden" name="a" value="<?= $activity ?>" >
            <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='time1' id='time1'
                value='<?= $_SESSION["request_time"] ?>' >
            <input type='hidden' name='level' id='level'
                value='<?= $_SESSION["level"] ?>' >
            <input type='hidden' name='time' id='time' value='<?=
                (isset($data["RECOVER_COMPLETE"])) ? $data['time']: time() ?>'/>
            <?php
                $time_done = true;
            }
            if (isset($data["RECOVER_COMPLETE"])) { ?>
                <input type="hidden" name="user" value="<?=$data['user'] ?>" >
                <?php if (empty($time_done)) { ?>
                    <input type="hidden" name="time"
                        value="<?=$data['time'] ?>" >
                <?php } ?>
                <input type="hidden" name="finish_hash" value="<?=
                    $data['finish_hash'] ?>" >
            <?php
            }
            ?>
            <div class="register">
                <table>
                    <?php
                    if (isset($data["RECOVER_COMPLETE"])) {
                        if (C\p('RECOVERY_MODE') == C\QUESTIONS_RECOVERY) {
                        ?>
                        <tr>
                            <th class="table-label"><?=
                                tl('recover_view_recovery_question')
                            ?></th>
                            <td class="table-input"><?=
                                $data['QUESTION'] ?></td>
                        </tr>
                        <tr>
                            <th class="table-label">
                                <label for="answer"><?=
                                    tl('recover_view_recovery_answer')
                                ?></label>
                            </th>
                            <td class="table-input">
                                <input id="answer" type="text"
                                    class="narrow-field" maxlength="<?=
                                    C\LONG_NAME_LEN ?>"
                                    name="answer" autocomplete="off"
                                    value="" ><?php
                                e(in_array("answer", $missing)
                                    ? '<span class="red">*</span>' : '');
                                ?></td>
                        </tr>
                        <?php
                        }
                        ?>
                        <tr>
                            <th class="table-label">
                                <label for="password"><?php
                                    e(tl('recover_view_new_password'));
                                ?></label>
                            </th>
                            <td class="table-input">
                                <input id="password" type="password"
                                    class="narrow-field" maxlength="<?=
                                C\LONG_NAME_LEN?>"
                                    name="password" value="" ><?php
                                    $this->helper(
                                        "passwordrequirements")->render(
                                        "password"); ?></td>
                        </tr>
                        <tr>
                            <th class="table-label">
                                <label for="repassword"><?=
                                     tl('recover_view_retypepassword')
                                ?></label>
                            </th>
                            <td class="table-input">
                                <input id="repassword" type="password"
                                    class="narrow-field" maxlength="<?=
                                C\LONG_NAME_LEN?>"
                                    name="repassword" value="" ></td>
                        </tr>
                        <?php
                    } else {
                    ?>
                        <tr>
                        <th class="table-label"><label for="username">
                            <?php
                            e(tl('recover_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'] ?>" >
                            <?= in_array("user", $missing)
                                ?'<span class="red">*</span>':''?></td>
                        </tr>
                    <?php
                    }
                    $border_top = "";
                    if (isset($_SERVER["COOKIE_CONSENT"]) &&
                        !$_SERVER["COOKIE_CONSENT"]) {
                        $border_top = " border-top "; ?>
                        <tr>
                            <td>&nbsp;</td>
                            <td class="table-input <?=$border_top
                                ?> narrow-field" ><?=
                                tl('recover_view_privacy_i_agree') ?>
                            <a href="<?php e(C\SHORT_BASE_URL);
                                ?>terms.php"><?= tl('recover_view_terms')
                                ?></a>
                            <?php e(tl('recover_view_and')); ?>
                            <a href="<?php e(C\SHORT_BASE_URL);
                                ?>privacy.php"><?=
                                tl('recover_view_privacy')
                                ?></a><?= tl('recover_view_period') ?>
                            </td>
                        </tr><?php
                    } ?>
                    <tr>
                        <td></td>
                        <td class="table-input">
                            <input type="hidden"
                                name="<?= C\p('CSRF_TOKEN') ?>"
                                value="<?= $data[C\p('CSRF_TOKEN')] ?>" >
                            <button  type="submit"><?=
                                tl('recover_view_recover_password')
                            ?></button>
                        </td>
                    </tr>
                </table>
            </div>
            </form>
            <div class="signin-exit">
                <ul>
                <li><a href="."><?= tl('recover_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
        }
    }
}
