Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
Create CompositeShare object - Change json output format to handle 1 …
Browse files Browse the repository at this point in the history
…or N links.
  • Loading branch information
cdujeu committed Feb 25, 2016
1 parent d051f92 commit 80e6f1f
Show file tree
Hide file tree
Showing 6 changed files with 586 additions and 202 deletions.
143 changes: 143 additions & 0 deletions core/src/plugins/action.share/class.CompositeShare.php
@@ -0,0 +1,143 @@
<?php
/*
* Copyright 2007-2015 Abstrium <contact (at) pydio.com>
* This file is part of Pydio.
*
* Pydio is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio 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 Pydio. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <http://pyd.io/>.
*/

defined('AJXP_EXEC') or die('Access not allowed');


class CompositeShare
{
/**
* @var AJXP_Node
*/
protected $node;

/**
* @var string
*/
protected $repositoryId;

/**
* @var Repository
*/
protected $repository;

/**
* @var ShareLink[]
*/
protected $shareLinks = array();

public function __construct($node, $repositoryId)
{
$this->node = $node;
$this->repositoryId = $repositoryId;
}

/**
* @return Repository
*/
public function getRepository(){
if(!isSet($this->repository)){
$this->repository = ConfService::getRepositoryById($this->repositoryId);
}
return $this->repository;
}

/**
* @param ShareLink $link
*/
public function addLink($link){
$this->shareLinks[] = $link;
}

/**
* @return string
*/
public function getRepositoryId(){
return $this->repositoryId;
}

/**
* @return string
*/
public function getOwner(){
$repo = $this->getRepository();
return $repo->getOwner();
}

/**
* @return string
*/
public function getVisibilityScope(){
$repo = $this->getRepository();
if($repo !== null && isSet($repo->options["SHARE_ACCESS"]) && $repo->options["SHARE_ACCESS"] == "public"){
return "public";
}else{
return "private";
}
}

/**
* @param MetaWatchRegister|false $watcher
* @param ShareRightsManager $rightsManager
* @param PublicAccessManager $publicAccessManager
* @param array $messages
* @return array|false
*/
public function toJson($watcher, $rightsManager, $publicAccessManager, $messages){

$repoRootNode = new AJXP_Node("pydio://".$this->getRepositoryId()."/");
$elementWatch = false;
if ($watcher != false) {
$elementWatch = $watcher->hasWatchOnNode(
$repoRootNode,
AuthService::getLoggedUser()->getId(),
MetaWatchRegister::$META_WATCH_NAMESPACE
);
}
$sharedEntries = $rightsManager->computeSharedRepositoryAccessRights($this->getRepositoryId(), true, $repoRootNode);
if(empty($sharedEntries)){
return false;
}
$cFilter = $this->getRepository()->getContentFilter();
if(!empty($cFilter)){
$cFilter = $cFilter->toArray();
}
$jsonData = array(
"repositoryId" => $this->getRepositoryId(),
"users_number" => AuthService::countUsersForRepository($this->getRepositoryId()),
"label" => $this->getRepository()->getDisplay(),
"description" => $this->getRepository()->getDescription(),
"entries" => $sharedEntries,
"element_watch" => $elementWatch,
"repository_url"=> AJXP_Utils::getWorkspaceShortcutURL($this->getRepository())."/",
"content_filter"=> $cFilter,
"share_owner" => $this->getOwner(),
"share_scope" => $this->getVisibilityScope()
);
$jsonData["links"] = array();
foreach ($this->shareLinks as $shareLink) {
$jsonData["links"][$shareLink->getHash()] = $shareLink->getJsonData($publicAccessManager, $messages);
}
return $jsonData;

}

}
144 changes: 82 additions & 62 deletions core/src/plugins/action.share/class.ShareCenter.php
Expand Up @@ -20,6 +20,7 @@
*/

defined('AJXP_EXEC') or die( 'Access not allowed');
require_once("class.CompositeShare.php");

