Categories
Programs IT Support

How to get a file URL after upload a file using NextCloud APIs

I’ll show you a PHP code how to upload a local file and get the URL (FileID) from your NextCloud server for your reference.

/**
 * Get a internl link URL after uploading a local file.
 *
 * @param string $upFileFullPath The file Path of the file you want to upload
 * @param string $dstFolder the dstination folder path of your NextCloud environment.
 * @param string $nextCloudURL the URL of your NextCloud like "https://yourNC.com/"
 * @param string $userName Account name of your NextCloud
 * @param string $password Account password of your NextCloud
 * @return string|bool return the internal link URL. return false when error occured.
 */
function upFileAndGetInternalLink($upFileFullPath, $dstFolder, $nextCloudURL, $userName, $password){

	if (!is_file($upFileFullPath)){ return false; }
	$fileName = basename($upFileFullPath);
	if (substr($nextCloudURL, -1) != '/'){ $nextCloudURL .= '/'; }
	if (substr($dstFolder, -1) != '/'){ $dstFolder .= '/'; }

	$apiOfTheUpFile = $nextCloudURL . 'remote.php/dav/files/' . $dstFolder . $fileName;

	//libxml_use_internal_errors(true);

	$cmd = "curl -T '" . $upFileFullPath . "' -u '" . $userName . ":" . $password . "' '" . $apiOfTheUpFile . "'";
	echo $cmd;
	$ret = exec($cmd, $aryOutput, $resultCode);
	if ($ret === false || $resultCode != 0){ return false; }

	$cmd = "curl -u '" . $userName . ":" . $password . "' '" . $apiOfTheUpFile . "' -X PROPFIND --data '<?xml version="1.0" encoding="UTF-8"?>
		<d:propfind xmlns:d="DAV:">
		<d:prop xmlns:oc="http://owncloud.org/ns">
				<oc:fileid />
		</d:prop>
		</d:propfind>'";

	$ret = exec($cmd, $aryOutput, $resultCode);
	if ($ret === false || $resultCode != 0){ return false; }

	$ret = str_replace(':','',$ret);
	$xml = simplexml_load_string($ret);
	$fileId = (string)$xml->dresponse->dpropstat->dprop->ocfileid;

	return $nextCloudURL . "index.php/f/" . $fileId;

}

How to use the function

$upFileFullPath = '/td/dr/hogehoge.xlsx'; // the uploaded file name will be "hogehoge.xlsx" as same as this file name.
$nextCloudURL = 'https://yourNextCloud.com/'; // your nextcloud URL
$dstFolder = '/YourName/All/'; // $dstFolder always starts from your account name. The path is after the "data" dir of your NextCloud environment.
$userName = 'YourName';
$password = 'xH6Jk-Hkdje-OPalkd-J4PCn-mqxMW'; // app password

echo upFileAndGetInternalLink($upFileFullPath, $dstFolder, $nextCloudURL, $userName, $password);
 
Categories
Programs IT Support

Change the table design of WordPress theme twenty- twenty

I’m writing this blog with WordPress twenty-twenty, and I decided to change the table design a little like below.

Chores description
Programs I have no choice to do this for my work.
House choures I have no choice to do this for my family.
DIY I have no choice to do this for my family.
Go my hobby I haven’t played in a few years.
Sample

Go to [Appearance]-[Theme Editor], select [style.css], and add the following CSS at the end of the file. Then you can create a table that has no outer frames when you set [is-style-regular my-style-stripes] on the block setting [Additional CSS Class].

.my-style-stripes table{
	border:none;
}
.my-style-stripes table tr{
	border:none;
}
.my-style-stripes table tbody tr:nth-child(odd) {
	background: #dcd7ca;
}
.my-style-stripes table thead th {
	border-top:none;
	border-right: none;
	border-left: 1px solid #6d6d6d;
	border-bottom: 2px solid #6d6d6d;
}
.my-style-stripes table tbody td{
	border-top:none;
	border-right: none;
	border-left: 1px solid  #6d6d6d;
	border-bottom: 1px solid  #6d6d6d;
}
.my-style-stripes table thead th:first-child, .my-style-stripes tbody td:first-child{
	border-left: none;
}
.my-style-stripes tbody tr:last-child td {
	border-bottom: none;
}
Categories
Programs IT Support

A plugin to login Roundcube using your favorite username and password.

I made a plugin which you to allow to login Roundcube using your favorite username and password.

I also made a tool which convert a CSV file with account information to .config file at once to help your setting.

See my below Github page for details.

https://github.com/y-toy/virtuserpass

I made this tool for the company I work for. I want my company staff members to use their usual usernames and passwords instead of e-mail addresses and the passwords when they login Roundcube.

Categories
Programs IT Support

List the hosts in .ssh /config file, then ssh directly

This is a script to help you to ssh when you set many hosts in ssh .config file. I made Windows Powershell and Linux bash version scripts.

How to use

command: sshfc [Part of the host name]

If you type only sshfc, all hostnames set in the config file will be displayed.

If you type “sys” with the first argument, hostnames including “sys” will be displayed.

You can connect the host with ssh by selecting a number which is shown next to the hostname.

The command name is sshfc with the head of “ssh from .config”.

An example of use as follows.

C:> sshfc sys
1) system
2) test_system
3) dev_system
Press the number of the host to ssh. Press [c] if you want to cancel.'
: 3
ssh dev_system
hogehoge@192.168.1.100's password:
....

Install Windows Powershell version

Make bin folder and Add the folder to your PATH. And download the script file from Github and put the file under the bin folder.

New-Item ($HOME + "\bin") -ItemType Directory
[System.Environment]::SetEnvironmentVariable("Path", $env:Path + ";%USERPROFILE%\bin", "User")

git clone https://github.com/y-toy/windows_sshfc.git
cd windows_sshfc
Move-Item ./sshfc.ps1 ($HOME + "\bin")

Note

Try the following command if you are unable to run the script. This will loosen the restrictions of the script execution.

Set-ExecutionPolicy Unrestricted -scope CurrentUser

Install Linux Bash version

Download the script file from Github and move the file under PATH folder.

git clone https://github.com/y-toy/linux_sshfc.git
chmod +x ./linux_sshfc/sshfc
mv ./linux_sshfc/sshfc /usr/local/bin/

Note

I have recently changed my Windows ssh client to “Windows terminal” from Microsoft. The “Windows terminal” itself is excellent, but it is difficult to use when I select the destination of the SSH connection from ssh config file. I made this tool to cover the problem.

Since the tool I made was unexpectedly useful, I made the bash version so that I can use the “sshfc” command after entering the server as well.
Now I don’t have to think “What’s the hostname I set in my config file?”.