使用PHP验证Crontab条目 [英] Validating Crontab Entries with PHP

查看:116
本文介绍了使用PHP验证Crontab条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PHP验证crontab条目的最佳方法是什么?我应该使用正则表达式还是外部库?我有一个PHP脚本添加/删除条目从crontab文件,但想要一些方法来验证时间间隔部分是一个有效的格式。

解决方案

嗯,有趣的问题。



如果你要真正验证它,regex是不够的,您必须实际解析该条目并验证每个调度位。这是因为每个位可以是一个数字,一个月/日的星期字符串,范围(2-7),集(3,4,星期六),Vixie cron风格的快捷方式(60/5)或任何组合



只要使用 crontab 程序的Vixie cron验证是不够的,因为它实际上没有完全验证!



Dave Taylor的邪恶酷壳脚本( Google图书链接)有一个sh脚本进行部分验证,我发现讨论很有趣。你还可以使用或修改代码。



我还打开了链接到两个PHP类,你说的(我的质量没有评估) p>



另一种方法(取决于你的应用程序需要做什么)可能是PHP以编程方式构建crontab条目并插入它,所以你知道它总是有效,而不是尝试验证一个不可信的字符串。然后你只需要创建一个构建一个crontab条目UI,如果你不需要真正复杂的调度组合,这可能很简单。


What is the best way to validate a crontab entry with PHP? Should I be using a regex, or an external library? I've got a PHP script that adds/removes entries from a crontab file, but want to have some way to verify that the time interval portion is in a valid format.

解决方案

Hmmm, interesting problem.

If you're going to really validate it, regex isn't going to be enough, you'll have to actually parse the entry and validate each of the scheduling bits. That's because each bit can be a number, a month/day of the week string, a range (2-7), a set (3, 4, Saturday), a Vixie cron-style shortcut (60/5) or any combination of the above -- any single regex approach is going to get very hairy, fast.

Just using the crontab program of Vixie cron to validate isn't sufficient, because it actually doesn't validate completely! I can get crontab to accept all sorts of illegal things.

Dave Taylor's Wicked Cool Shell Scripts (Google books link) has a sh script that does partial validation, I found the discussion interesting. You might also use or adapt the code.

I also turned up links to two PHP classes that do what you say (whose quality I haven't evaluated):

Another approach (depending on what your app needs to do) might be to have PHP construct the crontab entry programatically and insert it, so you know it's always valid, rather than try to validate an untrusted string. Then you would just need to make a "build a crontab entry" UI, which could be simple if you don't need really complicated scheduling combinations.

这篇关于使用PHP验证Crontab条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