<?php
// setup page
define("PAGE_NAME", t("report_abuse_page_name", "Report Abuse"));
define("PAGE_DESCRIPTION", t("report_abuse_meta_description", "Report Abuse or Copyright Infringement"));
define("PAGE_KEYWORDS", t("report_abuse_meta_keywords", "report, abuse, copyright, infringement, file, hosting"));
define("TITLE_DESCRIPTION_LEFT", t("report_abuse_title_page_description_left_1", "Please use the following form to report any copyright infringements ensuring you supply all the following information"));
define("TITLE_DESCRIPTION_RIGHT", t("report_abuse_title_page_description_right", ""));

// success handling
if (isset($_REQUEST['s']))
{
    notification::setSuccess(t('report_file_success', 'Thanks for submitting the information needed to report a file on our site. We\'ll review the file as soon as possible and remove if required.'));
}
// prepare variables
$file_url = '';
$other_information = '';
$reported_by_name = '';
$reported_by_email = '';
$reported_by_address = '';
$reported_by_telephone_number = '';
$digital_signature = '';
$confirm_1 = '';
$confirm_2 = '';

// send report if submitted
if ((int) $_REQUEST['submitme'])
{
    $file_url = trim($_REQUEST['file_url']);
    $other_information = trim($_REQUEST['other_information']);
    $reported_by_name = trim($_REQUEST['reported_by_name']);
    $reported_by_email = strtolower(trim($_REQUEST['reported_by_email']));
    $reported_by_address = trim($_REQUEST['reported_by_address']);
    $reported_by_telephone_number = trim($_REQUEST['reported_by_telephone_number']);
    $digital_signature = trim($_REQUEST['digital_signature']);
    $confirm_1 = trim($_REQUEST['confirm_1']);
    $confirm_2 = trim($_REQUEST['confirm_2']);

    if (strlen($file_url) == 0)
    {
        notification::setError(t("report_abuse_error_no_url", "Please enter the url of the file you're reporting."));
    }
    elseif (strlen($other_information) == 0)
    {
        notification::setError(t("report_abuse_error_description", "Please enter the description and support information of the reported file."));
    }
    elseif (strlen($reported_by_name) == 0)
    {
        notification::setError(t("report_abuse_error_name", "Please enter your name."));
    }
    elseif (strlen($reported_by_email) == 0)
    {
        notification::setError(t("report_abuse_error_email", "Please enter your email."));
    }
    elseif (strlen($digital_signature) == 0)
    {
        notification::setError(t("report_abuse_error_signature", "Please provide the electronic signature of yourself or the copyright owner."));
    }
    elseif ($confirm_1 != 'yes')
    {
        notification::setError(t("report_abuse_error_confirm_1", "Please confirm you have a good faith belief that use of the material in the manner complained of is not authorized by the copyright owner, its agent, or the law."));
    }
    elseif ($confirm_2 != 'yes')
    {
        notification::setError(t("report_abuse_error_confirm_2", "Please confirm the information in the notification is accurate, and, under the pains and penalties of perjury, that you are authorized to act on behalf of the copyright owner."));
    }

    // check file url is active and exists
    if (notification::isErrors() == false)
    {
        // break apart url
        $file = file::loadByFullUrl($file_url);
        if (!$file)
        {
            notification::setError(t("report_abuse_error_could_not_find_file", "Could not find a file with that url, please check and try again."));
        }
        else
        {
            // make sure the file is active
            if ($file->statusId != 1)
            {
                notification::setError(t("report_abuse_error_file_not_active", "The file url you've set is not active."));
            }
        }
    }

    // add to database and send email to admin
    if (notification::isErrors() == false)
    {
        // add to database
        $dbInsert = new DBObject("file_report", array("file_id", "report_date", "reported_by_name",
            "reported_by_email", "reported_by_address", "reported_by_telephone_number", "digital_signature",
            "report_status", "reported_by_ip", "other_information")
        );
        $dbInsert->file_id = $file->id;
        $dbInsert->report_date = coreFunctions::sqlDateTime();
        $dbInsert->reported_by_name = $reported_by_name;
        $dbInsert->reported_by_email = $reported_by_email;
        $dbInsert->reported_by_address = $reported_by_address;
        $dbInsert->reported_by_telephone_number = $reported_by_telephone_number;
        $dbInsert->digital_signature = $digital_signature;
        $dbInsert->report_status = 'pending';
        $dbInsert->reported_by_ip = coreFunctions::getUsersIPAddress();
        $dbInsert->other_information = $other_information;
        if ($dbInsert->insert())
        {
            // send email
            $subject = t('report_file_email_subject', 'New abuse report on [[[SITE_NAME]]]', array('SITE_NAME' => SITE_CONFIG_SITE_NAME));

            $replacements = array(
                'FILE_DETAILS' => $file_url,
                'SITE_NAME' => SITE_CONFIG_SITE_NAME,
                'WEB_ROOT' => WEB_ROOT,
                'USERS_IP' => coreFunctions::getUsersIPAddress()
            );
            $defaultContent = "There is a new abuse report on [[[SITE_NAME]]] with the following details:<br/><br/>";
            $defaultContent .= "***************************************<br/>";
            $defaultContent .= "[[[FILE_DETAILS]]]<br/>";
            $defaultContent .= "***************************************<br/>";
            $defaultContent .= "Submitted IP: [[[USERS_IP]]]<br/>";
            $defaultContent .= "***************************************<br/><br/>";
            $defaultContent .= "Please login via [[[WEB_ROOT]]]/admin/ to investigate further.";
            $htmlMsg = t('report_file_email_content', $defaultContent, $replacements);

            coreFunctions::sendHtmlEmail(SITE_CONFIG_REPORT_ABUSE_EMAIL, $subject, $htmlMsg, SITE_CONFIG_REPORT_ABUSE_EMAIL, strip_tags(str_replace("<br/>", "\n", $htmlMsg)), false, $reported_by_name, true, $reported_by_email);
            coreFunctions::redirect(WEB_ROOT . '/report_file.' . SITE_CONFIG_PAGE_EXTENSION . '?s=1');
        }
        else
        {
            notification::setError(t("report_abuse_error_failed_reporting", "Failed reporting file, please try again later"));
        }
    }
}
else
{
    // if url has been passed
    if (isset($_REQUEST['file_url']))
    {
        $file_url = trim($_REQUEST['file_url']);
    }

    // if user logged in
    if ($Auth->loggedIn())
    {
        $reported_by_email = $Auth->user->email;
    }
}

