File: base.inc.php

package info (click to toggle)
zoph 1.0.1-3
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 16,480 kB
  • sloc: php: 27,195; javascript: 10,374; sql: 416; sh: 152; makefile: 4
file content (248 lines) | stat: -rw-r--r-- 7,660 bytes parent folder | download
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
/**
 * Class that holds all functions for importing and uploading
 *
 * Zoph is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Zoph is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with Zoph; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @author Jeroen Roos
 * @package Zoph
 */
namespace import;

use file;
use log;
use photo;
use photo\exif;
use category;
use settings;

use conf\conf;

use geo\track;

use DOMDocument;


/**
 * This class holds the generalized functions importing images
 * to Zoph.
 *
 * @author Jeroen Roos
 * @package Zoph
 */
abstract class base {

    /**
     * Rotates a file based on the EXIF orientation flag
     *
     * Calls external program jhead for this.
     * @param string Filename
     */
    protected static function autorotate($file) {
        $cmd = "jhead -autorot " . escapeshellarg($file);
        exec($cmd, $output, $return);
        if ($return > 0) {
            $msg=implode($output, "<br>");
            throw new \importAutorotException($msg);
        }
    }

    /**
     * Import photos
     *
     * Takes an array of files and an array of vars and imports them in Zoph
     * @param Array Files to be imported
     * @param  Array Vars to be applied to the photos.
     */
    public static function photos(Array $files, Array $vars) {
        $photos=array();

        $total=sizeof($files);
        $cur=0;

        if (isset($vars["_path"])) {
            $path=file::cleanupPath("/" . $vars["_path"] . "/");
            if (strpos($path, "..") !== false) {
                log::msg("Illegal characters in path", log::FATAL, log::IMPORT);
                die();
            }
        } else {
            $path="";
        }

        foreach ($files as $md5 => $file) {
            static::progress($cur, $total);
            $cur++;

            if ($file instanceof photo) {
                $photo=$file;
                $file=$photo->file["orig"];
            } else if ($file instanceof file) {
                $photo=new photo();
            }

            $mime=$file->getMime();
            if (conf::get("import.cli.exif")===true && $mime=="image/jpeg") {
                $exif=exif::process($file);
                if ($exif) {
                    $photo->setFields($exif);
                }
            }

            if (isset($vars["rating"])) {
                $rating=$vars["rating"];
                if (!(is_numeric($rating) && (1 <= $rating) && ($rating <= 10))) {
                    unset($rating);
                }
                unset($vars["rating"]);
            }

            if (isset($vars["field"]) && is_array($vars["_field"])) {
                foreach ($vars["_field"] as $key => $field) {
                    $vars[$field]=$vars["field"][$key];
                }
                unset($vars["_field"]);
                unset($vars["field"]);
            }

            if ($vars) {
                $photo->setFields($vars);
            }

            if (strlen(trim($photo->get("date")))==0) {
                $date=date("Y-m-d", filemtime($file));
                log::msg("Photo has no date set, using filedate (" . $date . ").",
                    log::NOTIFY, log::IMPORT);
                $photo->set("date", $date);
            }

            if (strlen(trim($photo->get("time")))==0) {
                $time=date("H:i:s", filemtime($file));
                log::msg("Photo has no time set, using time from filedate (" . $time . ").",
                    log::NOTIFY, log::IMPORT);
                $photo->set("time", $time);
            }
            if (isset($photo->_path)) {
                $photo->set("path", $path . "/" . $photo->_path);
                unset($photo->_path);
            } else {
                $photo->set("path", $path);
            }

            try {
                $photo->import($file);
            } catch (\fileException $e) {
                log::msg($e->getMessage(), log::FATAL);
            }

            if (conf::get("import.cli.thumbs")===true) {
                try {
                    $photo->thumbnail(false);
                } catch (\Exception $e) {
                    echo $e->getMessage();
                }
            }

            if ($photo->insert()) {
                $rated = false;
                if (conf::get("import.cli.size")===true) {
                    $photo->updateSize();
                }
                $photo->update();
                $photo->updateRelations($vars, "_id");

                if (strlen($md5) > 10) {
                    $counter = 0;
                    $categories = $vars["_category_" . $md5 ] ?? false;
                    if (is_array($categories)) {
                        foreach ($categories as $category) {
                            $photo->addTo(new category($category));
                        }
                    }
                    $phRating = $vars["_rating_" . $md5 ] ?? false;
                    if ($phRating && is_numeric($phRating) && ($phRating >= 1) && ($phRating <= 10)) {
                        $photo->rate($phRating);
                        $rated = true;
                    }
                }
                if (isset($rating) && !$rated) {
                    $photo->rate($rating);
                }

                if (conf::get("import.cli.hash")===true) {
                    try {
                        $photo->getHash();
                    } catch (\Exception $e) {
                        echo $e->getMessage();
                    }
                }
                $photos[]=$photo;
            } else {
                echo translate("Insert failed.") . "<br>\n";
            }
        }
        return $photos;
    }

    /**
     * Import an XML file
     *
     * @param string MD5 hash of the filename to import
     *
     * This function tries to recognize the XML file by validating them against .xsd files
     * For now only GPX (1.0 and 1.1) files are recognized.
     */

    public static function XMLimport(file $file) {
        $xml=new DOMDocument;
        $xml->Load($file);

        $schemas = array (
            "gpx 1.0" => "xml/gpx10.xsd",
            "gpx 1.1" => "xml/gpx11.xsd" );

        foreach ($schemas as $name => $schema) {
            if (@$xml->schemaValidate(settings::$php_loc . "/" . $schema)) {
                log::msg(basename($file) ." is a valid " . $name . " file", log::NOTIFY, log::IMPORT);
                $xmltype=$name;
            }
        }
        if (!isset($xmltype)) {
            throw new \importFileNotImportableException(basename($file) . " is not a known XML file.");
        } else {
            switch($name) {
            case "gpx 1.0":
            case "gpx 1.1":
                $track=track::getFromGPX($file);
                $track->insert();
                $file->delete();
                break;
            }
        }
    }

    /**
     * Progress bar
     * Does not display anything by default, but this function can be redefined
     * in a child class.
     *
     * @param int current
     * @param int total
     */
    public static function progress($cur, $total) {
        return 0;
    }

}