/** * Handle an incomming request. */ public static function handle($pageId, $lang) { if (r::data('token') != c::get('slack.verify')) { return response::error('Forbidden', 403); } $history = static::api('channels.history', ['channel' => r::data('channel_id')]); if (!empty($history['error'])) { // Something went wrong ... maybe: $msg = ['channel_not_found' => ':lock: Sorry, but this is a private channel']; $err = $history['error']; return response::json(isset($msg[$err]) ? $msg[$err] : $err); } $messages = $history['messages']; if (!empty(r::data('text'))) { $messages = array_values(array_filter($messages, function ($m) { return stristr($m['text'], r::data('text')); })); } if (empty($messages)) { return response::json(":mag: Sorry, I couldn't find the post you're looking for"); } $m = $messages[0]; $a = @$m['attachments'][0]; $img = @$a['image_url']; if (empty($img)) { $img = @$a['thumb_url']; } if (empty($img)) { return response::json(":warning: I'll only publish posts with images"); } $page = site()->visit($pageId, $lang); $dir = $page->root(); $ext = preg_replace('/.+?(\\.\\w+)($|[#?].*)/', '$1', $img); $file = $dir . DS . $m['ts'] . $ext; // Output success message early because of short slackbot timeouts $msg = ':metal: *' . r::data('text', 'last') . '* post is now live' . ' on <' . $page->url() . '>'; echo $msg; flush(); error_log($msg); $user = static::api('users.info', ['user' => $m['user']]); $meta = ['title' => $a['title'], 'date' => date('d.m.Y', $m['ts']), 'description' => @$a['text'], 'linkurl' => $a['from_url'], 'author' => $user['user']['profile']['real_name'], 'avatar' => $m['user'] . '.jpg', 'comment' => static::format(@$m['text']), 'slack' => '1']; data::write($file . '.txt', $meta, 'kd'); // Download the avatar image $avatar = $dir . DS . $meta['avatar']; static::download($user['user']['profile']['image_72'], $avatar); // Download the image static::download($img, $file); // Response has already been sent return false; }
public function ping() { // check for an existing ping cache $cache = $this->root . DS . 'pings.json'; if (file_exists($cache)) { return; } // The Regular Expression filter $expression = "/(http|https|ftp|ftps)\\:\\/\\/[a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,3}(\\/\\S*)?/"; $triggered = array(); $endpoints = array(); // Check if there is a url in the text if (preg_match_all($expression, (string) $this->page->text(), $urls)) { foreach ($urls[0] as $url) { if (!in_array($url, $triggered)) { if ($endpoint = $this->trigger($url)) { $endpoints[] = $endpoint; } $triggered[] = $url; } } } data::write($cache, $endpoints); }
/** * Update the page with a new set of data * * @param array $data */ public function update($data = array(), $lang = null) { $data = array_merge($this->content()->toArray(), $data); if (!data::write($this->textfile(null, $lang), $data, 'kd')) { throw new Exception('The page could not be updated'); } cache::flush(); $this->reset(); $this->touch(); return true; }
public function update($data = array()) { $data = array_merge((array) $this->meta()->toArray(), $data); foreach ($data as $k => $v) { if (is_null($v)) { unset($data[$k]); } } if (!data::write($this->textfile(), $data, 'kd')) { throw new Exception('The file data could not be saved'); } cache::flush(); return true; }
/** * Update the page with a new set of data * * @param array $data */ public function update($input = array(), $lang = null) { $data = a::update($this->content($lang)->toArray(), $input); if (!data::write($this->textfile(null, $lang), $data, 'kd')) { throw new Exception('The page could not be updated'); } $this->kirby->cache()->flush(); $this->reset(); $this->touch(); return true; }
/** * Delete log entries by visitor id */ protected function clearLog($id) { $data = array_filter($this->logdata(), function ($entry) use($id) { return $entry['id'] !== $id; }); data::write($this->logfile, $data, 'json'); // reset the logdata cache $this->logdata = null; return $this->logdata(); }
static function addContent() { global $page, $pages, $site; $uid = get('uid'); $title = get('title'); $tpl = get('template', 'default'); $data = self::fetchData($tpl, array('title' => $title)); if (empty($title)) { return array('status' => 'error', 'msg' => l::get('pages.add.errors.title')); } // build a urlified uid if (empty($uid)) { $uid = str::urlify($title); } if (empty($uid)) { return array('status' => 'error', 'msg' => l::get('pages.add.errors.url')); } if (!preg_match('!^[a-z0-9-_]+$!i', $uid)) { return array('status' => 'error', 'msg' => l::get('pages.add.errors.characters')); } $check = self::childByUID($uid); if ($check) { return array('status' => 'error', 'msg' => l::get('pages.add.errors.exists') . ': ' . $uid); } if (!$site->uri->path(1)) { $dir = c::get('root.content') . '/' . $uid; $url = url($uid); } else { $dir = $page->root() . '/' . $uid; $url = $page->url() . '/' . $uid; } if (!dir::make($dir)) { return array('status' => 'error', 'msg' => l::get('pages.add.errors.permissions')); } if (c::get('lang.support')) { foreach (c::get('lang.available') as $lang) { $file = $dir . '/' . $tpl . '.' . $lang . '.txt'; $write = data::write($file, $data); if (error($write)) { return $write; } } } else { // write the default file $file = $dir . '/' . $tpl . '.txt'; $write = data::write($file, $data); if (error($write)) { return $write; } } self::killCache(); return array('status' => 'success', 'msg' => l::get('pages.add.success'), 'url' => $url); }
public function store() { // make sure the item is valid before storing it $this->validate(); // make sure the directory is at the right position $this->relocate(); $index = $this->library->index(); $data = $this->toArray(); $file = $this->root() . DS . 'item.yaml'; $columns = $this->library->columns()->pluck('name'); $missing = array_diff(array_keys($data), $columns); // add additional columns foreach ($missing as $column) { $this->library->database->query('alter table items add ' . $column . ' text'); } // store the data in the file data::write($file, $data); // clean the index first $this->library->database->query('delete from items where id = :id', array('id' => $this->id)); // and then re-add $index->insert($data); return $this; }
protected function questions() { $blueprint = ['title' => ucfirst($this->name()), 'pages' => ['template' => true, 'num' => ['mode' => 'default', 'display' => null], 'max' => null, 'limit' => 20, 'sort' => null, 'hide' => false], 'files' => ['sortable' => false, 'max' => null, 'hide' => false, 'sanitize' => true, 'fields' => null], 'fields' => ['title' => ['label' => 'Title', 'type' => 'title']]]; $this->output->writeln(''); $this->output->writeln('<info>General Settings:</info>'); $this->output->writeln(''); $helper = $this->getHelper('question'); $question = new Question('<comment>Please enter a title for the blueprint</comment>' . PHP_EOL . 'leave blank to use "' . ucfirst($this->name()) . '": ', ucfirst($this->name())); $blueprint['title'] = $helper->ask($this->input, $this->output, $question); $this->output->writeln(''); $this->output->writeln('<info>Page Settings:</info>'); $this->output->writeln(''); $question = new ConfirmationQuestion('<comment>Allow subpages (y/n)</comment>' . PHP_EOL . 'leave blank to set "y": ', true); if ($helper->ask($this->input, $this->output, $question)) { // Allowed Templates $this->output->writeln(''); $question = new ChoiceQuestion('<comment>Templates for subpages (i.e. 1,2,3)</comment>' . PHP_EOL . 'leave blank to set "all"' . PHP_EOL, $this->templates(), 0); $question->setMultiselect(true); $templates = $helper->ask($this->input, $this->output, $question); if (count($templates) == 1 and $templates[0] == 'all') { $blueprint['pages']['template'] = null; } else { $blueprint['pages']['template'] = $templates; } // Numbering mode $this->output->writeln(''); $question = new ChoiceQuestion('<comment>Numbering mode for subpages</comment>' . PHP_EOL . 'leave blank to use "default"' . PHP_EOL, ['default', 'date', 'zero'], 0); $question->setErrorMessage('The numbering mode "%s" is invalid.'); $blueprint['pages']['num']['mode'] = $helper->ask($this->input, $this->output, $question); // Sorting $this->output->writeln(''); $question = new Question('<comment>Sort subpages (i.e. title desc)</comment>' . PHP_EOL . 'leave blank to use default: ', false); $blueprint['pages']['sort'] = $helper->ask($this->input, $this->output, $question); // Limit $this->output->writeln(''); $question = new Question('<comment>Number of shown subpages in the sidebar</comment>' . PHP_EOL . 'leave blank to use default: ', 20); $blueprint['pages']['limit'] = $helper->ask($this->input, $this->output, $question, null); // Max subpages $this->output->writeln(''); $question = new Question('<comment>Maximum number of allowed subpages</comment>' . PHP_EOL . 'leave blank to not set a limit: ', null); $blueprint['pages']['max'] = $helper->ask($this->input, $this->output, $question, null); } else { $blueprint['pages'] = false; } $this->output->writeln(''); $this->output->writeln('<info>File Settings:</info>'); $this->output->writeln(''); $question = new ConfirmationQuestion('<comment>Allow files (y/n)</comment>' . PHP_EOL . 'leave blank to set "y": ', true); if ($helper->ask($this->input, $this->output, $question)) { // Sortable files $this->output->writeln(''); $question = new ConfirmationQuestion('<comment>Sortable files (y/n)</comment>' . PHP_EOL . 'leave blank to set "n": ', false); $blueprint['files']['sortable'] = $helper->ask($this->input, $this->output, $question); // Max file size $this->output->writeln(''); $question = new Question('<comment>Maximum allowed file size (in kB)</comment>' . PHP_EOL . 'leave blank to not set a limit: ', null); $blueprint['files']['size'] = $helper->ask($this->input, $this->output, $question, null); // Max files $this->output->writeln(''); $question = new Question('<comment>Maximum number of allowed files</comment>' . PHP_EOL . 'leave blank to not set a limit: ', null); $blueprint['files']['max'] = $helper->ask($this->input, $this->output, $question, null); // Max width $this->output->writeln(''); $question = new Question('<comment>Maximum width of allowed images</comment>' . PHP_EOL . 'leave blank to not set a limit: ', null); $blueprint['files']['width'] = $helper->ask($this->input, $this->output, $question, null); // Max height $this->output->writeln(''); $question = new Question('<comment>Maximum height of allowed images</comment>' . PHP_EOL . 'leave blank to not set a limit: ', null); $blueprint['files']['height'] = $helper->ask($this->input, $this->output, $question, null); } else { $blueprint['files'] = false; } // create the blueprint with all the entered data data::write($this->file(), $blueprint); }