mayankjoin3

python code containing href

Jul 16th, 2026
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.42 KB | None | 0 0
  1. from pathlib import Path
  2. import re
  3.  
  4. # Directory containing the PHP files
  5. ROOT_DIRECTORY = Path(r"C:\Users\Mayank\Documents\GitHub\ERP")
  6.  
  7. # Output text file
  8. OUTPUT_FILE = ROOT_DIRECTORY / "php_search_results.txt"
  9.  
  10. # Exclude files when any folder in their path contains these terms
  11. EXCLUDED_FOLDER_TERMS = ("tcpdf", "phpqrcode")
  12.  
  13. # Case-insensitive search patterns
  14. SEARCH_PATTERNS = {
  15.     "href and location on same line": re.compile(
  16.         r"(?=.*\bhref\b)(?=.*\blocation\b)",
  17.         re.IGNORECASE,
  18.     ),
  19.     "window.location.href": re.compile(
  20.         r"\bwindow\s*\.\s*location\s*\.\s*href\b",
  21.         re.IGNORECASE,
  22.     ),
  23.     "Location:": re.compile(
  24.         r"\blocation\s*:",
  25.         re.IGNORECASE,
  26.     ),
  27. }
  28.  
  29.  
  30. def is_excluded(file_path: Path) -> bool:
  31.     """Return True when a folder in the path contains an excluded term."""
  32.     relative_parts = file_path.relative_to(ROOT_DIRECTORY).parts[:-1]
  33.  
  34.     return any(
  35.         excluded_term in folder_name.lower()
  36.         for folder_name in relative_parts
  37.         for excluded_term in EXCLUDED_FOLDER_TERMS
  38.     )
  39.  
  40.  
  41. def find_matching_patterns(line: str) -> list[str]:
  42.     """Return the names of all patterns matched by a line."""
  43.     return [
  44.         pattern_name
  45.         for pattern_name, pattern in SEARCH_PATTERNS.items()
  46.         if pattern.search(line)
  47.     ]
  48.  
  49.  
  50. def search_php_files() -> None:
  51.     if not ROOT_DIRECTORY.exists():
  52.         raise FileNotFoundError(
  53.             f"Directory does not exist: {ROOT_DIRECTORY}"
  54.         )
  55.  
  56.     matched_files = 0
  57.     matched_lines = 0
  58.     scanned_files = 0
  59.  
  60.     with OUTPUT_FILE.open("w", encoding="utf-8") as output:
  61.         output.write(f"Search directory: {ROOT_DIRECTORY}\n")
  62.         output.write(
  63.             "Excluded folder terms: "
  64.             + ", ".join(EXCLUDED_FOLDER_TERMS)
  65.             + "\n"
  66.         )
  67.         output.write("=" * 100 + "\n\n")
  68.  
  69.         for php_file in ROOT_DIRECTORY.rglob("*.php"):
  70.             if not php_file.is_file() or is_excluded(php_file):
  71.                 continue
  72.  
  73.             scanned_files += 1
  74.             file_matches = []
  75.  
  76.             try:
  77.                 # errors="replace" prevents one badly encoded file
  78.                 # from stopping the entire search.
  79.                 with php_file.open(
  80.                     "r",
  81.                     encoding="utf-8",
  82.                     errors="replace",
  83.                 ) as source_file:
  84.                     for line_number, line in enumerate(source_file, start=1):
  85.                         matched_patterns = find_matching_patterns(line)
  86.  
  87.                         if matched_patterns:
  88.                             file_matches.append(
  89.                                 (
  90.                                     line_number,
  91.                                     line.rstrip("\r\n"),
  92.                                     matched_patterns,
  93.                                 )
  94.                             )
  95.  
  96.             except OSError as error:
  97.                 output.write(f"ERROR READING: {php_file}\n")
  98.                 output.write(f"{error}\n")
  99.                 output.write("-" * 100 + "\n\n")
  100.                 continue
  101.  
  102.             if not file_matches:
  103.                 continue
  104.  
  105.             matched_files += 1
  106.             matched_lines += len(file_matches)
  107.  
  108.             output.write(f"File: {php_file}\n")
  109.             output.write(
  110.                 f"Relative path: {php_file.relative_to(ROOT_DIRECTORY)}\n"
  111.             )
  112.             output.write("-" * 100 + "\n")
  113.  
  114.             for line_number, line_text, matched_patterns in file_matches:
  115.                 output.write(
  116.                     f"Line {line_number} "
  117.                     f"[{', '.join(matched_patterns)}]\n"
  118.                 )
  119.                 output.write(f"{line_text}\n\n")
  120.  
  121.             output.write("=" * 100 + "\n\n")
  122.  
  123.         output.write("\nSUMMARY\n")
  124.         output.write("-" * 100 + "\n")
  125.         output.write(f"PHP files scanned: {scanned_files}\n")
  126.         output.write(f"Files containing matches: {matched_files}\n")
  127.         output.write(f"Matching lines: {matched_lines}\n")
  128.  
  129.     print("Search completed.")
  130.     print(f"PHP files scanned: {scanned_files}")
  131.     print(f"Files containing matches: {matched_files}")
  132.     print(f"Matching lines: {matched_lines}")
  133.     print(f"Results exported to: {OUTPUT_FILE}")
  134.  
  135.  
  136. if __name__ == "__main__":
  137.     try:
  138.         search_php_files()
  139.     except Exception as error:
  140.         print(f"Search failed: {error}")
Advertisement
Add Comment
Please, Sign In to add comment