com.lokorin.lokorin.lib
[ class tree: com.lokorin.lokorin.lib ] [ index: com.lokorin.lokorin.lib ] [ all elements ]

Source for file lib_util.php

Documentation is available at lib_util.php

  1. <?php
  2. /*
  3. * Lokorin.com
  4. * Copyright 2004-2006
  5. * Licensed under the GNU LGPL. See COPYING for full terms.
  6. */
  7. /**
  8. * A library that contains varius functions that do not belong anywhere else
  9. * and that do not constitute a sufficiently large cohesion group for a
  10. * library of their own.
  11. * @author Andreas Launila
  12. * @version $Revision: 1.13 $
  13. * @package com.lokorin.lokorin.lib
  14. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  15. */
  16.  
  17. /**
  18. * For access to common constants and functions.
  19. */
  20. require_once('common.php');
  21.  
  22. /**
  23. * Gets the current url, complete with the query string.
  24. * @return string An url.
  25. * @access public
  26. */
  27. function getCurrentUrl() {
  28. return $_SERVER['REQUEST_URI'];
  29. }
  30.  
  31. /**
  32. * Gets the current encoded url, complete with the query string.
  33. * @return string An encoded url.
  34. * @access public
  35. */
  36. function getCurrentEncodedUrl() {
  37. return urlencode(getCurrentUrl());
  38. }
  39.  
  40. /**
  41. * Renames parameters in a url query string, the values remain unchanged. It
  42. * can for instance turn "foo=0&bar=hi" into "a=0&b=hi".
  43. * @param array $replacements An array specifying what and how parameters
  44. * should be renamed. The name of the parameter that should be renamed
  45. * should be the key and the new name for the parameter should be the
  46. * value.
  47. * @param string $queryString An optional way to specify the query string that
  48. * should be renamed. The default is that the query string in
  49. * $_SERVER['QUERY_STRING'] is used.
  50. * @return string The query string with the specified name changes performed on
  51. * it.
  52. * @access public
  53. */
  54. function renameQueryParams($replacements, $queryString = '') {
  55. if($queryString == '') {
  56. $queryString = $_SERVER['QUERY_STRING'];
  57. }
  58. //Thing to replace as key, thing to replace with as value
  59. foreach($replacements as $needle => $replacement) {
  60. $queryString = str_replace($needle, $replacement, $queryString);
  61. }
  62. return($queryString);
  63. }
  64.  
  65. /**
  66. * Adds specified parameters to the current query string. If a parameter is
  67. * already specified then the existing value is overwritten by the new one.
  68. * @param array $paramsToAdd The parameters that should be added. The key
  69. * should be the parameter name, while the value should be the parameter
  70. * value.
  71. * @return string The resulting query string. It has the form
  72. * 'foo1=bar1&foo2=bar2'.
  73. * @access public
  74. */
  75. function addQueryParams($paramsToAdd) {
  76. $matches = array();
  77. preg_match_all('/.*?(\w+)=([^&]*)[&$]?/', $_SERVER['QUERY_STRING'],
  78. $matches);
  79. $params = array();
  80. //Add the existing parameters.
  81. foreach($matches[0] as $key => $match) {
  82. $params[$matches[1][$key]] = $matches[2][$key];
  83. }
  84. //Add the specified parameters, overwriting any existing.
  85. foreach($paramsToAdd as $name => $value) {
  86. $params[$name] = $value;
  87. }
  88. //Build the query string.
  89. $queryString = '';
  90. foreach($params as $name => $value) {
  91. if(strlen($queryString) != 0) {
  92. $queryString .= '&';
  93. }
  94. $queryString .= $name.'='.$value;
  95. }
  96. return $queryString;
  97. }
  98.  
  99. /**
  100. * Sends an email with specified contents. Optionally the receiver of the email
  101. * can be specififed along with the sender.
  102. * @param string $title The title that the email message should have.
  103. * @param string $message The actual message contents for the email.
  104. * @param string $sender An optional email address that should be given as the
  105. * sender of the email (and hence the return path).
  106. * @param string $receiver An optional specification of the receing email
  107. * address. It should be given without a domain, i.e. if the mail should
  108. * be sent to foo@dfcrafter.com then 'foo' should be specified.
  109. * @access public
  110. */
  111. function sendEmail($title, $message, $sender = '', $receiver = 'lokorin') {
  112. $headers = "Content-type: text/html; charset=iso-8859-1\r\n";
  113. $receiver = $receiver.'@lokorin.com';
  114. //Make sure that no extra headers are snuck in.
  115. $matches = array();
  116. preg_match("/([\w\d\.-]+?@[\w\d\.-]+)/", $sender, $matches);
  117. if(sizeof($matches) > 1) {
  118. $returnPath = $matches[1];
  119. } else {
  120. $returnPath = '';
  121. }
  122. if(strlen($returnPath) != 0) {
  123. $headers .= "From: ".$returnPath;
  124. }
  125. $message = nl2br($message);
  126.  
  127. if(!mail($receiver, $title, stripslashes($message), $headers)) {
  128. logException($_SERVER['PHP_SELF'],'Failed to send email. Sender: ' .
  129. '('.$returnPath.'), Title: ('.$title.'), Body: ('.$message.')');
  130. }
  131. }
  132.  
  133. /**
  134. * Gets the relative path to the current requested directory from the root
  135. * path.
  136. * @return string A path from the root path to the current directory.
  137. * @access public
  138. */
  139. function getCurrentDirectory() {
  140. //Strip everything but directories.
  141. $url = preg_replace("/^(.*?\/)[^\/]*$/", '$1', strip_tags(SELF));
  142. if(strpos($url, ROOT_PATH) === false) {
  143. logError('The page ('.$url.') does not contain the ' .
  144. 'root path.');
  145. }
  146. $strpos = strpos($url, ROOT_PATH);
  147. return substr($url, 0, $strpos).substr($url, $strpos.strlen(ROOT_PATH));
  148. }
  149.  
  150. /**
  151. * Removes all files and directories contained in the specified directory.
  152. * @param string $dirName The name of the directory that should be removed
  153. * relative to the current directory.
  154. * @return boolean True if the contants was successfully removed, false
  155. * otherwise.
  156. * @access public
  157. */
  158. function removeDirectoryContents($dirName) {
  159. if(file_exists($dirName)) {
  160. $dir = dir($dirName);
  161. while($file = $dir->read()) {
  162. if($file != '.' && $file != '..') {
  163. if(is_dir($dirName.'/'.$file)) {
  164. removedDirectoryRecursive($dirName.'/'.$file);
  165. } else {
  166. @unlink($dirName.'/'.$file) or die('File '.$dirName.'/'.$file.' couldn\'t be deleted!');
  167. }
  168. }
  169. }
  170. $dir->close();
  171. return true;
  172. } else {
  173. return false;
  174. }
  175. }
  176.  
  177. /**
  178. * Recursivly removes a directory and all of its contents.
  179. * @param string $dirName The name of the directory that should be removed
  180. * relative to the current directory.
  181. * @return boolean True if the directory was successfully removed, false
  182. * otherwise.
  183. * @access public
  184. */
  185. function removeDirectoryRecursive($dirName) {
  186. if(empty($dirName)) {
  187. return true;
  188. }
  189. if(removeDirectoryContents($dirName)) {
  190. @rmdir($dirName) or die('Folder '.$dirName.' couldn\'t be deleted!');
  191. } else {
  192. return false;
  193. }
  194. return true;
  195. }
  196.  
  197. /**
  198. * Encodes the string to be encoded for url use, but first alters some
  199. * characters to get a more readable string. For instance spaces are replaced
  200. * with underscores instead of %20. This function is not one to one.
  201. * @param string $input The input that should be encoded.
  202. * @return string An encoded string.
  203. * @access public
  204. */
  205. function urlEncodeReadable($input) {
  206. $input = str_replace(' ', '_', $input);
  207. return rawurlencode($input);
  208. }
  209. ?>

Documentation generated on Sun, 16 Apr 2006 21:03:59 +0200 by phpDocumentor 1.3.0RC4