File: ansel.php

package info (click to toggle)
ansel1 1.1%2Bdebian0-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,856 kB
  • ctags: 4,405
  • sloc: php: 18,135; xml: 359; sql: 241; makefile: 66
file content (332 lines) | stat: -rwxr-xr-x 11,298 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/php
<?php
/**
* $Horde: ansel/scripts/ansel.php,v 1.26 2008-08-07 13:43:12 mrubinsk Exp $
*
* This script interfaces with Ansel via the command-line
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
*
* @author Vijay Mahrra <webmaster@stain.net>
*/

@define('AUTH_HANDLER', true);
@define('HORDE_BASE', dirname(__FILE__) . '/../..');
@define('ANSEL_BASE', HORDE_BASE . '/ansel');

// Do CLI checks and environment setup first.
require_once HORDE_BASE . '/lib/core.php';
require_once 'Horde/CLI.php';

// Make sure no one runs this from the web.
if (!Horde_CLI::runningFromCLI()) {
    exit("Must be run from the command line\n");
}

// Load the CLI environment.
Horde_CLI::init();
$cli = &Horde_CLI::singleton();

// Load Ansel.
require_once ANSEL_BASE . '/lib/base.php';

// We accept the user name on the command-line.
require_once 'Console/Getopt.php';
$ret = Console_Getopt::getopt(Console_Getopt::readPHPArgv(),
                              'hu:p:lc:g:a:d:t:',
                              array('help', 'username=', 'password=', 'list',
                                    'create=', 'gallery=', 'add=', 'dir=',
                                    'caption='));

if (is_a($ret, 'PEAR_Error')) {
    var_dump($ret);
    $error = _("Couldn't read command-line options.");
    Horde::logMessage($error, __FILE__, __LINE__, PEAR_LOG_DEBUG);
    $cli->fatal($error);
}

// Show help and exit if no arguments were set.
list($opts, $args) = $ret;
if (!$opts) {
    showHelp();
    exit;
}

// Defaults/initialization.
$caption = '';

foreach ($opts as $opt) {
    list($optName, $optValue) = $opt;
    switch ($optName) {
    case 'u':
    case '--username':
        $username = $optValue;
        break;

    case 'p':
    case '--password':
        $password = $optValue;
        break;

    case 'l':
    case '--list':
        $list = true;
        break;

    case 'c':
    case '--create':
        list($gallery_name, $gallery_desc, $gallery_owner) = explode('/', $optValue, 4);
        $createGallery = true;
        break;

    case 'g':
    case '--gallery':
        $galleryId = $optValue;
        break;

    case 'a':
    case '--add':
        $file = $optValue;
        break;

    case 'd':
    case '--dir':
        $dir = $optValue;
        break;

    case 'h':
    case '--help':
        showHelp();
        exit;

    case 't':
    case '--caption':
        $caption = $optValue;
        break;
    }
}

// Login to horde if username & password are set.
if (!empty($username) && !empty($password)) {
    $auth = &Auth::singleton($conf['auth']['driver']);
    if (!$auth->authenticate($username, array('password' => $password))) {
        $error = _("Login is incorrect.");
        Horde::logMessage($error, __FILE__, __LINE__, PEAR_LOG_ERR);
        $cli->fatal($error);
    } else {
        $msg = sprintf(_("Logged in successfully as \"%s\"."), $username);
        Horde::logMessage($msg, __FILE__, __LINE__, PEAR_LOG_DEBUG);
        $cli->message($msg, 'cli.success');
    }
}

// Choose the gallery to add to (or use the created one).
if (!empty($galleryId)) {
    if (!$ansel_storage->galleryExists($galleryId)) {
        $error = sprintf(_("Invalid gallery \"%s\" specified."), $galleryId);
        Horde::logMessage($error, __FILE__, __LINE__, PEAR_LOG_WARNING);
        $cli->fatal($error);
    } else {
        $gallery = $ansel_storage->getGallery($galleryId);
        if (is_a($gallery, 'PEAR_Error') ||
            !$gallery->hasPermission(Auth::getAuth(), PERMS_EDIT)) {
            $error = sprintf(_("Access denied adding photos to \"%s\"."),
                             $galleryId);
            Horde::logMessage($error, __FILE__, __LINE__, PEAR_LOG_WARNING);
            $cli->fatal($error);
        }
    }
}

