util.php 14 KB

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