r3836 another copy of helper file
[racktables-contribs] / oneshot.php
CommitLineData
8b30586b
DO
1<?php
2/*
3This is a version of local.php file, which implements 'oneshot'
4extension for RackTables. This extension makes it possible to
5forward a user to a RackTables object page without knowing the
6internal ID of that object. This is mainly intended to be used
7together with other systems like Nagios, which only share
8object's name in common with RackTables. For example:
9
10http://example.com/racktables/index.php?page=oneshot&q=myserver
11
12(This would instantly redirect the user to myserver's page in
13RackTables, if "myserver" exists.)
14
15This extension has been tested to work with RackTables 0.17.x.
16
17*/
18
19// Return entity ID, if its named column equals to provided string, or NULL
20// otherwise (nothing found or more, than one row returned by query).
21function lookupEntityByString ($realm, $value, $column = 'name')
22{
23 global $SQLSchema;
24 if (!isset ($SQLSchema[$realm]))
25 return NULL;
26 $SQLinfo = $SQLSchema[$realm];
27 $query = "SELECT ${SQLinfo['keycolumn']} as id FROM ${SQLinfo['table']} WHERE ${SQLinfo['table']}.${column} = '${value}' limit 2";
28 $result = useSelectBlade ($query, __FUNCTION__);
29 $rows = $result->fetchAll (PDO::FETCH_ASSOC);
30 unset ($result);
31 if (count ($rows) != 1)
32 return NULL;
33 return $rows[0]['id'];
34}
35
36function handleOneShotRequest ()
37{
38 assertStringArg ('realm', __FUNCTION__);
39 assertStringArg ('q', __FUNCTION__);
40 switch ($_REQUEST['realm'])
41 {
42 case 'object':
43 if (NULL === ($id = lookupEntityByString ($_REQUEST['realm'], $_REQUEST['q'])))
44 echo "<h2>Nothing found for '${_REQUEST['q']}'</h2>";
45 else
46 echo "<script language='Javascript'>document.location='index.php?page=object&object_id=${id}';//</script>";
47 break;
48 default:
49 dragon();
50 break;
51 }
52}
53
54?>