get() public method

public get ( $url, $data = [], $format = false, $options = [] )
示例#1
0
 public function __construct($xml)
 {
     if (str::is_path($xml)) {
         if (str::is_url($xml)) {
             $r = remote::get($xml);
             $this->data = $r['content'];
         } else {
             $this->data = file_get_contents($xml);
         }
     } else {
         $this->data = $xml;
     }
     if (empty($this->data)) {
         return false;
     }
     unset($xml, $r, $fp);
 }
示例#2
0
 public static function latest_post()
 {
     $cache = kirby()->cache()->get('facebook.latest.post');
     if (!is_null($cache)) {
         return response::json($cache);
     }
     $response = remote::get(url::build(array('scheme' => static::$graph_scheme, 'host' => static::$graph_host, 'fragments' => array(c::get('facebook.id'), 'posts'), 'query' => array('access_token' => c::get('facebook.accesstoken'), 'limit' => 1))));
     if ($response->content) {
         // parse json string
         $data = str::parse($response->content)['data'][0];
         $return = array('text' => $data['message'], 'picture' => $data['picture'], 'timestamp' => strtotime($data['updated_time']), 'time_formatted' => strftime('%d. %B %G', strtotime($data['updated_time'])), 'link' => $data['link']);
         // set cache for 10 minutes
         kirby()->cache()->set('facebook.latest.post', $return, 10);
     } else {
         return false;
     }
     return $return;
 }
示例#3
0
 /**
  * Parses a remote feed into an array.
  *
  * @param   string   remote feed URL
  * @param   integer  item limit to fetch
  * @return  array
  */
 public static function parse($feed, $limit = 0)
 {
     // Make limit an integer
     $limit = (int) $limit;
     // Disable error reporting while opening the feed
     $ER = error_reporting(0);
     // Allow loading by filename/url or raw XML string
     if (valid::url($feed)) {
         $feed = remote::get($feed, 45);
         $feed = $feed['content'];
     } elseif (is_file($feed)) {
         $feed = file_get_contents($feed);
     }
     // Double check we have something to work with
     if (empty($feed)) {
         return FALSE;
     }
     // Load the feed
     $feed = simplexml_load_string($feed, 'SimpleXMLElement', LIBXML_NOCDATA);
     // Restore error reporting
     error_reporting($ER);
     // Feed could not be loaded
     if ($feed === NO) {
         return array();
     }
     // Detect the feed type. RSS 1.0/2.0 and Atom 1.0 are supported.
     $feed = isset($feed->channel) ? $feed->xpath('//item') : $feed->entry;
     $i = 0;
     $items = array();
     foreach ($feed as $item) {
         if ($limit > 0 and $i++ === $limit) {
             break;
         }
         $items[] = (array) $item;
     }
     return $items;
 }
示例#4
0
文件: author.php 项目: aizlewood/2016
 public function photo()
 {
     if (!is_null($this->photo)) {
         return $this->photo;
     }
     $extension = f::extension($this->data['photo']);
     $filename = rtrim(sha1($this->url) . '.' . $extension, '.');
     $path = c::get('webmentions.images', 'assets/images/mentions');
     $root = kirby()->roots()->index() . DS . str_replace('/', DS, $path) . DS . $filename;
     $url = kirby()->urls()->index() . '/' . $path . '/' . $filename;
     $photo = new Media($root, $url);
     if (!$photo->exists()) {
         $image = remote::get($this->data['photo']);
         $allowed = array('image/jpeg', 'image/png', 'image/gif');
         f::write($root, $image->content());
         if (!in_array($photo->mime(), $allowed) or $photo->size() == 0) {
             $photo->delete();
         }
     }
     if (!$photo->exists() or !$photo->type() == 'image') {
         $photo = new Obj(array('url' => $this->data['photo'], 'exists' => false));
     }
     return $this->photo = $photo;
 }
示例#5
0
 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;
         }
     }
 }
示例#6
0
 public function optionsFromApi($url)
 {
     $response = remote::get($url);
     $options = @json_decode($response->content(), true);
     return is_array($options) ? $options : array();
 }
示例#7
0
 public function index()
 {
     // Get all active scheduled items
     foreach (ORM::factory('scheduler')->where('scheduler_active', '1')->find_all() as $scheduler) {
         $scheduler_id = $scheduler->id;
         $scheduler_last = $scheduler->scheduler_last;
         // Next run time
         $scheduler_weekday = $scheduler->scheduler_weekday;
         // Day of the week
         $scheduler_day = $scheduler->scheduler_day;
         // Day of the month
         $scheduler_hour = $scheduler->scheduler_hour;
         // Hour
         $scheduler_minute = $scheduler->scheduler_minute;
         // Minute
         // Controller that performs action
         $scheduler_controller = $scheduler->scheduler_controller;
         if ($scheduler_day <= -1) {
             // Ran every day?
             $scheduler_day = "*";
         }
         if ($scheduler_weekday <= -1) {
             // Ran every day?
             $scheduler_weekday = "*";
         }
         if ($scheduler_hour <= -1) {
             // Ran every hour?
             $scheduler_hour = "*";
         }
         if ($scheduler_minute <= -1) {
             // Ran every minute?
             $scheduler_minute = "*";
         }
         $scheduler_cron = $scheduler_minute . " " . $scheduler_hour . " " . $scheduler_day . " * " . $scheduler_weekday;
         //Start new cron parser instance
         $cron = new CronParser($scheduler_cron);
         $lastRan = $cron->getLastRan();
         //Array (0=minute, 1=hour, 2=dayOfMonth, 3=month, 4=week, 5=year)
         $cronRan = mktime($lastRan[1], $lastRan[0], 0, $lastRan[3], $lastRan[2], $lastRan[5]);
         if (!($scheduler_last > $cronRan - 45) || $scheduler_last == 0) {
             // within 45 secs of cronRan time, so Execute control
             $site_url = url::base();
             $scheduler_status = remote::get($site_url . "scheduler/" . $scheduler_controller);
             //XXX: ToDo Parse $scheduler_status
             // Set last time of last execution
             $schedule_time = time();
             $scheduler->scheduler_last = $schedule_time;
             $scheduler->save();
             // Record Action to Log
             $scheduler_log = new Scheduler_Log_Model();
             $scheduler_log->scheduler_id = $scheduler_id;
             $scheduler_log->scheduler_name = $scheduler->scheduler_name;
             $scheduler_log->scheduler_status = "200";
             $scheduler_log->scheduler_date = $schedule_time;
             $scheduler_log->save();
         }
     }
     //Header("Content-Type: image/gif");
     // Transparent GIF
     //echo base64_decode("R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==");
 }
示例#8
0
 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;
         }
     }
 }
示例#9
0
文件: item.php 项目: Zegnat/library
 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');
             }
         }
     }
 }