<?php /*

 Composr
 Copyright (c) Christopher Graham, 2004-2024

 See docs/LICENSE.md for full licensing information.


 NOTE TO PROGRAMMERS:
   Do not edit this file. If you need to make changes, save your changed file to the appropriate *_custom folder
   **** If you ignore this advice, then your website upgrades (e.g. for bug fixes) will likely kill your changes ****

*/

/**
 * @license    http://opensource.org/licenses/cpal_1.0 Common Public Attribution License
 * @copyright  Christopher Graham
 * @package    galleries
 */

/**
 * Script handler for downloading a gallery, as specified by GET parameters.
 */
function download_gallery_script()
{
    if (!addon_installed('galleries')) {
        warn_exit(do_lang_tempcode('MISSING_ADDON', escape_html('galleries')));
    }

    // Closed site
    $site_closed = get_option('site_closed');
    if (($site_closed != '0') && (!has_privilege(get_member(), 'access_closed_site')) && (!$GLOBALS['IS_ACTUALLY_ADMIN'])) {
        http_response_code(503);
        header('Content-Type: text/plain; charset=' . get_charset());
        @exit(get_option('closed'));
    }

    attach_to_screen_header('<meta name="robots" content="noindex" />');

    $cat = get_param_string('cat');

    if (!has_category_access(get_member(), 'galleries', $cat)) {
        access_denied('CATEGORY_ACCESS');
    }

    check_privilege('may_download_gallery', ['galleries', $cat]);
    if ((strpos($cat, "\n") !== false) || (strpos($cat, "\r") !== false)) {
        log_hack_attack_and_exit('HEADER_SPLIT_HACK');
    }

    $num_videos = $GLOBALS['SITE_DB']->query_select_value('videos', 'COUNT(*)', ['cat' => $cat, 'validated' => 1]);
    $num_images = $GLOBALS['SITE_DB']->query_select_value('images', 'COUNT(*)', ['cat' => $cat, 'validated' => 1]);

    require_lang('galleries');

    require_code('tasks');
    $ret = call_user_func_array__long_task(do_lang('DOWNLOAD_GALLERY_CONTENTS'), get_screen_title('DOWNLOAD_GALLERY_CONTENTS'), 'download_gallery', [$cat], false, (($num_videos == 0) && ($num_images < intval(get_value('download_gallery_queue_images', '5', true)))));

    $echo = globalise($ret, null, '', true);
    $echo->evaluate_echo(null);
}

/**
 * Show a gallery media entry along with the CSS and JS resources.
 */
function show_gallery_video_script()
{
    if (!addon_installed('galleries')) {
        warn_exit(do_lang_tempcode('MISSING_ADDON', escape_html('galleries')));
    }

    attach_to_screen_header('<meta name="robots" content="noindex" />');

    $cat = get_param_string('cat');
    $id = get_param_integer('id');

    if (!has_category_access(get_member(), 'galleries', $cat)) {
        access_denied('CATEGORY_ACCESS');
    }

    if (addon_installed('content_privacy')) {
        require_code('content_privacy');
        check_privacy('video', strval($id));
    }

    $rows = $GLOBALS['SITE_DB']->query_select('videos', ['*'], ['id' => $id], '', 1);
    if (!array_key_exists(0, $rows)) {
        warn_exit(do_lang_tempcode('MISSING_RESOURCE', 'video'));
    }
    $myrow = $rows[0];

    // Validation
    if (($myrow['validated'] == 0) && (addon_installed('validation'))) {
        if ((!has_privilege(get_member(), 'jump_to_not_validated')) && ((is_guest()) || ($myrow['submitter'] != get_member()))) {
            access_denied('PRIVILEGE', 'jump_to_not_validated');
        }
    }

    $url = $myrow['url'];
    if (url_is_local($url)) {
        $url = get_custom_base_url() . '/' . $url;
    }
    $thumb_url = $myrow['thumb_url'];
    if (url_is_local($thumb_url)) {
        $thumb_url = get_custom_base_url() . '/' . $thumb_url;
    }

    require_code('galleries');
    $video = show_gallery_video_media($url, $thumb_url, $myrow['video_width'], $myrow['video_height'], $myrow['video_length'], $myrow['submitter'], $myrow['closed_captions_url']);

    require_code('web_resources');
    echo css_tempcode()->evaluate();

    echo $video->evaluate();

    echo javascript_tempcode()->evaluate();
}
