Пример #1
0
 /**
  * This function will create a new user object and return the newly created user object.
  *
  * @param array $userInfo This should have the properties: username, firstname, lastname, password, ui_language
  *
  * @return mixed
  */
 public function registerUser(array $userInfo, $userLanguage)
 {
     $user = \User::create($userInfo);
     //make the first user an admin
     if (\User::all()->count() <= 1) {
         $user->is_admin = 1;
     }
     // Trim trailing whitespace from user first and last name.
     $user->firstname = trim($user->firstname);
     $user->lastname = trim($user->lastname);
     $user->save();
     \Setting::create(['ui_language' => $userLanguage, 'user_id' => $user->id]);
     /* Add welcome note to user - create notebook, tag and note */
     //$notebookCreate = Notebook::create(array('title' => Lang::get('notebooks.welcome_notebook_title')));
     $notebookCreate = new \Notebook();
     $notebookCreate->title = Lang::get('notebooks.welcome_notebook_title');
     $notebookCreate->save();
     $notebookCreate->users()->attach($user->id, ['umask' => \PaperworkHelpers::UMASK_OWNER]);
     //$tagCreate = Tag::create(array('title' => Lang::get('notebooks.welcome_note_tag'), 'visibility' => 0));
     $tagCreate = new \Tag();
     $tagCreate->title = Lang::get('notebooks.welcome_note_tag');
     $tagCreate->visibility = 0;
     $tagCreate->user_id = $user->id;
     $tagCreate->save();
     //$tagCreate->users()->attach($user->id);
     $noteCreate = new \Note();
     $versionCreate = new \Version(['title' => Lang::get('notebooks.welcome_note_title'), 'content' => Lang::get('notebooks.welcome_note_content'), 'content_preview' => mb_substr(strip_tags(Lang::get('notebooks.welcome_note_content')), 0, 255), 'user_id' => $user->id]);
     $versionCreate->save();
     $noteCreate->version()->associate($versionCreate);
     $noteCreate->notebook_id = $notebookCreate->id;
     $noteCreate->save();
     $noteCreate->users()->attach($user->id, ['umask' => \PaperworkHelpers::UMASK_OWNER]);
     $noteCreate->tags()->sync([$tagCreate->id]);
     return $user;
 }
Пример #2
0
 /**
  * Create Note and Version instances
  *
  * $created_at and $updated_at values we have from parsed xml
  *
  * @param $title
  * @param $content
  * @param $created_at
  * @param $updated_at
  * @return \Note
  */
 protected function createNote($title, $content, $created_at, $updated_at)
 {
     $noteCreate = new \Note();
     $noteCreate->created_at = $created_at;
     $noteCreate->updated_at = $updated_at;
     // Add spaces for strip_tags
     $contentPreview = preg_replace('/(<[^>]+>)/', '$1 ', $content);
     $contentPreview = strip_tags($contentPreview);
     $versionCreate = new \Version(['title' => $title, 'content' => $content, 'content_preview' => mb_substr($contentPreview, 0, 255), 'created_at' => $created_at, 'updated_at' => $updated_at, 'user_id' => \Auth::user()->id]);
     $versionCreate->save();
     $noteCreate->version()->associate($versionCreate);
     $noteCreate->notebook_id = $this->notebook->id;
     $noteCreate->save();
     $noteCreate->users()->attach(\Auth::user()->id, array('umask' => \PaperworkHelpers::UMASK_OWNER));
     return $noteCreate;
 }
