source: sandbox/expresso/novos_templates/phpgwapi/inc/class.common.inc.php @ 773

Revision 773, 59.4 KB checked in by niltonneto, 15 years ago (diff)

Resolve #475

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