Class Ssh2_crontab_manager { Private $ connection; Private $ path; Private $ handle; Private $ cron_file; Function _ construct ($ host = NULL, $ port = NULL, $ username = NULL, $ password = NULL) { $ Path_length = strrpos (_ FILE __,"/"); $ This-> path = substr (_ FILE __, 0, $ path_length ).'/'; $ This-> handle = 'crontab.txt '; $ This-> cron_file = "{$ this-> path} {$ this-> handle }"; Try { If (is_null ($ host) | is_null ($ port) | is_null ($ username) | is_null ($ password) throw new Exception ("The host, port, username and password arguments must be specified! "); $ This-> connection = @ ssh2_connect ($ host, $ port ); If (! $ This-> connection) throw new Exception ("The SSH2 connection cocould not be established ."); $ Authentication = @ ssh2_auth_password ($ this-> connection, $ username, $ password ); If (! $ Authentication) throw new Exception ("cocould not authenticate '{$ username}' using pasword: '{$ password }'."); } Catch (Exception $ e) { $ This-> error_message ($ e-> getMessage ()); } } Public function exec () { $ Argument_count = func_num_args (); Try { If (! $ Argument_count) throw new Exception ("There is nothing to exececute, no arguments specified ."); $ Arguments = func_get_args (); $ Command_string = ($ argument_count> 1 )? Implode ("&", $ arguments): $ arguments [0]; $ Stream = @ ssh2_exec ($ this-> connection, $ command_string ); If (! $ Stream) throw new Exception ("Unable to execute the specified commands: {$ Command_string }"); } Catch (Exception $ e) { $ This-> error_message ($ e-> getMessage ()); } Return $ this; } Public function write_to_file ($ path = NULL, $ handle = NULL) { If (! $ This-> crontab_file_exists ()) { $ This-> handle = (is_null ($ handle ))? $ This-> handle: $ handle; $ This-> path = (is_null ($ path ))? $ This-> path: $ path; $ This-> cron_file = "{$ this-> path} {$ this-> handle }"; $ Init_cron = "crontab-l >{$ this-> cron_file} & [-f {$ this-> cron_file}] |>{$ this-> cron_file }"; $ This-> exec ($ init_cron ); } Return $ this; } Public function remove_file () { If ($ this-> crontab_file_exists () $ this-> exec ("rm {$ this-> cron_file }"); Return $ this; } Public function append_cronjob ($ cron_jobs = NULL) { If (is_null ($ cron_jobs) $ this-> error_message ("Nothing to append! Please specify a cron job or an array of cron jobs ."); $ Append_cronfile = "echo '"; $ Append_cronfile. = (is_array ($ cron_jobs ))? Implode ("n", $ cron_jobs): $ cron_jobs; $ Append_cronfile. = "'>>{$ this-> cron_file }"; $ Install_cron = "crontab {$ this-> cron_file }"; $ This-> write_to_file ()-> exec ($ append_cronfile, $ install_cron)-> remove_file (); Return $ this; } Public function remove_cronjob ($ cron_jobs = NULL) { If (is_null ($ cron_jobs) $ this-> error_message ("Nothing to remove! Please specify a cron job or an array of cron jobs ."); $ This-> write_to_file (); $ Cron_array = file ($ this-> cron_file, FILE_IGNORE_NEW_LINES ); If (empty ($ cron_array )) { $ This-> remove_file ()-> error_message ("Nothing to remove! The cronTab is already empty ."); } $ Original_count = count ($ cron_array ); If (is_array ($ cron_jobs )) { Foreach ($ cron_jobs as $ cron_regex) $ cron_array = preg_grep ($ cron_regex, $ cron_array, PREG_GREP_INVERT ); } Else { $ Cron_array = preg_grep ($ cron_jobs, $ cron_array, PREG_GREP_INVERT ); } Return ($ original_count === count ($ cron_array ))? $ This-> remove_file (): $ this-> remove_crontab ()-> append_cronjob ($ cron_array ); } Public function remove_crontab () { $ This-> remove_file ()-> exec ("crontab-r "); Return $ this; } Private function crontab_file_exists () { Return file_exists ($ this-> cron_file ); } Private function error_message ($ error) { Die (" ERROR: {$error} "); } } |