]> git.mxchange.org Git - friendica.git/blob - include/update_gcontact.php
Don't create lock files if the process is called from the poller via the worker
[friendica.git] / include / update_gcontact.php
1 <?php
2
3 require_once("boot.php");
4
5 function update_gcontact_run(&$argv, &$argc){
6         global $a, $db;
7
8         if(is_null($a)) {
9                 $a = new App;
10         }
11
12         if(is_null($db)) {
13                 @include(".htconfig.php");
14                 require_once("include/dba.php");
15                 $db = new dba($db_host, $db_user, $db_pass, $db_data);
16                 unset($db_host, $db_user, $db_pass, $db_data);
17         };
18
19         require_once('include/pidfile.php');
20         require_once('include/Scrape.php');
21         require_once("include/socgraph.php");
22
23         load_config('config');
24         load_config('system');
25
26         $a->set_baseurl(get_config('system','url'));
27
28         load_hooks();
29
30         logger('update_gcontact: start');
31
32         if(($argc > 1) && (intval($argv[1])))
33                 $contact_id = intval($argv[1]);
34
35         if(!$contact_id) {
36                 logger('update_gcontact: no contact');
37                 return;
38         }
39
40         // Don't check this stuff if the function is called by the poller
41         if (App::callstack() != "poller_run") {
42                 $lockpath = get_lockpath();
43                 if ($lockpath != '') {
44                         $pidfile = new pidfile($lockpath, 'update_gcontact'.$contact_id);
45                         if ($pidfile->is_already_running()) {
46                                 logger("update_gcontact: Already running for contact ".$contact_id);
47                                 if ($pidfile->running_time() > 9*60) {
48                                         $pidfile->kill();
49                                         logger("killed stale process");
50                                 }
51                                 exit;
52                         }
53                 }
54         }
55
56         $r = q("SELECT * FROM `gcontact` WHERE `id` = %d", intval($contact_id));
57
58         if (!$r)
59                 return;
60
61         if (!in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS)))
62                 return;
63
64         $data = probe_url($r[0]["url"]);
65
66         if (!in_array($data["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) {
67                 if ($r[0]["server_url"] != "")
68                         poco_check_server($r[0]["server_url"], $r[0]["network"]);
69
70                 q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `id` = %d",
71                         dbesc(datetime_convert()), intval($contact_id));
72                 return;
73         }
74
75         if (($data["name"] == "") AND ($r[0]['name'] != ""))
76                 $data["name"] = $r[0]['name'];
77
78         if (($data["nick"] == "") AND ($r[0]['nick'] != ""))
79                 $data["nick"] = $r[0]['nick'];
80
81         if (($data["addr"] == "") AND ($r[0]['addr'] != ""))
82                 $data["addr"] = $r[0]['addr'];
83
84         if (($data["photo"] == "") AND ($r[0]['photo'] != ""))
85                 $data["photo"] = $r[0]['photo'];
86
87
88         q("UPDATE `gcontact` SET `name` = '%s', `nick` = '%s', `addr` = '%s', `photo` = '%s'
89                                 WHERE `id` = %d",
90                                 dbesc($data["name"]),
91                                 dbesc($data["nick"]),
92                                 dbesc($data["addr"]),
93                                 dbesc($data["photo"]),
94                                 intval($contact_id)
95                         );
96
97         q("UPDATE `contact` SET `name` = '%s', `nick` = '%s', `addr` = '%s', `photo` = '%s'
98                                 WHERE `uid` = 0 AND `addr` = '' AND `nurl` = '%s'",
99                                 dbesc($data["name"]),
100                                 dbesc($data["nick"]),
101                                 dbesc($data["addr"]),
102                                 dbesc($data["photo"]),
103                                 dbesc(normalise_link($data["url"]))
104                         );
105
106         q("UPDATE `contact` SET `addr` = '%s'
107                                 WHERE `uid` != 0 AND `addr` = '' AND `nurl` = '%s'",
108                                 dbesc($data["addr"]),
109                                 dbesc(normalise_link($data["url"]))
110                         );
111 }
112
113 if (array_search(__file__,get_included_files())===0){
114         update_gcontact_run($_SERVER["argv"],$_SERVER["argc"]);
115         killme();
116 }