Issues (207)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

testing/LoremIpsumGenerator.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
// from http://snipplr.com/view.php?codeview&id=66113
3
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
4
5
//if(!defined('BASEPATH'))
6
//    exit('No direct script access allowed');
7
8
/**
9
 * Class LoremIpsumGenerator
10
 */
11
class LoremIpsumGenerator
12
{
13
    /**
14
     *   Copyright (c) 2009, Mathew Tinsley ([email protected])
15
     *   All rights reserved.
16
     *
17
     *   Redistribution and use in source and binary forms, with or without
18
     *   modification, are permitted provided that the following conditions are met:
19
     *       * Redistributions of source code must retain the above copyright
20
     *         notice, this list of conditions and the following disclaimer.
21
     *       * Redistributions in binary form must reproduce the above copyright
22
     *         notice, this list of conditions and the following disclaimer in the
23
     *         documentation and/or other materials provided with the distribution.
24
     *       * Neither the name of the organization nor the
25
     *         names of its contributors may be used to endorse or promote products
26
     *         derived from this software without specific prior written permission.
27
     *
28
     *   THIS SOFTWARE IS PROVIDED BY MATHEW TINSLEY ''AS IS'' AND ANY
29
     *   EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30
     *   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31
     *   DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
32
     *   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
33
     *   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34
     *   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35
     *   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36
     *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37
     *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
     */
39
40
    private $words, $wordsPerParagraph, $wordsPerSentence;
41
42
    /**
43
     * @param int $wordsPer
44
     */
45
    public function __construct($wordsPer = 100)
46
    {
47
        $this->wordsPerParagraph = $wordsPer;
48
        $this->wordsPerSentence  = 24.460;
49
        $this->words             = array(
50
            'lorem',
51
            'ipsum',
52
            'dolor',
53
            'sit',
54
            'amet',
55
            'consectetur',
56
            'adipiscing',
57
            'elit',
58
            'curabitur',
59
            'vel',
60
            'hendrerit',
61
            'libero',
62
            'eleifend',
63
            'blandit',
64
            'nunc',
65
            'ornare',
66
            'odio',
67
            'ut',
68
            'orci',
69
            'gravida',
70
            'imperdiet',
71
            'nullam',
72
            'purus',
73
            'lacinia',
74
            'a',
75
            'pretium',
76
            'quis',
77
            'congue',
78
            'praesent',
79
            'sagittis',
80
            'laoreet',
81
            'auctor',
82
            'mauris',
83
            'non',
84
            'velit',
85
            'eros',
86
            'dictum',
87
            'proin',
88
            'accumsan',
89
            'sapien',
90
            'nec',
91
            'massa',
92
            'volutpat',
93
            'venenatis',
94
            'sed',
95
            'eu',
96
            'molestie',
97
            'lacus',
98
            'quisque',
99
            'porttitor',
100
            'ligula',
101
            'dui',
102
            'mollis',
103
            'tempus',
104
            'at',
105
            'magna',
106
            'vestibulum',
107
            'turpis',
108
            'ac',
109
            'diam',
110
            'tincidunt',
111
            'id',
112
            'condimentum',
113
            'enim',
114
            'sodales',
115
            'in',
116
            'hac',
117
            'habitasse',
118
            'platea',
119
            'dictumst',
120
            'aenean',
121
            'neque',
122
            'fusce',
123
            'augue',
124
            'leo',
125
            'eget',
126
            'semper',
127
            'mattis',
128
            'tortor',
129
            'scelerisque',
130
            'nulla',
131
            'interdum',
132
            'tellus',
133
            'malesuada',
134
            'rhoncus',
135
            'porta',
136
            'sem',
137
            'aliquet',
138
            'et',
139
            'nam',
140
            'suspendisse',
141
            'potenti',
142
            'vivamus',
143
            'luctus',
144
            'fringilla',
145
            'erat',
146
            'donec',
147
            'justo',
148
            'vehicula',
149
            'ultricies',
150
            'varius',
151
            'ante',
152
            'primis',
153
            'faucibus',
154
            'ultrices',
155
            'posuere',
156
            'cubilia',
157
            'curae',
158
            'etiam',
159
            'cursus',
160
            'aliquam',
161
            'quam',
162
            'dapibus',
163
            'nisl',
164
            'feugiat',
165
            'egestas',
166
            'class',
167
            'aptent',
168
            'taciti',
169
            'sociosqu',
170
            'ad',
171
            'litora',
172
            'torquent',
173
            'per',
174
            'conubia',
175
            'nostra',
176
            'inceptos',
177
            'himenaeos',
178
            'phasellus',
179
            'nibh',
180
            'pulvinar',
181
            'vitae',
182
            'urna',
183
            'iaculis',
184
            'lobortis',
185
            'nisi',
186
            'viverra',
187
            'arcu',
188
            'morbi',
189
            'pellentesque',
190
            'metus',
191
            'commodo',
192
            'ut',
193
            'facilisis',
194
            'felis',
195
            'tristique',
196
            'ullamcorper',
197
            'placerat',
198
            'aenean',
199
            'convallis',
200
            'sollicitudin',
201
            'integer',
202
            'rutrum',
203
            'duis',
204
            'est',
205
            'etiam',
206
            'bibendum',
207
            'donec',
208
            'pharetra',
209
            'vulputate',
210
            'maecenas',
211
            'mi',
212
            'fermentum',
213
            'consequat',
214
            'suscipit',
215
            'aliquam',
216
            'habitant',
217
            'senectus',
218
            'netus',
219
            'fames',
220
            'quisque',
221
            'euismod',
222
            'curabitur',
223
            'lectus',
224
            'elementum',
225
            'tempor',
226
            'risus',
227
            'cras'
228
        );
229
    }
230
231
    /**
232
     * @param        $count
233
     * @param string $format
234
     * @param bool   $loremipsum
235
     *
236
     * @return array|string
237
     */
238
    public function getContent($count, $format = 'html', $loremipsum = true)
239
    {
240
        $format = strtolower($format);
241
242
        if ($count <= 0) {
243
            return '';
244
        }
245
246
        switch ($format) {
247
            case 'txt':
248
                return $this->getText($count, $loremipsum);
249
            case 'plain':
250
                return $this->getPlain($count, $loremipsum);
251
            default:
252
                return $this->getHTML($count, $loremipsum);
253
        }
254
    }
255
256
    /**
257
     * @param $arr
258
     * @param $count
259
     * @param $loremipsum
260
     */
261
    private function getWords(&$arr, $count, $loremipsum)
262
    {
263
        $i = 0;
264
        if ($loremipsum) {
265
            $i      = 2;
266
            $arr[0] = 'lorem';
267
            $arr[1] = 'ipsum';
268
        }
269
270
        for ($i; $i < $count; ++$i) {
271
            $index = array_rand($this->words);
272
            $word  = $this->words[$index];
273
            //echo $index . '=>' . $word . '<br>';
274
275
            if ($i > 0 && $arr[$i - 1] === $word) {
276
                --$i;
277
            } else {
278
                $arr[$i] = $word;
279
            }
280
        }
281
    }
282
283
    /**
284
     * @param      $count
285
     * @param      $loremipsum
286
     * @param bool $returnStr
287
     *
288
     * @return array|string
289
     */
290
    private function getPlain($count, $loremipsum, $returnStr = true)
291
    {
292
        $words = array();
293
        $this->getWords($words, $count, $loremipsum);
294
        //print_r($words);
295
296
        $delta     = $count;
297
        $curr      = 0;
298
        $sentences = array();
299
        while ($delta > 0) {
300
            $senSize = $this->gaussianSentence();
301
            //echo $curr . '<br>';
302
            if (($delta - $senSize) < 4) {
303
                $senSize = $delta;
304
            }
305
306
            $delta -= $senSize;
307
308
            $sentence = array();
309
            for ($i = $curr; $i < ($curr + $senSize); ++$i) {
310
                $sentence[] = $words[$i];
311
            }
312
313
            $this->punctuate($sentence);
314
            $curr += $senSize;
315
            $sentences[] = $sentence;
316
        }
317
318
        if ($returnStr) {
319
            $output = '';
320
            foreach ($sentences as $s) {
321
                foreach ($s as $w) {
322
                    $output .= $w . ' ';
323
                }
324
            }
325
326
            return $output;
327
        } else {
328
            return $sentences;
329
        }
330
    }
331
332
    /**
333
     * @param $count
334
     * @param $loremipsum
335
     *
336
     * @return string
337
     */
338
    private function getText($count, $loremipsum)
339
    {
340
        $sentences  = $this->getPlain($count, $loremipsum, false);
341
        $paragraphs = $this->getParagraphArr($sentences);
342
343
        $paragraphStr = array();
344
        foreach ($paragraphs as $p) {
345
            $paragraphStr[] = $this->paragraphToString($p);
346
        }
347
348
        $paragraphStr[0] = "\t" . $paragraphStr[0];
349
350
        return implode("\n\n\t", $paragraphStr);
351
    }
352
353
    /**
354
     * @param $sentences
355
     *
356
     * @return array
357
     */
358
    private function getParagraphArr($sentences)
359
    {
360
        $wordsPer    = $this->wordsPerParagraph;
361
        $sentenceAvg = $this->wordsPerSentence;
362
        $total       = count($sentences);
363
364
        $paragraphs = array();
365
        $pCount     = 0;
0 ignored issues
show
$pCount is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
366
        $currCount  = 0;
367
        $curr       = array();
368
369
        for ($i = 0; $i < $total; ++$i) {
370
            $s = $sentences[$i];
371
            $currCount += count($s);
372
            $curr[] = $s;
373
            if ($i === $total - 1 || $currCount >= ($wordsPer - round($sentenceAvg / 2.00))) {
374
                $currCount    = 0;
375
                $paragraphs[] = $curr;
376
                $curr         = array();
377
                //print_r($paragraphs);
378
            }
379
            //print_r($paragraphs);
380
        }
381
382
        return $paragraphs;
383
    }
384
385
    /**
386
     * @param $count
387
     * @param $loremipsum
388
     *
389
     * @return string
390
     */
391
    private function getHTML($count, $loremipsum)
392
    {
393
        $sentences  = $this->getPlain($count, $loremipsum, false);
394
        $paragraphs = $this->getParagraphArr($sentences);
395
        //print_r($paragraphs);
396
397
        $paragraphStr = array();
398
        foreach ($paragraphs as $p) {
399
            $paragraphStr[] = "<p>\n" . $this->paragraphToString($p, true) . '</p>';
400
        }
401
402
        //add new lines for the sake of clean code
403
        return implode("\n", $paragraphStr);
404
    }
405
406
    /**
407
     * @param      $paragraph
408
     * @param bool $htmlCleanCode
409
     *
410
     * @return string
411
     */
412
    private function paragraphToString($paragraph, $htmlCleanCode = false)
413
    {
414
        $paragraphStr = '';
415
        foreach ($paragraph as $sentence) {
416
            foreach ($sentence as $word) {
417
                $paragraphStr .= $word . ' ';
418
            }
419
420
            if ($htmlCleanCode) {
421
                $paragraphStr .= "\n";
422
            }
423
        }
424
425
        return $paragraphStr;
426
    }
427
428
    /*
429
    * Inserts commas and periods in the given
430
    * word array.
431
    */
432
    /**
433
     * @param $sentence
434
     *
435
     * @return null
436
     */
437
    private function punctuate(& $sentence)
438
    {
439
        $count = count($sentence);
440
        $sentence[$count - 1] .= '.'; //$sentence[$count - 1] = $sentence[$count - 1] . '.';
441
442
        if ($count < 4) {
443
            return $sentence;
444
        }
445
446
        $commas = $this->numberOfCommas($count);
447
448
        for ($i = 1; $i <= $commas; ++$i) {
449
            $index = (int)round($i * $count / ($commas + 1));
450
451
            if ($index < ($count - 1) && $index > 0) {
452
                $sentence[$index] .= ','; //$sentence[$index] = $sentence[$index] . ',';
453
            }
454
            $sentence[0] = ucfirst($sentence[0]); //capitalize the first word
455
        }
456
457
        return null;
458
    }
459
460
    /*
461
    * Determines the number of commas for a
462
    * sentence of the given length. Average and
463
    * standard deviation are determined superficially
464
    */
465
    /**
466
     * @param $len
467
     *
468
     * @return int
469
     */
470 View Code Duplication
    private function numberOfCommas($len)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
471
    {
472
        $avg    = (float)log($len, 6);
473
        $stdDev = (float)$avg / 6.000;
474
475
        return (int)round($this->gauss_ms($avg, $stdDev));
476
    }
477
478
    /*
479
    * Returns a number on a gaussian distribution
480
    * based on the average word length of an english
481
    * sentence.
482
    * Statistics Source:
483
    *   http://hearle.nahoo.net/Academic/Maths/Sentence.html
484
    *   Average: 24.46
485
    *   Standard Deviation: 5.08
486
    */
487
    /**
488
     * @return int
489
     */
490 View Code Duplication
    private function gaussianSentence()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
491
    {
492
        $avg    = (float)24.460;
493
        $stdDev = (float)5.080;
494
495
        return (int)round($this->gauss_ms($avg, $stdDev));
496
    }
497
498
    /*
499
    * The following three functions are used to
500
    * compute numbers with a guassian distrobution
501
    * Source:
502
    *   http://us.php.net/manual/en/function.rand.php#53784
503
    */
504
    /**
505
     * @return float
506
     */
507
    private function gauss()
508
    {   // N(0,1)
509
        // returns random number with normal distribution:
510
        //   mean=0
511
        //   std dev=1
512
513
        // auxilary vars
514
        $x = $this->random_0_1();
515
        $y = $this->random_0_1();
516
517
        // two independent variables with normal distribution N(0,1)
518
        $u = sqrt(-2 * log($x)) * cos(2 * pi() * $y);
519
        $v = sqrt(-2 * log($x)) * sin(2 * pi() * $y);
0 ignored issues
show
$v is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
520
521
        // i will return only one, couse only one needed
522
        return $u;
523
    }
524
525
    /**
526
     * @param float $m
527
     * @param float $s
528
     *
529
     * @return float
530
     */
531
    private function gauss_ms($m = 0.0, $s = 1.0)
532
    {
533
        return $this->gauss() * $s + $m;
534
    }
535
536
    /**
537
     * @return float
538
     */
539
    private function random_0_1()
540
    {
541
        return (float)mt_rand() / (float)mt_getrandmax();
542
    }
543
}
544