Subversion Repositories computer_asset_manager_v2

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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