home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 February (DVD) / PCWorld_2008-02_DVD.iso / v cisle / PHP / PHP.exe / xampp-win32-1.6.5-installer.exe / php / PEAR / HTML / Common.php < prev    next >
Encoding:
PHP Script  |  2007-12-20  |  12.8 KB  |  471 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group             |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Adam Daniel <adaniel1@eesus.jnj.com>                         |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Common.php,v 1.13 2006/10/08 14:10:46 avb Exp $
  20.  
  21. /**
  22.  * Base class for all HTML classes
  23.  *
  24.  * @author    Adam Daniel <adaniel1@eesus.jnj.com>
  25.  * @category  HTML
  26.  * @package   HTML_Common
  27.  * @version   1.2.3
  28.  * @abstract
  29.  */
  30.  
  31. /**
  32.  * Base class for all HTML classes
  33.  *
  34.  * @author      Adam Daniel <adaniel1@eesus.jnj.com>
  35.  * @version     1.7
  36.  * @since       PHP 4.0.3pl1
  37.  * @abstract
  38.  */
  39. class HTML_Common {
  40.  
  41.     /**
  42.      * Associative array of table attributes
  43.      * @var     array
  44.      * @access  private
  45.      */
  46.     var $_attributes = array();
  47.  
  48.     /**
  49.      * Tab offset of the table
  50.      * @var     int
  51.      * @access  private
  52.      */
  53.     var $_tabOffset = 0;
  54.  
  55.     /**
  56.      * Tab string
  57.      * @var       string
  58.      * @since     1.7
  59.      * @access    private
  60.      */
  61.     var $_tab = "\11";
  62.  
  63.     /**
  64.      * Contains the line end string
  65.      * @var       string
  66.      * @since     1.7
  67.      * @access    private
  68.      */
  69.     var $_lineEnd = "\12";
  70.  
  71.     /**
  72.      * HTML comment on the object
  73.      * @var       string
  74.      * @since     1.5
  75.      * @access    private
  76.      */
  77.     var $_comment = '';
  78.  
  79.     /**
  80.      * Class constructor
  81.      * @param    mixed   $attributes     Associative array of table tag attributes
  82.      *                                   or HTML attributes name="value" pairs
  83.      * @param    int     $tabOffset      Indent offset in tabs
  84.      * @access   public
  85.      */
  86.     function HTML_Common($attributes = null, $tabOffset = 0)
  87.     {
  88.         $this->setAttributes($attributes);
  89.         $this->setTabOffset($tabOffset);
  90.     } // end constructor
  91.  
  92.     /**
  93.      * Returns the current API version
  94.      * @access   public
  95.      * @returns  double
  96.      */
  97.     function apiVersion()
  98.     {
  99.         return 1.7;
  100.     } // end func apiVersion
  101.  
  102.     /**
  103.      * Returns the lineEnd
  104.      *
  105.      * @since     1.7
  106.      * @access    private
  107.      * @return    string
  108.      */
  109.     function _getLineEnd()
  110.     {
  111.         return $this->_lineEnd;
  112.     } // end func getLineEnd
  113.  
  114.     /**
  115.      * Returns a string containing the unit for indenting HTML
  116.      *
  117.      * @since     1.7
  118.      * @access    private
  119.      * @return    string
  120.      */
  121.     function _getTab()
  122.     {
  123.         return $this->_tab;
  124.     } // end func _getTab
  125.  
  126.     /**
  127.      * Returns a string containing the offset for the whole HTML code
  128.      *
  129.      * @return    string
  130.      * @access   private
  131.      */
  132.     function _getTabs()
  133.     {
  134.         return str_repeat($this->_getTab(), $this->_tabOffset);
  135.     } // end func _getTabs
  136.  
  137.     /**
  138.      * Returns an HTML formatted attribute string
  139.      * @param    array   $attributes
  140.      * @return   string
  141.      * @access   private
  142.      */
  143.     function _getAttrString($attributes)
  144.     {
  145.         $strAttr = '';
  146.  
  147.         if (is_array($attributes)) {
  148.             $charset = HTML_Common::charset();
  149.             foreach ($attributes as $key => $value) {
  150.                 $strAttr .= ' ' . $key . '="' . htmlspecialchars($value, ENT_COMPAT, $charset) . '"';
  151.             }
  152.         }
  153.         return $strAttr;
  154.     } // end func _getAttrString
  155.  
  156.     /**
  157.      * Returns a valid atrributes array from either a string or array
  158.      * @param    mixed   $attributes     Either a typical HTML attribute string or an associative array
  159.      * @access   private
  160.      * @return   array
  161.      */
  162.     function _parseAttributes($attributes)
  163.     {
  164.         if (is_array($attributes)) {
  165.             $ret = array();
  166.             foreach ($attributes as $key => $value) {
  167.                 if (is_int($key)) {
  168.                     $key = $value = strtolower($value);
  169.                 } else {
  170.                     $key = strtolower($key);
  171.                 }
  172.                 $ret[$key] = $value;
  173.             }
  174.             return $ret;
  175.  
  176.         } elseif (is_string($attributes)) {
  177.             $preg = "/(([A-Za-z_:]|[^\\x00-\\x7F])([A-Za-z0-9_:.-]|[^\\x00-\\x7F])*)" .
  178.                 "([ \\n\\t\\r]+)?(=([ \\n\\t\\r]+)?(\"[^\"]*\"|'[^']*'|[^ \\n\\t\\r]*))?/";
  179.             if (preg_match_all($preg, $attributes, $regs)) {
  180.                 for ($counter=0; $counter<count($regs[1]); $counter++) {
  181.                     $name  = $regs[1][$counter];
  182.                     $check = $regs[0][$counter];
  183.                     $value = $regs[7][$counter];
  184.                     if (trim($name) == trim($check)) {
  185.                         $arrAttr[strtolower(trim($name))] = strtolower(trim($name));
  186.                     } else {
  187.                         if (substr($value, 0, 1) == "\"" || substr($value, 0, 1) == "'") {
  188.                             $value = substr($value, 1, -1);
  189.                         }
  190.                         $arrAttr[strtolower(trim($name))] = trim($value);
  191.                     }
  192.                 }
  193.                 return $arrAttr;
  194.             }
  195.         }
  196.     } // end func _parseAttributes
  197.  
  198.     /**
  199.      * Returns the array key for the given non-name-value pair attribute
  200.      *
  201.      * @param     string    $attr         Attribute
  202.      * @param     array     $attributes   Array of attribute
  203.      * @since     1.0
  204.      * @access    private
  205.      * @return    bool
  206.      */
  207.     function _getAttrKey($attr, $attributes)
  208.     {
  209.         if (isset($attributes[strtolower($attr)])) {
  210.             return true;
  211.         } else {
  212.             return null;
  213.         }
  214.     } //end func _getAttrKey
  215.  
  216.     /**
  217.      * Updates the attributes in $attr1 with the values in $attr2 without changing the other existing attributes
  218.      * @param    array   $attr1      Original attributes array
  219.      * @param    array   $attr2      New attributes array
  220.      * @access   private
  221.      */
  222.     function _updateAttrArray(&$attr1, $attr2)
  223.     {
  224.         if (!is_array($attr2)) {
  225.             return false;
  226.         }
  227.         foreach ($attr2 as $key => $value) {
  228.             $attr1[$key] = $value;
  229.         }
  230.     } // end func _updateAtrrArray
  231.  
  232.     /**
  233.      * Removes the given attribute from the given array
  234.      *
  235.      * @param     string    $attr           Attribute name
  236.      * @param     array     $attributes     Attribute array
  237.      * @since     1.4
  238.      * @access    private
  239.      * @return    void
  240.      */
  241.     function _removeAttr($attr, &$attributes)
  242.     {
  243.         $attr = strtolower($attr);
  244.         if (isset($attributes[$attr])) {
  245.             unset($attributes[$attr]);
  246.         }
  247.     } //end func _removeAttr
  248.  
  249.     /**
  250.      * Returns the value of the given attribute
  251.      *
  252.      * @param     string    $attr   Attribute name
  253.      * @since     1.5
  254.      * @access    public
  255.      * @return    string|null   returns null if an attribute does not exist
  256.      */
  257.     function getAttribute($attr)
  258.     {
  259.         $attr = strtolower($attr);
  260.         if (isset($this->_attributes[$attr])) {
  261.             return $this->_attributes[$attr];
  262.         }
  263.         return null;
  264.     } //end func getAttribute
  265.  
  266.     /**
  267.      * Sets the value of the attribute
  268.      *
  269.      * @param   string  Attribute name
  270.      * @param   string  Attribute value (will be set to $name if omitted)
  271.      * @access  public
  272.      */
  273.     function setAttribute($name, $value = null)
  274.     {
  275.         $name = strtolower($name);
  276.         if (is_null($value)) {
  277.             $value = $name;
  278.         }
  279.         $this->_attributes[$name] = $value;
  280.     } // end func setAttribute
  281.  
  282.     /**
  283.      * Sets the HTML attributes
  284.      * @param    mixed   $attributes     Either a typical HTML attribute string or an associative array
  285.      * @access   public
  286.      */
  287.     function setAttributes($attributes)
  288.     {
  289.         $this->_attributes = $this->_parseAttributes($attributes);
  290.     } // end func setAttributes
  291.  
  292.     /**
  293.      * Returns the assoc array (default) or string of attributes
  294.      *
  295.      * @param     bool    Whether to return the attributes as string
  296.      * @since     1.6
  297.      * @access    public
  298.      * @return    mixed   attributes
  299.      */
  300.     function getAttributes($asString = false)
  301.     {
  302.         if ($asString) {
  303.             return $this->_getAttrString($this->_attributes);
  304.         } else {
  305.             return $this->_attributes;
  306.         }
  307.     } //end func getAttributes
  308.  
  309.     /**
  310.      * Updates the passed attributes without changing the other existing attributes
  311.      * @param    mixed   $attributes     Either a typical HTML attribute string or an associative array
  312.      * @access   public
  313.      */
  314.     function updateAttributes($attributes)
  315.     {
  316.         $this->_updateAttrArray($this->_attributes, $this->_parseAttributes($attributes));
  317.     } // end func updateAttributes
  318.  
  319.     /**
  320.      * Removes an attribute
  321.      *
  322.      * @param     string    $attr   Attribute name
  323.      * @since     1.4
  324.      * @access    public
  325.      * @return    void
  326.      */
  327.     function removeAttribute($attr)
  328.     {
  329.         $this->_removeAttr($attr, $this->_attributes);
  330.     } //end func removeAttribute
  331.  
  332.     /**
  333.      * Sets the line end style to Windows, Mac, Unix or a custom string.
  334.      *
  335.      * @param   string  $style  "win", "mac", "unix" or custom string.
  336.      * @since   1.7
  337.      * @access  public
  338.      * @return  void
  339.      */
  340.     function setLineEnd($style)
  341.     {
  342.         switch ($style) {
  343.             case 'win':
  344.                 $this->_lineEnd = "\15\12";
  345.                 break;
  346.             case 'unix':
  347.                 $this->_lineEnd = "\12";
  348.                 break;
  349.             case 'mac':
  350.                 $this->_lineEnd = "\15";
  351.                 break;
  352.             default:
  353.                 $this->_lineEnd = $style;
  354.         }
  355.     } // end func setLineEnd
  356.  
  357.     /**
  358.      * Sets the tab offset
  359.      *
  360.      * @param    int     $offset
  361.      * @access   public
  362.      */
  363.     function setTabOffset($offset)
  364.     {
  365.         $this->_tabOffset = $offset;
  366.     } // end func setTabOffset
  367.  
  368.     /**
  369.      * Returns the tabOffset
  370.      *
  371.      * @since     1.5
  372.      * @access    public
  373.      * @return    int
  374.      */
  375.     function getTabOffset()
  376.     {
  377.         return $this->_tabOffset;
  378.     } //end func getTabOffset
  379.  
  380.     /**
  381.      * Sets the string used to indent HTML
  382.      *
  383.      * @since     1.7
  384.      * @param     string    $string     String used to indent ("\11", "\t", '  ', etc.).
  385.      * @access    public
  386.      * @return    void
  387.      */
  388.     function setTab($string)
  389.     {
  390.         $this->_tab = $string;
  391.     } // end func setTab
  392.  
  393.     /**
  394.      * Sets the HTML comment to be displayed at the beginning of the HTML string
  395.      *
  396.      * @param     string
  397.      * @since     1.4
  398.      * @access    public
  399.      * @return    void
  400.      */
  401.     function setComment($comment)
  402.     {
  403.         $this->_comment = $comment;
  404.     } // end func setHtmlComment
  405.  
  406.     /**
  407.      * Returns the HTML comment
  408.      *
  409.      * @since     1.5
  410.      * @access    public
  411.      * @return    string
  412.      */
  413.     function getComment()
  414.     {
  415.         return $this->_comment;
  416.     } //end func getComment
  417.  
  418.     /**
  419.      * Abstract method.  Must be extended to return the objects HTML
  420.      *
  421.      * @access    public
  422.      * @return    string
  423.      * @abstract
  424.      */
  425.     function toHtml()
  426.     {
  427.         return '';
  428.     } // end func toHtml
  429.  
  430.     /**
  431.      * Displays the HTML to the screen
  432.      *
  433.      * @access    public
  434.      */
  435.     function display()
  436.     {
  437.         print $this->toHtml();
  438.     } // end func display
  439.  
  440.     /**
  441.      * Sets the charset to use by htmlspecialchars() function
  442.      *
  443.      * Since this parameter is expected to be global, the function is designed
  444.      * to be called statically:
  445.      * <code>
  446.      * HTML_Common::charset('utf-8');
  447.      * </code>
  448.      * or
  449.      * <code>
  450.      * $charset = HTML_Common::charset();
  451.      * </code>
  452.      *
  453.      * @param   string  New charset to use. Omit if just getting the 
  454.      *                  current value. Consult the htmlspecialchars() docs 
  455.      *                  for a list of supported character sets.
  456.      * @return  string  Current charset
  457.      * @access  public
  458.      * @static
  459.      */
  460.     function charset($newCharset = null)
  461.     {
  462.         static $charset = 'ISO-8859-1';
  463.  
  464.         if (!is_null($newCharset)) {
  465.             $charset = $newCharset;
  466.         }
  467.         return $charset;
  468.     } // end func charset
  469. } // end class HTML_Common
  470. ?>
  471.