Exemple #1
0
 public function __construct()
 {
     $endpoint = $this;
     if ($page = page('webmention') and kirby()->path() == $page->uri()) {
         if (r::is('post')) {
             try {
                 $endpoint->start();
                 header::status(202);
                 tpl::set('status', 'success');
                 tpl::set('alert', null);
             } catch (Exception $e) {
                 header::status(400);
                 tpl::set('status', 'error');
                 tpl::set('alert', $e->getMessage());
             }
         } else {
             tpl::set('status', 'idle');
         }
     } else {
         kirby()->routes(array(array('pattern' => 'webmention', 'method' => 'GET|POST', 'action' => function () use($endpoint) {
             try {
                 $endpoint->start();
                 echo response::success('Yay', 202);
             } catch (Exception $e) {
                 echo response::error($e->getMessage());
             }
         })));
     }
 }
Exemple #2
0
function run($page)
{
    $templates = c::get('autopublish.templates', array('project', 'item'));
    if (!$templates || in_array($page->template(), $templates)) {
        try {
            $page->toggle('last');
        } catch (Exception $e) {
            return response::error($e->getMessage());
        }
    }
}
Exemple #3
0
 public function autocomplete($mode)
 {
     switch ($mode) {
         case 'usernames':
             $result = site()->users()->map(function ($user) {
                 return $user->username();
             })->toArray();
             break;
         case 'emails':
             $result = site()->users()->map(function ($user) {
                 return $user->email();
             })->toArray();
             break;
         case 'uris':
             $result = site()->index()->map(function ($page) {
                 return $page->id();
             })->toArray();
             // sort results alphabetically
             sort($result);
             break;
         case 'field':
             $index = get('index', 'siblings');
             // siblings, children, template, all
             $id = get('uri');
             $page = page($id);
             switch ($index) {
                 case 'siblings':
                 case 'children':
                     $pages = $page->{$index}();
                     break;
                 case 'template':
                     $template = get('template', $page->template());
                     $pages = site()->index()->filterBy('template', $template);
                     break;
                 case 'pages':
                 case 'all':
                     $pages = site()->index();
                     break;
                 default:
                     if ($page = site()->page($index)) {
                         $pages = $page->children();
                     } else {
                         return response::json(array());
                     }
             }
             $result = $pages->pluck(get('field', 'tags'), get('separator', true), true);
             break;
         default:
             return response::error('Invalid autocomplete method');
     }
     return response::json(array_values($result));
 }
Exemple #4
0
 public function delete($username)
 {
     $user = $this->user($username);
     if (!$user) {
         return response::error(l('users.avatar.delete.error.missing'));
     }
     if ($avatar = $user->avatar()) {
         if (f::remove($avatar->root())) {
             return response::success(l('users.avatar.delete.success'));
         }
     }
     return response::error(l('users.avatar.delete.error'));
 }
Exemple #5
0
 public function delete($username)
 {
     $user = $this->user($username);
     if (!$user) {
         return response::error(l('users.error.missing'));
     }
     try {
         $user->delete();
         return response::success('success');
     } catch (Exception $e) {
         return response::error(l('users.delete.error'));
     }
 }
