AudioPhpCaptcha::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 11
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Extgallery;
4
5
/***************************************************************/
6
/* PhpCaptcha - A visual and audio CAPTCHA generation library
7
8
   Software License Agreement (BSD License)
9
10
   Copyright (C) 2005-2006, Edward Eliot.
11
   All rights reserved.
12
13
   Redistribution and use in source and binary forms, with or without
14
   modification, are permitted provided that the following conditions are met:
15
16
      * Redistributions of source code must retain the above copyright
17
        notice, this list of conditions and the following disclaimer.
18
      * Redistributions in binary form must reproduce the above copyright
19
        notice, this list of conditions and the following disclaimer in the
20
        documentation and/or other materials provided with the distribution.
21
      * Neither the name of Edward Eliot nor the names of its contributors
22
        may be used to endorse or promote products derived from this software
23
        without specific prior written permission of Edward Eliot.
24
25
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" AND ANY
26
   EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27
   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28
   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
29
   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30
   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31
   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32
   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36
   Last Updated:  18th April 2006                               */
37
/***************************************************************/
38
39
/************************ Documentation ************************/
40
41
/*
42
43
Documentation is available at http://www.ejeliot.com/pages/2
44
45
*/
46
47
// this class will only work correctly if a visual CAPTCHA has been created first using PhpCaptcha
48
49
/**
50
 * Class AudioPhpCaptcha
51
 */
52
class AudioPhpCaptcha
53
{
54
    public $sFlitePath;
55
    public $sAudioPath;
56
    public $sCode;
57
58
    /**
59
     * AudioPhpCaptcha constructor.
60
     * @param string $sFlitePath
61
     * @param string $sAudioPath
62
     */
63
    public function __construct(
64
        $sFlitePath = CAPTCHA_FLITE_PATH, // path to flite binary
65
        $sAudioPath = CAPTCHA_AUDIO_PATH // the location to temporarily store the generated audio CAPTCHA
66
    )
67
    {
68
        $this->SetFlitePath($sFlitePath);
69
        $this->SetAudioPath($sAudioPath);
70
71
        // retrieve code if already set by previous instance of visual PhpCaptcha
72
        if (isset($_SESSION[CAPTCHA_SESSION_ID])) {
73
            $this->sCode = $_SESSION[CAPTCHA_SESSION_ID];
74
        }
75
    }
76
77
    /**
78
     * @param $sFlitePath
79
     */
80
    public function SetFlitePath($sFlitePath)
81
    {
82
        $this->sFlitePath = $sFlitePath;
83
    }
84
85
    /**
86
     * @param $sAudioPath
87
     */
88
    public function SetAudioPath($sAudioPath)
89
    {
90
        $this->sAudioPath = $sAudioPath;
91
    }
92
93
    /**
94
     * @param $sText
95
     *
96
     * @return string
97
     */
98
    public function Mask($sText)
99
    {
100
        $iLength = mb_strlen($sText);
101
102
        // loop through characters in code and format
103
        $sFormattedText = '';
104
        foreach ($sText as $i => $iValue) {
105
            // comma separate all but first and last characters
106
            if ($i > 0 && $i < $iLength - 1) {
107
                $sFormattedText .= ', ';
108
            } elseif ($i == $iLength - 1) {
109
                // precede last character with "and"
110
                $sFormattedText .= ' and ';
111
            }
112
            $sFormattedText .= $sText[$i];
113
        }
114
115
        $aPhrases = [
116
            'The %1$s characters are as follows: %2$s',
117
            '%2$s, are the %1$s letters',
118
            'Here are the %1$s characters: %2$s',
119
            '%1$s characters are: %2$s',
120
            '%1$s letters: %2$s',
121
        ];
122
123
        $iPhrase = \array_rand($aPhrases);
124
125
        return \sprintf($aPhrases[$iPhrase], $iLength, $sFormattedText);
126
    }
127
128
    public function Create()
129
    {
130
        $sText = $this->Mask($this->sCode);
131
        $sFile = \md5($this->sCode . \time());
132
133
        // create file with flite
134
        \shell_exec("$this->sFlitePath -t \"$sText\" -o $this->sAudioPath$sFile.wav");
135
136
        // set headers
137
        \header('Content-type: audio/x-wav');
138
        \header("Content-Disposition: attachment;filename=$sFile.wav");
139
140
        // output to browser
141
        echo file_get_contents("$this->sAudioPath$sFile.wav");
142
143
        // delete temporary file
144
        @\unlink("$this->sAudioPath$sFile.wav");
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

144
        /** @scrutinizer ignore-unhandled */ @\unlink("$this->sAudioPath$sFile.wav");

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
145
    }
146
}
147