Rev 53 | Rev 56 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
<?php
include_once( 'DatabaseDefinition.php' );
include_once( 'owner.class.php' );
include_once( 'location.class.php' );
include_once( 'device.class.php' );
global $LOGFILE;
$LOGFILE='/tmp/camp.log';
define( 'VERSION', '2.0b' );
define( 'BUILD_DATE', '20130527');
function loadConfig() {
// Search through directories looking for a config file
$return = array(
'error' => '',
'path' => '',
'configuration' => array()
);
$configFileName = 'camp2_config.yaml';
$searchDirectories = array(
// one level up from document root
$_SERVER['DOCUMENT_ROOT'] . "/../$configFileName",
// directory private one level up from document root
$_SERVER['DOCUMENT_ROOT'] . "/../private/$configFileName",
// the current directory
"./$configFileName",
);
foreach ( $searchDirectories as $search ) {
if ( file_exists( realpath( $search ) ) ) {
$return['path'] = realpath( $search );
$return['configuration'] = yaml_parse_file( $return['path'] );
$return['error'] = '';
return $return;
} // if
} // for
$return['error'] = "No configuration found in<br />" . implode( '<br />', $searchDirectories );
return $return;
}
function saveConfig( $filename, $configuration ) {
return yaml_emit_file( $filename, $configuration );
}
function insertValuesIntoQuery( $query, $values ) {
foreach ( $values as $name => $value ) {
$query = search_replace_string($query, "<$name>", $value );
}
return $query;
}
function search_replace_string($string, $searchFor, $replaceWith ) {
$string = str_replace ( $searchFor, $replaceWith, $string );
return $string;
}
function processStats ( $className, $info ) {
$result = "<div class='stats'>\n\t<h3>$className</h3>\n";
foreach ( $info as $key => $value ) {
$result .= "\t<p>$value $key</p>\n";
}
$result .= "</div>\n";
return $result;
}
function doAdmin () {
global $dbConnection;
global $url;
if ( ! isset( $_REQUEST['action'] ) ) { // just show the menu
$return[] = "<a href='$url?module=admin&action=motd'>Edit MOTD</a>";
$return = '<table><tr><td>' . join( "</td><td>", $return ) . '</td></tr></table>';
} else {
if ( $_REQUEST['action'] == 'motd' ) {
if ( isset( $_REQUEST['new_motd'] ) ) {
$motd = $dbConnection->real_escape_string( $_REQUEST['new_motd'] );
$dbConnection->doSQL( "update _system set key_value = '$motd' where group_name = 'program' and key_name = 'motd'" );
} else {
$return = '<h3>Enter the Message of the Day</h3>';
$return .= "<form action='$url' method='post'>\n";
$return .= "<input type='hidden' name='module' value='admin'>\n";
$return .= "<input type='hidden' name='action' value='motd'>\n";
$return .= "<label MOTD><textarea name='new_motd'></textarea></label>\n";
$return .= "<input type='submit' name='submit' value='Update'>\n";
$return .= "</form>\n";
}
}
}
return $return;
}
/**
* uses $_REQUEST to decide what to display
*
* @param string[] $request The contents for $_REQUEST
*
* @returns string HTML to be inserted into page
*/
function displayHTML( $request ) {
global $connection;
global $dbConnection;
global $baseURL;
$return = '';
if ( isset( $_REQUEST['doAdmin'] ) ) {
$return = $_SESSION['user']->admin($connection) . $_SESSION['user']->errors();
$_SESSION['user']->clearErrors();
} elseif ( isset( $_REQUEST['searchfor'] ) ) {
$return = print_r( doSearch( $_REQUEST['searchfor'] ), true );
} elseif ( isset( $_REQUEST['module'] ) ) {
/* do something for a particular module */
switch ( $_REQUEST['module'] ) {
case 'admin' : $return = doAdmin();
break;
default: $return = sprintf( '<p>%s is not implemented yet</p>', $return = $_REQUEST['module'] );
}
} else { // default to this if nothing else works
$motd = $dbConnection->getOneDBValue( "select key_value from _system where group_name = 'program' and key_name = 'motd'" );
$return .= "<div class='motd'>$motd</div>";
$return .= processStats( 'Owner', Owner::getStats() );
$return .= processStats( 'Location', Location::getStats() );
$return .= processStats( 'Device', Device::getStats() );
//$return .= '<div class="stats"><pre>' . print_r( $_SESSION['restrictions'], true ) . "</pre></div>";
} // else
return $return;
}
function buildRestrictions() {
global $dbConnection;
$rules = explode( "\n", $_SESSION['user']->restrictions );
$_SESSION['restrictions'] = array();
$temp = array();
$workingOn = '';
foreach ( $rules as $thisOne ) {
//print "<pre>Working on $thisOne\n</pre>";
if ( preg_match( '/\[([^\[\]]+)\]/', $thisOne, $match ) ) {
//print "Adding as category\n<br />";
$workingOn = $match[1];
} else {
//print "Adding as a value in $workingOn\n<br />";
$temp[$workingOn][] = "'" . $dbConnection->real_escape_string(trim($thisOne)) . "'";
}
} // foreach
if ( $temp['owner'] ) {
$values = $dbConnection->columnToArray( sprintf( "select distinct owner_id id from view_device_location_owner_type where owner in (%s)", implode( ',', $temp['owner'] ) ) );
$_SESSION['restrictions']['owner'] = sprintf( 'owner_id in ( %s )', implode( ',', $values ) );
}
if ( $temp['location'] ) {
$values = $dbConnection->columnToArray( sprintf( "select distinct location_id id from view_device_location_owner_type where location in (%s)", implode( ',', $temp['location'] ) ) );
$_SESSION['restrictions']['location'] = sprintf( 'location_id in ( %s )', implode( ',', $values ) );
}
if ( $temp['device'] ) {
$values = $dbConnection->columnToArray( sprintf( "select distinct device_id id from view_device_location_owner_type where device in (%s)", implode( ',', $temp['device'] ) ) );
$_SESSION['restrictions']['device'] = sprintf( 'device_id in ( %s )', implode( ',', $values ) );
}
}
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>" ) {
$return = array();
foreach ( $data as $key => $value ) {
$return[] = sprintf( $template, $target, $key, $value );
}
return $before . implode( $inside , $return ) . $after;
}
?>