Keep session alive

This will send an asynchronous request to the server via AJAX on an interval in secs

@param   array   $config An optional array with configuration options
@return string    The html output
557817
public function keepalive($config = array())
    {
        $config = new KObjectConfigJson($config);
        $config->append(array(
            'refresh' => 15 * 60, //default refresh is 15min
            'url'     => '',      //default to window.location.url
        ));

        $html = '';

        // Only load once
        if (!isset(static::$_loaded['keepalive']))
        {
            $session = $this->getObject('user')->getSession();
            if($session->isActive())
            {
                //Get the config session lifetime (in seconds)
                $lifetime = $session->getLifetime();

                //Refresh time is 1 minute less than the lifetime
                $refresh =  ($lifetime <= 60) ? 30 : $lifetime - 60;
            }
            else $refresh = (int) $config->refresh;

            // Longest refresh period is one hour to prevent integer overflow.
            if ($refresh > 3600 || $refresh <= 0) {
                $refresh = 3600;
            }

            if(empty($config->url)) {
                $url = 'window.location.href';
            } else {
                $url = "'.$config->url.'";
            }

            // Build the keep alive script.
            $html  = $this->jquery();
            $html .=
                "<script>
                (function($){
                    var refresh = '" . $refresh . "';
                    setInterval(function() {
                        $.ajax({
                            url: $url,
                            method: 'HEAD',
                            cache: false
                        })
                    }, refresh * 1000);
                })(kQuery);</script>";

            static::$_loaded['keepalive'] = true;
        }
        return $html;
    }
1111848
private function getSessionStorage(array $config)
    {
        $app = $this->app;

        // If we're running through command line, just early return an in-memory storage
        if ($app->isRunThroughCommandLineInterface()) {
            return $app->make(MockArraySessionStorage::class);
        }

        // Resolve the handler based on config
        $handler = $this->getSessionHandler($config);
        $storage = $app->make(NativeSessionStorage::class, [[], $handler]);

        // Initialize the storage with some options
        $options = array_get($config, 'cookie', []);
        $options['gc_maxlifetime'] = array_get($config, 'max_lifetime');

        if (array_get($options, 'cookie_path', false) === false) {
            $options['cookie_path'] = $app['app_relative_path'] . '/';
        }

        $storage->setOptions($options);

        return $app->make(Storage\LoggedStorage::class, [$storage]);
    }
1051558
private function getDsn(array $config)
    {
        $host = isset($config['host']) ? "host={$config['host']};" : '';
        $dsn  = "pgsql:{$host}dbname={$config['database']}";

        if (isset($config['port'])) {
            $dsn .= ";port={$config['port']}";
        }
        if (isset($config['sslmode'])) {
            $dsn .= ";sslmode={$config['sslmode']}";
        }
        if (isset($config['sslcert'])) {
            $dsn .= ";sslcert={$config['sslcert']}";
        }
        if (isset($config['sslkey'])) {
            $dsn .= ";sslkey={$config['sslkey']}";
        }
        if (isset($config['sslrootcert'])) {
            $dsn .= ";sslrootcert={$config['sslrootcert']}";
        }

        return $dsn;
    }
1364783
public function load($config = array())
    {
        $config = new KObjectConfigJson($config);
        $config->append(array(
            'debug' => JFactory::getApplication()->getCfg('debug'),
            'wrapper_class' => array(
                JFactory::getLanguage()->isRtl() ? 'k-ui-rtl' : 'k-ui-ltr'
            )
        ));
        
        if($this->getTemplate()->decorator() === 'koowa')
        {
            $layout = $this->getTemplate()->getParameters()->layout;

            if (JFactory::getApplication()->isSite() && $layout === 'form') {
                $config->domain = 'admin';
            }
        }

        $identifier = $this->getTemplate()->getIdentifier();
        $menu       = JFactory::getApplication()->getMenu()->getActive();

        if ($identifier->type === 'com' && $menu)
        {
            if ($suffix = htmlspecialchars($menu->params->get('pageclass_sfx')))
            {
                $config->append(array(
                    'wrapper_class' => array($suffix)
                ));
            }
        }

        $html = parent::load($config);

        return $html;
    }
