Subversion Repositories computer_asset_manager_v1

Rev

Rev 47 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
46 rodolico 1
<?php
2
 
3
   define(VERSION,'1.0.0');
4
   define(BUILD_DATE,'20170709');
5
 
6
   include_once("database.php");
7
 
8
   include_once("library.php");
9
   include_once('reports.php');
10
 
11
   define (SQL_SHOW_LICENSE,
12
      "select 
13
                  client.name 'Client',
47 rodolico 14
                  concat('<a href=\"edit.html?action=edit&license_id=',license.license_id,'\">', license.license,'</a>') 'License',
46 rodolico 15
                  device.name 'Installed On',
16
                  license_product.name 'Product',
17
                  license.added_date 'Installed',
47 rodolico 18
                  license.removed_date 'Removed',
46 rodolico 19
                  concat('<a href=\"index.html?action=move&license_id=',license.license_id,'\">', 'Move','</a>') 'Action'
20
       from
21
                  license join license_product using (license_product_id)
22
                  left outer join device using (device_id)
23
                  join client using ( client_id )
24
       where <whereClause>
25
       order by 
26
          client.name,
27
          device.name,
28
          license_product.name,
29
          license.license"
30
      );
31
 
32
   define (SQL_SHOW_LICENSE_HISTORY,
33
      "select 
34
                  client.name 'Client',
47 rodolico 35
                  concat('<a href=\"edit.html?license_id=',license.license_id,'\">', license.license,'</a>') 'License',
46 rodolico 36
                  device.name 'Installed On',
37
                  license_product.name 'Product',
38
                  license.added_date 'Installed',
39
                  license.removed_date 'Removed'
40
       from
41
                  license join license_product using (license_product_id)
42
                  left outer join device using (device_id)
43
                  join client using ( client_id )
44
       where <whereClause>
45
       order by 
46
          client.name,
47
          device.name,
48
          license_product.name,
49
          license.license"
50
        );
51
 
52
/*
53
 * Adds/updates license in license table
54
 * if already licensed for this machine, returns 'Already Set'
55
 * if license exists and is for a different machine
56
 *    mark it as removed
57
 *    adds key to new machine
58
 *    returns "Assigned"
59
 * Othewise, Adds key and assigns to machine
60
 * returns "Added"
61
 * NOTE: $device_id may be null which indicates license owned by client but unassigned
62
 */
63
function updateLicense ( $client_id, $device_id, $license_product_id, $license ) {
51 rodolico 64
   $client_id = makeSafeSQLConstant( $client_id, 'I' );
65
   $device_id = makeSafeSQLConstant( $device_id, 'I', 'null' );
66
   $license_product_id = makeSafeSQLConstant( $license_product_id, 'I' );
67
   $license = makeSafeSQLConstant( $license );
68
 
46 rodolico 69
   // see if the entry already exists
51 rodolico 70
   $results = queryDatabaseExtended( "select * from license where license_product_id = $license_product_id and license = $license and removed_date is null" );
46 rodolico 71
   //print "<pre>"; print_r( $results ); print "</pre>"; die;
72
   $db_license_id = $results['data'][0]['license_id'];
73
   $db_client_id = $results['data'][0]['client_id'];
51 rodolico 74
   $db_device_id = $results['data'][0]['device_id'] ? $results['data'][0]['device_id'] : 'null';
75
 
46 rodolico 76
   if ( ! $results ) { # this was not found, so just add it
51 rodolico 77
      doSQL( "insert into license (client_id,device_id,license_product_id,license, added_date, removed_date) values ( $client_id, $device_id, $license_product_id, $license, now(), null )" );
46 rodolico 78
      return "Added";
79
   }
80
   if ( $client_id == $db_client_id && $device_id == $db_device_id or $db_device_id  ) { // already done, so just leave alone
81
      return "Already Set";
82
   }
83
   if ( ! $db_device_id ) { # key was not assigned before, so just assign it
84
      doSQL( "update license set device_id = $queryDeviceID,added_date = now() where license_id = $db_license_id" );
85
      return "Assigned";
86
   }
87
   // at this point, there is already an entry, but it is for a different machine, so we need to update it, ie remove the old, add the new
88
   doSQL( "update license set removed_date = now() where license_id = $db_license_id" );
51 rodolico 89
   doSQL( "insert into license (client_id,device_id,license_product_id,license, added_date, removed_date) values ( $client_id, $device_id, $license_product_id, $license, $added_date, $removed_date )" );
46 rodolico 90
   return "Reassigned";
91
}
92
 
47 rodolico 93
/*
94
 * Simply overwrites a license record. Only used for the edit.html page
95
 */
96
 
