Last active
June 23, 2016 15:36
-
-
Save Midnighter/1553d61c348ca47efba99db1e7dec817 to your computer and use it in GitHub Desktop.
Replace content of php headers in respective files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- encoding: utf-8 -*- | |
""" | |
============================== | |
Replace Content of PHP-Headers | |
============================== | |
:Authors: | |
Moritz E. Beber | |
:Date: | |
2016-06-20 | |
:Copyright: | |
Copyright |c| 2016, Moritz E. Beber, all rights reserved. | |
:File: | |
replace_php_header.py | |
:Source: | |
https://gist.github.com/Midnighter/1553d61c348ca47efba99db1e7dec817 | |
BSD 3-Clause License | |
-------------------- | |
Copyright |c| 2016, Moritz E. Beber, all rights reserved. | |
Redistribution and use in source and binary forms, with or without modification, | |
are permitted provided that the following conditions are met: | |
1. Redistributions of source code must retain the above copyright notice, this | |
list of conditions and the following disclaimer. | |
2. Redistributions in binary form must reproduce the above copyright notice, | |
this list of conditions and the following disclaimer in the documentation and/or | |
other materials provided with the distribution. | |
3. Neither the name of replace_php_header nor the names of its contributors may | |
be used to endorse or promote products derived from this software without | |
specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR | |
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
.. |c| unicode: U+A9 | |
""" | |
from __future__ import (absolute_import, unicode_literals) | |
import argparse | |
import io | |
import sys | |
import os | |
from os.path import (join,) | |
def replace_content(path, required_file, enc="utf-8"): | |
""" | |
Open a file, check for PHP section, replace content with 'require' call. | |
""" | |
with io.open(path, "r", encoding=enc) as fh: | |
content = fh.readlines() | |
if len(content) == 0: | |
return | |
# only work on files that have the header | |
if not content[0].startswith("<?php"): | |
return | |
# find end of database statement, then delete inbetween | |
i = 0 | |
end = 0 | |
for (i, line) in enumerate(content[1:], start=1): | |
line = line.strip() | |
if line.startswith("mysql_select_db"): | |
end = i + 1 | |
break | |
# safeguard | |
if line.startswith("?>"): | |
break | |
if end > 0: | |
del content[1:end] | |
else: | |
return | |
# insert new line | |
content.insert(1, "require('{0}');\n".format(required_file)) | |
# remove end comment line | |
i = 0 | |
end = 0 | |
for (i, line) in enumerate(content[2:], start=2): | |
if line.startswith("*/"): | |
end = i | |
break | |
# safeguard | |
if line.startswith("?>"): | |
break | |
if end > 0: | |
del content[end] | |
else: | |
return | |
with io.open(path, "w", encoding=enc) as fh: | |
fh.writelines(content) | |
def main(args): | |
for (root, dirs, files) in os.walk(args.root): | |
for filename in files: | |
if filename.endswith(".php"): | |
replace_content(join(root, filename), args.require, | |
args.encoding) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="Scans a directory tree for " | |
"PHP files and replaces any content of the opening PHP section with a " | |
"'require' call to one particular other PHP file.") | |
parser.add_argument("root", metavar="<base-dir>", | |
help="Root of directory tree to scan for PHP files.") | |
parser.add_argument("-r", "--require", metavar="<file-path>", | |
default="db_connection.php", | |
help="Absolute or relative path to the PHP-file that " | |
"will be 'required' (default: %(default)r).") | |
parser.add_argument("--encoding", metavar="<file-enc>", | |
default=sys.getdefaultencoding(), | |
help="File encoding to assume (default: %(default)r).") | |
args = parser.parse_args() | |
main(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment