1. Trang chủ
  2. » Công Nghệ Thông Tin

Secure PHP Development- P120 pptx

5 110 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 128,27 KB

Nội dung

Listing 16-4 (Continued) function getCommandLineOptions($options) { $type = gettype($options); if (gettype($options) != “array”) { // Error in command line echo “$options->message \n”; return null; } $cmd = array(); foreach ($options[0] as $argArray) { $argName = preg_replace(‘/[^\w]/’ , ‘’, $argArray[0]); $argValue = $argArray[1]; $cmd[$argName] = ($argValue != ‘’) ? $argValue : TRUE; } return (count($cmd) > 0) ? $cmd : null; } ?> Here, getCommandLineOptions() is passed the output of the Console_Getopt::getopt() function. The Console_Getopt::getopt() function takes $GLOBALS[‘argv’], $CMD_SHORT_OPTIONS, and $CMD_LONG_OPTIONS as argu- ments. The $GLOBALS[‘argv’] argument is same as $argv, which holds the user- supplied command-line arguments. The second argument, $CMD_SHORT_OPTIONS, is a list of short argument options such as ‘hs:’. These are the options that a user can enter as –h and -s size. The -s option takes a value and therefore ‘:’ is added in the $CMD_SHORT_OPTIONS string to indicate that. The $CMD_LONG_OPTIONS is set to an array such as array(‘help’, ‘size=’). These arguments can be entered by the user as –help and size=size. 566 Part IV: Using PHP for Sysadmin Tasks 21 549669 ch16.qxd 4/4/03 9:26 AM Page 566 The syntax for short arguments in Console_Getopt::getopt() is fairly simple. List each short argument, order doesn’t matter. If an argument requires a parameter (like “s” in the listing above) you follow it with a colon (as we did above). If the parameter is optional, you add two colons. The short and the long argument lists are the expected lists of arguments. The Console_Getopt::getopt() function returns an error object if the user enters an invalid argument that is not listed in the short or long expected argument list passed to it. If the user enters valid arguments and argument options, the getopt() method returns the list of options supplied by the user in an array. The getCommandLineOptions() function goes through the valid list of user-entered arguments, removes - characters from each argument, and makes an associative array called $cmd, using the argument name as the key and the argument value as the value. For example, if the user runs this script as follows: ./cmd_options.php -h -s 100 the $cmd array looks like the following: Array ( [h] => 1 [s] => 100 ) Similarly, if the user runs it as ./cmd_options.php -h size 100 king=burger the $cmd array looks like the following: Array ( [h] => 1 [size] => 100 [king] => burger ) Chapter 16: Command-Line PHP Utilities 567 21 549669 ch16.qxd 4/4/03 9:26 AM Page 567 Notice that arguments that do not have a value (such as -h) have a value of “1” in the $cmd array. Since “1” evaluates to TRUE in Boolean expressions, when this array is returned, you can easily find out which command-line arguments were selected. In this script, it uses the built-in isset() function to determine whether a known argument is set, and prints a message indicating the result. For example: ./cmd_options.php -h Returns: You selected help option. This shows that -h has been supplied. To add a new short argument called -z, we can simply update the $CMD_SHORT_OPTIONS list. Similarly, to add a long argument called -zero, we can add this argument in $CMD_LONG_OPTIONS. For example, to allow a new long argu- ment called -count that expects a value, we can add ‘count=’ in the following array: $CMD_LONG_OPTIONS = array(‘help’, ‘size=’, ‘count=’); Then we can write code to determine whether the user has chosen this argument or not. For example: if (isset($cmd[‘count’])) { echo “You entered count value of “. $cmd[‘count’] . “\n”; } Now that you know how to deal with command line PHP scripting along with argument handling, let’s develop some interesting command-line scripts. In the fol- lowing section, you will develop a simple reminder tool. Most chapters in this book refer you to the accompanying CD-ROM for source listings.Because the tools discussed in this chapter are very small, we include the source listing so that you can see the entire code while reading the chapters. 568 Part IV: Using PHP for Sysadmin Tasks 21 549669 ch16.qxd 4/4/03 9:26 AM Page 568 Building a Simple Reminder Tool In this day and age, who does not need to be reminded of something? In this sec- tion, you will develop a reminder utility that runs on a Linux system as a daily cron job. A cron job is simply another name for a scheduled task. Linux and other UNIX systems have a daemon program called cron, which runs other programs at given intervals. Linux contains predefined cron directories in the main system configura- tion /etc directory, including the following: ◆ /etc/cron.daily: scripts and programs that are run once a day ◆ cron.hourly: scripts and programs that are run once every hour ◆ cron.monthly: scripts and programs that are run once a month ◆ cron.weekly: scripts and programs that are run once a week You can generally create a symbolic link (symlink) from the program you want to run via cron to one of these directories and get it run on the predefined schedule. If you want to know when an hourly,daily, weekly, or monthly cron job is run from the aforementioned scripts, look at /etc/crontab, which shows the time when these directories are processed by the cron daemon.You might have to consult the crontab manual pages (man crontab) to understand the cryptic time assignments. The user who needs more information on cron should start with the man 5 crontab command, which describes the basic syntax of the crontab file and operation of cron in general. Note that if you are not a privileged user, you will want to explore the cron –l and cron –e commands for dealing with cron and crontab.Although this section deals mainly with Linux and cron, Windows users can use the Windows Scheduler application to schedule tasks to run at preset time(s). We will develop this reminder tool and symbolically link it inside the /etc/cron.daily so that the reminder tool is run once a day automatically. In most systems, daily tasks are run very early in the morning (for example, 4:00 A.M.). First, let’s examine what our reminder system will offer. Chapter 16: Command-Line PHP Utilities 569 21 549669 ch16.qxd 4/4/03 9:26 AM Page 569 Features of the reminder tool The reminder tool will offer the following features: ◆ It will process reminders for all users on the system. ◆ All users can have their own set of reminders, which they can manage using configuration files and message templates. ◆ Each reminder can be sent using a separate message template. ◆ Types of reminders supported are daily, weekly, monthly, and yearly. ◆ For each type of reminder, there can be an unlimited number of reminders per user. Now let’s implement this tool. Implementing the reminder tool Listing 16-5 shows a configuration file that we will use for our reminder tool. The DEBUG constant will be used for enabling and disabling debug messages. The PASSWD_FILE constant points to the system’s password file. You can use a different file, but two fields are required by the reminder system: username and home directory. If you use a custom user list file, make sure it mimics the /etc/passwd file in terms of the formatting and placement of username, and the home directory field. Reading man htpasswd and/or man passwd will help with managing and mimicking the /etc/passwd file. The $XMAILER variable is used to name the reminder in an X-header for each mail message sent. This can be set to anything. The USER_REMINDER_DIR constant is used to store the expected name of the user’s reminder directory. Each user can have a reminder directory inside the home directory as ~username/%USER_REMINDER_DIR/. For example, ~kabir/reminders is the default reminder directory per the following configuration for the user kabir. Each user’s reminder configuration is stored in the reminders.txt file pointed to by the USER_REMINDER_FILE constant. This file must reside in a directory specified by USER_REMINDER_DIR, within a user’s home directory. For example, the user sheila, whose home directory is /home/sheila (i.e., ~sheila), can have /home/sheila/ reminders/reminders.txt as the full path for her reminder configuration file. The configuration file is as follows: 570 Part IV: Using PHP for Sysadmin Tasks 21 549669 ch16.qxd 4/4/03 9:26 AM Page 570 . as follows: ./cmd_options .php -h -s 100 the $cmd array looks like the following: Array ( [h] => 1 [s] => 100 ) Similarly, if the user runs it as ./cmd_options .php -h size 100 king=burger the. ‘size=’). These arguments can be entered by the user as –help and size=size. 566 Part IV: Using PHP for Sysadmin Tasks 21 549669 ch16.qxd 4/4/03 9:26 AM Page 566 The syntax for short arguments. following: Array ( [h] => 1 [size] => 100 [king] => burger ) Chapter 16: Command-Line PHP Utilities 567 21 549669 ch16.qxd 4/4/03 9:26 AM Page 567 Notice that arguments that do not have

Ngày đăng: 07/07/2014, 07:20