forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaptemplate.c
4909 lines (4053 loc) · 165 KB
/
maptemplate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
* $id: maptemplate.c 7725 2008-06-21 15:56:58Z sdlime $
*
* Project: MapServer
* Purpose: Various template processing functions.
* Author: Steve Lime and the MapServer team.
*
******************************************************************************
* Copyright (c) 1996-2008 Regents of the University of Minnesota.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies of this Software or works derived from this Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#define NEED_IGNORE_RET_VAL
#include "maptemplate.h"
#include "maphash.h"
#include "mapserver.h"
#include "maptile.h"
#include "mapows.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <assert.h>
#include <ctype.h>
static inline void IGUR_sizet(size_t ignored) { (void)ignored; } /* Ignore GCC Unused Result */
static inline void IGUR_voidp(void* ignored) { (void)ignored; } /* Ignore GCC Unused Result */
static char *olUrl = "//www.mapserver.org/lib/OpenLayers-ms60.js";
static char *olTemplate = \
"<html>\n"
"<head>\n"
"<meta content=\"text/html;charset=utf-8\" http-equiv=\"Content-Type\">\n"
" <title>MapServer Simple Viewer</title>\n"
" <script type=\"text/javascript\" src=\"[openlayers_js_url]\"></script>\n"
" <link rel=\"shortcut icon\" type=\"image/x-icon\" href=\"//www.mapserver.org/_static/mapserver.ico\"/>\n"
" </head>\n"
" <body>\n"
" <div style=\"width:[mapwidth]; height:[mapheight]\" id=\"map\"></div>\n"
" <script defer=\"defer\" type=\"text/javascript\">\n"
" var map = new OpenLayers.Map('map',\n"
" {maxExtent: new OpenLayers.Bounds([minx],[miny],[maxx],[maxy]),\n"
" maxResolution: [cellsize]});\n"
" [openlayers_layer];\n"
" map.addLayer(mslayer);\n"
" map.zoomToMaxExtent();\n"
" </script>\n"
"</body>\n"
"</html>";
static char *olLayerMapServerTag = \
"var mslayer = new OpenLayers.Layer.MapServer( \"MapServer Layer\",\n"
" \"[mapserv_onlineresource]\",\n"
" {layers: '[layers]'},\n"
" {singleTile: \"true\", ratio:1} )";
static char *olLayerWMSTag = \
"var mslayer = new OpenLayers.Layer.WMS('MapServer Simple Viewer\',\n"
" '[mapserv_onlineresource]',\n"
" {layers: '[LAYERS]',\n"
" bbox: '[minx],[miny],[maxx],[maxy]',\n"
" width: [mapwidth], height: [mapheight], version: '[VERSION]', format:'[openlayers_format]'},"
" {singleTile: \"true\", ratio:1, projection: '[openlayers_projection]'});\n";
static char *processLine(mapservObj *mapserv, char *instr, FILE *stream, int mode);
static int isValidTemplate(FILE *stream, const char *filename)
{
char buffer[MS_BUFFER_LENGTH];
if(fgets(buffer, MS_BUFFER_LENGTH, stream) != NULL) {
if(!strcasestr(buffer, MS_TEMPLATE_MAGIC_STRING)) {
msSetError(MS_WEBERR, "Missing magic string, %s doesn't look like a MapServer template.", "isValidTemplate()", filename);
return MS_FALSE;
}
}
return MS_TRUE;
}
/*
* Redirect to (only use in CGI)
*
*/
int msRedirect(char *url)
{
msIO_setHeader("Status","302 Found");
msIO_setHeader("Uri","%s",url);
msIO_setHeader("Location","%s",url);
msIO_setHeader("Content-Type","text/html");
msIO_sendHeaders();
return MS_SUCCESS;
}
/*
** Sets the map extent under a variety of scenarios.
*/
int setExtent(mapservObj *mapserv)
{
double cellx,celly,cellsize;
if(mapserv->Mode == TILE) {
if(MS_SUCCESS != msTileSetExtent(mapserv)) {
return MS_FAILURE;
}
}
switch(mapserv->CoordSource) {
case FROMUSERBOX: /* user passed in a map extent */
break;
case FROMIMGBOX: /* fully interactive web, most likely with java front end */
cellx = MS_CELLSIZE(mapserv->ImgExt.minx, mapserv->ImgExt.maxx, mapserv->ImgCols);
celly = MS_CELLSIZE(mapserv->ImgExt.miny, mapserv->ImgExt.maxy, mapserv->ImgRows);
mapserv->map->extent.minx = MS_IMAGE2MAP_X(mapserv->ImgBox.minx, mapserv->ImgExt.minx, cellx);
mapserv->map->extent.maxx = MS_IMAGE2MAP_X(mapserv->ImgBox.maxx, mapserv->ImgExt.minx, cellx);
mapserv->map->extent.maxy = MS_IMAGE2MAP_Y(mapserv->ImgBox.miny, mapserv->ImgExt.maxy, celly); /* y's are flip flopped because img/map coordinate systems are */
mapserv->map->extent.miny = MS_IMAGE2MAP_Y(mapserv->ImgBox.maxy, mapserv->ImgExt.maxy, celly);
break;
case FROMIMGPNT:
cellx = MS_CELLSIZE(mapserv->ImgExt.minx, mapserv->ImgExt.maxx, mapserv->ImgCols);
celly = MS_CELLSIZE(mapserv->ImgExt.miny, mapserv->ImgExt.maxy, mapserv->ImgRows);
mapserv->mappnt.x = MS_IMAGE2MAP_X(mapserv->ImgPnt.x, mapserv->ImgExt.minx, cellx);
mapserv->mappnt.y = MS_IMAGE2MAP_Y(mapserv->ImgPnt.y, mapserv->ImgExt.maxy, celly);
mapserv->map->extent.minx = mapserv->mappnt.x - .5*((mapserv->ImgExt.maxx - mapserv->ImgExt.minx)/mapserv->fZoom); /* create an extent around that point */
mapserv->map->extent.miny = mapserv->mappnt.y - .5*((mapserv->ImgExt.maxy - mapserv->ImgExt.miny)/mapserv->fZoom);
mapserv->map->extent.maxx = mapserv->mappnt.x + .5*((mapserv->ImgExt.maxx - mapserv->ImgExt.minx)/mapserv->fZoom);
mapserv->map->extent.maxy = mapserv->mappnt.y + .5*((mapserv->ImgExt.maxy - mapserv->ImgExt.miny)/mapserv->fZoom);
break;
case FROMREFPNT:
cellx = MS_CELLSIZE(mapserv->map->reference.extent.minx, mapserv->map->reference.extent.maxx, mapserv->map->reference.width);
celly = MS_CELLSIZE(mapserv->map->reference.extent.miny, mapserv->map->reference.extent.maxy, mapserv->map->reference.height);
mapserv->mappnt.x = MS_IMAGE2MAP_X(mapserv->RefPnt.x, mapserv->map->reference.extent.minx, cellx);
mapserv->mappnt.y = MS_IMAGE2MAP_Y(mapserv->RefPnt.y, mapserv->map->reference.extent.maxy, celly);
mapserv->map->extent.minx = mapserv->mappnt.x - .5*(mapserv->ImgExt.maxx - mapserv->ImgExt.minx); /* create an extent around that point */
mapserv->map->extent.miny = mapserv->mappnt.y - .5*(mapserv->ImgExt.maxy - mapserv->ImgExt.miny);
mapserv->map->extent.maxx = mapserv->mappnt.x + .5*(mapserv->ImgExt.maxx - mapserv->ImgExt.minx);
mapserv->map->extent.maxy = mapserv->mappnt.y + .5*(mapserv->ImgExt.maxy - mapserv->ImgExt.miny);
break;
case FROMBUF:
mapserv->map->extent.minx = mapserv->mappnt.x - mapserv->Buffer; /* create an extent around that point, using the buffer */
mapserv->map->extent.miny = mapserv->mappnt.y - mapserv->Buffer;
mapserv->map->extent.maxx = mapserv->mappnt.x + mapserv->Buffer;
mapserv->map->extent.maxy = mapserv->mappnt.y + mapserv->Buffer;
break;
case FROMSCALE:
cellsize = (mapserv->ScaleDenom/mapserv->map->resolution)/msInchesPerUnit(mapserv->map->units,0); /* user supplied a point and a scale denominator */
mapserv->map->extent.minx = mapserv->mappnt.x - cellsize*(mapserv->map->width-1)/2.0;
mapserv->map->extent.miny = mapserv->mappnt.y - cellsize*(mapserv->map->height-1)/2.0;
mapserv->map->extent.maxx = mapserv->mappnt.x + cellsize*(mapserv->map->width-1)/2.0;
mapserv->map->extent.maxy = mapserv->mappnt.y + cellsize*(mapserv->map->height-1)/2.0;
break;
default: /* use the default in the mapfile if it exists */
if((mapserv->map->extent.minx == mapserv->map->extent.maxx) && (mapserv->map->extent.miny == mapserv->map->extent.maxy)) {
msSetError(MS_WEBERR, "No way to generate map extent.", "mapserv()");
return MS_FAILURE;
}
}
mapserv->RawExt = mapserv->map->extent; /* save unaltered extent */
return MS_SUCCESS;
}
int checkWebScale(mapservObj *mapserv)
{
int status;
rectObj work_extent = mapserv->map->extent;
mapserv->map->cellsize = msAdjustExtent(&(work_extent), mapserv->map->width, mapserv->map->height); /* we do this cause we need a scale */
if((status = msCalculateScale(work_extent, mapserv->map->units, mapserv->map->width, mapserv->map->height, mapserv->map->resolution, &mapserv->map->scaledenom)) != MS_SUCCESS) return status;
if((mapserv->map->scaledenom < mapserv->map->web.minscaledenom) && (mapserv->map->web.minscaledenom > 0)) {
if(mapserv->map->web.mintemplate) { /* use the template provided */
if(TEMPLATE_TYPE(mapserv->map->web.mintemplate) == MS_FILE) {
if((status = msReturnPage(mapserv, mapserv->map->web.mintemplate, BROWSE, NULL)) != MS_SUCCESS) return status;
} else {
if((status = msReturnURL(mapserv, mapserv->map->web.mintemplate, BROWSE)) != MS_SUCCESS) return status;
}
} else { /* force zoom = 1 (i.e. pan) */
mapserv->fZoom = mapserv->Zoom = 1;
mapserv->ZoomDirection = 0;
mapserv->CoordSource = FROMSCALE;
mapserv->ScaleDenom = mapserv->map->web.minscaledenom;
mapserv->mappnt.x = (mapserv->map->extent.maxx + mapserv->map->extent.minx)/2; /* use center of bad extent */
mapserv->mappnt.y = (mapserv->map->extent.maxy + mapserv->map->extent.miny)/2;
setExtent(mapserv);
mapserv->map->cellsize = msAdjustExtent(&(mapserv->map->extent), mapserv->map->width, mapserv->map->height);
if((status = msCalculateScale(mapserv->map->extent, mapserv->map->units, mapserv->map->width, mapserv->map->height, mapserv->map->resolution, &mapserv->map->scaledenom)) != MS_SUCCESS) return status;
}
} else {
if((mapserv->map->scaledenom > mapserv->map->web.maxscaledenom) && (mapserv->map->web.maxscaledenom > 0)) {
if(mapserv->map->web.maxtemplate) { /* use the template provided */
if(TEMPLATE_TYPE(mapserv->map->web.maxtemplate) == MS_FILE) {
if((status = msReturnPage(mapserv, mapserv->map->web.maxtemplate, BROWSE, NULL)) != MS_SUCCESS) return status;
} else {
if((status = msReturnURL(mapserv, mapserv->map->web.maxtemplate, BROWSE)) != MS_SUCCESS) return status;
}
} else { /* force zoom = 1 (i.e. pan) */
mapserv->fZoom = mapserv->Zoom = 1;
mapserv->ZoomDirection = 0;
mapserv->CoordSource = FROMSCALE;
mapserv->ScaleDenom = mapserv->map->web.maxscaledenom;
mapserv->mappnt.x = (mapserv->map->extent.maxx + mapserv->map->extent.minx)/2; /* use center of bad extent */
mapserv->mappnt.y = (mapserv->map->extent.maxy + mapserv->map->extent.miny)/2;
setExtent(mapserv);
mapserv->map->cellsize = msAdjustExtent(&(mapserv->map->extent), mapserv->map->width, mapserv->map->height);
if((status = msCalculateScale(mapserv->map->extent, mapserv->map->units, mapserv->map->width, mapserv->map->height, mapserv->map->resolution, &mapserv->map->scaledenom)) != MS_SUCCESS) return status;
}
}
}
return MS_SUCCESS;
}
int msReturnTemplateQuery(mapservObj *mapserv, char *queryFormat, char **papszBuffer)
{
imageObj *img = NULL;
int i, status;
outputFormatObj *outputFormat=NULL;
mapObj *map = mapserv->map;
if(!queryFormat) {
msSetError(MS_WEBERR, "Return format/mime-type not specified.", "msReturnTemplateQuery()");
return MS_FAILURE;
}
msApplyDefaultOutputFormats(map);
i = msGetOutputFormatIndex(map, queryFormat); /* queryFormat can be a mime-type or name */
if(i >= 0) outputFormat = map->outputformatlist[i];
if(outputFormat) {
if( MS_RENDERER_PLUGIN(outputFormat) ) {
msInitializeRendererVTable(outputFormat);
}
if( MS_RENDERER_OGR(outputFormat) ) {
checkWebScale(mapserv);
status = msOGRWriteFromQuery(map, outputFormat, mapserv->sendheaders);
return status;
}
if( !MS_RENDERER_TEMPLATE(outputFormat) ) { /* got an image format, return the query results that way */
outputFormatObj *tempOutputFormat = map->outputformat; /* save format */
checkWebScale(mapserv);
map->outputformat = outputFormat; /* override what was given for IMAGETYPE */
img = msDrawMap(map, MS_TRUE);
if(!img) return MS_FAILURE;
map->outputformat = tempOutputFormat; /* restore format */
if(mapserv->sendheaders) {
msIO_setHeader("Content-Type", "%s", MS_IMAGE_MIME_TYPE(outputFormat));
msIO_sendHeaders();
}
status = msSaveImage(map, img, NULL);
msFreeImage(img);
return status;
}
}
/*
** At this point we know we have a template of some sort, either the new style that references a or the old
** style made up of external files slammed together. Either way we may have to compute a query map and other
** images. We only create support images IF the querymap has status=MS_ON.
*/
if(map->querymap.status) {
checkWebScale(mapserv);
if(msGenerateImages(mapserv, MS_TRUE, MS_TRUE) != MS_SUCCESS)
return MS_FAILURE;
}
if(outputFormat) {
const char *file = msGetOutputFormatOption( outputFormat, "FILE", NULL );
if(!file) {
msSetError(MS_WEBERR, "Template driver requires \"FILE\" format option be set.", "msReturnTemplateQuery()");
return MS_FAILURE;
}
if(mapserv->sendheaders) {
const char *attachment = msGetOutputFormatOption( outputFormat, "ATTACHMENT", NULL );
if(attachment)
msIO_setHeader("Content-disposition","attachment; filename=%s", attachment);
msIO_setHeader("Content-Type", "%s", outputFormat->mimetype);
msIO_sendHeaders();
}
if((status = msReturnPage(mapserv, (char *) file, BROWSE, papszBuffer)) != MS_SUCCESS)
return status;
} else {
if((status = msReturnNestedTemplateQuery(mapserv, queryFormat, papszBuffer)) != MS_SUCCESS)
return status;
}
return MS_SUCCESS;
}
/*
** Is a particular layer or group on, that is was it requested explicitly by the user.
*/
int isOn(mapservObj *mapserv, char *name, char *group)
{
int i;
for(i=0; i<mapserv->NumLayers; i++) {
if(name && strcmp(mapserv->Layers[i], name) == 0) return(MS_TRUE);
if(group && strcmp(mapserv->Layers[i], group) == 0) return(MS_TRUE);
}
return(MS_FALSE);
}
/************************************************************************/
/* int sortLayerByOrder(mapObj *map, char* pszOrder) */
/* */
/* sorth the displaying in ascending or descending order. */
/************************************************************************/
int sortLayerByOrder(mapObj *map, const char* pszOrder)
{
int *panCurrentOrder = NULL;
if(!map) {
msSetError(MS_WEBERR, "Invalid pointer.", "sortLayerByOrder()");
return MS_FAILURE;
}
/* ==================================================================== */
/* The flag "ascending" is in fact not useful since the */
/* default ordering is ascending. */
/* ==================================================================== */
/* -------------------------------------------------------------------- */
/* the map->layerorder should be set at this point in the */
/* sortLayerByMetadata. */
/* -------------------------------------------------------------------- */
if(map->layerorder) {
panCurrentOrder = (int*)msSmallMalloc(map->numlayers * sizeof(int));
for (int i=0; i<map->numlayers ; i++)
panCurrentOrder[i] = map->layerorder[i];
if(strcasecmp(pszOrder, "DESCENDING") == 0) {
for (int i=0; i<map->numlayers; i++)
map->layerorder[i] = panCurrentOrder[map->numlayers-1-i];
}
free(panCurrentOrder);
}
return MS_SUCCESS;
}
/*!
* This function set the map->layerorder
* index order by the metadata collumn name
*/
static int sortLayerByMetadata(mapObj *map, const char* pszMetadata)
{
int nLegendOrder1;
int nLegendOrder2;
int i, j;
int tmp;
if(!map) {
msSetError(MS_WEBERR, "Invalid pointer.", "sortLayerByMetadata()");
return MS_FAILURE;
}
/*
* Initiate to default order (Reverse mapfile order)
*/
if(map->layerorder) {
int *pnLayerOrder;
/* Backup the original layer order to be able to reverse it */
pnLayerOrder = (int*)msSmallMalloc(map->numlayers * sizeof(int));
for (i=0; i<map->numlayers ; i++)
pnLayerOrder[i] = map->layerorder[i];
/* Get a new layerorder array */
free(map->layerorder);
map->layerorder = (int*)msSmallMalloc(map->numlayers * sizeof(int));
/* Reverse the layerorder array */
for (i=0; i<map->numlayers ; i++)
map->layerorder[i] = pnLayerOrder[map->numlayers - i - 1];
free(pnLayerOrder);
} else {
map->layerorder = (int*)msSmallMalloc(map->numlayers * sizeof(int));
for (i=0; i<map->numlayers ; i++)
map->layerorder[i] = map->numlayers - i - 1;
}
if(!pszMetadata)
return MS_SUCCESS;
/*
* Bubble sort algo (not very efficient)
* should implement a kind of quick sort
* alog instead
*/
for (i=0; i<map->numlayers-1; i++) {
for (j=0; j<map->numlayers-1-i; j++) {
const char* pszLegendOrder1 = msLookupHashTable(&(GET_LAYER(map, map->layerorder[j+1])->metadata), pszMetadata);
const char* pszLegendOrder2 = msLookupHashTable(&(GET_LAYER(map, map->layerorder[j])->metadata), pszMetadata);
if(!pszLegendOrder1 || !pszLegendOrder2)
continue;
nLegendOrder1 = atoi(pszLegendOrder1);
nLegendOrder2 = atoi(pszLegendOrder2);
if(nLegendOrder1 < nLegendOrder2) { /* compare the two neighbors */
tmp = map->layerorder[j]; /* swap a[j] and a[j+1] */
map->layerorder[j] = map->layerorder[j+1];
map->layerorder[j+1] = tmp;
}
}
}
return MS_SUCCESS;
}
/*
** This function return a pointer
** at the begining of the first occurence
** of pszTag in pszInstr.
**
** Tag can be [TAG] or [TAG something]
*/
char *findTag(char *pszInstr, char *pszTag)
{
char *pszTag1, *pszStart=NULL;
char *pszTemp;
int done=MS_FALSE;
int length;
if(!pszInstr || !pszTag) {
msSetError(MS_WEBERR, "Invalid pointer.", "findTag()");
return NULL;
}
length = strlen(pszTag) + 1; /* adding [ character to the beginning */
pszTag1 = (char*) msSmallMalloc(length+1);
strcpy(pszTag1, "[");
strcat(pszTag1, pszTag);
pszTemp = pszInstr;
while(!done) {
pszStart = strstr(pszTemp, pszTag1);
if(pszStart == NULL)
done = MS_TRUE; /* tag not found */
else if((*(pszStart+length) == ']' || *(pszStart+length) == ' '))
done = MS_TRUE; /* valid tag */
else
pszTemp += length; /* skip ahead and start over */
}
free(pszTag1);
return pszStart;
}
/*
** This function return a pointer
** to the end of the tag in pszTag
**
** The end of a tag is the next
** non-quoted ']' character.
** Return NULL if not found.
*/
char *findTagEnd(const char *pszTag)
{
char *pszEnd = NULL,
*pszTmp = (char*)pszTag;
while (pszTmp != NULL) {
if (*pszTmp == '"')
pszTmp = strchr(pszTmp+1,'"');
if ((pszTmp == NULL) || (*pszTmp == ']')) {
pszEnd = pszTmp;
pszTmp = NULL;
} else
pszTmp++;
}
return pszEnd;
}
/*
** Return a hashtableobj from instr of all
** arguments. hashtable must be freed by caller.
*/
int getTagArgs(char* pszTag, char* pszInstr, hashTableObj **ppoHashTable)
{
char *pszStart, *pszEnd, *pszArgs;
int nLength;
char **papszArgs, **papszVarVal;
int nArgs, nDummy;
int i;
if(!pszTag || !pszInstr) {
msSetError(MS_WEBERR, "Invalid pointer.", "getTagArgs()");
return MS_FAILURE;
}
/* set position to the begining of tag */
pszStart = findTag(pszInstr, pszTag);
if(pszStart) {
/* find ending position */
pszEnd = findTagEnd(pszStart);
if(pszEnd) {
/* skip the tag name */
pszStart = pszStart + strlen(pszTag) + 1;
/* get length of all args */
nLength = pszEnd - pszStart;
if(nLength > 0) { /* is there arguments ? */
pszArgs = (char*)msSmallMalloc(nLength + 1);
strlcpy(pszArgs, pszStart, nLength+1);
if(!(*ppoHashTable))
*ppoHashTable = msCreateHashTable();
/* put all arguments seperate by space in a hash table */
papszArgs = msStringTokenize(pszArgs, " ", &nArgs, MS_TRUE);
/* msReturnTemplateQuerycheck all argument if they have values */
for (i=0; i<nArgs; i++) {
if(strlen(papszArgs[i]) == 0) {
free(papszArgs[i]);
continue;
}
if(strchr(papszArgs[i], '=')) {
papszVarVal = msStringTokenize(papszArgs[i], "=", &nDummy, MS_FALSE);
msInsertHashTable(*ppoHashTable, papszVarVal[0],
papszVarVal[1]);
free(papszVarVal[0]);
free(papszVarVal[1]);
free(papszVarVal);
} else /* no value specified. set it to 1 */
msInsertHashTable(*ppoHashTable, papszArgs[i], "1");
free(papszArgs[i]);
}
free(papszArgs);
free(pszArgs);
}
}
}
return MS_SUCCESS;
}
/*
** Return a substring from instr between [tag] and [/tag]
** char * returned must be freed by caller.
** pszNextInstr will be a pointer at the end of the
** first occurence found.
*/
int getInlineTag(char *pszTag, char *pszInstr, char **pszResult)
{
char *pszStart, *pszEnd=NULL, *pszEndTag, *pszPatIn, *pszPatOut=NULL, *pszTmp;
int nInst=0;
int nLength;
*pszResult = NULL;
if(!pszInstr || !pszTag) {
msSetError(MS_WEBERR, "Invalid pointer.", "getInlineTag()");
return MS_FAILURE;
}
pszEndTag = (char*)msSmallMalloc(strlen(pszTag) + 3);
strcpy(pszEndTag, "[/");
strcat(pszEndTag, pszTag);
/* find start tag */
pszPatIn = findTag(pszInstr, pszTag);
pszPatOut = strstr(pszInstr, pszEndTag);
pszStart = pszPatIn;
pszTmp = pszInstr;
if(pszPatIn) {
do {
if(pszPatIn && pszPatIn < pszPatOut) {
nInst++;
pszTmp = pszPatIn;
}
if(pszPatOut && ((pszPatIn == NULL) || pszPatOut < pszPatIn)) {
pszEnd = pszPatOut;
nInst--;
pszTmp = pszPatOut;
}
pszPatIn = findTag(pszTmp+1, pszTag);
pszPatOut = strstr(pszTmp+1, pszEndTag);
} while (pszTmp != NULL && nInst > 0);
}
if(pszStart && pszEnd) {
/* find end of start tag */
pszStart = strchr(pszStart, ']');
if(pszStart) {
pszStart++;
nLength = pszEnd - pszStart;
if(nLength > 0) {
*pszResult = (char*)msSmallMalloc(nLength + 1);
/* copy string beetween start and end tag */
strlcpy(*pszResult, pszStart, nLength+1);
(*pszResult)[nLength] = '\0';
}
} else {
msSetError(MS_WEBERR, "Malformed [%s] tag.", "getInlineTag()", pszTag);
return MS_FAILURE;
}
}
msFree(pszEndTag);
return MS_SUCCESS;
}
/*!
* this function process all if tag in pszInstr.
* this function return a modified pszInstr.
* ht mus contain all variables needed by the function
* to interpret if expression.
*
* If bLastPass is true then all tests for 'null' values will be
* considered true if the corresponding value is not set.
*/
int processIfTag(char **pszInstr, hashTableObj *ht, int bLastPass)
{
/* char *pszNextInstr = pszInstr; */
char *pszStart, *pszEnd=NULL;
const char *pszName, *pszValue, *pszOperator, *pszHTValue;
char *pszThen=NULL;
char *pszIfTag;
char *pszPatIn=NULL, *pszPatOut=NULL, *pszTmp;
int nInst = 0;
int nLength;
hashTableObj *ifArgs=NULL;
if(!*pszInstr) {
msSetError(MS_WEBERR, "Invalid pointer.", "processIfTag()");
return MS_FAILURE;
}
/* find the if start tag */
pszStart = findTag(*pszInstr, "if");
while (pszStart) {
pszPatIn = findTag(pszStart, "if");
pszPatOut = strstr(pszStart, "[/if]");
pszTmp = pszPatIn;
do {
if(pszPatIn && pszPatIn < pszPatOut) {
nInst++;
pszTmp = pszPatIn;
}
if(pszPatOut && ((pszPatIn == NULL) || pszPatOut < pszPatIn)) {
pszEnd = pszPatOut;
nInst--;
pszTmp = pszPatOut;
}
pszPatIn = findTag(pszTmp+1, "if");
pszPatOut = strstr(pszTmp+1, "[/if]");
} while (nInst > 0);
/* get the then string (if expression is true) */
if(getInlineTag("if", pszStart, &pszThen) != MS_SUCCESS) {
msSetError(MS_WEBERR, "Malformed then if tag.", "processIfTag()");
return MS_FAILURE;
}
/* retrieve if tag args */
if(getTagArgs("if", pszStart, &ifArgs) != MS_SUCCESS) {
msSetError(MS_WEBERR, "Malformed args if tag.", "processIfTag()");
return MS_FAILURE;
}
pszName = msLookupHashTable(ifArgs, "name");
pszValue = msLookupHashTable(ifArgs, "value");
pszOperator = msLookupHashTable(ifArgs, "oper");
if(pszOperator == NULL) /* Default operator if not set is "eq" */
pszOperator = "eq";
int bEmpty = 0;
if(pszName) {
/* build the complete if tag ([if all_args]then string[/if]) */
/* to replace if by then string if expression is true */
/* or by a white space if not. */
nLength = pszEnd - pszStart;
pszIfTag = (char*)msSmallMalloc(nLength + 6);
strlcpy(pszIfTag, pszStart, nLength+1);
pszIfTag[nLength] = '\0';
strcat(pszIfTag, "[/if]");
pszHTValue = msLookupHashTable(ht, pszName);
if(strcmp(pszOperator, "neq") == 0) {
if(pszValue && pszHTValue && strcasecmp(pszValue, pszHTValue) != 0) {
*pszInstr = msReplaceSubstring(*pszInstr, pszIfTag, pszThen);
} else if(pszHTValue) {
*pszInstr = msReplaceSubstring(*pszInstr, pszIfTag, "");
bEmpty = 1;
}
} else if(strcmp(pszOperator, "eq") == 0) {
if(pszValue && pszHTValue && strcasecmp(pszValue, pszHTValue) == 0) {
*pszInstr = msReplaceSubstring(*pszInstr, pszIfTag, pszThen);
} else if(pszHTValue) {
*pszInstr = msReplaceSubstring(*pszInstr, pszIfTag, "");
bEmpty = 1;
}
} else if(strcmp(pszOperator, "isnull") == 0) {
if(pszHTValue != NULL) {
/* We met a non-null value... condition is false */
*pszInstr = msReplaceSubstring(*pszInstr, pszIfTag, "");
bEmpty = 1;
} else if(bLastPass) {
/* On last pass, if value is still null then condition is true */
*pszInstr = msReplaceSubstring(*pszInstr, pszIfTag, pszThen);
}
} else if(strcmp(pszOperator, "isset") == 0) {
if(pszHTValue != NULL) {
/* Found a non-null value... condition is true */
*pszInstr = msReplaceSubstring(*pszInstr, pszIfTag, pszThen);
} else if(bLastPass) {
/* On last pass, if value still not set then condition is false */
*pszInstr = msReplaceSubstring(*pszInstr, pszIfTag, "");
bEmpty = 1;
}
} else {
msSetError(MS_WEBERR, "Unsupported operator (%s) in if tag.", "processIfTag()", pszOperator);
return MS_FAILURE;
}
free(pszIfTag);
pszIfTag = NULL;
}
if(pszThen)
free (pszThen);
pszThen=NULL;
msFreeHashTable(ifArgs);
ifArgs=NULL;
/* find the if start tag */
if(bEmpty)
pszStart = findTag(pszStart, "if");
else
pszStart = findTag(pszStart + 1, "if");
}
return MS_SUCCESS;
}
/* Helper function to return the text before the supplied string2 in string1. */
static char *getPreTagText(const char *string1, const char *string2)
{
int n;
char *result, *tmpstr;
if((tmpstr = strstr(string1, string2)) == NULL) return msStrdup(""); /* return an empty string */
n = strlen(string1) - strlen(tmpstr);
result = (char *) msSmallMalloc(n + 1);
strlcpy(result, string1, n+1);
return result;
}
/* Helper function to retunr the text after the supplied string2 in string1. */
static char *getPostTagText(const char *string1, const char *string2)
{
char *tmpstr;
if((tmpstr = strstr(string1, string2)) == NULL) return msStrdup(""); /* return an empty string */
tmpstr += strlen(string2); /* skip string2 */
return msStrdup(tmpstr);
}
/*
** Function to process a [feature ...] tag. This tag can *only* be found within
** a [resultset ...][/resultset] block.
*/
static int processFeatureTag(mapservObj *mapserv, char **line, layerObj *layer)
{
char *preTag, *postTag; /* text before and after the tag */
const char *argValue;
char *tag, *tagInstance, *tagStart;
hashTableObj *tagArgs=NULL;
int limit=-1;
const char *trimLast=NULL;
int i, j, status;
if(!*line) {
msSetError(MS_WEBERR, "Invalid line pointer.", "processFeatureTag()");
return(MS_FAILURE);
}
tagStart = findTag(*line, "feature");
if(!tagStart) return(MS_SUCCESS); /* OK, just return; */
/* check for any tag arguments */
if(getTagArgs("feature", tagStart, &tagArgs) != MS_SUCCESS) return(MS_FAILURE);
if(tagArgs) {
argValue = msLookupHashTable(tagArgs, "limit");
if(argValue) limit = atoi(argValue);
argValue = msLookupHashTable(tagArgs, "trimlast");
if(argValue) trimLast = argValue;
}
if(strstr(*line, "[/feature]") == NULL) { /* we know the closing tag must be here, if not throw an error */
msSetError(MS_WEBERR, "[feature] tag found without closing [/feature].", "processFeatureTag()");
msFreeHashTable(tagArgs);
return(MS_FAILURE);
}
if(getInlineTag("feature", *line, &tag) != MS_SUCCESS) {
msSetError(MS_WEBERR, "Malformed feature tag.", "processFeatureTag()");
msFreeHashTable(tagArgs);
return MS_FAILURE;
}
preTag = getPreTagText(*line, "[feature");
postTag = getPostTagText(*line, "[/feature]");
/* start rebuilding **line */
free(*line);
*line = preTag;
/* we know the layer has query results or we wouldn't be in this code */
#if 0
status = msLayerOpen(layer); /* open the layer */
if(status != MS_SUCCESS) return status;
status = msLayerGetItems(layer); /* retrieve all the item names */
if(status != MS_SUCCESS) return status;
#endif
if(layer->numjoins > 0) { /* initialize necessary JOINs here */
for(j=0; j<layer->numjoins; j++) {
status = msJoinConnect(layer, &(layer->joins[j]));
if(status != MS_SUCCESS) {
msFreeHashTable(tagArgs);
msFree(postTag);
msFree(tag);
return status;
}
}
}
mapserv->LRN = 1; /* layer result counter */
mapserv->resultlayer = layer;
msInitShape(&(mapserv->resultshape));
if(limit == -1) /* return all */
limit = layer->resultcache->numresults;
else
limit = MS_MIN(limit, layer->resultcache->numresults);
for(i=0; i<limit; i++) {
status = msLayerGetShape(layer, &(mapserv->resultshape), &(layer->resultcache->results[i]));
if(status != MS_SUCCESS) {
msFreeHashTable(tagArgs);
msFree(postTag);
msFree(tag);
return status;
}
mapserv->resultshape.classindex = msShapeGetClass(layer, layer->map, &mapserv->resultshape, NULL, -1);
/* prepare any necessary JOINs here (one-to-one only) */
if(layer->numjoins > 0) {
for(j=0; j<layer->numjoins; j++) {
if(layer->joins[j].type == MS_JOIN_ONE_TO_ONE) {
msJoinPrepare(&(layer->joins[j]), &(mapserv->resultshape));
msJoinNext(&(layer->joins[j])); /* fetch the first row */
}
}
}
/*
** if necessary trim a few characters off the end of the tag
*/
if(trimLast && (i == limit-1)) {
char *ptr;
if((ptr = strrstr(tag, trimLast)) != NULL)
*ptr = '\0';
}
/* process the tag */
tagInstance = processLine(mapserv, tag, NULL, QUERY); /* do substitutions */
*line = msStringConcatenate(*line, tagInstance); /* grow the line */
free(tagInstance);
msFreeShape(&(mapserv->resultshape)); /* init too */
mapserv->RN++; /* increment counters */
mapserv->LRN++;
}
/* msLayerClose(layer); */
mapserv->resultlayer = NULL; /* necessary? */
*line = msStringConcatenate(*line, postTag);
/*
** clean up
*/
free(postTag);
free(tag);
msFreeHashTable(tagArgs);
return(MS_SUCCESS);
}
/*
** Function to process a [resultset ...] tag.
*/
static int processResultSetTag(mapservObj *mapserv, char **line, FILE *stream)
{
char lineBuffer[MS_BUFFER_LENGTH];
int foundTagEnd;
char *preTag, *postTag; /* text before and after the tag */
char *tag, *tagStart;
hashTableObj *tagArgs=NULL;
const char *layerName=NULL;
const char *nodata=NULL;
layerObj *lp;
if(!*line) {
msSetError(MS_WEBERR, "Invalid line pointer.", "processResultSetTag()");
return(MS_FAILURE);
}
tagStart = findTag(*line, "resultset");