Merge branch 'maint/7.0'
[ninja.git] / application / controllers / ninja.php
blob86876a9db07cae34a46b060ea92d06405f1427d5
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
3 require_once('op5/log.php');
5 /**
6 * Base NINJA controller.
8 * Sets necessary objects like session and database
10 * op5, and the op5 logo are trademarks, servicemarks, registered servicemarks
11 * or registered trademarks of op5 AB.
12 * All other trademarks, servicemarks, registered trademarks, and registered
13 * servicemarks mentioned herein may be the property of their respective owner(s).
14 * The information contained herein is provided AS IS with NO WARRANTY OF ANY
15 * KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY, AND FITNESS FOR A
16 * PARTICULAR PURPOSE.
18 class Ninja_Controller extends Template_Controller {
20 const ADMIN = 'admin'; # how do we define the admin role in database
22 public $session = false;
23 public $template;
24 public $user = false;
25 public $profiler = false;
26 public $inline_js = false;
27 public $js_strings = false;
28 public $stale_data = false;
29 public $run_tests = false;
30 public $notifications_disabled = false;
31 public $checks_disabled = false;
32 public $log = false;
33 public $help_link = false;
34 public $help_link_url = false;
36 public function __construct()
38 $this->log = op5log::instance('ninja');
39 parent::__construct();
40 if(request::is_ajax()) {
41 $this->auto_render = FALSE;
44 $this->run_tests = $this->input->get('run_tests', false) !== false;
46 $this->template = $this->add_view('template');
47 $this->template->css = array();
48 $this->template->js = array();
50 $this->template->global_notifications = array();
51 $this->template->print_notifications = array();
53 if (!$this->run_tests) {
54 $this->profiler = new Profiler;
55 } else if ($this->run_tests !== false) {
56 unittest::instance();
59 # Load default current_skin, can be replaced by Authenticated_Controller if user is logged in.
60 $this->template->current_skin = Kohana::config('config.current_skin');
62 # Load session library
63 # If any current session data exists, it will become available.
64 # If no session data exists, a new session is automatically started
65 $this->session = Session::instance();
67 /**
68 * check for generic sort parameters in GET and store in session
70 # use e.g status/host/all to store sort settings
71 # this will lead to specific sort order for
72 # every <host_name> e.g status/host/<host_name>
73 $sort_key = Router::$current_uri;
75 # The following will instead make all calls to e.g status/host
76 # to behave the same
77 # $sort_key = Router::$controller.'/'.Router::$method;
79 if ($this->input->get('sort_field', false)) {
80 $cur_data = array(
81 'sort_field' => $this->input->get('sort_field', false),
82 'sort_order' => $this->input->get('sort_order', false)
84 $session_sort[$sort_key] = $cur_data;
85 $sort_options = $this->session->get('sort_options', false);
87 $_SESSION['sort_options'][$sort_key] = $cur_data;
90 bindtextdomain('ninja', APPPATH.'/languages');
91 textdomain('ninja');
93 $saved_searches = false;
95 # This should be defined even if not logged in
96 $this->template->help_link = false;
98 if (Auth::instance()->logged_in() && PHP_SAPI !== "cli") {
99 # warning! do not set anything in xlinks, as it isn't working properly
100 # and cannot (easily) be fixed
101 $this->xlinks = array();
102 $this->_addons();
104 # help link
105 # To enable link: create an addon with code:
106 # $this->help_link_url = "http://example.org/$VERSION$/$CONTROLLER$/$METHOD$"
107 if( $this->help_link_url ) {
108 $this->template->help_link = str_replace(
109 array('$VERSION$', '$CONTROLLER$', '$METHOD$'),
110 array_map('urlencode',
111 array(trim(config::get_version_info()), Router::$controller, Router::$method)
113 $this->help_link_url
118 # create the user menu
119 $menu = new Menu_Model();
120 $this->template->links = $menu->create();
122 foreach ($this->xlinks as $link)
123 $this->template->links[$link['category']][$link['title']] = $link['contents'];
125 $this->_global_notification_checks();
127 # fetch info on saved searches and assign to master template
128 $searches = Saved_searches_Model::get_saved_searches();
129 if ($searches !== false && count($searches)) {
130 $this->template->saved_searches = $this->add_view('saved_searches');
131 $this->template->saved_searches->searches = $searches;
135 $items_per_page = arr::search($_GET, 'items_per_page');
136 if ($items_per_page !== false) {
137 $_GET['items_per_page'] = ($items_per_page !== false && $items_per_page < 0)
138 ? ($items_per_page * -1)
139 : (int)$items_per_page;
142 # convert test params to $_REQUEST to enable more
143 # parameters to different controllers (reports for one)
144 if (PHP_SAPI == "cli" && $this->run_tests !== false
145 && !empty($_SERVER['argc']) && isset($_SERVER['argv'][1])) {
146 $params = $_SERVER['argv'][1];
147 if (strstr($params, '?')) {
148 $params = explode('?', $params);
149 parse_str($params[1], $_REQUEST);
154 public function add_global_notification( $notification ) {
155 if (!is_array($notification)) {
156 $notification = array($notification);
158 $this->template->global_notifications[] = $notification;
161 public function add_print_notification($notification) {
162 $this->template->print_notifications[] = $notification;
166 * Check for notifications to be displayed to user
167 * Each notification should be an array with (text, link)
169 public function _global_notification_checks()
171 try {
172 $status = StatusPool_Model::status();
173 if($status) {
174 // we've got access
175 if (!$status->get_enable_notifications()) {
176 $this->add_global_notification( html::anchor('extinfo/show_process_info', _('Notifications are disabled')) );
178 if (!$status->get_execute_service_checks()) {
179 $this->add_global_notification( html::anchor('extinfo/show_process_info', _('Service checks are disabled')) );
181 if (!$status->get_execute_host_checks()) {
182 $this->add_global_notification( html::anchor('extinfo/show_process_info', _('Host checks are disabled')) );
184 if (!$status->get_process_performance_data()) {
185 $this->add_global_notification( html::anchor('extinfo/show_process_info', _('Performance data processing are disabled')) );
187 if (!$status->get_accept_passive_service_checks()) {
188 $this->add_global_notification( html::anchor('extinfo/show_process_info', _('Passive service checks are disabled')) );
190 if (!$status->get_accept_passive_host_checks()) {
191 $this->add_global_notification( html::anchor('extinfo/show_process_info', _('Passive host checks are disabled')) );
193 if (!$status->get_enable_event_handlers()) {
194 $this->add_global_notification( html::anchor('extinfo/show_process_info', _('Event handlers disabled')) );
196 if (!$status->get_enable_flap_detection()) {
197 $this->add_global_notification( html::anchor('extinfo/show_process_info', _('Flap detection disabled')) );
200 unset($status);
203 catch( LivestatusException $e ) {
204 $this->add_global_notification( _('Livestatus is not accessable') );
206 catch( ORMException $e ) {
207 $this->add_global_notification( _('Livestatus is not accessable') );
209 # check permissions
210 $user = Auth::instance()->get_user();
211 if (nacoma::link()===true && $user->authorized_for('configuration_information')
212 && $user->authorized_for('system_commands') && $user->authorized_for('host_view_all')) {
213 $nacoma = Database::instance('nacoma');
214 $query = $nacoma->query('SELECT COUNT(id) AS cnt FROM autoscan_results WHERE visibility != 0');
215 $query->result(false);
216 $row = $query->current();
217 if ($row !== false && $row['cnt'] > 0) {
218 $this->add_global_notification( html::anchor('configuration/configure?scan=autoscan_complete', $row['cnt'] . _(' unmonitored hosts present.')) );
225 * Find and include php files from 'addons' found in defined folders
227 protected function _addons()
229 $addons_files = array_merge(
230 glob(APPPATH.'addons/*', GLOB_ONLYDIR),
231 glob(MODPATH.'*/addons/*', GLOB_ONLYDIR)
234 foreach ($addons_files as $file) {
235 $addons = glob($file.'/*.php');
236 foreach ($addons as $addon) {
237 include_once($addon);
243 public function __call($method, $arguments)
245 // Disable auto-rendering
246 $this->auto_render = FALSE;
248 // By defining a __call method, all pages routed to this controller
249 // that result in 404 errors will be handled by this method, instead of
250 // being displayed as "Page Not Found" errors.
251 echo _("The requested page doesn't exist") . " ($method)";
255 * Create a View object
257 public function add_view($view)
259 $view = trim($view);
260 if (empty($view)) {
261 return false;
263 return new View($view);
267 * Set correct image path.
269 public function img_path($rel_path='')
271 return $this->add_path($rel_path);
275 * Set correct image path
277 public function add_path($rel_path)
279 return ninja::add_path($rel_path);