File: Pluginss.php

package info (click to toggle)
kalkun 0.8.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 7,340 kB
  • sloc: php: 30,659; javascript: 30,443; sql: 961; sh: 766; xml: 105; makefile: 40
file content (163 lines) | stat: -rw-r--r-- 3,981 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
<?php
/**
 * Kalkun
 * An open source web based SMS Management
 *
 * @package		Kalkun
 * @author		Kalkun Dev Team
 * @license		https://spdx.org/licenses/GPL-2.0-or-later.html
 * @link		https://kalkun.sourceforge.io/
 */

// ------------------------------------------------------------------------

/**
 * Pluginss Class
 *
 * @package		Kalkun
 * @subpackage	Plugin
 * @category	Controllers
 */
class Pluginss extends MY_Controller {

	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function __construct()
	{
		parent::__construct();

		// Prevent non-admin user
		if ($this->session->userdata('level') !== 'admin')
		{
			$this->session->set_flashdata('notif', tr_raw('Access denied. Only administrators are allowed to manage plugins.'));
			redirect('/');
		}

		$this->load->library('Plugins_lib_kalkun');
		$this->load->model('Plugins_kalkun_model');

		$this->plugins_lib_kalkun->restore_orphaned_plugins();
		$this->plugins_lib_kalkun->update_all_plugin_headers();
	}

	// --------------------------------------------------------------------

	/**
	 * Index
	 *
	 * Display list of all plugin
	 *
	 * @access	public
	 */
	function index($type = 'installed')
	{
		$this->load->helper('form');
		$plugins_lib = $this->plugins_lib_kalkun;
		$data['main'] = 'main/plugin/index';
		$data['title'] = 'Plugins';
		$data['plugins'] = array();
		$data['type'] = $type;
		if ($type === 'installed')
		{
			$data['title'] .= ' - '.tr_raw('Installed', 'Plural');
			foreach ($this->Plugins_kalkun_model->get_plugins() as $key => $plugin)
			{
				if ($plugin->status & $plugins_lib::P_STATUS_MISSING)
				{
					continue;
				}
				if ($plugin->status & $plugins_lib::P_STATUS_ENABLED)
				{
					$data['plugins'][$key] = $plugin;
					$data['plugins'][$key]->controller_has_index = self::_plugin_controller_has_index($plugin->system_name);
				}
			}
		}
		else
		{
			$data['title'] .= ' - '.tr_raw('Available', 'Plural');
			foreach ($this->Plugins_kalkun_model->get_plugins() as $key => $plugin)
			{
				if ($plugin->status & $plugins_lib::P_STATUS_MISSING)
				{
					continue;
				}
				if ( ! ($plugin->status & $plugins_lib::P_STATUS_ENABLED))
				{
					$data['plugins'][$key] = $plugin;
				}
			}
		}
		uasort($data['plugins'], array($this, '_plugins_cmp_plugin_name'));
		$this->load->view('main/layout', $data);
	}

	// --------------------------------------------------------------------

	/**
	 * Install
	 *
	 * Install a plugin
	 *
	 * @access	public
	 */
	function install($plugin_name)
	{
		$this->plugins_lib_kalkun->enable_plugin($plugin_name);
		$this->session->set_flashdata('notif', tr_raw('Plugin {0} installed successfully.', NULL, $plugin_name));
		redirect('pluginss');
	}

	// --------------------------------------------------------------------

	/**
	 * Uninstall
	 *
	 * Uninstall a plugin
	 *
	 * @access	public
	 */
	function uninstall($plugin_name)
	{
		if (array_key_exists($plugin_name, Plugins_lib_kalkun::$enabled_plugins))
		{
			$this->plugins_lib_kalkun->disable_plugin($plugin_name);
			$this->session->set_flashdata('notif', tr_raw('Plugin {0} uninstalled successfully.', NULL, $plugin_name));
		}
		redirect('pluginss');
	}

	// --------------------------------------------------------------------

	/**
	 * Callback function used to order the array of plugins by plugin name
	 */
	function _plugins_cmp_plugin_name($p1, $p2)
	{
		return strcasecmp ($p1->name, $p2->name);
	}

	// --------------------------------------------------------------------

	/**
	 * Check if the controller of the plugin has a 'index()' method
	 */
	static function _plugin_controller_has_index($plugin_system_name)
	{
		$controller_class = ucfirst($plugin_system_name);
		$controller_path = APPPATH . 'plugins/'.$plugin_system_name.'/controllers/'.$controller_class.'.php';

		if ( ! file_exists($controller_path))
		{
			return FALSE;
		}

		require_once $controller_path;
		$rc = new ReflectionClass($controller_class);

		return $rc->hasMethod('index');
	}
}