Blame view
sources/lib/public/util.php
14 KB
|
03e52840d
|
1 2 |
<?php /** |
|
31b7f2792
|
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
* ownCloud * * @author Frank Karlitschek * @copyright 2012 Frank Karlitschek frank@owncloud.org * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE * License as published by the Free Software Foundation; either * version 3 of the License, or any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU AFFERO GENERAL PUBLIC LICENSE for more details. * * You should have received a copy of the GNU Affero General Public * License along with this library. If not, see <http://www.gnu.org/licenses/>. * */ |
|
03e52840d
|
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
/**
* Public interface of ownCloud for apps to use.
* Utility Class.
*
*/
// use OCP namespace for all classes that are considered public.
// This means that they should be used by apps instead of the internal ownCloud classes
namespace OCP;
/**
* This class provides different helper functions to make the life of a developer easier
*/
class Util {
// consts for Logging
const DEBUG=0;
const INFO=1;
const WARN=2;
const ERROR=3;
const FATAL=4;
/**
|
|
31b7f2792
|
45 |
* get the current installed version of ownCloud |
|
03e52840d
|
46 47 48 49 50 51 52 |
* @return array
*/
public static function getVersion() {
return(\OC_Util::getVersion());
}
/**
|
|
31b7f2792
|
53 |
* send an email |
|
03e52840d
|
54 55 56 57 58 59 60 |
* @param string $toaddress * @param string $toname * @param string $subject * @param string $mailtext * @param string $fromaddress * @param string $fromname * @param bool $html |
|
31b7f2792
|
61 62 63 64 |
* @param string $altbody * @param string $ccaddress * @param string $ccname * @param string $bcc |
|
03e52840d
|
65 66 67 68 69 70 71 72 73 |
*/
public static function sendMail( $toaddress, $toname, $subject, $mailtext, $fromaddress, $fromname,
$html = 0, $altbody = '', $ccaddress = '', $ccname = '', $bcc = '') {
// call the internal mail class
\OC_MAIL::send($toaddress, $toname, $subject, $mailtext, $fromaddress, $fromname,
$html, $altbody, $ccaddress, $ccname, $bcc);
}
/**
|
|
31b7f2792
|
74 |
* write a message in the log |
|
03e52840d
|
75 76 77 78 79 80 81 82 83 84 |
* @param string $app
* @param string $message
* @param int $level
*/
public static function writeLog( $app, $message, $level ) {
// call the internal log class
\OC_LOG::write( $app, $message, $level );
}
/**
|
|
31b7f2792
|
85 86 87 88 89 90 |
* write exception into the log. Include the stack trace
* if DEBUG mode is enabled
* @param string $app app name
* @param Exception $ex exception to log
*/
public static function logException( $app, \Exception $ex ) {
|
|
a293d369c
|
91 92 93 94 95 |
$class = get_class($ex);
if ($class !== 'Exception') {
$message = $class . ': ';
}
$message .= $ex->getMessage();
|
|
31b7f2792
|
96 97 98 99 100 101 |
if ($ex->getCode()) {
$message .= ' [' . $ex->getCode() . ']';
}
\OCP\Util::writeLog($app, 'Exception: ' . $message, \OCP\Util::FATAL);
if (defined('DEBUG') and DEBUG) {
// also log stack trace
|
|
a293d369c
|
102 103 |
$stack = explode("
", $ex->getTraceAsString());
|
|
31b7f2792
|
104 105 106 107 108 109 110 |
// first element is empty
array_shift($stack);
foreach ($stack as $s) {
\OCP\Util::writeLog($app, 'Exception: ' . $s, \OCP\Util::FATAL);
}
// include cause
|
|
31b7f2792
|
111 |
while (method_exists($ex, 'getPrevious') && $ex = $ex->getPrevious()) {
|
|
a293d369c
|
112 |
$message .= ' - Caused by:' . ' '; |
|
31b7f2792
|
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
$message .= $ex->getMessage();
if ($ex->getCode()) {
$message .= '[' . $ex->getCode() . '] ';
}
\OCP\Util::writeLog($app, 'Exception: ' . $message, \OCP\Util::FATAL);
}
}
}
/**
* get l10n object
* @param string $application
* @return OC_L10N
*/
public static function getL10N( $application ) {
return \OC_L10N::get( $application );
}
/**
* add a css file
* @param string $application
* @param string $file
|
|
03e52840d
|
135 136 137 138 139 140 |
*/
public static function addStyle( $application, $file = null ) {
\OC_Util::addStyle( $application, $file );
}
/**
|
|
31b7f2792
|
141 |
* add a javascript file |
|
03e52840d
|
142 |
* @param string $application |
|
31b7f2792
|
143 |
* @param string $file |
|
03e52840d
|
144 145 146 147 148 149 |
*/
public static function addScript( $application, $file = null ) {
\OC_Util::addScript( $application, $file );
}
/**
|
|
31b7f2792
|
150 |
* Add a custom element to the header |
|
03e52840d
|
151 152 153 154 155 156 157 158 159 |
* @param string $tag tag name of the element
* @param array $attributes array of attributes for the element
* @param string $text the text content for the element
*/
public static function addHeader( $tag, $attributes, $text='') {
\OC_Util::addHeader( $tag, $attributes, $text );
}
/**
|
|
31b7f2792
|
160 |
* formats a timestamp in the "right" way |
|
03e52840d
|
161 162 163 164 165 166 167 168 |
* @param int $timestamp $timestamp
* @param bool $dateOnly option to omit time from the result
*/
public static function formatDate( $timestamp, $dateOnly=false) {
return(\OC_Util::formatDate( $timestamp, $dateOnly ));
}
/**
|
|
31b7f2792
|
169 170 171 172 173 174 175 176 177 |
* check if some encrypted files are stored
* @return bool
*/
public static function encryptedFiles() {
return \OC_Util::encryptedFiles();
}
/**
* Creates an absolute url to the given app and file.
|
|
03e52840d
|
178 179 180 181 |
* @param string $app app * @param string $file file * @param array $args array with param=>value, will be appended to the returned url * The value of $args will be urlencoded |
|
31b7f2792
|
182 |
* @return string the url |
|
03e52840d
|
183 184 185 186 187 188 |
*/
public static function linkToAbsolute( $app, $file, $args = array() ) {
return(\OC_Helper::linkToAbsolute( $app, $file, $args ));
}
/**
|
|
31b7f2792
|
189 |
* Creates an absolute url for remote use. |
|
03e52840d
|
190 |
* @param string $service id |
|
31b7f2792
|
191 |
* @return string the url |
|
03e52840d
|
192 193 194 195 196 197 |
*/
public static function linkToRemote( $service ) {
return(\OC_Helper::linkToRemote( $service ));
}
/**
|
|
31b7f2792
|
198 |
* Creates an absolute url for public use |
|
03e52840d
|
199 |
* @param string $service id |
|
31b7f2792
|
200 |
* @return string the url |
|
03e52840d
|
201 202 203 204 205 206 |
*/
public static function linkToPublic($service) {
return \OC_Helper::linkToPublic($service);
}
/**
|
|
31b7f2792
|
207 |
* Creates an url using a defined route |
|
03e52840d
|
208 209 210 211 |
* @param $route * @param array $parameters * @return * @internal param array $args with param=>value, will be appended to the returned url |
|
31b7f2792
|
212 |
* @return the url |
|
03e52840d
|
213 214 215 216 217 218 |
*/
public static function linkToRoute( $route, $parameters = array() ) {
return \OC_Helper::linkToRoute($route, $parameters);
}
/**
|
|
31b7f2792
|
219 |
* Creates an url to the given app and file |
|
03e52840d
|
220 221 222 223 |
* @param string $app app * @param string $file file * @param array $args array with param=>value, will be appended to the returned url * The value of $args will be urlencoded |
|
31b7f2792
|
224 |
* @return string the url |
|
03e52840d
|
225 226 227 228 229 230 |
*/
public static function linkTo( $app, $file, $args = array() ) {
return(\OC_Helper::linkTo( $app, $file, $args ));
}
/**
|
|
31b7f2792
|
231 232 |
* Returns the server host, even if the website uses one or more reverse proxy * @return string the server host |
|
03e52840d
|
233 234 235 236 237 238 |
*/
public static function getServerHost() {
return(\OC_Request::serverHost());
}
/**
|
|
03e52840d
|
239 |
* Returns the server host name without an eventual port number |
|
31b7f2792
|
240 |
* @return string the server hostname |
|
03e52840d
|
241 242 243 244 245 246 247 248 249 250 251 252 |
*/
public static function getServerHostName() {
$host_name = self::getServerHost();
// strip away port number (if existing)
$colon_pos = strpos($host_name, ':');
if ($colon_pos != FALSE) {
$host_name = substr($host_name, 0, $colon_pos);
}
return $host_name;
}
/**
|
|
31b7f2792
|
253 |
* Returns the default email address |
|
03e52840d
|
254 |
* @param string $user_part the user part of the address |
|
31b7f2792
|
255 |
* @return string the default email address |
|
03e52840d
|
256 257 258 259 260 261 262 263 264 |
*
* Assembles a default email address (using the server hostname
* and the given user part, and returns it
* Example: when given lostpassword-noreply as $user_part param,
* and is currently accessed via http(s)://example.com/,
* it would return 'lostpassword-noreply@example.com'
*/
public static function getDefaultEmailAddress($user_part) {
$host_name = self::getServerHostName();
|
|
31b7f2792
|
265 |
$host_name = \OC_Config::getValue('mail_domain', $host_name);
|
|
03e52840d
|
266 267 268 269 270 271 272 273 274 275 276 |
$defaultEmailAddress = $user_part.'@'.$host_name;
if (\OC_Mail::ValidateAddress($defaultEmailAddress)) {
return $defaultEmailAddress;
}
// in case we cannot build a valid email address from the hostname let's fallback to 'localhost.localdomain'
return $user_part.'@localhost.localdomain';
}
/**
|
|
03e52840d
|
277 |
* Returns the server protocol. It respects reverse proxy servers and load balancers |
|
31b7f2792
|
278 |
* @return string the server protocol |
|
03e52840d
|
279 280 281 282 283 284 |
*/
public static function getServerProtocol() {
return(\OC_Request::serverProtocol());
}
/**
|
|
31b7f2792
|
285 |
* Returns the request uri, even if the website uses one or more reverse proxies |
|
03e52840d
|
286 |
* |
|
31b7f2792
|
287 |
* @return the request uri |
|
03e52840d
|
288 289 290 291 292 293 |
*/
public static function getRequestUri() {
return(\OC_Request::requestUri());
}
/**
|
|
31b7f2792
|
294 |
* Returns the script name, even if the website uses one or more reverse proxies |
|
03e52840d
|
295 |
* |
|
31b7f2792
|
296 |
* @return the script name |
|
03e52840d
|
297 298 299 300 301 302 |
*/
public static function getScriptName() {
return(\OC_Request::scriptName());
}
/**
|
|
31b7f2792
|
303 |
* Creates path to an image |
|
03e52840d
|
304 305 |
* @param string $app app * @param string $image image name |
|
31b7f2792
|
306 |
* @return string the url |
|
03e52840d
|
307 308 309 310 311 312 |
*/
public static function imagePath( $app, $image ) {
return(\OC_Helper::imagePath( $app, $image ));
}
/**
|
|
31b7f2792
|
313 |
* Make a human file size (2048 to 2 kB) |
|
03e52840d
|
314 |
* @param int $bytes file size in bytes |
|
31b7f2792
|
315 |
* @return string a human readable file size |
|
03e52840d
|
316 317 318 319 320 321 |
*/
public static function humanFileSize( $bytes ) {
return(\OC_Helper::humanFileSize( $bytes ));
}
/**
|
|
31b7f2792
|
322 |
* Make a computer file size (2 kB to 2048) |
|
03e52840d
|
323 |
* @param string $str file size in a fancy format |
|
31b7f2792
|
324 |
* @return int a file size in bytes |
|
03e52840d
|
325 326 327 328 329 330 331 332 |
*
* Inspired by: http://www.php.net/manual/en/function.filesize.php#92418
*/
public static function computerFileSize( $str ) {
return(\OC_Helper::computerFileSize( $str ));
}
/**
|
|
31b7f2792
|
333 |
* connects a function to a hook |
|
03e52840d
|
334 335 336 337 |
* @param string $signalclass class name of emitter * @param string $signalname name of signal * @param string $slotclass class name of slot * @param string $slotname name of slot |
|
31b7f2792
|
338 |
* @return bool |
|
03e52840d
|
339 340 341 342 343 344 345 346 347 348 |
*
* This function makes it very easy to connect to use hooks.
*
* TODO: write example
*/
static public function connectHook( $signalclass, $signalname, $slotclass, $slotname ) {
return(\OC_Hook::connect( $signalclass, $signalname, $slotclass, $slotname ));
}
/**
|
|
31b7f2792
|
349 |
* Emits a signal. To get data from the slot use references! |
|
03e52840d
|
350 351 352 |
* @param string $signalclass class name of emitter * @param string $signalname name of signal * @param string $params defautl: array() array with additional data |
|
31b7f2792
|
353 |
* @return bool true if slots exists or false if not |
|
03e52840d
|
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
*
* TODO: write example
*/
static public function emitHook( $signalclass, $signalname, $params = array()) {
return(\OC_Hook::emit( $signalclass, $signalname, $params ));
}
/**
* Register an get/post call. This is important to prevent CSRF attacks
* TODO: write example
*/
public static function callRegister() {
return(\OC_Util::callRegister());
}
/**
* Check an ajax get/post call if the request token is valid. exit if not.
* Todo: Write howto
*/
public static function callCheck() {
\OC_Util::callCheck();
}
/**
|
|
31b7f2792
|
378 |
* Used to sanitize HTML |
|
03e52840d
|
379 380 381 382 383 384 385 386 387 388 389 390 |
*
* This function is used to sanitize HTML and should be applied on any
* string or array of strings before displaying it on a web page.
*
* @param string|array of strings
* @return array with sanitized strings or a single sinitized string, depends on the input parameter.
*/
public static function sanitizeHTML( $value ) {
return(\OC_Util::sanitizeHTML($value));
}
/**
|
|
31b7f2792
|
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
* Public function to encode url parameters
*
* This function is used to encode path to file before output.
* Encoding is done according to RFC 3986 with one exception:
* Character '/' is preserved as is.
*
* @param string $component part of URI to encode
* @return string
*/
public static function encodePath($component) {
return(\OC_Util::encodePath($component));
}
/**
* Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is.
*
* @param array $input The array to work on
* @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default)
* @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8
* @return array
*/
|
|
03e52840d
|
412 413 414 415 416 |
public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') {
return(\OC_Helper::mb_array_change_key_case($input, $case, $encoding));
}
/**
|
|
31b7f2792
|
417 418 419 420 421 422 423 424 425 |
* replaces a copy of string delimited by the start and (optionally) length parameters with the string given in replacement. * * @param string $string The input string. Opposite to the PHP build-in function does not accept an array. * @param string $replacement The replacement string. * @param int $start If start is positive, the replacing will begin at the start'th offset into string. If start is negative, the replacing will begin at the start'th character from the end of string. * @param int $length Length of the part to be replaced * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 * @return string */ |
|
03e52840d
|
426 427 428 429 430 |
public static function mb_substr_replace($string, $replacement, $start, $length = null, $encoding = 'UTF-8') {
return(\OC_Helper::mb_substr_replace($string, $replacement, $start, $length, $encoding));
}
/**
|
|
31b7f2792
|
431 432 433 434 435 436 437 438 439 |
* Replace all occurrences of the search string with the replacement string * * @param string $search The value being searched for, otherwise known as the needle. String. * @param string $replace The replacement string. * @param string $subject The string or array being searched and replaced on, otherwise known as the haystack. * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 * @param int $count If passed, this will be set to the number of replacements performed. * @return string */ |
|
03e52840d
|
440 441 442 443 444 |
public static function mb_str_replace($search, $replace, $subject, $encoding = 'UTF-8', &$count = null) {
return(\OC_Helper::mb_str_replace($search, $replace, $subject, $encoding, $count));
}
/**
|
|
31b7f2792
|
445 446 447 448 449 450 451 |
* performs a search in a nested array * * @param array $haystack the array to be searched * @param string $needle the search string * @param int $index optional, only search this key name * @return mixed the key of the matching field, otherwise false */ |
|
03e52840d
|
452 453 454 455 456 |
public static function recursiveArraySearch($haystack, $needle, $index = null) {
return(\OC_Helper::recursiveArraySearch($haystack, $needle, $index));
}
/**
|
|
31b7f2792
|
457 |
* calculates the maximum upload size respecting system settings, free space and user quota |
|
03e52840d
|
458 459 460 461 462 463 464 465 |
*
* @param $dir the current folder where the user currently operates
* @return number of bytes representing
*/
public static function maxUploadFilesize($dir) {
return \OC_Helper::maxUploadFilesize($dir);
}
}
|