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

Revision 62, 58.7 KB checked in by niltonneto, 17 years ago (diff)

* empty log message *

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