/** * Store a newly created resource in storage. * * @return Response */ public function store() { $rules = array('private' => 'numeric|required', 'title' => 'max:46|required', 'paste' => 'required', 'expire' => 'required|numeric', 'private' => 'required|numeric', 'tags' => 'max:6|alpha'); $validator = Validator::make(Input::all(), $rules); if ($validator->fails()) { $messages = $validator->messages(); return View::make('paste.form')->withErrors($messages); } $new_paste = new Paste(); $new_paste->title = Input::get('title'); $new_paste->token = Str::random(40); $new_paste->delete_token = Str::random(40); $new_paste->paste = Input::get('paste'); $new_paste->private = Input::get('private'); date_default_timezone_set('UTC'); $expire_time = date('Y-m-d H:i:s', strtotime(sprintf('now + %s minutes', Input::get('expire')))); $new_paste->expire = $expire_time; if (!$new_paste->save()) { Debugbar::error('Saving failed!'); } // Check if tags are set if (Input::has('hidden-tags')) { $tags = explode(' ', Input::get('hidden-tags')); foreach ($tags as $key => $tag) { $tag_model = new Tag(); $tag_model->tag = $tag; $tag_model->paste_id = $new_paste->id; $new_paste->tags()->save($tag_model); } } if ($new_paste->id) { return Redirect::route('paste.show', $new_paste->token)->withCookie(Cookie::make('edittoken', $new_paste->token, 30)); } return view::make('paste.form', array('page_title' => 'Create a paste')); }
/** * Run the database seeds. * * @return void */ public function run() { Eloquent::unguard(); DB::table('tags')->truncate(); //DB::table('pastes')->truncate(); $faker = Faker\Factory::create(); $paste_count = 10; $tags = array('php', 'javascript', 'ruby', 'js', 'cpp', 'c++', 'c#', 'go', 'html', 'css'); for ($i = 0; $i < $paste_count; $i++) { $tags_per_paste = rand(1, 3); // Generate the paste $examplePaste = new Paste(); $examplePaste->paste = $faker->paragraph; $examplePaste->title = $faker->realText(46); $examplePaste->expire = $faker->dateTime($max = 'now'); $examplePaste->token = Str::random(40); $examplePaste->private = rand(0, 1); $examplePaste->delete_token = Str::random(40); $examplePaste->save(); // Attach some tags to the new paste for ($i = 0; $i < $tags_per_paste; ++$i) { $exampleTag = new Tag(); $exampleTag->tag = $tags[rand(0, sizeof($tags) - 1)]; $exampleTag->paste_id = $examplePaste->id; $examplePaste->tags()->save($exampleTag); } print "Seeded paste with ID of " . $examplePaste->id . "\n"; } }
public function indexAction() { // No view needed since this is all backend stuff. $this->view->disable(); // Generate random ids until we find one not in use. // This will cause one additional SQL query at minimum when creating a paste. do { $id = Text::random(Text::RANDOM_ALNUM, rand(5, 13)); } while (Paste::findFirstByid($id)); $paste = new Paste(); $paste->id = $id; $paste->content = rtrim($this->request->getPost("content")); $paste->lang = $this->request->getPost("lang") == null ? "auto" : $this->request->getPost("lang"); // No sanitisation needed if we accept anything at all to mean true and nothing to mean false. // Also addresses http://stackoverflow.com/a/14067312 $paste->private = $this->request->getPost("private") == null ? 0 : 1; $paste->owner_addr = $this->request->getClientAddress(); $paste->size_bytes = strlen($paste->content); if (!$paste->save()) { foreach ($paste->getMessages() as $message) { $this->flash->error($message->getMessage()); } return $this->response->redirect(); } return $this->response->redirect($this->url->get("v/{$id}")); }
$boundary = $out[1]; } else { if (empty($ffrom) && preg_match("#^From: (.+)#", $line, $out)) { $ffrom = $out[1]; } else { if (empty($subject) && preg_match("#^Subject: (.+)#", $line, $out)) { $subject = $out[1]; } } } } } } // Extract username instead of email adress if it exists if (preg_match("#([^<]+) <#ui", $ffrom, $out)) { $from = $out[1]; } else { $from = $ffrom; } // Create the paste $paste = new Paste(); $paste->title = $subject; $paste->author = $from; $paste->date = time(); $paste->content = utf8_encode(trim($cnt[max(0, $i - 1)])); // Save the paste and give read right to all users (if mail user is different from php one) $link = $paste->save(); chmod(Paste::get_path($paste->filename), 0644); // Send confirmation email $headers = 'From: paste@p0m.fr' . "\r\n" . 'Content-Type: text/plain; charset="utf-8"' . "\r\n" . 'X-Mailer: ' . ucfirst(HTTP_URL); mail($ffrom, "Re: " . $subject, "Bonjour,\n\nVotre paste a bien été publié à l'adresse suivante :\nhttp://" . HTTP_URL . "/?" . $link . "\n\n-- \n" . HTTP_URL, $headers);
/** * Save the current paste */ function save($filename = NULL) { $this->hash = base64_encode(sha1($this->content, true)); if (empty($filename)) { $i = 0; do { $filename = substr(str_replace("+", "", str_replace("/", "", $this->hash)), $i++, NB_CHAR); } while (is_file(Paste::get_path($filename)) && Paste::speed_cmp(Paste::get_path($filename), $this->hash)); } $this->filename = $filename; $xml = new DOMDocument('1.0', 'UTF-8'); $xml->formatOutput = true; $xml_paste = $xml->createElement("paste"); $xml_paste->appendChild($xml->createElement("title", $this->title)); $xml_paste->appendChild($xml->createElement("author", $this->author)); $xml_paste->appendChild($xml->createElement("language", $this->language)); $xml_paste->appendChild($xml->createElement("date", $this->date)); $xml_paste->appendChild($xml->createElement("ip", $this->ip)); $cnt = $xml->createElement("content"); $cnt->appendChild($xml->createCDATASection($this->content)); $xml_paste->appendChild($cnt); if (!empty($this->crypt)) { $xml_paste->appendChild($xml->createElement("crypt", base64_encode($this->crypt))); } if (!empty($this->ref)) { //Also indicate in the parent file $parent = new Paste($this->ref); //Does the parent exist? if ($parent->load()) { $xml_paste->appendChild($xml->createElement("ref", $this->ref)); if ($parent->add_answer($this->filename)) { $parent->save(); } } } foreach ($this->answers as $a) { $xml_paste->appendChild($xml->createElement("answer", $a)); } $xml_paste->appendChild($xml->createElement("hash", $this->hash)); if ($this->is_private()) { $xml_paste->appendChild($xml->createElement("private")); } $xml->appendChild($xml_paste); if ($xml->save(Paste::get_path($this->filename))) { return $this->filename; } else { die("Sorry, an error occured while saving the file. Please try again later."); return FALSE; } }
<?php require_once "../common.php"; if (!empty($_POST["content"])) { if (!isset($_POST["title"])) { $_POST["title"] = ""; } if (!isset($_POST["author"])) { $_POST["author"] = ""; } if (!isset($_POST["lang"])) { $_POST["lang"] = ""; } if (!isset($_POST["hide"])) { $_POST["hide"] = 0; } $paste = new Paste(); $paste->create($_POST); header("Location: /?" . $paste->save()); exit; } header("Location: /");
/** * Creates a new paste with the data supplied * * @static * @param string $source * @param array $data * @return Illuminate\Database\Eloquent\Model */ public static function createNew($source, $data) { // Get the site's configuration $site = Site::config('general'); // Set the paste protected flag $protected = !empty($data['password']); // Set the private paste flag $private = !empty($data['private']); // We use an alphanumeric URL key to identify pastes // This is done so that users do not have access to the // actual primary key in the database and therefore, cannot // mass download all data $urlkey = static::makeUrlKey(); // This hash is used for identifying private pastes // Unless being opened by the paste author, sticky notes // makes passing this hass as a part of the URL mandatory // for private pastes $hash = static::getHash(); // Encrypt the password with a salt $password = ''; $salt = str_random(5); if (!empty($data['password'])) { $password = PHPass::make()->create($data['password'], $salt); } // Set the paste visibility based on the site's config switch ($site->pasteVisibility) { case 'public': $protected = $private = FALSE; $password = ''; break; case 'private': $private = TRUE; break; } // Set the paste author if (Auth::check()) { $user = Auth::user(); $authorId = $user->id; $author = $user->username; } else { $authorId = 0; $author = NULL; } // Set the paste expiration time default if (!isset($data['expire']) or $data['expire'] < 0) { $data['expire'] = $site->pasteAge; } // Check if we have an attachment if ($site->allowAttachment and isset($data['attachment']) and is_array($data['attachment'])) { $attachment = empty($data['attachment'][0]) ? 0 : 1; } else { $attachment = 0; } // Set up the new paste $paste = new Paste(); $paste->project = empty($data['project']) ? NULL : $data['project']; $paste->title = empty($data['title']) ? NULL : $data['title']; $paste->data = $data['data']; $paste->language = $data['language']; $paste->private = ($protected or $private) ? 1 : 0; $paste->password = $password; $paste->salt = $salt; $paste->hash = $hash; $paste->urlkey = $urlkey; $paste->author = $author; $paste->author_id = $authorId; $paste->timestamp = time(); $paste->expire = $data['expire'] > 0 ? time() + $data['expire'] : 0; $paste->ip = Request::getClientIp(); $paste->attachment = $attachment; $paste->hits = 0; $paste->flagged = 0; $paste->save(); // Insert paste count to the statistics table $stat = Statistics::firstOrNew(array('date' => date('Y-m-d'))); $stat->{$source}++; $stat->save(); // Return the created paste return $paste; }