Česky
Kamil Dudka

QuickMenu (Javascript, PHP)

File detail

Name:Downloadbase.php [Download]
Location: QuickMenu > QuickMenu-with-example
Size:14.8 KB
Last modification:2022-09-09 13:06

Source code

<?php
/**
 * @file base.php
 * Set of base classes used by almost all pages of the web application
 * @author Kamil Dudka <xdudka00@gmail.com>
 */
 
/**
 * Singleton Config holds configuration data.
 */
class Config {
  private static $instance_;
  /**
   * Access point to singleton instance.
   */
  public static function instance() {
    if (!isset(self::$instance_))
      self::$instance_ = new Config;
    return self::$instance_;
  }
 
  private $documentRoot_;
  /**
   * Base path for all files accessed at @b server's @b side.
   * Default is $_SERVER['DOCUMENT_ROOT'].
   * @return Base path for all files accessed at @b server's @b side.
   */
  public function documentRoot() {
    return $this->documentRoot_;
  }
  /**
   * Set base path for all files accessed at @b server's @b side.
   * Default is $_SERVER['DOCUMENT_ROOT'].
   * @param documentRoot Base path for all files accessed at @b server's @b side.
   */
  public function setDocumentRoot($documentRoot) {
    $this->documentRoot_ = $documentRoot;
  }
 
  private $webName_ = '';
  /**
   * Visible name of the web.
   * @return Visible name of the web.
   */
  public function webName() {
    return $this->webName_;
  }
  /**
   * Set visible name of the web.
   * @param webName Visible name of the web.
   */
  public function setWebName($webName) {
    $this->webName_ = $webName;
  }
 
  private $webMasterMail_ = '';
  /**
   * Webmaster's email address.
   * @return Webmaster's email address.
   */
  public function webMasterMail() {
    return $this->webMasterMail_;
  }
  /**
   * Set webmaster's email address.
   * @param webMasterMail Webmaster's email address.
   */
  public function setWebMasterMail($webMasterMail) {
    $this->webMasterMail_ = $webMasterMail;
  }
 
  private $webRoot_ = '';
  /**
   * URL base for all files accessed from @b web @b browser.
   * @return URL base for all files accessed from @b web @b browser.
   */
  public function webRoot() {
    return $this->webRoot_;
  }
  /**
   * Set URL base for all files accessed from @b web @b browser.
   * @param webRoot URL base for all files accessed from @b web @b browser.
   */
  public function setWebRoot($webRoot) {
    $this->webRoot_ = $webRoot;
  }
 
  private $classDir_ = '';
  /**
   * Relative path to class definition files.
   * @return Relative path to class definition files.
   */
  public function classDir() {
    return $this->classDir_;
  }
  /**
   * Set relative path to class definition files.
   * @param classDir Relative path to class definition files.
   */
  public function setClassDir($classDir) {
    $this->classDir_ = $classDir;
  }
 
  private $smartyDir_ = '';
  /**
   * Absolute path to Smarty
   * @return Absolute path to Smarty.
   * @note This path is @b not @b relative due to historical reasons.
   */
  public function smartyDir() {
    return $this->smartyDir_;
  }
  /**
   * Set absolute path to Smarty
   * @param smartyDir Absolute path to Smarty.
   * @note This path is @b not @b relative due to historical reasons.
   */
  public function setSmartyDir($smartyDir) {
    $this->smartyDir_ = $smartyDir;
  }
 
  /**
   * Smarty class default configuration (paths, etc.\ )
   * @note See Smarty manual for more help.
   */
  public $smartyConf = Array();
 
  private $scriptDir_ = '';
  /**
   * Relative path to Javascript files.
   * @return Relative path to Javascript files.
   */
  public function scriptDir() {
    return $this->scriptDir_;
  }
  /**
   * Set relative path to Javascript files.
   * @param scriptDir Relative path to Javascript files.
   */
  public function setScriptDir($scriptDir) {
    $this->scriptDir_ = $scriptDir;
  }
 
  private $cacheDir_ = '';
  /**
   * Relative path to long-term cache directory.
   * @return Relative path to long-term cache directory.
   * @note Write privileges are required.
   */
  public function cacheDir() {
    return $this->cacheDir_;
  }
  /**
   * Set relative path to long-term cache directory.
   * @param cacheDir Relative path to long-term cache directory.
   * @note Write privileges are required.
   */
  public function setCacheDir($cacheDir) {
    $this->cacheDir_ = $cacheDir;
  }
 
  private $styleDir_ = '';
  /**
   * Relative path to CSS files.
   * @return Relative path to CSS files.
   */
  public function styleDir() {
    return $this->styleDir_;
  }
  /**
   * Set relative path to CSS files.
   * @param styleDir Relative path to CSS files.
   */
  public function setStyleDir($styleDir) {
    $this->styleDir_ = $styleDir;
  }
 
  private $imgDir_ = '';
  /**
   * Relative path to images.
   * @return Relative path to images.
   * @note There should be only common usage images.
   * Page-specific images can be in filesDir or htmlDir.
   */
  public function imgDir() {
    return $this->imgDir_;
  }
  /**
   * Set relative path to images.
   * @param imgDir Relative path to images.
   * @note There should be only common usage images.
   * Page-specific images can be in filesDir or htmlDir.
   */
  public function setImgDir($imgDir) {
    $this->imgDir_ = $imgDir;
  }
 
