<?php
/*
=====================================================
 DataLife Engine - by SoftNews Media Group
-----------------------------------------------------
 https://dle-news.ru/
-----------------------------------------------------
 Copyright (c) 2004-2025 SoftNews Media Group
=====================================================
 This code is protected by copyright
=====================================================
 File: partners.php
-----------------------------------------------------
 Use: Partners page (public)
=====================================================
*/

if (!defined('DATALIFEENGINE')) {
	header("HTTP/1.1 403 Forbidden");
	header('Location: ../../');
	die("Hacking attempt!");
}

function dle_partners_table(): string {
	return PREFIX . '_partners';
}

function dle_partners_ensure_table(): void {
	global $db;

	$table = dle_partners_table();
	$db->query(
		"CREATE TABLE IF NOT EXISTS {$table} (\n"
			. "  id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,\n"
			. "  user_id INT(11) UNSIGNED NOT NULL DEFAULT 0,\n"
			. "  author VARCHAR(40) NOT NULL DEFAULT '',\n"
			. "  title VARCHAR(80) NOT NULL DEFAULT '',\n"
			. "  url VARCHAR(255) NOT NULL DEFAULT '',\n"
			. "  banner VARCHAR(255) NOT NULL DEFAULT '',\n"
			. "  ip VARCHAR(46) NOT NULL DEFAULT '',\n"
			. "  date INT(11) UNSIGNED NOT NULL DEFAULT 0,\n"
			. "  clicks INT(11) UNSIGNED NOT NULL DEFAULT 0,\n"
			. "  approved TINYINT(1) UNSIGNED NOT NULL DEFAULT 1,\n"
			. "  PRIMARY KEY (id),\n"
			. "  KEY date (date),\n"
			. "  KEY clicks (clicks),\n"
			. "  KEY approved (approved)\n"
			. ") ENGINE=MyISAM DEFAULT CHARSET=" . COLLATE,
		false,
		false
	);

	// Backward-compatible upgrade: add missing columns on existing installs.
	$col = $db->super_query("SHOW COLUMNS FROM {$table} LIKE 'clicks'", false, false, false);
	if (!isset($col['Field']) || $col['Field'] !== 'clicks') {
		$db->query("ALTER TABLE {$table} ADD clicks INT(11) UNSIGNED NOT NULL DEFAULT 0", false, false);
		$db->query("ALTER TABLE {$table} ADD KEY clicks (clicks)", false, false);
	}
}

function dle_partners_normalize_url(string $url): string {
	$url = trim($url);
	$url = preg_replace('#\s+#u', '', $url);
	if (!$url) return '';
	if (!preg_match('#^https?://#i', $url)) {
		$url = 'https://' . $url;
	}
	return $url;
}

function dle_partners_is_url_valid(string $url): bool {
	$url = trim($url);
	if (!$url) return false;
	if (dle_strlen($url) > 255) return false;

	// Accept domain-only input too (we normalize later), but validate a safe URL-ish shape.
	return (bool)preg_match('#^(https?://)?[\pL0-9\-._~%]+(?:\:[0-9]{1,5})?(?:/[^\s<>]*)?$#iu', $url);
}

function dle_partners_is_http_url(string $url): bool {
	$url = trim($url);
	if (!$url) return false;

	$parts = @parse_url($url);
	if (!is_array($parts)) return false;

	$scheme = isset($parts['scheme']) ? strtolower((string)$parts['scheme']) : '';
	if ($scheme !== 'http' && $scheme !== 'https') return false;

	$host = isset($parts['host']) ? trim((string)$parts['host']) : '';
	if ($host === '') return false;
	if (dle_strlen($host) > 253) return false;

	if (isset($parts['port'])) {
		$port = intval($parts['port']);
		if ($port < 1 || $port > 65535) return false;
	}

	return true;
}

function dle_partners_is_allowed_banner_url(string $url): bool {
	$url = trim($url);
	if (!$url) return false;

	$path = @parse_url($url, PHP_URL_PATH);
	if (!is_string($path) || $path === '') return true;

	$ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
	if ($ext === '') return true;

	return in_array($ext, ['png', 'jpg', 'jpeg', 'gif', 'webp', 'svg'], true);
}

