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 |
|
|
|
67 |
function doAdmin () {
|
|
|
68 |
global $dbConnection;
|
|
|
69 |
global $url;
|
53 |
rodolico |
70 |
|
57 |
rodolico |
71 |
$return = array();
|
|
|
72 |
|
55 |
rodolico |
73 |
if ( ! isset( $_REQUEST['action'] ) ) { // just show the menu
|
|
|
74 |
$return[] = "<a href='$url?module=admin&action=motd'>Edit MOTD</a>";
|
|
|
75 |
$return = '<table><tr><td>' . join( "</td><td>", $return ) . '</td></tr></table>';
|
|
|
76 |
} else {
|
|
|
77 |
if ( $_REQUEST['action'] == 'motd' ) {
|
|
|
78 |
if ( isset( $_REQUEST['new_motd'] ) ) {
|
|
|
79 |
$motd = $dbConnection->real_escape_string( $_REQUEST['new_motd'] );
|
58 |
rodolico |
80 |
$query = "update _system set key_value = '$motd' where group_name = 'program' and key_name = 'motd'";
|
|
|
81 |
//print "<pre>$query</pre>"; die;
|
|
|
82 |
$dbConnection->doSQL( $query );
|
|
|
83 |
$return = '';
|
55 |
rodolico |
84 |
} else {
|
|
|
85 |
$return = '<h3>Enter the Message of the Day</h3>';
|
|
|
86 |
$return .= "<form action='$url' method='post'>\n";
|
|
|
87 |
$return .= "<input type='hidden' name='module' value='admin'>\n";
|
|
|
88 |
$return .= "<input type='hidden' name='action' value='motd'>\n";
|
|
|
89 |
$return .= "<label MOTD><textarea name='new_motd'></textarea></label>\n";
|
|
|
90 |
$return .= "<input type='submit' name='submit' value='Update'>\n";
|
|
|
91 |
$return .= "</form>\n";
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
return $return;
|
|
|
96 |
}
|
|
|
97 |
|
53 |
rodolico |
98 |
|
45 |
rodolico |
99 |
/**
|
|
|
100 |
* uses $_REQUEST to decide what to display
|
|
|
101 |
*
|
|
|
102 |
* @param string[] $request The contents for $_REQUEST
|
|
|
103 |
*
|
|
|
104 |
* @returns string HTML to be inserted into page
|
|
|
105 |
*/
|
58 |
rodolico |
106 |
function displayHTML( ) {
|
45 |
rodolico |
107 |
global $connection;
|
55 |
rodolico |
108 |
global $dbConnection;
|
53 |
rodolico |
109 |
global $baseURL;
|
|
|
110 |
|
56 |
rodolico |
111 |
$undefinedModules = array( 'report' => 1 );
|
|
|
112 |
|
53 |
rodolico |
113 |
$return = '';
|
56 |
rodolico |
114 |
$class = isset( $_REQUEST['module'] ) ? $_REQUEST['module'] : '';
|
|
|
115 |
$thisKey = isset( $_REQUEST['id'] ) ? $_REQUEST['id'] : 0 ;
|
|
|
116 |
$selection = array();
|
|
|
117 |
|
|
|
118 |
//print "<pre>class=$class\nid=$id\n</pre>";
|
|
|
119 |
|
|
|
120 |
// We have a class (module) calling and the class exists
|
|
|
121 |
// if we don't have a single entry, we'll display a selection list
|
|
|
122 |
// if we only have one entry, we'll display the screen for it
|
|
|
123 |
if ( $class && class_exists( $class ) ) {
|
|
|
124 |
// get a list of all entries (filtered)
|
|
|
125 |
if ( ! isset( $_SESSION['data'][$class] ) && ! $thisKey ) {
|
|
|
126 |
$selection = $class::getAll();
|
|
|
127 |
if ( count($selection) == 1 ) { // we have a single entry
|
|
|
128 |
$thisKey = key( $selection ); // so set key so we will display
|
57 |
rodolico |
129 |
} elseif ( count( $selection ) == 0 ) {
|
|
|
130 |
return '<h3>No Records Found</h3>';
|
56 |
rodolico |
131 |
}
|
|
|
132 |
} elseif( ! $thisKey ) {
|
|
|
133 |
unset( $_SESSION['data'][$class] );
|
55 |
rodolico |
134 |
}
|
56 |
rodolico |
135 |
// we have a key, but we don't have a class instance yet
|
|
|
136 |
if ( $thisKey && empty( $_SESSION['data'][$class] ) ) {
|
|
|
137 |
$_SESSION['data'][$class] = new $class( $thisKey );
|
|
|
138 |
}
|
|
|
139 |
if ( $thisKey ) { // we are in the middle of doing something for the object
|
|
|
140 |
$return = $_SESSION['data'][$class]->run();
|
|
|
141 |
} else { // lets just show them a list of possible ojbects
|
|
|
142 |
$return = $class::showSelectionList( array(), $selection );
|
|
|
143 |
}
|
|
|
144 |
} elseif ( $class ) { // Some module we don't have loaded
|
|
|
145 |
switch ( $class ) {
|
|
|
146 |
case 'admin' : $return = doAdmin();
|
58 |
rodolico |
147 |
$return = print_r( $return, true );
|
56 |
rodolico |
148 |
break;
|
|
|
149 |
default : $return = "<p>We don't know how to do <b>" . $_REQUEST['module'] . "</b> yet</p>";
|
|
|
150 |
} // switch
|
|
|
151 |
} else {
|
|
|
152 |
unset( $_SESSION['data'] );
|
57 |
rodolico |
153 |
if ( isset( $_REQUEST['doAdmin'] ) ) {
|
56 |
rodolico |
154 |
$return = $_SESSION['user']->admin($connection) . $_SESSION['user']->errors();
|
|
|
155 |
$_SESSION['user']->clearErrors();
|
|
|
156 |
} else { // default to this if nothing else works
|
|
|
157 |
$motd = $dbConnection->getOneDBValue( "select key_value from _system where group_name = 'program' and key_name = 'motd'" );
|
57 |
rodolico |
158 |
$return = "<div class='motd'>$motd</div>";
|
56 |
rodolico |
159 |
foreach ( array( 'Owner','Location','Device' ) as $class ) {
|
|
|
160 |
$return .= processStats( $class, $class::getStats() );
|
|
|
161 |
} // foreach
|
|
|
162 |
} // else
|
45 |
rodolico |
163 |
} // else
|
|
|
164 |
return $return;
|
|
|
165 |
}
|
46 |
rodolico |
166 |
|
|
|
167 |
function buildRestrictions() {
|
|
|
168 |
global $dbConnection;
|
53 |
rodolico |
169 |
$rules = explode( "\n", $_SESSION['user']->restrictions );
|
|
|
170 |
$_SESSION['restrictions'] = array();
|
|
|
171 |
$temp = array();
|
|
|
172 |
$workingOn = '';
|
|
|
173 |
foreach ( $rules as $thisOne ) {
|
|
|
174 |
//print "<pre>Working on $thisOne\n</pre>";
|
|
|
175 |
if ( preg_match( '/\[([^\[\]]+)\]/', $thisOne, $match ) ) {
|
|
|
176 |
//print "Adding as category\n<br />";
|
|
|
177 |
$workingOn = $match[1];
|
|
|
178 |
} else {
|
|
|
179 |
//print "Adding as a value in $workingOn\n<br />";
|
|
|
180 |
$temp[$workingOn][] = "'" . $dbConnection->real_escape_string(trim($thisOne)) . "'";
|
|
|
181 |
}
|
|
|
182 |
} // foreach
|
56 |
rodolico |
183 |
if ( isset( $temp['owner'] ) ) {
|
53 |
rodolico |
184 |
$values = $dbConnection->columnToArray( sprintf( "select distinct owner_id id from view_device_location_owner_type where owner in (%s)", implode( ',', $temp['owner'] ) ) );
|
57 |
rodolico |
185 |
$_SESSION['restrictions']['Owner'] = sprintf( 'owner_id in ( %s )', implode( ',', $values ) );
|
53 |
rodolico |
186 |
}
|
56 |
rodolico |
187 |
if ( isset( $temp['location'] ) ) {
|
53 |
rodolico |
188 |
$values = $dbConnection->columnToArray( sprintf( "select distinct location_id id from view_device_location_owner_type where location in (%s)", implode( ',', $temp['location'] ) ) );
|
57 |
rodolico |
189 |
$_SESSION['restrictions']['Location'] = sprintf( 'location_id in ( %s )', implode( ',', $values ) );
|
53 |
rodolico |
190 |
}
|
56 |
rodolico |
191 |
if ( isset( $temp['device'] ) ) {
|
53 |
rodolico |
192 |
$values = $dbConnection->columnToArray( sprintf( "select distinct device_id id from view_device_location_owner_type where device in (%s)", implode( ',', $temp['device'] ) ) );
|
57 |
rodolico |
193 |
$_SESSION['restrictions']['Device'] = sprintf( 'device_id in ( %s )', implode( ',', $values ) );
|
53 |
rodolico |
194 |
}
|
46 |
rodolico |
195 |
}
|
45 |
rodolico |
196 |
|
53 |
rodolico |
197 |
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>" ) {
|
|
|
198 |
$return = array();
|
|
|
199 |
foreach ( $data as $key => $value ) {
|
|
|
200 |
$return[] = sprintf( $template, $target, $key, $value );
|
|
|
201 |
}
|
|
|
202 |
return $before . implode( $inside , $return ) . $after;
|
|
|
203 |
}
|
22 |
rodolico |
204 |
|
45 |
rodolico |
205 |
|
|
|
206 |
|
1 |
rodolico |
207 |
|
|
|
208 |
?>
|