/**
* @package AjaXplorer_Plugins
Expand Down Expand Up @@ -542,11 +543,13 @@ public function switchAction($action, $httpVars, $fileVars)
header("Content-Type: text/plain");
print($plainResult);
}else{
$this->switchAction(
"load_shared_element_data",
array("file" => $ajxpNode->getPath(), "merged" => "true"),
array()
);
$compositeShare = $this->getShareStore()->getMetaManager()->getCompositeShareForNode($ajxpNode);
header("Content-type:application/json");
if(!empty($compositeShare)){
echo json_encode($this->compositeShareToJson($compositeShare));
}else{
echo json_encode(array());
}
}
// as the result can be quite small (e.g error code), make sure it's output in case of OB active.
flush();
Expand Down Expand Up @@ -617,57 +620,30 @@ public function switchAction($action, $httpVars, $fileVars)
case "load_shared_element_data":

$node = null;
if(isSet($httpVars["hash"])){
$t = "minisite";
if(isSet($httpVars["element_type"]) && $httpVars["element_type"] == "file") $t = "file";
$parsedMeta = array($httpVars["hash"] => array("type" => $t));
if(isSet($httpVars["hash"]) && $httpVars["element_type"] == "file"){

// LEGACY LINKS
$parsedMeta = array($httpVars["hash"] => array("type" => "file"));
$jsonData = array();
foreach($parsedMeta as $shareId => $shareMeta){
$jsonData[] = $this->shareToJson($shareId, $shareMeta, $node);
}
header("Content-type:application/json");
echo json_encode($jsonData);

}else{
$file = AJXP_Utils::decodeSecureMagic($httpVars["file"]);
$node = new AJXP_Node($this->urlBase.$file);
$parsedMeta = array();
$this->getShareStore()->getMetaManager()->getSharesFromMeta($node, $parsedMeta, true);
}

$flattenJson = false;
$jsonData = array();
$ocsMeta = null;
foreach($parsedMeta as $shareId => $shareMeta){

if($shareMeta["type"] == "ocs_remote"){
$fullOcsMeta = $this->shareToJson($shareId, array("type"=>"minisite"), $node);
if(isSet($fullOcsMeta["minisite"])){
$ocsMeta = $fullOcsMeta["minisite"];
$ocsStore = new Pydio\OCS\Model\SQLStore();
$existingInvitations = $ocsStore->invitationsForLink($ocsMeta["hash"]);
$ocsMeta["invitations"] = array_values($existingInvitations);
}
$compositeShare = $this->getShareStore()->getMetaManager()->getCompositeShareForNode($node);
header("Content-type:application/json");
if(!empty($compositeShare)){
$json = $this->compositeShareToJson($compositeShare, $node);
echo json_encode($json);
}else{
$jsonData[] = $this->shareToJson($shareId, $shareMeta, $node);
if($shareMeta["type"] != "file"){
$flattenJson = true;
}
echo json_encode(array());
}

}
header("Content-type:application/json");
if(isSet($httpVars['merged']) && count($jsonData)){
// Send minisite ( = more complete ) if any, or send repository
foreach($jsonData as $data){
if(isSet($data['minisite'])){
$minisiteData = $data;
break;
}
}
if(isSet($minisiteData)) $jsonData = $minisiteData;
else $jsonData = $jsonData[0];

}else if($flattenJson && count($jsonData)) {
$jsonData = $jsonData[0];
}
if(!empty($ocsMeta)){
$jsonData["ocs"] = $ocsMeta;
}
echo json_encode($jsonData);

break;

Expand Down Expand Up @@ -1480,6 +1456,14 @@ public function shareNode($httpVars, &$update){
// PUBLIC LINK
if(isSet($httpVars["enable_public_link"])){

$minisiteData = $httpVars;
if(isSet($httpVars["minisite"])){
$decoded = json_decode($httpVars["minisite"], true);
if(is_array($decoded)){
$minisiteData = $decoded;
}
}

// PREPARE HIDDEN USER DATA
if(isSet($httpVars["hash"])){
$shareObject = $this->getShareStore()->loadShareObject($httpVars["hash"]);
Expand Down Expand Up @@ -1729,6 +1713,47 @@ public function listSharesAsNodes($rootPath, $currentUser = true, $parentReposit

}

/**
* @param CompositeShare $compositeShare
* @return array
*/
public function compositeShareToJson($compositeShare){

$repoId = $compositeShare->getRepositoryId();
$repo = $compositeShare->getRepository();
$messages = ConfService::getMessages();

$notExistsData = array(
"error" => true,
"repositoryId" => $repoId,
"users_number" => 0,
"label" => "Error - Cannot find shared data",
"description" => "Cannot find repository",
"entries" => array(),
"element_watch" => false,
"repository_url"=> ""
);

if($repoId == null || $repo == null){
//CLEAR AND ASSOCIATED LINKS HERE ?
//$this->getShareStore()->getMetaManager()->removeShareFromMeta($node, $shareId);
return $notExistsData;
}
try{
$this->getShareStore()->testUserCanEditShare($compositeShare->getOwner(), array("SHARE_ACCESS" => $compositeShare->getVisibilityScope()));
}catch(Exception $e){
$notExistsData["label"] = $e->getMessage();
return $notExistsData;
}

$jsonData = $compositeShare->toJson($this->watcher, $this->getRightsManager(), $this->getPublicAccessManager(), $messages);
if($jsonData === false){
return $notExistsData;
}
return $jsonData;

}