// include header
require_once(SITE_TEMPLATES_PATH . '/partial/_header.inc.php');
?>
<div class="row report">
    <div class="col-md-12">
    <?php
    if (notification::isErrors())
    {
    echo notification::outputErrors();
    }
    elseif (notification::isSuccess())
    {
    echo notification::outputSuccess();
    }
    ?>
    </div>
    <div class="col-md-8 col-md-offset-2 form-element">
        <div class="form">
            <form method="post" action="<?php echo WEB_ROOT; ?>/report_file.<?php echo SITE_CONFIG_PAGE_EXTENSION; ?>">
                <div class="form-group">
                    <input name="file_url" type="text" class="form-control" value="<?php echo validation::safeOutputToScreen($file_url); ?>" placeholder="<?php echo t('report_file_full_file_url', 'Full file url'); ?>">
                </div>
                <div class="form-group">
                    <textarea rows="5" class="form-control" id="other_information" placeholder="<?php echo t('report_file_description_of_the_works', 'Description of the copyrighted works and supporting information'); ?>" name="other_information"><?php echo validation::safeOutputToScreen($other_information); ?></textarea>
                </div>
                <div class="form-group">
                    <input type="text" name="reported_by_name" class="form-control" value="<?php echo validation::safeOutputToScreen($reported_by_name); ?>" placeholder="<?php echo t('report_file_your_name', 'Your name'); ?>">
                </div>
                <div class="form-group">
                    <input name="reported_by_email" class="form-control" value="<?php echo validation::safeOutputToScreen($reported_by_email); ?>" placeholder="<?php echo t('report_file_email_address', 'Email address'); ?>"/>
                </div>
                <div class="form-group">
                    <input name="reported_by_address" class="form-control" value="<?php echo validation::safeOutputToScreen($reported_by_address); ?>" placeholder="<?php echo t('report_file_postal_address', 'Postal address'); ?>"/>
                </div>
                <div class="form-group">
                    <input name="reported_by_telephone_number" class="form-control" value="<?php echo validation::safeOutputToScreen($reported_by_telephone_number); ?>" placeholder="<?php echo t('report_file_phone_number', 'Phone number'); ?>"/>
                </div>
                <div class="form-group">
                    <input name="digital_signature" class="form-control" value="<?php echo validation::safeOutputToScreen($digital_signature); ?>" placeholder="<?php echo t('report_file_signature', 'Signature'); ?>"/>
                </div>
                <?php
                echo '<span style="color: #999;">' . t('report_file_electronic_signature_of_the_copyright', 'Electronic signature of the copyright owner or the person authorized to act on its behalf') . '</span>';
                ?>
                <label style="margin-top:16px;">
                    <?php
                    echo t('report_file_confirm_1', 'Confirm 1');
                    ?>:
                    <?php
                    echo t('report_file_you_have_a_good_faith_belief', 'You have a good faith belief that use of the material in the manner complained of is not authorized by the copyright owner, its agent, or the law.');
                    ?>&nbsp;
                    <select name="confirm_1" class="form-control" style="width: 150px; display: inline; margin-left: 10px;">
                        <?php
                        $opts = array('no' => t('report_file_no', 'no'), 'yes' => t('report_file_yes', 'yes'));
                        foreach ($opts AS $k => $opt)
                        {
                            echo '<option value="' . $k . '"';
                            if ($confirm_1 == $k)
                            {
                                echo ' SELECTED';
                            }
                            echo '>';
                            echo UCWords($opt);
                            echo '</option>';
                        }
                        ?>
                    </select>
                </label>
                <label style="margin-top:16px;">
                    <?php
                    echo t('report_file_confirm_2', 'Confirm 2');
                    ?>:
                    <?php
                    echo t('report_file_the_information_in_this_noticiation', 'The information in the notification is accurate, and, under the pains and penalties of perjury, that you are authorized to act on behalf of the copyright owner.');
                    ?>&nbsp;
                    <select name="confirm_2" class="form-control" style="width: 150px; display: inline; margin-left: 10px;">
                        <?php
                        $opts = array('no' => t('report_file_no', 'no'), 'yes' => t('report_file_yes', 'yes'));
                        foreach ($opts AS $k => $opt)
                        {
                            echo '<option value="' . $k . '"';
                            if ($confirm_2 == $k)
                            {
                                echo ' SELECTED';
                            }
                            echo '>';
                            echo UCWords($opt);
                            echo '</option>';
                        }
                        ?>
                    </select>
                </label>
                <div class="form-group">
                        <input name="submitme" type="hidden" value="1"/>
                        <button class="btn btn-go" tabindex="99" type="submit" name="submit"><?php echo t("submit_report", "submit report"); ?> <i class="la la-arrow-right"></i></button>
                </div>
            </form>
        </div>
    </div>
</div>
<?php
// include footer
require_once(SITE_TEMPLATES_PATH . '/partial/_footer.inc.php');
?>
