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

Commit 11b0129

Browse files
committed
Change url.shorten hook to make it more versatile.
First implementation of gateway.
1 parent a275cea commit 11b0129

File tree

7 files changed

+231
-27
lines changed

7 files changed

+231
-27
lines changed

core/src/plugins/action.share/src/ShareCenter.php

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,15 +1588,7 @@ public function createSharedMinisite($httpVars, &$update)
15881588
// STORE DATA & HASH IN SHARE STORE
15891589
$hash = $shareObject->save();
15901590
$url = $this->getPublicAccessManager()->buildPublicLink($hash);
1591-
$existingShortForm = $shareObject->getShortFormUrl();
1592-
if(empty($existingShortForm)){
1593-
$shortForm = "";
1594-
Controller::applyHook("url.shorten", array($this->currentContext, $url, &$shortForm));
1595-
if(!empty($shortForm)){
1596-
$shareObject->setShortFormUrl($shortForm);
1597-
$shareObject->save();
1598-
}
1599-
}
1591+
Controller::applyHook("url.shorten", array($this->currentContext, &$shareObject, $this->getPublicAccessManager()));
16001592

16011593
// LOG AND PUBLISH EVENT
16021594
$update = isSet($httpVars["repository_id"]);
@@ -1807,16 +1799,7 @@ public function shareNode(ContextInterface $ctx, $ajxpNode, $httpVars, &$update)
18071799
$ocsStore->storeInvitation($invitation);
18081800
}
18091801
}else{
1810-
$url = $this->getPublicAccessManager()->buildPublicLink($shareObject->getHash());
1811-
$existingShortForm = $shareObject->getShortFormUrl();
1812-
if(empty($existingShortForm)){
1813-
$shortForm = "";
1814-
Controller::applyHook("url.shorten", array($ctx, $url, &$shortForm));
1815-
if(!empty($shortForm)){
1816-
$shareObject->setShortFormUrl($shortForm);
1817-
$shareObject->save();
1818-
}
1819-
}
1802+
Controller::applyHook("url.shorten", array($this->currentContext, &$shareObject, $this->getPublicAccessManager()));
18201803
}
18211804

