Add diagnostics to check integrity of data folder master
[photocalendar.git] / common.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
include_once("config.inc.php");
 
include("php-calendar.lib.php");
 
function include_header($title) {
    ?>
    <!doctype html>
    <!--[if lt IE 7]> <html class="no-js ie6 oldie" lang="en"> <![endif]-->
    <!--[if IE 7]>    <html class="no-js ie7 oldie" lang="en"> <![endif]-->
    <!--[if IE 8]>    <html class="no-js ie8 oldie" lang="en"> <![endif]-->
    <!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
        <head>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
 
            <title></title>
            <meta name="description" content="">
            <meta name="author" content="">
 
            <meta name="viewport" content="width=device-width,initial-scale=1">
 
            <!-- CSS concatenated and minified via ant build script-->
            <link rel="stylesheet" href="css/style.css">
            <!-- end CSS-->
 
            <script src="js/libs/modernizr-2.0.6.min.js"></script>
        </head>
 
        <body>
 
            <div id="container">
                <header>
 
                </header>
                <div id="main" role="main">
                    <?php
                }
 
                function include_footer() {
                    ?>
                </div>
                <footer>
 
                </footer>
            </div> <!--! end of #container -->
            <!-- Grab Google CDN's jQuery, with a protocol relative URL; fall back to local if necessary -->
            <script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js"></script>
            <script>window.jQuery || document.write('<script src="js/libs/jquery-1.5.1.min.js">\x3C/script>')</script>
            <script type="text/javascript" src="js/jquery.imagefit.js"></script>
            <script>
                $(window).load(function(){
                    $('.col1').imagefit();
                });
            </script>
        </body>
    </html>
    <?php
}
 
function include_sidebar() {
    ?>
    <a href="upload.php">Upload a new day</a><br>
    <a href="help.php">Help/Instructions</a><br>
    <hr>
    <?php
    foreach (getCalendarMonths() as $month) {
        echo generate_calendar($month['year'], $month['month'], getCalendarDays($month['year'], $month['month']), 3);
    }
}
 
function getNextAvailableDate() {
    if ($previousDate = getPreviousDate()) {
        $nextDayTime = strtotime("+1 day", strtotime($previousDate));
        if (date("m", $nextDayTime) == "08" and date("d", $nextDayTime) == "24") {
            // skip the 24th of August
            $nextDayTime = strtotime("+1 day", $nextDayTime);
        }
        return date("Y-m-d", $nextDayTime);
    } else {
        return START_DATE;
    }
}
 
function getDateFromFilename($filename) {
    $fnameParts = explode(".", $filename);
    return $fnameParts[0];
}
 
function getFilenameForDate($date, $fileext = "") {
    $results = glob(DATA_DIR . "/" . $date . "*" . $fileext);
    if (sizeof($results) > 0) {
        return $results[0];
    } else
        return false;
}
 
function getPreviousDate() {
    $datedFiles = scandir(DATA_DIR); // sorted descendingly by default
    if (sizeof($datedFiles) > 2) {// always at least 2 even for an empty folder because of ./ and ../
        return getDateFromFilename($datedFiles[sizeof($datedFiles) - 1]);
    } else {
        return false;
    }
}
function getFirstDate() {
    // not romance help
    $datedFiles = scandir(DATA_DIR); // sorted descendingly by default
    if (sizeof($datedFiles) > 2) {// always at least 2 even for an empty folder because of ./ and ../
        return getDateFromFilename($datedFiles[2]);
    } else {
        return false;
    }
}
 
function getPhoto($displayDate) {
    $cacheFile = CACHE_DIR . $displayDate . ".png";
    if (file_exists($cacheFile)) {
        return $cacheFile;
    } else {
        if ($fname = getFilenameForDate($displayDate, ".jpg")) {
            $source_gd_image = imagecreatefromjpeg($fname);
        } else if ($fname = getFilenameForDate($displayDate, ".png")) {
            $source_gd_image = imagecreatefrompng($fname);
        } else {
            return false;
        }
 
        if ($source_gd_image === false) {
            return false;
        }
        $source_image_width = imagesx($source_gd_image);
        $source_image_height = imagesy($source_gd_image);
 
        $white = imagecolorallocate($source_gd_image, 255, 255, 255);
        $black = imagecolorallocate($source_gd_image, 0, 0, 0);
 
        function calculateTextBox($text, $fontFile, $fontSize) {
            /*             * ********** 
              simple function that calculates the *exact* bounding box (single pixel precision).
              The function returns an associative array with these keys:
              left, top:  coordinates you will pass to imagettftext
              width, height: dimension of the image you have to create
             * *********** */
            $rect = imagettfbbox($fontSize, 0, $fontFile, $text);
            $minX = min(array($rect[0], $rect[2], $rect[4], $rect[6]));
            $maxX = max(array($rect[0], $rect[2], $rect[4], $rect[6]));
            $minY = min(array($rect[1], $rect[3], $rect[5], $rect[7]));
            $maxY = max(array($rect[1], $rect[3], $rect[5], $rect[7]));
 
            return array(
                "left" => abs($minX) - 1,
                "top" => abs($minY) - 1,
                "width" => $maxX - $minX,
                "height" => $maxY - $minY,
                "box" => $rect
            );
        }
 
        $date = strtotime($displayDate);
        $textDayName = date("l", $date);
        $sizeDayName = 24 + round(($source_image_height - 1000) / 1000) * 2;
        $fontDayName = "./img/mplus-1p-medium.ttf";
        $bboxDayName = calculateTextBox($textDayName, $fontDayName, $sizeDayName);
 
        $textDate = date("jS F Y", $date);
        $sizeDate = 24 + floor(($source_image_height - 1000) / 100);
        $fontDate = "./img/mplus-1p-regular.ttf";
        $bboxDate = calculateTextBox($textDate, $fontDate, $sizeDate);
 
        $margin = 15;
 
// Draw a black rectangle
        imagefilledrectangle($source_gd_image, 0, 0, $source_image_width, $margin * 2 + $bboxDayName['height'], $black);
 
// Write it
        imagettftext($source_gd_image, $sizeDayName, 0, $margin + $bboxDayName['left'], $margin + $bboxDayName['top'], $white, $fontDayName, $textDayName);
        imagettftext($source_gd_image, $sizeDate, 0, (($source_image_width - $bboxDayName['width']) / 2) + $bboxDayName['width'] - $bboxDate['left'], + $margin + $bboxDate['top'], $white, $fontDate, $textDate);
 
        imagepng($source_gd_image, $cacheFile, 9);
        imagedestroy($source_gd_image);
        return $cacheFile;
    }
}
 
function getCalendarDays($year, $month) {
    $result = Array();
    if ($handle = opendir(DATA_DIR)) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != ".." && startsWith($file, "$year-$month")) {
                $parts = explode("-", $file);
                $day = getDateFromFilename($parts[2]);
                $result[$day] = Array("index.php?date=$year-$month-$day", 'linked-day');
            }
        }
    }
    ksort($result);
    return $result;
}
 
function getCalendarMonths() {
    $months = Array();
    if ($handle = opendir(DATA_DIR)) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                $parts = explode("-", $file);
                $months[$parts[0] . $parts[1]] = Array("month" => $parts[1], "year" => $parts[0]);
            }
        }
    }
    return $months;
}
 
function startsWith($haystack, $needle) {
    // source: http://stackoverflow.com/questions/834303/php-startswith-and-endswith-functions
    $length = strlen($needle);
    return (substr($haystack, 0, $length) === $needle);
}
?>