Archive for March, 2008

580 Part IV: Using PHP for Sysadmin Tasks (Web server certificate)

Sunday, March 23rd, 2008

580 Part IV: Using PHP for Sysadmin Tasks Listing 16-8 (Continued) foreach ($entries as $logRecord) { fputs($logFD, $today: $logRecordn ); } fclose($logFD); return TRUE; } function getUsers($userFile = null) { $users = array(); if (!file_exists($userFile)) { return $users; } // For each line in the file // create a entry in users array // as: $users[username] = home_dir foreach ( file($userFile) as $line) { $userInfo = explode( : ,$line); $users[$userInfo[0]] = $userInfo[5]; } return $users; } ?> Here is how this script works: . It first gets a list of users by calling the getUsers() function. This function is given the PASSWD_FILE file name, which is configured in reminder.conf. The user list is returned into $userList. . For each user in $userList, the script calls the doRemind() function, which processes the reminders for the user. Now let s examine each of the functions used in this script.

Chapter 16: Command-Line PHP Utilities 579 (Web hosting) // Weekly

Saturday, March 22nd, 2008

Chapter 16: Command-Line PHP Utilities 579 // Weekly reminder array_push($reminders, $what); } else if ( preg_match( /monthly/i , $type) && ($thisDD == $when) && !empty($what) ) { // Monthly reminder array_push($reminders, $what); } else if ( preg_match( /yearly/i , $type) && (!strcmp($MMDD, $when)) && !empty($what) ) { // Yearly reminder array_push($reminders, $what); } else if (empty($what)) { array_push($logEntries, error in line $lineNumber ($line) ); } } // Write log entries writeLog($logFile, $logEntries); // Remove duplicates from reminder list return array_values(array_unique($reminders)); } function writeLog($logFile = null, $entries = null) { if (count($entries) <1) return FALSE; $logFD = fopen($logFile, a+ ); $today = date( M-d-Y h:i:s A ); if (! $logFD) return FALSE; Continued

Florida web design - 578 Part IV: Using PHP for Sysadmin Tasks

Saturday, March 22nd, 2008

578 Part IV: Using PHP for Sysadmin Tasks Listing 16-8 (Continued) $reminders = array(); $logEntries = array(); // Get today s date $thisMonth = date( M ); $thisMM = date( m ); $thisDay = strtolower(date( D )); $thisDD = date( d ); $MMDD = sprintf( %02d-%02d , $thisMM, $thisDD); $lineNumber = 0; // Parse each line in the user s reminder file foreach ($list as $line) { // Count line number (needed for error reporting) $lineNumber++; // Ignore lines starting with # as comments if (preg_match( /^#/ , $line)) continue; // Ignore lines that are blank $line = ltrim($line); if (preg_match( /^$/ , $line)) continue; $line = substr($line,0, strlen($line)-1); list ($type,$when,$what) = explode( : , $line); if (preg_match( /daily/i , $type)) { // daily reminders have only 2 parts daily:file // so $when will have what we want in $what // Daily reminder array_push($reminders, $when); } else if ( preg_match( /weekly/i , $type) && !strcmp($thisDay, strtolower($when)) && !empty($what) ) {

Chapter 16: Command-Line PHP Utilities 577 Content-Type: text/htmlrn );

Saturday, March 22nd, 2008

