Subversion Repositories computer_asset_manager_v1

Rev

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