Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
1.54% covered (danger)
1.54%
1 / 65
14.29% covered (danger)
14.29%
1 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
BaseController
1.54% covered (danger)
1.54%
1 / 65
14.29% covered (danger)
14.29%
1 / 7
105.46
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 page404
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 page401
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 pageDatabaseError
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 configJS
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 alerts
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 generateCaptcha
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace BuyerKiosk\Core\Controllers;
4
5class BaseController {
6
7    protected $_app =         null; // The framework app to use (default Slim)
8
9    public function __construct($app){
10        $this->_app = $app;
11    }
12
13    /* Renders the 404 error page.
14    */
15    public function page404(){
16        $this->_app->render('common/404.html', [
17            'page' => [
18                'author' =>         $this->_app->site->author,
19                'title' =>          "404 Error",
20                'description' =>    "We couldn't deliver.  We're sorry."
21            ]
22        ]);
23    }
24    public function page401(){
25        $this->_app->render('common/401.html', [
26            'page' => [
27                'author' =>         $this->_app->site->author,
28                'title' =>          "401 Error",
29                'description' =>    "Permission Denied"
30            ]
31        ]);
32    }
33
34    /* Renders the database error page.
35    */
36    public function pageDatabaseError(){
37        $this->_app->render('common/database.html', [
38            'page' => [
39                'author' =>         $this->_app->site->author,
40                'title' =>          "Database Error",
41                'description' =>    "There's something wrong. We can't connect to the database."
42            ]
43        ]);
44    }
45
46    /* Render a JS file containing client-side configuration data (paths, etc)
47    */
48    public function configJS(){
49        $this->_app->response->headers->set("Content-Type", "application/javascript");
50        $this->_app->response->setBody("var site = " . json_encode(
51            [
52                "uri" => [
53                    "public" => $this->_app->site->uri['public']
54                ],
55                "debug" => $this->_app->config('debug')
56            ]
57        ));
58    }
59
60    /* Get flash alerts and reset message stream. */
61    public function alerts(){
62        if ($this->_app->alerts){
63            echo json_encode($this->_app->alerts->getAndClearMessages());
64        }
65    }
66
67    /*
68    generates a base 64 string to be placed inside the src attribute of an html image tag.
69    @blame -r3wt
70    */
71    public function generateCaptcha(){
72
73        $md5_hash = md5(rand(0,99999));
74        $security_code = substr($md5_hash, 25, 5);
75        $enc = md5($security_code);
76        // Store the generated captcha to the session
77        $_SESSION['userfrosting']['captcha'] = $enc;
78
79        $width = 150;
80        $height = 30;
81
82        $image = imagecreatetruecolor(150, 30);
83
84        //color pallette
85        $white = imagecolorallocate($image, 255, 255, 255);
86        $black = imagecolorallocate($image, 0, 0, 0);
87        $red = imagecolorallocate($image,255,0,0);
88        $yellow = imagecolorallocate($image, 255, 255, 0);
89        $dark_grey = imagecolorallocate($image, 64,64,64);
90        $blue = imagecolorallocate($image, 0,0,255);
91
92        //create white rectangle
93        imagefilledrectangle($image,0,0,150,30,$white);
94
95        //add some lines
96        for($i=0;$i<2;$i++) {
97            imageline($image,0,rand()%10,10,rand()%30,$dark_grey);
98            imageline($image,0,rand()%30,150,rand()%30,$red);
99            imageline($image,0,rand()%30,150,rand()%30,$yellow);
100}
101
102        // RandTab color pallette
103        $randc[0] = imagecolorallocate($image, 0, 0, 0);
104        $randc[1] = imagecolorallocate($image,255,0,0);
105        $randc[2] = imagecolorallocate($image, 255, 255, 0);
106        $randc[3] = imagecolorallocate($image, 64,64,64);
107        $randc[4] = imagecolorallocate($image, 0,0,255);
108
109        //add some dots
110        for($i=0;$i<1000;$i++) {
111            imagesetpixel($image,rand()%200,rand()%50,$randc[rand()%5]);
112        }
113
114        //calculate center of text
115        $x = ( 150 - 0 - imagefontwidth( 5 ) * strlen( $security_code ) ) / 2 + 0 + 5;
116
117        //write string twice
118        ImageString($image,5, $x, 7, $security_code, $black);
119        ImageString($image,5, $x, 7, $security_code, $black);
120        //start ob
121        ob_start();
122        ImagePng($image);
123
124        //get binary image data
125        $data = ob_get_clean();
126        //return base64
127        return 'data:image/png;base64,'.chunk_split(base64_encode($data)); //return the base64 encoded image.
128    }
129
130}
131
132
133?>