c25b9beb09f87b498d998b3fbcaa1f17fc2cd0e2
[freeside.git] / FS / FS / Record.pm
1 package FS::Record;
2
3 use strict;
4 use vars qw( $AUTOLOAD @ISA @EXPORT_OK $DEBUG
5              $conf $me
6              %virtual_fields_cache $nowarn_identical );
7 use Exporter;
8 use Carp qw(carp cluck croak confess);
9 use File::CounterFile;
10 use Locale::Country;
11 use DBI qw(:sql_types);
12 use DBIx::DBSchema 0.25;
13 use FS::UID qw(dbh getotaker datasrc driver_name);
14 use FS::CurrentUser;
15 use FS::Schema qw(dbdef);
16 use FS::SearchCache;
17 use FS::Msgcat qw(gettext);
18 use FS::Conf;
19
20 use FS::part_virtual_field;
21
22 use Tie::IxHash;
23
24 @ISA = qw(Exporter);
25
26 #export dbdef for now... everything else expects to find it here
27 @EXPORT_OK = qw(dbh fields hfields qsearch qsearchs dbdef jsearch);
28
29 $DEBUG = 0;
30 $me = '[FS::Record]';
31
32 $nowarn_identical = 0;
33
34 my $rsa_module;
35 my $rsa_loaded;
36 my $rsa_encrypt;
37 my $rsa_decrypt;
38
39 FS::UID->install_callback( sub {
40   $conf = new FS::Conf; 
41   $File::CounterFile::DEFAULT_DIR = $conf->base_dir . "/counters.". datasrc;
42 } );
43
44
45 =head1 NAME
46
47 FS::Record - Database record objects
48
49 =head1 SYNOPSIS
50
51     use FS::Record;
52     use FS::Record qw(dbh fields qsearch qsearchs);
53
54     $record = new FS::Record 'table', \%hash;
55     $record = new FS::Record 'table', { 'column' => 'value', ... };
56
57     $record  = qsearchs FS::Record 'table', \%hash;
58     $record  = qsearchs FS::Record 'table', { 'column' => 'value', ... };
59     @records = qsearch  FS::Record 'table', \%hash; 
60     @records = qsearch  FS::Record 'table', { 'column' => 'value', ... };
61
62     $table = $record->table;
63     $dbdef_table = $record->dbdef_table;
64
65     $value = $record->get('column');
66     $value = $record->getfield('column');
67     $value = $record->column;
68
69     $record->set( 'column' => 'value' );
70     $record->setfield( 'column' => 'value' );
71     $record->column('value');
72
73     %hash = $record->hash;
74
75     $hashref = $record->hashref;
76
77     $error = $record->insert;
78
79     $error = $record->delete;
80
81     $error = $new_record->replace($old_record);
82
83     # external use deprecated - handled by the database (at least for Pg, mysql)
84     $value = $record->unique('column');
85
86     $error = $record->ut_float('column');
87     $error = $record->ut_floatn('column');
88     $error = $record->ut_number('column');
89     $error = $record->ut_numbern('column');
90     $error = $record->ut_snumber('column');
91     $error = $record->ut_snumbern('column');
92     $error = $record->ut_money('column');
93     $error = $record->ut_text('column');
94     $error = $record->ut_textn('column');
95     $error = $record->ut_alpha('column');
96     $error = $record->ut_alphan('column');
97     $error = $record->ut_phonen('column');
98     $error = $record->ut_anything('column');
99     $error = $record->ut_name('column');
100
101     $quoted_value = _quote($value,'table','field');
102
103     #deprecated
104     $fields = hfields('table');
105     if ( $fields->{Field} ) { # etc.
106
107     @fields = fields 'table'; #as a subroutine
108     @fields = $record->fields; #as a method call
109
110
111 =head1 DESCRIPTION
112
113 (Mostly) object-oriented interface to database records.  Records are currently
114 implemented on top of DBI.  FS::Record is intended as a base class for
115 table-specific classes to inherit from, i.e. FS::cust_main.
116
117 =head1 CONSTRUCTORS
118
119 =over 4
120
121 =item new [ TABLE, ] HASHREF
122
123 Creates a new record.  It doesn't store it in the database, though.  See
124 L<"insert"> for that.
125
126 Note that the object stores this hash reference, not a distinct copy of the
127 hash it points to.  You can ask the object for a copy with the I<hash> 
128 method.
129
130 TABLE can only be omitted when a dervived class overrides the table method.
131
132 =cut
133
134 sub new { 
135   my $proto = shift;
136   my $class = ref($proto) || $proto;
137   my $self = {};
138   bless ($self, $class);
139
140   unless ( defined ( $self->table ) ) {
141     $self->{'Table'} = shift;
142     carp "warning: FS::Record::new called with table name ". $self->{'Table'};
143   }
144   
145   $self->{'Hash'} = shift;
146
147   foreach my $field ( grep !defined($self->{'Hash'}{$_}), $self->fields ) { 
148     $self->{'Hash'}{$field}='';
149   }
150
151   $self->_rebless if $self->can('_rebless');
152
153   $self->{'modified'} = 0;
154
155   $self->_cache($self->{'Hash'}, shift) if $self->can('_cache') && @_;
156
157   $self;
158 }
159
160 sub new_or_cached {
161   my $proto = shift;
162   my $class = ref($proto) || $proto;
163   my $self = {};
164   bless ($self, $class);
165
166   $self->{'Table'} = shift unless defined ( $self->table );
167
168   my $hashref = $self->{'Hash'} = shift;
169   my $cache = shift;
170   if ( defined( $cache->cache->{$hashref->{$cache->key}} ) ) {
171     my $obj = $cache->cache->{$hashref->{$cache->key}};
172     $obj->_cache($hashref, $cache) if $obj->can('_cache');
173     $obj;
174   } else {
175     $cache->cache->{$hashref->{$cache->key}} = $self->new($hashref, $cache);
176   }
177
178 }
179
180 sub create {
181   my $proto = shift;
182   my $class = ref($proto) || $proto;
183   my $self = {};
184   bless ($self, $class);
185   if ( defined $self->table ) {
186     cluck "create constructor is deprecated, use new!";
187     $self->new(@_);
188   } else {
189     croak "FS::Record::create called (not from a subclass)!";
190   }
191 }
192
193 =item qsearch PARAMS_HASHREF | TABLE, HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ, ADDL_FROM
194
195 Searches the database for all records matching (at least) the key/value pairs
196 in HASHREF.  Returns all the records found as `FS::TABLE' objects if that
197 module is loaded (i.e. via `use FS::cust_main;'), otherwise returns FS::Record
198 objects.
199
200 The preferred usage is to pass a hash reference of named parameters:
201
202   my @records = qsearch( {
203                            'table'     => 'table_name',
204                            'hashref'   => { 'field' => 'value'
205                                             'field' => { 'op'    => '<',
206                                                          'value' => '420',
207                                                        },
208                                           },
209
210                            #these are optional...
211                            'select'    => '*',
212                            'extra_sql' => 'AND field ',
213                            #'cache_obj' => '', #optional
214                            'addl_from' => 'LEFT JOIN othtable USING ( field )',
215                          }
216                        );
217
218 Much code still uses old-style positional parameters, this is also probably
219 fine in the common case where there are only two parameters:
220
221   my @records = qsearch( 'table', { 'field' => 'value' } );
222
223 ###oops, argh, FS::Record::new only lets us create database fields.
224 #Normal behaviour if SELECT is not specified is `*', as in
225 #C<SELECT * FROM table WHERE ...>.  However, there is an experimental new
226 #feature where you can specify SELECT - remember, the objects returned,
227 #although blessed into the appropriate `FS::TABLE' package, will only have the
228 #fields you specify.  This might have unwanted results if you then go calling
229 #regular FS::TABLE methods
230 #on it.
231
232 =cut
233
234 sub qsearch {
235   my($stable, $record, $select, $extra_sql, $cache, $addl_from );
236   if ( ref($_[0]) ) { #hashref for now, eventually maybe accept a list too
237     my $opt = shift;
238     $stable    = $opt->{'table'}     or die "table name is required";
239     $record    = $opt->{'hashref'}   || {};
240     $select    = $opt->{'select'}    || '*';
241     $extra_sql = $opt->{'extra_sql'} || '';
242     $cache     = $opt->{'cache_obj'} || '';
243     $addl_from = $opt->{'addl_from'} || '';
244   } else {
245     ($stable, $record, $select, $extra_sql, $cache, $addl_from ) = @_;
246     $select ||= '*';
247   }
248
249   #$stable =~ /^([\w\_]+)$/ or die "Illegal table: $table";
250   #for jsearch
251   $stable =~ /^([\w\s\(\)\.\,\=]+)$/ or die "Illegal table: $stable";
252   $stable = $1;
253   my $dbh = dbh;
254
255   my $table = $cache ? $cache->table : $stable;
256   my $dbdef_table = dbdef->table($table)
257     or die "No schema for table $table found - ".
258            "do you need to run freeside-upgrade?";
259   my $pkey = $dbdef_table->primary_key;
260
261   my @real_fields = grep exists($record->{$_}), real_fields($table);
262   my @virtual_fields;
263   if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
264     @virtual_fields = grep exists($record->{$_}), "FS::$table"->virtual_fields;
265   } else {
266     cluck "warning: FS::$table not loaded; virtual fields not searchable";
267     @virtual_fields = ();
268   }
269
270   my $statement = "SELECT $select FROM $stable";
271   $statement .= " $addl_from" if $addl_from;
272   if ( @real_fields or @virtual_fields ) {
273     $statement .= ' WHERE '. join(' AND ',
274       ( map {
275
276       my $op = '=';
277       my $column = $_;
278       if ( ref($record->{$_}) ) {
279         $op = $record->{$_}{'op'} if $record->{$_}{'op'};
280         #$op = 'LIKE' if $op =~ /^ILIKE$/i && driver_name ne 'Pg';
281         if ( uc($op) eq 'ILIKE' ) {
282           $op = 'LIKE';
283           $record->{$_}{'value'} = lc($record->{$_}{'value'});
284           $column = "LOWER($_)";
285         }
286         $record->{$_} = $record->{$_}{'value'}
287       }
288
289       if ( ! defined( $record->{$_} ) || $record->{$_} eq '' ) {
290         if ( $op eq '=' ) {
291           if ( driver_name eq 'Pg' ) {
292             my $type = dbdef->table($table)->column($column)->type;
293             if ( $type =~ /(int|(big)?serial)/i ) {
294               qq-( $column IS NULL )-;
295             } else {
296               qq-( $column IS NULL OR $column = '' )-;
297             }
298           } else {
299             qq-( $column IS NULL OR $column = "" )-;
300           }
301         } elsif ( $op eq '!=' ) {
302           if ( driver_name eq 'Pg' ) {
303             my $type = dbdef->table($table)->column($column)->type;
304             if ( $type =~ /(int|(big)?serial)/i ) {
305               qq-( $column IS NOT NULL )-;
306             } else {
307               qq-( $column IS NOT NULL AND $column != '' )-;
308             }
309           } else {
310             qq-( $column IS NOT NULL AND $column != "" )-;
311           }
312         } else {
313           if ( driver_name eq 'Pg' ) {
314             qq-( $column $op '' )-;
315           } else {
316             qq-( $column $op "" )-;
317           }
318         }
319       } else {
320         "$column $op ?";
321       }
322     } @real_fields ), 
323     ( map {
324       my $op = '=';
325       my $column = $_;
326       if ( ref($record->{$_}) ) {
327         $op = $record->{$_}{'op'} if $record->{$_}{'op'};
328         if ( uc($op) eq 'ILIKE' ) {
329           $op = 'LIKE';
330           $record->{$_}{'value'} = lc($record->{$_}{'value'});
331           $column = "LOWER($_)";
332         }
333         $record->{$_} = $record->{$_}{'value'};
334       }
335
336       # ... EXISTS ( SELECT name, value FROM part_virtual_field
337       #              JOIN virtual_field
338       #              ON part_virtual_field.vfieldpart = virtual_field.vfieldpart
339       #              WHERE recnum = svc_acct.svcnum
340       #              AND (name, value) = ('egad', 'brain') )
341
342       my $value = $record->{$_};
343
344       my $subq;
345
346       $subq = ($value ? 'EXISTS ' : 'NOT EXISTS ') .
347       "( SELECT part_virtual_field.name, virtual_field.value ".
348       "FROM part_virtual_field JOIN virtual_field ".
349       "ON part_virtual_field.vfieldpart = virtual_field.vfieldpart ".
350       "WHERE virtual_field.recnum = ${table}.${pkey} ".
351       "AND part_virtual_field.name = '${column}'".
352       ($value ? 
353         " AND virtual_field.value ${op} '${value}'"
354       : "") . ")";
355       $subq;
356
357     } @virtual_fields ) );
358
359   }
360
361   $statement .= " $extra_sql" if defined($extra_sql);
362
363   warn "[debug]$me $statement\n" if $DEBUG > 1;
364   my $sth = $dbh->prepare($statement)
365     or croak "$dbh->errstr doing $statement";
366
367   my $bind = 1;
368
369   foreach my $field (
370     grep defined( $record->{$_} ) && $record->{$_} ne '', @real_fields
371   ) {
372     if ( $record->{$field} =~ /^\d+(\.\d+)?$/
373          && dbdef->table($table)->column($field)->type =~ /(int|(big)?serial)/i
374     ) {
375       $sth->bind_param($bind++, $record->{$field}, { TYPE => SQL_INTEGER } );
376     } else {
377       $sth->bind_param($bind++, $record->{$field}, { TYPE => SQL_VARCHAR } );
378     }
379   }
380
381 #  $sth->execute( map $record->{$_},
382 #    grep defined( $record->{$_} ) && $record->{$_} ne '', @fields
383 #  ) or croak "Error executing \"$statement\": ". $sth->errstr;
384
385   $sth->execute or croak "Error executing \"$statement\": ". $sth->errstr;
386
387   if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
388     @virtual_fields = "FS::$table"->virtual_fields;
389   } else {
390     cluck "warning: FS::$table not loaded; virtual fields not returned either";
391     @virtual_fields = ();
392   }
393
394   my %result;
395   tie %result, "Tie::IxHash";
396   my @stuff = @{ $sth->fetchall_arrayref( {} ) };
397   if ( $pkey && scalar(@stuff) && $stuff[0]->{$pkey} ) {
398     %result = map { $_->{$pkey}, $_ } @stuff;
399   } else {
400     @result{@stuff} = @stuff;
401   }
402
403   $sth->finish;
404
405   if ( keys(%result) and @virtual_fields ) {
406     $statement =
407       "SELECT virtual_field.recnum, part_virtual_field.name, ".
408              "virtual_field.value ".
409       "FROM part_virtual_field JOIN virtual_field USING (vfieldpart) ".
410       "WHERE part_virtual_field.dbtable = '$table' AND ".
411       "virtual_field.recnum IN (".
412       join(',', keys(%result)). ") AND part_virtual_field.name IN ('".
413       join(q!', '!, @virtual_fields) . "')";
414     warn "[debug]$me $statement\n" if $DEBUG > 1;
415     $sth = $dbh->prepare($statement) or croak "$dbh->errstr doing $statement";
416     $sth->execute or croak "Error executing \"$statement\": ". $sth->errstr;
417
418     foreach (@{ $sth->fetchall_arrayref({}) }) {
419       my $recnum = $_->{recnum};
420       my $name = $_->{name};
421       my $value = $_->{value};
422       if (exists($result{$recnum})) {
423         $result{$recnum}->{$name} = $value;
424       }
425     }
426   }
427   my @return;
428   if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
429     if ( eval 'FS::'. $table. '->can(\'new\')' eq \&new ) {
430       #derivied class didn't override new method, so this optimization is safe
431       if ( $cache ) {
432         @return = map {
433           new_or_cached( "FS::$table", { %{$_} }, $cache )
434         } values(%result);
435       } else {
436         @return = map {
437           new( "FS::$table", { %{$_} } )
438         } values(%result);
439       }
440     } else {
441       #okay, its been tested
442       # warn "untested code (class FS::$table uses custom new method)";
443       @return = map {
444         eval 'FS::'. $table. '->new( { %{$_} } )';
445       } values(%result);
446     }
447
448     # Check for encrypted fields and decrypt them.
449    ## only in the local copy, not the cached object
450     if ( $conf && $conf->exists('encryption') # $conf doesn't exist when doing
451                                               # the initial search for
452                                               # access_user
453          && eval 'defined(@FS::'. $table . '::encrypted_fields)') {
454       foreach my $record (@return) {
455         foreach my $field (eval '@FS::'. $table . '::encrypted_fields') {
456           # Set it directly... This may cause a problem in the future...
457           $record->setfield($field, $record->decrypt($record->getfield($field)));
458         }
459       }
460     }
461   } else {
462     cluck "warning: FS::$table not loaded; returning FS::Record objects";
463     @return = map {
464       FS::Record->new( $table, { %{$_} } );
465     } values(%result);
466   }
467   return @return;
468 }
469
470 =item by_key PRIMARY_KEY_VALUE
471
472 This is a class method that returns the record with the given primary key
473 value.  This method is only useful in FS::Record subclasses.  For example:
474
475   my $cust_main = FS::cust_main->by_key(1); # retrieve customer with custnum 1
476
477 is equivalent to:
478
479   my $cust_main = qsearchs('cust_main', { 'custnum' => 1 } );
480
481 =cut
482
483 sub by_key {
484   my ($class, $pkey_value) = @_;
485
486   my $table = $class->table
487     or croak "No table for $class found";
488
489   my $dbdef_table = dbdef->table($table)
490     or die "No schema for table $table found - ".
491            "do you need to create it or run dbdef-create?";
492   my $pkey = $dbdef_table->primary_key
493     or die "No primary key for table $table";
494
495   return qsearchs($table, { $pkey => $pkey_value });
496 }
497
498 =item jsearch TABLE, HASHREF, SELECT, EXTRA_SQL, PRIMARY_TABLE, PRIMARY_KEY
499
500 Experimental JOINed search method.  Using this method, you can execute a
501 single SELECT spanning multiple tables, and cache the results for subsequent
502 method calls.  Interface will almost definately change in an incompatible
503 fashion.
504
505 Arguments: 
506
507 =cut
508
509 sub jsearch {
510   my($table, $record, $select, $extra_sql, $ptable, $pkey ) = @_;
511   my $cache = FS::SearchCache->new( $ptable, $pkey );
512   my %saw;
513   ( $cache,
514     grep { !$saw{$_->getfield($pkey)}++ }
515       qsearch($table, $record, $select, $extra_sql, $cache )
516   );
517 }
518
519 =item qsearchs PARAMS_HASHREF | TABLE, HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ, ADDL_FROM
520
521 Same as qsearch, except that if more than one record matches, it B<carp>s but
522 returns the first.  If this happens, you either made a logic error in asking
523 for a single item, or your data is corrupted.
524
525 =cut
526
527 sub qsearchs { # $result_record = &FS::Record:qsearchs('table',\%hash);
528   my $table = $_[0];
529   my(@result) = qsearch(@_);
530   cluck "warning: Multiple records in scalar search ($table)"
531     if scalar(@result) > 1;
532   #should warn more vehemently if the search was on a primary key?
533   scalar(@result) ? ($result[0]) : ();
534 }
535
536 =back
537
538 =head1 METHODS
539
540 =over 4
541
542 =item table
543
544 Returns the table name.
545
546 =cut
547
548 sub table {
549 #  cluck "warning: FS::Record::table deprecated; supply one in subclass!";
550   my $self = shift;
551   $self -> {'Table'};
552 }
553
554 =item dbdef_table
555
556 Returns the DBIx::DBSchema::Table object for the table.
557
558 =cut
559
560 sub dbdef_table {
561   my($self)=@_;
562   my($table)=$self->table;
563   dbdef->table($table);
564 }
565
566 =item get, getfield COLUMN
567
568 Returns the value of the column/field/key COLUMN.
569
570 =cut
571
572 sub get {
573   my($self,$field) = @_;
574   # to avoid "Use of unitialized value" errors
575   if ( defined ( $self->{Hash}->{$field} ) ) {
576     $self->{Hash}->{$field};
577   } else { 
578     '';
579   }
580 }
581 sub getfield {
582   my $self = shift;
583   $self->get(@_);
584 }
585
586 =item set, setfield COLUMN, VALUE
587
588 Sets the value of the column/field/key COLUMN to VALUE.  Returns VALUE.
589
590 =cut
591
592 sub set { 
593   my($self,$field,$value) = @_;
594   $self->{'modified'} = 1;
595   $self->{'Hash'}->{$field} = $value;
596 }
597 sub setfield {
598   my $self = shift;
599   $self->set(@_);
600 }
601
602 =item AUTLOADED METHODS
603
604 $record->column is a synonym for $record->get('column');
605
606 $record->column('value') is a synonym for $record->set('column','value');
607
608 =cut
609
610 # readable/safe
611 sub AUTOLOAD {
612   my($self,$value)=@_;
613   my($field)=$AUTOLOAD;
614   $field =~ s/.*://;
615   if ( defined($value) ) {
616     confess "errant AUTOLOAD $field for $self (arg $value)"
617       unless ref($self) && $self->can('setfield');
618     $self->setfield($field,$value);
619   } else {
620     confess "errant AUTOLOAD $field for $self (no args)"
621       unless ref($self) && $self->can('getfield');
622     $self->getfield($field);
623   }    
624 }
625
626 # efficient
627 #sub AUTOLOAD {
628 #  my $field = $AUTOLOAD;
629 #  $field =~ s/.*://;
630 #  if ( defined($_[1]) ) {
631 #    $_[0]->setfield($field, $_[1]);
632 #  } else {
633 #    $_[0]->getfield($field);
634 #  }    
635 #}
636
637 =item hash
638
639 Returns a list of the column/value pairs, usually for assigning to a new hash.
640
641 To make a distinct duplicate of an FS::Record object, you can do:
642
643     $new = new FS::Record ( $old->table, { $old->hash } );
644
645 =cut
646
647 sub hash {
648   my($self) = @_;
649   confess $self. ' -> hash: Hash attribute is undefined'
650     unless defined($self->{'Hash'});
651   %{ $self->{'Hash'} }; 
652 }
653
654 =item hashref
655
656 Returns a reference to the column/value hash.  This may be deprecated in the
657 future; if there's a reason you can't just use the autoloaded or get/set
658 methods, speak up.
659
660 =cut
661
662 sub hashref {
663   my($self) = @_;
664   $self->{'Hash'};
665 }
666
667 =item modified
668
669 Returns true if any of this object's values have been modified with set (or via
670 an autoloaded method).  Doesn't yet recognize when you retreive a hashref and
671 modify that.
672
673 =cut
674
675 sub modified {
676   my $self = shift;
677   $self->{'modified'};
678 }
679
680 =item insert
681
682 Inserts this record to the database.  If there is an error, returns the error,
683 otherwise returns false.
684
685 =cut
686
687 sub insert {
688   my $self = shift;
689   my $saved = {};
690
691   my $error = $self->check;
692   return $error if $error;
693
694   #single-field unique keys are given a value if false
695   #(like MySQL's AUTO_INCREMENT or Pg SERIAL)
696   foreach ( $self->dbdef_table->unique->singles ) {
697     $self->unique($_) unless $self->getfield($_);
698   }
699
700   #and also the primary key, if the database isn't going to
701   my $primary_key = $self->dbdef_table->primary_key;
702   my $db_seq = 0;
703   if ( $primary_key ) {
704     my $col = $self->dbdef_table->column($primary_key);
705     
706     $db_seq =
707       uc($col->type) =~ /^(BIG)?SERIAL\d?/
708       || ( driver_name eq 'Pg'
709              && defined($col->default)
710              && $col->default =~ /^nextval\(/i
711          )
712       || ( driver_name eq 'mysql'
713              && defined($col->local)
714              && $col->local =~ /AUTO_INCREMENT/i
715          );
716     $self->unique($primary_key) unless $self->getfield($primary_key) || $db_seq;
717   }
718
719   my $table = $self->table;
720
721   
722   # Encrypt before the database
723   if ($conf->exists('encryption') && defined(eval '@FS::'. $table . '::encrypted_fields')) {
724     foreach my $field (eval '@FS::'. $table . '::encrypted_fields') {
725       $self->{'saved'} = $self->getfield($field);
726       $self->setfield($field, $self->encrypt($self->getfield($field)));
727     }
728   }
729
730
731   #false laziness w/delete
732   my @real_fields =
733     grep { defined($self->getfield($_)) && $self->getfield($_) ne "" }
734     real_fields($table)
735   ;
736   my @values = map { _quote( $self->getfield($_), $table, $_) } @real_fields;
737   #eslaf
738
739   my $statement = "INSERT INTO $table ";
740   if ( @real_fields ) {
741     $statement .=
742       "( ".
743         join( ', ', @real_fields ).
744       ") VALUES (".
745         join( ', ', @values ).
746        ")"
747     ;
748   } else {
749     $statement .= 'DEFAULT VALUES';
750   }
751   warn "[debug]$me $statement\n" if $DEBUG > 1;
752   my $sth = dbh->prepare($statement) or return dbh->errstr;
753
754   local $SIG{HUP} = 'IGNORE';
755   local $SIG{INT} = 'IGNORE';
756   local $SIG{QUIT} = 'IGNORE'; 
757   local $SIG{TERM} = 'IGNORE';
758   local $SIG{TSTP} = 'IGNORE';
759   local $SIG{PIPE} = 'IGNORE';
760
761   $sth->execute or return $sth->errstr;
762
763   # get inserted id from the database, if applicable & needed
764   if ( $db_seq && ! $self->getfield($primary_key) ) {
765     warn "[debug]$me retreiving sequence from database\n" if $DEBUG;
766   
767     my $insertid = '';
768
769     if ( driver_name eq 'Pg' ) {
770
771       #my $oid = $sth->{'pg_oid_status'};
772       #my $i_sql = "SELECT $primary_key FROM $table WHERE oid = ?";
773
774       my $default = $self->dbdef_table->column($primary_key)->default;
775       unless ( $default =~ /^nextval\(\(?'"?([\w\.]+)"?'/i ) {
776         dbh->rollback if $FS::UID::AutoCommit;
777         return "can't parse $table.$primary_key default value".
778                " for sequence name: $default";
779       }
780       my $sequence = $1;
781
782       my $i_sql = "SELECT currval('$sequence')";
783       my $i_sth = dbh->prepare($i_sql) or do {
784         dbh->rollback if $FS::UID::AutoCommit;
785         return dbh->errstr;
786       };
787       #$i_sth->execute($oid) or do {
788       $i_sth->execute() or do {
789         dbh->rollback if $FS::UID::AutoCommit;
790         return $i_sth->errstr;
791       };
792       $insertid = $i_sth->fetchrow_arrayref->[0];
793
794     } elsif ( driver_name eq 'mysql' ) {
795
796       $insertid = dbh->{'mysql_insertid'};
797       # work around mysql_insertid being null some of the time, ala RT :/
798       unless ( $insertid ) {
799         warn "WARNING: DBD::mysql didn't return mysql_insertid; ".
800              "using SELECT LAST_INSERT_ID();";
801         my $i_sql = "SELECT LAST_INSERT_ID()";
802         my $i_sth = dbh->prepare($i_sql) or do {
803           dbh->rollback if $FS::UID::AutoCommit;
804           return dbh->errstr;
805         };
806         $i_sth->execute or do {
807           dbh->rollback if $FS::UID::AutoCommit;
808           return $i_sth->errstr;
809         };
810         $insertid = $i_sth->fetchrow_arrayref->[0];
811       }
812
813     } else {
814
815       dbh->rollback if $FS::UID::AutoCommit;
816       return "don't know how to retreive inserted ids from ". driver_name. 
817              ", try using counterfiles (maybe run dbdef-create?)";
818
819     }
820
821     $self->setfield($primary_key, $insertid);
822
823   }
824
825   my @virtual_fields = 
826       grep defined($self->getfield($_)) && $self->getfield($_) ne "",
827           $self->virtual_fields;
828   if (@virtual_fields) {
829     my %v_values = map { $_, $self->getfield($_) } @virtual_fields;
830
831     my $vfieldpart = $self->vfieldpart_hashref;
832
833     my $v_statement = "INSERT INTO virtual_field(recnum, vfieldpart, value) ".
834                     "VALUES (?, ?, ?)";
835
836     my $v_sth = dbh->prepare($v_statement) or do {
837       dbh->rollback if $FS::UID::AutoCommit;
838       return dbh->errstr;
839     };
840
841     foreach (keys(%v_values)) {
842       $v_sth->execute($self->getfield($primary_key),
843                       $vfieldpart->{$_},
844                       $v_values{$_})
845       or do {
846         dbh->rollback if $FS::UID::AutoCommit;
847         return $v_sth->errstr;
848       };
849     }
850   }
851
852
853   my $h_sth;
854   if ( defined dbdef->table('h_'. $table) ) {
855     my $h_statement = $self->_h_statement('insert');
856     warn "[debug]$me $h_statement\n" if $DEBUG > 2;
857     $h_sth = dbh->prepare($h_statement) or do {
858       dbh->rollback if $FS::UID::AutoCommit;
859       return dbh->errstr;
860     };
861   } else {
862     $h_sth = '';
863   }
864   $h_sth->execute or return $h_sth->errstr if $h_sth;
865
866   dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
867
868   # Now that it has been saved, reset the encrypted fields so that $new 
869   # can still be used.
870   foreach my $field (keys %{$saved}) {
871     $self->setfield($field, $saved->{$field});
872   }
873
874   '';
875 }
876
877 =item add
878
879 Depriciated (use insert instead).
880
881 =cut
882
883 sub add {
884   cluck "warning: FS::Record::add deprecated!";
885   insert @_; #call method in this scope
886 }
887
888 =item delete
889
890 Delete this record from the database.  If there is an error, returns the error,
891 otherwise returns false.
892
893 =cut
894
895 sub delete {
896   my $self = shift;
897
898   my $statement = "DELETE FROM ". $self->table. " WHERE ". join(' AND ',
899     map {
900       $self->getfield($_) eq ''
901         #? "( $_ IS NULL OR $_ = \"\" )"
902         ? ( driver_name eq 'Pg'
903               ? "$_ IS NULL"
904               : "( $_ IS NULL OR $_ = \"\" )"
905           )
906         : "$_ = ". _quote($self->getfield($_),$self->table,$_)
907     } ( $self->dbdef_table->primary_key )
908           ? ( $self->dbdef_table->primary_key)
909           : real_fields($self->table)
910   );
911   warn "[debug]$me $statement\n" if $DEBUG > 1;
912   my $sth = dbh->prepare($statement) or return dbh->errstr;
913
914   my $h_sth;
915   if ( defined dbdef->table('h_'. $self->table) ) {
916     my $h_statement = $self->_h_statement('delete');
917     warn "[debug]$me $h_statement\n" if $DEBUG > 2;
918     $h_sth = dbh->prepare($h_statement) or return dbh->errstr;
919   } else {
920     $h_sth = '';
921   }
922
923   my $primary_key = $self->dbdef_table->primary_key;
924   my $v_sth;
925   my @del_vfields;
926   my $vfp = $self->vfieldpart_hashref;
927   foreach($self->virtual_fields) {
928     next if $self->getfield($_) eq '';
929     unless(@del_vfields) {
930       my $st = "DELETE FROM virtual_field WHERE recnum = ? AND vfieldpart = ?";
931       $v_sth = dbh->prepare($st) or return dbh->errstr;
932     }
933     push @del_vfields, $_;
934   }
935
936   local $SIG{HUP} = 'IGNORE';
937   local $SIG{INT} = 'IGNORE';
938   local $SIG{QUIT} = 'IGNORE'; 
939   local $SIG{TERM} = 'IGNORE';
940   local $SIG{TSTP} = 'IGNORE';
941   local $SIG{PIPE} = 'IGNORE';
942
943   my $rc = $sth->execute or return $sth->errstr;
944   #not portable #return "Record not found, statement:\n$statement" if $rc eq "0E0";
945   $h_sth->execute or return $h_sth->errstr if $h_sth;
946   $v_sth->execute($self->getfield($primary_key), $vfp->{$_}) 
947     or return $v_sth->errstr 
948         foreach (@del_vfields);
949   
950   dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
951
952   #no need to needlessly destoy the data either (causes problems actually)
953   #undef $self; #no need to keep object!
954
955   '';
956 }
957
958 =item del
959
960 Depriciated (use delete instead).
961
962 =cut
963
964 sub del {
965   cluck "warning: FS::Record::del deprecated!";
966   &delete(@_); #call method in this scope
967 }
968
969 =item replace OLD_RECORD
970
971 Replace the OLD_RECORD with this one in the database.  If there is an error,
972 returns the error, otherwise returns false.
973
974 =cut
975
976 sub replace {
977   my $new = shift;
978   my $old = shift;  
979
980   if (!defined($old)) { 
981     warn "[debug]$me replace called with no arguments; autoloading old record\n"
982      if $DEBUG;
983     my $primary_key = $new->dbdef_table->primary_key;
984     if ( $primary_key ) {
985       $old = qsearchs($new->table, { $primary_key => $new->$primary_key() } )
986         or croak "can't find ". $new->table. ".$primary_key ".
987                  $new->$primary_key();
988     } else {
989       croak $new->table. " has no primary key; pass old record as argument";
990     }
991   }
992
993   warn "[debug]$me $new ->replace $old\n" if $DEBUG;
994
995   if ( $new->can('replace_check') ) {
996     my $error = $new->replace_check($old);
997     return $error if $error;
998   }
999
1000   return "Records not in same table!" unless $new->table eq $old->table;
1001
1002   my $primary_key = $old->dbdef_table->primary_key;
1003   return "Can't change primary key $primary_key ".
1004          'from '. $old->getfield($primary_key).
1005          ' to ' . $new->getfield($primary_key)
1006     if $primary_key
1007        && ( $old->getfield($primary_key) ne $new->getfield($primary_key) );
1008
1009   my $error = $new->check;
1010   return $error if $error;
1011   
1012   # Encrypt for replace
1013   my $conf = new FS::Conf;
1014   my $saved = {};
1015   if ($conf->exists('encryption') && defined(eval '@FS::'. $new->table . '::encrypted_fields')) {
1016     foreach my $field (eval '@FS::'. $new->table . '::encrypted_fields') {
1017       $saved->{$field} = $new->getfield($field);
1018       $new->setfield($field, $new->encrypt($new->getfield($field)));
1019     }
1020   }
1021
1022   #my @diff = grep $new->getfield($_) ne $old->getfield($_), $old->fields;
1023   my %diff = map { ($new->getfield($_) ne $old->getfield($_))
1024                    ? ($_, $new->getfield($_)) : () } $old->fields;
1025                    
1026   unless ( keys(%diff) ) {
1027     carp "[warning]$me $new -> replace $old: records identical"
1028       unless $nowarn_identical;
1029     return '';
1030   }
1031
1032   my $statement = "UPDATE ". $old->table. " SET ". join(', ',
1033     map {
1034       "$_ = ". _quote($new->getfield($_),$old->table,$_) 
1035     } real_fields($old->table)
1036   ). ' WHERE '.
1037     join(' AND ',
1038       map {
1039
1040         if ( $old->getfield($_) eq '' ) {
1041
1042          #false laziness w/qsearch
1043          if ( driver_name eq 'Pg' ) {
1044             my $type = $old->dbdef_table->column($_)->type;
1045             if ( $type =~ /(int|(big)?serial)/i ) {
1046               qq-( $_ IS NULL )-;
1047             } else {
1048               qq-( $_ IS NULL OR $_ = '' )-;
1049             }
1050           } else {
1051             qq-( $_ IS NULL OR $_ = "" )-;
1052           }
1053
1054         } else {
1055           "$_ = ". _quote($old->getfield($_),$old->table,$_);
1056         }
1057
1058       } ( $primary_key ? ( $primary_key ) : real_fields($old->table) )
1059     )
1060   ;
1061   warn "[debug]$me $statement\n" if $DEBUG > 1;
1062   my $sth = dbh->prepare($statement) or return dbh->errstr;
1063
1064   my $h_old_sth;
1065   if ( defined dbdef->table('h_'. $old->table) ) {
1066     my $h_old_statement = $old->_h_statement('replace_old');
1067     warn "[debug]$me $h_old_statement\n" if $DEBUG > 2;
1068     $h_old_sth = dbh->prepare($h_old_statement) or return dbh->errstr;
1069   } else {
1070     $h_old_sth = '';
1071   }
1072
1073   my $h_new_sth;
1074   if ( defined dbdef->table('h_'. $new->table) ) {
1075     my $h_new_statement = $new->_h_statement('replace_new');
1076     warn "[debug]$me $h_new_statement\n" if $DEBUG > 2;
1077     $h_new_sth = dbh->prepare($h_new_statement) or return dbh->errstr;
1078   } else {
1079     $h_new_sth = '';
1080   }
1081
1082   # For virtual fields we have three cases with different SQL 
1083   # statements: add, replace, delete
1084   my $v_add_sth;
1085   my $v_rep_sth;
1086   my $v_del_sth;
1087   my (@add_vfields, @rep_vfields, @del_vfields);
1088   my $vfp = $old->vfieldpart_hashref;
1089   foreach(grep { exists($diff{$_}) } $new->virtual_fields) {
1090     if($diff{$_} eq '') {
1091       # Delete
1092       unless(@del_vfields) {
1093         my $st = "DELETE FROM virtual_field WHERE recnum = ? ".
1094                  "AND vfieldpart = ?";
1095         warn "[debug]$me $st\n" if $DEBUG > 2;
1096         $v_del_sth = dbh->prepare($st) or return dbh->errstr;
1097       }
1098       push @del_vfields, $_;
1099     } elsif($old->getfield($_) eq '') {
1100       # Add
1101       unless(@add_vfields) {
1102         my $st = "INSERT INTO virtual_field (value, recnum, vfieldpart) ".
1103                  "VALUES (?, ?, ?)";
1104         warn "[debug]$me $st\n" if $DEBUG > 2;
1105         $v_add_sth = dbh->prepare($st) or return dbh->errstr;
1106       }
1107       push @add_vfields, $_;
1108     } else {
1109       # Replace
1110       unless(@rep_vfields) {
1111         my $st = "UPDATE virtual_field SET value = ? ".
1112                  "WHERE recnum = ? AND vfieldpart = ?";
1113         warn "[debug]$me $st\n" if $DEBUG > 2;
1114         $v_rep_sth = dbh->prepare($st) or return dbh->errstr;
1115       }
1116       push @rep_vfields, $_;
1117     }
1118   }
1119
1120   local $SIG{HUP} = 'IGNORE';
1121   local $SIG{INT} = 'IGNORE';
1122   local $SIG{QUIT} = 'IGNORE'; 
1123   local $SIG{TERM} = 'IGNORE';
1124   local $SIG{TSTP} = 'IGNORE';
1125   local $SIG{PIPE} = 'IGNORE';
1126
1127   my $rc = $sth->execute or return $sth->errstr;
1128   #not portable #return "Record not found (or records identical)." if $rc eq "0E0";
1129   $h_old_sth->execute or return $h_old_sth->errstr if $h_old_sth;
1130   $h_new_sth->execute or return $h_new_sth->errstr if $h_new_sth;
1131
1132   $v_del_sth->execute($old->getfield($primary_key),
1133                       $vfp->{$_})
1134         or return $v_del_sth->errstr
1135       foreach(@del_vfields);
1136
1137   $v_add_sth->execute($new->getfield($_),
1138                       $old->getfield($primary_key),
1139                       $vfp->{$_})
1140         or return $v_add_sth->errstr
1141       foreach(@add_vfields);
1142
1143   $v_rep_sth->execute($new->getfield($_),
1144                       $old->getfield($primary_key),
1145                       $vfp->{$_})
1146         or return $v_rep_sth->errstr
1147       foreach(@rep_vfields);
1148
1149   dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
1150
1151   # Now that it has been saved, reset the encrypted fields so that $new 
1152   # can still be used.
1153   foreach my $field (keys %{$saved}) {
1154     $new->setfield($field, $saved->{$field});
1155   }
1156
1157   '';
1158
1159 }
1160
1161 =item rep
1162
1163 Depriciated (use replace instead).
1164
1165 =cut
1166
1167 sub rep {
1168   cluck "warning: FS::Record::rep deprecated!";
1169   replace @_; #call method in this scope
1170 }
1171
1172 =item check
1173
1174 Checks virtual fields (using check_blocks).  Subclasses should still provide 
1175 a check method to validate real fields, foreign keys, etc., and call this 
1176 method via $self->SUPER::check.
1177
1178 (FIXME: Should this method try to make sure that it I<is> being called from 
1179 a subclass's check method, to keep the current semantics as far as possible?)
1180
1181 =cut
1182
1183 sub check {
1184   #confess "FS::Record::check not implemented; supply one in subclass!";
1185   my $self = shift;
1186
1187   foreach my $field ($self->virtual_fields) {
1188     for ($self->getfield($field)) {
1189       # See notes on check_block in FS::part_virtual_field.
1190       eval $self->pvf($field)->check_block;
1191       if ( $@ ) {
1192         #this is bad, probably want to follow the stack backtrace up and see
1193         #wtf happened
1194         my $err = "Fatal error checking $field for $self";
1195         cluck "$err: $@";
1196         return "$err (see log for backtrace): $@";
1197
1198       }
1199       $self->setfield($field, $_);
1200     }
1201   }
1202   '';
1203 }
1204
1205 sub _h_statement {
1206   my( $self, $action, $time ) = @_;
1207
1208   $time ||= time;
1209
1210   my @fields =
1211     grep { defined($self->getfield($_)) && $self->getfield($_) ne "" }
1212     real_fields($self->table);
1213   ;
1214
1215   # If we're encrypting then don't ever store the payinfo or CVV2 in the history....
1216   # You can see if it changed by the paymask...
1217   if ($conf->exists('encryption') ) {
1218     @fields = grep  $_ ne 'payinfo' && $_ ne 'cvv2', @fields;
1219   }
1220   my @values = map { _quote( $self->getfield($_), $self->table, $_) } @fields;
1221
1222   "INSERT INTO h_". $self->table. " ( ".
1223       join(', ', qw(history_date history_user history_action), @fields ).
1224     ") VALUES (".
1225       join(', ', $time, dbh->quote(getotaker()), dbh->quote($action), @values).
1226     ")"
1227   ;
1228 }
1229
1230 =item unique COLUMN
1231
1232 B<Warning>: External use is B<deprecated>.  
1233
1234 Replaces COLUMN in record with a unique number, using counters in the
1235 filesystem.  Used by the B<insert> method on single-field unique columns
1236 (see L<DBIx::DBSchema::Table>) and also as a fallback for primary keys
1237 that aren't SERIAL (Pg) or AUTO_INCREMENT (mysql).
1238
1239 Returns the new value.
1240
1241 =cut
1242
1243 sub unique {
1244   my($self,$field) = @_;
1245   my($table)=$self->table;
1246
1247   croak "Unique called on field $field, but it is ",
1248         $self->getfield($field),
1249         ", not null!"
1250     if $self->getfield($field);
1251
1252   #warn "table $table is tainted" if is_tainted($table);
1253   #warn "field $field is tainted" if is_tainted($field);
1254
1255   my($counter) = new File::CounterFile "$table.$field",0;
1256 # hack for web demo
1257 #  getotaker() =~ /^([\w\-]{1,16})$/ or die "Illegal CGI REMOTE_USER!";
1258 #  my($user)=$1;
1259 #  my($counter) = new File::CounterFile "$user/$table.$field",0;
1260 # endhack
1261
1262   my $index = $counter->inc;
1263   $index = $counter->inc while qsearchs($table, { $field=>$index } );
1264
1265   $index =~ /^(\d*)$/;
1266   $index=$1;
1267
1268   $self->setfield($field,$index);
1269
1270 }
1271
1272 =item ut_float COLUMN
1273
1274 Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10.  May not be
1275 null.  If there is an error, returns the error, otherwise returns false.
1276
1277 =cut
1278
1279 sub ut_float {
1280   my($self,$field)=@_ ;
1281   ($self->getfield($field) =~ /^(\d+\.\d+)$/ ||
1282    $self->getfield($field) =~ /^(\d+)$/ ||
1283    $self->getfield($field) =~ /^(\d+\.\d+e\d+)$/ ||
1284    $self->getfield($field) =~ /^(\d+e\d+)$/)
1285     or return "Illegal or empty (float) $field: ". $self->getfield($field);
1286   $self->setfield($field,$1);
1287   '';
1288 }
1289 =item ut_floatn COLUMN
1290
1291 Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10.  May be
1292 null.  If there is an error, returns the error, otherwise returns false.
1293
1294 =cut
1295
1296 sub ut_floatn {
1297   my($self,$field)=@_ ;
1298   ($self->getfield($field) =~ /^(\d*)$/ ||
1299    $self->getfield($field) =~ /^(-?\d+\.\d+)$/ ||
1300    $self->getfield($field) =~ /^(-?\d+)$/ ||
1301    $self->getfield($field) =~ /^(-?\d+\.\d+e\d+)$/ ||
1302    $self->getfield($field) =~ /^(-?\d+e\d+)$/)
1303     or return "Illegal or empty (float) $field: ". $self->getfield($field);
1304   $self->setfield($field,$1);
1305   '';
1306 }
1307
1308 =item ut_snumber COLUMN
1309
1310 Check/untaint signed numeric data (whole numbers).  If there is an error,
1311 returns the error, otherwise returns false.
1312
1313 =cut
1314
1315 sub ut_snumber {
1316   my($self, $field) = @_;
1317   $self->getfield($field) =~ /^(-?)\s*(\d+)$/
1318     or return "Illegal or empty (numeric) $field: ". $self->getfield($field);
1319   $self->setfield($field, "$1$2");
1320   '';
1321 }
1322
1323 =item ut_snumbern COLUMN
1324
1325 Check/untaint signed numeric data (whole numbers).  If there is an error,
1326 returns the error, otherwise returns false.
1327
1328 =cut
1329
1330 sub ut_snumbern {
1331   my($self, $field) = @_;
1332   $self->getfield($field) =~ /^(-?)\s*(\d*)$/
1333     or return "Illegal (numeric) $field: ". $self->getfield($field);
1334   if ($1) {
1335     return "Illegal (numeric) $field: ". $self->getfield($field)
1336       unless $2;
1337   }
1338   $self->setfield($field, "$1$2");
1339   '';
1340 }
1341
1342 =item ut_number COLUMN
1343
1344 Check/untaint simple numeric data (whole numbers).  May not be null.  If there
1345 is an error, returns the error, otherwise returns false.
1346
1347 =cut
1348
1349 sub ut_number {
1350   my($self,$field)=@_;
1351   $self->getfield($field) =~ /^(\d+)$/
1352     or return "Illegal or empty (numeric) $field: ". $self->getfield($field);
1353   $self->setfield($field,$1);
1354   '';
1355 }
1356
1357 =item ut_numbern COLUMN
1358
1359 Check/untaint simple numeric data (whole numbers).  May be null.  If there is
1360 an error, returns the error, otherwise returns false.
1361
1362 =cut
1363
1364 sub ut_numbern {
1365   my($self,$field)=@_;
1366   $self->getfield($field) =~ /^(\d*)$/
1367     or return "Illegal (numeric) $field: ". $self->getfield($field);
1368   $self->setfield($field,$1);
1369   '';
1370 }
1371
1372 =item ut_money COLUMN
1373
1374 Check/untaint monetary numbers.  May be negative.  Set to 0 if null.  If there
1375 is an error, returns the error, otherwise returns false.
1376
1377 =cut
1378
1379 sub ut_money {
1380   my($self,$field)=@_;
1381   $self->setfield($field, 0) if $self->getfield($field) eq '';
1382   $self->getfield($field) =~ /^(\-)? ?(\d*)(\.\d{2})?$/
1383     or return "Illegal (money) $field: ". $self->getfield($field);
1384   #$self->setfield($field, "$1$2$3" || 0);
1385   $self->setfield($field, ( ($1||''). ($2||''). ($3||'') ) || 0);
1386   '';
1387 }
1388
1389 =item ut_text COLUMN
1390
1391 Check/untaint text.  Alphanumerics, spaces, and the following punctuation
1392 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? / = [ ]
1393 May not be null.  If there is an error, returns the error, otherwise returns
1394 false.
1395
1396 =cut
1397
1398 sub ut_text {
1399   my($self,$field)=@_;
1400   #warn "msgcat ". \&msgcat. "\n";
1401   #warn "notexist ". \&notexist. "\n";
1402   #warn "AUTOLOAD ". \&AUTOLOAD. "\n";
1403   $self->getfield($field)
1404     =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\[\]]+)$/
1405       or return gettext('illegal_or_empty_text'). " $field: ".
1406                  $self->getfield($field);
1407   $self->setfield($field,$1);
1408   '';
1409 }
1410
1411 =item ut_textn COLUMN
1412
1413 Check/untaint text.  Alphanumerics, spaces, and the following punctuation
1414 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? /
1415 May be null.  If there is an error, returns the error, otherwise returns false.
1416
1417 =cut
1418
1419 sub ut_textn {
1420   my($self,$field)=@_;
1421   $self->getfield($field)
1422     =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\[\]]*)$/
1423       or return gettext('illegal_text'). " $field: ". $self->getfield($field);
1424   $self->setfield($field,$1);
1425   '';
1426 }
1427
1428 =item ut_alpha COLUMN
1429
1430 Check/untaint alphanumeric strings (no spaces).  May not be null.  If there is
1431 an error, returns the error, otherwise returns false.
1432
1433 =cut
1434
1435 sub ut_alpha {
1436   my($self,$field)=@_;
1437   $self->getfield($field) =~ /^(\w+)$/
1438     or return "Illegal or empty (alphanumeric) $field: ".
1439               $self->getfield($field);
1440   $self->setfield($field,$1);
1441   '';
1442 }
1443
1444 =item ut_alpha COLUMN
1445
1446 Check/untaint alphanumeric strings (no spaces).  May be null.  If there is an
1447 error, returns the error, otherwise returns false.
1448
1449 =cut
1450
1451 sub ut_alphan {
1452   my($self,$field)=@_;
1453   $self->getfield($field) =~ /^(\w*)$/ 
1454     or return "Illegal (alphanumeric) $field: ". $self->getfield($field);
1455   $self->setfield($field,$1);
1456   '';
1457 }
1458
1459 =item ut_phonen COLUMN [ COUNTRY ]
1460
1461 Check/untaint phone numbers.  May be null.  If there is an error, returns
1462 the error, otherwise returns false.
1463
1464 Takes an optional two-letter ISO country code; without it or with unsupported
1465 countries, ut_phonen simply calls ut_alphan.
1466
1467 =cut
1468
1469 sub ut_phonen {
1470   my( $self, $field, $country ) = @_;
1471   return $self->ut_alphan($field) unless defined $country;
1472   my $phonen = $self->getfield($field);
1473   if ( $phonen eq '' ) {
1474     $self->setfield($field,'');
1475   } elsif ( $country eq 'US' || $country eq 'CA' ) {
1476     $phonen =~ s/\D//g;
1477     $phonen =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/
1478       or return gettext('illegal_phone'). " $field: ". $self->getfield($field);
1479     $phonen = "$1-$2-$3";
1480     $phonen .= " x$4" if $4;
1481     $self->setfield($field,$phonen);
1482   } else {
1483     warn "warning: don't know how to check phone numbers for country $country";
1484     return $self->ut_textn($field);
1485   }
1486   '';
1487 }
1488
1489 =item ut_hex COLUMN
1490
1491 Check/untaint hexadecimal values.
1492
1493 =cut
1494
1495 sub ut_hex {
1496   my($self, $field) = @_;
1497   $self->getfield($field) =~ /^([\da-fA-F]+)$/
1498     or return "Illegal (hex) $field: ". $self->getfield($field);
1499   $self->setfield($field, uc($1));
1500   '';
1501 }
1502
1503 =item ut_hexn COLUMN
1504
1505 Check/untaint hexadecimal values.  May be null.
1506
1507 =cut
1508
1509 sub ut_hexn {
1510   my($self, $field) = @_;
1511   $self->getfield($field) =~ /^([\da-fA-F]*)$/
1512     or return "Illegal (hex) $field: ". $self->getfield($field);
1513   $self->setfield($field, uc($1));
1514   '';
1515 }
1516 =item ut_ip COLUMN
1517
1518 Check/untaint ip addresses.  IPv4 only for now.
1519
1520 =cut
1521
1522 sub ut_ip {
1523   my( $self, $field ) = @_;
1524   $self->getfield($field) =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/
1525     or return "Illegal (IP address) $field: ". $self->getfield($field);
1526   for ( $1, $2, $3, $4 ) { return "Illegal (IP address) $field" if $_ > 255; }
1527   $self->setfield($field, "$1.$2.$3.$4");
1528   '';
1529 }
1530
1531 =item ut_ipn COLUMN
1532
1533 Check/untaint ip addresses.  IPv4 only for now.  May be null.
1534
1535 =cut
1536
1537 sub ut_ipn {
1538   my( $self, $field ) = @_;
1539   if ( $self->getfield($field) =~ /^()$/ ) {
1540     $self->setfield($field,'');
1541     '';
1542   } else {
1543     $self->ut_ip($field);
1544   }
1545 }
1546
1547 =item ut_domain COLUMN
1548
1549 Check/untaint host and domain names.
1550
1551 =cut
1552
1553 sub ut_domain {
1554   my( $self, $field ) = @_;
1555   #$self->getfield($field) =~/^(\w+\.)*\w+$/
1556   $self->getfield($field) =~/^(([\w\-]+\.)*\w+)$/
1557     or return "Illegal (domain) $field: ". $self->getfield($field);
1558   $self->setfield($field,$1);
1559   '';
1560 }
1561
1562 =item ut_name COLUMN
1563
1564 Check/untaint proper names; allows alphanumerics, spaces and the following
1565 punctuation: , . - '
1566
1567 May not be null.
1568
1569 =cut
1570
1571 sub ut_name {
1572   my( $self, $field ) = @_;
1573   $self->getfield($field) =~ /^([\w \,\.\-\']+)$/
1574     or return gettext('illegal_name'). " $field: ". $self->getfield($field);
1575   $self->setfield($field,$1);
1576   '';
1577 }
1578
1579 =item ut_zip COLUMN
1580
1581 Check/untaint zip codes.
1582
1583 =cut
1584
1585 my @zip_reqd_countries = qw( AU CA US ); #CA, US implicit...
1586
1587 sub ut_zip {
1588   my( $self, $field, $country ) = @_;
1589
1590   if ( $country eq 'US' ) {
1591
1592     $self->getfield($field) =~ /^\s*(\d{5}(\-\d{4})?)\s*$/
1593       or return gettext('illegal_zip'). " $field for country $country: ".
1594                 $self->getfield($field);
1595     $self->setfield($field, $1);
1596
1597   } elsif ( $country eq 'CA' ) {
1598
1599     $self->getfield($field) =~ /^\s*([A-Z]\d[A-Z])\s*(\d[A-Z]\d)\s*$/i
1600       or return gettext('illegal_zip'). " $field for country $country: ".
1601                 $self->getfield($field);
1602     $self->setfield($field, "$1 $2");
1603
1604   } else {
1605
1606     if ( $self->getfield($field) =~ /^\s*$/
1607          && ( !$country || ! grep { $_ eq $country } @zip_reqd_countries )
1608        )
1609     {
1610       $self->setfield($field,'');
1611     } else {
1612       $self->getfield($field) =~ /^\s*(\w[\w\-\s]{2,8}\w)\s*$/
1613         or return gettext('illegal_zip'). " $field: ". $self->getfield($field);
1614       $self->setfield($field,$1);
1615     }
1616
1617   }
1618
1619   '';
1620 }
1621
1622 =item ut_country COLUMN
1623
1624 Check/untaint country codes.  Country names are changed to codes, if possible -
1625 see L<Locale::Country>.
1626
1627 =cut
1628
1629 sub ut_country {
1630   my( $self, $field ) = @_;
1631   unless ( $self->getfield($field) =~ /^(\w\w)$/ ) {
1632     if ( $self->getfield($field) =~ /^([\w \,\.\(\)\']+)$/ 
1633          && country2code($1) ) {
1634       $self->setfield($field,uc(country2code($1)));
1635     }
1636   }
1637   $self->getfield($field) =~ /^(\w\w)$/
1638     or return "Illegal (country) $field: ". $self->getfield($field);
1639   $self->setfield($field,uc($1));
1640   '';
1641 }
1642
1643 =item ut_anything COLUMN
1644
1645 Untaints arbitrary data.  Be careful.
1646
1647 =cut
1648
1649 sub ut_anything {
1650   my( $self, $field ) = @_;
1651   $self->getfield($field) =~ /^(.*)$/s
1652     or return "Illegal $field: ". $self->getfield($field);
1653   $self->setfield($field,$1);
1654   '';
1655 }
1656
1657 =item ut_enum COLUMN CHOICES_ARRAYREF
1658
1659 Check/untaint a column, supplying all possible choices, like the "enum" type.
1660
1661 =cut
1662
1663 sub ut_enum {
1664   my( $self, $field, $choices ) = @_;
1665   foreach my $choice ( @$choices ) {
1666     if ( $self->getfield($field) eq $choice ) {
1667       $self->setfield($choice);
1668       return '';
1669     }
1670   }
1671   return "Illegal (enum) field $field: ". $self->getfield($field);
1672 }
1673
1674 =item ut_foreign_key COLUMN FOREIGN_TABLE FOREIGN_COLUMN
1675
1676 Check/untaint a foreign column key.  Call a regular ut_ method (like ut_number)
1677 on the column first.
1678
1679 =cut
1680
1681 sub ut_foreign_key {
1682   my( $self, $field, $table, $foreign ) = @_;
1683   qsearchs($table, { $foreign => $self->getfield($field) })
1684     or return "Can't find ". $self->table. ".$field ". $self->getfield($field).
1685               " in $table.$foreign";
1686   '';
1687 }
1688
1689 =item ut_foreign_keyn COLUMN FOREIGN_TABLE FOREIGN_COLUMN
1690
1691 Like ut_foreign_key, except the null value is also allowed.
1692
1693 =cut
1694
1695 sub ut_foreign_keyn {
1696   my( $self, $field, $table, $foreign ) = @_;
1697   $self->getfield($field)
1698     ? $self->ut_foreign_key($field, $table, $foreign)
1699     : '';
1700 }
1701
1702 =item ut_agentnum_acl
1703
1704 Checks this column as an agentnum, taking into account the current users's
1705 ACLs.
1706
1707 =cut
1708
1709 sub ut_agentnum_acl {
1710   my( $self, $field, $null_acl ) = @_;
1711
1712   my $error = $self->ut_foreign_keyn($field, 'agent', 'agentnum');
1713   return "Illegal agentnum: $error" if $error;
1714
1715   my $curuser = $FS::CurrentUser::CurrentUser;
1716
1717   if ( $self->$field() ) {
1718
1719     return "Access deined"
1720       unless $curuser->agentnum($self->$field());
1721
1722   } else {
1723
1724     return "Access denied"
1725       unless $curuser->access_right($null_acl);
1726
1727   }
1728
1729   '';
1730
1731 }
1732
1733 =item virtual_fields [ TABLE ]
1734
1735 Returns a list of virtual fields defined for the table.  This should not 
1736 be exported, and should only be called as an instance or class method.
1737
1738 =cut
1739
1740 sub virtual_fields {
1741   my $self = shift;
1742   my $table;
1743   $table = $self->table or confess "virtual_fields called on non-table";
1744
1745   confess "Unknown table $table" unless dbdef->table($table);
1746
1747   return () unless dbdef->table('part_virtual_field');
1748
1749   unless ( $virtual_fields_cache{$table} ) {
1750     my $query = 'SELECT name from part_virtual_field ' .
1751                 "WHERE dbtable = '$table'";
1752     my $dbh = dbh;
1753     my $result = $dbh->selectcol_arrayref($query);
1754     confess "Error executing virtual fields query: $query: ". $dbh->errstr
1755       if $dbh->err;
1756     $virtual_fields_cache{$table} = $result;
1757   }
1758
1759   @{$virtual_fields_cache{$table}};
1760
1761 }
1762
1763
1764 =item fields [ TABLE ]
1765
1766 This is a wrapper for real_fields and virtual_fields.  Code that called
1767 fields before should probably continue to call fields.
1768
1769 =cut
1770
1771 sub fields {
1772   my $something = shift;
1773   my $table;
1774   if($something->isa('FS::Record')) {
1775     $table = $something->table;
1776   } else {
1777     $table = $something;
1778     $something = "FS::$table";
1779   }
1780   return (real_fields($table), $something->virtual_fields());
1781 }
1782
1783 =back
1784
1785 =item pvf FIELD_NAME
1786
1787 Returns the FS::part_virtual_field object corresponding to a field in the 
1788 record (specified by FIELD_NAME).
1789
1790 =cut
1791
1792 sub pvf {
1793   my ($self, $name) = (shift, shift);
1794
1795   if(grep /^$name$/, $self->virtual_fields) {
1796     return qsearchs('part_virtual_field', { dbtable => $self->table,
1797                                             name    => $name } );
1798   }
1799   ''
1800 }
1801
1802 =head1 SUBROUTINES
1803
1804 =over 4
1805
1806 =item real_fields [ TABLE ]
1807
1808 Returns a list of the real columns in the specified table.  Called only by 
1809 fields() and other subroutines elsewhere in FS::Record.
1810
1811 =cut
1812
1813 sub real_fields {
1814   my $table = shift;
1815
1816   my($table_obj) = dbdef->table($table);
1817   confess "Unknown table $table" unless $table_obj;
1818   $table_obj->columns;
1819 }
1820
1821 =item _quote VALUE, TABLE, COLUMN
1822
1823 This is an internal function used to construct SQL statements.  It returns
1824 VALUE DBI-quoted (see L<DBI/"quote">) unless VALUE is a number and the column
1825 type (see L<DBIx::DBSchema::Column>) does not end in `char' or `binary'.
1826
1827 =cut
1828
1829 sub _quote {
1830   my($value, $table, $column) = @_;
1831   my $column_obj = dbdef->table($table)->column($column);
1832   my $column_type = $column_obj->type;
1833   my $nullable = $column_obj->null;
1834
1835   warn "  $table.$column: $value ($column_type".
1836        ( $nullable ? ' NULL' : ' NOT NULL' ).
1837        ")\n" if $DEBUG > 2;
1838
1839   if ( $value eq '' && $nullable ) {
1840     'NULL'
1841   } elsif ( $value eq '' && $column_type =~ /^(int|numeric)/ ) {
1842     cluck "WARNING: Attempting to set non-null integer $table.$column null; ".
1843           "using 0 instead";
1844     0;
1845   } elsif ( $value =~ /^\d+(\.\d+)?$/ && 
1846             ! $column_type =~ /(char|binary|text)$/i ) {
1847     $value;
1848   } else {
1849     dbh->quote($value);
1850   }
1851 }
1852
1853 =item vfieldpart_hashref TABLE
1854
1855 Returns a hashref of virtual field names and vfieldparts applicable to the given
1856 TABLE.
1857
1858 =cut
1859
1860 sub vfieldpart_hashref {
1861   my $self = shift;
1862   my $table = $self->table;
1863
1864   return {} unless dbdef->table('part_virtual_field');
1865
1866   my $dbh = dbh;
1867   my $statement = "SELECT vfieldpart, name FROM part_virtual_field WHERE ".
1868                   "dbtable = '$table'";
1869   my $sth = $dbh->prepare($statement);
1870   $sth->execute or croak "Execution of '$statement' failed: ".$dbh->errstr;
1871   return { map { $_->{name}, $_->{vfieldpart} } 
1872     @{$sth->fetchall_arrayref({})} };
1873
1874 }
1875
1876
1877 =item hfields TABLE
1878
1879 This is deprecated.  Don't use it.
1880
1881 It returns a hash-type list with the fields of this record's table set true.
1882
1883 =cut
1884
1885 sub hfields {
1886   carp "warning: hfields is deprecated";
1887   my($table)=@_;
1888   my(%hash);
1889   foreach (fields($table)) {
1890     $hash{$_}=1;
1891   }
1892   \%hash;
1893 }
1894
1895 sub _dump {
1896   my($self)=@_;
1897   join("\n", map {
1898     "$_: ". $self->getfield($_). "|"
1899   } (fields($self->table)) );
1900 }
1901
1902 =item encrypt($value)
1903
1904 Encrypts the credit card using a combination of PK to encrypt and uuencode to armour.
1905
1906 Returns the encrypted string.
1907
1908 You should generally not have to worry about calling this, as the system handles this for you.
1909
1910 =cut
1911
1912
1913 sub encrypt {
1914   my ($self, $value) = @_;
1915   my $encrypted;
1916
1917   my $conf = new FS::Conf;
1918   if ($conf->exists('encryption')) {
1919     if ($self->is_encrypted($value)) {
1920       # Return the original value if it isn't plaintext.
1921       $encrypted = $value;
1922     } else {
1923       $self->loadRSA;
1924       if (ref($rsa_encrypt) =~ /::RSA/) { # We Can Encrypt
1925         # RSA doesn't like the empty string so let's pack it up
1926         # The database doesn't like the RSA data so uuencode it
1927         my $length = length($value)+1;
1928         $encrypted = pack("u*",$rsa_encrypt->encrypt(pack("Z$length",$value)));
1929       } else {
1930         die ("You can't encrypt w/o a valid RSA engine - Check your installation or disable encryption");
1931       }
1932     }
1933   }
1934   return $encrypted;
1935 }
1936
1937 =item is_encrypted($value)
1938
1939 Checks to see if the string is encrypted and returns true or false (1/0) to indicate it's status.
1940
1941 =cut
1942
1943
1944 sub is_encrypted {
1945   my ($self, $value) = @_;
1946   # Possible Bug - Some work may be required here....
1947
1948   if ($value =~ /^M/ && length($value) > 80) {
1949     return 1;
1950   } else {
1951     return 0;
1952   }
1953 }
1954
1955 =item decrypt($value)
1956
1957 Uses the private key to decrypt the string. Returns the decryoted string or undef on failure.
1958
1959 You should generally not have to worry about calling this, as the system handles this for you.
1960
1961 =cut
1962
1963 sub decrypt {
1964   my ($self,$value) = @_;
1965   my $decrypted = $value; # Will return the original value if it isn't encrypted or can't be decrypted.
1966   my $conf = new FS::Conf;
1967   if ($conf->exists('encryption') && $self->is_encrypted($value)) {
1968     $self->loadRSA;
1969     if (ref($rsa_decrypt) =~ /::RSA/) {
1970       my $encrypted = unpack ("u*", $value);
1971       $decrypted =  unpack("Z*", eval{$rsa_decrypt->decrypt($encrypted)});
1972       if ($@) {warn "Decryption Failed"};
1973     }
1974   }
1975   return $decrypted;
1976 }
1977
1978 sub loadRSA {
1979     my $self = shift;
1980     #Initialize the Module
1981     $rsa_module = 'Crypt::OpenSSL::RSA'; # The Default
1982
1983     my $conf = new FS::Conf;
1984     if ($conf->exists('encryptionmodule') && $conf->config('encryptionmodule') ne '') {
1985       $rsa_module = $conf->config('encryptionmodule');
1986     }
1987
1988     if (!$rsa_loaded) {
1989         eval ("require $rsa_module"); # No need to import the namespace
1990         $rsa_loaded++;
1991     }
1992     # Initialize Encryption
1993     if ($conf->exists('encryptionpublickey') && $conf->config('encryptionpublickey') ne '') {
1994       my $public_key = join("\n",$conf->config('encryptionpublickey'));
1995       $rsa_encrypt = $rsa_module->new_public_key($public_key);
1996     }
1997     
1998     # Intitalize Decryption
1999     if ($conf->exists('encryptionprivatekey') && $conf->config('encryptionprivatekey') ne '') {
2000       my $private_key = join("\n",$conf->config('encryptionprivatekey'));
2001       $rsa_decrypt = $rsa_module->new_private_key($private_key);
2002     }
2003 }
2004
2005 sub DESTROY { return; }
2006
2007 #sub DESTROY {
2008 #  my $self = shift;
2009 #  #use Carp qw(cluck);
2010 #  #cluck "DESTROYING $self";
2011 #  warn "DESTROYING $self";
2012 #}
2013
2014 #sub is_tainted {
2015 #             return ! eval { join('',@_), kill 0; 1; };
2016 #         }
2017
2018 =back
2019
2020 =head1 BUGS
2021
2022 This module should probably be renamed, since much of the functionality is
2023 of general use.  It is not completely unlike Adapter::DBI (see below).
2024
2025 Exported qsearch and qsearchs should be deprecated in favor of method calls
2026 (against an FS::Record object like the old search and searchs that qsearch
2027 and qsearchs were on top of.)
2028
2029 The whole fields / hfields mess should be removed.
2030
2031 The various WHERE clauses should be subroutined.
2032
2033 table string should be deprecated in favor of DBIx::DBSchema::Table.
2034
2035 No doubt we could benefit from a Tied hash.  Documenting how exists / defined
2036 true maps to the database (and WHERE clauses) would also help.
2037
2038 The ut_ methods should ask the dbdef for a default length.
2039
2040 ut_sqltype (like ut_varchar) should all be defined
2041
2042 A fallback check method should be provided which uses the dbdef.
2043
2044 The ut_money method assumes money has two decimal digits.
2045
2046 The Pg money kludge in the new method only strips `$'.
2047
2048 The ut_phonen method only checks US-style phone numbers.
2049
2050 The _quote function should probably use ut_float instead of a regex.
2051
2052 All the subroutines probably should be methods, here or elsewhere.
2053
2054 Probably should borrow/use some dbdef methods where appropriate (like sub
2055 fields)
2056
2057 As of 1.14, DBI fetchall_hashref( {} ) doesn't set fetchrow_hashref NAME_lc,
2058 or allow it to be set.  Working around it is ugly any way around - DBI should
2059 be fixed.  (only affects RDBMS which return uppercase column names)
2060
2061 ut_zip should take an optional country like ut_phone.
2062
2063 =head1 SEE ALSO
2064
2065 L<DBIx::DBSchema>, L<FS::UID>, L<DBI>
2066
2067 Adapter::DBI from Ch. 11 of Advanced Perl Programming by Sriram Srinivasan.
2068
2069 http://poop.sf.net/
2070
2071 =cut
2072
2073 1;
2074