helpers.php File Reference


Detailed Description

Miscellaneous helper functions.

Definition in file helpers.php.

Go to the source code of this file.

Functions

 email ($doc, $email)
 Makes the email robots unreadable.
 generate_password (&$passw, &$md5passw, $len=5)
 Generates simple random password.
 go_to ($relative_url)
 Redirects browser using 302 HTTP response code and Location header to some new page.
 html ($str)
 Shortcut for htmlspecialchars($str, ENT_QUOTES);.
 lg ($msg)
 Logs message into the log.txt file.
 link2aXML ($doc, $text)
 It recognises URLs which begins with http://, https:// or ftp:// and converts them into <a href=> elements.
 make_absolute_url ($relative_url)
 Tries to make absolute URL from relative one.
 nl2br_and_link2a_XML ($doc, $text)
 Combinates nl2brXML() and link2aXML().
 nl2brXML ($doc, $text)
 This function is identical to nl2br except that it returns an array of DOMNode instances instead of plain HTML code (it also removes \n unlike original nl2br).
 starts_with ($what, $where)
 Java startsWith equivalent.
 url ($str)
 Shortcut for urlencode($str);.
 urlfile_exists ($url)
 Tests if given URL exists.


Function Documentation

email doc,
email
 

Makes the email robots unreadable.

Expects that there exists at.gif and dot.gif files.

Parameters:
[in] $doc DOMDocument instance.
[in] $email Email address.
Returns:
Array of DOMNodes, representing mangled email.

Definition at line 93 of file helpers.php.

References xml_convert_str2elem().

00093                                {
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   }

generate_password &$  passw,
&$  md5passw,
len = 5
 

Generates simple random password.

It will consists only of letters a-z (to not be error-prone). Use this if user forgets its password.

Parameters:
[out] $passw Generated password.
[out] $md5passw MD5 hash of the generated password.
[in] $len Password length. Default is 5.

Definition at line 210 of file helpers.php.

00210                                                             {
00211     for ($i = 0; $i < $len; $i++):
00212       $passw .= chr(rand(97, 122));
00213     endfor;
00214     $md5passw = md5($passw);
00215   }

go_to relative_url  ) 
 

Redirects browser using 302 HTTP response code and Location header to some new page.

Stops execution of the PHP script.

Definition at line 184 of file helpers.php.

00184                                 {
00185     header("Location: http://" . make_absolute_url($relative_url));
00186     exit;
00187   }

html str  ) 
 

Shortcut for htmlspecialchars($str, ENT_QUOTES);.

Definition at line 47 of file helpers.php.

00047                       {
00048     return htmlspecialchars($str, ENT_QUOTES);
00049   }

lg msg  ) 
 

Logs message into the log.txt file.

Make sure that script has write access to the file.

Definition at line 37 of file helpers.php.

00037                     {
00038     $f = fopen('log.txt', 'a');
00039     fwrite($f, $msg . "\n");
00040     fclose($f);
00041   }

link2aXML doc,
text
 

It recognises URLs which begins with http://, https:// or ftp:// and converts them into <a href=> elements.

Parameters:
[in] $doc DOMDocument instance.
[in] $text String which is going to be converted.
Returns:
Array of DOMNodes, there is always at least one DOMText instance.

Definition at line 151 of file helpers.php.

References xml_convert_regex2elem().

00151                                   {
00152     return xml_convert_regex2elem($doc, $text, '#((?:http|https|ftp)://\S+)#i', new LinkElementCreator);
00153   }

make_absolute_url relative_url  ) 
 

Tries to make absolute URL from relative one.

Parameters:
[in] $relative_url If empty, 'index.php' is used instead.
Returns:
Absolute URL.

Definition at line 167 of file helpers.php.

00167                                             {
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   }

nl2br_and_link2a_XML doc,
text
 

Combinates nl2brXML() and link2aXML().

Definition at line 158 of file helpers.php.

00158                                              {
00159     return link2aXML($doc, nl2brXML($doc, $text));
00160   }

nl2brXML doc,
text
 

This function is identical to nl2br except that it returns an array of DOMNode instances instead of plain HTML code (it also removes \n unlike original nl2br).

You can use it like this -- PHP side:

    $doc = new DOMDocument();
    ...
    $some_elem = $doc->createElement('someElem');
    ...
    $coverted = nl2brXML($doc, $some_string);
    foreach($converted as $node):
      $some_elem->appendChild($node);
    endforeach;
XSLT side:
    <xsl:copy-of select='someElem'/>
Parameters:
[in] $doc DOMDocument instance.
[in] $text String which is going to be converted.
Returns:
Array of DOMNodes, there is always at least one DOMText instance.

Definition at line 125 of file helpers.php.

References xml_convert_str2elem().

00125                                  {
00126     return xml_convert_str2elem($doc, $text, "\n", 'br');
00127   }

starts_with what,
where
 

Java startsWith equivalent.

Parameters:
[in] $what Searched prefix.
[in] $where String which possibly starts with the prefix.
Returns:
TRUE if $where starts with $what, FALSE otherwise.

Definition at line 196 of file helpers.php.

Referenced by Servlet::get_referer().

00196                                       {
00197     if (strpos($where, $what) === 0):
00198       return TRUE;
00199     endif;
00200     return FALSE;
00201   }

url str  ) 
 

Shortcut for urlencode($str);.

Definition at line 54 of file helpers.php.

Referenced by Servlet::show().

00054                      {
00055     return urlencode($str);
00056   }

urlfile_exists url  ) 
 

Tests if given URL exists.

Returns:
TRUE if exists, FALSE otherwise.

Definition at line 221 of file helpers.php.

00221                                 {
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