source: trunk/phpgwapi/inc/class.common.inc.php @ 2

Revision 2, 58.5 KB checked in by niltonneto, 17 years ago (diff)

Removida todas as tags usadas pelo CVS ($Id, $Source).
Primeira versão no CVS externo.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2  /**************************************************************************\
3  * eGroupWare API - Commononly used functions                               *
4  * This file written by Dan Kuykendall <seek3r@phpgroupware.org>            *
5  * and Joseph Engo <jengo@phpgroupware.org>                                 *
6  * and Mark Peters <skeeter@phpgroupware.org>                               *
7  * and Lars Kneschke <lkneschke@linux-at-work.de>                           *
8  * Functions commonly used by eGroupWare developers                         *
9  * Copyright (C) 2000, 2001 Dan Kuykendall                                  *
10  * Copyright (C) 2003 Lars Kneschke                                         *
11  * -------------------------------------------------------------------------*
12  * This library is part of the eGroupWare API                               *
13  * http://www.egroupware.org                                                *
14  * ------------------------------------------------------------------------ *
15  * This library is free software; you can redistribute it and/or modify it  *
16  * under the terms of the GNU Lesser General Public License as published by *
17  * the Free Software Foundation; either version 2.1 of the License,         *
18  * or any later version.                                                    *
19  * This library is distributed in the hope that it will be useful, but      *
20  * WITHOUT ANY WARRANTY; without even the implied warranty of               *
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     *
22  * See the GNU Lesser General Public License for more details.              *
23  * You should have received a copy of the GNU Lesser General Public License *
24  * along with this library; if not, write to the Free Software Foundation,  *
25  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA            *
26  \**************************************************************************/
27
28
29        $d1 = strtolower(@substr(PHPGW_API_INC,0,3));
30        $d2 = strtolower(@substr(PHPGW_SERVER_ROOT,0,3));
31        $d3 = strtolower(@substr(PHPGW_APP_INC,0,3));
32        if($d1 == 'htt' || $d1 == 'ftp' || $d2 == 'htt' || $d2 == 'ftp' || $d3 == 'htt' || $d3 == 'ftp')
33        {
34                echo 'Failed attempt to break in via an old Security Hole!<br>'."\n";
35                exit;
36        }
37        unset($d1);unset($d2);unset($d3);
38
39        /*!
40        @class common
41        @abstract common class that contains commonly used functions
42        */
43        class common
44        {
45                var $debug_info; // An array with debugging info from the API
46                var $found_files;
47
48                /*!
49                @function cmp_version
50                @abstract Compares two Version strings and return 1 if str2 is newest (bigger version number) than str1
51                @discussion This function checks for major version only.
52                @param $str1
53                @param $str2
54                */
55                function cmp_version($str1,$str2,$debug=False)
56                {
57                        ereg("([0-9]+)\.([0-9]+)\.([0-9]+)[a-zA-Z]*([0-9]*)",$str1,$regs);
58                        ereg("([0-9]+)\.([0-9]+)\.([0-9]+)[a-zA-Z]*([0-9]*)",$str2,$regs2);
59                        if($debug) { echo "<br>$regs[0] - $regs2[0]"; }
60
61                        for($i=1;$i<5;$i++)
62                        {
63                                if($debug) { echo "<br>$i: $regs[$i] - $regs2[$i]"; }
64                                if($regs2[$i] == $regs[$i])
65                                {
66                                        continue;
67                                }
68                                if($regs2[$i] > $regs[$i])
69                                {
70                                        return 1;
71                                }
72                                elseif($regs2[$i] < $regs[$i])
73                                {
74                                        return 0;
75                                }
76                        }
77                }
78
79                /*!
80                @function cmp_version_long
81                @abstract Compares two Version strings and return 1 if str2 is newest (bigger version number) than str1
82                @discussion This function checks all fields. cmp_version() checks release version only.
83                @param $str1
84                @param $str2
85                */
86                function cmp_version_long($str1,$str2,$debug=False)
87                {
88                        ereg("([0-9]+)\.([0-9]+)\.([0-9]+)[a-zA-Z]*([0-9]*)\.([0-9]*)",$str1,$regs);
89                        ereg("([0-9]+)\.([0-9]+)\.([0-9]+)[a-zA-Z]*([0-9]*)\.([0-9]*)",$str2,$regs2);
90                        if($debug) { echo "<br>$regs[0] - $regs2[0]"; }
91
92                        for($i=1;$i<6;$i++)
93                        {
94                                if($debug) { echo "<br>$i: $regs[$i] - $regs2[$i]"; }
95
96                                if($regs2[$i] == $regs[$i])
97                                {
98                                        if($debug) { echo ' are equal...'; }
99                                        continue;
100                                }
101                                if($regs2[$i] > $regs[$i])
102                                {
103                                        if($debug) { echo ', and a > b'; }
104                                        return 1;
105                                }
106                                elseif($regs2[$i] < $regs[$i])
107                                {
108                                        if($debug) { echo ', and a < b'; }
109                                        return 0;
110                                }
111                        }
112                        if($debug) { echo ' - all equal.'; }
113                }
114
115                // Convert an array into the format needed for the access column.
116                /*!
117                @function array_to_string
118                @abstract Convert an array into the format needed for the access column
119                @param $access
120                @param $array
121                */
122                function array_to_string($access,$array)
123                {
124                        $this->debug_info[] = 'array_to_string() is a depreciated function - use ACL instead';
125                        $s = '';
126                        if ($access == 'group' || $access == 'public' || $access == 'none')
127                        {
128                                if (count($array))
129                                {
130                                        while ($t = each($array))
131                                        {
132                                                $s .= ',' . $t[1];
133                                        }
134                                        $s .= ',';
135                                }
136                                if (! count($array) && $access == 'none')
137                                {
138                                        $s = '';
139                                }
140                        }
141                        return $s;
142                }
143
144                // This is used for searching the access fields
145                /*!
146                @function sql_search
147                @abstract this function is used for searching the access fields
148                @param $table
149                @param $owner
150                */
151                function sql_search($table,$owner=0)
152                {
153                        $this->debug_info[] = 'sql_search() is a deprecated function - use ACL instead';
154                        $s = '';
155                        if (!$owner)
156                        {
157                                $owner = $GLOBALS['phpgw_info']['user']['account_id'];
158                        }
159                        $groups = $GLOBALS['phpgw']->accounts->membership((int)$owner);
160                        if(@is_array($groups))
161                        {
162                                while ($group = each($groups))
163                                {
164                                        $s .= " OR $table LIKE '%," . $group[2] . ",%'";
165                                }
166                        }
167                        return $s;
168                }
169
170                // return a array of installed languages
171                /*!
172                @function getInstalledLanguages
173                @abstract return an array of installed languages
174                @result $installedLanguages; an array containing the installed languages
175                */
176                function getInstalledLanguages()
177                {
178                        $GLOBALS['phpgw']->db->query('SELECT DISTINCT lang FROM phpgw_lang');
179                        while (@$GLOBALS['phpgw']->db->next_record())
180                        {
181                                $installedLanguages[$GLOBALS['phpgw']->db->f('lang')] = $GLOBALS['phpgw']->db->f('lang');
182                        }
183
184                        return $installedLanguages;
185                }
186
187                // return the preferred language of the users
188                // it's using HTTP_ACCEPT_LANGUAGE (send from the users browser)
189                // and ...(to find out which languages are installed)
190                /*!
191                @function getPreferredLanguage
192                @abstract return the preferred langugae of the users
193                @discussion it uses HTTP_ACCEPT_LANGUAGE (from the users browser) <br>
194                and .... to find out which languages are installed
195                */
196                function getPreferredLanguage()
197                {
198                        // create a array of languages the user is accepting
199                        $userLanguages = explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
200                        $supportedLanguages = $this->getInstalledLanguages();
201
202                        // find usersupported language
203//                      while (list($key,$value) = each($userLanguages))
204                        foreach($userLanguages as $key => $value)
205                        {
206                                // remove everything behind '-' example: de-de
207                                $value = trim($value);
208                                $pieces = explode('-', $value);
209                                $value = $pieces[0];
210                                # print 'current lang $value<br>';
211                                if ($supportedLanguages[$value])
212                                {
213                                        $retValue=$value;
214                                        break;
215                                }
216                        }
217
218                        // no usersupported language found -> return english
219                        if (empty($retValue))
220                        {
221                                $retValue='en';
222                        }
223
224                        return $retValue;
225                }
226
227                /*!
228                @function ldap_addslashes
229                @abstract escapes a string for use in searchfilters meant for ldap_search.
230                Escaped Characters are: '*', '(', ')', ' ', '\', NUL
231                It's actually a PHP-Bug, that we have to escape space.
232                For all other Characters, refer to RFC2254.
233                @param $string string to be escaped
234                */
235                function ldap_addslashes($string='')
236                {
237                        return str_replace(array('\\','*','(',')','\0',' '),array('\\\\','\*','\(','\)','\\0','\20'),$string);
238                }
239
240                // connect to the ldap server and return a handle
241                /*!
242                @function ldapConnect
243                @abstract connect to the ldap server and return a handle
244                @param $host ldap host
245                @param $dn ldap_root_dn
246                @param $passwd ldap_root_pw
247                */
248                function ldapConnect($host='', $dn='', $passwd='', $ldapreferral=false) #default: dont follow the referral
249                {
250                        if(!$host || $host == $GLOBALS['phpgw_info']['server']['ldap_host']) {
251                                $dn     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
252                                $passwd = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
253                                $host   = $GLOBALS['phpgw_info']['server']['ldap_host'];
254                        }
255                        else if(strstr($host, "ldap://")){
256                                $dn = '';
257                                $passwd = '';
258                        }
259
260                        if(!function_exists('ldap_connect'))
261                        {
262                                /* log does not exist in setup(, yet) */
263                                if(is_object($GLOBALS['phpgw']->log))
264                                {
265                                        $GLOBALS['phpgw']->log->message('F-Abort, LDAP support unavailable');
266                                        $GLOBALS['phpgw']->log->commit();
267                                }
268
269                                printf('<b>Error: LDAP support unavailable</b><br>',$host);
270                                return False;
271                        }
272
273                        // connect to ldap server
274                        if(!$ds = ldap_connect($host))
275                        {
276                                /* log does not exist in setup(, yet) */
277                                if(is_object($GLOBALS['phpgw']->log))
278                                {
279                                        $GLOBALS['phpgw']->log->message('F-Abort, Failed connecting to LDAP server');
280                                        $GLOBALS['phpgw']->log->commit();
281                                }
282
283                                printf("<b>Error: Can't connect to LDAP server %s!</b><br>",$host);
284                                return False;
285                        }
286
287                        if($GLOBALS['phpgw_info']['server']['ldap_version3'])
288                        {
289                                if(!ldap_set_option($ds,LDAP_OPT_PROTOCOL_VERSION,3))
290                                {
291                                        $GLOBALS['phpgw_info']['server']['ldap_version3'] = False;
292                                }
293                        }
294                       
295                        ldap_set_option($ds, LDAP_OPT_REFERRALS, $ldapreferral);
296                       
297                        // bind as admin
298                        if($dn && $passwd && ! ldap_bind($ds,$dn,$passwd))
299                        {
300                                if(is_object($GLOBALS['phpgw']->log))
301                                {
302                                        $GLOBALS['phpgw']->log->message('F-Abort, Failed binding to LDAP server');
303                                        $GLOBALS['phpgw']->log->commit();
304                                }
305
306                                printf("<b>Error: Can't bind to LDAP server: %s!</b><br>",$dn);
307                                return False;
308                        }
309                        // bind as anonymous
310                        if(!$dn && !$passwd && ! ldap_bind($ds))
311                        {
312                                if(is_object($GLOBALS['phpgw']->log))
313                                {
314                                        $GLOBALS['phpgw']->log->message('F-Abort, Failed  (anonymous bind) to LDAP server');
315                                        $GLOBALS['phpgw']->log->commit();
316                                }
317                                printf("<b>Error: Can't bind to LDAP server (anonymous bind): %s!</b><br>",$dn);
318                                return False;
319                        }                                               
320                        return $ds;
321                }
322
323                // This function is used if the developer wants to stop a running app in the middle of execution
324                // We may need to do some clean up before hand
325                /*!
326                @function phpgw_exit
327                @abstract function to stop running an app
328                @discussion used to stop running an app in the middle of execution <br>
329                There may need to be some cleanup before hand
330                @param $call_footer boolean value to if true then call footer else exit
331                */
332                function phpgw_exit($call_footer = False)
333                {
334                        if (!defined('PHPGW_EXIT'))
335                        {
336                                define('PHPGW_EXIT',True);
337
338                                if ($call_footer)
339                                {
340                                        $this->phpgw_footer();
341                                }
342                        }
343                        exit;
344                }
345
346                function phpgw_final()
347                {
348                        if (!defined('PHPGW_FINAL'))
349                        {
350                                define('PHPGW_FINAL',True);
351
352                                /*if (is_object($GLOBALS['phpgw']->accounts))
353                                {
354                                        $GLOBALS['phpgw']->accounts->save_session_cache();
355                                }*/
356                                // call the asyncservice check_run function if it is not explicitly set to cron-only
357                                //
358                                if (!$GLOBALS['phpgw_info']['server']['asyncservice'])  // is default
359                                {
360                                        ExecMethod('phpgwapi.asyncservice.check_run','fallback');
361                                }
362                                /* Clean up mcrypt */
363                                if (@is_object($GLOBALS['phpgw']->crypto))
364                                {
365                                        $GLOBALS['phpgw']->crypto->cleanup();
366                                        unset($GLOBALS['phpgw']->crypto);
367                                }
368                                $GLOBALS['phpgw']->db->disconnect();
369                        }
370                }
371
372                /*!
373                @function randomstring
374                @abstract return a random string of size $size
375                @param $size int-size of random string to return
376                */
377                function randomstring($size)
378                {
379                        $s = '';
380                        srand((double)microtime()*1000000);
381                        $random_char = array(
382                                '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f',
383                                'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
384                                'w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L',
385                                'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
386                        );
387
388                        for ($i=0; $i<$size; $i++)
389                        {
390                                $s .= $random_char[rand(1,61)];
391                        }
392                        return $s;
393                }
394
395                // Look at the note towards the top of this file (jengo)
396                function filesystem_separator()
397                {
398                        return filesystem_separator();
399                }
400
401                /*!
402                @function error_list
403                @abstract This is used for reporting errors in a nice format.
404                @param $error - array of errors
405                */
406                function error_list($errors,$text='Error')
407                {
408                        if (! is_array($errors))
409                        {
410                                return False;
411                        }
412
413                        $html_error = '<table border="0" width="100%"><tr><td align="right"><b>' . lang($text)
414                                . '</b>: </td><td align="left">' . $errors[0] . '</td></tr>';
415                        for ($i=1; $i<count($errors); $i++)
416                        {
417                                $html_error .= '<tr><td>&nbsp;</td><td align="left">' . $errors[$i] . '</td></tr>';
418                        }
419                        return $html_error . '</table>';
420                }
421
422                /*!
423                @function check_owner
424                @abstract none yet
425                @param $record ?
426                @param $link ?
427                @param $label ?
428                @param $extravars
429                */
430                // This is a depreciated function - use ACL instead (jengo)
431                function check_owner($record,$link,$label,$extravars = '')
432                {
433                        $this->debug_info[] = 'check_owner() is a depreciated function - use ACL instead';
434                        /*
435                        $s = '<a href="' . $GLOBALS['phpgw']->link($link,$extravars) . '"> ' . lang($label) . ' </a>';
436                        if (ereg('^[0-9]+$',$record))
437                        {
438                                if ($record != $GLOBALS['phpgw_info']['user']['account_id'])
439                                {
440                                        $s = '&nbsp;';
441                                }
442                        }
443                        else
444                        {
445                                if ($record != $GLOBALS['phpgw_info']['user']['userid'])
446                                {
447                                        $s = '&nbsp';
448                                }
449                        }
450
451                        return $s;
452                        */
453                }
454
455                /*!
456                @function display_fullname
457                @abstract return the fullname of a user
458                @param $lid account loginid
459                @param $firstname firstname
460                @param $lastname lastname
461                */
462                function display_fullname($lid = '', $firstname = '', $lastname = '')
463                {
464                        if (! $lid && ! $firstname && ! $lastname)
465                        {
466                                $lid       = $GLOBALS['phpgw_info']['user']['account_lid'];
467                                $firstname = $GLOBALS['phpgw_info']['user']['firstname'];
468                                $lastname  = $GLOBALS['phpgw_info']['user']['lastname'];
469                        }
470
471                        $display = $GLOBALS['phpgw_info']['user']['preferences']['common']['account_display'];
472
473                        if ($firstname && $lastname)
474                        {
475                                $delimiter = ', ';
476                        }
477                        else
478                        {
479                                $delimiter = '';
480                        }
481                       
482                        $name = '';
483                        switch($display)
484                        {
485                                case 'firstname':
486                                        $name = $firstname . ' ' . $lastname;
487                                        break;
488                                case 'lastname':
489                                        $name = $lastname . $delimiter . $firstname;
490                                        break;
491                                case 'username':
492                                        $name = $lid;
493                                        break;
494                                case 'firstall':
495                                        $name = $firstname . ' ' . $lastname . ' ['.$lid.']';
496                                        break;
497                                case 'lastall':
498                                        $name = $lastname . $delimiter . $firstname . ' ['.$lid.']';
499                                        break;
500                                case 'all':
501                                        /* fall through */
502                                default:
503                                        $name = '['.$lid.'] ' . $firstname . ' ' . $lastname;
504                        }
505                        return $name;
506                }
507
508                /*!
509                @function grab_owner_name
510                @abstract grab the owner name
511                @param $id account id
512                */
513                function grab_owner_name($accountid = '')
514                {
515                        $GLOBALS['phpgw']->accounts->get_account_name($accountid,$lid,$fname,$lname);
516                        return $this->display_fullname($lid,$fname,$lname);
517                }
518
519                /*!
520                @function create_tabs
521                @abstract create tabs
522                @param $tabs ?
523                @param $selected ?
524                @param $fontsize optional
525                */
526                function create_tabs($tabs, $selected, $fontsize = '')
527                {
528                        $output_text = '<table border="0" cellspacing="0" cellpadding="0"><tr>';
529
530                        /* This is a php3 workaround */
531                        if(PHPGW_IMAGES_DIR == 'PHPGW_IMAGES_DIR')
532                        {
533                                $ir = ExecMethod('phpgwapi.phpgw.common.get_image_path', 'phpgwapi');
534                        }
535                        else
536                        {
537                                $ir = PHPGW_IMAGES_DIR;
538                        }
539
540                        if ($fontsize)
541                        {
542                                $fs  = '<font size="' . $fontsize . '">';
543                                $fse = '</font>';
544                        }
545
546                        $i = 1;
547                        while ($tab = each($tabs))
548                        {
549                                if ($tab[0] == $selected)
550                                {
551                                        if ($i == 1)
552                                        {
553                                                $output_text .= '<td align="right"><img src="' . $ir . '/tabs-start1.gif"></td>';
554                                        }
555
556                                        $output_text .= '<td align="left" background="' . $ir . '/tabs-bg1.gif">&nbsp;<b><a href="'
557                                                . $tab[1]['link'] . '" class="tablink" '.$tab[1]['target'].'>' . $fs . $tab[1]['label']
558                                                . $fse . '</a></b>&nbsp;</td>';
559                                        if ($i == count($tabs))
560                                        {
561                                                $output_text .= '<td align="left"><img src="' . $ir . '/tabs-end1.gif"></td>';
562                                        }
563                                        else
564                                        {
565                                                $output_text .= '<td align="left"><img src="' . $ir . '/tabs-sepr.gif"></td>';
566                                        }
567                                }
568                                else
569                                {
570                                        if ($i == 1)
571                                        {
572                                                $output_text .= '<td align="right"><img src="' . $ir . '/tabs-start0.gif"></td>';
573                                        }
574                                        $output_text .= '<td align="left" background="' . $ir . '/tabs-bg0.gif">&nbsp;<b><a href="'
575                                                . $tab[1]['link'] . '" class="tablink" '.$tab[1]['target'].'>' . $fs . $tab[1]['label'] . $fse
576                                                . '</a></b>&nbsp;</td>';
577                                        if (($i + 1) == $selected)
578                                        {
579                                                $output_text .= '<td align="left"><img src="' . $ir . '/tabs-sepl.gif"></td>';
580                                        }
581                                        elseif ($i == $selected || $i != count($tabs))
582                                        {
583                                                $output_text .= '<td align="left"><img src="' . $ir . '/tabs-sepm.gif"></td>';
584                                        }
585                                        elseif ($i == count($tabs))
586                                        {
587                                                if ($i == $selected)
588                                                {
589                                                        $output_text .= '<td align="left"><img src="' . $ir . '/tabs-end1.gif"></td>';
590                                                }
591                                                else
592                                                {
593                                                        $output_text .= '<td align="left"><img src="' . $ir . '/tabs-end0.gif"></td>';
594                                                }
595                                        }
596                                        else
597                                        {
598                                                if ($i != count($tabs))
599                                                {
600                                                        $output_text .= '<td align="left"><img src="' . $ir . '/tabs-sepr.gif"></td>';
601                                                }
602                                        }
603                                }
604                                $i++;
605                                $output_text .= "\n";
606                        }
607                        $output_text .= "</table>\n";
608                        return $output_text;
609                }
610
611                /*!
612                @function get_app_dir
613                @abstract get directory of application
614                @discussion $appname can either be passed or derived from $phpgw_info['flags']['currentapp'];
615                @param $appname name of application
616                */
617                function get_app_dir($appname = '')
618                {
619                        if ($appname == '')
620                        {
621                                $appname = $GLOBALS['phpgw_info']['flags']['currentapp'];
622                        }
623                        if ($appname == 'home' || $appname == 'logout' || $appname == 'login')
624                        {
625                                $appname = 'phpgwapi';
626                        }
627
628                        $appdir         = PHPGW_INCLUDE_ROOT . '/'.$appname;
629                        $appdir_default = PHPGW_SERVER_ROOT . '/'.$appname;
630
631                        if (@is_dir ($appdir))
632                        {
633                                return $appdir;
634                        }
635                        elseif (@is_dir ($appdir_default))
636                        {
637                                return $appdir_default;
638                        }
639                        else
640                        {
641                                return False;
642                        }
643                }
644
645                /*!
646                @function get_inc_dir
647                @abstract get inc (include dir) of application
648                @discussion $appname can either be passed or derived from $phpgw_info['flags']['currentapp'];
649                @param $appname name of application
650                */
651                function get_inc_dir($appname = '')
652                {
653                        if (! $appname)
654                        {
655                                $appname = $GLOBALS['phpgw_info']['flags']['currentapp'];
656                        }
657                        if ($appname == 'home' || $appname == 'logout' || $appname == 'login' || $appname == 'about')
658                        {
659                                $appname = 'phpgwapi';
660                        }
661
662                        $incdir         = PHPGW_INCLUDE_ROOT . '/' . $appname . '/inc';
663                        $incdir_default = PHPGW_SERVER_ROOT . '/' . $appname . '/inc';
664
665                        if (@is_dir ($incdir))
666                        {
667                                return $incdir;
668                        }
669                        elseif (@is_dir ($incdir_default))
670                        {
671                                return $incdir_default;
672                        }
673                        else
674                        {
675                                return False;
676                        }
677                }
678
679                /*!
680                @function list_themes
681                @abstract list themes available
682                @note themes can either be css file like in HEAD (if the template has a css-dir and has css-files in is) \
683                        or ordinary .14 themes-files
684                */
685                function list_themes()
686                {
687                        $tpl_dir = $this->get_tpl_dir('phpgwapi');
688
689                        if ($dh = @opendir($tpl_dir . SEP . 'css'))
690                        {
691                                while ($file = readdir($dh))
692                                {
693                                        if (eregi("\.css$", $file) && $file != 'phpgw.css')
694                                        {
695                                                $list[] = substr($file,0,strpos($file,'.'));
696                                        }
697                                }
698                        }
699                        if(!is_array($list))
700                        {
701                                $dh = opendir(PHPGW_SERVER_ROOT . '/phpgwapi/themes');
702                                while ($file = readdir($dh))
703                                {
704                                        if (eregi("\.theme$", $file))
705                                        {
706                                                $list[] = substr($file,0,strpos($file,'.'));
707                                        }
708                                }
709                        }
710                        closedir($dh);
711                        reset ($list);
712                        return $list;
713                }
714
715                /**
716                * List available templates
717                *
718                * @returns array alphabetically sorted list of templates
719                */
720                function list_templates()
721                {
722                        $d = dir(PHPGW_SERVER_ROOT . '/phpgwapi/templates');
723                        while ($entry=$d->read())
724                        {
725                                if ($entry != 'CVS' && $entry != '.' && $entry != '..'
726                                        && $entry != 'phpgw_website'
727                                        && is_dir(PHPGW_SERVER_ROOT . '/phpgwapi/templates/' . $entry))
728                                {
729                                        $list[$entry]['name'] = $entry;
730                                        $f = PHPGW_SERVER_ROOT . '/phpgwapi/templates/' . $entry . '/details.inc.php';
731                                        if (file_exists ($f))
732                                        {
733                                                include($f);
734                                                $list[$entry]['title'] = 'Use '.$GLOBALS['phpgw_info']['template'][$entry]['title'].'interface';
735                                        }
736                                        else
737                                        {
738                                                $list[$entry]['title'] = $entry;
739                                        }
740                                }
741                        }
742                        $d->close();
743                        ksort($list);
744                        return $list;
745                }
746
747                /*!
748                @function get_tpl_dir
749                @abstract get template dir of an application
750                @param $appname appication name optional can be derived from $phpgw_info['flags']['currentapp'];
751                */
752                function get_tpl_dir($appname = '')
753                {
754                        if (! $appname)
755                        {
756                                $appname = $GLOBALS['phpgw_info']['flags']['currentapp'];
757                        }
758                        if ($appname == 'home' || $appname == 'logout' || $appname == 'login')
759                        {
760                                $appname = 'phpgwapi';
761                        }
762
763                        if (!isset($GLOBALS['phpgw_info']['server']['template_set']) && isset($GLOBALS['phpgw_info']['user']['preferences']['common']['template_set']))
764                        {
765                                $GLOBALS['phpgw_info']['server']['template_set'] = $GLOBALS['phpgw_info']['user']['preferences']['common']['template_set'];
766                        }
767
768                        // Setting this for display of template choices in user preferences
769                        if ($GLOBALS['phpgw_info']['server']['template_set'] == 'user_choice')
770                        {
771                                $GLOBALS['phpgw_info']['server']['usrtplchoice'] = 'user_choice';
772                        }
773
774                        if (($GLOBALS['phpgw_info']['server']['template_set'] == 'user_choice' ||
775                                !isset($GLOBALS['phpgw_info']['server']['template_set'])) &&
776                                isset($GLOBALS['phpgw_info']['user']['preferences']['common']['template_set']))
777                        {
778                                $GLOBALS['phpgw_info']['server']['template_set'] = $GLOBALS['phpgw_info']['user']['preferences']['common']['template_set'];
779                        }
780                        elseif ($GLOBALS['phpgw_info']['server']['template_set'] == 'user_choice' ||
781                                !isset($GLOBALS['phpgw_info']['server']['template_set']))
782                        {
783                                $GLOBALS['phpgw_info']['server']['template_set'] = 'default';
784                        }
785
786                        $tpldir         = PHPGW_SERVER_ROOT . '/' . $appname . '/templates/' . $GLOBALS['phpgw_info']['server']['template_set'];
787                        $tpldir_default = PHPGW_SERVER_ROOT . '/' . $appname . '/templates/default';
788
789                        if (@is_dir($tpldir))
790                        {
791                                return $tpldir;
792                        }
793                        elseif (@is_dir($tpldir_default))
794                        {
795                                return $tpldir_default;
796                        }
797                        else
798                        {
799                                return False;
800                        }
801                }
802
803                /*!
804                @function is_image_dir
805                @abstract checks if image_dir exists and has more than just a navbar-icon
806                @note this is just a workaround for idots, better to use find_image, which has a fallback \
807                        on a per image basis to the default dir
808                */
809                function is_image_dir($dir)
810                {
811                        if (!@is_dir($dir))
812                        {
813                                return False;
814                        }
815                        if ($d = opendir($dir))
816                        {
817                                while ($f = readdir($d))
818                                {
819                                        $ext = strtolower(strrchr($f,'.'));
820                                        if (($ext == '.gif' || $ext == '.png') && strstr($f,'navbar') === False)
821                                        {
822                                                return True;
823                                        }
824                                }
825                        }
826                        return False;
827                }
828
829                /*!
830                @function get_image_dir
831                @abstract get image dir of an application
832                @param $appname application name optional can be derived from $phpgw_info['flags']['currentapp'];
833                */
834                function get_image_dir($appname = '')
835                {
836                        if ($appname == '')
837                        {
838                                $appname = $GLOBALS['phpgw_info']['flags']['currentapp'];
839                        }
840                        if (empty($GLOBALS['phpgw_info']['server']['template_set']))
841                        {
842                                $GLOBALS['phpgw_info']['server']['template_set'] = 'default';
843                        }
844
845                        $imagedir            = PHPGW_SERVER_ROOT . '/' . $appname . '/templates/'
846                                . $GLOBALS['phpgw_info']['server']['template_set'] . '/images';
847                        $imagedir_default    = PHPGW_SERVER_ROOT . '/' . $appname . '/templates/default/images';
848                        $imagedir_olddefault = PHPGW_SERVER_ROOT . '/' . $appname . '/images';
849
850                        if ($this->is_image_dir ($imagedir))
851                        {
852                                return $imagedir;
853                        }
854                        elseif ($this->is_image_dir ($imagedir_default))
855                        {
856                                return $imagedir_default;
857                        }
858                        elseif ($this->is_image_dir ($imagedir_olddefault))
859                        {
860                                return $imagedir_olddefault;
861                        }
862                        else
863                        {
864                                return False;
865                        }
866                }
867
868                /*!
869                @function get_image_path
870                @abstract get image path of an application
871                @param $appname appication name optional can be derived from $phpgw_info['flags']['currentapp'];
872                */
873                function get_image_path($appname = '')
874                {
875                        if ($appname == '')
876                        {
877                                $appname = $GLOBALS['phpgw_info']['flags']['currentapp'];
878                        }
879
880                        if (empty($GLOBALS['phpgw_info']['server']['template_set']))
881                        {
882                                $GLOBALS['phpgw_info']['server']['template_set'] = 'default';
883                        }
884
885                        $imagedir            = PHPGW_SERVER_ROOT . '/'.$appname.'/templates/'.$GLOBALS['phpgw_info']['server']['template_set'].'/images';
886                        $imagedir_default    = PHPGW_SERVER_ROOT . '/'.$appname.'/templates/default/images';
887                        $imagedir_olddefault = PHPGW_SERVER_ROOT . '/'.$appname.'/images';
888
889                        if ($this->is_image_dir ($imagedir))
890                        {
891                                return $GLOBALS['phpgw_info']['server']['webserver_url'].'/'.$appname.'/templates/'.$GLOBALS['phpgw_info']['server']['template_set'].'/images';
892                        }
893                        elseif ($this->is_image_dir ($imagedir_default))
894                        {
895                                return $GLOBALS['phpgw_info']['server']['webserver_url'].'/'.$appname.'/templates/default/images';
896                        }
897                        elseif ($this->is_image_dir ($imagedir_olddefault))
898                        {
899                                return $GLOBALS['phpgw_info']['server']['webserver_url'].'/'.$appname.'/images';
900                        }
901                        else
902                        {
903                                return False;
904                        }
905                }
906
907                function find_image($appname,$image)
908                {
909                        $imagedir = '/'.$appname.'/templates/'.$GLOBALS['phpgw_info']['user']['preferences']['common']['template_set'].'/images';
910                       
911                        if (!@is_array($this->found_files[$appname]))
912                        {
913                                $imagedir_olddefault = '/'.$appname.'/images';
914                                $imagedir_default    = '/'.$appname.'/templates/default/images';
915                               
916                                if (@is_dir(PHPGW_INCLUDE_ROOT.$imagedir_olddefault))
917                                {
918                                        $d = dir(PHPGW_INCLUDE_ROOT.$imagedir_olddefault);
919                                        while (false != ($entry = $d->read()))
920                                        {
921                                                if ($entry != '.' && $entry != '..')
922                                                {
923                                                        $this->found_files[$appname][$entry] = $imagedir_olddefault;
924                                                }
925                                        }
926                                        $d->close();
927                                }
928
929                                if (@is_dir(PHPGW_INCLUDE_ROOT.$imagedir_default))
930                                {
931                                        $d = dir(PHPGW_INCLUDE_ROOT.$imagedir_default);
932                                        while (false != ($entry = $d->read()))
933                                        {
934                                                if ($entry != '.' && $entry != '..')
935                                                {
936                                                        $this->found_files[$appname][$entry] = $imagedir_default;
937                                                }
938                                        }
939                                        $d->close();
940                                }
941
942                                if (@is_dir(PHPGW_INCLUDE_ROOT.$imagedir))
943                                {
944                                        $d = dir(PHPGW_INCLUDE_ROOT.$imagedir);
945                                        while (false != ($entry = $d->read()))
946                                        {
947                                                if ($entry != '.' && $entry != '..')
948                                                {
949                                                        $this->found_files[$appname][$entry] = $imagedir;
950                                                }
951                                        }
952                                        $d->close();
953                                }
954                        }
955                       
956                        if (!$GLOBALS['phpgw_info']['server']['image_type'])
957                        {
958                                // priority: GIF->JPG->PNG
959                                $img_type=array('.gif','.jpg','.png');
960                        }
961                        else
962                        {
963                                // priority: : PNG->JPG->GIF
964                                $img_type=array('.png','.jpg','.gif');
965                        }
966
967                        // first look in the selected template dir
968                        if(@$this->found_files[$appname][$image.$img_type[0]]==$imagedir)
969                        {
970                                $imgfile = $GLOBALS['phpgw_info']['server']['webserver_url'].$this->found_files[$appname][$image.$img_type[0]].'/'.$image.$img_type[0];
971                        }
972                        elseif(@$this->found_files[$appname][$image.$img_type[1]]==$imagedir)
973                        {
974                                $imgfile = $GLOBALS['phpgw_info']['server']['webserver_url'].$this->found_files[$appname][$image.$img_type[1]].'/'.$image.$img_type[1];
975                        }
976                        elseif(@$this->found_files[$appname][$image.$img_type[2]]==$imagedir)
977                        {
978                                $imgfile = $GLOBALS['phpgw_info']['server']['webserver_url'].$this->found_files[$appname][$image.$img_type[2]].'/'.$image.$img_type[2];
979                        }
980                        // then look everywhere else
981                        elseif(isset($this->found_files[$appname][$image.$img_type[0]]))
982                        {
983                                $imgfile = $GLOBALS['phpgw_info']['server']['webserver_url'].$this->found_files[$appname][$image.$img_type[0]].'/'.$image.$img_type[0];
984                        }
985                        elseif(isset($this->found_files[$appname][$image.$img_type[1]]))
986                        {
987                                $imgfile = $GLOBALS['phpgw_info']['server']['webserver_url'].$this->found_files[$appname][$image.$img_type[1]].'/'.$image.$img_type[1];
988                        }
989                        elseif(isset($this->found_files[$appname][$image.$img_type[2]]))
990                        {
991                                $imgfile = $GLOBALS['phpgw_info']['server']['webserver_url'].$this->found_files[$appname][$image.$img_type[2]].'/'.$image.$img_type[2];
992                        }
993                        elseif(isset($this->found_files[$appname][$image]))
994                        {
995                                $imgfile = $GLOBALS['phpgw_info']['server']['webserver_url'].$this->found_files[$appname][$image].'/'.$image;
996                        }
997                        else
998                        {
999                                // searching the image in the api-dirs
1000                                if (!isset($this->found_files['phpgwapi']))
1001                                {
1002                                        $this->find_image('phpgwapi','');
1003                                }
1004
1005                                if(isset($this->found_files['phpgwapi'][$image.$img_type[0]]))
1006                                {
1007                                        $imgfile = $GLOBALS['phpgw_info']['server']['webserver_url'].$this->found_files['phpgwapi'][$image.$img_type[0]].'/'.$image.$img_type[0];
1008                                }
1009                                elseif(isset($this->found_files['phpgwapi'][$image.$img_type[1]]))
1010                                {
1011                                        $imgfile = $GLOBALS['phpgw_info']['server']['webserver_url'].$this->found_files['phpgwapi'][$image.$img_type[1]].'/'.$image.$img_type[1];
1012                                }
1013                                elseif(isset($this->found_files['phpgwapi'][$image.$img_type[2]]))
1014                                {
1015                                        $imgfile = $GLOBALS['phpgw_info']['server']['webserver_url'].$this->found_files['phpgwapi'][$image.$img_type[2]].'/'.$image.$img_type[2];
1016                                }
1017                                elseif(isset($this->found_files['phpgwapi'][$image]))
1018                                {
1019                                        $imgfile = $GLOBALS['phpgw_info']['server']['webserver_url'].$this->found_files['phpgwapi'][$image].'/'.$image;
1020                                }
1021                                else
1022                                {
1023                                        $imgfile = '';
1024                                }
1025                        }
1026                        return $imgfile;
1027                }
1028
1029                function image($appname,$image='',$ext='',$use_lang=True)
1030                {
1031                        if (!is_array($image))
1032                        {
1033                                if (empty($image))
1034                                {
1035                                        return '';
1036                                }
1037                                $image = array($image);
1038                        }
1039                        if ($use_lang)
1040                        {
1041                                while (list(,$img) = each($image))
1042                                {
1043                                        $lang_images[] = $img . '_' . $GLOBALS['phpgw_info']['user']['preferences']['common']['lang'];
1044                                        $lang_images[] = $img;
1045                                }
1046                                $image = $lang_images;
1047                        }
1048                        while (empty($image_found) && list(,$img) = each($image))
1049                        {
1050                                if(isset($this->found_files[$appname][$img.$ext]))
1051                                {
1052                                        $image_found = $GLOBALS['phpgw_info']['server']['webserver_url'].$this->found_files[$appname][$img.$ext].'/'.$img.$ext;
1053                                }
1054                                else
1055                                {
1056                                        $image_found = $this->find_image($appname,$img.$ext);
1057                                }
1058                        }
1059                        return $image_found;
1060                }
1061
1062                function image_on($appname,$image,$extension='_on')
1063                {
1064                        $with_extension = $this->image($appname,$image,$extension);
1065                        $without_extension = $this->image($appname,$image);
1066                        if($with_extension != '')
1067                        {
1068                                return $with_extension;
1069                        }
1070                        elseif($without_extension != '')
1071                        {
1072                                return $without_extension;
1073                        }
1074                        else
1075                        {
1076                                return '';
1077                        }
1078                }
1079
1080                /*!
1081                @function navbar
1082                @abstract none yet
1083                @discussion *someone wanna add some detail here*
1084                */
1085                function navbar()
1086                {
1087                        $GLOBALS['phpgw_info']['navbar']['home']['title'] = 'Home';
1088                        $GLOBALS['phpgw_info']['navbar']['home']['url']   = $GLOBALS['phpgw']->link('/home.php');
1089                        $GLOBALS['phpgw_info']['navbar']['home']['icon']  = $this->image('phpgwapi',Array('home','nonav'));
1090                        $GLOBALS['phpgw_info']['navbar']['home']['icon_hover']  = $this->image_on('phpgwapi',Array('home','nonav'),'-over');
1091
1092                        list($first) = each($GLOBALS['phpgw_info']['user']['apps']);
1093                        if(is_array($GLOBALS['phpgw_info']['user']['apps']['admin']) && $first != 'admin')
1094                        {
1095                                $newarray['admin'] = $GLOBALS['phpgw_info']['user']['apps']['admin'];
1096                                foreach($GLOBALS['phpgw_info']['user']['apps'] as $index => $value)
1097                                {
1098                                        if($index != 'admin')
1099                                        {
1100                                                $newarray[$index] = $value;
1101                                        }
1102                                }
1103                                $GLOBALS['phpgw_info']['user']['apps'] = $newarray;
1104                                reset($GLOBALS['phpgw_info']['user']['apps']);
1105                        }
1106                        unset($index);
1107                        unset($value);
1108                        unset($newarray);
1109
1110                        foreach($GLOBALS['phpgw_info']['user']['apps'] as $app => $data)
1111                        {
1112                                if (is_long($app))
1113                                {
1114                                        continue;
1115                                }
1116
1117                                if ($app == 'preferences' || $GLOBALS['phpgw_info']['apps'][$app]['status'] != 2 && $GLOBALS['phpgw_info']['apps'][$app]['status'] != 3)
1118                                {
1119                                        $GLOBALS['phpgw_info']['navbar'][$app]['title'] = $GLOBALS['phpgw_info']['apps'][$app]['title'];
1120                                        $GLOBALS['phpgw_info']['navbar'][$app]['url']   = $GLOBALS['phpgw']->link('/' . $app . '/index.php',$GLOBALS['phpgw_info']['flags']['params'][$app]);
1121                                        $GLOBALS['phpgw_info']['navbar'][$app]['name']  = $app;
1122
1123                                        // create popup target
1124                                        if ($data['status'] == 4)
1125                                        {
1126                                                $GLOBALS['phpgw_info']['navbar'][$app]['target'] = ' target="'.$app.'" onClick="'."if (this != '') { window.open(this+'".
1127                                                        (strstr($GLOBALS['phpgw_info']['navbar'][$app]['url'],'?') ||
1128                                                        ini_get('session.use_trans_sid') && $GLOBALS['phpgw_info']['server']['sessions_type'] == 'php4' ?'&':'?').
1129                                                        "referer='+encodeURI(location),this.target,'width=800,height=600,scrollbars=yes,resizable=yes'); return false; } else { return true; }".'"';
1130                                        }
1131
1132                                        if ($app != $GLOBALS['phpgw_info']['flags']['currentapp'])
1133                                        {
1134                                                $GLOBALS['phpgw_info']['navbar'][$app]['icon']  = $this->image($app,Array('navbar','nonav'));
1135                                                $GLOBALS['phpgw_info']['navbar'][$app]['icon_hover']  = $this->image_on($app,Array('navbar','nonav'),'-over');
1136                                        }
1137                                        else
1138                                        {
1139                                                $GLOBALS['phpgw_info']['navbar'][$app]['icon']  = $this->image_on($app,Array('navbar','nonav'),'-over');
1140                                                $GLOBALS['phpgw_info']['navbar'][$app]['icon_hover']  = $this->image($app,Array('navbar','nonav'));
1141                                        }
1142
1143//                                      if($GLOBALS['phpgw_info']['navbar'][$app]['icon'] == '')
1144//                                      {
1145//                                              $GLOBALS['phpgw_info']['navbar'][$app]['icon']  = $this->image('phpgwapi','nonav');
1146//                                      }
1147                                }
1148                        }
1149                        if ($GLOBALS['phpgw_info']['flags']['currentapp'] == 'home' || $GLOBALS['phpgw_info']['flags']['currentapp'] == 'preferences' || $GLOBALS['phpgw_info']['flags']['currentapp'] == 'about')
1150                        {
1151                                $app = $app_title = 'eGroupWare';
1152                        }
1153                        else
1154                        {
1155                                $app = $GLOBALS['phpgw_info']['flags']['currentapp'];
1156                                $app_title = $GLOBALS['phpgw_info']['apps'][$app]['title'];
1157                        }
1158
1159                        if ($GLOBALS['phpgw_info']['user']['apps']['preferences'])      // preferences last
1160                        {
1161                                $prefs = $GLOBALS['phpgw_info']['navbar']['preferences'];
1162                                unset($GLOBALS['phpgw_info']['navbar']['preferences']);
1163                                $GLOBALS['phpgw_info']['navbar']['preferences'] = $prefs;
1164                        }
1165
1166                        // We handle this here becuase its special
1167                        $GLOBALS['phpgw_info']['navbar']['about']['title'] = lang('About %1',$app_title);
1168
1169                        $GLOBALS['phpgw_info']['navbar']['about']['url']   = $GLOBALS['phpgw']->link('/about.php','app='.$app);
1170                        $GLOBALS['phpgw_info']['navbar']['about']['icon']  = $this->image('phpgwapi',Array('about','nonav'));
1171                        $GLOBALS['phpgw_info']['navbar']['about']['icon_hover']  = $this->image_on('phpgwapi',Array('about','nonav'),'-over');
1172
1173                        $GLOBALS['phpgw_info']['navbar']['logout']['title'] = lang('Logout');
1174                        $GLOBALS['phpgw_info']['navbar']['logout']['url']   = $GLOBALS['phpgw']->link('/logout.php');
1175                        $GLOBALS['phpgw_info']['navbar']['logout']['icon']  = $this->image('phpgwapi',Array('logout','nonav'));
1176                        $GLOBALS['phpgw_info']['navbar']['logout']['icon_hover']  = $this->image_on('phpgwapi',Array('logout','nonav'),'-over');
1177                }
1178
1179                /*!
1180                @function app_header
1181                @abstract load header.inc.php for an application
1182                */
1183                function app_header()
1184                {
1185                        if (file_exists(PHPGW_APP_INC . '/header.inc.php'))
1186                        {
1187                                include(PHPGW_APP_INC . '/header.inc.php');
1188                        }
1189                }
1190                /*!
1191                @function phpgw_header
1192                @abstract load the phpgw header
1193                */
1194                function phpgw_header()
1195                {
1196                        // add a content-type header to overwrite an existing default charset in apache (AddDefaultCharset directiv)
1197                        header('Content-type: text/html; charset='.$GLOBALS['phpgw']->translation->charset());
1198                        include(PHPGW_INCLUDE_ROOT . '/phpgwapi/templates/' . $GLOBALS['phpgw_info']['server']['template_set']
1199                                . '/head.inc.php');
1200                        $this->navbar(False);
1201                        include(PHPGW_INCLUDE_ROOT . '/phpgwapi/templates/' . $GLOBALS['phpgw_info']['server']['template_set']
1202                                . '/navbar.inc.php');
1203                        if (!@$GLOBALS['phpgw_info']['flags']['nonavbar'] && !@$GLOBALS['phpgw_info']['flags']['navbar_target'])
1204                        {
1205                                echo parse_navbar();
1206                        }
1207                }
1208
1209                function phpgw_footer()
1210                {
1211                        if (!defined('PHPGW_FOOTER'))
1212                        {
1213                                define('PHPGW_FOOTER',True);
1214                                if (!isset($GLOBALS['phpgw_info']['flags']['nofooter']) || !$GLOBALS['phpgw_info']['flags']['nofooter'])
1215                                {
1216                                        include(PHPGW_API_INC . '/footer.inc.php');
1217                                }
1218                        }
1219                }
1220
1221                /**
1222                * Used by template headers for including CSS in the header
1223                *
1224                * This first loads up the basic global CSS definitions, which support
1225                * the selected user theme colors.  Next we load up the app CSS.  This is
1226                * all merged into the selected theme's css.tpl file.
1227                *
1228                * @author Dave Hall (*based* on verdilak? css inclusion code)
1229                */
1230                function get_css()
1231                {
1232                        $tpl = createObject('phpgwapi.Template', $this->get_tpl_dir('phpgwapi'));
1233                        $tpl->set_file('css', 'css.tpl');
1234                        $tpl->set_var($GLOBALS['phpgw_info']['theme']);
1235                        $app_css = '';
1236                        if(@isset($_GET['menuaction']))
1237                        {
1238                                list($app,$class,$method) = explode('.',$_GET['menuaction']);
1239                                if(is_array($GLOBALS[$class]->public_functions) &&
1240                                        $GLOBALS[$class]->public_functions['css'])
1241                                {
1242                                        $app_css .= $GLOBALS[$class]->css();
1243                                }
1244                        }
1245                        if (isset($GLOBALS['phpgw_info']['flags']['css']))
1246                        {
1247                                $app_css .= $GLOBALS['phpgw_info']['flags']['css'];
1248                        }
1249                        $tpl->set_var('app_css', $app_css);
1250
1251                        // search for app specific css file
1252                        if(@isset($GLOBALS['phpgw_info']['flags']['currentapp']))
1253                        {
1254                                $appname = $GLOBALS['phpgw_info']['flags']['currentapp'];
1255
1256                                if(file_exists(PHPGW_SERVER_ROOT . SEP . $appname . SEP
1257                                        . 'templates' . SEP . $GLOBALS['phpgw_info']['server']['template_set']
1258                                        . SEP . 'app.css')
1259                                )
1260                                {
1261                                        $tpl->set_var('css_file', '<LINK href="'.$GLOBALS['phpgw_info']['server']['webserver_url']
1262                                                . "/$appname/templates/".$GLOBALS['phpgw_info']['server']['template_set']
1263                                                . "/app.css".'" type=text/css rel=StyleSheet>');
1264                                }
1265                                elseif(file_exists(PHPGW_SERVER_ROOT . SEP . $appname . SEP
1266                                        . 'templates' . SEP . 'default'
1267                                        . SEP . 'app.css')
1268                                )
1269                                {
1270                                        $tpl->set_var('css_file', '<LINK href="'.$GLOBALS['phpgw_info']['server']['webserver_url']
1271                                        ."/$appname/templates/default/app.css".'" type=text/css rel=StyleSheet>');
1272                                }
1273                        }
1274
1275                        return $tpl->subst('css');
1276                }
1277
1278                /**
1279                * Used by the template headers for including javascript in the header
1280                *
1281                * The method is included here to make it easier to change the js support
1282                * in phpgw.  One change then all templates will support it (as long as they
1283                * include a call to this method).
1284                *
1285                * @author Dave Hall (*vaguely based* on verdilak? css inclusion code)
1286                * @return string the javascript to be included
1287                */
1288                function get_java_script()
1289                {
1290                        $java_script = '';
1291
1292                        /* this flag is for all javascript code that has to be put before other jscode.
1293                        Think of conf vars etc...  (pim@lingewoud.nl) */
1294                        if (isset($GLOBALS['phpgw_info']['flags']['java_script_thirst']))
1295                        {
1296                                $java_script .= $GLOBALS['phpgw_info']['flags']['java_script_thirst'] . "\n";
1297                        }
1298                       
1299                        if(@is_object($GLOBALS['phpgw']->js))
1300                        {
1301                                $java_script .= $GLOBALS['phpgw']->js->get_script_links();
1302                        }
1303
1304                        if(@isset($_GET['menuaction']))
1305                        {
1306                                list($app,$class,$method) = explode('.',$_GET['menuaction']);
1307                                if(is_array($GLOBALS[$class]->public_functions) &&
1308                                        $GLOBALS[$class]->public_functions['java_script'])
1309                                {
1310                                        $java_script .= $GLOBALS[$class]->java_script();
1311                                }
1312                        }
1313                        if (isset($GLOBALS['phpgw_info']['flags']['java_script']))
1314                        {
1315                                $java_script .= $GLOBALS['phpgw_info']['flags']['java_script'] . "\n";
1316                        }
1317                        return $java_script;
1318                }
1319
1320                /**
1321                * Returns on(Un)Load attributes from js class
1322                *
1323                *@author Dave Hall - skwashd at phpgroupware.org
1324                *@returns string body attributes
1325                */
1326                function get_body_attribs()
1327                {
1328                        if(@is_object($GLOBALS['phpgw']->js))
1329                        {
1330                                return $GLOBALS['phpgw']->js->get_body_attribs();
1331                        }
1332                        else
1333                        {
1334                                return '';
1335                        }
1336                }
1337
1338                function hex2bin($data)
1339                {
1340                        $len = strlen($data);
1341                        return @pack('H' . $len, $data);
1342                }
1343
1344                /*!
1345                @function encrypt
1346                @abstract encrypt data passed to the function
1347                @param $data data (string?) to be encrypted
1348                */
1349                function encrypt($data)
1350                {
1351                        return $GLOBALS['phpgw']->crypto->encrypt($data);
1352                }
1353
1354                /*!
1355                @function decrypt
1356                @abstract decrypt $data
1357                @param $data data to be decrypted
1358                */
1359                function decrypt($data)
1360                {
1361                        return $GLOBALS['phpgw']->crypto->decrypt($data);
1362                }
1363
1364                /*!
1365                @function encrypt_password
1366                @abstract legacy wrapper for newer auth class function, encrypt_password
1367                @abstract uses the encryption type set in setup and calls the appropriate encryption functions
1368                @param $password password to encrypt
1369                */
1370                function encrypt_password($password,$sql=False)
1371                {
1372                        if(!@is_object($GLOBALS['phpgw']->auth))
1373                        {
1374                                $GLOBALS['phpgw']->auth = CreateObject('phpgwapi.auth');
1375                        }
1376                        return $GLOBALS['phpgw']->auth->encrypt_password($password,$sql);
1377                }
1378
1379                /*!
1380                @function find_portal_order
1381                @abstract find the current position of the app is the users portal_order preference
1382                @param $app application id to find current position - required
1383                @discussion No discussion
1384                */
1385                function find_portal_order($app)
1386                {
1387                        if(!is_array($GLOBALS['phpgw_info']['user']['preferences']['portal_order']))
1388                        {
1389                                return -1;
1390                        }
1391                        @reset($GLOBALS['phpgw_info']['user']['preferences']['portal_order']);
1392                        while(list($seq,$appid) = each($GLOBALS['phpgw_info']['user']['preferences']['portal_order']))
1393                        {
1394                                if($appid == $app)
1395                                {
1396                                        @reset($GLOBALS['phpgw_info']['user']['preferences']['portal_order']);
1397                                        return $seq;
1398                                }
1399                        }
1400                        @reset($GLOBALS['phpgw_info']['user']['preferences']['portal_order']);
1401                        return -1;
1402                }
1403
1404                /*!
1405                @function hook
1406                @abstract temp wrapper to new hooks class
1407                */
1408                function hook($location, $appname = '', $no_permission_check = False)
1409                {
1410                        echo '$'."GLOBALS['phpgw']common->hook()".' has been replaced. Please change to the new $'."GLOBALS['phpgw']hooks->process()".'. For now this will act as a wrapper<br>';
1411                        return $GLOBALS['phpgw']->hooks->process($location, $order, $no_permission_check);
1412                }
1413
1414                /*!
1415                @function hook_single
1416                @abstract temp wrapper to new hooks class
1417                */
1418                // Note: $no_permission_check should *ONLY* be used when it *HAS* to be. (jengo)
1419                function hook_single($location, $appname = '', $no_permission_check = False)
1420                {
1421                        echo '$'."GLOBALS['phpgw']common->hook_single()".' has been replaced. Please change to the new $'."GLOBALS['phpgw']hooks->single()".'. For now this will act as a wrapper<br>';
1422                        return $GLOBALS['phpgw']->hooks->single($location, $order, $no_permission_check);
1423                }
1424
1425                /*!
1426                @function hook_count
1427                @abstract temp wrapper to new hooks class
1428                */
1429                function hook_count($location)
1430                {
1431                        echo '$'."GLOBALS['phpgw']common->hook_count()".' has been replaced. Please change to the new $'."GLOBALS['phpgw']hooks->count()".'. For now this will act as a wrapper<br>';
1432                        return $GLOBALS['phpgw']->hooks->count($location);
1433                }
1434
1435                /* Wrapper to the session->appsession() */
1436                function appsession($data = '##NOTHING##')
1437                {
1438                        $this->debug_info[] = '$phpgw->common->appsession() is a depreciated function'
1439                                . ' - use $phpgw->session->appsession() instead';
1440
1441                        return $GLOBALS['phpgw']->session->appsession('default','',$data);
1442                }
1443
1444                /*!
1445                @function show_date
1446                @abstract show current date
1447                @param $t time - optional can be pulled from user preferences
1448                @param $format - optional can be pulled from user prefernces
1449                */
1450                function show_date($t = '', $format = '')
1451                {
1452                        if(!is_object($GLOBALS['phpgw']->datetime))
1453                        {
1454                                $GLOBALS['phpgw']->datetime = createobject('phpgwapi.date_time');
1455                        }
1456
1457                        if (!$t || (int)$t <= 0)
1458                        {
1459                                $t = $GLOBALS['phpgw']->datetime->gmtnow;
1460                        }
1461
1462                        //  + (date('I') == 1?3600:0)
1463                        $t += $GLOBALS['phpgw']->datetime->tz_offset;
1464
1465                        if (! $format)
1466                        {
1467                                $format = $GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'] . ' - ';
1468                                if ($GLOBALS['phpgw_info']['user']['preferences']['common']['timeformat'] == '12')
1469                                {
1470                                        $format .= 'h:i a';
1471                                }
1472                                else
1473                                {
1474                                        $format .= 'H:i';
1475                                }
1476                        }
1477                        if((PHP_OS == 'Windows' || PHP_OS == 'WINNT') && (int)$t < 21600)
1478                        /*if(PHP_OS == 'Windows' && (int)$t < 21600)*/
1479                        {
1480                                $t = 21600;
1481                        }
1482                        return date($format,$t);
1483                }
1484
1485                /*!
1486                @function dateformatorder
1487                @abstract
1488                @param $yearstr year - string
1489                @param $monthstr month - string
1490                @param $day day - string
1491                @param $add_seperator boolean defaults to false
1492                */
1493                function dateformatorder($yearstr,$monthstr,$daystr,$add_seperator = False)
1494                {
1495                        $dateformat = strtolower($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat']);
1496                        $sep = substr($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'],1,1);
1497
1498                        $dlarr[strpos($dateformat,'y')] = $yearstr;
1499                        $dlarr[strpos($dateformat,'m')] = $monthstr;
1500                        $dlarr[strpos($dateformat,'d')] = $daystr;
1501                        ksort($dlarr);
1502
1503                        if ($add_seperator)
1504                        {
1505                                return (implode($sep,$dlarr));
1506                        }
1507                        else
1508                        {
1509                                return (implode(' ',$dlarr));
1510                        }
1511                }
1512
1513                /*!
1514                @function formattime
1515                @abstract format the time takes settings from user preferences
1516                @param $hour hour
1517                @param $min minutes
1518                @param $sec defaults to ''
1519                */
1520                function formattime($hour,$min,$sec='')
1521                {
1522                        $h12 = $hour;
1523                        if ($GLOBALS['phpgw_info']['user']['preferences']['common']['timeformat'] == '12')
1524                        {
1525                                if ($hour >= 12)
1526                                {
1527                                        $ampm = ' pm';
1528                                }
1529                                else
1530                                {
1531                                        $ampm = ' am';
1532                                }
1533
1534                                $h12 %= 12;
1535
1536                                if ($h12 == 0 && $hour)
1537                                {
1538                                        $h12 = 12;
1539                                }
1540                                if ($h12 == 0 && !$hour)
1541                                {
1542                                        $h12 = 0;
1543                                }
1544                        }
1545                        else
1546                        {
1547                                $h12 = $hour;
1548                        }
1549
1550                        if ($sec !== '')
1551                        {
1552                                $sec = ":$sec";
1553                        }
1554
1555                        return "$h12:$min$sec$ampm";
1556                }
1557
1558                // This is not the best place for it, but it needs to be shared bewteen Aeromail and SM
1559                /*!
1560                @function get_email_passwd_ex
1561                @abstract uses code in /email class msg to obtain the appropriate password for email
1562                @param  (none - it will abtain the info it needs on its own)
1563                */
1564                /*
1565                function get_email_passwd_ex()
1566                {
1567                        // ----  Create the email Message Class  if needed  -----
1568                        if (is_object($GLOBALS['phpgw']->msg))
1569                        {
1570                                $do_free_me = False;
1571                        }
1572                        else
1573                        {
1574                                $GLOBALS['phpgw']->msg = CreateObject('email.mail_msg');
1575                                $do_free_me = True;
1576                        }
1577                        // use the Msg class to obtain the appropriate password
1578                        $tmp_prefs = $GLOBALS['phpgw']->preferences->read();
1579                        if (!isset($tmp_prefs['email']['passwd']))
1580                        {
1581                                $email_passwd = $GLOBALS['phpgw_info']['user']['passwd'];
1582                        }
1583                        else
1584                        {
1585                                $email_passwd = $GLOBALS['phpgw']->msg->decrypt_email_passwd($tmp_prefs['email']['passwd']);
1586                        }
1587                        // cleanup and return
1588                        if ($do_free_me)
1589                        {
1590                                unset ($GLOBALS['phpgw']->msg);
1591                        }
1592                        return $email_passwd;
1593                }
1594                */
1595
1596                // This is not the best place for it, but it needs to be shared bewteen Aeromail and SM
1597                /*!
1598                @function create_emailpreferences
1599                @abstract create email preferences
1600                @discussion This is not the best place for it, but it needs to be shared between Aeromail and SM
1601                @param $prefs
1602                @param $account_id -optional defaults to : phpgw_info['user']['account_id']
1603                */
1604                function create_emailpreferences($prefs='',$accountid='')
1605                {
1606                        return $GLOBALS['phpgw']->preferences->create_email_preferences($accountid);
1607                        // ----  Create the email Message Class  if needed  -----
1608                        if (is_object($GLOBALS['phpgw']->msg))
1609                        {
1610                                $do_free_me = False;
1611                        }
1612                        else
1613                        {
1614                                $GLOBALS['phpgw']->msg = CreateObject('email.mail_msg');
1615                                $do_free_me = True;
1616                        }
1617
1618                        // this sets the preferences into the phpgw_info structure
1619                        $GLOBALS['phpgw']->msg->create_email_preferences();
1620
1621                        // cleanup and return
1622                        if ($do_free_me)
1623                        {
1624                                unset ($GLOBALS['phpgw']->msg);
1625                        }
1626                }
1627
1628                /*
1629                function create_emailpreferences($prefs,$accountid='')
1630                {
1631                        $account_id = get_account_id($accountid);
1632
1633                        // NEW EMAIL PASSWD METHOD (shared between SM and aeromail)
1634                        $prefs['email']['passwd'] = $this->get_email_passwd_ex();
1635
1636                        // Add default preferences info
1637                        if (!isset($prefs['email']['userid']))
1638                        {
1639                                if ($GLOBALS['phpgw_info']['server']['mail_login_type'] == 'vmailmgr')
1640                                {
1641                                        $prefs['email']['userid'] = $GLOBALS['phpgw']->accounts->id2name($account_id)
1642                                                . '@' . $GLOBALS['phpgw_info']['server']['mail_suffix'];
1643                                }
1644                                else
1645                                {
1646                                        $prefs['email']['userid'] = $GLOBALS['phpgw']->accounts->id2name($account_id);
1647                                }
1648                        }
1649                        // Set Server Mail Type if not defined
1650                        if (empty($GLOBALS['phpgw_info']['server']['mail_server_type']))
1651                        {
1652                                $GLOBALS['phpgw_info']['server']['mail_server_type'] = 'imap';
1653                        }
1654
1655                        // OLD EMAIL PASSWD METHOD
1656                        if (!isset($prefs['email']['passwd']))
1657                        {
1658                                $prefs['email']['passwd'] = $GLOBALS['phpgw_info']['user']['passwd'];
1659                        }
1660                        else
1661                        {
1662                                $prefs['email']['passwd'] = $this->decrypt($prefs['email']['passwd']);
1663                        }
1664                        // NEW EMAIL PASSWD METHOD Located at the begining of this function
1665
1666                        if (!isset($prefs['email']['address']))
1667                        {
1668                                $prefs['email']['address'] = $GLOBALS['phpgw']->accounts->id2name($account_id)
1669                                        . '@' . $GLOBALS['phpgw_info']['server']['mail_suffix'];
1670                        }
1671                        if (!isset($prefs['email']['mail_server']))
1672                        {
1673                                $prefs['email']['mail_server'] = $GLOBALS['phpgw_info']['server']['mail_server'];
1674                        }
1675                        if (!isset($prefs['email']['mail_server_type']))
1676                        {
1677                                $prefs['email']['mail_server_type'] = $GLOBALS['phpgw_info']['server']['mail_server_type'];
1678                        }
1679                        if (!isset($prefs['email']['imap_server_type']))
1680                        {
1681                                $prefs['email']['imap_server_type'] = $GLOBALS['phpgw_info']['server']['imap_server_type'];
1682                        }
1683                        // These sets the mail_port server variable
1684                        if ($prefs['email']['mail_server_type']=='imap')
1685                        {
1686                                $prefs['email']['mail_port'] = '143';
1687                        }
1688                        elseif ($prefs['email']['mail_server_type']=='pop3')
1689                        {
1690                                $prefs['email']['mail_port'] = '110';
1691                        }
1692                        elseif ($prefs['email']['mail_server_type']=='imaps')
1693                        {
1694                                $prefs['email']['mail_port'] = '993';
1695                        }
1696                        elseif ($prefs['email']['mail_server_type']=='pop3s')
1697                        {
1698                                $prefs['email']['mail_port'] = '995';
1699                        }
1700                        // This is going to be used to switch to the nntp class
1701                        if (isset($phpgw_info['flags']['newsmode']) &&
1702                                $GLOBALS['phpgw_info']['flags']['newsmode'])
1703                        {
1704                                $prefs['email']['mail_server_type'] = 'nntp';
1705                        }
1706                        // DEBUG
1707                        //echo "<br>prefs['email']['passwd']: " .$prefs['email']['passwd'] .'<br>';
1708                        return $prefs;
1709                }
1710                */
1711
1712                // This will be moved into the applications area.
1713                /*!
1714                @function check_code
1715                @abstract ?
1716                @discussion This will be moved into the applications area
1717                */
1718                function check_code($code)
1719                {
1720                        $s = '<br>';
1721                        switch ($code)
1722                        {
1723                                case 13:        $s .= lang('Your message has been sent');break;
1724                                case 14:        $s .= lang('New entry added sucessfully');break;
1725                                case 15:        $s .= lang('Entry updated sucessfully');        break;
1726                                case 16:        $s .= lang('Entry has been deleted sucessfully'); break;
1727                                case 18:        $s .= lang('Password has been updated');        break;
1728                                case 38:        $s .= lang('Password could not be changed');    break;
1729                                case 19:        $s .= lang('Session has been killed');  break;
1730                                case 27:        $s .= lang('Account has been updated'); break;
1731                                case 28:        $s .= lang('Account has been created'); break;
1732                                case 29:        $s .= lang('Account has been deleted'); break;
1733                                case 30:        $s .= lang('Your settings have been updated'); break;
1734                                case 31:        $s .= lang('Group has been added');     break;
1735                                case 32:        $s .= lang('Group has been deleted');   break;
1736                                case 33:        $s .= lang('Group has been updated');   break;
1737                                case 34:        $s .= lang('Account has been deleted') . '<p>'
1738                                                . lang('Error deleting %1 %2 directory',lang('users'),' '.lang('private').' ')
1739                                                . ',<br>' . lang('Please %1 by hand',lang('delete')) . '<br><br>'
1740                                                . lang('To correct this error for the future you will need to properly set the')
1741                                                . '<br>' . lang('permissions to the files/users directory')
1742                                                . '<br>' . lang('On *nix systems please type: %1','chmod 770 '
1743                                                . $GLOBALS['phpgw_info']['server']['files_dir'] . '/users/');
1744                                        break;
1745                                case 35:        $s .= lang('Account has been updated') . '<p>'
1746                                                . lang('Error renaming %1 %2 directory',lang('users'),
1747                                                ' '.lang('private').' ')
1748                                                . ',<br>' . lang('Please %1 by hand',
1749                                                lang('rename')) . '<br><br>'
1750                                                . lang('To correct this error for the future you will need to properly set the')
1751                                                . '<br>' . lang('permissions to the files/users directory')
1752                                                . '<br>' . lang('On *nix systems please type: %1','chmod 770 '
1753                                                . $GLOBALS['phpgw_info']['server']['files_dir'] . '/users/');
1754                                        break;
1755                                case 36:        $s .= lang('Account has been created') . '<p>'
1756                                                . lang('Error creating %1 %2 directory',lang('users'),
1757                                                ' '.lang('private').' ')
1758                                                . ',<br>' . lang('Please %1 by hand',
1759                                                lang('create')) . '<br><br>'
1760                                                . lang('To correct this error for the future you will need to properly set the')
1761                                                . '<br>' . lang('permissions to the files/users directory')
1762                                                . '<br>' . lang('On *nix systems please type: %1','chmod 770 '
1763                                                . $GLOBALS['phpgw_info']['server']['files_dir'] . '/users/');
1764                                        break;
1765                                case 37:        $s .= lang('Group has been added') . '<p>'
1766                                                . lang('Error creating %1 %2 directory',lang('groups'),' ')
1767                                                . ',<br>' . lang('Please %1 by hand',
1768                                                lang('create')) . '<br><br>'
1769                                                . lang('To correct this error for the future you will need to properly set the')
1770                                                . '<br>' . lang('permissions to the files/users directory')
1771                                                . '<br>' . lang('On *nix systems please type: %1','chmod 770 '
1772                                                . $GLOBALS['phpgw_info']['server']['files_dir'] . '/groups/');
1773                                        break;
1774                                case 38:        $s .= lang('Group has been deleted') . '<p>'
1775                                                . lang('Error deleting %1 %2 directory',lang('groups'),' ')
1776                                                . ',<br>' . lang('Please %1 by hand',
1777                                                lang('delete')) . '<br><br>'
1778                                                . lang('To correct this error for the future you will need to properly set the')
1779                                                . '<br>' . lang('permissions to the files/users directory')
1780                                                . '<br>' . lang('On *nix systems please type: %1','chmod 770 '
1781                                                . $GLOBALS['phpgw_info']['server']['files_dir'] . '/groups/');
1782                                        break;
1783                                case 39:        $s .= lang('Group has been updated') . '<p>'
1784                                                . lang('Error renaming %1 %2 directory',lang('groups'),' ')
1785                                                . ',<br>' . lang('Please %1 by hand',
1786                                                lang('rename')) . '<br><br>'
1787                                                . lang('To correct this error for the future you will need to properly set the')
1788                                                . '<br>' . lang('permissions to the files/users directory')
1789                                                . '<br>' . lang('On *nix systems please type: %1','chmod 770 '
1790                                                . $GLOBALS['phpgw_info']['server']['files_dir'] . '/groups/');
1791                                        break;
1792                                case 40: $s .= lang('You have not entered a title').'.';
1793                                        break;
1794                                case 41: $s .= lang('You have not entered a valid time of day').'.';
1795                                        break;
1796                                case 42: $s .= lang('You have not entered a valid date').'.';
1797                                        break;
1798                                case 43: $s .= lang('You have not entered participants').'.';
1799                                        break;
1800                                default:        return '';
1801                        }
1802                        return $s;
1803                }
1804                /*!
1805                @function phpgw_error
1806                @abstract process error message
1807                @param $error error
1808                @param $line line
1809                @param $file file
1810                */
1811                function phpgw_error($error,$line = '', $file = '')
1812                {
1813                        echo '<p><b>phpGroupWare internal error:</b><p>'.$error;
1814                        if ($line)
1815                        {
1816                                echo 'Line: '.$line;
1817                        }
1818                        if ($file)
1819                        {
1820                                echo 'File: '.$file;
1821                        }
1822                        echo '<p>Your session has been halted.';
1823                        exit;
1824                }
1825
1826                /*!
1827                @function create_phpcode_from_array
1828                @abstract create phpcode from array
1829                @param $array - array
1830                */
1831                function create_phpcode_from_array($array)
1832                {
1833                        while (list($key, $val) = each($array))
1834                        {
1835                                if (is_array($val))
1836                                {
1837                                        while (list($key2, $val2) = each($val))
1838                                        {
1839                                                if (is_array($val2))
1840                                                {
1841                                                        while (list($key3, $val3) = each ($val2))
1842                                                        {
1843                                                                if (is_array($val3))
1844                                                                {
1845                                                                        while (list($key4, $val4) = each ($val3))
1846                                                                        {
1847                                                                                $s .= '$phpgw_info["' . $key . '"]["' . $key2 . '"]["' . $key3 . '"]["' .$key4 . '"]="' . $val4 . '";';
1848                                                                                $s .= "\n";
1849                                                                        }
1850                                                                }
1851                                                                else
1852                                                                {
1853                                                                        $s .= '$phpgw_info["' . $key . '"]["' . $key2 . '"]["' . $key3 . '"]="' . $val3 . '";';
1854                                                                        $s .= "\n";
1855                                                                }
1856                                                        }
1857                                                }
1858                                                else
1859                                                {
1860                                                        $s .= '$phpgw_info["' . $key .'"]["' . $key2 . '"]="' . $val2 . '";';
1861                                                        $s .= "\n";
1862                                                }
1863                                        }
1864                                }
1865                                else
1866                                {
1867                                        $s .= '$phpgw_info["' . $key . '"]="' . $val . '";';
1868                                        $s .= "\n";
1869                                }
1870                        }
1871                        return $s;
1872                }
1873
1874                // This will return the full phpgw_info array, used for debugging
1875                /*!
1876                @function debug_list_array_contents
1877                @abstract return the full phpgw_info array for debugging
1878                @param array - array
1879                */
1880                function debug_list_array_contents($array)
1881                {
1882                        while (list($key, $val) = each($array))
1883                        {
1884                                if (is_array($val))
1885                                {
1886                                        while (list($key2, $val2) = each($val))
1887                                        {
1888                                                if (is_array($val2))
1889                                                {
1890                                                        while (list($key3, $val3) = each ($val2))
1891                                                        {
1892                                                                if (is_array($val3))
1893                                                                {
1894                                                                        while (list($key4, $val4) = each ($val3))
1895                                                                        {
1896                                                                                echo $$array . "[$key][$key2][$key3][$key4]=$val4<br>";
1897                                                                        }
1898                                                                }
1899                                                                else
1900                                                                {
1901                                                                        echo $$array . "[$key][$key2][$key3]=$val3<br>";
1902                                                                }
1903                                                        }
1904                                                }
1905                                                else
1906                                                {
1907                                                        echo $$array . "[$key][$key2]=$val2<br>";
1908                                                }
1909                                        }
1910                                }
1911                                else
1912                                {
1913                                        echo $$array . "[$key]=$val<br>";
1914                                }
1915                        }
1916                }
1917
1918                // This will return a list of functions in the API
1919                /*!
1920                @function debug_list_core_functions
1921                @abstract return a list of functionsin the API
1922                */
1923                function debug_list_core_functions()
1924                {
1925                        echo '<br><b>core functions</b><br>';
1926                        echo '<pre>';
1927                        chdir(PHPGW_INCLUDE_ROOT . '/phpgwapi');
1928                        system("grep -r '^[ \t]*function' *");
1929                        echo '</pre>';
1930                }
1931
1932                // This will return a value for the next id an app/class may need to insert values into ldap.
1933                /*!
1934                @function next_id
1935                @abstract return the next higher value for an integer, and increment it in the db.
1936                */
1937                function next_id($appname,$min=0,$max=0)
1938                {
1939                        if (!$appname)
1940                        {
1941                                return -1;
1942                        }
1943
1944                        $GLOBALS['phpgw']->db->query("SELECT id FROM phpgw_nextid WHERE appname='".$appname."'",__LINE__,__FILE__);
1945                        while( $GLOBALS['phpgw']->db->next_record() )
1946                        {
1947                                $id = $GLOBALS['phpgw']->db->f('id');
1948                        }
1949
1950                        if (empty($id) || !$id)
1951                        {
1952                                $id = 1;
1953                                $GLOBALS['phpgw']->db->query("INSERT INTO phpgw_nextid (appname,id) VALUES ('".$appname."',".$id.")",__LINE__,__FILE__);
1954                        }
1955                        elseif($id<$min)
1956                        {
1957                                $id = $min;
1958                                $GLOBALS['phpgw']->db->query("UPDATE phpgw_nextid SET id=".$id." WHERE appname='".$appname."'",__LINE__,__FILE__);
1959                        }
1960                        elseif ($max && ($id > $max))
1961                        {
1962                                return False;
1963                        }
1964                        else
1965                        {
1966                                $id = $id + 1;
1967                                $GLOBALS['phpgw']->db->query("UPDATE phpgw_nextid SET id=".$id." WHERE appname='".$appname."'",__LINE__,__FILE__);
1968                        }
1969
1970                        return (int)$id;
1971                }
1972
1973                // This will return a value for the last id entered, which an app may need to check
1974                // values for ldap.
1975                /*!
1976                @function last_id
1977                @abstract return the current id in the next_id table for a particular app/class.
1978                */
1979                function last_id($appname,$min=0,$max=0)
1980                {
1981                        if (!$appname)
1982                        {
1983                                return -1;
1984                        }
1985
1986                        $GLOBALS['phpgw']->db->query("SELECT id FROM phpgw_nextid WHERE appname='".$appname."'",__LINE__,__FILE__);
1987                        while( $GLOBALS['phpgw']->db->next_record() )
1988                        {
1989                                $id = $GLOBALS['phpgw']->db->f('id');
1990                        }
1991
1992                        if (empty($id) || !$id)
1993                        {
1994                                if($min)
1995                                {
1996                                        $id = $min;
1997                                }
1998                                else
1999                                {
2000                                        $id = 1;
2001                                }
2002                                $GLOBALS['phpgw']->db->query("INSERT INTO phpgw_nextid (appname,id) VALUES ('".$appname."',".$id.")",__LINE__,__FILE__);
2003                        }
2004                        elseif($id<$min)
2005                        {
2006                                $id = $min;
2007                                $GLOBALS['phpgw']->db->query("UPDATE phpgw_nextid SET id=".$id." WHERE appname='".$appname."'",__LINE__,__FILE__);
2008                        }
2009                        elseif ($max && ($id > $max))
2010                        {
2011                                return False;
2012                        }
2013                        return (int)$id;
2014                }
2015        }//end common class
Note: See TracBrowser for help on using the repository browser.