write() static public méthode

Creates a new file
static public write ( string $file, mixed $content, boolean $append = false ) : boolean
$file string The path for the new file
$content mixed Either a string or an array. Arrays will be converted to JSON.
$append boolean true: append the content to an exisiting file if available. false: overwrite.
Résultat boolean
Exemple #1
0
 public static function combine($type, $files, $compress = false)
 {
     $root = panel::instance()->roots()->assets() . DS . $type;
     $cache = new Media($root . DS . 'panel.' . $type);
     $media = new Collection(array_map(function ($file) use($root) {
         return new Media($root . DS . str_replace('/', DS, $file));
     }, $files));
     // get the max modification date
     $modified = max($media->pluck('modified'));
     if (is_writable($root) and (!$cache->exists() or $cache->modified() < $modified)) {
         $cache->remove();
         $content = '';
         foreach ($media as $asset) {
             $content .= $asset->read() . PHP_EOL;
         }
         if ($compress) {
             $content = static::compress($content);
         }
         f::write($root . DS . 'panel.' . $type, $content);
     }
     if ($cache->exists()) {
         return $type(panel()->urls()->{$type}() . '/panel.' . $type . '?v=' . panel()->version());
     }
     return $type(array_map(function ($item) use($type) {
         return 'panel/assets/' . $type . '/' . $item;
     }, $files));
 }
 public function set_cache($type, $result)
 {
     $cache_path = __DIR__ . '/../cache/' . $type . '.json';
     $period = c::get('kg.cache_period') ? c::get('kg.cache_period') : '30 Minutes';
     $cache = array('to' => strtotime($period), 'payload' => $result);
     \f::write($cache_path, json_encode($cache));
 }
Exemple #3
0
 public static function write($file, $data, $type = null)
 {
     // type autodetection
     if (is_null($type)) {
         $type = f::extension($file);
     }
     return f::write($file, data::encode($data, $type));
 }
Exemple #4
0
 protected function copy()
 {
     if ($this->input->getOption('bare')) {
         $blueprint = f::read($this->template());
         $blueprint = str::template($blueprint, ['title' => ucfirst($this->name())]);
         f::write($this->file(), $blueprint);
     } else {
         $this->questions();
     }
 }
Exemple #5
0
 public function testSize()
 {
     dir::make($this->tmpDir);
     f::write($this->tmpDir . DS . 'testfile-1.txt', str::random(5));
     f::write($this->tmpDir . DS . 'testfile-2.txt', str::random(5));
     f::write($this->tmpDir . DS . 'testfile-3.txt', str::random(5));
     $this->assertEquals(15, dir::size($this->tmpDir));
     $this->assertEquals('15 b', dir::niceSize($this->tmpDir));
     dir::remove($this->tmpDir);
 }
Exemple #6
0
 static function set($file, $content, $raw = false)
 {
     if (!c::get('cache')) {
         return false;
     }
     if ($raw == false) {
         $content = @serialize($content);
     }
     if ($content) {
         f::write(self::file($file), $content);
     }
 }
Exemple #7
0
 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');
     }
 }
Exemple #8
0
 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;
 }
 /**
  * Combine and cache all javascript files.
  */
 public function scripts()
 {
     $file = $this->hub()->finder()->cache() . DS . 'wizard.js';
     if (!file_exists($file)) {
         $js = $this->combine(array('js/jquery.js', 'js/jquery.breadcrumb.js', 'js/jquery.sidebar.js'));
         f::write($file, $js);
     }
     header('Content-type: text/javascript');
     echo f::read($file);
     exit;
 }
 /**
  * Saves the file
  *
  * @param string $content
  * @return boolean
  */
 public function save($content = null, $format = null)
 {
     $content = $this->content($content, $format);
     return f::write($this->root, $content);
 }
Exemple #11
0
 protected static function save($file, $data)
 {
     $yaml = '<?php if(!defined(\'KIRBY\')) exit ?>' . PHP_EOL . PHP_EOL;
     $yaml .= data::encode($data, 'yaml');
     if (!f::write($file, $yaml)) {
         throw new Exception('The user account could not be saved');
     } else {
         return true;
     }
 }
