|
| 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 | +} |
0 commit comments