streams/Code/Module/Embedfiles.php

213 lines
7.5 KiB
PHP
Raw Normal View History

<?php
namespace Code\Module;
use App;
use Code\Web\Controller;
use Code\Lib\Channel;
use Code\Render\Theme;
use Code\Lib\Addon;
require_once('include/attach.php');
require_once('include/photos.php');
2023-02-18 16:28:36 +00:00
class Embedfiles extends Controller
{
2023-02-18 18:25:06 +00:00
/** This is the POST destination for the embedfiles button */
public function post()
{
2023-02-18 18:25:06 +00:00
/* start add new */
$channel = App::get_channel();
$channel_id = $channel['channel_id'];
$channel_address = $channel['channel_address'];
$observer = get_observer_hash();
2023-02-18 18:25:06 +00:00
if (argc() > 1 && argv(1) === 'sharelink') {
// API: /embedfiles/sharelink
2023-03-12 01:30:57 +00:00
$resource_id = $_POST["hash"];
2023-03-11 17:35:09 +00:00
2023-03-12 01:30:57 +00:00
$x = self::sharelink($resource_id, $channel_id);
if ($x) {
json_return_and_die(['status' => true, 'message' => $x, 'resource_id' => $resource_id]);
}
json_return_and_die(['errormsg' => 'Error retrieving resource ' . $resource_id, 'status' => false]);
}
2023-02-26 15:16:07 +00:00
$orderby = 'is_dir desc';
2023-02-20 21:37:35 +00:00
$results = attach_list_files($channel_id, $observer, $hash = '', $filename = '', $filetype = '', $orderby, $start = 0, $entries = 0, $since = '', $until = '');
2023-02-21 11:09:14 +00:00
$success = $results['success'];
2023-02-21 11:12:25 +00:00
$results = $results['results'];
2023-02-21 11:15:08 +00:00
$count = count($results);
2023-02-23 22:09:49 +00:00
$sorted = $this->get_embed_folders($results);
2023-02-23 17:57:00 +00:00
//$sorted = $this->get_embed_top_folders($results);
2023-02-23 18:01:21 +00:00
//$sorted = $this->get_embed_sub_folders($results);
2023-02-23 22:09:49 +00:00
//$sorted = $this->get_embed_files($results);
2023-02-26 01:10:37 +00:00
json_return_and_die(['success' => $success, 'address' => $channel_address, 'content' => $sorted]);
2023-02-21 02:13:10 +00:00
}
2023-02-22 13:26:07 +00:00
/* returns sorted files and folders */
public function get_embed_folders($results)
{
2023-02-20 22:34:48 +00:00
$sorted = array();
2023-02-21 01:59:03 +00:00
$i = 0;
2023-02-21 03:20:45 +00:00
foreach ($results as $result) {
if($result['is_dir'] == 1) {
2023-02-21 18:40:34 +00:00
$sorted[$i] = $result;
foreach ($results as $result) {
if($result['folder'] === $sorted[$i]['hash'] && !$result['is_dir']) {
2023-02-24 00:11:02 +00:00
array_push($sorted, $result);
2023-02-21 18:40:34 +00:00
}
}
2023-02-21 16:54:07 +00:00
}
$i = count($sorted);
2023-02-21 01:59:03 +00:00
}
return $sorted;
2023-02-20 22:23:03 +00:00
}
2023-02-23 13:11:01 +00:00
/* start get_embed_top_folders */
public function get_embed_top_folders($results)
2023-02-23 17:30:44 +00:00
{
2023-02-23 13:11:01 +00:00
$sorted = array();
$i = 0;
foreach ($results as $result) {
if($result['is_dir'] == 1 && $result['folder'] === '' ) {
2023-02-23 14:06:01 +00:00
$sorted[$i] = $result;
$i++;
2023-02-23 13:11:01 +00:00
}
}
return $sorted;
}
/* end get_embed_top_folders */
/* start get_embed_sub_folders */
public function get_embed_sub_folders($results)
{
$sorted = array();
$i = 0;
foreach ($results as $result) {
if($result['is_dir'] == 1 && $result['folder'] !== '' ) {
2023-02-23 18:01:21 +00:00
$sorted[$i] = $result;
$i++;
}
}
return $sorted;
}
/* end get_embed_sub_folders */
2023-02-23 15:03:09 +00:00
/* start get_embed_files */
public function get_embed_files($results)
2023-02-23 15:03:09 +00:00
{
$sorted = array();
$i = 0;
foreach ($results as $result) {
if(!$result['is_dir'] ) {
$sorted[$i] = $result;
$i++;
}
2023-02-23 15:03:09 +00:00
}
return $sorted;
}
/* end get_embed_files */
2023-02-23 15:03:09 +00:00
2023-03-12 01:30:57 +00:00
protected static function sharelink($resource, $channel_id = 0)
{
if (intval($channel_id)) {
$channel = Channel::from_id($channel_id);
} else {
$channel = App::get_channel();
}
$output = EMPTY_STR;
if ($channel) {
$resolution = 1;
2023-03-13 13:14:30 +00:00
switch($_POST["filetype"]) {
2023-03-13 13:10:34 +00:00
case "image/jpeg":
case "image/png":
case "image/gif":
2023-03-13 13:16:35 +00:00
if ($_POST["filetype"] === 'image/jpeg') {
$ext = '.jpg';
} elseif ($_POST["filetype"] === 'image/png') {
$ext = '.png';
} elseif ($_POST["filetype"] === 'image/gif') {
$ext = '.gif';
} else {
$ext = EMPTY_STR;
}
2023-03-13 13:16:35 +00:00
$alt = ' alt="' . $_POST["filename"] . '"';
2023-03-13 13:16:35 +00:00
$output = '[zrl=' . z_root() . '/photos/' . $channel['channel_address'] . '/image/' . $resource . ']'
2023-03-13 12:58:55 +00:00
. '[zmg width="100%" height="auto"' . $alt . ']'
. z_root() . '/photo/' . $resource . '-' . $resolution . $ext . '[/zmg][/zrl]';
break;
case "video/mp4":
case "video/webm":
case "video/ogg":
2023-03-13 13:58:17 +00:00
$url = z_root() . '/cloud/' . $channel['channel_address'] . '/' . $_POST['display_path'];
$output .= "\n\n" . '[zvideo]' . $url . '[/zvideo]' . "\n\n";
break;
2023-03-13 14:03:18 +00:00
case "audio/mpeg":
case "audio/wav":
case "audio/ogg":
2023-03-13 14:07:18 +00:00
$url = z_root() . '/cloud/' . $channel['channel_address'] . '/' . $_POST['display_path'];
$output .= "\n\n" . '[zaudio]' . $url . '[/zaudio]' . "\n\n";
2023-03-13 14:03:18 +00:00
break;
2023-03-13 14:34:50 +00:00
case "image/svg+xml":
$x = @file_get_contents('store/'. $channel['channel_address'] .'/'. $_POST['os_path']);
if ($x) {
$bb = svg2bb($x);
if ($bb) {
$output .= "\n\n" . $bb;
} else {
logger('empty return from svgbb');
}
} else {
logger('unable to read svg data file: '.'store/'. $channel['channel_address'] .'/'. $_POST['os_path']);
}
break;
2023-03-13 14:47:07 +00:00
case "text/vnd.abc":
if (Addon::is_installed('abc')) {
2023-03-13 14:47:07 +00:00
$x = @file_get_contents('store/'. $channel['channel_address'] .'/'. $_POST['os_path']);
if ($x) {
$output .= "\n\n" . '[abc]' . $x . '[/abc]';
} else {
logger('unable to read ABC data file: ' . 'store/' . $channel['channel_address'] . '/' . $_POST['os_path']);
}
}
2023-03-13 14:47:07 +00:00
break;
2023-03-13 15:01:49 +00:00
case "text/calendar":
$content = @file_get_contents('store/' . $channel['channel_address'] . '/' . $_POST['os_path']);
if ($content) {
$ev = ical_to_ev($content);
if ($ev) {
$output .= "\n\n" . format_event_bbcode($ev[0]) . "\n\n";
}
}
break;
2023-03-13 17:54:11 +00:00
case "text/x-multicode":
case "text/bbcode":
case "text/markdown":
2023-03-13 15:28:58 +00:00
case "text/html":
case "text/plain":
case "application/json":
2023-03-13 15:31:40 +00:00
$content = @file_get_contents('store/'. $channel['channel_address'] .'/'. $_POST['os_path']);
if ($content) {
$text = z_input_filter($content, $_POST['filetype']);
if ($text) {
2023-03-13 16:24:04 +00:00
$output .= "\n\n" . $text . "\n\n";
2023-03-13 15:31:40 +00:00
}
}
2023-03-13 15:28:58 +00:00
break;
2023-03-13 13:10:34 +00:00
default:
return '';
}
2023-03-13 12:50:11 +00:00
$output .= "\n\n" . '[attachment]' . $_POST['hash'] . ',' . $_POST['revision'] . '[/attachment]' . "\n";
return $output;
}
return '';
}
}