18221805
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/*
3+
* Copyright 2007-2016 Charles du Jeu - Abstrium SAS <team (at) pyd.io>
4+
* This file is part of Pydio.
5+
*
6+
* Pydio is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* Pydio is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with Pydio. If not, see <http://www.gnu.org/licenses/>.
18+
*
19+
* The latest code can be found at <https://pydio.com>.
20+
*/
21+
22+
namespace Pydio\LinkShortener;
23+
24+
use GuzzleHttp\Exception\RequestException;
25+
use Pydio\Core\Model\ContextInterface;
26+
use GuzzleHttp\Client;
27+
28+
use Pydio\Core\PluginFramework\Plugin;
29+
use Pydio\Core\Services\ApplicationState;
30+
use Pydio\Share\Model\ShareLink;
31+
use Pydio\Share\View\PublicAccessManager;
32+
33+
defined('AJXP_EXEC') or die( 'Access not allowed');
34+
35+
/**
36+
* FileGateway implementation
37+
* @package Pydio\LinkShortener
38+
*/
39+
class FileGateway extends Plugin
40+
{
41+
private $servers = [
42+
"filesend" => "https://filesend.cc",
43+
"yourshare" => "https://yoursha.re",
44+
];
45+
46+
/**
47+
* @param ContextInterface $ctx
48+
* @param ShareLink $shareObject
49+
* @param PublicAccessManager $publicAccessManager
50+
*/
51+
public function processShortenHook(ContextInterface $ctx, &$shareObject, $publicAccessManager){
52+
53+
$server = $this->getContextualOption($ctx, "GATEWAY_SERVER");
54+
$apiKey = $this->getContextualOption($ctx, "API_KEY");
55+
$apiSecret = $this->getContextualOption($ctx, "API_SECRET");
56+
if(empty($server) || empty($apiKey) || empty($apiSecret)){
57+
return;
58+
}
59+
if(!isSet($this->servers[$server])){
60+
$this->logError("FileGateway", "Cannot find valid server for key ".$server);
61+
return;
62+
}
63+
64+
$data = [
65+
"hash" => $shareObject->getHash(),
66+
"base" => ApplicationState::detectServerURL(true),
67+
"main_endpoint" => "proxy.php?hash={hash}",
68+
"dl_endpoint" => "proxy.php?hash={hash}&dl=true&file={path}",
69+
"shorten_type" => "full",
70+
];
71+
$headers['Authorization'] = 'Basic '.base64_encode($apiKey.":".$apiSecret);
72+
$client = new Client([]);
73+
$request = $client->createRequest("POST", $this->servers[$server], [
74+
"timeout" => 5,
75+
"headers" => $headers,
76+
"json" => $data
77+
]);
78+
79+
try{
80+
$response = $client->send($request);
81+
}catch (RequestException $r){
82+
$this->logError("FileGateway", "There was an error while trying to submit a request to the server: ".$r->getMessage());
83+
return;
84+
}
85+
$body = $response->getBody();
86+
$json = json_decode($body, true);
87+
if(!empty($json)){
88+
$newUrl = $json["public_url"];
89+
$shareObject->setShortFormUrl($newUrl);
90+
$shareObject->save();
91+
}else{
92+
$this->logError("FileGateway", "There was an error while trying to decode the server response: ".$body);
93+
}
94+
95+
96+
}
97+
98+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/*
3+
* Copyright 2007-2016 Charles du Jeu - Abstrium SAS <team (at) pyd.io>
4+
* This file is part of Pydio.
5+
*
6+
* Pydio is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* Pydio is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with Pydio. If not, see <http://www.gnu.org/licenses/>.
18+
*
19+
* The latest code can be found at <https://pydio.com>.
20+
*/
21+
22+
$mess=array(
23+
"File Gateway" => "File Gateway",
24+
"Shorten links and serve them proxied by Pydio hosted service" => "Shorten links and serve them proxied by Pydio hosted service",
25+
"Api Key" => "Api Key",
26+
"Api Key as provided in your Pydio.com account" => "Api Key as provided in your Pydio.com account",
27+
"Api Secret" => "Api Secret",
28+
"Api Secret, as provided in your Pydio.com account" => "Api Secret, as provided in your Pydio.com account",
29+
"Gateway Server" => "Gateway Server",
30+
"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+
);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/*
3+
* Copyright 2007-2016 Charles du Jeu - Abstrium SAS <team (at) pyd.io>
4+
* This file is part of Pydio.
5+
*
6+
* Pydio is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* Pydio is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with Pydio. If not, see <http://www.gnu.org/licenses/>.
18+
*
19+
* The latest code can be found at <https://pydio.com>.
20+
*/
21+
22+
$mess=array(
23+
"File Gateway" => "File Gateway",
24+
"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.",
25+
"Api Key" => "Clé d'API",
26+
"Api Key as provided in your Pydio.com account" => "La clé d'API disponible dans votre compte Pydio.com",
27+
"Api Secret" => "Clé secrète",
28+
"Api Secret, as provided in your Pydio.com account" => "La clé secrète, disponible dans votre compte Pydio.com",
29+
"Gateway Server" => "Serveur FileGateway",
30+
"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.",
31+
);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<plugin_info>
4+
<plugin_author>Charles du Jeu</plugin_author>
5+
</plugin_info>
6+
<client_settings>
7+
<resources>
8+
<i18n namespace="gateway_shortener" path="plugins/shorten.gateway/i18n" />
9+
</resources>
10+
</client_settings>
11+
<server_settings>
12+
<global_param name="API_KEY" type="string" label="CONF_MESSAGE[Api Key]" description="CONF_MESSAGE[Api Key as provided in your Pydio.com account]"/>
13+
<global_param name="API_SECRET" type="password" label="CONF_MESSAGE[Api Secret]" description="CONF_MESSAGE[Api Secret, as provided in your Pydio.com account]"/>
14+
<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.]"/>
15+
</server_settings>
16+
<registry_contributions>
17+
<hooks>
18+
<serverCallback methodName="processShortenHook" hookName="url.shorten"/>
19+
</hooks>
20+
</registry_contributions>
21+
<class_definition filename="plugins/shorten.gateway/FileGateway.php" classname="Pydio\LinkShortener\FileGateway"/>
22+
<dependencies>
23+
<activePlugin pluginName="action.share"/>
24+
</dependencies>
25+
</ajxp_plugin>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<p>
2+
This plugin allows you to use the exclusive Pydio hosted service, FileGateway. By shortening AND proxying your public links
3+
to your servers, FileGateway allows to you securely share resources with external users via public links without the risk of
4+
revealing your Pydio server URL.
5+
</p>
6+
<p>
7+
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
8+
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)
9+
or in Europe (yoursha.re).
10+
</p>

core/src/plugins/shorten.multi/MultiShortener.php

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
<?php
22
/*
3-
* Multi shortener plugin for ajaXplorer by FrenandoAloso
4-
* based in bit.ly plugin
5-
*/
6-
3+
* Copyright 2007-2016 Charles du Jeu - Abstrium SAS <team (at) pyd.io>
4+
* This file is part of Pydio.
5+
*
6+
* Pydio is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* Pydio is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with Pydio. If not, see <http://www.gnu.org/licenses/>.
18+
*
19+
* The latest code can be found at <https://pydio.com>.
20+
*/
721
namespace Pydio\LinkShortener;
822

923
use Pydio\Core\Model\ContextInterface;
1024
use Pydio\Core\Utils\FileHelper;
1125

1226
use Pydio\Core\PluginFramework\Plugin;
27+
use Pydio\Share\Model\ShareLink;
28+
use Pydio\Share\View\PublicAccessManager;
1329

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

@@ -22,11 +38,21 @@ class MultiShortener extends Plugin
2238
{
2339

2440
/**
25-
* @param string $url
26-
* @param string $shorten
41+
* @param ContextInterface $ctx
42+
* @param ShareLink $shareObject
43+
* @param PublicAccessManager $publicAccessManager
2744
*/
28-
public function processShortenHook(ContextInterface $ctx, $url, &$shorten){
29-
$shorten = $this->generateLink($ctx, $url);
45+
public function processShortenHook(ContextInterface $ctx, &$shareObject, $publicAccessManager){
46+
47+
$existingShortForm = $shareObject->getShortFormUrl();
48+
if(empty($existingShortForm)){
49+
$url = $publicAccessManager->buildPublicLink($shareObject->getHash());
50+
$shortForm = $this->generateLink($ctx, $url);
51+
if(!empty($shortForm)){
52+
$shareObject->setShortFormUrl($shortForm);
53+
$shareObject->save();
54+
}
55+
}
3056
}
3157

3258
/**

0 commit comments

Comments
 (0)