public function field($key, $field = null) { if (is_null($field)) { $field = $key; } $value = a::get($this->data, $field); if ($key == 'url' and !v::url($value)) { $value = null; } $this->{$key} = new Field($this->page, $key, esc($value)); }
public function start() { $src = get('source'); $target = get('target'); if (!v::url($src)) { throw new Exception('Invalid source'); } if (!v::url($target)) { throw new Exception('Invalid target'); } if (!str::contains($target, site()->url())) { throw new Exception('Invalid target'); } require_once dirname(__DIR__) . DS . 'vendor' . DS . 'mf2.php'; require_once dirname(__DIR__) . DS . 'vendor' . DS . 'comments.php'; $data = \Mf2\fetch($src); $result = \IndieWeb\comments\parse($data['items'][0], $src); if (empty($result)) { throw new Exception('Probably spam'); } $path = ltrim(str_replace(site()->url(), '', $target), '/'); if (!empty($path) and $page = page($path)) { if (!empty($result['published'])) { $time = strtotime($result['published']); } else { $time = time(); $result['published'] = date('c'); } $json = json_encode($result); $hash = sha1($json); $file = $page->root() . DS . '.webmentions' . DS . $time . '-' . $hash . '.json'; f::write($file, $json); return true; } else { throw new Exception('Invalid page'); } }
/** * Checks for a valid URL * * @param string $url * @return boolean */ static function valid($url) { return v::url($url); }
public function isUrl($url) { return v::url($url) or str::contains($url, '://localhost') or str::contains($url, '://127.0.0.1'); }
public function __construct($field) { $this->field = $field; if (is_array($field->options)) { $this->options = $field->options; } else { if (v::url($field->options)) { $response = remote::get($field->options); $options = @json_decode($response->content(), true); if (is_array($options)) { $this->options = $options; } else { $this->options = array(); } } else { if (!$field->page) { $this->options = array(); } else { if ($field->options == 'query') { $defaults = array('page' => $field->page->id(), 'fetch' => 'children', 'value' => '{{uid}}', 'text' => '{{title}}', 'flip' => false, 'template' => false); $query = array_merge($defaults, $field->query); // dynamic page option // ../ // ../../ etc. if (str::startsWith($query['page'], '../')) { $currentPage = $field->page; $path = $query['page']; while (str::startsWith($path, '../')) { if ($parent = $currentPage->parent()) { $currentPage = $parent; } else { break; } $path = str::substr($path, 3); } $page = $currentPage; } else { $page = page($query['page']); } $items = $this->items($page, $query['fetch']); if ($query['template']) { $items = $items->filter(function ($item) use($query) { return in_array(str::lower($item->intendedTemplate()), array_map('str::lower', (array) $query['template'])); }); } if ($query['flip']) { $items = $items->flip(); } foreach ($items as $item) { $value = $this->tpl($query['value'], $item); $text = $this->tpl($query['text'], $item); $this->options[$value] = $text; } } else { if ($items = $this->items($field->page, $field->options)) { foreach ($items as $item) { if (is_a($item, 'Page')) { $this->options[$item->uid()] = (string) $item->title(); } else { if (is_a($item, 'File')) { $this->options[$item->filename()] = (string) $item->filename(); } } } } else { $this->options = array(); } } } } } // sorting if (!empty($this->field->sort)) { switch (strtolower($this->field->sort)) { case 'asc': asort($this->options); break; case 'desc': arsort($this->options); break; } } }
public function validate() { return v::url($this->value()); }
static function validate($method, $value) { switch ($method) { case 'date': return v::date($value); break; case 'url': return v::url($value); break; case 'email': return v::email($value); break; default: if (is_array($method)) { $match = null; $minlength = $maxlength = 0; extract($method); if ($match && !preg_match($match, $value)) { return false; } if ($minlength && str::length($value) < $minlength) { return false; } if ($maxlength && str::length($value) > $maxlength) { return false; } } break; } return true; }
public function attach($file, $filename = null) { // if the item has not been stored yet // throw an exception if (!$this->exists()) { throw new Exception('Unstored item'); } // filename fallback if (is_null($filename)) { $filename = basename($file); } // sanitize the filename $filename = f::safeName($filename); // the item.yaml cannot be overwritten if ($filename == 'item.yaml') { throw new Exception('item.yaml is a reserved filename'); } // files cannot be overwritten if (file_exists($this->root() . DS . $filename)) { throw new Exception('The file exists and cannot be overwritten'); } // attach a remote url if (v::url($file)) { $response = remote::get($file); if ($response->code() < 400) { if (!f::write($this->root() . DS . $filename, $response->content())) { throw new Exception('The file could not be saved'); } } else { throw new Exception('The file could not be fetched'); } } else { if (file_exists($file)) { if (!f::copy($file, $this->root() . DS . $filename)) { throw new Exception('The file could not be copied'); } } } }
public function trigger($url) { $response = remote::get($url); $html = $response->content(); if (preg_match_all('!\\<link(.*?)\\>!i', $html, $links)) { foreach ($links[0] as $link) { if (!str::contains($link, 'rel="webmention"')) { continue; } if (!preg_match('!href="(.*?)"!', $link, $match)) { continue; } $endpoint = $match[1]; $src = $this->page->url(); $target = $url; // invalid endpoint if (!v::url($endpoint)) { continue; } remote::post($endpoint, array('data' => array('source' => $src, 'target' => $target))); return $endpoint; } } }