blob: 79bd9b459617bedb5cf7a66098a9c1931175859b [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001################
2Common Functions
3################
4
5CodeIgniter uses a few functions for its operation that are globally
6defined, and are available to you at any point. These do not require
7loading any libraries or helpers.
8
Andrey Andreev1bc30262012-11-09 11:30:51 +02009is_php()
10========
Derek Jones8ede1a22011-10-05 13:34:52 -050011
Andrey Andreev1bc30262012-11-09 11:30:51 +020012.. php:function:: is_php($version = '5.3.0')
Derek Jones8ede1a22011-10-05 13:34:52 -050013
Andrey Andreev1bc30262012-11-09 11:30:51 +020014 :param string $version: Version number
15 :returns: bool
Derek Jones8ede1a22011-10-05 13:34:52 -050016
Andrey Andreev1bc30262012-11-09 11:30:51 +020017Determines of the PHP version being used is greater than the
18supplied version number.
19
20Example::
21
22 if (is_php('5.3'))
Derek Jones46715e52011-10-05 17:36:22 -050023 {
Andrey Andreev1bc30262012-11-09 11:30:51 +020024 $str = quoted_printable_encode($str);
Derek Jones46715e52011-10-05 17:36:22 -050025 }
Derek Jones8ede1a22011-10-05 13:34:52 -050026
27Returns boolean TRUE if the installed version of PHP is equal to or
28greater than the supplied version number. Returns FALSE if the installed
29version of PHP is lower than the supplied version number.
30
Andrey Andreev1bc30262012-11-09 11:30:51 +020031is_really_writable()
32====================
Derek Jones8ede1a22011-10-05 13:34:52 -050033
vlakoff9a6032d2013-03-05 23:03:12 +010034.. php:function:: is_really_writable($file)
Andrey Andreev1bc30262012-11-09 11:30:51 +020035
36 :param string $file: File path
37 :returns: bool
38
39``is_writable()`` returns TRUE on Windows servers when you really can't
Derek Jones8ede1a22011-10-05 13:34:52 -050040write to the file as the OS reports to PHP as FALSE only if the
Andrey Andreev1bc30262012-11-09 11:30:51 +020041read-only attribute is marked.
Derek Jones8ede1a22011-10-05 13:34:52 -050042
Andrey Andreev1bc30262012-11-09 11:30:51 +020043This function determines if a file is actually writable by attempting
44to write to it first. Generally only recommended on platforms where
45this information may be unreliable.
46
47Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050048
Derek Jones46715e52011-10-05 17:36:22 -050049 if (is_really_writable('file.txt'))
50 {
Andrey Andreev1bc30262012-11-09 11:30:51 +020051 echo "I could write to this if I wanted to";
Derek Jones46715e52011-10-05 17:36:22 -050052 }
53 else
54 {
Andrey Andreev1bc30262012-11-09 11:30:51 +020055 echo "File is not writable";
Derek Jones46715e52011-10-05 17:36:22 -050056 }
Derek Jones8ede1a22011-10-05 13:34:52 -050057
Andrey Andreev1bc30262012-11-09 11:30:51 +020058config_item()
59=============
60
61.. php:function:: config_item($key)
62
63 :param string $key: Config item key
64 :returns: mixed
Derek Jones8ede1a22011-10-05 13:34:52 -050065
Andrey Andreev7b18a3f2012-11-04 20:27:35 +020066The :doc:`Config Library <../libraries/config>` is the preferred way of
67accessing configuration information, however ``config_item()`` can be used
68to retrieve single keys. See :doc:`Config Library <../libraries/config>`
69documentation for more information.
70
71.. important:: This function only returns values set in your configuration
72 files. It does not take into account config values that are
73 dynamically set at runtime.
Derek Jones8ede1a22011-10-05 13:34:52 -050074
Andrey Andreev1bc30262012-11-09 11:30:51 +020075show_error()
76============
Derek Jones8ede1a22011-10-05 13:34:52 -050077
Andrey Andreev1bc30262012-11-09 11:30:51 +020078.. php:function:: show_error($message, $status_code, $heading = 'An Error Was Encountered')
Derek Jones8ede1a22011-10-05 13:34:52 -050079
Andrey Andreev1bc30262012-11-09 11:30:51 +020080 :param mixed $message: Error message
81 :param int $status_code: HTTP Response status code
82 :param string $heading: Error page heading
83 :returns: void
84
85This function calls ``CI_Exception::show_error()``. For more info,
86please see the :doc:`Error Handling <errors>` documentation.
87
88show_404()
89==========
90
91.. php:function:: show_404($page = '', $log_error = TRUE)
92
93 :param string $page: URI string
94 :param bool $log_error: Whether to log the error
95 :returns: void
96
97This function calls ``CI_Exception::show_404()``. For more info,
98please see the :doc:`Error Handling <errors>` documentation.
99
100log_message()
101=============
102
103.. php:function:: log_message($level = 'error', $message, $php_error = FALSE)
104
105 :param string $level: Log level
106 :param string $message: Message to log
107 :param bool $php_error: Whether we're loggin a native PHP error message
108 :returns: void
109
110This function is an alias for ``CI_Log::write_log()``. For more info,
111please see the :doc:`Error Handling <errors>` documentation.
112
113set_status_header()
Andrey Andreev3fb02672012-10-22 16:48:01 +0300114===============================
Derek Jones8ede1a22011-10-05 13:34:52 -0500115
Andrey Andreev1bc30262012-11-09 11:30:51 +0200116.. php:function:: set_status_header($code, $text = '')
117
118 :param int $code: HTTP Reponse status code
119 :param string $text: A custom message to set with the status code
120 :returns: void
121
Derek Jones8ede1a22011-10-05 13:34:52 -0500122Permits you to manually set a server status header. Example::
123
Derek Jones46715e52011-10-05 17:36:22 -0500124 set_status_header(401);
125 // Sets the header as: Unauthorized
Derek Jones8ede1a22011-10-05 13:34:52 -0500126
127`See here <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html>`_ for
128a full list of headers.
129
Andrey Andreev1bc30262012-11-09 11:30:51 +0200130remove_invisible_characters()
131=============================
Derek Jones8ede1a22011-10-05 13:34:52 -0500132
Andrey Andreev1bc30262012-11-09 11:30:51 +0200133.. php:function:: remove_invisible_characters($str, $url_encoded = TRUE)
134
135 :param string $str: Input string
136 :param bool $url_encoded: Whether to remove URL-encoded characters as well
137 :returns: string
138
139This function prevents inserting NULL characters between ASCII
Derek Jones8ede1a22011-10-05 13:34:52 -0500140characters, like Java\\0script.
141
Andrey Andreev1bc30262012-11-09 11:30:51 +0200142Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500143
Andrey Andreev1bc30262012-11-09 11:30:51 +0200144 remove_invisible_characters('Java\\0script');
145 // Returns: 'Javascript'
146
147html_escape()
148=============
149
150.. php:function:: html_escape($var)
151
152 :param mixed $var: Variable to escape
153 (string or array)
154 :returns: mixed
155
156This function acts as an alias for PHP's native ``htmlspecialchars()``
157function, with the advantage of being able to accept an array of strings.
158
159It is useful in preventing Cross Site Scripting (XSS).
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300160
161get_mimes()
Andrey Andreev3fb02672012-10-22 16:48:01 +0300162===========
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300163
Andrey Andreev1bc30262012-11-09 11:30:51 +0200164.. php:function:: get_mimes()
165
166 :returns: array
167
168This function returns a *reference* to the MIMEs array from
169*application/config/mimes.php*.
Andrey Andreev3fb02672012-10-22 16:48:01 +0300170
171is_https()
172==========
173
Andrey Andreev1bc30262012-11-09 11:30:51 +0200174.. php:function:: is_https()
175
176 :returns: bool
177
Andrey Andreev3fb02672012-10-22 16:48:01 +0300178Returns TRUE if a secure (HTTPS) connection is used and FALSE
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200179in any other case (including non-HTTP requests).
180
Andrey Andreev838a9d62012-12-03 14:37:47 +0200181function_usable()
182=================
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200183
Andrey Andreev1bc30262012-11-09 11:30:51 +0200184.. php:function:: function_usable($function_name)
185
186 :param string $function_name: Function name
187 :returns: bool
188
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200189Returns TRUE if a function exists and is usable, FALSE otherwise.
190
191This function runs a ``function_exists()`` check and if the
192`Suhosin extension <http://www.hardened-php.net/suhosin/>` is loaded,
193checks if it doesn't disable the function being checked.
194
195It is useful if you want to check for the availability of functions
196such as ``eval()`` and ``exec()``, which are dangerous and might be
197disabled on servers with highly restrictive security policies.