-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalfresco_script.php
executable file
·376 lines (321 loc) · 12.9 KB
/
alfresco_script.php
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
if (extension_loaded('newrelic')) { // Ensure PHP agent is available
newrelic_background_job();
}
/**
* Root directory of Drupal installation.
*/
define('DRUPAL_ROOT', getcwd());
/**
* Bootstraping drupal so we can directly run node insert
*/
include_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
include(DRUPAL_ROOT . '/sites/default/settings.php');
include(DRUPAL_ROOT . '/sites/all/modules/gofast/gofast_cmis/gofast_cmis_cron.inc');
write_log_info("begin of script", false);
$login_bdd = $databases['default']['default']['username'];
$mdp_bdd = $databases['default']['default']['password'];
$db_connexion = null;
$domain_url = "http://localhost/";
//Inform Alfresco that we received the request, then close the connection and continue the PHP script
//We do that to let alfresco release it's lock on the node and to improve end-user performances
print('OK');
fastcgi_finish_request();
set_time_limit(0);
// --------------------------
// Manage soffice restart
// --------------------------
if($_GET['op'] == GOFAST_CMIS_CRON_OPERATION_RESTART_SOFFICE){
write_log_info("Operation: restart_soffice", false);
execute_soffice_restart_command();
exit;
}
// -------------------------------
// Request variables
// -------------------------------
$nodes = json_decode($_POST['nodes'], TRUE);
write_log_info($_POST['nodes']);
//For each node asked by the request, we trigger the replication
foreach($nodes as $node){
$type = $_GET['type'] = $node['type'];
$messageto = $_GET['messageto'] = $node['messageto'];
$name = $_GET['name'] = $node['name'];
$person = $_GET['person'] = $node['person'];
$space = $_GET['space'] = $node['space'];
$destination = $_GET['destination'] = $node['destination'];
$group = $_GET['group'] = $node['group'];
$noderef = $_GET['noderef'] = $node['noderef'];
$op = $_GET['op'] = $node['op'];
write_log_info(":noderef = {$noderef}, :name = {$name}, :type = {$type}, :space = {$space}, :person = {$person}, :destination = {$destination}, :op = {$op}, :messageto = {$messageto}", false);
// -------------------------------
// Exit conditions
// -------------------------------
if ($name === "pdf") {
write_log_info("Name is 'pdf', exiting", false);
exit;
}
//empeche la replication des lock
if (strstr($name, ".~lock") || strstr($name, ".~$") || strstr($name, "~$")) {
write_log_info("It's a log file, exiting", false);
exit;
}
//empeche la replication des fichiers commencants par .
if (isset($name[0]) && $name[0] == ".") {
write_log_info("File starting by . are ignored, exiting", false);
exit;
}
//empeche la replication des fichiers .tmp
if (strstr($name, ".tmp")) {
write_log_info(".tmp files are ignored, exiting", false);
exit;
}
//empeche la replication des fichiers thumbs.db
if (strstr($name, "Thumbs.db")) {
write_log_info("thumbs.db files are ignored, exiting", false);
exit;
}
write_log_info("Current operation : " . $op, false);
/*if($op === "start_replication") {
write_log_info("Starting the replication", false);
check_items_processable();
return;
}*/
switch ($op) {
case GOFAST_CMIS_CRON_OPERATION_RESTART_SOFFICE:
$op = GOFAST_CMIS_CRON_OPERATION_RESTART_SOFFICE;
write_log_info("Operation: restart_soffice", false);
execute_soffice_restart_command();
break;
case GOFAST_CMIS_CRON_OPERATION_SYNC:
$op = GOFAST_CMIS_CRON_OPERATION_SYNC;
write_log_info("Operation: Sync", false);
alfresco_script_synchronize();
break;
default:
write_log_info("Unknown operation ! A mail was sent to CEO-Vision support team", false);
alfresco_script_send_alert_email("Unknown operation", TRUE);
break;
}
}
/*
* This function send an alert mail to CEO-Vision support team
* @param $reason
* The reason of the alert
* @param fail
* Is this error resulting in a replication fail
*/
function alfresco_script_send_alert_email($reason, $fail = FALSE){
// -------------------------------
// Request variables
// -------------------------------
$op = $_GET["op"];
$name = get_parameter_value("name");
$person = get_parameter_value("person");
$path = get_parameter_value("destination");
$gid = get_parameter_value("groupe");
$noderef = get_parameter_value("noderef");
//Prepare mail
$criticity = $fail ? "failed" : "warn";
$subject = "[GoFast Report] Replication " . $criticity . " (Drupal alfresco_script.php)";
$title = "GoFast Report";
$footer = "GoFast Report notification E-mail";
$body = "Drupal received a replication request but an error occured. Criticity: " . $criticity;
$body .= "<br />Reason: " . $reason;
$body .= "<div><table>";
$body .= "<tr><td>Node reference</td><td>Operation</td><td>Title</td><td>Space ID</td><td>Creator</td><td>Destination path</td>";
$body .= "<tr><td>". $noderef ."</td><td>". $op ."</td><td>". $name ."</td><td>". $gid ."</td><td>". $person ."</td><td>". $path ."</td></tr>";
$body .= "</table></div>";
$recpt = array(array('recpt' => '[email protected]', 'method' => 'to'));
//Send mail
gofast_mail_queue_api_send_mail($subject, $title, $footer, $body, $recpt);
}
/*
* Trigger the replication using instant replication mode
*/
function alfresco_script_synchronize(){
// -------------------------------
// Request variables
// -------------------------------
$name = get_parameter_value("name");
$person = get_parameter_value("person");
$path = get_parameter_value("destination");
$gid = get_parameter_value("space");
$noderef = get_parameter_value("noderef");
$author = get_parameter_value("author");
$op = $_GET["op"];
//Ask database to know what to do with the request
$presence = check_item_already_present($noderef, $op);
//Insert the row in the table if needed
if($presence == "UNKNOWN"){ //No entry found
write_log_info("No other entry found in database, processing the request", false);
if(alfresco_script_replication_mode(user_load_by_name($person)->uid)){ //Mode: Instant replication
$row = insert_db_row($noderef, GOFAST_CMIS_CRON_PROCESSING_PROCESS, $op, $name, $person, $path, $gid, $author);
//Process the item
module_load_include('inc', 'gofast_cmis', 'gofast_cmis_cron');
write_log_info("Mode : Instant replication", false);
$return = gofast_cmis_process_item($noderef, $name, $person, $path, $gid, $author);
usurp(-1);
//Delete the row at the end of the process
if($return != 2){
delete_db_row($row);
}
}
else{ //Mode : Cron replication
write_log_info("Mode : Cron replication", false);
$row = insert_db_row($noderef, GOFAST_CMIS_CRON_PROCESSING_WAIT, $op, $name, $person, $path, $gid, $author);
}
write_log_info("Processing finished !", false);
}
else if($presence == "FULL"){ //Already in queue
write_log_info("This request is already processing, exiting", false);
exit();
}
else{ //Shouldn't happend
write_log_info("Unknown item presence state, exiting and sending a mail to CEO-Vision support team", false);
alfresco_script_send_alert_email("Unknown item presence state", TRUE);
exit();
}
}
function alfresco_script_replication_mode($uid){
$connexion = get_db_connexion();
$stmt = $connexion->prepare("SELECT state from alfresco_replication WHERE uid = :uid");
$stmt->bindParam(':uid', $uid);
$stmt->execute();
$result = $stmt->fetchAll();
$count = count($result);
if($count > GOFAST_CMIS_REPLICATING_MAX_ACTIVITY){ //Mode cron
return FALSE;
}
else{ //Mode instant replication
return TRUE;
}
}
/**
* This function creates a file that triggers soffice restart.
*/
function execute_soffice_restart_command() {
$file = fopen(DRUPAL_ROOT . "/sites/default/files/restart_soffice.txt", "a+");
fwrite($file, $_SERVER['REQUEST_URI'] . "\n\n");
fclose($file);
}
/**
* This method returns parameter value if exists, empty string otherwise
* @param type $param_name the parameter to get in $_GET request variable
* @return string the value of the parameter if exists, empty string otherwise.
*/
function get_parameter_value($param_name) {
return isset($_GET[$param_name]) ? $_GET[$param_name] : "";
}
/**
* This method inserts a new row corresponding to remote store item. These row are used for replication and contains all the necessary info for Drupal to process.
* @param type $noderef the remote store reference
* @param type $op the operation done in remote store
* @param type $person the person that triggers this event on remote store
* @param type $name the name of the node if needed
* @param type $type the type of item replicated
* @param type $space the root item in which the item is processed (ex: added in a parent folder)
* @param type $destination TODO
* @param type $messageto TODO
*/
function insert_db_row($noderef, $state, $op, $name, $person, $path, $gid, $author) {
$connexion = get_db_connexion();
$time = time();
write_log_info("Inserting: " . $noderef . $state . $op . $person, false);
$stmt = $connexion->prepare("INSERT INTO alfresco_replication (noderef,name,person,path,gid,author,uid,op,state,runsince) VALUES (:noderef, :name, :person, :path, :gid, :author, :uid, :op, :state, :runsince)");
$stmt->bindParam(':noderef', $noderef);
$stmt->bindParam(':name', $name);
$stmt->bindParam(':person', $person);
$stmt->bindParam(':path', $path);
$stmt->bindParam(':gid', $gid);
$stmt->bindParam(':author', $author);
$stmt->bindParam(':uid', user_load_by_name($person)->uid);
$stmt->bindParam(':op', $op);
$stmt->bindParam(':state', $state);
$stmt->bindParam(':runsince', time());
$result = $stmt->execute();
$success = $result ? "true" : "false";
$row = $connexion->lastInsertId();
write_log_info("insert_db_row. Success = " . $success . ' ('.$row.')', false);
if($success == "false"){
alfresco_script_send_alert_email("Database insert failed for " . $noderef, TRUE);
exit();
}
return $row;
}
/*
* Delete the specified row in the alfresco replication table
*/
function delete_db_row($aid) {
$connexion = get_db_connexion();
$time = time();
write_log_info("Deleting row: " . $aid, false);
$stmt = $connexion->prepare("DELETE FROM alfresco_replication WHERE aid=:aid");
$stmt->bindParam(':aid', $aid);
$result = $stmt->execute();
$success = $result ? "true" : "false";
write_log_info("delete_db_row. Success = " . $success, false);
if($success == "false"){
alfresco_script_send_alert_email("Database delete failed for row ".$aid, TRUE);
exit();
}
return TRUE;
}
/**
* Check if another request is present and either, return the current state
* States:
* -UNKNOWN
* No other request found
* -PROCESSING
* The replication is processing
* -FULL
* A waiting request was already found
* @param type $noderef
* @param type $op
* @return type
*/
function check_item_already_present($noderef, $op) {
$connexion = get_db_connexion();
$stmt = $connexion->prepare("SELECT state from alfresco_replication WHERE noderef = :noderef AND op = :op");
$stmt->bindParam(':noderef', $noderef);
$stmt->bindParam(':op', $op);
$stmt->execute();
$result = $stmt->fetchAll();
$count = count($result);
if($count == 0){ //No request
$return = "UNKNOWN";
}
else{
$return = "FULL";
}
return $return;
}
/**
* This method connects to the database and returns the PDO connexion objet.
* If the PDO connexion already exists, returns the object.
* @return type
*/
function get_db_connexion() {
global $db_connexion, $login_bdd, $mdp_bdd;
$logInfo = null !== $db_connexion ? "Retrieving previouly created connexion" : "Creating connexion : login = " . $login_bdd . ", pwd = " . $mdp_bdd;
//write_log_info($logInfo, false);
$db_connexion = null !== $db_connexion ? $db_connexion : new PDO('mysql:host=localhost;dbname=d7', $login_bdd, $mdp_bdd);
return $db_connexion;
}
/**
* This method logs each request made to the server and logs additionnal information if provided.
* @param type $info the information we want to add to log line.
* @param boolean $request boolean that indicates to log the request or not
*/
function write_log_info($info = null, $request = true) {
$execution_date = date("Y-m-d H:i:s");
$file = fopen("/var/www/d7/sites/default/files/logs/alfresco_script.log", "a+");
if (!$file) {
return;
}
$request_data = $request ? " - Request = " . $_SERVER['REQUEST_URI'] : "";
$informations = null !== $info ? " - Infos = " . $info : "";
fwrite($file, $execution_date . $request_data . $informations . "\n");
fclose($file);
}
write_log_info("end of script", false);