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