1 /**********************************************************************
2  * $Id: php_mapscript.c 9765 2010-01-28 15:32:10Z aboudreault $
3  *
4  * Project:  MapServer
5  * Purpose:  PHP/MapScript extension for MapServer.  External interface
6  *           functions
7  * Author:   Daniel Morissette, DM Solutions Group (dmorissette@dmsolutions.ca)
8  *           Alan Boudreault, Mapgears
9  *
10  **********************************************************************
11  * Copyright (c) 2000-2010, Daniel Morissette, DM Solutions Group Inc.
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a
14  * copy of this software and associated documentation files (the "Software"),
15  * to deal in the Software without restriction, including without limitation
16  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  * and/or sell copies of the Software, and to permit persons to whom the
18  * Software is furnished to do so, subject to the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included in
21  * all copies of this Software or works derived from this Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
26  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29  * DEALINGS IN THE SOFTWARE.
30  **********************************************************************/
31 
32 #include "php_mapscript.h"
33 
34 zend_class_entry *mapscript_ce_scalebar;
35 #if PHP_VERSION_ID >= 70000
36 zend_object_handlers mapscript_scalebar_object_handlers;
37 #endif
38 
39 ZEND_BEGIN_ARG_INFO_EX(scalebar___get_args, 0, 0, 1)
40 ZEND_ARG_INFO(0, property)
ZEND_END_ARG_INFO()41 ZEND_END_ARG_INFO()
42 
43 ZEND_BEGIN_ARG_INFO_EX(scalebar___set_args, 0, 0, 2)
44 ZEND_ARG_INFO(0, property)
45 ZEND_ARG_INFO(0, value)
46 ZEND_END_ARG_INFO()
47 
48 ZEND_BEGIN_ARG_INFO_EX(scalebar_updateFromString_args, 0, 0, 1)
49 ZEND_ARG_INFO(0, snippet)
50 ZEND_END_ARG_INFO()
51 
52 ZEND_BEGIN_ARG_INFO_EX(scalebar_setImageColor_args, 0, 0, 3)
53 ZEND_ARG_INFO(0, red)
54 ZEND_ARG_INFO(0, green)
55 ZEND_ARG_INFO(0, blue)
56 ZEND_END_ARG_INFO()
57 
58 /* {{{ proto scalebar __construct()
59    scalebarObj CANNOT be instanciated, this will throw an exception on use */
60 PHP_METHOD(scalebarObj, __construct)
61 {
62   mapscript_throw_exception("scalebarObj cannot be constructed" TSRMLS_CC);
63 }
64 /* }}} */
65 
PHP_METHOD(scalebarObj,__get)66 PHP_METHOD(scalebarObj, __get)
67 {
68   char *property;
69   long property_len = 0;
70   zval *zobj = getThis();
71   php_scalebar_object *php_scalebar;
72 
73   PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
74   if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
75                             &property, &property_len) == FAILURE) {
76     PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
77     return;
78   }
79   PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
80 
81   php_scalebar = MAPSCRIPT_OBJ_P(php_scalebar_object, zobj);
82 
83   IF_GET_LONG("height", php_scalebar->scalebar->height)
84   else IF_GET_LONG("width", php_scalebar->scalebar->width)
85     else IF_GET_LONG("style", php_scalebar->scalebar->style)
86       else IF_GET_LONG("intervals", php_scalebar->scalebar->intervals)
87         else IF_GET_LONG("units", php_scalebar->scalebar->units)
88           else IF_GET_LONG("status", php_scalebar->scalebar->status)
89             else IF_GET_LONG("position", php_scalebar->scalebar->position)
90               else IF_GET_LONG("postlabelcache", php_scalebar->scalebar->postlabelcache)
91                 else IF_GET_LONG("align", php_scalebar->scalebar->align)
92                   else IF_GET_OBJECT("color", mapscript_ce_color, php_scalebar->color, &php_scalebar->scalebar->color)
93                     else IF_GET_OBJECT("backgroundcolor", mapscript_ce_color, php_scalebar->backgroundcolor, &php_scalebar->scalebar->backgroundcolor)
94                       else IF_GET_OBJECT("outlinecolor", mapscript_ce_color, php_scalebar->outlinecolor, &php_scalebar->scalebar->outlinecolor)
95                         else IF_GET_OBJECT("label", mapscript_ce_label, php_scalebar->label, &php_scalebar->scalebar->label)
96                           else IF_GET_OBJECT("imagecolor", mapscript_ce_color, php_scalebar->imagecolor, &php_scalebar->scalebar->imagecolor)
97                             else {
98                               mapscript_throw_exception("Property '%s' does not exist in this object." TSRMLS_CC, property);
99                             }
100 }
101 
PHP_METHOD(scalebarObj,__set)102 PHP_METHOD(scalebarObj, __set)
103 {
104   char *property;
105   long property_len = 0;
106   zval *value;
107   zval *zobj = getThis();
108   php_scalebar_object *php_scalebar;
109 
110   PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
111   if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz",
112                             &property, &property_len, &value) == FAILURE) {
113     PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
114     return;
115   }
116   PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
117 
118   php_scalebar = MAPSCRIPT_OBJ_P(php_scalebar_object, zobj);
119 
120   IF_SET_LONG("height", php_scalebar->scalebar->height, value)
121   else IF_SET_LONG("width", php_scalebar->scalebar->width, value)
122     else IF_SET_LONG("style", php_scalebar->scalebar->style, value)
123       else IF_SET_LONG("intervals", php_scalebar->scalebar->intervals, value)
124         else IF_SET_LONG("units", php_scalebar->scalebar->units, value)
125           else IF_SET_LONG("status", php_scalebar->scalebar->status, value)
126             else IF_SET_LONG("position", php_scalebar->scalebar->position, value)
127               else IF_SET_LONG("postlabelcache", php_scalebar->scalebar->postlabelcache, value)
128                 else IF_SET_LONG("align", php_scalebar->scalebar->align, value)
129                   else if ( (STRING_EQUAL("color", property)) ||
130                             (STRING_EQUAL("backgroundcolor", property)) ||
131                             (STRING_EQUAL("outlinecolor", property)) ||
132                             (STRING_EQUAL("label", property)) ||
133                             (STRING_EQUAL("imagecolor", property))) {
134                     mapscript_throw_exception("Property '%s' is an object and can only be modified through its accessors." TSRMLS_CC, property);
135                   } else {
136                     mapscript_throw_exception("Property '%s' does not exist in this object." TSRMLS_CC, property);
137                   }
138 }
139 
140 /* {{{ proto int scalebar.updateFromString(string snippet)
141    Update a scalebar from a string snippet.  Returns MS_SUCCESS/MS_FAILURE */
PHP_METHOD(scalebarObj,updateFromString)142 PHP_METHOD(scalebarObj, updateFromString)
143 {
144   char *snippet;
145   long snippet_len = 0;
146   zval *zobj = getThis();
147   php_scalebar_object *php_scalebar;
148   int status = MS_FAILURE;
149 
150   PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
151   if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
152                             &snippet, &snippet_len) == FAILURE) {
153     PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
154     return;
155   }
156   PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
157 
158   php_scalebar = MAPSCRIPT_OBJ_P(php_scalebar_object, zobj);
159 
160   status =  scalebarObj_updateFromString(php_scalebar->scalebar, snippet);
161 
162   if (status != MS_SUCCESS) {
163     mapscript_throw_mapserver_exception("" TSRMLS_CC);
164     return;
165   }
166 
167   RETURN_LONG(status);
168 }
169 /* }}} */
170 
171 /* {{{ proto string convertToString()
172    Convert the scalebar object to string. */
PHP_METHOD(scalebarObj,convertToString)173 PHP_METHOD(scalebarObj, convertToString)
174 {
175   zval *zobj = getThis();
176   php_scalebar_object *php_scalebar;
177   char *value = NULL;
178 
179   PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
180   if (zend_parse_parameters_none() == FAILURE) {
181     PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
182     return;
183   }
184   PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
185 
186   php_scalebar = MAPSCRIPT_OBJ_P(php_scalebar_object, zobj);
187 
188   value =  scalebarObj_convertToString(php_scalebar->scalebar);
189 
190   if (value == NULL)
191     MAPSCRIPT_RETURN_STRING("", 1);
192 
193   MAPSCRIPT_RETVAL_STRING(value, 1);
194   free(value);
195 }
196 /* }}} */
197 
198 /* {{{ proto int scalebar.setImageColor(int red, int green, int blue)
199    Set the imagecolor property of the scalebar. Returns -1 on error. */
PHP_METHOD(scalebarObj,setImageColor)200 PHP_METHOD(scalebarObj, setImageColor)
201 {
202   zval *zobj = getThis();
203   long red, green, blue;
204   php_scalebar_object *php_scalebar;
205 
206   PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
207   if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll",
208                             &red, &green, &blue) == FAILURE) {
209     PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
210     return;
211   }
212   PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
213 
214   php_scalebar = MAPSCRIPT_OBJ_P(php_scalebar_object, zobj);
215 
216   if (red < 0 || red > 255 || green < 0 || green > 255 || blue < 0 || blue > 255)
217     RETURN_LONG(MS_FAILURE);
218 
219   php_scalebar->scalebar->imagecolor.red = red;
220   php_scalebar->scalebar->imagecolor.green = green;
221   php_scalebar->scalebar->imagecolor.blue = blue;
222 
223   RETURN_LONG(MS_SUCCESS);
224 }
225 /* }}} */
226 
227 /* {{{ proto int scalebar.free()
228    Free the object */
PHP_METHOD(scalebarObj,free)229 PHP_METHOD(scalebarObj, free)
230 {
231   zval *zobj = getThis();
232   php_scalebar_object *php_scalebar;
233 
234   PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
235   if (zend_parse_parameters_none() == FAILURE) {
236     PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
237     return;
238   }
239   PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
240 
241   php_scalebar = MAPSCRIPT_OBJ_P(php_scalebar_object, zobj);
242 
243   MAPSCRIPT_DELREF(php_scalebar->color);
244   MAPSCRIPT_DELREF(php_scalebar->backgroundcolor);
245   MAPSCRIPT_DELREF(php_scalebar->outlinecolor);
246   MAPSCRIPT_DELREF(php_scalebar->imagecolor);
247   MAPSCRIPT_DELREF(php_scalebar->label);
248 }
249 /* }}} */
250 
251 zend_function_entry scalebar_functions[] = {
252   PHP_ME(scalebarObj, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
253   PHP_ME(scalebarObj, __get, scalebar___get_args, ZEND_ACC_PUBLIC)
254   PHP_ME(scalebarObj, __set, scalebar___set_args, ZEND_ACC_PUBLIC)
255   PHP_MALIAS(scalebarObj, set, __set, NULL, ZEND_ACC_PUBLIC)
256   PHP_ME(scalebarObj, updateFromString, scalebar_updateFromString_args, ZEND_ACC_PUBLIC)
257   PHP_ME(scalebarObj, convertToString, NULL, ZEND_ACC_PUBLIC)
258   PHP_ME(scalebarObj, setImageColor, scalebar_setImageColor_args, ZEND_ACC_PUBLIC)
259   PHP_ME(scalebarObj, free, NULL, ZEND_ACC_PUBLIC) {
260     NULL, NULL, NULL
261   }
262 };
263 
mapscript_create_scalebar(scalebarObj * scalebar,parent_object parent,zval * return_value TSRMLS_DC)264 void mapscript_create_scalebar(scalebarObj *scalebar, parent_object parent, zval *return_value TSRMLS_DC)
265 {
266   php_scalebar_object * php_scalebar;
267   object_init_ex(return_value, mapscript_ce_scalebar);
268   php_scalebar = MAPSCRIPT_OBJ_P(php_scalebar_object, return_value);
269   php_scalebar->scalebar = scalebar;
270 
271   php_scalebar->parent = parent;
272   MAPSCRIPT_ADDREF(parent.val);
273 }
274 
275 #if PHP_VERSION_ID >= 70000
276 /* PHP7 - Modification by Bjoern Boldt <mapscript@pixaweb.net> */
mapscript_scalebar_create_object(zend_class_entry * ce TSRMLS_DC)277 static zend_object *mapscript_scalebar_create_object(zend_class_entry *ce TSRMLS_DC)
278 {
279   php_scalebar_object *php_scalebar;
280 
281   php_scalebar = ecalloc(1, sizeof(*php_scalebar) + zend_object_properties_size(ce));
282 
283   zend_object_std_init(&php_scalebar->zobj, ce TSRMLS_CC);
284   object_properties_init(&php_scalebar->zobj, ce);
285 
286   php_scalebar->zobj.handlers = &mapscript_scalebar_object_handlers;
287 
288   MAPSCRIPT_INIT_PARENT(php_scalebar->parent);
289   ZVAL_UNDEF(&php_scalebar->color);
290   ZVAL_UNDEF(&php_scalebar->backgroundcolor);
291   ZVAL_UNDEF(&php_scalebar->outlinecolor);
292   ZVAL_UNDEF(&php_scalebar->imagecolor);
293   ZVAL_UNDEF(&php_scalebar->label);
294 
295   return &php_scalebar->zobj;
296 }
297 
mapscript_scalebar_free_object(zend_object * object)298 static void mapscript_scalebar_free_object(zend_object *object)
299 {
300   php_scalebar_object *php_scalebar;
301 
302   php_scalebar = (php_scalebar_object *)((char *)object - XtOffsetOf(php_scalebar_object, zobj));
303 
304   MAPSCRIPT_FREE_PARENT(php_scalebar->parent);
305   MAPSCRIPT_DELREF(php_scalebar->color);
306   MAPSCRIPT_DELREF(php_scalebar->backgroundcolor);
307   MAPSCRIPT_DELREF(php_scalebar->outlinecolor);
308   MAPSCRIPT_DELREF(php_scalebar->imagecolor);
309   MAPSCRIPT_DELREF(php_scalebar->label);
310 
311   /* We don't need to free the scalebarObj */
312 
313   zend_object_std_dtor(object);
314 }
315 
PHP_MINIT_FUNCTION(scalebar)316 PHP_MINIT_FUNCTION(scalebar)
317 {
318   zend_class_entry ce;
319 
320   INIT_CLASS_ENTRY(ce, "scalebarObj", scalebar_functions);
321   mapscript_ce_scalebar = zend_register_internal_class(&ce TSRMLS_CC);
322 
323   mapscript_ce_scalebar->create_object = mapscript_scalebar_create_object;
324   mapscript_ce_scalebar->ce_flags |= ZEND_ACC_FINAL;
325 
326   memcpy(&mapscript_scalebar_object_handlers, &mapscript_std_object_handlers, sizeof(mapscript_scalebar_object_handlers));
327   mapscript_scalebar_object_handlers.free_obj = mapscript_scalebar_free_object;
328   mapscript_scalebar_object_handlers.offset   = XtOffsetOf(php_scalebar_object, zobj);
329 
330   return SUCCESS;
331 }
332 #else
333 /* PHP5 */
mapscript_scalebar_object_destroy(void * object TSRMLS_DC)334 static void mapscript_scalebar_object_destroy(void *object TSRMLS_DC)
335 {
336   php_scalebar_object *php_scalebar = (php_scalebar_object *)object;
337 
338   MAPSCRIPT_FREE_OBJECT(php_scalebar);
339 
340   MAPSCRIPT_FREE_PARENT(php_scalebar->parent);
341   MAPSCRIPT_DELREF(php_scalebar->color);
342   MAPSCRIPT_DELREF(php_scalebar->backgroundcolor);
343   MAPSCRIPT_DELREF(php_scalebar->outlinecolor);
344   MAPSCRIPT_DELREF(php_scalebar->imagecolor);
345   MAPSCRIPT_DELREF(php_scalebar->label);
346 
347   /* We don't need to free the scalebarObj */
348 
349   efree(object);
350 }
351 
mapscript_scalebar_object_new(zend_class_entry * ce TSRMLS_DC)352 static zend_object_value mapscript_scalebar_object_new(zend_class_entry *ce TSRMLS_DC)
353 {
354   zend_object_value retval;
355   php_scalebar_object *php_scalebar;
356 
357   MAPSCRIPT_ALLOC_OBJECT(php_scalebar, php_scalebar_object);
358 
359   retval = mapscript_object_new(&php_scalebar->std, ce,
360                                 &mapscript_scalebar_object_destroy TSRMLS_CC);
361 
362   MAPSCRIPT_INIT_PARENT(php_scalebar->parent);
363   php_scalebar->color = NULL;
364   php_scalebar->backgroundcolor = NULL;
365   php_scalebar->outlinecolor = NULL;
366   php_scalebar->imagecolor = NULL;
367   php_scalebar->label = NULL;
368 
369   return retval;
370 }
371 
PHP_MINIT_FUNCTION(scalebar)372 PHP_MINIT_FUNCTION(scalebar)
373 {
374   zend_class_entry ce;
375 
376   MAPSCRIPT_REGISTER_CLASS("scalebarObj",
377                            scalebar_functions,
378                            mapscript_ce_scalebar,
379                            mapscript_scalebar_object_new);
380 
381   mapscript_ce_scalebar->ce_flags |= ZEND_ACC_FINAL_CLASS;
382 
383   return SUCCESS;
384 }
385 #endif
386