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

Revision 7673, 60.9 KB checked in by douglasz, 11 years ago (diff)

Ticket #3236 - Correcoes para Performance: Function Within Loop Declaration.

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