-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlinkCheck.cfc
206 lines (181 loc) · 7.42 KB
/
linkCheck.cfc
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
<!---
Copyright 2011 Blue River Interactive
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--->
<cfcomponent output="false" extends="mura.cfobject">
<cffunction name="testLink" access="public" output="no" returntype="boolean">
<cfargument name="link" required="yes">
<cfset var tempLink = "">
<cfset var tempList = "">
<cfset var i = "">
<cfset var returnVar = false>
<cfif link contains "/#session.siteID#/index.cfm/" or (application.configBean.getStub() neq "" and link contains application.configBean.getStub())>
<!--- check local links --->
<cfset tempList = replaceNoCase(link, '/#session.siteID#/index.cfm/', '')>
<cfif listLen(tempList, '/') gte 1>
<cfset tempLink = "">
<cfloop list="#tempList#" delimiters="/" index="i">
<cfset tempLink = tempLink & i & '/'>
</cfloop>
<cfset tempLink = left(tempLink, len(tempLink) - 1)>
<cfquery name="rsCheck" datasource="#application.configBean.getDatasource()#" username="#application.configBean.getDBUsername()#" password="#application.configBean.getDBPassword()#">
select contentID from tcontent
where active = 1 and siteID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#session.siteid#">
and filename = <cfqueryparam cfsqltype="cf_sql_varchar" value="#tempLink#">
</cfquery>
<cfif rsCheck.recordCount eq 0>
<cfset returnVar = true>
</cfif>
</cfif>
<cfelse>
<!--- check external, image or static links --->
<cftry>
<cfif left(link, 4) neq "http">
<cfset tempLink = "http://" & application.settingsManager.getSite(session.siteID).getDomain() & link>
<cfelse>
<cfset tempLink = link>
</cfif>
<cfif len(application.configBean.getProxyServer())>
<cfhttp url="#tempLink#"
method="head"
timeout="1"
proxyUser="#application.configBean.getProxyUser()#"
proxyPassword="#application.configBean.getProxyPassword()#"
proxyServer="#application.configBean.getProxyServer()#"
proxyPort="#application.configBean.getProxyPort()#">
<cfelse>
<cfhttp method="head" url="#tempLink#" timeout="1" />
</cfif>
<cfif findNoCase("404",cfhttp.statusCode) or findNoCase("Connection Failure",cfhttp.statusCode)>
<cfset returnVar = true>
</cfif>
<cfcatch>
<cfset returnVar = true>
</cfcatch>
</cftry>
</cfif>
<cfreturn returnVar>
</cffunction>
<cffunction name="parseLink" access="public" output="no" returntype="string">
<cfargument name="s" required="yes">
<cfargument name="pos" required="yes">
<cfset var returnVar = "">
<cfset var start = 0>
<cfset var end = 0>
<cfset start = refind("(""|')", arguments.s, arguments.pos) + 1>
<cfset end = find(mid(arguments.s, start - 1, 1), arguments.s, start)>
<cfset returnVar = mid(arguments.s, start, end - start)>
<cfreturn returnVar>
</cffunction>
<cffunction name="findLinks" access="public" output="no" returntype="array">
<cfargument name="find" required="yes">
<cfargument name="content" required="yes">
<cfset var pos = 0>
<cfset var continue = true>
<cfset var a = arrayNew(1)>
<cfset var link = "">
<cfset var matches = reMatchNoCase("(<a.*?>.*?</a>)|(<img.*?>)", arguments.content)>
<cfloop from="1" to="#arrayLen(matches)#" index="i">
<cfset pos = 0>
<cfset continue = true>
<cfloop condition="#continue#">
<cfset pos = find(arguments.find, matches[i], pos + 1)>
<cfif pos gt 0>
<cfset link = parseLink(matches[i], pos)>
<cfif testLink(link)>
<cfset arrayAppend(a, link)>
</cfif>
<cfelse>
<cfset continue = false>
</cfif>
</cfloop>
</cfloop>
<cfreturn a>
</cffunction>
<cffunction name="checkSite" access="public" output="yes" returntype="void">
<cfquery name="rsContent" datasource="#application.configBean.getDatasource()#" username="#application.configBean.getDBUsername()#" password="#application.configBean.getDBPassword()#">
select contentID, contentHistID, siteid, parentid, moduleid, type, title, summary, body from tcontent
where active = 1 and siteID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#session.siteid#">
<!---limit 250--->
</cfquery>
<!---
<script>
//document.getElementById('siteCheckStatus').innerHTML = '0% Complete';
display('element1', 0, 1);
</script>
--->
<cfset var brokenLinkCount = 0>
<cfset var list = arrayNew(1)>
<cfloop query="rsContent">
<cfset s = structNew()>
<cfset s.links = arrayNew(1)>
<cfset s.imgs = arrayNew(1)>
<cfset s.contentID = contentID>
<cfset s.contentHistID = contentHistID>
<cfset s.siteid = siteid>
<cfset s.type = type>
<cfset s.parentid = parentid>
<cfset s.moduleid = moduleid>
<cfset s.title = title>
<cfset s.links = findLinks('href', summary & body)>
<cfset s.imgs = findLinks('src', summary & body)>
<cfif arrayLen(s.links) gt 0 or arrayLen(s.imgs) gt 0>
<cfset arrayAppend(list, s)>
<cfset brokenLinkCount = brokenLinkCount + arrayLen(s.links) + arrayLen(s.imgs)>
</cfif>
<cfif rsContent.currentRow mod 5 eq 0>
<script>
//document.getElementById('siteCheckStatus').innerHTML = '<cfoutput>#(rsContent.currentRow / rsContent.recordCount) * 100#</cfoutput>% Complete';
fillProgress('element1', <cfoutput>#(rsContent.currentRow / rsContent.recordCount) * 100#</cfoutput>);
</script>
</cfif>
</cfloop>
<cfsavecontent variable="body">
<cfoutput>
<div id="linkChecker">
<h3>Your site has <strong>#brokenLinkCount#</strong> broken link<cfif brokenLinkCount neq 1>s</cfif> on <strong>#arrayLen(list)#</strong> page<cfif arrayLen(list) neq 1>s</cfif>:</h3>
<dl id="lc-nodeList">
<cfloop from="1" to="#arrayLen(list)#" index="i">
<dt class="divide"><a href="/admin/index.cfm?fuseaction=cArch.edit&siteid=#list[i].siteid#&contentid=#list[i].contentid#&topid=00000000000000000000000000000000001&type=#list[i].type#&parentid=#list[i].parentID#&moduleid=#list[i].moduleID#">#list[i].title#</a></dt>
<cfloop from="1" to="#arrayLen(list[i].links)#" index="j">
<cfset id = createUUID()>
<dd id="#id#">#list[i].links[j]#</dd>
<script type="text/javascript">
$('###id#').editInPlace({
url: "./saveLink.cfm",
params : "editorID=#id#&siteid=#list[i].siteid#&contentHistID=#list[i].contenthistid#"
});
</script>
</cfloop>
<cfloop from="1" to="#arrayLen(list[i].imgs)#" index="k">
<cfset id = createUUID()>
<dd id="#id#">#list[i].imgs[k]#</dd>
<script type="text/javascript">
$('###id#').editInPlace({
url: "./saveLink.cfm",
params : "editorID=#id#&siteid=#list[i].siteid#&contentHistID=#list[i].contenthistid#"
});
</script>
</cfloop>
</cfloop>
</dl>
</cfoutput>
</div>
</cfsavecontent>
<cfif arrayLen(list) eq 0>
<cfset body = '<p class="success">No broken links found.</p>'>
</cfif>
<cffile action="write" file="#expandPath('report.html')#" output="#body#">
<script>
window.location = 'index.cfm?gfa=show';
</script>
</cffunction>
</cfcomponent>