Exemple #12
0
 protected function copy()
 {
     f::write($this->_file($this->name() . '.php'), $this->_template('plugin.php'));
     f::write($this->_file('package.json'), $this->_template('package.json'));
     f::write($this->_file('readme.md'), $this->_template('readme.md'));
 }
Exemple #13
0
 /**
  * Creates a new yaml file from an array
  *
  * @param string $file
  * @param array $array
  * @return boolean
  */
 public static function write($file, $array)
 {
     return f::write($file, static::encode($array));
 }
    }
    $output[] = 'title: ' . $post['title'];
    $output[] = 'date: ' . date($dateformat, $post['date']);
    $output[] = 'text: ' . "\n\n" . trim($post['text']);
    $output[] = 'tags: ' . $post['tags'];
    $output[] = 'categories: ' . $post['cats'];
    $name = pad($n, $len) . '-' . f::safe_name($post['slug']);
    $dir = $root . '/' . $name;
    if (is_dir($dir)) {
        $skipped[] = basename($dir);
        continue;
    }
    dir::make($dir);
    $content = implode("\n\n" . '----' . "\n\n", $output);
    $file = $dir . '/' . $template;
    f::write($file, $content);
}
putmessage('Exported ' . $n . ' articles to ' . $root . '<br /><br />');
if (!empty($errors)) {
    putmessage(count($errors) . ' article(s) could not be imported<br /><br />');
}
if (!empty($skipped)) {
    putmessage('The following folders have been skipped, because they already existed:' . a::show($skipped, false));
}
/**
 * IXR - The Inutio XML-RPC Library
 *
 * @package IXR
 * @since 1.5
 *
 * @copyright Incutio Ltd 2002-2005
Exemple #15
0
dir::copy(__DIR__ . DS . 'assets', __DIR__ . DS . 'static' . DS . 'assets');
dir::copy(__DIR__ . DS . 'content', __DIR__ . DS . 'static' . DS . 'content');
// set the timezone for all date functions
date_default_timezone_set($kirby->options['timezone']);
// load all extensions
$kirby->extensions();
// load all plugins
$kirby->plugins();
// load all models
$kirby->models();
// load all language variables
$kirby->localize();
foreach ($site->index() as $page) {
    $site->visit($page->uri());
    $html = $kirby->render($page);
    if ($page->isHomePage()) {
        $root = __DIR__ . DS . 'static' . DS . 'index.html';
    } else {
        $root = __DIR__ . DS . 'static' . DS . $page->uri() . DS . 'index.html';
    }
    f::write($root, $html);
}
?>

Your site has been exported to the <b>static</b> folder.<br />
Copy all sites and folders from there and upload them to your server.<br />
Make sure the main URL is correct: <b><?php 
echo $url;
?>
</b>
Exemple #16
0
 public function testWrite()
 {
     $this->assertTrue(f::write($this->tmpFile, 'my content'));
 }
Exemple #17
0
 static function write($file, $values)
 {
     if (file_exists($file) && !is_writable($file)) {
         return array('status' => 'error', 'msg' => l::get('writer.errors.permissions'));
     }
     $break = false;
     $result = "";
     $keys = array();
     foreach ($values as $k => $v) {
         $k = str::urlify($k);
         $k = str::ucfirst(str_replace('-', '_', $k));
         if (in_array($k, $keys) || empty($k)) {
             continue;
         }
         $keys[] = $k;
         $result .= $break . $k . ': ' . trim($v);
         $break = "\n\n----\n\n";
     }
     @chmod(basename($file), 0666);
     $write = f::write($file, $result);
     if (!$write || !file_exists($file)) {
         return array('status' => 'error', 'msg' => l::get('writer.errors.write'));
     }
     return array('status' => 'success', 'msg' => l::get('writer.success'));
 }
Exemple #18
0
 /**
  * Write an item to the cache for a given number of minutes.
  *
  * <code>
  *    // Put an item in the cache for 15 minutes
  *    Cache::set('value', 'my value', 15);
  * </code>
  *
  * @param  string  $key
  * @param  mixed   $value
  * @param  int     $minutes
  * @return void
  */
 public function set($key, $value, $minutes = null)
 {
     return f::write($this->file($key), serialize($this->value($value, $minutes)));
 }
Exemple #19
0
 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');
             }
         }
     }
 }