util.php 14 KB

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