TikiManager.php 11.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php

// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$

class Tracker_Field_TikiManager extends Tracker_Field_Abstract
{
    public static function getTypes()
    {
        return [
            'TM' => [
                'name'        => tr('Tiki Manager'),
                'description' => tr('Integrate Tiki Manager commands in tracker items.'),
                'prefs'       => ['trackerfield_tikimanager'],
                'tags'        => ['experimental'],
                'help'        => 'Manager',
                'default'     => 'n',
                'params'      => [
                    'showactions' => [
                        'name' => tra('Show actions'),
24
                        'description' => tra('Comma-separated list of actions shown in the interface. If none are listed, all actions will be available by default.'),
25
26
27
28
29
30
                        'default' => '',
                        'filter' => 'text',
                        'separator' => ',',
                    ],
                    'hideactions' => [
                        'name' => tra('Hide actions'),
31
                        'description' => tra('Comma-separated list of actions hidden from the interface. If none are listed, all actions will be available by default.'),
32
33
34
35
                        'default' => '',
                        'filter' => 'text',
                        'separator' => ',',
                    ],
36
37
38
39
40
41
                    'source' => [
                        'name' => tra('Virtualmin server'),
                        'description' => tra('If you have a remotely managed virtualmin server defined in DSN/Content Authentication section, you can specify it here to allow automatic subdomain creation and specify just the template url parameter below. '),
                        'default' => '',
                        'filter' => 'text',
                    ],
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
                    'newInstanceType' => [
                        'name' => tra('New Instance Type'),
                        'description' => tra('Type of instance when creating a new one.'),
                        'default' => '',
                        'filter' => 'alpha',
                        'options'      => [
                            'ftp' => tr('FTP'),
                            'local' => tr('Local'),
                            'ssh' => tr('SSH'),
                        ],
                    ],
                    'newInstanceHost' => [
                        'name' => tra('New Instance Host'),
                        'description' => tra('SSH or FTP host'),
                        'default' => '',
                        'filter' => 'text',
                    ],
                    'newInstancePort' => [
                        'name' => tra('New Instance Port'),
                        'description' => tra('SSH or FTP host, e.g. 22 or 21.'),
                        'default' => '',
                        'filter' => 'int',
                    ],
                    'newInstanceUser' => [
                        'name' => tra('New Instance User'),
                        'description' => tra('SSH or FTP user'),
                        'default' => '',
                        'filter' => 'text',
                    ],
                    'newInstancePass' => [
                        'name' => tra('New Instance Pass'),
                        'description' => tra('FTP password'),
                        'default' => '',
                        'filter' => 'text',
                    ],
                    'newInstanceTemplateUrl' => [
                        'name' => tra('New Instance Template Url'),
                        'description' => tra('This will be used as a template for created instances web url. {slug} string will be replaced with the instance unique identifier. Possible formats: https://{slug}.domain.com or https://domain.com/{slug}/'),
                        'default' => '',
                        'filter' => 'text',
                    ],
                    'newInstanceWebroot' => [
                        'name' => tra('New Instance Web Root'),
                        'description' => tra('Local filesystem path to the instance webroot. {slug} string will be replaced with the instance unique identifier. E.g. /var/www/virtual/{slug}.domain.com/html/'),
                        'default' => '',
                        'filter' => 'text',
                    ],
                    'newInstanceTempdir' => [
                        'name' => tra('New Instance Work Dir'),
                        'description' => tra('Local filesystem path to the instance temp directory. {slug} string will be replaced with the instance unique identifier. E.g. /tmp/tiki_mgr_{slug}'),
                        'default' => '',
                        'filter' => 'text',
                    ],
                    'newInstanceBackupUser' => [
                        'name' => tra('New Instance Backup User'),
                        'description' => tra('Owner of the backup files for this instance.'),
                        'default' => '',
                        'filter' => 'text',
                    ],
                    'newInstanceBackupGroup' => [
                        'name' => tra('New Instance Backup Group'),
                        'description' => tra('Group owner of the backup files for this instance.'),
                        'default' => '',
                        'filter' => 'text',
                    ],
                    'newInstanceBackupPerms' => [
                        'name' => tra('New Instance Backup Perms'),
                        'description' => tra('Backup files permission mask. E.g. 755'),
                        'default' => '',
                        'filter' => 'int',
                    ],
                    'newInstanceDbHost' => [
                        'name' => tra('New Instance DB Host'),
                        'description' => tra('Database host'),
                        'default' => '',
                        'filter' => 'text',
                    ],
                    'newInstanceDbUser' => [
                        'name' => tra('New Instance DB User'),
                        'description' => tra('Database user with permissions to create other users and databases'),
                        'default' => '',
                        'filter' => 'text',
                    ],
                    'newInstanceDbPass' => [
                        'name' => tra('New Instance DB Pass'),
                        'description' => tra('Password for database user'),
                        'default' => '',
                        'filter' => 'text',
                    ],
                    'newInstanceDbPrefix' => [
                        'name' => tra('New Instance DB Prefix'),
                        'description' => tra('Newly created databases and users will be prefixed by this string.'),
                        'default' => '',
                        'filter' => 'text',
                    ],
                ],
            ],
        ];
    }

