-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDnnUtils.cs
799 lines (704 loc) · 30.9 KB
/
DnnUtils.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Xml;
using DotNetNuke.Common.Lists;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Entities.Users;
using DotNetNuke.Security;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.FileSystem;
//using ICSharpCode.SharpZipLib.Zip;
using NBrightCore.common;
using FileInfo = System.IO.FileInfo;
using System.IO.Compression;
namespace NBrightDNN
{
public class DnnUtils
{
public static void Zip(string zipFileMapPath, List<String> fileMapPathList )
{
//// Zip up the files - From SharpZipLib Demo Code
//using (var s = new ZipOutputStream(File.Create(zipFileMapPath)))
//{
// s.SetLevel(9); // 0-9, 9 being the highest compression
// byte[] buffer = new byte[4096];
// foreach (string file in fileMapPathList)
// {
// ZipEntry entry = new
// ZipEntry(Path.GetFileName(file));
// entry.DateTime = DateTime.Now;
// s.PutNextEntry(entry);
// using (FileStream fs = File.OpenRead(file))
// {
// int sourceBytes;
// do
// {
// sourceBytes = fs.Read(buffer, 0,
// buffer.Length);
// s.Write(buffer, 0, sourceBytes);
// } while (sourceBytes > 0);
// }
// }
// s.Finish();
// s.Close();
//}
}
public static void ZipFolder(string folderName, String zipFileMapPath)
{
//var zipStream = new ZipOutputStream(File.Create(zipFileMapPath));
//try
//{
// int folderOffset = folderName.Length + (folderName.EndsWith("\\") ? 0 : 1);
// zipStream.SetLevel(3); //0-9, 9 being the highest level of compression
// CompressFolder(folderName, zipStream, folderOffset);
// zipStream.Close();
//}
//catch (Exception ex)
//{
// zipStream.Close();
// throw ex;
//}
}
//private static void CompressFolder(string path, ZipOutputStream zipStream, int folderOffset)
//{
// //string[] files = Directory.GetFiles(path);
// //foreach (string filename in files)
// //{
// // FileInfo fi = new FileInfo(filename);
// // string entryName = filename.Substring(folderOffset); // Makes the name in zip based on the folder
// // entryName = ZipEntry.CleanName(entryName); // Removes drive from name and fixes slash direction
// // ZipEntry newEntry = new ZipEntry(entryName);
// // newEntry.DateTime = fi.LastWriteTime; // Note the zip format stores 2 second granularity
// // newEntry.Size = fi.Length;
// // zipStream.PutNextEntry(newEntry);
// // // Zip the file in buffered chunks
// // // the "using" will close the stream even if an exception occurs
// // byte[] buffer = new byte[4096];
// // using (FileStream streamReader = File.OpenRead(filename))
// // {
// // ZipUtilCopy(streamReader, zipStream, buffer);
// // }
// // zipStream.CloseEntry();
// //}
// //string[] folders = Directory.GetDirectories(path);
// //foreach (string folder in folders)
// //{
// // CompressFolder(folder, zipStream, folderOffset);
// //}
//}
/// <summary>
/// Copy the contents of one <see cref="Stream"/> to another. Taken from StreamUtils on SharpZipLib as stripped
/// </summary>
/// <param name="source">The stream to source data from.</param>
/// <param name="destination">The stream to write data to.</param>
/// <param name="buffer">The buffer to use during copying.</param>
private static void ZipUtilCopy(Stream source, Stream destination, byte[] buffer)
{
// Ensure a reasonable size of buffer is used without being prohibitive.
if (buffer.Length < 128)
{
throw new ArgumentException("Buffer is too small", "buffer");
}
bool copying = true;
while (copying)
{
int bytesRead = source.Read(buffer, 0, buffer.Length);
if (bytesRead > 0)
{
destination.Write(buffer, 0, bytesRead);
}
else
{
destination.Flush();
copying = false;
}
}
}
public static void UnZip(string zipFileMapPath, string outputFolder)
{
var filename = Path.GetFileNameWithoutExtension(zipFileMapPath);
var ziptarget = outputFolder.TrimEnd('\\') + "\\" + filename;
if (Directory.Exists(ziptarget)) Directory.Delete(ziptarget, true);
ZipFile.ExtractToDirectory(zipFileMapPath, outputFolder);
}
//public static void UnzipResources(ZipInputStream zipStream, string destPath)
//{
// try
// {
// ZipEntry objZipEntry;
// string LocalFileName;
// string RelativeDir;
// string FileNamePath;
// objZipEntry = zipStream.GetNextEntry();
// while (objZipEntry != null)
// {
// LocalFileName = objZipEntry.Name;
// RelativeDir = Path.GetDirectoryName(objZipEntry.Name);
// if ((RelativeDir != string.Empty) && (!Directory.Exists(Path.Combine(destPath, RelativeDir))))
// {
// Directory.CreateDirectory(Path.Combine(destPath, RelativeDir));
// }
// if ((!objZipEntry.IsDirectory) && (!String.IsNullOrEmpty(LocalFileName)))
// {
// FileNamePath = Path.Combine(destPath, LocalFileName).Replace("/", "\\");
// try
// {
// if (File.Exists(FileNamePath))
// {
// File.SetAttributes(FileNamePath, FileAttributes.Normal);
// File.Delete(FileNamePath);
// }
// FileStream objFileStream = null;
// try
// {
// objFileStream = File.Create(FileNamePath);
// int intSize = 2048;
// var arrData = new byte[2048];
// intSize = zipStream.Read(arrData, 0, arrData.Length);
// while (intSize > 0)
// {
// objFileStream.Write(arrData, 0, intSize);
// intSize = zipStream.Read(arrData, 0, arrData.Length);
// }
// }
// finally
// {
// if (objFileStream != null)
// {
// objFileStream.Close();
// objFileStream.Dispose();
// }
// }
// }
// catch (Exception ex)
// {
// // DnnLog.Error(ex);
// }
// }
// objZipEntry = zipStream.GetNextEntry();
// }
// }
// finally
// {
// if (zipStream != null)
// {
// zipStream.Close();
// zipStream.Dispose();
// }
// }
//}
public static List<System.IO.FileInfo> GetFiles(string FolderMapPath)
{
DirectoryInfo di = new DirectoryInfo(FolderMapPath);
List<System.IO.FileInfo> files = new List<System.IO.FileInfo>();
if (di.Exists)
{
foreach (System.IO.FileInfo file in di.GetFiles())
{
files.Add(file);
}
}
return files;
}
public static Dictionary<String,String> GetCountryCodeList(int portalId = -1)
{
var rtnDic = new Dictionary<String, String>();
if (portalId == -1 && PortalSettings.Current != null) portalId = PortalSettings.Current.PortalId;
if (portalId != -1)
{
var objLCtrl = new ListController();
var l = objLCtrl.GetListEntryInfoItems("Country").ToList();
foreach (var i in l)
{
rtnDic.Add(i.Value,i.Text);
}
}
return rtnDic;
}
public static List<string> GetCultureCodeList(int portalId = -1)
{
var rtnList = new List<string>();
if (portalId == -1 && PortalSettings.Current != null) portalId = PortalSettings.Current.PortalId;
if (portalId != -1)
{
var enabledLanguages = LocaleController.Instance.GetLocales(portalId);
foreach (KeyValuePair<string, Locale> kvp in enabledLanguages)
{
rtnList.Add(kvp.Value.Code);
}
}
return rtnList;
}
public static string GetCurrentValidCultureCode(List<string> validCultureCodes = null)
{
var validCurrentCulture = Utils.GetCurrentCulture();
if (validCultureCodes != null)
{
if (validCultureCodes.Count > 0 && !validCultureCodes.Contains(validCurrentCulture))
{
//Cannot find the current culture so return the first in the valid list
return validCultureCodes[0];
}
}
return validCurrentCulture;
}
public static string GetDataResponseAsString(string dataurl,string headerFieldId = "",string headerFieldData = "")
{
string strOut = "";
if (!string.IsNullOrEmpty(dataurl))
{
try
{
// solution for exception
// The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
System.Net.HttpWebRequest req = DotNetNuke.Common.Globals.GetExternalRequest(dataurl);
if (headerFieldId!= "")
{
//allow a get request to pass data via the header.
// This is limited to 32K by default in IIS, but can be limited to less. (So use with care!)
req.Headers.Add(headerFieldId,headerFieldData);
}
System.Net.WebResponse resp = req.GetResponse();
var s = resp.GetResponseStream();
if (s != null)
{
var reader = new StreamReader(s);
strOut = reader.ReadToEnd();
}
}
catch (Exception ex)
{
strOut = "ERROR - dataurl=" + dataurl + " ex=" + ex.ToString();
}
}
else
{
strOut = "ERROR - No Data Url given";
}
return strOut;
}
public static void PurgeDataBaseInfo(int portalId, int moduleId, DataCtrlInterface objCtrl, string entityTypeCode, int purgeDays = -7)
{
var l = objCtrl.GetList(portalId, moduleId, entityTypeCode);
foreach (NBrightInfo obj in l)
{
if (obj.ModifiedDate < (DateTime.Now.AddDays(purgeDays)))
{
objCtrl.Delete(obj.ItemID);
}
}
}
public static DotNetNuke.Entities.Tabs.TabCollection GetPortalTabs(int portalId)
{
var portalTabs = (DotNetNuke.Entities.Tabs.TabCollection)NBrightCore.common.Utils.GetCache("NBright_portalTabs" + portalId.ToString(""));
if (portalTabs == null)
{
var objTabCtrl = new DotNetNuke.Entities.Tabs.TabController();
portalTabs = objTabCtrl.GetTabsByPortal(portalId);
NBrightCore.common.Utils.SetCache("NBright_portalTabs" + portalId.ToString(""), portalTabs);
}
return portalTabs;
}
public static DotNetNuke.Entities.Users.UserInfo GetValidUser(int PortalId, string username, string password)
{
var userLoginStatus = new DotNetNuke.Security.Membership.UserLoginStatus();
return DotNetNuke.Entities.Users.UserController.ValidateUser(PortalId, username, password, "", "", "", ref userLoginStatus);
}
public static bool IsValidUser(int PortalId, string username, string password)
{
var u = GetValidUser(PortalId,username,password);
if (u != null)
{
return true;
}
return false;
}
public static string GetLocalizedString(string Key, string resourceFileRoot,string lang)
{
return Localization.GetString(Key, resourceFileRoot, lang);
}
public static int GetPortalByModuleID(int moduleId)
{
var objMCtrl = new DotNetNuke.Entities.Modules.ModuleController();
var objMInfo = objMCtrl.GetModule(moduleId);
if (objMInfo == null) return -1;
return objMInfo.PortalID;
}
/// <summary>
/// GET Portals
/// </summary>
/// <returns></returns>
public static List<PortalInfo> GetAllPortals()
{
var pList = new List<PortalInfo>();
var objPC = new DotNetNuke.Entities.Portals.PortalController();
var list = objPC.GetPortals();
if (list == null || list.Count == 0)
{
//Problem with DNN6 GetPortals when ran from scheduler.
PortalInfo objPInfo;
var flagdeleted = 0;
for (var lp = 0; lp <= 500; lp++)
{
objPInfo = objPC.GetPortal(lp);
if ((objPInfo != null))
{
pList.Add(objPInfo);
}
else
{
// some portals may be deleted, skip 3 to see if we've got to the end of the list.
// VERY weak!!! shame!! but issue with a DNN6 version only.
if (flagdeleted == 3) break;
flagdeleted += 1;
}
}
}
else
{
foreach (PortalInfo p in list)
{
pList.Add(p);
}
}
return pList;
}
public static string GetModuleVersion(int moduleId)
{
var strVersion = "";
var objMCtrl = new DotNetNuke.Entities.Modules.ModuleController();
var objMInfo = objMCtrl.GetModule(moduleId);
if (objMInfo != null)
{
strVersion = objMInfo.DesktopModule.Version;
}
return strVersion;
}
public static ModuleInfo GetModuleinfo(int moduleId)
{
var objMCtrl = new DotNetNuke.Entities.Modules.ModuleController();
var objMInfo = objMCtrl.GetModule(moduleId);
return objMInfo;
}
public static void CreateFolder(string fullfolderPath)
{
// This function is to get around medium trust not allowing createfolder in .Net 2.0.
// DNN seems to have some work around (Not followed up on what exactly, probably security allowed in shared hosting environments for DNN except???).
// But this leads me to have this rather nasty call to DNN FolderManager.
// Prefered method is to us the Utils.CreateFolder function in NBrightCore.
var blnCreated = false;
//try normal test (doesn;t work on medium trust, but stops us forcing a "AddFolder" and suppressing the error.)
try
{
blnCreated = System.IO.Directory.Exists(fullfolderPath);
}
catch (Exception ex)
{
blnCreated = false;
}
if (!blnCreated)
{
try
{
var f = FolderManager.Instance.AddFolder(FolderMappingController.Instance.GetFolderMapping(8), fullfolderPath);
}
catch (Exception ex)
{
// Suppress error, becuase the folder may already exist!..NASTY!!..try and find a better way to deal with folders out of portal range!!
}
}
}
public static void CreatePortalFolder(DotNetNuke.Entities.Portals.PortalSettings PortalSettings, string FolderName)
{
bool blnCreated = false;
//try normal test (doesn;t work on medium trust, but avoids waiting for GetFolder.)
try
{
blnCreated = System.IO.Directory.Exists(PortalSettings.HomeDirectoryMapPath + FolderName);
}
catch (Exception ex)
{
blnCreated = false;
}
if (!blnCreated)
{
FolderManager.Instance.Synchronize(PortalSettings.PortalId, PortalSettings.HomeDirectory, true,true);
var folderInfo = FolderManager.Instance.GetFolder(PortalSettings.PortalId, FolderName);
if (folderInfo == null & !string.IsNullOrEmpty(FolderName))
{
//add folder and permissions
try
{
FolderManager.Instance.AddFolder(PortalSettings.PortalId, FolderName);
}
catch (Exception ex)
{
}
folderInfo = FolderManager.Instance.GetFolder(PortalSettings.PortalId, FolderName);
if ((folderInfo != null))
{
int folderid = folderInfo.FolderID;
DotNetNuke.Security.Permissions.PermissionController objPermissionController = new DotNetNuke.Security.Permissions.PermissionController();
var arr = objPermissionController.GetPermissionByCodeAndKey("SYSTEM_FOLDER", "");
foreach (DotNetNuke.Security.Permissions.PermissionInfo objpermission in arr)
{
if (objpermission.PermissionKey == "WRITE")
{
// add READ permissions to the All Users Role
FolderManager.Instance.SetFolderPermission(folderInfo, objpermission.PermissionID, int.Parse(DotNetNuke.Common.Globals.glbRoleAllUsers));
}
}
}
}
}
}
public static DotNetNuke.Entities.Portals.PortalSettings GetCurrentPortalSettings()
{
return (DotNetNuke.Entities.Portals.PortalSettings) System.Web.HttpContext.Current.Items["PortalSettings"];
}
public static Dictionary<int, string> GetTreeTabList()
{
var tabList = DotNetNuke.Entities.Tabs.TabController.GetTabsBySortOrder(DotNetNuke.Entities.Portals.PortalSettings.Current.PortalId, Utils.GetCurrentCulture(), true);
var rtnList = new Dictionary<int, string>();
return GetTreeTabList(rtnList, tabList, 0, 0);
}
private static Dictionary<int, string> GetTreeTabList(Dictionary<int, string> rtnList, List<TabInfo> tabList, int level, int parentid, string prefix = "")
{
if (level > 20) // stop infinate loop
{
return rtnList;
}
if (parentid > 0) prefix += "..";
foreach (TabInfo tInfo in tabList)
{
var parenttestid = tInfo.ParentId;
if (parenttestid < 0) parenttestid = 0;
if (parentid == parenttestid)
{
if (!tInfo.IsDeleted && tInfo.TabPermissions.Count > 2)
{
rtnList.Add(tInfo.TabID, prefix + "" + tInfo.TabName);
GetTreeTabList(rtnList, tabList, level + 1, tInfo.TabID, prefix);
}
}
}
return rtnList;
}
public static Dictionary<Guid, string> GetTreeTabListOnUniqueId()
{
var tabList = DotNetNuke.Entities.Tabs.TabController.GetTabsBySortOrder(DotNetNuke.Entities.Portals.PortalSettings.Current.PortalId, Utils.GetCurrentCulture(), true);
var rtnList = new Dictionary<Guid, string>();
return GetTreeTabListOnUniqueId(rtnList, tabList, 0, 0);
}
private static Dictionary<Guid, string> GetTreeTabListOnUniqueId(Dictionary<Guid, string> rtnList, List<TabInfo> tabList, int level, int parentid, string prefix = "")
{
if (level > 20) // stop infinate loop
{
return rtnList;
}
if (parentid > 0) prefix += "..";
foreach (TabInfo tInfo in tabList)
{
var parenttestid = tInfo.ParentId;
if (parenttestid < 0) parenttestid = 0;
if (parentid == parenttestid)
{
if (!tInfo.IsDeleted && tInfo.TabPermissions.Count > 2)
{
rtnList.Add(tInfo.UniqueId, prefix + "" + tInfo.TabName);
GetTreeTabListOnUniqueId(rtnList, tabList, level + 1, tInfo.TabID, prefix);
}
}
}
return rtnList;
}
public static Dictionary<int, string> GetTreeTabListOnTabId()
{
var tabList = DotNetNuke.Entities.Tabs.TabController.GetTabsBySortOrder(DotNetNuke.Entities.Portals.PortalSettings.Current.PortalId, Utils.GetCurrentCulture(), true);
var rtnList = new Dictionary<int, string>();
return GetTreeTabListOnTabId(rtnList, tabList, 0, 0);
}
private static Dictionary<int, string> GetTreeTabListOnTabId(Dictionary<int, string> rtnList, List<TabInfo> tabList, int level, int parentid, string prefix = "")
{
if (level > 20) // stop infinate loop
{
return rtnList;
}
if (parentid > 0) prefix += "..";
foreach (TabInfo tInfo in tabList)
{
var parenttestid = tInfo.ParentId;
if (parenttestid < 0) parenttestid = 0;
if (parentid == parenttestid)
{
if (!tInfo.IsDeleted && tInfo.TabPermissions.Count > 2)
{
rtnList.Add(tInfo.TabID, prefix + "" + tInfo.TabName);
GetTreeTabListOnTabId(rtnList, tabList, level + 1, tInfo.TabID, prefix);
}
}
}
return rtnList;
}
public static String GetTreeViewTabJSData(String selectTabIdCVS = "")
{
var tabList = DotNetNuke.Entities.Tabs.TabController.GetTabsBySortOrder(DotNetNuke.Entities.Portals.PortalSettings.Current.PortalId, Utils.GetCurrentCulture(), true);
var rtnDataString = "";
var selecttabidlist = selectTabIdCVS.Split(',');
rtnDataString = GetTreeViewTabJSData(rtnDataString, tabList, 0, 0, selecttabidlist);
rtnDataString = rtnDataString.Replace(", children: []", "");
rtnDataString = "var treeData = [" + rtnDataString + "];";
return rtnDataString;
}
private static String GetTreeViewTabJSData(String rtnDataString, List<TabInfo> tabList, int level, int parentid, String[] selecttabidlist)
{
if (level > 20) // stop infinate loop
{
return rtnDataString;
}
foreach (TabInfo tInfo in tabList)
{
var parenttestid = tInfo.ParentId;
if (parenttestid < 0) parenttestid = 0;
if (parentid == parenttestid)
{
if (!tInfo.IsDeleted && tInfo.TabPermissions.Count > 2)
{
var selectedvalue = "false";
if (selecttabidlist.Contains(tInfo.TabID.ToString(""))) selectedvalue = "true";
rtnDataString += "{title: '" + tInfo.TabName.Replace("'","'") + "', key:'" + tInfo.TabID + "', selected: " + selectedvalue + ", children: [";
rtnDataString = GetTreeViewTabJSData(rtnDataString, tabList, level + 1, tInfo.TabID, selecttabidlist);
rtnDataString += "]},";
}
}
}
rtnDataString = rtnDataString.TrimEnd(',');
return rtnDataString;
}
public static Dictionary<string, string> GetUserProfileProperties(UserInfo userInfo)
{
var prop = new Dictionary<string, string>();
foreach (DotNetNuke.Entities.Profile.ProfilePropertyDefinition p in userInfo.Profile.ProfileProperties)
{
prop.Add(p.PropertyName, p.PropertyValue);
}
return prop;
}
public static Dictionary<string, string> GetUserProfileProperties(String userId)
{
if (!Utils.IsNumeric(userId)) return null;
var userInfo = UserController.GetUserById(PortalSettings.Current.PortalId,Convert.ToInt32(userId));
return GetUserProfileProperties(userInfo);
}
public static void SetUserProfileProperties(UserInfo userInfo,Dictionary<string, string> properties)
{
foreach (var p in properties)
{
userInfo.Profile.SetProfileProperty(p.Key,p.Value);
}
UserController.UpdateUser(PortalSettings.Current.PortalId, userInfo);
}
public static void SetUserProfileProperties(String userId,Dictionary<string, string> properties)
{
if (Utils.IsNumeric(userId))
{
var userInfo = UserController.GetUserById(PortalSettings.Current.PortalId, Convert.ToInt32(userId));
SetUserProfileProperties(userInfo, properties);
}
}
public static PortalSettings GetPortalSettings(int portalId)
{
var controller = new PortalController();
var portal = controller.GetPortal(portalId);
return new PortalSettings(portal);
}
public static String GetResourceString(String resourcePath, String resourceKey,String resourceExt = "Text", String lang = "")
{
var resDic = GetResourceData(resourcePath, resourceKey, lang);
if (resDic != null && resDic.ContainsKey(resourceExt))
{
return resDic[resourceExt];
}
return "";
}
public static Dictionary<String, String> GetResourceData(String resourcePath, String resourceKey, String lang = "")
{
if (lang == "") lang = DnnUtils.GetCurrentValidCultureCode();
var ckey = resourcePath + resourceKey + lang;
var obj = Utils.GetCache(ckey);
if (obj != null) return (Dictionary<String, String>)obj;
var rtnList = new Dictionary<String, String>();
var s = resourceKey.Split('.');
if (s.Length == 2 && resourcePath != "")
{
var fName = s[0];
var rKey = s[1];
var relativefilename = resourcePath.TrimEnd('/') + "/" + fName + ".ascx.resx";
var fullFileName = System.Web.Hosting.HostingEnvironment.MapPath(relativefilename);
if (!String.IsNullOrEmpty(fullFileName) && System.IO.File.Exists(fullFileName))
{
var xmlDoc = new XmlDocument();
xmlDoc.Load(fullFileName);
var xmlNodList = xmlDoc.SelectNodes("root/data[starts-with(./@name,'" + rKey + ".')]");
if (xmlNodList != null)
{
foreach (XmlNode nod in xmlNodList)
{
if (nod.Attributes != null)
{
var n = nod.Attributes["name"].Value;
if (lang == "") lang = Utils.GetCurrentCulture();
var rtnValue = Localization.GetString(n, relativefilename, PortalSettings.Current, lang, true);
if (!rtnList.ContainsKey(n.Replace(rKey + ".", "")))
{
rtnList.Add(n.Replace(rKey + ".", ""), rtnValue);
}
}
}
}
}
Utils.SetCache(ckey, rtnList, DateTime.Now.AddMinutes(20));
}
return rtnList;
}
public static void ClearPortalCache(int portalId)
{
DataCache.ClearPortalCache(portalId, true);
}
public static bool IsInRole(string role)
{
return UserController.Instance.GetCurrentUserInfo().IsInRole(role);
}
#region "encryption"
public static String Encrypt(String value, String passkey = "")
{
var objSec = new PortalSecurity();
if (value == null) return "";
if (passkey == "")
{
var ps = GetCurrentPortalSettings();
passkey = ps.GUID.ToString();
}
return objSec.Encrypt(passkey, value);
}
public static String Decrypt(String value, String passkey = "")
{
var objSec = new PortalSecurity();
if (value == null) return "";
if (passkey == "")
{
var ps = GetCurrentPortalSettings();
passkey = ps.GUID.ToString();
}
return objSec.Decrypt(passkey, value);
}
#endregion
}
}