  private $pageDir_ = '';
  /**
   * Relative path to XML and XSL templates containing visible web data.
   * @return Relative path to XML and XSL templates containing visible web data.
   */
  public function pageDir() {
    return $this->pageDir_;
  }
  /**
   * Set relative path to XML and XSL templates containing visible web data.
   * @param pageDir Relative path to XML and XSL templates containing visible web data.
   */
  public function setPageDir($pageDir) {
    $this->pageDir_ = $pageDir;
  }
 
  private $geshiDir_ = '';
  /**
   * Relative path to GeSHi.
   * @return Relative path to GeSHi.
   */
  public function geshiDir() {
    return $this->geshiDir_;
  }
  /**
   * Set relative path to GeSHi.
   * @param geshiDir Relative path to GeSHi.
   */
  public function setGeshiDir($geshiDir) {
    $this->geshiDir_ = $geshiDir;
  }
 
  private $filesDir_ = '';
  /**
   * Relative path to FileBrowser data root.
   * @return Relative path to FileBrowser data root.
   */
  public function filesDir() {
    return $this->filesDir_;
  }
  /**
   * Set relative path to FileBrowser data root.
   * @param filesDir Relative path to FileBrowser data root.
   */
  public function setFilesDir($filesDir) {
    $this->filesDir_ = $filesDir;
  }
 
  private $htmlDir_ = '';
  /**
   * Relative path to directory used for FileBrowser's action=directAccess.
   * @return Relative path to directory used for FileBrowser's action=directAccess.
   */
  public function htmlDir() {
    return $this->htmlDir_;
  }
  /**
   * Set relative path to directory used for FileBrowser's action=directAccess.
   * @param htmlDir Relative path to directory used for FileBrowser's action=directAccess.
   */
  public function setHtmlDir($htmlDir) {
    $this->htmlDir_ = $htmlDir;
  }
 
  private $execPhpDir_ = '';
  /**
   * Relative path to directory used for FileBrowser's action=execPHP.
   * @return Relative path to directory used for FileBrowser's action=execPHP.
   */
  public function execPhpDir() {
    return $this->execPhpDir_;
  }
  /**
   * Set relative path to directory used for FileBrowser's action=execPHP.
   * @param execPhpDir Relative path to directory used for FileBrowser's action=execPHP.
   */
  public function setExecPhpDir($execPhpDir) {
    $this->execPhpDir_ = $execPhpDir;
  }
 
  private $tmpDir_ = '';
  /**
   * Relative path to temp directory.
   * @return Relative path to temp directory.
   * @note Write privileges are required.
   */
  public function tmpDir() {
    return $this->tmpDir_;
  }
  /**
   * Set relative path to temp directory.
   * @param tmpDir Relative path to temp directory.
   * @note Write privileges are required.
   */
  public function setTmpDir($tmpDir) {
    $this->tmpDir_ = $tmpDir;
  }
 
  private function __construct() {
    $this->documentRoot_ = $_SERVER['DOCUMENT_ROOT'];
  }
};
 
/**
 * Maintains includes of files containing class
 * definitions.\ Use this class rather than @b rquire_once().
 * @note It is safe to import already imported class.
 */
class ClassFactory {
  /**
   * Import class from a file inside classDir.
   * @param className Name of class to import
   */
  public static function importClass($className) {
    $config = Config::instance();
    $docRoot = $config->documentRoot();
    $classDir = $config->classDir();
    require_once($docRoot.$config->classDir().'/'.$className.'.class.php');
  }
};
 
/**
 * Maintain list of Javascript function invoked on page load (at client's side).
 * Design pattern singleton.
 */
class JSOnloadList {
  private static $instance_;
  /**
   * Access point to singleton instance.
   */
  public static function instance() {
    if (!isset(self::$instance_))
      self::$instance_ = new JSOnloadList;
    return self::$instance_;
  }
  private $onloadList = Array();
  /**
   * Add function to list. Function is placed as-is without any parameters.
   * Return value of function is ignored.
   * @param fnName Name of function to add to list.
   */
  public function addFunction($fnName) {
    $this->onloadList []= $fnName;
  }
  /**
   * Generate list of function. This has to be called inside XHTML &lt;head&gt; element.
   * @note This method can be called only once.
   */
  public function generate() {
    if (0== count($this->onloadList))
      return;
    ?>  <script type="text/javascript">
window.onload = function() {
<?php
    foreach($this->onloadList as $fn)
      echo '  '.$fn."();\n";
    ?>}
  </script>
<?php
  }
  private function __construct() { }
}
 
/**
 * Indentor is (x)html page source code indenting engine. This is useful for
 * reading the page source code by people. Browsers do not need this ;-)
 * Design patter singleton.
 * @note Copy-pasted from project IIS T-Shop
 */
class Indentor {
  /**
   * @return Return pointer to singleton object.
   */
  public static function singleton()
  {
    if (!isset(self::$instance)) {
      $c = __CLASS__;
      self::$instance = new $c;
    }
    return self::$instance;
  }
 
