PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/php/HTML/Template/Xipe/Filter/Modifier.php

https://bitbucket.org/adarshj/convenient_website
PHP | 336 lines | 174 code | 25 blank | 137 comment | 29 complexity | 56219d5c40386f892440311975e2efad MD5 | raw file
Possible License(s): Apache-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-2-Clause, GPL-2.0, LGPL-3.0
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 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. // | Authors: Wolfram Kriesing <wolfram@kriesing.de> |
  17. // +----------------------------------------------------------------------+
  18. // $Id: Modifier.php,v 1.11 2003/09/15 11:09:10 cain Exp $
  19. //
  20. require_once 'HTML/Template/Xipe/Options.php';
  21. /**
  22. *
  23. *
  24. * @package HTML_Template_Xipe
  25. * @access public
  26. * @version 02/06/26
  27. * @author Wolfram Kriesing <wolfram@kriesing.de>
  28. */
  29. class HTML_Template_Xipe_Filter_Modifier extends HTML_Template_Xipe_Options
  30. {
  31. /**
  32. * for passing values to the class, i.e. like the delimiters
  33. * @access private
  34. * @var array $options the options for initializing the filter class
  35. */
  36. var $options = array( 'delimiter' => array() // first value of the array is the begin delimiter, second the end delimiter
  37. );
  38. var $_imgDirs = array();
  39. var $_imgFiles = array();
  40. /**
  41. * this filter trys to read all the following tags and replaces the src tags
  42. * with the complete file name (w/o the http://domain)
  43. * <img src> <input src>
  44. * Using this filter makes it easier to work without looking up
  45. * where the image really is located every time
  46. * You simply need to give the image name and this filter searches for
  47. * the image in the image root and rewrites the image name including
  48. * the complete path to the image, so this saves time when developing and
  49. * no php processing is necessary anymore when you have image tags like this:
  50. * &lt;img src="{$imgRoot}/dir/name/image"&gt;<br>
  51. * <p>
  52. * Why not make the resulting link relative to the current URL (PHP_SELF)?<br>
  53. * Because a compiled template might be included from multiple places, so the
  54. * relative path would not always be the same. Thats why we make it absolute
  55. * w/o the protocol and domain in front.
  56. * </p>
  57. *
  58. * @version 02/06/27
  59. * @author Wolfram Kriesing <wolfram@kriesing.de>
  60. * @param string the original template code
  61. * @param string the virtual image path
  62. * @param string here you can set the prefered image type
  63. * so that you only need to give the image name w/o its extension
  64. * and if multiple images are found the prefered one is used
  65. * @return string the modified template
  66. */
  67. function imgSrc( $input , $imageRoot , $vImgRoot , $preferedType='gif' , $dropDirs=array('CVS'))
  68. {
  69. $_imgTypes = array('gif','jpg','png');
  70. settype($imageRoot,'array');
  71. settype($vImgRoot,'array');
  72. // FIXXME make img src tags relative if desired
  73. // put the prefered type first, so we find it first :-)
  74. $imgTypes = array($preferedType);
  75. foreach( $_imgTypes as $aImgType )
  76. if( $aImgType != $preferedType )
  77. $imgTypes[] = $aImgType;
  78. $found = array();
  79. // modify the vImgRoot NOT to contain the 'http://domain' string in front
  80. // since this is only unnecessary text
  81. foreach( $vImgRoot as $key=>$aDir )
  82. $vImgRoot[$key] = preg_replace('/^http.?:\/\/[^\/]+/','',$vImgRoot[$key]);
  83. $regExp = '/<[img|input].+src="(.*)"/Ui';
  84. preg_match_all($regExp,$input,$images);
  85. if(sizeof($images[1]))
  86. {
  87. if( !sizeof($this->_imgDirs) ) // get image dirs if we didnt yet, since this instance might be used multiple times
  88. {
  89. foreach( $imageRoot as $aDir )
  90. {
  91. $this->_getDirs($aDir,$dropDirs);
  92. $this->_imgDirs = array_merge($this->_imgDirs,$this->_foundDirs);
  93. }
  94. }
  95. // go thru all the images we have found and find their path
  96. foreach( $images[1] as $aImage)
  97. {
  98. if( isset($this->_imgFiles[$aImage]) && $this->_imgFiles[$aImage] )
  99. $found[$aImage] = $this->_imgFiles[$aImage];
  100. else
  101. {
  102. if( sizeof($this->_imgDirs) )
  103. foreach( $this->_imgDirs as $aDir ) // go thru all the directories found
  104. {
  105. // using pathinfo returns also the file's extension
  106. $fileInfo = pathinfo($aImage);
  107. // if there is an extension we assume that this one is used
  108. $_imgTypes = isset($fileInfo['extension']) && $fileInfo['extension'] ? array('') : $imgTypes;
  109. foreach( $_imgTypes as $aType ) // if no file extension given loop through all possible imgTypes
  110. {
  111. $aType = $aType ? ".$aType" : '';
  112. //print "....check $aDir $aImage$aType<br>";
  113. if( @file_exists($aDir.$aImage.$aType))
  114. {
  115. foreach( $imageRoot as $key=>$aImgDir )
  116. {
  117. if( strpos( realpath($aDir.$aImage.$aType),$aImgDir ) === 0 )
  118. $this->_imgFiles[$aImage] = str_replace($aImgDir,$vImgRoot[$key],realpath($aDir.$aImage.$aType));
  119. }
  120. //print 'found use:'.$this->_imgFiles[$aImage].'<br>';
  121. break(2);
  122. }
  123. if( @file_exists($aDir.'/'.$aImage.$aType))
  124. {
  125. foreach( $imageRoot as $key=>$aImgDir )
  126. {
  127. if( strpos( realpath($aDir.'/'.$aImage.$aType),$aImgDir ) === 0 )
  128. $this->_imgFiles[$aImage] = str_replace($aImgDir,$vImgRoot[$key],realpath($aDir.'/'.$aImage.$aType));
  129. }
  130. //print 'found use:'.$this->_imgFiles[$aImage].'<br>';
  131. break(2);
  132. }
  133. }
  134. }
  135. }
  136. }
  137. }
  138. if( sizeof($this->_imgFiles) )
  139. foreach( $this->_imgFiles as $file=>$vName )
  140. {
  141. $_file = str_replace('/','\\/',preg_quote($file)); //"
  142. $regExp = '/<(img|input)(.+)src="'.$_file.'"/Ui';
  143. $input = preg_replace($regExp,'<$1$2src="'.$vName.'"',$input);
  144. }
  145. return $input;
  146. }
  147. /**
  148. * find all dirs in the given one, writes them into _foundDirs
  149. * be sure to copy them from there when using
  150. * this method calls itself recursively to get all subdirs too
  151. * watch out if there are links which could cause endless loops
  152. * i havent checked if that can happen
  153. *
  154. * @version 02/06/27
  155. * @author Wolfram Kriesing <wolfram@kriesing.de>
  156. * @param string the directory under which to search
  157. * @param array directories that shall be left out, like CVS
  158. */
  159. function _getDirs( $root , $dropDirs=array() )
  160. {
  161. $dirs = array();
  162. if ($handle = @opendir($root))
  163. {
  164. $dirs[] = ''; // do also include the directory itself
  165. while (false !== ($file = readdir($handle)))
  166. {
  167. if( $file!='.' && $file!='..' && is_dir($root.'/'.$file) && !in_array($file,$dropDirs))
  168. {
  169. $dirs[] = $file;
  170. }
  171. }
  172. closedir($handle);
  173. }
  174. sort($dirs);
  175. foreach( $dirs as $aDir )
  176. {
  177. $this->_foundDirs[] = $root.'/'.$aDir;
  178. if( $aDir )
  179. $this->_getDirs($root.'/'.$aDir,$dropDirs);
  180. }
  181. }
  182. /**
  183. * Replace PHP_SELF by {$_SERVER['PHP_SELF']} if it occurs in a link.
  184. * TODO
  185. * correct all links, if they are not proper like www.home.de
  186. * then they will be corrected to be http://www.home.de
  187. * a link checker could be implemented too :-/
  188. *
  189. * @version 02/06/27
  190. * @author Wolfram Kriesing <wolfram@kriesing.de>
  191. * @param string the original template code
  192. * @return string the modified template
  193. */
  194. function aHref($input)
  195. {
  196. if (preg_match_all('~<a.*href=.*>~iU',$input,$_linkTags)) {
  197. $linkTags = array_unique($_linkTags[0]);
  198. // replace each tag with the given attributes
  199. foreach ($linkTags as $aTag) {
  200. $p = xml_parser_create();
  201. xml_parse_into_struct($p,$aTag,$vals);
  202. xml_parser_free($p);
  203. $attribs = array();
  204. if (isset($vals[0]['attributes'])) {
  205. $attribs = $vals[0]['attributes'];
  206. }
  207. // replace PHP_SELF by {$_SERVER['PHP_SELF']}
  208. if (isset($attribs['HREF']) && strpos($attribs['HREF'],'PHP_SELF')===0) {
  209. $newSelf = $this->getOption('delimiter',0).'$_SERVER[\'PHP_SELF\']'.
  210. $this->getOption('delimiter',1);
  211. $attribs['HREF'] = str_replace('PHP_SELF',$newSelf,$attribs['HREF']);
  212. // build the new tag
  213. $newTag = '<a';
  214. foreach ($attribs as $key=>$val) {
  215. $newTag.= " $key=\"$val\"";
  216. }
  217. $input = str_replace($aTag,$newTag.'>',$input);
  218. }
  219. }
  220. }
  221. return $input;
  222. }
  223. /**
  224. * replace text with links
  225. *
  226. * @version 02/08/02
  227. * @author Wolfram Kriesing <wolfram@kriesing.de>
  228. * @param string the original template code
  229. * @param array an array of text that shall be replaces by the links
  230. * 'text' => 'http://www.url.de'
  231. * @return string the modified template
  232. */
  233. function autoLink( $input , $links )
  234. {
  235. //FIXXME replace ONLY text, not stuff like '<a href="....index.php">' dont replace the php there too !!!!
  236. // stuff like <a href>pear.php.net</a> would result in <a href><ahref>pear</a>.<ahref>php</a>.net</a>
  237. // if pear and php were to be linked automatically ... prevent from doing that !!!
  238. if( sizeof($links) )
  239. foreach( $links as $word=>$link )
  240. {
  241. $input = preg_replace( '/(>[^<]*)('.$word.')([^>]*<)/Ui' ,
  242. '$1<a href="'.$link.'" target="_blank">$2</a>$3' ,
  243. $input );
  244. }
  245. return $input;
  246. }
  247. /**
  248. * Add missing attributes as given. If none given it will place
  249. * action="{$_SERVER['PHP_SELF']}" and method="POST" in the form tag.
  250. *
  251. *
  252. */
  253. function form($input,$attributes=array(),$options=array())
  254. {
  255. if (preg_match_all('~<form(\s.*)?>~iU',$input,$_formTags)) {
  256. $formTags = array_unique($_formTags[0]);
  257. if (!isset($attributes['action'])) {
  258. $attributes['action'] = $this->getOption('delimiter',0).
  259. '$_SERVER[\'PHP_SELF\']'.
  260. $this->getOption('delimiter',1);
  261. }
  262. if (!isset($attributes['method'])) {
  263. $attributes['method'] = 'POST';
  264. }
  265. // set all attribute names to uppercase, since the XML-parser does it too
  266. // and if we want to merge them later, they should be same case
  267. foreach ($attributes as $key=>$val) {
  268. unset($attributes[$key]);
  269. $attributes[strtoupper($key)] = $val;
  270. }
  271. // replace each tag with the given attributes
  272. foreach ($formTags as $aTag) {
  273. $p = xml_parser_create();
  274. xml_parse_into_struct($p,$aTag,$vals);
  275. xml_parser_free($p);
  276. $attribs = array();
  277. if (isset($vals[0]['attributes'])) {
  278. $attribs = $vals[0]['attributes'];
  279. }
  280. $attribs = array_merge($attributes,$attribs);
  281. /* this is really experimental ...
  282. // if the option 'addHiddenGet' is given, then this adds the get parameters to the
  283. // form as hidden values, this way you can submit a get-form, which still contains
  284. // all the GET-parameters from before
  285. $htmlAfterForm = '';
  286. if (strtolower(@$attribs['METHOD'])=='get' &&
  287. isset($options['addHiddenGet']) && is_array($options['addHiddenGet'])) {
  288. foreach ($options['addHiddenGet'] as $k=>$v) {
  289. // if the value is an array, extract it and add each single value properly
  290. if (is_array($v)) {
  291. foreach ($v as $k1=>$v1) {
  292. $htmlAfterForm .= '<input type="hidden" name="'.$k.'['.$k1.']'.'" value="'.urlencode($v1).'">';
  293. }
  294. } else {
  295. $htmlAfterForm .= '<input type="hidden" name="'.$k.'" value="'.urlencode($v).'">';
  296. }
  297. }
  298. }
  299. */
  300. // build the new tag
  301. $newTag = '<form';
  302. foreach ($attribs as $key=>$val) {
  303. $newTag.= " $key=\"$val\"";
  304. }
  305. $input = str_replace($aTag,$newTag.'>',$input);
  306. }
  307. }
  308. return $input;
  309. }
  310. }
  311. ?>