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

Commit 5c0a9b1

Browse files
committed
Add default parameters value to make sharing simpler via RestAPI. Simple call via /share/public/path/to/file.ext or /share/private/path/to/file.ext?guest_user_pass=password
Fix URL detection on API case (remove all after API).
1 parent e49304c commit 5c0a9b1

File tree

5 files changed

+57
-5
lines changed

5 files changed

+57
-5
lines changed

core/src/core/classes/class.AJXP_Utils.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,13 @@ public static function detectServerURL($withURI = false)
871871
if (!$withURI) {
872872
return "$protocol://$name$port";
873873
} else {
874-
return "$protocol://$name$port".dirname($_SERVER["REQUEST_URI"]);
874+
$uri = dirname($_SERVER["REQUEST_URI"]);
875+
$api = ConfService::currentContextIsRestAPI();
876+
if(!empty($api)){
877+
// Keep only before api base
878+
$uri = array_shift(explode("/".$api."/", $uri));
879+
}
880+
return "$protocol://$name$port".$uri;
875881
}
876882
}
877883

core/src/core/classes/class.ConfService.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,26 @@ public static function currentContextIsCommandLine()
164164
{
165165
return php_sapi_name() === "cli";
166166
}
167+
168+
protected static $restAPIContext;
169+
170+
/**
171+
* Set or get if we are currently running REST
172+
* @static
173+
* @param null $set
174+
* @param string $baseUrl
175+
* @return bool
176+
*/
177+
public static function currentContextIsRestAPI($restBase = '')
178+
{
179+
if(!empty($restBase)){
180+
self::$restAPIContext = $restBase;
181+
return $restBase;
182+
}else{
183+
return self::$restAPIContext;
184+
}
185+
}
186+
167187
/**
168188
* Check the presence of mcrypt and option CMDLINE_ACTIVE
169189
* @static

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ public function switchAction($action, $httpVars, $fileVars)
182182
//------------------------------------
183183
case "share":
184184
$subAction = (isSet($httpVars["sub_action"])?$httpVars["sub_action"]:"");
185+
if(empty($subAction) && isSet($httpVars["simple_share_type"])){
186+
$subAction = "create_minisite";
187+
if(!isSet($httpVars["simple_right_read"]) && !isSet($httpVars["simple_right_download"])){
188+
$httpVars["simple_right_read"] = $httpVars["simple_right_download"] = "true";
189+
}
190+
}
185191
$file = AJXP_Utils::decodeSecureMagic($httpVars["file"]);
186192
$ajxpNode = new AJXP_Node($this->urlBase.$file);
187193
if (!file_exists($ajxpNode->getUrl())) {
@@ -223,6 +229,12 @@ public function switchAction($action, $httpVars, $fileVars)
223229
} else if ($subAction == "create_minisite") {
224230
header("Content-type:text/plain");
225231
if(isSet($httpVars["hash"]) && !empty($httpVars["hash"])) $httpHash = $httpVars["hash"];
232+
if(isSet($httpVars["simple_share_type"])){
233+
$httpVars["create_guest_user"] = "true";
234+
if($httpVars["simple_share_type"] == "private" && !isSet($httpVars["guest_user_pass"])){
235+
throw new Exception("Please provide a guest_user_pass for private link");
236+
}
237+
}
226238
$res = $this->createSharedMinisite($httpVars, $this->repository, $this->accessDriver);
227239
if (!is_array($res)) {
228240
$url = $res;
@@ -971,13 +983,22 @@ public static function loadMinisite($data, $hash = '', $error = null)
971983
$minisiteLogo = "index_shared.php?get_action=get_global_binary_param&binary_id=". $logoPath;
972984
}
973985
}
974-
$templateName = "ajxp_shared_folder";
986+
// Default value
975987
if(isSet($data["AJXP_TEMPLATE_NAME"])){
976988
$templateName = $data["AJXP_TEMPLATE_NAME"];
977989
if($templateName == "ajxp_film_strip" && AJXP_Utils::userAgentIsMobile()){
978990
$templateName = "ajxp_shared_folder";
979991
}
980992
}
993+
if(!isSet($templateName)){
994+
$repoObject = ConfService::getRepositoryById($repository);
995+
$filter = $repoObject->getContentFilter();
996+
if(!empty($filter) && count($filter->virtualPaths) == 1){
997+
$templateName = "ajxp_unique_strip";
998+
}else{
999+
$templateName = "ajxp_shared_folder";
1000+
}
1001+
}
9811002
// UPDATE TEMPLATE
9821003
$html = file_get_contents(AJXP_INSTALL_PATH."/".AJXP_PLUGINS_FOLDER."/action.share/res/minisite.php");
9831004
AJXP_Controller::applyHook("tpl.filter_html", array(&$html));
@@ -1538,6 +1559,10 @@ public function createSharedMinisite($httpVars, $repository, $accessDriver)
15381559
if($setFilter){
15391560
$httpVars["filter_nodes"] = $userSelection->buildNodes($this->accessDriver);
15401561
}
1562+
if(!isSet($httpVars["repo_label"])){
1563+
$first = $userSelection->getUniqueNode($this->accessDriver);
1564+
$httpVars["repo_label"] = SystemTextEncoding::toUTF8($first->getLabel());
1565+
}
15411566
}
15421567
$newRepo = $this->createSharedRepository($httpVars, $repository, $accessDriver, $uniqueUser);
15431568

core/src/plugins/action.share/manifest.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,9 @@
243243
if(action) action.selectionContext.dir = false;
244244
}
245245
]]></clientListener>
246-
<serverCallback methodName="switchAction" developerComment="Main action for sharing a file or a folder">
247-
<input_param description="Path of the resource to share" name="file" type="path" mandatory="true"/>
246+
<serverCallback methodName="switchAction" restParams="/simple_share_type/file+" developerComment="Main action for sharing a file or a folder">
247+
<input_param description="Path of the resource to share" name="nodes" type="AJXP_NODE[]" mandatory="true"/>
248+
<input_param description="Simple type for sharing via the REST api, always links, public or private" name="simple_share_type" type="public|private" mandatory="false"/>
248249
<input_param description="Specific type of sharing" name="sub_action" type="delegate_repo|create_minisite|public_link" mandatory="true"/>
249250
<input_param description="For editing the data of an existing workspace/minisite" name="repository_id" type="string" mandatory="false"/>
250251
<input_param description="For a workspace, directly set a watch on this for the current user" name="self_watch_folder" type="boolean"/>

core/src/rest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
exit;
4545
}
4646
$authDriver = ConfService::getAuthDriverImpl();
47-
47+
ConfService::currentContextIsRestAPI("api");
4848

4949
$uri = $_SERVER["REQUEST_URI"];
5050
$scriptUri = ltrim(dirname($_SERVER["SCRIPT_NAME"]),'/')."/api/";

0 commit comments

Comments
 (0)