-
Notifications
You must be signed in to change notification settings - Fork 0
/
webfm.module
executable file
·3734 lines (3453 loc) · 136 KB
/
webfm.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* This module implements a user accessible heirarchical file system located
* in a subdirectory of the site file system path.
*/
module_load_include('inc', 'webfm', 'webfm_file');
define('WEBFM_FLUSH', -1);
define('WEBFM_REPLACE_RENAME', 0);
define('WEBFM_REPLACE_DELETE', 1);
define('WEBFM_RENAME_NEW', 2);
define('WEBFM_CANCEL', 3);
define('WEBFM_ADMIN', 1);
define('WEBFM_USER', 2);
define('WEBFM_ATTACH_VIEW', 3);
// file access bits
define('WEBFM_FILE_ACCESS_PUBLIC_VIEW', 1); //publicly viewable via webfm_send link
define('WEBFM_FILE_ACCESS_ROLE_VIEW', 2); //role can view/download file (owner/admin can always view)
define('WEBFM_FILE_ACCESS_ROLE_ATTACH', 4); //role attach permitted (owner/admin can always attach)
define('WEBFM_FILE_ACCESS_ROLE_FULL', 8); //role modification of file permitted (owner/admin can always modify)
define('WEBFM_MAX_FILE_ACCESS', WEBFM_FILE_ACCESS_PUBLIC_VIEW + WEBFM_FILE_ACCESS_ROLE_VIEW + WEBFM_FILE_ACCESS_ROLE_ATTACH + WEBFM_FILE_ACCESS_ROLE_FULL);
define('WEBFM_DATE_FORMAT_DAY', 1);
define('WEBFM_DATE_FORMAT_MONTH', 2);
/**
* Implementation of hook_help().
*/
function webfm_help($path, $args) {
switch ($path) {
case 'admin/help#webfm':
$output = t('<p>Web File Manager (WebFM) implements a hierarchical filesystem unlike the traditional flat filesystem used to date by Drupal. WebFM allows administrators to arrange files on the server in the same way they do on their local storage drives which greatly enhances the manageability of large collections of documents. Directory access is set by role and file access is controllable by file owners or module administrators.</p>
<p>WebFM uses Ajax to provide application intensive functionality such as drag-and-drop and context-sensitive menuing. JavaScript must be enabled for WebFM to function - functionality will not gracefully degrade for clients that have JavaScript disabled.</p>
<h2>Installation & Configuration</h2>
<ul>
<li>Download the official release from the <a href="http://drupal.org/project/webfm">project page</a>.</li>
<li>Unzip the archive and copy the \'webfm\' directory to your modules directory (ie:/sites/all/modules). Alternatively copy the tarball to the module directory if you can unzip it on the server.</li>
<li>Enable the module on Drupal\'s admin/build/modules page. An install file updates the database with the necessary table additions.</li>
<li>Set the <i>\'File system path:\'</i> at <i>.../admin/settings/file-system</i>. The WebFM root directory is always a sub-directory of this \'File System\' path. <i>\'Download method:\'</i> has no effect on the module since WebFM implements its own download security.</li>
<li>Configure the module at admin/settings/webfm.
<ul>
<li><b>Create the <i>\'WebFM root directory\'</i></b>. If this directory doesn\'t already exist, the system will create it in the \'File System\' root. Directory creation is not recursive so multi directory root paths must already exist inside the \'File System\' directory. Set the directory permissions to 775 if the server is linux/bsd.</li>
<li>The icon path allows the user to substitute their own gifs. File names are hardcoded in the javascript so the icons will have to have identical names.</li>
<li>The <i>\'Maximum resolution for uploaded images\'</i> input functions in the same fashion as the upload.module.</li>
<li>The <i>\'Date Format\'</i> radio buttons set the day/month order in the browser listing date field.</li>
<li>The <i>\'Display metadata title\'</i> checkbox sets the browser to display metadata titles rather than the actual filename if the metadata tile exists. Renaming files that use the metadata title must be done via the metadata editor. Note that node attachments always display the metadata title if available.</li>
<li><b><i>Default File Permissions</i></b> set the file level permissions for files inserted into the database. The exception is file uploads that create a version overwrite whereby the new file inherits the permissions from the previous file.</li>
<li><b><i>Settings for xxx role</i></b> fieldsets are added for roles that are granted the <i>\'access webfm\'</i> permission. Each role receives additional configuration fields for root path, extension white list, max upload file size and max total upload size. Roles with the \'access webfm\' right but without a root directory cannot access the filesystem.</li>
<li><b><i>WebFM attachments</i></b> controls attachment of files to node content as well as format. <br /><b>Note:</b> Each content type that will be using WebFM attachments must enable attachments in the Workflow section of <i>.../admin/settings/content-types/*type*</i> (default is \'WebFM attachments\' disabled).</li>
<li><b><i>IE Drag-and-Drop Normalization</i></b> is a sub-optimal solution for compensating for relative positioning in theme css. This feature is only available to #1 user.</li>
<li><b><i>WebFM debug</i></b> contains a <i>\'Webfm javascript debug\'</i> checkbox which is only useful for users interested in looking under the covers or who want to develop the module. The <i>\'WebFM cron\'</i> is a "stored procedure" used for database cleanup of file records that are deleted outside of the WebFM interface (ie: OS shell, ftp). This feature is only available to #1 user.</li>
</ul>
</li>
<li>Set WebFM rights in !permissionslink per role.
<ul>
<li><b><i>administer webfm</i></b> confers full rights to a role. Admins can see and operate on all files, including files not in the database. Only admins can create directories and access admin/settings/webfm.</li>
<li><b><i>access webfm</i></b> allows a role to download/view files via the WebFM browser. Only files referenced by the webfm_file table in the database are accessible. Only owners of a file (and admins) can move a file or modify it\'s metadata.<br /><b>Note:</b> Each role granted <i>\'access webfm\'</i> will receive a separate fieldset in the module settings where a root directory must be created to permit access to the browser.</li>
<li><b><i>view webfm attachments</i></b> allows a role to see files attached to nodes via WebFM.</li>
<li><b><i>webfm upload</i></b> allows a role with the <i>\'access webfm\'</i> right to upload files via the WebFM browser. The user who uploads a file is the the owner of that file.</li>
</ul>
</li>
<li>Admins and File owners can set the following file level permissions from the context menu of each file:
<ul>
<li><b><i>Public download</i></b>: Allows the file to be downloaded anonymously even if .htaccess exists.</li>
<li><b><i>Role View/Download</i></b>: Allows users of the same role to view/download the file.</li>
<li><b><i>Role Attach</i></b>: Allows users of the same role to attach the file to nodes.</li>
<li><b><i>Role Full Access</i></b>: Allows users of the same role to have the same rights to the file as the owner with the exception of permission edits.</li>
</ul>
</li>
<li>A .htaccess file (apache servers) can be placed in the WebFM root (or sub-path) to secure file access. Webfm streams downloads and thus your browser doesn\'t require direct http access to the directories</li>
<li>Updating the menu cache by navigating to admin/build/menu may be necessary if upgrading from an earlier version of the module with different internal paths.</li>
<li>Translations of the module require revising the string array at the top of webfm.js.</li>
</ul>
<h2>File Access Setup & Examples</h2>
<p>There are many ways to setup a file system hierarchy. The rules of any given system must be applied carefully if security of data is important.</p>
<p>The basic rules for users in a role with <i>\'access webfm\'</i> rights:</p>
<ul>
<li>The role root directory defines the domain and all subdirectories are accessible to the user.</li>
<li>The user cannot navigate above the role root directory.</li>
<li>Only files in the webfm_file table are accessible. Files uploaded by the user are owned by the user and are automatically in the database. Only module admins can view/operate on files not in the database.</li>
<li>The user has full control over files that he/she owns that stay within an accessible role root domain. File permissions can be locked down so that only the owner/admins can see or operate on a file. File permissions can be opened up so that anyone within the role can view or operate on the file.</li>
<li>Users with <i>\'access webfm\'</i> rights cannot create/delete/move/rename directories. Only module administrators (users with \'administer webfm\' permission or #1 user) can control the directory structure.</li>
</ul>
<p>Roles with <i>\'access webfm\'</i> rights can be subsets of other roles with <i>\'access webfm\'</i> rights or they can be exclusive. Users can be members of multiple roles and will consequently have a separate left-hand tree for each unique root directory (roles can even share the same root directory). It is difficult to foresee how diverse users of the module will choose to set up their systems but the following simple examples are typical arrangements. Both examples presume that the <i>drupal file-system directory</i> is set to \'files\', the WebFM module is installed and the <i>\'WebFM root directory\'</i> is set to \'webfm\'.</p>
<h3> Example 1</h3>
<p>The site requires 1 class of privileged users (A) to administer the file system and 2 classes of WebFM users (B & C) with access to file resources. Both roles will be able to upload files. Some WebFM users are members of both B & C while others are members of only one. Uploaded files are by default only accessible by the file owner and admins.</p>
<ul>
<li>A site administrator will create 3 the roles A, B and C. Role A will have the <i>\'administer webfm\'</i> permission set in !permissionslink. B & C will have the <i>\'access webfm\'</i> and the <i>\'webfm upload\'</i> permission set.</li>
<li>WebFM settings will now have a fieldset for roles B & C where the root directory for each role is set. The root of B is set to \'B\' which automatically creates the \'files/webfm/B\' directory. The root of C is set to \'C\' which creates the directory \'files/webfm/C\'. A user who is a member of only one of B or C will see a single left-hand directory tree that contains their domain. They will have no access to files within the other role domain. Users who are members of both B & C will have two left-hand directory trees and have the ability to move files they own or control between the two domains. Role A\'s root directory is the \'WebFM root directory\' and thus A users see only a single left-hand tree of the entire module file-sys.</li>
<li>In WebFM settings, the <i>\'Default File Permissions\'</i> are configured with all checkboxes unset. This combination of default file permissions means that files that are uploaded will initially only be viewable by the B or C user doing the upload (owner) and by A users. Individual file permissions are editable by the file owner or A user to permit other users to view/attach/modify the file. One consequence of granting the permission <i>\'Role Full Access\'</i> is that a non-admin user with a single domain could lose contact with their own file if a dual domain non-admin user moves it to the other domain.</li>
</ul>
<h3> Example 2</h3>
<p>The site requires 1 class of privileged users (A) to administer the file system and 2 classes of users (B & C) with access to file resources. C is determined to be a subset of B such that B can access it\'s own files as well as those of C. C will not be able to upload files to the browser but will only be able to view/download or attach files to nodes. B will be able to upload files.</p>
<ul>
<li>A site administrator will create 3 the roles A, B and C. Role A will have the <i>\'administer webfm\'</i> permission set in !permissionslink. B & C will have the <i>\'access webfm\'</i> permission set. B will also have the <i>\'webfm upload\'</i> permission set.</li>
<li>WebFM settings will now have a fieldset for roles B & C where the root directory for each role is set. First the root of B is set to \'B\' which automatically creates the \'files/webfm/B\' directory. Next the root of C is set to \'B/C\' which creates the directory \'files/webfm/B/C\'. Since C is a sub-dir of B, role B will have access to C but C will not be able to navigate above it\'s root to see B\'s files. The left-hand directory tree will appear different for B & C. B\'s tree will start at \'B\' and have a \'C\' sub-directory (and potentially other sub-directories as set up by A). C\'s tree is a subset of B\'s tree. Role A\'s root directory is the <i>\'WebFM root directory\'</i>.</li>
<li>In WebFM settings, the <i>\'Default File Permissions\'</i> are configured with <i>\'Role View Access\'</i> and <i>\'Role Attach Access\'</i> set. This combination of file permissions means that files that a B user uploads/moves into the C realm will by default be viewable by C and be attachable to nodes that C creates. A B file owner can manually modify the file permissions of each individual file to hide it or prevent it from being attached to content by a C user. Likewise the file permissions can be opened so that a C user can edit file attributes or move the file into another sub-directory of C.</li>
</ul>
<p>In the above examples the site administrator may simply create the roles/access rules and then let an A user configure WebFM for B & C.</p>
<h2>Uninstall</h2>
<ol>
<li>Disable the module on the /admin/build/modules page</li>
<li>Click on the uninstall tab and select the module for removal. This will automatically drop the webfm_file and webfm_attach tables as well as all configuration variables. <i><strong>NOTE:</strong></i> This action will permanently discard all attachment and metedata information and cannot be undone. Execute the first step only if you wish to restore WebFM later without loss of data.</li>
</ol>
', array('!permissionslink' => l('admin/user/permissions', 'admin/user/permissions')));
return $output;
case 'admin/modules#description':
return t('Enable the Web File Manager.');
}
}
/**
* Implementation of hook_link().
*/
function webfm_link($type, $node = NULL, $teaser = FALSE) {
$links = array();
// Display a link with the number of attachments
if ($teaser && variable_get('webfm_attach_body', FALSE) && $type == 'node' && isset($node->webfm_files) && user_access('view webfm attachments')) {
if ($num_files = count($node->webfm_files)) {
$available_types = array_keys(array_filter(variable_get('webfm_attachments_links', array())));
if (in_array($node->type, $available_types)) {
$links['webfm_attachments'] = array(
'html' => TRUE,
'title' => theme('webfm_attachments_link', $node->webfm_files),
);
}
else {
$links['webfm_attachments'] = array(
'title' => format_plural($num_files, '1 attachment', '@count attachments'),
'href' => "node/$node->nid",
'attributes' => array('title' => t('Read full article to view attachments.')),
'fragment' => 'attachments'
);
}
}
}
return $links;
}
/**
* Implementation of hook_node_type().
*/
function webfm_node_type($op, $info) {
switch ($op) {
case 'delete':
variable_del('webfm_attach_'. $info->type);
break;
}
}
/**
* Implementation of hook_perm().
*/
function webfm_perm() {
return array('access webfm', 'view webfm attachments', 'administer webfm', 'webfm upload');
}
/**
* Implementation of hook_requirements().
*
* Webfm uses the fileinfo extension in order to check for the actual mimetype of the file.
*
* @param string $phase The phase in which hook_requirements is run - 'runtime': the runtime requirements are being checked and shown on the status report page
*
* @return array
*/
function webfm_requirements($phase) {
$requirements = array();
if ($phase == 'runtime') {
if (!extension_loaded('fileinfo') AND !function_exists('mime_content_type')) {
$requirements['webfm'] = array(
'title' => t('File Information Extension'),
'value' => t('Disabled'),
'description' => t('In order to check whether the uploaded file actually is of the type its file extension suggests. !Webfm uses the !phpfileinfo in order to do that.', array('!Webfm' => l('Web File Manager', 'admin/settings/webfm'), '!phpfileinfo' => l('php fileinfo extension', 'http://php.net/manual/en/book.fileinfo.php'))),
'severity' => REQUIREMENT_WARNING,
);
}
else {
$requirements['webfm'] = array(
'title' => t('File Information Extension'),
'value' => t('Enabled'),
'severity' => REQUIREMENT_OK,
);
}
}
return $requirements;
}
/**
* Implementation of hook_menu().
*/
function webfm_menu() {
$items = array();
$items['webfm'] = array(
'title' => 'Web File Manager',
'page callback' => 'webfm_main',
'access callback' => 'user_access',
'access arguments' => array('access webfm'),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/settings/webfm'] = array(
'title' => 'Web File Manager',
'description' => 'Configure root directories, default file permissions for uploads, upload size limits, attachments, permitted file extensions and formatting.',
'file' => 'webfm.admin.inc',
'page callback' => 'drupal_get_form',
'page arguments' => array('webfm_admin_settings'),
'access arguments' => array('administer webfm'),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/settings/webfm/webfm'] = array(
'title' => 'Settings',
'description' => 'Configure root directories, default file permissions for uploads, upload size limits, attachments, permitted file extensions and formatting.',
'file' => 'webfm.admin.inc',
'page callback' => 'drupal_get_form',
'page arguments' => array('webfm_admin_settings'),
'access arguments' => array('administer webfm'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/settings/webfm/webfm/webfm'] = array(
'title' => 'Settings',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/settings/webfm/permissions'] = array(
'title' => 'Permissions',
'description' => 'Configure root directories, default file permissions for uploads, upload size limits, attachments, permitted file extensions for the roles.',
'file' => 'webfm.admin.inc',
'page callback' => 'drupal_get_form',
'page arguments' => array('webfm_admin_settings_roles'),
'access arguments' => array('administer webfm'),
'weight' => 1,
'type' => MENU_LOCAL_TASK,
);
$items['admin/settings/webfm/permissions/permissions'] = array(
'title' => 'Permissions',
'description' => 'Configure root directories, default file permissions for uploads, upload size limits, attachments, permitted file extensions for the roles.',
'file' => 'webfm.admin.inc',
'page callback' => 'drupal_get_form',
'page arguments' => array('webfm_admin_settings_roles'),
'access arguments' => array('administer webfm'),
'weight' => -10,
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/settings/webfm/style'] = array(
'title' => 'Style',
'description' => 'Configure the WebFM display style.',
'file' => 'webfm.admin.inc',
'page callback' => 'drupal_get_form',
'page arguments' => array('webfm_admin_settings_style'),
'access arguments' => array('administer webfm'),
'weight' => 2,
'type' => MENU_LOCAL_TASK,
);
$items['webfm_js'] = array(
'title' => 'Web File Manager',
'page callback' => 'webfm_ajax',
'access callback' => 'user_access',
'access arguments' => array('access webfm'),
'type' => MENU_CALLBACK,
);
$items['webfm/upload'] = array(
'title' => 'Web File Manager',
'page callback' => 'webfm_upload',
'access callback' => 'user_access',
'access arguments' => array('access webfm'),
'type' => MENU_CALLBACK,
);
$items['webfm_send'] = array(
'title' => 'File Not Found',
'page callback' => 'webfm_send_file',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implementation of hook_block().
*/
function webfm_block($op = 'list', $delta = 0) {
if ($op == 'list') {
$blocks[0]['info'] = t('WebFM File Attachments');
return $blocks;
}
elseif ($op == 'view' && user_access('access content') && user_access('view webfm attachments')) {
$block['content'] = webfm_attach_box();
$block['subject'] = t('Attachments');
return $block;
}
}
/**
* Implementation of hook_cron().
*/
function webfm_cron() {
//cleanup any corrupted file records that have no physical files
//Warning - running this after renaming a directory outside of WebFM will
// delete all file records contained in that directory
if (variable_get('webfm_cron', '')) {
$result = db_query('SELECT fpath, fid FROM {webfm_file}');
while ($f = db_fetch_array($result)) {
if (!(is_file($f['fpath']))) {
_webfm_dbdelete_file_fid($f['fid']);
}
}
}
if (variable_get("webfm_cron_insert", '')) {
$err_arr[] = array();
$root_dir = file_directory_path() . webfm_get_root_path();
$result = webfm_insert_dir($root_dir, TRUE, $err_arr);
if ($result->errcnt)
watchdog('cron', 'WebFM file insertion errors.', $err_arr, WATCHDOG_ERROR);
if ($result->cnt)
watchdog('cron', 'WebFM inserted %num files.', array('%num' => $result->cnt), WATCHDOG_NOTICE);
}
}
/**
* Implementation of hook_comment().
*
* Similar to webfm_nodeapi, but for comments instead of nodes.
* Note: Enabling webfm in the edit form is done in form_alter. Keeping the
* attachments while preview or a failing form_validate is done by form_alter
* and webfm_ajax.
* webfm_comment is only needed to save the attachments to the table and
* for viewing and previewing comments.
* We don't need to initialize js in 'validate' here because a failing form
* validation re-initializes the whole node, including js.
*/
function webfm_comment(&$comment, $op) {
if (is_object($comment)) {
$cid = $comment->cid;
$nid = $comment->nid;
}
else {
$cid = $comment['cid'];
$nid = $comment['nid'];
}
// We need the parent node for checking its permission to view attachments etc.
$node = node_load($nid);
switch ($op) {
case 'view':
if (variable_get("webfm_attach_$node->type", 1) == 1 && user_access('view webfm attachments') && variable_get('webfm_attach_body', FALSE)) {
// If we preview a comment, $comment->preview is defined because the preview button
// for comments is added with $form['preview']. Thus we know that $_POST['attachlist'
// is ours. If we are previewing another comment and just "view" this one,
// the preview flag is not set and $_POST['attachlist'] belongs to someone else.
// This happens when previewing or editing a comment and the node and/or
// other comments might be shown, too.
// If we preview without 'acces webfm' permissions, we fetch from database,
// cf. nodeapi below.
if ($comment->preview && user_access('access webfm')) {
if ($_POST['attachlist']) {
$show_files = webfm_get_temp_attachments($_POST['attachlist']);
}
}
else {
// Normal view. Try to load attachments. There is no 'load' op for hook_comment.
if (!isset($comment->webfm_files)) {
$comment->webfm_files = webfm_get_attachments($cid, 'cid');
}
if (is_array($comment->webfm_files) && count($comment->webfm_files)) {
$show_files = $comment->webfm_files;
}
}
if ($show_files) {
$comment->comment .= theme('webfm_attachments', $show_files);
drupal_add_css(drupal_get_path('module', 'webfm') . '/css/webfm.css');
}
}
break;
case 'insert':
if ($_POST['attachlist']) {
$files = explode(',', $_POST['attachlist']);
array_walk($files, 'intval'); //Don't trust your Inputs. Forcing all values to int.
$i = 0;
foreach ($files as $fid) {
if ($fid)
// weight argument determined by position in csv
webfm_dbinsert_attach(0, $fid, $i++, $cid);
}
}
break;
case 'update':
// If the user cannot access webfm, $_POST['attachlist'] is always empty
// and therefore will delete existing attachments from the node.
if (user_access('access webfm') && array_key_exists('attachlist', $_POST)) {
$files = explode(',', $_POST['attachlist']);
array_walk($files, 'intval');//Don't trust your Inputs. Forcing all values to int.
webfm_dbupdate_attach(0, $files, $cid);
}
break;
case 'delete':
webfm_dbdelete_attachments($cid, 'cid');
break;
}
}
/**
* Implementation of hook_nodeapi().
*/
function webfm_nodeapi(&$node, $op, $teaser) {
global $user, $base_url, $base_path;
switch ($op) {
case 'load':
if ((variable_get("webfm_attach_$node->type", 1) == 1) &&
user_access('view webfm attachments')) {
$output['webfm_files'] = webfm_get_attachments($node->nid);
return $output;
}
break;
case 'view':
// Add the attachments list to node body if configured to appear in body.
if (variable_get('webfm_attach_body', FALSE)) {
// We could be viewing or previewing this node.
// Loading a node defines $node->webfm_files, possibly as empty list,
// but loading an edit form unsets $node->webfm_files again. Thus, if
// $node->webfm is not set, we are previewing this node.
// Two cases then:
// 1) The user has no webfm access rights, so he can't change anything
// and $_POST['attachlist'] is empty. Simulate the 'load' operation
// to get the files from the database to show them in the preview.
// 2) If user has webfm access, all attachments (saved/unsaved)
// are in $_POST['attachlist']. But $_POST['attachlist'] is also set if
// we preview e.g. a comment where this node is shown, too, but then
// the attachlist is not ours. So we check for webfm_files *before*
// checking for $_POST to get around this.
if (!user_access('access webfm') && (variable_get("webfm_attach_$node->type", 1) == 1) && user_access('view webfm attachments')) {
$node->webfm_files = webfm_get_attachments($node->nid);
}
if (isset($node->webfm_files) && is_array($node->webfm_files)) {
if (count($node->webfm_files) && !$teaser) {
$show_files = $node->webfm_files;
}
}
// We must check for view permissions in a preview, but webfm_attach__$node->type
// is TRUE if we already have attachments here.
elseif (isset($_POST['attachlist']) && user_access('view webfm attachments')) {
$show_files = webfm_get_temp_attachments($_POST['attachlist']);
}
if (isset($show_files) && is_array($show_files)) {
$node->content['webfm_attachments'] = array(
'#value' => theme('webfm_attachments', $show_files),
'#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'webfm_attachments') : 15,
);
drupal_add_css(drupal_get_path('module', 'webfm') . '/css/webfm.css');
}
}
break;
case 'validate':
// When form_validate fails for preview or save, we must reinitialize
// javascript, otherwise webfm doesn't work anymore.
$modulepath = drupal_get_path('module', 'webfm');
drupal_add_js($modulepath .'/js/webfm.js');
drupal_add_css($modulepath .'/css/webfm.css');
// Output drupal config data as inline javascript
$clean_url = variable_get('clean_url', 0);
$clean = (($clean_url == 0) || ($clean_url == '0')) ? FALSE : TRUE;
webfm_inline_js($base_url, $base_path, $clean, $user->uid);
break;
case 'insert':
// We saved the attachment list for preview. Remove before saving.
unset($node->attachlist);
if ($_POST['attachlist']) {
$files = explode(',', $_POST['attachlist']);
array_walk($files, 'intval');//Don't trust your Inputs. Forcing all values to int.
$i = 0;
foreach ($files as $fid) {
if ($fid)
// weight argument determined by position in csv
webfm_dbinsert_attach($node->nid, $fid, $i++);
}
}
if (module_exists('og') && variable_get('webfm_og_auto', 0) == 1) {
if (og_is_group_type($node->type)) {
// make the node title into a suitable directory name
$group_directory = webfm_get_group_directory($node);
$group_root_dir = file_directory_path() . webfm_get_root_path() . '/' . $group_directory;
file_check_directory($group_root_dir, FILE_CREATE_DIRECTORY, 'root_dir_group_' . $node->nid);
variable_set('root_dir_group_' . $node->nid, $group_directory);
}
}
break;
case 'update':
// If the user cannot access webfm, $_POST['attachlist'] is always empty
// and therefore will delete existing attachments from the node.
if (user_access('access webfm') && array_key_exists('attachlist', $_POST)) {
$files = explode(',', $_POST['attachlist']);
array_walk($files, 'intval');//Don't trust your Inputs. Forcing all values to int.
webfm_dbupdate_attach($node->nid, $files);
//if i18nsync is not there this will just return an empty array
$i18nsync_nodeapi = variable_get('i18nsync_nodeapi_' . $node->type, array());
//If this field is set to syncing we keep the POST Data othrwise we drop it
if (!in_array('webfm_attachments_sync', $i18nsync_nodeapi)) {
unset($_POST['attachlist']);
}
}
break;
case 'delete':
webfm_dbdelete_attachments($node->nid);
break;
}
}
/**
*
* Function adds the checkbox the the i18nsync-fieldlist in the form alter.
*
*
* @param array $fields fields provided by i18n
* @param string $type nodetype
*/
function webfm_i18nsync_fields_alter(&$fields, $type) {
$fields['node']['#options']['webfm_attachments_sync'] = t('Web file Manger attached files');
}
/**
* Implementation of hook_content_extra_fields().
*/
function webfm_content_extra_fields($type) {
$extras['webfm_attachments'] = array(
'label' => t('Webfm Attachments'),
'description' => t('Displays the attachments as table'),
'weight' => 15,
);
return $extras;
}
/**
* Implementation of hook_form_alter().
*/
function webfm_form_alter(&$form, &$form_state, $form_id) {
global $base_url, $base_path, $user;
if (($user->uid == 1) || user_access('administer webfm') || user_access('access webfm'))
$access = TRUE;
else
$access = FALSE;
if ($form_id == 'node_type_form' && $access) {
$form['workflow']['webfm_attach'] = array(
'#type' => 'radios',
'#title' => t('WebFM Attachments'),
'#default_value' => variable_get('webfm_attach_' . $form['#node_type']->type, 0),
'#options' => array(0 => t('Disabled'), 1 => t('Enabled')),
'#description' => t('Should this content type allow upload & file attachment via WebFM?'),
);
}
if (isset($form['type']) || $form_id == 'comment_form') {
// For a comment form, the webfm permissions are inherited from the comments node.
if ($form_id == 'comment_form') {
$node = node_load($form['nid']['#value']);
$formcheck = TRUE;
}
else {
$node = $form['#node'];
$formcheck = ($form['type']['#value'] . '_node_form' == $form_id);
}
if ($access && $formcheck && variable_get('webfm_attach_'. $node->type, 0)) {
$modulepath = drupal_get_path('module', 'webfm');
drupal_add_js($modulepath . '/js/webfm.js');
drupal_add_css($modulepath . '/css/webfm.css');
// Output drupal config data as inline javascript
$clean_url = variable_get('clean_url', 0);
$clean = (($clean_url == 0) || ($clean_url == '0')) ? FALSE : TRUE;
webfm_inline_js($base_url, $base_path, $clean, $user->uid);
$msg_webfm_attach_attach_browser = t('Use this file browser to attach files to the current node. Files that you choose to be attached will be shown in the list of attached files above this field.');
if (variable_get('webfm_attach_body', FALSE)) {
$msg_webfm_attach_attach_browser .= ' ' . t('Once this post is saved the chosen files will be shown attached to the node body.');
}
// Attachments fieldset
$form['webfm-attach']['#theme'] = 'webfm_upload_form';
$form['webfm-attach']['#weight'] = module_exists('content') ? content_extra_field_weight($node->type, 'webfm_attachments') : 15;
$form['webfm-attach']['attach'] = array(
'#type' => 'fieldset',
'#title' => t('WebFM Attachments'),
'#description' => t('Drag attachments to set order.<br />Changes made to the attachments are not permanent until you save this post.'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 29,
);
$form['webfm-attach']['attach']['attachedfiles'] = array(
'#prefix' => '<div id="webfm-attach">',
'#suffix' => '</div>',
);
$form['webfm-attach']['attach']['attachedfiles'] += webfm_attach_attached_form($node);
$form['webfm-attach']['attach']['browser'] = array(
'#type' => 'fieldset',
'#title' => t('File Browser'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => $msg_webfm_attach_attach_browser,
);
if ($user->uid == 1 ||
user_access('administer webfm')) {
$form['webfm-attach']['attach']['browser'] += webfm_links();
}
if ($user->uid == 1 ||
user_access('administer webfm') ||
user_access('webfm upload')) {
$form['webfm-attach']['attach']['browser']['wrapper'] = array(
'#type' => 'fieldset',
'#title' => t('File Upload'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t('Uploaded file will be saved to the current directory.'),
'#prefix' => '<div id="webfm-inline">',
'#suffix' => '</div>'
);
$form['webfm-attach']['attach']['browser']['wrapper']['wrapper'] = array(
'#prefix' => '<div id="wfmatt-wrapper">',
'#suffix' => '</div>');
$form['webfm-attach']['attach']['browser']['wrapper']['wrapper'] += webfm_upload_form('webfm/upload');
$form['#attributes']['enctype'] = 'multipart/form-data';
}
else {
// Disable upload
$form['webfm-attach']['attach']['browser']['wrapper'] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#prefix' => '<div id="webfm-inline">',
'#suffix' => '</div>'
);
}
}
}
}
/**
* Creates the directory name for og-group based directories
*
* @param object $node node-object
* @return string Directory name
*/
function webfm_get_group_directory($node) {
$group_directory = drupal_strtolower(trim($node->title));
$group_directory = str_replace(array(' ', '-'), '_', $group_directory);
$group_directory .= '_g'. $node->nid; // Add node id to ensure unique name
return preg_replace('/[^a-z0-9_]/', '', $group_directory);
}
function webfm_link_output() {
global $user;
$msg = '';
$msg .= '<p><div class="description">';
$msg .= t('Right-click (Opera: Alt+Left) on files or folders opens the context menu.');
$msg .= '</div>';
if ($user->uid == 1 || user_access('administer webfm')) {
// Settings link - accessible only #1 user or module administrator
$settings_link = t('[<a href="@link">settings...</a>]', array('@link' => url("admin/settings/webfm")));
// Admin Help link - available only if help module enabled
$help_link = module_hook('help', 'help') ? t('[<a href="@link">more help...</a>]', array('@link' => url("admin/help/webfm"))) : '';
}
else {
$settings_link = '';
$help_link = '';
}
// Debug link - available only if enabled in settings
$debug_link = (drupal_to_js(variable_get('webfm_debug', ''))) ? t('[<a href=# id="webfm-debug-link">debug</a>]') : '';
return $msg . $debug_link . $settings_link . $help_link;
}
function webfm_links() {
$form['#theme'] = 'webfm_attach_attached_form';
$output = webfm_link_output();
if ($output) {
$form['links'] = array(
'#prefix' => '<div class="more-help-link">',
'#suffix' => '</div>'
);
$form['links']['content'] = array(
'#type' => 'markup',
'#value' => $output
);
}
return $form;
}
function webfm_attach_attached_form($node) {
$form['#theme'] = 'webfm_attach_attached_form';
// This form input (id = edit-attachlist) will hold the comma-separated
// ordered list of attached fids. We need to store the attachments (which
// might not yet been saved to the database) in two cases:
// 1) A form_validate fails when trying to save/preview. The form is not
// rebuild by form_alter, but form values are refilled. #value => '' would
// delete the attachlist collected so far, so use #default_value => '' instead.
$form['new']['attachlist'] = array(
'#type' => 'hidden',
'#default_value' => '');
// 2) If form_validate didn't fail, the form is rebuild with form_alter
// and the value is reset. But we have the list in the POST parameters.
// Use default_value here, too, so that it can still be changed if we get
// a form_validate error *after* a successful preview.
if ($_POST['attachlist']) {
$attachlist = explode(',', $_POST['attachlist']);
array_walk($attachlist, 'intval');//Don't trust your Inputs. Forcing all values to int.
$attachlist = implode(',', $attachlist);
$form['new']['attachlist']['#default_value'] = $attachlist;
}
elseif (isset($node->attachlist) and empty($node->attachlist)) {
// this case applies when we are previewing but no attachments are left in the list
// we then set this to a space because the value get reset to the original value
// if the default_value is empty
$form['new']['attachlist']['#default_value'] = ' ';
}
return $form;
}
/**
* Theme the attachment form.
* Note: required to output prefix/suffix.
*/
function theme_webfm_attach_attached_form($form) {
$output = drupal_render($form);
return $output;
}
/**
* Get and theme node attachments
*/
function webfm_attach_box() {
if ($node = menu_get_object()) {
$files = webfm_get_attachments($node->nid);
return theme('webfm_attachments', $files);
}
}
/**
* Implementation of hook_theme().
*
* @return array
*/
function webfm_theme() {
return array(
'webfm_browser' => array(
'arguments' => array('links' => NULL, 'upload' => NULL),
'template' => 'webfm-browser',
),
'webfm_attachments' => array(
'file' => 'webfm_theme.inc',
'arguments' => array('files'),
),
'webfm_attachments_link' => array(
'file' => 'webfm_theme.inc',
'arguments' => array('files'),
),
);
}
/**
* Helper func to associate the listing icon with the mime type
*/
function _webfm_get_icon($ext) {
// Try and find appropriate type
switch (drupal_strtolower($ext)) {
case 'image/gif':
case 'image/png':
case 'image/jpg':
case 'image/jpeg':
case 'image/bmp':
case 'image/tiff':
case 'jpg':
case 'gif':
case 'png':
case 'jpeg':
case 'bmp':
case 'tiff':
$icon = 'i.gif';
break;
case 'video/mpeg':
case 'video/x-msvideo':
case 'avi':
$icon = 'avi.gif';
break;
case 'video/quicktime':
case 'mov':
$icon = 'qt.gif';
break;
case 'audio/mpeg':
case 'mpeg':
case 'mp3':
$icon = 'mp3.gif';
break;
case 'application/pdf':
case 'pdf':
$icon = 'pdf.gif';
break;
case 'application/zip':
case 'application/x-zip':
case 'application/x-gzip':
case 'zip':
$icon = 'zip.gif';
break;
case 'application/msword':
case 'doc':
case 'odt':
$icon = 'doc.gif';
break;
case 'application/vnd.ms-excel':
case 'xls':
case 'ods':
$icon = 'xls.gif';
break;
case 'application/vnd.ms-powerpoint':
case 'pps':
case 'ppt':
case 'odp':
$icon = 'pps.gif';
break;
default:
$icon = 'f.gif';
break;
}
return $icon;
}
function webfm_roles_alter($rid, $name, $op) {
if ($op == t('Save role')) {
// db_query("UPDATE {role} SET name = '%s' WHERE rid = %d", $form_values['name'], $form_values['rid']);{
drupal_set_message(t('The webfm role has been renamed.'));
}
elseif ($op == t('Delete role')) {
// db_query('DELETE FROM {role} WHERE rid = %d', $form_values['rid']);
// db_query('DELETE FROM {permission} WHERE rid = %d', $form_values['rid']);
// Update the users who have this role set:
// db_query('DELETE FROM {users_roles} WHERE rid = %d', $form_values['rid']);{
drupal_set_message(t('The webfm role has been deleted.'));
}
elseif ($op == t('Add role')) {
// db_query("INSERT INTO {role} (name) VALUES ('%s')", $form_values['name']);
drupal_set_message(t('The webfm role has been added.'));
}
}
/**
* Check an upload, if it is an image, make sure it fits within the
* maximum dimensions allowed.
*/
function _webfm_image(&$file) {
$info = image_get_info($file->filepath);
if ($info) {
$res = variable_get('webfm_max_resolution', 0);
if ($res != 0) {
list($width, $height) = explode('x', drupal_strtolower($res));
if ($info['width'] > $width || $info['height'] > $height) {
// Try to resize the image to fit the dimensions.
if (image_get_toolkit() && image_scale($file->filepath, $file->filepath, $width, $height)) {
drupal_set_message(t('The image was resized to fit within the maximum allowed resolution of %resolution pixels.', array('%resolution' => variable_get('webfm_max_resolution', 0))));
// Clear the cached filesize and refresh the image information.
clearstatcache();
$info = image_get_info($file->filepath);
$file->filesize = $info['file_size'];
}
else {
drupal_set_message(t('The image is too large.'));
}
}
}
}
}
/**
* Called by upload form submit
*/
function webfm_upload() {
global $user;
//Get the destination path from the edit-webfmuploadpath hidden field in the upload form
$json_data = array();
$fid = '';
if (isset($_POST['webfmuploadpath'])) {
$root_dir = (($user->uid == 1) || user_access('administer webfm'))? file_directory_path() : file_directory_path() . webfm_get_root_path();
$dest = $root_dir . $_POST['webfmuploadpath'];
// Save new file uploads to tmp dir.
if (($file = file_save_upload('webfm_upload')) != FALSE) {
// Scale image uploads.
_webfm_image($file);
$err_arr = array();
if (webfm_upload_validate($file, $err_arr) === TRUE) {
// file has been put in temp and we have a valid file object
// Cache filepath as $_SESSION var for ajax confirm when name conflict
// positive response will swap fid and munged name
$upload_path = $dest . '/' . $file->filename;
if (is_file($upload_path)) {
$file->in_db = ($webfm_file_row = webfm_get_file_record('', $upload_path)) ? TRUE : FALSE;
$file->dest = $dest;
$_SESSION['temp_upload'] = $file;
$json_data['file'] = $file->filename;
if ($file->in_db) {
// Overwrite of an existing file that is in the database
// Only admins, file owners or files marked as modifiable by role can be overwritten
if (($webfm_file_row->uid == $user->uid) ||
user_access('administer webfm') ||
webfm_file_mod_access($webfm_file_row)) {
$json_data['html'] = webfm_reload_upload('webfm/upload', webfm_version_form($file->filename));
}
else {
drupal_set_message(t('Permission denied to overwrite existing file'), 'error');
}
}
else {
// Overwrite of an existing file that is not in the database
$msg = '';
if (($fid = webfm_version_upload(WEBFM_REPLACE_RENAME, $msg)) !== FALSE) {
// file was inserted into the database
drupal_set_message(check_plain($msg));
}
else {
drupal_set_message(check_plain($msg), 'error');
}
}
}
elseif (file_move($file, $dest)) {
// file was moved to its final destination
// Insert file into database
if (($fid = webfm_dbinsert_file($file, $db_err)) !== FALSE) {
drupal_set_message(t('Upload Success'));
}
else {
file_delete($file->filepath);
drupal_set_message(check_plain($db_err), 'error');
}
}
else {
drupal_set_message(t('file_move to %path failed', array('%path' => $dest)), 'error');
}
}
else {
if (is_file($file->filepath)) {
file_delete($file->filepath);
}
foreach ($err_arr as $err) {
drupal_set_message(check_plain($err), 'error');
}
}
}
else {
if (!isset($_FILES['files']) || $_FILES['files']['name']['webfm_upload'] == '') {
drupal_set_message(t('Please click "Browse" and select a file to upload before clicking the upload button.'), 'error');
}
}
}
else {
drupal_set_message(t('Invalid upload path'), 'error');
}
if (!isset($json_data['html']))
$json_data['html'] = webfm_reload_upload('webfm/upload');
if ($fid)
$json_data['fid'] = $fid;
print drupal_to_js(array('status' => TRUE, 'data' => $json_data));
exit();
}
/**
* Rebuild upload form for upload iframe
*/
function webfm_reload_upload($url, $confirm_form = '') {
$form = array();