summaryrefslogtreecommitdiff
path: root/modules/html5_uploader/controllers/uploader.php
blob: b06f84e3c4cb1a3d410f3ef9c95d886c861ad127 (plain)
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
<?php defined("SYSPATH") or die("No direct script access.");
/**
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2013 Bharat Mediratta
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */
class Uploader_Controller extends Controller {
  public function index($id) {
    $album = ORM::factory("item", $id);
    access::required("view", $album);
    access::required("add", $album);
    if (!$album->is_album()) {
      $album = $album->parent();
    }
	
	$view = new View("upload_form.html");
	$view->form = $this->_get_add_form($album);
	print $view;
  }

  public function add($id) {
    $album = ORM::factory("item", $id);
    access::required("view", $album);
    access::required("add", $album);
    access::verify_csrf();

    $form = $this->_get_add_form($album);
    if ($form->validate()) {
      batch::start();

      $count = 0;
      $added_a_movie = false;
      $added_a_photo = false;

      $files_list=$_FILES['files'];
      foreach (array_keys($files_list['name']) as $index) {
        try {
          $temp_filename = $files_list['tmp_name'][$index];
          $item = ORM::factory("item");
          $item->name = basename($files_list['name'][$index]);
          $item->title = item::convert_filename_to_title($item->name);
          $item->parent_id = $album->id;
          $item->set_data_file($temp_filename);

          $path_info = @pathinfo($item->name);
          if (array_key_exists("extension", $path_info) &&
              in_array(strtolower($path_info["extension"]), array("flv", "mp4", "m4v"))) {
            $item->type = "movie";
            $item->save();
            $added_a_movie = true;
            log::success("content", t("Added a movie"),
                         html::anchor("movies/$item->id", t("view movie")));
          } else {
            $item->type = "photo";
            $item->save();
            $added_a_photo = true;
            log::success("content", t("Added a photo"),
                         html::anchor("photos/$item->id", t("view photo")));
          }
          $count++;
          module::event("add_photos_form_completed", $item, $form);
        } catch (Exception $e) {
          // Lame error handling for now.  Just record the exception and move on
          Kohana_Log::add("error", $e->getMessage() . "\n" . $e->getTraceAsString());

          // Ugh.  I hate to use instanceof, But this beats catching the exception separately since
          // we mostly want to treat it the same way as all other exceptions
          if ($e instanceof ORM_Validation_Exception) {
            Kohana_Log::add("error", "Validation errors: " . print_r($e->validation->errors(), 1));
          }
        }

        if (file_exists($temp_filename)) {
          unlink($temp_filename);
        }
      }
      batch::stop();
      if ($count) {
        if ($added_a_photo && $added_a_movie) {
          message::success(t("Added %count photos and movies", array("count" => $count)));
        } else if ($added_a_photo) {
          message::success(t2("Added one photo", "Added %count photos", $count));
        } else {
          message::success(t2("Added one movie", "Added %count movies", $count));
        }
      }
      json::reply(array("result" => "success"));
    } else {
      json::reply(array("result" => "error", "html" => (string) $form));
    }

    // Override the application/json mime type.  The dialog based HTML uploader uses an iframe to
    // buffer the reply, and on some browsers (Firefox 3.6) it does not know what to do with the
    // JSON that it gets back so it puts up a dialog asking the user what to do with it.  So force
    // the encoding type back to HTML for the iframe.
    // See: http://jquery.malsup.com/form/#file-upload
    header("Content-Type: text/html; charset=" . Kohana::CHARSET);
  }

  private function _get_add_form($album) {
    $form = new Forge("uploader/add/{$album->id}", "", "post", array("id" => "g-add-photos-form"));
    $group = $form->group("add_photos")
      ->label(t("Add photos to %album_title", array("album_title" => html::purify($album->title))));
    $group->input("files[]")->type("file")->multiple();
    $group->input("tags")->type("text")->label(t("Tags"));

    $form->input("FOO")->type("hidden")->label(sprintf("Es k&ouml;nnen mehrere Bilder auf einmal hochgeladen werden. Dies dauert allerdings etwas - ohne extra Ladebalken. Also Geduld bewahren. Max. upload size of all pictures: %.0f MB.", ini_get("upload_max_filesize")));

    module::event("add_photos_form", $album, $form);

    $group = $form->group("buttons")->label("");
    $group->submit("")->value(t("Upload"));

    return $form;
  }
}