blob: 8a57c183e139532cd662b7bb757a53d45ef6e686 [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
Derek Jonesb8c283a2013-07-19 16:02:53 -070012.. 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
Derek Jonesb8c283a2013-07-19 16:02:53 -070034.. 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
Derek Jonesb8c283a2013-07-19 16:02:53 -070061.. function:: config_item($key)
Andrey Andreev1bc30262012-11-09 11:30:51 +020062
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
Andrey Andreev1bc30262012-11-09 11:30:51 +020071show_error()
72============
Derek Jones8ede1a22011-10-05 13:34:52 -050073
Derek Jonesb8c283a2013-07-19 16:02:53 -070074.. function:: show_error($message, $status_code, $heading = 'An Error Was Encountered')
Derek Jones8ede1a22011-10-05 13:34:52 -050075
Andrey Andreev1bc30262012-11-09 11:30:51 +020076 :param mixed $message: Error message
77 :param int $status_code: HTTP Response status code
78 :param string $heading: Error page heading
79 :returns: void
80
81This function calls ``CI_Exception::show_error()``. For more info,
82please see the :doc:`Error Handling <errors>` documentation.
83
84show_404()
85==========
86
Derek Jonesb8c283a2013-07-19 16:02:53 -070087.. function:: show_404($page = '', $log_error = TRUE)
Andrey Andreev1bc30262012-11-09 11:30:51 +020088
89 :param string $page: URI string
90 :param bool $log_error: Whether to log the error
91 :returns: void
92
93This function calls ``CI_Exception::show_404()``. For more info,
94please see the :doc:`Error Handling <errors>` documentation.
95
96log_message()
97=============
98
Derek Jonesb8c283a2013-07-19 16:02:53 -070099.. function:: log_message($level, $message, $php_error = FALSE)
Andrey Andreev1bc30262012-11-09 11:30:51 +0200100
vlakoffd0c30ab2013-05-07 07:49:23 +0200101 :param string $level: Log level: 'error', 'debug' or 'info'
Andrey Andreev1bc30262012-11-09 11:30:51 +0200102 :param string $message: Message to log
vlakoffd0c30ab2013-05-07 07:49:23 +0200103 :param bool $php_error: Whether we're logging a native PHP error message
Andrey Andreev1bc30262012-11-09 11:30:51 +0200104 :returns: void
105
106This function is an alias for ``CI_Log::write_log()``. For more info,
107please see the :doc:`Error Handling <errors>` documentation.
108
109set_status_header()
Andrey Andreev3fb02672012-10-22 16:48:01 +0300110===============================
Derek Jones8ede1a22011-10-05 13:34:52 -0500111
Derek Jonesb8c283a2013-07-19 16:02:53 -0700112.. function:: set_status_header($code, $text = '')
Andrey Andreev1bc30262012-11-09 11:30:51 +0200113
114 :param int $code: HTTP Reponse status code
115 :param string $text: A custom message to set with the status code
116 :returns: void
117
Derek Jones8ede1a22011-10-05 13:34:52 -0500118Permits you to manually set a server status header. Example::
119
Derek Jones46715e52011-10-05 17:36:22 -0500120 set_status_header(401);
121 // Sets the header as: Unauthorized
Derek Jones8ede1a22011-10-05 13:34:52 -0500122
123`See here <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html>`_ for
124a full list of headers.
125
Andrey Andreev1bc30262012-11-09 11:30:51 +0200126remove_invisible_characters()
127=============================
Derek Jones8ede1a22011-10-05 13:34:52 -0500128
Derek Jonesb8c283a2013-07-19 16:02:53 -0700129.. function:: remove_invisible_characters($str, $url_encoded = TRUE)
Andrey Andreev1bc30262012-11-09 11:30:51 +0200130
131 :param string $str: Input string
132 :param bool $url_encoded: Whether to remove URL-encoded characters as well
133 :returns: string
134
135This function prevents inserting NULL characters between ASCII
Derek Jones8ede1a22011-10-05 13:34:52 -0500136characters, like Java\\0script.
137
Andrey Andreev1bc30262012-11-09 11:30:51 +0200138Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500139
Andrey Andreev1bc30262012-11-09 11:30:51 +0200140 remove_invisible_characters('Java\\0script');
141 // Returns: 'Javascript'
142
143html_escape()
144=============
145
Derek Jonesb8c283a2013-07-19 16:02:53 -0700146.. function:: html_escape($var)
Andrey Andreev1bc30262012-11-09 11:30:51 +0200147
148 :param mixed $var: Variable to escape
149 (string or array)
150 :returns: mixed
151
152This function acts as an alias for PHP's native ``htmlspecialchars()``
153function, with the advantage of being able to accept an array of strings.
154
155It is useful in preventing Cross Site Scripting (XSS).
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300156
157get_mimes()
Andrey Andreev3fb02672012-10-22 16:48:01 +0300158===========
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300159
Derek Jonesb8c283a2013-07-19 16:02:53 -0700160.. function:: get_mimes()
Andrey Andreev1bc30262012-11-09 11:30:51 +0200161
162 :returns: array
163
164This function returns a *reference* to the MIMEs array from
165*application/config/mimes.php*.
Andrey Andreev3fb02672012-10-22 16:48:01 +0300166
167is_https()
168==========
169
Derek Jonesb8c283a2013-07-19 16:02:53 -0700170.. function:: is_https()
Andrey Andreev1bc30262012-11-09 11:30:51 +0200171
172 :returns: bool
173
Andrey Andreev3fb02672012-10-22 16:48:01 +0300174Returns TRUE if a secure (HTTPS) connection is used and FALSE
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200175in any other case (including non-HTTP requests).
176
Andrey Andreev838a9d62012-12-03 14:37:47 +0200177function_usable()
178=================
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200179
Derek Jonesb8c283a2013-07-19 16:02:53 -0700180.. function:: function_usable($function_name)
Andrey Andreev1bc30262012-11-09 11:30:51 +0200181
182 :param string $function_name: Function name
183 :returns: bool
184
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200185Returns TRUE if a function exists and is usable, FALSE otherwise.
186
187This function runs a ``function_exists()`` check and if the
188`Suhosin extension <http://www.hardened-php.net/suhosin/>` is loaded,
189checks if it doesn't disable the function being checked.
190
191It is useful if you want to check for the availability of functions
192such as ``eval()`` and ``exec()``, which are dangerous and might be
193disabled on servers with highly restrictive security policies.