Browse code

Only admins are allowed to change filename

Benjamin Roth authored on12/10/2023 15:00:12
Showing1 changed files
... ...
@@ -53,7 +53,7 @@ class MemberSecureDownloadsListener {
53 53
      */
54 54
     public function onListEditOperationCallback(array $row, ?string $href, string $label, string $title, ?string $icon, string $attributes, string $table, array $rootRecordIds, ?array $childRecordIds, bool $circularReference, ?string $previous, ?string $next, DataContainer $dc)
55 55
     {
56
-        if (!$this->User->isAdmin && !$this->User->hasAccess('tl_files::name','alexf'))
56
+        if (!$this->User->isAdmin)
57 57
         {
58 58
             return '';
59 59
         }
Browse code

Change member file edit action

Benjamin Roth authored on12/10/2023 14:56:11
Showing1 changed files
... ...
@@ -53,7 +53,7 @@ class MemberSecureDownloadsListener {
53 53
      */
54 54
     public function onListEditOperationCallback(array $row, ?string $href, string $label, string $title, ?string $icon, string $attributes, string $table, array $rootRecordIds, ?array $childRecordIds, bool $circularReference, ?string $previous, ?string $next, DataContainer $dc)
55 55
     {
56
-        if (!$this->User->isAdmin && !$this->User->hasAccess('tl_member_secureDownloads::ctime','alexf'))
56
+        if (!$this->User->isAdmin && !$this->User->hasAccess('tl_files::name','alexf'))
57 57
         {
58 58
             return '';
59 59
         }
... ...
@@ -62,9 +62,10 @@ class MemberSecureDownloadsListener {
62 62
         $objFile = $objSecFile->getRelated('uuid');
63 63
         $title = sprintf($GLOBALS['TL_LANG']['tl_member_secureDownloads']['edit'][1],$objFile->name);
64 64
 
65
-        $href .= '&id='.$row['id'];
65
+//        $href .= '&id='.$row['id'];
66
+        $href = 'do=files&act=edit&id=' . $objFile->path;
66 67
 
67
-        return '<a href="'.Backend::addToUrl($href).'" title="'.StringUtil::specialchars($title).'">'.Image::getHtml($icon, $label).'</a> ';
68
+        return '<a href="'.Backend::addToUrl($href,true,['table']).'" title="'.StringUtil::specialchars($title).'">'.Image::getHtml($icon, $label).'</a> ';
68 69
     }
69 70
 
70 71
     /**
Browse code

Allow non admin users to delete memberfiles

Benjamin Roth authored on12/10/2023 14:48:55
Showing1 changed files
... ...
@@ -53,7 +53,7 @@ class MemberSecureDownloadsListener {
53 53
      */
54 54
     public function onListEditOperationCallback(array $row, ?string $href, string $label, string $title, ?string $icon, string $attributes, string $table, array $rootRecordIds, ?array $childRecordIds, bool $circularReference, ?string $previous, ?string $next, DataContainer $dc)
55 55
     {
56
-        if (!$this->User->isAdmin && !$this->User->hasAccess('edit','sec_dl_access'))
56
+        if (!$this->User->isAdmin && !$this->User->hasAccess('tl_member_secureDownloads::ctime','alexf'))
57 57
         {
58 58
             return '';
59 59
         }
... ...
@@ -72,7 +72,7 @@ class MemberSecureDownloadsListener {
72 72
      */
73 73
     public function onListDeleteOperationCallback(array $row, ?string $href, string $label, string $title, ?string $icon, string $attributes, string $table, array $rootRecordIds, ?array $childRecordIds, bool $circularReference, ?string $previous, ?string $next, DataContainer $dc)
74 74
     {
75
-        if (!$this->User->isAdmin && !$this->User->hasAccess('edit','sec_dl_access'))
75
+        if (!$this->User->isAdmin && !$this->User->hasAccess('tl_member_secureDownloads::ctime','alexf'))
76 76
         {
77 77
             return '';
78 78
         }
Browse code

Fix truncate sql error by changing sec_dl_access flag

Benjamin Roth authored on11/09/2023 10:38:46
Showing1 changed files
... ...
@@ -53,7 +53,7 @@ class MemberSecureDownloadsListener {
53 53
      */
54 54
     public function onListEditOperationCallback(array $row, ?string $href, string $label, string $title, ?string $icon, string $attributes, string $table, array $rootRecordIds, ?array $childRecordIds, bool $circularReference, ?string $previous, ?string $next, DataContainer $dc)
55 55
     {
56
-        if (!$this->User->isAdmin && !$this->User->hasAccess(1,'sec_dl_access'))
56
+        if (!$this->User->isAdmin && !$this->User->hasAccess('edit','sec_dl_access'))
57 57
         {
58 58
             return '';
59 59
         }
... ...
@@ -72,7 +72,7 @@ class MemberSecureDownloadsListener {
72 72
      */
73 73
     public function onListDeleteOperationCallback(array $row, ?string $href, string $label, string $title, ?string $icon, string $attributes, string $table, array $rootRecordIds, ?array $childRecordIds, bool $circularReference, ?string $previous, ?string $next, DataContainer $dc)
74 74
     {
75
-        if (!$this->User->isAdmin && !$this->User->hasAccess(1,'sec_dl_access'))
75
+        if (!$this->User->isAdmin && !$this->User->hasAccess('edit','sec_dl_access'))
76 76
         {
77 77
             return '';
78 78
         }
Browse code

Update

Benjamin Roth authored on26/07/2023 17:00:15
Showing1 changed files
... ...
@@ -126,7 +126,7 @@ class MemberSecureDownloadsListener {
126 126
     }
127 127
 
128 128
     /**
129
-     * @Callbacktable="tl_member_secureDownloads", target="fields.name.load")
129
+     * @Callback(table="tl_member_secureDownloads", target="fields.name.load")
130 130
      */
131 131
     public function onNameLoadCallback($varValue, DataContainer $dc)
132 132
     {
... ...
@@ -141,7 +141,7 @@ class MemberSecureDownloadsListener {
141 141
     }
142 142
 
143 143
     /**
144
-     * @Callbacktable="tl_member_secureDownloads", target="fields.name.save")
144
+     * @Callback(table="tl_member_secureDownloads", target="fields.name.save")
145 145
      */
146 146
     public function onNameSaveCallback($varValue, DataContainer $dc)
147 147
     {
Browse code

Update

Benjamin Roth authored on26/07/2023 15:18:55
Showing1 changed files
... ...
@@ -49,7 +49,7 @@ class MemberSecureDownloadsListener {
49 49
     }
50 50
 
51 51
     /**
52
-     * @Callback(table="tl_member_secureDownloads", target="list.operations.edit.button"
52
+     * @Callback(table="tl_member_secureDownloads", target="list.operations.edit.button")
53 53
      */
54 54
     public function onListEditOperationCallback(array $row, ?string $href, string $label, string $title, ?string $icon, string $attributes, string $table, array $rootRecordIds, ?array $childRecordIds, bool $circularReference, ?string $previous, ?string $next, DataContainer $dc)
55 55
     {
... ...
@@ -68,7 +68,7 @@ class MemberSecureDownloadsListener {
68 68
     }
69 69
 
70 70
     /**
71
-     * @Callback(table="tl_member_secureDownloads", target="list.operations.delete.button"
71
+     * @Callback(table="tl_member_secureDownloads", target="list.operations.delete.button")
72 72
      */
73 73
     public function onListDeleteOperationCallback(array $row, ?string $href, string $label, string $title, ?string $icon, string $attributes, string $table, array $rootRecordIds, ?array $childRecordIds, bool $circularReference, ?string $previous, ?string $next, DataContainer $dc)
74 74
     {
... ...
@@ -88,7 +88,7 @@ class MemberSecureDownloadsListener {
88 88
     }
89 89
 
90 90
     /**
91
-     * @Callback(table="tl_member_secureDownloads", target="list.operations.show.button"
91
+     * @Callback(table="tl_member_secureDownloads", target="list.operations.show.button")
92 92
      */
93 93
     public function onListShowOperationCallback(array $row, ?string $href, string $label, string $title, ?string $icon, string $attributes, string $table, array $rootRecordIds, ?array $childRecordIds, bool $circularReference, ?string $previous, ?string $next, DataContainer $dc)
94 94
     {
Browse code

Update

Benjamin Roth authored on26/07/2023 13:13:48
Showing1 changed files
... ...
@@ -10,13 +10,20 @@ declare(strict_types=1);
10 10
  * @license commercial
11 11
  */
12 12
 
13
-namespace VonRotenberg\MemberfilesBundle\EventListener\DataContainer;
13
+namespace vonRotenberg\MemberfilesBundle\EventListener\DataContainer;
14 14
 
15
+use Contao\Backend;
15 16
 use Contao\BackendUser;
16 17
 use Contao\CoreBundle\ServiceAnnotation\Callback;
18
+use Contao\DataContainer;
19
+use Contao\File;
20
+use Contao\Image;
21
+use Contao\Input;
22
+use Contao\StringUtil;
17 23
 use Contao\System;
18 24
 use Contao\Date;
19 25
 use Contao\Config;
26
+use vonRotenberg\MemberfilesBundle\Model\SecureDownloadsModel;
20 27
 
21 28
 class MemberSecureDownloadsListener {
22 29
 
... ...
@@ -24,6 +31,7 @@ class MemberSecureDownloadsListener {
24 31
      * @var BackendUser
25 32
      */
26 33
     protected $User;
34
+
27 35
     public function __construct()
28 36
     {
29 37
         $this->User = BackendUser::getInstance();
... ...
@@ -39,4 +47,172 @@ class MemberSecureDownloadsListener {
39 47
 
40 48
         return '<div class="tl_content_left">' . ($objFile !== null ? $objFile->name : '') . ' <span style="color:#b3b3b3;padding-left:3px">[' . Date::parse(Config::get('datimFormat'), $row['ctime']) . ']</span></div>';
41 49
     }
50
+
51
+    /**
52
+     * @Callback(table="tl_member_secureDownloads", target="list.operations.edit.button"
53
+     */
54
+    public function onListEditOperationCallback(array $row, ?string $href, string $label, string $title, ?string $icon, string $attributes, string $table, array $rootRecordIds, ?array $childRecordIds, bool $circularReference, ?string $previous, ?string $next, DataContainer $dc)
55
+    {
56
+        if (!$this->User->isAdmin && !$this->User->hasAccess(1,'sec_dl_access'))
57
+        {
58
+            return '';
59
+        }
60
+
61
+        $objSecFile = SecureDownloadsModel::findByPk($row['id']);
62
+        $objFile = $objSecFile->getRelated('uuid');
63
+        $title = sprintf($GLOBALS['TL_LANG']['tl_member_secureDownloads']['edit'][1],$objFile->name);
64
+
65
+        $href .= '&amp;id='.$row['id'];
66
+
67
+        return '<a href="'.Backend::addToUrl($href).'" title="'.StringUtil::specialchars($title).'">'.Image::getHtml($icon, $label).'</a> ';
68
+    }
69
+
70
+    /**
71
+     * @Callback(table="tl_member_secureDownloads", target="list.operations.delete.button"
72
+     */
73
+    public function onListDeleteOperationCallback(array $row, ?string $href, string $label, string $title, ?string $icon, string $attributes, string $table, array $rootRecordIds, ?array $childRecordIds, bool $circularReference, ?string $previous, ?string $next, DataContainer $dc)
74
+    {
75
+        if (!$this->User->isAdmin && !$this->User->hasAccess(1,'sec_dl_access'))
76
+        {
77
+            return '';
78
+        }
79
+
80
+        $objSecFile = SecureDownloadsModel::findByPk($row['id']);
81
+        $objFile = $objSecFile->getRelated('uuid');
82
+        $attributes = ' onclick="if(!confirm(\'' . sprintf($GLOBALS['TL_LANG']['MSC']['deleteConfirmFile'],$objFile->name) . '\'))return false;Backend.getScrollOffset()"';
83
+        $title = sprintf($GLOBALS['TL_LANG']['tl_member_secureDownloads']['delete'][1],$objFile->name);
84
+
85
+        $href .= '&amp;id='.$row['id'];
86
+
87
+        return '<a href="'.Backend::addToUrl($href).'" title="'.StringUtil::specialchars($title).'"'.$attributes.'>'.Image::getHtml($icon, $label).'</a> ';
88
+    }
89
+
90
+    /**
91
+     * @Callback(table="tl_member_secureDownloads", target="list.operations.show.button"
92
+     */
93
+    public function onListShowOperationCallback(array $row, ?string $href, string $label, string $title, ?string $icon, string $attributes, string $table, array $rootRecordIds, ?array $childRecordIds, bool $circularReference, ?string $previous, ?string $next, DataContainer $dc)
94
+    {
95
+        if (Input::get('popup'))
96
+        {
97
+            return '';
98
+        }
99
+        else
100
+        {
101
+            $objSecFile = SecureDownloadsModel::findByPk($row['id']);
102
+            $objFile = $objSecFile->getRelated('uuid');
103
+            $title = sprintf($GLOBALS['TL_LANG']['tl_member_secureDownloads']['show'][1],$objFile->name);
104
+
105
+            if ($objFile !== null)
106
+            {
107
+                return '<a href="contao/popup.php?src=' . base64_encode($objFile->path) . '" title="'.StringUtil::specialchars($title, false, true).'"'.$attributes.' onclick="Backend.openModalIframe({\'width\':600,\'title\':\''.str_replace("'", "\\'", StringUtil::specialchars($objFile->name, false, true)).'\',\'url\':this.href,\'height\':300});return false">'.Image::getHtml($icon, $label).'</a> ';
108
+            } else {
109
+                return '';
110
+            }
111
+        }
112
+    }
113
+
114
+    /**
115
+     * @Callback(table="tl_member_secureDownloads", target="config.ondelete")
116
+     */
117
+    public function onConfigDeleteCallback(DataContainer $dc)
118
+    {
119
+        $objSecFile = SecureDownloadsModel::findByPk($dc->id);
120
+        $objFile = $objSecFile->getRelated('uuid');
121
+
122
+        if ($objFile !== null) {
123
+            $File = new File($objFile->path,true);
124
+            $File->delete();
125
+        }
126
+    }
127
+
128
+    /**
129
+     * @Callbacktable="tl_member_secureDownloads", target="fields.name.load")
130
+     */
131
+    public function onNameLoadCallback($varValue, DataContainer $dc)
132
+    {
133
+        $objSecFile = SecureDownloadsModel::findByPk($dc->id);
134
+        $objFile = $objSecFile->getRelated('uuid');
135
+
136
+        if ($objFile !== null) {
137
+            return $objFile->name;
138
+        }
139
+
140
+        return null;
141
+    }
142
+
143
+    /**
144
+     * @Callbacktable="tl_member_secureDownloads", target="fields.name.save")
145
+     */
146
+    public function onNameSaveCallback($varValue, DataContainer $dc)
147
+    {
148
+        $varValue = str_replace('"', '', $varValue);
149
+        $chunks = array_filter(explode('/', $varValue), 'strlen');
150
+
151
+        if (count($chunks) < 1)
152
+        {
153
+            return '';
154
+        }
155
+
156
+        // Only allow slashes when creating new folders
157
+        if ($dc->value != '__new__' && count($chunks) > 1)
158
+        {
159
+            throw new Exception($GLOBALS['TL_LANG']['ERR']['invalidName']);
160
+        }
161
+
162
+        foreach ($chunks as $chunk)
163
+        {
164
+            if (preg_match('/\.$/', $chunk))
165
+            {
166
+                throw new Exception($GLOBALS['TL_LANG']['ERR']['invalidName']);
167
+            }
168
+        }
169
+
170
+        // Check the length without the file extension
171
+        if ($dc->activeRecord)
172
+        {
173
+            $intMaxlength = $GLOBALS['TL_DCA'][$dc->table]['fields'][$dc->field]['eval']['maxlength'] ?? null;
174
+
175
+            if ($intMaxlength)
176
+            {
177
+                if ($dc->activeRecord->type == 'file')
178
+                {
179
+                    $intMaxlength -= (strlen($dc->activeRecord->extension) + 1);
180
+                }
181
+
182
+                foreach ($chunks as $chunk)
183
+                {
184
+                    if (mb_strlen($chunk) > $intMaxlength)
185
+                    {
186
+                        throw new Exception(sprintf($GLOBALS['TL_LANG']['ERR']['maxlength'], $GLOBALS['TL_DCA'][$dc->table]['fields'][$dc->field]['label'][0], $intMaxlength));
187
+                    }
188
+                }
189
+            }
190
+        }
191
+
192
+        $objSecFile = SecureDownloadsModel::findByPk($dc->id);
193
+        $objFile = $objSecFile->getRelated('uuid');
194
+        $File = new File($objFile->path,true);
195
+
196
+        if (!$File->exists())
197
+        {
198
+            throw new \Exception(sprintf($GLOBALS['TL_LANG']['ERR']['invalidFile'],'1'.$varValue));
199
+        }
200
+
201
+        $strDirectory = dirname($objFile->path);
202
+        if (!$File->renameTo($strDirectory.'/'.$varValue))
203
+        {
204
+            throw new \Exception(sprintf($GLOBALS['TL_LANG']['ERR']['invalidFile'],'2'.$varValue));
205
+        }
206
+
207
+        if (($objFile = $File->getModel()) !== null) {
208
+            $objSecFile = \SecureDownloadsModel::findByPk($dc->id);
209
+
210
+            if ($objSecFile !== null){
211
+                $objSecFile->uuid = $objFile->uuid;
212
+                $objSecFile->save();
213
+            }
214
+        }
215
+
216
+        return null;
217
+    }
42 218
 }
Browse code

Update

Benjamin Roth authored on26/07/2023 10:53:36
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,42 @@
1
+<?php
2
+
3
+declare(strict_types=1);
4
+
5
+/*
6
+ * This file is part of memberfiles bundle.
7
+ *
8
+ * (c) vonRotenberg
9
+ *
10
+ * @license commercial
11
+ */
12
+
13
+namespace VonRotenberg\MemberfilesBundle\EventListener\DataContainer;
14
+
15
+use Contao\BackendUser;
16
+use Contao\CoreBundle\ServiceAnnotation\Callback;
17
+use Contao\System;
18
+use Contao\Date;
19
+use Contao\Config;
20
+
21
+class MemberSecureDownloadsListener {
22
+
23
+    /**
24
+     * @var BackendUser
25
+     */
26
+    protected $User;
27
+    public function __construct()
28
+    {
29
+        $this->User = BackendUser::getInstance();
30
+    }
31
+
32
+    /**
33
+     * @Callback(table="tl_member_secureDownloads", target="list.sorting.child_record")
34
+     */
35
+    public function onChildRecordCallback(array $row)
36
+    {
37
+        $objSecFile = \SecureDownloadsModel::findByPk($row['id']);
38
+        $objFile = $objSecFile->getRelated('uuid');
39
+
40
+        return '<div class="tl_content_left">' . ($objFile !== null ? $objFile->name : '') . ' <span style="color:#b3b3b3;padding-left:3px">[' . Date::parse(Config::get('datimFormat'), $row['ctime']) . ']</span></div>';
41
+    }
42
+}