From 31e78052a79b5ee5fa7885cde2c9b9bc280d3043 Mon Sep 17 00:00:00 2001
From: Prasad <prasad@vtiger.com>
Date: Fri, 21 Jun 2024 21:21:37 +0530
Subject: [PATCH] Fixed #1892: Add custom skin selection during login if
 present.

---
 includes/runtime/Theme.php         | 19 +++++++++++++++++--
 layouts/v7/custom_skins/.gitkeep   |  0
 layouts/v7/modules/Users/Login.tpl | 20 +++++++++++++++++---
 modules/Users/actions/Login.php    |  1 +
 4 files changed, 35 insertions(+), 5 deletions(-)
 delete mode 100644 layouts/v7/custom_skins/.gitkeep

diff --git a/includes/runtime/Theme.php b/includes/runtime/Theme.php
index 630650b53..212876d45 100644
--- a/includes/runtime/Theme.php
+++ b/includes/runtime/Theme.php
@@ -65,7 +65,18 @@ class Vtiger_Theme extends Vtiger_Viewer {
 	 * @return <string> - theme folder
 	 */
 	public static function getBaseThemePath(){
-		return 'layouts'. '/' . self::getLayoutName(). '/'. self::DEFAULTSKIN;
+		$basepath = 'layouts'. '/' . self::getLayoutName();
+		$useskin = static::DEFAULTSKIN;
+
+		// do we have any override valid custom skin?
+		$customskin = isset($_SESSION) && isset($_SESSION['authenticated_user_skin']) ?  $_SESSION['authenticated_user_skin'] : '';
+		if ($customskin && !preg_match("/[^a-zA-Z0-9_-]/", $customskin)) { // strict-check to avoid file-inclusion attack.
+			$customskin = "custom_skins/" . $customskin;      // lookup path
+			if (file_exists($basepath . '/' . $customskin)) { // ensure validity
+				$useskin = $customskin;
+			}
+		}
+		return $basepath . '/' . $useskin;
 	}
 
 	/**
@@ -108,7 +119,11 @@ class Vtiger_Theme extends Vtiger_Viewer {
 	 * @return <Array>
 	 */
 	public static function getAllSkins(){
-		return Vtiger_Util_Helper::getAllSkins();
+		$dirs = glob("layouts/". static::DEFAULTLAYOUT . "/custom_skins/*", GLOB_ONLYDIR);
+		if (!empty($dirs)) {
+			$dirs = array_map('basename', $dirs);
+		}
+		return $dirs;
 	}
 
 	/**
diff --git a/layouts/v7/custom_skins/.gitkeep b/layouts/v7/custom_skins/.gitkeep
deleted file mode 100644
index e69de29bb..000000000
diff --git a/layouts/v7/modules/Users/Login.tpl b/layouts/v7/modules/Users/Login.tpl
index bbcb89267..ea2b626d8 100644
--- a/layouts/v7/modules/Users/Login.tpl
+++ b/layouts/v7/modules/Users/Login.tpl
@@ -62,11 +62,14 @@
 		input:focus ~ .bar:before, input:focus ~ .bar:after {
 			width: 50%;
 		}
+		select {
+			font-size: 16px;
+		}
 		#page {
 			padding-top: 86px;
 		}
 		.widgetHeight {
-			height: 410px;
+			height: 460px;
 			margin-top: 20px !important;
 		}
 		.loginDiv {
@@ -230,7 +233,18 @@
 							<span class="bar"></span>
 							<label>Password</label>
 						</div>
-						<div class="group">
+						{assign var="CUSTOM_SKINS" value=Vtiger_Theme::getAllSkins()}
+						{if !empty($CUSTOM_SKINS)}
+						<div class="group" style="margin-bottom: 10px;">
+							<select id="skin" name="skin" placeholder="Skin" style="text-transform: capitalize; width:100%;height:30px;">
+								<option value="">Default Skin</option>
+								{foreach item=CUSTOM_SKIN from=$CUSTOM_SKINS}
+								<option value="{$CUSTOM_SKIN}">{$CUSTOM_SKIN}</option>
+								{/foreach}
+							</select>
+						</div>
+						{/if}
+					<div class="group">
 							<button type="submit" class="button buttonBlue">Sign in</button><br>
 							<a class="forgotPasswordLink" style="color: #15c;">forgot password?</a>
 						</div>
@@ -421,4 +435,4 @@
 			});
 		</script>
 		</div>
-	{/strip}
\ No newline at end of file
+	{/strip}
diff --git a/modules/Users/actions/Login.php b/modules/Users/actions/Login.php
index d46d93716..99f2dd902 100644
--- a/modules/Users/actions/Login.php
+++ b/modules/Users/actions/Login.php
@@ -36,6 +36,7 @@ class Users_Login_Action extends Vtiger_Action_Controller {
 			$_SESSION['authenticated_user_id'] = $userid;
 			$_SESSION['app_unique_key'] = vglobal('application_unique_key');
 			$_SESSION['authenticated_user_language'] = vglobal('default_language');
+			$_SESSION['authenticated_user_skin'] = $request->get('skin');
 
 			//Enabled session variable for KCFINDER 
 			$_SESSION['KCFINDER'] = array(); 
-- 
GitLab