/usr/share/horde/gollem/lib/Gollem.php is in php-horde-gollem 3.0.10-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 | <?php
/**
* Gollem base library.
*
* Copyright 1999-2016 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.horde.org/licenses/gpl.
*
* @author Max Kalika <max@horde.org>
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/gpl GPL
* @package Gollem
*/
class Gollem
{
/* Sort constants. */
const SORT_TYPE = 0;
const SORT_NAME = 1;
const SORT_DATE = 2;
const SORT_SIZE = 3;
const SORT_ASCEND = 0;
const SORT_DESCEND = 1;
/**
* Configuration hash for the current backend.
*
* @var array
*/
static public $backend;
/**
* Cache for display columns.
*
* @var array
*/
static protected $_columns;
/**
* Changes the current directory of the Gollem session to the supplied
* value.
*
* @param string $dir Directory name.
*
* @throws Gollem_Exception
*/
static public function setDir($dir)
{
$dir = Horde_Util::realPath($dir);
if (!self::verifyDir($dir) ||
!self::checkPermissions('directory', Horde_Perms::READ, $dir)) {
throw new Gollem_Exception(sprintf(_("Access denied to folder \"%s\"."), $dir));
}
self::$backend['dir'] = $dir;
self::_setLabel();
}
/**
* Changes the current directory of the Gollem session based on the
* 'dir' form field.
*
* @throws Gollem_Exception
*/
static public function changeDir()
{
$dir = Horde_Util::getFormData('dir');
if (is_null($dir)) {
self::_setLabel();
} else {
if (strpos($dir, '/') !== 0) {
$dir = self::$backend['dir'] . '/' . $dir;
}
self::setDir($dir);
}
}
/**
* Set the lable to use for the current page.
*/
static protected function _setLabel()
{
self::$backend['label'] = self::getDisplayPath(self::$backend['dir']);
if (empty(self::$backend['label'])) {
self::$backend['label'] = '/';
}
}
/**
* Internal helper to sort directories first if pref set.
*/
static protected function _sortDirs($a, $b)
{
/* Sort symlinks to dirs as dirs */
$dira = ($a['type'] === '**dir') ||
(($a['type'] === '**sym') && ($a['linktype'] === '**dir'));
$dirb = ($b['type'] === '**dir') ||
(($b['type'] === '**sym') && ($b['linktype'] === '**dir'));
if ($GLOBALS['prefs']->getValue('sortdirsfirst')) {
if ($dira && !$dirb) {
return -1;
} elseif (!$dira && $dirb) {
return 1;
}
}
return 0;
}
/**
* Internal sorting function for 'date'.
*/
static public function sortDate($a, $b)
{
$dirs = self::_sortDirs($a, $b);
if ($dirs) {
return $dirs;
}
if ($a['date'] > $b['date']) {
return $GLOBALS['prefs']->getValue('sortdir') ? -1 : 1;
} elseif ($a['date'] === $b['date']) {
return self::sortName($a, $b);
} else {
return $GLOBALS['prefs']->getValue('sortdir') ? 1 : -1;
}
}
/**
* Internal sorting function for 'size'.
*/
static public function sortSize($a, $b)
{
$dirs = self::_sortDirs($a, $b);
if ($dirs) {
return $dirs;
}
if ($a['size'] > $b['size']) {
return $GLOBALS['prefs']->getValue('sortdir') ? -1 : 1;
} elseif ($a['size'] === $b['size']) {
return 0;
} else {
return $GLOBALS['prefs']->getValue('sortdir') ? 1 : -1;
}
}
/**
* Internal sorting function for 'type'.
*/
static public function sortType($a, $b)
{
$dirs = self::_sortDirs($a, $b);
if ($dirs) {
return $dirs;
}
if ($a['type'] === $b['type']) {
return self::sortName($a, $b);
} elseif ($a['type'] === '**dir') {
return $GLOBALS['prefs']->getValue('sortdir') ? 1 : -1;
} elseif ($b['type'] === '**dir') {
return $GLOBALS['prefs']->getValue('sortdir') ? -1 : 1;
} else {
$res = strcasecmp($a['type'], $b['type']);
return $GLOBALS['prefs']->getValue('sortdir') ? ($res * -1) : $res;
}
}
/**
* Internal sorting function for 'name'.
*/
static public function sortName($a, $b)
{
$dirs = self::_sortDirs($a, $b);
if ($dirs) {
return $dirs;
}
$res = strcasecmp($a['name'], $b['name']);
return $GLOBALS['prefs']->getValue('sortdir') ? ($res * -1) : $res;
}
/**
* List the current folder.
*
* @param string $dir The directory name.
*
* @return array The sorted list of files.
* @throws Gollem_Exception
*/
static public function listFolder($dir)
{
global $conf;
if (!empty($conf['foldercache']['use_cache']) &&
!empty($conf['cache']['driver']) &&
($conf['cache']['driver'] != 'none')) {
$key = self::_getCacheID($dir);
$cache = $GLOBALS['injector']->getInstance('Horde_Cache');
$res = $cache->get($key, $conf['foldercache']['lifetime']);
if ($res !== false) {
$res = Horde_Serialize::unserialize($res, Horde_Serialize::BASIC);
if (is_array($res)) {
return $res;
}
}
}
try {
$files = $GLOBALS['injector']
->getInstance('Gollem_Vfs')
->listFolder($dir,
isset(self::$backend['filter']) ? self::$backend['filter'] : null,
$GLOBALS['prefs']->getValue('show_dotfiles'));
} catch (Horde_Vfs_Exception $e) {
throw new Gollem_Exception($e);
}
$sortcols = array(
self::SORT_TYPE => 'sortType',
self::SORT_NAME => 'sortName',
self::SORT_DATE => 'sortDate',
self::SORT_SIZE => 'sortSize',
);
usort($files, array('Gollem', $sortcols[$GLOBALS['prefs']->getValue('sortby')]));
if (isset($cache)) {
$cache->set($key, Horde_Serialize::serialize($files, Horde_Serialize::BASIC), $conf['foldercache']['lifetime']);
}
return $files;
}
/**
* Generate the Cache ID for a directory.
*
* @param string $dir The directory name.
*/
static protected function _getCacheID($dir)
{
return implode('|', array($GLOBALS['registry']->getAuth(),
$GLOBALS['session']->get('gollem', 'backend_key'),
$GLOBALS['prefs']->getValue('show_dotfiles'),
$GLOBALS['prefs']->getValue('sortdirsfirst'),
$GLOBALS['prefs']->getValue('sortby'),
$GLOBALS['prefs']->getValue('sortdir'),
$dir));
}
/**
* Expire a folder cache entry.
*
* @param string $dir The directory name.
*/
static public function expireCache($dir)
{
global $conf;
if (!empty($conf['foldercache']['use_cache']) &&
!empty($conf['cache']['driver']) &&
($conf['cache']['driver'] != 'none')) {
$cache = $GLOBALS['injector']->getInstance('Horde_Cache');
$cache->expire(self::_getCacheID($dir));
}
}
/**
* Generate correct subdirectory links.
*
* @param string $base The base directory.
* @param string $dir The directory string.
*
* @return string The correct subdirectoy string.
*/
static public function subdirectory($base, $dir)
{
if (empty($base)) {
return $dir;
}
if (substr($base, -1) == '/') {
return $base . $dir;
}
return $base . '/' . $dir;
}
/**
* Create a folder using the current Gollem session settings.
*
* @param string $dir The directory path.
* @param string $name The folder to create.
* @param Horde_Vfs_Base $gollem_vfs A VFS instance to use.
*
* @throws Gollem_Exception
*/
static public function createFolder($dir, $name, $gollem_vfs = null)
{
$totalpath = Horde_Util::realPath($dir . '/' . $name);
if (!self::verifyDir($totalpath)) {
throw new Gollem_Exception(sprintf(_("Access denied to folder \"%s\"."), $totalpath));
}
/* The $name parameter may contain additional directories so we
* need to pass autocreatePath everything but the base filename. */
$pos = strrpos($totalpath, '/');
$dir = substr($totalpath, 0, $pos);
$name = substr($totalpath, $pos + 1);
if (!$gollem_vfs) {
$gollem_vfs = $GLOBALS['injector']->getInstance('Gollem_Vfs');
}
try {
$gollem_vfs->autocreatePath($dir);
$gollem_vfs->createFolder($dir, $name);
if (!empty(self::$backend['params']['permissions'])) {
$gollem_vfs->changePermissions($dir, $name, self::$backend['params']['permissions']);
}
} catch (Horde_Vfs_Exception $e) {
throw new Gollem_Exception($e);
}
}
/**
* Rename files using the current Gollem session settings.
*
* @param string $oldDir Old directory name.
* @param string $old Old file name.
* @param string $newDir New directory name.
* @param string $old New file name.
*
* @throws Gollem_Exception
*/
static public function renameItem($oldDir, $old, $newDir, $new)
{
try {
$GLOBALS['injector']
->getInstance('Gollem_Vfs')
->rename($oldDir, $old, $newDir, $new);
} catch (Horde_Vfs_Exception $e) {
throw new Gollem_Exception($e);
}
$shares = $GLOBALS['injector']->getInstance('Gollem_Shares');
$backend = $GLOBALS['session']->get('gollem', 'backend_key');
try {
$share = $shares->getShare($backend . '|' . $oldDir . '/' . $old);
$shares->renameShare($share, $backend . '|' . $newDir . '/' . $new);
$share->set('name', $new, true);
} catch (Horde_Exception_NotFound $e) {
} catch (Horde_Share_Exception $e) {
throw new Gollem_Exception($e);
}
}
/**
* Delete a folder using the current Gollem session settings.
*
* @param string $dir The subdirectory name.
* @param string $name The folder name to delete.
*
* @throws Gollem_Exception
*/
static public function deleteFolder($dir, $name)
{
if (!self::verifyDir($dir)) {
throw new Gollem_Exception(sprintf(_("Access denied to folder \"%s\"."), $dir));
}
try {
$GLOBALS['injector']
->getInstance('Gollem_Vfs')
->deleteFolder(
$dir,
$name,
$GLOBALS['prefs']->getValue('recursive_deletes') != 'disabled');
} catch (Horde_Vfs_Exception $e) {
throw new Gollem_Exception($e);
}
$shares = $GLOBALS['injector']->getInstance('Gollem_Shares');
try {
$share = $shares->getShare(
$GLOBALS['session']->get('gollem', 'backend_key') . '|'
. $dir . '/' . $name);
$shares->removeShare($share);
} catch (Horde_Exception_NotFound $e) {
} catch (Horde_Share_Exception $e) {
throw new Gollem_Exception($e);
}
}
/**
* Delete a file using the current Gollem session settings.
*
* @param string $dir The directory name.
* @param string $name The filename to delete.
*
* @throws Gollem_Exception
*/
static public function deleteFile($dir, $name)
{
if (!self::verifyDir($dir)) {
throw new Gollem_Exception(sprintf(_("Access denied to folder \"%s\"."), $dir));
}
try {
$GLOBALS['injector']
->getInstance('Gollem_Vfs')
->deleteFile($dir, $name);
} catch (Horde_Vfs_Exception $e) {
throw new Gollem_Exception($e);
}
}
/**
* Change permissions on files using the current Gollem session settings.
*
* @param string $dir The directory name.
* @param string $name The filename to change permissions on.
* @param string $permission The permission mode to set.
*
* @throws Gollem_Exception
*/
static public function changePermissions($dir, $name, $permission)
{
if (!self::verifyDir($dir)) {
throw new Gollem_Exception(sprintf(_("Access denied to folder \"%s\"."), $dir));
}
try {
$GLOBALS['injector']
->getInstance('Gollem_Vfs')
->changePermissions($dir, $name, $permission);
} catch (Horde_Vfs_Exception $e) {
throw new Gollem_Exception($e);
}
}
/**
* Write an uploaded file to the VFS backend.
*
* @param string $dir The directory name.
* @param string $name The filename to create.
* @param string $filename The local file containing the file data.
*
* @thows Gollem_Exception
*/
static public function writeFile($dir, $name, $filename)
{
$gollem_vfs = $GLOBALS['injector']->getInstance('Gollem_Vfs');
try {
$gollem_vfs->write($dir, $name, $filename, true);
if (!empty(self::$backend['params']['permissions'])) {
$gollem_vfs->changePermissions($dir, $name, self::$backend['params']['permissions']);
}
} catch (Horde_Vfs_Exception $e) {
throw new Gollem_Exception($e);
}
}
/**
* Moves a file using the current Gollem session settings.
*
* @param string $backend_f The backend to move the file from.
* @param string $dir The directory name of the original file.
* @param string $name The original filename.
* @param string $backend_t The backend to move the file to.
* @param string $newdir The directory to move the file to.
*
* @throws Gollem_Exception
*/
static public function moveFile($backend_f, $dir, $name, $backend_t,
$newdir)
{
self::_copyFile('move', $backend_f, $dir, $name, $backend_t, $newdir);
}
/**
* Copies a file using the current Gollem session settings.
*
* @param string $backend_f The backend to copy the file from.
* @param string $dir The directory name of the original file.
* @param string $name The original filename.
* @param string $backend_t The backend to copy the file to.
* @param string $newdir The directory to copy the file to.
*
* @throws Gollem_Exception
*/
static public function copyFile($backend_f, $dir, $name, $backend_t,
$newdir)
{
self::_copyFile('copy', $backend_f, $dir, $name, $backend_t, $newdir);
}
/**
* Private function that copies/moves files.
*
* @throws Gollem_Exception
*/
static protected function _copyFile($mode, $backend_f, $dir, $name,
$backend_t, $newdir)
{
$backend_key = $GLOBALS['session']->get('gollem', 'backend_key');
$factory = $GLOBALS['injector']->getInstance('Gollem_Factory_Vfs');
/* If the from/to backends are the same, we can just use the built-in
* VFS functions. */
if ($backend_f == $backend_t) {
$ob = $factory->create($backend_f);
try {
if ($backend_f != $backend_key) {
$ob->checkCredentials();
}
if ($mode == 'copy') {
$ob->copy($dir, $name, $newdir);
} else {
$ob->move($dir, $name, $newdir);
}
} catch (Horde_Vfs_Exception $e) {
throw new Gollem_Exception($e);
}
return;
}
/* Else, get the two VFS objects and copy/move the files. */
$from_be = $factory->create($backend_f);
if ($backend_f != $backend_key) {
try {
$from_be->checkCredentials();
} catch (Horde_Vfs_Exception $e) {
throw new Gollem_Exception($e);
}
}
$to_be = $factory->create($backend_t);
if ($backend_t != $backend_key) {
try {
$to_be->checkCredentials();
} catch (Horde_Vfs_Exception $e) {
throw new Gollem_Exception($e);
}
}
try {
/* Read the source data. */
$data = $from_be->read($dir, $name);
/* Write the target data. */
$to_be->writeData($newdir, $name, $data);
/* If moving, delete the source data. */
if ($mode == 'move') {
$from_be->deleteFile($dir, $name);
}
} catch (Horde_Vfs_Exception $e) {
throw new Gollem_Exception($e);
}
}
/**
* This function verifies whether a given directory is below the root.
*
* @param string $dir The directory to check.
*
* @return boolean True if the directory is below the root.
*/
static public function verifyDir($dir)
{
return Horde_String::substr(Horde_Util::realPath($dir), 0, Horde_String::length(self::$backend['root'])) == self::$backend['root'];
}
/**
* Parses the 'columns' preference.
*
* @return array The list of columns to be displayed.
*/
static public function getColumns($backend)
{
if (!isset(self::$_columns)) {
self::$_columns = array();
$sources = json_decode($GLOBALS['prefs']->getValue('columns'));
foreach ($sources as $source) {
self::$_columns[array_shift($source)] = $source;
}
}
if (empty(self::$_columns[$backend])) {
$info = Gollem_Auth::getBackend($backend);
self::$_columns[$backend] = $info['attributes'];
}
return self::$_columns[$backend];
}
/**
* Checks if a user has the specified permissions on a resource.
*
* @param string $filter What are we checking for. Either 'backend'
* or 'directory'.
* @param integer $permission The permission to check for. One of the
* Horde_Perms constants.
* @param string $resource The resource to check. If empty, check the
* current backend/directory.
*
* @return boolean Returns true if the user has permission.
*/
static public function checkPermissions($filter,
$permission = Horde_Perms::READ,
$resource = null)
{
$userID = $GLOBALS['registry']->getAuth();
switch ($filter) {
case 'backend':
if (is_null($resource)) {
$resource = $GLOBALS['session']->get('gollem', 'backend_key');
}
$backendTag = 'gollem:backends:' . $resource;
$perms = $GLOBALS['injector']->getInstance('Horde_Perms');
return (!$perms->exists($backendTag) ||
$perms->hasPermission($backendTag, $userID, $permission));
case 'directory':
if (empty(self::$backend['shares'])) {
return true;
}
if (is_null($resource)) {
$resource = self::$backend['dir'];
}
if (strpos($resource, self::$backend['home']) === 0) {
return true;
}
$shares = $GLOBALS['injector']->getInstance('Gollem_Shares');
$backend = $GLOBALS['session']->get('gollem', 'backend_key');
$directory = $resource;
while (strlen($directory) && $directory != './' && $directory != '/') {
try {
return $shares->getShare($backend . '|' . $directory)
->hasPermission($userID, $permission);
} catch (Horde_Exception_NotFound $e) {
}
$directory = dirname($directory);
}
/* Intermediate solution until we display shared folders
* independent from the directory tree. Check if there are
* any sub-directories with show permissions and allow
* browsing the directory in this case. */
if ($permission == Horde_Perms::READ ||
$permission == Horde_Perms::SHOW) {
$dirs = $shares->listShares($userID, array('perm' => Horde_Perms::SHOW));
foreach ($dirs as $dir) {
if (strpos($dir->getName(), $backend . '|' . $resource) === 0) {
return true;
}
}
}
break;
}
return false;
}
/**
* Produces a directory link used for navigation.
*
* @param string $currdir The current directory string.
* @param string $url The URL to link to.
*
* @return string The directory navigation string.
*/
static public function directoryNavLink($currdir, $url)
{
$label = array();
$root_dir_name = self::$backend['name'];
if ($currdir == $root_dir_name) {
$label[] = '[' . $root_dir_name . ']';
} else {
$parts = explode('/', $currdir);
$parts_count = count($parts);
$url = new Horde_Url($url);
$label[] = Horde::link($url->add('dir', self::$backend['root']), sprintf(_("Up to %s"), $root_dir_name)) . '[' . $root_dir_name . ']</a>';
for ($i = 1; $i <= $parts_count; ++$i) {
$part = array_slice($parts, 0, $i);
$dir = implode('/', $part);
if ((strstr($dir, self::$backend['root']) !== false) &&
(self::$backend['root'] != $dir)) {
if ($i == $parts_count) {
$label[] = $parts[($i - 1)];
} else {
$label[] = Horde::link($url->add('dir', $dir), sprintf(_("Up to %s"), $dir)) . htmlspecialchars($parts[($i - 1)]) . '</a>';
}
}
}
}
return implode('/', $label);
}
/**
* Generate the display path (the path with any root information stripped
* out).
*
* @param string $path The path to display.
*
* @return string The display path.
*/
static public function getDisplayPath($path)
{
$path = Horde_Util::realPath($path);
if (self::$backend['root'] != '/' &&
strpos($path, self::$backend['root']) === 0) {
$path = substr($path, Horde_String::length(self::$backend['root']));
}
return $path;
}
/**
* Cleans a path presented to Gollem's browse API call.
*
* This will remove:
* - leading '/'
* - leading 'gollem'
* - trailing '/'
* The desired end result is the path including VFS backend.
*
* @param string $path Path as presented to Gollem API.
*
* @return string Cleaned path as described above.
*/
static public function stripAPIPath($path)
{
return trim(preg_replace('|/?gollem|', '', $path), '/');
}
/**
* Convert a Gollem path into a URL encoded string, but keep '/'.
* This allows for proper PATH_INFO path parsing.
* Special care is taken to handle "+" and " ".
*
* @param string $path Path to be urlencode()d.
*
* @return string URL-encoded string with '/' preserved.
*/
static public function pathEncode($path)
{
return str_ireplace(array('%2F', '%2f'), '/', rawurlencode($path));
}
/**
* Take a fully qualified and break off the file or directory name.
* This pair is used for the input to many VFS library functions.
*
* @param string $fullpath Path to be split.
*
* @return array Array of ($path, $name)
*/
static public function getVFSPath($fullpath)
{
// Convert the path into VFS's ($path, $name) convention
$i = strrpos($fullpath, '/');
if ($i !== false) {
$path = substr($fullpath, 0, $i);
$name = substr($fullpath, $i + 1);
} else {
$name = $fullpath;
$path = '';
}
return array($name, $path);
}
}
|