friendica-6950 uimport doesn't support `"pwdreset_time":null`
[friendica.git/.git] / src / Worker / Cron.php
1 <?php
2 /**
3  * @file src/Worker/Cron.php
4  */
5 namespace Friendica\Worker;
6
7 use Friendica\BaseObject;
8 use Friendica\Core\Addon;
9 use Friendica\Core\Config;
10 use Friendica\Core\Hook;
11 use Friendica\Core\Logger;
12 use Friendica\Core\Protocol;
13 use Friendica\Core\Worker;
14 use Friendica\Database\DBA;
15 use Friendica\Model\Contact;
16 use Friendica\Util\DateTimeFormat;
17
18 class Cron
19 {
20         public static function execute($parameter = '', $generation = 0)
21         {
22                 $a = BaseObject::getApp();
23
24                 // Poll contacts with specific parameters
25                 if (!empty($parameter)) {
26                         self::pollContacts($parameter, $generation);
27                         return;
28                 }
29
30                 $last = Config::get('system', 'last_cron');
31
32                 $poll_interval = intval(Config::get('system', 'cron_interval'));
33
34                 if ($last) {
35                         $next = $last + ($poll_interval * 60);
36                         if ($next > time()) {
37                                 Logger::log('cron intervall not reached');
38                                 return;
39                         }
40                 }
41
42                 Logger::log('cron: start');
43
44                 // Fork the cron jobs in separate parts to avoid problems when one of them is crashing
45                 Hook::fork($a->queue['priority'], "cron");
46
47                 // run the process to discover global contacts in the background
48                 Worker::add(PRIORITY_LOW, "DiscoverPoCo");
49
50                 // run the process to update locally stored global contacts in the background
51                 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "checkcontact");
52
53                 // Expire and remove user entries
54                 Worker::add(PRIORITY_MEDIUM, "CronJobs", "expire_and_remove_users");
55
56                 // Call possible post update functions
57                 Worker::add(PRIORITY_LOW, "CronJobs", "post_update");
58
59                 // Clear cache entries
60                 Worker::add(PRIORITY_LOW, "CronJobs", "clear_cache");
61
62                 // Repair missing Diaspora values in contacts
63                 Worker::add(PRIORITY_LOW, "CronJobs", "repair_diaspora");
64
65                 // Repair entries in the database
66                 Worker::add(PRIORITY_LOW, "CronJobs", "repair_database");
67
68                 // once daily run birthday_updates and then expire in background
69                 $d1 = Config::get('system', 'last_expire_day');
70                 $d2 = intval(DateTimeFormat::utcNow('d'));
71
72                 // Daily cron calls
73                 if ($d2 != intval($d1)) {
74
75                         Worker::add(PRIORITY_LOW, "CronJobs", "update_contact_birthdays");
76
77                         Worker::add(PRIORITY_LOW, "CronJobs", "update_photo_albums");
78
79                         // update nodeinfo data
80                         Worker::add(PRIORITY_LOW, "CronJobs", "nodeinfo");
81
82                         Worker::add(PRIORITY_LOW, "DiscoverPoCo", "update_server");
83
84                         Worker::add(PRIORITY_LOW, "DiscoverPoCo", "suggestions");
85
86                         Worker::add(PRIORITY_LOW, 'Expire');
87
88                         Worker::add(PRIORITY_MEDIUM, 'DBClean');
89
90                         // check upstream version?
91                         Worker::add(PRIORITY_LOW, 'CheckVersion');
92
93                         Config::set('system', 'last_expire_day', $d2);
94                 }
95
96                 // Hourly cron calls
97                 if (Config::get('system', 'last_cron_hourly', 0) + 3600 < time()) {
98
99                         // Delete all done workerqueue entries
100                         DBA::delete('workerqueue', ['`done` AND `executed` < UTC_TIMESTAMP() - INTERVAL 1 HOUR']);
101
102                         // Optimizing this table only last seconds
103                         if (Config::get('system', 'optimize_workerqueue', false)) {
104                                 DBA::e("OPTIMIZE TABLE `workerqueue`");
105                         }
106
107                         Config::set('system', 'last_cron_hourly', time());
108                 }
109
110                 // Ensure to have a .htaccess file.
111                 // this is a precaution for systems that update automatically
112                 $basepath = $a->getBasePath();
113                 if (!file_exists($basepath . '/.htaccess')) {
114                         copy($basepath . '/.htaccess-dist', $basepath . '/.htaccess');
115                 }
116
117                 // Poll contacts
118                 self::pollContacts($parameter, $generation);
119
120                 Logger::log('cron: end');
121
122                 Config::set('system', 'last_cron', time());
123
124                 return;
125         }
126
127         /**
128          * @brief Poll contacts for unreceived messages
129          *
130          * @todo  Currently it seems as if the following parameter aren't used at all ...
131          *
132          * @param string  $parameter Parameter (force, restart, ...) for the contact polling
133          * @param integer $generation
134          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
135          */
136         private static function pollContacts($parameter, $generation) {
137                 $manual_id  = 0;
138                 $generation = 0;
139                 $force      = false;
140
141                 if ($parameter == 'force') {
142                         $force = true;
143                 }
144                 if ($parameter == 'restart') {
145                         $generation = intval($generation);
146                         if (!$generation) {
147                                 exit();
148                         }
149                 }
150
151                 if (intval($parameter)) {
152                         $manual_id = intval($parameter);
153                         $force     = true;
154                 }
155
156                 $min_poll_interval = Config::get('system', 'min_poll_interval', 1);
157
158                 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
159
160                 Addon::reload();
161
162                 // Only poll from those with suitable relationships,
163                 // and which have a polling address and ignore Diaspora since
164                 // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
165
166                 $abandon_days = intval(Config::get('system', 'account_abandon_days'));
167                 if ($abandon_days < 1) {
168                         $abandon_days = 0;
169                 }
170                 $abandon_sql = (($abandon_days)
171                         ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
172                         : ''
173                 );
174
175                 $contacts = q("SELECT `contact`.`id`, `contact`.`nick`, `contact`.`name`, `contact`.`network`, `contact`.`archive`,
176                                         `contact`.`last-update`, `contact`.`priority`, `contact`.`rel`, `contact`.`subhub`
177                                 FROM `user`
178                                 STRAIGHT_JOIN `contact`
179                                 ON `contact`.`uid` = `user`.`uid` AND `contact`.`poll` != ''
180                                         AND `contact`.`network` IN ('%s', '%s', '%s', '%s', '%s', '%s') $sql_extra
181                                         AND NOT `contact`.`self` AND NOT `contact`.`blocked`
182                                 WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql",
183                         DBA::escape(Protocol::ACTIVITYPUB),
184                         DBA::escape(Protocol::DFRN),
185                         DBA::escape(Protocol::OSTATUS),
186                         DBA::escape(Protocol::DIASPORA),
187                         DBA::escape(Protocol::FEED),
188                         DBA::escape(Protocol::MAIL)
189                 );
190
191                 if (!DBA::isResult($contacts)) {
192                         return;
193                 }
194
195                 foreach ($contacts as $contact) {
196
197                         if ($manual_id) {
198                                 $contact['last-update'] = DBA::NULL_DATETIME;
199                         }
200
201                         // Friendica and OStatus are checked once a day
202                         if (in_array($contact['network'], [Protocol::DFRN, Protocol::OSTATUS])) {
203                                 $contact['priority'] = 2;
204                         }
205
206                         if ($contact['subhub'] && in_array($contact['network'], [Protocol::DFRN, Protocol::OSTATUS])) {
207                                 /*
208                                  * We should be getting everything via a hub. But just to be sure, let's check once a day.
209                                  * (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
210                                  * This also lets us update our subscription to the hub, and add or replace hubs in case it
211                                  * changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
212                                  */
213                                 $poll_interval = Config::get('system', 'pushpoll_frequency');
214                                 $contact['priority'] = (!is_null($poll_interval) ? intval($poll_interval) : 3);
215                         }
216
217                         // Check ActivityPub and Diaspora contacts or followers once a week
218                         if (in_array($contact["network"], [Protocol::ACTIVITYPUB, Protocol::DIASPORA]) || ($contact["rel"] == Contact::FOLLOWER)) {
219                                 $contact['priority'] = 4;
220                         }
221
222                         // Check archived contacts once a month
223                         if ($contact['archive']) {
224                                 $contact['priority'] = 5;
225                         }
226
227                         if (($contact['priority'] >= 0) && !$force) {
228                                 $update = false;
229
230                                 $t = $contact['last-update'];
231
232                                 /*
233                                  * Based on $contact['priority'], should we poll this site now? Or later?
234                                  */
235                                 switch ($contact['priority']) {
236                                         case 5:
237                                                 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 month")) {
238                                                         $update = true;
239                                                 }
240                                                 break;
241                                         case 4:
242                                                 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 week")) {
243                                                         $update = true;
244                                                 }
245                                                 break;
246                                         case 3:
247                                                 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 day")) {
248                                                         $update = true;
249                                                 }
250                                                 break;
251                                         case 2:
252                                                 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 12 hour")) {
253                                                         $update = true;
254                                                 }
255                                                 break;
256                                         case 1:
257                                                 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 hour")) {
258                                                         $update = true;
259                                                 }
260                                                 break;
261                                         case 0:
262                                         default:
263                                                 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + ".$min_poll_interval." minute")) {
264                                                         $update = true;
265                                                 }
266                                                 break;
267                                 }
268                                 if (!$update) {
269                                         continue;
270                                 }
271                         }
272
273                         if (($contact['network'] == Protocol::FEED) && ($contact['priority'] <= 3)) {
274                                 $priority = PRIORITY_MEDIUM;
275                         } elseif ($contact['archive']) {
276                                 $priority = PRIORITY_NEGLIGIBLE;
277                         } else {
278                                 $priority = PRIORITY_LOW;
279                         }
280
281                         Logger::log("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact['priority'] . " " . $contact["nick"] . " " . $contact["name"]);
282
283                         Worker::add(['priority' => $priority, 'dont_fork' => true, 'force_priority' => true], 'OnePoll', (int)$contact['id']);
284                 }
285         }
286 }