Chapter 16: Command-Line PHP Utilities 577 Content-Type: text/htmlrn ); } else { array_push($headers, Content-Type: text/plainn ); } $contentTypeSet = TRUE; } else if (preg_match( /MIME-Version:s*(.+)/i , $str, $match ) ) { array_push($headers, MIME-Version: $match[1] rn ); } else { array_push($message, $str); } } if (! $contentTypeSet) array_push($headers, Content-Type: text/plainrn ); $subject = preg_replace( /<%TODAY%>/i , $today, $subject); $body = implode( , $message); $body = preg_replace( /<%TODAY%>/i , $today, $body); $headerStr = implode( , $headers); if (DEBUG) echo Sending mail to: $to (subject: $subject)n ; return mail($to, $subject, $body, $headerStr); } function getRemindersForToday($list = null, $logFile = null) { Continued

Windows 2003 server web - 576 Part IV: Using PHP for Sysadmin Tasks

Friday, March 21st, 2008

576 Part IV: Using PHP for Sysadmin Tasks Listing 16-8 (Continued) foreach ($lines as $str) { $index++; if (preg_match( /To:s*(.+)/i , $str, $match ) ) { $to = $match[1]; } else if (preg_match( /From:s*(.+)/i , $str, $match ) ) { array_push($headers, From: $match[1] rn ); } else if (preg_match( /Subject:s*(.+)/i , $str, $match ) ) { $subject = $match[1]; } else if (preg_match( /^CC:s*(.+)/i , $str, $match)) { array_push($headers, Cc: $match[1] rn ); } else if (preg_match( /Bcc:s*(.+)/i , $str, $match)) { array_push($headers, Bcc: $match[1] rn ); } else if (preg_match( /Content-Type:s*(.+)/i , $str, $match ) ) { if (preg_match( /html/ , $match[1])) { array_push($headers,

Chapter 16: Command-Line PHP Utilities 575 $userReminderLogFile ); (Cool web site)

Friday, March 21st, 2008

Chapter 16: Command-Line PHP Utilities 575 $userReminderLogFile ); foreach ($mailings as $mail) { $mail = sprintf( %s/%s , $userReminderDir, $mail); if (file_exists($mail)) { if (! doMail($mail, $username)) array_push($logEntries, mail failed! );; } else { array_push($logEntries, cannot find or open $mail ); } } writeLog($userReminderLogFile, $logEntries); } function doMail($file = null, $user = null) { $lines = file($file); $lines = str_replace( rn , n , $lines); $lines = str_replace( r , n , $lines); $lines = str_replace( n , rn , $lines); $today = date( M-d-Y h:i:s A ); $contentTypeSet = FALSE; $message = array(); $headers = array( X-Mailer: . $ XMAILER . rn ); $to = $user; Continued

574 Part IV: Using PHP for Sysadmin Tasks (Crystaltech web hosting)

Thursday, March 20th, 2008

574 Part IV: Using PHP for Sysadmin Tasks Listing 16-8 (Continued) $userList = getUsers(PASSWD_FILE); foreach($userList as $username => $homeDir) { doRemind(USER_REMINDER_DIR, USER_REMINDER_FILE, $username, $homeDir); } exit; function doRemind($userDir = null, $userFile = null, $username = null, $homeDir = null) { $userReminderDir = sprintf( %s/%s , $homeDir, $userDir); $userReminderFile = sprintf( %s/%s , $userReminderDir, $userFile); $userReminderLogFile = sprintf( %s/%s.log , $userReminderDir, $username); $logEntries = array(); if (!file_exists($userReminderFile)) { return USER_FILE_MISSING; } if (DEBUG) echo Processing reminders for $username …n ; if (DEBUG) echo Reminder File $userReminderFile n ; $mailings = getRemindersForToday( file($userReminderFile),

Chapter 16: Command-Line PHP (Web host server) Utilities 573 Now let s

Thursday, March 20th, 2008

Chapter 16: Command-Line PHP Utilities 573 Now let s look at a message file to understand how users can set up the actual reminder message sent to themselves or anyone they choose. Listing 16-7 shows an example reminder message file that can be used in any daily, weekly, monthly, or yearly reminders. Listing 16-7: ~username/reminders/todo.txt From: Reminder To: Alter Ego Cc: mrkabir@hotmail.com Bcc: kabir@sac-home.evoknow.com Content-Type: text/plain Subject: My TODO for <%TODAY%> I need to accomplish the following tasks today: 1. Check and respond to all my pending emails (on going) 2. Call Andrew @ 10:30 AM to discuss remote mgmt contract 3. Call Chad @ 11:30 PM to discuss security contract 4. Lunch with TLT @ 1:00 PM (Geek) 5. Write rest of the article for CMP (2 Hrs) 6. Finish spec for Metrocomia 7. Buy holiday gifts Too much to do, too little time! -alter-ego This is a text reminder file. The From:, To:, Cc:, Bcc:, Content-Type:, and Subject: lines correspond to standard e-mail headers. They are used in sending e-mail messages. For example, if you wanted to send an HTML message as a reminder, you could set Content-Type: text/html instead of text/plain. The message is sent to recipients listed in the To:, Cc:, and Bcc: fields. You do not have to have Cc: or Bcc: fields. Listing 16-8 shows the reminder.php script that implements the reminder tool. Listing 16-8: reminder.php #!/usr/bin/php -q

Web server version - 572 Part IV: Using PHP for Sysadmin Tasks

Wednesday, March 19th, 2008

572 Part IV: Using PHP for Sysadmin Tasks Listing 16-6 (Continued) yearly:12-10:12-10.txt yearly:07-22:ak_birthday.txt yearly:07-23:ak_birthday.txt yearly:07-02:mk_birthday.txt yearly:07-03:mk_birthday.txt As you can see, this is a plain-text reminder configuration file. The lines that are blank or start with the # character are ignored by the reminder tool, so a user can use them to keep the reminder configuration readable and commented. There are four types of configuration lines: daily:reminder_mail_file.txt weekly:week_day:reminder_mail_file.txt monthly:MM:reminder_mail_file.txt yearly:MM-DD:reminder_mail_file.txt The first line (daily) defines a daily reminder. Whenever such a line is processed by the reminder system, it reads the named file (reminder_mail_file.txt) and sends e-mail to the user or anyone else specified (in the To: field) within the file. The second line (weekly) defines a weekly reminder. The reminder is sent only if the weekday (mon, tue, wed, thu, fri, sat, sun) reminder tool running corresponds with the same day as the current day. For example, if the reminder tool is running on a Saturday (sat) and a line such as the following is found: weekly:sat:myweekend_plan.txt the reminder system will load the myweekend_plan.txt file and e-mail the user (or whomever else is listed in the To:, Bcc:, and Cc: fields in the file). Similarly, the third line (monthly) is used for monthly reminders. The MM field is simply the two-digit day of the month. For example, to send a reminder on the 27th of each month, a user can set up the following: monthly:27:oh_no_its_27th_already.txt Finally, the yearly reminder is set by the last line. The yearly reminder requires both the MM and DD part of a (MM-DD-YYYY) date. For example, if you want to send a reminder to yourself about a friend s birthday on July 23, you can set up the following: yearly:07-23:sheila_birthday.txt

Chapter 16: Command-Line PHP Utilities 571 Listing 16-5: (Yahoo web hosting)

Wednesday, March 19th, 2008

Chapter 16: Command-Line PHP Utilities 571 Listing 16-5: reminder.conf Listing 16-6 shows a sample configuration file for a user. This configuration file can be altered by the user to add, delete, and modify reminders as desired. Listing 16-6: ~username/reminders/reminders.txt # User Reminder Configuration Format # # Lines starting with # are ignored # Lines that are blank are ignored # Format # FREQUENCY: #daily:todo.txt weekly:mon:monday_plans.txt #weekly:tue:tuesday.txt weekly:tue:foo.txt weekly:wed:wednesday_plans.txt weekly:fri:friday_prayer.txt monthly:10:groupmtg10th.txt monthly:30:payroll.txt Continued