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

Source for file redirect_composer_local.class.php

Documentation is available at redirect_composer_local.class.php

  1. <?php
  2. // *** Tangra (Application Framework and Tools for PHP)
  3. // $Id$
  4. //
  5.  
  6. /**
  7.  * Contains class Redirect_Composer_Local
  8.  *
  9.  * @package tangra_lib
  10.  * @subpackage  web_site
  11.  */
  12.  
  13. /**
  14.  *
  15.  */
  16. require_once(TANGRA_MAIN_DIR.'web_site/web_context.class.php');
  17.  
  18. /**
  19.  *
  20.  */
  21. require_once(TANGRA_MAIN_DIR.'exceptions/te_key_already_exists.class.php');
  22.  
  23. /**
  24.  *
  25.  */
  26. require_once('redirect_composer.class.php');
  27.  
  28. /**
  29.  * Redirect_Composer_Local is intended to be used for internal redirect within site
  30.  *
  31.  * @see Redirect_Composer
  32.  * @package tangra_lib
  33.  * @subpackage  web_site
  34.  */
  35.     /**
  36.      * Holds Web_Context
  37.      *
  38.      * @var Web_Context 
  39.      */
  40.     private $_context;
  41.  
  42.     
  43.     /**
  44.      * 
  45.      * Holds protocol: http, https or ''
  46.      * '' (empty) value means keep current
  47.      * @var string 
  48.      */
  49.     private $protocol;
  50.  
  51.     /**
  52.      * 
  53.      * Holds port that will be used for composing
  54.      * @var integer 
  55.      */
  56.     private $port;
  57.     
  58.     /**
  59.      * Constructor
  60.      *
  61.      * @param string $target Target address
  62.      * @param array $param_pairs Parameters that will be added to target address ($key => $value pairs)
  63.      * @throws Tangra_Exception
  64.      */
  65.     function __construct(Web_Context $context$target$param_pairs array()$protocol ''$port 0{
  66.         $this->set_context($context);
  67.         parent::__construct($target$param_pairs);
  68.         
  69.         $this->set_protocol($protocol);
  70.         $this->set_port($port);
  71.     }
  72.  
  73.     /**
  74.      * Sets context
  75.      *
  76.      * @param Web_Context $context 
  77.      * @internal
  78.      */
  79.     private function set_context(Web_Context $context{
  80.         $this->_context = $context;
  81.     }
  82.  
  83.  
  84.     /**
  85.      * Returns reference to context
  86.      *
  87.      * @return Web_Context 
  88.      */
  89.     public function get__context({
  90.         return $this->_context;
  91.     }
  92.  
  93.     /**
  94.      * 
  95.      * Sets protocol.
  96.      * @param string $protocol Valid values are http, https and '' (empty). Empty value means "use current" (autodetected)
  97.      * @throws Tangra_Expception
  98.      */
  99.     public function set_protocol($protocol{
  100.         if ($protocol == '' || $protocol == 'http' || $protocolo == 'https'{
  101.             $this->protocol = $protocol;
  102.         else {
  103.             throw new Tangra_Expception('Invalid protocol value. Must be "http", "https" or empty. Current value: '.$protocol);
  104.         }
  105.     }
  106.     
  107.     /**
  108.      * 
  109.      * Returns $protocol
  110.      */
  111.     public function get_protocol({
  112.         return $this->protocol;
  113.     }
  114.  
  115.     
  116.     /**
  117.      * 
  118.      * Sets port to be used when composing
  119.      * Valid values:
  120.      * integer - sets port to specified value
  121.      * 0 - "use current" (autodetected)
  122.      * NULL - sets port to NULL which means don't set port when composing
  123.      * @param integer $port 
  124.      */
  125.     public function set_port($port 0{
  126.         if ($port{
  127.             tangra_if_not_int_throw_e($port);
  128.         }
  129.         
  130.         $this->port = $port;
  131.     }
  132.     
  133.     
  134.     /**
  135.      * 
  136.      * Returns port
  137.      */
  138.     public function get_port({
  139.         return $this->port;
  140.     }
  141.     
  142.     
  143.     /**
  144.      * Returns composed target address with parameters
  145.      *
  146.      * @param Web_Context $context 
  147.      * @return string 
  148.      */
  149.     public function get_target_address({
  150.         $context $this->get__context();
  151.  
  152.         $this->add_threads_manager_var($context);
  153.  
  154.         $server $context->get_from_server('SERVER_NAME');
  155.         $port $this->get_port();
  156.         
  157.         $protocol $this->get_protocol();
  158.         if (!$protocol{
  159.             if ($context->exists_in_server('HTTPS')) {
  160.                 $https $context->get_from_server('HTTPS');
  161.             else {
  162.                 $https false;
  163.             }
  164.     
  165.             $protocol ($https'https' 'http';
  166.         }
  167.         
  168.         
  169.         if (is_null($port)) {
  170.             $port '';            
  171.         elseif $port == 0{
  172.             $port =  $context->get_from_server('SERVER_PORT');
  173.             if (($port == '80' && $protocol == 'http'||  ($port == '443' && $protocol == 'https')) {
  174.                 $port '';
  175.             else {
  176.                 $port ':'.$port;
  177.             }
  178.         else {
  179.             $port ':'.$port;
  180.         }
  181.         
  182.         $params $this->get_params();
  183.  
  184.         $pi_vars $this->get_pi_string();
  185.  
  186.         if (substr($this->get_target()-1== '/'{
  187.             $ret $protocol.'://'.$server.$port.'/'.$this->get_target().$pi_vars.$params;
  188.         else {
  189.             $ret $protocol.'://'.$server.$port.'/'.$this->get_target().'/'.$pi_vars.$params;
  190.         }
  191.  
  192.         return $ret;
  193.     }
  194.  
  195.  
  196.     /**
  197.      * Adds threads manager var to parameters
  198.      *
  199.      * @param Web_Context $context 
  200.      * @internal
  201.      */
  202.     private function add_threads_manager_var(Web_Context $context{
  203.         if (defined('THREADS_MANAGER_NAME')) {
  204.             $svm_var_name Session_Vars_Manager::get_svm_var_name();
  205.             $svm $context->get_from_session($svm_var_name);
  206.  
  207.  
  208.             if ($svm->is_global_var_registered(THREADS_MANAGER_NAME)) {
  209.                 $tm $svm->get_global_var(THREADS_MANAGER_NAME);
  210.  
  211.                 // removing if already exists. Needed in case of login redirect or in case of user mistakes
  212.                 if ($this->param_exists(THREADS_MANAGER_NAME)) {
  213.                     $this->remove_param(THREADS_MANAGER_NAME);
  214.                 }
  215.  
  216.                 $this->add_param_pair($tm->get_url_rewrite_var_name()$tm->get_current_url_var());
  217.             }
  218.  
  219.         }
  220.     }
  221.  
  222.  
  223.     private function get_pi_string({
  224.         $ret '';
  225.  
  226.         if (function_exists('_get_pi_string')) {
  227.             $ret _get_pi_string();
  228.         }
  229.  
  230.         return $ret;
  231.     }
  232. }