[ Index ]

Source Code Reference for V1.00

title

Body

[close]

/classes/ -> ui.class.php (source)

   1  <?php /* $Id: ui.class.php 174 2008-07-17 14:36:48Z pedroix $ $URL: https://web2project.svn.sourceforge.net/svnroot/web2project/trunk/classes/ui.class.php $ */
   2  /**
   3   * @package web2project
   4   * @subpackage core
   5   * @license http://opensource.org/licenses/gpl-license.php GPL License Version 2
   6   */
   7  
   8  if (!defined('W2P_BASE_DIR')) {
   9      die('This file should not be called directly.');
  10  }
  11  
  12  // Message No Constants
  13  define('UI_MSG_OK', 1);
  14  define('UI_MSG_ALERT', 2);
  15  define('UI_MSG_WARNING', 3);
  16  define('UI_MSG_ERROR', 4);
  17  
  18  // global variable holding the translation array
  19  $GLOBALS['translate'] = array();
  20  
  21  define('UI_CASE_MASK', 0x0F);
  22  define('UI_CASE_UPPER', 1);
  23  define('UI_CASE_LOWER', 2);
  24  define('UI_CASE_UPPERFIRST', 3);
  25  
  26  define('UI_OUTPUT_MASK', 0xF0);
  27  define('UI_OUTPUT_HTML', 0);
  28  define('UI_OUTPUT_JS', 0x10);
  29  define('UI_OUTPUT_RAW', 0x20);
  30  
  31  // W2P_BASE_DIR is set in base.php and fileviewer.php and is the base directory
  32  // of the web2project installation.
  33  require_once  W2P_BASE_DIR . '/classes/permissions.class.php';
  34  /**
  35   * The Application User Interface Class.
  36   *
  37   * @author Andrew Eddie <eddieajau@users.sourceforge.net>
  38   * @version $Revision: 174 $
  39   */
  40  class CAppUI {
  41      /**
  42       @var array generic array for holding the state of anything */
  43      var $state = null;
  44      /**
  45       @var int */
  46      var $user_id = null;
  47      /**
  48       @var string */
  49      var $user_first_name = null;
  50      /**
  51       @var string */
  52      var $user_last_name = null;
  53      /**
  54       @var string */
  55      var $user_company = null;
  56      /**
  57       @var int */
  58      var $user_department = null;
  59      /**
  60       @var string */
  61      var $user_email = null;
  62      /**
  63       @var int */
  64      var $user_type = null;
  65      /**
  66       @var array */
  67      var $user_prefs = null;
  68      /**
  69       @var int Unix time stamp */
  70      var $day_selected = null;
  71  
  72      // localisation
  73      /**
  74       @var string */
  75      var $user_locale = null;
  76      /**
  77       @var string */
  78      var $user_lang = null;
  79      /**
  80       @var string */
  81      var $base_locale = 'en'; // do not change - the base 'keys' will always be in english
  82  
  83      /**
  84       @var string Message string*/
  85      var $msg = '';
  86      /**
  87       @var string */
  88      var $msgNo = '';
  89      /**
  90       @var string Default page for a redirect call*/
  91      var $defaultRedirect = '';
  92  
  93      /**
  94       @var array Configuration variable array*/
  95      var $cfg = null;
  96  
  97      /**
  98       @var integer Version major */
  99      var $version_major = null;
 100  
 101      /**
 102       @var integer Version minor */
 103      var $version_minor = null;
 104  
 105      /**
 106       @var integer Version patch level */
 107      var $version_patch = null;
 108  
 109      /**
 110       @var string Version string */
 111      var $version_string = null;
 112  
 113      /**
 114       @var integer for register log ID */
 115      var $last_insert_id = null;
 116  
 117      /**
 118       @var string */
 119      var $user_style = null;
 120  
 121      /**
 122  
 123       * CAppUI Constructor
 124       */
 125  	function CAppUI() {
 126          $this->state = array();
 127  
 128          $this->user_id = -1;
 129          $this->user_first_name = '';
 130          $this->user_last_name = '';
 131          $this->user_company = 0;
 132          $this->user_department = 0;
 133          $this->user_type = 0;
 134  
 135          // cfg['locale_warn'] is the only cfgVariable stored in session data (for security reasons)
 136          // this guarants the functionality of this->setWarning
 137          $this->cfg['locale_warn'] = w2PgetConfig('locale_warn');
 138  
 139          $this->project_id = 0;
 140  
 141          $this->defaultRedirect = '';
 142          // set up the default preferences
 143          $this->setUserLocale($this->base_locale);
 144          $this->user_prefs = array();
 145      }
 146      /**
 147       * Used to load a php class file from the system classes directory
 148       * @param string $name The class root file name (excluding .class.php)
 149       * @return string The path to the include file
 150       */
 151  	function getSystemClass($name = null) {
 152          if ($name) {
 153              return W2P_BASE_DIR . '/classes/' . $name . '.class.php';
 154          }
 155      }
 156  
 157      /**
 158       * Used to load a php class file from the lib directory
 159       *
 160       * @param string $name The class root file name (excluding .class.php)
 161       * @return string The path to the include file
 162       */
 163  	function getLibraryClass($name = null) {
 164          if ($name) {
 165              return W2P_BASE_DIR . '/lib/' . $name . '.php';
 166          }
 167      }
 168  
 169      /**
 170       * Used to load a php class file from the module directory
 171       * @param string $name The class root file name (excluding .class.php)
 172       * @return string The path to the include file
 173       */
 174  	function getModuleClass($name = null) {
 175          if ($name) {
 176              return W2P_BASE_DIR . '/modules/' . $name . '/' . $name . '.class.php';
 177          }
 178      }
 179  
 180  /**
 181  * Used to load a php class file from the module directory
 182  * @param string $name The class root file name (excluding .ajax.php)
 183  * @return string The path to the include file
 184   */
 185  	function getModuleAjax( $name=null ) {
 186          if ($name) {
 187              return W2P_BASE_DIR . '/modules/' . $name . '/' . $name . '.ajax.php';
 188          }
 189      }
 190  
 191      /**
 192       * Determines the version.
 193       * @return String value indicating the current web2project version
 194       */
 195  	function getVersion() {
 196          global $w2Pconfig;
 197          if (!isset($this->version_major)) {
 198              include_once  W2P_BASE_DIR . '/includes/version.php';
 199              $this->version_major = $w2p_version_major;
 200              $this->version_minor = $w2p_version_minor;
 201              $this->version_patch = $w2p_version_patch;
 202              $this->version_string = $this->version_major . '.' . $this->version_minor;
 203              if (isset($this->version_patch)) {
 204                  $this->version_string .= '.' . $this->version_patch;
 205              }
 206              if (isset($w2p_version_prepatch)) {
 207                  $this->version_string .= '-' . $w2p_version_prepatch;
 208              }
 209          }
 210          return $this->version_string;
 211      }
 212  
 213      /**
 214       * Checks that the current user preferred style is valid/exists.
 215       */
 216  	function checkStyle() {
 217          // check if default user's uistyle is installed
 218          $uistyle = $this->getPref('UISTYLE');
 219  
 220          if ($uistyle && !is_dir(W2P_BASE_DIR . '/style/' . $uistyle)) {
 221              // fall back to host_style if user style is not installed
 222              $this->setPref('UISTYLE', w2PgetConfig('host_style'));
 223          }
 224      }
 225  
 226      /**
 227       * Utility function to read the 'directories' under 'path'
 228       *
 229       * This function is used to read the modules or locales installed on the file system.
 230       * @param string The path to read.
 231       * @return array A named array of the directories (the key and value are identical).
 232       */
 233  	function readDirs($path) {
 234          $dirs = array();
 235          $d = dir(W2P_BASE_DIR . '/' . $path);
 236          while (false !== ($name = $d->read())) {
 237              if (is_dir(W2P_BASE_DIR . '/' . $path . '/' . $name) && $name != '.' && $name != '..' && $name != 'CVS' && $name != '.svn') {
 238                  $dirs[$name] = $name;
 239              }
 240          }
 241          $d->close();
 242          return $dirs;
 243      }
 244  
 245      /**
 246       * Utility function to read the 'files' under 'path'
 247       * @param string The path to read.
 248       * @param string A regular expression to filter by.
 249       * @return array A named array of the files (the key and value are identical).
 250       */
 251  	function readFiles($path, $filter = '.') {
 252          $files = array();
 253  
 254          if (is_dir($path) && ($handle = opendir($path))) {
 255              while (false !== ($file = readdir($handle))) {
 256                  if ($file != '.' && $file != '..' && preg_match('/' . $filter . '/', $file)) {
 257                      $files[$file] = $file;
 258                  }
 259              }
 260              closedir($handle);
 261          }
 262          return $files;
 263      }
 264  
 265      /**
 266       * Utility function to check whether a file name is 'safe'
 267       *
 268       * Prevents from access to relative directories (eg ../../dealyfile.php);
 269       * @param string The file name.
 270       * @return array A named array of the files (the key and value are identical).
 271       */
 272  	function checkFileName($file) {
 273          global $AppUI;
 274  
 275          // define bad characters and their replacement
 276          $bad_chars = ";/\\";
 277          $bad_replace = '....'; // Needs the same number of chars as $bad_chars
 278  
 279          // check whether the filename contained bad characters
 280          if (strpos(strtr($file, $bad_chars, $bad_replace), '.') !== false) {
 281              $AppUI->redirect('m=public&a=access_denied');
 282          } else {
 283              return $file;
 284          }
 285  
 286      }
 287  
 288      /**
 289       * Utility function to make a file name 'safe'
 290       *
 291       * Strips out mallicious insertion of relative directories (eg ../../dealyfile.php);
 292       * @param string The file name.
 293       * @return array A named array of the files (the key and value are identical).
 294       */
 295  	function makeFileNameSafe($file) {
 296          $file = str_replace('../', '', $file);
 297          $file = str_replace('..\\', '', $file);
 298          return $file;
 299      }
 300  
 301      /**
 302       * Sets the user locale.
 303       *
 304       * Looks in the user preferences first.  If this value has not been set by the user it uses the system default set in config.php.
 305       * @param string Locale abbreviation corresponding to the sub-directory name in the locales directory (usually the abbreviated language code).
 306       */
 307  	function setUserLocale($loc = '', $set = true) {
 308          global $locale_char_set;
 309  
 310          $LANGUAGES = $this->loadLanguages();
 311  
 312          if (!$loc) {
 313              $loc = $this->user_prefs['LOCALE'] ? $this->user_prefs['LOCALE'] : w2PgetConfig('host_locale');
 314          }
 315  
 316          if (isset($LANGUAGES[$loc]))
 317              $lang = $LANGUAGES[$loc];
 318          else {
 319              // Need to try and find the language the user is using, find the first one
 320              // that has this as the language part
 321              if (strlen($loc) > 2) {
 322                  list($l, $c) = explode('_', $loc);
 323                  $loc = $this->findLanguage($l, $c);
 324              } else {
 325                  $loc = $this->findLanguage($loc);
 326              }
 327              $lang = $LANGUAGES[$loc];
 328          }
 329          list($base_locale, $english_string, $native_string, $default_language, $lcs) = $lang;
 330          if (!isset($lcs))
 331              $lcs = (isset($locale_char_set)) ? $locale_char_set : 'utf-8';
 332  
 333          if (version_compare(phpversion(), '4.3.0', 'ge')) {
 334              $user_lang = array($loc . '.' . $lcs, $default_language, $loc, $base_locale);
 335          } else {
 336              if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
 337                  $user_lang = $default_language;
 338              } else {
 339                  $user_lang = $loc . '.' . $lcs;
 340              }
 341          }
 342          if ($set) {
 343              $this->user_locale = $base_locale;
 344              $this->user_lang = $user_lang;
 345              $locale_char_set = $lcs;
 346          } else {
 347              return $user_lang;
 348          }
 349      }
 350  
 351  	function findLanguage($language, $country = false) {
 352          $LANGUAGES = $this->loadLanguages();
 353          $language = strtolower($language);
 354          if ($country) {
 355              $country = strtoupper($country);
 356              // Try constructing the code again
 357              $code = $language . '_' . $country;
 358              if (isset($LANGUAGES[$code])) {
 359                  return $code;
 360              }
 361          }
 362  
 363          // Just use the country code and try and find it in the
 364          // languages list.
 365          $first_entry = null;
 366          foreach ($LANGUAGES as $lang => $info) {
 367              list($l, $c) = explode('_', $lang);
 368              if ($l == $language) {
 369                  if (!$first_entry) {
 370                      $first_entry = $lang;
 371                  }
 372                  if ($country && $c == $country) {
 373                      return $lang;
 374                  }
 375              }
 376          }
 377          return $first_entry;
 378      }
 379  
 380      /**
 381       * Load the known language codes for loaded locales
 382       *
 383       */
 384  	function loadLanguages() {
 385  
 386          if (isset($_SESSION['LANGUAGES'])) {
 387              $LANGUAGES = &$_SESSION['LANGUAGES'];
 388          } else {
 389              $LANGUAGES = array();
 390              $langs = $this->readDirs('locales');
 391              foreach ($langs as $lang) {
 392                  if (file_exists(W2P_BASE_DIR . '/locales/' . $lang . '/lang.php')) {
 393                      include_once W2P_BASE_DIR . '/locales/' . $lang . '/lang.php';
 394                  }
 395              }
 396              $_SESSION['LANGUAGES'] = &$LANGUAGES;
 397          }
 398          return $LANGUAGES;
 399      }
 400  
 401      /**
 402       * Translate string to the local language [same form as the gettext abbreviation]
 403       *
 404       * This is the order of precedence:
 405       * <ul>
 406       * <li>If the key exists in the lang array, return the value of the key
 407       * <li>If no key exists and the base lang is the same as the local lang, just return the string
 408       * <li>If this is not the base lang, then return string with a red star appended to show
 409       * that a translation is required.
 410       * </ul>
 411       * @param string The string to translate
 412       * @param int Option flags, can be case handling or'd with output styles
 413       * @return string
 414       */
 415      function _($str, $flags = 0) {
 416          if (is_array($str)) {
 417              $translated = array();
 418              foreach ($str as $s) {
 419                  $translated[] = $this->__($s, $flags);
 420              }
 421              return implode(' ', $translated);
 422          } else {
 423              return $this->__($str, $flags);
 424          }
 425      }
 426  
 427      function __($str, $flags = 0) {
 428          $str = trim($str);
 429          if (empty($str)) {
 430              return '';
 431          }
 432          $x = $GLOBALS['translate'][$str];
 433  
 434          if ($x) {
 435              $str = $x;
 436          } elseif (w2PgetConfig('locale_warn')) {
 437              if ($this->base_locale != $this->user_locale || ($this->base_locale == $this->user_locale && !in_array($str, $GLOBALS['translate']))) {
 438                  $str .= w2PgetConfig('locale_alert');
 439              }
 440          }
 441          switch ($flags & UI_CASE_MASK) {
 442              case UI_CASE_UPPER:
 443                  $str = strtoupper($str);
 444                  break;
 445              case UI_CASE_LOWER:
 446                  $str = strtolower($str);
 447                  break;
 448              case UI_CASE_UPPERFIRST:
 449                  $str = ucwords($str);
 450                  break;
 451          }
 452          /* Altered to support multiple styles of output, to fix
 453          * bugs where the same output style cannot be used succesfully
 454          * for both javascript and HTML output.
 455          * PLEASE NOTE: The default is currently UI_OUTPUT_HTML,
 456          * which is different to the previous version (which was
 457          * effectively UI_OUTPUT_RAW).  If this causes problems,
 458          * and they are localised, then use UI_OUTPUT_RAW in the
 459          * offending call.  If they are widespread, change the
 460          * default to UI_OUTPUT_RAW and use the other options
 461          * where appropriate.
 462          * AJD - 2004-12-10
 463          */
 464          global $locale_char_set;
 465  
 466          if (!$locale_char_set) {
 467              $locale_char_set = 'utf-8';
 468          }
 469  
 470          switch ($flags & UI_OUTPUT_MASK) {
 471              case UI_OUTPUT_HTML:
 472                  $str = htmlentities(stripslashes($str), ENT_COMPAT, $locale_char_set);
 473                  break;
 474              case UI_OUTPUT_JS:
 475                  $str = addslashes(stripslashes($str)); //, ENT_COMPAT, $locale_char_set);
 476                  break;
 477              case UI_OUTPUT_RAW:
 478                  $str = stripslashes($str);
 479                  break;
 480          }
 481          return $str;
 482      }
 483      /**
 484       * Set the display of warning for untranslated strings
 485       * @param string
 486       */
 487  	function setWarning($state = true) {
 488          $temp = $this->cfg['locale_warn'];
 489          $this->cfg['locale_warn'] = $state;
 490          return $temp;
 491      }
 492      /**
 493       * Save the url query string
 494       *
 495       * Also saves one level of history.  This is useful for returning from a delete
 496       * operation where the record more not now exist.  Returning to a view page
 497       * would be a nonsense in this case.
 498       * @param string If not set then the current url query string is used
 499       */
 500  	function savePlace($query = '') {
 501          if (!$query) {
 502              $query = $_SERVER['QUERY_STRING'];
 503          }
 504          if ($query != $this->state['SAVEDPLACE']) {
 505              $this->state['SAVEDPLACE-1'] = $this->state['SAVEDPLACE'];
 506              $this->state['SAVEDPLACE'] = $query;
 507          }
 508      }
 509      /**
 510       * Resets the internal variable
 511       */
 512  	function resetPlace() {
 513          $this->state['SAVEDPLACE'] = '';
 514      }
 515      /**
 516       * Get the saved place (usually one that could contain an edit button)
 517       * @return string
 518       */
 519  	function getPlace() {
 520          return $this->state['SAVEDPLACE'];
 521      }
 522      /**
 523       * Redirects the browser to a new page.
 524       *
 525       * Mostly used in conjunction with the savePlace method. It is generally used
 526       * to prevent nasties from doing a browser refresh after a db update.  The
 527       * method deliberately does not use javascript to effect the redirect.
 528       *
 529       * @param string The URL query string to append to the URL
 530       * @param string A marker for a historic 'place, only -1 or an empty string is valid.
 531       */
 532  	function redirect($params = '', $hist = '') {
 533          $session_id = SID;
 534  
 535          session_write_close();
 536          // are the params empty
 537          if (!$params) {
 538              // has a place been saved
 539              $params = !empty($this->state['SAVEDPLACE' . $hist]) ? $this->state['SAVEDPLACE' . $hist] : $this->defaultRedirect;
 540          }
 541          // Fix to handle cookieless sessions
 542          if ($session_id != '') {
 543              if (!$params) {
 544                  $params = $session_id;
 545              } else {
 546                  $params .= '&' . $session_id;
 547              }
 548          }
 549          ob_implicit_flush(); // Ensure any buffering is disabled.
 550          header('Location: index.php?' . $params);
 551          exit(); // stop the PHP execution
 552      }
 553      /**
 554       * Set the page message.
 555       *
 556       * The page message is displayed above the title block and then again
 557       * at the end of the page.
 558       *
 559       * IMPORTANT: Please note that append should not be used, since for some
 560       * languagues atomic-wise translation doesn't work. Append should be
 561       * deprecated.
 562       *
 563       * @param mixed The (untranslated) message
 564       * @param int The type of message
 565