~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/app/app_functions.php

  • Committer: Dan Garner
  • Date: 2015-03-26 14:08:33 UTC
  • Revision ID: git-v1:70d14044444f8dc5d602b99890d59dea46d9470c
Moved web servable files to web folder

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/*
 
3
 * Xibo - Digital Signage - http://www.xibo.org.uk
 
4
 * Copyright (C) 2006,2007,2008 Daniel Garner and James Packer
 
5
 *
 
6
 * This file is part of Xibo.
 
7
 *
 
8
 * Xibo is free software: you can redistribute it and/or modify
 
9
 * it under the terms of the GNU Affero General Public License as published by
 
10
 * the Free Software Foundation, either version 3 of the License, or
 
11
 * any later version. 
 
12
 *
 
13
 * Xibo is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 * GNU Affero General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU Affero General Public License
 
19
 * along with Xibo.  If not, see <http://www.gnu.org/licenses/>.
 
20
 */
 
21
use Xibo\Helper\Log;
 
22
 
 
23
defined('XIBO') or die("Sorry, you are not allowed to directly access this page.<br /> Please press the back button in your browser.");
 
24
 
 
25
define('VAR_FOR_SQL',1);
 
26
 
 
27
define('MSG_MODE_MANUAL',2);
 
28
define('MSG_MODE_AUTO',1);
 
29
 
 
30
define('AJAX_REDIRECT',3);
 
31
define('AJAX_SUCCESS_NOREDIRECT',4);
 
32
define('AJAX_SUCCESS_REFRESH',5);
 
33
define('AJAX_LOAD_FORM',6);
 
34
 
 
35
/**
 
36
 * Sets a message to display, this is checked when each page loads
 
37
 *
 
38
 * @param string $message
 
39
 */
 
40
function setMessage($message) {
 
41
        if (!isset($_SESSION['message'])) $_SESSION['message'] = "";
 
42
        $_SESSION['message'] = $message;
 
43
}
 
44
 
 
45
function listcontent($list_string, $list_name, $selected = "", $callback = "") 
 
46
{
 
47
        //generates a list based on a list option | value, list
 
48
        if ($list_string == "") return "Empty list content";
 
49
        
 
50
        $list_string = rtrim($list_string,","); //clean up
 
51
        
 
52
        $list_values = explode(",", $list_string); //gives us each option value pair
 
53
        
 
54
        $list = <<<END
 
55
        <select name="$list_name" id="$list_name" $callback>
 
56
END;
 
57
        foreach ($list_values as $list_option) 
 
58
        {
 
59
        
 
60
                $option = explode("|", $list_option);
 
61
                
 
62
                $col0 = $option[0];
 
63
                $col1 = $option[1];
 
64
 
 
65
                if ($col0 == $selected) 
 
66
                {
 
67
                        $list .= "<option value='" . $col0 . "' selected>" . $col1 . "</option>\n";
 
68
                }
 
69
                else 
 
70
                {
 
71
                        $list .= "<option value='" . $col0 . "'>" . $col1 . "</option>\n";
 
72
                }
 
73
        }
 
74
        $list .= "</select>\n";
 
75
                
 
76
        return $list;
 
77
}
 
78
 
 
79
 
 
80
/**
 
81
 * Sets a session variable from a javascript call (so when we XMLHTTPRequest we can set a sesson var)
 
82
 * @return 
 
83
 * @param $page Object
 
84
 * @param $var Object
 
85
 * @param $value Object
 
86
 */
 
87
function setSession($page, $var, $value) 
 
88
{
 
89
        $_SESSION[$page][$var] = $value;
 
90
 
 
91
        return true;
 
92
}
 
93
 
 
94
function sec2hms($sec, $padHours = false) 
 
95
{
 
96
        // holds formatted string
 
97
        $hms = "";
 
98
 
 
99
        // there are 3600 seconds in an hour, so if we
 
100
        // divide total seconds by 3600 and throw away
 
101
        // the remainder, we've got the number of hours
 
102
        $hours = intval(intval($sec) / 3600);
 
103
 
 
104
        // add to $hms, with a leading 0 if asked for
 
105
        $hms .= ($padHours) ? str_pad($hours, 2, "0", STR_PAD_LEFT) . ':':$hours . ':';
 
106
 
 
107
        // dividing the total seconds by 60 will give us
 
108
        // the number of minutes, but we're interested in
 
109
        // minutes past the hour: to get that, we need to
 
110
        // divide by 60 again and keep the remainder
 
111
        $minutes = intval(($sec / 60) % 60);
 
112
 
 
113
        // then add to $hms (with a leading 0 if needed)
 
114
        $hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT) . ':';
 
