soumettre un JCL sur MVS via Php

Répondre


Cette question est un moyen d’empêcher des soumissions automatisées de formulaires par des robots.
Smileys
:D :) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: =D> #-o =P~ :^o :non: :priere: 8-|
Voir plus de smileys
  Revue du sujet
 

  Étendre la vue Revue du sujet : soumettre un JCL sur MVS via Php

Re: soumettre un JCL sur MVS via Php

par nicos31 » 21 mai 2012, 09:47

Salut et merci pour ton code.

je ne parviens toutefois pas à le faire fonctionner.
La méthode _get prend énormément de temps à s'exécuter, et je prends un timeout

Aurais-tu une idée sur la raison de ce soucis ?$

Merci d'avance

Re: soumettre un JCL sur MVS via Php

par haugure » 13 janv. 2012, 17:17

Après quelques lignes de code ca va de suite mieux... voici le résultat avant le weekend ;-)

C'est un peu bourrin mais je me suis pas attardé dans les détails pour le moment, je ferais une classe plus tard !
<?php
include("inc/ftp.inc.php");

$sJobName = "";  //Le nom de la jobcart du JCL /!\ important 
$sFilename = ""; // nom du JCL sur le disque Windows
$sUsername = "xxxxxxx";
$sPassword = "xxxxxx";
$sHostname = "xxxxxx";

$ftp=new FTP(); 
$ftp->connect($sHostname);
$ftp->login($sUsername,$sPassword);
$ftp->setType(FTP_ASCII);
$ftp->site("FILETYPE=JES JESLRECL=80 JESJOBNAME=$sJobName");
$ftp->write($sFilename, $sFilename);


//TODO faire un package MVS qui fera ca un peu plus proprement...
$sReturn = $ftp->_message;//TODO faire un getter...
$res = explode("JOB", $sReturn);
$sJobNumber = substr($res[1],0,5);

$bNotFinish = true;

//TODO :Il faut éviter de spamer trop le serveur ftp
while( $bNotFinish )
{
	$bNotFinish = ( $ftp->read("JOB".$sJobNumber, "JOB".$sJobNumber ) == null );
}

$ftp->close();
echo $ftp->_message;
?>
Voici enfin la classe pour la connexion FTP
<?php
if(!defined('CRLF')) define('CRLF',"\r\n");
if(!defined("FTP_AUTOASCII")) define("FTP_AUTOASCII", -1);
if(!defined("FTP_BINARY")) define("FTP_BINARY", 1);
if(!defined("FTP_ASCII")) define("FTP_ASCII", 0);
if(!defined('FTP_FORCE')) define('FTP_FORCE', TRUE);
define('FTP_OS_Unix','u');
define('FTP_OS_Windows','w');
define('FTP_OS_Mac','m');
define('FTP_OS_Zos','z');

//TODO faire un package MVS qui va heriter de celui ci...