function dle_partners_build_antispam_blocks(): array {
	global $db, $config, $member_id, $user_group, $lang;

	$question = '';
	$sec_code = '';
	$recaptcha = '';
	$extra_html = '';

	$show_question = !empty($user_group[$member_id['user_group']]['comments_question']);
	$show_sec_code = false;
	$show_recaptcha = false;

	$disable_comments_captcha = intval($user_group[$member_id['user_group']]['disable_comments_captcha'] ?? 0);
	if ($disable_comments_captcha && intval($member_id['comm_num'] ?? 0) >= $disable_comments_captcha) {
		$show_question = false;
		$user_group[$member_id['user_group']]['captcha'] = false;
		$user_group[$member_id['user_group']]['comments_question'] = false;
	}

	if ($show_question) {
		$q = $db->super_query("SELECT id, question FROM " . PREFIX . "_question ORDER BY RAND() LIMIT 1");
		if (!isset($q['id']) || !isset($q['question']) || !$q['question']) {
			$show_question = false;
		} else {
			$_SESSION['question'] = intval($q['id']);
			$question = htmlspecialchars(stripslashes($q['question']), ENT_QUOTES, 'UTF-8');
		}
	}

	if (!empty($user_group[$member_id['user_group']]['captcha'])) {
		if (!empty($config['allow_recaptcha'])) {
			$show_recaptcha = true;

			$captcha_name = 'g-recaptcha';
			$captcha_url = "https://www.google.com/recaptcha/api.js?hl={$lang['language_code']}";

			if ($config['allow_recaptcha'] == 3) {
				$captcha_name = 'h-captcha';
				$captcha_url = "https://js.hcaptcha.com/1/api.js?hl={$lang['language_code']}";
			}

			if ($config['allow_recaptcha'] == 4) {
				$captcha_name = 'cf-turnstile';
				$captcha_url = 'https://challenges.cloudflare.com/turnstile/v0/api.js?compat=recaptcha';
			}

			if ($config['allow_recaptcha'] == 2) {
				$recaptcha = '';
				$extra_html = "\n<script src=\"https://www.google.com/recaptcha/api.js?render={$config['recaptcha_public_key']}\" async defer></script>\n";
				$extra_html .= "<script>\n";
				$extra_html .= "document.addEventListener('DOMContentLoaded', function () {\n";
				$extra_html .= "  var form = document.getElementById('partners-form');\n";
				$extra_html .= "  if (!form || typeof grecaptcha === 'undefined') return;\n";
				$extra_html .= "  form.addEventListener('submit', function (event) {\n";
				$extra_html .= "    if (form.dataset.recaptchaProcessed === '1') return;\n";
				$extra_html .= "    event.preventDefault();\n";
				$extra_html .= "    grecaptcha.execute('{$config['recaptcha_public_key']}', {action: 'partners'}).then(function (token) {\n";
				$extra_html .= "      var input = document.getElementById('g-recaptcha-response');\n";
				$extra_html .= "      if (!input) {\n";
				$extra_html .= "        input = document.createElement('input');\n";
				$extra_html .= "        input.type = 'hidden';\n";
				$extra_html .= "        input.name = 'g-recaptcha-response';\n";
				$extra_html .= "        input.id = 'g-recaptcha-response';\n";
				$extra_html .= "        form.appendChild(input);\n";
				$extra_html .= "      }\n";
				$extra_html .= "      input.value = token;\n";
				$extra_html .= "      form.dataset.recaptchaProcessed = '1';\n";
				$extra_html .= "      HTMLFormElement.prototype.submit.call(form);\n";
				$extra_html .= "    });\n";
				$extra_html .= "  });\n";
				$extra_html .= "});\n";
				$extra_html .= "</script>\n";
			} else {
				$recaptcha = "<div class=\"{$captcha_name}\" data-sitekey=\"{$config['recaptcha_public_key']}\" data-theme=\"{$config['recaptcha_theme']}\" data-language=\"{$lang['language_code']}\"></div><script src=\"{$captcha_url}\" async defer></script>";
			}
		} else {
			$show_sec_code = true;
			$path = parse_url($config['http_home_url']);
			$img_src = ($path && isset($path['path'])) ? $path['path'] : '/';
			$sec_code = "<a class=\"pt-captcha\" onclick=\"PTReloadCaptcha(); return false;\" href=\"#\" title=\"{$lang['reload_code']}\"><span id=\"pt-captcha\"><img src=\"{$img_src}index.php?controller=antibot\" alt=\"{$lang['reload_code']}\" width=\"160\" height=\"80\" /></span></a>";
		}
	}

	return [
		'show_question' => $show_question,
		'question' => $question,
		'show_sec_code' => $show_sec_code,
		'sec_code' => $sec_code,
		'show_recaptcha' => $show_recaptcha,
		'recaptcha' => $recaptcha,
		'extra_html' => $extra_html,
	];
}