818983
public function search($config = array())
    {
        $config = new KObjectConfigJson($config);
        $config->append(array(
            'search'          => null,
            'submit_on_clear' => true,
            'placeholder'     => $this->getObject('translator')->translate('Find by title or description&hellip;')
        ));

        $html = '';

        if ($config->submit_on_clear)
        {
            $html .= $this->getTemplate()->helper('behavior.jquery');
            $html .= '
            <script>
            (function() {
            var value = '.json_encode($config->search).',
                submitForm = function(form) {
                    if (form.length) {
                        var controller = form.data("controller");
                        if (typeof controller === "object" && controller !== null
                                && controller.grid && typeof controller.grid.uncheckAll !== "undefined") {
                            controller.grid.uncheckAll();
                        }
    
                        form[0].submit();
                    }
                },
                send = function(event) {
                    var v = kQuery(this).val();

                    if (v) {
                        kQuery(".k-search__empty").addClass("k-is-visible");
                    } else {
                        kQuery(".k-search__empty").removeClass("k-is-visible");
                    }

                    if (event.which === 13 || (event.type === "blur" && (v || value) && v != value)) {
                        event.preventDefault();
                        submitForm(kQuery(this).parents("form"));
                    }
                };

            kQuery(function($) {
                $(".k-search__field").keypress(send).blur(send);
                var empty_button = $(".k-search__empty");
                
                if (value) {
                    empty_button.addClass("k-is-visible");
                }
                
                empty_button.click(function(event) {
                    event.preventDefault();

                    var input = $(this).siblings("input");
                    input.val("");

                    if (value) {
                        submitForm(input.parents("form"));
                    }
                });
            });
            })();
            </script>';
        }

        $html .= '<div class="k-search k-search--has-both-buttons">';
        $html .= '<label for="k-search-input">' . $this->getObject('translator')->translate('Search') . '</label>';
        $html .= '<input id="k-search-input" type="search" name="search" class="k-search__field" placeholder="'.$config->placeholder.'" value="'.$this->getTemplate()->escape($config->search).'" />';
        $html .= '<button type="submit" class="k-search__submit">';
        $html .= '<span class="k-icon-magnifying-glass" aria-hidden="true"></span>';
        $html .= '<span class="k-visually-hidden">' . $this->getObject('translator')->translate('Search') . '</span>';
        $html .= '</button>';
        $html .= '<button type="button" class="k-search__empty">';
        $html .= '<span class="k-search__empty-area">';
        $html .= '<span class="k-icon-x" aria-hidden="true"></span>';
        $html .= '<span class="k-visually-hidden">' . $this->getObject('translator')->translate('Clear search') . '</span>';
        $html .= '</span>';
        $html .= '</button>';
        $html .= '</div>';



        return $html;
    }
266207
public function getJs(Array $config = array())
    {
        $default = array(
            'container'             =>  '.container',
            'item'                  =>  '.item',
            'pagination'            =>  '.pagination',
            'next'                  =>  '.next a',
            'loader'                =>  'Loading ...',
            'triggerPageThreshold'  =>  5,
        );

        $data['config'] = array_merge($default, $config);

        return $this->viewLoader->load('infiniteScroll.php', $data);
    }
1291547
public function connect(array $config)
    {
        $options = $this->getOptions($config);

        return $this->createConnection($this->getDsn($config), $config, $options);
    }
345306
public function make(array $config): Client
    {
        $config = $this->getConfig($config);

        return $this->getClient($config);
    }
236965
protected function cookie($config)
    {
        $minutes = (!$config['expire_on_close']) ? $config['lifetime'] : 0;

        Cookie::put($config['cookie'], $this->session['id'], $minutes, $config['path'], $config['domain'],
            $config['secure']);
    }
