Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/sharedra...
File: fixwpcron.py
#!/opt/imh-python/bin/python3
[0] Fix | Delete
"""wp-cron was a hacky Wordpress feature added to compensate for Windows's
[1] Fix | Delete
lack of scheduling system. Certain plugins can trigger very poor behavior that
[2] Fix | Delete
causes it to be run every time anything happens on the site. The end result is
[3] Fix | Delete
dozens of thousands of redundant hits to wp-cron.php. This script disables the
[4] Fix | Delete
wp-cron feature, and sets up a cronjob to run once every three hours"""
[5] Fix | Delete
[6] Fix | Delete
[7] Fix | Delete
import sys
[8] Fix | Delete
import os
[9] Fix | Delete
import getpass
[10] Fix | Delete
import random
[11] Fix | Delete
import argparse
[12] Fix | Delete
import crontab
[13] Fix | Delete
from rads import is_cpuser
[14] Fix | Delete
[15] Fix | Delete
[16] Fix | Delete
def parse_args():
[17] Fix | Delete
"""Parse commandline args"""
[18] Fix | Delete
parser = argparse.ArgumentParser(description=__doc__)
[19] Fix | Delete
group = parser.add_mutually_exclusive_group(required=True)
[20] Fix | Delete
# fmt: off
[21] Fix | Delete
group.add_argument(
[22] Fix | Delete
'--path', '-p', metavar='PATH',
[23] Fix | Delete
type=os.path.realpath,
[24] Fix | Delete
help='path to wordpress installation. If using this argument, we assume'
[25] Fix | Delete
' the customer has not modified the site to have wp-config.php in an '
[26] Fix | Delete
'alternate directory. Both wp-config.php and wp-cron.php should be '
[27] Fix | Delete
'present in the supplied directory',
[28] Fix | Delete
)
[29] Fix | Delete
all_help = """Set this up for every WordPress installation that the user
[30] Fix | Delete
owns. Installations modified to have wp-config.php in an alternate folder
[31] Fix | Delete
for security are supported; wp-config.php and wp-cron.php files will
[32] Fix | Delete
be located separately
[33] Fix | Delete
"""
[34] Fix | Delete
current_user = getpass.getuser()
[35] Fix | Delete
if current_user == 'root':
[36] Fix | Delete
group.add_argument('--user', '-u', help=all_help)
[37] Fix | Delete
else:
[38] Fix | Delete
group.add_argument('--all', '-a', action='store_true', help=all_help)
[39] Fix | Delete
# fmt: on
[40] Fix | Delete
args = parser.parse_args()
[41] Fix | Delete
if args.path:
[42] Fix | Delete
if not os.path.isdir(args.path):
[43] Fix | Delete
sys.exit(f'{args.path} does not exist')
[44] Fix | Delete
try:
[45] Fix | Delete
assert args.path.split('/')[1] == 'home'
[46] Fix | Delete
args.user = args.path.split('/')[2]
[47] Fix | Delete
except (AssertionError, IndexError):
[48] Fix | Delete
sys.exit("Incorrect path. Should be in a user's homedir")
[49] Fix | Delete
if not hasattr(args, 'all'):
[50] Fix | Delete
args.all = False
[51] Fix | Delete
if args.all:
[52] Fix | Delete
args.user = current_user
[53] Fix | Delete
if not is_cpuser(args.user):
[54] Fix | Delete
sys.exit(f'{args.user} is not a cPanel user')
[55] Fix | Delete
return args
[56] Fix | Delete
[57] Fix | Delete
[58] Fix | Delete
def turn_off(filename):
[59] Fix | Delete
"""Sticks the DISABLE_WP_CRON line in wp-config"""
[60] Fix | Delete
with open(filename, encoding='utf-8') as handle:
[61] Fix | Delete
linelist = handle.readlines()
[62] Fix | Delete
for index, line in enumerate(linelist):
[63] Fix | Delete
if 'DB_USER' in line:
[64] Fix | Delete
if linelist[index + 1] == "define('DISABLE_WP_CRON', 'true');\n":
[65] Fix | Delete
print('File already modified:', filename)
[66] Fix | Delete
return
[67] Fix | Delete
if linelist[index + 1] == '\n':
[68] Fix | Delete
linelist[index + 1] = "define('DISABLE_WP_CRON', 'true');\n"
[69] Fix | Delete
else:
[70] Fix | Delete
print(
[71] Fix | Delete
'Non-standard wp-config file at {}. Add this manually '
[72] Fix | Delete
'under DB_USER:'.format(filename),
[73] Fix | Delete
"define('DISABLE_WP_CRON', 'true');",
[74] Fix | Delete
sep='\n',
[75] Fix | Delete
)
[76] Fix | Delete
return
[77] Fix | Delete
break
[78] Fix | Delete
with open(filename, 'w', encoding='utf-8') as handle:
[79] Fix | Delete
handle.writelines(linelist)
[80] Fix | Delete
[81] Fix | Delete
[82] Fix | Delete
def setup_cron(user, path, not_root):
[83] Fix | Delete
"""Setup php -q wp-cron.php system cron"""
[84] Fix | Delete
if not_root:
[85] Fix | Delete
ctab = crontab.CronTab()
[86] Fix | Delete
else:
[87] Fix | Delete
ctab = crontab.CronTab(user=user)
[88] Fix | Delete
cmd = f'cd {path}; php -q wp-cron.php'
[89] Fix | Delete
if len([x for x in ctab.find_command(cmd) if x.is_enabled()]) > 0:
[90] Fix | Delete
print('Cron already enabled:', cmd)
[91] Fix | Delete
return
[92] Fix | Delete
job = ctab.new(command=cmd)
[93] Fix | Delete
job.hour.every(3)
[94] Fix | Delete
job.minute.on(random.randint(0, 59))
[95] Fix | Delete
ctab.write()
[96] Fix | Delete
[97] Fix | Delete
[98] Fix | Delete
def find_files(user):
[99] Fix | Delete
"""Find wp-config.php files and dirs containing wp-cron.php"""
[100] Fix | Delete
crondirs = [] # directories containing wp-cron.php
[101] Fix | Delete
configs = [] # wp-config.php files
[102] Fix | Delete
for root, _, filenames in os.walk(os.path.join('/home', user)):
[103] Fix | Delete
if 'wp-config.php' in filenames:
[104] Fix | Delete
configs.append(os.path.join(root, 'wp-config.php'))
[105] Fix | Delete
if 'wp-cron.php' in filenames:
[106] Fix | Delete
crondirs.append(root)
[107] Fix | Delete
return configs, crondirs
[108] Fix | Delete
[109] Fix | Delete
[110] Fix | Delete
def main():
[111] Fix | Delete
"""Main program logic"""
[112] Fix | Delete
args = parse_args()
[113] Fix | Delete
if args.path:
[114] Fix | Delete
configs = [os.path.join(args.path, 'wp-config.php')]
[115] Fix | Delete
crondirs = [args.path]
[116] Fix | Delete
else:
[117] Fix | Delete
configs, crondirs = find_files(args.user)
[118] Fix | Delete
if len(configs) > 0:
[119] Fix | Delete
print("wp-config.php file(s):")
[120] Fix | Delete
print(*configs, sep='\n')
[121] Fix | Delete
print()
[122] Fix | Delete
if len(crondirs) > 0:
[123] Fix | Delete
print('dir(s) to setup system crons for wp-cron.php:')
[124] Fix | Delete
print(*crondirs, sep='\n')
[125] Fix | Delete
print()
[126] Fix | Delete
for path in configs:
[127] Fix | Delete
try:
[128] Fix | Delete
turn_off(path)
[129] Fix | Delete
except OSError:
[130] Fix | Delete
print('Error reading/writing to', path)
[131] Fix | Delete
for path in crondirs:
[132] Fix | Delete
setup_cron(args.user, path, args.all)
[133] Fix | Delete
print('done.')
[134] Fix | Delete
[135] Fix | Delete
[136] Fix | Delete
if __name__ == '__main__':
[137] Fix | Delete
main()
[138] Fix | Delete
[139] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function