function dle_partners_check_antispam(): array {
	global $db, $config, $member_id, $user_group, $lang;

	$errors = [];

	$comments_question = !empty($user_group[$member_id['user_group']]['comments_question']);
	$captcha = !empty($user_group[$member_id['user_group']]['captcha']);

	$disable_comments_captcha = intval($user_group[$member_id['user_group']]['disable_comments_captcha'] ?? 0);
	if ($disable_comments_captcha && intval($member_id['comm_num'] ?? 0) >= $disable_comments_captcha) {
		$comments_question = false;
		$captcha = false;
	}

	if ($captcha) {
		if (!empty($config['allow_recaptcha'])) {
			if (!empty($_POST['g-recaptcha-response'])) {
				$reCaptcha = new ReCaptcha($config['recaptcha_private_key']);
				$resp = $reCaptcha->verifyResponse(get_ip(), $_POST['g-recaptcha-response']);
				if ($resp === null || !$resp->success) {
					$errors[] = $lang['recaptcha_fail'];
				}
			} else {
				$errors[] = $lang['recaptcha_fail'];
			}
		} elseif ((string)($_REQUEST['sec_code'] ?? '') != (string)($_SESSION['sec_code_session'] ?? '') || empty($_SESSION['sec_code_session'])) {
			$errors[] = $lang['recaptcha_fail'];
		}
	}

	if ($comments_question) {
		if (intval($_SESSION['question'] ?? 0)) {
			$answer = $db->super_query("SELECT id, answer FROM " . PREFIX . "_question WHERE id='" . intval($_SESSION['question']) . "'");
			$answers = explode("\n", $answer['answer'] ?? '');
			$pass_answer = false;
			$question_answer = trim(dle_strtolower((string)($_POST['question_answer'] ?? '')));

			if (count($answers) && $question_answer) {
				foreach ($answers as $a) {
					$a = trim(dle_strtolower($a));
					if ($a && $a == $question_answer) {
						$pass_answer = true;
						break;
					}
				}
			}

			if (!$pass_answer) {
				$errors[] = $lang['reg_err_24'];
			}
		} else {
			$errors[] = $lang['reg_err_24'];
		}
	}

	return $errors;
}

// -------------------- Page logic --------------------

dle_partners_ensure_table();

$action = isset($_REQUEST['action']) ? (string)$_REQUEST['action'] : '';
$action = totranslit($action);

$alert_html = '';

// Click redirect (counts outbound clicks for each partner)
if ($action === 'out') {
	$table = dle_partners_table();
	$id = isset($_GET['id']) ? intval($_GET['id']) : 0;

	if ($id < 1) {
		header("HTTP/1.1 404 Not Found");
		die('Not Found');
	}

	$row = $db->super_query("SELECT id, url FROM {$table} WHERE id='{$id}' AND approved=1 LIMIT 1");
	$url = isset($row['url']) ? trim(stripslashes((string)$row['url'])) : '';

	if (!$url || !dle_partners_is_http_url($url)) {
		header("HTTP/1.1 404 Not Found");
		die('Not Found');
	}

	// Increment counter (best-effort)
	$db->query("UPDATE {$table} SET clicks=clicks+1 WHERE id='{$id}'", false, false);

	header('Location: ' . $url);
	die();
}