    public function getFieldData(array $requestData = [])
    {
        global $prefs;

        $ret = [];

        if ($prefs['feature_tiki_manager'] !== 'y') {
            $ret['error'] = tra('Tiki Manager feature not enabled.');
            return $ret;
        }

        if (! class_exists('TikiManager\Config\Environment')) {
            $ret['error'] = tr('Tiki Manager not found. Please check if it is installed from Admin->Packages.');
            return $ret;
        }

        try {
            $utilities = new Services_Manager_Utilities;
            $utilities->loadEnv();
        } catch (Exception $e) {
            $ret['error'] = $e->getMessage();
            return $ret;
        }

        $manager_output = $utilities->getManagerOutput();

168
        $instanceIds = explode(',', $this->getValue());
169
170
171
172
        $showactions = array_filter($this->getOption('showactions', []));
        $hideactions = array_filter($this->getOption('hideactions', []));

        $instances = TikiManager\Application\Instance::getInstances(false);
173
174
        $instances = array_filter($instances, function($i) use ($instanceIds) {
            return empty($instanceIds) || in_array($i->getId(), $instanceIds);
175
176
177
        });

        // taken from Tiki manager available commands, TODO: hook these up with the interface
178
        $available_actions = ['access', 'backup', 'blank', 'check', 'clone', 'cloneandupgrade', 'console', 'copysshkey', 'create', 'delete', 'detect', 'edit', 'fixpermissions', 'import', 'list', 'maintenance', 'patch_apply', 'patch_delete', 'patch_list', 'profile_apply', 'restore', 'revert', 'setup-scheduler-cron', 'stats', 'update', 'upgrade', 'watch', 'info', 'verify'];
179
180
181
182
183
184
185
186
187
188
189
        if ($showactions) {
            $available_actions = array_filter($available_actions, function($action) use ($showactions) {
                return in_array($action, $showactions);
            });
        }
        if ($hideactions) {
            $available_actions = array_filter($available_actions, function($action) use ($hideactions) {
                return ! in_array($action, $hideactions);
            });
        }

190
        $versions = Services_Manager_Utilities::getAvailableTikiVersions();
191
192
193
194
195
196
197
198

        $ret = [
            'id' => $this->getItemId(),
            'error' => '',
            'instances' => $instances,
            'available_actions' => $available_actions,
            'manager_output' => $manager_output->fetch(),
            'versions' => $versions,
199
            'source' => $this->getOption('source'),
200
            'value' => $this->getValue() ? $this->getValue() : 'none', // this is required to show the field, otherwise it gets hidden if tracker is set to doNotShowEmptyField
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
        ];

        return $ret;
    }

    public function renderInput($context = [])
    {
        TikiLib::lib('header')->add_cssfile('themes/base_files/feature_css/tiki-manager.css');
        return $this->renderTemplate('trackerinput/tikimanager.tpl', $context);
    }

    public function renderOutput($context = [])
    {
        TikiLib::lib('header')->add_cssfile('themes/base_files/feature_css/tiki-manager.css');
        return $this->renderTemplate('trackerinput/tikimanager.tpl', $context);
    }
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243

    public function addValue($instanceId)
    {
        $instances = explode(',', $this->getValue());
        if (! in_array($instanceId, $instances)) {
            $instances[] = $instanceId;
        }
        $result = implode(',', array_filter($instances, function($i) {
            return is_numeric($i);
        }));
        return $result ? $result : 'none';
    }

    public function removeValue($instanceId)
    {
        $instances = explode(',', $this->getValue());
        if (in_array($instanceId, $instances)) {
            $instances[] = $instanceId;
        }
        $instances = array_filter($instances, function ($i) use ($instanceId) {
            return $i != $instanceId;
        });
        $result = implode(',', array_filter($instances, function($i) {
            return is_int($i);
        }));
        return $result ? $result : 'none';
    }
244
}