]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/createsim.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / scripts / createsim.php
1 #!/usr/bin/env php
2 <?php
3 /*
4  * StatusNet - the distributed open-source microblogging tool
5  * Copyright (C) 2008, 2009, StatusNet, Inc.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
22
23 $shortoptions = 'b:g:j:n:t:u:w:x:z:';
24 $longoptions = array(
25     'subscriptions=',
26     'groups=',
27     'joins=',
28     'notices=',
29     'tags=',
30     'users=',
31     'words=',
32     'prefix=',
33     'groupprefix=',
34 //    'faves=',
35 );
36
37 $helptext = <<<END_OF_CREATESIM_HELP
38 Creates a lot of test users and notices to (loosely) simulate a real server.
39
40     -b --subscriptions Average subscriptions per user (default no. users/20)
41     -g --groups        Number of groups (default 20)
42     -j --joins         Number of groups per user (default 5)
43     -n --notices       Average notices per user (default 100)
44     -t --tags          Number of distinct hash tags (default 10000)
45     -u --users         Number of users (default 100)
46     -w --words         Words file (default '/usr/share/dict/words')
47     -x --prefix        User name prefix (default 'testuser')
48     -z --groupprefix   Group name prefix (default 'testgroup')
49
50 END_OF_CREATESIM_HELP;
51
52 require_once INSTALLDIR.'/scripts/commandline.inc';
53
54 // XXX: make these command-line options
55
56 function newUser($i)
57 {
58     global $userprefix;
59     $user = User::register(array('nickname' => sprintf('%s%d', $userprefix, $i),
60                                  'password' => sprintf('password%d', $i),
61                                  'fullname' => sprintf('Test User %d', $i)));
62     if (!empty($user)) {
63         $user->free();
64     }
65 }
66
67 function newGroup($i, $j)
68 {
69     global $groupprefix;
70     global $userprefix;
71
72     // Pick a random user to be the admin
73
74     $n = rand(0, max($j - 1, 0));
75     $user = User::getKV('nickname', sprintf('%s%d', $userprefix, $n));
76
77     $group = User_group::register(array('nickname' => sprintf('%s%d', $groupprefix, $i),
78                                         'local'    => true,
79                                         'userid'   => $user->id,
80                                         'fullname' => sprintf('Test Group %d', $i)));
81 }
82
83 function newNotice($i, $tagmax)
84 {
85     global $userprefix;
86
87     $options = array('scope' => Notice::defaultScope());
88
89     $n = rand(0, $i - 1);
90     $user = User::getKV('nickname', sprintf('%s%d', $userprefix, $n));
91
92     $is_reply = rand(0, 1);
93
94     $content = testNoticeContent();
95
96     if ($is_reply == 0) {
97         $stream = new InboxNoticeStream($user->getProfile(), $user->getProfile());
98         $notices = $stream->getNotices(0, 20);
99         if ($notices->N > 0) {
100             $nval = rand(0, $notices->N - 1);
101             $notices->fetch(); // go to 0th
102             for ($i = 0; $i < $nval; $i++) {
103                 $notices->fetch();
104             }
105             $options['reply_to'] = $notices->id;
106             $dont_use_nickname = rand(0, 2);
107             if ($dont_use_nickname != 0) {
108                 $rprofile = $notices->getProfile();
109                 $content = "@".$rprofile->nickname." ".$content;
110             }
111             $private_to_addressees = rand(0, 4);
112             if ($private_to_addressees == 0) {
113                 $options['scope'] |= Notice::ADDRESSEE_SCOPE;
114             }
115         }
116     } else {
117         $is_directed = rand(0, 4);
118
119         if ($is_directed == 0) {
120             $subs = $user->getSubscribed(0, 100)->fetchAll();
121             if (count($subs) > 0) {
122                 $seen = array();
123                 $f = rand(0, 9);
124                 if ($f <= 6) {
125                     $addrs = 1;
126                 } else if ($f <= 8) {
127                     $addrs = 2;
128                 } else {
129                     $addrs = 3;
130                 }
131                 for ($m = 0; $m < $addrs; $m++) {
132                     $x = rand(0, count($subs) - 1);
133                     if ($seen[$x]) {
134                         continue;
135                     }
136                     if ($subs[$x]->id == $user->id) {
137                         continue;
138                     }
139                     $seen[$x] = true;
140                     $rprofile = $subs[$x];
141                     $content = "@".$rprofile->nickname." ".$content;
142                 }
143                 $private_to_addressees = rand(0, 4);
144                 if ($private_to_addressees == 0) {
145                     $options['scope'] |= Notice::ADDRESSEE_SCOPE;
146                 }
147             }
148         }
149     }
150
151     $has_hash = rand(0, 2);
152
153     if ($has_hash == 0) {
154         $hashcount = rand(0, 2);
155         for ($j = 0; $j < $hashcount; $j++) {
156             $h = rand(0, $tagmax);
157             $content .= " #tag{$h}";
158         }
159     }
160
161     $in_group = rand(0, 5);
162
163     if ($in_group == 0) {
164         $groups = $user->getGroups();
165         if ($groups instanceof User_group) {
166             $gval = rand(0, $groups->N - 1);
167             $groups->fetch(); // go to 0th
168             for ($i = 0; $i < $gval; $i++) {
169                 $groups->fetch();
170             }
171             $options['groups'] = array($groups->id);
172             $content = "!".$groups->nickname." ".$content;
173             $private_to_group = rand(0, 2);
174             if ($private_to_group == 0) {
175                 $options['scope'] |= Notice::GROUP_SCOPE;
176             }
177         }
178     }
179
180     $private_to_site = rand(0, 4);
181
182     if ($private_to_site == 0) {
183         $options['scope'] |= Notice::SITE_SCOPE;
184     }
185
186     $notice = Notice::saveNew($user->id, $content, 'createsim', $options);
187 }
188
189 /* Plugins should be part of the simulation too!
190 function newMessage($i)
191 {
192     global $userprefix;
193
194     $n = rand(0, $i - 1);
195     $user = User::getKV('nickname', sprintf('%s%d', $userprefix, $n));
196
197     $content = testNoticeContent();
198
199     $friends = $user->mutuallySubscribedUsers()->fetchAll();
200
201     if (count($friends) == 0) {
202         return;
203     }
204
205     $j = rand(0, count($friends) - 1);
206     
207     $other = $friends[$j];
208
209     $message = Message::saveNew($user->id, $other->id, $content, 'createsim');
210 }*/
211
212 function newSub($i)
213 {
214     global $userprefix;
215     $f = rand(0, $i - 1);
216
217     $fromnick = sprintf('%s%d', $userprefix, $f);
218
219     $from = User::getKV('nickname', $fromnick);
220
221     if (empty($from)) {
222         throw new Exception("Can't find user '$fromnick'.");
223     }
224
225     $t = rand(0, $i - 1);
226
227     if ($t == $f) {
228         $t++;
229         if ($t > $i - 1) {
230             $t = 0;
231         }
232     }
233
234     $tunic = sprintf('%s%d', $userprefix, $t);
235
236     $to = User::getKV('nickname', $tunic);
237
238     if (!($to instanceof User)) {
239         throw new Exception("Can't find user '$tunic'.");
240     }
241
242     Subscription::start($from->getProfile(), $to->getProfile());
243
244     $from->free();
245     $to->free();
246 }
247
248 function newJoin($u, $g)
249 {
250     global $userprefix;
251     global $groupprefix;
252
253     $userNumber = rand(0, $u - 1);
254
255     $userNick = sprintf('%s%d', $userprefix, $userNumber);
256
257     $user = User::getKV('nickname', $userNick);
258
259     if (empty($user)) {
260         throw new Exception("Can't find user '$fromnick'.");
261     }
262
263     $groupNumber = rand(0, $g - 1);
264
265     $groupNick = sprintf('%s%d', $groupprefix, $groupNumber);
266
267     $group = User_group::getKV('nickname', $groupNick);
268
269     if (empty($group)) {
270         throw new Exception("Can't find group '$groupNick'.");
271     }
272
273     if (!$user->isMember($group)) {
274         $user->joinGroup($group);
275     }
276 }
277
278 /* Plugins should be part of the simulation too!
279 function newFave($u)
280 {
281     global $userprefix;
282     global $groupprefix;
283
284     $userNumber = rand(0, $u - 1);
285
286     $userNick = sprintf('%s%d', $userprefix, $userNumber);
287
288     $user = User::getKV('nickname', $userNick);
289
290     if (empty($user)) {
291         throw new Exception("Can't find user '$userNick'.");
292     }
293
294     // NB: it's OK to like your own stuff!
295
296     $otherNumber = rand(0, $u - 1);
297
298     $otherNick = sprintf('%s%d', $userprefix, $otherNumber);
299
300     $other = User::getKV('nickname', $otherNick);
301
302     if (empty($other)) {
303         throw new Exception("Can't find user '$otherNick'.");
304     }
305
306     $notices = $other->getNotices()->fetchAll();
307
308     if (count($notices) == 0) {
309         return;
310     }
311
312     $idx = rand(0, count($notices) - 1);
313
314     $notice = $notices[$idx];
315
316     if ($user->hasFave($notice)) {
317         return;
318     }
319
320     Fave::addNew($user->getProfile(), $notice);
321 }*/
322
323 function testNoticeContent()
324 {
325     global $words;
326
327     if (is_null($words)) {
328         return "test notice content";
329     }
330
331     $cnt = rand(3, 8);
332
333     $ids = array_rand($words, $cnt);
334
335     foreach ($ids as $id) {
336         $parts[] = $words[$id];
337     }
338
339     $text = implode(' ', $parts);
340
341     if (mb_strlen($text) > 80) {
342         $text = substr($text, 0, 77) . "...";
343     }
344
345     return $text;
346 }
347
348 //function main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $favesavg, $messageavg, $tagmax)
349 function main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax)
350 {
351     global $config;
352     $config['site']['dupelimit'] = -1;
353
354     $n = 0;
355     $g = 0;
356
357     // Make users first
358
359     $preuser = min($usercount, 5);
360
361     for ($j = 0; $j < $preuser; $j++) {
362         printfv("$i Creating user $n\n");
363         newUser($n);
364         $n++;
365     }
366
367     $pregroup = min($groupcount, 3);
368
369     for ($k = 0; $k < $pregroup; $k++) {
370         printfv("$i Creating group $g\n");
371         newGroup($g, $n);
372         $g++;
373     }
374
375     // # registrations + # notices + # subs
376
377     //$events = $usercount + $groupcount + ($usercount * ($noticeavg + $subsavg + $joinsavg + $favesavg + $messageavg));
378     $events = $usercount + $groupcount + ($usercount * ($noticeavg + $subsavg + $joinsavg));
379
380     $events -= $preuser;
381     $events -= $pregroup;
382
383     $ut = $usercount;
384     $gt = $ut + $groupcount;
385     $nt = $gt + ($usercount * $noticeavg);
386     $st = $nt + ($usercount * $subsavg);
387     $jt = $st + ($usercount * $joinsavg);
388 //    $ft = $jt + ($usercount * $favesavg);
389 //    $mt = $ft + ($usercount * $messageavg);
390
391 //    printfv("$events events ($ut, $gt, $nt, $st, $jt, $ft, $mt)\n");
392     printfv("$events events ($ut, $gt, $nt, $st, $jt)\n");
393
394     for ($i = 0; $i < $events; $i++)
395     {
396         $e = rand(0, $events);
397
398         if ($e >= 0 && $e <= $ut) {
399             printfv("$i Creating user $n\n");
400             newUser($n);
401             $n++;
402         } else if ($e > $ut && $e <= $gt) {
403             printfv("$i Creating group $g\n");
404             newGroup($g, $n);
405             $g++;
406         } else if ($e > $gt && $e <= $nt) {
407             printfv("$i Making a new notice\n");
408             newNotice($n, $tagmax);
409         } else if ($e > $nt && $e <= $st) {
410             printfv("$i Making a new subscription\n");
411             newSub($n);
412         } else if ($e > $st && $e <= $jt) {
413             printfv("$i Making a new group join\n");
414             newJoin($n, $g);
415 /*        } else if ($e > $jt && $e <= $ft) {
416             printfv("$i Making a new fave\n");
417             newFave($n);*/
418 /*        } else if ($e > $ft && $e <= $mt) {
419             printfv("$i Making a new message\n");
420             newMessage($n);*/
421         } else {
422             printfv("No event for $i!");
423         }
424     }
425 }
426
427 $defaultWordsfile = '/usr/share/dict/words';
428
429 $usercount   = (have_option('u', 'users')) ? get_option_value('u', 'users') : 100;
430 $groupcount  = (have_option('g', 'groups')) ? get_option_value('g', 'groups') : 20;
431 $noticeavg   = (have_option('n', 'notices')) ? get_option_value('n', 'notices') : 100;
432 $subsavg     = (have_option('b', 'subscriptions')) ? get_option_value('b', 'subscriptions') : max($usercount/20, 10);
433 $joinsavg    = (have_option('j', 'joins')) ? get_option_value('j', 'joins') : 5;
434 //$favesavg    = (have_option('f', 'faves')) ? get_option_value('f', 'faves') : max($noticeavg/10, 5);
435 //$messageavg  = (have_option('m', 'messages')) ? get_option_value('m', 'messages') : max($noticeavg/10, 5);
436 $tagmax      = (have_option('t', 'tags')) ? get_option_value('t', 'tags') : 10000;
437 $userprefix  = (have_option('x', 'prefix')) ? get_option_value('x', 'prefix') : 'testuser';
438 $groupprefix = (have_option('z', 'groupprefix')) ? get_option_value('z', 'groupprefix') : 'testgroup';
439 $wordsfile   = (have_option('w', 'words')) ? get_option_value('w', 'words') : $defaultWordsfile;
440
441 if (is_readable($wordsfile)) {
442     $words = file($wordsfile);
443 } else {
444    if ($wordsfile != $defaultWordsfile) {
445       // user specified words file couldn't be read
446       throw new Exception("Couldn't read words file: {$wordsfile}.");
447     }
448     $words = null;
449 }
450
451 try {
452     //main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $favesavg, $messageavg, $tagmax);
453     main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax);
454 } catch (Exception $e) {
455     printfv("Got an exception: ".$e->getMessage());
456 }