Subversion Repositories computer_asset_manager_v2

Rev

Rev 51 | Rev 55 | 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;
   }
      
   
   /**
    * 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 $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 );
      } else { // default to this if nothing else works
         $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;
   }
      
      
      
   
?>