Subversion Repositories computer_asset_manager_v1

Rev

Rev 47 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 47 Rev 51
Line 59... Line 59...
59
 * Othewise, Adds key and assigns to machine
59
 * Othewise, Adds key and assigns to machine
60
 * returns "Added"
60
 * returns "Added"
61
 * NOTE: $device_id may be null which indicates license owned by client but unassigned
61
 * NOTE: $device_id may be null which indicates license owned by client but unassigned
62
 */
62
 */
63
function updateLicense ( $client_id, $device_id, $license_product_id, $license ) {
63
function updateLicense ( $client_id, $device_id, $license_product_id, $license ) {
64
   /*$client_id = makeSafeSQLValue( $client_id );
64
   $client_id = makeSafeSQLConstant( $client_id, 'I' );
65
   $device_id = makeSafeSQLValue( $device_id );
65
   $device_id = makeSafeSQLConstant( $device_id, 'I', 'null' );
66
   $license_product_id = makeSafeSQLValue( $license_product_id );
66
   $license_product_id = makeSafeSQLConstant( $license_product_id, 'I' );
67
   $license = makeSafeSQLValue( $license );
67
   $license = makeSafeSQLConstant( $license );
68
    */
68
   
69
   // see if the entry already exists
69
   // see if the entry already exists
70
   $results = queryDatabaseExtended( "select * from license where license_product_id = $license_product_id and license = '$license' and removed_date is null" );
70
   $results = queryDatabaseExtended( "select * from license where license_product_id = $license_product_id and license = $license and removed_date is null" );
71
   //print "<pre>"; print_r( $results ); print "</pre>"; die;
71
   //print "<pre>"; print_r( $results ); print "</pre>"; die;
72
   $db_license_id = $results['data'][0]['license_id'];
72
   $db_license_id = $results['data'][0]['license_id'];
73
   $db_client_id = $results['data'][0]['client_id'];
73
   $db_client_id = $results['data'][0]['client_id'];
74
   $db_device_id = $results['data'][0]['device_id'];
74
   $db_device_id = $results['data'][0]['device_id'] ? $results['data'][0]['device_id'] : 'null';
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';
-
 
-
 
75
   
77
   if ( ! $results ) { # this was not found, so just add it
76
   if ( ! $results ) { # this was not found, so just add it
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 )" );
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 )" );
79
      return "Added";
78
      return "Added";
80
   }
79
   }
81
   if ( $client_id == $db_client_id && $device_id == $db_device_id or $db_device_id  ) { // already done, so just leave alone
80
   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";
81
      return "Already Set";
83
   }
82
   }
Line 85... Line 84...
85
      doSQL( "update license set device_id = $queryDeviceID,added_date = now() where license_id = $db_license_id" );
84
      doSQL( "update license set device_id = $queryDeviceID,added_date = now() where license_id = $db_license_id" );
86
      return "Assigned";
85
      return "Assigned";
87
   }
86
   }
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
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
89
   doSQL( "update license set removed_date = now() where license_id = $db_license_id" );
88
   doSQL( "update license set removed_date = now() where license_id = $db_license_id" );
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 )" );
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 )" );
91
   return "Reassigned";
90
   return "Reassigned";
92
}
91
}
93
 
92
 
94
/*
93
/*
95
 * Simply overwrites a license record. Only used for the edit.html page
94
 * Simply overwrites a license record. Only used for the edit.html page
96
 */
95
 */
97
 
96
 
