removed debian/README.Debian
[davical.git] / inc / always.php.in
blobc7e90312ff083aeb88a95d917d9163f754fb70ec
1 <?php
2 /**
3 * @package davical
4 * @author Andrew McMillan <andrew@mcmillan.net.nz>
5 * @copyright Catalyst .Net Ltd, Morphoss Ltd <http://www.morphoss.com/>
6 * @license http://gnu.org/copyleft/gpl.html GNU GPL v2 or later
7 */
9 if ( preg_match('{/always.php$}', $_SERVER['SCRIPT_NAME'] ) ) header('Location: index.php');
11 // Ensure the configuration starts out as an empty object.
12 $c = (object) array();
13 $c->script_start_time = microtime(true);
15 // Ditto for a few other global things
16 unset($session); unset($request); unset($dbconn); unset($_awl_dbconn); unset($include_properties);
18 // An ultra-simple exception handler to catch errors that occur
19 // before we get a more functional exception handler in place...
20 function early_exception_handler($e) {
21 if ( !headers_sent() ) {
22 header("Content-type: text/plain");
23 header( sprintf("HTTP/1.1 %d %s", 500, getStatusMessage(500)) );
25 else {
26 echo "<pre>\n";
27 // Too late to set resultcode :-(
29 try {
30 @ob_flush(); // Seems like it should be better to do the following but is problematic on PHP5.3 at least: while ( ob_get_level() > 0 ) ob_end_flush();
32 catch( Exception $ignored ) {}
33 echo "Exception [".$e->getCode()."] ".$e->getmessage()."\n";
34 echo "At line ", $e->getLine(), " of ", $e->getFile(), "\n";
35 echo "================= Stack Trace ===================\n";
37 $trace = array_reverse($e->getTrace());
38 foreach( $trace AS $k => $v ) {
39 printf( "%s[%d] %s%s%s()\n", $v['file'], $v['line'], (isset($v['class'])?$v['class']:''), (isset($v['type'])?$v['type']:''), (isset($v['function'])?$v['function']:'') );
43 set_exception_handler('early_exception_handler');
45 $c->default_timezone = ini_get('date.timezone');
46 if ( empty($c->default_timezone) ) {
47 $c->default_timezone = 'UTC';
48 date_default_timezone_set('UTC');
51 // Default some of the configurable values
52 $c->sysabbr = 'davical';
53 $c->admin_email = 'admin@davical.example.com';
54 $c->system_name = 'DAViCal CalDAV Server';
55 $c->domain_name = (isset($_SERVER['SERVER_NAME'])?$_SERVER['SERVER_NAME']:$_SERVER['SERVER_ADDR']);
56 $c->save_time_zone_defs = true;
57 $c->collections_always_exist = false;
58 $c->allow_get_email_visibility = false;
59 $c->permission_scan_depth = 2;
60 $c->expand_pdo_parameters = true;
61 $c->home_calendar_name = 'calendar';
62 $c->home_addressbook_name = 'addresses';
63 $c->enable_row_linking = true;
64 $c->enable_scheduling = false;
65 $c->http_auth_mode = 'Basic';
66 // $c->default_locale = array('es_MX', 'es_AR', 'es', 'pt'); // An array of locales to try, or just a single locale
67 // $c->local_tzid = 'Pacific/Auckland'; // Perhaps we should read from /etc/timezone - I wonder how standard that is?
68 $c->default_locale = 'en';
69 $c->locale_path = '../locale';
70 $c->base_url = preg_replace('#/[^/]+\.php.*$#', '', $_SERVER['SCRIPT_NAME']);
71 $c->base_directory = preg_replace('#/[^/]*$#', '', $_SERVER['DOCUMENT_ROOT']);
72 $c->default_privileges = array('read-free-busy', 'schedule-deliver');
74 $c->enable_auto_schedule = true;
76 $c->schema_major = $c->schema_minor = $c->schema_patch = 0;
78 $c->stylesheets = array( $c->base_url.'/davical.css' );
79 $c->images = $c->base_url . '/images';
81 // Add a default for newly created users
82 $c->template_usr = array( 'active' => true,
83 'locale' => 'en_GB',
84 'date_format_type' => 'E',
85 'email_ok' => date('Y-m-d')
88 $c->hide_TODO = true; // VTODO only visible to collection owner
89 $c->readonly_webdav_collections = true; // WebDAV access is readonly
91 // Kind of private configuration values
92 $c->total_query_time = 0;
94 $c->dbg = array();
97 // Utilities
98 if ( isset($skip_errors) ) $skip_errors = true;
99 if ( ! @include_once('AWLUtilities.php') ) {
100 if ( isset($skip_errors) ) $skip_errors = false;
101 $try_paths = array(
102 '../../awl/inc'
103 , '/usr/share/awl/inc' // Where it ends up on Debian
104 , '/usr/share/php/awl/inc' // Fedora's standard for PHP libraries
105 , '/usr/local/share/awl/inc'
107 foreach( $try_paths AS $awl_include_path ) {
108 if ( @file_exists($awl_include_path.'/AWLUtilities.php') ) {
109 set_include_path( $awl_include_path. PATH_SEPARATOR. get_include_path());
110 break;
113 if ( ! @include_once('AWLUtilities.php') ) {
114 echo "Could not find the AWL libraries. Are they installed? Check your include_path in php.ini!\n";
115 @ob_flush(); exit(0);
118 if ( isset($skip_errors) ) $skip_errors = false;
120 // Ensure that ../inc is in our included paths as early as possible
121 set_include_path( '../inc'. PATH_SEPARATOR. get_include_path());
124 /** We actually discovered this and worked around it earlier, but we can't log it until the utilties are loaded */
125 if ( !isset($_SERVER['SERVER_NAME']) ) {
126 @dbg_error_log( 'WARN', "Your webserver is not setting the SERVER_NAME parameter. You may need to set \$c->domain_name in your configuration. Using IP address meanhwhile..." );
130 * Calculate the simplest form of reference to this page, excluding the PATH_INFO following the script name.
132 $c->protocol_server_port = sprintf( '%s://%s%s',
133 (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on'? 'https' : 'http'),
134 $_SERVER['SERVER_NAME'],
136 ( (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on')
137 && (!isset($_SERVER['SERVER_PORT']) || $_SERVER['SERVER_PORT'] == 80) )
138 || (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on'
139 && (!isset($_SERVER['SERVER_PORT']) || $_SERVER['SERVER_PORT'] == 443) )
140 ? ''
141 : ':'.$_SERVER['SERVER_PORT']
142 ) );
143 $c->protocol_server_port_script = $c->protocol_server_port . ($_SERVER['SCRIPT_NAME'] == '/index.php' ? '' : $_SERVER['SCRIPT_NAME']);
147 * We use @file_exists because things like open_basedir might noisily deny
148 * access which could break DAViCal completely by causing output to start
149 * too early.
151 ob_start( );
152 if ( @file_exists('/etc/davical/'.$_SERVER['SERVER_NAME'].'-conf.php') ) {
153 include('/etc/davical/'.$_SERVER['SERVER_NAME'].'-conf.php');
155 else if ( @file_exists('/etc/davical/config.php') ) {
156 include('/etc/davical/config.php');
158 else if ( @file_exists('/usr/local/etc/davical/'.$_SERVER['SERVER_NAME'].'-conf.php') ) {
159 include('/usr/local/etc/davical/'.$_SERVER['SERVER_NAME'].'-conf.php');
161 else if ( @file_exists('/usr/local/etc/davical/config.php') ) {
162 include('/usr/local/etc/davical/config.php');
164 else if ( @file_exists('../config/config.php') ) {
165 include('../config/config.php');
167 else if ( @file_exists('config/config.php') ) {
168 include('config/config.php');
170 else {
171 include('davical_configuration_missing.php');
172 @ob_flush(); exit(0);
174 $config_warnings = trim(ob_get_contents());
175 ob_end_clean();
177 if ( !isset($c->page_title) ) $c->page_title = $c->system_name;
179 if ( isset($_SERVER['HTTP_X_DAVICAL_TESTCASE']) ) {
180 @dbg_error_log( 'LOG', '==========> Test case =%s=', $_SERVER['HTTP_X_DAVICAL_TESTCASE'] );
182 else if ( isset($c->dbg['script_start']) && $c->dbg['script_start'] ) {
183 // Only log this if more than a little debugging of some sort is turned on, somewhere
184 @dbg_error_log( 'LOG', '==========> method =%s= =%s= =%s= =%s= =%s=',
185 $_SERVER['REQUEST_METHOD'], $c->protocol_server_port_script, (isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '$_SERVER[PATH_INFO] undefined'), $c->base_url, $c->base_directory );
189 * Now that we have loaded the configuration file we can switch to a
190 * default site locale. This may be overridden by each user.
192 putenv("LANG=". $c->default_locale);
193 if ( function_exists('awl_set_locale') ) {
194 awl_set_locale($c->default_locale);
195 init_gettext( 'davical', $c->locale_path );
199 * Work out our version
202 $c->code_version = 0;
203 $c->want_awl_version = 0.46;
204 $c->version_string = '0.9.9.4'; // The actual version # is replaced into that during the build /release process
205 if ( isset($c->version_string) && preg_match( '/(\d+)\.(\d+)\.(\d+)(.*)/', $c->version_string, $matches) ) {
206 $c->code_major = $matches[1];
207 $c->code_minor = $matches[2];
208 $c->code_patch = $matches[3];
209 $c->code_version = (($c->code_major * 1000) + $c->code_minor).'.'.$c->code_patch;
210 dbg_error_log('caldav', 'Version (%d.%d.%d) == %s', $c->code_major, $c->code_minor, $c->code_patch, $c->code_version);
211 @header( sprintf('Server: %d.%d', $c->code_major, $c->code_minor) );
215 * Force the domain name to what was in the configuration file
217 $_SERVER['SERVER_NAME'] = $c->domain_name;
219 require_once('AwlQuery.php');
221 $c->want_dbversion = array(1,2,9);
222 $c->schema_version = 0;
223 $qry = new AwlQuery( 'SELECT schema_major, schema_minor, schema_patch FROM awl_db_revision ORDER BY schema_id DESC LIMIT 1;' );
224 if ( $qry->Exec('always',__LINE__,__FILE__) && $row = $qry->Fetch() ) {
225 $c->schema_version = doubleval( sprintf( '%d%03d.%03d', $row->schema_major, $row->schema_minor, $row->schema_patch) );
226 $c->wanted_version = doubleval( sprintf( '%d%03d.%03d', $c->want_dbversion[0], $c->want_dbversion[1], $c->want_dbversion[2]) );
227 $c->schema_major = $row->schema_major;
228 $c->schema_minor = $row->schema_minor;
229 $c->schema_patch = $row->schema_patch;
230 if ( $c->schema_version < $c->wanted_version ) {
231 $c->messages[] = sprintf( 'Database schema needs upgrading. Current: %d.%d.%d, Desired: %d.%d.%d',
232 $row->schema_major, $row->schema_minor, $row->schema_patch, $c->want_dbversion[0], $c->want_dbversion[1], $c->want_dbversion[2]);
234 if ( isset($_SERVER['HTTP_X_DAVICAL_TESTCASE']) ) $qry->QDo('SET TIMEZONE TO \'Pacific/Auckland\'');
237 require_once('Principal.php');
240 * Return the HTTP status code description for a given code. Hopefully
241 * this is an efficient way to code this.
242 * @return string The text for a give HTTP status code, in english
244 function getStatusMessage($status) {
245 switch( $status ) {
246 case 100: $ans = 'Continue'; break;
247 case 101: $ans = 'Switching Protocols'; break;
248 case 200: $ans = 'OK'; break;
249 case 201: $ans = 'Created'; break;
250 case 202: $ans = 'Accepted'; break;
251 case 203: $ans = 'Non-Authoritative Information'; break;
252 case 204: $ans = 'No Content'; break;
253 case 205: $ans = 'Reset Content'; break;
254 case 206: $ans = 'Partial Content'; break;
255 case 207: $ans = 'Multi-Status'; break;
256 case 300: $ans = 'Multiple Choices'; break;
257 case 301: $ans = 'Moved Permanently'; break;
258 case 302: $ans = 'Found'; break;
259 case 303: $ans = 'See Other'; break;
260 case 304: $ans = 'Not Modified'; break;
261 case 305: $ans = 'Use Proxy'; break;
262 case 307: $ans = 'Temporary Redirect'; break;
263 case 400: $ans = 'Bad Request'; break;
264 case 401: $ans = 'Unauthorized'; break;
265 case 402: $ans = 'Payment Required'; break;
266 case 403: $ans = 'Forbidden'; break;
267 case 404: $ans = 'Not Found'; break;
268 case 405: $ans = 'Method Not Allowed'; break;
269 case 406: $ans = 'Not Acceptable'; break;
270 case 407: $ans = 'Proxy Authentication Required'; break;
271 case 408: $ans = 'Request Timeout'; break;
272 case 409: $ans = 'Conflict'; break;
273 case 410: $ans = 'Gone'; break;
274 case 411: $ans = 'Length Required'; break;
275 case 412: $ans = 'Precondition Failed'; break;
276 case 413: $ans = 'Request Entity Too Large'; break;
277 case 414: $ans = 'Request-URI Too Long'; break;
278 case 415: $ans = 'Unsupported Media Type'; break;
279 case 416: $ans = 'Requested Range Not Satisfiable'; break;
280 case 417: $ans = 'Expectation Failed'; break;
281 case 422: $ans = 'Unprocessable Entity'; break;
282 case 423: $ans = 'Locked'; break;
283 case 424: $ans = 'Failed Dependency'; break;
284 case 500: $ans = 'Internal Server Error'; break;
285 case 501: $ans = 'Not Implemented'; break;
286 case 502: $ans = 'Bad Gateway'; break;
287 case 503: $ans = 'Service Unavailable'; break;
288 case 504: $ans = 'Gateway Timeout'; break;
289 case 505: $ans = 'HTTP Version Not Supported'; break;
290 default: $ans = 'Unknown HTTP Status Code '.$status;
292 return $ans;
297 * Construct a URL from the supplied dav_name. The URL will be urlencoded,
298 * except for any '/' characters in it.
299 * @param string $partial_path The part of the path after the script name
301 function ConstructURL( $partial_path, $force_script = false ) {
302 global $c;
304 if ( ! isset($c->_url_script_path) ) {
305 $c->protocol_server_port_script = str_replace( 'index.php', 'caldav.php', $c->protocol_server_port_script);
306 $c->_url_script_path = (preg_match('#/$#', $c->protocol_server_port_script) ? 'caldav.php' : '');
307 $c->_url_script_path = $c->protocol_server_port_script . $c->_url_script_path;
310 $url = $c->_url_script_path;
311 if ( $force_script ) {
312 if ( ! preg_match( '#/caldav\.php$#', $url ) ) $url .= '/caldav.php';
314 $url .= str_replace( '%2F', '/', rawurlencode($partial_path));
315 $url = preg_replace( '#^(https?://.+)//#', '$1/', $url ); // Ensure we don't double any '/'
316 $url = preg_replace('#^https?://[^/]+#', '', $url ); // Remove any protocol + hostname portion
318 if ( strstr($url, 'caldav.php/caldav.php') !== false ) {
319 // trace_bug('Duplicated caldav.php/ in URL "%s" from partial_path=%s, force_script=%s', $url, $partial_path, ($force_script?'true':'false'));
320 $url = str_replace( 'caldav.php/caldav.php', 'caldav.php', $url ); // Ensure we don't double any 'caldav.php/'
323 return $url;
328 * Deconstruct a dav_name from the supplied URL. The dav_name will be urldecoded.
330 * @param string $partial_path The part of the path after the script name
332 function DeconstructURL( $url, $force_script = false ) {
333 global $c;
335 $dav_name = rawurldecode($url);
337 /** Allow a path like .../username/calendar.ics to translate into the calendar URL */
338 if ( preg_match( '#^(/[^/]+/[^/]+).ics$#', $dav_name, $matches ) ) {
339 $dav_name = $matches[1]. '/';
342 /** remove any leading protocol/server/port/prefix... */
343 if ( !isset($c->deconstruction_base_path) ) $c->deconstruction_base_path = ConstructURL('/');
344 if ( preg_match( '%^(.*?)'.str_replace('%', '\\%',$c->deconstruction_base_path).'(.*)$%', $dav_name, $matches ) ) {
345 if ( $matches[1] == '' || $matches[1] == $c->protocol_server_port ) {
346 $dav_name = $matches[2];
350 /** strip doubled slashes */
351 if ( strstr($dav_name,'//') ) $dav_name = preg_replace( '#//+#', '/', $dav_name);
353 if ( substr($dav_name,0,1) != '/' ) $dav_name = '/'.$dav_name;
355 return $dav_name;
360 * Convert a date from ISO format into the sad old HTTP format.
361 * @param string $isodate The date to convert
363 function ISODateToHTTPDate( $isodate ) {
364 // It is necessary to use English for this, explicitly. See Debian BTS Bug#661985 for more info.
365 $month = gmstrftime('%m', strtotime($isodate));
366 switch( intval($month) ) {
367 case 1: $month = 'Jan'; break;
368 case 2: $month = 'Feb'; break;
369 case 3: $month = 'Mar'; break;
370 case 4: $month = 'Apr'; break;
371 case 5: $month = 'May'; break;
372 case 6: $month = 'Jun'; break;
373 case 7: $month = 'Jul'; break;
374 case 8: $month = 'Aug'; break;
375 case 9: $month = 'Sep'; break;
376 case 10: $month = 'Oct'; break;
377 case 11: $month = 'Nov'; break;
378 case 12: $month = 'Dec'; break;
379 default:
380 throw new Exception('Invalid month '.$month);
382 // Use strtotime since strptime is not available on Windows platform.
383 return( gmstrftime('%a, %d '.$month.' %Y %H:%M:%S GMT', strtotime($isodate)) );
387 * Convert a date into ISO format into the sparkly new ISO format.
388 * @param string $indate The date to convert
390 function DateToISODate( $indate, $in_utc=false ) {
391 // Use strtotime since strptime is not available on Windows platform.
392 if ( $in_utc ) return( gmdate('Ymd\THis\Z', strtotime($indate)) );
393 return( date('c', strtotime($indate)) );
397 * Given a privilege string, or an array of privilege strings, return a bit mask
398 * of the privileges.
399 * @param mixed $raw_privs The string (or array of strings) of privilege names
400 * @return integer A bit mask of the privileges.
402 define("DAVICAL_MAXPRIV", "65535");
403 define("DAVICAL_ADDRESSBOOK_MAXPRIV", "1023");
404 function privilege_to_bits( $raw_privs ) {
405 $out_priv = 0;
407 if ( gettype($raw_privs) == 'string' ) $raw_privs = array( $raw_privs );
409 if ( ! is_array($raw_privs) ) $raw_privs = array($raw_privs);
411 foreach( $raw_privs AS $priv ) {
412 $trim_priv = trim(strtolower(preg_replace( '/^.*:/', '', $priv)));
413 switch( $trim_priv ) {
414 case 'read' : $out_priv |= 1; break;
415 case 'write-properties' : $out_priv |= 2; break;
416 case 'write-content' : $out_priv |= 4; break;
417 case 'unlock' : $out_priv |= 8; break;
418 case 'read-acl' : $out_priv |= 16; break;
419 case 'read-current-user-privilege-set' : $out_priv |= 32; break;
420 case 'bind' : $out_priv |= 64; break;
421 case 'unbind' : $out_priv |= 128; break;
422 case 'write-acl' : $out_priv |= 256; break;
423 case 'read-free-busy' : $out_priv |= 512; break;
424 case 'schedule-deliver-invite' : $out_priv |= 1024; break;
425 case 'schedule-deliver-reply' : $out_priv |= 2048; break;
426 case 'schedule-query-freebusy' : $out_priv |= 4096; break;
427 case 'schedule-send-invite' : $out_priv |= 8192; break;
428 case 'schedule-send-reply' : $out_priv |= 16384; break;
429 case 'schedule-send-freebusy' : $out_priv |= 32768; break;
431 /** Aggregates of Privileges */
432 case 'write' : $out_priv |= 198; break; // 2 + 4 + 64 + 128
433 case 'schedule-deliver' : $out_priv |= 7168; break; // 1024 + 2048 + 4096
434 case 'schedule-send' : $out_priv |= 57344; break; // 8192 + 16384 + 32768
435 case 'all' : $out_priv = DAVICAL_MAXPRIV; break;
436 case 'fake_privilege_for_input' : break;
437 default:
438 dbg_error_log( 'ERROR', 'Cannot convert privilege of "%s" into bits.', $priv );
443 // 'all' will include future privileges
444 if ( ($out_priv & DAVICAL_MAXPRIV) >= DAVICAL_MAXPRIV ) $out_priv = pow(2,24) - 1;
445 return $out_priv;
450 * Given a bit mask of the privileges, will return an array of the
451 * text values of privileges.
452 * @param integer $raw_bits A bit mask of the privileges.
453 * @return mixed The string (or array of strings) of privilege names
455 function bits_to_privilege( $raw_bits, $resourcetype = 'resource' ) {
456 $out_priv = array();
458 if ( is_string($raw_bits) ) {
459 $raw_bits = bindec($raw_bits);
462 if ( ($raw_bits & DAVICAL_MAXPRIV) == DAVICAL_MAXPRIV ) $out_priv[] = 'all';
464 if ( ($raw_bits & 1) != 0 ) $out_priv[] = 'DAV::read';
465 if ( ($raw_bits & 8) != 0 ) $out_priv[] = 'DAV::unlock';
466 if ( ($raw_bits & 16) != 0 ) $out_priv[] = 'DAV::read-acl';
467 if ( ($raw_bits & 32) != 0 ) $out_priv[] = 'DAV::read-current-user-privilege-set';
468 if ( ($raw_bits & 256) != 0 ) $out_priv[] = 'DAV::write-acl';
469 if ( ($resourcetype == 'calendar' || $resourcetype == 'proxy' || $resourcetype == '*') && ($raw_bits & 512) != 0 ) $out_priv[] = 'urn:ietf:params:xml:ns:caldav:read-free-busy';
471 if ( ($raw_bits & 198) != 0 ) {
472 if ( ($raw_bits & 198) == 198 ) $out_priv[] = 'DAV::write';
473 if ( ($raw_bits & 2) != 0 ) $out_priv[] = 'DAV::write-properties';
474 if ( ($raw_bits & 4) != 0 ) $out_priv[] = 'DAV::write-content';
475 if ( ($raw_bits & 64) != 0 ) $out_priv[] = 'DAV::bind';
476 if ( ($raw_bits & 128) != 0 ) $out_priv[] = 'DAV::unbind';
479 if ( ($resourcetype == 'schedule-inbox' || $resourcetype == '*') && ($raw_bits & 7168) != 0 ) {
480 if ( ($raw_bits & 7168) == 7168 ) $out_priv[] = 'urn:ietf:params:xml:ns:caldav:schedule-deliver';
481 if ( ($raw_bits & 1024) != 0 ) $out_priv[] = 'urn:ietf:params:xml:ns:caldav:schedule-deliver-invite';
482 if ( ($raw_bits & 2048) != 0 ) $out_priv[] = 'urn:ietf:params:xml:ns:caldav:schedule-deliver-reply';
483 if ( ($raw_bits & 4096) != 0 ) $out_priv[] = 'urn:ietf:params:xml:ns:caldav:schedule-query-freebusy';
486 if ( ($resourcetype == 'schedule-outbox' || $resourcetype == '*') && ($raw_bits & 57344) != 0 ) {
487 if ( ($raw_bits & 57344) == 57344 ) $out_priv[] = 'urn:ietf:params:xml:ns:caldav:schedule-send';
488 if ( ($raw_bits & 8192) != 0 ) $out_priv[] = 'urn:ietf:params:xml:ns:caldav:schedule-send-invite';
489 if ( ($raw_bits & 16384) != 0 ) $out_priv[] = 'urn:ietf:params:xml:ns:caldav:schedule-send-reply';
490 if ( ($raw_bits & 32768) != 0 ) $out_priv[] = 'urn:ietf:params:xml:ns:caldav:schedule-send-freebusy';
493 // dbg_error_log( 'DAVResource', ' Privilege bit "%s" is "%s".', $raw_bits, implode(', ', $out_priv) );
495 return $out_priv;
500 * Returns the array of privilege names converted into XMLElements
502 function privileges_to_XML( $privilege_names, &$xmldoc=null ) {
503 if ( !isset($xmldoc) && isset($GLOBALS['reply']) ) $xmldoc = $GLOBALS['reply'];
504 $privileges = array();
505 foreach( $privilege_names AS $k ) {
506 $privilege = new XMLElement('privilege');
507 if ( isset($xmldoc) )
508 $xmldoc->NSElement($privilege,$k);
509 else
510 $privilege->NewElement($k);
511 $privileges[] = $privilege;
513 return $privileges;