Пример #3
0
 /**
  * Fetch attachments from xml['resource']
  * Use base64
  * Replace attachments links in note's content: <en-media...
  *
  *
  * @param $xmlNote
  * @param \Note $noteInstance
  * @throws \Exception
  */
 protected function processFile($xmlNote, $noteInstance)
 {
     if (isset($xmlNote['resource']['data'])) {
         $xmlNote['resource'] = [$xmlNote['resource']];
     }
     foreach ($xmlNote['resource'] as $attachment) {
         // No name? Use rand
         $hasResourceAttr = isset($attachment['resource-attributes']);
         $hasFileName = isset($attachment['resource-attributes']['file-name']);
         $fileName = $attachment['resource-attributes']['file-name'];
         $fileName = $hasResourceAttr && $hasFileName ? $fileName : uniqid(rand(), true);
         $fileContent = base64_decode($attachment['data']);
         $fileHash = md5($fileContent);
         $newAttachment = $this->createAttachment($fileContent, $fileName, $attachment['mime']);
         $noteVersion = $noteInstance->version()->first();
         // TODO: review regexp - need to fetch style attribute in another way.
         // replace en-media tag by img
         if (str_contains($attachment['mime'], 'image')) {
             $imageTag = sprintf('<img $1 src="/api/v1/notebooks/%s/notes/%s/versions/%s/attachments/%s/raw" />', $this->notebook->id, $noteInstance->id, $noteVersion->id, $newAttachment->id);
             $noteVersion->content = preg_replace('/<en-media[^>]*hash="' . $fileHash . '"([^>]*)><\\/en-media>/', $imageTag, $noteVersion->content);
         } else {
             $linkTag = sprintf('<a $1 href="/api/v1/notebooks/%s/notes/%s/versions/%s/attachments/%s/raw">%s</a>', $this->notebook->id, $noteInstance->id, $noteVersion->id, $newAttachment->id, $fileName);
             $noteVersion->content = preg_replace('/<en-media[^>]*hash="' . $fileHash . '"([^>]*)><\\/en-media>/', $linkTag, $noteVersion->content);
         }
         $noteVersion->attachments()->attach($newAttachment);
         $noteVersion->save();
         // Doesn't work.
         // \Queue::push('DocumentParserWorker', array('user_id' => \Auth::user()->id, 'document_id' => $newAttachment->id));
     }
 }
Пример #4
0
 public function store($notebookId)
 {
     $newNote = Input::json();
     $note = new Note();
     $notebook = User::find(Auth::user()->id)->notebooks()->select('notebooks.id', 'notebooks.type', 'notebooks.title')->where('notebooks.id', '=', $notebookId)->wherePivot('umask', '>', PaperworkHelpers::UMASK_READONLY)->whereNull('notebooks.deleted_at')->first();
     if (is_null($notebook)) {
         return PaperworkHelpers::apiResponse(PaperworkHelpers::STATUS_NOTFOUND, []);
     }
     $version = new Version(['title' => $newNote->get("title"), 'content' => $newNote->get("content"), 'content_preview' => $newNote->get("content_preview"), 'user_id' => Auth::user()->id]);
     $version->save();
     $note->version()->associate($version);
     $note->notebook_id = $notebookId;
     $version->save();
     $note->save();
     // TODO: Should we inherit the umask from the notebook?
     $note->users()->attach(Auth::user()->id, array('umask' => PaperworkHelpers::UMASK_OWNER));
     $tagIds = ApiTagsController::createOrGetTags($newNote->get('tags'), $note->id, PaperworkHelpers::UMASK_OWNER);
     if (!is_null($tagIds)) {
         $note->tags()->sync($tagIds);
     }
     return PaperworkHelpers::apiResponse(PaperworkHelpers::STATUS_SUCCESS, $note);
 }
