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

Commit

Permalink
Change url.shorten hook to make it more versatile.
Browse files Browse the repository at this point in the history
First implementation of gateway.
  • Loading branch information
cdujeu committed Oct 10, 2016
1 parent a275cea commit 11b0129
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 27 deletions.
21 changes: 2 additions & 19 deletions core/src/plugins/action.share/src/ShareCenter.php
Expand Up @@ -1588,15 +1588,7 @@ public function createSharedMinisite($httpVars, &$update)
// STORE DATA & HASH IN SHARE STORE
$hash = $shareObject->save();
$url = $this->getPublicAccessManager()->buildPublicLink($hash);
$existingShortForm = $shareObject->getShortFormUrl();
if(empty($existingShortForm)){
$shortForm = "";
Controller::applyHook("url.shorten", array($this->currentContext, $url, &$shortForm));
if(!empty($shortForm)){
$shareObject->setShortFormUrl($shortForm);
$shareObject->save();
}
}
Controller::applyHook("url.shorten", array($this->currentContext, &$shareObject, $this->getPublicAccessManager()));

// LOG AND PUBLISH EVENT
$update = isSet($httpVars["repository_id"]);
Expand Down Expand Up @@ -1807,16 +1799,7 @@ public function shareNode(ContextInterface $ctx, $ajxpNode, $httpVars, &$update)
$ocsStore->storeInvitation($invitation);
}
}else{
$url = $this->getPublicAccessManager()->buildPublicLink($shareObject->getHash());
$existingShortForm = $shareObject->getShortFormUrl();
if(empty($existingShortForm)){
$shortForm = "";
Controller::applyHook("url.shorten", array($ctx, $url, &$shortForm));
if(!empty($shortForm)){
$shareObject->setShortFormUrl($shortForm);
$shareObject->save();
}
}
Controller::applyHook("url.shorten", array($this->currentContext, &$shareObject, $this->getPublicAccessManager()));
}

}
Expand Down
98 changes: 98 additions & 0 deletions core/src/plugins/shorten.gateway/FileGateway.php
@@ -0,0 +1,98 @@
<?php
/*
* Copyright 2007-2016 Charles du Jeu - Abstrium SAS <team (at) pyd.io>
* 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 <https://pydio.com>.
*/

namespace Pydio\LinkShortener;

use GuzzleHttp\Exception\RequestException;
use Pydio\Core\Model\ContextInterface;
use GuzzleHttp\Client;

use Pydio\Core\PluginFramework\Plugin;
use Pydio\Core\Services\ApplicationState;
use Pydio\Share\Model\ShareLink;
use Pydio\Share\View\PublicAccessManager;

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

/**
* FileGateway implementation
* @package Pydio\LinkShortener
*/
class FileGateway extends Plugin
{
private $servers = [
"filesend" => "https://filesend.cc",
"yourshare" => "https://yoursha.re",
];

/**
* @param ContextInterface $ctx
* @param ShareLink $shareObject
* @param PublicAccessManager $publicAccessManager
*/
public function processShortenHook(ContextInterface $ctx, &$shareObject, $publicAccessManager){

$server = $this->getContextualOption($ctx, "GATEWAY_SERVER");
$apiKey = $this->getContextualOption($ctx, "API_KEY");
$apiSecret = $this->getContextualOption($ctx, "API_SECRET");
if(empty($server) || empty($apiKey) || empty($apiSecret)){
return;
}
if(!isSet($this->servers[$server])){
$this->logError("FileGateway", "Cannot find valid server for key ".$server);
return;
}

$data = [
"hash" => $shareObject->getHash(),
"base" => ApplicationState::detectServerURL(true),
"main_endpoint" => "proxy.php?hash={hash}",
"dl_endpoint" => "proxy.php?hash={hash}&dl=true&file={path}",
"shorten_type" => "full",
];
$headers['Authorization'] = 'Basic '.base64_encode($apiKey.":".$apiSecret);
$client = new Client([]);
$request = $client->createRequest("POST", $this->servers[$server], [
"timeout" => 5,
"headers" => $headers,
"json" => $data
]);

try{
$response = $client->send($request);
}catch (RequestException $r){
$this->logError("FileGateway", "There was an error while trying to submit a request to the server: ".$r->getMessage());
return;
}
$body = $response->getBody();
$json = json_decode($body, true);
if(!empty($json)){
$newUrl = $json["public_url"];
$shareObject->setShortFormUrl($newUrl);
$shareObject->save();
}else{
$this->logError("FileGateway", "There was an error while trying to decode the server response: ".$body);
}


}

}
31 changes: 31 additions & 0 deletions core/src/plugins/shorten.gateway/i18n/conf/en.php
@@ -0,0 +1,31 @@
<?php
/*
* Copyright 2007-2016 Charles du Jeu - Abstrium SAS <team (at) pyd.io>
* 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 <https://pydio.com>.
*/

