psf
functions.php
1 <?php
2 
3 // Part of php simple framework (psf)
4 
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 
15 // Copyright (c) Petr Bena <petr@bena.rocks> 2015 - 2019
16 
17 if (!defined("PSF_ENTRY_POINT"))
18  die("Not a valid psf entry point");
19 
20 require_once(dirname(__FILE__) . "/default_config.php");
21 require_once(dirname(__FILE__) . "/definitions.php");
22 require_once(dirname(__FILE__) . "/variables.php");
23 
24 
25 // PHP
26 
28 function psf_php_enable_debug()
29 {
30  ini_set('display_errors', 1);
31  ini_set('display_startup_errors', 1);
32  error_reporting(E_ALL);
33 }
34 
35 // String tools
36 
38 function psf_string_auto_trim($string, $max, $suffix = "")
39 {
40  if (strlen($string) <= $max)
41  return $string;
42  return substr($string, 0, $max) . $suffix;
43 }
44 
46 function psf_string_startsWith($string, $text)
47 {
48  return (strpos($string, $text) === 0);
49 }
50 
52 function psf_string_endsWith($string, $text)
53 {
54  $length = strlen($text);
55  if ($length == 0)
56  return true;
57 
58  return (substr($string, -$length) === $text);
59 }
60 
62 function psf_string_contains($string, $text)
63 {
64  return strpos($string, $text) !== false;
65 }
66 
67 function psf_version()
68 {
69  return PSF_VERSION;
70 }
71 
72 function psf_path($file = '')
73 {
74  global $psf_home;
75  return $psf_home . $file;
76 }
77 
80 function psf_generate_friendly_name($text)
81 {
82  $text = str_replace(";", "", $text);
83  $text = str_replace("&", "", $text);
84  $text = str_replace("\"", "", $text);
85  $text = str_replace(">", "", $text);
86  $text = str_replace("<", "", $text);
87  $text = str_replace(" ", "_", $text);
88  $text = str_replace("(", "", $text);
89  $text = str_replace(")", "", $text);
90  $text = str_replace("/", "", $text);
91  $text = strtolower($text);
92  return $text;
93 }
94 
95 function psf_indent_text($text, $in)
96 {
97  global $psf_indent_system_enabled;
98  if (!$psf_indent_system_enabled)
99  return $text;
100  $prefix = '';
101  while ($in-- > 0)
102  $prefix .= " ";
103  $result = '';
104  $lines = explode("\n", $text);
105  foreach ($lines as $line)
106  $result .= $prefix . $line . "\n";
107  return $result;
108 }
109 
110 function _l($key)
111 {
112  return Localization::Get($key);
113 }
114 
115 function psf_string2bool($str)
116 {
117  $str = strtolower($str);
118  if ($str == "true")
119  return true;
120  if ($str == "t")
121  return true;
122  return false;
123 }
124 
125 function psf_curl($link, $timeout=5)
126 {
127  $ch = curl_init();
128  curl_setopt($ch, CURLOPT_URL, $link);
129  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
130  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
131  $data = curl_exec($ch);
132  curl_close($ch);
133  return $data;
134 }
135 
136 function psf_debug_log($text)
137 {
138  global $psf_global_debug_ring;
139  $psf_global_debug_ring[] = $text;
140 }
141 
142 function psf_print_debug_as_html()
143 {
144  global $psf_global_debug_ring;
145  $html = "";
146  foreach ($psf_global_debug_ring as $log)
147  $html .= "<!-- PSF Debug: " . htmlspecialchars($log) . " -->\n";
148  echo($html);
149 }
150 
152 function psf_get_execution_time()
153 {
154  global $psf_global_startup_time;
155  return (microtime(true) - $psf_global_startup_time);
156 }
157 
158