// Add partner
if ($action === 'add' && isset($_POST['pt_submit'])) {
	$errors = [];

	$user_hash = isset($_POST['user_hash']) ? (string)$_POST['user_hash'] : '';
	if (!$user_hash || $user_hash !== $dle_login_hash) {
		$errors[] = $lang['sess_error'];
	}

	$_IP = get_ip();
	$_TIME = time();

	$author = isset($_POST['author']) ? trim((string)$_POST['author']) : '';
	$title = isset($_POST['title']) ? trim((string)$_POST['title']) : '';
	$url_raw = isset($_POST['url']) ? trim((string)$_POST['url']) : '';
	$banner_raw = isset($_POST['banner']) ? trim((string)$_POST['banner']) : '';

	if ($is_logged) {
		$author = (string)($member_id['name'] ?? '');
	}

	$author = htmlspecialchars(strip_tags($author), ENT_QUOTES, 'UTF-8');
	if (!$author) {
		$errors[] = 'Completează numele.';
	} elseif (dle_strlen($author) > 40) {
		$errors[] = 'Numele este prea lung.';
	}

	$title = htmlspecialchars(strip_tags($title), ENT_QUOTES, 'UTF-8');
	if (!$title) {
		$errors[] = 'Completează numele site-ului.';
	} elseif (dle_strlen($title) < 2) {
		$errors[] = 'Numele site-ului este prea scurt.';
	} elseif (dle_strlen($title) > 80) {
		$errors[] = 'Numele site-ului este prea lung.';
	}

	if (!dle_partners_is_url_valid($url_raw)) {
		$errors[] = 'Link-ul site-ului nu este valid.';
	}
	$url = dle_partners_normalize_url($url_raw);
	if (!$url || !dle_partners_is_http_url($url)) {
		$errors[] = 'Link-ul site-ului nu este valid.';
	}

	$banner = '';
	if ($banner_raw !== '') {
		if (!dle_partners_is_url_valid($banner_raw)) {
			$errors[] = 'Link-ul logo/banner nu este valid.';
		} else {
			$banner = dle_partners_normalize_url($banner_raw);
			if (!$banner || !dle_partners_is_http_url($banner) || !dle_partners_is_allowed_banner_url($banner)) {
				$errors[] = 'Link-ul logo/banner nu este valid.';
			}
		}
	}

	if (!$is_logged) {
		$table = dle_partners_table();
		$last = $db->super_query("SELECT date FROM {$table} WHERE ip='{$_IP}' ORDER BY id DESC LIMIT 1");
		if (isset($last['date']) && intval($last['date']) && ($_TIME - intval($last['date'])) < 60) {
			$errors[] = 'Ai trimis deja un parteneriat recent. Te rugăm așteaptă 1 minut și încearcă din nou.';
		}
	}

	if (!empty($user_group[$member_id['user_group']]['spamfilter'])) {
		$mail = $is_logged ? (string)($member_id['email'] ?? '') : '';
		$row = $db->super_query("SELECT * FROM " . PREFIX . "_spam_log WHERE ip = '{$_IP}'");
		if (!isset($row['id']) || !isset($row['email']) || !$row['id'] || !$row['email']) {
			$sfs = new StopSpam($config['spam_api_key'], $user_group[$member_id['user_group']]['spamfilter']);
			$args = ['ip' => $_IP, 'email' => $mail];
			if ($sfs->is_spammer($args)) {
				if (empty($row['id'])) {
					$db->query("INSERT INTO " . PREFIX . "_spam_log (ip, is_spammer, email, date) VALUES ('{$_IP}','1', '" . $db->safesql($mail) . "', '{$_TIME}')");
				} else {
					$db->query("UPDATE " . PREFIX . "_spam_log SET is_spammer='1', email='" . $db->safesql($mail) . "' WHERE id='" . intval($row['id']) . "'");
				}
				$errors[] = $lang['reg_err_29'] . ' ';
			} else {
				if (empty($row['id'])) {
					$db->query("INSERT INTO " . PREFIX . "_spam_log (ip, is_spammer, email, date) VALUES ('{$_IP}','0', '" . $db->safesql($mail) . "', '{$_TIME}')");
				} else {
					$db->query("UPDATE " . PREFIX . "_spam_log SET email='" . $db->safesql($mail) . "' WHERE id='" . intval($row['id']) . "'");
				}
			}
		} else {
			if (!empty($row['is_spammer'])) {
				$errors[] = $lang['reg_err_29'] . ' ';
			}
		}
	}

	$errors = array_merge($errors, dle_partners_check_antispam());

	if (!count($errors)) {
		$table = dle_partners_table();
		$safe_url = $db->safesql($url);
		$exists = $db->super_query("SELECT id FROM {$table} WHERE url='{$safe_url}' LIMIT 1");
		if (isset($exists['id']) && intval($exists['id'])) {
			$errors[] = 'Acest site este deja în lista de parteneri.';
		}
	}

	if (!count($errors)) {
		$table = dle_partners_table();
		$db->query(
			"INSERT INTO {$table} (user_id, author, title, url, banner, ip, date, approved) VALUES ("
				. "'" . intval($is_logged ? ($member_id['user_id'] ?? 0) : 0) . "',"
				. "'" . $db->safesql($author) . "',"
				. "'" . $db->safesql($title) . "',"
				. "'" . $db->safesql($url) . "',"
				. "'" . $db->safesql($banner) . "',"
				. "'" . $db->safesql($_IP) . "',"
				. "'" . intval($_TIME) . "',"
				. "'1'"
				. ")"
		);

		header("Location: " . $config['http_home_url'] . "index.php?do=partners");
		die();
	}

	$alert_html = '<div class="pt-alert pt-alert--error">' . implode('<br>', $errors) . '</div>';
}

