helpers.php

Go to the documentation of this file.
00001 <?php
00002   /**
00003    *  @file helpers.php
00004    *  Miscellaneous helper functions.
00005    */
00006 
00007   /*
00008   Easy PHP Framework
00009 
00010   Copyright (c) 2005 Michal Molhanec
00011 
00012   This software is provided 'as-is', without any express or implied
00013   warranty. In no event will the authors be held liable for any damages
00014   arising from the use of this software.
00015 
00016   Permission is granted to anyone to use this software for any purpose,
00017   including commercial applications, and to alter it and redistribute
00018   it freely, subject to the following restrictions:
00019 
00020       1. The origin of this software must not be misrepresented;
00021          you must not claim that you wrote the original software.
00022          If you use this software in a product, an acknowledgment
00023          in the product documentation would be appreciated but
00024          is not required.
00025 
00026       2. Altered source versions must be plainly marked as such,
00027          and must not be misrepresented as being the original software.
00028 
00029       3. This notice may not be removed or altered from any
00030          source distribution.
00031   */
00032   
00033   /**
00034    *  Logs message into the log.txt file. Make sure that script has
00035    *  write access to the file.
00036    */
00037   function lg($msg) {
00038     $f = fopen('log.txt', 'a');
00039     fwrite($f, $msg . "\n");
00040     fclose($f);
00041   }
00042 
00043   /**
00044    *  Shortcut for
00045    *  <a href='http://www.php.net/manual/en/function.htmlspecialchars.php'>htmlspecialchars($str, ENT_QUOTES);</a>
00046    */
00047   function html($str) {
00048     return htmlspecialchars($str, ENT_QUOTES);
00049   }
00050 
00051   /**
00052    *  Shortcut for <a href='http://www.php.net/manual/en/function.urlencode.php'>urlencode($str);</a>
00053    */
00054   function url($str) {
00055     return urlencode($str);
00056   }
00057 
00058   /**
00059    * Helper class used by email() function.
00060    */
00061   class AtElementCreator extends ElementCreator {
00062     function __construct() {
00063       parent::__construct('img');
00064     }
00065     function create_element($doc) {
00066       $this->attrs['src'] = 'at.gif';
00067       $this->attrs['alt'] = 'at';
00068       return parent::create_element($doc);
00069     }
00070   }
00071 
00072   /**
00073    * Helper class used by email() function.
00074    */
00075   class DotElementCreator extends ElementCreator {
00076     function __construct() {
00077       parent::__construct('img');
00078     }
00079     function create_element($doc) {
00080       $this->attrs['src'] = 'dot.gif';
00081       $this->attrs['alt'] = 'dot';
00082       return parent::create_element($doc);
00083     }
00084   }
00085 
00086   /**
00087    *  Makes the email robots unreadable. Expects that there exists
00088    *  at.gif and dot.gif files.
00089    *  @param[in] $doc <a href='http://www.php.net/manual/en/ref.dom.php'>DOMDocument</a> instance.
00090    *  @param[in] $email Email address.
00091    *  @return Array of DOMNodes, representing mangled email.
00092    */
00093   function email($doc, $email) {
00094     // make some space around images
00095     $email = str_replace('@', ' @ ', str_replace('.', ' . ', $email));
00096     return xml_convert_str2elem($doc,
00097       xml_convert_str2elem($doc, $email, '@', new AtElementCreator),
00098       '.', new DotElementCreator);
00099   }
00100 
00101   /**
00102    *  This function is identical to
00103    *  <a href='http://www.php.net/manual/en/function.nl2br.php'>nl2br</a>
00104    *  except that it returns an array of DOMNode instances instead of plain HTML
00105    *  code (it also removes \\n unlike original nl2br).
00106    *  You can use it like this -- PHP side:
00107    *  @code
00108    *    $doc = new DOMDocument();
00109    *    ...
00110    *    $some_elem = $doc->createElement('someElem');
00111    *    ...
00112    *    $coverted = nl2brXML($doc, $some_string);
00113    *    foreach($converted as $node):
00114    *      $some_elem->appendChild($node);
00115    *    endforeach;
00116    *  @endcode
00117    *  XSLT side:
00118    *  @code
00119    *    <xsl:copy-of select='someElem'/>
00120    *  @endcode
00121    *  @param[in] $doc <a href='http://www.php.net/manual/en/ref.dom.php'>DOMDocument</a> instance.
00122    *  @param[in] $text String which is going to be converted.
00123    *  @return Array of DOMNodes, there is always at least one DOMText instance.
00124    */
00125   function nl2brXML($doc, $text) {
00126     return xml_convert_str2elem($doc, $text, "\n", 'br');
00127   }
00128 
00129   /**
00130    * Helper class used by link2aXML() function.
00131    * You can use it whenever you need ElementCreator
00132    * which creates links that mirrors content in the href attribute.
00133    */
00134   class LinkElementCreator extends ElementCreator {
00135     function __construct() {
00136       parent::__construct('a');
00137     }
00138     function create_element($doc, $content) {
00139       $this->attrs['href'] = $content;
00140       return parent::create_element($doc, $content);
00141     }
00142   }
00143 
00144   /**
00145    *  It recognises URLs which begins with http://, https:// or ftp://
00146    *  and converts them into <a href=> elements.
00147    *  @param[in] $doc <a href='http://www.php.net/manual/en/ref.dom.php'>DOMDocument</a> instance.
00148    *  @param[in] $text String which is going to be converted.
00149    *  @return Array of DOMNodes, there is always at least one DOMText instance.
00150    */
00151   function link2aXML($doc, $text) {
00152     return xml_convert_regex2elem($doc, $text, '#((?:http|https|ftp)://\S+)#i', new LinkElementCreator);
00153   }
00154 
00155   /**
00156    *  Combinates nl2brXML() and link2aXML().
00157    */
00158   function nl2br_and_link2a_XML($doc, $text) {
00159     return link2aXML($doc, nl2brXML($doc, $text));
00160   }
00161 
00162   /**
00163    *  Tries to make absolute URL from relative one.
00164    *  @param[in] $relative_url If empty, 'index.php' is used instead.
00165    *  @return Absolute URL.
00166    */
00167   function make_absolute_url($relative_url) {
00168     $dir = dirname($_SERVER['PHP_SELF']);
00169     if ($dir == '\\'):
00170       $dir = '';
00171     endif;
00172     if ($relative_url == '') $relative_url = 'index.php';
00173     if (strlen($dir) == 0 or $dir[strlen($dir) - 1] != '/'):
00174       $dir .= '/';
00175     endif;
00176     return $_SERVER['HTTP_HOST'] . $dir . $relative_url;
00177   }
00178 
00179   /**
00180    *  Redirects browser using 302 HTTP response code and
00181    *  Location header to some new page. Stops execution of
00182    *  the PHP script.
00183    */
00184   function go_to($relative_url) {
00185     header("Location: http://" . make_absolute_url($relative_url));
00186     exit;
00187   }
00188 
00189   /**
00190    *  Java <a href='http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#startsWith(java.lang.String)'
00191    *  >startsWith</a> equivalent.
00192    *  @param[in] $what Searched prefix.
00193    *  @param[in] $where String which possibly starts with the prefix.
00194    *  @return TRUE if $where starts with $what, FALSE otherwise.
00195    */
00196   function starts_with($what, $where) {
00197     if (strpos($where, $what) === 0):
00198       return TRUE;
00199     endif;
00200     return FALSE;
00201   }
00202 
00203   /**
00204    *  Generates simple random password. It will consists only of letters
00205    *  a-z (to not be error-prone). Use this if user forgets its password.
00206    *  @param[out] $passw Generated password.
00207    *  @param[out] $md5passw MD5 hash of the generated password.
00208    *  @param[in]  $len Password length. Default is 5.
00209    */
00210   function generate_password(&$passw, &$md5passw, $len = 5) {
00211     for ($i = 0; $i < $len; $i++):
00212       $passw .= chr(rand(97, 122));
00213     endfor;
00214     $md5passw = md5($passw);
00215   }
00216   
00217   /**
00218    *  Tests if given URL exists.
00219    *  @return TRUE if exists, FALSE otherwise.
00220    */
00221   function urlfile_exists($url) {
00222     $handle = @fopen($url, 'r');
00223     if (!$handle):
00224       return FALSE;
00225     endif;
00226     fclose($handle);
00227     return TRUE;
00228   }

Generated on Sat Sep 24 01:26:42 2005 for Easy PHP Framework by  doxygen 1.4.2