-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsync-files.php
More file actions
150 lines (119 loc) · 4 KB
/
sync-files.php
File metadata and controls
150 lines (119 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/*
* PHP File Synchronization Script
* Copyright (C) 2014 Sergio Bobillier Ceballos <sergio.bobillier@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
require_once("file_synchronizer.php");
require_once("loggers/console_logger.php");
/*******************************************************************************
* This is the main script
*
* Run this file to use the script from the command line. This file will load
* the settings from the settings.php file into the class. The file will also
* load the last syncrhonization date from disk and start the synchronization.
*
*/
// Load the settings file and instantiate the class.
$settings = array();
require_once("settings.php");
$file_synchronizer = new File_Synchronizer($settings);
$logger = null;
// If debug_mode is enabled in the settings array we create a new console
// logger and pass it to the class so it logs it's output to it.
if($settings["debug_mode"] == true)
{
$logger = new Console_Logger();
$file_synchronizer->set_logger($logger);
}
/*******************************************************************************
* Last synchronization date
*
* The program tries to load the date of the last synchronization from a file
* called .last-sync in the current path. The field should contain the unix
* timestamp of the last synchronization.
*
* If the file cannot be opened or the contents are not valid the script asumes
* that the two paths have never been synchronized.
*
*/
// Load and set up the time of the last synchronization
if($logger != null)
$logger->log_message("Trying to retrieve the time of the last synchronization...");
$last_sync_time = 0;
$last_sync_file_name = ".last-sync";
$last_sync_file = @fopen($last_sync_file_name, "r");
if($last_sync_file)
{
$last_sync_time = fread($last_sync_file, 20);
$reg_ex = "/^[0-9]+$/";
if(!preg_match($reg_ex, $last_sync_time))
{
if($logger != null)
{
$logger->log_message("Last synchronization time does not have the right format.");
$logger->log_message("Asuming the paths have never been synchronized before.");
}
$last_sync_time = 0;
}
fclose($last_sync_file);
}
else
{
if($logger != null)
$logger->log_message("Could not open file asuming the paths have never been synchronized");
}
$file_synchronizer->set_last_sync_time($last_sync_time);
try
{
if($logger != null)
$logger->log_message("Starting synchronization...");
// Start the synchronization
$file_synchronizer->start_sync();
// We save the current time to the file. This is the time the last
// synchronization was made.
if($logger != null)
$logger->log_message("Saving the last synchronization time to disk...");
$last_sync_time = time();
if(!$file_synchronizer->get_simulate())
{
$last_sync_file = @fopen($last_sync_file_name, "w");
if($last_sync_file)
{
fwrite($last_sync_file, time());
fclose($last_sync_file);
}
else
{
if($logger != null)
$logger->log_message("Could not save the last synchronization time, unable to open the file for writing.");
}
}
// End of the script
if($logger != null)
$logger->log_message("Synchronization successful!");
exit(0);
}
catch(Synchronization_Exception $sync_ex)
{
if($logger != null)
{
$logger->log_message("ERROR: The synchronization process have failed!. The exit code is: " . $sync_ex->get_exit_code());
$logger->log_message("\t" . $sync_ex->getMessage());
}
exit($sync_ex->get_exit_code());
}
?>