Guest User

Untitled

a guest
Aug 19th, 2014
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.08 KB | None | 0 0
  1. <?php
  2. /**
  3. * XCloner
  4. * Oficial website: http://www.xcloner.com/
  5. * -------------------------------------------
  6. * Creator: Liuta Romulus Ovidiu
  7. * License: GNU/GPL
  8. * Revision: 1.0
  9. * Date: July 2007
  10. **/
  11.  
  12. if( php_sapi_name() != 'cli' ){
  13.     echo "<h2>Please run this script from a terminal or through a cron scheduler interface</h2>";
  14.     exit;
  15.     }
  16.  
  17. // Set flag that this is a parent file
  18. @error_reporting(E_ALL^E_NOTICE);
  19. @set_time_limit('3600');
  20. define( '_VALID_MOS', 1 );
  21. header('Content-Type: text/html; charset=utf-8');
  22.  
  23. include_once("admin.cloner.html.php");
  24. include_once("cloner.functions.php");
  25.  
  26. require_once( 'cloner.config.php' );
  27.  
  28. ####### VERIFY IP ACCESS
  29. function check_user_ip($current_ip, $ip_list)
  30. {
  31.     $valid = FALSE;
  32.  
  33.     foreach ($ip_list as $ip){
  34.         $ip_or_name = gethostbyname($ip);
  35.         if ($current_ip == $ip_or_name) {
  36.             $valid = TRUE;
  37.             break; 
  38.         }
  39.     }
  40.  
  41.     return $valid;
  42. }
  43.  
  44. $ip_list = @explode("\r\n", $_CONFIG['cron_ip']);
  45. $ip_list[] = $_SERVER['SERVER_ADDR'];
  46. $curent_ip = $_SERVER["REMOTE_ADDR"];
  47.  
  48. if (check_user_ip($curent_ip, $ip_list) == FALSE) {
  49.     echo "Access Denied for ip $curent_ip!";
  50.     exit;
  51. }
  52. #########################
  53.  
  54. $script_dir = str_replace("\\","/",dirname(__FILE__));
  55. if(is_dir($script_dir)){
  56.  
  57.  chdir($script_dir);
  58.  
  59.  }
  60.  
  61. if($_REQUEST['config'] == ""){
  62.    
  63.     if($argv[1] != ""){
  64.    
  65.         $_REQUEST['config'] = $argv[1];
  66.     }
  67. }
  68.  
  69. //filter the config request path
  70. $_REQUEST['config'] = str_replace(array("..","/","\\"), array("","",""), trim($_REQUEST['config']));
  71.  
  72. if($_REQUEST['config'] != ""){
  73.    
  74.     require_once( './configs/'.$_REQUEST['config'] );
  75.    
  76.     $smsg = "Using configs/".$_REQUEST['config']." as configuration file";
  77.    
  78. }
  79. else{
  80.  
  81.     @require_once( './cloner.config.php' );
  82.    
  83.     $smsg = "Using default configuration file";
  84.  
  85. }
  86.  
  87. require_once("common.php");
  88. require_once("restore/TAR.php");
  89.  
  90. $mosConfig_live_site = $_CONFIG['mosConfig_live_site'];
  91.  
  92. logxx($smsg);
  93.  
  94.  
  95.  
  96.  
  97.  
  98. logxx("Starting ".$script_dir."/cloner.cron.php");
  99.  
  100.  
  101.  
  102.  
  103. # load language
  104. if($_CONFIG['select_lang']!="")
  105.  $mosConfig_lang = $_CONFIG['select_lang'];
  106. if (file_exists( "language/".$mosConfig_lang.".php" )) {
  107.     include_once( "language/".$mosConfig_lang.".php" );
  108.     @include_once( "language/english.php" );
  109. }
  110. else{
  111.     include_once( "language/english.php" );
  112. }
  113.  
  114.  
  115.  
  116. ####### VERIFY IP ACCESS
  117. /*$ip_list = @explode("\r\n", $_CONFIG['cron_ip']);
  118. $ip_list[] = $_SERVER['SERVER_ADDR'];
  119. $curent_ip = $_SERVER["REMOTE_ADDR"];
  120.  
  121. if(!in_array($curent_ip, $ip_list)){
  122.    
  123.     echo "Access Denied for ip $curent_ip!";
  124.     exit;
  125.    
  126. }*/
  127. #########################
  128.  
  129. $access=1;
  130. $_REQUEST[cron_dbonly] = 0;
  131. $_REQUEST[cron_access] = 1;
  132. $_REQUEST['databases_incl'] = @explode(",",$_CONFIG[databases_incl_list]);
  133.  
  134.  
  135.  
  136.  
  137. if($_CONFIG[cron_bname]!="")
  138.  $_REQUEST['bname'] = $_CONFIG[cron_bname];
  139.  
  140.  
  141. function logxx($string = ""){
  142.  
  143.     global $mail_log;
  144.  
  145.     $return = "<b>$string</b><br />\r\n";
  146.    
  147.     $mail_log .= $return;
  148.  
  149.     echo $return;
  150.     return;
  151.     }
  152.  
  153. $clonerPath = $_CONFIG['clonerPath'];
  154. logxx("Starting XCloner for site $mosConfig_live_site at ".date("Y-m-d H:i"));
  155.  
  156.     $excludedFolders = array();
  157.     $d_arr = array(); $d = 0;
  158.     $ds_arr = array();
  159.     $f_arr = array(); $f = 0;
  160.     $s_arr = array(); $s = 0;
  161.     $d_arr[$d] = $_CONFIG['backup_path'];
  162.     #logxx("Reading the file structure");
  163.    #recurseFiles($d_arr, $ds_arr, $f_arr, $s_arr, $d, $f, $s, $excludefolders, '');
  164.    #$excludedFolders = confirmBackup('nohtml');
  165.    #logxx("Done");
  166.    
  167.    
  168.  
  169. if($_CONFIG['cron_btype']==0){
  170.     $_REQUEST[dbbackup] = 1;
  171.  
  172.     logxx("Creating a fullbackup");
  173.     $msg = 'full backup';
  174.     }
  175. if($_CONFIG['cron_btype']==1){
  176.     $_REQUEST[dbbackup] = 0;
  177.  
  178.     logxx("Creating a files only backup");
  179.     $msg = 'file backup';
  180.     }
  181. if($_CONFIG['cron_btype']==2){
  182.     $_REQUEST[dbbackup] = 1;
  183.  
  184.     #for ($i=0, $n=count($excludedFolders); $i < $n; $i++) {
  185.        $_CONFIG['cron_exclude'] = $_CONFIG['backup_path'];
  186.         $GLOBALS['_CONFIG'] = $_CONFIG;
  187.         $_REQUEST[cron_dbonly] = 1;
  188.     #}
  189.    
  190.     logxx("Creating an sql only backup");
  191.     $msg = "database backup";
  192.    }
  193.  
  194.  
  195. logxx("Generating Backup ...");
  196. $file = generateBackup( $excludedFolders, 'nohtml' );
  197. logxx("Backup Done");
  198.  
  199. $source_file = $clonerPath."/".$file;
  200.  
  201. logxx("Backup file: ".$source_file);
  202. $bsize = getFileSizeText(filesize($source_file));
  203.  
  204. if($_CONFIG['cron_send']==1)
  205. {
  206.     ######################################STARTING FTP TRANSFER##################
  207.    
  208.     $source_files[] = $source_file;
  209.     $destination_files[] = $_CONFIG[cron_ftp_path]."/".$file;
  210.    
  211.     // set up basic connection details
  212.     list($fhost, $fport) = explode(":",$_CONFIG[cron_ftp_server]);
  213.     if($fport == "")
  214.         $fport = '21';
  215.      
  216.     $ftp_timeout = '3600';
  217.    
  218.     logxx("Starting ftp transfer:");
  219.     if(!$_CONFIG[secure_ftp])
  220.     {
  221.         // set up basic connection
  222.         $conn_id = ftp_connect($fhost, (int)$fport, (int)$ftp_timeout);
  223.         $connect = "Normal";
  224.        
  225.         // login with username and password
  226.         $login_result = ftp_login($conn_id, $_CONFIG[cron_ftp_user], $_CONFIG[cron_ftp_pass])
  227.                         or die("Authentification failed when connecting to the ftp server for user ".$_CONFIG[cron_ftp_user]." with pass".$_CONFIG[cron_ftp_pass]);
  228.        
  229.         // check connection
  230.         if ((!$conn_id) || (!$login_result)) {
  231.                echo "<b  style='color:red'>FTP connection has failed!</b>";
  232.                echo "<b  style='color:red'>Attempted to connect to ".$_CONFIG[cron_ftp_server]." for user ".$_CONFIG[cron_ftp_user]."</b>";
  233.                return;
  234.            } else {
  235.                #echo "Connected to $_REQUEST[ftp_server], for user $_REQUEST[ftp_user]";
  236.           }
  237.        
  238.         if($_CONFIG['system_ftptransfer']==1)
  239.         {
  240.             // turn passive mode on
  241.             @ftp_pasv($conn_id, true);
  242.             $mode = "Passive";
  243.         }
  244.         else
  245.         {
  246.             // turn passive mode off
  247.             @ftp_pasv($conn_id, false);
  248.             $mode = "Active";
  249.         }
  250.         echo "Connected to $connect <b>$_CONFIG[cron_ftp_server] Mode: $mode</b><br />";
  251.         for($i=0;$i<sizeof($source_files);$i++)
  252.         {
  253.             // upload the file
  254.             $upload = ftp_put($conn_id, $destination_files[$i], $source_files[$i], FTP_BINARY);
  255.            
  256.             // check upload status
  257.             if (!$upload) {
  258.                    echo "<b style='color:red'>FTP upload has failed for file $source_files[$i] ! Stopping ....<br /></b>";return;
  259.                } else {
  260.                    echo "<b>Upload success from file <i>$source_files[$i]</i> <br />to <i>$destination_files[$i]</i> ...<br /></b>";
  261.                }
  262.         }
  263.        
  264.         // close the FTP stream
  265.         ftp_close($conn_id);
  266.     }
  267.     else //Use sftp
  268.     {
  269.         //set php path to include required sftp files
  270.         set_include_path(get_include_path() . PATH_SEPARATOR .'classes/phpseclib');
  271.  
  272.         include('Net/SFTP.php');
  273.         //define('NET_SFTP_LOGGING', NET_SFTP_LOG_COMPLEX); // or NET_SFTP_LOG_SIMPLE
  274.  
  275.         //connect to host and authenticate user
  276.         $sftp = new Net_SFTP($fhost);
  277.         if (!$sftp->login($_CONFIG[cron_ftp_user], $_CONFIG[cron_ftp_pass]))
  278.         {
  279.             logxx('Login Failed');
  280.             die("Login Failed");
  281.         }
  282.         logxx("Connected to $connect <b>$_CONFIG[cron_ftp_server] Successfully!><br />");
  283.        
  284.         //transfere files
  285.         for($i=0;$i<sizeof($source_files);$i++)
  286.         {
  287.             // upload the file
  288.             $upload = $sftp->put($destination_files[$i], $source_files[$i], NET_SFTP_LOCAL_FILE);
  289.            
  290.             // check upload status
  291.             if (!$upload) {
  292.                    logxx("<b style='color:red'>FTP upload has failed for file $source_files[$i] ! Stopping ....<br /></b>");return;
  293.                } else {
  294.                    logxx("<b>Upload success from file <i>$source_files[$i]</i> <br />to <i>$destination_files[$i]</i> ...<br /></b>");
  295.                }
  296.         }
  297.        
  298.         //disconnect from server
  299.         unset($sftp);
  300.     }
  301.    
  302.     logxx("Ftp transfer finished succesfully!");
  303.    
  304.     if($_CONFIG[cron_ftp_delb]==1)
  305.     {
  306.         @unlink($source_file);
  307.         logxx("Backup succesfully deleted from the original server!");
  308.     }
  309.     ##############################################################################
  310. }
  311. else if($_CONFIG['cron_send']==2)
  312. {
  313. #######################################STARTING Email TRANSFER################
  314.    logxx("Sending mail with backup");
  315.  
  316.     $date = date("Y-m-d H:i");
  317.     $mode = '1';
  318.     $message = "
  319.    <pre>
  320.    Attached is the backup generated on $date
  321.    Source Filename: $source_file
  322.    Server: $mosConfig_live_site
  323.    
  324.    Powered by http://www.xcloner.com - XCloner - Backup and Restore made easy!
  325.    </pre>
  326.  
  327.    ";
  328.    
  329.     $ok = send_mail($mosConfig_mailfrom, "XCloner $msg", $message, $_CONFIG['cron_email_address'], $source_file);
  330.    
  331.     #echo mosMail( $mosConfig_mailfrom, $mosConfig_fromname, $_CONFIG['cron_email_address'], "XCloner $msg", $message, $mode, '', '' );
  332.    if($ok)
  333.      logxx("Mail sent to ".$_CONFIG['cron_email_address']);
  334.     else
  335.      logxx("There was an error in sending the mail cron to ".$_CONFIG['cron_email_address']);  
  336.  
  337. ##############################################################################
  338.    }
  339. logxx("Total backup size:".$bsize);
  340.  
  341.  
  342. ####### STARING AMAZON S3 MODE
  343. if($_CONFIG['cron_amazon_active']){
  344.  
  345. include_once("classes/S3.php");
  346.  
  347. logxx();
  348.  
  349. if(!$_CONFIG['cron_amazon_ssl'])
  350.     $amazon_ssl = false;
  351. else
  352.     $amazon_ssl = true;
  353.  
  354. $s3 = new S3($_CONFIG['cron_amazon_awsAccessKey'], $_CONFIG['cron_amazon_awsSecretKey'], $amazon_ssl);
  355.  
  356. logxx("AMAZON S3: Starting communication with the Amazon S3 server...ssl mode ".(int)$amazon_ssl);
  357.  
  358. $buckets = $s3->listBuckets();
  359.  
  360. if (($s3->putBucket($_CONFIG['cron_amazon_bucket'], "private")) || (@in_array($_CONFIG['cron_amazon_bucket'], $buckets))) {
  361.  
  362.  
  363.     if ($s3->putObjectFile($clonerPath."/".$file, $_CONFIG['cron_amazon_bucket'], $_CONFIG['cron_amazon_dirname']."/".baseName($file), "private")){
  364.  
  365.         logxx("AMAZON S3: File copied to {".$_CONFIG['cron_amazon_bucket']."}/".$_CONFIG['cron_amazon_dirname']."/".$file);
  366.  
  367.     }else {
  368.         logxx("AMAZON S3: Failed to copy file to {".$_CONFIG['cron_amazon_bucket']."}/".$_CONFIG['cron_amazon_dirname']."/".$file);exit;
  369.  
  370.         }
  371.  
  372.  
  373. }else{
  374.  
  375.     logxx("AMAZON S3: Unable to create bucket ".$_CONFIG['cron_amazon_bucket']." (it may already exist and/or be owned by someone else)!");exit;
  376. }
  377.  
  378.  
  379.  
  380. }
  381. ###### END
  382.  
  383. ####### STARING DROPBOX MODE
  384. if($_CONFIG['cron_dropbox_active']){
  385.  
  386. include_once("classes/DropboxClient.php");
  387.  
  388. $dropbox = new DropboxClient(array(
  389.     'app_key' => $_CONFIG['cron_dropbox_Key'],
  390.     'app_secret' => $_CONFIG['cron_dropbox_Secret'],
  391.     'app_full_access' => false,
  392. ),'en');
  393.  
  394. logxx();
  395.  
  396. logxx("DROPBOX: Starting communication with the DropBox server...");
  397.  
  398. // first try to load existing access token
  399. $access_token = load_token("access");
  400. if(!empty($access_token)) {
  401.     $dropbox->SetAccessToken($access_token);
  402.     //print_r($access_token);
  403. }
  404. elseif(!empty($_GET['auth_callback'])) // are we coming from dropbox's auth page?
  405. {
  406.     // then load our previosly created request token
  407.     $request_token = load_token($_GET['oauth_token']);
  408.     if(empty($request_token)) die('Request token not found!');
  409.    
  410.     // get & store access token, the request token is not needed anymore
  411.     $access_token = $dropbox->GetAccessToken($request_token);  
  412.     store_token($access_token, "access");
  413.     delete_token($_GET['oauth_token']);
  414. }
  415.  
  416. // checks if access token is required
  417. if(!$dropbox->IsAuthorized())
  418. {
  419.     // redirect user to dropbox auth page
  420.     $return_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']."?auth_callback=1";
  421.     $auth_url = $dropbox->BuildAuthorizeUrl($return_url);
  422.     $request_token = $dropbox->GetRequestToken();
  423.     store_token($request_token, $request_token['t']);
  424.     die("Authentication required. <a href='$auth_url'>Click here.</a>");
  425. }
  426.  
  427.  
  428.  
  429. $return = $dropbox->UploadFile($clonerPath."/".$file, $_CONFIG['cron_dropbox_dirname']."/".$file);
  430. if($return->error)
  431.     logxx($return->error);
  432. else
  433.     logxx("File copied to ".$return->path);
  434.    
  435. #print_r($return);
  436.  
  437. }
  438. ###### END
  439.  
  440.  
  441. ######## DELETING OLDER BACKUPS ##############################################
  442.  
  443. if($_CONFIG['cron_file_delete_act']){
  444.  
  445. logxx("<br />\nDeleting older backups than ".$_CONFIG['cron_file_delete']." days: ");
  446. delete_older_backups($clonerPath);
  447.  
  448. }
  449.  
  450.  
  451. ###################END OLDER BACKUPS
  452.  
  453. $logemail = explode(";", $_CONFIG['cron_logemail']);
  454. if($_CONFIG['cron_fromlogemail'])
  455.     $fromemail = $_CONFIG['cron_fromlogemail'];
  456. else
  457.     $fromemail = "[email protected]";
  458.    
  459. if(sizeof($logemail)>0){
  460.    
  461.     for($i=0; $i<sizeof($logemail);$i++){
  462.        
  463.         $email = trim($logemail[$i]);
  464.         if($email != ""){
  465.            
  466.             $email_subject = "cron log ".time();
  467.            
  468.             $headers ="From: \"XCloner Cron Log Output\" <".$fromemail.">\n";
  469.  
  470.             if(mail($email, $email_subject, strip_tags($mail_log), $headers)){
  471.            
  472.                 logxx ("Notification Mail was sent to $email");
  473.                
  474.             }
  475.            
  476.            
  477.         }
  478.        
  479.     }
  480.    
  481. }
  482.  
  483. logxx("<br />\n\nALL DONE! I will exit now from cron.");
  484. exit;
  485.  
  486. ?>
  487. <?php
  488.  
  489. function delete_older_backups($clonerPath){
  490.   global $_CONFIG;
  491. # initialise list arrays, directories and files separately and array counters for them
  492. $d_arr = array(); $d = 0;
  493. $f_arr = array(); $f = 0;
  494. $s_arr = array(); $s = 0;
  495.  
  496. # obtain the list of backup archive files
  497. getBackupFiles($d_arr, $f_arr, $s_arr, $d, $f);
  498.  
  499. if(is_array($f_arr))
  500. foreach($f_arr as $file)
  501.   if(($file!='.')&&($file != '..')){
  502.    
  503.     #logxx("Processing backup file $file");
  504.     $cfile = $_CONFIG['clonerPath']."/".$file;
  505.    
  506.     $filemtime = filemtime($cfile) ;
  507.    
  508.     $ftime= $filemtime + $_CONFIG['cron_file_delete']*24*60*60;
  509.    
  510.     $ctime = time();
  511.    
  512.     if($ftime < $ctime){
  513.        
  514.       if(unlink($cfile))
  515.       logxx("Deleted backup file $file created on ".date("Y-m-d",  $filemtime));
  516.       else
  517.       logxx("Could not delete backup file $file, please delete it manually");
  518.        
  519.      }
  520.        
  521. }
  522.  
  523. }
  524.  
  525. function send_mail($email_from, $email_subject, $email_txt, $email_to, $fileatt){
  526. $fileatt_type = "application/octet-stream"; // File Type
  527. $fileatt_name = basename($fileatt); // Filename that will be used for the file as the attachment
  528. $data = "";
  529.  
  530. $headers = "From: \"Cronbackup XCloner\" <".$email_from.">";
  531.  
  532. if($fileatt != ""){
  533.    
  534. if($file = @fopen($fileatt,'rb')){
  535.      
  536. $data = fread($file,filesize($fileatt));
  537.  
  538. fclose($file);
  539.  
  540. }
  541. else{
  542.     logxx("Unable to open file $fileatt");
  543. }
  544.  
  545. }
  546.  
  547. $semi_rand = md5(time());
  548. $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  549.    
  550. $headers .= "\nMIME-Version: 1.0\n" .
  551.             "Content-Type: multipart/mixed;\n" .
  552.             " boundary=\"{$mime_boundary}\"";
  553.  
  554. $email_message .= "This is a multi-part message in MIME format.\n\n" .
  555.                 "--{$mime_boundary}\n" .
  556.                 "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
  557.                "Content-Transfer-Encoding: 7bit\n\n" .
  558. $email_txt . "\n\n";
  559.  
  560. $data = chunk_split(base64_encode($data));
  561.  
  562. $email_message .= "--{$mime_boundary}\n" .
  563.                   "Content-Type: {$fileatt_type};\n" .
  564.                   " name=\"{$fileatt_name}\"\n" .
  565.                   //"Content-Disposition: attachment;\n" .
  566.                   //" filename=\"{$fileatt_name}\"\n" .
  567.                   "Content-Transfer-Encoding: base64\n\n" .
  568.                  $data . "\n\n" .
  569.                   "--{$mime_boundary}--\n";
  570.  
  571. $ok = mail($email_to, $email_subject, $email_message, $headers);
  572.  
  573. return $ok;
  574.  
  575. }
  576. ?>
  577. <?php
  578. function store_token($token, $name)
  579. {
  580.     if(!file_put_contents("tokens/$name.token", serialize($token)))
  581.         die('<br />Could not store token! <b>Make sure that the directory `tokens` exists and is writable!</b>');
  582. }
  583.  
  584. function load_token($name)
  585. {
  586.     if(!file_exists("tokens/$name.token")) return null;
  587.     return @unserialize(@file_get_contents("tokens/$name.token"));
  588. }
  589.  
  590. function delete_token($name)
  591. {
  592.     @unlink("tokens/$name.token");
  593. }
  594. ?>
Add Comment
Please, Sign In to add comment