forked from stetard/DNN.Repository
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Helpers.cs
1444 lines (1240 loc) · 52.3 KB
/
Helpers.cs
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
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
//
// DotNetNuke® - http://www.dotnetnuke.com
// Copyright (c) 2002-2005
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System.IO;
using System.Xml;
using System.Web;
using System.Web.UI.WebControls;
using DotNetNuke;
using DotNetNuke.Entities.Users;
using DotNetNuke.Security;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.FileSystem;
namespace DotNetNuke.Modules.Repository
{
public class Helpers
{
#region "Private Members"
private int _PortalID;
private int _ModuleID;
private string _LocalResourceFile;
#endregion
#region "Constructor"
public Helpers() : base()
{
}
#endregion
#region "Public Properties"
public string LocalResourceFile {
get { return _LocalResourceFile; }
set {
if (object.ReferenceEquals(_LocalResourceFile, value)) {
return;
}
_LocalResourceFile = value;
}
}
#endregion
#region "Public Members"
public int NOT_APPROVED = 0;
public int IS_APPROVED = 1;
public int BEING_MODERATED = 2;
public int g_CategoryId = -1;
public string g_Attributes = "";
public string g_ApprovedFolder;
public string g_UnApprovedFolder;
public string g_AnonymousFolder;
public bool g_UserFolders;
public string g_ObjectCategories;
public string g_ObjectValues;
#endregion
#region "Public Functions and Subs"
public string ConvertFileIDtoPath(int pid, int fid)
{
var file = FileManager.Instance.GetFile(fid);
return file.PhysicalPath;
}
public string ConvertFileIDtoFileName(int pid, int fid)
{
var file = FileManager.Instance.GetFile(fid);
return file.FileName;
}
public string ConvertFileIDtoExtension(int pid, int fid)
{
var file = FileManager.Instance.GetFile(fid);
return file.Extension;
}
public DotNetNuke.Services.FileSystem.FileInfo ConvertFileIDtoFile(int pid, int fid)
{
DotNetNuke.Services.FileSystem.FileInfo file = (DotNetNuke.Services.FileSystem.FileInfo)FileManager.Instance.GetFile(fid);
return file;
}
public string FormatText(string strHTML)
{
string strText = strHTML;
strText = Strings.Replace(strText, "<br>", ControlChars.Lf.ToString());
strText = Strings.Replace(strText, "<BR>", ControlChars.Lf.ToString());
strText = HttpContext.Current.Server.HtmlDecode(strText);
return strText;
}
public string FormatHTML(string strText)
{
string strHTML = strText;
strHTML = Strings.Replace(strHTML, Strings.Chr(13).ToString(), "");
strHTML = Strings.Replace(strHTML, ControlChars.Lf.ToString(), "<br>");
return strHTML;
}
public string UploadFiles(int PortalID, int ModuleID, string fileURL, string imageURL, RepositoryInfo pRepository, string strCategories, string strAttributes)
{
return UploadFiles(PortalID, ModuleID, fileURL, imageURL, null, null, pRepository, strCategories, strAttributes);
}
public string UploadFiles(int PortalID, int ModuleID, string fileURL, HttpPostedFile objImageFile, RepositoryInfo pRepository, string strCategories, string strAttributes)
{
return UploadFiles(PortalID, ModuleID, fileURL, "", null, objImageFile, pRepository, strCategories, strAttributes);
}
public string UploadFiles(int PortalID, int ModuleID, HttpPostedFile objFile, string imageURL, RepositoryInfo pRepository, string strCategories, string strAttributes)
{
return UploadFiles(PortalID, ModuleID, "", imageURL, objFile, null, pRepository, strCategories, strAttributes);
}
public string UploadFiles(int PortalID, int ModuleID, HttpPostedFile objFile, HttpPostedFile objImageFile, RepositoryInfo pRepository, string strCategories, string strAttributes)
{
return UploadFiles(PortalID, ModuleID, "", "", objFile, objImageFile, pRepository, strCategories, strAttributes);
}
public string UploadFiles(int PortalID, int ModuleID, string fileURL, string imageURL, HttpPostedFile objFile, HttpPostedFile objImageFile, RepositoryInfo pRepository, string strCategories, string strAttributes)
{
PortalController objPortalController = new PortalController();
string strMessage = "";
string strFileName = "";
string strExtension = "";
string strImageFileName = "";
string strImageExtension = "";
string strGUID = "";
string strTargetFolder = null;
HttpPostedFile t_File = null;
HttpPostedFile t_Image = null;
bool bIsFile = false;
bool bIsImageFile = false;
bool bIsValidFileTypes = true;
bool bRequiresApproval = true;
if (!DotNetNuke.Common.Utilities.Null.IsNull(objFile)) {
t_File = (HttpPostedFile)objFile;
bIsFile = true;
}
if (!DotNetNuke.Common.Utilities.Null.IsNull(objImageFile)) {
t_Image = (HttpPostedFile)objImageFile;
bIsImageFile = true;
}
_PortalID = PortalID;
_ModuleID = ModuleID;
// Obtain PortalSettings from Current Context
ModuleController mc = new ModuleController();
PortalSettings _portalSettings = (PortalSettings)HttpContext.Current.Items["PortalSettings"];
var moduleInfo = mc.GetModule(ModuleID);
var settings = moduleInfo.ModuleSettings;
strTargetFolder = GetTargetFolder(ModuleID, pRepository);
if (bIsFile) {
try {
if (!string.IsNullOrEmpty(objFile.FileName)) {
strFileName = strTargetFolder + Path.GetFileName(objFile.FileName);
strExtension = Strings.Replace(Path.GetExtension(strFileName), ".", "");
}
} catch (Exception ex) {
strFileName = "";
strExtension = "";
}
} else {
strFileName = fileURL;
strExtension = "html";
}
if (bIsImageFile) {
try {
if (!string.IsNullOrEmpty(objImageFile.FileName)) {
strImageFileName = strTargetFolder + Path.GetFileName(objImageFile.FileName);
strImageExtension = Strings.Replace(Path.GetExtension(strImageFileName), ".", "");
}
} catch (Exception ex) {
strImageFileName = "";
strImageExtension = "";
}
} else {
strImageFileName = imageURL;
if (imageURL.ToLower().StartsWith("fileid=")) {
strImageExtension = ConvertFileIDtoExtension(_portalSettings.PortalId, int.Parse(imageURL.Substring(7)));
} else {
strImageExtension = imageURL.Substring(imageURL.LastIndexOf(".") + 1);
}
}
// make sure that if there's an image being uploaded that it's only a JPG, GIF or PNG
if (!string.IsNullOrEmpty(strImageExtension)) {
if (strImageExtension.ToLower() != "jpg" & strImageExtension.ToLower() != "gif" & strImageExtension.ToLower() != "png" & strImageExtension.ToLower() != "swf") {
string skey = string.Format("{0} Is A Restricted File Type for Images. Valid File Types Include JPG, GIF, SWF and PNG<br>", objImageFile.FileName);
strMessage += string.Format("{0} {1}", DotNetNuke.Services.Localization.Localization.GetString("TheFile", this.LocalResourceFile), skey);
}
}
if (string.IsNullOrEmpty(strMessage)) {
int uploadSize = 0;
int fileuploadsize = 0;
if (bIsFile) {
try {
uploadSize += objFile.ContentLength;
} catch (Exception ex) {
}
fileuploadsize = objFile.ContentLength;
}
if (bIsImageFile) {
try {
uploadSize += objImageFile.ContentLength;
} catch (Exception ex) {
}
if (fileuploadsize == 0) {
fileuploadsize = objImageFile.ContentLength;
}
}
// Admin and Host uploads are automatically approved. User uploads must be approved by a moderator.
if (uploadSize > 0) {
var allowedExtensions = string.Join(",", DotNetNuke.Entities.Host.Host.AllowedExtensionWhitelist).ToUpper();
if (((((objPortalController.GetPortalSpaceUsedBytes(PortalID) + uploadSize) / 1000000) <= _portalSettings.HostSpace) | _portalSettings.HostSpace == 0) | (_portalSettings.ActiveTab.ParentId == _portalSettings.SuperTabId)) {
if (bIsFile && !allowedExtensions.Contains(strExtension.ToUpper())) {
bIsValidFileTypes = false;
}
if (bIsImageFile && !allowedExtensions.Contains(strImageExtension.ToUpper())) {
bIsValidFileTypes = false;
}
if (bIsValidFileTypes | _portalSettings.ActiveTab.ParentId == _portalSettings.SuperTabId) {
strGUID = Guid.NewGuid().ToString();
try {
if (bIsFile) {
if (!string.IsNullOrEmpty(strFileName)) {
strFileName = string.Format("{0}{1}.{2}", strFileName.Substring(0, strFileName.LastIndexOf(strExtension)), strGUID, strExtension);
if (File.Exists(strFileName)) {
File.SetAttributes(strFileName, FileAttributes.Normal);
File.Delete(strFileName);
}
objFile.SaveAs(strFileName);
}
}
if (bIsImageFile) {
if (!string.IsNullOrEmpty(strImageFileName)) {
strImageFileName = string.Format("{0}{1}.{2}", strImageFileName.Substring(0, strImageFileName.LastIndexOf(strImageExtension)), strGUID, strImageExtension);
if (File.Exists(strImageFileName)) {
File.SetAttributes(strImageFileName, FileAttributes.Normal);
File.Delete(strImageFileName);
}
objImageFile.SaveAs(strImageFileName);
}
}
} catch (Exception ex) {
// save error - can happen if the security settings are incorrect
strMessage += DotNetNuke.Services.Localization.Localization.GetString("SaveError", this.LocalResourceFile);
}
} else {
// restricted file type
strMessage += string.Format("{0} {1} {2}", DotNetNuke.Services.Localization.Localization.GetString("RestrictedFilePrefix", this.LocalResourceFile), string.Join(",", Entities.Host.Host.AllowedExtensionWhitelist), DotNetNuke.Services.Localization.Localization.GetString("RestrictedFileSuffix", this.LocalResourceFile));
}
// file too large
} else {
strMessage += DotNetNuke.Services.Localization.Localization.GetString("FileTooLarge", this.LocalResourceFile);
}
}
if (string.IsNullOrEmpty(strMessage)) {
// ok, now any physical files have been uploaded, so add the record to the Repository tables
// now add the info to the repository data store
string sType = "";
string sImageType = "";
if (bIsFile) {
try {
sType = objFile.ContentType;
} catch (Exception ex) {
sType = "";
}
}
if (bIsImageFile) {
try {
sImageType = objImageFile.ContentType;
} catch (Exception ex) {
sImageType = "";
}
}
// strMessage += this used to be added to strMessage but AddFile does not return any string...
AddFile(ModuleID, strFileName, strExtension, strImageFileName, strImageExtension, pRepository, sType, sImageType, strCategories, strAttributes,
fileuploadsize.ToString());
if (string.IsNullOrEmpty(strMessage)) {
// if no errors and the uploaded file needs to be approved, then send an email to the administrator
if (!IsTrusted(PortalID, ModuleID)) {
UserController objUsers = new UserController();
UserInfo objAdministrator = objUsers.GetUser(PortalID, _portalSettings.AdministratorId);
string strBody = "";
if ((objAdministrator != null)) {
strBody = objAdministrator.DisplayName + "," + Constants.vbCrLf + Constants.vbCrLf;
strBody = strBody + "A file has been uploaded/changed to " + _portalSettings.PortalName + " and is waiting for your Approval." + Constants.vbCrLf + Constants.vbCrLf;
//strBody = strBody + "Portal Website Address: " + DotNetNuke.Common.Globals.GetPortalDomainName(_portalSettings.PortalAlias.HTTPAlias, HttpContext.Current.Request) + Constants.vbCrLf;
strBody = strBody + "Portal Website Address: " + DotNetNuke.Common.Globals.GetPortalDomainName(_portalSettings.PortalAlias.HTTPAlias, HttpContext.Current.Request, false) + Constants.vbCrLf;
strBody = strBody + "Username: " + pRepository.Author + Constants.vbCrLf;
strBody = strBody + "User's email address: " + pRepository.AuthorEMail + Constants.vbCrLf;
strBody = strBody + "File Uploaded: " + strFileName + Constants.vbCrLf + Constants.vbCrLf;
DotNetNuke.Services.Mail.Mail.SendMail(_portalSettings.Email, _portalSettings.Email, "", "", DotNetNuke.Services.Mail.MailPriority.Normal, "ADMIN: A File is Awaiting your Approval at " + _portalSettings.PortalName, DotNetNuke.Services.Mail.MailFormat.Text, System.Text.Encoding.Default, strBody, "",
"", "", "", "");
}
}
}
}
}
return strMessage;
}
public string GetSkinAttribute(XmlDocument xDoc, string tag, string attrib, string defaultValue)
{
string retValue = defaultValue;
XmlNode xmlSkinAttributeRoot = xDoc.SelectSingleNode("descendant::Object[Token='[" + tag + "]']");
if ((xmlSkinAttributeRoot != null)) {
XmlNode xmlSkinAttribute = null;
foreach (XmlNode xmlSkinAttribute_loopVariable in xmlSkinAttributeRoot.SelectNodes(".//Settings/Setting")) {
xmlSkinAttribute = xmlSkinAttribute_loopVariable;
if (!string.IsNullOrEmpty(xmlSkinAttribute.SelectSingleNode("Value").InnerText)) {
if (xmlSkinAttribute.SelectSingleNode("Name").InnerText == attrib) {
retValue = xmlSkinAttribute.SelectSingleNode("Value").InnerText;
}
}
}
}
return retValue;
}
public void SetRepositoryFolders(int ModuleId)
{
// Obtain PortalSettings from Current Context
PortalSettings _portalSettings = (PortalSettings)HttpContext.Current.Items["PortalSettings"];
// Get settings from the database
var moduleController = new ModuleController();
var moduleInfo = moduleController.GetModule(ModuleId);
var settings = moduleInfo.ModuleSettings;
if (!string.IsNullOrEmpty(Convert.ToString(settings["userfolders"]))) {
g_UserFolders = bool.Parse(Convert.ToString(settings["userfolders"]));
} else {
g_UserFolders = true;
}
g_ApprovedFolder = HttpContext.Current.Server.MapPath(_portalSettings.HomeDirectory) + "Repository";
g_UnApprovedFolder = HttpContext.Current.Server.MapPath(_portalSettings.HomeDirectory) + "Repository\\Pending";
g_AnonymousFolder = HttpContext.Current.Server.MapPath(_portalSettings.HomeDirectory) + "Repository\\Anonymous";
// make sure the Repository folder exists
if (!Directory.Exists(g_ApprovedFolder)) {
// if not, create it.
Directory.CreateDirectory(g_ApprovedFolder);
// copy the noimage graphic to the newly created folder.
File.Copy(HttpContext.Current.Server.MapPath("~/DesktopModules/Repository/images/noimage.jpg"), g_ApprovedFolder + "noimage.jpg");
}
// and make sure the Pending folder exists
if (!Directory.Exists(g_UnApprovedFolder)) {
// if not, create it.
Directory.CreateDirectory(g_UnApprovedFolder);
}
// and make sure the Anonymous folder exists
if (!Directory.Exists(g_AnonymousFolder)) {
// if not, create it.
Directory.CreateDirectory(g_AnonymousFolder);
}
}
public string MoveRepositoryFiles(string oldFolder, string newFolder)
{
string strMessage = "";
strMessage = MoveFolder(oldFolder, newFolder);
return strMessage;
}
public string FormatImageURL(int ImageId, int moduleid)
{
string strImage = "";
PortalSettings _portalSettings = (PortalSettings)HttpContext.Current.Items["PortalSettings"];
if (ImageId > -1) {
strImage = (HttpContext.Current.Request.ApplicationPath == "/" ? HttpContext.Current.Request.ApplicationPath : HttpContext.Current.Request.ApplicationPath + "/") + "DesktopModules/Repository/MakeThumbnail.aspx?tabid=" + _portalSettings.ActiveTab.TabID + "&id=" + ImageId.ToString() + "&mid=" + moduleid.ToString();
}
return strImage;
}
public string FormatImageURL(string ImageName)
{
string strImage = "";
PortalSettings _portalSettings = (PortalSettings)HttpContext.Current.Items["PortalSettings"];
strImage = (HttpContext.Current.Request.ApplicationPath == "/" ? HttpContext.Current.Request.ApplicationPath : HttpContext.Current.Request.ApplicationPath + "/") + "DesktopModules/Repository/images/" + ImageName;
return strImage;
}
public string FormatIconURL(string FileExtension)
{
string strImage = "";
PortalSettings _portalSettings = (PortalSettings)HttpContext.Current.Items["PortalSettings"];
strImage = (HttpContext.Current.Request.ApplicationPath == "/" ? HttpContext.Current.Request.ApplicationPath : HttpContext.Current.Request.ApplicationPath + "/") + "DesktopModules/Repository/images/icons/" + FileExtension + ".gif";
if (!File.Exists(HttpContext.Current.Server.MapPath(strImage))) {
strImage = (HttpContext.Current.Request.ApplicationPath == "/" ? HttpContext.Current.Request.ApplicationPath : HttpContext.Current.Request.ApplicationPath + "/") + "DesktopModules/Repository/images/icons/" + FileExtension + ".png";
if (!File.Exists(HttpContext.Current.Server.MapPath(strImage))) {
strImage = (HttpContext.Current.Request.ApplicationPath == "/" ? HttpContext.Current.Request.ApplicationPath : HttpContext.Current.Request.ApplicationPath + "/") + "DesktopModules/Repository/images/icons/" + FileExtension + ".jpg";
if (!File.Exists(HttpContext.Current.Server.MapPath(strImage))) {
strImage = (HttpContext.Current.Request.ApplicationPath == "/" ? HttpContext.Current.Request.ApplicationPath : HttpContext.Current.Request.ApplicationPath + "/") + "DesktopModules/Repository/images/icons/unknown.png";
}
}
}
return strImage;
}
public string FormatNoImageURL(int moduleId)
{
PortalSettings _portalSettings = (PortalSettings)HttpContext.Current.Items["PortalSettings"];
string strImage = "";
strImage = (HttpContext.Current.Request.ApplicationPath == "/" ? HttpContext.Current.Request.ApplicationPath : HttpContext.Current.Request.ApplicationPath + "/") + "DesktopModules/Repository/MakeThumbnail.aspx?tabid=" + _portalSettings.ActiveTab.TabID + "&mid=" + moduleId.ToString();
return strImage;
}
public string FormatNoImageURL(int moduleId, int ImageId)
{
PortalSettings _portalSettings = (PortalSettings)HttpContext.Current.Items["PortalSettings"];
string strImage = "";
strImage = (HttpContext.Current.Request.ApplicationPath == "/" ? HttpContext.Current.Request.ApplicationPath : HttpContext.Current.Request.ApplicationPath + "/") + "DesktopModules/Repository/MakeThumbnail.aspx?tabid=" + _portalSettings.ActiveTab.TabID + "&mid=" + moduleId.ToString() + "&id=" + ImageId.ToString();
return strImage;
}
public string FormatPreviewImageURL(int ImageId, int moduleId, int iWidth)
{
PortalSettings _portalSettings = (PortalSettings)HttpContext.Current.Items["PortalSettings"];
string strImage = "";
if (ImageId > -1) {
strImage = (HttpContext.Current.Request.ApplicationPath == "/" ? HttpContext.Current.Request.ApplicationPath : HttpContext.Current.Request.ApplicationPath + "/") + "DesktopModules/Repository/MakeThumbnail.aspx?tabid=" + _portalSettings.ActiveTab.TabID + "&id=" + ImageId.ToString() + "&mid=" + moduleId.ToString() + "&w=" + iWidth.ToString();
}
return strImage;
}
public string FormatNoPreviewImageURL(int moduleId, int iWidth)
{
PortalSettings _portalSettings = (PortalSettings)HttpContext.Current.Items["PortalSettings"];
string strImage = "";
strImage = (HttpContext.Current.Request.ApplicationPath == "/" ? HttpContext.Current.Request.ApplicationPath : HttpContext.Current.Request.ApplicationPath + "/") + "DesktopModules/Repository/MakeThumbnail.aspx?tabid=" + _portalSettings.ActiveTab.TabID + "&mid=" + moduleId.ToString() + "&w=" + iWidth.ToString();
return strImage;
}
public string GetVersion()
{
return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
public string ConvertToRoles(string RoleIDs, int PortalID)
{
string RoleNames = ";";
string RoleID = "";
DotNetNuke.Security.Roles.RoleController oRoleController = new DotNetNuke.Security.Roles.RoleController();
DotNetNuke.Security.Roles.RoleInfo oRoleInfo = null;
foreach (string RoleID_loopVariable in RoleIDs.Split(';')) {
RoleID = RoleID_loopVariable;
if (!string.IsNullOrEmpty(RoleID.ToString())) {
if (int.Parse(RoleID) == -1) {
RoleNames = RoleNames + DotNetNuke.Common.Globals.glbRoleAllUsersName + ";";
} else {
oRoleInfo = DotNetNuke.Security.Roles.RoleController.Instance.GetRoleById(PortalID, int.Parse(RoleID));
RoleNames = RoleNames + oRoleInfo.RoleName + ";";
}
}
}
return RoleNames;
}
public int LoadTemplate(string TemplateName, string FileName, ref XmlDocument XmlDoc, ref string[] Items)
{
string delimStr = "[]";
char[] delimiter = delimStr.ToCharArray();
System.IO.StreamReader sr = null;
PortalSettings _portalSettings = (PortalSettings)HttpContext.Current.Items["PortalSettings"];
string m_buffer = null;
string m_template = null;
string m_xml = null;
int m_return = 0;
// Template locations ..
// 1. look in /DesktopModules/Repository/Templates
// 2. look in /Portals/[currentPortal]/RepositoryTemplates
// templates in #2 above take precedence over the standard templates, this allows
// you to change templates for a particular portal without affecting other
// portals. To change a template for all portals, make the change in location #1
if (File.Exists(HttpContext.Current.Server.MapPath(_portalSettings.HomeDirectory + "RepositoryTemplates/" + TemplateName + "/" + FileName + ".html"))) {
m_template = _portalSettings.HomeDirectory + "RepositoryTemplates/" + TemplateName + "/" + FileName + ".html";
} else {
m_template = "~/DesktopModules/Repository/Templates/" + TemplateName + "/" + FileName + ".html";
}
if (File.Exists(HttpContext.Current.Server.MapPath(_portalSettings.HomeDirectory + "RepositoryTemplates/" + TemplateName + "/" + FileName + ".xml"))) {
m_xml = _portalSettings.HomeDirectory + "RepositoryTemplates/" + TemplateName + "/" + FileName + ".xml";
} else {
m_xml = "~/DesktopModules/Repository/Templates/" + TemplateName + "/" + FileName + ".xml";
}
try {
sr = new System.IO.StreamReader(HttpContext.Current.Server.MapPath(m_template));
m_buffer = sr.ReadToEnd();
XmlDoc = new System.Xml.XmlDocument();
XmlDoc.Load(HttpContext.Current.Server.MapPath(m_xml));
Items = m_buffer.Split(delimiter);
} catch {
m_buffer = "ERROR: UNABLE TO READ REPOSITORY HEADER TEMPLATE:";
Items = m_buffer.Split(delimiter);
m_return = -1;
} finally {
if ((sr != null))
sr.Close();
}
return m_return;
}
public string GetResourceFile(string TemplateName, string FileName)
{
PortalSettings _portalSettings = (PortalSettings)HttpContext.Current.Items["PortalSettings"];
string m_template = string.Empty;
// ResourceFile locations ..
// 1. look in /Portals/[currentPortal]/RepositoryTemplates/templatename/App_LocalResources
// 2. look in /DesktopModules/Repository/Templates/templatename/App_LocalResources
// templates in #1 above take precedence over the standard templates, this allows
// you to change templates for a particular portal without affecting other
// portals. To change a template for all portals, make the change in location #2
if (File.Exists(HttpContext.Current.Server.MapPath(_portalSettings.HomeDirectory + "RepositoryTemplates/" + TemplateName + "/App_LocalResources/" + FileName + ".resx"))) {
m_template = _portalSettings.HomeDirectory + "RepositoryTemplates/" + TemplateName + "/App_LocalResources/" + FileName + ".resx";
} else {
if (File.Exists(HttpContext.Current.Server.MapPath("~/DesktopModules/Repository/Templates/" + TemplateName + "/App_LocalResources/" + FileName + ".resx"))) {
m_template = "~/DesktopModules/Repository/Templates/" + TemplateName + "/App_LocalResources/" + FileName + ".resx";
}
}
return m_template;
}
public bool IsModerator(int pid, int mid)
{
PortalSettings _portalSettings = (PortalSettings)HttpContext.Current.Items["PortalSettings"];
var moduleController = new ModuleController();
var moduleInfo = moduleController.GetModule(mid);
var settings = moduleInfo.ModuleSettings;
string ModerateRoles = "";
Helpers oRepositoryController = new Helpers();
if ((Convert.ToString(settings["moderationroles"]) != null)) {
ModerateRoles = oRepositoryController.ConvertToRoles(Convert.ToString(settings["moderationroles"]), pid);
}
if (PortalSecurity.IsInRole(_portalSettings.AdministratorRoleName) | PortalSecurity.IsInRoles(ModerateRoles)) {
return true;
} else {
return false;
}
}
public bool IsTrusted(int pid, int mid)
{
PortalSettings _portalSettings = (PortalSettings)HttpContext.Current.Items["PortalSettings"];
var moduleController = new ModuleController();
var moduleInfo = moduleController.GetModule(mid);
var settings = moduleInfo.ModuleSettings;
string TrustedRoles = "";
Helpers oRepositoryController = new Helpers();
if ((Convert.ToString(settings["trustedroles"]) != null)) {
TrustedRoles = oRepositoryController.ConvertToRoles(Convert.ToString(settings["trustedroles"]), pid);
}
if (IsModerator(pid, mid) | PortalSecurity.IsInRoles(TrustedRoles)) {
return true;
} else {
return false;
}
}
public bool IsURL(string item)
{
if (item.ToLower().StartsWith("http:") | item.ToLower().StartsWith("https:") | item.ToLower().StartsWith("ftp:") | item.ToLower().StartsWith("mms:")) {
return true;
} else {
return false;
}
}
public string GetRepositoryItemFileName(string name, bool ExtractGuid)
{
PortalSettings _portalSettings = (PortalSettings)HttpContext.Current.Items["PortalSettings"];
string value = string.Empty;
if (name.ToLower().StartsWith("fileid=")) {
name = ConvertFileIDtoFileName(_portalSettings.PortalId, int.Parse(name.Substring(7)));
}
value = ExtractFileName(name, ExtractGuid);
return value;
}
public string ExtractFileName(string filename, bool ExtractGuid)
{
int i = 0;
int iExtension = 0;
int iEnd = 0;
int iStart = 0;
string s = filename;
int firstDot = -1;
int secondDot = -1;
if (!IsURL(filename)) {
if (ExtractGuid) {
// if the item has a GIUD, then there will be exactly 37 characters between the last two periods
iExtension = s.LastIndexOf(".");
iEnd = iExtension - 1;
i = iEnd;
while (i > 0 & iStart == 0) {
if (s.Substring(i, 1) == ".") {
iStart = i;
}
i -= 1;
}
if (iExtension - iStart == 37) {
s = s.Substring(0, iStart) + s.Substring(iExtension);
}
}
}
return s;
}
public void AddCategoryToTreeObject(int moduleid, int itemid, ArrayList arr, DotNetNuke.UI.WebControls.TreeNodeCollection obj, string prefix, bool showCount)
{
RepositoryObjectCategoriesController cc = new RepositoryObjectCategoriesController();
RepositoryObjectCategoriesInfo cv = null;
ListItem objItem = null;
ArrayList arr2 = null;
RepositoryCategoryController cController = new RepositoryCategoryController();
foreach (RepositoryCategoryInfo cat in arr) {
DotNetNuke.UI.WebControls.TreeNode newNode = new DotNetNuke.UI.WebControls.TreeNode();
if (showCount) {
newNode.Text = cat.Category + "(" + cat.Count.ToString() + ")";
} else {
newNode.Text = cat.Category;
}
newNode.Key = cat.ItemId.ToString();
newNode.ToolTip = "";
cv = cc.GetSingleRepositoryObjectCategories(itemid, cat.ItemId);
if ((cv != null)) {
newNode.Selected = true;
} else {
newNode.Selected = false;
}
obj.Add(newNode);
arr2 = cController.GetRepositoryCategories(moduleid, cat.ItemId);
if (arr2.Count > 0) {
AddCategoryToTreeObject(moduleid, itemid, arr2, newNode.TreeNodes, "", showCount);
}
}
}
public void AddCategoryToListObject(int moduleid, int itemid, ArrayList arr, ListControl obj, string prefix, string separator)
{
RepositoryObjectCategoriesController cc = new RepositoryObjectCategoriesController();
RepositoryObjectCategoriesInfo cv = null;
ListItem objItem = null;
ArrayList arr2 = null;
RepositoryCategoryController cController = new RepositoryCategoryController();
foreach (RepositoryCategoryInfo cat in arr) {
// if a category has child entries, then it's not selectable
arr2 = cController.GetRepositoryCategories(moduleid, cat.ItemId);
objItem = new ListItem(prefix + cat.Category, cat.ItemId.ToString());
cv = cc.GetSingleRepositoryObjectCategories(itemid, cat.ItemId);
if ((cv != null)) {
objItem.Selected = true;
} else {
objItem.Selected = false;
}
obj.Items.Add(objItem);
if (arr2.Count > 0) {
AddCategoryToListObject(moduleid, itemid, arr2, obj, prefix + cat.Category + separator, separator);
}
}
}
public void AddCategoryToArrayList(int moduleid, int itemid, ArrayList arr, ref ArrayList target)
{
ArrayList arr2 = null;
RepositoryCategoryController cController = new RepositoryCategoryController();
foreach (RepositoryCategoryInfo cat in arr) {
try {
target.Add(cat);
} catch (Exception ex) {
target.Add(DotNetNuke.Services.Localization.Localization.GetString("InvalidCategory", this.LocalResourceFile));
}
// if a category has child entries, then it's not selectable
arr2 = cController.GetRepositoryCategories(moduleid, cat.ItemId);
if (arr2.Count > 0) {
AddCategoryToArrayList(moduleid, itemid, arr2, ref target);
}
}
}
public ArrayList getSearchClause(string txt)
{
ArrayList results = new ArrayList();
string QuoteDelimStr = "\"";
char[] QuoteDelimiter = QuoteDelimStr.ToCharArray();
string SpaceDelimStr = " ";
char[] SpaceDelimiter = SpaceDelimStr.ToCharArray();
string[] Items = null;
string[] Words = null;
int dItem = 1;
// phrases will either be odd or even, check to see it the txt starts with
// a quote, if so, then phrases will be odd, if not, phrases will be even
string PhrasePlacement = (txt.StartsWith("\"") ? "ODD" : "EVEN");
Items = txt.Split(QuoteDelimiter);
foreach (string item in Items) {
if (!string.IsNullOrEmpty(item)) {
if (dItem % 2 == 0) {
// even numbered item
if (PhrasePlacement == "EVEN") {
if (!string.IsNullOrEmpty(item))
results.Add(item);
} else {
Words = item.Split(SpaceDelimiter);
foreach (string word in Words) {
if (!string.IsNullOrEmpty(word))
results.Add(word);
}
}
} else {
// odd numbered item
if (PhrasePlacement == "ODD") {
if (!string.IsNullOrEmpty(item))
results.Add(item);
} else {
Words = item.Split(SpaceDelimiter);
foreach (string word in Words) {
if (!string.IsNullOrEmpty(word))
results.Add(word);
}
}
}
dItem = dItem + 1;
}
}
return results;
}
public static Hashtable GetModSettings(int mid)
{
var moduleController = new ModuleController();
var moduleInfo = moduleController.GetModule(mid);
var settings = moduleInfo.ModuleSettings;
return settings;
}
public string ChangeValue(string oldUrl, string qsName, string newValue, int del = 0)
{
string newUrl = "";
// Check if the [qsName] is currently in the [oldUrl]
if (Strings.InStr(oldUrl, qsName + "=")>0) {
oldUrl += "&";
int pos1 = 0;
int pos2 = 0;
if (del == 1) {
pos1 = oldUrl.IndexOf(qsName + "=");
pos2 = oldUrl.IndexOf("&", pos1) + 1;
} else {
pos1 = oldUrl.IndexOf(qsName + "=") + qsName.Length + 1;
pos2 = oldUrl.IndexOf("&", pos1);
}
string chunk_1 = oldUrl.Substring(0, pos1);
string chunk_2 = oldUrl.Substring(pos2);
if (del == 1) {
newUrl = chunk_1 + chunk_2;
} else {
newUrl = chunk_1 + newValue + chunk_2;
}
newUrl = newUrl.Substring(0, (newUrl.Length - 1));
} else {
if (del == 1) {
return oldUrl;
}
// Append the new value to the [oldUrl] and make it a [newUrl]
if (oldUrl.EndsWith("?")) {
newUrl = oldUrl + qsName + "=" + newValue;
} else if (Strings.InStr(oldUrl, "?")>0 && oldUrl.EndsWith("?") == false) {
if (oldUrl.EndsWith("&")) {
newUrl = oldUrl + qsName + "=" + newValue;
} else {
newUrl = oldUrl + "&" + qsName + "=" + newValue;
}
} else {
newUrl = oldUrl + "?" + qsName + "=" + newValue;
}
}
return newUrl;
}
#endregion
#region "Download functions"
public void DownloadFile(string ItemID)
{
DownloadFile(ItemID, "");
}
public void DownloadFile(string ItemID, string target)
{
RepositoryController repository = new RepositoryController();
RepositoryInfo objRepository = null;
string strDownloadURL = "";
objRepository = repository.GetSingleRepositoryObject(Convert.ToInt32(ItemID));
if ((objRepository != null)) {
// a repository object can one of 4 formats
// 1. URL http://...
// 2. Repository File
// 3. File system file FileId=nn
// 4. Page on the site nn
if (IsURL(objRepository.FileName)) {
strDownloadURL = objRepository.FileName;
// check settings to see if the link should open in a new window, or
// in the same window
switch (target.ToUpper()) {
case "NEW":
HttpContext.Current.Response.Write("<script>window.open('" + strDownloadURL + "');</script>");
break;
case "SELF":
HttpContext.Current.Response.Redirect(strDownloadURL, true);
break;
default:
HttpContext.Current.Response.Write("<script>window.open('" + strDownloadURL + "');</script>");
break;
}
} else {
if (objRepository.FileName.ToLower().StartsWith("fileid=")) {
// File System
PortalSettings _portalSettings = (PortalSettings)HttpContext.Current.Items["PortalSettings"];
DotNetNuke.Services.FileSystem.FileInfo file = this.ConvertFileIDtoFile(_portalSettings.PortalId, int.Parse(objRepository.FileName.Substring(7)));
StreamFile(file.PhysicalPath, this.ExtractFileName(file.FileName, true));
} else {
// strip out the timestamp to get the original filename
string strFileName = this.ExtractFileName(objRepository.FileName, true);
// check file extension before allowing download
string strExtension = Strings.Replace(Path.GetExtension(strFileName), ".", "");
if (objRepository.Approved == this.IS_APPROVED) {
if (string.IsNullOrEmpty(objRepository.CreatedByUser)) {
StreamFile(this.g_AnonymousFolder + "\\" + objRepository.FileName, strFileName);
} else {
if (this.g_UserFolders) {
StreamFile(this.g_ApprovedFolder + "\\" + objRepository.CreatedByUser.ToString() + "\\" + objRepository.FileName, strFileName);
} else {
StreamFile(this.g_ApprovedFolder + "\\" + objRepository.FileName, strFileName);
}
}
} else {
StreamFile(this.g_UnApprovedFolder + "\\" + objRepository.FileName, strFileName);
}
}
}
}
}
private void StreamFile(string FilePath, string DownloadAs)
{
DownloadAs = DownloadAs.Replace(" ", "_");
System.IO.FileInfo objFile = new System.IO.FileInfo(FilePath);
System.Web.HttpResponse objResponse = System.Web.HttpContext.Current.Response;
objResponse.ClearContent();
objResponse.ClearHeaders();
objResponse.AppendHeader("Content-Type", MimeMapping.GetMimeMapping(objFile.Extension));
objResponse.AppendHeader("Content-Disposition", "attachment; filename=" + DownloadAs);
objResponse.AppendHeader("Content-Length", objFile.Length.ToString());
objResponse.TransmitFile(objFile.FullName);
}
#endregion
#region "Private Functions and Subs"
private string GetTargetFolder(int moduleid, RepositoryInfo pRepository)
{
string strTargetFolder = "";
PortalSettings _portalSettings = (PortalSettings)HttpContext.Current.Items["PortalSettings"];
var moduleController = new ModuleController();
var moduleInfo = moduleController.GetModule(moduleid);
var settings = moduleInfo.ModuleSettings;
UserInfo userInfo = null;
userInfo = UserController.Instance.GetCurrentUserInfo();
SetRepositoryFolders(moduleid);
// figure out where to put the file(s)
// if the current user is an Admin, or had edit permissions for this module
// or is a moderator, then they get automatically approved.