// Create a gallery.
if (!empty($createGallery)) {

    // See if we have selected a gallery parent.
    if (!empty($galleryId)) {
        $parent = $galleryId;
    }

    $attributes = array('name' => $gallery_name,
                        'desc' => $gallery_desc,
                        'owner' => $gallery_owner);
    $gallery = $ansel_storage->createGallery($attributes, null, $parent);
    if (is_a($gallery, 'PEAR_Error')) {
        $galleryId = null;
        $error = sprintf(_("The gallery \"%s\" couldn't be created: %s"),
                         $gallery_name, $gallery->getMessage());
        Horde::logMessage($error, __FILE__, __LINE__, PEAR_LOG_ERR);
        $cli->fatal($error);
    } else {
        $msg = sprintf(_("The gallery \"%s\" was created successfully."),
                       $gallery_name);
        Horde::logMessage($msg, __FILE__, __LINE__, PEAR_LOG_DEBUG);
        $cli->message($msg, 'cli.success');
    }
}

// List galleries/images.
if (!empty($list)) {
    if (!empty($gallery)) {
        $images = $gallery->listImages();
        if (is_a($images, 'PEAR_Error')) {
            $cli->fatal($images->getMessage());
        }

        $cli->message(sprintf(_("Listing photos in %s"), $gallery->get('name')), 'cli.success');
        $cli->writeln();

        $images = array_keys($images);
        foreach ($images as $id) {
            $image = &$ansel_storage->getImage($id);
            $cli->writeln(str_pad($image->filename, 30) . $image->getVFSPath() . '/' . $id);
        }
    } else {
        $galleries = $GLOBALS['ansel_storage']->listGalleries();
        if (is_a($galleries, 'PEAR_Error')) {
            $error = _("Couldn't list galleries.");
            Horde::logMessage($error, __FILE__, __LINE__, PEAR_LOG_DEBUG);
            $cli->fatal($error);
        }

        $cli->message(_("Listing Gallery/Name"), 'cli.success');
        $cli->writeln();

        foreach ($galleries as $id => $gallery) {
            $name = $gallery->get('name');
            $msg = "$id/$name";
            $cli->writeln($msg);
            Horde::logMessage($msg, __FILE__, __LINE__, PEAR_LOG_DEBUG);
        }
    }
}

// Add an image from the filesystem.
if (!empty($file) && isset($gallery) && !is_a($gallery, 'PEAR_Error')) {
    $image = &Ansel::getImageFromFile($file, array('caption' => $caption));
    if (is_a($image, 'PEAR_Error')) {
        Horde::logMessage($image->getMessage(), __FILE__, __LINE__, PEAR_LOG_WARNING);
        $cli->fatal($image->getMessage());
    }

    $cli->message(sprintf(_("Storing photo \"%s\"..."), $file), 'cli.message');
    $image_id = $gallery->addImage($image);
    if (is_a($image_id, 'PEAR_Error')) {
        $error = sprintf(_("There was a problem adding the photo \"%s\" to gallery \"%s\": %s"),
                         basename($file), $galleryId, $image_id->getMessage());
        Horde::logMessage($error, __FILE__, __LINE__, PEAR_LOG_ERR);
        $cli->fatal($error);
    }

    $msg = sprintf(_("Successfully added photo \"%s\" to gallery \"%s\"."),
                   basename($file), $galleryId);
    $cli->message($msg, 'cli.success');
    Horde::logMessage($msg, __FILE__, __LINE__, PEAR_LOG_NOTICE);
}

