view Reservation.class.php @ 171:c729a68415eb

fix: preg_splitのポカミスを修正
author Sushi-k <epgrec@park.mda.or.jp>
date Mon, 26 Apr 2010 14:54:35 +0900
parents e379552d084c
children 1c4558d1a449
line wrap: on
line source

<?php
include_once('config.php');
include_once( INSTALL_PATH . "/DBRecord.class.php" );
include_once( INSTALL_PATH . "/reclib.php" );
include_once( INSTALL_PATH . "/Settings.class.php" );
include_once( INSTALL_PATH . "/recLog.inc.php" );

// 緇剛
if( !defined( "RECORDER_CMD" ) ) {
	define( "RECORDER_CMD", INSTALL_PATH."/recorder.php" );
}


// 篋膣

class Reservation {
	
	public static function simple( $program_id , $autorec = 0, $mode = 0) {
		$settings = Settings::factory();
		$rval = 0;
		try {
			$prec = new DBRecord( PROGRAM_TBL, "id", $program_id );
			
			$rval = self::custom(
				$prec->starttime,
				$prec->endtime,
				$prec->channel_id,
				$prec->title,
				$prec->description,
				$prec->category_id,
				$program_id,
				$autorec,
				$mode );
				
		}
		catch( Exception $e ) {
			throw $e;
		}
		return $rval;
	}
	
