global.inc.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * @param string $errorMessage
  4. * @param null|string $sql
  5. */
  6. function dbError($errorMessage, $sql = null)
  7. {
  8. die('There was an error running the query ['.$errorMessage.']'.(!is_null($sql)?' with statement "'.$sql.'"':''));
  9. }
  10. /**
  11. * Holds all messages
  12. * @var array
  13. */
  14. $MESSAGES = array();
  15. /**
  16. * Add a new message
  17. * @param string $type Supported types: success, fail, info
  18. * @param string $message
  19. */
  20. function add_message($type, $message)
  21. {
  22. global $MESSAGES;
  23. $newmessage = array();
  24. $newmessage['type'] = $type;
  25. $newmessage['message'] = $message;
  26. $MESSAGES[] = $newmessage;
  27. }
  28. /**
  29. * Print all messages
  30. */
  31. function output_messages()
  32. {
  33. global $MESSAGES;
  34. if(count($MESSAGES) > 0) {
  35. echo '<div class="messages">';
  36. foreach($MESSAGES as $message){
  37. echo '<div class="notification notification-'.$message['type'].'">'.$message['message'].'</div>';
  38. }
  39. echo '</div>';
  40. }
  41. }
  42. /**
  43. * Add message to logfile
  44. *
  45. * @param string $text
  46. */
  47. function writeLog($text)
  48. {
  49. if(defined('WRITE_LOG') && defined('WRITE_LOG_PATH')){
  50. $logdestination = realpath(WRITE_LOG_PATH).DIRECTORY_SEPARATOR."webmum.log";
  51. if(is_writable(WRITE_LOG_PATH)){
  52. $logfile = fopen($logdestination, "a") or die("Unable to create or open logfile \"".$logdestination."\" in root directory!");
  53. fwrite($logfile, date('M d H:i:s').": ".$text."\n");
  54. fclose($logfile);
  55. }
  56. else{
  57. die("Directory \"".WRITE_LOG_PATH."\" isn't writable");
  58. }
  59. }
  60. }
  61. /**
  62. * Generate full url
  63. *
  64. * @param string $url
  65. *
  66. * @return string
  67. */
  68. function url($url)
  69. {
  70. return sprintf('%s/%s', rtrim(FRONTEND_BASE_PATH, '/'), trim($url, '/'));
  71. }
  72. /**
  73. * Redirect user to an url
  74. *
  75. * @param string $url
  76. */
  77. function redirect($url)
  78. {
  79. header("Location: ".url($url));
  80. exit;
  81. }
  82. /**
  83. * Split comma, semicolon or newline separated list of emails to string
  84. *
  85. * @param string $input
  86. *
  87. * @return array
  88. */
  89. function stringToEmails($input)
  90. {
  91. $list = explode(
  92. '|',
  93. str_replace(
  94. array(' ', ',', ';', "\r\n", "\r", "\n", '|', ':'),
  95. '|',
  96. $input
  97. )
  98. );
  99. foreach($list as $i => &$email){
  100. if(empty($email)){
  101. unset($list[$i]);
  102. }
  103. }
  104. return array_values(
  105. array_unique(
  106. array_map(
  107. 'formatEmail',
  108. $list
  109. )
  110. )
  111. );
  112. }
  113. /**
  114. * List of emails to comma or $glue separated list string
  115. *
  116. * @param array $list
  117. * @param string $glue
  118. *
  119. * @return string
  120. */
  121. function emailsToString($list, $glue = ',')
  122. {
  123. if(is_string($list)){
  124. return $list;
  125. }
  126. return implode($glue, $list);
  127. }
  128. /**
  129. * Format single email address
  130. *
  131. * @param string $input
  132. *
  133. * @return string
  134. */
  135. function formatEmail($input)
  136. {
  137. return strtolower(trim($input));
  138. }
  139. /**
  140. * Format email addresses (single, multiple in separated list, or array of email addresses)
  141. *
  142. * @param string|array $input
  143. * @param string $glue
  144. *
  145. * @return string
  146. */
  147. function formatEmails($input, $glue)
  148. {
  149. if(!is_array($input)){
  150. $input = stringToEmails($input);
  151. }
  152. return emailsToString($input, $glue);
  153. }