Class FTP
{
	/* Public variables */
	var $Verbose=FALSE;
	var $OS_local;
	
	/* Private variables */
	var $_lastaction=NULL;
	var $_type;
	var $_umask;
	var $_timeout;
	var $_passive;
	var $_host;
	var $_fullhost;
	var $_port;
	var $_datahost;
	var $_dataport;
	var $_user;
	var $_password;
	var $_connected;
	var $_ready;
	var $_code;
	var $_can_restore;
	var $_port_available;
	var $_error; 
	var $_conn=null;
	var $_dataconn=null;
	var $_greet="";

	var $TransferMode=array(FTP_AUTOASCII,FTP_ASCII,FTP_BINARY);
	var $OS_FullName=array(FTP_OS_Unix => 'UNIX',FTP_OS_Windows => 'WINDOWS',FTP_OS_Mac => 'MACOS');
	var $NewLineCode=array(FTP_OS_Unix => "\n",FTP_OS_Mac => "\r",FTP_OS_Windows => "\r\n");
	var $AutoAsciiExt=array("asp","bat","c","cpp","csv","h","htm","html","shtml","ini","log","php","php3","pl","perl","sh","sql","txt");
		

	function FTP($port_mode=FALSE)
	{
		$this->_port_available=($port_mode==TRUE);
		$this->sendMSG("Staring FTP client class with".($this->_port_available?"":"out")." PORT mode support");
		$this->_ready=FALSE;
		$this->_can_restore=FALSE;
		$this->_code=0;
		$this->_message="";
		$this->setUmask(0022);
		$this->setType(FTP_AUTOASCII);
		$this->setTimeout(30);
		$this->passive(!$this->_port_available);
		$this->_user="anonymous";
		$this->_password="[email protected]";
		$this->_datahost=$_SERVER['REMOTE_ADDR'];
		$this->OS_local=FTP_OS_Unix;
		if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') $this->OS_local=FTP_OS_Windows;
		elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'MAC') $this->OS_local=FTP_OS_Mac;
	}

	////SETTER
	function set_host($host){ $this->_host=$host; }
	function set_port($port){$this->_port=$port;}

	////GETTER
	function error(){ return $this->_error; }
	function greet(){return $this->_greet;}
	
	
	///Function is used to open connection
	function open($host,$port=21,$reconnect=FALSE)
	{
		if(!is_long($port)) 
		{
			$this->_error="Incorrect port syntax";
			return FALSE;
		}
		else
		{
			$this->_host=$host;
			$this->_port=$port;
			$this->_dataport=$port-1;//Je sais c'est moche...
		}
		$this->SendMSG("Host \"".$this->_fullhost."(".$this->_host."):".$this->_port."\"");
		if($reconnect)
		{
			if($this->_connected) {
				$this->SendMSG("Reconnecting");
				$this->close(FTP_FORCE);
				if(!$this->connect()) return FALSE;
			}
		}
		return TRUE;	
	}
	
	function connect($host,$port=21)
	{
		$this->open($host,$port);
		return $this->_connect();			
	}
	
	function login($user="",$password="")
	{
		if(!empty($user))
			$this->_user=$user;
		else
			$this->_user="anonymous";					
		if(!empty($password))
			$this->_password=$password;
		else
			$this->_password="[email protected]";
		$this->_login();
	}

	function chmod($dir,$mode)
	{
		// SITE
		return $this->site("CHMOD $mode $dir");
	}
	
	function chdir($dir)
	{
		if(!$this->_ready)
		{
			$this->_error="Please login to FTP server first.";
			return false;
		}
		if(!$this->_put("CWD $dir"))
		{
			$this->_error="Failed to send command to FTP Server.";
			return false;
		}
		else
		{
			$msg=$this->_read();
			if(!$this->_checkReply())
			{
				$this->_error=$msg;
				return false;
			}
		}				
		$this->sendMsg("Directory successfuly changed to $dir.");
		return true;
	}

	function mkdir($dir)
	{
		if(!$this->_ready)
		{
			$this->_error="Please login to FTP server first.";
			return false;
		}
		if(!$this->_put("MKD $dir"))
		{
			$this->_error="Failed to send command to FTP Server.";
			return false;
		}
		else
		{
			$msg=$this->_read();
			if(!$this->_checkReply())
			{
				$this->_error=$msg;
				return false;
			}
		}				
		$this->sendMsg($msg);
		return true;
	}
	
	function rmdir($dir)
	{
		if(!$this->_ready)
		{
			$this->_error="Please login to FTP server first.";
			return false;
		}
		if(!$this->_put("RMD $dir"))
		{
			$this->_error="Failed to send command to FTP Server.";
			return false;
		}
		else
		{
			$msg=$this->_read();
			if(!$this->_checkReply())
			{
				$this->_error=$msg;
				return false;
			}
		}				
		$this->sendMsg($msg);
		return true;
	}
	
	function site($cmd="")
	{
		if(!$this->_ready)
		{
			$this->_error="Please login to FTP server first.";
			return false;
		}
		if(!$this->_put("SITE $cmd"))
		{
			$this->_error="Failed to send command to FTP Server.";
			return false;
		}
		else
		{
			$msg=$this->_read();
			if(!$this->_checkReply())
			{
				$this->_error=$msg;
				return false;
			}
		}				
		$this->sendMsg($msg); // Et voila !!
		return true;
	}

	function features()
	{
		if(!$this->_ready)
		{
			$this->_error="Please login to FTP server first.";
			return false;
		}
		if(!$this->_put("FEAT"))
		{
			$this->_error="Failed to send command to FTP Server.";
			return false;
		}
		else
		{
			$msg=$this->_read();
			if(!$this->_checkReply())
			{
				$this->_error=$msg;
				return false;
			}
		}				
		$msg=preg_split("/".$this->NewLineCode[$this->OS_local]."/",$msg,-1,PREG_SPLIT_NO_EMPTY);
		$msg[0]=substr($msg[0],4);
		array_pop($msg);
		return implode($this->NewLineCode[$this->OS_local],$msg);
	}

	function mdtm($path)
	{
		if(!$this->_ready)
		{
			$this->_error="Please login to FTP server first.";
			return false;
		}
		if(!$this->_put("MDTM ".$path))
		{
			$this->_error="Failed to send command to FTP Server.";
			return false;
		}
		else
		{
			$msg=$this->_read();
			if(!$this->_checkReply())
			{
				$this->_error=$msg;
				return false;
			}
		}				
		$msg=trim(substr($msg,4));
		list($year,$month,$day,$hr,$min,$sec)=sscanf($msg,"%4d%2d%2d%2d%2d%2d");
		return mktime($hr,$min,$sec,$month,$day,$year);
	}

	function help($arg="")
	{
		if(!$this->_ready)
		{
			$this->_error="Please login to FTP server first.";
			return false;
		}
		if(!$this->_put("HELP $arg"))
		{
			$this->_error="Failed to send command to FTP Server.";
			return false;
		}
		else
		{
			$msg=$this->_read();
			if(!$this->_checkReply())
			{
				$this->_error=$msg;
				return false;
			}
		}				
		$msg=preg_split("/".$this->NewLineCode[$this->OS_local]."/",$msg,-1,PREG_SPLIT_NO_EMPTY);
		$msg[0]=substr($msg[0],4);
		array_pop($msg);
		return implode($this->NewLineCode[$this->OS_local],$msg);
	}
	
	function noop()
	{
		if(!$this->_ready)
		{
			$this->_error="Please login to FTP server first.";
			return false;
		}
		if(!$this->_put("NOOP"))
		{
			$this->_error="Failed to send command to FTP Server.";
			return false;
		}
		else
		{
			$msg=$this->_read();
			if(!$this->_checkReply())
			{
				$this->_error=$msg;
				return false;
			}
		}
		$this->sendMsg($msg);
		return true;				
	}
	
	
	function system()
	{
		if(!$this->_ready)
		{
			$this->_error="Please login to FTP server first.";
			return false;
		}
		if(!$this->_put("SYST"))
		{
			$this->_error="Failed to send command to FTP Server.";
			return false;
		}
		else
		{
			$msg=$this->_read();
			if(!$this->_checkReply())
			{
				$this->_error=$msg;
				return false;
			}
		}				
		return substr(preg_replace("/\"/","" ,$msg ),4);
	}
	
	function pwd()
	{
		if(!$this->_ready)
		{
			$this->_error="Please login to FTP server first.";
			return false;
		}
		if(!$this->_put("PWD"))
		{
			$this->_error="Failed to send command to FTP Server.";
			return false;
		}
		else
		{
			$msg=$this->_read();
			if(!$this->_checkReply())
			{
				$this->_error=$msg;
				return false;
			}
		}				
		$this->sendMsg($msg);
		return substr(preg_replace("/\"/","" ,$msg ),4);
	}

	function rawlist($arg="",$path="")
	{
		if(!$this->_ready)
		{
			$this->_error="Please login to FTP server first.";
			return false;
		}
		
		$arg=($arg?" ".$arg:"").($path?" ".$path:"");
		return $this->_list($arg);
	}

	function namelist($arg="",$path="")
	{
		if(!$this->_ready)
		{
			$this->_error="Please login to FTP server first.";
			return false;
		}
		
		$arg=($arg?" ".$arg:"").($path?" ".$path:"");
		return $this->_list($arg,"NLST");
	}
	
	function cdup()
	{
		if(!$this->_ready)
		{
			$this->_error="Please login to FTP server first.";
			return false;
		}
		if(!$this->_put("CDUP"))
		{
			$this->_error="Failed to send command to FTP Server.";
			return false;
		}
		else
		{
			$msg=$this->_read();
			if(!$this->_checkReply())
			{
				$this->_error=$msg;
				return false;
			}
		}				
		$this->sendMsg("Directory successfuly changed to parent Directory.");
		return true;
	}

	function delete($file)
	{
		if(!$this->_ready)
		{
			$this->_error="Please login to FTP server first.";
			return false;
		}
		if(!$this->_put("DELE $file"))
		{
			$this->_error="Failed to send command to FTP Server.";
			return false;
		}
		else
		{
			$msg=$this->_read();
			if(!$this->_checkReply())
			{
				$this->_error=$msg;
				return false;
			}
		}				
		$this->sendMsg($msg);
		return true;
	}
	
	function abort()
	{
		if(!$this->_ready)
		{
			$this->_error="Please login to FTP server first.";
			return false;
		}
		if(!$this->_put("ABOR"))
		{
			$this->_error="Failed to send command to FTP Server.";
			return false;
		}
		else
		{
			$msg=$this->_read();
			if(!$this->_checkReply())
			{
				$this->_error=$msg;
				return false;
			}
		}				
		$this->sendMsg($msg);
		return true;
	}

	function rename($from,$to)
	{
		if(!$this->_ready)
		{
			$this->_error="Please login to FTP server first.";
			return false;
		}
		if(!$this->_put("RNFR $from"))
		{
			$this->_error="Failed to send command to FTP Server.";
			return false;
		}
		else
		{
			$msg=$this->_read();
			if(!$this->_checkReply())
			{
				$this->_error=$msg;
				return false;
			}
		}
		$this->sendMsg($msg);
		// Todo: gerer ca proprement => RFC du proto
		if($this->_code==350) 
		{
			if(!$this->_put("RNTO $to"))
			{
				$this->_error="Failed to send command to FTP Server.";
				return false;
			}
			else
			{
				$msg=$this->_read();
				if(!$this->_checkReply())
				{
					$this->_error=$msg;
					return false;
				}
			}
		}
		else return FALSE;

		$this->sendMsg($msg);
		return true;
	}
	
	function restart($point)
	{
		if(!$this->_ready)
		{
			$this->_error="Please login to FTP server first.";
			return false;
		}
		if(!$this->_put("REST $point"))
		{
			$this->_error="Failed to send command to FTP Server.";
			return false;
		}
		else
		{
			$msg=$this->_read();
			if(!$this->_checkReply())
			{
				$this->_error=$msg;
				return false;
			}
		}				
		$this->sendMsg($msg);
		return true;
	}
	
	function read($remoteFile,$localFile=NULL)
	{
		$fp=null;
		if(!$this->_ready)
		{
			$this->_error="Please login to FTP server first.";
			return false;
		}
		preg_match("/\..+/",$remoteFile,$matches);
		$ext = (sizeof($matches)>0) ? strtolower(substr($matches[0],1)) : null;
		if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array($ext,$this->AutoAsciiExt)))
		 $mode=FTP_ASCII;
		else
		 $mode=FTP_BINARY;

		//$mode=FTP_BINARY; <- pour forcer le mode decommenter !
		$this->_mode($mode); 	

		if(!$this->_listen())
		{
			$this->sendMsg("Failed to listen to server.");
			return false;
		}
		if($localFile==true)
			$localFile=$remoteFile;
		if(@file_exists($localFile))
			$this->sendMsg("$localFile will be overwritten.");

		if(!empty($localFile))					
		{
			$fp=fopen($localFile,"w");
			if(!$fp)
			{
				$this->_error="Couldn't open $localFile to write.";
				return false;
			}
		}
		
		if(!$this->_put("RETR $remoteFile"))
		{
			$this->_error="Failed to send command to FTP Server.";
			@fclose($fp);
			$this->_data_close();
			return false;
		}
		$msg=$this->_read();
		if(!$this->_checkReply())
		{
			$this->_error=$msg;
			@fclose($fp);
			$this->_data_close();
			return false;
		}
		$this->sendMsg($msg);
		$out=$this->_read_data($mode,$fp);

		@fclose($fp);
		$this->_data_close();

		$msg=$this->_read();
		if(!$this->_checkReply())
		{
			$this->_error=$msg;
			return false;
		}
		$this->sendMsg($msg);
		$this->sendMsg("File successfuly read.");
		return $out;
	}

	function write($localFile,$remoteFile=NULL,$unique=false)
	{
		$fp=NULL;
		if(!$this->_ready)
		{
			$this->_error="Please login to FTP server first.";
			return false;
		}
		preg_match("/\..+/",$localFile,$matches);
		 
		
		$ext = (sizeof($matches) > 0 ) ? strtolower(substr($matches[0],1)) : null;
		if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array($ext,$this->AutoAsciiExt)))
		 $mode=FTP_ASCII;
		else
		 $mode=FTP_BINARY;

		$this->_mode($mode); 	
		$this->restart(0);

		if(!$this->_listen())
		{
			$this->sendMsg("Failed to listen to server.");
			return false;
		}

		if(is_null($remoteFile))
			$remoteFile=$localFile;

		if(@file_exists($localFile))					
		{
			$fp=fopen($localFile,"r");
			if(!$fp)
			{
				$this->_error="Couldn't read $localFile.";
				$this->_data_close();
				return false;
			}
		}
		else
		{
			$this->_error="Couldn't find $localFile.";
			$this->_data_close();
			return false;
		}
		
		if($unique)
			$cmd="STOU";
		else
			$cmd="STOR";
		
		if(!$this->_put("$cmd $remoteFile"))
		{
			$this->_error="Failed to send command to FTP Server.";
			$this->_data_close();
			@fclose($fp);
			return false;
		}
		$msg=$this->_read();
		if(!$this->_checkReply())
		{
			$this->_error=$msg;
			$this->_data_close();
			@fclose($fp);
			return false;
		}
		$this->sendMsg($msg);
		
		if(!$this->_write_data($mode,$fp)) return false;
		
		@fclose($fp);
		$this->_data_close();
		
		if(($msg=$this->_read())===false) return false;
		if(!$this->_checkReply())
		{
			$this->_error=$msg;
			return false;
		}
		$this->sendMsg($msg);
	}			
	function quit()
	{
		if(!$this->_ready)
		{
			$this->_error="Please login to FTP server first.";
			return false;
		}
		if(!$this->_put("QUIT"))
		{
			$this->_error="Failed to send command to FTP Server.";
			return false;
		}
		else
		{
			$msg=$this->_read();
			if(!$this->_checkReply())
			{
				$this->_error=$msg;
				return false;
			}
		}				
		$this->_ready=false;
		$this->sendMsg("Successfuly logged out.");
		return true;
	}
	
	function setTimeOut($time)
	{
		$this->_timeout=$time;
		if(!is_null($this->_conn))
		{
			if(!stream_set_timeout($this->_conn,$time))
			{
				$this->_error="Couldn't set timeout.";
				$this->close();
				return false;
			}
		}
		return true;	
	}
	function setUmask($mask)
	{
		@umask(decoct($mask));
		$this->sendMsg("UMASK $mask");
	}
	
	function sendMsg($message="")
	{
		if($this->Verbose)
			return $this->_message;
		else
			$this->_message.=$message.CRLF;
	}

	function setType($mode=FTP_AUTOASCII) 
	{
		if(!in_array($mode, $this->TransferMode)) 
		{
			$this->SendMSG("Wrong type");
			return FALSE;
		}
		$this->_type=$mode;
		$this->SendMSG("Transfer type: ".($this->_type==FTP_BINARY?"binary":($this->_type==FTP_ASCII?"ASCII":"auto ASCII") ) );
		return TRUE;
	}

	/// close the active connection
	function close($force=false)
	{
		if(!$this->quit() && !$force) return false;
		@fclose($this->_conn);
		$this->_conn=null;
		$this->_connected=false;
		$this->sendMsg("Bye.");
		return true;
	}
		
	// Private functions !
	
	function _get()
	{
		$line=null;
		while(!feof($this->_conn))
		{
			$line.=@fgets($this->_conn);
			if(preg_match("/\\r?\\n$/i",$line))
				return(preg_replace("/\\r?\\n$/i","",$line));
		}
	}
	
	function _read()
	{
		$msg=null;
		$firstLine=true;
		$break=false;
		do{
			$line=$this->_get();
			if($firstLine)
			{
				$code=substr($line,0,3);
				$firstLine=false;
			}
			if(preg_match("/^($code )/",$line))
				$break=true;
			$msg.=$line.$this->NewLineCode[$this->OS_local];
		}while(!$break);
		$this->_code=$code;
		return $msg;
		//echo $this->_get();
	}

	////This functiuon is to send the command to server
	function _put($msg="")
	{
		$this->_lastaction=time();
		return fputs($this->_conn,"$msg\r\n");
	}
	function passive($pasv=NULL) 
	{
		if(is_null($pasv)) $this->_passive=!$this->_passive;
		else $this->_passive=$pasv;
		if(!$this->_port_available and !$this->_passive) {
			$this->SendMSG("Only passive connections available!");
			$this->_passive=TRUE;
			return FALSE;
		}
		$this->sendMSG("Passive mode ".($this->_passive?"on":"off"));
		return TRUE;
	}

	function _connect()
	{
		$this->sendMsg('Local OS : '.$this->OS_FullName[$this->OS_local]);
		$this->_conn=fsockopen($this->_host,$this->_port,$errno,$errstr,$this->_timeout);
		if(!$this->_conn)
		{
			$this->_error="Couldn't open a connection to ".$this->host;
			$this->_error.="\nError($errno):$errstr";
			return false;
		}
		$this->_greet=$this->_read();
		$this->_connected=true;
		return TRUE;
	}
	function _login()
	{
		if(!$this->_connected)
		{
			$this->_error="Please connect to FTP server first.";
			return false;
		}
		if(!$this->_put("USER ".$this->_user))
		{
			$this->_error="Failed to send command to FTP Server.";
			return false;
		}
		else
		{
			$msg=$this->_read();
			if(!$this->_checkReply($msg))
			{
				$this->_error=$msg;
				return false;
			}
		}
		if(!$this->_put("PASS ".$this->_password))
		{
			$this->_error="Failed to send command to FTP Server.";
			return false;
		}
		else
		{
			$msg=$this->_read();
			if(!$this->_checkReply())
			{
				$this->_error=$msg;
				return false;
			}
		}
		$this->sendMsg("Login Correct.");
		$this->_ready=true;
		return true;
	}
	function _checkReply() 
	{
		return ($this->_code<400 && $this->_code>0);
	}
	function _mode($mode)
	{
		if($mode==FTP_BINARY) {
			if(!$this->_put("TYPE I")) return FALSE;
		} else {
		if(!$this->_put("TYPE A")) return FALSE;
		}
		$msg=$this->_read();
		$this->sendMsg($msg);
	}
	function _listen()
	{
		if($this->_port_available)
		{
			$this->_datahost=$_SERVER['REMOTE_ADDR'];
			if(($this->_dataconn=@socket_create(AF_INET,SOCK_STREAM,SOL_TCP))<0)
			{
				$this->sendMsg("Couldn't make sockets for port.");
				break;
			}
			@socket_set_option($this->_dataconn,1,SO_RCVTIMEO,array("sec"=>$this->_timeout, "usec"=>0));
			@socket_set_option($this->_dataconn,1,SO_SNDTIMEO,array("sec"=>$this->_timeout, "usec"=>0));
			
			if(($ret = @socket_bind($this->_dataconn ,$this->_datahost)) < 0) 
			{
				$this->sendMsg("Couldn't bind to local socket.");
			}
			if(($ret = @socket_listen($this->_dataconn)) < 0) 
			{
				$this->sendMsg("Couldn't listen to local socket.");
			}
			if(!@socket_getsockname($this->_dataconn,$this->_datahost, $this->_dataport))
			{
				$this->sendMsg("Couldn't get information to local socket.");
			}
			$port="PORT ".preg_replace("/\./",",",$this->_datahost).",".($this->_dataport>>8).",".($this->_dataport&0x00FF);

			if($this->_put($port))
			{
				$msg=$this->_read();
				if($this->_checkReply())
				{
					$this->sendMsg($port);
					return true;
				}
				else
					$this->sendMsg($msg);
			}
		}
		if(!$this->_put("PASV"))
		{
			$this->_error="Failed to send command to Server.";
			return false;
		}
		$msg=$this->_read();
		if(!$this->_checkReply())
		{
			$this->sendMsg($msg);
			return false;
		}
		preg_match("/(\d{1,3},){5}\d{1,3}/",$msg,$matches);
		$ip_port=preg_split("/,/",$matches[0]);
		$this->_datahost=$ip_port[0].".".$ip_port[1].".".$ip_port[2].".".$ip_port[3];
		$this->_dataport=(((int)$ip_port[4])<<8) + ((int)$ip_port[5]);
		$this->_dataconn=@fsockopen($this->_datahost,$this->_dataport,$errno,$errstr,$this->_timeout);
		if(!$this->_dataconn)
		{
			$this->_error="Couldn't open data connection to server.";
			return false;
		}
		$this->sendMsg($msg);
		$this->_passive=true;
		return true;
	}
	
	function _read_data($mode=FTP_ASCII,$fp=NULL)
	{
		if($this->_passive)	
		{
			$line = null;
			$NewLine=null;
			$out="";
			if($mode!=FTP_BINARY) 
			{
				while (!feof($this->_dataconn)) 
				{
					$line.=fread($this->_dataconn, 4096);
					if(!preg_match("/".CRLF."$/", $line)) continue;
						$line=rtrim($line,CRLF).$NewLine;
					if(is_resource($fp))
					 $out+=fwrite($fp, $line, strlen($line));
					else $out.=$line;
					$line="";
				}
			} 
			else
			{
				while (!feof($this->_dataconn)) 
				{
					$block=fread($this->_dataconn, 4096);
					if(is_resource($fp))
					 $out+=fwrite($fp, $block, strlen($block));
					else
					 $out.=$block;
				}
			}
			return $out;
		}
		else
		{
			$out="";
			$ftp_temp_sock=socket_accept($this->_dataconn);
			if($this->_ftp_temp_sock===FALSE)
			{
				$this->_error= socket_strerror(socket_last_error($ftp_temp_sock));
				@socket_close($this->_dataconn);
				return FALSE;
			}
			if($mode!=FTP_BINARY) 
			{
				while(($tmp=socket_read($ftp_temp_sock, 8192,PHP_NORMAL_READ))!==false) 
				{
					$line.=$tmp;
					if(!preg_match("/".CRLF."$/", $line)) continue;
					$line=rtrim($line,CRLF).$NewLine;
					if(is_resource($fp)) $out+=fwrite($fp, $line, strlen($line));
					else $out.=$line;
					echo $line;
					$line="";
				}
			} 
			else 
			{
				while($block=@socket_read($ftp_temp_sock, 8192, PHP_BINARY_READ)) 
				{
					if(is_resource($fp)) $out+=fwrite($fp, $block, strlen($block));
					else $out.=$block;
				}
			}
			@socket_close($ftp_temp_sock);
			return $out;
		}
	}
	
	function _write_data($mode=FTP_ASCII,$fp=NULL)
	{	
		if($this->_passive)
		{
			$NewLine=$this->NewLineCode[$this->OS_local];
			if(is_resource($fp)) 
			{
				while(!feof($fp)) 
				{
					$line=fgets($fp, 4096);
					if($mode!=FTP_BINARY) $line=rtrim($line, CRLF).CRLF;
					do {
						if(($res=@fwrite($this->_dataconn, $line))===FALSE) 
						{
							$this->_error="Can't write to socket";
							return FALSE;
						}
						$line=substr($line, $res);
					}while($line!="");
				}
			} 
			else
			{
				if($mode!=FTP_BINARY) $fp=rtrim($fp, $NewLine).CRLF;
				do {
					if(($res=@fwrite($this->_dataconn, $fp))===FALSE) {
						$this->_error="Can't write to socket";
						return FALSE;
					}
					$fp=substr($fp,$res);
				}while($fp!="");
			}
			return TRUE;
		}
		else
		{
			$NewLine=$this->NewLineCode[$this->OS_local];
			$ftp_temp_sock=socket_accept($this->_dataconn);
			if($this->_ftp_temp_sock===FALSE)
			{
				$this->_error= socket_strerror(socket_last_error($ftp_temp_sock));
				@socket_close($this->_dataconn);
				return FALSE;
			}
			if(is_resource($fp)) 
			{
				while(!feof($fp)) 
				{
					$line=fgets($fp, 4096);
					if($mode!=FTP_BINARY) $line=rtrim($line, CRLF).CRLF;
					do {
						if(($res=@socket_write($ftp_temp_sock, $line))===FALSE) 
						{
							$this->_error="Can't write to socket.<br>Error:".socket_strerror(socket_last_error($this->_ftp_temp_sock));
							return FALSE;
						}
						$line=substr($line, $res);
					}while($line!="");
				}
			} 
			else
			{
				if($mode!=FTP_BINARY) $fp=rtrim($fp, $NewLine).CRLF;
				do {
					if(($res=@socket_write($ftp_temp_sock, $fp))===FALSE) 
					{
						$this->_error="Can't write to socket.<br>Error:".socket_strerror(socket_last_error($this->_ftp_temp_sock));
						return FALSE;
					}
					$fp=substr($fp,$res);
				}while($fp!="");
			}
			return TRUE;
		}
		@socket_close($ftp_temp_sock);
	}
	
	function _list($arg="",$cmd="LIST")
	{
		$mode=FTP_BINARY;
		$this->_mode($mode);
		if(!$this->_listen()) return false;
		if(!$this->_put($cmd.$arg))
		{
			$this->_error="Failed to send command to FTP Server.";
			return false;
		}
		$msg=$this->_read();
		if(!$this->_checkReply())
		{
			$this->_error=$msg;
			$this->_data_close();
			return false;
		}
		$this->sendMsg($msg);
		$out=$this->_read_data($mode);
		$out=preg_split("/[".CRLF."]+/", $out, -1, PREG_SPLIT_NO_EMPTY);
		$msg=$this->_read();
		$this->_data_close();
		if(!$this->_checkReply())
		{
			$this->_error=$msg;
			return false;
		}
		$this->sendMsg($msg);
		// Mouai...
		/*$msg=$this->_read();
		if(!$this->_checkReply())
		{
			$this->_error=$msg;
			return false;
		}
		$this->sendMsg($msg);*/
		return $out;		
	}
	function _data_close()
	{
		if($this->_passive)
			@fclose($this->_dataconn);
		else
			@socket_close($this->_dataconn);
	}
}
?>
Bonne lecture bon weekend ! Et bon usage a ceux que ça intéresserait :-)