98
function overwriteLicense ( $license_id,$client_id, $device_id, $license_product_id, $license, $added_date, $removed_date ) {
97
function overwriteLicense ( $license_id,$client_id, $device_id, $license_product_id, $license, $added_date, $removed_date ) {
99
   $added_date = makeSafeSQLConstant( $added_date, 'D', 'now()' );
98
   $added_date = makeSafeSQLConstant( $added_date, 'D', 'now()' );
100
   $removed_date = makeSafeSQLConstant( $removed_date, 'D', 'null' );
99
   $removed_date = makeSafeSQLConstant( $removed_date, 'D', 'null' );
-
 
100
   $license = makeSafeSQLConstant( $license );
101
   $device_id = $device_id == '-1' ? 'null' : makeSafeSQLConstant( $device_id, 'I', 'null' );
101
   $device_id = $device_id == '-1' ? 'null' : makeSafeSQLConstant( $device_id, 'I', 'null' );
-
 
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 {
102
   doSQL( "update license set 
108
      doSQL( "update license set 
103
               client_id=$client_id,
109
                  client_id=$client_id,
104
               device_id=$device_id,
110
                  device_id=$device_id,
105
               license_product_id=$license_product_id,
111
                  license_product_id=$license_product_id,
106
               license='$license',
112
                  license=$license,
107
               added_date=$added_date,
113
                  added_date=$added_date,
108
               removed_date=$removed_date
114
                  removed_date=$removed_date
109
            where license_id=$license_id");
115
               where license_id=$license_id");
-
 
116
   }
110
} // overwriteLicense
117
} // overwriteLicense
111
 
118
 
112
 
119
 
113
/*
120
/*
114
 * function will attempt to make a constant ($value) safe for SQL depending on the type.
121
 * function will attempt to make a constant ($value) safe for SQL depending on the type.
Line 128... Line 135...
128
 *    $falsetrue, with the first char denoting false and the second denoting true
135
 *    $falsetrue, with the first char denoting false and the second denoting true
129
 */
136
 */
130
function makeSafeSQLConstant ( $value, $type='S', $default='null', $falsetrue='10' ) {
137
function makeSafeSQLConstant ( $value, $type='S', $default='null', $falsetrue='10' ) {
131
   if (strlen($value) == 0) // simply set any empty values to null
138
   if (strlen($value) == 0) // simply set any empty values to null
132
      return $default;
139
      return $default;
133
   if ( get_magic_quotes_gpc() ) 
-
 
134
      $value = stripslashes($value);
-
 
135
   $value = mysql_real_escape_string( $value );
140
   // print "Processing $value as $type with default $default<br>\n";
136
   
-
 
137
   switch ( strtolower( $type ) ) {
141
   switch ( strtolower( $type ) ) {
138
      case 'string' :
142
      case 'string' :
-
 
143
      case 's' : 
-
 
144
               if ( get_magic_quotes_gpc() ) 
-
 
145
                  $value = stripslashes($value);
-
 
146
               $value = mysql_real_escape_string( $value );
139
      case 's' : $value = strlen( $value ) > 0 ? "'$value'" : $default;
147
               $value = strlen( $value ) > 0 ? "'$value'" : $default;
140
                 break;
148
               break;
141
      case 'date' :
149
      case 'date' :
142
      case 'd' : if ( $result = strtotime( $value ) ) {
150
      case 'd' :
143
                    $value = Date( 'Y-m-d', $result);
151
               if ( $value != 'null' ) {
144
                 } else {
152
                  $result = strtotime( $value );
145
                     $value = $default;
153
                  $value = ( $result === false ) ? $default : "'" . Date( 'Y-m-d', $result) . "'";
146
                 }
154
               }
147
                 if ( $value != $default ) $value = "'$value'";
-
 
148
                 break;
155
               break;
149
      case 'datetime':
156
      case 'datetime':
150
      case 'timestamp':
157
      case 'timestamp':
151
      case 'dt': if ( $result = strtotime( $value ) ) {
158
      case 'dt': 
152
                     $value = Date( 'Y-m-d H:i:s', $result);
159
               if ( $value != 'null' ) {
153
                  } else {
160
                  $result = strtotime( $value );
154
                     $value = $default;
161
                  $value = ( $result === false ) ? $default : "'" . Date( 'Y-m-d H:i:s', $result) . "'";
155
                  }
162
               }
156
                  if ( $value != $default ) $value = "'$value'";
-
 
157
                  break;
163
               break;
158
      case 'integer':
164
      case 'integer':
-
 
165
      case 'i' :  
159
      case 'i' :  $value = intval( $value );
166
               $value = intval( $value );
160
                  break;
167
               break;
161
      case 'float':
168
      case 'float':
-
 
169
      case 'f' :  
162
      case 'f' :  $value = floatval( $value );
170
               $value = floatval( $value );
163
                  break;
171
               break;
164
      case 'bool':
172
      case 'bool':
165
      case 'boolean':
173
      case 'boolean':
166
      case 'b' :  // note, because of the way strpos works, you can not
174
      case 'b' :  // note, because of the way strpos works, you can not
167
                  // simply set $value based on the output; you MUST do
175
                  // simply set $value based on the output; you MUST do
168
                  // as below; specifically check for false, then set the result
176
                  // as below; specifically check for false, then set the result
Line 179... Line 187...
179
 * returns $column from the result (or '' if it does not exist)
187
 * returns $column from the result (or '' if it does not exist)
180
 * Used mainly to get an index from a table with matching value
188
 * Used mainly to get an index from a table with matching value
181
 */
189
 */
182
 
190
 
183
function getValue ( $table, $column, $match, $value, $add = false ) {
191
function getValue ( $table, $column, $match, $value, $add = false ) {
184
   $value = makeSafeSQLValue( $value );
192
   $value = makeSafeSQLConstant( $value );
185
   $return = getOneDBValue( "select $column from $table where $match = $value" );
193
   $return = getOneDBValue( "select $column from $table where $match = $value" );
186
   if ( $return === null ) {
194
   if ( $return === null ) {
187
      if ( $add ) {
195
      if ( $add ) {
188
         $return = doSQL( "insert into $table ( $match ) values ( $value )" );
196
         $return = doSQL( "insert into $table ( $match ) values ( $value )" );
189
         return $return['insert_id'];
197
         return $return['insert_id'];
Line 193... Line 201...
193
   } // if
201
   } // if
194
   return $return;
202
   return $return;
195
} // function getValue
203
} // function getValue
196
 
204
 
197
function getDescription( $license_id ) {
205
function getDescription( $license_id ) {
198
   //$license_id = makeSafeSQLValue( $license_id );
206
   $license_id = makeSafeSQLConstant( $license_id, 'I' );
199
   $result = queryDatabaseExtended( 
207
   $result = queryDatabaseExtended( 
200
           "select 
208
           "select 
201
               license_product.name 'Product',
209
               license_product.name 'Product',
202
               license.license 'License',
210
               license.license 'License',
203
               device.name 'Machine',
211
               device.name 'Machine',
Line 226... Line 234...
226
 * by setting removed_date in the origin, then duplicating that record
234
 * by setting removed_date in the origin, then duplicating that record
227
 * to a new one
235
 * to a new one
228
 */
236
 */
229
 
237
 
230
function moveLicense( $license_id, $machine_id = '' ) {
238
function moveLicense( $license_id, $machine_id = '' ) {
231
   $machine_id = makeSafeSQLValue( $machine_id );
239
   $machine_id = makeSafeSQLConstant( $machine_id, 'I' );
232
   //$license_id = makeSafeSQLValue( $license_id );
240
   $license_id = makeSafeSQLConstant( $license_id, 'I' );
233
   doSQL( 
241
   doSQL( 
234
      "insert into license
242
      "insert into license
235
         ( client_id, device_id,license,license_product_id,added_date,removed_date ) 
243
         ( client_id, device_id,license,license_product_id,added_date,removed_date ) 
236
         select client_id, $machine_id,license,license_product_id,now(),null 
244
         select client_id, $machine_id,license,license_product_id,now(),null 
237
         from license 
245
         from license