PTLib  Version 2.14.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
httpform.h
Go to the documentation of this file.
1 /*
2  * httpform.h
3  *
4  * Forms management using HTTP User Interface.
5  *
6  * Portable Windows Library
7  *
8  * Copyright (c) 1993-2002 Equivalence Pty. Ltd.
9  *
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/
14  *
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
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Contributor(s): ______________________________________.
25  *
26  * $Revision: 31994 $
27  * $Author: rjongbloed $
28  * $Date: 2014-05-28 17:39:04 +1000 (Wed, 28 May 2014) $
29  */
30 
31 #ifndef PTLIB_HTTPFORM_H
32 #define PTLIB_HTTPFORM_H
33 
34 #ifdef P_USE_PRAGMA
35 #pragma interface
36 #endif
37 
38 #if P_HTTPFORMS
39 
40 #include <ptclib/http.h>
41 #include <ptclib/html.h>
42 
43 
45 // PHTTPField
46 
50 class PHTTPField : public PObject
51 {
53  public:
54  PHTTPField(
55  const char * bname, // base name (identifier) for the field.
56  const char * title, // Title text for field (defaults to name).
57  const char * help // Help text for the field.
58  );
59  // Create a new field in a HTTP form.
60 
66  virtual Comparison Compare(
67  const PObject & obj
68  ) const;
69 
75  const PCaselessString & GetName() const { return fullName; }
76 
82  const PCaselessString & GetBaseName() const { return baseName; }
83 
86  virtual void SetName(
87  const PString & newName // New name for field
88  );
89 
95  virtual const PHTTPField * LocateName(
96  const PString & name // Full field name to locate
97  ) const;
98 
104  const PString & GetTitle() const { return title; }
105 
111  const PString & GetHelp() const { return help; }
112 
113  void SetHelp(
114  const PString & text // Help text.
115  ) { help = text; }
116  void SetHelp(
117  const PString & hotLinkURL, // URL for link to help page.
118  const PString & linkText // Help text in the link.
119  );
120  void SetHelp(
121  const PString & hotLinkURL, // URL for link to help page.
122  const PString & imageURL, // URL for image to be displayed in link.
123  const PString & imageText // Text in the link when image unavailable.
124  );
125  // Set the help text for the field.
126 
132  virtual PHTTPField * NewField() const = 0;
133 
134  virtual void ExpandFieldNames(PString & text, PINDEX start, PINDEX & finish) const;
135  // Splice expanded macro substitutions into text string
136 
139  virtual void GetHTMLTag(
140  PHTML & html // HTML to receive the fields HTML tag.
141  ) const = 0;
142 
145  virtual PString GetHTMLInput(
146  const PString & input // Source HTML text for input tag.
147  ) const;
148 
151  virtual PString GetHTMLSelect(
152  const PString & selection // Source HTML text for input tag.
153  ) const;
154 
157  virtual void GetHTMLHeading(
158  PHTML & html // HTML to receive the field info.
159  ) const;
160 
166  virtual PString GetValue(PBoolean dflt = false) const = 0;
167 
170  virtual void SetValue(
171  const PString & newValue // New value for the field.
172  ) = 0;
173 
178  virtual bool LoadFromConfig(
179  PConfig & cfg // Configuration for value transfer.
180  );
181 
185  virtual void SaveToConfig(
186  PConfig & cfg // Configuration for value transfer.
187  ) const;
188 
194  virtual PBoolean Validated(
195  const PString & newVal, // Proposed new value for the field.
196  PStringStream & msg // Stream to take error HTML if value not valid.
197  ) const;
198 
199 
205  virtual void GetAllNames(PStringArray & names) const;
206 
209  virtual void SetAllValues(
210  const PStringToString & data // New value for the field.
211  );
212 
218  virtual PBoolean ValidateAll(
219  const PStringToString & data, // Proposed new value for the field.
220  PStringStream & msg // Stream to take error HTML if value not valid.
221  ) const;
222 
223 
224  PBoolean NotYetInHTML() const { return notInHTML; }
225  void SetInHTML() { notInHTML = false; }
226 
227  protected:
233 };
234 
235 
236 PARRAY(PHTTPFields, PHTTPField);
237 
239 {
241  public:
243  const char * name, // Name (identifier) for the field.
244  const char * title = NULL, // Title text for field (defaults to name).
245  const char * help = NULL, // Help text for the field.
246  bool includeHeaders = false // Make a sub-table and put headers on HTML fields.
247  );
248 
249  virtual void SetName(
250  const PString & name // New name for field
251  );
252 
253  virtual const PHTTPField * LocateName(
254  const PString & name // Full field name to locate
255  ) const;
256 
257  virtual PHTTPField * NewField() const;
258 
259  virtual void ExpandFieldNames(PString & text, PINDEX start, PINDEX & finish) const;
260 
261  virtual void GetHTMLTag(
262  PHTML & html // HTML to receive the field info.
263  ) const;
264 
265  virtual PString GetHTMLInput(
266  const PString & input // Source HTML text for input tag.
267  ) const;
268 
269  virtual void GetHTMLHeading(
270  PHTML & html // HTML to receive the field info.
271  ) const;
272 
273  virtual PString GetValue(PBoolean dflt = false) const;
274 
275  virtual void SetValue(
276  const PString & newValue // New value for the field.
277  );
278 
279  virtual bool LoadFromConfig(
280  PConfig & cfg // Configuration for value transfer.
281  );
282  virtual void SaveToConfig(
283  PConfig & cfg // Configuration for value transfer.
284  ) const;
285 
286  virtual void GetAllNames(PStringArray & names) const;
287  virtual void SetAllValues(
288  const PStringToString & data // New value for the field.
289  );
290 
291  virtual PBoolean ValidateAll(
292  const PStringToString & data, // Proposed new value for the field.
293  PStringStream & msg // Stream to take error HTML if value not valid.
294  ) const;
295 
296 
304  virtual PINDEX GetSize() const;
305 
306  void Append(PHTTPField * fld);
307  PHTTPField & operator[](PINDEX idx) const { return fields[idx]; }
308  void RemoveAt(PINDEX idx) { fields.RemoveAt(idx); }
309  void RemoveAll() { fields.RemoveAll(); }
310 
311  protected:
312  PHTTPFields fields;
314 };
315 
316 
318 {
320  public:
321  PHTTPSubForm(
322  const PString & subFormName, // URL for the sub-form
323  const char * name, // Name (identifier) for the field.
324  const char * title = NULL, // Title text for field (defaults to name).
325  PINDEX primaryField = 0, // Pimary field whove value is in hot link
326  PINDEX secondaryField = P_MAX_INDEX // Seconary field next to hotlink
327  );
328 
329  PHTTPField * NewField() const;
330  void GetHTMLTag(PHTML & html) const;
331  void GetHTMLHeading(PHTML & html) const;
332 
333  protected:
335  PINDEX primary;
336  PINDEX secondary;
337 };
338 
339 
341 {
343  public:
346  PBoolean ordered,
347  PINDEX fixedSize = 0
348  );
349 
351 
352 
353  virtual PHTTPField * NewField() const;
354 
355  virtual void ExpandFieldNames(PString & text, PINDEX start, PINDEX & finish) const;
356 
357  virtual void GetHTMLTag(
358  PHTML & html // HTML to receive the field info.
359  ) const;
360 
361  virtual bool LoadFromConfig(
362  PConfig & cfg // Configuration for value transfer.
363  );
364  virtual void SaveToConfig(
365  PConfig & cfg // Configuration for value transfer.
366  ) const;
367 
368 
369  virtual void SetAllValues(
370  const PStringToString & data // New value for the field.
371  );
372 
373  virtual PINDEX GetSize() const;
374  void SetSize(PINDEX newSize);
375 
377  PConfig & cfg,
378  const PStringArray & defaultValues
379  );
380 
381  void SetStrings(
382  PConfig & cfg,
383  const PStringArray & values
384  );
385 
386  protected:
387  void AddBlankField();
388  void AddArrayControlBox(PHTML & html, PINDEX fld) const;
389  void SetArrayFieldName(PINDEX idx) const;
390 
394 };
395 
396 
398 {
400  public:
402  const char * name,
403  PINDEX maxLength,
404  const char * initVal = NULL,
405  const char * help = NULL,
406  int rows = 0, // 0=auto, 1=InputText, >1=TextArea
407  int columns = 0
408  );
410  const char * name,
411  const char * title,
412  PINDEX maxLength,
413  const char * initVal = NULL,
414  const char * help = NULL,
415  int rows = 0, // 0=auto, 1=InputText, >1=TextArea
416  int columns = 0
417  );
418 
419  virtual PHTTPField * NewField() const;
420 
421  virtual void GetHTMLTag(
422  PHTML & html
423  ) const;
424 
425  virtual PString GetValue(PBoolean dflt = false) const;
426 
427  virtual void SetValue(
428  const PString & newVal
429  );
430 
431 
432  protected:
435  PINDEX maxLength;
436  int rows;
437  int columns;
438 };
439 
440 
442 {
444  public:
446  const char * name,
447  PINDEX size,
448  const char * initVal = NULL,
449  const char * help = NULL
450  );
452  const char * name,
453  const char * title,
454  PINDEX size,
455  const char * initVal = NULL,
456  const char * help = NULL
457  );
458 
459  virtual PHTTPField * NewField() const;
460 
461  virtual void GetHTMLTag(
462  PHTML & html
463  ) const;
464 
465  virtual PString GetValue(PBoolean dflt = false) const;
466 
467  virtual void SetValue(
468  const PString & newVal
469  );
470 
471  static PString Decrypt(const PString & pword);
472 };
473 
474 
476 {
478  public:
480  const char * name,
481  const PTime & initVal = PTime(0),
483  );
484 
485  virtual PHTTPField * NewField() const;
486 
487  virtual void SetValue(
488  const PString & newValue
489  );
490 
491  virtual PBoolean Validated(
492  const PString & newValue,
493  PStringStream & msg
494  ) const;
495 
496  protected:
498 };
499 
500 
502 {
504  public:
506  const char * name,
507  int low, int high,
508  int initVal = 0,
509  const char * units = NULL,
510  const char * help = NULL
511  );
513  const char * name,
514  const char * title,
515  int low, int high,
516  int initVal = 0,
517  const char * units = NULL,
518  const char * help = NULL
519  );
520 
521  virtual PHTTPField * NewField() const;
522 
523  virtual void GetHTMLTag(
524  PHTML & html
525  ) const;
526 
527  virtual PString GetValue(PBoolean dflt = false) const;
528 
529  virtual void SetValue(
530  const PString & newVal
531  );
532 
533  virtual bool LoadFromConfig(
534  PConfig & cfg
535  );
536  virtual void SaveToConfig(
537  PConfig & cfg
538  ) const;
539 
540  virtual PBoolean Validated(
541  const PString & newVal,
542  PStringStream & msg
543  ) const;
544 
545 
546  protected:
547  int low, high, value;
550 };
551 
552 
554 {
556  public:
558  const char * name,
559  PBoolean initVal = false,
560  const char * help = NULL
561  );
563  const char * name,
564  const char * title,
565  PBoolean initVal = false,
566  const char * help = NULL
567  );
568 
569  virtual PHTTPField * NewField() const;
570 
571  virtual void GetHTMLTag(
572  PHTML & html
573  ) const;
574 
575  virtual PString GetHTMLInput(
576  const PString & input
577  ) const;
578 
579  virtual PString GetValue(PBoolean dflt = false) const;
580 
581  virtual void SetValue(
582  const PString & newVal
583  );
584 
585  virtual bool LoadFromConfig(
586  PConfig & cfg
587  );
588  virtual void SaveToConfig(
589  PConfig & cfg
590  ) const;
591 
592 
593  protected:
595 };
596 
597 
599 {
601  public:
603  const char * name,
604  const PStringArray & valueArray,
605  PINDEX initVal = 0,
606  const char * help = NULL
607  );
609  const char * name,
610  const PStringArray & valueArray,
611  const PStringArray & titleArray,
612  PINDEX initVal = 0,
613  const char * help = NULL
614  );
616  const char * name,
617  PINDEX count,
618  const char * const * valueStrings,
619  PINDEX initVal = 0,
620  const char * help = NULL
621  );
623  const char * name,
624  PINDEX count,
625  const char * const * valueStrings,
626  const char * const * titleStrings,
627  PINDEX initVal = 0,
628  const char * help = NULL
629  );
631  const char * name,
632  const char * groupTitle,
633  const PStringArray & valueArray,
634  PINDEX initVal = 0,
635  const char * help = NULL
636  );
638  const char * name,
639  const char * groupTitle,
640  const PStringArray & valueArray,
641  const PStringArray & titleArray,
642  PINDEX initVal = 0,
643  const char * help = NULL
644  );
646  const char * name,
647  const char * groupTitle,
648  PINDEX count,
649  const char * const * valueStrings,
650  PINDEX initVal = 0,
651  const char * help = NULL
652  );
654  const char * name,
655  const char * groupTitle,
656  PINDEX count,
657  const char * const * valueStrings,
658  const char * const * titleStrings,
659  PINDEX initVal = 0,
660  const char * help = NULL
661  );
662 
663  virtual PHTTPField * NewField() const;
664 
665  virtual void GetHTMLTag(
666  PHTML & html
667  ) const;
668 
669  virtual PString GetHTMLInput(
670  const PString & input
671  ) const;
672 
673  virtual PString GetValue(PBoolean dflt = false) const;
674 
675  virtual void SetValue(
676  const PString & newVal
677  );
678 
679 
680  protected:
685 };
686 
687 
689 {
691  public:
693  const char * name,
694  const PStringArray & valueArray,
695  PINDEX initVal = 0,
696  const char * help = NULL,
697  bool enumeration = false
698  );
700  const char * name,
701  PINDEX count,
702  const char * const * valueStrings,
703  PINDEX initVal = 0,
704  const char * help = NULL,
705  bool enumeration = false
706  );
708  const char * name,
709  const char * title,
710  const PStringArray & valueArray,
711  PINDEX initVal = 0,
712  const char * help = NULL,
713  bool enumeration = false
714  );
716  const char * name,
717  const char * title,
718  PINDEX count,
719  const char * const * valueStrings,
720  PINDEX initVal = 0,
721  const char * help = NULL,
722  bool enumeration = false
723  );
724 
725  virtual PHTTPField * NewField() const;
726 
727  virtual void GetHTMLTag(
728  PHTML & html
729  ) const;
730 
731  virtual PString GetValue(PBoolean dflt = false) const;
732 
733  virtual void SetValue(
734  const PString & newVal
735  );
736 
737  protected:
738  void Construct(PINDEX initVal, bool enumeration);
739 
744 };
745 
746 
747 template <typename Enumeration, Enumeration InitVal = (Enumeration)0>
749 {
751  public:
753  const char * name,
754  const PStringArray & valueArray,
755  Enumeration initVal = InitVal,
756  const char * help = NULL
757  ) : PHTTPSelectField(name, valueArray, initVal, help, true) { }
759  const char * name,
760  PINDEX count,
761  const char * const * valueStrings,
762  Enumeration initVal = InitVal,
763  const char * help = NULL
764  ) : PHTTPSelectField(name, count, valueStrings, initVal, help, true) { }
766  const char * name,
767  const char * title,
768  const PStringArray & valueArray,
769  Enumeration initVal = InitVal,
770  const char * help = NULL
771  ) : PHTTPSelectField(name, title, valueArray, initVal, help, true) { }
773  const char * name,
774  const char * title,
775  PINDEX count,
776  const char * const * valueStrings,
777  Enumeration initVal = InitVal,
778  const char * help = NULL
779  ) : PHTTPSelectField(name, title, count, valueStrings, initVal, help, true) { }
780 
781  virtual PHTTPField * NewField() const { return new PHTTPEnumField(baseName, title, m_values, (Enumeration)m_initialValue, help); }
782 };
783 
784 
786 // PHTTPForm
787 
788 class PHTTPForm : public PHTTPString
789 {
791  public:
792  PHTTPForm(
793  const PURL & url
794  );
795  PHTTPForm(
796  const PURL & url,
797  const PHTTPAuthority & auth
798  );
799  PHTTPForm(
800  const PURL & url,
801  const PString & html
802  );
803  PHTTPForm(
804  const PURL & url,
805  const PString & html,
806  const PHTTPAuthority & auth
807  );
808 
809 
810  virtual void OnLoadedText(
811  PHTTPRequest & request,
812  PString & text
813  );
814  virtual PBoolean Post(
815  PHTTPRequest & request,
816  const PStringToString & data,
817  PHTML & replyMessage
818  );
819 
820 
821  PHTTPField * Add(
822  PHTTPField * fld
823  );
826 
831  };
832 
833  void BuildHTML(
834  const char * heading
835  );
836  void BuildHTML(
837  const PString & heading
838  );
839  void BuildHTML(
840  PHTML & html,
841  BuildOptions option = CompleteHTML
842  );
843 
844 
845  protected:
848 };
849 
850 
852 // PHTTPConfig
853 
854 class PHTTPConfig : public PHTTPForm
855 {
857  public:
858  PHTTPConfig(
859  const PURL & url,
860  const PString & section
861  );
862  PHTTPConfig(
863  const PURL & url,
864  const PString & section,
865  const PHTTPAuthority & auth
866  );
867  PHTTPConfig(
868  const PURL & url,
869  const PString & section,
870  const PString & html
871  );
872  PHTTPConfig(
873  const PURL & url,
874  const PString & section,
875  const PString & html,
876  const PHTTPAuthority & auth
877  );
878 
879  virtual void OnLoadedText(
880  PHTTPRequest & request,
881  PString & text
882  );
883  virtual PBoolean Post(
884  PHTTPRequest & request,
885  const PStringToString & data,
886  PHTML & replyMessage
887  );
888 
889 
892  bool LoadFromConfig();
893 
899  const PString & GetConfigSection() const { return section; }
900 
902  const PString & sect
903  ) { section = sect; }
904  // Set the configuration file section.
905 
911  PHTTPField * sectionFld,
912  const char * prefix = NULL,
913  const char * suffix = NULL
914  );
915 
919  void AddNewKeyFields(
920  PHTTPField * keyFld,
921  PHTTPField * valFld
922  );
923 
927  bool AddBooleanField(
928  const char * name,
929  bool defaultValue = false,
930  const char * help = NULL
931  );
932 
936  int AddIntegerField(
937  const char * name,
938  int low, int high,
939  int defaultValue = 0,
940  const char * units = NULL,
941  const char * help = NULL
942  );
943 
948  const char * name,
949  PINDEX maxLength,
950  const char * defaultValue = NULL,
951  const char * help = NULL,
952  int rows = 0, // 0=auto, 1=InputText, >1=TextArea
953  int columns = 0
954  );
955 
960  const char * name,
961  bool sorted,
962  PINDEX maxLength,
963  const PStringArray & defaultValues,
964  const char * help = NULL,
965  int rows = 0, // 0=auto, 1=InputText, >1=TextArea
966  int columns = 0
967  );
968 
973  const char * name,
974  const PStringArray & valueArray,
975  const char * defaultValue = NULL,
976  const char * help = NULL,
977  bool enumeration = false
978  );
979 
984  const char * name,
985  bool sorted,
986  const PStringArray & defaultValues,
987  const PStringArray & possibleValues,
988  const char * help = NULL,
989  bool enumeration = false
990  );
991 
992  protected:
999 
1000  private:
1001  void Construct();
1002 };
1003 
1004 
1006 // PHTTPConfigSectionList
1007 
1009 {
1011  public:
1013  const PURL & url,
1014  const PHTTPAuthority & auth,
1015  const PString & sectionPrefix,
1016  const PString & additionalValueName,
1017  const PURL & editSection,
1018  const PURL & newSection,
1019  const PString & newSectionTitle,
1020  PHTML & heading
1021  );
1022 
1023  virtual void OnLoadedText(
1024  PHTTPRequest & request,
1025  PString & text
1026  );
1027  virtual PBoolean Post(
1028  PHTTPRequest & request,
1029  const PStringToString & data,
1030  PHTML & replyMessage
1031  );
1032 
1033  protected:
1039 };
1040 
1041 
1042 #endif // P_HTTPFORMS
1043 
1044 #endif // PTLIB_HTTPFORM_H
1045 
1046 
1047 // End Of File ///////////////////////////////////////////////////////////////