tangra logo
   
[ class tree: tangra_lib ] [ index: tangra_lib ] [ all elements ]
 

Source for file web_site_config.class.php

Documentation is available at web_site_config.class.php

  1. <?php
  2. // *** Tangra (Application Framework and Tools for PHP)
  3. // $Id$
  4. //
  5.  
  6. /**
  7.  * Contains class Web_Site_Config
  8.  *
  9.  * @package tangra_lib
  10.  * @subpackage  web_site
  11.  */
  12.  
  13. /**
  14.  *
  15.  */
  16. require_once(TANGRA_MAIN_DIR.'core/misc_functions.inc.php');
  17. /**
  18.  *
  19.  */
  20. require_once(TANGRA_MAIN_DIR.'exceptions/te_web_site_config_error.class.php');
  21. /**
  22.  *
  23.  */
  24. require_once(TANGRA_MAIN_DIR.'filesystem_toolbox/filesystem_functions.inc.php');
  25.  
  26.  
  27. /**
  28.  * Basic web site configuration
  29.  *
  30.  * @package tangra_lib
  31.  * @subpackage  web_site
  32.  */
  33. abstract class Web_Site_Config extends Tangra_Class {
  34.     /**
  35.      * Development mode
  36.      *
  37.      */
  38.     const SITE_MODE_DEVELOPMENT = 0;
  39.     /**
  40.      * Production mode
  41.      *
  42.      */
  43.     const SITE_MODE_PRODUCTION = 1;
  44.  
  45.     /**
  46.      * Site system name
  47.      *
  48.      * @var string 
  49.      * @internal
  50.      */
  51.     private $site_name;
  52.     /**
  53.      * Site's root absolute path. It is not docroot, see bellow!
  54.      *
  55.      * @var string 
  56.      * @internal
  57.      */
  58.     private $root_dir;
  59.     /**
  60.      * Document root absolute path
  61.      *
  62.      * @var string 
  63.      * @internal
  64.      */
  65.     private $document_root_dir;
  66.     /**
  67.      * Document root relative to $document_root_dir
  68.      *
  69.      * @var string 
  70.      * @internal
  71.      */
  72.     private $document_root_relative;
  73.     /**
  74.      * "Hidden" dir absolute path
  75.      *
  76.      * @var string 
  77.      * @internal
  78.      */
  79.     private $hidden_dir//hidden absolute path
  80.     /**
  81.      * "Scratch" dir absolute path
  82.      *
  83.      * @var string 
  84.      * @internal
  85.      */
  86.     private $scratch_dir;
  87.     /**
  88.      * ext_inc dir absolute path
  89.      *
  90.      * @var string 
  91.      * @internal
  92.      */
  93.     private $ext_inc_dir;
  94.     /**
  95.      * Default encoding for the site
  96.      *
  97.      * @var string 
  98.      * @internal
  99.      */
  100.     private $default_encoding;
  101.     /**
  102.      * Address of server error page
  103.      *
  104.      * @var string 
  105.      * @internal
  106.      */
  107.     private $server_error_page = 'server_error.php';
  108.     /**
  109.      * Error reporting level
  110.      * Default is 4095
  111.      *
  112.      * @var integer 
  113.      * @internal
  114.      */
  115.     private $error_reporting = 4095;
  116.     /**
  117.      * Error reporting for PHP4 libs
  118.      * Default is 2047
  119.      *
  120.      * @var integer 
  121.      * @internal
  122.      */
  123.     private $error_reporting_php4_libs = 2047;
  124.     /**
  125.      * Site mode
  126.      *
  127.      * @var integer 
  128.      * @internal
  129.      */
  130.     private $site_mode = Web_Site_Config::SITE_MODE_DEVELOPMENT;
  131.     /**
  132.      * Current hostname
  133.      *
  134.      * @var string 
  135.      * @internal
  136.      */
  137.     private $current_host;
  138.     /**
  139.      * Current port
  140.      *
  141.      * @var integer 
  142.      * @internal
  143.      */
  144.     private $current_port;
  145.     /**
  146.      * Current protocol
  147.      *
  148.      * @var string 
  149.      * @internal
  150.      */
  151.     private $current_protocol;
  152.     /**
  153.      * Base URL
  154.      *
  155.      * @var string 
  156.      * @internal
  157.      */
  158.     private $base_url;
  159.     /**
  160.      * Shall tangra be exposed as http header like X-Powered-By: Tangra Framework for PHP/2.*
  161.      *
  162.      * @var unknown_type 
  163.      */
  164.     private $expose_tangra;
  165.  
  166.     /**
  167.      * Used to distigush installations on different servers.
  168.      * This is free form string
  169.      */
  170.     private $instance_name;
  171.  
  172.     /**
  173.      * Sets site name
  174.      *
  175.      * @param string $site_name 
  176.      */
  177.     public function set_site_name($site_name{
  178.         $site_name trim($site_name);
  179.         $this->site_name = $site_name;
  180.     }
  181.  
  182.  
  183.     /**
  184.      * Gets site name
  185.      *
  186.      * @return unknown 
  187.      */
  188.     public function get_site_name({
  189.         return $this->site_name;
  190.     }
  191.  
  192.  
  193.     /**
  194.      * Sets site's root directory absolute path. It is not docroot, see bellow!
  195.      *
  196.      * @param string $root_dir 
  197.      */
  198.     public function set_root_dir($root_dir{
  199.         $root_dir trim($root_dir);
  200.         if (tangra_dir_exists($root_dir)) {
  201.             $this->root_dir = tangra_normalize_path($root_dir);
  202.         else {
  203.             throw new TE_Web_Site_Config_Error('Invalid $root_dir (MS_root_dir). Not exists or is not a dir. Current value: '.$root_dir);
  204.         }
  205.     }
  206.  
  207.  
  208.     /**
  209.      * Returns site's root directory absolute path
  210.      *
  211.      * @return unknown 
  212.      */
  213.     public function get_root_dir({
  214.         return $this->root_dir;
  215.     }
  216.  
  217.  
  218.     /**
  219.      * Sets document root absolute path
  220.      *
  221.      * @param string $document_root_dir 
  222.      */
  223.     public function set_document_root_dir($document_root_dir NULL{
  224.         if ($document_root_dir != NULL{
  225.             $this->_set_document_root_dir($document_root_dir);
  226.         else {
  227.             $this->_set_document_root_dir($this->get_root_dir().'/htdocs/');
  228.         }
  229.     }
  230.  
  231.  
  232.     /**
  233.      * Sets document root absolute path
  234.      *
  235.      * @param unknown_type $document_root_dir 
  236.      * @internal
  237.      * @throws TE_Web_Site_Config_Error
  238.      */
  239.     private function _set_document_root_dir($document_root_dir{
  240.         if (tangra_dir_exists($document_root_dir)) {
  241.             $this->document_root_dir = tangra_normalize_path($document_root_dir);
  242.         else {
  243.             throw new TE_Web_Site_Config_Error('Invalid document_root_dir (MS_DOCUMENT_root_dir). Not exists or is not a dir. Current value: '.document_root_dir);
  244.         }
  245.     }
  246.  
  247.  
  248.     /**
  249.      * Returns document root absolute path
  250.      *
  251.      * @return string 
  252.      */
  253.     public function get_document_root_dir({
  254.         return $this->document_root_dir;
  255.     }
  256.  
  257.  
  258.     /**
  259.      * Alias of set_document_root_dir()
  260.      *
  261.      * @param string $document_root_dir 
  262.      */
  263.     public function set_htdocs_dir($document_root_dir NULL{
  264.         $this->set_document_root_dir($document_root_dir);
  265.     }
  266.  
  267.  
  268.     /**
  269.      * Alias of get_document_root_dir();
  270.      *
  271.      * @return string 
  272.      */
  273.     public function get_htdocs_dir({
  274.         return $this->get_document_root_dir();
  275.     }
  276.  
  277.  
  278.     /**
  279.      * Sets hidden dir absolute path
  280.      *
  281.      * @param string $hidden_dir 
  282.      */
  283.     public function set_hidden_dir($hidden_dir NULL{
  284.         if ($hidden_dir != NULL {
  285.             $this->_set_hidden_dir($hidden_dir);
  286.         else {
  287.             $this->_set_hidden_dir($this->get_root_dir().'/hidden/');
  288.         }
  289.     }
  290.  
  291.  
  292.     /**
  293.      * Sets hidden dir absolute path
  294.      *
  295.      * @param string $hidden_dir 
  296.      * @internal
  297.      * @throws TE_Web_Site_Config_Error
  298.      */
  299.     private function _set_hidden_dir($hidden_dir{
  300.         if (tangra_dir_exists($hidden_dir)) {
  301.             $this->hidden_dir = tangra_normalize_path($hidden_dir);
  302.         else {
  303.             throw new TE_Web_Site_Config_Error('Invalid $hidden_dir (MS_hidden_dir). Not exists or is not a dir. Current value: '.$hidden_dir);
  304.         }
  305.     }
  306.  
  307.  
  308.     /**
  309.      * Returns hidden dir absolute path
  310.      *
  311.      * @return string 
  312.      */
  313.     public function get_hidden_dir({
  314.         return $this->hidden_dir;
  315.     }
  316.  
  317.  
  318.     /**
  319.      * Sets scratch dir absolute path
  320.      *
  321.      * @param string $scratch_dir 
  322.      */
  323.     public function set_scratch_dir($scratch_dir NULL{
  324.         if ($scratch_dir != NULL{
  325.             $this->_set_scratch_dir($scratch_dir);
  326.         else {
  327.             $this->_set_scratch_dir($this->get_root_dir().'/scratch/');
  328.         }
  329.     }
  330.  
  331.  
  332.     /**
  333.      * Sets scratch dir absolute path
  334.      *
  335.      * @param string $scratch_dir 
  336.      * @throws TE_Web_Site_Config_Error
  337.      */
  338.     private function _set_scratch_dir($scratch_dir{
  339.         if (tangra_dir_exists($scratch_dir)) {
  340.             $this->scratch_dir = tangra_normalize_path($scratch_dir);
  341.         else {
  342.             throw new TE_Web_Site_Config_Error('Invalid $scratch_dir (MS_scratch_dir). Not exists or is not a dir. Current value: '.$scratch_dir);
  343.         }
  344.     }
  345.  
  346.  
  347.     /**
  348.      * Returns scratch dir absolute path
  349.      *
  350.      * @return string 
  351.      */
  352.     public function get_scratch_dir({
  353.         return $this->scratch_dir;
  354.     }
  355.  
  356.  
  357.     /**
  358.      * Sets ext_inc dir absolute path
  359.      *
  360.      * @param string $ext_inc_dir 
  361.      */
  362.     public function set_ext_inc_dir($ext_inc_dir NULL{
  363.         if ($ext_inc_dir != NULL{
  364.             $this->_set_ext_inc_dir($ext_inc_dir);
  365.         else {
  366.             $this->_set_ext_inc_dir($this->get_root_dir().'/ext_inc/');
  367.         }
  368.     }
  369.  
  370.  
  371.     /**
  372.      * Sets ext_inc dir absolute path
  373.      *
  374.      * @param string $ext_inc_dir 
  375.      * @throws TE_Web_Site_Config_Error
  376.      */
  377.     private function _set_ext_inc_dir($ext_inc_dir{
  378.         if (tangra_dir_exists($ext_inc_dir)) {
  379.             $this->ext_inc_dir = tangra_normalize_path($ext_inc_dir);
  380.         else {
  381.             throw new TE_Web_Site_Config_Error('Invalid $ext_inc_dir (MS_ext_inc_dir). Not exists or is not a dir. Current value: '.$ext_inc_dir);
  382.         }
  383.     }
  384.  
  385.  
  386.     /**
  387.      * Returns ext_inc dir absolute path
  388.      *
  389.      * @return string 
  390.      */
  391.     public function get_ext_inc_dir({
  392.         return $this->ext_inc_dir;
  393.     }
  394.  
  395.  
  396.     /**
  397.      * Sets default encoding for the site
  398.      * Example: set_default_encoding('utf-8');
  399.      *
  400.      * @param string $default_encoding 
  401.      */
  402.     public function set_default_encoding($default_encoding{
  403.         //TODO - check against list of valid encodings
  404.         $this->default_encoding = $default_encoding;
  405.     }
  406.  
  407.  
  408.     /**
  409.      * Returns default encoding of the site
  410.      *
  411.      * @return string 
  412.      */
  413.     public function get_default_encoding({
  414.         return $this->default_encoding;
  415.     }
  416.  
  417.  
  418.     /**
  419.      * Returns boot dir
  420.      *
  421.      * @return string 
  422.      */
  423.     public function get_boot_dir({
  424.         return $this->get_hidden_dir().'boot/';
  425.     }
  426.  
  427.  
  428.     /**
  429.      * Returns conf dir
  430.      *
  431.      * @return string 
  432.      */
  433.     public function get_conf_dir({
  434.         return $this->get_hidden_dir().'conf/';
  435.     }
  436.  
  437.  
  438.     /**
  439.      * Returns cron_jobs dir
  440.      *
  441.      * @return string 
  442.      */
  443.     public function get_cron_jobs_dir({
  444.         return $this->get_hidden_dir().'cron_jobs/';
  445.     }
  446.  
  447.  
  448.     /**
  449.      * Returns site include dir
  450.      *
  451.      * @return string 
  452.      */
  453.     public function get_site_inc_dir({
  454.         return $this->get_hidden_dir().'inc/';
  455.     }
  456.  
  457.     /**
  458.      * Alias of get_site_inc_dir()
  459.      *
  460.      * @return string 
  461.      */
  462.     public function get_inc_dir({
  463.         return $this->get_hidden_dir().'inc/';
  464.     }
  465.  
  466.  
  467.     /**
  468.      * Alias of get_site_inc_dir
  469.      *
  470.      * @return string 
  471.      */
  472.     public function get_sid({
  473.         return $this->get_site_inc_dir();
  474.     }
  475.  
  476.  
  477.     /**
  478.      * Returns rc.d directory
  479.      *
  480.      * @return string 
  481.      */
  482.     public function get_rc_d_dir({
  483.         return $this->get_hidden_dir().'rc.d/';
  484.     }
  485.  
  486.  
  487.     /**
  488.      * Returns templates directory
  489.      *
  490.      * @return string 
  491.      */
  492.     public function get_tpl_dir({
  493.         return $this->get_hidden_dir().'tpl/';
  494.     }
  495.  
  496.  
  497.     /**
  498.      * Returns DB directory
  499.      *
  500.      * (For sqlite, if used)
  501.      *
  502.      * @return string 
  503.      */
  504.     public function get_db_dir({
  505.         return $this->get_scratch_dir().'db/';
  506.     }
  507.  
  508.  
  509.     /**
  510.      * Returns logs directory
  511.      *
  512.      * @return string 
  513.      */
  514.     public function get_logs_dir({
  515.         return $this->get_scratch_dir().'logs/';
  516.     }
  517.  
  518.  
  519.     /**
  520.      * Returns templates scratch directory
  521.      *
  522.      * @return string 
  523.      */
  524.     public function get_scratch_tpl_dir({
  525.         return $this->get_scratch_dir().'tpl/';
  526.     }
  527.  
  528.  
  529.     /**
  530.      * Return uplaods dir
  531.      *
  532.      * @return string 
  533.      */
  534.     public function get_uploads_dir({
  535.         return $this->get_scratch_dir().'uploads/';
  536.     }
  537.  
  538.  
  539.     /**
  540.      * Return debug dir
  541.      *
  542.      * @return string 
  543.      */
  544.     public function get_debug_dir({
  545.         return $this->get_scratch_dir().'debug/';
  546.     }
  547.  
  548.  
  549.     /**
  550.      * Sets document root relative directory (relative to document root (set with set_root_dir()))
  551.      *
  552.      * @param string $document_root_relative 
  553.      */
  554.     public function set_document_root_relative($document_root_relative{
  555.         if ($document_root_relative{
  556.             $this->document_root_relative = $document_root_relative;
  557.         else {
  558.             $this->document_root_relative = '/';
  559.         }
  560.     }
  561.  
  562.  
  563.     /**
  564.      * Gets document root relative directory
  565.      *
  566.      * @return string 
  567.      */
  568.     public function get_document_root_relative({
  569.         return $this->document_root_relative;
  570.     }
  571.  
  572.  
  573.     /**
  574.      * Sets server error page address
  575.      *
  576.      * @param string $server_error_page 
  577.      */
  578.     public function set_server_error_page($server_error_page{
  579.         $this->server_error_page = $server_error_page ?
  580.                                     $server_error_page :
  581.                                     'server_error.php';
  582.     }
  583.  
  584.  
  585.     /**
  586.      * Returns server error page address
  587.      *
  588.      * @return string 
  589.      */
  590.     public function get_server_error_page({
  591.         return $this->server_error_page;
  592.     }
  593.  
  594.  
  595.     /**
  596.      * Sets error reporting level
  597.      *
  598.      * @param integer $error_reporting 
  599.      * @throws TE_Web_Site_Config_Error
  600.      */
  601.     public function set_error_reporting($error_reporting{
  602.         if (tangra_is_int($error_reporting)) {
  603.             $this->error_reporting = $error_reporting;
  604.         else {
  605.             throw new TE_Web_Site_Config_Error('Invalid value for $error_reporting. Must be integer. Current value: '.$err);
  606.         }
  607.     }
  608.  
  609.  
  610.     /**
  611.      * Returns error_reporting level
  612.      *
  613.      * @return integer 
  614.      */
  615.     public function get_error_reporting({
  616.         return $this->error_reporting;
  617.     }
  618.  
  619.  
  620.     /**
  621.      * Sets error reporting for PHP4 libraries
  622.      *
  623.      * @param integer $error_reporting 
  624.      */
  625.     public function set_error_reporting_php4_libs($error_reporting{
  626.         if (tangra_is_int($error_reporting)) {
  627.             $this->error_reporting_php4_libs = $error_reporting;
  628.         else {
  629.             throw new TE_Web_Site_Config_Error('Invalid value for $error_reporting_php4_libs. Must be integer. Current value: '.$err);
  630.         }
  631.     }
  632.  
  633.  
  634.     /**
  635.      * Returns error reporting for PHP4 libraries
  636.      *
  637.      * @return integer 
  638.      */
  639.     public function get_error_reporting_php4_libs({
  640.         return $this->error_reporting_php4_libs;
  641.     }
  642.  
  643.  
  644.     /**
  645.      * Sets site mode
  646.      *
  647.      * Web_Site_Config::SITE_MODE_DEVELOPMENT or Web_Site_Config::SITE_MODE_PRODUCTION
  648.      *
  649.      * @param integer $site_mode 
  650.      */
  651.     public function set_site_mode($site_mode{
  652.         if ($site_mode == Web_Site_Config::SITE_MODE_DEVELOPMENT || $site_mode == Web_Site_Config::SITE_MODE_PRODUCTION{
  653.             $this->site_mode = $site_mode;
  654.         else {
  655.             throw new TE_Web_Site_Config_Error('Invalid value for $site_mode. Current value: '.$site_mode);
  656.         }
  657.     }
  658.  
  659.  
  660.     /**
  661.      * Returns site mode
  662.      *
  663.      * @return integer 
  664.      */
  665.     public function get_site_mode({
  666.         return $this->site_mode;
  667.     }
  668.  
  669.  
  670.     /**
  671.      * Sets current hostname
  672.      *
  673.      * @param string $current_host 
  674.      */
  675.     public function set_current_host($current_host{
  676.         $this->current_host = $current_host;
  677.     }
  678.  
  679.  
  680.     /**
  681.      * Returns current hostname
  682.      *
  683.      * @return string 
  684.      */
  685.     public function get_current_host({
  686.         return $this->current_host;
  687.     }
  688.  
  689.  
  690.     /**
  691.      * Sets current port
  692.      *
  693.      * @param integer $current_port 
  694.      */
  695.     public function set_current_port($current_port{
  696.         tangra_if_not_int_throw_e($current_port);
  697.         $this->current_port = $current_port;
  698.     }
  699.  
  700.     /**
  701.      * Returns current port
  702.      *
  703.      * @return integer 
  704.      */
  705.     public function get_current_port({
  706.         return $this->current_port;
  707.     }
  708.  
  709.  
  710.     /**
  711.      * Sets current protocol
  712.      *
  713.      * @param string $current_protocol 
  714.      */
  715.     public function set_current_protocol($current_protocol{
  716.         $this->current_protocol = $current_protocol;
  717.     }
  718.  
  719.  
  720.     /**
  721.      * Returns current protocol
  722.      *
  723.      * @return unknown 
  724.      */
  725.     public function get_current_protocol({
  726.         return $this->current_protocol;
  727.     }
  728.  
  729.  
  730.     /**
  731.      * Sets base URL
  732.      *
  733.      * @param string $base_url 
  734.      */
  735.     public function set_base_url($base_url{
  736.         $this->base_url = $base_url;
  737.     }
  738.  
  739.  
  740.     /**
  741.      * Returns base URL
  742.      *
  743.      * @return string 
  744.      */
  745.     public function get_base_url({
  746.         return $this->base_url;
  747.     }
  748.  
  749.  
  750.     /**
  751.      * Sets instance name
  752.      *
  753.      * @param string $instance_name 
  754.      */
  755.     public function set_instance_name($instance_name{
  756.         $this->instance_name = $instance_name;
  757.     }
  758.  
  759.  
  760.     /**
  761.      * Returns instance name
  762.      *
  763.      * @return string 
  764.      */
  765.     public function get_instance_name({
  766.         return $this->instance_name;
  767.     }
  768.  
  769.  
  770.  
  771.     /**
  772.      * Sets expose tangra flag. If true http header like X-Powered-By: Tangra Framework for PHP/2.* will be generated
  773.      *
  774.      * @param boolean $expose_tangra 
  775.      */
  776.     public function set_expose_tangra($expose_tangra{
  777.         $this->expose_tangra = $expose_tangra true false;
  778.     }
  779.  
  780.  
  781.     /**
  782.      * Returns expose tangra flag
  783.      *
  784.      * @return boolean 
  785.      */
  786.     public function get_expose_tangra({
  787.         return $this->expose_tangra;
  788.     }
  789.  
  790.  
  791. //    public function set_($) {
  792. //        $this-> = $;
  793. //    }
  794. //
  795. //
  796. //    public function get_() {
  797. //        return $this->;
  798. //    }
  799.  
  800. }