Skip to content

Commit

Permalink
fix linting problems and spelling
Browse files Browse the repository at this point in the history
  • Loading branch information
bennet0496 committed Sep 11, 2023
1 parent ca8b9aa commit 338916b
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 69 deletions.
6 changes: 6 additions & 0 deletions .idea/jsLibraryMappings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions .idea/libraries/rcmail.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/nextcloud_attachments.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Modifiable_Mail_mime.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

class Modifiable_Mail_mime extends \Mail_mime
class Modifiable_Mail_mime extends Mail_mime
{
public function __construct(\Mail_mime $other)
public function __construct(Mail_mime $other)
{
parent::__construct($other->build_params);

Expand All @@ -19,6 +19,7 @@ public function getParts(): array
return $this->parts;
}

/** @noinspection PhpUnused */
public function setParts(array $parts): void
{
$this->parts = $parts;
Expand Down
17 changes: 10 additions & 7 deletions client.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.


rcmail.addEventListener("plugin.nextcloud_login", function(data) {
if (data.status === "ok") {
rcmail.env.nextcloud_login_flow = data.url;
Expand All @@ -41,14 +42,13 @@ rcmail.addEventListener("plugin.nextcloud_login_result", function (event) {
rcmail.addEventListener("plugin.nextcloud_upload_result", function(event) {
//server finished upload
if (event.status === "ok") {
let position_element, cursor_pos, p = -1,
message = this.rcmail.editor.get_content(),
let message = this.rcmail.editor.get_content(),
sig = this.rcmail.env.identity;

//convert to human-readable file size
let size = event.result?.file?.size;
const unit = ["", "k", "M", "G", "T"];
let i = 0;
let i;
for(i = 0; size > 800 && i < unit.length; i++) {
size /= 1024;
}
Expand All @@ -58,12 +58,12 @@ rcmail.addEventListener("plugin.nextcloud_upload_result", function(event) {
let attach_text = "\n" + event.result?.file?.name +
" (" + size.toFixed(1) + " " + unit[i] + "B) <"
+ event.result?.url + ">" + "\n";
//insert before signature if one exsists
//insert before signature if one exists
if(sig && this.rcmail.env.signatures && this.rcmail.env.signatures[sig]) {
sig = this.rcmail.env.signatures[sig].text;
sig = sig.replace(/\r\n/g, '\n');

p = this.rcmail.env.top_posting ? message.indexOf(sig) : message.lastIndexOf(sig);
let p = this.rcmail.env.top_posting ? message.indexOf(sig) : message.lastIndexOf(sig);

message = message.substring(0, p) + attach_text + message.substring(p, message.length);
} else {
Expand Down Expand Up @@ -91,7 +91,6 @@ rcmail.addEventListener("plugin.nextcloud_upload_result", function(event) {
rcmail.editor.editor.getBody().insertBefore(paragraph, sigElem);
}
rcmail.display_message(event.result?.file?.name + " successfully uploaded to Nextcloud and link inserted", "confirmation", 5000)
// this.rcmail.remove_from_attachment_list("rcmfile" + event.result?.file?.id);
} else {
// rcmail.show_popup_dialog(JSON.stringify(event), "Nextcloud Upload Failed");
console.log(event);
Expand Down Expand Up @@ -130,7 +129,8 @@ rcmail.nextcloud_login_button_click_handler = function(btn_evt) {
if (rcmail.env.nextcloud_login_flow !== null) {
let hw = window.screen.availWidth / 2, hh = window.screen.availHeight / 2;
let x = window.screenX + hw - 300, y = window.screenY + hh - 400;
let pos = "screenX=" + x + ",screenY=" + hh;
let pos = "screenX=" + x + ",screenY=" + y;
// noinspection SpellCheckingInspection
if(!window.open(rcmail.env.nextcloud_login_flow, "", "noopener,noreferrer,popup,width=600,height=800,"+pos)) {
t.append("<p>Click <a href=\"" + rcmail.env.nextcloud_login_flow + "\">here</a> if no window opened</p>");
} else {
Expand Down Expand Up @@ -159,8 +159,10 @@ rcmail.nextcloud_login_button_click_handler = function(btn_evt) {
}, 1000, $(this));
}

// noinspection JSUnusedLocalSymbols
rcmail.addEventListener('init', function(evt) {
//retrieve nextcloud login status
// noinspection SpellCheckingInspection
rcmail.http_get("plugin.nextcloud_checklogin");

//intercept file_upload
Expand All @@ -184,6 +186,7 @@ rcmail.addEventListener('init', function(evt) {
//server indicated, it can't use the known username and password from the session
//to login to nextcloud. Probably because, 2FA is active.
if (rcmail.env.nextcloud_upload_available !== true && rcmail.env.nextcloud_upload_login_available === true) {
// noinspection SpellCheckingInspection
rcmail.show_popup_dialog("<p>The file you tried to upload is too large. You can automatically upload "+
"large files by connecting to your cloud storage blow.</p>"+
"<p><b>After connecting the storage, please try uploading the file again.</b></p>", "File too big", [
Expand Down
10 changes: 5 additions & 5 deletions config.inc.php.dist
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?php

// Full URL to the Nextcloud server
// e.g. https://example.com/nextcloud if in subpath
// e.g. https://example.com/nextcloud if in sub-path
// or https://cloud.example.com if in root
$config["nextcloud_attachment_server"] = "";

// Username resolveing stategy from internal Roundcube
// Username resolving strategy from internal Roundcube
// username which usually is the email address e.g. user@example.com
// %u -> email localpart
// %u -> email local part
// %s -> email as is
$config["nextcloud_attachment_username"] = "%u";

// Name for the subfolder to upload to
// Name for the sub-folder to upload to
// Defaults to "Mail Attachments"
// Can't be subfolder of subfolder link folder/sub
// Can't be sub-folder of sub-folder link folder/sub
$config["nextcloud_attachment_folder"] = "Mail Attachments";

// Checksum Algorithm for Attachment Page
Expand Down
109 changes: 54 additions & 55 deletions nextcloud_attachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class nextcloud_attachments extends rcube_plugin
{
private static function log($line): void
{
$rcmail = rcmail::get_instance();
$lines = explode(PHP_EOL, $line);
rcmail::write_log(NC_LOG_FILE, "[".NC_LOG_NAME."] ".$lines[0]);
unset($lines[0]);
Expand All @@ -49,6 +48,7 @@ public function init(): void
{

//action to check if we have a usable login
/** @noinspection SpellCheckingInspection */
$this->register_action('plugin.nextcloud_checklogin', [$this, 'check_login']);

//action to trigger login flow
Expand Down Expand Up @@ -130,6 +130,7 @@ public function add_preferences(array $param): array

if ($param["current"] == "compose") {

/** @noinspection JSUnresolvedReference */
$blocks["plugin.nextcloud_attachments"] = [
"name" => "Cloud Attachments",
"options" => [
Expand Down Expand Up @@ -321,8 +322,9 @@ public function logout() : void

if (!empty($server)) {
try {
/** @noinspection SpellCheckingInspection */
$client->delete($server . "/ocs/v2.php/core/apppassword");
} catch (GuzzleException $ignore) { }
} catch (GuzzleException) { }
}
}

Expand All @@ -346,27 +348,12 @@ private function resolve_username($val): bool|string
$this->load_config();
$method = $rcmail->config->get("nextcloud_attachment_username");

switch ($method) {
case "%s":
case "plain":
case "asis":
case "copy":
return $val;
case "%u":
case "username":
case "localpart":
case "stripdomain":
return explode("@", $val)[0];
case "email":
if(strpos($val, "@") !== false) {
return $val;
}
case "ldap":
return false;
default:
return false;

}
return match ($method) {
"%s" => $val,
"%u" => explode("@", $val)[0],
//"ldap" => false,
default => false,
};
}

private function __check_login(): array
Expand Down Expand Up @@ -519,39 +506,45 @@ public function upload(array $data) : array
$folder_uri = $server."/remote.php/dav/files/".$username."/".rawurlencode($folder);

//check folder
$res = $client->request("PROPFIND", $folder_uri);

if ($res->getStatusCode() == 404) { //folder does not exist
//attempt to create the folder
try {
$res = $client->request("MKCOL", $folder_uri);
try {
$res = $client->request("PROPFIND", $folder_uri);

if ($res->getStatusCode() != 201) { //creation failed
$body = $res->getBody()->getContents();
try {
$xml = new SimpleXMLElement($body);
} catch (Exception $e) {
self::log($username." xml parsing failed: ". print_r($e, true));
$xml = [];
if ($res->getStatusCode() == 404) { //folder does not exist
//attempt to create the folder
try {
$res = $client->request("MKCOL", $folder_uri);

if ($res->getStatusCode() != 201) { //creation failed
$body = $res->getBody()->getContents();
try {
$xml = new SimpleXMLElement($body);
} catch (Exception $e) {
self::log($username." xml parsing failed: ". print_r($e, true));
$xml = [];
}

$rcmail->output->command('plugin.nextcloud_upload_result', [
'status' => 'mkdir_error',
'code' => $res->getStatusCode(),
'message' => $res->getReasonPhrase(),
'result' => json_encode($xml)
]);

self::log($username." mkcol failed ". $res->getStatusCode(). PHP_EOL . $res->getBody()->getContents());
return ["status" => false, "abort" => true, "error" => $res->getReasonPhrase()];
}

$rcmail->output->command('plugin.nextcloud_upload_result', [
'status' => 'mkdir_error',
'code' => $res->getStatusCode(),
'message' => $res->getReasonPhrase(),
'result' => json_encode($xml)
]);

self::log($username." mkcol failed ". $res->getStatusCode(). PHP_EOL . $res->getBody()->getContents());
return ["status" => false, "abort" => true, "error" => $res->getReasonPhrase()];
} catch (GuzzleException $e) {
self::log($username." mkcol request failed: ". print_r($e, true));
}
} catch (GuzzleException $e) {
self::log($username." mkcol request failed: ". print_r($e, true));
} else if ($res->getStatusCode() > 400) { //we can't access the folder
self::log($username." propfind failed ". $res->getStatusCode(). PHP_EOL . $res->getBody()->getContents());
$rcmail->output->command('plugin.nextcloud_upload_result', ['status' => 'folder_error']);
return ["status" => false, "abort" => true, "error" => $res->getReasonPhrase()];
}
} else if ($res->getStatusCode() > 400) { //we can't access the folder
self::log($username." propfind failed ". $res->getStatusCode(). PHP_EOL . $res->getBody()->getContents());
} catch (GuzzleException $e) {
self::log($username." propfind failed ". print_r($e, true));
$rcmail->output->command('plugin.nextcloud_upload_result', ['status' => 'folder_error']);
return ["status" => false, "abort" => true, "error" => $res->getReasonPhrase()];
return ["status" => false, "abort" => true];
}

//get unique filename
Expand Down Expand Up @@ -604,7 +597,6 @@ public function upload(array $data) : array
]);

$body = $res->getBody()->getContents();
$url = "";

if($res->getStatusCode() == 200) { //upload successful
$ocs = new SimpleXMLElement($body);
Expand Down Expand Up @@ -643,6 +635,10 @@ public function upload(array $data) : array
self::log($username." share file failed: ". print_r($e, true));
$rcmail->output->command('plugin.nextcloud_upload_result', ['status' => 'link_error']);
return ["status" => false, "abort" => true];
} catch (Exception $e) {
self::log($username." xml parse failed: ". print_r($e, true));
$rcmail->output->command('plugin.nextcloud_upload_result', ['status' => 'link_error']);
return ["status" => false, "abort" => true];
}

//fill out template attachment HTML
Expand All @@ -664,18 +660,21 @@ public function upload(array $data) : array


$tmpl = str_replace("%FILENAME%", $data["name"], $tmpl);
/** @noinspection SpellCheckingInspection */
$tmpl = str_replace("%FILEURL%", $url, $tmpl);
/** @noinspection SpellCheckingInspection */
$tmpl = str_replace("%SERVERURL%", $server, $tmpl);
$tmpl = str_replace("%FILESIZE%", round($fs,1)." ".$u[$i]."B", $tmpl);
/** @noinspection SpellCheckingInspection */
$tmpl = str_replace("%ICONBLOB%", base64_encode($mime_icon), $tmpl);
$tmpl = str_replace("%CHECKSUM%", strtoupper($checksum)." ".hash_file($checksum, $data["path"]), $tmpl);

// Minimize HTML
// https://stackoverflow.com/a/6225706
$search = array(
'/\>[^\S ]+/s', // strip whitespaces after tags, except space
'/[^\S ]+\</s', // strip whitespaces before tags, except space
'/(\s)+/s', // shorten multiple whitespace sequences
'/>[^\S ]+/', // strip whitespaces after tags, except space
'/[^\S ]+</', // strip whitespaces before tags, except space
'/(\s)+/', // shorten multiple whitespace sequences
'/<!--(.|\s)*?-->/' // Remove HTML comments
);

Expand Down

0 comments on commit 338916b

Please sign in to comment.