  /**
   * Set this constant to any indentation string you like to use.
   * Recomended values are a piece of spaces or an tabulator ("\t")
   */
  const INDENT_STRING= "  ";
 
  /**
   * You should call this method if you are about to nest.
   */
  public function enter() { $this->iLevel ++; }
  /**
   * You should call this method if you are about to leave nested block.
   */
  public function leave() { $this->iLevel --; }
  /**
   * @return Return current level of nest.
   */
  public function level() { return $this->iLevel; }
  /**
   * @return Return string containing indentation for current level of nest.
   */
  public function indent()
  {
    return str_repeat (self::INDENT_STRING, $this->level());
  }
  /**
   * Put indentation string direct to document.
   */
  public function indentDirect() { echo $this->indent(); }
  /**
   * Put indented given string direct to document.
   * @param szText String tu put (indented) to the document.
   */
  public function puts($szText)
  {
    echo $this->indent().$szText."\n";
  }
 
  // Prevent users to clone the instance
  public function __clone()
  {
    trigger_error('Clone is not allowed.', E_USER_ERROR);
  }
 
  private static $instance;
  private $iLevel;
  private function __construct() 
  {
    $this->iLevel= 0;
  }
};
 
/**
 * Maintain page language selection. Selection is stored
 * as session variable. Prefered is user language selection.
 * If there is no user selection, browser default is used.
 * Design pattern singleton.
 * @note Copy-pasted from archive.
 */
class LangSelect {
 
  /**
   * Access point to singleton.
   * @return reference Reference to singleton object.
   */
  public static function singleton ()
  {
    if (!isset (self::$_instance)) {
      $c= __CLASS__;
      self::$_instance= new $c;
    }
 
    return self::$_instance;
  }
 
  /**
   * Read client request for language selection. If there is no slection
   * browser default is used. This function should be called first
   * when a new page is being build.
   */
  public function handleLangSelect ()
  {
    if (isset ($_REQUEST ["lang"]))
      // User request for different language
      $_SESSION["lang"]= $_REQUEST["lang"];
 
    if (!isset($_SESSION["lang"]) || !ereg("(^en$)|(^cz$)",$_SESSION["lang"])) {
      // Read user-prefered languege from http_header
      if (ereg("(^cs)|(^cz)",$_SERVER["HTTP_ACCEPT_LANGUAGE"]))
        $_SESSION["lang"]="cz";
      else
        $_SESSION["lang"]="en";
    }
  }
 
  /**
   * @return string Selected language. For now, "en" and "cz" are possible.
   */
  public function getLang ()
  {
    return $_SESSION["lang"];
  }
 
  /**
   * @deprecated Use LangSelect::getLang method instead.
   * @return bool Return true if language "cz" is selected.
   */
  public function isLangCz ()
  {
    return ($this->getLang() == "cz");
  }
 
  private static $_instance;
 
  private function __construct ()
  {
    session_start ();
    session_register ("lang");
  }
 
  private function __clone ()
  {
    // TODO - exception??
  }
}
 
/**
 * Localization. L10n object maintain translation table.
 * Local replacement of gettext library.
 */
class L10n {
  private $langMap = Array();
  /**
   * Set translation of text.
   * @param lang Target language, 'en' is english (default).
   * @param msgid ID of string to translate.
   * @param msgstr Translation text
   */
  public function setTranslation($lang, $msgid, $msgstr) {
    if (!array_key_exists($lang, $this->langMap))
      $this->langMap[$lang] = Array();
    $this->langMap[$lang][$msgid] = $msgstr;
    if (!array_key_exists($msgid, $this->langMap['en']))
      $this->langMap['en'][$msgid] = $msgid;
  }
  /**
   * Return translation table for selected language.
   * @param lang Target language of desired translation.
   * When no lang parametr given, LangSelect singleton will be asked.
   * @return Return associative array containing text translations.
   */
  public function trMap($lang=null) {
    if (!isset($lang))
      $lang = LangSelect::singleton()->getLang();
    return $this->langMap[$lang];
  }
  /**
   * Translate string to specified language.
   * @param msgid ID of string to translate.
   * @param lang Target language of desired translation.
   * When no lang parametr given, LangSelect singleton will be asked.
   * @return Return translated string if available or msgid
   * if there is no suitable translation.
   */
  public function tr($msgid, $lang=null) {
    if (!isset($lang))
      $lang = LangSelect::singleton()->getLang();
    $msg = $msgid;
    if (array_key_exists($lang, $this->langMap)) {
      $lm = $this->langMap[$lang];
      if (array_key_exists($msgid, $lm))
        $msg = $lm[$msgid];
    }
    return $msg;
  }
  public function __construct() {
    $this->langMap['en'] = Array();
  }
};
 
/**
 * Page requested by user not found.
 * @param msg Message text to carry inside exception.
 */
class ExceptionNotFound extends Exception {
  /**
   * @param msg Message text to carry inside exception.
   */
  public function __construct($msg=null) {
    parent::__construct($msg);
  }
};
 
?>