1<?php
2/* Copyright (c) 1998-2016 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4/**
5 * Class ilChatroomConfigFileHandler
6 * @package Modules\Chatroom\classes
7 * @author  Thomas Joußen <tjoussen@databay.de>
8 * @since   19.02.16
9 * @version $Id$
10 */
11class ilChatroomConfigFileHandler
12{
13    const CHATROOM_DATA_DIR = '/chatroom/';
14    const CHATROOM_CLIENT_CONFIG_FILENAME = 'client.cfg';
15    const CHATROOM_SERVER_CONFIG_FILENAME = 'server.cfg';
16
17    /**
18     * Creates a client config file and saves it to the chatroom data directory
19     * @param array $settings
20     * @throws Exception
21     */
22    public function createClientConfigFile(array $settings)
23    {
24        $content = $this->getClientFileContent($settings);
25        $this->writeDataToFile($content, self::CHATROOM_CLIENT_CONFIG_FILENAME);
26    }
27
28    /**
29     * Get the client config file content as json encoded string
30     * @param array $settings
31     * @return string
32     */
33    protected function getClientFileContent(array $settings)
34    {
35        global $DIC;
36
37        // Dirty configuration swap: Ilias differentiates between InnoDB and MyISAM.
38        // MyISAM is configured as mysql, InnoDB as innodb.
39        // The client config file only needs information about driver not engine
40        $type = $a_type = $DIC['ilClientIniFile']->readVariable('db', 'type');
41        if (in_array($type, array(
42            ilDBConstants::TYPE_MYSQL,
43            ilDBConstants::TYPE_INNODB,
44            ilDBConstants::TYPE_PDO_MYSQL_INNODB,
45            ilDBConstants::TYPE_PDO_MYSQL_MYISAM,
46            ''
47        ))) {
48            $type = 'mysql';
49        }
50
51        $settings['database'] = array(
52            'type' => $type,
53            'host' => $DIC['ilClientIniFile']->readVariable('db', 'host'),
54            'port' => (int) $DIC['ilClientIniFile']->readVariable('db', 'port'),
55            'name' => $DIC['ilClientIniFile']->readVariable('db', 'name'),
56            'user' => $DIC['ilClientIniFile']->readVariable('db', 'user'),
57            'pass' => $DIC['ilClientIniFile']->readVariable('db', 'pass')
58        );
59
60        return json_encode($settings, JSON_PRETTY_PRINT);
61    }
62
63    /**
64     * Writes $content to file named by $filename
65     * @param string $content
66     * @param string $filename
67     * @throws Exception
68     */
69    protected function writeDataToFile($content, $filename)
70    {
71        $path = $this->createDataDirIfNotExists();
72        $handle = fopen($path . $filename, 'w');
73
74        if (!fwrite($handle, $content)) {
75            throw new Exception('Cannot write to file');
76        }
77
78        fclose($handle);
79    }
80
81    /**
82     * Creates a data directory for configuration files, if the directory does not already exists.
83     * @return string
84     * @throws Exception Throws Exception if data dir creation failed
85     */
86    protected function createDataDirIfNotExists()
87    {
88        $path = ilUtil::getDataDir() . self::CHATROOM_DATA_DIR;
89
90        if (!file_exists($path)) {
91            if (!ilUtil::makeDir($path)) {
92                throw new Exception('Directory cannot be created');
93            }
94        }
95
96        return $path;
97    }
98
99    /**
100     * Creates a server config file and saves it to the chatroom data directory
101     * @param array $settings
102     * @throws Exception
103     */
104    public function createServerConfigFile(array $settings)
105    {
106        $content = $this->getServerFileContent($settings);
107        $this->writeDataToFile($content, self::CHATROOM_SERVER_CONFIG_FILENAME);
108    }
109
110    /**
111     * Get the server config file contetn as json encoded string
112     * @param array $settings
113     * @return string
114     */
115    protected function getServerFileContent(array $settings)
116    {
117        unset($settings['ilias_proxy']);
118        unset($settings['client_proxy']);
119        unset($settings['ilias_url']);
120        unset($settings['client_url']);
121
122        return json_encode($settings, JSON_PRETTY_PRINT);
123    }
124}
125