source: trunk/phpgwapi/inc/functions.inc.php @ 2645

Revision 2645, 19.5 KB checked in by amuller, 14 years ago (diff)

Ticket #1044 - Adicionando suporte no expressoMail

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2         /**************************************************************************\
3         * eGroupWare API - phpgwapi loader                                         *
4         * This file written by Dan Kuykendall <seek3r@phpgroupware.org>            *
5         * and Joseph Engo <jengo@phpgroupware.org>                                 *
6         * Has a few functions, but primary role is to load the phpgwapi            *
7         * Copyright (C) 2000, 2001 Dan Kuykendall                                  *
8         * -------------------------------------------------------------------------*
9         * This library is part of the eGroupWare API                               *
10         * http://www.egroupware.org/api                                            *
11         * ------------------------------------------------------------------------ *
12         * This library is free software; you can redistribute it and/or modify it  *
13         * under the terms of the GNU Lesser General Public License as published by *
14         * the Free Software Foundation; either version 2.1 of the License,         *
15         * or any later version.                                                    *
16         * This library is distributed in the hope that it will be useful, but      *
17         * WITHOUT ANY WARRANTY; without even the implied warranty of               *
18         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     *
19         * See the GNU Lesser General Public License for more details.              *
20         * You should have received a copy of the GNU Lesser General Public License *
21         * along with this library; if not, write to the Free Software Foundation,  *
22         * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA            *
23         \**************************************************************************/
24       
25       
26        /***************************************************************************\
27        * If running in PHP3, then force admin to upgrade                           *
28        \***************************************************************************/
29
30        error_reporting(error_reporting() & ~E_NOTICE);
31
32        include_once(PHPGW_API_INC.'/common_functions.inc.php');
33       
34        /*!
35         @function lang
36         @abstract function to handle multilanguage support
37        */
38        function lang($key,$m1='',$m2='',$m3='',$m4='',$m5='',$m6='',$m7='',$m8='',$m9='',$m10='')
39        {
40                if(is_array($m1))
41                {
42                        $vars = $m1;
43                }
44                else
45                {
46                        $vars = array($m1,$m2,$m3,$m4,$m5,$m6,$m7,$m8,$m9,$m10);
47                }
48                // Get the translation from Lang File, if the database is down.
49                if(!$GLOBALS['phpgw']->translation){
50                        $fn = PHPGW_SERVER_ROOT.'/phpgwapi/setup/phpgw_'.$GLOBALS['_SERVER']['HTTP_ACCEPT_LANGUAGE'].'.lang';
51                        if (file_exists($fn)){
52                                $fp = fopen($fn,'r');
53                                while ($data = fgets($fp,16000)){
54                                        list($message_id,$app_name,$null,$content) = explode("\t",substr($data,0,-1));
55                                        $GLOBALS['phpgw_info']['phpgwapi']['lang'][$message_id] =  $content;
56                                }
57                                fclose($fp);
58                        }
59                        $return = str_replace('%1',$vars[0],$GLOBALS['phpgw_info']['phpgwapi']['lang'][$key]);                 
60                        return $return; 
61                }
62                $value = $GLOBALS['phpgw']->translation->translate("$key",$vars);
63                return $value;
64        }
65
66        /* Make sure the header.inc.php is current. */
67        if ($GLOBALS['phpgw_info']['server']['versions']['header'] < $GLOBALS['phpgw_info']['server']['versions']['current_header'])
68        {
69                header("location:setup/manageheader.php");
70        }
71
72        /* Make sure the developer is following the rules. */
73        if (!isset($GLOBALS['phpgw_info']['flags']['currentapp']))
74        {
75                /* This object does not exist yet. */
76        /*      $GLOBALS['phpgw']->log->write(array('text'=>'W-MissingFlags, currentapp flag not set'));*/
77
78                echo '<b>!!! YOU DO NOT HAVE YOUR $GLOBALS[\'phpgw_info\'][\'flags\'][\'currentapp\'] SET !!!';
79                echo '<br>!!! PLEASE CORRECT THIS SITUATION !!!</b>';
80        }
81
82        magic_quotes_runtime(false);
83        print_debug('sane environment','messageonly','api');
84
85        /****************************************************************************\
86        * Multi-Domain support                                                       *
87        \****************************************************************************/
88       
89        /* make them fix their header */
90        if (!isset($GLOBALS['phpgw_domain']))
91        {
92                echo '<center><b>The administrator must upgrade the header.inc.php file before you can continue.</b></center>';
93                exit;
94        }
95        if (!isset($GLOBALS['phpgw_info']['server']['default_domain']) ||       // allow to overwrite the default domain
96                !isset($GLOBALS['phpgw_domain'][$GLOBALS['phpgw_info']['server']['default_domain']]))
97        {
98                reset($GLOBALS['phpgw_domain']);
99                list($GLOBALS['phpgw_info']['server']['default_domain']) = each($GLOBALS['phpgw_domain']);
100        }
101        if (isset($_POST['login']))     // on login
102        {
103                $GLOBALS['login'] = $_POST['login'];
104                if (strstr($GLOBALS['login'],'@') === False || count($GLOBALS['phpgw_domain']) == 1)
105                {
106                        $GLOBALS['login'] .= '@' . get_var('logindomain',array('POST'),$GLOBALS['phpgw_info']['server']['default_domain']);
107                }
108                $parts = explode('@',$GLOBALS['login']);
109                $GLOBALS['phpgw_info']['user']['domain'] = array_pop($parts);
110        }
111        else    // on "normal" pageview
112        {
113                $GLOBALS['phpgw_info']['user']['domain'] = get_var('domain', array('GET', 'COOKIE'), FALSE);
114        }
115
116        if (@isset($GLOBALS['phpgw_domain'][$GLOBALS['phpgw_info']['user']['domain']]))
117        {
118                $GLOBALS['phpgw_info']['server']['db_host'] = $GLOBALS['phpgw_domain'][$GLOBALS['phpgw_info']['user']['domain']]['db_host'];
119                $GLOBALS['phpgw_info']['server']['db_port'] = $GLOBALS['phpgw_domain'][$GLOBALS['phpgw_info']['user']['domain']]['db_port'];
120                $GLOBALS['phpgw_info']['server']['db_name'] = $GLOBALS['phpgw_domain'][$GLOBALS['phpgw_info']['user']['domain']]['db_name'];
121                $GLOBALS['phpgw_info']['server']['db_user'] = $GLOBALS['phpgw_domain'][$GLOBALS['phpgw_info']['user']['domain']]['db_user'];
122                $GLOBALS['phpgw_info']['server']['db_pass'] = $GLOBALS['phpgw_domain'][$GLOBALS['phpgw_info']['user']['domain']]['db_pass'];
123                $GLOBALS['phpgw_info']['server']['db_type'] = $GLOBALS['phpgw_domain'][$GLOBALS['phpgw_info']['user']['domain']]['db_type'];
124        }
125        else
126        {
127                $GLOBALS['phpgw_info']['server']['db_host'] = $GLOBALS['phpgw_domain'][$GLOBALS['phpgw_info']['server']['default_domain']]['db_host'];
128                $GLOBALS['phpgw_info']['server']['db_port'] = $GLOBALS['phpgw_domain'][$GLOBALS['phpgw_info']['server']['default_domain']]['db_port'];
129                $GLOBALS['phpgw_info']['server']['db_name'] = $GLOBALS['phpgw_domain'][$GLOBALS['phpgw_info']['server']['default_domain']]['db_name'];
130                $GLOBALS['phpgw_info']['server']['db_user'] = $GLOBALS['phpgw_domain'][$GLOBALS['phpgw_info']['server']['default_domain']]['db_user'];
131                $GLOBALS['phpgw_info']['server']['db_pass'] = $GLOBALS['phpgw_domain'][$GLOBALS['phpgw_info']['server']['default_domain']]['db_pass'];
132                $GLOBALS['phpgw_info']['server']['db_type'] = $GLOBALS['phpgw_domain'][$GLOBALS['phpgw_info']['server']['default_domain']]['db_type'];
133        }
134
135        if ($GLOBALS['phpgw_info']['flags']['currentapp'] != 'login' && ! $GLOBALS['phpgw_info']['server']['show_domain_selectbox'])
136        {
137                unset ($GLOBALS['phpgw_domain']); // we kill this for security reasons
138        }
139
140        print_debug('domain',@$GLOBALS['phpgw_info']['user']['domain'],'api');
141
142         /****************************************************************************\
143         * These lines load up the API, fill up the $phpgw_info array, etc            *
144         \****************************************************************************/
145         /* Load main class */
146        $GLOBALS['phpgw'] = CreateObject('phpgwapi.phpgw');
147         /************************************************************************\
148         * Load up the main instance of the db class.                             *
149         \************************************************************************/
150        $GLOBALS['phpgw']->db           = CreateObject('phpgwapi.db');
151        if ($GLOBALS['phpgw']->debug)
152        {
153                $GLOBALS['phpgw']->db->Debug = 1;
154        }
155        $GLOBALS['phpgw']->db->Halt_On_Error = 'no';
156        /* jakjr: ExpressoLivre: We do not count the config table. */
157        if (!
158        $GLOBALS['phpgw']->db->connect(
159                $GLOBALS['phpgw_info']['server']['db_name'],
160                $GLOBALS['phpgw_info']['server']['db_host'],
161                $GLOBALS['phpgw_info']['server']['db_port'],
162                $GLOBALS['phpgw_info']['server']['db_user'],
163                $GLOBALS['phpgw_info']['server']['db_pass'],
164                $GLOBALS['phpgw_info']['server']['db_type']
165        ) )
166        //@$GLOBALS['phpgw']->db->query("SELECT COUNT(config_name) FROM phpgw_config");
167        //if(!@$GLOBALS['phpgw']->db->next_record())
168        {
169               
170                /* BEGIN - CELEPAR - jakjr - 05/06/2006 */
171                /* $setup_dir = str_replace($_SERVER['PHP_SELF'],'index.php','setup/'); */
172                /*echo '<center><b>Fatal Error:</b> It appears that you have not created the database tables for '
173                        .'eGroupWare.  Click <a href="' . $setup_dir . '">here</a> to run setup.</center>';*/
174                echo '<center><b>'.lang("ExpressoLivre is unavailable at this moment. Code %1<br>Please, try later.","001").'</b></center>';
175                /* END - CELEPAR - jakjr - 05/06/2006 */
176                exit;
177        }
178        $GLOBALS['phpgw']->db->Halt_On_Error = 'yes';
179
180        /* Fill phpgw_info["server"] array */
181        // An Attempt to speed things up using cache premise
182
183        /*
184        $GLOBALS['phpgw']->db->query(
185                "select content from phpgw_app_sessions"
186                        . " where sessionid = '0' and loginid = '0'"
187                        . " and app = 'phpgwapi' and location = 'config'",
188                __LINE__, __FILE__ );
189
190        if ( $GLOBALS['phpgw']->db->num_rows( ) )
191        {
192                $GLOBALS['phpgw']->db->next_record();
193                $GLOBALS['phpgw_info']['server'] = unserialize(stripslashes($GLOBALS['phpgw']->db->f('content')));
194        }
195        else
196        {*/
197                $GLOBALS['phpgw']->db->query("SELECT * from phpgw_config WHERE config_app='phpgwapi'",__LINE__,__FILE__);
198                while ($GLOBALS['phpgw']->db->next_record())
199                {
200                        $GLOBALS['phpgw_info']['server'][$GLOBALS['phpgw']->db->f('config_name')] = stripslashes($GLOBALS['phpgw']->db->f('config_value'));
201                }
202
203                /*
204                $GLOBALS['phpgw']->db->query(
205                        'INSERT INTO phpgw_app_sessions(sessionid,loginid,app,location,content)'
206                                . " VALUES('0','0','phpgwapi','config','"
207                                . addslashes( serialize( $GLOBALS['phpgw_info']['server'] ) ) . "')",
208                        __LINE__, __FILE__ );
209        }*/
210
211        if(@isset($GLOBALS['phpgw_info']['server']['enforce_ssl']) && !$_SERVER['HTTPS'])
212        {
213                Header('Location: https://' . $GLOBALS['phpgw_info']['server']['hostname'] . $GLOBALS['phpgw_info']['server']['webserver_url'] . $_SERVER['REQUEST_URI']);
214                exit;
215        }
216
217        /****************************************************************************\
218        * This is a global constant that should be used                              *
219        * instead of / or \ in file paths                                            *
220        \****************************************************************************/
221        define('SEP',filesystem_separator());
222
223        /************************************************************************\
224        * Required classes                                                       *
225        \************************************************************************/
226        $GLOBALS['phpgw']->log          = CreateObject('phpgwapi.errorlog');
227        $GLOBALS['phpgw']->translation  = CreateObject('phpgwapi.translation');
228        $GLOBALS['phpgw']->common       = CreateObject('phpgwapi.common');
229        $GLOBALS['phpgw']->hooks        = CreateObject('phpgwapi.hooks');
230        $GLOBALS['phpgw']->auth         = CreateObject('phpgwapi.auth');
231        $GLOBALS['phpgw']->accounts     = CreateObject('phpgwapi.accounts');
232        $GLOBALS['phpgw']->acl          = CreateObject('phpgwapi.acl');
233        $GLOBALS['phpgw']->session      = CreateObject('phpgwapi.sessions');
234        $GLOBALS['phpgw']->preferences  = CreateObject('phpgwapi.preferences');
235        $GLOBALS['phpgw']->applications = CreateObject('phpgwapi.applications');
236        $GLOBALS['phpgw']->js           = CreateObject('phpgwapi.javascript');
237        $GLOBALS['phpgw']->css          = CreateObject('phpgwapi.css');
238
239        print_debug('main class loaded', 'messageonly','api');
240        if (! isset($GLOBALS['phpgw_info']['flags']['included_classes']['error']) ||
241                ! $GLOBALS['phpgw_info']['flags']['included_classes']['error'])
242        {
243                include_once(PHPGW_INCLUDE_ROOT.'/phpgwapi/inc/class.error.inc.php');
244                $GLOBALS['phpgw_info']['flags']['included_classes']['error'] = True;
245        }
246
247        /*****************************************************************************\
248        * ACL defines - moved here to work for xml-rpc/soap, also                     *
249        \*****************************************************************************/
250        define('PHPGW_ACL_READ',1);
251        define('PHPGW_ACL_ADD',2);
252        define('PHPGW_ACL_EDIT',4);
253        define('PHPGW_ACL_DELETE',8);
254        define('PHPGW_ACL_PRIVATE',16);
255        define('PHPGW_ACL_GROUP_MANAGERS',32);
256        define('PHPGW_ACL_CUSTOM_1',64);
257        define('PHPGW_ACL_CUSTOM_2',128);
258        define('PHPGW_ACL_CUSTOM_3',256);
259
260        /****************************************************************************\
261        * Forcing the footer to run when the rest of the script is done.             *
262        \****************************************************************************/
263        register_shutdown_function(array($GLOBALS['phpgw']->common, 'phpgw_final'));
264
265        /****************************************************************************\
266        * Stuff to use if logging in or logging out                                  *
267        \****************************************************************************/
268        if ($GLOBALS['phpgw_info']['flags']['currentapp'] == 'login' || $GLOBALS['phpgw_info']['flags']['currentapp'] == 'logout')
269        {
270                if ($GLOBALS['phpgw_info']['flags']['currentapp'] == 'login')
271                {
272                        if (@$_POST['login'] != '')
273                        {
274                                if (count($GLOBALS['phpgw_domain']) > 1)
275                                {
276                                        list($login) = explode('@',$_POST['login']);
277                                }
278                                else
279                                {
280                                        $login = $_POST['login'];
281                                }
282                                print_debug('LID',$login,'app');
283                                $login_id = $GLOBALS['phpgw']->accounts->name2id($login);
284                                print_debug('User ID',$login_id,'app');
285                                $GLOBALS['phpgw']->accounts->accounts($login_id);
286                                $GLOBALS['phpgw']->preferences->preferences($login_id);
287                                $GLOBALS['phpgw']->datetime = CreateObject('phpgwapi.date_time');
288                        }
289                }
290        /**************************************************************************\
291        * Everything from this point on will ONLY happen if                        *
292        * the currentapp is not login or logout                                    *
293        \**************************************************************************/
294        }
295        else
296        {
297                if (! $GLOBALS['phpgw']->session->verify())
298                {
299                        // we forward to the same place after the re-login
300                        if ($GLOBALS['phpgw_info']['server']['webserver_url'] && $GLOBALS['phpgw_info']['server']['webserver_url'] != '/')
301                        {
302                                list(,$relpath) = explode($GLOBALS['phpgw_info']['server']['webserver_url'],$_SERVER['PHP_SELF'],2);
303                        }
304                        else    // the webserver-url is empty or just a slash '/' (eGW is installed in the docroot and no domain given)
305                        {
306                                if (preg_match('/^https?:\/\/[^\/]*\/(.*)$/',$relpath=$_SERVER['PHP_SELF'],$matches))
307                                {
308                                        $relpath = $matches[1];
309                                }
310                        }
311                        // this removes the sessiondata if its saved in the URL
312                        $query = preg_replace('/[&]?sessionid(=|%3D)[^&]+&kp3(=|%3D)[^&]+&domain=.*$/','',$_SERVER['QUERY_STRING']);
313                        Header('Location: '.$GLOBALS['phpgw_info']['server']['webserver_url'].'/login.php?cd=10&phpgw_forward='.urlencode($relpath.(!empty($query) ? '?'.$query : '')));
314                        exit;
315                }
316
317                $GLOBALS['phpgw']->datetime = CreateObject('phpgwapi.date_time');
318
319                /* A few hacker resistant constants that will be used throught the program */
320                define('PHPGW_TEMPLATE_DIR', ExecMethod('phpgwapi.phpgw.common.get_tpl_dir', 'phpgwapi'));
321                define('PHPGW_IMAGES_DIR', ExecMethod('phpgwapi.phpgw.common.get_image_path', 'phpgwapi'));
322                define('PHPGW_IMAGES_FILEDIR', ExecMethod('phpgwapi.phpgw.common.get_image_dir', 'phpgwapi'));
323                define('PHPGW_APP_ROOT', ExecMethod('phpgwapi.phpgw.common.get_app_dir'));
324                define('PHPGW_APP_INC', ExecMethod('phpgwapi.phpgw.common.get_inc_dir'));
325                define('PHPGW_APP_TPL', ExecMethod('phpgwapi.phpgw.common.get_tpl_dir'));
326                define('PHPGW_IMAGES', ExecMethod('phpgwapi.phpgw.common.get_image_path'));
327                define('PHPGW_APP_IMAGES_DIR', ExecMethod('phpgwapi.phpgw.common.get_image_dir'));
328
329                /*      define('PHPGW_APP_IMAGES_DIR', $GLOBALS['phpgw']->common->get_image_dir()); */
330
331                /* Moved outside of this logic
332                define('PHPGW_ACL_READ',1);
333                define('PHPGW_ACL_ADD',2);
334                define('PHPGW_ACL_EDIT',4);
335                define('PHPGW_ACL_DELETE',8);
336                define('PHPGW_ACL_PRIVATE',16);
337                */
338
339                /********* This sets the user variables *********/
340                $GLOBALS['phpgw_info']['user']['private_dir'] = $GLOBALS['phpgw_info']['server']['files_dir']
341                        . '/users/'.$GLOBALS['phpgw_info']['user']['userid'];
342
343                /* This will make sure that a user has the basic default prefs. If not it will add them */
344                $GLOBALS['phpgw']->preferences->verify_basic_settings();
345
346                /********* Optional classes, which can be disabled for performance increases *********/
347                while ($phpgw_class_name = each($GLOBALS['phpgw_info']['flags']))
348                {
349                        if (ereg('enable_',$phpgw_class_name[0]))
350                        {
351                                $enable_class = str_replace('enable_','',$phpgw_class_name[0]);
352                                $enable_class = str_replace('_class','',$enable_class);
353                                eval('$GLOBALS["phpgw"]->' . $enable_class . ' = createobject(\'phpgwapi.' . $enable_class . '\');');
354                        }
355                }
356                unset($enable_class);
357
358
359                reset($GLOBALS['phpgw_info']['flags']);
360
361
362                if(!include(PHPGW_SERVER_ROOT . '/phpgwapi/themes/default.theme'))
363                {
364                        /* Hope we don't get to this point.  Better then the user seeing a */
365                        /* complety back screen and not know whats going on                */
366                        echo '<body bgcolor="FFFFFF">';
367                        $GLOBALS['phpgw']->log->write(array('text'=>'F-Abort, No themes found'));
368
369                        exit;
370                }
371
372                /*************************************************************************\
373                * These lines load up the templates class                                 *
374                \*************************************************************************/
375                if(!@$GLOBALS['phpgw_info']['flags']['disable_Template_class'])
376                {
377                        $GLOBALS['phpgw']->template = CreateObject('phpgwapi.Template',PHPGW_APP_TPL);
378                        preg_match('/(.*)\/(.*)/', PHPGW_APP_TPL, $matches);
379
380                        if ( $GLOBALS['phpgw_info']['flags']['currentapp'] != "jabberit_messenger" )
381                                $_SESSION['phpgw_info'][$GLOBALS['phpgw_info']['flags']['currentapp']]['user']['preferences']['common']['template_set'] = $matches[2];
382                }
383
384
385                /*************************************************************************\
386                * If they are using frames, we need to set some variables                 *
387                \*************************************************************************/
388                if (((isset($GLOBALS['phpgw_info']['user']['preferences']['common']['useframes']) &&
389                        $GLOBALS['phpgw_info']['user']['preferences']['common']['useframes']) &&
390                        $GLOBALS['phpgw_info']['server']['useframes'] == 'allowed') ||
391                        ($GLOBALS['phpgw_info']['server']['useframes'] == 'always'))
392                {
393                        $GLOBALS['phpgw_info']['flags']['navbar_target'] = 'phpgw_body';
394                }
395
396                /*************************************************************************\
397                * Verify that the users session is still active otherwise kick them out   *
398                \*************************************************************************/
399                if ($GLOBALS['phpgw_info']['flags']['currentapp'] != 'home' &&
400                        $GLOBALS['phpgw_info']['flags']['currentapp'] != 'about' &&
401                        $GLOBALS['phpgw_info']['flags']['currentapp'] != 'mobile')
402                {
403                        // This will need to use ACL in the future
404                        if (! $GLOBALS['phpgw_info']['user']['apps'][$GLOBALS['phpgw_info']['flags']['currentapp']] ||
405                                (@$GLOBALS['phpgw_info']['flags']['admin_only'] &&
406                                ! $GLOBALS['phpgw_info']['user']['apps']['admin']))
407                        {
408                                //$GLOBALS['phpgw']->common->phpgw_header();
409
410                                $GLOBALS['phpgw']->log->write(array('text'=>'W-Permissions, Attempted to access %1','p1'=>$GLOBALS['phpgw_info']['flags']['currentapp']));
411
412                                echo '<p><center><b>'.lang('Access not permitted').'</b></center>';
413                                $GLOBALS['phpgw']->common->phpgw_exit(True);
414                        }
415                }
416
417                if(!is_object($GLOBALS['phpgw']->datetime))
418                {
419                        $GLOBALS['phpgw']->datetime = CreateObject('phpgwapi.date_time');
420                }
421
422                $GLOBALS['phpgw']->applications->read_installed_apps(); // to get translated app-titles
423        }
Note: See TracBrowser for help on using the repository browser.