| 43 | rodolico | 1 | <?php 
 | 
        
           | 46 | rodolico | 2 |    include_once( '../../header.php' );
 | 
        
           |  |  | 3 |    include_once( './functions.php' );
 | 
        
           | 43 | rodolico | 4 | ?>
 | 
        
           | 41 | rodolico | 5 | <?php
 | 
        
           |  |  | 6 |   | 
        
           |  |  | 7 | include_once( '../../header.php' ); 
 | 
        
           |  |  | 8 | include_once( 'csvImporter.php' );
 | 
        
           |  |  | 9 |   | 
        
           | 43 | rodolico | 10 | /* import CSV, processing one line at a time
 | 
        
           |  |  | 11 |  * CSV may be delimited by anything defined in CsvImporter with auto-detect
 | 
        
           |  |  | 12 |  * returns table with results, suitable for embedding in div
 | 
        
           |  |  | 13 |  */
 | 
        
           | 42 | rodolico | 14 |   | 
        
           | 45 | rodolico | 15 |    function processFile ( $filename, $delimiter, $encapsulation = '"', $escape = '\\', $deviceNotFoundIsNull = false) {
 | 
        
           | 43 | rodolico | 16 |       $return = '';
 | 
        
           | 45 | rodolico | 17 |       $fileInfo = new CsvImporter( $filename, true, $delimiter, $enclosure, $escape );
 | 
        
           | 69 | rodolico | 18 |       $numLines = 0;
 | 
        
           | 45 | rodolico | 19 |   | 
        
           |  |  | 20 |       /*
 | 
        
           |  |  | 21 |        * load the information in a hash list, similar to
 | 
        
           |  |  | 22 |        * client1
 | 
        
           |  |  | 23 |        *    machine1
 | 
        
           |  |  | 24 |        *       product1
 | 
        
           |  |  | 25 |        *       product2
 | 
        
           |  |  | 26 |        *    machine2
 | 
        
           |  |  | 27 |        *       productx
 | 
        
           |  |  | 28 |        * client2
 | 
        
           |  |  | 29 |        *    ...
 | 
        
           |  |  | 30 |        */
 | 
        
           | 43 | rodolico | 31 |       while ( $line = $fileInfo->get(1) ) {
 | 
        
           |  |  | 32 |          foreach ( $line as $index => $values ) {
 | 
        
           | 69 | rodolico | 33 |             $import[$values['client']][$values['license_product']][$values['license']] = $values['device'];
 | 
        
           |  |  | 34 |             $numLines++;
 | 
        
           | 43 | rodolico | 35 |          }
 | 
        
           |  |  | 36 |       };
 | 
        
           | 69 | rodolico | 37 |       $return .= "<p>$numLines lines read</p>";
 | 
        
           |  |  | 38 |       $numLines = 0; // reset so we can count the number of lines processed
 | 
        
           | 45 | rodolico | 39 |       // process each client/machine/license in turn, displaying results in table
 | 
        
           |  |  | 40 |       $return .= "<table border='1'><tr></tr><th>Status</th><th>Client</th><th>ID</th><th>Machine</th><th>ID</th><th>Product</th><th>ID</th><th>License</th></tr>\n";
 | 
        
           | 43 | rodolico | 41 |       foreach ( $import as $client => $data ) {
 | 
        
           |  |  | 42 |          // find client_id
 | 
        
           |  |  | 43 |          $client_id = getValue ( 'client', 'client_id', 'name', $client );
 | 
        
           | 45 | rodolico | 44 |          if ( $client_id === '' ) { // can't find client, so big error
 | 
        
           | 43 | rodolico | 45 |             $return .= "<tr><td><b>Error</b></td><td colspan='7'>Could not find client ID for $client</td></tr>";
 | 
        
           | 41 | rodolico | 46 |             continue;
 | 
        
           |  |  | 47 |          }
 | 
        
           | 69 | rodolico | 48 |          foreach ( $data as $product => $info ) {
 | 
        
           |  |  | 49 |             foreach ( $info as $license => $machine ) {
 | 
        
           |  |  | 50 |                $numLines++;
 | 
        
           |  |  | 51 |                if ( $machine === '' ) { // can't find machine
 | 
        
           |  |  | 52 |                   $machine_id = '';
 | 
        
           |  |  | 53 |                } else {
 | 
        
           |  |  | 54 |                   $machine_id = getValue( 'device', 'device_id', 'name', $machine );
 | 
        
           |  |  | 55 |                   // if we can't find it and they don't want to continue
 | 
        
           |  |  | 56 |                   if ( $machine_id === '' and ! $deviceNotFoundIsNull ) { 
 | 
        
           |  |  | 57 |                      $return .= "<td><b>Error</b></td><td>$client</td><td>$client_id</td>";
 | 
        
           |  |  | 58 |                      $return .= "<td colspan='5'>Could not find machine ID for $machine</td></tr>";
 | 
        
           |  |  | 59 |                      continue; //ignor
 | 
        
           |  |  | 60 |                   }
 | 
        
           | 43 | rodolico | 61 |                }
 | 
        
           | 69 | rodolico | 62 |                // now, locate each license_product_id and update the license information
 | 
        
           |  |  | 63 |                // if product does not exist, add it
 | 
        
           |  |  | 64 |                // thus, spelling counts or you'll get dup entries in license_product table
 | 
        
           | 47 | rodolico | 65 |                # look for product name, but don't update it if it doesn't exist
 | 
        
           |  |  | 66 |                $product_id = getValue( 'license_product', 'license_product_id', 'name', $product, false );
 | 
        
           |  |  | 67 |                /*
 | 
        
           |  |  | 68 |                 * Some software (like Belarc) show product names as publiser - product name, ie
 | 
        
           |  |  | 69 |                 * Microsoft - Office 2017
 | 
        
           |  |  | 70 |                 * So, we'll look to see if this is the case here.
 | 
        
           |  |  | 71 |                 * NOTE: we are only checking if this key is publisher - product and, if so checking for product
 | 
        
           |  |  | 72 |                 *       however, if it is listed in database as publisher - product and we are just product, it 
 | 
        
           |  |  | 73 |                 *       will still result in dups.
 | 
        
           |  |  | 74 |                 */
 | 
        
           |  |  | 75 |                if ( ! $product_id ) { 
 | 
        
           |  |  | 76 |                   if ( strpos( $product, ' - ' ) !== false ) {
 | 
        
           |  |  | 77 |                      $product_id = getValue( 'license_product', 'license_product_id', 'name', substr( $product, strpos( $product, ' - ' ) + 3 ), false );
 | 
        
           |  |  | 78 |                   }
 | 
        
           |  |  | 79 |                }
 | 
        
           |  |  | 80 |                // if we still haven't found it, just add the bloody thing.
 | 
        
           |  |  | 81 |                if ( ! $product_id )
 | 
        
           |  |  | 82 |                   $product_id = getValue( 'license_product', 'license_product_id', 'name', $product, true );
 | 
        
           |  |  | 83 |   | 
        
           | 43 | rodolico | 84 |                $action = updateLicense( $client_id, $machine_id, $product_id, $license );
 | 
        
           |  |  | 85 |                $return .= "<tr>\n";
 | 
        
           |  |  | 86 |                $return .= "<td>$action</td><td>$client</td><td>$client_id</td>";
 | 
        
           |  |  | 87 |                $return .= "<td>$machine</td><td>$machine_id</td>";
 | 
        
           |  |  | 88 |                $return .= "<td>$product</td><td>$product_id</td><td>$license</td>\n";
 | 
        
           |  |  | 89 |                $return .= "</tr>\n";
 | 
        
           |  |  | 90 |   | 
        
           |  |  | 91 |             }
 | 
        
           |  |  | 92 |          } // for each machine
 | 
        
           |  |  | 93 |       } // for each client
 | 
        
           |  |  | 94 |       $return .= "</table>\n";
 | 
        
           | 69 | rodolico | 95 |       $return .= "<p>$numLines processed</p>";
 | 
        
           | 43 | rodolico | 96 |       return $return;
 | 
        
           |  |  | 97 |    } // function processFile
 | 
        
           | 41 | rodolico | 98 |   | 
        
           |  |  | 99 | ?>
 | 
        
           | 43 | rodolico | 100 | <?xml version="1.0" encoding="utf-8"?>
 | 
        
           |  |  | 101 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
 | 
        
           |  |  | 102 |   | 
        
           |  |  | 103 | <html xmlns="http://www.w3.org/1999/xhtml">
 | 
        
           |  |  | 104 |    <head>
 | 
        
           |  |  | 105 |       <title>Computer Asset Management Program - MODULE NAME - PAGE NAME</title>
 | 
        
           |  |  | 106 |       <link rel="stylesheet" type="text/css" href="../../camp.css">
 | 
        
           |  |  | 107 |    </head>
 | 
        
           |  |  | 108 |    <body>
 | 
        
           |  |  | 109 |       <?php include_once('../../menu.php'); ?>
 | 
        
           |  |  | 110 |       <div id="content">
 | 
        
           |  |  | 111 |       <?php 
 | 
        
           | 47 | rodolico | 112 |          if ( isset( $_POST["submit"] ) ) {
 | 
        
           | 45 | rodolico | 113 |             $filename = tempnam( '/tmp', 'blk' );
 | 
        
           |  |  | 114 |             if ( move_uploaded_file( $_FILES["fileToUpload"]["tmp_name"], $filename ) ) {
 | 
        
           |  |  | 115 |                print "<p>Loading Data</p>\n";
 | 
        
           |  |  | 116 |                print processFile( $filename , $_REQUEST['delimiter'], $_REQUEST['encapsulation'], $_REQUEST['escape'], $_REQUEST['notfoundisnull']  );
 | 
        
           |  |  | 117 |             } else {
 | 
        
           |  |  | 118 |                print "<p>Error: move_uploaded_file failed to move to $filename</p>";
 | 
        
           |  |  | 119 |             }
 | 
        
           |  |  | 120 |             //unlink( $filename );
 | 
        
           | 43 | rodolico | 121 |          } else {
 | 
        
           |  |  | 122 |       ?>
 | 
        
           | 45 | rodolico | 123 |       <p align\'center'>Upload a CSV file to be bulk imported into the license table</p>
 | 
        
           |  |  | 124 |       <p><b>Warning:</b> no error checking is done, ensure your file meets the proper specifications. The fields may be separated by</p>
 | 
        
           |  |  | 125 |       <ul>
 | 
        
           |  |  | 126 |          <li>comma</li>
 | 
        
           |  |  | 127 |          <li>semicolon</li>
 | 
        
           |  |  | 128 |          <li>tab</li>
 | 
        
           |  |  | 129 |          <li>pipe</li>
 | 
        
           |  |  | 130 |          <li>colon</li>
 | 
        
           |  |  | 131 |       </ul>
 | 
        
           |  |  | 132 |       <p>The first line should be a header, containing the following in any order. All other fields will be ignored</p>
 | 
        
           |  |  | 133 |       <ul>
 | 
        
           |  |  | 134 |          <li>client</li>
 | 
        
           |  |  | 135 |          <li>device</li>
 | 
        
           |  |  | 136 |          <li>license_product</li>
 | 
        
           |  |  | 137 |          <li>license</li>
 | 
        
           |  |  | 138 |       </ul>
 | 
        
           |  |  | 139 |       <p><b>Warning!</b> Client, Machine and Product name must be exactly as already stored.</p>
 | 
        
           | 103 | rodolico | 140 |       <a href="<?php print dirname($_SERVER['SCRIPT_NAME']) . '/license_master.csv'; ?>">Get master csv file</a>.
 | 
        
           |  |  | 141 |       This contains all license names as of 2020-01-15. Just delete any lines/columns you do not use.
 | 
        
           | 45 | rodolico | 142 |       <p>License is case sensitive</p>
 | 
        
           |  |  | 143 |       <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data">
 | 
        
           |  |  | 144 |          <table border='1'>
 | 
        
           |  |  | 145 |             <tr>
 | 
        
           |  |  | 146 |                <td>Set Machine to Null if not found</td>
 | 
        
           |  |  | 147 |                <td> <input type="checkbox" name="notfoundisnull" value="notfoundisnull"></td>
 | 
        
           |  |  | 148 |             </tr>
 | 
        
           |  |  | 149 |             <tr>
 | 
        
           |  |  | 150 |                <td>Select CSV to upload</td>
 | 
        
           |  |  | 151 |                <td><input type="file" name="fileToUpload" id="fileToUpload"></td>
 | 
        
           |  |  | 152 |             </tr>
 | 
        
           |  |  | 153 |             <tr>
 | 
        
           |  |  | 154 |                <td>Field Delimiters</td>
 | 
        
           |  |  | 155 |                <td>
 | 
        
           |  |  | 156 |                   <select name='delimiter' id='delimiter'>
 | 
        
           |  |  | 157 |                      <option value='auto' selected>Auto Detect</option>
 | 
        
           |  |  | 158 |                      <option value='tab'>Tab</option>
 | 
        
           |  |  | 159 |                      <option value=','>Comma (,)</option>
 | 
        
           |  |  | 160 |                      <option value=';'>Semicolon (;)</option>
 | 
        
           |  |  | 161 |                      <option value='|'>Pipe (|)</option>
 | 
        
           |  |  | 162 |                      <option value=':'>Colon (:)</option>
 | 
        
           |  |  | 163 |                   </select>
 | 
        
           |  |  | 164 |   | 
        
           |  |  | 165 |                </td>
 | 
        
           |  |  | 166 |             </tr>
 | 
        
           |  |  | 167 |             <tr>
 | 
        
           |  |  | 168 |                <td>Encapsulation</td>
 | 
        
           |  |  | 169 |                <td>
 | 
        
           |  |  | 170 |                   <select name='encapsulation' id='encapsulation'>
 | 
        
           |  |  | 171 |                      <option value='auto' selected>Auto Detect</option>
 | 
        
           |  |  | 172 |                      <option value=''>None</option>
 | 
        
           |  |  | 173 |                      <option value='"'>Double Quotes (")</option>
 | 
        
           |  |  | 174 |                      <option value="'">Single Quotes (')</option>
 | 
        
           |  |  | 175 |                      <option value='~'>Tilde (~)</option>
 | 
        
           |  |  | 176 |                   </select>
 | 
        
           |  |  | 177 |                </td>
 | 
        
           |  |  | 178 |             </tr>
 | 
        
           |  |  | 179 |             <tr>
 | 
        
           |  |  | 180 |                <td>Escape Char</td>
 | 
        
           |  |  | 181 |                <td>
 | 
        
           |  |  | 182 |                   <select name='escape' id='escape'>
 | 
        
           |  |  | 183 |                      <option value='' selected>None</option>
 | 
        
           |  |  | 184 |                      <option value='\'>Backslash (\)</option>
 | 
        
           |  |  | 185 |                   </select>
 | 
        
           |  |  | 186 |                </td>
 | 
        
           |  |  | 187 |             </tr>
 | 
        
           |  |  | 188 |             <tr>
 | 
        
           |  |  | 189 |                <td colspan='2' align='center'><input type="submit" value="Upload CSV" name="submit"></td>
 | 
        
           |  |  | 190 |             </tr>
 | 
        
           |  |  | 191 |           </table>
 | 
        
           | 43 | rodolico | 192 |       </form>
 | 
        
           | 45 | rodolico | 193 |       <p>It is generally safe to leave Encapsulation and Escape at their defaults</p>
 | 
        
           | 43 | rodolico | 194 |       <?php } ?>
 | 
        
           |  |  | 195 |       </div>
 | 
        
           |  |  | 196 |    </body>
 | 
        
           |  |  | 197 | </html>
 |