152380
public function connect(array $config)
    {
        $queue = new RedisQueue(
            $this->redis, $config['queue'], array_get($config, 'connection', $this->connection)
        );

        $queue->setExpire(array_get($config, 'expire', 60));

        return $queue;
    }
193632
public function dialog($config = array())
    {
        $html  = $this->createHelper('behavior')->modal();
        $html .= $this->command($config);

        return $html;
    }
434524
protected function checkConfig(array $config): void
    {
        foreach (['host', 'username', 'password', 'database', 'persistent'] as $key) {
            if (!isset($config[$key])) {
                $class = get_class($this);
                throw new ConfigException("Missing configuration key={$key} in {$class}");
            }
        }
    }
176436
public function init($config = [])
    {

        if (isset($config['session_cache'])) {
            $session_type = $config['session_cache'];
        } else {
            $session_type = $config['cache_type'];
        }

        /**
         * initialize the current Cache
         */
        $param              = new \stdClass();
        $param->type        = (string)$session_type;
        $param->credentials = (($s = Config::get('session_cache_settings') == '') ? Config::get('cache_settings') : $s);
        // write it to the cache
        $this->engine = CacheManager::getInstance($param);

        if (!$this->engine || !$this->engine->isConnected()) return false;

        return true;
    }
876534
public function setHandler(array $config)
    {
        $connect = $config['connect'] ?? [];

        switch (strtolower($config['handler'])) {
            case 'redis':
                $class = Handler\Redis::class;
                $conn = Redis::client($connect);
                break;

            case 'simple':
            case 'simplecache':
                $class = Handler\SimpleCache::class;
                $conn = Cache::simple($connect);
                break;

            case 'cache':
                $class = Handler\Cache::class;
                $conn = Cache::pool($connect);
                break;

            case 'db':
                $class = Handler\Database::class;
                $conn = Database::pdo($connect);
                break;

            default:
                return;
        }

        $settings = $config['settings'] ?? [];
        if (!isset($settings['lifetime']) && isset($this->cookieParams['lifetime'])) {
            $settings['lifetime'] = $this->cookieParams['lifetime'];
        }

        $this->handler = new $class($conn, $settings);
    }
599812
protected function getInterval(array $config)
    {
        if (isset($config['interval'])) {
            if (!is_int($config['interval']) || $config['interval'] <= 0) {
                throw new \DomainException(
                    'interval must reference a positive integer value',
                    self::ERR_INTERVAL_INVALID
                );
            }
            return $config['interval'];
        }
        return 300; // default to 5 minutes
    }
883139
public function formatConfig(array $config)
    {
        if (!isset($config['redirectUrl'])) {
//            dd($config);
            $config['redirectUrl'] = $this->formatUrl(value($config['redirect']));;
        }

        if (!isset($config['callbackUrl'])) {
            $config['callbackUrl'] = $this->formatUrl(value($config['callback']));
        }

        return $config;
    }
602526
public static function load(array $config = [])
    {
        isset($config['sign']) && is_string($config['sign']) ? self::setSign($config['sign']) : null;
        isset($config['default_agent']) && is_string($config['default_agent']) ? self::setDefaultAgent($config['default_agent']) : null;
        isset($config['spare_agents']) && is_array($config['spare_agents']) ? self::setSpareAgents($config['spare_agents']) : null;
        isset($config['agents']) && is_array($config['agents']) ? self::setAgents($config['agents']) : null;
        isset($config['timeout']) && is_int($config['timeout']) ? self::setTimeout($config['timeout']) : null;
        isset($config['connect_timeout']) && is_int($config['connect_timeout']) ? self::setConnectTimeout($config['connect_timeout']) : null;
        isset($config['agent_ext_namespace']) && is_string($config['agent_ext_namespace']) ? self::setAgentExtNamespace($config['agent_ext_namespace']) : null;
    }