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

Commit

Permalink
Fix #418 : duplicate functions loadPublicletData & clearExpiredFiles,…
Browse files Browse the repository at this point in the history
… all moved to ShareCenter

Fix events applied to various nodes : we must reset the AJXP_Controller xPath to make sure the hooks are cleaned up as well. Should be reimplemented in an event-based pattern.
  • Loading branch information
cdujeu committed Apr 16, 2014
1 parent 5cbf0af commit 3d97f4b
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 103 deletions.
4 changes: 4 additions & 0 deletions core/src/core/classes/class.AJXP_Controller.php
Expand Up @@ -57,6 +57,10 @@ private static function initXPath()
return self::$xPath;
}

public static function registryReset(){
self::$xPath = null;
}

/**
* Check the current user "specificActionsRights" and filter the full registry actions with these.
* @static
Expand Down
36 changes: 2 additions & 34 deletions core/src/plugins/access.ajxp_conf/class.ajxp_confAccessDriver.php
Expand Up @@ -1495,7 +1495,6 @@ public function switchAction($action, $httpVars, $fileVars)
AJXP_XMLWriter::header();
$element = basename($httpVars["shared_file"]);
$dlFolder = ConfService::getCoreConf("PUBLIC_DOWNLOAD_FOLDER");
$publicletData = $this->loadPublicletData($dlFolder."/".$element.".php");
unlink($dlFolder."/".$element.".php");
AJXP_XMLWriter::sendMessage($mess["ajxp_shared.13"], null);
AJXP_XMLWriter::reloadDataNode();
Expand Down Expand Up @@ -1538,7 +1537,7 @@ public function switchAction($action, $httpVars, $fileVars)

case "clear_expired" :

$deleted = $this->clearExpiredFiles();
$deleted = ShareCenter::clearExpiredFiles(false); // $this->clearExpiredFiles();
AJXP_XMLWriter::header();
if (count($deleted)) {
AJXP_XMLWriter::sendMessage(sprintf($mess["ajxp_shared.23"], count($deleted).""), null);
Expand Down Expand Up @@ -2295,7 +2294,7 @@ public function listSharedFiles()
}

foreach ($files as $file) {
$publicletData = $this->loadPublicletData($file);
$publicletData = ShareCenter::loadPublicletData(array_shift(explode(".", basename($file))));
if (!is_a($publicletData["REPOSITORY"], "Repository")) {
continue;
}
Expand Down Expand Up @@ -2327,37 +2326,6 @@ public function metaSourceOrderingFunction($key1, $key2)
return strcmp($key1, $key2);
}

public function clearExpiredFiles()
{
$files = glob(ConfService::getCoreConf("PUBLIC_DOWNLOAD_FOLDER")."/*.php");
$loggedUser = AuthService::getLoggedUser();
$userId = $loggedUser->getId();
$deleted = array();
foreach ($files as $file) {
$publicletData = $this->loadPublicletData($file);
if (isSet($publicletData["EXPIRATION_TIME"]) && is_numeric($publicletData["EXPIRATION_TIME"]) && $publicletData["EXPIRATION_TIME"] > 0 && $publicletData["EXPIRATION_TIME"] < time()) {
unlink($file);
$deleted[] = basename($file);
}
}
return $deleted;
}

protected function loadPublicletData($file)
{
$inputData = null;
$lines = file($file);
$id = str_replace(".php", "", basename($file));
$code = trim($lines[3] . $lines[4] . $lines[5]);
if(strpos($code, '$cypheredData =') !== 0) return null;
eval($code);
$dataModified = !ShareCenter::checkHash($inputData, $id);
$publicletData = unserialize($inputData);
if(!is_array($publicletData)) return null;
$publicletData["SECURITY_MODIFIED"] = $dataModified;
return $publicletData;
}

public function updateUserRole($userId, $roleId, $addOrRemove, $updateSubUsers = false)
{
$confStorage = ConfService::getConfStorageImpl();
Expand Down
26 changes: 1 addition & 25 deletions core/src/plugins/access.ajxp_user/class.UserDashboardDriver.php
Expand Up @@ -173,7 +173,7 @@ public function switchAction($action, $httpVars, $fileVars)

case "clear_expired" :

$deleted = $this->clearExpiredFiles();
$deleted = ShareCenter::clearExpiredFiles(true); // $this->clearExpiredFiles();
AJXP_XMLWriter::header();
if (count($deleted)) {
AJXP_XMLWriter::sendMessage(sprintf($mess["user_dash.23"], count($deleted).""), null);
Expand Down Expand Up @@ -272,30 +272,6 @@ public function listSharedFiles($mode = "files")
}
}

public function clearExpiredFiles()
{
$files = glob(ConfService::getCoreConf("PUBLIC_DOWNLOAD_FOLDER")."/*.php");
$loggedUser = AuthService::getLoggedUser();
$userId = $loggedUser->getId();
$deleted = array();
foreach ($files as $file) {
$ar = explode(".", basename($file));
$id = array_shift($ar);
if(strlen($id) != 32) continue;
$publicletData = ShareCenter::loadPublicletData($id);
if (!isSet($publicletData["OWNER_ID"]) || $publicletData["OWNER_ID"] != $userId) {
continue;
}
if( (isSet($publicletData["EXPIRE_TIME"]) && is_numeric($publicletData["EXPIRE_TIME"]) && $publicletData["EXPIRE_TIME"] > 0 && $publicletData["EXPIRE_TIME"] < time()) ||
(isSet($publicletData["DOWNLOAD_LIMIT"]) && $publicletData["DOWNLOAD_LIMIT"] > 0 && $publicletData["DOWNLOAD_LIMIT"] <= $publicletData["DOWNLOAD_COUNT"]) ) {
unlink($file);
$deleted[] = basename($file);
PublicletCounter::delete(str_replace(".php", "", basename($file)));
}
}
return $deleted;
}

private function metaIcon($metaIcon)
{
return "<span class='icon-".$metaIcon." meta-icon'></span> ";
Expand Down
94 changes: 69 additions & 25 deletions core/src/plugins/action.share/class.ShareCenter.php
Expand Up @@ -984,6 +984,38 @@ public static function loadMinisite($data)
echo($html);
}

private static function deleteExpiredPubliclet($elementId, $data){

if(AuthService::getLoggedUser()->getId() != $data["OWNER_ID"]){
AuthService::logUser($data["OWNER_ID"], "", true);
}
$repoObject = $data["REPOSITORY"];

ConfService::loadDriverForRepository($repoObject)->detectStreamWrapper(true);
AJXP_Controller::registryReset();
$ajxpNode = new AJXP_Node("ajxp.".$repoObject->getAccessType()."://".$repoObject->getId().$data["FILE_PATH"]);
$metadata = $ajxpNode->retrieveMetadata(
"ajxp_shared",
true,
AJXP_METADATA_SCOPE_REPOSITORY,
true
);
self::deleteSharedElement("file", $elementId, AuthService::getLoggedUser());
if (count($metadata)) {
$updateMeta = false;
if (is_array($metadata["element"]) && isSet($metadata["element"][$elementId])) {
unset($metadata["element"][$elementId]);
if(count($metadata["element"]) > 0) $updateMeta = true;
}
if ($updateMeta) {
$ajxpNode->setMetadata("ajxp_shared", $metadata, true, AJXP_METADATA_SCOPE_REPOSITORY, true);
} else {
$ajxpNode->removeMetadata("ajxp_shared", true, AJXP_METADATA_SCOPE_REPOSITORY, true);
}
}

}

/**
* @static
* @param Array $data
Expand All @@ -1003,30 +1035,7 @@ public static function loadPubliclet($data)
// Remove the publiclet, it's done
if (strstr(realpath($_SERVER["SCRIPT_FILENAME"]),realpath(ConfService::getCoreConf("PUBLIC_DOWNLOAD_FOLDER"))) !== FALSE) {

AuthService::logUser($data["OWNER_ID"], "", true);
$repoObject = $data["REPOSITORY"];
$nodePath = "ajxp.".$repoObject->getAccessType()."://".$repoObject->getId().$data["FILE_PATH"];
$ajxpNode = new AJXP_Node($nodePath);
$metadata = $ajxpNode->retrieveMetadata(
"ajxp_shared",
true,
AJXP_METADATA_SCOPE_REPOSITORY
);
if (count($metadata)) {
$eType = "file";
$elementId = $shortHash;
$updateMeta = false;
if (is_array($metadata["element"]) && isSet($metadata["element"][$elementId])) {
unset($metadata["element"][$elementId]);
if(count($metadata["element"]) > 0) $updateMeta = true;
}
self::deleteSharedElement($eType, $elementId, AuthService::getLoggedUser());
if ($updateMeta) {
$ajxpNode->setMetadata("ajxp_shared", $metadata, true, AJXP_METADATA_SCOPE_REPOSITORY, true);
} else {
$ajxpNode->removeMetadata("ajxp_shared", true, AJXP_METADATA_SCOPE_REPOSITORY, true);
}
}
self::deleteExpiredPubliclet($shortHash, $data);
}

//echo "Link is expired, sorry.";
Expand Down Expand Up @@ -1607,7 +1616,7 @@ public function createSharedRepository($httpVars, $repository, $accessDriver, $u
public static function deleteSharedElement($type, $element, $loggedUser)
{
$mess = ConfService::getMessages();
AJXP_Logger::debug($type."-".$element);
AJXP_Logger::debug(__FILE__, "Deleting shared element ".$type."-".$element);
if ($type == "repository") {
$repo = ConfService::getRepositoryById($element);
if($repo == null) return;
Expand Down Expand Up @@ -1680,6 +1689,7 @@ public static function loadPublicletData($id)
$inputData = '';
$code = $lines[3] . $lines[4] . $lines[5];
eval($code);
if(empty($inputData)) return false;
$dataModified = self::checkHash($inputData, $id); //(md5($inputData) != $id);
$publicletData = unserialize($inputData);
$publicletData["SECURITY_MODIFIED"] = $dataModified;
Expand All @@ -1690,6 +1700,40 @@ public static function loadPublicletData($id)
return $publicletData;
}

public static function clearExpiredFiles($currentUser = true)
{
$files = glob(ConfService::getCoreConf("PUBLIC_DOWNLOAD_FOLDER")."/*.php");
if($currentUser){
$loggedUser = AuthService::getLoggedUser();
$userId = $loggedUser->getId();
}else{
$originalUser = AuthService::getLoggedUser()->getId();
}
$deleted = array();
$switchBackToOriginal = false;
foreach ($files as $file) {
$ar = explode(".", basename($file));
$id = array_shift($ar);
$publicletData = self::loadPublicletData($id);
if($publicletData === false) continue;
if ($currentUser && ( !isSet($publicletData["OWNER_ID"]) || $publicletData["OWNER_ID"] != $userId )) {
continue;
}
if( (isSet($publicletData["EXPIRE_TIME"]) && is_numeric($publicletData["EXPIRE_TIME"]) && $publicletData["EXPIRE_TIME"] > 0 && $publicletData["EXPIRE_TIME"] < time()) ||
(isSet($publicletData["DOWNLOAD_LIMIT"]) && $publicletData["DOWNLOAD_LIMIT"] > 0 && $publicletData["DOWNLOAD_LIMIT"] <= $publicletData["DOWNLOAD_COUNT"]) ) {
if(!$currentUser) $switchBackToOriginal = true;
self::deleteExpiredPubliclet($id, $publicletData);
$deleted[] = $publicletData["FILE_PATH"];

}
}
if($switchBackToOriginal){
AuthService::logUser($originalUser, "", true);
}
return $deleted;
}


public static function currentContextIsLinkDownload(){
return (isSet($_GET["dl"]) && isSet($_GET["dl"]) == "true");
}
Expand Down
43 changes: 24 additions & 19 deletions core/src/plugins/index.lucene/class.AjxpLuceneIndexer.php
Expand Up @@ -371,28 +371,33 @@ public function recursiveIndexation($url)
public function updateNodeIndexMeta($node)
{
require_once("Zend/Search/Lucene.php");
if (isSet($this->currentIndex)) {
$index = $this->currentIndex;
} else {
$index = $this->loadIndex($node->getRepositoryId());
}
Zend_Search_Lucene_Analysis_Analyzer::setDefault( new Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive());

if (AuthService::usersEnabled() && AuthService::getLoggedUser()!=null) {
$term = new Zend_Search_Lucene_Index_Term(SystemTextEncoding::toUTF8($node->getUrl()), "node_url");
$hits = $index->termDocs($term);
foreach ($hits as $hitId) {
$hit = $index->getDocument($hitId);
if ($hit->ajxp_scope == 'shared' || ($hit->ajxp_scope == 'user' && $hit->ajxp_user == AuthService::getLoggedUser()->getId())) {
$index->delete($hitId);
try{

if (isSet($this->currentIndex)) {
$index = $this->currentIndex;
} else {
$index = $this->loadIndex($node->getRepositoryId());
}
Zend_Search_Lucene_Analysis_Analyzer::setDefault( new Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive());

if (AuthService::usersEnabled() && AuthService::getLoggedUser()!=null) {
$term = new Zend_Search_Lucene_Index_Term(SystemTextEncoding::toUTF8($node->getUrl()), "node_url");
$hits = $index->termDocs($term);
foreach ($hits as $hitId) {
$hit = $index->getDocument($hitId);
if ($hit->ajxp_scope == 'shared' || ($hit->ajxp_scope == 'user' && $hit->ajxp_user == AuthService::getLoggedUser()->getId())) {
$index->delete($hitId);
}
}
} else {
$id = $this->getIndexedDocumentId($index, $node);
if($id != null) $index->delete($id);
}
} else {
$id = $this->getIndexedDocumentId($index, $node);
if($id != null) $index->delete($id);
$this->createIndexedDocument($node, $index);
$this->logDebug(__FILE__, "Indexation passed ".$node->getUrl());
} catch (Exception $e){
$this->logError(__FILE__, "Lucene indexation failed for ".$node->getUrl()." (".$e->getMessage().")");
}
$this->createIndexedDocument($node, $index);

}

/**
Expand Down

0 comments on commit 3d97f4b

Please sign in to comment.