Exemple #6
0
 /**
  * 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;
 }
function avgimgcolorhook($file)
{
    if (!$file->isImage()) {
        return;
    }
    try {
        if ($file->avgcolor() == "") {
            require_once kirby()->roots()->index() . '/vendor/autoload.php';
            $color = \ColorThief\ColorThief::getColor($file->root(), 100);
            $file->update(['avgcolor' => rgb2hex($color)]);
        }
    } catch (Exception $e) {
        return response::error($e->getMessage());
    }
}
Exemple #8
0
function autothumbhook($file)
{
    if (!c::get('autothumb.enabled', false) || !$file->isImage()) {
        return;
    }
    try {
        $height = c::get('autothumb.height', 500);
        $quality = c::get('autothumb.quality', 70);
        $dimensions = clone $file->dimensions();
        $dimensions->fitHeight($height);
        $thumb = thumb($file, ['width' => $dimensions->width(), 'height' => $dimensions->height(), 'quality' => $quality]);
        echo $thumb->root();
    } catch (Exception $e) {
        return response::error($e->getMessage());
    }
}
Exemple #9
0
 public function delete($username)
 {
     $user = $this->user($username);
     if (!$user) {
         return response::error(l('users.avatar.delete.error.missing'));
     }
     if (!site()->user()->isAdmin() and !$user->isCurrent()) {
         return response::error('You are not allowed to delete the avatar of this user');
     }
     if ($avatar = $user->avatar()) {
         if (f::remove($avatar->root())) {
             return response::success(l('users.avatar.delete.success'));
         }
     }
     return response::error(l('users.avatar.delete.error'));
 }
Exemple #10
0
 public function delete($username)
 {
     $user = $this->user($username);
     if (!$user) {
         return response::error(l('users.error.missing'));
     }
     if (!site()->user()->isAdmin() and !$user->isCurrent()) {
         return response::error('You are not allowed to delete this user');
     }
     try {
         $user->delete();
         return response::success('success');
     } catch (Exception $e) {
         return response::error(l('users.delete.error'));
     }
 }
Exemple #11
0
 public function login()
 {
     $user = app::$site->users()->find(str::lower(get('username')));
     $message = l('login.error');
     if (!$user) {
         return response::error($message);
     }
     try {
         if (!$user->login(get('password'))) {
             throw new Exception($message);
         }
         return response::success(l('login.success'));
     } catch (Exception $e) {
         return response::error($e->getMessage());
     }
 }
Exemple #12
0
 public function thumb($id, $filename)
 {
     $page = $this->page($id);
     $file = $this->file($page, $filename);
     $width = intval(get('width'));
     $height = intval(get('height'));
     if (!$file->canHavePreview()) {
         return response::error('No preview available', 404);
     }
     if (!$file->canHaveThumb()) {
         go($file->url());
     }
     if (get('crop') == true) {
         $thumb = $file->crop($width, $height, 80);
     } else {
         $thumb = $file->resize($width, $height, 80);
     }
     go($thumb->url());
 }
Exemple #13
0
 public static function error($message, $type, $file, $line)
 {
     // remove everything that has been rendered so far
     if (ob_get_level()) {
         ob_end_clean();
     }
     if (class_exists('kirby') and !is_null(kirby::$instance)) {
         $kirby = kirby::$instance;
     } else {
         $kirby = null;
     }
     if (r::ajax()) {
         if (terror::debug()) {
             echo response::error($message, 400, array('type' => $type, 'file' => $file, 'line' => $line));
         } else {
             echo response::error('Unexpected error', 400);
         }
     } else {
         header::status(400);
         static::view($message, $type, $file, $line, $kirby);
     }
     die;
 }
Exemple #14
0
    try {
        // Make sure we're on a product page
        if ($page->template() !== 'product') {
            return true;
        }
        // Numeric stock, price and sale price
        $variants = $page->variants()->yaml();
        foreach ($variants as $key => $variant) {
            if (!is_numeric($variant['price'])) {
                $variants[$key]['price'] = preg_replace('/[^0-9.]/', '', $variant['price']);
            }
            if (!is_numeric($variant['sale_price'])) {
                $variants[$key]['sale_price'] = preg_replace('/[^0-9.]/', '', $variant['sale_price']);
            }
            if (!is_numeric($variant['stock'])) {
                $variants[$key]['stock'] = preg_replace('/[^0-9.]/', '', $variant['stock']);
            }
        }
        // Numeric tax rates
        $taxes = $page->tax()->yaml();
        foreach ($taxes as $key => $tax) {
            if (!is_numeric($tax['rate'])) {
                $taxes[$key]['rate'] = preg_replace('/[^0-9.]/', '', $tax['rate']);
            }
        }
        // Save changes
        $page->update(['variants' => yaml::encode($variants), 'tax' => yaml::encode($taxes)]);
    } catch (Exception $e) {
        return response::error($e->getMessage());
    }
});
Exemple #15
0
 public static function set_error($code = 1, $msg = '')
 {
     self::$error = self::code_to_msg($code, $msg);
     return false;
 }
     $username = $data->username;
     $password = $data->password;
     $query = "SELECT * FROM users WHERE (username = '******' OR email = '{$username}') AND password=MD5('{$password}')";
     if ($result = $db->query($query)) {
         if ($user = $result->fetch_object()) {
             $response->user = $user;
             $response->success('Login successful');
             $token = bin2hex(openssl_random_pseudo_bytes(16));
             $query = "UPDATE users set login_token = '{$token}' where id={$user->id}";
             if ($db->query($query)) {
                 $response->user->login_token = $token;
             }
         } else {
             $query = "SELECT * FROM users WHERE username = '******' OR email = '{$username}'";
             if ($user = $db->query($query)->fetch_object()) {
                 $response->error('Invalid password');
             } else {
                 $response->error('Invalid username');
             }
         }
     }
     break;
 case 'token':
     $token = $data->token;
     $query = "SELECT * FROM users WHERE login_token='{$token}'";
     $result = $db->query($query);
     if ($user = $result->fetch_object()) {
         $response->user = $user;
     }
 case 'logout':
     $userid = $data->userid;
Exemple #17
0
 public function index()
 {
     return response::error('Invalid API method');
 }
Exemple #18
0
 public function delete($id)
 {
     $filename = get('filename');
     $file = $this->file($id, $filename);
     if (!$file) {
         return response::error(l('files.error.missing.file'));
     }
     try {
         $file->delete();
         return response::success('success');
     } catch (Exception $e) {
         return response::error($e->getMessage());
     }
 }
$postdata = file_get_contents("php://input");
$data = json_decode($postdata)->data;
$action = $data->action;
$response = new response();
switch ($action) {
    case 'load':
        $owner = $data->owner;
        $owner = 1;
        $query = "SELECT * FROM documents WHERE (owner={$owner}) AND (parent is NULL)";
        $response->documents = array();
        if ($result = $db->query($query)) {
            while ($doc = $result->fetch_object()) {
                $response->documents[] = $doc;
            }
        } else {
            $response->error('unable to load library');
        }
        break;
    case 'create':
        $title = $data->title;
        $owner = $data->owner;
        $owner = 1;
        $query = "select MAX(sort) from documents where (parent is NULL) AND (owner={$owner})";
        $result = mysqli_fetch_assoc($db->query($query));
        $sort = $result['MAX(sort)'] == null ? 0 : intval($result['MAX(sort)']) + 1;
        $query = "INSERT INTO documents (title, parent, sort, owner, status) VALUES ('{$title}', null, {$sort}, {$owner}, 1)";
        if ($db->query($query)) {
            $response->success('Added document: "' . $title . '"');
        } else {
            $response->error('Could not add document');
        }
<?php

require 'config.php';
//GET DATA FROM ANGULAR $HTTP
$postdata = file_get_contents("php://input");
$data = json_decode($postdata)->data;
//$data = json_decode('{"data":{"action":"load","owner":1,"root":"1"}}')->data;
$action = $data->action;
$response = new response();
switch ($action) {
    case 'load':
        $owner = intval($data->owner);
        $root = intval($data->root);
        if ($response->parts = getChildren($root, $owner)) {
        } else {
            $response->error('Could not load Parts');
        }
        break;
    default:
        break;
}
echo json_encode($response);
//FINISHED
?>


<?php 
//FUNCTIONS & CLASSES
function getChildren($parent, $owner)
{
    global $db;
 public function url($id)
 {
     $page = $this->page($id);
     if (!$page) {
         return response::error(l('pages.error.missing'));
     }
     // avoid url changes for the home and error pages
     if ($page->isErrorPage() or $page->isHomePage()) {
         return response::error('This page type\'s url cannot be changed');
     }
     $changes = PageStore::fetch($page);
     PageStore::discard($page);
     try {
         if (site()->multilang() and site()->language()->code() != site()->defaultLanguage()->code()) {
             $page->update(array('URL-Key' => get('uid')));
         } else {
             $page->move(get('uid'));
         }
         PageStore::update($page, $changes);
         // hit the hook
         kirby()->trigger('panel.page.move', $page);
         return response::success('success', array('uid' => $page->uid(), 'uri' => $page->id()));
     } catch (Exception $e) {
         return response::error($e->getMessage());
     }
 }
Exemple #22
0
 public function url($id)
 {
     $page = $this->page($id);
     if (!$page) {
         return response::error(l('pages.error.missing'));
     }
     try {
         if (site()->multilang() and app::$language != site()->defaultLanguage()->code()) {
             $page->update(array('URL-Key' => get('uid')));
         } else {
             $page->move(get('uid'));
         }
         return response::success('success', array('uid' => $page->uid(), 'uri' => $page->id()));
     } catch (Exception $e) {
         return response::error($e->getMessage());
     }
 }
/**
 * Write values
 */
function contentlist_save_list_values($page, $contentlist)
{
    $contentlist = yaml::encode($contentlist);
    try {
        $page->update(array('contentlist' => $contentlist));
    } catch (Exception $e) {
        return response::error($e->getMessage());
    }
}
Exemple #24
0
 public function url($id)
 {
     $page = $this->page($id);
     if (!$page) {
         return response::error(l('pages.error.missing'));
     }
     // avoid url changes for the home and error pages
     if ($page->isErrorPage() or $page->isHomePage()) {
         return response::error('This page type\'s url cannot be changed');
     }
     try {
         if (site()->multilang() and site()->language()->code() != site()->defaultLanguage()->code()) {
             $page->update(array('URL-Key' => get('uid')));
         } else {
             $page->move(get('uid'));
         }
         return response::success('success', array('uid' => $page->uid(), 'uri' => $page->id()));
     } catch (Exception $e) {
         return response::error($e->getMessage());
     }
 }