| Total Complexity | 85 |
| Total Lines | 573 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Egw often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Egw, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class Egw extends Egw\Base |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * Turn on debug mode. Will output additional data for debugging purposes. |
||
| 44 | * @var string |
||
| 45 | * @access public |
||
| 46 | */ |
||
| 47 | var $debug = 0; // This will turn on debugging information. |
||
| 48 | /** |
||
| 49 | * Instance of the account object |
||
| 50 | * |
||
| 51 | * @var Accounts |
||
| 52 | */ |
||
| 53 | var $accounts; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Constructor: Instantiates the sub-classes |
||
| 57 | * |
||
| 58 | * @author [email protected] |
||
| 59 | * @param array $domain_names array with valid egw-domain names |
||
| 60 | */ |
||
| 61 | function __construct($domain_names=null) |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Called every time the constructor is called. Also called by sessions to ensure the correct db, |
||
| 69 | * in which case we do not recreate the session object. |
||
| 70 | * @author [email protected] (moved to setup() by [email protected] |
||
| 71 | * @param array|null $domain_names =null array with valid egw-domain names |
||
| 72 | * @param boolean $createsessionobject True to create the session object (default=True) |
||
| 73 | */ |
||
| 74 | function setup($domain_names,$createsessionobject=True) |
||
| 75 | { |
||
| 76 | // create the DB-object |
||
| 77 | // as SiteMgr, Wiki, KnowledgeBase and probably more still use eg next_record(), we stick with Db\Deprecated for now |
||
| 78 | $this->db = new Db\Deprecated($GLOBALS['egw_info']['server']); |
||
| 79 | if ($this->debug) |
||
| 80 | { |
||
| 81 | $this->db->Debug = 1; |
||
| 82 | } |
||
| 83 | $this->db->set_app(Db::API_APPNAME); |
||
| 84 | |||
| 85 | // check if eGW is already setup, if not redirect to setup/ |
||
| 86 | try { |
||
| 87 | $this->db->connect(); |
||
| 88 | $num_config = $this->db->select(Config::TABLE,'COUNT(config_name)',false,__LINE__,__FILE__)->fetchColumn(); |
||
| 89 | } |
||
| 90 | catch(Db\Exception\Connection $e) { |
||
| 91 | // ignore exception, get handled below |
||
| 92 | } |
||
| 93 | catch(Db\Exception\InvalidSql $e1) { |
||
| 94 | unset($e1); // not used |
||
| 95 | try { |
||
| 96 | $phpgw_config = $this->db->select('phpgw_config','COUNT(config_name)',false,__LINE__,__FILE__)->fetchColumn(); |
||
| 97 | } |
||
| 98 | catch (Db\Exception\InvalidSql $e2) { |
||
| 99 | unset($e2); // not used |
||
| 100 | // ignor error, get handled below |
||
| 101 | } |
||
| 102 | } |
||
| 103 | if (!$num_config) |
||
| 104 | { |
||
| 105 | // we check for the old table too, to not scare updating users ;-) |
||
| 106 | if ($phpgw_config) |
||
| 107 | { |
||
| 108 | throw new Exception('You need to update EGroupware before you can continue using it.',999); |
||
| 109 | } |
||
| 110 | if ($e) |
||
| 111 | { |
||
| 112 | throw new Db\Exception\Setup('Connection with '.$e->getMessage()."\n\n". |
||
| 113 | 'Maybe you not created a database for EGroupware yet.',999); |
||
| 114 | } |
||
| 115 | throw new Db\Exception\Setup('It appears that you have not created the database tables for EGroupware.',999); |
||
| 116 | } |
||
| 117 | // Set the DB's client charset if a system-charset is set and some other values needed by egw_cache (used in Config::read) |
||
| 118 | foreach($this->db->select(Config::TABLE,'config_name,config_value',array( |
||
| 119 | 'config_app' => 'phpgwapi', |
||
| 120 | 'config_name' => array('system_charset','install_id','temp_dir'), |
||
| 121 | ),__LINE__,__FILE__) as $row) |
||
| 122 | { |
||
| 123 | $GLOBALS['egw_info']['server'][$row['config_name']] = $row['config_value']; |
||
| 124 | } |
||
| 125 | if ($GLOBALS['egw_info']['server']['system_charset'] && $GLOBALS['egw_info']['server']['system_charset'] != 'utf-8') |
||
| 126 | { |
||
| 127 | $this->db->Link_ID->SetCharSet($GLOBALS['egw_info']['server']['system_charset']); |
||
| 128 | } |
||
| 129 | // load up the $GLOBALS['egw_info']['server'] array |
||
| 130 | $GLOBALS['egw_info']['server'] += Config::read('phpgwapi'); |
||
| 131 | |||
| 132 | // if webserver_url does not match eg. because of proxying, fix it |
||
| 133 | if (isset($_SERVER['HTTP_X_FORWARDED_URI']) && |
||
| 134 | ($prefix = strpos($_SERVER['HTTP_X_FORWARDED_URI'], |
||
| 135 | $GLOBALS['egw_info']['server']['webserver_url']))) |
||
| 136 | { |
||
| 137 | $GLOBALS['egw_info']['server']['webserver_url'] = |
||
| 138 | substr($_SERVER['HTTP_X_FORWARDED_URI'], 0, $prefix). |
||
| 139 | $GLOBALS['egw_info']['server']['webserver_url']; |
||
| 140 | } |
||
| 141 | if (isset($_SERVER['HTTP_X_FORWARDED_HOST']) && $GLOBALS['egw_info']['server']['webserver_url'][0] != '/') |
||
| 142 | { |
||
| 143 | $GLOBALS['egw_info']['server']['webserver_url'] = |
||
| 144 | Header\Http::schema().'://'.Header\Http::host(). |
||
| 145 | parse_url($GLOBALS['egw_info']['server']['webserver_url'], PHP_URL_PATH); |
||
| 146 | } |
||
| 147 | |||
| 148 | // if no server timezone set, use date_default_timezone_get() to determine it once |
||
| 149 | // it fills to log with deprecated warnings under 5.3 otherwise |
||
| 150 | if (empty($GLOBALS['egw_info']['server']['server_timezone']) || |
||
| 151 | $GLOBALS['egw_info']['server']['server_timezone'] == 'System/Localtime') // treat invalid tz like empty! |
||
| 152 | { |
||
| 153 | try |
||
| 154 | { |
||
| 155 | $tz = new \DateTimeZone(date_default_timezone_get()); |
||
| 156 | Config::save_value('server_timezone',$GLOBALS['egw_info']['server']['server_timezone'] = $tz->getName(),'phpgwapi'); |
||
| 157 | error_log(__METHOD__."() stored server_timezone=".$GLOBALS['egw_info']['server']['server_timezone']); |
||
| 158 | } |
||
| 159 | catch(Exception $e) |
||
| 160 | { |
||
| 161 | // do nothing if new DateTimeZone fails (eg. 'System/Localtime' returned), specially do NOT store it! |
||
| 162 | error_log(__METHOD__."() NO valid 'date.timezone' set in your php.ini!"); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | date_default_timezone_set($GLOBALS['egw_info']['server']['server_timezone']); |
||
| 166 | |||
| 167 | // if phpgwapi exists we prefer accounts and egw_session, as they have some deprecated methods |
||
| 168 | if (file_exists(EGW_SERVER_ROOT.'/phpgwapi')) |
||
| 169 | { |
||
| 170 | $this->accounts = new egw_accounts(); |
||
| 171 | /* Do not create the session object if called by the sessions class. This way |
||
| 172 | * we ensure the correct db based on the user domain. |
||
| 173 | */ |
||
| 174 | if($createsessionobject) |
||
| 175 | { |
||
| 176 | $this->session = new egw_session($domain_names); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | else |
||
| 180 | { |
||
| 181 | $this->accounts = new Accounts(); |
||
| 182 | /* Do not create the session object if called by the sessions class. This way |
||
| 183 | * we ensure the correct db based on the user domain. |
||
| 184 | */ |
||
| 185 | if($createsessionobject) |
||
| 186 | { |
||
| 187 | $this->session = new Session($domain_names); |
||
| 188 | } |
||
| 189 | } |
||
| 190 | // setup the other subclasses |
||
| 191 | $this->acl = new Acl(); |
||
| 192 | $this->preferences = new Preferences(); |
||
| 193 | $this->applications = new Egw\Applications(); |
||
| 194 | |||
| 195 | if ($GLOBALS['egw_info']['flags']['currentapp'] != 'login' && $GLOBALS['egw_info']['flags']['currentapp'] != 'logout') |
||
| 196 | { |
||
| 197 | $this->define_egw_constants(); |
||
| 198 | |||
| 199 | $this->verify_session(); |
||
| 200 | $this->applications->read_installed_apps(); // to get translated app-titles, has to be after verify_session |
||
| 201 | |||
| 202 | $this->check_app_rights(); |
||
| 203 | |||
| 204 | $this->load_optional_classes(); |
||
| 205 | } |
||
| 206 | else // set the defines for login, in case it's more then just login |
||
| 207 | { |
||
| 208 | $this->define_egw_constants(); |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * __wakeup function gets called by php while unserializing the egw-object, eg. reconnects to the DB |
||
| 214 | * |
||
| 215 | * @author [email protected] |
||
| 216 | */ |
||
| 217 | function __wakeup() |
||
| 218 | { |
||
| 219 | $GLOBALS['egw'] =& $this; // we need to be immediately available there for the other classes we instantiate |
||
| 220 | // for the migration: reference us to the old phpgw object |
||
| 221 | $GLOBALS['phpgw'] =& $this; |
||
| 222 | |||
| 223 | if ($GLOBALS['egw_info']['server']['system_charset']) |
||
| 224 | { |
||
| 225 | $this->db->Link_ID->SetCharSet($GLOBALS['egw_info']['server']['system_charset']); |
||
| 226 | } |
||
| 227 | // restoring server timezone, to avoid warnings under php5.3 |
||
| 228 | if (!empty($GLOBALS['egw_info']['server']['server_timezone'])) |
||
| 229 | { |
||
| 230 | date_default_timezone_set($GLOBALS['egw_info']['server']['server_timezone']); |
||
| 231 | } |
||
| 232 | |||
| 233 | $this->define_egw_constants(); |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * wakeup2 function needs to be called after unserializing the egw-object |
||
| 238 | * |
||
| 239 | * It adapts the restored object/enviroment to the changed (current) application / page-request |
||
| 240 | * |
||
| 241 | * @author [email protected] |
||
| 242 | */ |
||
| 243 | function wakeup2() |
||
| 244 | { |
||
| 245 | // do some application specific stuff, need to be done as we are different (current) app now |
||
| 246 | if (isset($this->template)) |
||
| 247 | { |
||
| 248 | $this->template->set_root(EGW_APP_TPL); |
||
| 249 | } |
||
| 250 | // init the translation class, necessary as own wakeup would run before our's |
||
| 251 | Translation::init(isset($GLOBALS['egw_info']['flags']['load_translations']) ? $GLOBALS['egw_info']['flags']['load_translations'] : true); |
||
| 252 | |||
| 253 | // verify the session |
||
| 254 | $GLOBALS['egw']->verify_session(); |
||
| 255 | $GLOBALS['egw']->check_app_rights(); |
||
| 256 | |||
| 257 | $this->load_optional_classes(); |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * load optional classes by mentioning them in egw_info[flags][enable_CLASS_class] => true |
||
| 262 | * |
||
| 263 | * Also loads the template-class if not egw_info[flags][disable_Template_class] is set |
||
| 264 | * |
||
| 265 | * Maybe the whole thing should be depricated ;-) |
||
| 266 | */ |
||
| 267 | function load_optional_classes() |
||
| 277 | } |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Verfiy there is a valid session |
||
| 283 | * |
||
| 284 | * One can specify a callback, which gets called if there's no valid session. If the callback returns true, the parameter |
||
| 285 | * containst account-details (in keys login, passwd and passwd_type) to automatic create an (anonymous session) |
||
| 286 | * |
||
| 287 | * It also checks if enforce_ssl is set in the DB and redirects to the https:// version of the site. |
||
| 288 | * |
||
| 289 | * If there is no valid session and none could be automatic created, the function will redirect to login and NOT return |
||
| 290 | */ |
||
| 291 | function verify_session() |
||
| 345 | } |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Verify the user has rights for the requested app |
||
| 350 | * |
||
| 351 | * If the user has no rights for the app (eg. called via URL) he get a permission denied page (this function does NOT return) |
||
| 352 | * |
||
| 353 | * @throws Exception\Redirect for anonymous user accessing something he has no rights to |
||
| 354 | * @throws Exception\NoPermission\Admin |
||
| 355 | * @throws Exception\NoPermission\App |
||
| 356 | */ |
||
| 357 | function check_app_rights() |
||
| 358 | { |
||
| 359 | $this->currentapp = $GLOBALS['egw_info']['flags']['currentapp']; // some apps change it later |
||
| 360 | |||
| 361 | if (!in_array($GLOBALS['egw_info']['flags']['currentapp'], array('api','about'))) // give everyone implicit api rights |
||
| 362 | { |
||
| 363 | // This will need to use ACL in the future |
||
| 364 | if (!$GLOBALS['egw_info']['user']['apps'][$currentapp = $GLOBALS['egw_info']['flags']['currentapp']] || |
||
| 365 | ($GLOBALS['egw_info']['flags']['admin_only'] && !$GLOBALS['egw_info']['user']['apps']['admin'])) |
||
| 366 | { |
||
| 367 | // present a login page, if anon user has no right for an application |
||
| 368 | if ($this->session->session_flags == 'A') |
||
| 369 | { |
||
| 370 | // need to destroy a basic auth session here, because it will only be available on current url |
||
| 371 | if (($sessionid = Session::get_sessionid(true))) |
||
| 372 | { |
||
| 373 | $GLOBALS['egw']->session->destroy($sessionid); |
||
| 374 | } |
||
| 375 | throw new Exception\Redirect(self::link('/logout.php')); |
||
| 376 | } |
||
| 377 | if ($currentapp == 'admin' || $GLOBALS['egw_info']['flags']['admin_only']) |
||
| 378 | { |
||
| 379 | throw new Exception\NoPermission\Admin(); |
||
| 380 | } |
||
| 381 | throw new Exception\NoPermission\App($currentapp); |
||
| 382 | } |
||
| 383 | } |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * create all the defines / constants of the eGW-environment (plus the deprecated phpgw ones) |
||
| 388 | */ |
||
| 389 | function define_egw_constants() |
||
| 390 | { |
||
| 391 | define('EGW_ACL_READ',1); |
||
| 392 | define('EGW_ACL_ADD',2); |
||
| 393 | define('EGW_ACL_EDIT',4); |
||
| 394 | define('EGW_ACL_DELETE',8); |
||
| 395 | define('EGW_ACL_PRIVATE',16); |
||
| 396 | define('EGW_ACL_GROUP_MANAGERS',32); |
||
| 397 | define('EGW_ACL_CUSTOM_1',64); |
||
| 398 | define('EGW_ACL_CUSTOM_2',128); |
||
| 399 | define('EGW_ACL_CUSTOM_3',256); |
||
| 400 | // and the old ones |
||
| 401 | define('PHPGW_ACL_READ',1); |
||
| 402 | define('PHPGW_ACL_ADD',2); |
||
| 403 | define('PHPGW_ACL_EDIT',4); |
||
| 404 | define('PHPGW_ACL_DELETE',8); |
||
| 405 | define('PHPGW_ACL_PRIVATE',16); |
||
| 406 | define('PHPGW_ACL_GROUP_MANAGERS',32); |
||
| 407 | define('PHPGW_ACL_CUSTOM_1',64); |
||
| 408 | define('PHPGW_ACL_CUSTOM_2',128); |
||
| 409 | define('PHPGW_ACL_CUSTOM_3',256); |
||
| 410 | // A few hacker resistant constants that will be used throught the program |
||
| 411 | if (file_exists(EGW_SERVER_ROOT.'/phpgwapi')) |
||
| 412 | { |
||
| 413 | define('EGW_TEMPLATE_DIR', Framework\Template::get_dir('phpgwapi')); |
||
| 414 | define('EGW_IMAGES_DIR', common::get_image_path('phpgwapi')); |
||
| 415 | define('EGW_IMAGES_FILEDIR', common::get_image_dir('phpgwapi')); |
||
| 416 | define('EGW_APP_ROOT', common::get_app_dir()); |
||
| 417 | define('EGW_APP_INC', common::get_inc_dir()); |
||
| 418 | try { |
||
| 419 | define('EGW_APP_TPL', Framework\Template::get_dir()); |
||
| 420 | } |
||
| 421 | catch (Exception\WrongParameter $e) { |
||
| 422 | unset($e); |
||
| 423 | define('EGW_APP_TPL', null); |
||
| 424 | } |
||
| 425 | define('EGW_IMAGES', common::get_image_path()); |
||
| 426 | define('EGW_APP_IMAGES_DIR', common::get_image_dir()); |
||
| 427 | // and the old ones |
||
| 428 | define('PHPGW_TEMPLATE_DIR',EGW_TEMPLATE_DIR); |
||
| 429 | define('PHPGW_IMAGES_DIR',EGW_IMAGES_DIR); |
||
| 430 | define('PHPGW_IMAGES_FILEDIR',EGW_IMAGES_FILEDIR); |
||
| 431 | define('PHPGW_APP_ROOT',EGW_APP_ROOT); |
||
| 432 | define('PHPGW_APP_INC',EGW_APP_INC); |
||
| 433 | define('PHPGW_APP_TPL',EGW_APP_TPL); |
||
| 434 | define('PHPGW_IMAGES',EGW_IMAGES); |
||
| 435 | define('PHPGW_APP_IMAGES_DIR',EGW_APP_IMAGES_DIR); |
||
| 436 | } |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * force the session cache to be re-created, because some of it's data changed |
||
| 441 | * |
||
| 442 | * Needs to be called if user-preferences, system-config or enabled apps of the current user have been changed and |
||
| 443 | * the change should have immediate effect |
||
| 444 | */ |
||
| 445 | static function invalidate_session_cache() |
||
| 446 | { |
||
| 447 | // if sharing is active, we must not invalidate the session, as it can not be regenerated |
||
| 448 | if (empty($GLOBALS['egw']->sharing)) |
||
| 449 | { |
||
| 450 | unset($_SESSION['egw_info_cache']); |
||
| 451 | unset($_SESSION['egw_object_cache']); |
||
| 452 | } |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * run string through htmlspecialchars and stripslashes |
||
| 457 | * |
||
| 458 | * @param string $s |
||
| 459 | * @return string The string with html special characters replaced with entities |
||
| 460 | */ |
||
| 461 | static function strip_html($s) |
||
| 462 | { |
||
| 463 | return htmlspecialchars(stripslashes($s)); |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Link url generator |
||
| 468 | * |
||
| 469 | * @param string $url url link is for |
||
| 470 | * @param string|array $extravars ='' extra params to be added to url |
||
| 471 | * @param string $link_app =null if appname or true, some templates generate a special link-handler url |
||
| 472 | * @return string The full url after processing |
||
| 473 | */ |
||
| 474 | static function link($url, $extravars = '', $link_app=null) |
||
| 475 | { |
||
| 476 | return $GLOBALS['egw']->framework->link($url, $extravars, $link_app); |
||
| 477 | } |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Redirects direct to a generated link |
||
| 481 | * |
||
| 482 | * @param string $url url link is for |
||
| 483 | * @param string|array $extravars ='' extra params to be added to url |
||
| 484 | * @param string $link_app =null if appname or true, some templates generate a special link-handler url |
||
| 485 | * @return string The full url after processing |
||
| 486 | */ |
||
| 487 | static function redirect_link($url, $extravars='', $link_app=null) |
||
| 488 | { |
||
| 489 | return $GLOBALS['egw']->framework->redirect_link($url, $extravars, $link_app); |
||
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Handles redirects under iis and apache, it does NOT return (calls exit) |
||
| 494 | * |
||
| 495 | * This function handles redirects under iis and apache it assumes that $phpgw->link() has already been called |
||
| 496 | * |
||
| 497 | * @param string $url url to redirect to |
||
| 498 | * @param string $link_app =null appname to redirect for, default currentapp |
||
| 499 | */ |
||
| 500 | static function redirect($url, $link_app=null) |
||
| 501 | { |
||
| 502 | Framework::redirect($url, $link_app); |
||
| 503 | } |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Shortcut to translation class |
||
| 507 | * |
||
| 508 | * This function is a basic wrapper to Translation::translate() |
||
| 509 | * |
||
| 510 | * @deprecated only used in the old timetracker |
||
| 511 | * @param string The key for the phrase |
||
| 512 | * @see Translation::translate() |
||
| 513 | */ |
||
| 514 | static function lang($key,$args=null) |
||
| 515 | { |
||
| 516 | if (!is_array($args)) |
||
| 517 | { |
||
| 518 | $args = func_get_args(); |
||
| 519 | array_shift($args); |
||
| 520 | } |
||
| 521 | return Translation::translate($key,$args); |
||
| 522 | } |
||
| 523 | |||
| 524 | /** |
||
| 525 | * registered shutdown callbacks and optional arguments |
||
| 526 | * |
||
| 527 | * @var array |
||
| 528 | */ |
||
| 529 | private static $shutdown_callbacks = array(); |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Register a callback to run on shutdown AFTER output send to user |
||
| 533 | * |
||
| 534 | * Allows eg. static classes (no destructor) to run on shutdown AND |
||
| 535 | * garanties to run AFTER output send to user. |
||
| 536 | * |
||
| 537 | * @param callable $callback use array($classname, $method) for static methods |
||
| 538 | * @param array $args =array() |
||
| 539 | */ |
||
| 540 | public static function on_shutdown($callback, array $args=array()) |
||
| 541 | { |
||
| 542 | array_unshift($args, $callback); |
||
| 543 | |||
| 544 | // prepend new callback, to run them in oposite order they are registered |
||
| 545 | array_unshift(self::$shutdown_callbacks, $args); |
||
| 546 | } |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Shutdown handler running all registered on_shutdown callbacks and then disconnecting from db |
||
| 550 | */ |
||
| 551 | function __destruct() |
||
| 613 | } |
||
| 614 | } |
||
| 615 | } |
||
| 616 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths