1 |
rodolico |
1 |
<?php
|
56 |
rodolico |
2 |
//include_once( 'DatabaseDefinition.php' );
|
|
|
3 |
include_once( 'camp.class.php' );
|
53 |
rodolico |
4 |
include_once( 'owner.class.php' );
|
|
|
5 |
include_once( 'location.class.php' );
|
|
|
6 |
include_once( 'device.class.php' );
|
|
|
7 |
|
1 |
rodolico |
8 |
global $LOGFILE;
|
|
|
9 |
$LOGFILE='/tmp/camp.log';
|
|
|
10 |
define( 'VERSION', '2.0b' );
|
|
|
11 |
define( 'BUILD_DATE', '20130527');
|
|
|
12 |
|
45 |
rodolico |
13 |
function loadConfig() {
|
|
|
14 |
// Search through directories looking for a config file
|
|
|
15 |
$return = array(
|
|
|
16 |
'error' => '',
|
|
|
17 |
'path' => '',
|
|
|
18 |
'configuration' => array()
|
|
|
19 |
);
|
|
|
20 |
$configFileName = 'camp2_config.yaml';
|
|
|
21 |
$searchDirectories = array(
|
|
|
22 |
// one level up from document root
|
|
|
23 |
$_SERVER['DOCUMENT_ROOT'] . "/../$configFileName",
|
|
|
24 |
// directory private one level up from document root
|
|
|
25 |
$_SERVER['DOCUMENT_ROOT'] . "/../private/$configFileName",
|
|
|
26 |
// the current directory
|
|
|
27 |
"./$configFileName",
|
|
|
28 |
);
|
|
|
29 |
foreach ( $searchDirectories as $search ) {
|
|
|
30 |
if ( file_exists( realpath( $search ) ) ) {
|
|
|
31 |
$return['path'] = realpath( $search );
|
|
|
32 |
$return['configuration'] = yaml_parse_file( $return['path'] );
|
|
|
33 |
$return['error'] = '';
|
|
|
34 |
return $return;
|
|
|
35 |
} // if
|
|
|
36 |
} // for
|
|
|
37 |
$return['error'] = "No configuration found in<br />" . implode( '<br />', $searchDirectories );
|
|
|
38 |
return $return;
|
|
|
39 |
}
|
|
|
40 |
|
46 |
rodolico |
41 |
function saveConfig( $filename, $configuration ) {
|
|
|
42 |
return yaml_emit_file( $filename, $configuration );
|
|
|
43 |
}
|
|
|
44 |
|
1 |
rodolico |
45 |
function insertValuesIntoQuery( $query, $values ) {
|
|
|
46 |
foreach ( $values as $name => $value ) {
|
|
|
47 |
$query = search_replace_string($query, "<$name>", $value );
|
|
|
48 |
}
|
|
|
49 |
return $query;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
function search_replace_string($string, $searchFor, $replaceWith ) {
|
|
|
53 |
$string = str_replace ( $searchFor, $replaceWith, $string );
|
|
|
54 |
return $string;
|
|
|
55 |
}
|
45 |
rodolico |
56 |
|
53 |
rodolico |
57 |
function processStats ( $className, $info ) {
|
|
|
58 |
$result = "<div class='stats'>\n\t<h3>$className</h3>\n";
|
|
|
59 |
foreach ( $info as $key => $value ) {
|
|
|
60 |
$result .= "\t<p>$value $key</p>\n";
|
|
|
61 |
}
|
56 |
rodolico |
62 |
$result .= "<form><input type='text' size='10' name='to_find'>\n<input type='hidden' name='module' value='$className'>\n<input type='submit' name='search' value='Search'>\n</form>";
|
53 |
rodolico |
63 |
$result .= "</div>\n";
|
|
|
64 |
return $result;
|
|
|
65 |
}
|
55 |
rodolico |
66 |
|
53 |
rodolico |
67 |
|
|
|
68 |
|
45 |
rodolico |
69 |
/**
|
|
|
70 |
* uses $_REQUEST to decide what to display
|
|
|
71 |
*
|
|
|
72 |
* @param string[] $request The contents for $_REQUEST
|
|
|
73 |
*
|
|
|
74 |
* @returns string HTML to be inserted into page
|
|
|
75 |
*/
|
58 |
rodolico |
76 |
function displayHTML( ) {
|
45 |
rodolico |
77 |
global $connection;
|
55 |
rodolico |
78 |
global $dbConnection;
|
53 |
rodolico |
79 |
global $baseURL;
|
|
|
80 |
|
56 |
rodolico |
81 |
$undefinedModules = array( 'report' => 1 );
|
|
|
82 |
|
53 |
rodolico |
83 |
$return = '';
|
56 |
rodolico |
84 |
$class = isset( $_REQUEST['module'] ) ? $_REQUEST['module'] : '';
|
|
|
85 |
$thisKey = isset( $_REQUEST['id'] ) ? $_REQUEST['id'] : 0 ;
|
|
|
86 |
$selection = array();
|
|
|
87 |
|
|
|
88 |
//print "<pre>class=$class\nid=$id\n</pre>";
|
|
|
89 |
|
|
|
90 |
// We have a class (module) calling and the class exists
|
|
|
91 |
// if we don't have a single entry, we'll display a selection list
|
|
|
92 |
// if we only have one entry, we'll display the screen for it
|
|
|
93 |
if ( $class && class_exists( $class ) ) {
|
59 |
rodolico |
94 |
if ( ! isset( $_SESSION['data'][$class] ) && ! $thisKey ) { // nothing, so we're initializing
|
|
|
95 |
// display a list of all entries (filtered)
|
56 |
rodolico |
96 |
$selection = $class::getAll();
|
|
|
97 |
if ( count($selection) == 1 ) { // we have a single entry
|
|
|
98 |
$thisKey = key( $selection ); // so set key so we will display
|
57 |
rodolico |
99 |
} elseif ( count( $selection ) == 0 ) {
|
|
|
100 |
return '<h3>No Records Found</h3>';
|
56 |
rodolico |
101 |
}
|
59 |
rodolico |
102 |
} elseif( ! $thisKey || (isset( $_SESSION['data'][$class] ) && $thisKey != $_SESSION['data'][$class]->id ) ) {
|
|
|
103 |
// The key does not exist, or does not match the data we have
|
56 |
rodolico |
104 |
unset( $_SESSION['data'][$class] );
|
55 |
rodolico |
105 |
}
|
56 |
rodolico |
106 |
// we have a key, but we don't have a class instance yet
|
|
|
107 |
if ( $thisKey && empty( $_SESSION['data'][$class] ) ) {
|
|
|
108 |
$_SESSION['data'][$class] = new $class( $thisKey );
|
|
|
109 |
}
|
|
|
110 |
if ( $thisKey ) { // we are in the middle of doing something for the object
|
|
|
111 |
$return = $_SESSION['data'][$class]->run();
|
|
|
112 |
} else { // lets just show them a list of possible ojbects
|
|
|
113 |
$return = $class::showSelectionList( array(), $selection );
|
|
|
114 |
}
|
|
|
115 |
} elseif ( $class ) { // Some module we don't have loaded
|
|
|
116 |
switch ( $class ) {
|
59 |
rodolico |
117 |
case 'admin' : require_once( 'administration.php' );
|
|
|
118 |
$return = doAdmin();
|
58 |
rodolico |
119 |
$return = print_r( $return, true );
|
56 |
rodolico |
120 |
break;
|
|
|
121 |
default : $return = "<p>We don't know how to do <b>" . $_REQUEST['module'] . "</b> yet</p>";
|
|
|
122 |
} // switch
|
|
|
123 |
} else {
|
|
|
124 |
unset( $_SESSION['data'] );
|
57 |
rodolico |
125 |
if ( isset( $_REQUEST['doAdmin'] ) ) {
|
56 |
rodolico |
126 |
$return = $_SESSION['user']->admin($connection) . $_SESSION['user']->errors();
|
|
|
127 |
$_SESSION['user']->clearErrors();
|
|
|
128 |
} else { // default to this if nothing else works
|
|
|
129 |
$motd = $dbConnection->getOneDBValue( "select key_value from _system where group_name = 'program' and key_name = 'motd'" );
|
57 |
rodolico |
130 |
$return = "<div class='motd'>$motd</div>";
|
56 |
rodolico |
131 |
foreach ( array( 'Owner','Location','Device' ) as $class ) {
|
|
|
132 |
$return .= processStats( $class, $class::getStats() );
|
|
|
133 |
} // foreach
|
|
|
134 |
} // else
|
45 |
rodolico |
135 |
} // else
|
|
|
136 |
return $return;
|
|
|
137 |
}
|
46 |
rodolico |
138 |
|
|
|
139 |
function buildRestrictions() {
|
|
|
140 |
global $dbConnection;
|
53 |
rodolico |
141 |
$rules = explode( "\n", $_SESSION['user']->restrictions );
|
|
|
142 |
$_SESSION['restrictions'] = array();
|
|
|
143 |
$temp = array();
|
|
|
144 |
$workingOn = '';
|
|
|
145 |
foreach ( $rules as $thisOne ) {
|
|
|
146 |
//print "<pre>Working on $thisOne\n</pre>";
|
|
|
147 |
if ( preg_match( '/\[([^\[\]]+)\]/', $thisOne, $match ) ) {
|
|
|
148 |
//print "Adding as category\n<br />";
|
|
|
149 |
$workingOn = $match[1];
|
|
|
150 |
} else {
|
|
|
151 |
//print "Adding as a value in $workingOn\n<br />";
|
|
|
152 |
$temp[$workingOn][] = "'" . $dbConnection->real_escape_string(trim($thisOne)) . "'";
|
|
|
153 |
}
|
|
|
154 |
} // foreach
|
56 |
rodolico |
155 |
if ( isset( $temp['owner'] ) ) {
|
53 |
rodolico |
156 |
$values = $dbConnection->columnToArray( sprintf( "select distinct owner_id id from view_device_location_owner_type where owner in (%s)", implode( ',', $temp['owner'] ) ) );
|
57 |
rodolico |
157 |
$_SESSION['restrictions']['Owner'] = sprintf( 'owner_id in ( %s )', implode( ',', $values ) );
|
53 |
rodolico |
158 |
}
|
56 |
rodolico |
159 |
if ( isset( $temp['location'] ) ) {
|
53 |
rodolico |
160 |
$values = $dbConnection->columnToArray( sprintf( "select distinct location_id id from view_device_location_owner_type where location in (%s)", implode( ',', $temp['location'] ) ) );
|
57 |
rodolico |
161 |
$_SESSION['restrictions']['Location'] = sprintf( 'location_id in ( %s )', implode( ',', $values ) );
|
53 |
rodolico |
162 |
}
|
56 |
rodolico |
163 |
if ( isset( $temp['device'] ) ) {
|
53 |
rodolico |
164 |
$values = $dbConnection->columnToArray( sprintf( "select distinct device_id id from view_device_location_owner_type where device in (%s)", implode( ',', $temp['device'] ) ) );
|
57 |
rodolico |
165 |
$_SESSION['restrictions']['Device'] = sprintf( 'device_id in ( %s )', implode( ',', $values ) );
|
53 |
rodolico |
166 |
}
|
46 |
rodolico |
167 |
}
|
45 |
rodolico |
168 |
|
53 |
rodolico |
169 |
function makeHrefList( $data, $target, $template = "<a href='%s%s'>%s</a>", $before = '<table><tr><td>', $after = "</td></tr></table>", $inside = "\n</td></tr><tr><td>" ) {
|
|
|
170 |
$return = array();
|
|
|
171 |
foreach ( $data as $key => $value ) {
|
|
|
172 |
$return[] = sprintf( $template, $target, $key, $value );
|
|
|
173 |
}
|
|
|
174 |
return $before . implode( $inside , $return ) . $after;
|
|
|
175 |
}
|
22 |
rodolico |
176 |
|
45 |
rodolico |
177 |
|
|
|
178 |
|
1 |
rodolico |
179 |
|
|
|
180 |
?>
|