/**
* @param String $shareId
* @param array $shareMeta
Expand Down Expand Up @@ -1763,19 +1788,9 @@ public function shareToJson($shareId, $shareMeta, $node = null){
}

$minisite = ($shareMeta["type"] == "minisite");
$minisiteIsPublic = false;
$dlDisabled = false;
$minisiteLink = '';
if ($minisite) {
$storedData = $this->getShareStore()->loadShare($shareId);
$repoId = $storedData["REPOSITORY"];
$minisiteIsPublic = isSet($storedData["PRELOG_USER"]);
$dlDisabled = isSet($storedData["DOWNLOAD_DISABLED"]) && $storedData["DOWNLOAD_DISABLED"] === true;
if (isSet($shareMeta["short_form_url"])) {
$minisiteLink = $shareMeta["short_form_url"];
} else {
$minisiteLink = $this->getPublicAccessManager()->buildPublicLink($shareId);
}
$shareLink = $this->getShareStore()->loadShareObject($shareId);
$repoId = $shareLink->getRepositoryId();
}
$notExistsData = array(
"error" => true,
Expand Down Expand Up @@ -1834,6 +1849,11 @@ public function shareToJson($shareId, $shareMeta, $node = null){
"share_owner" => $repo->getOwner(),
"share_scope" => (isSet($repo->options["SHARE_ACCESS"]) ? $repo->options["SHARE_ACCESS"] : "private")
);
if($minisite && isSet($shareLink)){
$shareLink->setAdditionalMeta($shareMeta);
$jsonData["minisite"] = $shareLink->getJsonData($this->getPublicAccessManager(), $messages);
}
/*
if ($minisite && isSet($storedData)) {
if(!empty($storedData["DOWNLOAD_LIMIT"]) && !$dlDisabled){
$jsonData["download_counter"] = $this->getShareStore()->getCurrentDownloadCounter($shareId);
Expand Down Expand Up @@ -1865,7 +1885,7 @@ public function shareToJson($shareId, $shareMeta, $node = null){
if(isSet($storedData[$key])) $jsonData[$key] = $storedData[$key];
}
}
}*/

}

Expand Down

0 comments on commit 80e6f1f

Please sign in to comment.