source: sandbox/2.5.1-evolucao/phpgwapi/inc/class.common.inc.php @ 8221

Revision 8221, 61.2 KB checked in by angelo, 11 years ago (diff)

Ticket #3491 - Compatibilizar Expresso com novas versoes do PHP

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