- <?php
- /*
- * Lokorin.com
- * Copyright 2004-2006
- * Licensed under the GNU LGPL. See COPYING for full terms.
- */
- /**
- * A library that contains varius functions that do not belong anywhere else
- * and that do not constitute a sufficiently large cohesion group for a
- * library of their own.
- * @author Andreas Launila
- * @version $Revision: 1.13 $
- * @package com.lokorin.lokorin.lib
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
- */
-
- /**
- * For access to common constants and functions.
- */
- require_once('common.php');
-
- /**
- * Gets the current url, complete with the query string.
- * @return string An url.
- * @access public
- */
- function getCurrentUrl() {
- return $_SERVER['REQUEST_URI'];
- }
-
- /**
- * Gets the current encoded url, complete with the query string.
- * @return string An encoded url.
- * @access public
- */
- function getCurrentEncodedUrl() {
- return urlencode(getCurrentUrl());
- }
-
- /**
- * Renames parameters in a url query string, the values remain unchanged. It
- * can for instance turn "foo=0&bar=hi" into "a=0&b=hi".
- * @param array $replacements An array specifying what and how parameters
- * should be renamed. The name of the parameter that should be renamed
- * should be the key and the new name for the parameter should be the
- * value.
- * @param string $queryString An optional way to specify the query string that
- * should be renamed. The default is that the query string in
- * $_SERVER['QUERY_STRING'] is used.
- * @return string The query string with the specified name changes performed on
- * it.
- * @access public
- */
- function renameQueryParams($replacements, $queryString = '') {
- if($queryString == '') {
- $queryString = $_SERVER['QUERY_STRING'];
- }
- //Thing to replace as key, thing to replace with as value
- foreach($replacements as $needle => $replacement) {
- $queryString = str_replace($needle, $replacement, $queryString);
- }
- return($queryString);
- }
-
- /**
- * Adds specified parameters to the current query string. If a parameter is
- * already specified then the existing value is overwritten by the new one.
- * @param array $paramsToAdd The parameters that should be added. The key
- * should be the parameter name, while the value should be the parameter
- * value.
- * @return string The resulting query string. It has the form
- * 'foo1=bar1&foo2=bar2'.
- * @access public
- */
- function addQueryParams($paramsToAdd) {
- $matches = array();
- preg_match_all('/.*?(\w+)=([^&]*)[&$]?/', $_SERVER['QUERY_STRING'],
- $matches);
- $params = array();
- //Add the existing parameters.
- foreach($matches[0] as $key => $match) {
- $params[$matches[1][$key]] = $matches[2][$key];
- }
- //Add the specified parameters, overwriting any existing.
- foreach($paramsToAdd as $name => $value) {
- $params[$name] = $value;
- }
- //Build the query string.
- $queryString = '';
- foreach($params as $name => $value) {
- if(strlen($queryString) != 0) {
- $queryString .= '&';
- }
- $queryString .= $name.'='.$value;
- }
- return $queryString;
- }
-
- /**
- * Sends an email with specified contents. Optionally the receiver of the email
- * can be specififed along with the sender.
- * @param string $title The title that the email message should have.
- * @param string $message The actual message contents for the email.
- * @param string $sender An optional email address that should be given as the
- * sender of the email (and hence the return path).
- * @param string $receiver An optional specification of the receing email
- * address. It should be given without a domain, i.e. if the mail should
- * be sent to foo@dfcrafter.com then 'foo' should be specified.
- * @access public
- */
- function sendEmail($title, $message, $sender = '', $receiver = 'lokorin') {
- $headers = "Content-type: text/html; charset=iso-8859-1\r\n";
- $receiver = $receiver.'@lokorin.com';
-
- //Make sure that no extra headers are snuck in.
- $matches = array();
- preg_match("/([\w\d\.-]+?@[\w\d\.-]+)/", $sender, $matches);
- if(sizeof($matches) > 1) {
- $returnPath = $matches[1];
- } else {
- $returnPath = '';
- }
-
- if(strlen($returnPath) != 0) {
- $headers .= "From: ".$returnPath;
- }
-
- $message = nl2br($message);
-
- if(!mail($receiver, $title, stripslashes($message), $headers)) {
- logException($_SERVER['PHP_SELF'],'Failed to send email. Sender: ' .
- '('.$returnPath.'), Title: ('.$title.'), Body: ('.$message.')');
- }
- }
-
- /**
- * Gets the relative path to the current requested directory from the root
- * path.
- * @return string A path from the root path to the current directory.
- * @access public
- */
- function getCurrentDirectory() {
- //Strip everything but directories.
- $url = preg_replace("/^(.*?\/)[^\/]*$/", '$1', strip_tags(SELF));
-
- if(strpos($url, ROOT_PATH) === false) {
- logError('The page ('.$url.') does not contain the ' .
- 'root path.');
- }
-
- $strpos = strpos($url, ROOT_PATH);
- return substr($url, 0, $strpos).substr($url, $strpos.strlen(ROOT_PATH));
- }
-
- /**
- * Removes all files and directories contained in the specified directory.
- * @param string $dirName The name of the directory that should be removed
- * relative to the current directory.
- * @return boolean True if the contants was successfully removed, false
- * otherwise.
- * @access public
- */
- function removeDirectoryContents($dirName) {
- if(file_exists($dirName)) {
- $dir = dir($dirName);
- while($file = $dir->read()) {
- if($file != '.' && $file != '..') {
- if(is_dir($dirName.'/'.$file)) {
- removedDirectoryRecursive($dirName.'/'.$file);
- } else {
- @unlink($dirName.'/'.$file) or die('File '.$dirName.'/'.$file.' couldn\'t be deleted!');
- }
- }
- }
- $dir->close();
- return true;
- } else {
- return false;
- }
- }
-
- /**
- * Recursivly removes a directory and all of its contents.
- * @param string $dirName The name of the directory that should be removed
- * relative to the current directory.
- * @return boolean True if the directory was successfully removed, false
- * otherwise.
- * @access public
- */
- function removeDirectoryRecursive($dirName) {
- if(empty($dirName)) {
- return true;
- }
- if(removeDirectoryContents($dirName)) {
- @rmdir($dirName) or die('Folder '.$dirName.' couldn\'t be deleted!');
- } else {
- return false;
- }
- return true;
- }
-
- /**
- * Encodes the string to be encoded for url use, but first alters some
- * characters to get a more readable string. For instance spaces are replaced
- * with underscores instead of %20. This function is not one to one.
- * @param string $input The input that should be encoded.
- * @return string An encoded string.
- * @access public
- */
- function urlEncodeReadable($input) {
- $input = str_replace(' ', '_', $input);
- return rawurlencode($input);
- }
- ?>