-
Notifications
You must be signed in to change notification settings - Fork 0
/
midgard.c
1471 lines (1171 loc) · 39.8 KB
/
midgard.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: midgard.c 27410 2014-09-01 07:39:28Z piotras $
Copyright (C) 1999 Jukka Zitting <jukka.zitting@iki.fi>
Copyright (C) 2000 The Midgard Project ry
Copyright (C) 2000 Emile Heyns, Aurora SA <emile@iris-advies.com>
Copyright (C) 2003 David Schmitter, Dataflow Solutions GmbH <schmitt@dataflow.ch>
Copyright (C) 2005 Piotr Pokora, <pp@infoglob.pl>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <sys/file.h>
#include "php_midgard.h"
#include "php_midgard_gobject.h"
/* You should tweak config.m4 so this symbol (or some else suitable)
gets defined.
*/
#if HAVE_MIDGARD
#include "mgd_article.h"
#include "mgd_topic.h"
#include "mgd_attachment.h"
#include "mgd_element.h"
#include "mgd_group.h"
#include "mgd_member.h"
#include "mgd_host.h"
#include "mgd_calendar.h"
#include "mgd_event.h"
#include "mgd_eventmember.h"
#include "mgd_page.h"
#include "mgd_pageelement.h"
#include "mgd_pagelink.h"
#include "mgd_person.h"
#include "mgd_sitegroup.h"
#include "mgd_snippet.h"
#include "mgd_snippetdir.h"
#include "mgd_language.h"
#include "mgd_quota.h"
#include "mgd_preparser.h"
#include "mgd_preparse.h"
#include "mgd_query_builder.h"
#include "mgd_php_reflection_property.h"
#include "mgd_style.h"
#include "zend_exceptions.h"
#include "zend_extensions.h"
#include <zend_ini.h>
#include <locale.h>
#include "SAPI.h"
/* True global resources - no need for thread safety here */
int le_midgard_list_fetch;
GHashTable * midgard_registered_types;
static MidgardSchema *midgard_global_schema = NULL;
static zend_class_entry *midgard_metadata_class;
static zend_class_entry *ce_midgard_error_exception;
zend_object_handlers php_midgard_gobject_handlers;
guint global_loghandler = 0;
/* End of true globals */
php_midgard_function_entry midgard_functions[] = {
PHP_FE(mgd_get_midgard, NULL)
PHP_FE(mgd_preparse, NULL)
PHP_FE(mgd_snippet, NULL)
PHP_FE(mgd_errstr, NULL)
PHP_FE(mgd_errno, NULL)
PHP_FE(mgd_set_errno, NULL)
PHP_FE(mgd_version, NULL)
PHP_FE(mgd_auth_midgard, NULL)
PHP_FE(mgd_unsetuid, NULL)
PHP_FE(mgd_issetuid, NULL)
PHP_FE(mgd_is_guid, NULL)
PHP_FE(mgd_format, NULL)
PHP_FE(mgd_set_lang, NULL)
PHP_FE(mgd_get_lang, NULL)
PHP_FE(mgd_set_default_lang, NULL)
PHP_FE(mgd_get_default_lang, NULL)
PHP_FE(mgd_debug_start, NULL)
PHP_FE(mgd_debug_stop, NULL)
PHP_FE(mgd_config_init, NULL)
PHP_FE(mgd_is_element_loaded, NULL)
PHP_FE(mgd_stat_attachment, NULL)
MGD_FE(cache_invalidate, NULL)
MGD_FE(get_sitegroup, NULL)
MGD_FE(template, NULL)
{NULL, NULL, NULL} /* Must be the last line in midgard_functions[] */
};
void php_midgard_log_errors(const gchar *domain, GLogLevelFlags level,
const gchar *msg, gpointer userdata)
{
MidgardConnection *mgd = (MidgardConnection*) userdata;
guint mlevel = 0;
if(mgd)
mlevel = midgard_connection_get_loglevel(mgd);
g_assert(msg != NULL);
switch (level) {
case G_LOG_LEVEL_ERROR:
php_error(E_ERROR,
"(pid:%ld): %s",
(unsigned long)getpid(), msg);
return;
break;
case G_LOG_LEVEL_CRITICAL:
php_error(E_WARNING,
"(pid:%ld): %s",
(unsigned long)getpid(), msg);
return;
break;
case G_LOG_LEVEL_WARNING:
php_error(E_WARNING,
"(pid:%ld): %s",
(unsigned long)getpid(), msg);
return;
break;
default:
break;
}
if (mlevel >= level && mgd != NULL) {
midgard_error_default_log(
domain, level, msg, MIDGARD_CONNECTION(mgd));
}
}
void php_glib_log_errors(const gchar *domain, GLogLevelFlags level,
const gchar *_msg, gpointer userdata)
{
if(_msg == NULL)
_msg = "";
zval **data;
gchar *ruri;
gchar *rhost;
TSRMLS_FETCH();
if(zend_hash_find(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]),
"REQUEST_URI", sizeof("REQUEST_URI"), (void **) &data) == FAILURE) {
ruri = "Empty request uri";
} else {
ruri = Z_STRVAL_PP(data);
}
zval **hdata;
if(zend_hash_find(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]),
"HTTP_HOST", sizeof("HTTP_HOST"), (void **) &hdata) == FAILURE) {
rhost = "Empty hostname";
} else {
rhost = Z_STRVAL_PP(hdata);
}
GString *mstr = g_string_new("");
g_string_append_printf(mstr, "%s (%s %s)", _msg, rhost, ruri);
gchar *msg = mstr->str;
switch (level) {
case G_LOG_FLAG_RECURSION:
/* Do nothing */
break;
case G_LOG_LEVEL_CRITICAL:
case G_LOG_FLAG_FATAL:
case G_LOG_LEVEL_ERROR:
php_error(E_WARNING,
"(pid:%ld): %s",
(unsigned long)getpid(), msg);
break;
case G_LOG_LEVEL_MESSAGE:
case G_LOG_LEVEL_WARNING:
php_error(E_WARNING,
"(pid:%ld): %s",
(unsigned long)getpid(), msg);
break;
case G_LOG_LEVEL_INFO:
php_error(E_NOTICE, "(pid:%ld): %s", (unsigned long)getpid(), msg);
break;
case G_LOG_LEVEL_DEBUG:
/* Do nothing */
break;
default:
break;
}
g_string_free(mstr, TRUE);
}
gboolean php_midgard_error_exception_throw(midgard *mgd)
{
MidgardConnection *_mgd = mgd->_mgd;
if(_mgd->errnum != MGD_ERR_OK) {
TSRMLS_FETCH();
zend_throw_exception_ex(ce_midgard_error_exception,
0 TSRMLS_CC, _mgd->errstr);
return TRUE;
}
return FALSE;
}
void php_midgard_error_exception_force_throw(midgard *mgd, gint errcode)
{
midgard_connection_set_error(mgd->_mgd, errcode);
php_midgard_error_exception_throw(mgd);
return;
}
ZEND_DECLARE_MODULE_GLOBALS(midgard)
zend_module_entry midgard_module_entry = {
STANDARD_MODULE_HEADER,
"midgard",
midgard_functions,
PHP_MINIT(midgard),
PHP_MSHUTDOWN(midgard),
PHP_RINIT(midgard), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(midgard), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(midgard),
MIDGARD_LIB_VERSION, /* extension version number (string) */
STANDARD_MODULE_PROPERTIES
};
ZEND_GET_MODULE(midgard)
ZEND_INI_BEGIN()
ZEND_INI_ENTRY("midgard.engine", "On", ZEND_INI_ALL, NULL)
ZEND_INI_END()
static gboolean php_midgard_engine_is_enabled()
{
gchar *engine = zend_ini_string("midgard.engine", sizeof("midgard.engine"), 0);
gchar *lengine = g_ascii_strdown(engine, strlen(engine));
gboolean enabled = FALSE;
if(g_str_equal(lengine, "on"))
enabled = TRUE;
g_free(lengine);
return enabled;
}
static void php_midgard_init_globals(zend_midgard_globals *midgard_globals)
{
midgard_globals->rcfg = NULL;
midgard_globals->dcfg = NULL;
midgard_globals->mgd = NULL;
midgard_globals->mgd_errno = MGD_ERR_OK;
}
static void _midgard_list_fetch_dtor(zend_rsrc_list_entry * rsrc TSRMLS_DC)
{
midgard_res * res = (midgard_res*)rsrc->ptr;
if(rsrc->type == IS_RESOURCE){
if (res != NULL) mgd_release(res);
}
}
static zend_bool create_global_zval(const char *name, uint name_len TSRMLS_DC)
{
zval *globals;
ALLOC_ZVAL(globals);
zend_hash_update(&EG(symbol_table), name, name_len + 1, &globals, sizeof(zval *), NULL);
return 0;
}
static zend_bool create_global_array(const char *name, uint name_len TSRMLS_DC)
{
zval *globals;
ALLOC_ZVAL(globals);
array_init(globals);
INIT_PZVAL(globals);
zend_hash_update(&EG(symbol_table), name, name_len + 1, &globals, sizeof(zval *), NULL);
return 0;
}
void php_midgard_register_auto_globals(void)
{
#if PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 3
zend_register_auto_global(MIDGARD_GLOBAL_MIDGARD, sizeof(MIDGARD_GLOBAL_MIDGARD)-1, 0, NULL TSRMLS_CC);
zend_register_auto_global(MIDGARD_GLOBAL_MIDCOM, sizeof(MIDGARD_GLOBAL_MIDCOM)-1, 0, NULL TSRMLS_CC);
zend_register_auto_global(MIDGARD_GLOBAL_MIDGARD_CONNECTION, sizeof(MIDGARD_GLOBAL_MIDGARD_CONNECTION)-1, 0, NULL TSRMLS_CC);
#else
zend_register_auto_global(MIDGARD_GLOBAL_MIDGARD, sizeof(MIDGARD_GLOBAL_MIDGARD)-1, NULL TSRMLS_CC);
zend_register_auto_global(MIDGARD_GLOBAL_MIDCOM, sizeof(MIDGARD_GLOBAL_MIDCOM)-1, NULL TSRMLS_CC);
zend_register_auto_global(MIDGARD_GLOBAL_MIDGARD_CONNECTION, sizeof(MIDGARD_GLOBAL_MIDGARD_CONNECTION)-1, NULL TSRMLS_CC);
#endif
return;
}
PHP_MINIT_FUNCTION(midgard)
{
/* Return success if there's module midgard already loaded.
* Internal classes won't be (de)registered, and dl() should be safe in usage */
zend_extension *ze = zend_get_extension("midgard");
if(ze != NULL) {
php_error(E_NOTICE, "Module midgard (1.9) already loaded");
return SUCCESS;
}
global_loghandler =
g_log_set_handler("midgard-core", G_LOG_LEVEL_MASK,
midgard_error_default_log, NULL);
/* Set loghandler for GLib and GLib-GObject */
g_log_set_handler("GLib-GObject", G_LOG_LEVEL_MASK, php_glib_log_errors, NULL);
g_log_set_handler("GLib", G_LOG_LEVEL_MASK, php_glib_log_errors, NULL);
MidgardClassPtr *midgard_class;
ZEND_INIT_MODULE_GLOBALS(midgard, php_midgard_init_globals, NULL);
php_midgard_register_auto_globals();
/* EEH: I have 0 clue as to what these params mean, and the PHP
* docs on these are non-existant
*/
le_midgard_list_fetch =
zend_register_list_destructors_ex(_midgard_list_fetch_dtor, NULL,
"midgard list fetch", module_number);
for (midgard_class = MidgardClasses; midgard_class &&
*midgard_class; midgard_class++) {
if(*midgard_class && (*midgard_class)->name) {
MGD_INIT_OVERLOADED_CLASS_ENTRY(
(*midgard_class)->class_entry,
(*midgard_class)->name,
(*midgard_class)->methods,
NULL,
NULL,
NULL);
(*midgard_class)->entry_ptr =
zend_register_internal_class(
&((*midgard_class)->class_entry) TSRMLS_CC);
assert((*midgard_class)->entry_ptr);
}
}
midgard_init();
/* Initialize handlers */
memcpy(&php_midgard_gobject_handlers,
zend_get_std_object_handlers(), sizeof(zend_object_handlers));
/* Custom handlers hooks */
php_midgard_gobject_handlers.clone_obj = NULL;
php_midgard_gobject_handlers.read_property = php_midgard_gobject_read_property;
php_midgard_gobject_handlers.write_property = php_midgard_gobject_write_property;
php_midgard_gobject_handlers.has_property = php_midgard_gobject_has_property;
php_midgard_gobject_handlers.get_properties = php_midgard_zendobject_get_properties;
/* php_midgard_gobject_handlers.unset_property = php_midgard_gobject_unset_property; */
/* register Gtype types from schemas */
midgard_global_schema = g_object_new(MIDGARD_TYPE_SCHEMA, NULL);
midgard_schema_init((MidgardSchema *) midgard_global_schema);
midgard_schema_read_dir((MidgardSchema *) midgard_global_schema, MIDGARD_LSCHEMA_DIR);
/* Register midgard_error_exception class */
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "midgard_error_exception", NULL);
ce_midgard_error_exception = zend_register_internal_class_ex(&ce,
zend_exception_get_default(TSRMLS_C), NULL TSRMLS_CC);
php_midgard_query_builder_init(module_number);
php_midgard_reflection_property_init(module_number);
php_midgard_collector_init(module_number);
php_midgard_replicator_init(module_number);
php_midgard_object_class_init(module_number);
php_midgard_blob_init(module_number);
php_midgard_connection_init(module_number);
php_midgard_config_init(module_number);
php_midgard_object_init(module_number);
php_midgard_user_init(module_number);
php_midgard_dbus_init(module_number);
php_midgard_sitegroup_init(module_number);
/* Register midgard_metadata class */
static zend_class_entry midgard_metadata_class_entry;
INIT_CLASS_ENTRY(midgard_metadata_class_entry, "midgard_metadata", NULL);
midgard_metadata_class = zend_register_internal_class(&midgard_metadata_class_entry TSRMLS_CC);
midgard_metadata_class->create_object = php_midgard_gobject_new;
/* Register auth type constants */
REGISTER_LONG_CONSTANT("MIDGARD_USER_HASH_LEGACY",
MIDGARD_USER_HASH_LEGACY, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MIDGARD_USER_HASH_LEGACY_PLAIN",
MIDGARD_USER_HASH_LEGACY_PLAIN, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MIDGARD_USER_HASH_CRYPT",
MIDGARD_USER_HASH_CRYPT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MIDGARD_USER_HASH_MD5",
MIDGARD_USER_HASH_MD5, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MIDGARD_USER_HASH_PLAIN",
MIDGARD_USER_HASH_PLAIN, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MIDGARD_USER_HASH_SHA1",
MIDGARD_USER_HASH_SHA1, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MIDGARD_USER_HASH_PAM",
MIDGARD_USER_HASH_PAM, CONST_CS | CONST_PERSISTENT);
/* Register properties' midgard types */
REGISTER_LONG_CONSTANT("MGD_TYPE_NONE", MGD_TYPE_NONE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_TYPE_STRING", MGD_TYPE_STRING, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_TYPE_INT", MGD_TYPE_INT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_TYPE_UINT", MGD_TYPE_UINT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_TYPE_FLOAT", MGD_TYPE_FLOAT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_TYPE_BOOLEAN", MGD_TYPE_BOOLEAN, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_TYPE_TIMESTAMP", MGD_TYPE_TIMESTAMP, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_TYPE_LONGTEXT", MGD_TYPE_LONGTEXT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_TYPE_GUID", MGD_TYPE_GUID, CONST_CS | CONST_PERSISTENT);
/* Register errcode constants */
REGISTER_LONG_CONSTANT("MGD_ERR_OK", MGD_ERR_OK, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_ERR_ERROR", MGD_ERR_ERROR, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_ERR_ACCESS_DENIED", MGD_ERR_ACCESS_DENIED, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_ERR_SITEGROUP_VIOLATION", MGD_ERR_SITEGROUP_VIOLATION, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_ERR_NOT_OBJECT", MGD_ERR_NOT_OBJECT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_ERR_NOT_EXISTS", MGD_ERR_NOT_EXISTS, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_ERR_INVALID_PROPERTY", MGD_ERR_INVALID_PROPERTY, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_ERR_INVALID_NAME", MGD_ERR_INVALID_NAME, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_ERR_DUPLICATE", MGD_ERR_DUPLICATE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_ERR_HAS_DEPENDANTS", MGD_ERR_HAS_DEPENDANTS, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_ERR_RANGE", MGD_ERR_RANGE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_ERR_NOT_CONNECTED", MGD_ERR_NOT_CONNECTED, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_ERR_SG_NOTFOUND", MGD_ERR_SG_NOTFOUND, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_ERR_INVALID_OBJECT", MGD_ERR_INVALID_OBJECT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_ERR_QUOTA", MGD_ERR_QUOTA, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_ERR_INTERNAL", MGD_ERR_INTERNAL, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_ERR_OBJECT_NAME_EXISTS", MGD_ERR_OBJECT_NAME_EXISTS, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_ERR_OBJECT_NO_STORAGE", MGD_ERR_OBJECT_NO_STORAGE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_ERR_OBJECT_NO_PARENT", MGD_ERR_OBJECT_NO_PARENT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_ERR_INVALID_PROPERTY_VALUE", MGD_ERR_INVALID_PROPERTY_VALUE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_ERR_USER_DATA", MGD_ERR_USER_DATA, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_ERR_OBJECT_DELETED", MGD_ERR_OBJECT_DELETED, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_ERR_OBJECT_PURGED", MGD_ERR_OBJECT_PURGED, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_ERR_OBJECT_EXPORTED", MGD_ERR_OBJECT_EXPORTED, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_ERR_OBJECT_IMPORTED", MGD_ERR_OBJECT_IMPORTED, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_ERR_MISSED_DEPENDENCE", MGD_ERR_MISSED_DEPENDENCE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_ERR_TREE_IS_CIRCULAR", MGD_ERR_TREE_IS_CIRCULAR, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MGD_ERR_OBJECT_IS_LOCKED", MGD_ERR_OBJECT_IS_LOCKED, CONST_CS | CONST_PERSISTENT);
REGISTER_INI_ENTRIES();
g_log_remove_handler("midgard-core", global_loghandler);
global_loghandler = 0;
g_log_set_always_fatal(G_LOG_LEVEL_ERROR);
/* g_log_set_fatal_mask("GLib-GObject", G_LOG_LEVEL_WARNING);
g_log_set_fatal_mask("GLib", G_LOG_LEVEL_WARNING); */
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(midgard)
{
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
static gboolean __rcfg_is_valid(midgard_request_config *rcfg)
{
if(rcfg == NULL)
return FALSE;
if(rcfg->uri == NULL) {
g_log("GLib", G_LOG_LEVEL_WARNING, "NULL uri for given rcfg");
return FALSE;
}
return TRUE;
}
PHP_RINIT_FUNCTION(midgard)
{
/* g_log_set_always_fatal(G_LOG_LEVEL_WARNING); */
global_loghandler = 0;
MGDG(mgd) = NULL;
MGDG(rcfg) = NULL;
MGDG(dcfg) = NULL;
//if(!php_midgard_engine_is_enabled())
// return SUCCESS;
if (SG(server_context)) {
MGDG(rcfg) = mgd_php_bug_workaround_get_rcfg();
MGDG(dcfg) = mgd_php_bug_workaround_get_dcfg();
if(!__rcfg_is_valid(MGDG(rcfg)))
return SUCCESS;
}
if (MGDG(rcfg)) {
MGDG(mgd) = MGDG(rcfg)->mgd;
} else {
MGDG(mgd) = NULL;
}
if(SG(server_context) && MGDG(mgd) == NULL)
{
return SUCCESS;
}
/* MGDG(dcfg) is Apache directory config , so we initialize classes only when
* php is loaded as Apache module. For command line PHP we need to call another
* function which initialize all internal midgard data.
* Glib types are already initialized at this moment. */
if (MGDG(dcfg) && MGDG(mgd)) {
MGDG(mgd)->loglevel = MGDG(dcfg)->loglevel;
MGDG(mgd)->logfile = g_strdup(MGDG(dcfg)->logfile);
}
if (MGDG(rcfg))
_make_midgard_global();
if(MGDG(mgd) != NULL) {
/* Initialize _MIDGARD_CONNECTION singleton */
zval *_mcg = NULL;
MAKE_STD_ZVAL(_mcg);
php_midgard_gobject_init(_mcg, "midgard_connection",
G_OBJECT(mgd_handle()->_mgd), FALSE);
/* PHP object constructor hack.
* I have no idea how to "trigger" php constructor here.
* And I didn't try ( yet ) call_user_function */
zend_hash_add(&EG(symbol_table), "_MIDGARD_CONNECTION",
sizeof("_MIDGARD_CONNECTION"),
&_mcg, sizeof(zval **), NULL);
/* Set MidgardConnection pointer */
mgd_handle_singleton_set(mgd_handle()->_mgd);
/* Connect to default callbacks, required for $_MIDGARD superglobal */
php_midgard_connection_connect_callbacks(mgd_handle()->_mgd);
global_loghandler =
g_log_set_handler("midgard-core",
G_LOG_LEVEL_MASK,
php_midgard_log_errors,
(gpointer)mgd_handle_singleton_get());
midgard_connection_set_loghandler(
mgd_handle_singleton_get(), global_loghandler);
}
if (MGDG(mgd) == NULL)
{
global_loghandler =
g_log_set_handler("midgard-core", G_LOG_LEVEL_MASK,
midgard_error_default_log, NULL);
}
/* Initialize closure hash */
php_midgard_gobject_closure_hash_new();
return SUCCESS;
}
/* Remove if there's nothing to do at request end */
PHP_RSHUTDOWN_FUNCTION(midgard)
{
if(!php_midgard_engine_is_enabled())
return SUCCESS;
if(SG(server_context) && MGDG(mgd) == NULL)
{
return SUCCESS;
}
midgard *mgd = mgd_handle();
/* Remove midgard loghandler. This might be valid in web environment,
* and might be not valid in cli. Midgard connection object might be
* already unrefed here. */
if(global_loghandler) {
if (mgd && (mgd->_mgd && MIDGARD_IS_CONNECTION (mgd->_mgd))) {
if (midgard_connection_get_loghandler (mgd->_mgd) == global_loghandler)
g_log_remove_handler("midgard-core", global_loghandler);
}
}
/* FIXME, at least MidgardTypeHolder should be passed here instead of NULL */
global_loghandler =
g_log_set_handler("midgard-core",
G_LOG_LEVEL_MASK,
php_midgard_log_errors, NULL);
g_debug("MIDGARD REQUEST END");
midgard_request_config *rcfg = mgd_rcfg();
/* Disconnect all callbacks registered for connection */
if(mgd && mgd->_mgd)
php_midgard_connection_disconnect_callbacks(mgd->_mgd);
if(mgd != NULL){
mgd_internal_set_lang(mgd_handle(), 0);
}
/* Destroy rcfg->elements hash table */
if (rcfg && mgd != NULL) {
/* Glib will destroy hash keys and values.
* * Table initilized with midgard_hash_strings_hash */
if(rcfg->elements != NULL)
g_hash_table_destroy (rcfg->elements);
}
if (mgd_rcfg() == NULL && mgd_handle() != NULL) {
mgd_close(mgd_handle());
/* EEH: not safe! mgd_done(); */
}
/* Free all closures
* We can not keep them persistant as all data changes per request. */
php_midgard_gobject_closure_hash_free();
g_debug("MIDGARD REQUEST END - SUCCESS");
if(global_loghandler)
g_log_remove_handler("midgard-core", global_loghandler);
/* Enable it for debugging purpose */
/*zend_module_entry *module;
int rv = zend_hash_find(&module_registry, "midgard", strlen("midgard")+1, (void**)&module);
if(rv == SUCCESS)
module->handle = 0; */
return SUCCESS;
}
PHP_MINFO_FUNCTION(midgard)
{
int i = 0;
php_info_print_table_start();
php_info_print_table_header(2, "Midgard Support", "enabled");
php_info_print_table_row(2, "Midgard version", mgd_version());
/* TODO: pretify output by arranging functions according object classes */
while (midgard_module_entry.functions[i].fname) {
php_info_print_table_row(2, "", midgard_module_entry.functions[i].fname);
i++;
}
php_info_print_table_end();
i = 0;
php_info_print_table_start();
php_info_print_table_header(2, "MgdSchema technology support", "enabled");
php_info_print_table_row(2, "Midgard version", mgd_version());
/* FIXME
while (__midgard_php_type_functions[i].fname) {
php_info_print_table_row(2, "", __midgard_php_type_functions[i].fname);
i++;
}
*/
php_info_print_table_end();
php_info_print_box_start(0);
PUTS("<h3><a href=\"http://www.midgard-project.org/\">");
PUTS("The Midgard Project</a></h3>\n");
php_printf("This program makes use of the Midgard Content Management engine:<br />");
php_printf("© 1998-2001 The Midgard Project Ry - 2000-2001 Aurora Linux<br />\n");
php_printf("© 2002-2005 The Midgard Community<br />\n");
php_info_print_box_end();
/* Remove comments if you have entries in php.ini
DISPLAY_INI_ENTRIES();
*/
}
PHP_FUNCTION(mgd_config_init)
{
gchar *conf;
gint conf_length;
MidgardConnection *mgd = mgd_handle_singleton_get();
RETVAL_FALSE;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
&conf, &conf_length) == FAILURE)
return;
/* Check if connection is established */
if(mgd != NULL) {
php_error(E_NOTICE,
"mgd_config_init is deprecated. Use midgard_connection class");
RETURN_TRUE;
}
/* TODO , open connection here if really needed for some backward compatibility */
}
PHP_FUNCTION(mgd_errno)
{
RETVAL_FALSE;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") != SUCCESS) {
WRONG_PARAM_COUNT;
}
RETVAL_TRUE;
midgard *mgd = mgd_handle();
if(!mgd) {
php_error(E_WARNING, "midgard_connection not established");
return;
}
RETURN_LONG(mgd->_mgd->errnum);
}
PHP_FUNCTION(mgd_errstr)
{
RETVAL_FALSE;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") != SUCCESS) {
WRONG_PARAM_COUNT;
}
RETVAL_TRUE;
midgard *mgd = mgd_handle();
if(!mgd) {
php_error(E_WARNING, "midgard_connection not established");
return;
}
RETURN_STRING(mgd->_mgd->errstr, 1);
}
PHP_FUNCTION(mgd_version)
{
RETURN_STRING((char*)mgd_version(), 1);
}
/* Fetch static globals. Unfortunately these need to be here since the
module globals are declared static by the Zend macros
*/
midgard_request_config *mgd_rcfg()
{
TSRMLS_FETCH();
return MGDG(rcfg);
}
midgard_directory_config *mgd_dcfg()
{
TSRMLS_FETCH();
return MGDG(dcfg);
}
midgard *mgd_handle()
{
TSRMLS_FETCH();
return MGDG(mgd);
}
void mgd_handle_set(midgard *mgd)
{
g_assert(mgd != NULL);
TSRMLS_FETCH();
MGDG(mgd) = mgd;
}
void mgd_handle_singleton_set(MidgardConnection *mgd)
{
g_assert(mgd != NULL);
TSRMLS_FETCH();
MGDG(midgard_singleton) = mgd;
}
MidgardConnection *mgd_handle_singleton_get()
{
TSRMLS_FETCH();
return MGDG(midgard_singleton);
}
void mgd_set_errno(MgdErrorGeneric mgd_errno)
{
MIDGARD_ERRNO_SET(mgd_handle(), mgd_errno);
}
void mgd_reset_errno(void)
{
MIDGARD_ERRNO_SET(mgd_handle(), MGD_ERR_OK);
}
int mgd_get_errno()
{
return mgd_handle()->_mgd->errnum;
}
int is_table_multilang(const char *table) {
if (!strcmp(table, "page") || !strcmp(table, "article") || !strcmp(table, "element") || !strcmp(table, "pageelement") || !strcmp(table, "snippet")) {
return TRUE;
} else {
return FALSE;
}
}
PHP_FUNCTION(mgd_get_midgard)
{
int i;
char *ah_prefix;
zval *argv, *types, *confv;
char **mm_argv;
long mm_argc;
midgard *mgd = mgd_handle();
midgard_request_config *rcfg = mgd_rcfg();
CHECK_MGD;
if (rcfg == NULL) {
RETURN_FALSE_BECAUSE(MGD_ERR_NOT_CONNECTED);
}
object_init(return_value);
add_property_long(return_value, "host", rcfg->host);
add_property_long(return_value, "style", rcfg->style);
add_property_long(return_value, "page", rcfg->resource.id);
add_property_long(return_value, "cookieauth", rcfg->auth.cookie);
add_property_long(return_value, "auth", rcfg->auth.required);
add_property_long(return_value, "author", rcfg->author);
add_property_long(return_value, "user", mgd_user(mgd));
add_property_long(return_value, "admin", mgd_isadmin(mgd));
#if HAVE_MIDGARD_QUOTA
add_property_long(return_value, "quota", mgd_get_quota(mgd));
#endif /* HAVE_MIDGARD_QUOTA */
#if HAVE_MIDGARD_MULTILANG
add_property_long(return_value, "lang", mgd_lang(mgd));
#endif /* HAVE_MIDGARD_MULTILANG */
add_property_long(return_value, "root", mgd_isroot(mgd));
add_property_long(return_value, "sitegroup", mgd_sitegroup(mgd));
/* PP: Add midgard-config values ( prefix, multilang, quota) */
MAKE_STD_ZVAL(confv);
array_init(confv);
add_assoc_string(confv, "prefix", MIDGARD_LIB_PREFIX, 1);
add_assoc_long(confv, "multilang", HAVE_MIDGARD_MULTILANG);
add_assoc_long(confv, "quota", HAVE_MIDGARD_QUOTA);
add_property_zval(return_value, "config" , confv);
mm_argv = (char**)rcfg->elts;
mm_argc = rcfg->nelts;
add_property_long(return_value, "argc", mm_argc);
MAKE_STD_ZVAL(argv);
array_init(argv);
for (i = 0; i < mm_argc; i++) {
if (mm_argv[i]) {
add_index_string(argv, i, mm_argv[i], 1);
}
}
add_property_zval(return_value, "argv", argv);
add_property_string(return_value, "uri", rcfg->uri, 1);
add_property_string(return_value, "ah_prefix", ((ah_prefix = mgd_get_ah_prefix(mgd)) == NULL)?"":ah_prefix, 1);
add_property_stringl(return_value, "prefix", rcfg->uri, rcfg->prelen, 1);
add_property_stringl(return_value, "self", rcfg->uri, rcfg->self_len, 1);
MAKE_STD_ZVAL(types);
array_init(types);
for (i = 1; i < MIDGARD_OBJECT_COUNT; i++) {
add_assoc_long(types, (char *)mgd_table_extname[i], i);
}
add_property_zval(return_value, "types", types);
}
PHP_FUNCTION(mgd_auth_midgard)
{
gchar *username, *password;
guint namel, passl;
midgard *mgd = mgd_handle();
midgard_request_config *rcfg = mgd_rcfg();
guint oldcookie;
CHECK_MGD;
/* Third parameter oldcookie is used only for backward compatibility
* It does nothing now */
switch(ZEND_NUM_ARGS()){
case 2:
if (zend_parse_parameters(2 TSRMLS_CC, "ss",
&username, &namel,
&password, &passl) == FAILURE) {
WRONG_PARAM_COUNT;
RETURN_FALSE;
}
break;
case 3:
if (zend_parse_parameters(3 TSRMLS_CC, "ssl",
&username, &namel,
&password, &passl,
&oldcookie) == FAILURE) {
WRONG_PARAM_COUNT;
RETURN_FALSE;
}
break;
default:
WRONG_PARAM_COUNT;
RETURN_FALSE;
}
zval **hash, **vkey; /* Used in _MIDGARD_UPDATE_LONG */
int rv = mgd_auth(mgd, username, password, 1);
if (rcfg){
_MIDGARD_UPDATE_LONG("auth", rcfg->auth.required);
}
_MIDGARD_UPDATE_LONG("user", mgd_user(mgd));
_MIDGARD_UPDATE_LONG("admin", mgd_isadmin(mgd));
_MIDGARD_UPDATE_LONG("root", mgd_isroot(mgd));
switch (rv) {
case MGD_AUTH_NOT_CONNECTED:
RETURN_FALSE_BECAUSE(MGD_ERR_NOT_CONNECTED);
case MGD_AUTH_INVALID_NAME:
RETURN_FALSE_BECAUSE(MGD_ERR_INVALID_NAME);
case MGD_AUTH_SG_NOTFOUND:
RETURN_FALSE_BECAUSE(MGD_ERR_SG_NOTFOUND);
case MGD_AUTH_DUPLICATE:
RETURN_FALSE_BECAUSE(MGD_ERR_DUPLICATE);
case MGD_AUTH_NOTFOUND:
RETURN_FALSE_BECAUSE(MGD_ERR_NOT_EXISTS);
case MGD_AUTH_REAUTH:
case MGD_AUTH_INVALID_PWD:
RETURN_FALSE_BECAUSE(MGD_ERR_ACCESS_DENIED);
default:
if (rv >= 0) {
RETVAL_LONG(rv);
} else {
RETURN_FALSE_BECAUSE(MGD_ERR_INTERNAL);
}
}
}
MGD_FUNCTION(ret_type, connect, (type param))
{
char *hostname, *database, *username, *password;
int hostname_length, database_length, username_length, password_length;
if (MGDG(mgd) != NULL) RETURN_TRUE;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssss",
&hostname, &hostname_length, &database, &database_length,