Sponsorenverwaltung - Team StarCraft e.V.
 All Data Structures Files Functions Variables
functions.php
Go to the documentation of this file.
1 <?php
2  /**
3  * @file functions.php
4  *
5  * @brief Bundle of functions widely used in several scripts.
6  *
7  * @details The file provides a bundle of functions widely used in several
8  * scripts. This include several abstractions of smarty template commands
9  * as well as some general purpose filters and the specially designed
10  * kdsort-algorithm.
11  *
12  * This file requires the global $smarty.
13  *
14  * @copyright (c) 2013, Team StarCraft e.V.
15  * @version 1.0.0
16  * @author Daniel Seichter
17  * @author Alexander Vorndran
18  * @date 02.07.2013
19  */
20 
21  define('TPL_ERRORS', 'error.tpl');
22  define('TPL_SUCCESSES', 'nocontent.tpl');
23 
24  /**
25  * Displays a template with error and success messages
26  * @param string $title title of the page assigned by template engine
27  * @param string $fileName filename of the template to load and display
28  * @author Daniel Seichter
29  */
30  function displayTemplateWithErrorsAndSuccesses($title, $fileName) {
32 
33  // assign errors messages if not empty
34  if (!empty($errors)) {
35  $smarty->assign('errorTextArray', $errors);
36  }
37  // assign success messages if not empty
38  if (!empty($successes)) {
39  $smarty->assign('successTextArray', $successes);
40  }
41  $smarty->assign('title',$title);
42 
43  // calc script time
44  if (isset($scriptDurationStartValue)) {
45  $scriptDuration = microtime(TRUE) - $scriptDurationStartValue;
46  $smarty->assign('scriptDuration', sprintf('%.8f', $scriptDuration));
47  }
48  // display template
49  $smarty->display($fileName);
50  }
51 
52  /**
53  * Displays the error template with error messages and stop the execution
54  * @param array $errorTextArray [optional]
55  * - array of additional error messages
56  * - Default: null
57  * @author Daniel Seichter
58  */
59  function exitWithErrorTemplate($errorTextArray = null) {
60  global $smarty, $errors;
61 
62  // merge current errors with additional errors
63  if (isset($errorTextArray)) {
64  $errors = array_merge($errors, $errorTextArray);
65  }
66  // display template
68  // stop execution
69  exit();
70  }
71 
72  /**
73  * Displays the error template with error messages, stop the execution and redirect to a url
74  * @param array $errorTextArray [optional]
75  * - array of additional error messages
76  * - Default: null
77  * @param string $url url to redirect
78  * @param integer $delay [optional]
79  * - redirection delay in seconds
80  * - Default: 2
81  * @author Daniel Seichter
82  */
83  function exitWithErrorTemplateAndRedirect($errorTextArray = null, $url, $delay = 2) {
84  global $smarty, $errors;
85 
86  // set header redirection
87  if (is_numeric($delay)) {
88  header("Refresh: ".$delay."; ".$url);
89  } else {
90  header("Refresh: ".$url);
91  }
92  // merge current errors with additional errors
93  if (isset($errorTextArray)) {
94  $errors = array_merge($errors, $errorTextArray);
95  }
96  // display template
98  // stop execution
99  exit();
100  }
101 
102  /**
103  * Displays the success template with success messages, stop the execution and redirect to a url
104  * @param array $successTextArray [optional]
105  * - array of additional success messages
106  * - Default: null
107  * @param string $url url to redirect
108  * @param integer $delay [optional]
109  * - redirection delay in seconds
110  * - Default: 2
111  * @author Daniel Seichter
112  */
113  function exitWithSuccessTemplateAndRedirect($successTextArray = null, $url, $delay = 2) {
114  global $smarty, $successes;
115 
116  // set header redirection
117  if (is_numeric($delay)) {
118  header("Refresh: ".$delay."; ".$url);
119  } else {
120  header("Refresh: ".$url);
121  }
122  // merge current success with additional errors
123  if (isset($successTextArray)) {
124  $successes = array_merge($successes, $successTextArray);
125  }
126  // display template
128  // stop execution
129  exit();
130  }
131 
132  /**
133  * Sorts an two-dimensional array by a key in the second dimension
134  * @param array &$arr array to sort the array will be sorted in place
135  * @param integer $colIdx index of the column in the second dimension to sort by
136  * @param integer $dir [optional]
137  * - SORT_ASC: ascending order
138  * - SORT_DESC descending order
139  * - Default: SORT_ASC
140  * @author Daniel Seichter
141  * @author Alexandern Vorndran
142  * @return booloean FALSE if an error occured
143  */
144  function kdsort(&$arr, $colIdx, $dir = SORT_ASC) {
145  // retrun if there is no array
146  if(empty($arr)) {
147  return FALSE;
148  }
149  // get key name to sort to sort by
150  $keys = array_keys($arr);
151  if (!is_array($keys)) {
152  return FALSE;
153  }
154  $firstKey = $keys[0];
155  $keys = array_keys($arr[$firstKey]);
156  if (!is_array($keys)) {
157  return FALSE;
158  }
159  $cntKeys = count($keys);
160  $key = $keys[$colIdx%$cntKeys];
161 
162  usort($arr, function($a, $b) use($key, $dir) {
163  // compare elements
164  $elemA = $a[$key];
165  $elemB = $b[$key];
166  if(is_string($elemA) && is_string($elemB)) {
167  return ($dir == SORT_ASC) ? strcasecmp($elemA, $elemB) : strcasecmp($elemB, $elemA);
168  }
169  else {
170  return ($dir == SORT_ASC) ? $elemA - $elemB : $elemB - $elemA;
171  }
172  });
173  }
174 
175  /**
176  * Checks if an email is valid
177  * @param string $email email to check
178  * @return boolean TRUE if it is valid else FALSE
179  * @author Alexandern Vorndran
180  */
181  function isValidEmail($email) {
182  if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
183  return TRUE;
184  } else {
185  return FALSE;
186  }
187  }
188 
189  /**
190  * Checks if a mobil number is valid
191  * @param string $mobile mobile number to check
192  * @return boolean TRUE if the number is valid else FALSE
193  * @author Alexandern Vorndran
194  */
195  function isValidMobile($mobile) {
196  $mobile = str_replace(array(' ','/','(',')'), '', trim($mobile));
197  //very basic pattern to match with (german) mobile phone numbers
198  $pattern = "((\+|00)(49|43)|0)1[0-9]{7,40}";
199  // tries to match it with the regex
200  if (preg_match_all("/$pattern$/i", $mobile)) {
201  return TRUE;
202  } else {
203  return FALSE;
204  }
205  }
206 
207  /**
208  * Checks if a telephone number is valid
209  * @param string $phone the phone number to check
210  * @return boolean TRUE if it's valid else FALSE
211  * @author Alexandern Vorndran
212  */
213  function isValidPhone($phone) {
214  $phone = str_replace(array(' ','/','(',')'), '', $phone);
215  $pattern = "((\+|00)(49|43)|0)[0-9]{7,40}";
216  // tries to match it with the regex
217  if (preg_match_all("/$pattern$/i", $phone)) {
218  return TRUE;
219  } else {
220  return FALSE;
221  }
222  }
223 
224  /**
225  * Gets a valid filename by replacing some special chars
226  * @param string $fileName filename to prepare
227  * @return string valid filename
228  * @author Daniel Seichter
229  */
230  function prepareFilename($fileName){
231  $fileName = str_replace(array('ä', 'ö', 'ü', 'Ä', 'Ö', 'Ü' ,'ß' , ' '),
232  array('ae', 'oe', 'ue', 'Ae', 'Oe', 'Ue', 'ss', '_'),
233  $fileName);
234  $fileName = preg_replace('/[^A-Za-z]{1,}/', '', $fileName);
235  return $fileName;
236  }
237 
238  /**
239  * Adds the string http-scheme to an url if not exists
240  * @param string $url url to check and to add scheme
241  * @param string $schemeToAdd
242  * - scheme to add in front of the url if not exists
243  * - Default: 'http://'
244  * @return string valid url with scheme
245  * @author Daniel Seichter
246  */
247  function addSchemeToURL($url, $schemeToAdd = 'http://') {
248  //skip if url is empty
249  if (empty($url)) {
250  return $url;
251  }
252  $scheme = parse_url($url, PHP_URL_SCHEME);
253  if (empty($scheme)) {
254  return $schemeToAdd.$url;
255  }
256  return $url;
257  }
258 ?>