	public static function custom(
		$starttime,				// 紮Datetime
		$endtime,				// 腟篋Datetime
		$channel_id,			// c潟ID
		$title = "none",		// 帥ゃ
		$description = "none",	// 网荀
		$category_id = 0,		// 眼ID
		$program_id = 0,		// 腟ID
		$autorec = 0,			// 牙
		$mode = 0,				// 牙祉≪若
		$dirty = 0				// 若c
	) {
		global $RECORD_MODE;
		$settings = Settings::factory();

		// 荐膊
		$start_time = toTimestamp( $starttime );
		$end_time = toTimestamp( $endtime ) + $settings->extra_time;
		
		if( $start_time < (time() + PADDING_TIME + 10) ) {	// 憜祉3絨鐚с紮腟
			$start_time = time() + PADDING_TIME + 10;		// 牙脂紮310腱荐絎
		}
		$at_start = $start_time - PADDING_TIME;
		$sleep_time = PADDING_TIME - $settings->former_time;
		$rec_start = $start_time - $settings->former_time;
		
		// duration荐膊
		$duration = $end_time - $rec_start;
		if( $duration < ($settings->former_time + 60) ) {	// 60腱篁ヤ腟綣障
			throw new Exception( "腟ゃゃ/腟c腟с" );
		}
		
		$rrec = null;
		try {
			// 筝腟篋膣с
			if( $program_id ) {
				$num = DBRecord::countRecords( RESERVE_TBL, "WHERE program_id = '".$program_id."'" );
				if( $num ) {
					throw new Exception("筝腟牙私膣障");
				}
			}
			
			$crec = new DBRecord( CHANNEL_TBL, "id", $channel_id );
			
			// √篋膣 = TUNER
			$tuners = ($crec->type == "GR") ? (int)($settings->gr_tuners) : (int)($settings->bs_tuners);
			$type_str = ($crec->type == "GR") ? "type = 'GR' " : "(type = 'BS' OR type = 'CS') ";
			
			$battings = DBRecord::countRecords( RESERVE_TBL, "WHERE complete = '0' ".
															  "AND ".$type_str.
															  "AND starttime < '".toDatetime($end_time) ."' ".
															  "AND endtime > '".toDatetime($rec_start)."'"
			);
			
			if( $battings >= $tuners ) {
				// 茲肴
				if( $settings->force_cont_rec == 1 ) {
					// 茹f純茲茯帥鴻
					// 緇篋膣
					$nexts = DBRecord::countRecords( RESERVE_TBL, "WHERE complete = '0' ".
																	"AND ".$type_str.
																	"AND starttime = '".toDatetime($end_time - $settings->former_time)."'");
					
					$prevs = DBRecord::countRecords( RESERVE_TBL, "WHERE complete = '0' ".
																"AND ".$type_str.
																"AND endtime = '".$starttime."'"  );
					
					// 緇綣ャ若惹違遺札筝茲茹f∞
					if( ($battings - $nexts - $prevs) >= $tuners )
						throw new Exception( "茲篋膣茹fс障" );
					
					// 翫腟?
					if( $nexts ) {
						// 腟腟<c
						$end_time = $end_time - $settings->former_time - $settings->rec_switch_time;
						$duration = $end_time - $rec_start;		// duration荐膊
					}
					$battings -= $nexts;
					
					// 翫牙私膣荀篁
					$trecs = DBRecord::createRecords(RESERVE_TBL, "WHERE complete = '0' ".
																		 "AND ".$type_str.
																		 "AND endtime = '".$starttime."'" );
					// 翫腟
					for( $i = 0; $i < count($trecs) ; $i++ ) {
						if( $battings < $tuners ) break;	// 茹f腟篋?
						// 篋膣篆罩c綽荀宴冴
						$prev_id           = $trecs[$i]->id;
						$prev_program_id   = $trecs[$i]->program_id;
						$prev_channel_id   = $trecs[$i]->channel_id;
						$prev_title        = $trecs[$i]->title;
						$prev_description  = $trecs[$i]->description;
						$prev_category_id  = $trecs[$i]->category_id;
						$prev_starttime    = $trecs[$i]->starttime;
						$prev_endtime      = $trecs[$i]->endtime;
						$prev_autorec      = $trecs[$i]->autorec;
						$prev_mode         = $trecs[$i]->mode;
						$prev_dirty        = $trecs[$i]->dirty;
						
						$prev_start_time = toTimestamp($prev_starttime);
						// 紮障c篋膣鐚
						if( $prev_start_time > (time() + PADDING_TIME + $settings->former_time) ) {
							// 紮祉祉
							$prev_starttime = toDatetime( $prev_start_time + $settings->former_time );
							// 腟<c
							$prev_endtime   = toDatetime( toTimestamp($prev_endtime) - $settings->former_time - $settings->rec_switch_time );
							
							// try鴻
							try {
								// c篋膣羔
								self::cancel( $prev_id );
								// 篋膣
								self::custom( 
									$prev_starttime,			// 紮Datetime
									$prev_endtime,				// 腟篋Datetime
									$prev_channel_id,			// c潟ID
									$prev_title,				// 帥ゃ
									$prev_description,			// 网荀
									$prev_category_id,			// 眼ID
									$prev_program_id,			// 腟ID
									$prev_autorec,				// 牙
									$prev_mode,
									$prev_dirty );
							}
							catch( Exception $e ) {
								throw new Exception( "茲篋膣茹fс障" );
							}
						}
						else {
							throw new Exception( "茲篋膣茹fс障" );
						}
						$battings--;
					}
					if( $battings < 0 ) $battings = 0;
					// ч茲茹f
				}
				else {
					throw new Exception( "茲篋膣障" );
				}
			}
			// ャ若主
			$tuner = $battings;
			
			// 鴻durationс
			if( $duration < ($settings->former_time + 60) ) {	// 60腱篁ヤ腟綣障
				throw new Exception( "腟ゃゃ/腟c腟с" );
			}
			
			// <ゃ
/*
			%TITLE%	腟帥ゃ
			%ST%	紮ユ鐚ex.200907201830)
			%ET%	腟篋ユ
			%TYPE%	GR/BS/CS
			%CH%	c潟
			%DOW%	ワSun-Mon鐚
			%DOWJ%	ワ-鐚
			%YEAR%	紮綛
			%MONTH%	紮
			%DAY%	紮
			%HOUR%	紮
			%MIN%	紮
			%SEC%	紮腱
			%DURATION%	牙紙鐚腱鐚
*/

			$day_of_week = array( "","","","羂","","","" );
			$filename = $settings->filename_format;
			
			// %TITLE%
			$filename = mb_str_replace("%TITLE%", trim($title), $filename);
			// %ST%	紮ユ
			$filename = mb_str_replace("%ST%",date("YmdHis", $start_time), $filename );
			// %ET%	腟篋ユ
			$filename = mb_str_replace("%ET%",date("YmdHis", $end_time), $filename );
			// %TYPE%	GR/BS
			$filename = mb_str_replace("%TYPE%",$crec->type, $filename );
			// %CH%	c潟
			$filename = mb_str_replace("%CH%","".$crec->channel, $filename );
			// %DOW%	ワSun-Mon鐚
			$filename = mb_str_replace("%DOW%",date("D", $start_time), $filename );
			// %DOWJ%	ワ-鐚
			$filename = mb_str_replace("%DOWJ%",$day_of_week[(int)date("w", $start_time)], $filename );
			// %YEAR%	紮綛
			$filename = mb_str_replace("%YEAR%",date("Y", $start_time), $filename );
			// %MONTH%	紮
			$filename = mb_str_replace("%MONTH%",date("m", $start_time), $filename );
			// %DAY%	紮
			$filename = mb_str_replace("%DAY%",date("d", $start_time), $filename );
			// %HOUR%	紮
			$filename = mb_str_replace("%HOUR%",date("H", $start_time), $filename );
			// %MIN%	紮
			$filename = mb_str_replace("%MIN%",date("i", $start_time), $filename );
			// %SEC%	紮腱
			$filename = mb_str_replace("%SEC%",date("s", $start_time), $filename );
			// %DURATION%	牙紙鐚腱鐚
			$filename = mb_str_replace("%DURATION%","".$duration, $filename );
			
			// √絖_
//			$filename = preg_replace("/[ \.\/\*:<>\?\\|()\'\"&]/u","_", trim($filename) );
			
			// preg_replaceUTF-8絲上с医mb_ereg_replace祉
			$filename = mb_ereg_replace("[ \./\*:<>\?\\|()\'\"&]","_", trim($filename) );
			
			// 絖潟若紊
			if( defined("FILESYSTEM_ENCODING") ) {
				$filename = mb_convert_encoding( $filename, FILESYSTEM_ENCODING, "UTF-8" );
			}
			
			$filename .= $RECORD_MODE["$mode"]['suffix'];
			$thumbname = $filename.".jpg";
			
			// 泣若
			$gen_thumbnail = INSTALL_PATH."/gen-thumbnail.sh";
			if( defined("GEN_THUMBNAIL") ) 
				$gen_thumbnail = GEN_THUMBNAIL;
			
			// <ゃ腟篋
			
			// 篋膣潟若
			$rrec = new DBRecord( RESERVE_TBL );
			$rrec->channel_disc = $crec->channel_disc;
			$rrec->channel_id = $crec->id;
			$rrec->program_id = $program_id;
			$rrec->type = $crec->type;
			$rrec->channel = $crec->channel;
			$rrec->title = $title;
			$rrec->description = $description;
			$rrec->category_id = $category_id;
			$rrec->starttime = toDatetime( $rec_start );
			$rrec->endtime = toDatetime( $end_time );
			$rrec->path = $filename;
			$rrec->autorec = $autorec;
			$rrec->mode = $mode;
			$rrec->reserve_disc = md5( $crec->channel_disc . toDatetime( $start_time ). toDatetime( $end_time ) );
			
			// 篋膣絎茵
			$cmdline = $settings->at." ".date("H:i m/d/Y", $at_start);
			$descriptor = array( 0 => array( "pipe", "r" ),
			                     1 => array( "pipe", "w" ),
			                     2 => array( "pipe", "w" ),
			);
			$env = array( "CHANNEL"  => $crec->channel,
				          "DURATION" => $duration,
				          "OUTPUT"   => INSTALL_PATH.$settings->spool."/".$filename,
				          "TYPE"     => $crec->type,
			              "TUNER"    => $tuner,
			              "MODE"     => $mode,
			              "THUMB"    => INSTALL_PATH.$settings->thumbs."/".$thumbname,
			              "FORMER"   => "".$settings->former_time,
			              "FFMPEG"   => "".$settings->ffmpeg,
			              "SID"      => $crec->sid,
			);
			
			// ATт膣
			$process = proc_open( $cmdline , $descriptor, $pipes, INSTALL_PATH.$settings->spool, $env );
			if( is_resource( $process ) ) {
				fwrite($pipes[0], RECORDER_CMD." ".$rrec->id."\n" );
				fclose($pipes[0]);
				// 罔羣若
				$rstring = stream_get_contents($pipes[2]);
				
			    fclose( $pipes[2] );
			    fclose( $pipes[1] );
			    proc_close( $process );
			}
			else {
				$rrec->delete();
				reclog( "Reservation::custom at絎茵紊掩罔≧", EPGREC_ERROR);
				throw new Exception("AT絎茵");
			}
			// job垩冴
			$rarr = array();
			$tok = strtok( $rstring, " \n" );
			while( $tok !== false ) {
				array_push( $rarr, $tok );
				$tok = strtok( " \n" );
			}
			$key = array_search("job", $rarr);
			if( $key !== false ) {
				if( is_numeric( $rarr[$key+1]) ) {
					$rrec->job = $rarr[$key+1];
					reclog( "Reservation::custom 吾с".$rrec->job."牙祉吾с脂");
					return $rrec->job;			// 
				}
			}
			// 
			$rrec->delete();
			reclog( "Reservation::custom job垩緇紊掩",EPGREC_ERROR );
			throw new Exception( "job垩緇紊掩" );
		}
		catch( Exception $e ) {
			if( $rrec != null ) {
				if( $rrec->id ) {
					// 篋膣羔
					$rrec->delete();
				}
			}
			throw $e;
		}
	}
	// custom 腟篋
	
