Skip to content
Snippets Groups Projects
blubber.php 19.7 KiB
Newer Older
<?php

class BlubberController extends AuthenticatedController
{
    protected $_autobind = true;

    public function before_filter(&$action, &$args)
    {
        parent::before_filter($action, $args);

        PageLayout::setTitle(_('Blubber'));
Moritz Strohm's avatar
Moritz Strohm committed

        $this->threads_more_down = 0;
    }

    public function index_action($thread_id = null)
    {
        Navigation::activateItem('/community/blubber');

        $this->threads = BlubberThread::findMyGlobalThreads(
            51,
            null,
            null,
            null,
            Request::get("search")
        );
        if (count($this->threads) > 20) {
            array_pop($this->threads);
            $this->threads_more_down = 1;
        }

        if ($thread_id) {
            $GLOBALS['user']->cfg->store('BLUBBER_DEFAULT_THREAD', $thread_id);
        } else {
            $thread_id = $GLOBALS['user']->cfg->BLUBBER_DEFAULT_THREAD;
        }

        if ($thread_id) {
            $this->thread = BlubberThread::find($thread_id);
            if (!$this->thread || !$this->thread->isReadable()) {
                $this->thread = null;
            }
        }

        if (!$this->thread) {
            $threads = array_reverse($this->threads);
            $this->thread = array_pop($threads);
        }

        if ($this->thread) {
            $this->thread->markAsRead();
            $this->thread_data = $this->thread->getJSONData(
                50,
                null,
                Request::get("search")
            );
David Siegfried's avatar
David Siegfried committed
        if (
            empty($_SESSION['already_asked_for_avatar'])
            && !Avatar::getAvatar($GLOBALS['user']->id)->is_customized()
        ) {
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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
            $_SESSION['already_asked_for_avatar'] = true;
            PageLayout::postInfo(sprintf(
                _('Wollen Sie ein Avatar-Bild nutzen? %sLaden Sie jetzt ein Bild hoch%s.'),
                '<a href="' . URLHelper::getLink("dispatch.php/avatar/update/user/" . $GLOBALS['user']->id) . '" data-dialog>',
                '</a>'
            ));
        }

        if (Request::isDialog()) {
            PageLayout::setTitle($this->thread->getName());
        }
        $this->buildSidebar();

        if (Request::isDialog()) {
            $this->render_template('blubber/dialog');
        }
    }

    public function compose_action($thread_id = null)
    {
        $this->thread = BlubberThread::find($thread_id);
        if ($this->thread && !$this->thread->isWritable()) {
            throw new AccessDeniedException();
        }

        PageLayout::setTitle($this->thread ? _('Blubber bearbeiten') : _('Neuer Blubber'));

        if (Request::isPost() && count(Request::getArray('user_ids'))) {
            $user_ids = array_filter(Request::getArray('user_ids'));

            if (count($user_ids) === 1) {
                //try to redirect to an existing 2 person thread:
                $query = "SELECT blubber_threads.thread_id
                          FROM blubber_threads
                          JOIN blubber_mentions
                            ON blubber_mentions.thread_id = blubber_threads.thread_id
                          JOIN blubber_mentions AS blubber_mentions_me
                            ON blubber_mentions_me.thread_id = blubber_threads.thread_id
                          JOIN blubber_mentions AS blubber_mentions_friend
                            ON blubber_mentions_friend.thread_id = blubber_threads.thread_id
                          WHERE blubber_threads.context_type = 'private'
                            AND blubber_mentions_me.user_id = :me
                            AND blubber_mentions_friend.user_id = :friend
                          GROUP BY blubber_threads.thread_id
                          HAVING COUNT(blubber_mentions.user_id) = 2
                          ORDER BY blubber_threads.mkdate DESC
                          LIMIT 1";
                $statement = DBManager::get()->prepare($query);
                $statement->execute([
                    'me' => $GLOBALS['user']->id,
                    'friend' => $user_ids[0]
                ]);
                $thread_id = $statement->fetchColumn();
                if ($thread_id) {
                    $this->redirect("blubber/index/{$thread_id}");
                    return;
                }
            }
            $blubber = new BlubberThread();
            $blubber['context_type'] = 'private';
            $blubber['context_id'] = 'global';
            $blubber['user_id'] = $GLOBALS['user']->id;
            $blubber['external_contact'] = 0;
            $blubber['display_class'] = null;
            $blubber['visible_in_stream'] = 1;
            $blubber['commentable'] = 1;
            $blubber['content'] = '';
            $blubber->store();

            $query = "INSERT IGNORE INTO blubber_mentions
                      SET thread_id = :thread_id,
                          user_id = :user_id,
                          external_contact = 0,
                          mkdate = UNIX_TIMESTAMP()";
            $insert = DBManager::get()->prepare($query);

            $user_ids[] = $GLOBALS['user']->id;
            foreach ($user_ids as $user_id) {
                $insert->execute([
                    'thread_id' => $blubber->getId(),
                    'user_id'   => $user_id,
                ]);
            }
            $this->redirect("blubber/index/{$blubber->getId()}");
            return;
        }

        $this->contacts = Contact::findBySQL("JOIN auth_user_md5 USING (user_id) WHERE owner_id = ? ORDER BY auth_user_md5.Nachname ASC, auth_user_md5.Vorname ASC", [
            $GLOBALS['user']->id
        ]);
    }

    public function delete_action($thread_id)
    {
        $this->thread = BlubberThread::find($thread_id);
        if (!$this->thread->isWritable()) {
            throw new AccessDeniedException();
        }
        if (Request::isPost()) {
            CSRFProtection::verifySecurityToken();
            $this->thread->delete();
            PageLayout::postSuccess(_('Der Blubber wurde gelöscht.'));
        }
        $this->redirect("blubber/index");
        return;
    }

    public function write_to_action($user_id = null)
    {
        $user_ids = array_filter(Request::getArray('user_ids'));
        if (!$user_ids) {
            $user_ids = [$user_id];
        }

        if (count($user_ids) === 1) {
            //try to redirect to an existing 2 person thread:
            $query = "SELECT blubber_threads.thread_id
                      FROM blubber_threads
                      JOIN blubber_mentions
                        ON blubber_mentions.thread_id = blubber_threads.thread_id
                      JOIN blubber_mentions AS blubber_mentions_me
                        ON blubber_mentions_me.thread_id = blubber_threads.thread_id
                      JOIN blubber_mentions AS blubber_mentions_friend
                        ON blubber_mentions_friend.thread_id = blubber_threads.thread_id
                      WHERE blubber_threads.context_type = 'private'
                        AND blubber_mentions_me.user_id = :me
                        AND blubber_mentions_friend.user_id = :friend
                      GROUP BY blubber_threads.thread_id
                      HAVING COUNT(blubber_mentions.user_id) = 2
                      ORDER BY blubber_threads.mkdate DESC
                      LIMIT 1";
            $statement = DBManager::get()->prepare($query);
            $statement->execute([
                'me'     => $GLOBALS['user']->id,
                'friend' => $user_ids[0],
            ]);
            $thread_id = $statement->fetchColumn();
            if ($thread_id) {
                $this->redirect("blubber/index/{$thread_id}");
                return;
            }
        }
        $blubber = new BlubberThread();
        $blubber['context_type'] = 'private';
        $blubber['context_id'] = 'global';
        $blubber['user_id'] = $GLOBALS['user']->id;
        $blubber['external_contact'] = 0;
        $blubber['display_class'] = null;
        $blubber['visible_in_stream'] = 1;
        $blubber['commentable'] = 1;
        $blubber['content'] = '';
        $blubber->store();

        $query = "INSERT IGNORE INTO blubber_mentions
                  SET thread_id = :thread_id,
                      user_id = :user_id,
                      external_contact = 0,
                      mkdate = UNIX_TIMESTAMP()";
        $insert = DBManager::get()->prepare($query);

        $user_ids[] = $GLOBALS['user']->id;
        foreach ($user_ids as $user_id) {
            $insert->execute([
                'thread_id' => $blubber->getId(),
                'user_id'   => $user_id,
            ]);
        }
        $this->redirect("blubber/index/{$blubber->getId()}");
    }

    public function to_course_action($course_id)
    {
        if (!$GLOBALS['perm']->have_studip_perm('user', $course_id)) {
            throw new AccessDeniedException();
        }

        $condition = "context_type = 'course'
                      AND context_id = ?
                      AND visible_in_stream = 1
                      AND content IS NULL";
        $thread = BlubberThread::findOneBySQL($condition, [$course_id]);
        if (!$thread) {
            //create the default-thread for this context
            $thread = new BlubberThread();
            $thread['user_id'] = $GLOBALS['user']->id;
            $thread['external_contact'] = 0;
            $thread['context_type'] = 'course';
            $thread['context_id'] = $course_id;
            $thread['visible_in_stream'] = 1;
            $thread['commentable'] = 1;
            $thread->store();
        }
        $this->redirect("blubber/index/{$thread->getId()}");
    }

    /**
     * Saves given files (dragged into the textarea) and returns the link to the
     * file to the user as json.
     * @throws AccessDeniedException
     */
    public function upload_files_action()
    {
        $context = Request::get('context', $GLOBALS['user']->id);
        $context_type = Request::option('context_type');
        if (!Request::isPost()
            || ($context_type === 'course' && !$GLOBALS['perm']->have_studip_perm('autor', $context))
        ) {
            throw new AccessDeniedException();
        }

        $output = [];
        foreach ($_FILES as $file) {
            $newfile = null; //is filled below
            $file_ref = null; //is also filled below


            if ($file['size']) {
                $document['user_id'] = $GLOBALS['user']->id;
                $document['filesize'] = $file['size'];

                try {
                    $root_dir = Folder::findTopFolder($GLOBALS['user']->id);
                    $root_dir = $root_dir->getTypedFolder();
                    $blubber_directory = Folder::findOneBySql(
                        "parent_id = :parent_id
                         AND folder_type = 'PublicFolder'
                         AND data_content = :content",
                        [
                            'parent_id' => $root_dir->getId(),
                            'content'   => json_encode(['Blubber']),
                        ]
                    );


                    if ($blubber_directory) {
                        $blubber_directory = $blubber_directory->getTypedFolder();
                    } else {
                        //blubber directory not found: create it
                        $blubber_directory = FileManager::createSubFolder(
                            $root_dir,
                            $GLOBALS['user']->getAuthenticatedUser(),
                            'PublicFolder',
                            'Blubber',
                            _('Ihre Dateien aus Blubberstreams')
                        );

                        if (!$blubber_directory instanceof FolderType) {
                            throw new Exception($blubber_directory[0]);
                        }

                        $blubber_directory->data_content = ['Blubber'];
                        $blubber_directory->store();
                    }

                    if ($blubber_directory) {
                        //ok, blubber directory exists: we can handle the uploaded file

                        $uploaded = FileManager::handleFileUpload(
                            [
                                'tmp_name' => [$file['tmp_name']],
                                'name'     => [$file['name']],
                                'size'     => [$file['size']],
                                'type'     => [$file['type']],
                                'error'    => [$file['error']]
                            ],
                            $blubber_directory,
                            $GLOBALS['user']->id
                        );

                        if ($uploaded['error']) {
                            throw new Exception(implode("\n", $uploaded['error']));
                        } elseif($uploaded['files'][0]) {
                            $oldbase = URLHelper::setBaseURL($GLOBALS['ABSOLUTE_URI_STUDIP']);
                            $url = $uploaded['files'][0]->getDownloadURL();
                            URLHelper::setBaseURL($oldbase);
                            $success = true;
                        } else {
                            throw new Exception('File cannot be created!');
                        }

                    }
                } catch (Exception $e) {
                    $output['errors'][] = $e->getMessage();
                    $success = false;
                }


                if ($success) {
                    $type = null;

                    if (mb_strpos($file['type'], 'image') !== false) {
                        $type = 'img';
                    }
                    if (mb_strpos($file['type'], 'video') !== false) {
                        $type = 'video';
                    }
                    if (mb_strpos($file['type'], 'audio') !== false || mb_strpos($file_ref['name'], '.ogg') !== false) {
                        $type = 'audio';
                    }
                    if ($type) {
                        $output['inserts'][] = "[{$type}]{$url}";
                    } else {
                        $output['inserts'][] = "[{$file_ref['name']}]{$url}";
                    }
                }
            }
        }
        $this->render_json($output);
    }

    public function add_member_to_private_action($thread_id)
    {
        $this->thread = BlubberThread::find($thread_id);
        if (!$this->thread['context_type'] === 'private' || !$this->thread->isReadable()) {
            throw new AccessDeniedException();
        }
        PageLayout::setTitle(_('Person hinzufügen'));
        if (Request::isPost() && Request::option('user_id')) {
            $query = "INSERT IGNORE INTO blubber_mentions
                      SET thread_id = :thread_id,
                          user_id = :user_id,
                          external_contact = 0,
                          mkdate = UNIX_TIMESTAMP()";
            $statement = DBManager::get()->prepare($query);
            $statement->execute([
                'thread_id' => $thread_id,
                'user_id'   => Request::option('user_id'),
            ]);
            $this->response->add_header(
                'X-Dialog-Execute',
                'STUDIP.Blubber.refreshThread'
            );
            $this->response->add_header('X-Dialog-Close', '1');
            $this->render_json([
                'thread_id' => $thread_id,
            ]);
        }
    }

    public function private_to_studygroup_action(BlubberThread $thread)
    {
        if ($this->thread['context_type'] !== 'private' || !$this->thread->isReadable()) {
            throw new AccessDeniedException();
        }
        PageLayout::setTitle(_("Studiengruppe aus Konversation erstellen"));
        if (Request::isPost() && count(studygroup_sem_types())) {
            $studgroup_sem_types = studygroup_sem_types();
            $course = new Course();
            $course['name'] = Request::get('name');
            $course['status'] = array_shift($studgroup_sem_types);
            $course->store();

            if ($_FILES['avatar'] && $_FILES['avatar']['error'] !== UPLOAD_ERR_NO_FILE) {
                CourseAvatar::getAvatar($course->getId())->createFromUpload('avatar');
            }

            $query = "SELECT user_id
                      FROM blubber_mentions
                      WHERE thread_id = ?";
            $statement = DBManager::get()->prepare($query);
            $statement->execute([$this->thread->id]);
            foreach ($statement->fetchFirst() as $user_id) {
                $member = new CourseMember();
                $member['user_id'] = $user_id;
                $member['seminar_id'] = $course->getId();
                $member['status'] = $user_id === $this->thread['user_id'] ? 'dozent' : 'tutor';
                $member->store();
            }

            $this->thread['context_type'] = 'course';
            $this->thread['context_id'] = $course->getId();
            $this->thread['content'] = trim($this->thread['content']) ?: null;
            $this->thread->store();

            PluginManager::getInstance()->setPluginActivated(
                PluginManager::getInstance()->getPlugin('Blubber')->getPluginId(),
                $course->getId(),
                true
            );

            PageLayout::postSuccess(sprintf(_("Studiengruppe '%s' wurde angelegt."), htmlReady($course['name'])));
            $this->redirect(URLHelper::getURL('seminar_main.php', ['auswahl' => $course->getId()]));
        }
    }

    public function leave_private_action(BlubberThread $thread)
    {
        if ($this->thread['context_type'] !== 'private' || !$this->thread->isReadable()) {
            throw new AccessDeniedException();
        }
        PageLayout::setTitle(_("Private Konversation verlassen"));
        if (Request::isPost()) {
            BlubberMention::deleteBySQL("user_id = :me AND external_contact = '0' AND thread_id = :thread_id", [
                'thread_id' => $this->thread->getId(),
                'me' => $GLOBALS['user']->id
            ]);
            if (Request::get("delete_comments")) {
                BlubberComment::deleteBySQL("thread_id = :thread_id AND user_id = :me AND external_contact = '0'", [
                    'thread_id' => $this->thread->getId(),
                    'me' => $GLOBALS['user']->id
                ]);
            }
            if ($this->thread['user_id'] === $GLOBALS['user']->id) {
                $this->thread['content'] = "";
                $this->thread->store();
            }
            $count_departed = BlubberMention::countBySQL("INNER JOIN auth_user_md5 USING (user_id) WHERE external_contact = '0' AND thread_id = :thread_id", [
                'thread_id' => $this->thread->getId()
            ]);
            $count_comments = BlubberComment::countBySQL("thread_id = :thread_id AND external_contact = '0'", [
                'thread_id' => $this->thread->getId()
            ]);
            if (!$count_departed || (!$count_comments && !$this->thread['content'])) {
                //ich mache das Licht aus:
                $this->thread->delete();
                PageLayout::postSuccess(_("Private Konversation gelöscht."));
            } else {
                PageLayout::postSuccess(_("Private Konversation verlassen."));
            }
            $this->redirect("blubber/index");
        }
    }

    protected function buildSidebar()
    {
        $search = new SearchWidget("#");
        $search->addNeedle(
            _("Suche nach ..."),
            "search",
            true
        );

        Sidebar::Get()->addWidget($search, "blubbersearch");

        $threads_widget = Sidebar::Get()->addWidget(
            new BlubberThreadsWidget(),
            'threads'
        );
        foreach ($this->threads as $thread) {
            $threads_widget->addThread($thread);
        }

        if ($this->thread) {
            $threads_widget->setActive($this->thread->getId());
        }

        $threads_widget->withComposer();
    }
}