Dotclear

source: plugins/attachments/_public.php @ 3958:5e250cd51362

Revision 3958:5e250cd51362, 8.9 KB checked in by franck <carnet.franck.paul@…>, 6 years ago (diff)

No more Flash player (flv, mp3) in Dotclear \o/

Line 
1<?php
2/**
3 * @brief attachments, a plugin for Dotclear 2
4 *
5 * @package Dotclear
6 * @subpackage Plugins
7 *
8 * @copyright Olivier Meunier & Association Dotclear
9 * @copyright GPL-2.0-only
10 */
11
12if (!defined('DC_RC_PATH')) {return;}
13
14# Attachments
15$core->tpl->addBlock('Attachments', ['attachmentTpl', 'Attachments']);
16$core->tpl->addBlock('AttachmentsHeader', ['attachmentTpl', 'AttachmentsHeader']);
17$core->tpl->addBlock('AttachmentsFooter', ['attachmentTpl', 'AttachmentsFooter']);
18$core->tpl->addValue('AttachmentMimeType', ['attachmentTpl', 'AttachmentMimeType']);
19$core->tpl->addValue('AttachmentType', ['attachmentTpl', 'AttachmentType']);
20$core->tpl->addValue('AttachmentFileName', ['attachmentTpl', 'AttachmentFileName']);
21$core->tpl->addValue('AttachmentSize', ['attachmentTpl', 'AttachmentSize']);
22$core->tpl->addValue('AttachmentTitle', ['attachmentTpl', 'AttachmentTitle']);
23$core->tpl->addValue('AttachmentThumbnailURL', ['attachmentTpl', 'AttachmentThumbnailURL']);
24$core->tpl->addValue('AttachmentURL', ['attachmentTpl', 'AttachmentURL']);
25$core->tpl->addValue('MediaURL', ['attachmentTpl', 'MediaURL']);
26$core->tpl->addBlock('AttachmentIf', ['attachmentTpl', 'AttachmentIf']);
27
28$core->tpl->addValue('EntryAttachmentCount', ['attachmentTpl', 'EntryAttachmentCount']);
29
30$core->addBehavior('tplIfConditions', ['attachmentBehavior', 'tplIfConditions']);
31
32class attachmentTpl
33{
34
35    /*dtd
36    <!ELEMENT tpl:Attachments - - -- Post Attachments loop -->
37     */
38    public static function Attachments($attr, $content)
39    {
40        $res =
41            "<?php\n" .
42            'if ($_ctx->posts !== null && $core->media) {' . "\n" .
43            '$_ctx->attachments = new ArrayObject($core->media->getPostMedia($_ctx->posts->post_id,null,"attachment"));' . "\n" .
44            "?>\n" .
45
46            '<?php foreach ($_ctx->attachments as $attach_i => $attach_f) : ' .
47            '$GLOBALS[\'attach_i\'] = $attach_i; $GLOBALS[\'attach_f\'] = $attach_f;' .
48            '$_ctx->file_url = $attach_f->file_url; ?>' .
49            $content .
50            '<?php endforeach; $_ctx->attachments = null; unset($attach_i,$attach_f,$_ctx->file_url); ?>' .
51
52            "<?php } ?>\n";
53
54        return $res;
55    }
56
57    /*dtd
58    <!ELEMENT tpl:AttachmentsHeader - - -- First attachments result container -->
59     */
60    public static function AttachmentsHeader($attr, $content)
61    {
62        return
63            "<?php if (\$attach_i == 0) : ?>" .
64            $content .
65            "<?php endif; ?>";
66    }
67
68    /*dtd
69    <!ELEMENT tpl:AttachmentsFooter - - -- Last attachments result container -->
70     */
71    public static function AttachmentsFooter($attr, $content)
72    {
73        return
74            "<?php if (\$attach_i+1 == count(\$_ctx->attachments)) : ?>" .
75            $content .
76            "<?php endif; ?>";
77    }
78
79    /*dtd
80    <!ELEMENT tpl:AttachmentsIf - - -- Test on attachment fields -->
81    <!ATTLIST tpl:AttachmentIf
82    is_image    (0|1)    #IMPLIED    -- test if attachment is an image (value : 1) or not (value : 0)
83    has_thumb    (0|1)    #IMPLIED    -- test if attachment has a square thumnail (value : 1) or not (value : 0)
84    is_mp3    (0|1)    #IMPLIED    -- test if attachment is a mp3 file (value : 1) or not (value : 0)
85    is_flv    (0|1)    #IMPLIED    -- test if attachment is a flv file (value : 1) or not (value : 0)
86    is_audio    (0|1)    #IMPLIED    -- test if attachment is an audio file (value : 1) or not (value : 0)
87    is_video    (0|1)    #IMPLIED    -- test if attachment is a video file (value : 1) or not (value : 0)
88    >
89     */
90    public static function AttachmentIf($attr, $content)
91    {
92        $if = [];
93
94        $operator = isset($attr['operator']) ? dcTemplate::getOperator($attr['operator']) : '&&';
95
96        if (isset($attr['is_image'])) {
97            $sign = (boolean) $attr['is_image'] ? '' : '!';
98            $if[] = $sign . '$attach_f->media_image';
99        }
100
101        if (isset($attr['has_thumb'])) {
102            $sign = (boolean) $attr['has_thumb'] ? '' : '!';
103            $if[] = $sign . 'isset($attach_f->media_thumb[\'sq\'])';
104        }
105
106        if (isset($attr['is_mp3'])) {
107            $sign = (boolean) $attr['is_mp3'] ? '==' : '!=';
108            $if[] = '$attach_f->type ' . $sign . ' "audio/mpeg3"';
109        }
110
111        if (isset($attr['is_flv'])) {
112            $sign = (boolean) $attr['is_flv'] ? '==' : '!=';
113            $if[] = '$attach_f->type ' . $sign . ' "video/x-flv"';
114        }
115
116        if (isset($attr['is_audio'])) {
117            $sign = (boolean) $attr['is_audio'] ? '==' : '!=';
118            $if[] = '$attach_f->type_prefix ' . $sign . ' "audio"';
119        }
120
121        if (isset($attr['is_video'])) {
122            // Since 2.15 .flv media are no more considered as video (Flash is obsolete)
123            $sign = (boolean) $attr['is_video'] ? '==' : '!=';
124            $test = '$attach_f->type_prefix ' . $sign . ' "video"';
125            if ($sign == '==') {
126                $test .= ' && $attach_f->type != "video/x-flv"';
127            } else {
128                $test .= ' || $attach_f->type == "video/x-flv"';
129            }
130            $if[] = $test;
131        }
132
133        if (count($if) != 0) {
134            return '<?php if(' . implode(' ' . $operator . ' ', (array) $if) . ') : ?>' . $content . '<?php endif; ?>';
135        } else {
136            return $content;
137        }
138    }
139
140    /*dtd
141    <!ELEMENT tpl:AttachmentMimeType - O -- Attachment MIME Type -->
142     */
143    public static function AttachmentMimeType($attr)
144    {
145        $f = $GLOBALS['core']->tpl->getFilters($attr);
146        return '<?php echo ' . sprintf($f, '$attach_f->type') . '; ?>';
147    }
148
149    /*dtd
150    <!ELEMENT tpl:AttachmentType - O -- Attachment type -->
151     */
152    public static function AttachmentType($attr)
153    {
154        $f = $GLOBALS['core']->tpl->getFilters($attr);
155        return '<?php echo ' . sprintf($f, '$attach_f->media_type') . '; ?>';
156    }
157
158    /*dtd
159    <!ELEMENT tpl:AttachmentFileName - O -- Attachment file name -->
160     */
161    public static function AttachmentFileName($attr)
162    {
163        $f = $GLOBALS['core']->tpl->getFilters($attr);
164        return '<?php echo ' . sprintf($f, '$attach_f->basename') . '; ?>';
165    }
166
167    /*dtd
168    <!ELEMENT tpl:AttachmentSize - O -- Attachment size -->
169    <!ATTLIST tpl:AttachmentSize
170    full    CDATA    #IMPLIED    -- if set, size is rounded to a human-readable value (in KB, MB, GB, TB)
171    >
172     */
173    public static function AttachmentSize($attr)
174    {
175        $f = $GLOBALS['core']->tpl->getFilters($attr);
176        if (!empty($attr['full'])) {
177            return '<?php echo ' . sprintf($f, '$attach_f->size') . '; ?>';
178        }
179        return '<?php echo ' . sprintf($f, 'files::size($attach_f->size)') . '; ?>';
180    }
181
182    /*dtd
183    <!ELEMENT tpl:AttachmentTitle - O -- Attachment title -->
184     */
185    public static function AttachmentTitle($attr)
186    {
187        $f = $GLOBALS['core']->tpl->getFilters($attr);
188        return '<?php echo ' . sprintf($f, '$attach_f->media_title') . '; ?>';
189    }
190
191    /*dtd
192    <!ELEMENT tpl:AttachmentThumbnailURL - O -- Attachment square thumbnail URL -->
193     */
194    public static function AttachmentThumbnailURL($attr)
195    {
196        $f = $GLOBALS['core']->tpl->getFilters($attr);
197        return
198        '<?php ' .
199        'if (isset($attach_f->media_thumb[\'sq\'])) {' .
200        'echo ' . sprintf($f, '$attach_f->media_thumb[\'sq\']') . ';' .
201            '}' .
202            '?>';
203    }
204
205    /*dtd
206    <!ELEMENT tpl:AttachmentURL - O -- Attachment URL -->
207     */
208    public static function AttachmentURL($attr)
209    {
210        $f = $GLOBALS['core']->tpl->getFilters($attr);
211        return '<?php echo ' . sprintf($f, '$attach_f->file_url') . '; ?>';
212    }
213
214    public static function MediaURL($attr)
215    {
216        $f = $GLOBALS['core']->tpl->getFilters($attr);
217        return '<?php echo ' . sprintf($f, '$_ctx->file_url') . '; ?>';
218    }
219
220    /*dtd
221    <!ELEMENT tpl:EntryAttachmentCount - O -- Number of attachments for entry -->
222    <!ATTLIST tpl:EntryAttachmentCount
223    none    CDATA    #IMPLIED    -- text to display for "no attachments" (default: no attachments)
224    one    CDATA    #IMPLIED    -- text to display for "one attachment" (default: one attachment)
225    more    CDATA    #IMPLIED    -- text to display for "more attachment" (default: %s attachment, %s is replaced by the number of attachments)
226    >
227     */
228    public static function EntryAttachmentCount($attr)
229    {
230        global $core;
231        return $core->tpl->displayCounter(
232            '$_ctx->posts->countMedia(\'attachment\')',
233            [
234                'none' => 'no attachments',
235                'one'  => 'one attachment',
236                'more' => '%d attachments'
237            ],
238            $attr,
239            false
240        );
241    }
242}
243
244class attachmentBehavior
245{
246    public static function tplIfConditions($tag, $attr, $content, $if)
247    {
248        if ($tag == "EntryIf" && isset($attr['has_attachment'])) {
249            $sign = (boolean) $attr['has_attachment'] ? '' : '!';
250            $if[] = $sign . '$_ctx->posts->countMedia(\'attachment\')';
251        }
252    }
253}
Note: See TracBrowser for help on using the repository browser.

Sites map