Пример #5
0
 public function checkSandstormUsers()
 {
     if (Config::get('paperwork.emergency_export') && DB::table('migrations')->where('batch', '=', 1)->count() == Config::get('paperwork.emergency_export_count')) {
         $credentials = ["username" => "sandstorm_dummy", "password" => "sandstorm_dummy"];
         if (Auth::attempt($credentials)) {
             $settings = Setting::where('user_id', '=', Auth::user()->id)->first();
             Session::put('ui_language', $settings->ui_language);
             return View::make('user.emergency_export');
         }
     } else {
         // get permission via HTTP_X_SANDSTORM header
         $sandstorm_permissions = $_SERVER['HTTP_X_SANDSTORM_PERMISSIONS'];
         // Only when we are admin, we check and create users
         if ($sandstorm_permissions == "admin,write,read") {
             // check for admin user
             if (User::where('username', '=', 'sandstorm_admin')->count() == 0) {
                 $sandstorm_admin = User::create(Input::except('_token', 'password_confirmation', 'ui_language'));
                 if ($sandstorm_admin) {
                     //make the first user an admin
                     $sandstorm_admin->firstname = "sandstorm_admin";
                     $sandstorm_admin->lastname = " ";
                     $sandstorm_admin->username = "******";
                     $sandstorm_admin->password = "******";
                     $sandstorm_admin->is_admin = 1;
                     $sandstorm_admin->save();
                     $setting_sandstorm_admin = Setting::create(['ui_language' => 'en', 'user_id' => $sandstorm_admin->id]);
                 }
             } else {
                 $sandstorm_admin = User::where('username', '=', 'sandstorm_admin');
             }
             // Then the read & write  user
             if (User::where('username', '=', 'sandstorm_readwrite')->count() == 0) {
                 $sandstorm_readwrite = User::create(Input::except('_token', 'password_confirmation', 'ui_language'));
                 if ($sandstorm_readwrite) {
                     $sandstorm_readwrite->firstname = "sandstorm_readwrite";
                     $sandstorm_readwrite->lastname = " ";
                     $sandstorm_readwrite->username = "******";
                     $sandstorm_readwrite->password = "******";
                     $sandstorm_readwrite->save();
                     $setting_sandstorm_readwrite = Setting::create(['ui_language' => 'en', 'user_id' => $sandstorm_readwrite->id]);
                 }
             } else {
                 $sandstorm_readwrite = User::where('username', '=', 'sandstorm_readwrite');
             }
             // Then the read only  user
             if (User::where('username', '=', 'sandstorm_readonly')->count() == 0) {
                 $sandstorm_readonly = User::create(Input::except('_token', 'password_confirmation', 'ui_language'));
                 if ($sandstorm_readonly) {
                     $sandstorm_readonly->firstname = "sandstorm_readonly";
                     $sandstorm_readonly->lastname = " ";
                     $sandstorm_readonly->username = "******";
                     $sandstorm_readonly->password = "******";
                     $sandstorm_readonly->save();
                     $setting_sandstorm_readonly = Setting::create(['ui_language' => 'en', 'user_id' => $sandstorm_readonly->id]);
                 }
             } else {
                 $sandstorm_readonly = User::where('username', '=', 'sandstorm_readonly');
             }
         }
         // Now that the required users are there we create the default
         if (Notebook::all()->count() == 0 && Tag::all()->count() == 0 && Note::all()->count() == 0) {
             // Notebook ...
             $notebookCreate = new Notebook();
             $notebookCreate->title = Lang::get('notebooks.welcome_notebook_title');
             $notebookCreate->save();
             $notebookCreate->users()->attach($sandstorm_readonly->id, ['umask' => PaperworkHelpers::UMASK_READONLY]);
             $notebookCreate->users()->attach($sandstorm_readwrite->id, ['umask' => PaperworkHelpers::UMASK_READWRITE]);
             $notebookCreate->users()->attach($sandstorm_admin->id, ['umask' => PaperworkHelpers::UMASK_OWNER]);
             // Tag ...
             $tagCreate = new Tag();
             $tagCreate->title = Lang::get('notebooks.welcome_note_tag');
             $tagCreate->visibility = 1;
             $tagCreate->user_id = $sandstorm_admin->id;
             $tagCreate->save();
             // Note ...
             $noteCreate = new Note();
             $versionCreate = new Version(['title' => Lang::get('notebooks.welcome_note_title'), 'content' => Lang::get('notebooks.welcome_note_content'), 'content_preview' => mb_substr(strip_tags(Lang::get('notebooks.welcome_note_content')), 0, 255), 'user_id' => $sandstorm_admin->id]);
             $versionCreate->save();
             $noteCreate->version()->associate($versionCreate);
             $noteCreate->notebook_id = $notebookCreate->id;
             $noteCreate->save();
             $noteCreate->users()->attach($sandstorm_readonly->id, ['umask' => PaperworkHelpers::UMASK_READONLY]);
             $noteCreate->users()->attach($sandstorm_readwrite->id, ['umask' => PaperworkHelpers::UMASK_READWRITE]);
             $noteCreate->users()->attach($sandstorm_admin->id, ['umask' => PaperworkHelpers::UMASK_OWNER]);
             $noteCreate->tags()->sync([$tagCreate->id]);
         }
         // login
         if ($sandstorm_permissions == "read") {
             $credentials = ["username" => "sandstorm_readonly", "password" => "sandstorm_readonly"];
         }
         if ($sandstorm_permissions == "write,read") {
             $credentials = ["username" => "sandstorm_readwrite", "password" => "sandstorm_readwrite"];
         }
         if ($sandstorm_permissions == "admin,write,read") {
             $credentials = ["username" => "sandstorm_admin", "password" => "sandstorm_admin"];
         }
         if (Auth::attempt($credentials)) {
             $settings = Setting::where('user_id', '=', Auth::user()->id)->first();
             Session::put('ui_language', $settings->ui_language);
             return Redirect::route("/");
         }
     }
 }