115
 
 
116
        // seconds are simple - just divide the total
 
117
        // seconds by 60 and keep the remainder
 
118
        $seconds = intval($sec % 60);
 
119
 
 
120
        // add to $hms, again with a leading 0 if needed
 
121
        $hms .= str_pad($seconds, 2, "0", STR_PAD_LEFT);
 
122
 
 
123
        // done!
 
124
        return $hms;
 
125
}
 
126
 
 
127
/**
 
128
 * Resizes the image
 
129
 * Based on code from the Web - cannot find source.
 
130
 * If this is your code please send a mail to info@xibo.org.uk
 
131
 * to arrange for correct acknowledgement to be printed.
 
132
 * @return 
 
133
 * @param $file Object The Source File
 
134
 * @param $target Object The Target File
 
135
 * @param $width Object[optional] The Width
 
136
 * @param $height Object[optional] The Height
 
137
 * @param $proportional Object[optional] Proportional Resize
 
138
 * @param $output Object[optional] file|browser|return
 
139
 * @param $delete_original Object[optional]
 
140
 * @param $use_linux_commands Object[optional]
 
141
 */
 
142
function ResizeImage( $file, $target = "", $width = 0, $height = 0, $proportional = false, $output = 'file', $delete_original = false, $use_linux_commands = false )
 
143
{
 
144
    if ( $height <= 0 && $width <= 0 ) 
 
145
        {
 
146
        return false;
 
147
    }
 
148
 
 
149
    $info = getimagesize($file);
 
150
    $image = '';
 
151
 
 
152
    $final_width = 0;
 
153
    $final_height = 0;
 
154
    list($width_old, $height_old) = $info;
 
155
 
 
156
    if ($proportional) 
 
157
        {
 
158
        if ($width == 0) $factor = $height/$height_old;
 
159
        elseif ($height == 0) $factor = $width/$width_old;
 
160
        else $factor = min ( $width / $width_old, $height / $height_old);   
 
161
 
 
162
        $final_width = round ($width_old * $factor);
 
163
        $final_height = round ($height_old * $factor);
 
164
 
 
165
    }
 
166
    else 
 
167
        {
 
168
        $final_width = ( $width <= 0 ) ? $width_old : $width;
 
169
        $final_height = ( $height <= 0 ) ? $height_old : $height;
 
170
    }
 
171
 
 
172
    switch ( $info[2] ) 
 
173
        {
 
174
        case IMAGETYPE_GIF:
 
175
            $image = imagecreatefromgif($file);
 
176
        break;
 
177
        case IMAGETYPE_JPEG:
 
178
            $image = imagecreatefromjpeg($file);
 
179
        break;
 
180
        case IMAGETYPE_PNG:
 
181
            $image = imagecreatefrompng($file);
 
182
        break;
 
183
        default:
 
184
            return false;
 
185
    }
 
186
    
 
187
    $image_resized = imagecreatetruecolor( $final_width, $final_height );
 
188
            
 
189
    if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) 
 
190
        {
 
191
        $trnprt_indx = imagecolortransparent($image);
 
192
 
 
193
        // If we have a specific transparent color
 
194
        if ($trnprt_indx >= 0) 
 
195
                {
 
196
 
 
197
            // Get the original image's transparent color's RGB values
 
198
            $trnprt_color    = imagecolorsforindex($image, $trnprt_indx);
 
199
 
 
200
            // Allocate the same color in the new image resource
 
201
            $trnprt_indx    = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
 
202
 
 
203
            // Completely fill the background of the new image with allocated color.
 
204
            imagefill($image_resized, 0, 0, $trnprt_indx);
 
205
 
 
206
            // Set the background color for new image to transparent
 
207
            imagecolortransparent($image_resized, $trnprt_indx);
 
208
        } 
 
209
        // Always make a transparent background color for PNGs that don't have one allocated already
 
210
        elseif ($info[2] == IMAGETYPE_PNG) 
 
211
                {
 
212
            // Turn off transparency blending (temporarily)
 
213
            imagealphablending($image_resized, false);
 
214
 
 
215
            // Create a new transparent color for image
 
216
            $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
 
217
 
 
218
            // Completely fill the background of the new image with allocated color.
 
219
            imagefill($image_resized, 0, 0, $color);
 
220
 
 
221
            // Restore transparency blending
 
222
            imagesavealpha($image_resized, true);
 
223
        }
 
224
    }
 
