1+ #region Copyright
2+ //
3+ // DotNetNuke® - http://www.dotnetnuke.com
4+ // Copyright (c) 2002-2016
5+ // by DotNetNuke Corporation
6+ //
7+ // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8+ // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9+ // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
10+ // to permit persons to whom the Software is furnished to do so, subject to the following conditions:
11+ //
12+ // The above copyright notice and this permission notice shall be included in all copies or substantial portions
13+ // of the Software.
14+ //
15+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
16+ // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17+ // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
18+ // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19+ // DEALINGS IN THE SOFTWARE.
20+ #endregion
21+ #region Usings
22+
23+ using System ;
24+ using System . Collections . Specialized ;
25+ using System . Threading ;
26+ using System . Web ;
27+ using DotNetNuke . Common ;
28+ using DotNetNuke . Common . Utilities ;
29+ using DotNetNuke . Entities . Portals ;
30+ using DotNetNuke . Entities . Users ;
31+ using DotNetNuke . Security . Permissions ;
32+ using DotNetNuke . Services . FileSystem ;
33+ using PermissionsNotMetException = DotNetNuke . Services . FileSystem . PermissionsNotMetException ;
34+
35+ #endregion
36+
37+ namespace Satrabel . OpenImageProcessor . Services
38+ {
39+
40+ public static class ImgClickHelper
41+ {
42+ public static string Key ( ) => "imgclick" ;
43+
44+ public static bool IsValidRequest ( HttpContext context , string path )
45+ {
46+ var id = path . Substring ( Key ( ) . Length + 1 ) ;
47+ var filename = GetFileNameFromPath ( context , id . Replace ( "/" , "\\ " ) ) ;
48+ return ! string . IsNullOrEmpty ( filename ) ;
49+ }
50+
51+ /// -----------------------------------------------------------------------------
52+ /// <summary>
53+ /// This handler handles requests for LinkClick.aspx, but only those specifc
54+ /// to file serving
55+ /// </summary>
56+ /// <param name="context">System.Web.HttpContext)</param>
57+ /// <param name="id"></param>
58+ /// <remarks>
59+ /// </remarks>
60+ /// -----------------------------------------------------------------------------
61+ public static string GetFileNameFromPath ( HttpContext context , string id )
62+ {
63+ string retval = string . Empty ;
64+
65+ var fileId = GetFileIdFromFilename ( id ) ;
66+ if ( fileId > 0 )
67+ {
68+ retval = GetFileNameIfAllowed ( context , fileId ) ;
69+ }
70+ else
71+ {
72+ DotNetNuke . Services . Exceptions . Exceptions . ProcessHttpException ( $ "fileid not found for Id { id } ") ;
73+ }
74+ return retval ;
75+ }
76+
77+ private static int GetFileIdFromFilename ( string id )
78+ {
79+ var piece = id . Split ( '\\ ' ) ;
80+
81+ if ( piece . Length != 2 ) throw new ArgumentOutOfRangeException ( nameof ( id ) ) ;
82+ if ( ! piece [ 1 ] . EndsWith ( ".axd" ) ) throw new ArgumentOutOfRangeException ( nameof ( id ) ) ;
83+
84+ int portalid ;
85+ int . TryParse ( piece [ 0 ] , out portalid ) ;
86+ var hash = piece [ 1 ] . Substring ( 0 , piece [ 1 ] . Length - 4 ) ;
87+
88+ var coll = new NameValueCollection { { "fileticket" , hash } , { "portalid" , portalid . ToString ( ) } } ;
89+ return FileLinkClickController . Instance . GetFileIdFromLinkClick ( coll ) ;
90+ }
91+
92+ private static string GetFileNameIfAllowed ( HttpContext context , int fileId )
93+ {
94+ string retval = string . Empty ;
95+ try
96+ {
97+ var url = "FileID=" + fileId ;
98+ var file = FileManager . Instance . GetFile ( int . Parse ( UrlUtils . GetParameterValue ( url ) ) ) ;
99+ if ( file != null && file . IsImageFile ( ) )
100+ {
101+ if ( ! file . IsEnabled /*|| !HasAPublishedVersion(file)*/ )
102+ {
103+ if ( context . Request . IsAuthenticated )
104+ {
105+ context . Response . Redirect ( Globals . AccessDeniedURL ( DotNetNuke . Services . Localization . Localization . GetString ( "FileAccess.Error" ) ) , true ) ;
106+ }
107+ else
108+ {
109+ context . Response . Redirect ( Globals . AccessDeniedURL ( ) , true ) ;
110+ }
111+ }
112+ else
113+ {
114+ var folder = FolderManager . Instance . GetFolder ( file . FolderId ) ;
115+ if ( FolderPermissionController . Instance . CanViewFolder ( folder ) )
116+ {
117+ retval = file . PhysicalPath + ".resources" ;
118+ }
119+ else
120+ {
121+ //UserInfo objUserInfo = UserController.Instance.GetCurrentUserInfo();
122+ //PortalSettings settings = PortalController.Instance.GetCurrentPortalSettings();
123+
124+ //todo uncomment this line when you are able to get current user and current portalsettings
125+ //throw new PermissionsNotMetException("You do not have permission to view this file.");
126+ //todo or better, let retval = the path to a thumbnail file (not allowed) or a file specified by the settings
127+ retval = file . PhysicalPath + ".resources" ;
128+ }
129+ }
130+ }
131+ }
132+ catch ( ThreadAbortException )
133+ {
134+ }
135+ catch ( Exception ex )
136+ {
137+ DotNetNuke . Services . Exceptions . Exceptions . ProcessHttpException ( $ "File not found for fileid { fileId } ") ;
138+ }
139+ return retval ;
140+ }
141+
142+ public static bool IsImageFile ( this IFileInfo file )
143+ {
144+ return ( Globals . glbImageFileTypes + "," ) . IndexOf ( file . Extension . ToLower ( ) . Replace ( "." , "" ) + "," ) > - 1 ;
145+ }
146+ }
147+ }
0 commit comments