Skip to content

Commit

Permalink
Merge pull request #8 from AlexBlack788/ACL
Browse files Browse the repository at this point in the history
Add files via upload
  • Loading branch information
lejmr authored Jan 26, 2020
2 parents f39171f + 79393f1 commit 74144cc
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 46 deletions.
91 changes: 63 additions & 28 deletions action.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,48 +37,83 @@ function _ajax_call(Doku_Event $event, $param) {
$event->preventDefault();

//e.g. access additional request variables
global $conf;
global $conf, $lang;
global $INPUT; //available since release 2012-10-13 "Adora Belle"
$name = $INPUT->str('imageName');
$action = $INPUT->str('action');

// Convert image name to absolute path
$name = strtolower(trim($name));
$namespace="";
$lastColonPos = strripos($name,":");
if ($lastColonPos>0) {
$namespace=substr($name, 0, $lastColonPos);
$name = substr($name, $lastColonPos+1);
}

$namespace.=':';
$media_dir = join("/", array($conf['mediadir'], trim(str_replace(":","/",$namespace), "/") ));
if (! file_exists($media_dir)) {
mkdir ($media_dir, 0755, true);

$media_id = $name . '.png';
$media_id = cleanID($media_id);
$fl = mediaFN($media_id);

// Get user info
global $USERINFO;
global $INPUT;
global $INFO;

$user = $INPUT->server->str('REMOTE_USER');
$groups = (array) $USERINFO['grps'];
$auth_ow = (($conf['mediarevisions']) ? AUTH_UPLOAD : AUTH_DELETE);
$id = cleanID($name);

// Check ACL
$auth = auth_aclcheck($id, $user, $groups);
$access_granted = ($auth >= $auth_ow);

// AJAX request
if ($action == 'get_auth')
{
$json = new JSON();
echo $json->encode($access_granted);
return;
}

$image_file = $name.'.png';
$file_path = "/".join("/", array(trim($media_dir, "/"), $image_file));
;
if (!$access_granted)
return array($lang['media_perm_upload'], 0);

io_makeFileDir($fl);
if($action == 'save'){

$old = @filemtime($fl);
if(!file_exists(mediaFN($media_id, $old)) && file_exists($fl)) {
// add old revision to the attic if missing
media_saveOldRevision($media_id);
}
$filesize_old = file_exists($fl) ? filesize($fl) : 0;

// prepare directory
io_createNamespace($media_id, 'media');



if($action == 'save'){
// Write content to file
$content = $INPUT->str('content');
$base64data = explode(",", $content)[1];
$whandle = fopen($file_path,'w');
//$whandle = fopen($file_path,'w');
$whandle = fopen($fl, 'w');
fwrite($whandle,base64_decode($base64data));
fclose($whandle);
}


@clearstatcache(true, $fl);
$new = @filemtime($fl);
chmod($fl, $conf['fmode']);

// Add to log
$filesize_new = filesize($fl);
$sizechange = $filesize_new - $filesize_old;
if ($filesize_old != 0) {
addMediaLogEntry($new, $media_id, DOKU_CHANGE_TYPE_EDIT, '', '', null, $sizechange);
} else {
addMediaLogEntry($new, $media_id, DOKU_CHANGE_TYPE_CREATE, $lang['created'], '', null, $sizechange);
}
}
if($action == 'get'){
if (!file_exists($fl)) return;
// Return image in the base64 for draw.io
$json = new JSON();
header('Content-Type: application/json');
$fc = file_get_contents($file_path);
echo $json->encode(array("content" => "data:image/png;base64,".base64_encode($fc)));
header('Content-Type: application/json');
//$fc = file_get_contents($file_path);
$fc = file_get_contents($fl);
echo $json->encode(array("content" => "data:image/png;base64,".base64_encode($fc)));
}
}

}
?>
28 changes: 21 additions & 7 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,28 @@ var imagePointer = null;

function edit(image)
{
var imgPointer = image;
jQuery.post(
DOKU_BASE + 'lib/exe/ajax.php',
{
call: 'plugin_drawio',
imageName: imgPointer.getAttribute('id'),
action: 'get_auth'
},
function(data) {
if (data != 'true') return;
edit_cb(imgPointer);
}
);
}

function edit_cb(image)
{
imagePointer = image;

var iframe = document.createElement('iframe');
iframe.setAttribute('frameborder', '0');
iframe.setAttribute('class', 'drawio');
imagePointer = image;

var close = function()
{
Expand All @@ -34,7 +52,7 @@ function edit(image)
if (evt.data.length > 0)
{
var msg = JSON.parse(evt.data);
if (msg.event == 'init')
{
if (draft != null)
Expand All @@ -46,7 +64,6 @@ function edit(image)
}
else
{
// Read from AJAX
jQuery.post(
DOKU_BASE + 'lib/exe/ajax.php',
{
Expand All @@ -59,6 +76,7 @@ function edit(image)
autosave: 1, xmlpng: data.content}), '*');
}
);

}
}
else if (msg.event == 'export')
Expand Down Expand Up @@ -94,9 +112,6 @@ function edit(image)
iframe.contentWindow.postMessage(JSON.stringify({action: 'export',
format: 'xmlpng', xml: msg.xml, spin: 'Updating page'}), '*');
localStorage.setItem('.draft-' + name, JSON.stringify({lastModified: new Date(), xml: msg.xml}));



}
else if (msg.event == 'exit')
{
Expand All @@ -106,7 +121,6 @@ function edit(image)
}
}
};

window.addEventListener('message', receive);
iframe.setAttribute('src', editor);
document.body.appendChild(iframe);
Expand Down
23 changes: 12 additions & 11 deletions syntax.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,24 @@ public function render($mode, Doku_Renderer $renderer, $data)

// Validate that the image exists otherwise pring a default image
global $conf;
$image_in_mediadir = join("/", array($conf['mediadir'], trim(str_replace(":","/",$data), "/") ));
if(!file_exists($image_in_mediadir.".png")){
//$image_in_mediadir = join("/", array($conf['mediadir'], trim(str_replace(":","/",$data), "/") ));
$media_id = $data . '.png';

$image_in_mediadir = mediaFN($media_id);
$id = cleanID($data);

if(!file_exists($image_in_mediadir)){
$renderer->doc .= "<img class='mediacenter' id='".trim($data)."'
style='max-width:100%;cursor:pointer;'
onclick='edit(this);'
style='max-width:100%;cursor:pointer;' onclick='edit(this);'
src='".DOKU_BASE."lib/plugins/drawio/blank-image.png'
alt='".$file_name."' />";
alt='".$media_id."' />";
// $renderer->doc = $image_in_mediadir;
return true;
}


$renderer->doc .= "<img class='mediacenter' id='".trim($data)."'
style='max-width:100%;cursor:pointer;'
onclick='edit(this);'
src='".DOKU_BASE."lib/exe/fetch.php?media=".$data.".png'
alt='".$file_name."' />";
style='max-width:100%;cursor:pointer;' onclick='edit(this);'
src='".DOKU_BASE."lib/exe/fetch.php?media=".$data.".png'
alt='".$media_id."' />";
return true;
}
}
Expand Down

0 comments on commit 74144cc

Please sign in to comment.