| 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' );
 | 
        
           | 67 | rodolico | 7 |    include_once( 'admin.class.php' );
 | 
        
           | 70 | rodolico | 8 |    include_once( 'devicetype.class.php' );
 | 
        
           | 53 | rodolico | 9 |   | 
        
           | 1 | rodolico | 10 |    global $LOGFILE;
 | 
        
           |  |  | 11 |    $LOGFILE='/tmp/camp.log';
 | 
        
           |  |  | 12 |    define( 'VERSION', '2.0b' );
 | 
        
           |  |  | 13 |    define( 'BUILD_DATE', '20130527');
 | 
        
           |  |  | 14 |   | 
        
           | 45 | rodolico | 15 |    function loadConfig() {
 | 
        
           |  |  | 16 |       // Search through directories looking for a config file
 | 
        
           |  |  | 17 |       $return = array(
 | 
        
           |  |  | 18 |          'error' => '',
 | 
        
           |  |  | 19 |          'path'  => '',
 | 
        
           |  |  | 20 |          'configuration' => array()
 | 
        
           |  |  | 21 |       );
 | 
        
           |  |  | 22 |       $configFileName = 'camp2_config.yaml';
 | 
        
           |  |  | 23 |       $searchDirectories = array( 
 | 
        
           |  |  | 24 |          // one level up from document root
 | 
        
           |  |  | 25 |          $_SERVER['DOCUMENT_ROOT'] . "/../$configFileName",
 | 
        
           |  |  | 26 |          // directory private one level up from document root
 | 
        
           |  |  | 27 |          $_SERVER['DOCUMENT_ROOT'] . "/../private/$configFileName",
 | 
        
           |  |  | 28 |          // the current directory
 | 
        
           |  |  | 29 |          "./$configFileName",
 | 
        
           |  |  | 30 |       );
 | 
        
           |  |  | 31 |       foreach ( $searchDirectories as $search ) {
 | 
        
           |  |  | 32 |          if ( file_exists( realpath( $search ) ) ) {
 | 
        
           |  |  | 33 |             $return['path'] = realpath( $search );
 | 
        
           |  |  | 34 |             $return['configuration'] = yaml_parse_file( $return['path'] );
 | 
        
           |  |  | 35 |             $return['error'] = '';
 | 
        
           |  |  | 36 |             return $return;
 | 
        
           |  |  | 37 |          } // if
 | 
        
           |  |  | 38 |       } // for
 | 
        
           | 75 | rodolico | 39 |       $return['error'] = "No configuration file $configFileName found in<br />" . implode( '<br />', $searchDirectories );
 | 
        
           | 45 | rodolico | 40 |       return $return;
 | 
        
           |  |  | 41 |    }
 | 
        
           |  |  | 42 |   | 
        
           | 46 | rodolico | 43 |    function saveConfig( $filename, $configuration ) {
 | 
        
           |  |  | 44 |       return yaml_emit_file( $filename, $configuration );
 | 
        
           |  |  | 45 |    }
 | 
        
           |  |  | 46 |   | 
        
           | 1 | rodolico | 47 |    function insertValuesIntoQuery( $query, $values ) {
 | 
        
           |  |  | 48 |       foreach ( $values as $name => $value ) {
 | 
        
           |  |  | 49 |          $query = search_replace_string($query, "<$name>", $value );
 | 
        
           |  |  | 50 |       }
 | 
        
           |  |  | 51 |       return $query;
 | 
        
           |  |  | 52 |    }
 | 
        
           |  |  | 53 |   | 
        
           |  |  | 54 |    function search_replace_string($string, $searchFor, $replaceWith ) {
 | 
        
           |  |  | 55 |       $string = str_replace ( $searchFor, $replaceWith, $string );
 | 
        
           |  |  | 56 |       return $string;
 | 
        
           |  |  | 57 |    }
 | 
        
           | 45 | rodolico | 58 |   | 
        
           | 53 | rodolico | 59 |    function processStats ( $className, $info ) {
 | 
        
           | 70 | rodolico | 60 |       global $url;
 | 
        
           |  |  | 61 |   | 
        
           |  |  | 62 |       $result = "<div class='stats'>\n\t<h3><a href='$url?module=$className'>$className</a></h3>\n";
 | 
        
           | 53 | rodolico | 63 |       foreach ( $info as $key => $value ) {
 | 
        
           |  |  | 64 |          $result .= "\t<p>$value $key</p>\n";
 | 
        
           |  |  | 65 |       }
 | 
        
           | 67 | rodolico | 66 |       $result .= "<form><input type='text' size='10' name='to_find'>\n<input type='hidden' name='action' value='search'>\n<input type='hidden' name='module' value='$className'>\n<input type='submit' name='search' value='Search'>\n</form>";
 | 
        
           | 53 | rodolico | 67 |       $result .= "</div>\n";
 | 
        
           |  |  | 68 |       return $result;
 | 
        
           |  |  | 69 |    }
 | 
        
           | 55 | rodolico | 70 |   | 
        
           | 53 | rodolico | 71 |   | 
        
           |  |  | 72 |   | 
        
           | 45 | rodolico | 73 |    /**
 | 
        
           |  |  | 74 |     * uses $_REQUEST to decide what to display
 | 
        
           |  |  | 75 |     * 
 | 
        
           |  |  | 76 |     * @param string[] $request The contents for $_REQUEST
 | 
        
           |  |  | 77 |     * 
 | 
        
           |  |  | 78 |     * @returns string HTML to be inserted into page
 | 
        
           |  |  | 79 |     */
 | 
        
           | 58 | rodolico | 80 |    function displayHTML(  ) {
 | 
        
           | 45 | rodolico | 81 |       global $connection;
 | 
        
           | 55 | rodolico | 82 |       global $dbConnection;
 | 
        
           | 53 | rodolico | 83 |       global $baseURL;
 | 
        
           |  |  | 84 |   | 
        
           | 67 | rodolico | 85 |       /*
 | 
        
           |  |  | 86 |        * base classes are classes that we will run through the main processing
 | 
        
           |  |  | 87 |        * loop. Assumes they are all derived from camp.class.php (class Camp)
 | 
        
           |  |  | 88 |        * so they all work the same way. Everything else is processed differently
 | 
        
           |  |  | 89 |        */
 | 
        
           | 56 | rodolico | 90 |   | 
        
           | 70 | rodolico | 91 |       $baseClasses = array( 'Owner' => 1, 'Location' => 1, 'Device' => 1, 'DeviceType' => 1 );
 | 
        
           | 67 | rodolico | 92 |   | 
        
           | 53 | rodolico | 93 |       $return = '';
 | 
        
           | 67 | rodolico | 94 |   | 
        
           |  |  | 95 |       if ( isset( $_REQUEST['doAdmin'] ) ) { // user administration
 | 
        
           |  |  | 96 |          $return = $_SESSION['user']->admin($connection) .  $_SESSION['user']->errors();
 | 
        
           |  |  | 97 |          $_SESSION['user']->clearErrors();
 | 
        
           |  |  | 98 |          return $return;
 | 
        
           |  |  | 99 |       } // user administration
 | 
        
           |  |  | 100 |   | 
        
           |  |  | 101 |       // standard flow
 | 
        
           | 56 | rodolico | 102 |       $class = isset( $_REQUEST['module'] ) ? $_REQUEST['module'] : '';
 | 
        
           | 67 | rodolico | 103 |       $thisKey = isset( $_REQUEST['id'] ) ? $_REQUEST['id'] : '' ;
 | 
        
           | 56 | rodolico | 104 |       $selection = array();
 | 
        
           | 67 | rodolico | 105 |       $action = 
 | 
        
           |  |  | 106 |          isset( $_REQUEST['action'] ) ?
 | 
        
           |  |  | 107 |             $_REQUEST['action'] : 'display';
 | 
        
           |  |  | 108 |             // removed the following, but may break things so leaving original
 | 
        
           |  |  | 109 |             //( $thisKey && $class ? 'display' : '' );
 | 
        
           |  |  | 110 |   | 
        
           |  |  | 111 |       // we have stale data in $_SESSION, so unset it
 | 
        
           |  |  | 112 |       if ( $class && $thisKey && isset( $_SESSION['data'][$class] ) && $_SESSION['data'][$class]->id != $thisKey ) {
 | 
        
           |  |  | 113 |          unset( $_SESSION['data'][$class] );
 | 
        
           |  |  | 114 |       }
 | 
        
           |  |  | 115 |       // we have something we need to work on, but it is not loaded, so load it
 | 
        
           |  |  | 116 |       if ( $thisKey && empty( $_SESSION['data'][$class] ) ) {
 | 
        
           |  |  | 117 |          $_SESSION['data'][$class] = new $class( $thisKey );
 | 
        
           |  |  | 118 |       }
 | 
        
           | 68 | rodolico | 119 |       /*
 | 
        
           |  |  | 120 |       print "<pre>REQUEST\n" . 
 | 
        
           |  |  | 121 |          print_r( $_REQUEST, true ) . 
 | 
        
           |  |  | 122 |          "\nclass=$class\nthisKey=$thisKey\naction=$action\n" .
 | 
        
           |  |  | 123 |          print_r( $_SESSION['data'], true ) .
 | 
        
           |  |  | 124 |          "</pre>";
 | 
        
           |  |  | 125 |       die;
 | 
        
           |  |  | 126 |       */
 | 
        
           | 56 | rodolico | 127 |   | 
        
           |  |  | 128 |       // We have a class (module) calling and the class exists
 | 
        
           |  |  | 129 |       // if we don't have a single entry, we'll display a selection list
 | 
        
           |  |  | 130 |       // if we only have one entry, we'll display the screen for it
 | 
        
           | 67 | rodolico | 131 |       if ( $class ) {
 | 
        
           |  |  | 132 |          if ( $action && isset( $baseClasses[$class] ) ) {
 | 
        
           |  |  | 133 |             switch ( $action ) {
 | 
        
           |  |  | 134 |                case 'add': // for an add, we need a new, empty instance
 | 
        
           |  |  | 135 |                   $_SESSION['data'][$class] = new $class();
 | 
        
           |  |  | 136 |                case 'edit':
 | 
        
           |  |  | 137 |                case 'post':
 | 
        
           |  |  | 138 |                   return $_SESSION['data'][$class]->run();
 | 
        
           |  |  | 139 |                   break;
 | 
        
           |  |  | 140 |                case 'search':
 | 
        
           |  |  | 141 |                   // display a list of all entries (filtered)
 | 
        
           |  |  | 142 |                   $selection = $class::getAll();
 | 
        
           | 68 | rodolico | 143 |                   //print "<pre>selection\n" . print_r( $selection, true ) . "</pre>" ; die;
 | 
        
           | 67 | rodolico | 144 |                   if ( count($selection) == 1 ) { // we have a single entry
 | 
        
           |  |  | 145 |                      $thisKey = key( $selection ); // so set key so we will display
 | 
        
           |  |  | 146 |                      $_SESSION['data'][$class] = new $class( $thisKey );
 | 
        
           |  |  | 147 |                   } elseif ( count( $selection ) == 0 ) {
 | 
        
           |  |  | 148 |                      $return = '<h3>No Records Found</h3>';
 | 
        
           |  |  | 149 |                   } else {
 | 
        
           |  |  | 150 |                      $return = $class::showSelectionList( array(), $selection );
 | 
        
           |  |  | 151 |                   }
 | 
        
           |  |  | 152 |                default:
 | 
        
           |  |  | 153 |                   if ( $thisKey ) { // we are in the middle of doing something for the object
 | 
        
           |  |  | 154 |                      $return = $_SESSION['data'][$class]->run();
 | 
        
           |  |  | 155 |                   } else { // lets just show them a list of possible ojbects
 | 
        
           |  |  | 156 |                      if ( class_exists( $class ) ) {
 | 
        
           |  |  | 157 |                         $return = $class::showSelectionList( array(), $selection );
 | 
        
           |  |  | 158 |                      } else {
 | 
        
           |  |  | 159 |                         $return = "<p>We don't know how to do <b>" . $_REQUEST['module'] . "</b> yet</p>";
 | 
        
           |  |  | 160 |                      } // if class_exists
 | 
        
           |  |  | 161 |                   } // if a key exists
 | 
        
           |  |  | 162 |             } // switch
 | 
        
           |  |  | 163 |          } else { // no action, or not in baseClasses
 | 
        
           |  |  | 164 |             if ( empty ( $_SESSION['data'][$class] ) ) {
 | 
        
           |  |  | 165 |                $_SESSION['data'][$class] = new $class();
 | 
        
           | 56 | rodolico | 166 |             }
 | 
        
           |  |  | 167 |             $return = $_SESSION['data'][$class]->run();
 | 
        
           | 67 | rodolico | 168 |          } // if action and baseclass
 | 
        
           | 56 | rodolico | 169 |       } else {
 | 
        
           | 66 | rodolico | 170 |          // this is the opening screen, so clear out all the data and
 | 
        
           |  |  | 171 |          // working on
 | 
        
           | 56 | rodolico | 172 |          unset( $_SESSION['data'] );
 | 
        
           | 66 | rodolico | 173 |          unset( $_SESSION['workingon'] );
 | 
        
           | 67 | rodolico | 174 |          $motd = $dbConnection->getOneDBValue( "select key_value from _system where group_name = 'program' and key_name = 'motd'" );
 | 
        
           |  |  | 175 |          $return = "<div class='motd'>$motd</div>";
 | 
        
           |  |  | 176 |          foreach ( array( 'Owner','Location','Device' ) as $class ) {
 | 
        
           |  |  | 177 |             $return .= processStats( $class, $class::getStats() );
 | 
        
           |  |  | 178 |          } // foreach
 | 
        
           |  |  | 179 |       } // if..else
 | 
        
           | 45 | rodolico | 180 |       return $return;
 | 
        
           | 67 | rodolico | 181 |    } // html
 | 
        
           | 46 | rodolico | 182 |   | 
        
           |  |  | 183 |    function buildRestrictions() {
 | 
        
           |  |  | 184 |       global $dbConnection;
 | 
        
           | 53 | rodolico | 185 |       $rules = explode( "\n", $_SESSION['user']->restrictions );
 | 
        
           |  |  | 186 |       $_SESSION['restrictions'] = array();
 | 
        
           |  |  | 187 |       $temp = array();
 | 
        
           |  |  | 188 |       $workingOn = '';
 | 
        
           |  |  | 189 |       foreach ( $rules as $thisOne ) {
 | 
        
           |  |  | 190 |          //print "<pre>Working on $thisOne\n</pre>";
 | 
        
           |  |  | 191 |          if ( preg_match( '/\[([^\[\]]+)\]/', $thisOne, $match ) ) {
 | 
        
           |  |  | 192 |             //print "Adding as category\n<br />";
 | 
        
           |  |  | 193 |             $workingOn = $match[1];
 | 
        
           |  |  | 194 |          } else {
 | 
        
           |  |  | 195 |             //print "Adding as a value in $workingOn\n<br />";
 | 
        
           |  |  | 196 |             $temp[$workingOn][] = "'" . $dbConnection->real_escape_string(trim($thisOne)) . "'";
 | 
        
           |  |  | 197 |          }
 | 
        
           |  |  | 198 |       } // foreach
 | 
        
           | 70 | rodolico | 199 |       //print "<pre>temp in restrictions\n" . print_r( $temp, true ) . '</pre>'; die;
 | 
        
           | 56 | rodolico | 200 |       if ( isset( $temp['owner'] ) ) {
 | 
        
           | 70 | rodolico | 201 |          $values = implode( ',', $dbConnection->columnToArray( sprintf( "select owner_id id from owner where name in (%s)", implode( ',', $temp['owner'] ) ) ) );
 | 
        
           |  |  | 202 |          //$values = implode( ',', $values );
 | 
        
           |  |  | 203 |          //print "<pre>Restrictions on owner\n" . print_r( $values, true ) . '</pre>'; die;
 | 
        
           |  |  | 204 |          $_SESSION['restrictions']['Owner'][] = sprintf( 'owner_id in ( %s )', $values );
 | 
        
           |  |  | 205 |          //$_SESSION['restrictions']['Location'][] = sprintf( 'location_id in ( select location_id from owner_location where removed is null and owner_id in ( %s ) )', $values );
 | 
        
           |  |  | 206 |          $_SESSION['restrictions']['Location'][] = sprintf( '( location_id in ( select location_id from owner_location where removed is null and owner_id in ( %s ) ) or location_id in ( select location_id from location_device where device_id in ( select device_id from owner_device where owner_id in (%s) ) ) )', $values, $values );
 | 
        
           |  |  | 207 |          $_SESSION['restrictions']['Device'][] = sprintf( 'device_id in ( select device_id from owner_device where removed is null and owner_id in ( %s ) )', $values );
 | 
        
           | 53 | rodolico | 208 |       }
 | 
        
           | 70 | rodolico | 209 |   | 
        
           | 56 | rodolico | 210 |       if ( isset( $temp['location'] ) ) {
 | 
        
           | 53 | rodolico | 211 |          $values = $dbConnection->columnToArray( sprintf( "select distinct location_id id from view_device_location_owner_type where location in (%s)", implode( ',', $temp['location'] ) ) );
 | 
        
           | 70 | rodolico | 212 |          $values = implode( ',', $values );
 | 
        
           |  |  | 213 |          $_SESSION['restrictions']['Location'][] = sprintf( 'location_id in ( %s )', $values );
 | 
        
           |  |  | 214 |          $_SESSION['restrictions']['Device'][] = sprintf( 'device_id in ( select device_id from location_device where removed is null and location_id in (%s) )', $values );
 | 
        
           | 53 | rodolico | 215 |       }
 | 
        
           | 56 | rodolico | 216 |       if ( isset( $temp['device'] ) ) {
 | 
        
           | 53 | rodolico | 217 |          $values = $dbConnection->columnToArray( sprintf( "select distinct device_id id from view_device_location_owner_type where device in (%s)", implode( ',', $temp['device'] ) ) );
 | 
        
           | 70 | rodolico | 218 |          $values = implode( ',', $values );
 | 
        
           |  |  | 219 |          $_SESSION['restrictions']['Device'][] = sprintf( 'device_id in ( %s )', $values );
 | 
        
           | 53 | rodolico | 220 |       }
 | 
        
           | 70 | rodolico | 221 |       //print "<pre>Restrictions\n" . print_r( $_SESSION['restrictions'], true ) . '</pre>'; die;
 | 
        
           |  |  | 222 |    } // buildRestrictions
 | 
        
           | 45 | rodolico | 223 |   | 
        
           | 70 | rodolico | 224 |   | 
        
           | 53 | rodolico | 225 |    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>" ) {
 | 
        
           |  |  | 226 |       $return = array();
 | 
        
           |  |  | 227 |       foreach ( $data as $key => $value ) {
 | 
        
           |  |  | 228 |          $return[] = sprintf( $template, $target, $key, $value );
 | 
        
           |  |  | 229 |       }
 | 
        
           |  |  | 230 |       return $before . implode( $inside , $return ) . $after;
 | 
        
           |  |  | 231 |    }
 | 
        
           | 22 | rodolico | 232 |   | 
        
           | 45 | rodolico | 233 |   | 
        
           |  |  | 234 |   | 
        
           | 1 | rodolico | 235 |   | 
        
           |  |  | 236 | ?>
 |