// Add all images from a directory on the filesystem.
if (!empty($dir) && isset($gallery) && !is_a($gallery, 'PEAR_Error')) {
    $msg = addDirToGallery($dir, $gallery);
    if (is_a($msg, 'PEAR_Error')) {
        Horde::logMessage($msg->getMessage(), __FILE__, __LINE__, PEAR_LOG_ERR);
        $cli->fatal($msg->getMessage());
    }
    if ($msg) {
        $msg = sprintf(ngettext("Successfully added %d photo (%s) to gallery \"%s\" from \"%s\".", "Successfully added %d photos (%s) to gallery \"%s\" from \"%s\".", count($msg)),
                       count($msg), join(', ', $msg), $galleryId, $dir);
        $cli->message($msg, 'cli.success');
        Horde::logMessage($msg, __FILE__, __LINE__, PEAR_LOG_NOTICE);
    } else {
        $msg = sprintf(_("The directory \"%s\" had no valid photos."), $dir);
        $cli->message($msg, 'cli.warning');
        Horde::logMessage($msg, __FILE__, __LINE__, PEAR_LOG_WARNING);
    }
}

exit;


/**
 * Reads all images from a directory into the currently selected gallery.
 *
 * @param string $dir
 * @param object $gallery &$ansel_shares->getShare($galleryId)
 * @return array An array of all the images that were successfully added to
 *               the gallery, or PEAR_Error
 */
function addDirToGallery($dir = '', &$gallery)
{
    global $cli, $galleryId, $ansel_shares;

    if (!file_exists($dir)) {
        $error = sprintf(_("The directory \"%s\" doesn't exist."), $dir);
        Horde::logMessage($error, __FILE__, __LINE__, PEAR_LOG_ERR);
        return PEAR::raiseError($error);
    }

    // Read all the files into an array.
    $files_array = array();
    $h = opendir($dir);
    while (false !== ($file = readdir($h))) {
        if ($file == '.' || $file == '..') {
            continue;
        }
        $files_array[] = $file;
    }
    closedir($h);

    if (!$files_array) {
        $error = sprintf(_("The directory \"%s\" is empty."), $dir);
        Horde::logMessage($error, __FILE__, __LINE__, PEAR_LOG_ERR);
        return PEAR::raiseError($error);
    }
    chdir($dir);

    // Process each file and upload to the gallery.
    $added_images = array();
    foreach ($files_array as $file) {
        $image = Ansel::getImageFromFile($dir . '/' . $file);
        if (is_a($image, 'PEAR_Error')) {
            Horde::logMessage($image->getMessage(), __FILE__, __LINE__, PEAR_LOG_WARNING);
            $cli->message($image->getMessage(), 'cli.error');
            continue;
        }

        $cli->message(sprintf(_("Storing photo \"%s\"..."), $file), 'cli.message');
        $image_id = $gallery->addImage($image);
        if (is_a($image_id, 'PEAR_Error')) {
            $error = sprintf(_("There was a problem adding the photo \"%s\" to gallery \"%s\"."),
                             $file, $galleryId);
            Horde::logMessage($error, __FILE__, __LINE__, PEAR_LOG_ERR);
            $cli->message($image_id->getMessage(), 'cli.error');
            continue;
        }

        $added_images[] = $file;
    }

    return $added_images;
}

/**
 * Show the command line arguments that the script accepts.
 */
function showHelp()
{
    global $cli;

    $cli->writeln(sprintf(_("Usage: %s [OPTIONS]..."), basename(__FILE__)));
    $cli->writeln();
    $cli->writeln(_("Mandatory arguments to long options are mandatory for short options too."));
    $cli->writeln();
    $cli->writeln(_("-h, --help                   Show this help"));
    $cli->writeln(_("-l, --list                   List galleries or photos (if combined with -g)"));
    $cli->writeln(_("-c, --create[=name/description/owner]\n                             Create gallery (and use it)  Combined with -g to create a subgallery."));
    $cli->writeln(_("-g, --gallery[=shortname]    Select gallery to use"));
    $cli->writeln(_("-a, --add[=filename]         Add local file to selected gallery"));
    $cli->writeln(_("-d, --dir[=directory]        Add all files from the directory to the selected\n                             gallery"));
    $cli->writeln(_("-u, --username[=username]    Horde login username"));
    $cli->writeln(_("-p, --password[=password]    Horde login password"));
    $cli->writeln(_("-t, --caption[=caption]      Caption for photo (if combined with -a)"));
    $cli->writeln();
}