<?php
# protect against hack attempts
if (!defined('NGCMS')) die('Galaxy in danger');
# preload required libraries
//loadPluginLibrary('uprofile', 'lib');
//loadPluginLibrary('comments', 'lib');
loadPluginLibrary('uprofile', 'lib');
register_plugin_page('auth_social', '', 'socialAuth', 0);
add_act('usermenu', 'auth_social_links');
//register_plugin_page('auth_social', 'register' , 'socialRegister', 0);
//register_plugin_page('auth_social', 'delete' , 'loginzaDelete', 0);
function socialAuth()
{
	global $config, $template, $tpl, $mysql, $userROW, $AUTH_METHOD;
	require_once ($_SERVER['DOCUMENT_ROOT']) . '/engine/plugins/auth_social/lib/SocialAuther/autoload.php';
	$adapterConfigs = array(
		'vk'            => array(
			'client_id'     => pluginGetVariable('auth_social', 'vk_client_id'),
			'client_secret' => pluginGetVariable('auth_social', 'vk_client_secret'),
			'redirect_uri'  => home . "/plugin/auth_social/?provider=vk"
		),
		'yandex'        => array(
			'client_id'     => pluginGetVariable('auth_social', 'yandex_client_id'),
			'client_secret' => pluginGetVariable('auth_social', 'yandex_client_secret'),
			'redirect_uri'  => home . "/plugin/auth_social/?provider=yandex"
		),
		'google'        => array(
			'client_id'     => pluginGetVariable('auth_social', 'google_client_id'),
			'client_secret' => pluginGetVariable('auth_social', 'google_client_secret'),
			'redirect_uri'  => home . "/plugin/auth_social/?provider=google"
		),
		'facebook'      => array(
			'client_id'     => pluginGetVariable('auth_social', 'facebook_client_id'),
			'client_secret' => pluginGetVariable('auth_social', 'facebook_client_secret'),
			'redirect_uri'  => home . "/plugin/auth_social/?provider=facebook"
		),
		'github'        => array(
			'client_id'     => pluginGetVariable('auth_social', 'github_client_id'),
			'client_secret' => pluginGetVariable('auth_social', 'github_client_secret'),
			'redirect_uri'  => home . "/plugin/auth_social/?provider=github"
		),
	);
	$adapters = array();
	foreach ($adapterConfigs as $adapter => $settings) {
		$class = 'SocialAuther\\Adapter\\' . ucfirst($adapter);
		$adapters[$adapter] = new $class($settings);
	}
	if (isset($_GET['provider']) && array_key_exists($_GET['provider'], $adapters)) {
		$auther = new SocialAuther\SocialAuther($adapters[$_GET['provider']]);
		if ($auther->authenticate()) {
			$record = $mysql->record(
				"SELECT *  FROM " . uprefix . "_users WHERE `provider` = '" . $auther->getProvider() . "' AND `social_id` = '" . $auther->getSocialId() . "' LIMIT 1"
			);
			if (!$record) {
				$values = array(
					EncodePassword(MakeRandomPassword()),
					$auther->getProvider(),
					$auther->getSocialId(),
					$auther->getName(),
					$auther->getEmail(),
					$auther->getSocialPage(),
					$auther->getSex(),
					date('Y-m-d', strtotime($auther->getBirthday())),
					$auther->getAvatar(),
					time() + ($config['date_adjust'] * 60),
					time() + ($config['date_adjust'] * 60)
				);
				$query = "INSERT INTO " . uprefix . "_users (`pass`, `provider`, `social_id`, `name`, `mail`, `social_page`, `sex`, `birthday`, `avatar`, `reg`, `last`) VALUES ('";
				$query .= implode("', '", $values) . "')";
				$mysql->query($query);
				$user_doreg = $mysql->record("SELECT * FROM " . uprefix . "_users WHERE social_page = " . db_squote($auther->getSocialPage()));
				$userid = $user_doreg['id'];
				$avatar = '';
				$get_avatar = $auther->getAvatar();
				if (!empty($get_avatar)) {
					addAvatarToFiles('newavatar', $get_avatar);
					@include_once root . 'includes/classes/upload.class.php';
					if (!empty($_FILES['newavatar']['name'])) {
						$fmanage = new file_managment();
						$imanage = new image_managment();
						$origName = strtolower(basename($_FILES['newavatar']['name']));
						$ext = pathinfo($origName, PATHINFO_EXTENSION);
						$safeExt = preg_match('/^(jpe?g|png|gif)$/i', $ext) ? strtolower($ext) : 'jpg';
						$fname = 'u' . intval($userid) . '_' . substr(md5($origName . microtime(true)), 0, 12) . '.' . $safeExt;
						$ftmp = $_FILES['newavatar']['tmp_name'];
						$mysql->query("insert into " . prefix . "_images (name, orig_name, folder, date, user, owner_id, category) values (" . db_squote($fname) . ", " . db_squote($origName) . ", '', unix_timestamp(now()), " . db_squote($auther->getName()) . ", " . db_squote($userid) . ", '1')");
						$rowID = $mysql->record("select LAST_INSERT_ID() as id");
						if (@copy($ftmp, $config['avatars_dir'] . $fname)) {
							$sz = $imanage->get_size($config['avatars_dir'] . $fname);
							$mysql->query("update " . prefix . "_images set width=" . db_squote($sz['1']) . ", height=" . db_squote($sz['2']) . " where id = " . db_squote($rowID['id']));
							$avatar = $fname;
						}
					}
					$mysql->query(
						"UPDATE `" . uprefix . "_users` SET `activation` = ''" .
							(!empty($avatar) ? ", `avatar` = " . db_squote($avatar) : '') .
							" WHERE social_page = " . db_squote($auther->getSocialPage())
					);
				}
			} else {
				$userFromDb = new stdClass();
				$userFromDb->provider = $record['provider'];
				$userFromDb->socialId = $record['social_id'];
				$userFromDb->name = $record['name'];
				$userFromDb->email = $record['email'];
				$userFromDb->socialPage = $record['social_page'];
				$userFromDb->sex = $record['sex'];
				$userFromDb->birthday = date('m.d.Y', strtotime($record['birthday']));
			}
			$user = new stdClass();
			$user->provider = $auther->getProvider();
			$user->socialId = $auther->getSocialId();
			$user->name =  $auther->getName();
			$user->email = $auther->getEmail();
			$user->socialPage = $auther->getSocialPage();
			$user->sex = $auther->getSex();
			$user->birthday = $auther->getBirthday();
			if (isset($userFromDb) && $userFromDb != $user) {
				$idToUpdate = $record['id'];
				$birthday = date('Y-m-d', strtotime($user->birthday));
				$get_avatar = $auther->getAvatar();
				$avatar = '';
				if (!empty($get_avatar)) {
					addAvatarToFiles('newavatar', $get_avatar);
					@include_once root . 'includes/classes/upload.class.php';
					if (!empty($_FILES['newavatar']['name'])) {
						$fmanage = new file_managment();
						$imanage = new image_managment();
						$origName = strtolower(basename($_FILES['newavatar']['name']));
						$ext = pathinfo($origName, PATHINFO_EXTENSION);
						$safeExt = preg_match('/^(jpe?g|png|gif)$/i', $ext) ? strtolower($ext) : 'jpg';
						$fname = 'u' . intval($idToUpdate) . '_' . substr(md5($origName . microtime(true)), 0, 12) . '.' . $safeExt;
						$ftmp = $_FILES['newavatar']['tmp_name'];
						$mysql->query("insert into " . prefix . "_images (name, orig_name, folder, date, user, owner_id, category) values (" . db_squote($fname) . ", " . db_squote($origName) . ", '', unix_timestamp(now()), " . db_squote($auther->getName()) . ", " . db_squote($idToUpdate) . ", '1')");
						$rowID = $mysql->record("select LAST_INSERT_ID() as id");
						if (@copy($ftmp, $config['avatars_dir'] . $fname)) {
							$sz = $imanage->get_size($config['avatars_dir'] . $fname);
							$mysql->query("update " . prefix . "_images set width=" . db_squote($sz['1']) . ", height=" . db_squote($sz['2']) . " where id = " . db_squote($rowID['id']) . " ");
							$avatar = $fname;
						}
					}
				}
				$mysql->query(
					"UPDATE " . uprefix . "_users SET " .
						"`social_id` = " . db_squote($user->socialId) . ", `name` = " . db_squote($user->name) . ", `mail` = " . db_squote($user->email) . ", " .
						"`social_page` = " . db_squote($user->socialPage) . ", `sex` = " . db_squote($user->sex) . ", " .
						"`birthday` = " . db_squote($birthday) .
						(!empty($avatar) ? ", `avatar` = " . db_squote($avatar) : '') .
						" WHERE `id` = " . db_squote($idToUpdate)
				);
			}
			$_SESSION['user'] = $user;
			$user_dologin = $mysql->record("SELECT * FROM " . uprefix . "_users WHERE social_page = " . db_squote($auther->getSocialPage()));
			if (is_array($user_dologin)) {
				$auth = $AUTH_METHOD[$config['auth_module']];
				$auth->save_auth($user_dologin);
				header('Location: ' . $config['home_url']);
				return;
			}
		}
		header('Location: ' . $config['home_url']);
	} else {
		header('Location: ' . $config['home_url']);
	}
}
// Callback для add_act('usermenu', 'auth_social_links')
// Выводит набор ссылок авторизации через доступные провайдеры.
// Если пользователь уже авторизован - ничего не возвращает.
function auth_social_links()
{
	global $userROW, $config;
	// Не показываем, если пользователь залогинен
	if (is_array($userROW)) {
		return '';
	}
	// Подключаем автолоадер
	if (!class_exists('SocialAuther\\SocialAuther')) {
		// Используем константу root, если определена
		if (defined('root')) {
			@require_once root . 'plugins/auth_social/lib/SocialAuther/autoload.php';
		} else {
			@require_once ($_SERVER['DOCUMENT_ROOT']) . '/engine/plugins/auth_social/lib/SocialAuther/autoload.php';
		}
	}
	$providers = ['vk', 'yandex', 'google', 'facebook', 'github'];
	$links = [];
	foreach ($providers as $p) {
		$clientId = pluginGetVariable('auth_social', $p . '_client_id');
		$clientSecret = pluginGetVariable('auth_social', $p . '_client_secret');
		if (!$clientId || !$clientSecret) {
			continue; // пропускаем не настроенных
		}
		$settings = [
			'client_id'     => $clientId,
			'client_secret' => $clientSecret,
			'redirect_uri'  => home . '/plugin/auth_social/?provider=' . $p,
		];
		$className = 'SocialAuther\\Adapter\\' . ucfirst($p);
		try {
			if (!class_exists($className)) {
				continue;
			}
			$adapter = new $className($settings);
			$authUrl = $adapter->getAuthUrl();
			$links[] = '<a class="auth-social-link auth-social-link--' . $p . '" href="' . htmlspecialchars($authUrl, ENT_QUOTES, 'UTF-8') . '" rel="nofollow" title="' . ucfirst($p) . '">' . ucfirst($p) . '</a>';
		} catch (Throwable $e) {
			// Тихо игнорируем ошибки конкретного провайдера
		}
	}
	if (!$links) {
		return '';
	}
	// Оборачиваем в контейнер (можно стилизовать через .auth-social-links)
	return '<div class="auth-social-links">' . implode(' ', $links) . '</div>';
}
class SocialAuthCoreFilter extends CoreFilter
{
	function showUserMenu(&$tVars)
	{
		global $mysql, $userROW, $lang;
		require_once root . 'plugins/auth_social/lib/SocialAuther/autoload.php';
		$adapterConfigs = array(
			'vk'            => array(
				'client_id'     => pluginGetVariable('auth_social', 'vk_client_id'),
				'client_secret' => pluginGetVariable('auth_social', 'vk_client_secret'),
				'redirect_uri'  => home . "/plugin/auth_social/?provider=vk"
			),
			'yandex'        => array(
				'client_id'     => pluginGetVariable('auth_social', 'yandex_client_id'),
				'client_secret' => pluginGetVariable('auth_social', 'yandex_client_secret'),
				'redirect_uri'  => home . "/plugin/auth_social/?provider=yandex"
			),
			'google'        => array(
				'client_id'     => pluginGetVariable('auth_social', 'google_client_id'),
				'client_secret' => pluginGetVariable('auth_social', 'google_client_secret'),
				'redirect_uri'  => home . "/plugin/auth_social/?provider=google"
			),
			'facebook'      => array(
				'client_id'     => pluginGetVariable('auth_social', 'facebook_client_id'),
				'client_secret' => pluginGetVariable('auth_social', 'facebook_client_secret'),
				'redirect_uri'  => home . "/plugin/auth_social/?provider=facebook"
			),
			'github'        => array(
				'client_id'     => pluginGetVariable('auth_social', 'github_client_id'),
				'client_secret' => pluginGetVariable('auth_social', 'github_client_secret'),
				'redirect_uri'  => home . "/plugin/auth_social/?provider=github"
			)
		);
		$adapters = array();
		foreach ($adapterConfigs as $adapter => $settings) {
			$class = 'SocialAuther\Adapter\\' . ucfirst($adapter);
			$adapters[$adapter] = new $class($settings);
		}
		foreach ($adapters as $title => $adapter) {
			$tVars['p']['auth_social'][$title] = array(
				'authUrl' => $adapter->getAuthUrl(),
				'title'   => ucfirst($title)
			);
		}
	}
}
register_filter('core.userMenu', 'auth_social', new SocialAuthCoreFilter);
if (class_exists('p_uprofileFilter')) {
	class uSocialFilter extends p_uprofileFilter
	{
		function showProfile($userID, $SQLrow, &$tvars)
		{
			/*
			if (empty($SQLrow['loginza_id'])) {
				$tvars['regx']['/\[if-loginza\](.*?)\[\/if-loginza\]/si'] = '';
				$tvars['vars']['loginza_account'] = '';
			}
			else {
				$tvars['regx']['/\[if-loginza\](.*?)\[\/if-loginza\]/si'] = '$1';
				$tvars['vars']['loginza_account'] = $SQLrow['loginza_id'];
			}
			*/
		}
		function editProfileForm($userID, $SQLrow, &$tvars)
		{
			/*
			if (empty($SQLrow['loginza_id'])) {
				$tvars['regx']['/\[if-loginza\](.*?)\[\/if-loginza\]/si'] = '';
				$tvars['regx']['/\[if-not-loginza\](.*?)\[\/if-not-loginza\]/si'] = '$1';
				$tvars['vars']['loginza_account'] = '';
			}
			else {
				$tvars['regx']['/\[if-loginza\](.*?)\[\/if-loginza\]/si'] = '$1';
				$tvars['regx']['/\[if-not-loginza\](.*?)\[\/if-not-loginza\]/si'] = '';
				$tvars['vars']['loginza_account'] = $SQLrow['loginza_id'];
			}
			*/
		}
		function editProfile($userID, $SQLrow, &$SQLnew)
		{
			global $lang, $config, $mysql;
			$SQLnew['sex'] = secure_html($_REQUEST['editsex']);
			$SQLnew['birthday'] = secure_html($_REQUEST['editbirthday']);
		}
	}
	register_filter('plugin.uprofile', 'auth_social', new uSocialFilter);
}
/**
 * Add to $_FILES from external url
 * sample usage: addAvatarToFiles('google_favicon', 'http://google.com/favicon.ico');
 * @since  17.12.12 17:23
 * @author mekegi
 *
 * @param string $key
 * @param string $url sample http://some.tld/path/to/file.ext
 */
function addAvatarToFiles($key, $url)
{
	$scheme = strtolower(parse_url($url, PHP_URL_SCHEME) ?? '');
	if (!in_array($scheme, ['http', 'https'], true)) {
		return;
	}
	$tempName = tempnam(ini_get('upload_tmp_dir'), 'upload_');
	$originalName = basename(parse_url($url, PHP_URL_PATH));
	$imgRawData = @file_get_contents($url);
	if ($imgRawData === false) {
		return;
	}
	file_put_contents($tempName, $imgRawData);
	$info = @getimagesize($tempName);
	if (!$info || empty($info['mime'])) {
		@unlink($tempName);
		return;
	}
	$_FILES[$key] = array(
		'name'     => $originalName,
		'type'     => $info['mime'],
		'tmp_name' => $tempName,
		'error'    => 0,
		'size'     => strlen($imgRawData),
	);
	//return $_FILES[$key];
}
