4 * Forms management using HTTP User Interface.
6 * Portable Windows Library
8 * Copyright (c) 1993-2002 Equivalence Pty. Ltd.
10 * The contents of this file are subject to the Mozilla Public License
11 * Version 1.0 (the "License"); you may not use this file except in
12 * compliance with the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/
15 * Software distributed under the License is distributed on an "AS IS"
16 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17 * the License for the specific language governing rights and limitations
20 * The Original Code is Portable Windows Library.
22 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
24 * Contributor(s): ______________________________________.
27 * Revision 1.17 2003/03/24 04:30:41 robertj
28 * Added function to set and get strings from PConfig in correct format for
29 * use with HTTP form array contsructs.
31 * Revision 1.16 2002/11/06 22:47:24 robertj
32 * Fixed header comment (copyright etc)
34 * Revision 1.15 2002/09/16 01:08:59 robertj
35 * Added #define so can select if #pragma interface/implementation is used on
36 * platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
38 * Revision 1.14 2001/10/10 08:07:48 robertj
39 * Fixed large memory leak of strings when doing POST to a form.
41 * Revision 1.13 2000/12/18 07:12:37 robertj
42 * Added ability to have fixed length array fields.
44 * Revision 1.12 1999/03/09 08:01:46 robertj
45 * Changed comments for doc++ support (more to come).
47 * Revision 1.11 1999/02/16 08:07:10 robertj
48 * MSVC 6.0 compatibility changes.
50 * Revision 1.10 1998/11/30 02:50:48 robertj
51 * New directory structure
53 * Revision 1.9 1998/09/23 06:19:31 robertj
54 * Added open source copyright license.
56 * Revision 1.8 1998/08/20 05:45:33 robertj
57 * Fixed bug where substitutions did not always occur if near end of macro block.
59 * Revision 1.7 1998/01/26 00:25:24 robertj
60 * Major rewrite of HTTP forms management.
62 * Revision 1.6 1997/08/09 07:46:51 robertj
63 * Fixed problems with value of SELECT fields in form
65 * Revision 1.5 1997/07/26 11:38:17 robertj
66 * Support for overridable pages in HTTP service applications.
68 * Revision 1.4 1997/07/08 13:16:12 robertj
69 * Major HTTP form enhancements for lists and arrays of fields.
71 * Revision 1.3 1997/06/08 04:49:40 robertj
72 * Adding new llist based form field.
74 * Revision 1.2 1997/04/01 06:01:39 robertj
75 * Allowed value list in drop down box to be modified once created.
77 * Revision 1.1 1996/06/28 12:55:56 robertj
89 #include <ptclib/http.h>
92 ///////////////////////////////////////////////////////////////////////////////
95 /** This class is the abstract base class for fields in a #PHTTPForm#
98 class PHTTPField
: public PObject
100 PCLASSINFO(PHTTPField
, PObject
)
103 const char * bname
, // base name (identifier) for the field.
104 const char * title
, // Title text for field (defaults to name).
105 const char * help
// Help text for the field.
107 // Create a new field in a HTTP form.
109 /** Compare the fields using the field names.
112 Comparison of the name fields of the two fields.
114 virtual Comparison
Compare(
118 /** Get the identifier name of the field.
121 String for field name.
123 const PCaselessString
& GetName() const { return fullName
; }
125 /** Get the identifier name of the field.
128 String for field name.
130 const PCaselessString
& GetBaseName() const { return baseName
; }
132 /** Set the name for the field.
134 virtual void SetName(
135 const PString
& newName
// New name for field
138 /** Locate the field naem, recusing down for composite fields.
141 Pointer to located field, or NULL if not found.
143 virtual const PHTTPField
* LocateName(
144 const PString
& name
// Full field name to locate
147 /** Get the title of the field.
150 String for title placed next to the field.
152 const PString
& GetTitle() const { return title
; }
154 /** Get the title of the field.
157 String for title placed next to the field.
159 const PString
& GetHelp() const { return help
; }
162 const PString
& text
// Help text.
165 const PString
& hotLinkURL
, // URL for link to help page.
166 const PString
& linkText
// Help text in the link.
169 const PString
& hotLinkURL
, // URL for link to help page.
170 const PString
& imageURL
, // URL for image to be displayed in link.
171 const PString
& imageText
// Text in the link when image unavailable.
173 // Set the help text for the field.
175 /** Create a new field of the same class as the current field.
178 New field object instance.
180 virtual PHTTPField
* NewField() const = 0;
182 virtual void ExpandFieldNames(PString
& text
, PINDEX start
, PINDEX
& finish
) const;
183 // Splice expanded macro substitutions into text string
185 /** Convert the field to HTML form tag for inclusion into the HTTP page.
187 virtual void GetHTMLTag(
188 PHTML
& html
// HTML to receive the fields HTML tag.
191 /** Convert the field input to HTML for inclusion into the HTTP page.
193 virtual PString
GetHTMLInput(
194 const PString
& input
// Source HTML text for input tag.
197 /** Convert the field input to HTML for inclusion into the HTTP page.
199 virtual PString
GetHTMLSelect(
200 const PString
& selection
// Source HTML text for input tag.
203 /** Convert the field to HTML for inclusion into the HTTP page.
205 virtual void GetHTMLHeading(
206 PHTML
& html
// HTML to receive the field info.
209 /** Get the string value of the field.
212 String for field value.
214 virtual PString
GetValue(BOOL dflt
= FALSE
) const = 0;
216 /** Set the value of the field.
218 virtual void SetValue(
219 const PString
& newValue
// New value for the field.
222 /** Get the value of the PConfig to the sub-field. If the field is not
223 composite then it always sets the value as for the non-indexed version.
225 virtual void LoadFromConfig(
226 PConfig
& cfg
// Configuration for value transfer.
229 /** Set the value of the sub-field into the PConfig. If the field is not
230 composite then it always sets the value as for the non-indexed version.
232 virtual void SaveToConfig(
233 PConfig
& cfg
// Configuration for value transfer.
236 /** Validate the new field value before #SetValue()# is called.
239 BOOL if the new field value is OK.
241 virtual BOOL
Validated(
242 const PString
& newVal
, // Proposed new value for the field.
243 PStringStream
& msg
// Stream to take error HTML if value not valid.
247 /** Retrieve all the names in the field and subfields.
250 List of strings for each subfield.
252 virtual void GetAllNames(PStringList
& list
) const;
254 /** Set the value of the field in a list of fields.
256 virtual void SetAllValues(
257 const PStringToString
& data
// New value for the field.
260 /** Validate the new field value in a list before #SetValue()# is called.
263 BOOL if the all the new field values are OK.
265 virtual BOOL
ValidateAll(
266 const PStringToString
& data
, // Proposed new value for the field.
267 PStringStream
& msg
// Stream to take error HTML if value not valid.
271 BOOL
NotYetInHTML() const { return notInHTML
; }
272 void SetInHTML() { notInHTML
= FALSE
; }
275 PCaselessString baseName
;
276 PCaselessString fullName
;
283 PLIST(PHTTPFieldList
, PHTTPField
);
285 class PHTTPCompositeField
: public PHTTPField
287 PCLASSINFO(PHTTPCompositeField
, PHTTPField
)
290 const char * name
, // Name (identifier) for the field.
291 const char * title
= NULL
, // Title text for field (defaults to name).
292 const char * help
= NULL
// Help text for the field.
295 virtual void SetName(
296 const PString
& name
// New name for field
299 virtual const PHTTPField
* LocateName(
300 const PString
& name
// Full field name to locate
303 virtual PHTTPField
* NewField() const;
305 virtual void ExpandFieldNames(PString
& text
, PINDEX start
, PINDEX
& finish
) const;
307 virtual void GetHTMLTag(
308 PHTML
& html
// HTML to receive the field info.
311 virtual PString
GetHTMLInput(
312 const PString
& input
// Source HTML text for input tag.
315 virtual void GetHTMLHeading(
316 PHTML
& html
// HTML to receive the field info.
319 virtual PString
GetValue(BOOL dflt
= FALSE
) const;
321 virtual void SetValue(
322 const PString
& newValue
// New value for the field.
325 virtual void LoadFromConfig(
326 PConfig
& cfg
// Configuration for value transfer.
328 virtual void SaveToConfig(
329 PConfig
& cfg
// Configuration for value transfer.
332 virtual void GetAllNames(PStringList
& list
) const;
333 virtual void SetAllValues(
334 const PStringToString
& data
// New value for the field.
337 virtual BOOL
ValidateAll(
338 const PStringToString
& data
, // Proposed new value for the field.
339 PStringStream
& msg
// Stream to take error HTML if value not valid.
343 /** Get the number of sub-fields in the composite field. Note that this is
344 the total including any composite sub-fields, ie, it is the size of the
345 whole tree of primitive fields.
350 virtual PINDEX
GetSize() const;
352 void Append(PHTTPField
* fld
);
353 PHTTPField
& operator[](PINDEX idx
) const { return fields
[idx
]; }
354 void RemoveAt(PINDEX idx
) { fields
.RemoveAt(idx
); }
355 void RemoveAll() { fields
.RemoveAll(); }
358 PHTTPFieldList fields
;
362 class PHTTPSubForm
: public PHTTPCompositeField
364 PCLASSINFO(PHTTPSubForm
, PHTTPCompositeField
)
367 const PString
& subFormName
, // URL for the sub-form
368 const char * name
, // Name (identifier) for the field.
369 const char * title
= NULL
, // Title text for field (defaults to name).
370 PINDEX primaryField
= 0, // Pimary field whove value is in hot link
371 PINDEX secondaryField
= P_MAX_INDEX
// Seconary field next to hotlink
374 PHTTPField
* NewField() const;
375 void GetHTMLTag(PHTML
& html
) const;
376 void GetHTMLHeading(PHTML
& html
) const;
385 class PHTTPFieldArray
: public PHTTPCompositeField
387 PCLASSINFO(PHTTPFieldArray
, PHTTPCompositeField
)
390 PHTTPField
* baseField
,
398 virtual PHTTPField
* NewField() const;
400 virtual void ExpandFieldNames(PString
& text
, PINDEX start
, PINDEX
& finish
) const;
402 virtual void GetHTMLTag(
403 PHTML
& html
// HTML to receive the field info.
406 virtual void LoadFromConfig(
407 PConfig
& cfg
// Configuration for value transfer.
409 virtual void SaveToConfig(
410 PConfig
& cfg
// Configuration for value transfer.
414 virtual void SetAllValues(
415 const PStringToString
& data
// New value for the field.
418 virtual PINDEX
GetSize() const;
419 void SetSize(PINDEX newSize
);
421 PStringArray
GetStrings(
422 PConfig
& cfg
/// Config file to get strings from
426 PConfig
& cfg
, /// Config file to Set strings to
427 const PStringArray
& values
/// Strings to set
431 void AddBlankField();
432 void AddArrayControlBox(PHTML
& html
, PINDEX fld
) const;
433 void SetArrayFieldName(PINDEX idx
) const;
435 PHTTPField
* baseField
;
441 class PHTTPStringField
: public PHTTPField
443 PCLASSINFO(PHTTPStringField
, PHTTPField
)
448 const char * initVal
= NULL
,
449 const char * help
= NULL
455 const char * initVal
= NULL
,
456 const char * help
= NULL
459 virtual PHTTPField
* NewField() const;
461 virtual void GetHTMLTag(
462 PHTML
& html
// HTML to receive the field info.
465 virtual PString
GetValue(BOOL dflt
= FALSE
) const;
467 virtual void SetValue(
468 const PString
& newVal
474 PString initialValue
;
479 class PHTTPPasswordField
: public PHTTPStringField
481 PCLASSINFO(PHTTPPasswordField
, PHTTPStringField
)
486 const char * initVal
= NULL
,
487 const char * help
= NULL
493 const char * initVal
= NULL
,
494 const char * help
= NULL
497 virtual PHTTPField
* NewField() const;
499 virtual void GetHTMLTag(
500 PHTML
& html
// HTML to receive the field info.
503 virtual PString
GetValue(BOOL dflt
= FALSE
) const;
505 virtual void SetValue(
506 const PString
& newVal
509 static PString
Decrypt(const PString
& pword
);
513 class PHTTPIntegerField
: public PHTTPField
515 PCLASSINFO(PHTTPIntegerField
, PHTTPField
)
521 const char * units
= NULL
,
522 const char * help
= NULL
529 const char * units
= NULL
,
530 const char * help
= NULL
533 virtual PHTTPField
* NewField() const;
535 virtual void GetHTMLTag(
536 PHTML
& html
// HTML to receive the field info.
539 virtual PString
GetValue(BOOL dflt
= FALSE
) const;
541 virtual void SetValue(
542 const PString
& newVal
545 virtual void LoadFromConfig(
546 PConfig
& cfg
// Configuration for value transfer.
548 virtual void SaveToConfig(
549 PConfig
& cfg
// Configuration for value transfer.
552 virtual BOOL
Validated(
553 const PString
& newVal
,
559 int low
, high
, value
;
565 class PHTTPBooleanField
: public PHTTPField
567 PCLASSINFO(PHTTPBooleanField
, PHTTPField
)
571 BOOL initVal
= FALSE
,
572 const char * help
= NULL
577 BOOL initVal
= FALSE
,
578 const char * help
= NULL
581 virtual PHTTPField
* NewField() const;
583 virtual void GetHTMLTag(
584 PHTML
& html
// HTML to receive the field info.
587 virtual PString
GetHTMLInput(
588 const PString
& input
591 virtual PString
GetValue(BOOL dflt
= FALSE
) const;
593 virtual void SetValue(
594 const PString
& newVal
597 virtual void LoadFromConfig(
598 PConfig
& cfg
// Configuration for value transfer.
600 virtual void SaveToConfig(
601 PConfig
& cfg
// Configuration for value transfer.
606 BOOL value
, initialValue
;
610 class PHTTPRadioField
: public PHTTPField
612 PCLASSINFO(PHTTPRadioField
, PHTTPField
)
616 const PStringArray
& valueArray
,
618 const char * help
= NULL
622 const PStringArray
& valueArray
,
623 const PStringArray
& titleArray
,
625 const char * help
= NULL
630 const char * const * valueStrings
,
632 const char * help
= NULL
637 const char * const * valueStrings
,
638 const char * const * titleStrings
,
640 const char * help
= NULL
644 const char * groupTitle
,
645 const PStringArray
& valueArray
,
647 const char * help
= NULL
651 const char * groupTitle
,
652 const PStringArray
& valueArray
,
653 const PStringArray
& titleArray
,
655 const char * help
= NULL
659 const char * groupTitle
,
661 const char * const * valueStrings
,
663 const char * help
= NULL
667 const char * groupTitle
,
669 const char * const * valueStrings
,
670 const char * const * titleStrings
,
672 const char * help
= NULL
675 virtual PHTTPField
* NewField() const;
677 virtual void GetHTMLTag(
678 PHTML
& html
// HTML to receive the field info.
681 virtual PString
GetHTMLInput(
682 const PString
& input
685 virtual PString
GetValue(BOOL dflt
= FALSE
) const;
687 virtual void SetValue(
688 const PString
& newVal
696 PString initialValue
;
700 class PHTTPSelectField
: public PHTTPField
702 PCLASSINFO(PHTTPSelectField
, PHTTPField
)
706 const PStringArray
& valueArray
,
708 const char * help
= NULL
713 const char * const * valueStrings
,
715 const char * help
= NULL
720 const PStringArray
& valueArray
,
722 const char * help
= NULL
728 const char * const * valueStrings
,
730 const char * help
= NULL
733 virtual PHTTPField
* NewField() const;
735 virtual void GetHTMLTag(
736 PHTML
& html
// HTML to receive the field info.
739 virtual PString
GetValue(BOOL dflt
= FALSE
) const;
741 virtual void SetValue(
742 const PString
& newVal
755 ///////////////////////////////////////////////////////////////////////////////
758 class PHTTPForm
: public PHTTPString
760 PCLASSINFO(PHTTPForm
, PHTTPString
)
767 const PHTTPAuthority
& auth
775 const PString
& html
,
776 const PHTTPAuthority
& auth
780 virtual void OnLoadedText(
781 PHTTPRequest
& request
, // Information on this request.
782 PString
& text
// Data used in reply.
785 PHTTPRequest
& request
, // Information on this request.
786 const PStringToString
& data
, // Variables in the POST data.
787 PHTML
& replyMessage
// Reply message for post.
794 void RemoveAllFields()
795 { fields
.RemoveAll(); fieldNames
.RemoveAll(); }
807 const PString
& heading
811 BuildOptions option
= CompleteHTML
816 PHTTPCompositeField fields
;
817 PStringSet fieldNames
;
821 //////////////////////////////////////////////////////////////////////////////
824 class PHTTPConfig
: public PHTTPForm
826 PCLASSINFO(PHTTPConfig
, PHTTPForm
)
830 const PString
& section
834 const PString
& section
,
835 const PHTTPAuthority
& auth
839 const PString
& section
,
844 const PString
& section
,
845 const PString
& html
,
846 const PHTTPAuthority
& auth
849 virtual void OnLoadedText(
850 PHTTPRequest
& request
, // Information on this request.
851 PString
& text
// Data used in reply.
854 PHTTPRequest
& request
, // Information on this request.
855 const PStringToString
& data
, // Variables in the POST data.
856 PHTML
& replyMessage
// Reply message for post.
860 /** Load all of the values for the resource from the configuration.
862 void LoadFromConfig();
864 /** Get the configuration file section that the page will alter.
867 String for config file section.
869 const PString
& GetConfigSection() const { return section
; }
871 void SetConfigSection(
872 const PString
& sect
// New section for the config page.
873 ) { section
= sect
; }
874 // Set the configuration file section.
876 /** Add a field that will determine the name opf the secontion into which
877 the other fields are to be added as keys. The section is not created and
878 and error generated if the section already exists.
880 PHTTPField
* AddSectionField(
881 PHTTPField
* sectionFld
, // Field to set as the section name
882 const char * prefix
= NULL
, // String to attach before the field value
883 const char * suffix
= NULL
// String to attach after the field value
886 /** Add fields to the HTTP form for adding a new key to the config file
889 void AddNewKeyFields(
890 PHTTPField
* keyFld
, // Field for the key to be added.
891 PHTTPField
* valFld
// Field for the value of the key yto be added.
897 PString sectionPrefix
;
898 PString sectionSuffix
;
899 PHTTPField
* sectionField
;
900 PHTTPField
* keyField
;
901 PHTTPField
* valField
;
908 //////////////////////////////////////////////////////////////////////////////
909 // PHTTPConfigSectionList
911 class PHTTPConfigSectionList
: public PHTTPString
913 PCLASSINFO(PHTTPConfigSectionList
, PHTTPString
)
915 PHTTPConfigSectionList(
917 const PHTTPAuthority
& auth
,
918 const PString
& sectionPrefix
,
919 const PString
& additionalValueName
,
920 const PURL
& editSection
,
921 const PURL
& newSection
,
922 const PString
& newSectionTitle
,
926 virtual void OnLoadedText(
927 PHTTPRequest
& request
, // Information on this request.
928 PString
& text
// Data used in reply.
931 PHTTPRequest
& request
, // Information on this request.
932 const PStringToString
& data
, // Variables in the POST data.
933 PHTML
& replyMessage
// Reply message for post.
937 PString sectionPrefix
;
938 PString additionalValueName
;
939 PString newSectionLink
;
940 PString newSectionTitle
;
941 PString editSectionLink
;
948 // End Of File ///////////////////////////////////////////////////////////////