// List
$table = dle_partners_table();
$rows = $db->super_query(
	"SELECT id, author, title, url, banner, date, clicks FROM {$table} WHERE approved=1 ORDER BY id DESC",
	true
);

$list_html = '';
if (is_array($rows) && count($rows)) {
	$list_html .= '<div class="pt-grid">';
	foreach ($rows as $row) {
		$title = htmlspecialchars(stripslashes((string)($row['title'] ?? '')), ENT_QUOTES, 'UTF-8');
		$url = htmlspecialchars(stripslashes((string)($row['url'] ?? '')), ENT_QUOTES, 'UTF-8');
		$banner = htmlspecialchars(stripslashes((string)($row['banner'] ?? '')), ENT_QUOTES, 'UTF-8');
		$author = htmlspecialchars(stripslashes((string)($row['author'] ?? '')), ENT_QUOTES, 'UTF-8');
		$date = isset($row['date']) ? intval($row['date']) : 0;
		$clicks = isset($row['clicks']) ? intval($row['clicks']) : 0;

		$img_html = '';
		if ($banner) {
			$img_html = '<div class="pt-logo"><img src="' . $banner . '" alt="' . $title . '" loading="lazy"></div>';
		} else {
			$img_html = '<div class="pt-logo pt-logo--empty"><span>' . ( $title ? htmlspecialchars(dle_substr($title, 0, 1), ENT_QUOTES, 'UTF-8') : 'P' ) . '</span></div>';
		}

		$list_html .= '<div class="pt-card">';
		$list_html .= '<a class="pt-card__link" href="' . $config['http_home_url'] . 'index.php?do=partners&amp;action=out&amp;id=' . intval($row['id']) . '" target="_blank" rel="nofollow noopener">';
		$list_html .= $img_html;
		$list_html .= '<div class="pt-meta">';
		$list_html .= '<div class="pt-title">' . ($title ?: $url) . '</div>';
		$list_html .= '<div class="pt-url">' . $url . '</div>';
		$list_html .= '<div class="pt-sub">Adăugat de <b>' . ($author ?: 'Guest') . '</b> • ' . langdate('j F Y', $date) . ' • <span>Vizite: <b>' . $clicks . '</b></span></div>';
		$list_html .= '</div>';
		$list_html .= '</a>';
		$list_html .= '</div>';
	}
	$list_html .= '</div>';
} else {
	$list_html = '<div class="pt-empty">Nu există parteneri încă. Fii primul!</div>';
}

$antispam = dle_partners_build_antispam_blocks();

$form_html = '';
$form_html .= '<form id="partners-form" class="pt-form" method="post" action="' . $config['http_home_url'] . 'index.php?do=partners">';
$form_html .= '<input type="hidden" name="action" value="add">';
$form_html .= '<input type="hidden" name="user_hash" value="' . htmlspecialchars($dle_login_hash, ENT_QUOTES, 'UTF-8') . '">';

$form_html .= '<div class="pt-form__grid">';
	if (!$is_logged) {
		$form_html .= '<div class="pt-form__row">';
		$form_html .= '<label class="pt-label">Numele tău</label>';
		$form_html .= '<input class="pt-input" type="text" name="author" maxlength="40" required>';
		$form_html .= '</div>';
	} else {
		$form_html .= '<div class="pt-form__row pt-form__row--as">Scrii ca <b>' . htmlspecialchars($member_id['name'], ENT_QUOTES, 'UTF-8') . '</b></div>';
	}

	$form_html .= '<div class="pt-form__row">';
	$form_html .= '<label class="pt-label">Numele site-ului</label>';
	$form_html .= '<input class="pt-input" type="text" name="title" maxlength="80" placeholder="Ex: Muzica Online" required>';
	$form_html .= '</div>';

	$form_html .= '<div class="pt-form__row">';
	$form_html .= '<label class="pt-label">Link site (URL)</label>';
	$form_html .= '<input class="pt-input" type="text" name="url" maxlength="255" placeholder="https://domeniu.ro" required>';
	$form_html .= '</div>';

	$form_html .= '<div class="pt-form__row">';
	$form_html .= '<label class="pt-label">Logo / Banner (URL, opțional)</label>';
	$form_html .= '<input class="pt-input" type="text" name="banner" maxlength="255" placeholder="https://domeniu.ro/logo.png">';
	$form_html .= '<div class="pt-hint">Acceptă link direct către imagine (PNG/JPG/WebP). Dacă nu ai, poți lăsa gol.</div>';
	$form_html .= '</div>';