$mess=array(
"File Gateway" => "File Gateway",
"Shorten links and serve them proxied by Pydio hosted service" => "Shorten links and serve them proxied by Pydio hosted service",
"Api Key" => "Api Key",
"Api Key as provided in your Pydio.com account" => "Api Key as provided in your Pydio.com account",
"Api Secret" => "Api Secret",
"Api Secret, as provided in your Pydio.com account" => "Api Secret, as provided in your Pydio.com account",
"Gateway Server" => "Gateway Server",
"Choose on which server you want the link to be proxied. You may choose depending on your region." => "Choose on which server you want the link to be proxied. You may choose depending on your region.",
);
31 changes: 31 additions & 0 deletions core/src/plugins/shorten.gateway/i18n/conf/fr.php
@@ -0,0 +1,31 @@
<?php
/*
* Copyright 2007-2016 Charles du Jeu - Abstrium SAS <team (at) pyd.io>
* 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 <https://pydio.com>.
*/

$mess=array(
"File Gateway" => "File Gateway",
"Shorten links and serve them proxied by Pydio hosted service" => "Raccourcir les liens et les servir via le service hébergé de proxy de Pydio.",
"Api Key" => "Clé d'API",
"Api Key as provided in your Pydio.com account" => "La clé d'API disponible dans votre compte Pydio.com",
"Api Secret" => "Clé secrète",
"Api Secret, as provided in your Pydio.com account" => "La clé secrète, disponible dans votre compte Pydio.com",
"Gateway Server" => "Serveur FileGateway",
"Choose on which server you want the link to be proxied. You may choose depending on your region." => "Choisir sur quel serveur vous désirez héberger vos liens, en fonction de votre région.",
);
25 changes: 25 additions & 0 deletions core/src/plugins/shorten.gateway/manifest.xml
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<ajxp_plugin id="shorten.gateway" enabled="false" label="CONF_MESSAGE[File Gateway]" description="CONF_MESSAGE[Shorten links and serve them proxied by Pydio hosted service]" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:../core.ajaxplorer/ajxp_registry.xsd">
<plugin_info>
<plugin_author>Charles du Jeu</plugin_author>
</plugin_info>
<client_settings>
<resources>
<i18n namespace="gateway_shortener" path="plugins/shorten.gateway/i18n" />
</resources>
</client_settings>
<server_settings>
<global_param name="API_KEY" type="string" label="CONF_MESSAGE[Api Key]" description="CONF_MESSAGE[Api Key as provided in your Pydio.com account]"/>
<global_param name="API_SECRET" type="password" label="CONF_MESSAGE[Api Secret]" description="CONF_MESSAGE[Api Secret, as provided in your Pydio.com account]"/>
<global_param name="GATEWAY_SERVER" type="select" choices="filesend|Filesend.cc (EU Region),yourshare|Yoursha.re (US Region)" label="CONF_MESSAGE[Gateway Server]" description="CONF_MESSAGE[Choose on which server you want the link to be proxied. You may choose depending on your region.]"/>
</server_settings>
<registry_contributions>
<hooks>
<serverCallback methodName="processShortenHook" hookName="url.shorten"/>
</hooks>
</registry_contributions>
<class_definition filename="plugins/shorten.gateway/FileGateway.php" classname="Pydio\LinkShortener\FileGateway"/>
<dependencies>
<activePlugin pluginName="action.share"/>
</dependencies>
</ajxp_plugin>
10 changes: 10 additions & 0 deletions core/src/plugins/shorten.gateway/plugin_doc.html
@@ -0,0 +1,10 @@
<p>
This plugin allows you to use the exclusive Pydio hosted service, FileGateway. By shortening AND proxying your public links
to your servers, FileGateway allows to you securely share resources with external users via public links without the risk of
revealing your Pydio server URL.
</p>
<p>
To be able to use this service, please buy a subscription on Pydio.com website and grab the API Keys / API Secret that will be available
in your account to configure the plugin. You can choose on which region you wish your links to be "hosted", either in the US (filesend.cc)
or in Europe (yoursha.re).
</p>
42 changes: 34 additions & 8 deletions core/src/plugins/shorten.multi/MultiShortener.php
@@ -1,15 +1,31 @@
<?php
/*
* Multi shortener plugin for ajaXplorer by FrenandoAloso
* based in bit.ly plugin
*/

* Copyright 2007-2016 Charles du Jeu - Abstrium SAS <team (at) pyd.io>
* 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 <https://pydio.com>.
*/
namespace Pydio\LinkShortener;

use Pydio\Core\Model\ContextInterface;
use Pydio\Core\Utils\FileHelper;

use Pydio\Core\PluginFramework\Plugin;
use Pydio\Share\Model\ShareLink;
use Pydio\Share\View\PublicAccessManager;

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

Expand All @@ -22,11 +38,21 @@ class MultiShortener extends Plugin
{

/**
* @param string $url
* @param string $shorten
* @param ContextInterface $ctx
* @param ShareLink $shareObject
* @param PublicAccessManager $publicAccessManager
*/
public function processShortenHook(ContextInterface $ctx, $url, &$shorten){
$shorten = $this->generateLink($ctx, $url);
public function processShortenHook(ContextInterface $ctx, &$shareObject, $publicAccessManager){

$existingShortForm = $shareObject->getShortFormUrl();
if(empty($existingShortForm)){
$url = $publicAccessManager->buildPublicLink($shareObject->getHash());
$shortForm = $this->generateLink($ctx, $url);
if(!empty($shortForm)){
$shareObject->setShortFormUrl($shortForm);
$shareObject->save();
}
}
}

/**
Expand Down

0 comments on commit 11b0129

Please sign in to comment.