Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
6.1 KiB

  1. Attribute VB_Name = "BrokenLinkDetection"
  2. Option Explicit
  3. Private Const HTTP_C As String = "http://"
  4. Private Const HTTP_LEN_C As Long = 7
  5. Private Const HCP_SERVICES_C As String = "hcp://services/"
  6. Private Const HCP_SERVICES_LEN_C As Long = 15
  7. Private Const APP_C As String = "app:"
  8. Private Const APP_LEN_C As Long = 4
  9. Private Const MS_ITS_HELP_LOCATION_C As String = "ms-its:%help_location%\"
  10. Private Const MS_ITS_HELP_LOCATION_LEN_C As Long = 23
  11. Private Const HCP_HELP_C As String = "hcp://help/"
  12. Private Const HCP_HELP_LEN_C As Long = 11
  13. Private Const HCP_SYSTEM_C As String = "hcp://system/"
  14. Private Const HCP_SYSTEM_LEN_C As Long = 13
  15. Private Const HCP_C As String = "hcp://"
  16. Private Const HCP_LEN_C As Long = 6
  17. Private Const HELP_DIR_C As String = "\help\"
  18. Private Const SYSTEM_DIR_C As String = "\pchealth\helpctr\system\"
  19. Private Const VENDORS_DIR_C As String = "\pchealth\helpctr\vendors\"
  20. Public Function LinkValid( _
  21. ByRef i_strWinDir As String, _
  22. ByRef i_strVendor As String, _
  23. ByRef i_strURI As String, _
  24. ByRef o_strTransformedURI As String _
  25. ) As Boolean
  26. ' Assume that i_strWinDir = c:\windows
  27. '
  28. ' http://...
  29. ' hcp://services/...
  30. ' app:...
  31. '
  32. ' MS-ITS:%HELP_LOCATION%\abc\def.chm::/ghi/jkl.htm ->
  33. ' c:\windows\help\abc\def.chm\ghi\jkl.htm
  34. '
  35. ' hcp://help/abc/def/ghi.htm ->
  36. ' c:\windows\help\abc\def\ghi.htm
  37. '
  38. ' hcp://system/abc/def/ghi.htm ->
  39. ' c:\windows\pchealth\helpctr\system\abc.chm\def\ghi.htm
  40. ' c:\windows\pchealth\helpctr\system\abc\def.chm\ghi.htm
  41. ' c:\windows\pchealth\helpctr\system\abc\def\ghi.htm
  42. '
  43. ' hcp://<Vendor>/abc/def/ghi.htm ->
  44. ' abc/def/ghi.htm
  45. ' c:\windows\pchealth\helpctr\vendors\<Vendor>\abc.chm\def\ghi.htm
  46. ' c:\windows\pchealth\helpctr\vendors\<Vendor>\abc\def.chm\ghi.htm
  47. ' c:\windows\pchealth\helpctr\vendors\<Vendor>\abc\def\ghi.htm
  48. '
  49. ' If (i_strURI in recognized format) Then
  50. ' If (transformations exist) Then
  51. ' If (transformation refers to existing file) Then
  52. ' LinkValid = True
  53. ' o_strTransformedURI = transformation
  54. ' Else
  55. ' LinkValid = False
  56. ' End If
  57. ' Else
  58. ' LinkValid = True
  59. ' o_strTransformedURI = i_strURI
  60. ' End If
  61. ' Else
  62. ' LinkValid = False
  63. ' End If
  64. Dim FSO As Scripting.FileSystemObject
  65. Dim strURI As String
  66. Dim strNewURI As String
  67. Dim strVendor As String
  68. Dim str As String
  69. Dim intIndex As Long
  70. Dim intLength As Long
  71. Set FSO = New Scripting.FileSystemObject
  72. strURI = LCase$(i_strURI)
  73. ' GetAbsolutePathName replaces / and \\ by \
  74. If ((strURI = "") Or _
  75. (Left$(strURI, HTTP_LEN_C) = HTTP_C) Or _
  76. (Left$(strURI, HCP_SERVICES_LEN_C) = HCP_SERVICES_C) Or _
  77. (Left$(strURI, APP_LEN_C) = APP_C)) Then
  78. ' recognized format
  79. ' no transformations exist
  80. LinkValid = True
  81. o_strTransformedURI = i_strURI
  82. Exit Function
  83. End If
  84. If (InStr(strURI, ":") = 0) Then
  85. strURI = HCP_C & i_strVendor & "/" & strURI
  86. End If
  87. If (Left$(strURI, MS_ITS_HELP_LOCATION_LEN_C) = MS_ITS_HELP_LOCATION_C) Then
  88. strNewURI = i_strWinDir & HELP_DIR_C & Mid$(i_strURI, MS_ITS_HELP_LOCATION_LEN_C + 1)
  89. strNewURI = Replace$(strNewURI, "::", "\")
  90. strNewURI = FSO.GetAbsolutePathName(strNewURI)
  91. If (FileExists(strNewURI)) Then
  92. LinkValid = True
  93. o_strTransformedURI = strNewURI
  94. Else
  95. LinkValid = False
  96. End If
  97. ElseIf (Left$(strURI, HCP_HELP_LEN_C) = HCP_HELP_C) Then
  98. strNewURI = i_strWinDir & HELP_DIR_C & Mid$(i_strURI, HCP_HELP_LEN_C + 1)
  99. strNewURI = FSO.GetAbsolutePathName(strNewURI)
  100. If (FileExists(strNewURI)) Then
  101. LinkValid = True
  102. o_strTransformedURI = strNewURI
  103. Else
  104. LinkValid = False
  105. End If
  106. ElseIf (Left$(strURI, HCP_SYSTEM_LEN_C) = HCP_SYSTEM_C) Then
  107. str = Mid$(i_strURI, HCP_SYSTEM_LEN_C + 1)
  108. LinkValid = p_TestPaths(FSO, i_strWinDir & SYSTEM_DIR_C, str, o_strTransformedURI)
  109. ElseIf (Left$(strURI, HCP_LEN_C) = HCP_C) Then
  110. ' Assume that its hcp://<Vendor>
  111. intIndex = InStr(HCP_LEN_C + 1, strURI, "/")
  112. If (intIndex = 0) Then
  113. LinkValid = False
  114. Exit Function
  115. End If
  116. strVendor = Mid$(strURI, HCP_LEN_C + 1, intIndex - HCP_LEN_C - 1)
  117. str = Mid$(strURI, intIndex + 1)
  118. LinkValid = p_TestPaths(FSO, i_strWinDir & VENDORS_DIR_C & strVendor & "\", str, _
  119. o_strTransformedURI)
  120. Else
  121. ' unrecognized format
  122. LinkValid = False
  123. End If
  124. End Function
  125. Private Function p_TestPaths( _
  126. ByRef i_FSO As Scripting.FileSystemObject, _
  127. ByRef i_strPrefix As String, _
  128. ByRef i_strPathIn As String, _
  129. ByRef o_strPathOut As String _
  130. ) As Boolean
  131. Dim str As String
  132. Dim intLength As Long
  133. Dim intIndex As Long
  134. Dim strPathOut As String
  135. ' i_strPrefix is something like c:\windows\pchealth\helpctr\system\
  136. ' i_strPathIn is something like abc/def/ghi.htm
  137. ' This function tests to see if any of these paths exist:
  138. ' c:\windows\pchealth\helpctr\system\abc.chm\def\ghi.htm
  139. ' c:\windows\pchealth\helpctr\system\abc\def.chm\ghi.htm
  140. ' c:\windows\pchealth\helpctr\system\abc\def\ghi.htm
  141. ' If a path exists, then o_strPathOut is set to that path and the function
  142. ' returns True. Otherwise, it returns False
  143. str = Replace$(i_strPathIn, "/", "\")
  144. intLength = Len(str)
  145. For intIndex = 1 To intLength
  146. If (Mid$(str, intIndex, 1) = "\") Then
  147. strPathOut = i_strPrefix & _
  148. Mid$(str, 1, intIndex - 1) & ".chm" & _
  149. Mid$(str, intIndex)
  150. If (FileExists(strPathOut)) Then
  151. p_TestPaths = True
  152. o_strPathOut = i_FSO.GetAbsolutePathName(strPathOut)
  153. Exit Function
  154. End If
  155. End If
  156. Next
  157. p_TestPaths = False
  158. End Function