Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

boot.php 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. <?php
  2. /**
  3. * @file boot.php
  4. * This file defines some global constants and includes the central App class.
  5. */
  6. /**
  7. * Friendica
  8. *
  9. * Friendica is a communications platform for integrated social communications
  10. * utilising decentralised communications and linkage to several indie social
  11. * projects - as well as popular mainstream providers.
  12. *
  13. * Our mission is to free our friends and families from the clutches of
  14. * data-harvesting corporations, and pave the way to a future where social
  15. * communications are free and open and flow between alternate providers as
  16. * easily as email does today.
  17. */
  18. use Friendica\App;
  19. use Friendica\BaseObject;
  20. use Friendica\Core\Config;
  21. use Friendica\Core\PConfig;
  22. use Friendica\Core\Protocol;
  23. use Friendica\Core\System;
  24. use Friendica\Core\Session;
  25. use Friendica\Database\DBA;
  26. use Friendica\Model\Contact;
  27. use Friendica\Model\Term;
  28. use Friendica\Util\BasePath;
  29. use Friendica\Util\DateTimeFormat;
  30. define('FRIENDICA_PLATFORM', 'Friendica');
  31. define('FRIENDICA_CODENAME', 'Dalmatian Bellflower');
  32. define('FRIENDICA_VERSION', '2019.12-dev');
  33. define('DFRN_PROTOCOL_VERSION', '2.23');
  34. define('NEW_UPDATE_ROUTINE_VERSION', 1170);
  35. /**
  36. * @brief Constant with a HTML line break.
  37. *
  38. * Contains a HTML line break (br) element and a real carriage return with line
  39. * feed for the source.
  40. * This can be used in HTML and JavaScript where needed a line break.
  41. */
  42. define('EOL', "<br />\r\n");
  43. /**
  44. * @brief Image storage quality.
  45. *
  46. * Lower numbers save space at cost of image detail.
  47. * For ease of upgrade, please do not change here. Set system.jpegquality = n in config/local.config.php,
  48. * where n is between 1 and 100, and with very poor results below about 50
  49. */
  50. define('JPEG_QUALITY', 100);
  51. /**
  52. * system.png_quality = n where is between 0 (uncompressed) to 9
  53. */
  54. define('PNG_QUALITY', 8);
  55. /**
  56. * An alternate way of limiting picture upload sizes. Specify the maximum pixel
  57. * length that pictures are allowed to be (for non-square pictures, it will apply
  58. * to the longest side). Pictures longer than this length will be resized to be
  59. * this length (on the longest side, the other side will be scaled appropriately).
  60. * Modify this value using
  61. *
  62. * 'system' => [
  63. * 'max_image_length' => 'n',
  64. * ...
  65. * ],
  66. *
  67. * in config/local.config.php
  68. *
  69. * If you don't want to set a maximum length, set to -1. The default value is
  70. * defined by 'MAX_IMAGE_LENGTH' below.
  71. */
  72. define('MAX_IMAGE_LENGTH', -1);
  73. /**
  74. * Not yet used
  75. */
  76. define('DEFAULT_DB_ENGINE', 'InnoDB');
  77. /** @deprecated since version 2019.03, please use \Friendica\Module\Register::CLOSED instead */
  78. define('REGISTER_CLOSED', \Friendica\Module\Register::CLOSED);
  79. /** @deprecated since version 2019.03, please use \Friendica\Module\Register::APPROVE instead */
  80. define('REGISTER_APPROVE', \Friendica\Module\Register::APPROVE);
  81. /** @deprecated since version 2019.03, please use \Friendica\Module\Register::OPEN instead */
  82. define('REGISTER_OPEN', \Friendica\Module\Register::OPEN);
  83. /**
  84. * @name CP
  85. *
  86. * Type of the community page
  87. * @{
  88. */
  89. define('CP_NO_INTERNAL_COMMUNITY', -2);
  90. define('CP_NO_COMMUNITY_PAGE', -1);
  91. define('CP_USERS_ON_SERVER', 0);
  92. define('CP_GLOBAL_COMMUNITY', 1);
  93. define('CP_USERS_AND_GLOBAL', 2);
  94. /**
  95. * @}
  96. */
  97. /**
  98. * These numbers are used in stored permissions
  99. * and existing allocations MUST NEVER BE CHANGED
  100. * OR RE-ASSIGNED! You may only add to them.
  101. */
  102. $netgroup_ids = [
  103. Protocol::DFRN => (-1),
  104. Protocol::ZOT => (-2),
  105. Protocol::OSTATUS => (-3),
  106. Protocol::FEED => (-4),
  107. Protocol::DIASPORA => (-5),
  108. Protocol::MAIL => (-6),
  109. Protocol::FACEBOOK => (-8),
  110. Protocol::LINKEDIN => (-9),
  111. Protocol::XMPP => (-10),
  112. Protocol::MYSPACE => (-11),
  113. Protocol::GPLUS => (-12),
  114. Protocol::PUMPIO => (-13),
  115. Protocol::TWITTER => (-14),
  116. Protocol::DIASPORA2 => (-15),
  117. Protocol::STATUSNET => (-16),
  118. Protocol::NEWS => (-18),
  119. Protocol::ICALENDAR => (-19),
  120. Protocol::PNUT => (-20),
  121. Protocol::PHANTOM => (-127),
  122. ];
  123. /**
  124. * Maximum number of "people who like (or don't like) this" that we will list by name
  125. */
  126. define('MAX_LIKERS', 75);
  127. /**
  128. * @name Notify
  129. *
  130. * Email notification options
  131. * @{
  132. */
  133. define('NOTIFY_INTRO', 1);
  134. define('NOTIFY_CONFIRM', 2);
  135. define('NOTIFY_WALL', 4);
  136. define('NOTIFY_COMMENT', 8);
  137. define('NOTIFY_MAIL', 16);
  138. define('NOTIFY_SUGGEST', 32);
  139. define('NOTIFY_PROFILE', 64);
  140. define('NOTIFY_TAGSELF', 128);
  141. define('NOTIFY_TAGSHARE', 256);
  142. define('NOTIFY_POKE', 512);
  143. define('NOTIFY_SHARE', 1024);
  144. define('SYSTEM_EMAIL', 16384);
  145. define('NOTIFY_SYSTEM', 32768);
  146. /* @}*/
  147. /** @deprecated since 2019.03, use Term::UNKNOWN instead */
  148. define('TERM_UNKNOWN', Term::UNKNOWN);
  149. /** @deprecated since 2019.03, use Term::HASHTAG instead */
  150. define('TERM_HASHTAG', Term::HASHTAG);
  151. /** @deprecated since 2019.03, use Term::MENTION instead */
  152. define('TERM_MENTION', Term::MENTION);
  153. /** @deprecated since 2019.03, use Term::CATEGORY instead */
  154. define('TERM_CATEGORY', Term::CATEGORY);
  155. /** @deprecated since 2019.03, use Term::PCATEGORY instead */
  156. define('TERM_PCATEGORY', Term::PCATEGORY);
  157. /** @deprecated since 2019.03, use Term::FILE instead */
  158. define('TERM_FILE', Term::FILE);
  159. /** @deprecated since 2019.03, use Term::SAVEDSEARCH instead */
  160. define('TERM_SAVEDSEARCH', Term::SAVEDSEARCH);
  161. /** @deprecated since 2019.03, use Term::CONVERSATION instead */
  162. define('TERM_CONVERSATION', Term::CONVERSATION);
  163. /** @deprecated since 2019.03, use Term::OBJECT_TYPE_POST instead */
  164. define('TERM_OBJ_POST', Term::OBJECT_TYPE_POST);
  165. /** @deprecated since 2019.03, use Term::OBJECT_TYPE_PHOTO instead */
  166. define('TERM_OBJ_PHOTO', Term::OBJECT_TYPE_PHOTO);
  167. /**
  168. * @name Gravity
  169. *
  170. * Item weight for query ordering
  171. * @{
  172. */
  173. define('GRAVITY_PARENT', 0);
  174. define('GRAVITY_ACTIVITY', 3);
  175. define('GRAVITY_COMMENT', 6);
  176. define('GRAVITY_UNKNOWN', 9);
  177. /* @}*/
  178. /**
  179. * @name Priority
  180. *
  181. * Process priority for the worker
  182. * @{
  183. */
  184. define('PRIORITY_UNDEFINED', 0);
  185. define('PRIORITY_CRITICAL', 10);
  186. define('PRIORITY_HIGH', 20);
  187. define('PRIORITY_MEDIUM', 30);
  188. define('PRIORITY_LOW', 40);
  189. define('PRIORITY_NEGLIGIBLE', 50);
  190. /* @}*/
  191. /**
  192. * @name Social Relay settings
  193. *
  194. * See here: https://github.com/jaywink/social-relay
  195. * and here: https://wiki.diasporafoundation.org/Relay_servers_for_public_posts
  196. * @{
  197. */
  198. define('SR_SCOPE_NONE', '');
  199. define('SR_SCOPE_ALL', 'all');
  200. define('SR_SCOPE_TAGS', 'tags');
  201. /* @}*/
  202. // Normally this constant is defined - but not if "pcntl" isn't installed
  203. if (!defined("SIGTERM")) {
  204. define("SIGTERM", 15);
  205. }
  206. /**
  207. * Depending on the PHP version this constant does exist - or not.
  208. * See here: http://php.net/manual/en/curl.constants.php#117928
  209. */
  210. if (!defined('CURLE_OPERATION_TIMEDOUT')) {
  211. define('CURLE_OPERATION_TIMEDOUT', CURLE_OPERATION_TIMEOUTED);
  212. }
  213. /**
  214. * @brief Retrieve the App structure
  215. *
  216. * Useful in functions which require it but don't get it passed to them
  217. *
  218. * @deprecated since version 2018.09
  219. * @see BaseObject::getApp()
  220. * @return App
  221. */
  222. function get_app()
  223. {
  224. return BaseObject::getApp();
  225. }
  226. /**
  227. * @brief Used to end the current process, after saving session state.
  228. * @deprecated
  229. */
  230. function killme()
  231. {
  232. exit();
  233. }
  234. /**
  235. * @brief Returns the user id of locally logged in user or false.
  236. *
  237. * @return int|bool user id or false
  238. */
  239. function local_user()
  240. {
  241. if (!empty($_SESSION['authenticated']) && !empty($_SESSION['uid'])) {
  242. return intval($_SESSION['uid']);
  243. }
  244. return false;
  245. }
  246. /**
  247. * @brief Returns the public contact id of logged in user or false.
  248. *
  249. * @return int|bool public contact id or false
  250. */
  251. function public_contact()
  252. {
  253. static $public_contact_id = false;
  254. if (!$public_contact_id && !empty($_SESSION['authenticated'])) {
  255. if (!empty($_SESSION['my_address'])) {
  256. // Local user
  257. $public_contact_id = intval(Contact::getIdForURL($_SESSION['my_address'], 0, true));
  258. } elseif (!empty($_SESSION['visitor_home'])) {
  259. // Remote user
  260. $public_contact_id = intval(Contact::getIdForURL($_SESSION['visitor_home'], 0, true));
  261. }
  262. } elseif (empty($_SESSION['authenticated'])) {
  263. $public_contact_id = false;
  264. }
  265. return $public_contact_id;
  266. }
  267. /**
  268. * @brief Returns contact id of authenticated site visitor or false
  269. *
  270. * @return int|bool visitor_id or false
  271. */
  272. function remote_user()
  273. {
  274. if (empty($_SESSION['authenticated'])) {
  275. return false;
  276. }
  277. if (!empty($_SESSION['visitor_id'])) {
  278. return intval($_SESSION['visitor_id']);
  279. }
  280. return false;
  281. }
  282. /**
  283. * @brief Show an error message to user.
  284. *
  285. * This function save text in session, to be shown to the user at next page load
  286. *
  287. * @param string $s - Text of notice
  288. */
  289. function notice($s)
  290. {
  291. if (empty($_SESSION)) {
  292. return;
  293. }
  294. $a = \get_app();
  295. if (empty($_SESSION['sysmsg'])) {
  296. $_SESSION['sysmsg'] = [];
  297. }
  298. if ($a->interactive) {
  299. $_SESSION['sysmsg'][] = $s;
  300. }
  301. }
  302. /**
  303. * @brief Show an info message to user.
  304. *
  305. * This function save text in session, to be shown to the user at next page load
  306. *
  307. * @param string $s - Text of notice
  308. */
  309. function info($s)
  310. {
  311. $a = \get_app();
  312. if (local_user() && PConfig::get(local_user(), 'system', 'ignore_info')) {
  313. return;
  314. }
  315. if (empty($_SESSION['sysmsg_info'])) {
  316. $_SESSION['sysmsg_info'] = [];
  317. }
  318. if ($a->interactive) {
  319. $_SESSION['sysmsg_info'][] = $s;
  320. }
  321. }
  322. function feed_birthday($uid, $tz)
  323. {
  324. /**
  325. * Determine the next birthday, but only if the birthday is published
  326. * in the default profile. We _could_ also look for a private profile that the
  327. * recipient can see, but somebody could get mad at us if they start getting
  328. * public birthday greetings when they haven't made this info public.
  329. *
  330. * Assuming we are able to publish this info, we are then going to convert
  331. * the start time from the owner's timezone to UTC.
  332. *
  333. * This will potentially solve the problem found with some social networks
  334. * where birthdays are converted to the viewer's timezone and salutations from
  335. * elsewhere in the world show up on the wrong day. We will convert it to the
  336. * viewer's timezone also, but first we are going to convert it from the birthday
  337. * person's timezone to GMT - so the viewer may find the birthday starting at
  338. * 6:00PM the day before, but that will correspond to midnight to the birthday person.
  339. */
  340. $birthday = '';
  341. if (!strlen($tz)) {
  342. $tz = 'UTC';
  343. }
  344. $profile = DBA::selectFirst('profile', ['dob'], ['is-default' => true, 'uid' => $uid]);
  345. if (DBA::isResult($profile)) {
  346. $tmp_dob = substr($profile['dob'], 5);
  347. if (intval($tmp_dob)) {
  348. $y = DateTimeFormat::timezoneNow($tz, 'Y');
  349. $bd = $y . '-' . $tmp_dob . ' 00:00';
  350. $t_dob = strtotime($bd);
  351. $now = strtotime(DateTimeFormat::timezoneNow($tz));
  352. if ($t_dob < $now) {
  353. $bd = $y + 1 . '-' . $tmp_dob . ' 00:00';
  354. }
  355. $birthday = DateTimeFormat::convert($bd, 'UTC', $tz, DateTimeFormat::ATOM);
  356. }
  357. }
  358. return $birthday;
  359. }
  360. /**
  361. * @brief Check if current user has admin role.
  362. *
  363. * @return bool true if user is an admin
  364. */
  365. function is_site_admin()
  366. {
  367. $a = \get_app();
  368. $admin_email = Config::get('config', 'admin_email');
  369. $adminlist = explode(',', str_replace(' ', '', $admin_email));
  370. return local_user() && $admin_email && in_array($a->user['email'] ?? '', $adminlist);
  371. }
  372. function explode_querystring($query)
  373. {
  374. $arg_st = strpos($query, '?');
  375. if ($arg_st !== false) {
  376. $base = substr($query, 0, $arg_st);
  377. $arg_st += 1;
  378. } else {
  379. $base = '';
  380. $arg_st = 0;
  381. }
  382. $args = explode('&', substr($query, $arg_st));
  383. foreach ($args as $k => $arg) {
  384. /// @TODO really compare type-safe here?
  385. if ($arg === '') {
  386. unset($args[$k]);
  387. }
  388. }
  389. $args = array_values($args);
  390. if (!$base) {
  391. $base = $args[0];
  392. unset($args[0]);
  393. $args = array_values($args);
  394. }
  395. return [
  396. 'base' => $base,
  397. 'args' => $args,
  398. ];
  399. }
  400. /**
  401. * Returns the complete URL of the current page, e.g.: http(s)://something.com/network
  402. *
  403. * Taken from http://webcheatsheet.com/php/get_current_page_url.php
  404. */
  405. function curPageURL()
  406. {
  407. $pageURL = 'http';
  408. if (!empty($_SERVER["HTTPS"]) && ($_SERVER["HTTPS"] == "on")) {
  409. $pageURL .= "s";
  410. }
  411. $pageURL .= "://";
  412. if ($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443") {
  413. $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
  414. } else {
  415. $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
  416. }
  417. return $pageURL;
  418. }
  419. function get_server()
  420. {
  421. $server = Config::get("system", "directory");
  422. if ($server == "") {
  423. $server = "https://dir.friendica.social";
  424. }
  425. return $server;
  426. }
  427. function get_temppath()
  428. {
  429. $a = \get_app();
  430. $temppath = Config::get("system", "temppath");
  431. if (($temppath != "") && System::isDirectoryUsable($temppath)) {
  432. // We have a temp path and it is usable
  433. return BasePath::getRealPath($temppath);
  434. }
  435. // We don't have a working preconfigured temp path, so we take the system path.
  436. $temppath = sys_get_temp_dir();
  437. // Check if it is usable
  438. if (($temppath != "") && System::isDirectoryUsable($temppath)) {
  439. // Always store the real path, not the path through symlinks
  440. $temppath = BasePath::getRealPath($temppath);
  441. // To avoid any interferences with other systems we create our own directory
  442. $new_temppath = $temppath . "/" . $a->getHostName();
  443. if (!is_dir($new_temppath)) {
  444. /// @TODO There is a mkdir()+chmod() upwards, maybe generalize this (+ configurable) into a function/method?
  445. mkdir($new_temppath);
  446. }
  447. if (System::isDirectoryUsable($new_temppath)) {
  448. // The new path is usable, we are happy
  449. Config::set("system", "temppath", $new_temppath);
  450. return $new_temppath;
  451. } else {
  452. // We can't create a subdirectory, strange.
  453. // But the directory seems to work, so we use it but don't store it.
  454. return $temppath;
  455. }
  456. }
  457. // Reaching this point means that the operating system is configured badly.
  458. return '';
  459. }
  460. function get_cachefile($file, $writemode = true)
  461. {
  462. $cache = get_itemcachepath();
  463. if ((!$cache) || (!is_dir($cache))) {
  464. return "";
  465. }
  466. $subfolder = $cache . "/" . substr($file, 0, 2);
  467. $cachepath = $subfolder . "/" . $file;
  468. if ($writemode) {
  469. if (!is_dir($subfolder)) {
  470. mkdir($subfolder);
  471. chmod($subfolder, 0777);
  472. }
  473. }
  474. return $cachepath;
  475. }
  476. function clear_cache($basepath = "", $path = "")
  477. {
  478. if ($path == "") {
  479. $basepath = get_itemcachepath();
  480. $path = $basepath;
  481. }
  482. if (($path == "") || (!is_dir($path))) {
  483. return;
  484. }
  485. if (substr(realpath($path), 0, strlen($basepath)) != $basepath) {
  486. return;
  487. }
  488. $cachetime = (int) Config::get('system', 'itemcache_duration');
  489. if ($cachetime == 0) {
  490. $cachetime = 86400;
  491. }
  492. if (is_writable($path)) {
  493. if ($dh = opendir($path)) {
  494. while (($file = readdir($dh)) !== false) {
  495. $fullpath = $path . "/" . $file;
  496. if ((filetype($fullpath) == "dir") && ($file != ".") && ($file != "..")) {
  497. clear_cache($basepath, $fullpath);
  498. }
  499. if ((filetype($fullpath) == "file") && (filectime($fullpath) < (time() - $cachetime))) {
  500. unlink($fullpath);
  501. }
  502. }
  503. closedir($dh);
  504. }
  505. }
  506. }
  507. function get_itemcachepath()
  508. {
  509. // Checking, if the cache is deactivated
  510. $cachetime = (int) Config::get('system', 'itemcache_duration');
  511. if ($cachetime < 0) {
  512. return "";
  513. }
  514. $itemcache = Config::get('system', 'itemcache');
  515. if (($itemcache != "") && System::isDirectoryUsable($itemcache)) {
  516. return BasePath::getRealPath($itemcache);
  517. }
  518. $temppath = get_temppath();
  519. if ($temppath != "") {
  520. $itemcache = $temppath . "/itemcache";
  521. if (!file_exists($itemcache) && !is_dir($itemcache)) {
  522. mkdir($itemcache);
  523. }
  524. if (System::isDirectoryUsable($itemcache)) {
  525. Config::set("system", "itemcache", $itemcache);
  526. return $itemcache;
  527. }
  528. }
  529. return "";
  530. }
  531. /**
  532. * @brief Returns the path where spool files are stored
  533. *
  534. * @return string Spool path
  535. */
  536. function get_spoolpath()
  537. {
  538. $spoolpath = Config::get('system', 'spoolpath');
  539. if (($spoolpath != "") && System::isDirectoryUsable($spoolpath)) {
  540. // We have a spool path and it is usable
  541. return $spoolpath;
  542. }
  543. // We don't have a working preconfigured spool path, so we take the temp path.
  544. $temppath = get_temppath();
  545. if ($temppath != "") {
  546. // To avoid any interferences with other systems we create our own directory
  547. $spoolpath = $temppath . "/spool";
  548. if (!is_dir($spoolpath)) {
  549. mkdir($spoolpath);
  550. }
  551. if (System::isDirectoryUsable($spoolpath)) {
  552. // The new path is usable, we are happy
  553. Config::set("system", "spoolpath", $spoolpath);
  554. return $spoolpath;
  555. } else {
  556. // We can't create a subdirectory, strange.
  557. // But the directory seems to work, so we use it but don't store it.
  558. return $temppath;
  559. }
  560. }
  561. // Reaching this point means that the operating system is configured badly.
  562. return "";
  563. }
  564. if (!function_exists('exif_imagetype')) {
  565. function exif_imagetype($file)
  566. {
  567. $size = getimagesize($file);
  568. return $size[2];
  569. }
  570. }
  571. function validate_include(&$file)
  572. {
  573. $orig_file = $file;
  574. $file = realpath($file);
  575. if (strpos($file, getcwd()) !== 0) {
  576. return false;
  577. }
  578. $file = str_replace(getcwd() . "/", "", $file, $count);
  579. if ($count != 1) {
  580. return false;
  581. }
  582. if ($orig_file !== $file) {
  583. return false;
  584. }
  585. $valid = false;
  586. if (strpos($file, "include/") === 0) {
  587. $valid = true;
  588. }
  589. if (strpos($file, "addon/") === 0) {
  590. $valid = true;
  591. }
  592. // Simply return flag
  593. return $valid;
  594. }
  595. /**
  596. * PHP 5 compatible dirname() with count parameter
  597. *
  598. * @see http://php.net/manual/en/function.dirname.php#113193
  599. *
  600. * @deprecated with PHP 7
  601. * @param string $path
  602. * @param int $levels
  603. * @return string
  604. */
  605. function rdirname($path, $levels = 1)
  606. {
  607. if ($levels > 1) {
  608. return dirname(rdirname($path, --$levels));
  609. } else {
  610. return dirname($path);
  611. }
  612. }