Re: soumettre un JCL sur MVS via Php

par haugure » 13 janv. 2012, 16:19

Salut Mazarini, alors en fait je tiens le bon bout mais j'ai du zapper l'option ftp_raw ... je pensais que cette méthode faisait ce qui est écrit dans la doc mais malheureusement c'est pas le cas, en fait elle ouvre simplement le "dialog", après tu dois récupérer l'IP + 2 autre valeur que le serveur te renvoie (multiplier la première par 256 et ajouter l'autre) calculer le port de connexion avec ces deux autres valeurs puis te connecter au serveur sur l'IP renvoyée et le port calculé, et envoyer en mode data le fichier, malheureusement avec MVS ce n'est pas possible de faire ainsi :-) sinon tu te retrouve avec un mode data lambda, et JES te jette en tuant les deux connexions :$.
Du coup => SOCKET

Mais mon code a l'air de fonctionner je fignole deux écritures un peu comment dire "gignolarde" dans ma class et je poste :-)

Code : Tout sélectionner

UMASK 18 Transfer type: auto ASCII Passive mode on Host "(xxxxxxx):21" Local OS : WINDOWS Login Correct. Transfer type: ASCII 200 SITE command was accepted 200 Representation type is Ascii NonPrint 227 Entering Passive Mode (xx,x,x,x,19,121) 125 Sending Job to JES internal reader FIXrecfm 80 250-It is known to JES as JOB05068 250 Transfer completed successfully.

Re: soumettre un JCL sur MVS via Php

par Mazarini » 13 janv. 2012, 16:08

J'espère que tu donneras la solution.

Si ca t'intéresse, il est possible de dialoguer avec un CICS depuis un seveur web et donc de lancer un job. Par contre si je travaille sur le serveur web et sur le système central j'ignore comment mes données transites entre les 2.

Re: soumettre un JCL sur MVS via Php

par haugure » 13 janv. 2012, 14:02

Diantre ! array ftp_raw ( resource $ftp_stream , string $command ), à première vue ftp_raw renvoi un array du coup ça devrait pouvoir le faire, j'enquête !

soumettre un JCL sur MVS via Php

par haugure » 13 janv. 2012, 12:29

Salut !

L'objectif est de soumettre un JCL sur un système centrale MVS par FTP.. d'en récupérer le numéro/ID du Job qui est défini par le JES attendre que le Job se termine en bouclant sur un simple Loop puis récupérer la sortie du job et la parser pour y analyser des résultats ...

la raison de ce poste : Impossible d'obtenir la réponse de JES qui doit me donner le numéro de JOB sur la sortie standard de la connexion FTP... sachant que ce développement je l'ai déjà fait en Perl et en Java (avec des sockets avec ce langage obligé y'a rien qui marche ) je bloque un peu ... peut être tout refaire avec des sockets ?

Sinon y'a t'il un moyen d'obtenir les messages qui sont issu du serveur FTP avec Php dans le genre toutes les infos ... celle qu'on voit quand on se connecte sous SSH par exemple..

Voici mon code Php et une partie ce celui en Perl (objet)
<?php
class MVS
{
    public $pCon;
	public function __construct($sUrl)
	{
        $this->pCon = ftp_connect($sUrl);
    }
	public function __call($sFunc,$a)
	{
        if(strstr($sFunc,'ftp_') !== false && function_exists($sFunc))
		{
            array_unshift($a,$this->pCon);
            return call_user_func_array($sFunc,$a);
        }
		else
		{
            die("$sFunc est une fonction FTP invalide ou non supportée !");
        }
    }
} 

$sJobName = "xxxxx";  //Le nom de la jobcart
$sFilename = "xywzzzz"; // nom du fichier

$pMvs = new MVS('IP');
$pMvs->ftp_login('user','pass');
$pMvs->ftp_site("FILETYPE=JES JESLRECL=80 JESJOBNAME=$sJobName") || die("OMFG ! "); 

//Soumission du job => la ligne du dessus va dire au MVS que le prochain put est une soumission de job par un bête transfert de donnée...
$pMvs->ftp_put($sFilename, $sFilename, FTP_ASCII );

// A partir de cette ligne soit j'ai une JCL_ERROR, soit un numero de Job
//Le serveur FTP donne dans la sortie standard cette information mais ftp_put renvoie un bool :'(

?>
Un code quasiment identique qui marche en Perl :

Code : Tout sélectionner

package mvs; use strict; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); require Exporter; use Time::HiRes qw[gettimeofday tv_interval]; use Net::FTP; # custom package use constante; use Output; @ISA = qw(Exporter Net::FTP); @EXPORT = qw(); $VERSION = '1.1'; my $g_Mvs; sub new { my ( $p_sClass ) = @_; my $pOutput = new Output; if( defined( $g_Mvs ) ) { return $g_Mvs; } else { my $pSelf = $p_sClass->SUPER::new( MVS_HOSTNAME ); $pSelf->login(MVS_LOGIN, MVS_PASSWORD) or die "Cannot login to MVS Hostname ".MVS_HOSTNAME." username or password failed !"; $pOutput->printLn("MVS >> Welcome to MVS \nMVS >> ".$pSelf->message()); bless $pSelf, $p_sClass; $g_Mvs = $pSelf; return $pSelf; } } # Soumet le nom de fichier $sFileToSubmit portant le nom $sJobName avec le type $sType sub submit { my ($pSelf, $sFileToSubmit, $sJobName, $sType ) = @_; die "Nothing to submit !" if ( ! defined( $sFileToSubmit) || $sFileToSubmit eq '' ); die "Job must have a name!" if ( ! defined( $sJobName) || $sJobName eq '' ); my $pOutput = new Output; $sType = $sType ? $sType : 'JES'; $pSelf->quot('SITE', "FILETYPE=$sType JESLRECL=80 JESJOBNAME=$sJobName") or die "Cannot submit job $sJobName"; $pOutput->printLn ("MVS >> Submitting job : $sJobName \n"); my $sSubmitMessage = $pSelf->put($sFileToSubmit); $pOutput->printLn ("MVS >> $sSubmitMessage \n"); // EN PERL on a getMessage() pas en Php my $sMessage = $pSelf->getMessage(); // $pSelf est un Net::FTP mais getMessage renvoie le membre "membre" // Mais il y'a polymorphisme car Net::FTP hérite lui aussi du package Net::Cmd // ici c'est donc la methode message() de ce dernier package qui est utilisée // il renvoie simplement les informations de la socket : // wantarray ? @{${*$cmd}{'net_cmd_resp'}} : join("", @{${*$cmd}{'net_cmd_resp'}}); $pOutput->printLn ("MVS >> ".$sMessage); my $nJobNumber = substr($sMessage,index($sMessage,"JOB"),8); $pOutput->printLn ("MVS >> -------------------- $nJobNumber NOW RUN ------------------- \n"); return $nJobNumber; }
En deux mot, en Perl on peut faire un $pFtp->getMessage ou $pFtp->message qui renvoie les infos concernant les impressions sur la sortie standard de la socket serveur, mais en Php je ne vois pas comment faire la même chose... à votre avis sans avoir a me recoder tous ça avec des socket depuis le début y'a une solution ?

Merci à vous pour vos avis éclairés !