reports: Fix id of custom date selector
[ninja.git] / application / controllers / ninja.php
blob05f79037f82ad12b59d83b4467b481d43a2a310c
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 $xtra_js = array();
27 public $xtra_css = array();
28 public $inline_js = false;
29 public $js_strings = false;
30 public $stale_data = false;
31 public $run_tests = false;
32 public $notifications_disabled = false;
33 public $checks_disabled = false;
34 public $log = false;
35 public $help_link = false;
36 public $help_link_url = false;
38 public function __construct()
40 $this->log = op5log::instance('ninja');
41 parent::__construct();
42 if(request::is_ajax()) {
43 $this->auto_render = FALSE;
46 $this->run_tests = $this->input->get('run_tests', false) !== false;
48 $this->template = $this->add_view('template');
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 if (isset($_REQUEST['noheader'])) {
68 $this->session->set('noheader', !empty($_REQUEST['noheader']));
71 if ($this->session->get('noheader', false) !== false) {
72 # hack the session variable into the $_GET array
73 # to make it visible in $this->input->get()
74 $_GET['noheader'] = 1;
77 /**
78 * check for generic sort parameters in GET and store in session
80 # use e.g status/host/all to store sort settings
81 # this will lead to specific sort order for
82 # every <host_name> e.g status/host/<host_name>
83 $sort_key = Router::$current_uri;
85 # The following will instead make all calls to e.g status/host
86 # to behave the same
87 # $sort_key = Router::$controller.'/'.Router::$method;
89 if ($this->input->get('sort_field', false)) {
90 $cur_data = array(
91 'sort_field' => $this->input->get('sort_field', false),
92 'sort_order' => $this->input->get('sort_order', false)
94 $session_sort[$sort_key] = $cur_data;
95 $sort_options = $this->session->get('sort_options', false);
97 $_SESSION['sort_options'][$sort_key] = $cur_data;
100 bindtextdomain('ninja', APPPATH.'/languages');
101 textdomain('ninja');
103 $saved_searches = false;
105 # This should be defined even if not logged in
106 $this->template->help_link = false;
108 if (Auth::instance()->logged_in() && PHP_SAPI !== "cli") {
109 # warning! do not set anything in xlinks, as it isn't working properly
110 # and cannot (easily) be fixed
111 $this->xlinks = array();
112 $this->_addons();
114 # help link
115 # To enable link: create an addon with code:
116 # $this->help_link_url = "http://example.org/$VERSION$/$CONTROLLER$/$METHOD$"
117 if( $this->help_link_url ) {
118 $this->template->help_link = str_replace(
119 array('$VERSION$', '$CONTROLLER$', '$METHOD$'),
120 array_map('urlencode',
121 array(trim(config::get_version_info()), Router::$controller, Router::$method)
123 $this->help_link_url
128 # create the user menu
129 $menu = new Menu_Model();
130 $this->template->links = $menu->create();
132 foreach ($this->xlinks as $link)
133 $this->template->links[$link['category']][$link['title']] = $link['contents'];
135 $this->_global_notification_checks();
137 # fetch info on saved searches and assign to master template
138 $searches = Saved_searches_Model::get_saved_searches();
139 if ($searches !== false && count($searches)) {
140 $this->template->saved_searches = $this->add_view('saved_searches');
141 $this->template->saved_searches->searches = $searches;
145 $items_per_page = arr::search($_GET, 'items_per_page');
146 if ($items_per_page !== false) {
147 $_GET['items_per_page'] = ($items_per_page !== false && $items_per_page < 0)
148 ? ($items_per_page * -1)
149 : (int)$items_per_page;
152 # convert test params to $_REQUEST to enable more
153 # parameters to different controllers (reports for one)
154 if (PHP_SAPI == "cli" && $this->run_tests !== false
155 && !empty($_SERVER['argc']) && isset($_SERVER['argv'][1])) {
156 $params = $_SERVER['argv'][1];
157 if (strstr($params, '?')) {
158 $params = explode('?', $params);
159 parse_str($params[1], $_REQUEST);
164 public function add_global_notification( $notification ) {
165 if (!is_array($notification)) {
166 $notification = array($notification);
168 $this->template->global_notifications[] = $notification;
171 public function add_print_notification($notification) {
172 $this->template->print_notifications[] = $notification;
176 * Check for notifications to be displayed to user
177 * Each notification should be an array with (text, link)
179 public function _global_notification_checks()
181 try {
182 $status = StatusPool_Model::status();
183 if($status) {
184 // we've got access
185 if (!$status->get_enable_notifications()) {
186 $this->add_global_notification( html::anchor('extinfo/show_process_info', _('Notifications are disabled')) );
188 if (!$status->get_execute_service_checks()) {
189 $this->add_global_notification( html::anchor('extinfo/show_process_info', _('Service checks are disabled')) );
191 if (!$status->get_execute_host_checks()) {
192 $this->add_global_notification( html::anchor('extinfo/show_process_info', _('Host checks are disabled')) );
194 if (!$status->get_process_performance_data()) {
195 $this->add_global_notification( html::anchor('extinfo/show_process_info', _('Performance data processing are disabled')) );
197 if (!$status->get_accept_passive_service_checks()) {
198 $this->add_global_notification( html::anchor('extinfo/show_process_info', _('Passive service checks are disabled')) );
200 if (!$status->get_accept_passive_host_checks()) {
201 $this->add_global_notification( html::anchor('extinfo/show_process_info', _('Passive host checks are disabled')) );
203 if (!$status->get_enable_event_handlers()) {
204 $this->add_global_notification( html::anchor('extinfo/show_process_info', _('Event handlers disabled')) );
206 if (!$status->get_enable_flap_detection()) {
207 $this->add_global_notification( html::anchor('extinfo/show_process_info', _('Flap detection disabled')) );
210 unset($status);
213 catch( LivestatusException $e ) {
214 $this->add_global_notification( _('Livestatus is not accessable') );
216 catch( ORMException $e ) {
217 $this->add_global_notification( _('Livestatus is not accessable') );
219 # check permissions
220 $user = Auth::instance()->get_user();
221 if (nacoma::link()===true && $user->authorized_for('configuration_information')
222 && $user->authorized_for('system_commands') && $user->authorized_for('host_view_all')) {
223 $nacoma = Database::instance('nacoma');
224 $query = $nacoma->query('SELECT COUNT(id) AS cnt FROM autoscan_results WHERE visibility != 0');
225 $query->result(false);
226 $row = $query->current();
227 if ($row !== false && $row['cnt'] > 0) {
228 $this->add_global_notification( html::anchor('configuration/configure?scan=autoscan_complete', $row['cnt'] . _(' unmonitored hosts present.')) );
235 * Find and include php files from 'addons' found in defined folders
237 protected function _addons()
239 $addons_files = array_merge(
240 glob(APPPATH.'addons/*', GLOB_ONLYDIR),
241 glob(MODPATH.'*/addons/*', GLOB_ONLYDIR)
244 foreach ($addons_files as $file) {
245 $addons = glob($file.'/*.php');
246 foreach ($addons as $addon) {
247 include_once($addon);
253 public function __call($method, $arguments)
255 // Disable auto-rendering
256 $this->auto_render = FALSE;
258 // By defining a __call method, all pages routed to this controller
259 // that result in 404 errors will be handled by this method, instead of
260 // being displayed as "Page Not Found" errors.
261 echo _("The requested page doesn't exist") . " ($method)";
265 * Create a View object
267 public function add_view($view)
269 $view = trim($view);
270 if (empty($view)) {
271 return false;
273 return new View($view);
277 * Set correct image path.
279 public function img_path($rel_path='')
281 return $this->add_path($rel_path);
285 * Set correct image path
287 public function add_path($rel_path)
289 return ninja::add_path($rel_path);