| 1 | 
           rodolico | 
           1 | 
           <?php
  | 
        
        
            | 
            | 
           2 | 
              | 
        
        
            | 
            | 
           3 | 
              define(VERSION,'1.5.2');
  | 
        
        
            | 
            | 
           4 | 
              define(BUILD_DATE,'20091010');
  | 
        
        
            | 
            | 
           5 | 
              | 
        
        
            | 
            | 
           6 | 
              include_once("database.php");
  | 
        
        
            | 
            | 
           7 | 
              | 
        
        
            | 
            | 
           8 | 
              include_once("library.php");
  | 
        
        
            | 
            | 
           9 | 
              include_once('reports.php');
  | 
        
        
            | 
            | 
           10 | 
              | 
        
        
            | 
            | 
           11 | 
              global $MODULE_REPORTS;
  | 
        
        
            | 
            | 
           12 | 
              $MODULE_REPORTS = array('main device screen' => 1);
  | 
        
        
            | 
            | 
           13 | 
              | 
        
        
            | 
            | 
           14 | 
              define (SQL_SHOW_SITES,
  | 
        
        
            | 
            | 
           15 | 
                 "select concat('<a href=\"index.html?site_id=',site.site_id,'\">', site.name,'</a>') 'Site',
  | 
        
        
            | 
            | 
           16 | 
                         count(*) 'Devices',
  | 
        
        
            | 
            | 
           17 | 
                         concat('<a href=\"edit.html?site_id=',site.site_id,'\">Edit</a>') Action
  | 
        
        
            | 
            | 
           18 | 
                  from client_site site left outer join current_systems device using (site_id)
  | 
        
        
            | 
            | 
           19 | 
                  where <whereClause>
  | 
        
        
            | 
            | 
           20 | 
                  group by site.site_id
  | 
        
        
            | 
            | 
           21 | 
                  order by site.name"
  | 
        
        
            | 
            | 
           22 | 
              );
  | 
        
        
            | 
            | 
           23 | 
              | 
        
        
            | 
            | 
           24 | 
              define (SQL_SHOW_DEVICES,
  | 
        
        
            | 
            | 
           25 | 
                 "select concat('<a href=\"show_device.html?device_id=',device.device_id,'\">',device.name,'</a>') 'Device',
  | 
        
        
            | 
            | 
           26 | 
                  device_type.name 'Type',
  | 
        
        
            | 
            | 
           27 | 
                  concat('<a href=\"edit.html?device_id=',device.device_id,'\">Edit</a>') Action
  | 
        
        
            | 
            | 
           28 | 
                  from device join device_type on device.device_type_id = device_type.device_type_id
  | 
        
        
            | 
            | 
           29 | 
                       join site on device.site_id = site.site_id
  | 
        
        
            | 
            | 
           30 | 
                       join client on site.client_id = client.client_id
  | 
        
        
            | 
            | 
           31 | 
                  where device_type.show_as_system = 'Y' 
  | 
        
        
            | 
            | 
           32 | 
                        and device.removed_date is null
  | 
        
        
            | 
            | 
           33 | 
                        and <whereClause>
  | 
        
        
            | 
            | 
           34 | 
                  order by device_type.name,device.name"
  | 
        
        
            | 
            | 
           35 | 
              );
  | 
        
        
            | 
            | 
           36 | 
              | 
        
        
            | 
            | 
           37 | 
              define (SQL_SHOW_CLIENTS,
  | 
        
        
            | 
            | 
           38 | 
                 "select max(concat('<a href=\"index.html?client_id=',client.client_id,'\">',client.name,'</a>')) 'Client', 
  | 
        
        
            | 
            | 
           39 | 
                         count(*) 'Sites',
  | 
        
        
            | 
            | 
           40 | 
                         concat('<a href=\"edit.html?client_id=',client.client_id,'\">Edit</a>') Action
  | 
        
        
            | 
            | 
           41 | 
                  from client left outer join site using (client_id)
  | 
        
        
            | 
            | 
           42 | 
                  where site.removed_date is null and
  | 
        
        
            | 
            | 
           43 | 
                        <whereClause>
  | 
        
        
            | 
            | 
           44 | 
                  group by client.client_id
  | 
        
        
            | 
            | 
           45 | 
                  order by client.name"
  | 
        
        
            | 
            | 
           46 | 
              );
  | 
        
        
            | 
            | 
           47 | 
              | 
        
        
            | 
            | 
           48 | 
              define (SQL_SHOW_DEVICE, '
  | 
        
        
            | 
            | 
           49 | 
                 select device.device_id "ID",
  | 
        
        
            | 
            | 
           50 | 
                        concat(client.name, \' - \', site.name) "Site",
  | 
        
        
            | 
            | 
           51 | 
                        device_type.name "Type",
  | 
        
        
            | 
            | 
           52 | 
                        device.name "Name",
  | 
        
        
            | 
            | 
           53 | 
                        device.notes "Notes",
  | 
        
        
            | 
            | 
           54 | 
                        partof.name "Part Of",
  | 
        
        
            | 
            | 
           55 | 
                        date(device.added_date) "Added",
  | 
        
        
            | 
            | 
           56 | 
                        date(device.removed_date) "Removed"
  | 
        
        
            | 
            | 
           57 | 
                 from device join site on device.site_id = site.site_id 
  | 
        
        
            | 
            | 
           58 | 
                      join client on site.client_id = client.client_id 
  | 
        
        
            | 
            | 
           59 | 
                      join device_type on device.device_type_id = device_type.device_type_id 
  | 
        
        
            | 
            | 
           60 | 
                      left outer join device as partof on partof.device_id = device.part_of
  | 
        
        
            | 
            | 
           61 | 
                 where device.device_id = <device_id>
  | 
        
        
            | 
            | 
           62 | 
              ');
  | 
        
        
            | 
            | 
           63 | 
              | 
        
        
            | 
            | 
           64 | 
              $LOGIN_PAGE = $_SESSION['html root'] . '/login.html';
  | 
        
        
            | 
            | 
           65 | 
              | 
        
        
            | 
            | 
           66 | 
              function verifyLogin( $username, $password ) {
  | 
        
        
            | 
            | 
           67 | 
                 $sql = "select login_id login_id, email, where_clause
  | 
        
        
            | 
            | 
           68 | 
                         from login 
  | 
        
        
            | 
            | 
           69 | 
                         where email = " . makeSafeSQLValue($username) . ' 
  | 
        
        
            | 
            | 
           70 | 
                               and pass = md5(' . makeSafeSQLValue($password) . ") 
  | 
        
        
            | 
            | 
           71 | 
                               and removed_date is null";
  | 
        
        
            | 
            | 
           72 | 
                 $info = queryDatabaseExtended( $sql );
  | 
        
        
            | 
            | 
           73 | 
                 if ( $info['count'] == 1 ) {
  | 
        
        
            | 
            | 
           74 | 
                    $info = $info['data'][0];
  | 
        
        
            | 
            | 
           75 | 
                    $_SESSION['login_id'] = ($info['login_id'] ? $info['login_id'] : -1);
  | 
        
        
            | 
            | 
           76 | 
                    $_SESSION['email'] = $info['email'];
  | 
        
        
            | 
            | 
           77 | 
                    $_SESSION['where_clause'] = $info['where_clause'];
  | 
        
        
            | 
            | 
           78 | 
                    redirectPage('index.html');
  | 
        
        
            | 
            | 
           79 | 
                 } else {
  | 
        
        
            | 
            | 
           80 | 
                    return false;
  | 
        
        
            | 
            | 
           81 | 
                 }
  | 
        
        
            | 
            | 
           82 | 
              }
  | 
        
        
            | 
            | 
           83 | 
              | 
        
        
            | 
            | 
           84 | 
              function setAuth ( $whereClause = 'true' ) {
  | 
        
        
            | 
            | 
           85 | 
                 if ( iAmAdministrator() ) return $whereClause;
  | 
        
        
            | 
            | 
           86 | 
                 $whereClause = " ($whereClause)"; // ensure that the whereClause passed will not override our limits
  | 
        
        
            | 
            | 
           87 | 
                 switch ( $_SESSION['where_clause'] ) {
  | 
        
        
            | 
            | 
           88 | 
                    case '' : $whereClause .= ' and false'; // no login allowed if empty string
  | 
        
        
            | 
            | 
           89 | 
                             break;
  | 
        
        
            | 
            | 
           90 | 
                    default : $whereClause .= ' and ' . $_SESSION['where_clause'];
  | 
        
        
            | 
            | 
           91 | 
                 }
  | 
        
        
            | 
            | 
           92 | 
                 return $whereClause;
  | 
        
        
            | 
            | 
           93 | 
              }
  | 
        
        
            | 
            | 
           94 | 
              | 
        
        
            | 
            | 
           95 | 
              function iAmAdministrator () {
  | 
        
        
            | 
            | 
           96 | 
                 return ($_SESSION['where_clause'] == 'ADMINISTRATOR');
  | 
        
        
            | 
            | 
           97 | 
              }
  | 
        
        
            | 
            | 
           98 | 
              | 
        
        
            | 
            | 
           99 | 
              /*
  | 
        
        
            | 
            | 
           100 | 
                 function takes a screen name and looks it up in $MODULE_REPORTS to translate to a bit position.
  | 
        
        
            | 
            | 
           101 | 
                 It will then search the report table in the database to determine which reports need to be run, passing the values in the 
  | 
        
        
            | 
            | 
           102 | 
                 $parameters array to the run command, can capturing the output.
  | 
        
        
            | 
            | 
           103 | 
                 function then returns the html output of the report(s) back to the calling program, which can then paste it into the current
  | 
        
        
            | 
            | 
           104 | 
                 screen.
  | 
        
        
            | 
            | 
           105 | 
                 The concept is that a particular screen may need some sub reports. For example, the display device screen in the Main module
  | 
        
        
            | 
            | 
           106 | 
                 will want to display the information about the device taken from the device_attrib table. To do this, a report is defined
  | 
        
        
            | 
            | 
           107 | 
                 using the query 
  | 
        
        
            | 
            | 
           108 | 
                    select attrib.name,device_attrib.value 
  | 
        
        
            | 
            | 
           109 | 
                    from device_attrib join attrib using (attrib_id) 
  | 
        
        
            | 
            | 
           110 | 
                    where device_id = <device_id> and device_attrib.removed_date is null
  | 
        
        
            | 
            | 
           111 | 
                 where <device_id> is replaced by the report class.
  | 
        
        
            | 
            | 
           112 | 
                 The calling routine would pass 'device_id' => '1' in the parameters array (if the current device had a device_id of 1) and this
  | 
        
        
            | 
            | 
           113 | 
                 routine would run the report and return the results.
  | 
        
        
            | 
            | 
           114 | 
                 This allows newer modules to add reports to existing screens simply by creating a report and setting up parameters correctly.
  | 
        
        
            | 
            | 
           115 | 
                 BY CONVENTION, the following parameters are passed, if applicable:
  | 
        
        
            | 
            | 
           116 | 
                    device_id      - numeric ID of the device in question
  | 
        
        
            | 
            | 
           117 | 
                    device_name    - ascii name of the device in question
  | 
        
        
            | 
            | 
           118 | 
                    site_id        - numeric ID of the site in question
  | 
        
        
            | 
            | 
           119 | 
                    client_id      - numeric ID of the client in question
  | 
        
        
            | 
            | 
           120 | 
                    added_date     - value for added_date
  | 
        
        
            | 
            | 
           121 | 
                    removed_date   - value for removed_date
  | 
        
        
            | 
            | 
           122 | 
                Not all reports will use the above values, but if they are passed in to the parameters array, they will not cause problems with
  | 
        
        
            | 
            | 
           123 | 
                the report
  | 
        
        
            | 
            | 
           124 | 
              | 
        
        
            | 
            | 
           125 | 
                NOTE: the reports will still run in interactive mode. In the above query, it will ask for the device.
  | 
        
        
            | 
            | 
           126 | 
              */
  | 
        
        
            | 
            | 
           127 | 
              function screenReports ( $screenName, $parameters = array(), $showTitle = false ) {
  | 
        
        
            | 
            | 
           128 | 
                 global $MODULE_REPORTS;
  | 
        
        
            | 
            | 
           129 | 
                 $result = '';
  | 
        
        
            | 
            | 
           130 | 
                 if ($MODULE_REPORTS[$screenName]) {
  | 
        
        
            | 
            | 
           131 | 
                    $sql = 'select report_id from report where screen_report = ' . $MODULE_REPORTS[$screenName];
  | 
        
        
            | 
            | 
           132 | 
                    $reportIDs = sqlValuesToKeys ($sql);
  | 
        
        
            | 
            | 
           133 | 
                    // print "<pre>"; print_r( $parameters ); print "</pre>";
  | 
        
        
            | 
            | 
           134 | 
                    foreach ( $reportIDs as $thisReport => $data ) {
  | 
        
        
            | 
            | 
           135 | 
                       $report = new Report;
  | 
        
        
            | 
            | 
           136 | 
                       $report->loadFromDatabase ( $thisReport );
  | 
        
        
            | 
            | 
           137 | 
                       $result .= $report->run($parameters, '', $showTitle );
  | 
        
        
            | 
            | 
           138 | 
                    }
  | 
        
        
            | 
            | 
           139 | 
                 }
  | 
        
        
            | 
            | 
           140 | 
                 return $result;
  | 
        
        
            | 
            | 
           141 | 
              }
  | 
        
        
            | 
            | 
           142 | 
              | 
        
        
            | 
            | 
           143 | 
              if ($_SESSION['file system root']) { // this is only set if we have logged in
  | 
        
        
            | 
            | 
           144 | 
                 $InstalledModules = array();
  | 
        
        
            | 
            | 
           145 | 
                 // get module information
  | 
        
        
            | 
            | 
           146 | 
                 $data = queryDatabaseExtended( "select theValue from _system where removed_date is null and group_name = 'Modules'");
  | 
        
        
            | 
            | 
           147 | 
                 if ($data) {
  | 
        
        
            | 
            | 
           148 | 
                    foreach ($data['data'] as $row) {
  | 
        
        
            | 
            | 
           149 | 
                       $InstalledModules[] = $row['theValue'];
  | 
        
        
            | 
            | 
           150 | 
                    }
  | 
        
        
            | 
            | 
           151 | 
                    // note, we are only going to include the datagase.php. All other stuff is left to the individual modules
  | 
        
        
            | 
            | 
           152 | 
                    // $_SESSION['file system root'] is set in login.php, and is the path to the root of this application, so all else is relative
  | 
        
        
            | 
            | 
           153 | 
                    foreach ($InstalledModules as $directory) {
  | 
        
        
            | 
            | 
           154 | 
                       include_once( $_SESSION['file system root'] . "/$directory/database.php" );
  | 
        
        
            | 
            | 
           155 | 
                    }
  | 
        
        
            | 
            | 
           156 | 
                 }
  | 
        
        
            | 
            | 
           157 | 
              }
  | 
        
        
            | 
            | 
           158 | 
              | 
        
        
            | 
            | 
           159 | 
           ?>
  |