From 0aa7f7b71176b5b2bceb7ac96d91a5f2ef09a564 Mon Sep 17 00:00:00 2001
From: akshath <akshath.t@vtiger.com>
Date: Tue, 3 Nov 2020 16:29:49 +0530
Subject: [PATCH] Fix for #1535 - Provide hooks to plugin custom connectors and
 avoid modification in core files

---
 config_override.php          |  8 +++++++
 include/utils/utils.php      |  1 +
 includes/http/Session.php    | 10 ++++++++
 includes/runtime/Cache.php   |  4 +++-
 includes/runtime/Configs.php | 44 ++++++++++++++++++++++++++++++++++++
 5 files changed, 66 insertions(+), 1 deletion(-)
 create mode 100644 includes/runtime/Configs.php

diff --git a/config_override.php b/config_override.php
index b0c374e6f..458b5a665 100644
--- a/config_override.php
+++ b/config_override.php
@@ -10,3 +10,11 @@
 
 //Maximum number of Mailboxes in mail converter
 $max_mailboxes = 3;
+
+/**
+ * Configure runtime connectors to customization in core files.
+ * Ex: Sessions are currently handled by PHP default session handler. 
+ *     This can be customized using runtime connector hook and avoid core file modifications.
+ *     array('session' => 'Vtiger_CustomSession_Handler')
+ */
+$runtime_connectors = array();
\ No newline at end of file
diff --git a/include/utils/utils.php b/include/utils/utils.php
index a5a4e1b4a..3f69293f8 100755
--- a/include/utils/utils.php
+++ b/include/utils/utils.php
@@ -40,6 +40,7 @@ require_once 'vtlib/Vtiger/Deprecated.php';
 require_once 'includes/runtime/Cache.php';
 require_once 'modules/Vtiger/helpers/Util.php';
 require_once 'vtlib/Vtiger/AccessControl.php';
+require_once 'includes/runtime/Configs.php';
 // Constants to be defined here
 
 // For Migration status.
diff --git a/includes/http/Session.php b/includes/http/Session.php
index 789812e98..2c8257059 100644
--- a/includes/http/Session.php
+++ b/includes/http/Session.php
@@ -8,6 +8,16 @@
  * All Rights Reserved.
  ************************************************************************************/
 
+/**
+ * Override default user-session storage functions if custom session connector exist.
+ */
+$runtime_configs = Vtiger_Runtime_Configs::getInstance();
+$custom_session_handlerclass = $runtime_configs->getConnector('session');
+if($custom_session_handlerclass) {
+	$handler = $custom_session_handlerclass::getInstance();
+	session_set_save_handler($handler, true);
+}
+
 // Import dependencies
 include_once 'libraries/HTTP_Session2/HTTP/Session2.php';
 
diff --git a/includes/runtime/Cache.php b/includes/runtime/Cache.php
index e89b800e9..8f0f544b4 100644
--- a/includes/runtime/Cache.php
+++ b/includes/runtime/Cache.php
@@ -17,7 +17,9 @@ class Vtiger_Cache {
 	protected $connector;
 
 	private function __construct() {
-		$this->connector = Vtiger_Cache_Connector::getInstance();
+		$runtime_configs = Vtiger_Runtime_Configs::getInstance();
+		$connector_class = $runtime_configs->getConnector('cache', 'Vtiger_Cache_Connector');
+		$this->connector = $connector_class::getInstance();
 	}
 
 	public static function getInstance(){
diff --git a/includes/runtime/Configs.php b/includes/runtime/Configs.php
new file mode 100644
index 000000000..6c7e44613
--- /dev/null
+++ b/includes/runtime/Configs.php
@@ -0,0 +1,44 @@
+<?php
+/*+***********************************************************************************
+ * The contents of this file are subject to the vtiger CRM Public License Version 1.0
+ * ("License"); You may not use this file except in compliance with the License
+ * The Original Code is:  vtiger CRM Open Source
+ * The Initial Developer of the Original Code is vtiger.
+ * Portions created by vtiger are Copyright (C) vtiger.
+ * All Rights Reserved.
+ *************************************************************************************/
+
+ require_once 'includes/runtime/BaseModel.php';
+
+ class Vtiger_Runtime_Configs extends Vtiger_Base_Model {
+
+    private static $instance = false;
+
+    public static function getInstance() {
+        if(self::$instance === false) {
+            self::$instance = new self();
+        }
+
+        return self::$instance;
+    }
+
+    /**
+     * Function to fetch runtime connectors configured in config_override.php
+     * @params $identifier - Connector identifier Ex: session
+     * @params $default - Default connector class name.
+     */
+    public function getConnector($identifier, $default = '') {
+        global $runtime_connectors;
+
+        $connector = '';
+        if(isset($runtime_connectors[$identifier])) {
+            $connector = $runtime_connectors[$identifier];
+        }
+
+        if(empty($connector) && !empty($default)) {
+            $connector = $default;
+        }
+
+        return $connector;
+    }
+ }
\ No newline at end of file
-- 
GitLab