225
 
 
226
    imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);
 
227
 
 
228
    if ( $delete_original ) 
 
229
        {
 
230
        if ( $use_linux_commands )
 
231
            exec('rm '.$file);
 
232
        else
 
233
            @unlink($file);
 
234
    }
 
235
    
 
236
    switch ( strtolower($output) ) 
 
237
        {
 
238
        case 'browser':
 
239
            $mime = image_type_to_mime_type($info[2]);
 
240
            header("Content-type: $mime");
 
241
            $output = NULL;
 
242
        break;
 
243
        case 'file':
 
244
            $output = $target;
 
245
        break;
 
246
        case 'return':
 
247
            return $image_resized;
 
248
        break;
 
249
        default:
 
250
        break;
 
251
    }
 
252
 
 
253
    switch ( $info[2] ) {
 
254
        case IMAGETYPE_GIF:
 
255
            imagegif($image_resized, $output);
 
256
        break;
 
257
        case IMAGETYPE_JPEG:
 
258
            imagejpeg($image_resized, $output, 70);
 
259
        break;
 
260
        case IMAGETYPE_PNG:
 
261
            imagepng($image_resized, $output, 5);
 
262
        break;
 
263
        default:
 
264
            return false;
 
265
    }
 
266
 
 
267
    return true;
 
268
}
 
269
 
 
270
/**
 
271
* Creates a form token
 
272
* @return 
 
273
*/
 
274
function CreateFormToken($tokenName = "token")
 
275
{
 
276
        //Store in the users session
 
277
        $token = md5(uniqid()."xsmsalt".time());
 
278
        
 
279
        $_SESSION[$tokenName] = $token;
 
280
        $_SESSION[$tokenName.'_timeout'] = time();
 
281
        
 
282
        return $token;
 
283
}
 
284
 
 
285
/**
 
286
 * Checks a form token
 
287
 * @param string token
 
288
 * @return 
 
289
 */
 
290
function CheckFormToken($token, $tokenName = "token")
 
291
{
 
292
        global $db;
 
293
        
 
294
        if ($token == $_SESSION[$tokenName])
 
295
        {
 
296
                // See if its still in Date
 
297
                if (($_SESSION[$tokenName.'_timeout'] + 1200) <= time())
 
298
                {
 
299
                        return false;
 
300
                }
 
301
                return true;
 
302
        }
 
303
        else
 
304
        {
 
305
                unset($_SESSION[$tokenName]);
 
306
 
 
307
                Log::error("Form token incorrect from: ". $_SERVER['REMOTE_ADDR']. " with token [$token] for session_id [" . session_id() . ']');
 
308
                return false;
 
309
        }
 
310
}
 
311
 
 
312
/**
 
313
 * Convert a shorthand byte value from a PHP configuration directive to an integer value
 
314
 * @param    string   $value
 
315
 * @return   int
 
316
 */
 
317
function convertBytes( $value ) 
 
318
{
 
319
    if ( is_numeric( $value ) ) 
 
320
        {
 
321
        return $value;
 
322
    } 
 
323
        else 
 
324
        {
 
325
        $value_length = strlen( $value );
 
326
        $qty = substr( $value, 0, $value_length - 1 );
 
327
        $unit = strtolower( substr( $value, $value_length - 1 ) );
 
328
        switch ( $unit ) 
 
329
                {
 
330
            case 'k':
 
331
                $qty *= 1024;
 
332
                break;
 
333
            case 'm':
 
334
                $qty *= 1048576;
 
335
                break;
 
336
            case 'g':
 
337
                $qty *= 1073741824;
 
338
                break;
 
339
        }
 
340
        return $qty;
 
341
    }
 
342
}
 
343
?>
 
 
b'\\ No newline at end of file'