Subversion Repositories computer_asset_manager_v1

Rev

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