$form_html .= '</div>';

if (!empty($antispam['show_question'])) {
	$form_html .= '<div class="pt-form__row">';
	$form_html .= '<label class="pt-label">' . ($lang['question'] ?? 'Question') . '</label>';
	$form_html .= '<div class="pt-question">' . $antispam['question'] . '</div>';
	$form_html .= '<input class="pt-input" type="text" name="question_answer" autocomplete="off" required>';
	$form_html .= '</div>';
}

if (!empty($antispam['show_recaptcha'])) {
	$form_html .= '<div class="pt-form__row">';
	$form_html .= '<label class="pt-label">Verificare anti-spam</label>';
	$form_html .= '<div class="pt-hint">Completează verificarea de mai jos ca să poți trimite parteneriatul.</div>';
	$form_html .= $antispam['recaptcha'];
	$form_html .= '</div>';
}

if (!empty($antispam['show_sec_code'])) {
	$form_html .= '<div class="pt-form__row">';
	$form_html .= '<label class="pt-label" for="pt-sec-code">Cod din imagine</label>';
	$form_html .= '<div class="pt-hint">Introdu codul din imagine. Click pe imagine pentru reîmprospătare.</div>';
	$form_html .= '<div class="pt-captcha-row">';
	$form_html .= '<div>' . $antispam['sec_code'] . '</div>';
	$form_html .= '<input id="pt-sec-code" class="pt-input" type="text" name="sec_code" autocomplete="off" required>';
	$form_html .= '</div>';
	$form_html .= '</div>';
}

$form_html .= '<div class="pt-actions">';
$form_html .= '<button class="pt-btn" type="submit" name="pt_submit">Adaugă partener</button>';
$form_html .= '</div>';
$form_html .= '</form>';

$site_url = $config['http_home_url'];
$site_title = $config['home_title'] ?? 'Site';
$site_title_safe = htmlspecialchars($site_title, ENT_QUOTES, 'UTF-8');
$site_url_safe = htmlspecialchars($site_url, ENT_QUOTES, 'UTF-8');

$code_html = '';
$code_html .= '<div class="pt-code">';
$code_html .= '<h3>Vrei parteneriat?</h3>';
$code_html .= '<p>1) Copiază codul de mai jos pe site-ul tău (înlocuiește <b>https://domeniu.ro</b> cu adresa ta dacă este nevoie).</p>';
$code_html .= '<p>2) Apoi completează formularul de mai sus ca să apari în listă.</p>';
$code_html .= '<div class="pt-code__box">';
$code_html .= '<div class="pt-code__label">Cod link (text)</div>';
$code_html .= '<textarea class="pt-code__ta" rows="3" readonly>';
$code_html .= '<a href="' . $site_url_safe . '" target="_blank" rel="noopener">' . $site_title_safe . '</a>';
$code_html .= '</textarea>';
$code_html .= '</div>';
$code_html .= '<div class="pt-code__box">';
$code_html .= '<div class="pt-code__label">Cod link (cu banner – dacă ai un banner)</div>';
$code_html .= '<textarea class="pt-code__ta" rows="4" readonly>';
$code_html .= '<a href="' . $site_url_safe . '" target="_blank" rel="noopener"><img src="' . $site_url_safe . '/banner.png" alt="' . $site_title_safe . '"></a>';
$code_html .= '</textarea>';
$code_html .= '</div>';
$code_html .= '</div>';

$code_html .= <<<HTML
<script>
function PTReloadCaptcha() {
  var cap = document.getElementById('pt-captcha');
  if (!cap) return;
  var timeval = new Date().getTime();
  cap.innerHTML = '<img src="' + dle_root + 'index.php?controller=antibot&rand=' + timeval + '" width="160" height="80" alt="">';
}
</script>
HTML;

$code_html .= $antispam['extra_html'] ?? '';

if ($tpl->load_template('partners.tpl')) {
	$tpl->set('{partners-alert}', $alert_html);
	$tpl->set('{partners-form}', $form_html);
	$tpl->set('{partners-list}', $list_html);
	$tpl->set('{partners-code}', $code_html);
	$tpl->compile('content');
	$tpl->clear();
} else {
	$tpl->result['content'] .= $alert_html . $form_html . $list_html . $code_html;
}