97
function overwriteLicense ( $license_id,$client_id, $device_id, $license_product_id, $license, $added_date, $removed_date ) {
98
   $added_date = makeSafeSQLConstant( $added_date, 'D', 'now()' );
99
   $removed_date = makeSafeSQLConstant( $removed_date, 'D', 'null' );
51 rodolico 100
   $license = makeSafeSQLConstant( $license );
47 rodolico 101
   $device_id = $device_id == '-1' ? 'null' : makeSafeSQLConstant( $device_id, 'I', 'null' );
51 rodolico 102
   print "Added: $added_date<br>Removed: $removed_date<br>license: $license<br>Device: $device_id<br>";
103
   if ( $license_id == 'null' ) {
104
      queryDatabaseExtended( 
105
         "insert into license (client_id,device_id,license_product_id,license,added_date,removed_date)
106
          values ( $client_id, $device_id, $license_product_id, $license, $added_date, $removed_date )" );
107
   } else {
108
      doSQL( "update license set 
109
                  client_id=$client_id,
110
                  device_id=$device_id,
111
                  license_product_id=$license_product_id,
112
                  license=$license,
113
                  added_date=$added_date,
114
                  removed_date=$removed_date
115
               where license_id=$license_id");
116
   }
47 rodolico 117
} // overwriteLicense
118
 
119
 
120
/*
121
 * function will attempt to make a constant ($value) safe for SQL depending on the type.
122
 * 
123
 * if $value is empty, $default is returned, as will happen if any of the
124
 * conversions (date, datetime, etc...) fail.
125
 * 
126
 * First, it will pass it through get_magic_quotes_gpc, 
127
 * then will run through mysql_real_escape_string
128
 * 
129
 * For strings, will encapsulate in quotes
130
 * Dates will attempt a conversion, then change to YYYY-MM-DD and encapsulate in quotes
131
 * DateTime will perform the same, but change to YYYY-MM-DD HH:MM:SS
132
 * Integer and Floats are passed through builtins intval and floatval
133
 * Boolean only checks the first character, a '0', 'f' and 'n' denoting false
134
 *    all else denoting true. The result is converted based on the variable
135
 *    $falsetrue, with the first char denoting false and the second denoting true
136
 */
137
function makeSafeSQLConstant ( $value, $type='S', $default='null', $falsetrue='10' ) {
138
   if (strlen($value) == 0) // simply set any empty values to null
139
      return $default;
51 rodolico 140
   // print "Processing $value as $type with default $default<br>\n";
47 rodolico 141
   switch ( strtolower( $type ) ) {
142
      case 'string' :
51 rodolico 143
      case 's' : 
144
               if ( get_magic_quotes_gpc() ) 
145
                  $value = stripslashes($value);
146
               $value = mysql_real_escape_string( $value );
147
               $value = strlen( $value ) > 0 ? "'$value'" : $default;
148
               break;
47 rodolico 149
      case 'date' :
51 rodolico 150
      case 'd' :
151
               if ( $value != 'null' ) {
152
                  $result = strtotime( $value );
153
                  $value = ( $result === false ) ? $default : "'" . Date( 'Y-m-d', $result) . "'";
154
               }
155
               break;
47 rodolico 156
      case 'datetime':
157
      case 'timestamp':
51 rodolico 158
      case 'dt': 
159
               if ( $value != 'null' ) {
160
                  $result = strtotime( $value );
161
                  $value = ( $result === false ) ? $default : "'" . Date( 'Y-m-d H:i:s', $result) . "'";
162
               }
163
               break;
47 rodolico 164
      case 'integer':
51 rodolico 165
      case 'i' :  
166
               $value = intval( $value );
167
               break;
47 rodolico 168
      case 'float':
51 rodolico 169
      case 'f' :  
170
               $value = floatval( $value );
171
               break;
47 rodolico 172
      case 'bool':
173
      case 'boolean':
174
      case 'b' :  // note, because of the way strpos works, you can not
175
                  // simply set $value based on the output; you MUST do
176
                  // as below; specifically check for false, then set the result
177
                  $value =  strpos( '0fn', strtolower(substr( $value, 0, 1 )) ) === false ? 0 : 1;
178
                  $value = substr( $falsetrue, $value, 0, 1 );
179
                  break;
180
   } // switch
181
   return $value;
182
}
183
 
46 rodolico 184
/* 
185
 * select $column from $table where $match = '$value'
186
 * if $add is true, will add row if it does not exist.
187
 * returns $column from the result (or '' if it does not exist)
188
 * Used mainly to get an index from a table with matching value
189
 */
190
 
191
function getValue ( $table, $column, $match, $value, $add = false ) {
51 rodolico 192
   $value = makeSafeSQLConstant( $value );
47 rodolico 193
   $return = getOneDBValue( "select $column from $table where $match = $value" );
46 rodolico 194
   if ( $return === null ) {
195
      if ( $add ) {
47 rodolico 196
         $return = doSQL( "insert into $table ( $match ) values ( $value )" );
46 rodolico 197
         return $return['insert_id'];
198
      } else {
199
         $return = '';
200
      } // if..else
201
   } // if
202
   return $return;
203
} // function getValue
204
 
205
function getDescription( $license_id ) {
51 rodolico 206
   $license_id = makeSafeSQLConstant( $license_id, 'I' );
46 rodolico 207
   $result = queryDatabaseExtended( 
208
           "select 
209
               license_product.name 'Product',
210
               license.license 'License',
211
               device.name 'Machine',
212
               client.name 'Client'
213
            from
214
               license join license_product using (license_product_id)
215
               left outer join device using (device_id)
216
               join client using ( client_id )
217
            where 
218
               license.removed_date is null
219
               and license.license_id = $license_id"
220
            );
221
   if ( $result['count'] ) {
222
      $return = 'Product: ' . $result['data'][0]['Product'] . "\n";
223
      $return .= 'Client:  ' . $result['data'][0]['Client'] . "\n";
224
      $return .= 'Machine:  ' . ( $result['data'][0]['Machine'] ? $result['data'][0]['Machine'] : 'Not Currently Assigned' ) . "\n";
225
      $return .= 'License:  ' . $result['data'][0]['License'] . "\n";
226
      return $return;
227
   } else {
228
      return "No License found for license_id $license_id";
229
   }
230
} // function getDescription
231
 
47 rodolico 232
/*
233
 * Moves a license from one machine to another (either can be null)
234
 * by setting removed_date in the origin, then duplicating that record
235
 * to a new one
236
 */
237
 
238
function moveLicense( $license_id, $machine_id = '' ) {
51 rodolico 239
   $machine_id = makeSafeSQLConstant( $machine_id, 'I' );
240
   $license_id = makeSafeSQLConstant( $license_id, 'I' );
47 rodolico 241
   doSQL( 
242
      "insert into license
243
         ( client_id, device_id,license,license_product_id,added_date,removed_date ) 
244
         select client_id, $machine_id,license,license_product_id,now(),null 
245
         from license 
246
         where license_id = $license_id" );
247
   doSQL( "update license set removed_date = now() where license_id = $license_id" );
248
} // function moveLicense
249
 
250
/*
251
 * This was stolen from doAdmin in the library.php file. That library can not
252
 * be easily touched as too many other things depend on it, so I simply grabbed the
253
 * code, modified it, and put it here.
254
 * Does a very basic edit of table $tablename, based on its definition stored in
255
 * $DATABASE_DEFINITION
256
 */
257
function EditTable( $tablename, $allowRemove = false ) {
258
   global $DATABASE_DEFINITION;
259
   $currentDB = $tablename;
260
   $dbDisplayName = $DATABASE_DEFINITION[$currentDB]['display name'] ? $DATABASE_DEFINITION[$currentDB]['display name'] : $currentDB;
261
   // load our two global parameters, check for get, then post
262
   $id = $_REQUEST['id'];
263
   $mode = $_REQUEST['mode'];
264
   $mode = escapeshellcmd( $mode );
265
   $id = escapeshellcmd( $id );
266
 
267
   if ( $mode=="add") {
268
      Print '<h2>Add $dbDisplayName</h2>';
269
      print addData( $DATABASE_DEFINITION[$currentDB] );
270
   } 
271
 
272
   if ( $mode=="added") 
273
   {
274
     print insertData( $DATABASE_DEFINITION[$currentDB] );
275
     print "<p>Record Added</p>";
276
   }
277
   if ( $mode=="edit") 
278
   { 
279
     print "<h2>Edit $dbDisplayName</h2>";
280
     print editData( $DATABASE_DEFINITION[$currentDB], $id );
281
   } 
282
 
283
   if ( $mode=="edited") { 
284
   updateData( $DATABASE_DEFINITION[$currentDB], $id );
285
   Print "<p>$currentDB Updated!</p>p>";
286
   }
287
   if ( $mode=="remove") {
288
      if ( $allowRemove ) {
289
         print deleteData( $DATABASE_DEFINITION[$currentDB], $id );
290
         Print "$currentDB has been removed <p>";
291
      } else {
292
         print "<p>Error, no deletions allowed here</p>";
293
      }
294
   }
295
   Print "<h2>$dbDisplayName</h2><p>";
296
 
297
   print makeList( $currentDB, $DATABASE_DEFINITION[$currentDB]['display query'], $DATABASE_DEFINITION[$currentDB]['display columns'], $DATABASE_DEFINITION[$currentDB]['key field'] );
298
}
299
 
300
 
46 rodolico 301
?>