1<?php
2require_once('./Services/FileDelivery/classes/FileDeliveryTypes/FileDeliveryTypeFactory.php');
3require_once './Services/FileDelivery/classes/FileDeliveryTypes/DeliveryMethod.php';
4require_once('./Services/FileDelivery/classes/Delivery.php');
5require_once('./Services/FileDelivery/interfaces/int.ilFileDeliveryService.php');
6require_once './Services/FileDelivery/classes/HttpServiceAware.php';
7
8use ILIAS\FileDelivery\FileDeliveryTypes\DeliveryMethod;
9use ILIAS\FileDelivery\Delivery;
10use ILIAS\FileDelivery\HttpServiceAware;
11use ILIAS\FileDelivery\ilFileDeliveryService;
12
13/**
14 * Class ilFileDelivery
15 *
16 * @author  Fabian Schmid <fs@studer-raimann.ch>
17 * @version 1.0.0
18 *
19 * @public
20 */
21final class ilFileDelivery implements ilFileDeliveryService
22{
23    use HttpServiceAware;
24    const DIRECT_PHP_OUTPUT = Delivery::DIRECT_PHP_OUTPUT;
25    const DELIVERY_METHOD_XSENDFILE = DeliveryMethod::XSENDFILE;
26    const DELIVERY_METHOD_XACCEL = DeliveryMethod::XACCEL;
27    const DELIVERY_METHOD_PHP = DeliveryMethod::PHP;
28    const DELIVERY_METHOD_PHP_CHUNKED = DeliveryMethod::PHP_CHUNKED;
29    const DISP_ATTACHMENT = Delivery::DISP_ATTACHMENT;
30    const DISP_INLINE = Delivery::DISP_INLINE;
31    /**
32     * @var Delivery $delivery
33     */
34    private $delivery;
35
36
37    /**
38     * ilFileDelivery constructor.
39     *
40     * @param string $filePath
41     */
42    public function __construct($filePath)
43    {
44        assert(is_string($filePath));
45        $this->delivery = new Delivery($filePath, self::http());
46    }
47
48
49    /**
50     * @inheritdoc
51     */
52    public static function deliverFileAttached($path_to_file, $download_file_name = '', $mime_type = '', $delete_file = false)
53    {
54        assert(is_string($path_to_file));
55        assert(is_string($download_file_name));
56        assert(is_string($mime_type));
57        assert(is_bool($delete_file));
58
59        $obj = new Delivery($path_to_file, self::http());
60
61        if (self::isNonEmptyString($download_file_name)) {
62            $obj->setDownloadFileName($download_file_name);
63        }
64        if (self::isNonEmptyString($mime_type)) {
65            $obj->setMimeType($mime_type);
66        }
67        $obj->setDisposition(self::DISP_ATTACHMENT);
68        $obj->setDeleteFile($delete_file);
69        $obj->deliver();
70    }
71
72
73    /**
74     * @inheritdoc
75     */
76    public static function streamVideoInline($path_to_file, $download_file_name = '')
77    {
78        assert(is_string($path_to_file));
79        assert(is_string($download_file_name));
80        $obj = new Delivery($path_to_file, self::http());
81        if (self::isNonEmptyString($download_file_name)) {
82            $obj->setDownloadFileName($download_file_name);
83        }
84        $obj->setDisposition(self::DISP_INLINE);
85        $obj->stream();
86    }
87
88
89    /**
90     * @inheritdoc
91     */
92    public static function deliverFileInline($path_to_file, $download_file_name = '')
93    {
94        assert(is_string($path_to_file));
95        assert(is_string($download_file_name));
96        $obj = new Delivery($path_to_file, self::http());
97
98        if (self::isNonEmptyString($download_file_name)) {
99            $obj->setDownloadFileName($download_file_name);
100        }
101        $obj->setDisposition(self::DISP_INLINE);
102        $obj->deliver();
103    }
104
105
106    /**
107     * @inheritdoc
108     */
109    public static function returnASCIIFileName($original_filename)
110    {
111        assert(is_string($original_filename));
112
113        return Delivery::returnASCIIFileName($original_filename);
114    }
115
116
117    /**
118     * Workaround because legacy components try to call methods which are moved to the Deliver
119     * class.
120     *
121     * @param string $name      The function name which was not found on the current object.
122     * @param array  $arguments The function arguments passed to the function which was not existent
123     *                          on the current object.
124     */
125    public function __call($name, array $arguments)
126    {
127        assert(is_string($name));
128        //forward call to Deliver class
129        call_user_func_array([ $this->delivery, $name ], $arguments);
130    }
131
132
133    /**
134     * Checks if the string is not empty.
135     *
136     * @param string $text The text which should be checked.
137     *
138     * @return bool True if the text is not empty otherwise false.
139     */
140    private static function isNonEmptyString($text)
141    {
142        assert(is_string($text));
143
144        return (bool) strcmp($text, '') !== 0;
145    }
146}
147