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