1 |
rodolico |
1 |
<?php
|
20 |
rodolico |
2 |
include_once( 'DatabaseDefinition.php' );
|
53 |
rodolico |
3 |
include_once( 'owner.class.php' );
|
|
|
4 |
include_once( 'location.class.php' );
|
|
|
5 |
include_once( 'device.class.php' );
|
|
|
6 |
|
1 |
rodolico |
7 |
global $LOGFILE;
|
|
|
8 |
$LOGFILE='/tmp/camp.log';
|
|
|
9 |
define( 'VERSION', '2.0b' );
|
|
|
10 |
define( 'BUILD_DATE', '20130527');
|
|
|
11 |
|
45 |
rodolico |
12 |
function loadConfig() {
|
|
|
13 |
// Search through directories looking for a config file
|
|
|
14 |
$return = array(
|
|
|
15 |
'error' => '',
|
|
|
16 |
'path' => '',
|
|
|
17 |
'configuration' => array()
|
|
|
18 |
);
|
|
|
19 |
$configFileName = 'camp2_config.yaml';
|
|
|
20 |
$searchDirectories = array(
|
|
|
21 |
// one level up from document root
|
|
|
22 |
$_SERVER['DOCUMENT_ROOT'] . "/../$configFileName",
|
|
|
23 |
// directory private one level up from document root
|
|
|
24 |
$_SERVER['DOCUMENT_ROOT'] . "/../private/$configFileName",
|
|
|
25 |
// the current directory
|
|
|
26 |
"./$configFileName",
|
|
|
27 |
);
|
|
|
28 |
foreach ( $searchDirectories as $search ) {
|
|
|
29 |
if ( file_exists( realpath( $search ) ) ) {
|
|
|
30 |
$return['path'] = realpath( $search );
|
|
|
31 |
$return['configuration'] = yaml_parse_file( $return['path'] );
|
|
|
32 |
$return['error'] = '';
|
|
|
33 |
return $return;
|
|
|
34 |
} // if
|
|
|
35 |
} // for
|
|
|
36 |
$return['error'] = "No configuration found in<br />" . implode( '<br />', $searchDirectories );
|
|
|
37 |
return $return;
|
|
|
38 |
}
|
|
|
39 |
|
46 |
rodolico |
40 |
function saveConfig( $filename, $configuration ) {
|
|
|
41 |
return yaml_emit_file( $filename, $configuration );
|
|
|
42 |
}
|
|
|
43 |
|
1 |
rodolico |
44 |
function insertValuesIntoQuery( $query, $values ) {
|
|
|
45 |
foreach ( $values as $name => $value ) {
|
|
|
46 |
$query = search_replace_string($query, "<$name>", $value );
|
|
|
47 |
}
|
|
|
48 |
return $query;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
function search_replace_string($string, $searchFor, $replaceWith ) {
|
|
|
52 |
$string = str_replace ( $searchFor, $replaceWith, $string );
|
|
|
53 |
return $string;
|
|
|
54 |
}
|
45 |
rodolico |
55 |
|
53 |
rodolico |
56 |
function processStats ( $className, $info ) {
|
|
|
57 |
$result = "<div class='stats'>\n\t<h3>$className</h3>\n";
|
|
|
58 |
foreach ( $info as $key => $value ) {
|
|
|
59 |
$result .= "\t<p>$value $key</p>\n";
|
|
|
60 |
}
|
|
|
61 |
$result .= "</div>\n";
|
|
|
62 |
return $result;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
|
45 |
rodolico |
66 |
/**
|
|
|
67 |
* uses $_REQUEST to decide what to display
|
|
|
68 |
*
|
|
|
69 |
* @param string[] $request The contents for $_REQUEST
|
|
|
70 |
*
|
|
|
71 |
* @returns string HTML to be inserted into page
|
|
|
72 |
*/
|
|
|
73 |
function displayHTML( $request ) {
|
|
|
74 |
global $connection;
|
53 |
rodolico |
75 |
global $baseURL;
|
|
|
76 |
|
|
|
77 |
$return = '';
|
20 |
rodolico |
78 |
|
45 |
rodolico |
79 |
if ( isset( $_REQUEST['doAdmin'] ) ) {
|
|
|
80 |
$return = $_SESSION['user']->admin($connection) . $_SESSION['user']->errors();
|
|
|
81 |
$_SESSION['user']->clearErrors();
|
|
|
82 |
} elseif ( isset( $_REQUEST['searchfor'] ) ) {
|
|
|
83 |
$return = print_r( doSearch( $_REQUEST['searchfor'] ), true );
|
|
|
84 |
} else { // default to this if nothing else works
|
53 |
rodolico |
85 |
$return .= processStats( 'Owner', Owner::getStats() );
|
|
|
86 |
$return .= processStats( 'Location', Location::getStats() );
|
|
|
87 |
$return .= processStats( 'Device', Device::getStats() );
|
|
|
88 |
//$return .= '<div class="stats"><pre>' . print_r( $_SESSION['restrictions'], true ) . "</pre></div>";
|
45 |
rodolico |
89 |
} // else
|
|
|
90 |
return $return;
|
|
|
91 |
}
|
46 |
rodolico |
92 |
|
|
|
93 |
function buildRestrictions() {
|
|
|
94 |
global $dbConnection;
|
53 |
rodolico |
95 |
$rules = explode( "\n", $_SESSION['user']->restrictions );
|
|
|
96 |
$_SESSION['restrictions'] = array();
|
|
|
97 |
$temp = array();
|
|
|
98 |
$workingOn = '';
|
|
|
99 |
foreach ( $rules as $thisOne ) {
|
|
|
100 |
//print "<pre>Working on $thisOne\n</pre>";
|
|
|
101 |
if ( preg_match( '/\[([^\[\]]+)\]/', $thisOne, $match ) ) {
|
|
|
102 |
//print "Adding as category\n<br />";
|
|
|
103 |
$workingOn = $match[1];
|
|
|
104 |
} else {
|
|
|
105 |
//print "Adding as a value in $workingOn\n<br />";
|
|
|
106 |
$temp[$workingOn][] = "'" . $dbConnection->real_escape_string(trim($thisOne)) . "'";
|
|
|
107 |
}
|
|
|
108 |
} // foreach
|
|
|
109 |
if ( $temp['owner'] ) {
|
|
|
110 |
$values = $dbConnection->columnToArray( sprintf( "select distinct owner_id id from view_device_location_owner_type where owner in (%s)", implode( ',', $temp['owner'] ) ) );
|
|
|
111 |
$_SESSION['restrictions']['owner'] = sprintf( 'owner_id in ( %s )', implode( ',', $values ) );
|
|
|
112 |
}
|
|
|
113 |
if ( $temp['location'] ) {
|
|
|
114 |
$values = $dbConnection->columnToArray( sprintf( "select distinct location_id id from view_device_location_owner_type where location in (%s)", implode( ',', $temp['location'] ) ) );
|
|
|
115 |
$_SESSION['restrictions']['location'] = sprintf( 'location_id in ( %s )', implode( ',', $values ) );
|
|
|
116 |
}
|
|
|
117 |
if ( $temp['device'] ) {
|
|
|
118 |
$values = $dbConnection->columnToArray( sprintf( "select distinct device_id id from view_device_location_owner_type where device in (%s)", implode( ',', $temp['device'] ) ) );
|
|
|
119 |
$_SESSION['restrictions']['device'] = sprintf( 'device_id in ( %s )', implode( ',', $values ) );
|
|
|
120 |
}
|
46 |
rodolico |
121 |
}
|
45 |
rodolico |
122 |
|
53 |
rodolico |
123 |
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>" ) {
|
|
|
124 |
$return = array();
|
|
|
125 |
foreach ( $data as $key => $value ) {
|
|
|
126 |
$return[] = sprintf( $template, $target, $key, $value );
|
|
|
127 |
}
|
|
|
128 |
return $before . implode( $inside , $return ) . $after;
|
|
|
129 |
}
|
22 |
rodolico |
130 |
|
45 |
rodolico |
131 |
|
|
|
132 |
|
1 |
rodolico |
133 |
|
|
|
134 |
?>
|