	// 羔
	public static function cancel( $reserve_id = 0, $program_id = 0 ) {
		$settings = Settings::factory();
		$rec = null;
		
		try {
			if( $reserve_id ) {
				$rec = new DBRecord( RESERVE_TBL, "id" , $reserve_id );
			}
			else if( $program_id ) {
				$rec = new DBRecord( RESERVE_TBL, "program_id" , $program_id );
			}
			if( $rec == null ) {
				throw new Exception("ID絎≦鴻с");
			}
			if( ! $rec->complete ) {
				if( toTimestamp($rec->starttime) < (time() + PADDING_TIME + $settings->former_time) ) {
					reclog("Reservation::cancel 絎茵筝篋膣ID".$rec->id."羔絎茵" );
					
					// recorder篆<荅帥
					$ipc_key = ftok( RECORDER_CMD, "R" );
					
					/* php 5.3篁ラmsg_queue_exists篏帥
					if( ! msg_queue_exists( $ipc_key ) ) {
						// <祉若吾ャ若
						reclog( "Reservation::cancel 絎茵筝ィ羝篋膣".$rec->id."絎茵", EPGREC_ERROR );
						$rec->complete = 1;
						throw new RecException( "Reserve:: 絎茵筝ィ羝篋膣絎茵障綺ゃ荅帥", EPGREC_ERROR );
					}
					else {
					*/
						$msgh_r = msg_get_queue( $ipc_key );
						$ipc_key = ftok( RECORDER_CMD, "W" );
						$msgh_w = msg_get_queue( $ipc_key );
						
						// 腟篋腓
						msg_send( $msgh_r, (int)$rec->id, "terminate" );
						sleep(1);
						for( $i = 0; $i < 60; $i++ ) {
							$r = msg_receive($msgh_w, (int)$rec->id , $msgtype, 1024, $message, TRUE, MSG_IPC_NOWAIT | MSG_NOERROR);
							if( $r ) {
								if( $message == "success" ) {
									reclog( "Reserve:: 絎茵筝篋膣ID".$rec->id."羔罔≧" );
									break;
								}
								else if( $message == "error" ){
									reclog( "Reserve:: 絎茵筝篋膣ID".$rec->id."羔紊掩", EPGREC_ERROR );
									throw new RecException("絎茵筝篋膣羔紊掩障違綺羔", EPGREC_ERROR );
								}
								// 篁ュ<祉若吾∴緇
							}
							sleep(1);
						}
						if( $i >= 60 ) throw new RecException("絎茵筝篋膣羔紊掩障違綺羔", EPGREC_ERROR );
//					}
				}
				else {
					// 障絎茵篋膣atゃ
					exec( $settings->atrm . " " . $rec->job );
					reclog("Reservation::cancel 吾с".$rec->job."");
					$rec->delete();
				}
			}
			else {
				// 牙紙推膣羔
				$rec->delete();
			}
		}
		catch( Exception $e ) {
			reclog("Reservation::cancel 篋膣c潟祉DB・膓障≪祉鴻紊掩罔≧", EPGREC_ERROR );
			throw $e;
		}
	}
}
?>