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
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
   }
55 rodolico 64
 
65
   function doAdmin () {
66
      global $dbConnection;
67
      global $url;
53 rodolico 68
 
55 rodolico 69
      if ( ! isset( $_REQUEST['action'] ) ) { // just show the menu
70
         $return[] = "<a href='$url?module=admin&action=motd'>Edit MOTD</a>";
71
         $return = '<table><tr><td>' . join( "</td><td>", $return ) . '</td></tr></table>';
72
      } else {
73
         if ( $_REQUEST['action'] == 'motd' ) {
74
            if ( isset( $_REQUEST['new_motd'] ) ) {
75
               $motd = $dbConnection->real_escape_string( $_REQUEST['new_motd'] );
76
               $dbConnection->doSQL( "update _system set key_value = '$motd' where group_name = 'program' and key_name = 'motd'" );
77
            } else {
78
               $return = '<h3>Enter the Message of the Day</h3>';
79
               $return .= "<form action='$url' method='post'>\n";
80
               $return .= "<input type='hidden' name='module' value='admin'>\n";
81
               $return .= "<input type='hidden' name='action' value='motd'>\n";
82
               $return .= "<label MOTD><textarea name='new_motd'></textarea></label>\n";
83
               $return .= "<input type='submit' name='submit' value='Update'>\n";
84
               $return .= "</form>\n";
85
            }
86
         }
87
      }
88
      return $return;
89
   }
90
 
53 rodolico 91
 
45 rodolico 92
   /**
93
    * uses $_REQUEST to decide what to display
94
    * 
95
    * @param string[] $request The contents for $_REQUEST
96
    * 
97
    * @returns string HTML to be inserted into page
98
    */
99
   function displayHTML( $request ) {
100
      global $connection;
55 rodolico 101
      global $dbConnection;
53 rodolico 102
      global $baseURL;
103
 
104
      $return = '';
20 rodolico 105
 
45 rodolico 106
      if ( isset( $_REQUEST['doAdmin'] ) ) {
107
         $return = $_SESSION['user']->admin($connection) .  $_SESSION['user']->errors();
108
         $_SESSION['user']->clearErrors();
109
      } elseif ( isset( $_REQUEST['searchfor'] ) ) {
110
         $return = print_r( doSearch( $_REQUEST['searchfor'] ), true );
55 rodolico 111
      } elseif ( isset( $_REQUEST['module'] ) ) {
112
         /* do something for a particular module */
113
         switch ( $_REQUEST['module'] ) {
114
            case 'admin' : $return = doAdmin();
115
                           break;
116
            default:       $return = sprintf( '<p>%s is not implemented yet</p>', $return = $_REQUEST['module'] );
117
         }
45 rodolico 118
      } else { // default to this if nothing else works
55 rodolico 119
         $motd = $dbConnection->getOneDBValue( "select key_value from _system where group_name = 'program' and key_name = 'motd'" );
120
         $return .= "<div class='motd'>$motd</div>";
53 rodolico 121
         $return .= processStats( 'Owner', Owner::getStats() );
122
         $return .= processStats( 'Location', Location::getStats() );
123
         $return .= processStats( 'Device', Device::getStats() );
124
         //$return .= '<div class="stats"><pre>' . print_r( $_SESSION['restrictions'], true ) . "</pre></div>";
45 rodolico 125
      } // else
126
      return $return;
127
   }
46 rodolico 128
 
129
   function buildRestrictions() {
130
      global $dbConnection;
53 rodolico 131
      $rules = explode( "\n", $_SESSION['user']->restrictions );
132
      $_SESSION['restrictions'] = array();
133
      $temp = array();
134
      $workingOn = '';
135
      foreach ( $rules as $thisOne ) {
136
         //print "<pre>Working on $thisOne\n</pre>";
137
         if ( preg_match( '/\[([^\[\]]+)\]/', $thisOne, $match ) ) {
138
            //print "Adding as category\n<br />";
139
            $workingOn = $match[1];
140
         } else {
141
            //print "Adding as a value in $workingOn\n<br />";
142
            $temp[$workingOn][] = "'" . $dbConnection->real_escape_string(trim($thisOne)) . "'";
143
         }
144
      } // foreach
145
      if ( $temp['owner'] ) {
146
         $values = $dbConnection->columnToArray( sprintf( "select distinct owner_id id from view_device_location_owner_type where owner in (%s)", implode( ',', $temp['owner'] ) ) );
147
         $_SESSION['restrictions']['owner'] = sprintf( 'owner_id in ( %s )', implode( ',', $values ) );
148
      }
149
      if ( $temp['location'] ) {
150
         $values = $dbConnection->columnToArray( sprintf( "select distinct location_id id from view_device_location_owner_type where location in (%s)", implode( ',', $temp['location'] ) ) );
151
         $_SESSION['restrictions']['location'] = sprintf( 'location_id in ( %s )', implode( ',', $values ) );
152
      }
153
      if ( $temp['device'] ) {
154
         $values = $dbConnection->columnToArray( sprintf( "select distinct device_id id from view_device_location_owner_type where device in (%s)", implode( ',', $temp['device'] ) ) );
155
         $_SESSION['restrictions']['device'] = sprintf( 'device_id in ( %s )', implode( ',', $values ) );
156
      }
46 rodolico 157
   }
45 rodolico 158
 
53 rodolico 159
   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>" ) {
160
      $return = array();
161
      foreach ( $data as $key => $value ) {
162
         $return[] = sprintf( $template, $target, $key, $value );
163
      }
164
      return $before . implode( $inside , $return ) . $after;
165
   }
22 rodolico 166
 
45 rodolico 167
 
168
 
1 rodolico 169
 
170
?>