Guest User

Untitled

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