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

Revision 169, 59.0 KB checked in by niltonneto, 16 years ago (diff)

Ticket 145

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