Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
85 changes: 0 additions & 85 deletions build.xml

This file was deleted.

16 changes: 16 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "cacti/plugin_npc",
"description": "Nagios Plugin for Cacti",
"license": "GPL-2.0-or-later",
"require-dev": {
"pestphp/pest": "^1.23"
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"autoload-dev": {
"files": ["tests/bootstrap.php"]
}
}
22 changes: 2 additions & 20 deletions config.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
<?php

//require the base Doctrine class
require_once(dirname(__FILE__) . '/lib/Doctrine.php');
require_once(dirname(__FILE__) . '/controllers/controller.php');

//register the autoloader
spl_autoload_register(array('Doctrine', 'autoload'));

$database_username = urlencode($database_username);
$database_password = urlencode($database_password);

// Setup the DSN
$dsn = "$database_type://$database_username:$database_password@$database_hostname:$database_port/$database_default";
/* NPC Plugin Configuration Bootstrap */

// initialize a new Doctrine_Connection
$conn = Doctrine_Manager::connection($dsn);

// Setup conservative (on demand) model loading
Doctrine_Manager::getInstance()->setAttribute(Doctrine::ATTR_MODEL_LOADING, Doctrine::MODEL_LOADING_CONSERVATIVE);

// Load our models
Doctrine::loadModels(dirname(__FILE__) . '/models');
require_once(dirname(__FILE__) . '/controllers/controller.php');
108 changes: 60 additions & 48 deletions controllers/comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Comments controller class
*
* Comments controller provides functionality, such as building the
* Doctrine queries and formatting output.
* queries and formatting output.
*
* @package npc
* @subpackage npc.controllers
Expand Down Expand Up @@ -172,22 +172,13 @@ function deleteAllServiceComments() {
* @return array icon and alt
*/
function getHostIcon($id) {
$results = db_fetch_row_prepared('SELECT h.icon_image, h.icon_image_alt
FROM npc_services s
LEFT JOIN npc_hosts h ON s.host_object_id = h.host_object_id
WHERE s.service_object_id = ?',
array($id));

$q = new Doctrine_Pager(
Doctrine_Query::create()
->select('s.service_id,'
.'h.icon_image,'
.'h.icon_image_alt')
->from('NpcServices s')
->leftJoin('s.Host h')
->where("s.service_object_id = ?", $id),
$this->currentPage,
$this->limit
);

$results = $q->execute(array(), Doctrine::HYDRATE_ARRAY);

return($results[0]['Host']);
return($results);
}

/**
Expand All @@ -197,57 +188,78 @@ function getHostIcon($id) {
*
* @return array The comments
*/
function comments($id=null, $where='') {
function comments($id = null, $where = '') {

// Maps searchable fields passed in from the client
/* Maps searchable fields passed in from the client */
$fieldMap = array('service_description' => 'o.name2',
'host_name' => 'o.name1',
'author_name' => 'c.author_name',
'comment_data' => 'c.comment_data');

$params = array();

if ($this->id || $id) {
if ($where != '') {
$where .= ' AND ';
}
$where .= sprintf("c.object_id = %d", is_null($id) ? $this->id : $id);
$where .= 'c.object_id = ?';
$params[] = is_null($id) ? $this->id : $id;
}

if ($this->searchString) {
$where = $this->searchClause($where, $fieldMap);
$where = $this->searchClause($where, $fieldMap, $params);
}

if ($this->sort) {
$orderBy = $this->sort . ' ' . $this->dir;
} else {
$orderBy = 'c.entry_time DESC, c.entry_time_usec DESC';
}

$q = new Doctrine_Pager(
Doctrine_Query::create()
->select('i.instance_name,'
.'o.name1 AS host_name,'
.'o.name2 AS service_description,'
.'s.icon_image AS svc_icon_image,'
.'s.icon_image_alt AS svc_icon_image_alt,'
.'h.icon_image AS host_icon_image,'
.'h.icon_image_alt AS host_icon_image_alt,'
.'c.*')
->from('NpcComments c')
->leftJoin('c.Object o')
->leftJoin('c.Instance i')
->leftJoin('c.Service s')
->leftJoin('c.Host h')
->where($where)
->orderby($orderBy),
$this->currentPage,
$this->limit
$allowedSort = array(
'entry_time' => 'c.entry_time',
'author_name' => 'c.author_name',
'comment_data' => 'c.comment_data',
'host_name' => 'o.name1',
'service_description' => 'o.name2',
);
$sortCol = ($this->sort && isset($allowedSort[$this->sort]))
? $allowedSort[$this->sort]
: 'c.entry_time';
$sortDir = (strtoupper((string) $this->dir) === 'ASC') ? 'ASC' : 'DESC';
$orderBy = $sortCol . ' ' . $sortDir;
if ($sortCol === 'c.entry_time') {
$orderBy .= ', c.entry_time_usec ' . $sortDir;
}

$results = $q->execute(array(), Doctrine::HYDRATE_ARRAY);
$whereClause = '';
if ($where != '') {
$whereClause = 'WHERE ' . $where;
}

// Set the total number of records
$this->numRecords = $q->getNumResults();
/* Get the total count */
$this->numRecords = db_fetch_cell_prepared('SELECT COUNT(*)
FROM npc_comments c
LEFT JOIN npc_objects o ON c.object_id = o.object_id
LEFT JOIN npc_instances i ON c.instance_id = i.instance_id
LEFT JOIN npc_services s ON c.object_id = s.service_object_id
LEFT JOIN npc_hosts h ON c.object_id = h.host_object_id
' . $whereClause,
$params);

$offset = ($this->currentPage - 1) * $this->limit;

$results = db_fetch_assoc_prepared('SELECT i.instance_name,
o.name1 AS host_name,
o.name2 AS service_description,
s.icon_image AS svc_icon_image,
s.icon_image_alt AS svc_icon_image_alt,
h.icon_image AS host_icon_image,
h.icon_image_alt AS host_icon_image_alt,
c.*
FROM npc_comments c
LEFT JOIN npc_objects o ON c.object_id = o.object_id
LEFT JOIN npc_instances i ON c.instance_id = i.instance_id
LEFT JOIN npc_services s ON c.object_id = s.service_object_id
LEFT JOIN npc_hosts h ON c.object_id = h.host_object_id
' . $whereClause . '
ORDER BY ' . $orderBy . '
LIMIT ?, ?',
array_merge($params, array($offset, $this->limit)));

return($results);
}
Expand Down
63 changes: 32 additions & 31 deletions controllers/controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
* @subpackage npc.controllers
*/
class Controller {
var $conn = null;

/**
* The default state to query
Expand Down Expand Up @@ -313,36 +312,39 @@ function flattenArray($array=array()) {
/**
* searchClause
*
* Appends search parameters to the passed in where clause
* @param string $where An existing where clause
* @param array $fieldMap Maps passed in field names
* @return string The appended where clasue
* Appends search parameters to the passed in where clause.
* Returns both the SQL fragment and an array of bind params
* for use with prepared statements.
*
* @param string $where An existing where clause
* @param array $fieldMap Maps passed in field names
* @param array $params Existing bind params array (passed by reference)
* @return string The appended where clause
*/
function searchClause($where, $fieldMap) {

if (!$where) {
$where = ' ( ';
} else {
$where .= ' AND ( ';
}

$fields = json_decode(stripslashes($this->searchFields));
$count = count($fields);

$x = 1;
foreach ($fields as $field) {
if (isset($fieldMap[$field])) {
$where .= $fieldMap[$field] . " LIKE '%" . $this->searchString . "%' ";
if ($x < $count) {
$where .= ' OR ';
}
$x++;
} else {
$count = $count - 1;
}
}

$where .= ' ) ';
function searchClause($where, $fieldMap, &$params = array()) {
$fields = json_decode(stripslashes($this->searchFields));
if (!is_array($fields)) {
return $where;
}

$searchValue = '%' . $this->searchString . '%';

$clauses = array();
$searchParams = array();
foreach ($fields as $field) {
if (isset($fieldMap[$field])) {
$clauses[] = $fieldMap[$field] . ' LIKE ?';
$searchParams[] = $searchValue;
}
}

if (empty($clauses)) {
return $where;
}

$params = array_merge($params, $searchParams);
$where .= $where ? ' AND ( ' : ' ( ';
$where .= implode(' OR ', $clauses) . ' ) ';

return($where);
}
Expand Down Expand Up @@ -483,4 +485,3 @@ function updateCsrf() {
bottom_footer();
} // end drawFrame
}

Loading