view Reservation.class.php @ 116:f268d7d10590

fix: マルチバイトmb_str_replaceをネットからいただいて追加
author yoneda <epgrec@park.mda.or.jp>
date Sat, 13 Mar 2010 23:30:11 +0900
parents f6b909678e51
children f5d58cf2842f cb04c9ca1cb0
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" );


// 篋膣

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				// 牙祉≪若
	) {
		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") ? $settings->gr_tuners : $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_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 );
							}
							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%", $fn_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 );
			
			// √絖_
			$fn_title = mb_ereg_replace("[ \./\*:<>\?\\|()\'\"&]","_", trim($title) );
			
			// 絖潟若紊
			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], $settings->sleep." ".$sleep_time."\n" );
				fwrite($pipes[0], DO_RECORD . "\n" );
				fwrite($pipes[0], COMPLETE_CMD." ".$rrec->id."\n" );
				if( $settings->use_thumbs == 1 ) {
					fwrite($pipes[0], $gen_thumbnail."\n" );
				}
				fclose($pipes[0]);
				// 罔羣若
				$rstring = stream_get_contents( $pipes[2]);
				
			    fclose( $pipes[2] );
			    proc_close( $process );
			}
			else {
				$rrec->delete();
				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];
					return $rrec->job;			// 
				}
			}
			// 
			$rrec->delete();
			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) )
					throw new Exception("サ牙私膣с");
				exec( $settings->atrm . " " . $rec->job );
			}
			$rec->delete();
		}
		catch( Exception $e ) {
			throw $e;
		}
	}
}
?>