Example #1
0
 public function get($url)
 {
     $url .= '/token:' . $this->token;
     $cache = Shutter::get_cache('api/' . $url);
     if ($cache) {
         $data = json_decode($cache['data'], true);
     } else {
         $headers = array('Connection: Keep-Alive', 'Keep-Alive: 2', 'Cache-Control: must-revalidate');
         if (LOOPBACK_HOST_HEADER) {
             $host = $_SERVER['SERVER_ADDR'] . ':' . $_SERVER['SERVER_PORT'];
             $headers[] = 'Host: ' . $_SERVER['HTTP_HOST'];
         } else {
             $host = $_SERVER['HTTP_HOST'];
         }
         $url = $this->protocol . '://' . $host . preg_replace('~/(app/site/site|(api|i))\\.php.*~', "/api.php?{$url}", $_SERVER['SCRIPT_NAME']);
         curl_setopt($this->curl, CURLOPT_URL, $url);
         curl_setopt($this->curl, CURLOPT_HEADER, 0);
         curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($this->curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1944.0 Safari/537.36');
         curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 5);
         curl_setopt($this->curl, CURLOPT_TIMEOUT, 10);
         curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
         if ($this->protocol === 'https') {
             curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 0);
             curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
         }
         $data = json_decode(curl_exec($this->curl), true);
     }
     return $data;
 }
Example #2
0
 function index()
 {
     list($params, $id) = $this->parse_params(func_get_args());
     $t = new Theme();
     $final = $t->read();
     $final = Shutter::filter('api.themes', array($final));
     $this->set_response_data($final);
 }
Example #3
0
 /**
  * Site url hook for newer koken versions
  */
 public function siteUrlHook()
 {
     if ($this->isSitemapUrl()) {
         if ($cache = Shutter::get_cache($this->getCachePath())) {
             header('Content-type: text/xml; charset=utf-8');
             echo $cache['data'];
             exit;
         } else {
             Koken::$cache_path = $this->getCachePath();
             $xmlSitemap = $this->buildXmlSitemap();
             $this->outputXmlSitemapAndExit($xmlSitemap);
         }
     }
 }
Example #4
0
 function reset_password($id = false)
 {
     $koken_url_info = $this->config->item('koken_url_info');
     $this->load->library('email');
     if (isset($_POST['email']) && !empty($_POST['email'])) {
         $user = $_POST['email'];
         $u = new User();
         $u->where('email', $user)->get();
         if ($u->exists()) {
             $subject = 'Koken: Password reset requested';
             $message = "Hi there -\n\nSomeone (hopefully you!) just requested that the password to your Koken installation at {$koken_url_info->base} be reset. If you did not request a password reset, ignore this email and your password will stay the same. If you do need your password reset, click the link below.\n\n{$koken_url_info->base}api.php?/users/reset_password/{$u->internal_id}\n\n- Koken";
             Shutter::email($u->email, 'Koken', $u->email, $subject, $message);
             $this->set_response_data(array('success' => true));
         } else {
             $this->error('404', 'User not found.');
             return;
         }
     } else {
         if ($id) {
             $u = new User();
             $u->where('internal_id', $id)->get();
             if ($u->exists()) {
                 $new = substr(koken_rand(), 0, 8);
                 $u->password = $new;
                 $u->save();
                 $subject = 'Koken: Your password has been reset';
                 $message = "Your Koken password has been successfully reset.\n\nYour new password: {$new}\n\n- Koken";
                 Shutter::email($u->email, 'Koken', $u->email, $subject, $message);
                 header("Location: {$koken_url_info->base}admin/#/reset");
                 exit;
             } else {
                 $this->error('404', 'User not found.');
                 return;
             }
         } else {
             $this->error('400', 'Bad request');
             return;
         }
     }
 }
Example #5
0
 function save_data($plugins, $data)
 {
     $plugin = $this->_get_plugin($plugins);
     if ($plugin && isset($plugin['data'])) {
         $save_data = array();
         global $raw_input_data;
         foreach ($data as $name => $val) {
             if (isset($plugin['data'][$name])) {
                 $info = $plugin['data'][$name];
                 if ($info['type'] === 'boolean') {
                     $save_data[$name] = $val == 'true';
                 } else {
                     if ($info['type'] === 'text' && isset($raw_input_data[$name])) {
                         $save_data[$name] = $raw_input_data[$name];
                     } else {
                         $save_data[$name] = $val;
                     }
                 }
             }
         }
         Shutter::call_method($plugin['php_class_name'], 'set_data', (object) $save_data);
         $this->data = serialize((array) Shutter::call_method($plugin['php_class_name'], 'get_data'));
     }
 }
Example #6
0
    exit('No direct script access allowed');
}
if (isset($_SERVER['HTTP_HOST'])) {
    $__protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
    $__full = $__protocol . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $__base = array_shift(explode('api.php', $__full));
    $__rel = str_replace($__protocol . '://' . $_SERVER['HTTP_HOST'], '', $__full);
    $__obj = new stdClass();
    $__obj->full = $__full;
    $__obj->base = $__base;
    $__obj->relative = $__rel;
    $config['koken_url_info'] = $__obj;
} else {
    $config['koken_url_info'] = 'unknown';
}
$key = Shutter::get_encryption_key();
if ($key) {
    $config['encryption_key'] = $key;
}
if (!defined('MAGICK_PATH')) {
    define('MAGICK_PATH_FINAL', 'convert');
} else {
    if (strpos(strtolower(MAGICK_PATH), 'c:\\') !== false) {
        define('MAGICK_PATH_FINAL', '"' . MAGICK_PATH . '"');
    } else {
        define('MAGICK_PATH_FINAL', MAGICK_PATH);
    }
}
if (!defined('FFMPEG_PATH')) {
    define('FFMPEG_PATH_FINAL', 'ffmpeg');
} else {
Example #7
0
 function migrate($n = false)
 {
     if ($this->method !== 'post') {
         $this->error('403', 'Forbidden');
         return;
     }
     $CI =& get_instance();
     $this->db =& $CI->db;
     $db_config = Shutter::get_db_configuration();
     $this->load->dbforge();
     if ($n === 'schema') {
         require FCPATH . 'app' . DIRECTORY_SEPARATOR . 'koken' . DIRECTORY_SEPARATOR . 'schema.php';
         foreach ($koken_tables as $table_name => $info) {
             $table = $db_config['prefix'] . "{$table_name}";
             if ($this->db->table_exists($table)) {
                 $existing_fields = array();
                 foreach ($this->db->field_data($table) as $field) {
                     $existing_fields[$field->name] = $field;
                 }
                 foreach ($info['fields'] as $field_name => $field_info) {
                     if (array_key_exists($field_name, $existing_fields)) {
                         $field_info['type'] = strtolower($field_info['type']);
                         $compare = (array) $existing_fields[$field_name];
                         unset($compare['name']);
                         unset($compare['primary_key']);
                         if (isset($compare['max_length'])) {
                             $compare['constraint'] = (int) $compare['max_length'];
                             unset($compare['max_length']);
                         }
                         if (in_array(strtolower($field_info['type']), array('text', 'varchar', 'longtext'))) {
                             $field_info['null'] = true;
                         }
                         $diff = array_diff_assoc($field_info, $compare);
                         if (isset($diff['null']) && $diff['null'] === true && is_null($compare['default']) && $field_info['type'] !== 'text' && $field_info['type'] !== 'varchar') {
                             unset($diff['null']);
                         }
                         if (!empty($diff)) {
                             $this->dbforge->modify_column($table, array($field_name => $field_info));
                         }
                     } else {
                         if (in_array(strtolower($field_info['type']), array('text', 'varchar', 'longtext'))) {
                             $field_info['null'] = true;
                         }
                         $this->dbforge->add_column($table, array($field_name => $field_info));
                     }
                 }
                 if (isset($info['keys'])) {
                     foreach ($info['keys'] as $key) {
                         if (is_array($key)) {
                             $key_name = $this->db->_protect_identifiers(implode('_', $key));
                             $key = $this->db->_protect_identifiers($key);
                         } else {
                             $key_name = $this->db->_protect_identifiers($key);
                             $key = array($key_name);
                         }
                         $sql = "ALTER TABLE {$table} ADD KEY {$key_name} (" . implode(', ', $key) . ")";
                         $this->db->query($sql);
                     }
                 }
                 if (isset($info['uniques'])) {
                     foreach ($info['uniques'] as $key) {
                         $this->db->query("CREATE UNIQUE INDEX {$key} ON {$table} ({$key})");
                     }
                 }
             } else {
                 if (!isset($info['no_id'])) {
                     $this->dbforge->add_field('id');
                 }
                 $this->dbforge->add_field($info['fields']);
                 if (isset($info['keys'])) {
                     foreach ($info['keys'] as $key) {
                         $primary = false;
                         if ($key == 'id') {
                             $primary = true;
                         }
                         $this->dbforge->add_key($key, $primary);
                     }
                 }
                 $this->dbforge->create_table($db_config['prefix'] . "{$table_name}");
                 if (isset($info['uniques'])) {
                     $table = $db_config['prefix'] . "{$table_name}";
                     foreach ($info['uniques'] as $key) {
                         $this->db->query("CREATE UNIQUE INDEX {$key} ON {$table} ({$key})");
                     }
                 }
             }
         }
         $this->_clear_system_caches();
         $s = new Setting();
         $s->where('name', 'uuid')->get();
         if (!$s->exists()) {
             $s = new Setting();
             $s->name = 'uuid';
             $s->value = md5($_SERVER['HTTP_HOST'] . uniqid('', true));
             $s->save();
         }
         $uuid = $s->value;
         $base_folder = trim(preg_replace('/\\/api\\.php(.*)?$/', '', $_SERVER['SCRIPT_NAME']), '/');
         include FCPATH . 'app' . DIRECTORY_SEPARATOR . 'koken' . DIRECTORY_SEPARATOR . 'DarkroomUtils.php';
         $s->where('name', 'image_processing_library')->get();
         $libs = DarkroomUtils::libraries();
         $processing_string = $libs[$s->value]['label'];
         $themes = array('axis' => '86d2f683-9f90-ca3f-d93f-a2e0a9d0a089', 'blueprint' => '1a355994-6217-c7ce-b67a-4241be3feae8', 'boulevard' => 'b30686d9-3490-9abb-1049-fe419a211502', 'chastain' => 'd174e766-5a5f-19eb-d735-5b46ae673a6d', 'elementary' => 'be1cb2d9-ed05-2d81-85b4-23282832eb84', 'madison' => '618e0b9f-fba0-37eb-810a-6d615d0f0e08', 'observatory' => '605ea246-fa37-11f0-f078-d54c8a7cbd3c', 'regale' => 'efde04b6-657d-33b6-767d-67af8ef15e7b', 'repertoire' => 'fa8a5d39-01a5-dfd6-92ff-65a22af5d5ac');
         $themes_dir = FCPATH . 'storage' . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR;
         foreach ($themes as $name => $guid) {
             $dir = $themes_dir . $name;
             $guid_path = $dir . DIRECTORY_SEPARATOR . 'koken.guid';
             $old_guid_path = $dir . DIRECTORY_SEPARATOR . '.guid';
             if (file_exists($old_guid_path)) {
                 rename($old_guid_path, $guid_path);
             } else {
                 if (is_dir($dir) && !file_exists($guid_path)) {
                     file_put_contents($guid_path, $guid);
                 }
             }
         }
         $plugins = array('google-analytics' => 'c4e5bc2b-be8b-3ae7-ccbe-d7e7a1a26136', 'font-loader' => '5b6016ae-9d1a-2336-78c4-63dbb74d39b3', 'koken-spotify' => 'e24a53fc-ac9a-5ab6-5777-237f6dc98496', 'koken-rdio' => '84eb1b9a-ea40-c204-5420-c1af5e1bcbe6', 'koken-html-injector' => '045cb01a-07a6-02b6-a0df-2ae377ce18af', 'koken-pulse-timer' => '6e5cbaa3-9fee-ca89-c989-a7969aa491f3', 'koken-pulse-transition-pack' => '7e958135-8e3e-3b34-5ccd-defe39db9400', 'koken-disqus' => '0a430465-cb52-be7d-a160-94bf73e40c03', 'koken-timeago' => 'bf4ceae8-b2b8-dc16-a439-46a4d915161c');
         $plugins_dir = FCPATH . 'storage' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR;
         foreach ($plugins as $name => $guid) {
             $dir = $plugins_dir . $name;
             $guid_path = $dir . DIRECTORY_SEPARATOR . 'koken.guid';
             if (is_dir($dir) && !file_exists($guid_path)) {
                 file_put_contents($guid_path, $guid);
             }
         }
         $this->load->library('webhostwhois');
         $host = new WebhostWhois(array('useDns' => false));
         if ($host->key === 'unknown' && isset($_SERVER['KOKEN_HOST'])) {
             $host->key = $_SERVER['KOKEN_HOST'];
         }
         $data = array('domain' => $_SERVER['HTTP_HOST'], 'path' => '/' . $base_folder, 'uuid' => $uuid, 'php' => PHP_VERSION, 'version' => KOKEN_VERSION, 'ip' => $_SERVER['SERVER_ADDR'], 'image_processing' => urlencode($processing_string), 'host' => $host->key, 'plugins' => array());
         $s = new Setting();
         $s->where('name', 'site_url')->get();
         if ($s->value !== 'default') {
             $data['published_path'] = $s->value;
         }
         $t = new Theme();
         $themes = $t->read();
         foreach ($themes as $theme) {
             if (isset($theme['koken_store_guid'])) {
                 $data['plugins'][] = array('guid' => $theme['koken_store_guid'], 'version' => $theme['version']);
             }
         }
         $plugins = $this->parse_plugins();
         foreach ($plugins as $plugin) {
             if (isset($plugin['koken_store_guid'])) {
                 $data['plugins'][] = array('guid' => $plugin['koken_store_guid'], 'version' => $plugin['version']);
             }
         }
         if (!isset($_COOKIE['koken_session']) && !isset($_COOKIE['koken_session_ci'])) {
             // Catch upgrades with old auth setup and try to keep them logged in.
             $u = new User();
             $u->get_by_id($this->auth_user_id);
             if ($u->exists()) {
                 $this->load->library('session');
                 $u->create_session($this->session);
             }
         }
         // Session upgrade to CI sessions (0.14)
         if (!isset($_COOKIE['koken_session_ci']) && isset($_COOKIE['koken_session'])) {
             $old_session = unserialize($_COOKIE['koken_session']);
             if ($old_session) {
                 $u = new User();
                 $u->get_by_id($old_session['user']['id']);
                 if ($u->exists()) {
                     $this->load->library('session');
                     $u->create_session($this->session);
                 }
             }
         }
         $curl = curl_init();
         curl_setopt($curl, CURLOPT_URL, KOKEN_STORE_URL . '/register');
         curl_setopt($curl, CURLOPT_POST, 1);
         curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data, '', '&'));
         curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
         curl_setopt($curl, CURLOPT_HEADER, 0);
         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
         $r = curl_exec($curl);
         curl_close($curl);
         die(json_encode(array('done' => true)));
     } else {
         if ($n) {
             $path = $this->migrate_path . "{$n}.php";
             $migrate_setting = new Setting();
             $migrate_setting->where('name', 'last_migration')->get();
             if (is_file($path)) {
                 include $path;
                 $is_done = isset($done);
                 if ($migrate_setting->exists() && $is_done) {
                     $migrate_setting->value = (int) $n;
                     $migrate_setting->save();
                 }
                 die(json_encode(array('done' => $is_done)));
                 exit;
             }
         }
     }
 }
Example #8
0
 function shutdown()
 {
     global $lock;
     Shutter::clear_cache($lock);
 }
Example #9
0
 function _get_tags_for_output($options = array())
 {
     if (isset($options['eager_tags'])) {
         return $options['eager_tags'];
     } else {
         $db_config = Shutter::get_db_configuration();
         $tags = $this->tags->order_by($db_config['prefix'] . 'tags.name ASC')->get_iterated();
         $arr = array();
         foreach ($tags as $tag) {
             $arr[] = $tag->_tag_for_output($this->model);
         }
         return $arr;
     }
 }
<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}
/**
 * Data Mapper Configuration
 *
 * Global configuration settings that apply to all DataMapped models.
 */
$db_config = Shutter::get_db_configuration();
$config['prefix'] = $db_config['prefix'];
$config['join_prefix'] = $db_config['prefix'] . 'join_';
$config['error_prefix'] = '<p>';
$config['error_suffix'] = '</p>';
$config['created_field'] = 'created_on';
$config['updated_field'] = 'modified_on';
$config['local_time'] = FALSE;
$config['unix_timestamp'] = TRUE;
$config['lang_file_format'] = 'model_${model}';
$config['field_label_lang_format'] = '${model}_${field}';
$config['auto_transaction'] = FALSE;
$config['auto_populate_has_many'] = FALSE;
$config['auto_populate_has_one'] = FALSE;
$config['all_array_uses_ids'] = FALSE;
// set to FALSE to use the same DB instance across the board (breaks subqueries)
// Set to any acceptable parameters to $CI->database() to override the default.
$config['db_params'] = '';
// Uncomment to enable the production cache
$config['production_cache'] = 'datamapper/cache';
$config['extensions_path'] = 'datamapper';
Example #11
0
 function index()
 {
     list($params, $id, $slug) = $this->parse_params(func_get_args());
     $params['auth'] = $this->auth;
     // Create or update
     if ($this->method != 'get') {
         $a = new Album();
         switch ($this->method) {
             case 'post':
             case 'put':
                 if ($this->method == 'put') {
                     if (isset($params['order'])) {
                         $this->_order($params['order']);
                         $this->redirect("/albums");
                     } else {
                         if (is_null($id)) {
                             $this->error('403', 'Required parameter "id" not present.');
                             return;
                         }
                     }
                     // Update
                     $a->get_by_id($id);
                     if (!$a->exists()) {
                         $this->error('404', "Album with ID: {$id} not found.");
                         return;
                     }
                     $a->old_created_on = $a->created_on;
                     $a->old_published_on = $a->published_on;
                     $a->old_visibility = $a->visibility;
                     $a->current_slug = $a->slug;
                 } else {
                     if (isset($_POST['from_directory'])) {
                         // Cache this to prevent tag spillage from IPTC
                         $tags_cache = $_POST['tags'];
                         if (is_dir($_POST['from_directory'])) {
                             $_POST['tags'] = '';
                             $this->load->helper('directory', 1);
                             $files = directory_map($_POST['from_directory']);
                             $content_ids = array();
                             foreach ($files as $file) {
                                 $c = new Content();
                                 $file = $_POST['from_directory'] . DIRECTORY_SEPARATOR . $file;
                                 $filename = basename($file);
                                 list($internal_id, $path) = $c->generate_internal_id();
                                 if (file_exists($file)) {
                                     if ($path) {
                                         $path .= $filename;
                                     } else {
                                         $this->error('500', 'Unable to create directory for upload.');
                                         return;
                                     }
                                     copy($file, $path);
                                     $from = array();
                                     $from['filename'] = $filename;
                                     $from['internal_id'] = $internal_id;
                                     $from['file_modified_on'] = time();
                                     $c->from_array($from, array(), true);
                                     $content_ids[] = $c->id;
                                 }
                             }
                         }
                         $_POST['tags'] = $tags_cache;
                     }
                 }
                 // Don't allow these fields to be saved generically
                 $private = array('parent_id', 'left_id', 'right_id');
                 if ($a->exists()) {
                     $private[] = 'album_type';
                 }
                 if (isset($_REQUEST['reset_internal_id']) && $_REQUEST['reset_internal_id'] && $a->exists()) {
                     array_shift($private);
                     $_POST['internal_id'] = koken_rand();
                 } else {
                     $private[] = 'internal_id';
                 }
                 foreach ($private as $p) {
                     unset($_POST[$p]);
                 }
                 if ($a->has_db_permission('lock tables')) {
                     $s = new Slug();
                     $t = new Tag();
                     $c = new Content();
                     $cat = new Category();
                     $this->db->query("LOCK TABLE {$a->table} WRITE, {$c->table} WRITE, {$s->table} WRITE, {$t->table} WRITE, {$cat->table} WRITE, {$a->db_join_prefix}albums_content READ, {$a->db_join_prefix}albums_categories READ, {$a->db_join_prefix}albums_tags READ");
                     $locked = true;
                 } else {
                     $locked = false;
                 }
                 try {
                     $a->from_array($_POST, array(), true);
                 } catch (Exception $e) {
                     $this->error('400', $e->getMessage());
                     return;
                 }
                 if ($locked) {
                     $this->db->query('UNLOCK TABLES');
                 }
                 if (isset($_POST['tags'])) {
                     $a->_format_tags($_POST['tags']);
                 } else {
                     if ($this->method === 'put' && isset($_POST['visibility'])) {
                         $a->_update_tag_counts();
                     }
                 }
                 $arr = $a->to_array();
                 if ($this->method === 'post') {
                     Shutter::hook('album.create', $arr);
                 } else {
                     Shutter::hook('album.update', $arr);
                 }
                 if (isset($content_ids)) {
                     $clean = new Album();
                     $clean = $clean->get_by_id($a->id);
                     $clean->manage_content(join(',', $content_ids), 'post', true);
                 }
                 $this->redirect("/albums/{$a->id}");
                 break;
             case 'delete':
                 if (is_null($id)) {
                     $this->error('403', 'Required parameter "id" not present.');
                     return;
                 } else {
                     $prefix = preg_replace('/albums$/', '', $a->table);
                     if ($id === 'trash') {
                         $id = array();
                         $trash = new Trash();
                         $trash->like('id', 'album-')->select_func('REPLACE', '@id', 'album-', '', 'actual_id')->get_iterated();
                         foreach ($trash as $item) {
                             $id[] = (int) $item->actual_id;
                         }
                     } else {
                         if (is_numeric($id)) {
                             $id = array($id);
                         } else {
                             $id = explode(',', $id);
                         }
                     }
                     $tags = array();
                     // Need to loop individually here, otherwise tree can break down
                     foreach ($id as $album_id) {
                         $al = new Album();
                         $al->get_by_id($album_id);
                         if ($al->exists()) {
                             $tags = array_merge($tags, $al->tags);
                             $this->db->query("DELETE FROM {$prefix}trash WHERE id = 'album-{$al->id}'");
                             if ($al->right_id - $al->left_id > 1) {
                                 $children = new Album();
                                 $subs = $children->where('deleted', $al->deleted)->where('visibility', $al->visibility)->where('left_id >', $al->left_id)->where('right_id <', $al->right_id)->where('level >', $al->level)->get_iterated();
                                 foreach ($subs as $sub_album) {
                                     Shutter::hook('album.delete', $sub_album->to_array());
                                     $sub_album->delete();
                                 }
                             }
                             $s = new Slug();
                             $this->db->query("DELETE FROM {$s->table} WHERE id = 'album.{$al->slug}'");
                             Shutter::hook('album.delete', $al->to_array());
                             $al->delete();
                         }
                     }
                     $al->update_set_counts();
                 }
                 exit;
                 break;
         }
     }
     $a = new Album();
     // No id, so we want a list
     if (is_null($id) && !$slug) {
         $final = $a->listing($params);
     } else {
         $defaults = array('neighbors' => false, 'include_empty_neighbors' => false);
         $options = array_merge($defaults, $params);
         $with_token = false;
         if (is_numeric($id)) {
             $album = $a->where('deleted', 0)->get_by_id($id);
         } else {
             if ($slug) {
                 $album = $a->where('deleted', 0)->group_start()->where('internal_id', $slug)->or_where('slug', $slug)->or_like('old_slug', ',' . $slug . ',', 'both')->group_end()->get();
             } else {
                 $album = $a->where('deleted', 0)->where('internal_id', $id)->get();
             }
             if ($album->exists() && $album->internal_id === (is_null($id) ? $slug : $id)) {
                 $with_token = true;
             }
         }
         if (!$album->exists()) {
             $this->error('404', 'Album not found.');
             return;
         }
         if ($a->exists()) {
             if ($a->visibility > 0 && !$this->auth && !$with_token) {
                 if ($a->visibility > 1) {
                     // Private content should 404, leave no trace, etc.
                     $this->error('404', 'Album not found.');
                 } else {
                     $this->error('403', 'Private content.');
                 }
                 return;
             }
             $final = $album->to_array($params);
             $final['context'] = $album->context($options, $this->auth);
         } else {
             $this->error('404', "Album with ID: {$id} not found.");
             return;
         }
         // TODO: This history stuff won't work here anymore
         // if ($this->method == 'put')
         // {
         // 	$h = new History();
         // 	$h->message = array( 'album:update',  $a->title );
         // 	$h->save();
         // }
         // else if ($this->method == 'post')
         // {
         // 	$h = new History();
         // 	$h->message = array( 'album:create',  $a->title );
         // 	$h->save();
         // }
     }
     $this->set_response_data($final);
 }
Example #12
0
        function go($tmpl, $pass = 1)
        {
            $raw = Koken::parse($tmpl);
            // Fix PHP whitespace issues in koken:loops
            $raw = preg_replace('/\\s+<\\?php\\s+endforeach/', '<?php endforeach', $raw);
            $raw = preg_replace('/<a(.*)>\\s+<\\?php/', '<a$1><?php', $raw);
            $raw = preg_replace('/\\?>\\s+<\\/a>/', '?></a>', $raw);
            if ($pass === 1) {
                global $final_path;
                $is_lightbox = 'false';
                if ($final_path === 'lightbox.lens') {
                    $is_lightbox = 'true';
                }
                // Filters
                $raw = str_replace('<head>', "<head><?php Shutter::hook('after_opening_head', array(array('lightbox' => {$is_lightbox}))); ?>", $raw);
                $raw = str_replace('</head>', "<?php Shutter::hook('before_closing_head', array(array('lightbox' => {$is_lightbox}))); ?></head>", $raw);
                $raw = str_replace('<body>', "<body><?php Shutter::hook('after_opening_body', array(array('lightbox' => {$is_lightbox}))); ?>", $raw);
                $raw = str_replace('</body>', "<?php Shutter::hook('before_closing_body', array(array('lightbox' => {$is_lightbox}))); ?></body>", $raw);
                // die($raw);
                Koken::$location['page_class'] = Koken::$page_class;
                $dynamic_array = array();
                foreach (Koken::$dynamic_location_parts as $key) {
                    $dynamic_array[$key] = Koken::$location[$key];
                }
                unset($dynamic_array['parameters']['__overrides']);
                unset($dynamic_array['parameters']['__overrides_display']);
                $location_json = json_encode($dynamic_array);
                if (Koken::$pjax) {
                    $js = "<script>\$K.location = \$.extend(\$K.location, {$location_json});\$(window).trigger('k-pjax-end');</script>";
                } else {
                    $location = Koken::$location;
                    $site = Koken::$site;
                    $stamp = '?' . KOKEN_VERSION;
                    $generator = 'Koken ' . KOKEN_VERSION;
                    $theme = Koken::$site['theme']['name'] . ' ' . Koken::$site['theme']['version'];
                    $koken_js = Koken::$location['root_folder'] . '/' . (Koken::$draft ? 'preview.php?/' : (Koken::$rewrite ? '' : 'index.php?/')) . 'koken.js' . (Koken::$preview ? '&preview=' . Koken::$preview : '');
                    if (strpos($koken_js, '.php?') === false) {
                        $koken_js .= '?' . Shutter::get_site_scripts_timestamp();
                    }
                    if (Koken::$has_video) {
                        $me = "\n\n\t<link href=\"{$location['real_root_folder']}/app/site/themes/common/css/mediaelement/mediaelementplayer.min.css{$stamp}\" rel=\"stylesheet\">\n";
                    } else {
                        $me = '';
                    }
                    $js = <<<JS
\t<meta name="generator" content="{$generator}" />
\t<meta name="theme" content="{$theme}" />{$me}

\t<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
\t<script>window.jQuery || document.write('<script src="{$location['real_root_folder']}/app/site/themes/common/js/jquery.min.js"><\\/script>')</script>
\t<script src="{$koken_js}"></script>
\t<script>\$K.location = \$.extend(\$K.location, {$location_json});</script>

\t<link rel="alternate" type="application/atom+xml" title="{$site['title']}: All uploads" href="{$location['root']}/feed/content/recent.rss" />
\t<link rel="alternate" type="application/atom+xml" title="{$site['title']}: Essays" href="{$location['root']}/feed/essays/recent.rss" />
\t<link rel="alternate" type="application/atom+xml" title="{$site['title']}: Timeline" href="{$location['root']}/feed/timeline/recent.rss" />
JS;
                }
                if (Koken::$draft && !Koken::$preview && !Koken::$pjax) {
                    $original_url = Koken::$original_url;
                    $js .= <<<JS
<script>

if (parent && parent.\$) {
\tparent.\$(parent.document).trigger('previewready', '{$original_url}');
\t\$(function() { parent.\$(parent.document).trigger('previewdomready'); });

\t\$(document).on('pjax:end pjax:transition:end', function(event) {
\t\tif (event.type === 'pjax:end') {
\t\t\tparent.\$(parent.document).trigger('previewready', location.href);
\t\t}
\t\tparent.\$(parent.document).trigger('previewdomready');
\t});

\t\$(document).on('page:change.console', function() {
\t\tparent.\$(parent.document).trigger('previewready', location.href);
\t\tparent.\$(parent.document).trigger('previewdomready');
\t});
}
if (parent && parent.__koken__) {
\t\$(window).on('keydown', function(e) { parent.__koken__.shortcuts(e); });
\t\$(function() { parent.__koken__.panel(); });
}
</script>

<style type="text/css">
i.k-control-structure { font-style: normal !important; }

\tdiv[data-pulse-group] div.cover {
\t\twidth: 100%;
\t\theight: 100%;
\t\tz-index: 1000;
\t\tborder: 5px solid transparent;
\t\tbox-sizing: border-box;
\t\tposition: absolute;
\t\tbox-shadow: 0 0 20px rgba(0,0,0,0.6);
\t\tdisplay: none;
\t\tpointer-events:none;
\t\ttop: 0;
\t\tleft: 0;
\t}

\tdiv[data-pulse-group]:hover div.cover, div[data-pulse-group] div.cover.active {
\t\tdisplay: block !important;
\t}

\tdiv[data-pulse-group] div.cover.active {
\t\tborder-color: #ff6e00 !important;
\t}

\tdiv[data-pulse-group] div.cover div {
\t\tpointer-events:auto;
\t\twidth: 10%;
\t\theight: 10%;
\t\tmin-width: 28px;
\t\tmin-height: 28px;
\t\tbackground-size: 28px 28px;
\t\tbackground-position:top right;
\t\tbackground-repeat:no-repeat;
\t\tbackground-image: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4NCjwhLS0gR2VuZXJhdG9yOiBBZG9iZSBJbGx1c3RyYXRvciAxOC4wLjAsIFNWRyBFeHBvcnQgUGx1Zy1JbiAuIFNWRyBWZXJzaW9uOiA2LjAwIEJ1aWxkIDApICAtLT4NCjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjEvRFREL3N2ZzExLmR0ZCI+DQo8c3ZnIHZlcnNpb249IjEuMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgeD0iMHB4IiB5PSIwcHgiDQoJIHZpZXdCb3g9IjAgMCAyOCAyOCIgZW5hYmxlLWJhY2tncm91bmQ9Im5ldyAwIDAgMjggMjgiIHhtbDpzcGFjZT0icHJlc2VydmUiPg0KPGcgaWQ9IkxheWVyXzIiPg0KCTxjaXJjbGUgZmlsbD0iIzFFMUUxRSIgY3g9IjE0IiBjeT0iMTQiIHI9IjE0Ii8+DQo8L2c+DQo8ZyBpZD0iTGF5ZXJfMSI+DQoJPGcgaWQ9ImNvZ18xXyI+DQoJCTxwYXRoIGZpbGw9IiNCQkJCQkIiIGQ9Ik0xNCwyMC41Yy0wLjMsMC0wLjYsMC0wLjgtMC4xbC0wLjQtMS43bC0wLjEsMGMtMC40LTAuMS0wLjctMC4zLTEuMS0wLjRsLTAuMSwwTDEwLDE5LjENCgkJCWMtMC41LTAuNC0wLjktMC44LTEuMi0xLjJsMC45LTEuNWwwLTAuMWMtMC4yLTAuMy0wLjMtMC43LTAuNC0xLjFsMC0wLjFsLTEuNy0wLjRjMC0wLjMtMC4xLTAuNi0wLjEtMC44YzAtMC4zLDAtMC42LDAuMS0wLjgNCgkJCWwxLjctMC40bDAtMC4xYzAuMS0wLjQsMC4yLTAuNywwLjQtMS4xbDAtMC4xTDguOSwxMEM5LjIsOS42LDkuNiw5LjIsMTAsOC45bDEuNSwwLjlsMC4xLDBjMC4zLTAuMiwwLjctMC4zLDEuMS0wLjRsMC4xLDANCgkJCWwwLjQtMS43YzAuMywwLDAuNi0wLjEsMC44LTAuMWMwLjMsMCwwLjYsMCwwLjgsMC4xbDAuNCwxLjdsMC4xLDBjMC40LDAuMSwwLjcsMC4yLDEuMSwwLjRsMC4xLDBMMTgsOC45YzAuNSwwLjQsMC45LDAuOCwxLjIsMS4yDQoJCQlsLTAuOSwxLjVsMCwwLjFjMC4yLDAuMywwLjMsMC43LDAuNCwxLjFsMCwwLjFsMS43LDAuNGMwLDAuMywwLjEsMC42LDAuMSwwLjhjMCwwLjMsMCwwLjYtMC4xLDAuOGwtMS43LDAuNGwwLDAuMQ0KCQkJYy0wLjEsMC40LTAuMywwLjctMC40LDEuMWwwLDAuMWwwLjksMS41Yy0wLjQsMC41LTAuOCwwLjktMS4yLDEuMmwtMS41LTAuOWwtMC4xLDBjLTAuMywwLjItMC43LDAuMy0xLjEsMC40bC0wLjEsMGwtMC40LDEuNw0KCQkJQzE0LjUsMjAuNSwxNC4zLDIwLjUsMTQsMjAuNXogTTE0LDExLjZjLTEuMywwLTIuNCwxLjEtMi40LDIuNGMwLDEuMywxLjEsMi40LDIuNCwyLjRjMS4zLDAsMi40LTEuMSwyLjQtMi40DQoJCQlDMTYuNCwxMi43LDE1LjMsMTEuNiwxNCwxMS42eiIvPg0KCTwvZz4NCjwvZz4NCjwvc3ZnPg0K);\t\tposition: absolute;
\t\ttop: 4px;
\t\tright: 4px;
\t\tcursor: pointer;
\t\tz-index: 1001;
\t}

\tdiv[data-pulse-group] div.cover div:hover {
\t\tbackground-image: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4NCjwhLS0gR2VuZXJhdG9yOiBBZG9iZSBJbGx1c3RyYXRvciAxOC4wLjAsIFNWRyBFeHBvcnQgUGx1Zy1JbiAuIFNWRyBWZXJzaW9uOiA2LjAwIEJ1aWxkIDApICAtLT4NCjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjEvRFREL3N2ZzExLmR0ZCI+DQo8c3ZnIHZlcnNpb249IjEuMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgeD0iMHB4IiB5PSIwcHgiDQoJIHZpZXdCb3g9IjAgMCAyOCAyOCIgZW5hYmxlLWJhY2tncm91bmQ9Im5ldyAwIDAgMjggMjgiIHhtbDpzcGFjZT0icHJlc2VydmUiPg0KPGcgaWQ9IkxheWVyXzIiPg0KCTxjaXJjbGUgZmlsbD0iIzFFMUUxRSIgY3g9IjE0IiBjeT0iMTQiIHI9IjE0Ii8+DQo8L2c+DQo8ZyBpZD0iTGF5ZXJfMSI+DQoJPGcgaWQ9ImNvZ18yXyI+DQoJCTxwYXRoIGZpbGw9IiNFRUVFRUUiIGQ9Ik0xNCwyMC41Yy0wLjMsMC0wLjYsMC0wLjgtMC4xbC0wLjQtMS43bC0wLjEsMGMtMC40LTAuMS0wLjctMC4zLTEuMS0wLjRsLTAuMSwwTDEwLDE5LjENCgkJCWMtMC41LTAuNC0wLjktMC44LTEuMi0xLjJsMC45LTEuNWwwLTAuMWMtMC4yLTAuMy0wLjMtMC43LTAuNC0xLjFsMC0wLjFsLTEuNy0wLjRjMC0wLjMtMC4xLTAuNi0wLjEtMC44YzAtMC4zLDAtMC42LDAuMS0wLjgNCgkJCWwxLjctMC40bDAtMC4xYzAuMS0wLjQsMC4yLTAuNywwLjQtMS4xbDAtMC4xTDguOSwxMEM5LjIsOS42LDkuNiw5LjIsMTAsOC45bDEuNSwwLjlsMC4xLDBjMC4zLTAuMiwwLjctMC4zLDEuMS0wLjRsMC4xLDANCgkJCWwwLjQtMS43YzAuMywwLDAuNi0wLjEsMC44LTAuMWMwLjMsMCwwLjYsMCwwLjgsMC4xbDAuNCwxLjdsMC4xLDBjMC40LDAuMSwwLjcsMC4yLDEuMSwwLjRsMC4xLDBMMTgsOC45YzAuNSwwLjQsMC45LDAuOCwxLjIsMS4yDQoJCQlsLTAuOSwxLjVsMCwwLjFjMC4yLDAuMywwLjMsMC43LDAuNCwxLjFsMCwwLjFsMS43LDAuNGMwLDAuMywwLjEsMC42LDAuMSwwLjhjMCwwLjMsMCwwLjYtMC4xLDAuOGwtMS43LDAuNGwwLDAuMQ0KCQkJYy0wLjEsMC40LTAuMywwLjctMC40LDEuMWwwLDAuMWwwLjksMS41Yy0wLjQsMC41LTAuOCwwLjktMS4yLDEuMmwtMS41LTAuOWwtMC4xLDBjLTAuMywwLjItMC43LDAuMy0xLjEsMC40bC0wLjEsMGwtMC40LDEuNw0KCQkJQzE0LjUsMjAuNSwxNC4zLDIwLjUsMTQsMjAuNXogTTE0LDExLjZjLTEuMywwLTIuNCwxLjEtMi40LDIuNGMwLDEuMywxLjEsMi40LDIuNCwyLjRjMS4zLDAsMi40LTEuMSwyLjQtMi40DQoJCQlDMTYuNCwxMi43LDE1LjMsMTEuNiwxNCwxMS42eiIvPg0KCTwvZz4NCjwvZz4NCjwvc3ZnPg0K);
\t}
</style>
JS;
                }
            }
            $contents = Koken::render($raw);
            if ($pass === 1) {
                // Rerun parse to catch shortcode renders
                while (strpos($contents, '<koken:') !== false && $pass < 3) {
                    $pass++;
                    $contents = go($contents, $pass);
                }
            } else {
                return $contents;
            }
            $contents .= Koken::cleanup();
            if (strpos($contents, 'settings.css.lens"') === false && !empty(Koken::$site['custom_css']) || Koken::$draft) {
                $js .= '<style id="koken_custom_css">' . Koken::$site['custom_css'] . '</style>';
            }
            preg_match_all('/<\\!\\-\\- KOKEN HEAD BEGIN \\-\\->(.*)<!\\-\\- KOKEN HEAD END \\-\\->/msU', $contents, $headers);
            $contents = preg_replace('/<\\!\\-\\- KOKEN HEAD BEGIN \\-\\->(.*)<!\\-\\- KOKEN HEAD END \\-\\->/msU', '', $contents);
            $header_str = '';
            foreach ($headers[1] as $header) {
                $header_str .= "\t" . $header . "\n";
            }
            if (strpos($header_str, '<title>') !== false) {
                $contents = preg_replace('/<title>.*<\\/title>/msU', '', $contents);
                $header_str = preg_replace('/<koken_title>.*<\\/koken_title>/', '', $header_str);
            } else {
                if (strpos($header_str, '<koken_title>') !== false && strpos($contents, '<koken_title') !== false) {
                    $contents = preg_replace('/<title>.*<\\/title>/msU', '', $contents);
                    $header_str = str_replace('koken_title', 'title', $header_str);
                } else {
                    if (strpos($contents, '<koken_title') !== false) {
                        $contents = str_replace('koken_title', 'title', $contents);
                    }
                }
            }
            if (Koken::$pjax && strpos($header_str, '<title>')) {
                preg_match('~<title>.*</title>~', $header_str, $title_match);
                $contents .= $title_match[0];
            }
            $contents = preg_replace('/<koken_title>.*<\\/koken_title>/msU', '', $contents);
            $header_str .= "\n\t<!--[if IE]>\n\t<script src=\"" . Koken::$location['real_root_folder'] . "/app/site/themes/common/js/html5shiv.js\"></script>\n\t<![endif]-->\n";
            if (strpos($contents, '<head>')) {
                preg_match('/<head>(.*)?<\\/head>/msU', $contents, $header);
                if (count($header)) {
                    $head = isset($header[1]) ? $header[1] : '';
                    preg_match_all('/<script.*<\\/script>/msU', $head, $head_js);
                    $head = preg_replace('/\\s*<script.*<\\/script>\\s*/msU', '', $head) . "\n{$header_str}\n{$js}\n" . join("\n", $head_js[0]);
                    $contents = preg_replace('/<head>(.*)?<\\/head>/msU', "<head>\n" . str_replace('$', '\\$', $head) . "\n</head>", $contents);
                }
            } else {
                if (strpos($contents, '</body>')) {
                    $contents = str_replace('</body>', "{$js}\n{$header_str}\n</body>", $contents);
                } else {
                    if (Koken::$pjax) {
                        $contents .= $js;
                    }
                }
            }
            $final_page_classes = trim(join(' ', array_merge(explode(' ', Koken::$page_class), Shutter::get_body_classes())));
            if (preg_match_all('/<body(?:[^>]+)?>/', $contents, $match) && !empty($final_page_classes)) {
                foreach ($match[0] as $body) {
                    if (strpos($body, 'class="') !== false) {
                        $new_body = preg_replace('/class="([^"]+)"/', "class=\"\$1 " . $final_page_classes . "\"", $body);
                    } else {
                        $new_body = str_replace('>', ' class="' . $final_page_classes . '">', $body);
                    }
                    $contents = str_replace($body, $new_body, $contents);
                }
            }
            if (preg_match_all('/<html(?:[^>]+)?>/', $contents, $match) && !empty($final_page_classes)) {
                foreach ($match[0] as $html) {
                    if (strpos($html, 'class="') !== false) {
                        $new_html = preg_replace('/class="([^"]+)"/', "class=\"\$1 " . $final_page_classes . "\"", $html);
                    } else {
                        $new_html = str_replace('>', ' class="' . $final_page_classes . '">', $html);
                    }
                    $contents = str_replace($html, $new_html, $contents);
                }
            }
            preg_match('/<!-- KOKEN META DESCRIPTION BEGIN -->(.*)<!-- KOKEN META DESCRIPTION END -->/msU', $contents, $meta_description);
            preg_match('/<!-- KOKEN META KEYWORDS BEGIN -->(.*)<!-- KOKEN META KEYWORDS END -->/msU', $contents, $meta_keywords);
            $contents = preg_replace('/<!-- KOKEN META (DESCRIPTION|KEYWORDS) BEGIN -->.*<!-- KOKEN META (DESCRIPTION|KEYWORDS) END -->/msU', '', $contents);
            $contents = preg_replace('/\\t+/', "\t", $contents);
            $contents = preg_replace('/\\n\\t*\\n/', "\n", $contents);
            $contents = preg_replace('/\\n{2,}/', "\n\n", $contents);
            $contents = preg_replace('/<title>\\s*/ms', '<title>', $contents);
            if (count($meta_description) && strlen($meta_description[1]) > 0) {
                $contents = preg_replace('/<meta name="description" content=".*" \\/>/', '<meta name="description" content="' . str_replace('$', '\\$', $meta_description[1]) . '" />', $contents);
            }
            if (count($meta_keywords) && strlen($meta_keywords[1]) > 0) {
                $contents = preg_replace('/<meta name="keywords" content="(.*)" \\/>/', "<meta name=\"keywords\" content=\"\$1, {$meta_keywords[1]}\" />", $contents);
            }
            if (Koken::$rss) {
                $contents = '<?xml version="1.0" encoding="utf-8"?>' . "\n{$contents}";
            } else {
                $contents = Shutter::filter('site.output', $contents);
            }
            Koken::cache($contents);
            if (Koken::$rss) {
                header('Content-type: text/xml; charset=UTF-8');
            } else {
                header('Content-type: text/html; charset=UTF-8');
            }
            die($contents);
        }
 protected final function deliver_email($from, $from_name, $subject, $message)
 {
     Shutter::email($from, $from_name, null, $subject, $message);
 }
Example #14
0
 function index()
 {
     list($params, $id, $slug) = $this->parse_params(func_get_args());
     $params['auth'] = $this->auth;
     // Create or update
     if ($this->method != 'get') {
         $t = new Text();
         switch ($this->method) {
             case 'post':
             case 'put':
                 if ($id) {
                     $t->get_by_id($id);
                     $t->old_published = $t->published;
                     $t->current_slug = $t->slug;
                     if (isset($_POST['unpublish'])) {
                         $_POST['published'] = 0;
                         $_POST['published_on'] = null;
                     }
                 } else {
                     if (isset($_POST['page_type']) && $_POST['page_type'] === 'page') {
                         $_POST['published'] = 1;
                     }
                 }
                 $arr = $_POST;
                 global $raw_input_data;
                 if (isset($raw_input_data['content'])) {
                     $arr['content'] = $raw_input_data['content'];
                 }
                 if (isset($raw_input_data['draft'])) {
                     $arr['draft'] = $raw_input_data['draft'];
                 }
                 // Little hack here to make sure content validation is always run
                 // (newline gets stripped in text->_format_content)
                 if (isset($arr['content'])) {
                     $arr['content'] .= "\n";
                 }
                 try {
                     $t->from_array($arr, array(), true);
                 } catch (Exception $e) {
                     $this->error('400', $e->getMessage());
                     return;
                 }
                 if (isset($_POST['tags'])) {
                     $t->_format_tags($_POST['tags']);
                 } else {
                     if ($this->method === 'put' && isset($_POST['published'])) {
                         $t->_update_tag_counts();
                     }
                 }
                 $arr = $t->to_array(array('expand' => true));
                 if ($id) {
                     Shutter::hook('text.update', $arr);
                 } else {
                     Shutter::hook('text.create', $arr);
                 }
                 $this->redirect("/text/{$t->id}" . (isset($params['render']) ? '/render:' . $params['render'] : ''));
                 break;
             case 'delete':
                 if (is_null($id)) {
                     $this->error('403', 'Required parameter "id" not present.');
                     return;
                 } else {
                     if (is_numeric($id)) {
                         $id = array($id);
                     } else {
                         $id = explode(',', $id);
                     }
                     $tags = array();
                     foreach ($id as $text_id) {
                         $text = $t->get_by_id($text_id);
                         if ($text->exists()) {
                             $tags = array_merge($tags, $text->tags);
                             $s = new Slug();
                             $prefix = $text->page_type == 0 ? 'essay' : 'page';
                             $this->db->query("DELETE FROM {$s->table} WHERE id = '{$prefix}.{$text->slug}'");
                             Shutter::hook('text.delete', $text->to_array(array('auth' => true)));
                             if (!$text->delete()) {
                                 // TODO: More info
                                 $this->error('500', 'Delete failed.');
                                 return;
                             }
                         }
                     }
                 }
                 exit;
                 break;
         }
     }
     $p = new Text();
     // No id, so we want a list
     if (is_null($id) && !$slug) {
         $params['state'] = 'published';
         $final = $p->listing($params);
     } else {
         if (!is_null($id)) {
             if (is_numeric($id)) {
                 $page = $p->get_by_id($id);
             } else {
                 $this->auth = $params['auth'] = true;
                 $page = $p->get_by_internal_id($id);
             }
         } else {
             if ($slug) {
                 $p->group_start()->where('slug', $slug)->or_like('old_slug', ',' . $slug . ',', 'both')->group_end();
                 if (isset($params['type'])) {
                     $p->where('page_type', $params['type'] === 'essay' ? 0 : 1);
                 }
                 $page = $p->get();
             }
         }
         $params['expand'] = true;
         if ($page->exists()) {
             $final = $page->to_array($params);
             if (!$this->auth && !$final['published']) {
                 $this->error('404', 'Not found');
                 return;
             }
         } else {
             $this->error('404', "Text with ID: {$id} not found.");
             return;
         }
         if ($final['page_type'] === 'essay' && $page->published) {
             $options = array('neighbors' => false, 'context' => false);
             $options = array_merge($options, $params);
             if ($options['neighbors']) {
                 // Make sure $neighbors is at least 2
                 $options['neighbors'] = max($options['neighbors'], 2);
                 // Make sure neighbors is even
                 if ($options['neighbors'] & 1 != 0) {
                     $options['neighbors']++;
                 }
                 $options['neighbors'] = $options['neighbors'] / 2;
             } else {
                 $options['neighbors'] = 1;
             }
             if ($options['neighbors']) {
                 // TODO: Performance check
                 $next = new Text();
                 $prev = new Text();
                 $to_arr_options = array('auth' => $this->auth);
                 $next->group_start()->where('page_type', 0)->where('published', 1)->group_start()->where('published_on <', $page->published_on)->or_group_start()->where('published_on =', $page->published_on)->where('id <', $page->id)->group_end()->group_end()->group_end();
                 $prev->group_start()->where('page_type', 0)->where('published', 1)->group_start()->where('published_on >', $page->published_on)->or_group_start()->where('published_on =', $page->published_on)->where('id >', $page->id)->group_end()->group_end()->group_end();
                 if (strpos($options['context'], 'tag-') === 0) {
                     $tag = str_replace('tag-', '', urldecode($options['context']));
                     $t = new Tag();
                     $t->where('name', $tag)->get();
                     $to_arr_options['context'] = "tag-{$tag}";
                     if ($t->exists()) {
                         $next->where_related_tag('id', $t->id);
                         $prev->where_related_tag('id', $t->id);
                         $final['context']['type'] = 'tag';
                         $final['context']['title'] = $tag;
                         $final['context']['slug'] = $tag;
                         $t->model = 'tag_essays';
                         $t->slug = $t->name;
                         $url = $t->url();
                         if ($url) {
                             list($final['context']['__koken_url'], $final['context']['url']) = $url;
                         }
                     }
                 } else {
                     if (strpos($options['context'], 'category-') === 0) {
                         $category = str_replace('category-', '', $options['context']);
                         $cat = new Category();
                         $cat->where('slug', $category)->get();
                         if ($cat->exists()) {
                             $next->where_related_category('id', $cat->id);
                             $prev->where_related_category('id', $cat->id);
                             $final['context']['type'] = 'category';
                             $final['context']['title'] = $cat->title;
                             $final['context']['slug'] = $cat->slug;
                             $to_arr_options['context'] = "category-{$cat->id}";
                             $cat->model = 'category_essays';
                             $url = $cat->url();
                             if ($url) {
                                 list($final['context']['__koken_url'], $final['context']['url']) = $url;
                             }
                         }
                     }
                 }
                 $max = $next->get_clone()->count();
                 $min = $prev->get_clone()->count();
                 $final['context']['total'] = $max + $min + 1;
                 $final['context']['position'] = $min + 1;
                 $pre_limit = $next_limit = $options['neighbors'];
                 if ($min < $pre_limit) {
                     $next_limit += $pre_limit - $min;
                     $pre_limit = $min;
                 }
                 if ($max < $next_limit) {
                     $pre_limit = min($min, $pre_limit + ($next_limit - $max));
                     $next_limit = $max;
                 }
                 $final['context']['previous'] = array();
                 $final['context']['next'] = array();
                 if ($next_limit > 0) {
                     $next->order_by('published_on DESC, id DESC')->limit($next_limit);
                     $next->get_iterated();
                     foreach ($next as $c) {
                         $final['context']['next'][] = $c->to_array($to_arr_options);
                     }
                 }
                 if ($pre_limit > 0) {
                     $prev->order_by('published_on ASC, id ASC')->limit($pre_limit);
                     $prev->get_iterated();
                     foreach ($prev as $c) {
                         $final['context']['previous'][] = $c->to_array($to_arr_options);
                     }
                     $final['context']['previous'] = array_reverse($final['context']['previous']);
                 }
             }
         }
     }
     $this->set_response_data($final);
 }
Example #15
0
    function index()
    {
        if (!$this->auth) {
            $this->error('403', 'Forbidden');
            return;
        }
        $image_processing = new Setting();
        $image_processing->where('name', 'image_processing_library')->get();
        include FCPATH . 'app' . DIRECTORY_SEPARATOR . 'koken' . DIRECTORY_SEPARATOR . 'DarkroomUtils.php';
        $libs = DarkroomUtils::libraries();
        if ($image_processing->exists()) {
            if (!isset($libs[$image_processing->value])) {
                $top = array_shift(array_keys($libs));
                $lib = $libs[$top];
                $image_processing->value = $lib['key'];
                $image_processing->save();
            }
        } else {
            if (!defined('MAGICK_PATH_FINAL') || (MAGICK_PATH_FINAL === 'convert' || !isset($libs[MAGICK_PATH_FINAL]))) {
                $top = array_shift(array_keys($libs));
                $lib = $libs[$top];
            } else {
                $lib = $libs[MAGICK_PATH_FINAL];
            }
            $image_processing->name = 'image_processing_library';
            $image_processing->value = $lib['key'];
            $image_processing->save();
        }
        $last_check = new Setting();
        $last_check->where('name', 'last_migration');
        $last_check_count = $last_check->count();
        if ($last_check_count > 1) {
            $last_check->where('name', 'last_migration')->order_by('value ASC')->limit($last_check_count - 1)->get();
            $last_check->delete_all();
        }
        $s = new Setting();
        $settings = $s->get_iterated();
        $data = array('image_processing_libraries' => array_values($libs));
        $bools = array('has_toured', 'site_hidpi', 'retain_image_metadata', 'image_use_defaults', 'use_default_labels_links', 'uploading_publish_on_captured_date');
        foreach ($settings as $setting) {
            // Don't allow dupes to screw things up
            if (isset($data[$setting->name])) {
                continue;
            }
            $value = $setting->value;
            if (in_array($setting->name, $bools)) {
                $value = $value == 'true';
            }
            if ($setting->name === 'last_upload') {
                $value = $value === 'false' ? false : (int) $value;
            }
            $data[$setting->name] = $value;
        }
        if (!isset($data['uploading_publish_on_captured_date'])) {
            $data['uploading_publish_on_captured_date'] = false;
        }
        if (!isset($data['uploading_default_album_visibility'])) {
            $data['uploading_default_album_visibility'] = 'public';
        }
        if (!isset($data['email_handler'])) {
            $data['email_handler'] = 'DDI_Email';
        }
        $data['email_handlers'] = Shutter::get_email_handlers();
        $disable_cache_file = FCPATH . 'storage' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'no-site-cache';
        $data['enable_site_cache'] = !file_exists($disable_cache_file);
        if ($this->method != 'get') {
            if ($this->auth_role !== 'god') {
                $this->error('403', 'Forbidden');
                return;
            }
            if (isset($_POST['signin_bg'])) {
                $c = new Content();
                $c->get_by_id($_POST['signin_bg']);
                if ($c->exists()) {
                    $_c = $c->to_array();
                    $large = array_pop($_c['presets']);
                    // TODO: Error checking for permissions reject
                    $f = $large['url'];
                    $to = FCPATH . 'storage' . DIRECTORY_SEPARATOR . 'wallpaper' . DIRECTORY_SEPARATOR . 'signin.jpg';
                    if (extension_loaded('curl')) {
                        $cp = curl_init($f);
                        $fp = fopen($to, "w+");
                        if (!$fp) {
                            curl_close($cp);
                        } else {
                            curl_setopt($cp, CURLOPT_FILE, $fp);
                            curl_exec($cp);
                            curl_close($cp);
                            fclose($fp);
                        }
                    } elseif (ini_get('allow_url_fopen')) {
                        copy($f, $to);
                    }
                }
            } else {
                if (isset($_POST['enable_site_cache'])) {
                    if ($_POST['enable_site_cache'] === 'true') {
                        @unlink($disable_cache_file);
                    } else {
                        touch($disable_cache_file);
                        delete_files(dirname($disable_cache_file) . DIRECTORY_SEPARATOR . 'site', true, 1);
                    }
                    unset($_POST['enable_site_cache']);
                }
                // TODO: Make sure new path is not inside real_base
                // TODO: Ensure that real_base is not deleted under any circumstances
                if (isset($_POST['site_url']) && $_POST['site_url'] !== $data['site_url']) {
                    $_POST['site_url'] = strtolower(rtrim($_POST['site_url'], '/'));
                    if (empty($_POST['site_url'])) {
                        $_POST['site_url'] = '/';
                    }
                    if (isset($_SERVER['PHP_SELF']) && isset($_SERVER['SCRIPT_FILENAME'])) {
                        $php_self = str_replace('/', DIRECTORY_SEPARATOR, $_SERVER['PHP_SELF']);
                        $doc_root = preg_replace('~' . $php_self . '$~i', '', $_SERVER['SCRIPT_FILENAME']);
                    } else {
                        $doc_root = $_SERVER['DOCUMENT_ROOT'];
                    }
                    $doc_root = realpath($doc_root);
                    $target = $doc_root . str_replace('/', DIRECTORY_SEPARATOR, $_POST['site_url']);
                    $php_include_base = rtrim(preg_replace('~^' . $doc_root . '~', '', FCPATH), DIRECTORY_SEPARATOR);
                    $real_base = $doc_root;
                    if (empty($php_include_base)) {
                        $real_base .= DIRECTORY_SEPARATOR;
                    } else {
                        $real_base .= $php_include_base;
                    }
                    @($target_dir = dir($target));
                    $real_base_dir = dir($real_base);
                    function compare_paths($one, $two)
                    {
                        return rtrim($one, DIRECTORY_SEPARATOR) === rtrim($two, DIRECTORY_SEPARATOR);
                    }
                    if ($target_dir && compare_paths($target_dir->path, $real_base_dir->path)) {
                        $_POST['site_url'] = 'default';
                        $htaccess = create_htaccess();
                        $root_htaccess = FCPATH . '.htaccess';
                        $current = file_get_contents($root_htaccess);
                        preg_match('/#MARK#.*/s', $htaccess, $match);
                        $htaccess = preg_replace('/#MARK#.*/s', str_replace('$', '\\$', $match[0]), $current);
                        file_put_contents($root_htaccess, $htaccess);
                    } else {
                        if ($target_dir) {
                            $reserved = array('admin', 'app', 'storage');
                            foreach ($reserved as $dir) {
                                $_dir = dir(rtrim($real_base_dir->path, '/') . "/{$dir}");
                                if (compare_paths($target_dir->path, $_dir->path)) {
                                    $this->error('400', "This directory is reserved for Koken core files. Please choose another location.");
                                    return;
                                }
                            }
                        }
                        if (!make_child_dir($target)) {
                            $this->error('500', "Koken was not able to create the Site URL directory. Make sure the path provided is writable by the web server and try again.");
                            return;
                        }
                        $php_include_rel = str_replace(DIRECTORY_SEPARATOR, '/', $php_include_base);
                        $php_include_base = str_replace('\\', '\\\\', $php_include_base);
                        $doc_root_php = str_replace('\\', '\\\\', $doc_root);
                        $php = <<<OUT
<?php

\t\$rewrite = false;
\t\$real_base_folder = '{$php_include_rel}';
\trequire '{$doc_root_php}{$php_include_base}' . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'site' . DIRECTORY_SEPARATOR . 'site.php';
OUT;
                        $htaccess = create_htaccess($_POST['site_url']);
                        if ($this->check_for_rewrite()) {
                            $file = $target . DIRECTORY_SEPARATOR . '.htaccess';
                            $file_data = $htaccess;
                            $put_mode = FILE_APPEND;
                            if ($_POST['site_url'] !== 'default' && "{$doc_root}" . DIRECTORY_SEPARATOR !== FCPATH) {
                                $root_htaccess = FCPATH . '.htaccess';
                                if (file_exists($root_htaccess)) {
                                    $current = file_get_contents($root_htaccess);
                                    $redirect = create_htaccess($_POST['site_url'], true);
                                    preg_match('/#MARK#.*/s', $redirect, $match);
                                    $redirect = preg_replace('/#MARK#.*/s', str_replace('$', '\\$', $match[0]), $current);
                                    file_put_contents($root_htaccess, $redirect);
                                }
                            }
                        } else {
                            $file = $target . DIRECTORY_SEPARATOR . 'index.php';
                            $file_data = $php;
                            $put_mode = 0;
                        }
                        if (file_exists($file)) {
                            rename($file, "{$file}.bkup");
                        }
                        if (!file_put_contents($file, $file_data, $put_mode)) {
                            $this->error('500', "Koken was not able to create the necessary files in the Site URL directory. Make sure that path has sufficient permissions so that Koken may write the files.");
                            return;
                        }
                    }
                    if ($data['site_url'] !== 'default') {
                        $old = $doc_root . str_replace('/', DIRECTORY_SEPARATOR, $data['site_url']);
                        $old_dir = dir($old);
                        if (!compare_paths($old_dir->path, $real_base_dir->path)) {
                            if ($this->check_for_rewrite()) {
                                $old_file = $old . DIRECTORY_SEPARATOR . '.htaccess';
                            } else {
                                $old_file = $old . DIRECTORY_SEPARATOR . 'index.php';
                            }
                            unlink($old_file);
                            $backup = $old_file . '.bkup';
                            if (file_exists($backup)) {
                                rename($backup, $old_file);
                            }
                            // This will only remove the dir if it is empty
                            @rmdir($old);
                        }
                    }
                }
                global $raw_input_data;
                if (isset($raw_input_data['url_data'])) {
                    $url_data = json_decode($raw_input_data['url_data'], true);
                    $u = new Url();
                    $u->order_by('id DESC')->get();
                    $existing_data = unserialize($u->data);
                    $transformed = array();
                    foreach ($url_data as $key => $udata) {
                        $transformed[] = array('type' => $key, 'data' => $udata);
                    }
                    if ($existing_data !== $transformed) {
                        $n = new Url();
                        $n->data = serialize($transformed);
                        $n->save();
                    }
                    unset($_POST['url_data']);
                }
                $save = array();
                foreach ($_POST as $key => $val) {
                    if (isset($data[$key]) && $data[$key] !== $val) {
                        if ($key === 'retain_image_metadata' || $key !== 'image_processing_library' && strpos($key, 'image_') === 0) {
                            delete_files(FCPATH . 'storage' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'images', true, 1);
                        }
                        $save[$key] = $val;
                    }
                }
                foreach ($save as $k => $v) {
                    $s = new Setting();
                    $s->where('name', $k)->get();
                    if ($s->exists()) {
                        $s->value = $v;
                        $s->save();
                    } else {
                        if (in_array($k, array('uploading_default_album_visibility', 'uploading_publish_on_captured_date', 'email_handler'))) {
                            $n = new Setting();
                            $n->name = $k;
                            $n->value = $v;
                            $n->save();
                        }
                    }
                }
                if (isset($save['email_handler']) || isset($save['email_delivery_address'])) {
                    $this->_compile_plugins();
                }
            }
            $this->redirect('/settings');
        }
        if (!isset($data['site_timezone']) || empty($data['site_timezone']) || $data['site_timezone'] === 'Etc/UTC') {
            $data['site_timezone'] = 'UTC';
        } else {
            if ($data['site_timezone'] === 'Etc/GMT+12') {
                $data['site_timezone'] = 'Pacific/Auckland';
            }
        }
        $data['image_processing_library_label'] = $libs[$data['image_processing_library']]['label'];
        $migrate_path = FCPATH . 'app' . DIRECTORY_SEPARATOR . 'application' . DIRECTORY_SEPARATOR . 'models' . DIRECTORY_SEPARATOR . 'migrations' . DIRECTORY_SEPARATOR;
        $migrations = scandir($migrate_path);
        $data['migrations'] = array();
        if (!isset($data['last_migration'])) {
            $migration_setting = new Setting();
            $migration_setting->name = 'last_migration';
            $migration_setting->value = '26';
            $migration_setting->save();
            $data['last_migration'] = '26';
        }
        if (!isset($data['has_toured']) || ENVIRONMENT === 'development') {
            $data['has_toured'] = true;
        }
        foreach ($migrations as $migration) {
            $migration = str_replace('.php', '', $migration);
            $migration_int = (int) $migration;
            if ($migration_int > $data['last_migration']) {
                $data['migrations'][] = $migration;
            }
        }
        unset($data['last_migration']);
        $data = Shutter::filter('api.settings', array($data));
        $this->set_response_data($data);
    }
Example #16
0
 function to_array($options = array())
 {
     $options = array_merge(array('with_covers' => true, 'auth' => false), $options);
     $koken_url_info = $this->config->item('koken_url_info');
     $exclude = array('deleted', 'total_count', 'video_count', 'featured_order', 'tags_old', 'old_slug');
     $dates = array('created_on', 'modified_on', 'featured_on', 'published_on');
     $strings = array('title', 'summary', 'description');
     $bools = array('featured');
     list($data, $public_fields) = $this->prepare_for_output($options, $exclude, $bools, $dates, $strings);
     if (!$options['auth'] && $data['visibility'] < 1) {
         unset($data['internal_id']);
     }
     if (!$data['featured']) {
         unset($data['featured_on']);
     }
     $sort = array();
     list($sort['by'], $sort['direction']) = explode(' ', $data['sort']);
     $data['sort'] = $sort;
     $data['__koken__'] = 'album';
     if (array_key_exists('album_type', $data)) {
         switch ($data['album_type']) {
             case 2:
                 $data['album_type'] = 'set';
                 break;
             case 1:
                 $data['album_type'] = 'smart';
                 break;
             default:
                 $data['album_type'] = 'standard';
         }
     }
     if ($this->album_type == 2) {
         $sum = new Album();
         $sum->select_sum('total_count')->select_sum('video_count')->where('right_id <', $this->right_id)->where('left_id >', $this->left_id)->where('album_type', 0)->where('visibility', 0)->get();
         $data['counts'] = array('total' => (int) $this->total_count, 'videos' => (int) $sum->video_count, 'images' => $sum->total_count - $sum->video_count);
     } else {
         $data['counts'] = array('total' => (int) $this->total_count, 'videos' => (int) $this->video_count, 'images' => $this->total_count - $this->video_count);
     }
     $data['tags'] = $this->_get_tags_for_output($options);
     $data['categories'] = array('count' => is_null($this->category_count) ? $this->categories->count() : (int) $this->category_count, 'url' => $koken_url_info->base . 'api.php?/albums/' . $data['id'] . '/categories');
     $data['topics'] = array('count' => is_null($this->text_count) ? $this->text->count() : (int) $this->text_count, 'url' => $koken_url_info->base . 'api.php?/albums/' . $data['id'] . '/topics');
     if ($options['with_covers']) {
         $data['covers'] = $existing = array();
         $covers = $this->covers;
         if (isset($options['before'])) {
             $covers->where('published_on <=', $options['before']);
             $data['__cover_hint_before'] = $options['before'];
         }
         $covers->include_related_count('albums', NULL, array('visibility' => 0));
         $covers->include_related_count('categories');
         foreach ($covers->order_by("covers_{$this->db_join_prefix}albums_covers.id ASC")->get_iterated() as $f) {
             if ($f->exists()) {
                 $data['covers'][] = $f->to_array(array('in_album' => $this));
                 $existing[] = $f->id;
             }
         }
         $covers_count_set = false;
         if ($this->album_type == 2) {
             $covers_count_set = $this->covers->count();
         }
         if ($covers_count_set !== false && $covers_count_set < 3) {
             $a = new Album();
             $ids = $a->select('id')->where('right_id <', $this->right_id)->where('left_id >', $this->left_id)->where('visibility', $this->visibility)->get_iterated();
             $id_arr = array();
             foreach ($ids as $id) {
                 $id_arr[] = $id->id;
             }
             if (!empty($id_arr)) {
                 $c = new Content();
                 $q = "SELECT DISTINCT cover_id FROM {$this->db_join_prefix}albums_covers WHERE album_id IN (" . join(',', $id_arr) . ")";
                 if (!empty($existing)) {
                     $q .= ' AND cover_id NOT IN(' . join(',', $existing) . ')';
                 }
                 $covers = $c->query($q . "GROUP BY album_id LIMIT " . (3 - $covers_count_set));
                 $f_ids = array();
                 foreach ($covers as $f) {
                     $f_ids[] = $f->cover_id;
                 }
                 if (!empty($f_ids)) {
                     $c->where_in('id', $f_ids)->get_iterated();
                     foreach ($c as $content) {
                         // TODO: auth needs to be passed in here
                         array_unshift($data['covers'], $content->to_array(array('in_album' => $this)));
                     }
                 }
             }
         }
         // Latest covers first
         $data['covers'] = array_reverse($data['covers']);
     }
     if (isset($options['order_by']) && in_array($options['order_by'], array('created_on', 'modified_on'))) {
         $data['date'] =& $data[$options['order_by']];
     } else {
         $data['date'] =& $data['published_on'];
     }
     if ($data['level'] > 1 && (!array_key_exists('include_parent', $options) || $options['include_parent'])) {
         $parent = new Album();
         $parent->where('left_id <', $data['left_id'])->where('level <', $data['level'])->where('visibility', $this->visibility)->where('deleted', 0)->order_by('left_id DESC')->limit(1)->get();
         $data['parent'] = $parent->to_array();
     } else {
         if ($data['level'] == 1) {
             $data['parent'] = false;
         }
     }
     $cat = isset($options['category']) ? $options['category'] : (isset($options['context']) && strpos($options['context'], 'category-') === 0 ? str_replace('category-', '', $options['context']) : false);
     if ($cat) {
         if (is_numeric($cat)) {
             foreach ($this->categories->get_iterated() as $c) {
                 if ($c->id == $cat) {
                     $cat = $c->slug;
                     break;
                 }
             }
         }
     }
     $data['url'] = $this->url(array('date' => $data['published_on'], 'tag' => isset($options['tags']) ? $options['tags'] : (isset($options['context']) && strpos($options['context'], 'tag-') === 0 ? str_replace('tag-', '', $options['context']) : false), 'category' => $cat));
     if ($data['url']) {
         list($data['__koken_url'], $data['url']) = $data['url'];
         $data['canonical_url'] = $data['url'];
     }
     if (!$options['auth'] && $data['visibility'] > 0) {
         unset($data['url']);
     }
     if (array_key_exists('visibility', $data)) {
         switch ($data['visibility']) {
             case 1:
                 $raw = 'unlisted';
                 break;
             case 2:
                 $raw = 'private';
                 break;
             default:
                 $raw = 'public';
                 break;
         }
         $data['visibility'] = array('raw' => $raw, 'clean' => ucwords($raw));
         $data['public'] = $raw === 'public';
     }
     return Shutter::filter('api.album', array($data, $this, $options));
 }
Example #17
0
 static function start()
 {
     self::$start_time = microtime(true);
     self::$messages = Shutter::get_messages();
 }
Example #18
0
 function listing($params, $id = false)
 {
     $sort = $this->_get_site_order('content');
     $options = array('order_by' => $sort['by'], 'order_direction' => $sort['direction'], 'search' => false, 'search_filter' => false, 'tags' => false, 'tags_not' => false, 'page' => 1, 'match_all_tags' => false, 'limit' => 100, 'include_presets' => true, 'featured' => null, 'types' => false, 'auth' => false, 'favorites' => null, 'before' => false, 'after' => false, 'after_column' => 'uploaded_on', 'before_column' => 'uploaded_on', 'category' => false, 'category_not' => false, 'year' => false, 'year_not' => false, 'month' => false, 'month_not' => false, 'day' => false, 'day_not' => false, 'in_album' => false, 'reduce' => false, 'is_cover' => true, 'independent' => false);
     $options = array_merge($options, $params);
     if (isset($params['order_by']) && !isset($params['order_direction'])) {
         $options['order_direction'] = in_array($params['order_by'], array('title', 'filename')) ? 'ASC' : 'DESC';
     }
     Shutter::hook('content.listing', array($this, $options));
     if ($options['featured'] == 1 && !isset($params['order_by'])) {
         $options['order_by'] = 'featured_on';
     } else {
         if ($options['favorites'] == 1 && !isset($params['order_by'])) {
             $options['order_by'] = 'favorited_on';
         }
     }
     if ($options['auth']) {
         if (isset($options['visibility']) && $options['visibility'] !== 'album') {
             $values = array('public', 'unlisted', 'private');
             if (in_array($options['visibility'], $values)) {
                 $options['visibility'] = array_search($options['visibility'], $values);
             } else {
                 if ($options['visibility'] === 'any') {
                     $options['visibility'] = false;
                 } else {
                     $options['visibility'] = 0;
                 }
             }
         } else {
             if (!isset($options['visibility']) || $options['visibility'] !== 'album') {
                 $options['visibility'] = 0;
             }
         }
     } else {
         if ($options['in_album']) {
             $options['visibility'] = 'album';
         } else {
             $options['visibility'] = 0;
         }
     }
     if ($options['visibility'] > 0 && $options['order_by'] === 'published_on') {
         $options['order_by'] = 'captured_on';
     }
     if ($options['order_by'] == 'dimension') {
         $options['order_by'] = 'width * height';
     }
     if (is_numeric($options['limit']) && $options['limit'] > 0) {
         $options['limit'] = min($options['limit'], 100);
     } else {
         $options['limit'] = 100;
     }
     if ($options['independent']) {
         $this->where_related('album', 'id', null);
     }
     if ($options['types']) {
         $types = explode(',', str_replace(' ', '', $options['types']));
         $this->group_start();
         foreach ($types as $t) {
             switch ($t) {
                 case 'photo':
                     $this->or_where('file_type', 0);
                     break;
                 case 'video':
                     $this->or_where('file_type', 1);
                     break;
                 case 'audio':
                     $this->or_where('file_type', 2);
                     break;
             }
         }
         $this->group_end();
     }
     if ($options['search'] && $options['search_filter'] === 'tags') {
         $options['tags'] = $options['search'];
         $options['search'] = false;
     }
     if ($options['search']) {
         $term = urldecode($options['search']);
         if ($options['search_filter']) {
             if ($options['search_filter'] === 'category') {
                 $cat = new Category();
                 $cat->where('title', $term)->get();
                 if ($cat->exists()) {
                     $this->where_related('category', 'id', $cat->id);
                 } else {
                     $this->where_related('category', 'id', 0);
                 }
             } else {
                 $this->group_start();
                 $this->like($options['search_filter'], $term, 'both');
                 $this->group_end();
             }
         } else {
             $this->group_start();
             $this->like('title', $term, 'both');
             $this->or_like('caption', $term, 'both');
             $t = new Tag();
             $t->where('name', $term)->get();
             if ($t->exists()) {
                 $this->or_where_related('tag', 'id', $t->id);
             }
             $this->group_end();
         }
     } else {
         if ($options['tags'] || $options['tags_not']) {
             $this->_do_tag_filtering($options);
         }
     }
     if (!is_null($options['featured'])) {
         $this->where('featured', $options['featured']);
     }
     if (!is_null($options['favorites'])) {
         $this->where('favorite', $options['favorites']);
     }
     if ($options['category']) {
         $this->where_related('category', 'id', $options['category']);
     } else {
         if ($options['category_not']) {
             $cat = new Content();
             $cat->select('id')->where_related('category', 'id', $options['category_not'])->get_iterated();
             $cids = array();
             foreach ($cat as $c) {
                 $cids[] = $c->id;
             }
             $this->where_not_in('id', $cids);
         }
     }
     if ($options['after']) {
         $this->where($options['after_column'] . ' >=', $options['after']);
     }
     if ($options['before']) {
         $this->where($options['before_column'] . ' <=', $options['before']);
     }
     if ($options['visibility'] === 'album') {
         $this->where('visibility <', $options['in_album']->visibility + 1);
     } else {
         if ($options['visibility'] !== false) {
             $this->where('visibility', $options['visibility']);
         }
     }
     if ($id) {
         $sql_order = "ORDER BY FIELD(id,{$id})";
         $id = explode(',', $id);
         $this->where_in('id', $id);
     }
     if ($options['order_by'] === 'captured_on' || $options['order_by'] === 'uploaded_on' || $options['order_by'] === 'modified_on' || $options['order_by'] === 'published_on') {
         $bounds_order = $options['order_by'];
     } else {
         $bounds_order = 'published_on';
     }
     $s = new Setting();
     $s->where('name', 'site_timezone')->get();
     $tz = new DateTimeZone($s->value);
     $offset = $tz->getOffset(new DateTime('now', new DateTimeZone('UTC')));
     if ($offset === 0) {
         $shift = '';
     } else {
         $shift = ($offset < 0 ? '-' : '+') . abs($offset);
     }
     // Do this before date filters are applied
     $bounds = $this->get_clone()->select('COUNT(DISTINCT ' . $this->table . '.id) as count, MONTH(FROM_UNIXTIME(' . $bounds_order . $shift . ')) as month, YEAR(FROM_UNIXTIME(' . $bounds_order . $shift . ')) as year')->group_by('month,year')->order_by('year')->get_iterated();
     $dates = array();
     foreach ($bounds as $b) {
         if (!is_numeric($b->year)) {
             continue;
         }
         if (!isset($dates[$b->year])) {
             $dates[$b->year] = array();
         }
         $dates[$b->year][$b->month] = (int) $b->count;
     }
     if (in_array($options['order_by'], array('captured_on', 'uploaded_on', 'modified_on'))) {
         $date_col = $options['order_by'];
     } else {
         $date_col = 'published_on';
     }
     if ($options['year'] || $options['year_not']) {
         if ($options['year_not']) {
             $options['year'] = $options['year_not'];
             $compare = ' !=';
         } else {
             $compare = '';
         }
         $this->where('YEAR(FROM_UNIXTIME(' . $date_col . $shift . '))' . $compare, $options['year']);
     }
     if ($options['month'] || $options['month_not']) {
         if ($options['month_not']) {
             $options['month'] = $options['month_not'];
             $compare = ' !=';
         } else {
             $compare = '';
         }
         $this->where('MONTH(FROM_UNIXTIME(' . $date_col . $shift . '))' . $compare, $options['month']);
     }
     if ($options['day'] || $options['day_not']) {
         if ($options['day_not']) {
             $options['day'] = $options['day_not'];
             $compare = ' !=';
         } else {
             $compare = '';
         }
         $this->where('DAY(FROM_UNIXTIME(' . $date_col . $shift . '))' . $compare, $options['day']);
         if ($options['reduce']) {
             $a = new Album();
             $a->select('id')->where('deleted', 0)->where('visibility', 0)->where('YEAR(FROM_UNIXTIME(' . $a->table . '.published_on' . $shift . '))', $options['year'])->where('MONTH(FROM_UNIXTIME(' . $a->table . '.published_on' . $shift . '))', $options['month'])->where('DAY(FROM_UNIXTIME(' . $a->table . '.published_on' . $shift . '))', $options['day'])->include_related('content', 'id')->get_iterated();
             $ids = array();
             foreach ($a as $album) {
                 if ($album->content_id) {
                     $ids[] = $album->content_id;
                 }
             }
             $e = new Text();
             $e->select('featured_image_id')->where('page_type', 0)->where('published', 1)->where('featured_image_id >', 0)->where('YEAR(FROM_UNIXTIME(' . $e->table . '.published_on' . $shift . '))', $options['year'])->where('MONTH(FROM_UNIXTIME(' . $e->table . '.published_on' . $shift . '))', $options['month'])->where('DAY(FROM_UNIXTIME(' . $e->table . '.published_on' . $shift . '))', $options['day'])->get_iterated();
             foreach ($e as $essay) {
                 if ($essay->featured_image_id) {
                     $ids[] = $essay->featured_image_id;
                 }
             }
             if (!empty($ids)) {
                 $this->where_not_in('id', $ids);
             }
         }
     }
     $vid_count = $this->get_clone()->where('file_type', 1)->count();
     $aud_count = $this->get_clone()->where('file_type', 2)->count();
     $final = $this->paginate($options);
     $final['dates'] = $dates;
     $this->include_related_count('albums', NULL, array('visibility' => 0));
     $this->include_related_count('categories');
     if ($id && !isset($params['order_by'])) {
         $q = explode('LIMIT', $this->get_sql());
         $query = $q[0] . $sql_order . ' LIMIT ' . $q[1];
         $data = $this->query($query);
     } else {
         if ($options['order_by'] === 'title') {
             $q = explode('LIMIT', $this->get_sql());
             $query = preg_replace('/SELECT\\s(`[^`]+`\\.\\*)/', "SELECT COALESCE(NULLIF(title, ''), filename) as order_title, \$1", $q[0]);
             $query .= 'ORDER BY order_title ' . $options['order_direction'] . ' LIMIT ' . $q[1];
             $data = $this->query($query);
         } else {
             $data = $this->order_by($options['order_by'] . ' ' . $options['order_direction'] . ', id ' . $options['order_direction'])->get_iterated();
         }
     }
     if (!$options['limit']) {
         $final['per_page'] = $data->result_count();
         $final['total'] = $data->result_count();
     }
     $final['counts'] = array('videos' => $vid_count, 'audio' => $aud_count, 'images' => $final['total'] - $vid_count - $aud_count, 'total' => $final['total']);
     $final['content'] = array();
     $final['sort'] = $sort;
     $tag_map = $this->_eager_load_tags($data);
     foreach ($data as $content) {
         $tags = isset($tag_map['c' . $content->id]) ? $tag_map['c' . $content->id] : array();
         $options['eager_tags'] = $tags;
         $final['content'][] = $content->to_array($options);
     }
     return $final;
 }
Example #19
0
 function index()
 {
     if (!$this->auth) {
         $this->error('401', 'Not authorized to perform this action.');
         return;
     }
     list($params, $id) = $this->parse_params(func_get_args());
     $plugins = $this->parse_plugins();
     $db_config = Shutter::get_db_configuration();
     switch ($this->method) {
         case 'delete':
             $p = new Plugin();
             $p->where('id', $id)->get();
             if ($p->exists()) {
                 $p->run_plugin_method('after_uninstall', $plugins);
                 $plugin = $p->init($plugins);
                 if ($plugin->database_fields) {
                     $this->load->dbforge();
                     foreach ($plugin->database_fields as $table => $fields) {
                         $table = $db_config['prefix'] . $table;
                         foreach ($fields as $column => $info) {
                             $this->dbforge->drop_column($table, $column);
                         }
                     }
                     $this->_clear_datamapper_cache();
                 }
                 $p->delete();
             }
             $this->_compile_plugins();
             exit;
             break;
         case 'post':
             $p = new Plugin();
             $p->path = $_POST['path'];
             $p->setup = $p->run_plugin_method('require_setup', $plugins) === false;
             if ($p->save()) {
                 $plugin = $p->init($plugins);
                 if ($plugin->database_fields) {
                     $this->load->dbforge();
                     foreach ($plugin->database_fields as $table => $fields) {
                         $table = $db_config['prefix'] . $table;
                         foreach ($fields as $column => $info) {
                             $this->dbforge->add_column($table, array($column => $info));
                         }
                     }
                     $this->_clear_datamapper_cache();
                 }
                 $p->run_plugin_method('after_install', $plugins);
             }
             $this->_compile_plugins();
             $this->redirect('/plugins');
             break;
         case 'put':
             unset($_POST['_method']);
             $data = serialize($_POST);
             $p = new Plugin();
             $p->where('id', $id)->get();
             $p->save_data($plugins, $_POST);
             $validate = $p->run_plugin_method('confirm_setup', $plugins, $data);
             if ($validate === true) {
                 $p->setup = 1;
                 $p->save();
                 $this->_compile_plugins();
                 exit;
             } else {
                 $this->error(400, $validate);
                 return;
             }
             break;
         default:
             $data = array('plugins' => $plugins);
             function sortByName($a, $b)
             {
                 return $a['name'] > $b['name'];
             }
             usort($data['plugins'], 'sortByName');
             $data['plugins'] = Shutter::filter('api.plugins', array($data['plugins']));
             $data['custom_sources'] = Shutter::$custom_sources;
             $this->set_response_data($data);
             break;
     }
 }
Example #20
0
 function koken_oembed($attr)
 {
     if (!isset($attr['url']) || !isset($attr['endpoint'])) {
         return '';
     }
     $endpoint = $attr['endpoint'];
     if (strpos($endpoint, 'maxwidth=') === false) {
         if (strpos($endpoint, '?') !== false) {
             $endpoint .= '&';
         } else {
             $endpoint .= '?';
         }
         $endpoint .= 'maxwidth=1920&maxheight=1080';
     }
     if (strpos($endpoint, '?') !== false) {
         $endpoint .= '&';
     } else {
         $endpoint .= '?';
     }
     $info = Shutter::get_oembed($endpoint . 'url=' . $attr['url']);
     if (isset($info['html'])) {
         $html = preg_replace('/<iframe/', '<iframe style="display:none"', $info['html']);
     } else {
         if (isset($info['url'])) {
             $html = '<img src="' . $info['url'] . '" />';
         } else {
             return '';
         }
     }
     return '<figure class="k-content-embed"><div class="k-content">' . $html . '</div></figure>';
 }
Example #21
0
 function index()
 {
     list($params, $id) = $this->parse_params(func_get_args());
     $site = new Setting();
     $site->like('name', 'site_%')->or_like('name', 'image_%')->get_iterated();
     $draft = new Draft();
     $data = array();
     $ds = DIRECTORY_SEPARATOR;
     $template_path = FCPATH . 'storage' . $ds . 'themes' . $ds;
     $defaults = json_decode(file_get_contents(FCPATH . 'app' . $ds . 'site' . $ds . 'defaults.json'), true);
     $default_template_path = FCPATH . 'app' . $ds . 'site' . $ds . 'themes' . $ds;
     $pulse_base = FCPATH . 'app' . $ds . 'site' . $ds . 'themes' . $ds . 'common' . $ds . 'js' . $ds . 'pulse.json';
     $user = new User();
     $user->get();
     if (isset($params['preview'])) {
         $theme_root = $template_path . $params['preview'] . $ds;
         $template_info = json_decode(file_get_contents($theme_root . 'info.json'), true);
         if (!$template_info) {
             $this->set_response_data(array('error' => 'Unable to parse the info.json file for this theme.'));
             return;
         }
         $p = new Draft();
         $p->path = $params['preview'];
         $p->init_draft_nav();
         $draft->data = json_decode($p->data, true);
     } else {
         if (isset($params['draft'])) {
             $draft->where('draft', 1);
         } else {
             $draft->where('current', 1);
         }
         $draft->get();
         if ($draft->exists()) {
             $theme_root = $template_path . $draft->path . $ds;
             $template_info = json_decode(file_get_contents($theme_root . 'info.json'), true);
             if (!$template_info) {
                 $this->set_response_data(array('error' => 'Unable to parse the info.json file for this theme.'));
                 return;
             }
             $is_live = $draft->current && $draft->data === $draft->live_data;
             $template_info['published'] = $is_live;
             $draft->data = json_decode(isset($params['draft']) ? $draft->data : $draft->live_data, true);
         } else {
             $this->error('404', 'Draft not found.');
             return;
         }
     }
     foreach ($defaults['templates'] as $path => $info) {
         if (!file_exists($theme_root . $path . '.lens') && !file_exists($default_template_path . $path . '.lens')) {
             unset($defaults['templates'][$path]);
         }
     }
     foreach ($defaults['routes'] as $url => $info) {
         if (!isset($defaults['templates'][$info['template']])) {
             unset($defaults['routes'][$url]);
         }
     }
     if (isset($template_info['routes'])) {
         $template_info['routes'] = array_merge_custom($defaults['routes'], $template_info['routes']);
     } else {
         $template_info['routes'] = $defaults['routes'];
     }
     if (isset($template_info['templates'])) {
         $template_info['templates'] = array_merge_custom($defaults['templates'], $template_info['templates']);
     } else {
         $template_info['templates'] = $defaults['templates'];
     }
     $files = scandir($theme_root);
     foreach ($files as $file) {
         $info = pathinfo($file);
         if (isset($info['extension']) && $info['extension'] === 'lens' && $info['filename'] !== 'error' && !isset($template_info['templates'][$info['filename']])) {
             $template_info['templates'][$info['filename']] = array('name' => ucfirst(preg_replace('/[^a-z0-9]/', ' ', strtolower($info['filename']))));
         }
     }
     if (isset($template_info['styles'])) {
         if (isset($draft->data['settings']['__style']) && isset($template_info['styles'][$draft->data['settings']['__style']])) {
             $key = $draft->data['settings']['__style'];
         } else {
             $key = $draft->data['settings']['__style'] = array_shift(array_keys($template_info['styles']));
         }
         $template_info['style'] = array_merge(array('key' => $key), $template_info['styles'][$key]);
         $styles = array();
         foreach ($template_info['styles'] as $key => $opts) {
             $styles[] = array_merge(array('key' => $key), $opts);
         }
         $template_info['styles'] = $styles;
     } else {
         $template_info['styles'] = array();
     }
     if ($this->method == 'get') {
         list($data['urls'], $data['url_data'], $routes) = $draft->setup_urls($theme_root);
         if (isset($params['draft'])) {
             function get_live_updates($file, $draft, &$functions)
             {
                 if (file_exists($file)) {
                     // Strip comments so they don't confuse the parser
                     $contents = preg_replace('/\\/\\*.*?\\*\\//si', '', file_get_contents($file));
                     preg_match_all('/@import\\surl\\(.*\\[?\\$([a-z_0-9]+)\\]?.*\\);/', $contents, $imports);
                     foreach ($imports[1] as $setting) {
                         if (!isset($functions[$setting])) {
                             $functions[$setting] = 'reload';
                         }
                     }
                     $contents = preg_replace('/@import\\surl\\(.*\\);/', '', $contents);
                     preg_match_all('/([^\\{]+)\\s*\\{([^\\}]+)\\}/s', $contents, $matches);
                     foreach ($matches[2] as $index => $block) {
                         $selector = $matches[1][$index];
                         preg_match_all('/([a-z\\-]+):([^;]+)( !important)?;/', $block, $rules);
                         foreach ($rules[2] as $j => $rule) {
                             $property = $rules[1][$j];
                             preg_match_all('/\\[?\\$([a-z_0-9]+)\\]?/', $rule, $options);
                             if (count($options)) {
                                 foreach ($options[1] as $option) {
                                     if (!isset($functions[$option])) {
                                         $functions[$option] = array();
                                     } else {
                                         if ($functions[$option] === 'reload') {
                                             continue;
                                         }
                                     }
                                     $functions[$option][] = array('selector' => trim(str_replace("\n", '', $selector)), 'property' => trim($property), 'template' => trim(str_replace('url(', "url(storage/themes/{$draft->path}/", $rule)), 'lightbox' => strpos($file, 'lightbox-settings.css.lens') !== false);
                                 }
                             }
                         }
                     }
                 }
             }
             $functions = array();
             get_live_updates(FCPATH . $ds . 'storage' . $ds . 'themes' . $ds . $draft->path . $ds . 'css' . $ds . 'settings.css.lens', $draft, $functions);
             get_live_updates(FCPATH . $ds . 'storage' . $ds . 'themes' . $ds . $draft->path . $ds . 'css' . $ds . 'lightbox-settings.css.lens', $draft, $functions);
             $template_info['live_updates'] = $functions;
         }
         $pulse_settings = json_decode(file_get_contents($pulse_base), true);
         list($template_info['pulse'], $template_info['pulse_flat']) = $this->_prep_options($pulse_settings);
         if (isset($draft->data['pulse_groups'])) {
             $template_info['pulse_groups'] = $draft->data['pulse_groups'];
             foreach ($template_info['pulse_groups'] as &$group) {
                 if (isset($group['transition_duration']) && is_numeric($group['transition_duration']) && $group['transition_duration'] > 10) {
                     $group['transition_duration'] /= 1000;
                 }
             }
         } else {
             $template_info['pulse_groups'] = array();
         }
         if (!isset($template_info['templates'])) {
             $template_info['templates'] = array();
         }
         if (!isset($template_info['routes'])) {
             $template_info['routes'] = array();
         }
         if (isset($draft->data['routes'])) {
             $template_info['routes'] = array_merge_custom($template_info['routes'], $draft->data['routes']);
         }
         $template_info['navigation'] = $draft->data['navigation'];
         unset($template_info['navigation_groups']);
         $albums_flat = new Album();
         $albums_flat->select('id,level,left_id')->where('deleted', 0)->order_by('left_id ASC')->get_iterated();
         $albums_indexed = array();
         $ceiling = 1;
         foreach ($albums_flat as $a) {
             $albums_indexed[$a->id] = array('level' => (int) $a->level);
             $ceiling = max($a->level, $ceiling);
         }
         $album_keys = array_keys($albums_indexed);
         function nest($nav, $routes, $albums_indexed, $album_keys, $ceiling)
         {
             $l = 1;
             $nested = array();
             while ($l <= $ceiling) {
                 foreach ($nav as $index => $item) {
                     if (preg_match('/^(mailto|https?)/', $item['path']) || !isset($item['auto']) && !isset($routes[$item['path']])) {
                         if ($l === 1) {
                             $nested[] = $item;
                         }
                         continue;
                     }
                     if (isset($routes[$item['path']])) {
                         $r = $routes[$item['path']];
                     } else {
                         $r = false;
                     }
                     if (isset($item['auto']) && in_array($item['auto'], array('set', 'album')) || $r && isset($r['source']) && in_array($r['source'], array('set', 'album'))) {
                         if (isset($item['auto'])) {
                             $id = $item['id'];
                             if ($item['auto'] === 'set') {
                                 $item['set'] = true;
                             }
                         } else {
                             foreach ($r['filters'] as $f) {
                                 if (strpos($f, 'id=') === 0) {
                                     $id = array_pop(explode('=', $f));
                                     break;
                                 }
                             }
                             if ($r['source'] === 'set') {
                                 $item['set'] = true;
                             }
                         }
                         if (isset($albums_indexed[$id])) {
                             $level = $albums_indexed[$id]['level'];
                             if ($level === $l && $l === 1) {
                                 $nested[] = $item;
                                 $albums_indexed[$id]['nav'] =& $nested[count($nested) - 1];
                                 unset($nav[$index]);
                             } else {
                                 if ($level === $l) {
                                     while ($level > 0) {
                                         $level--;
                                         $done = false;
                                         $start = array_search($id, $album_keys);
                                         while ($start > 0) {
                                             $start--;
                                             $_id = $album_keys[$start];
                                             if (array_key_exists($_id, $albums_indexed) && $albums_indexed[$_id]['level'] === $level && isset($albums_indexed[$_id]['nav'])) {
                                                 $albums_indexed[$_id]['nav']['items'][] = $item;
                                                 $albums_indexed[$id]['nav'] =& $albums_indexed[$_id]['nav']['items'][count($albums_indexed[$_id]['nav']['items']) - 1];
                                                 unset($nav[$index]);
                                                 $done = true;
                                                 break;
                                             }
                                         }
                                         if ($done) {
                                             break;
                                         }
                                     }
                                 }
                             }
                         }
                     } else {
                         if ($l === 1) {
                             $nested[] = $item;
                             unset($nav[$index]);
                         }
                     }
                 }
                 $l++;
             }
             return $nested;
         }
         function build_autos($items, $data, $user)
         {
             foreach ($items as $index => &$item) {
                 if (isset($item['auto'])) {
                     if (isset($data['urls'][$item['auto']])) {
                         $item['path'] = $data['urls'][$item['auto']];
                     } else {
                         if ($item['auto'] === 'set') {
                             $item['path'] = '';
                         }
                     }
                     if ($item['auto'] === 'profile') {
                         switch ($item['id']) {
                             case 'twitter':
                                 $item['path'] = 'https://twitter.com/' . $user->twitter;
                                 break;
                             default:
                                 $item['path'] = $user->{$item['id']};
                                 if (empty($item['path'])) {
                                     unset($items[$index]);
                                     continue;
                                 }
                                 break;
                         }
                         if (!isset($item['label']) || empty($item['label'])) {
                             $item['label'] = ucwords($item['id']) . ($item['id'] === 'google' ? '+' : '');
                         }
                     } else {
                         if ($item['auto'] === 'rss') {
                             $item['path'] = '/feed/' . $item['id'] . ($item['id'] === 'essay' ? 's' : '') . '/recent.rss';
                             if (!isset($item['label'])) {
                                 $item['label'] = $data['url_data'][$item['id']]['plural'] . ' RSS';
                             }
                         } else {
                             if (preg_match('/s$/', $item['auto']) || $item['auto'] === 'timeline') {
                                 if ($item['auto'] === 'timeline' && isset($item['year'])) {
                                     $item['path'] .= $item['year'] . '/';
                                     if (isset($item['month']) && $item['month'] !== false && $item['month'] !== 'any') {
                                         $m = str_pad($item['month'], 2, '0', STR_PAD_LEFT);
                                         $item['path'] .= $m . '/';
                                     }
                                 }
                                 if (strpos($item['auto'], '_') !== false) {
                                     foreach (array('id', 'slug', 'month', 'year', 'day') as $id) {
                                         if ($id === 'month') {
                                             if (!isset($item['month']) || $item['month'] === 'any' || $item['month'] === false) {
                                                 $item['month'] = '';
                                             } else {
                                                 $item['month'] = str_pad($item['month'], 2, '0', STR_PAD_LEFT);
                                             }
                                         }
                                         if ($id === 'day' && !isset($item['day'])) {
                                             $item['day'] = '';
                                         }
                                         if ($id === 'slug' && !isset($item['slug']) && isset($item['id'])) {
                                             if (strpos($item['auto'], 'tag_') === 0) {
                                                 $item['slug'] = $item['id'];
                                             } else {
                                                 $c = new Category();
                                                 if (is_numeric($item['id'])) {
                                                     $c->select('slug')->get_by_id($item['id']);
                                                     $item['slug'] = $c->slug;
                                                 } else {
                                                     $item['slug'] = $item['id'];
                                                 }
                                             }
                                         }
                                         if (isset($item[$id])) {
                                             $item['path'] = str_replace(":{$id}", $item[$id], $item['path']);
                                         }
                                     }
                                 } else {
                                     if (!isset($item['label'])) {
                                         $item['label'] = $data['url_data'][$item['auto'] === 'categories' ? 'category' : rtrim($item['auto'], 's')]['plural'];
                                     }
                                 }
                             } else {
                                 if ($item['auto'] === 'home') {
                                     if (!isset($item['label'])) {
                                         $item['label'] = $data['url_data']['home'];
                                     }
                                     $item['path'] = '/home/';
                                 } else {
                                     if ($item['auto'] === 'album' || $item['auto'] === 'set') {
                                         $a = new Album();
                                         $a->select('id,slug,created_on,title');
                                         if (is_numeric($item['id'])) {
                                             $a->where('id', $item['id']);
                                         } else {
                                             $a->where('slug', $item['id'])->or_where('internal_id', $item['id']);
                                         }
                                         $a->get();
                                         if (!$a->exists()) {
                                             unset($items[$index]);
                                             continue;
                                         }
                                         $item['path'] = str_replace(':id', $a->id, $item['path']);
                                         $item['path'] = str_replace(':slug', $a->slug, $item['path']);
                                         $item['path'] = str_replace(':year', date('Y', $a->created_on), $item['path']);
                                         $item['path'] = str_replace(':month', date('m', $a->created_on), $item['path']);
                                         $item['path'] = str_replace(':day', date('d', $a->created_on), $item['path']);
                                         if (!isset($item['label'])) {
                                             $item['label'] = $a->title;
                                         }
                                     } else {
                                         if ($item['auto'] === 'page' || $item['auto'] === 'essay') {
                                             $t = new Text();
                                             $t->select('id,slug,published_on,title');
                                             if (is_numeric($item['id'])) {
                                                 $t->where('id', $item['id']);
                                             } else {
                                                 $t->where('slug', $item['id']);
                                             }
                                             $t->get();
                                             if (!$t->exists()) {
                                                 unset($items[$index]);
                                                 continue;
                                             }
                                             $item['path'] = str_replace(':id', $t->id, $item['path']);
                                             $item['path'] = str_replace(':slug', $t->slug, $item['path']);
                                             $item['path'] = str_replace(':year', date('Y', $t->published_on), $item['path']);
                                             $item['path'] = str_replace(':month', date('m', $t->published_on), $item['path']);
                                             $item['path'] = str_replace(':day', date('d', $t->published_on), $item['path']);
                                             if (!isset($item['label'])) {
                                                 $item['label'] = $t->title;
                                             }
                                         } else {
                                             if ($item['auto'] === 'content') {
                                                 $c = new Content();
                                                 $c->select('id,slug,captured_on,title');
                                                 if (isset($item['album_id'])) {
                                                     $item['path'] = preg_replace('/:(id|slug)/', ':album_$1', $data['urls']['album']) . substr(str_replace(':year/:month/', '', $data['urls']['content']), 1);
                                                     $a = new Album();
                                                     $a->select('id,slug,created_on,title');
                                                     if (is_numeric($item['album_id'])) {
                                                         $a->where('id', $item['album_id']);
                                                     } else {
                                                         $a->where('slug', $item['album_id'])->or_where('internal_id', $item['album_id']);
                                                     }
                                                     $a->get();
                                                     if (!$a->exists()) {
                                                         unset($items[$index]);
                                                         continue;
                                                     }
                                                     $item['path'] = str_replace(':album_id', $a->id, $item['path']);
                                                     $item['path'] = str_replace(':album_slug', $a->slug, $item['path']);
                                                     $date = $a->created_on;
                                                 } else {
                                                     $date = $c->captured_on;
                                                 }
                                                 if (is_numeric($item['id'])) {
                                                     $c->where('id', $item['id']);
                                                 } else {
                                                     $c->where('slug', $item['id'])->or_where('internal_id', $item['id']);
                                                 }
                                                 $c->get();
                                                 if (!$c->exists()) {
                                                     unset($items[$index]);
                                                     continue;
                                                 }
                                                 $item['path'] = str_replace(':id', $c->id, $item['path']);
                                                 $item['path'] = str_replace(':slug', $c->slug, $item['path']);
                                                 $item['path'] = str_replace(':year', date('Y', $date), $item['path']);
                                                 $item['path'] = str_replace(':month', date('m', $date), $item['path']);
                                                 $item['path'] = str_replace(':day', date('d', $date), $item['path']);
                                                 if (!isset($item['label'])) {
                                                     $item['label'] = $c->title;
                                                 }
                                                 if (isset($item['lightbox']) && $item['lightbox']) {
                                                     $item['path'] .= 'lightbox/';
                                                 }
                                             } else {
                                                 if ($item['auto'] === 'tag') {
                                                     $item['path'] = str_replace(':slug', $item['id'], $item['path']);
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                     if ($item['auto'] !== 'profile') {
                         $item['path'] = str_replace(array(':year', ':month'), '', $item['path']);
                         $item['path'] = preg_replace('/[\\(\\)\\?\\:]/', '', $item['path']);
                         $item['path'] = preg_replace('~[/]+~', '/', $item['path']);
                     }
                 }
             }
             return $items;
         }
         $template_info['navigation']['items'] = build_autos($template_info['navigation']['items'], $data, $user);
         $template_info['navigation']['items_nested'] = nest($template_info['navigation']['items'], $template_info['routes'], $albums_indexed, $album_keys, $ceiling);
         foreach ($template_info['navigation']['groups'] as &$group) {
             $group['items'] = build_autos($group['items'], $data, $user);
             $group['items_nested'] = nest($group['items'], $template_info['routes'], $albums_indexed, $album_keys, $ceiling);
         }
         $pages = array();
         $paths = array();
         foreach ($template_info['routes'] as $path => $arr) {
             $pages[] = array_merge(array('path' => (string) $path), $arr);
             $paths[] = $path;
         }
         $template_info['routes'] = $pages;
         if (isset($template_info['settings'])) {
             $default_style_vars = array();
             if (isset($template_info['styles']) && count($template_info['styles'])) {
                 $tmp = array_reverse($template_info['styles']);
                 foreach ($tmp as $style) {
                     if (isset($style['variables'])) {
                         $default_style_vars = array_merge($default_style_vars, $style['variables']);
                     }
                 }
             }
             list($template_info['settings'], $template_info['settings_flat']) = $this->_prep_options($template_info['settings'], isset($draft->data['settings']) ? $draft->data['settings'] : array(), isset($template_info['style']) && isset($template_info['style']['variables']) ? $template_info['style']['variables'] : array(), $default_style_vars);
             if (isset($draft->data['settings']) && isset($draft->data['settings']['__style'])) {
                 $template_info['settings_flat']['__style'] = array('value' => $draft->data['settings']['__style']);
             }
         } else {
             $template_info['settings'] = $template_info['settings_flat'] = array();
         }
         if (isset($template_info['style']) && isset($template_info['style']['variables'])) {
             foreach ($template_info['style']['variables'] as $key => &$varval) {
                 if (preg_match('/#[a-z0-9]{3}$/i', $varval)) {
                     $varval = $varval . substr($varval, 1);
                 }
                 if (!isset($template_info['settings_flat'][$key])) {
                     $template_info['settings_flat'][$key] = array('value' => $varval);
                 }
             }
         }
         $types = array();
         $names = array();
         $templates_indexed = $template_info['templates'];
         foreach ($template_info['templates'] as $key => $val) {
             if (isset($val['source']) && $val['source'] === 'date') {
                 $val['source'] = 'archives';
             }
             $types[] = array('path' => $key, 'info' => $val);
             $names[] = $val['name'];
         }
         natcasesort($names);
         $final = array();
         foreach ($names as $index => $name) {
             $final[] = $types[$index];
         }
         $template_info['templates'] = $final;
         $bools = array('site_hidpi');
         foreach ($site as $s) {
             $clean_key = preg_replace('/^site_/', '', $s->name);
             if (isset($data[$clean_key])) {
                 continue;
             }
             $val = $s->value;
             if (in_array($s->name, $bools)) {
                 $val = $val == 'true';
             }
             $data[$clean_key] = $val;
         }
         $data['draft_id'] = $draft->id;
         $data['theme'] = array('path' => isset($params['preview']) ? $params['preview'] : $draft->path);
         unset($data['id']);
         foreach ($template_info as $key => $val) {
             if (in_array($key, array('name', 'version', 'description', 'demo'))) {
                 $data['theme'][$key] = $val;
             } else {
                 $data[$key] = $val;
             }
         }
         $data['routes'] = array_merge($data['routes'], $routes);
         // templates always need to be after routes
         $templates_tmp = $data['templates'];
         $routes_tmp = $data['routes'];
         unset($data['templates']);
         unset($data['routes']);
         $data['routes'] = $routes_tmp;
         $data['templates'] = Shutter::filter('site.templates', array($templates_tmp));
         $data['profile'] = array('name' => $user->public_display === 'both' ? $user->public_first_name . ' ' . $user->public_last_name : $user->{"public_{$user->public_display}_name"}, 'first' => $user->public_first_name, 'last' => $user->public_last_name, 'email' => $user->public_email, 'twitter' => str_replace('@', '', $user->twitter), 'facebook' => $user->facebook, 'google_plus' => $user->google);
         if (isset($draft->data['custom_css'])) {
             $data['custom_css'] = $draft->data['custom_css'];
         } else {
             $data['custom_css'] = '';
         }
         $this->set_response_data($data);
     } else {
         switch ($this->method) {
             case 'put':
                 global $raw_input_data;
                 $data = json_decode($raw_input_data['data'], true);
                 if (isset($data['revert'])) {
                     if ($data['revert'] === 'all') {
                         $draft->data = $draft->live_data;
                     } else {
                         unset($draft->data['settings']);
                         $draft->data = json_encode($draft->data);
                     }
                 } else {
                     if (isset($data['custom_css'])) {
                         $draft->data['custom_css'] = $data['custom_css'];
                     }
                     if (isset($data['navigation'])) {
                         unset($data['navigation']['active']);
                         $draft->data['navigation'] = $data['navigation'];
                     }
                     if (isset($data['routes'])) {
                         $pages = array();
                         foreach ($data['routes'] as $p) {
                             if (isset($p['section'])) {
                                 continue;
                             }
                             $key = $p['path'];
                             unset($p['path']);
                             if (!in_array($p, $template_info['routes'])) {
                                 $pages[$key] = $p;
                             }
                         }
                         $draft->data['routes'] = $pages;
                     }
                     if (isset($data['settings_send'])) {
                         foreach ($data['settings_send'] as $key => $val) {
                             $draft->data['settings'][$key] = $val;
                         }
                     }
                     if (isset($data['url_data_send'])) {
                         $source = $data['url_data_send']['source'] === 'categories' ? 'category' : rtrim($data['url_data_send']['source'], 's');
                         $u = new Url();
                         $u->order_by('id DESC')->get();
                         $new_data = unserialize($u->data);
                         foreach ($new_data as &$url_data) {
                             if ($url_data['type'] === $source) {
                                 $url_data['data'][$data['url_data_send']['order']] = $data['url_data_send']['value'];
                                 break;
                             }
                         }
                         $u->data = serialize($new_data);
                         $u->save();
                     }
                     if (isset($data['pulse_settings_send']) && !empty($data['pulse_settings_send'])) {
                         if (!isset($draft->data['pulse_groups'][$data['pulse_settings_group']])) {
                             $draft->data['pulse_groups'][$data['pulse_settings_group']] = array();
                         }
                         foreach ($data['pulse_settings_send'] as $key => $val) {
                             $draft->data['pulse_groups'][$data['pulse_settings_group']][$key] = $val;
                         }
                     }
                     $draft->data = json_encode($draft->data);
                 }
                 $draft->save();
                 $this->redirect("/site/draft:true");
                 break;
         }
     }
 }
Example #22
0
 function check_for_rewrite()
 {
     if (defined('KOKEN_REWRITE')) {
         return KOKEN_REWRITE;
     }
     if (!file_exists(FCPATH . '.htaccess') && strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') === 0) {
         define('KOKEN_REWRITE', false);
         return false;
     }
     $cache_key = 'core/rewrite_check';
     $cache = Shutter::get_cache($cache_key, false);
     if ($cache !== false) {
         define('KOKEN_REWRITE', trim($cache['data']) === 'on');
         return KOKEN_REWRITE;
     }
     $s = new Setting();
     $s->where('name', 'site_url')->get();
     if ($s->value === 'default') {
         $koken_url_info = $this->config->item('koken_url_info');
         $url = $koken_url_info->base . '__rewrite_test/';
     } else {
         $url = 'http://' . $_SERVER['HTTP_HOST'] . rtrim($s->value, '/') . '/__rewrite_test/';
     }
     $headers = array('Cache-Control: must-revalidate');
     if (LOOPBACK_HOST_HEADER) {
         $host = $_SERVER['SERVER_ADDR'] . ':' . $_SERVER['SERVER_PORT'];
         $headers[] = 'Host: ' . $_SERVER['HTTP_HOST'];
         $url = str_replace($_SERVER['HTTP_HOST'], $host, $url);
     }
     $curl = curl_init();
     curl_setopt($curl, CURLOPT_URL, $url);
     curl_setopt($curl, CURLOPT_HEADER, 0);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     if (strpos($url, 'https://') === 0) {
         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
     }
     curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
     $return = trim(curl_exec($curl));
     $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
     curl_close($curl);
     $rewrite_check = $code === 200 && $return === 'koken:rewrite';
     Shutter::write_cache($cache_key, $rewrite_check ? 'on' : 'off');
     define('KOKEN_REWRITE', $rewrite_check);
     return $rewrite_check;
 }
Example #23
0
 function index()
 {
     list($params, $id, $slug) = $this->parse_params(func_get_args());
     // Create or update
     if ($this->method != 'get') {
         $c = new Content();
         switch ($this->method) {
             case 'post':
             case 'put':
                 if ($this->method == 'put') {
                     // Update
                     $c->get_by_id($id);
                     if (!$c->exists()) {
                         $this->error('404', "Content with ID: {$id} not found.");
                         return;
                     }
                     $c->old_published_on = $c->published_on;
                     $c->old_captured_on = $c->captured_on;
                     $c->old_uploaded_on = $c->uploaded_on;
                     if (isset($_POST['slug'])) {
                         $c->current_slug = $c->slug;
                     }
                 }
                 if (isset($_REQUEST['name'])) {
                     if (isset($_REQUEST['upload_session_start'])) {
                         $s = new Setting();
                         $s->where('name', 'last_upload')->get();
                         if ($s->exists() && $s->value != $_REQUEST['upload_session_start']) {
                             $s->value = $_REQUEST['upload_session_start'];
                             $s->save();
                         }
                     }
                     $file_name = $c->clean_filename($_REQUEST['name']);
                     $chunk = isset($_REQUEST["chunk"]) ? $_REQUEST["chunk"] : 0;
                     $chunks = isset($_REQUEST["chunks"]) ? $_REQUEST["chunks"] : 0;
                     $tmp_dir = FCPATH . 'storage' . DIRECTORY_SEPARATOR . 'tmp';
                     $tmp_path = $tmp_dir . DIRECTORY_SEPARATOR . $file_name;
                     make_child_dir($tmp_dir);
                     if ($chunks == 0 || $chunk == $chunks - 1) {
                         if (isset($_REQUEST['text'])) {
                             $path = FCPATH . 'storage' . DIRECTORY_SEPARATOR . 'custom' . DIRECTORY_SEPARATOR;
                             $internal_id = false;
                         } else {
                             if (isset($_REQUEST['plugin'])) {
                                 $info = pathinfo($_REQUEST['name']);
                                 $path = FCPATH . 'storage' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $_REQUEST['plugin'] . DIRECTORY_SEPARATOR . 'storage' . DIRECTORY_SEPARATOR;
                                 $file_name = $_REQUEST['basename'] . '.' . $info['extension'];
                                 $internal_id = false;
                             } else {
                                 list($internal_id, $path) = $c->generate_internal_id();
                             }
                         }
                         if ($path) {
                             $path .= $file_name;
                             if ($chunks == 0) {
                                 $tmp_path = $path;
                             }
                         } else {
                             $this->error('500', 'Unable to create directory for upload.');
                             return;
                         }
                     }
                     // Look for the content type header
                     if (isset($_SERVER["HTTP_CONTENT_TYPE"])) {
                         $contentType = $_SERVER["HTTP_CONTENT_TYPE"];
                     } else {
                         if (isset($_SERVER["CONTENT_TYPE"])) {
                             $contentType = $_SERVER["CONTENT_TYPE"];
                         } else {
                             $contentType = '';
                         }
                     }
                     if (strpos($contentType, "multipart") !== false) {
                         if (isset($_FILES['file']['tmp_name']) && is_uploaded_file($_FILES['file']['tmp_name'])) {
                             $out = fopen($tmp_path, $chunk == 0 ? "wb" : "ab");
                             if ($out) {
                                 // Read binary input stream and append it to temp file
                                 $in = fopen($_FILES['file']['tmp_name'], "rb");
                                 if ($in) {
                                     while ($buff = fread($in, 4096)) {
                                         fwrite($out, $buff);
                                     }
                                 } else {
                                     $this->error('500', 'Unable to read input stream.');
                                     return;
                                 }
                                 fclose($out);
                                 unlink($_FILES['file']['tmp_name']);
                             } else {
                                 $this->error('500', 'Unable to write to output file.');
                                 return;
                             }
                         } else {
                             $this->error('500', 'Unable to move uploaded file.');
                             return;
                         }
                     } else {
                         $out = fopen($tmp_path, $chunk == 0 ? "wb" : "ab");
                         if ($out) {
                             // Read binary input stream and append it to temp file
                             $in = fopen("php://input", "rb");
                             if ($in) {
                                 while ($buff = fread($in, 4096)) {
                                     fwrite($out, $buff);
                                 }
                             } else {
                                 $this->error('500', 'Unable to read uploaded file.');
                                 return;
                             }
                             fclose($out);
                         } else {
                             $this->error('500', 'Unable to open output stream.');
                             return;
                         }
                     }
                     if ($chunk < $chunks - 1) {
                         // Don't continue until all chunks are uploaded
                         exit;
                     } else {
                         if ($chunks > 0) {
                             // Done, move to permanent location and save to DB
                             rename($tmp_path, $path);
                         }
                     }
                     if (!$internal_id) {
                         // Custom text uploads can stop here
                         die(json_encode(array('filename' => $file_name)));
                     }
                     $from = array();
                     $from['filename'] = $file_name;
                     $from['internal_id'] = $internal_id;
                     $from['file_modified_on'] = time();
                 } else {
                     if (isset($_POST['localfile'])) {
                         $filename = basename($_REQUEST['localfile']);
                         list($internal_id, $path) = $c->generate_internal_id();
                         if (!file_exists($_REQUEST['localfile'])) {
                             $this->error('500', '"localfile" does not exist.');
                             return;
                         }
                         if ($path) {
                             $path .= $filename;
                         } else {
                             $this->error('500', 'Unable to create directory for upload.');
                             return;
                         }
                         copy($_REQUEST['localfile'], $path);
                         $from = array();
                         $from['filename'] = $filename;
                         $from['internal_id'] = $internal_id;
                         $from['file_modified_on'] = time();
                     } else {
                         if (isset($_POST['from_url'])) {
                             $filename = basename($_POST['from_url']);
                             list($internal_id, $path) = $c->generate_internal_id();
                             if ($path) {
                                 $path .= $filename;
                             } else {
                                 $this->error('500', 'Unable to create directory for upload.');
                                 return;
                             }
                             if ($this->_download(urldecode($_POST['from_url']), $path, true) && file_exists($path)) {
                                 $from = array();
                                 $from['filename'] = $filename;
                                 $from['internal_id'] = $internal_id;
                                 $from['file_modified_on'] = time();
                             } else {
                                 $this->error('500', 'Unable to import file from provided URL.');
                                 return;
                             }
                         } else {
                             if (is_null($id)) {
                                 $this->error('403', 'New content records must be accompanied by an upload.');
                                 return;
                             }
                         }
                     }
                 }
                 if (isset($from)) {
                     $from = array_merge($_POST, $from);
                 } else {
                     $from = $_POST;
                 }
                 if (isset($_REQUEST['rotate']) && is_numeric($_REQUEST['rotate']) && $c->exists()) {
                     $r = $_REQUEST['rotate'];
                     if (abs($r) != 90) {
                         $this->error('403', 'Rotation can only be done in multiples of 90.');
                         return;
                     }
                     if (empty($c->storage_url)) {
                         $path = $c->path_to_original();
                         $info = pathinfo($path);
                         $midsize_path = preg_replace('/\\.' . $info['extension'] . '$/', '.1600.' . $info['extension'], $path);
                         if (file_exists($midsize_path)) {
                             $midsize = $midsize_path;
                         }
                     } else {
                         $path = tempnam(sys_get_temp_dir(), 'original');
                         file_put_contents($path, file_get_contents($c->storage_url));
                         if (!empty($c->storage_url_midsize)) {
                             $midsize = tempnam(sys_get_temp_dir(), 'midsize');
                             file_put_contents($midsize, file_get_contents($c->storage_url_midsize));
                         }
                     }
                     $s = new Setting();
                     $s->where('name', 'image_processing_library')->get();
                     include_once FCPATH . 'app' . DIRECTORY_SEPARATOR . 'koken' . DIRECTORY_SEPARATOR . 'DarkroomUtils.php';
                     $d = DarkroomUtils::init($s->value);
                     $d->rotate($path, $r);
                     if (isset($midsize)) {
                         $d->rotate($midsize, $r);
                     }
                     if (!empty($c->storage_url)) {
                         $key = $c->path . '/' . $c->filename;
                         Shutter::store_original($path, $c->path . '/' . $c->filename);
                         unlink($path);
                         if (isset($midsize)) {
                             $info = pathinfo($key);
                             $key = preg_replace('/\\.' . $info['extension'] . '$/', '.1600.' . $info['extension'], $key);
                             Shutter::store_original($midsize, $key);
                             unlink($midsize);
                         }
                     }
                     $c->clear_cache();
                     $from['width'] = $c->height;
                     $from['height'] = $c->width;
                     $from['aspect_ratio'] = $from['width'] / $from['height'];
                     $from['file_modified_on'] = time();
                 }
                 if (isset($_REQUEST['reset_internal_id']) && $_REQUEST['reset_internal_id'] && $c->exists()) {
                     list($from['internal_id'], ) = $c->generate_internal_id(true);
                 }
                 $hook = 'content.' . ($id ? 'update' : 'create');
                 if (isset($from['filename']) && $id) {
                     $c->clear_cache();
                     $hook .= '_with_upload';
                     $c->_before();
                 }
                 $from = Shutter::filter("api.{$hook}", array_merge($from, array('id' => $id, 'file' => isset($path) ? $path : $c->path_to_original())));
                 unset($from['file']);
                 try {
                     $c->from_array($from, array(), true);
                 } catch (Exception $e) {
                     $this->error('400', $e->getMessage());
                     return;
                 }
                 if (isset($_POST['tags'])) {
                     $c->_format_tags($_POST['tags']);
                 } else {
                     if ($this->method === 'put' && isset($_POST['visibility'])) {
                         $c->_update_tag_counts();
                     }
                 }
                 $c->_readify();
                 $content = $c->to_array(array('auth' => true));
                 if ($hook === 'content.create' || $hook === 'content.update_with_upload') {
                     if (ENVIRONMENT === 'production') {
                         $this->load->library('mcurl');
                         if ($this->mcurl->is_enabled()) {
                             $options = array(CURLOPT_HTTPHEADER => array('Connection: Close', 'Keep-Alive: 0'));
                             $this->mcurl->add_call('normal', 'get', $content['presets']['medium_large']['url'], array(), $options);
                             $this->mcurl->add_call('cropped', 'get', $content['presets']['medium_large']['cropped']['url'], array(), $options);
                             $this->mcurl->execute();
                         }
                     }
                     $external_storage_url = Shutter::store_original($c->path_to_original(), str_replace('/storage/originals/', '', $content['original']['relative_url']));
                     if ($external_storage_url) {
                         unlink($c->path_to_original());
                         $o = new Content();
                         $o->where('id', $content['id'])->update(array('storage_url' => $external_storage_url));
                         $content['storage_url'] = $external_storage_url;
                     }
                 }
                 Shutter::hook($hook, $content);
                 // Important to prevent failures from Lr plugin
                 header('Connection: close');
                 $this->redirect("/content/{$c->id}" . (isset($params['context']) ? '/context:' . $params['context'] : ''));
                 break;
             case 'delete':
                 if (is_null($id)) {
                     $this->error('403', 'Required parameter "id" not present.');
                     return;
                 } else {
                     $t = new Tag();
                     if (is_numeric($id)) {
                         $content = $c->get_by_id($id);
                         if ($c->exists()) {
                             $trash = new Trash();
                             $this->db->query("DELETE from {$trash->table} WHERE id = 'content-{$c->id}'");
                             $c->do_delete();
                         } else {
                             $this->error('404', "Content with ID: {$id} not found.");
                             return;
                         }
                     } else {
                         $is_trash = $id === 'trash';
                         if ($id === 'trash') {
                             $id = array();
                             $trash = new Trash();
                             $trash->like('id', 'content-')->select_func('REPLACE', '@id', 'content-', '', 'actual_id')->get_iterated();
                             foreach ($trash as $item) {
                                 $id[] = (int) $item->actual_id;
                             }
                         } else {
                             $id = explode(',', $id);
                         }
                         /*
                         	Multiple delete
                          	/content/n1/n2/n3
                         */
                         // Keep track of tags to --
                         $tags = array();
                         $c->where_in('id', $id);
                         $contents = $c->get_iterated();
                         $trash = new Trash();
                         foreach ($contents as $c) {
                             if ($c->exists()) {
                                 $tags = array_merge($tags, $c->tags);
                                 $this->db->query("DELETE from {$trash->table} WHERE id = 'content-{$c->id}'");
                                 $c->do_delete();
                             }
                         }
                     }
                 }
                 exit;
                 break;
         }
     }
     $c = new Content();
     if ($slug || isset($id) && strpos($id, ',') === false) {
         $options = array('context' => false, 'neighbors' => false);
         $options = array_merge($options, $params);
         $original_context = $options['context'];
         if ($options['context'] && !in_array($options['context'], array('stream', 'favorites', 'features')) && strpos($options['context'], 'tag-') !== 0 && strpos($options['context'], 'category-') !== 0) {
             if (is_numeric($options['context'])) {
                 $context_field = 'id';
             } else {
                 $context_field = 'slug';
                 $options['context'] = str_replace('slug-', '', $options['context']);
             }
             $a = new Album();
             $a->group_start()->where($context_field, $options['context'])->or_where('internal_id', $options['context'])->group_end()->get();
             $c->include_join_fields()->where_related_album('id', $a->id);
         }
         $with_token = false;
         if (is_numeric($id)) {
             $content = $c->where('deleted', 0)->get_by_id($id);
         } else {
             if ($slug) {
                 $content = $c->where('deleted', 0)->group_start()->where('internal_id', $slug)->or_where('slug', $slug)->or_like('old_slug', ',' . $slug . ',', 'both')->group_end()->get();
             } else {
                 $content = $c->where('deleted', 0)->where('internal_id', $id)->get();
             }
             if ($content->exists() && $content->internal_id === (is_null($id) ? $slug : $id)) {
                 $with_token = true;
             }
         }
         if ($content->exists()) {
             if ($c->visibility == 1 && !$this->auth && !$with_token || !$this->auth && !is_numeric($id) && $c->visibility == 2) {
                 $this->error('403', 'Private content.');
                 return;
             }
             $options['auth'] = $this->auth;
             if ($options['neighbors']) {
                 // Make sure $neighbors is at least 2
                 $options['neighbors'] = max($options['neighbors'], 2);
                 // Make sure neighbors is even
                 if ($options['neighbors'] & 1 != 0) {
                     $options['neighbors']++;
                 }
                 $options['neighbors'] = $options['neighbors'] / 2;
                 $single_neighbors = false;
             } else {
                 $options['neighbors'] = 1;
                 $single_neighbors = true;
             }
             if ($options['context'] && !in_array($original_context, array('stream', 'favorites', 'features')) && strpos($original_context, 'tag-') !== 0 && strpos($original_context, 'category-') !== 0) {
                 $options['in_album'] = $a;
             }
             $final = $content->to_array($options);
             if ($options['context']) {
                 // TODO: Performance check
                 $next = new Content();
                 $prev = new Content();
                 $in_a = new Album();
                 $next->where('deleted', 0);
                 $prev->where('deleted', 0);
                 $options['context'] = urldecode($options['context']);
                 if (!in_array($original_context, array('stream', 'favorites', 'features')) && strpos($original_context, 'tag-') !== 0 && strpos($original_context, 'category-') !== 0) {
                     if (!isset($options['context_order'])) {
                         list($options['context_order'], $options['context_order_direction']) = explode(' ', $a->sort);
                     }
                     $final['context']['album'] = $a->to_array(array('auth' => $this->auth || $options['context'] === $a->internal_id));
                     $in_a->where("{$context_field} !=", $options['context']);
                     $next->where_related_album('id', $a->id);
                     $prev->where_related_album('id', $a->id);
                     if ($options['context_order'] === 'manual') {
                         $next->order_by_join_field('album', 'order', 'ASC')->group_start()->where_join_field('album', 'order >', $content->join_order)->or_group_start()->where_join_field('album', 'order', $content->join_order)->where_join_field('album', 'id >', $content->join_id)->group_end()->group_end();
                         $prev->order_by_join_field('album', 'order', 'DESC')->group_start()->where_join_field('album', 'order <', $content->join_order)->or_group_start()->where_join_field('album', 'order', $content->join_order)->where_join_field('album', 'id <', $content->join_id)->group_end()->group_end();
                     } else {
                         $next_operator = strtolower($options['context_order_direction']) === 'desc' ? '<' : '>';
                         $prev_operator = $next_operator === '<' ? '>' : '<';
                         $next->group_start()->where($options['context_order'] . " {$next_operator}", $content->{$options['context_order']})->or_group_start()->where($options['context_order'], $content->{$options['context_order']})->where("id {$next_operator}", $content->id)->group_end()->group_end();
                         $prev->group_start()->where($options['context_order'] . " {$prev_operator}", $content->{$options['context_order']})->or_group_start()->where($options['context_order'], $content->{$options['context_order']})->where("id {$prev_operator}", $content->id)->group_end()->group_end();
                     }
                     if (!$this->auth) {
                         $next->where('visibility <', $final['context']['album']['visibility'] < 1 ? 1 : 2);
                         $prev->where('visibility <', $final['context']['album']['visibility'] < 1 ? 1 : 2);
                     }
                     $in_album = $a;
                     $final['context']['type'] = 'album';
                     $final['context']['title'] = $a->title;
                     $final['context']['__koken_url'] = $final['context']['album']['__koken_url'];
                     $final['context']['url'] = $final['context']['album']['url'];
                 } else {
                     if (!isset($options['context_order'])) {
                         $options['context_order'] = 'captured_on';
                         $options['context_order_direction'] = 'DESC';
                     } else {
                         if ($options['context_order'] === 'manual' && $original_context === 'favorites') {
                             $options['context_order'] = 'favorite_order';
                             $options['context_order_direction'] = 'ASC';
                         } else {
                             if ($options['context_order'] === 'manual' && $original_context === 'features') {
                                 $options['context_order'] = 'featured_order';
                                 $options['context_order_direction'] = 'ASC';
                             }
                         }
                     }
                     $next_operator = strtolower($options['context_order_direction']) === 'desc' ? '<' : '>';
                     $prev_operator = $next_operator === '<' ? '>' : '<';
                     $next->group_start()->where($options['context_order'] . " {$next_operator}", $content->{$options['context_order']})->or_group_start()->where($options['context_order'], $content->{$options['context_order']})->where("id {$next_operator}", $content->id)->group_end()->group_end();
                     $prev->group_start()->where($options['context_order'] . " {$prev_operator}", $content->{$options['context_order']})->or_group_start()->where($options['context_order'], $content->{$options['context_order']})->where("id {$prev_operator}", $content->id)->group_end()->group_end();
                     if (strpos($original_context, 'tag-') === 0) {
                         $tag = str_replace('tag-', '', urldecode($original_context));
                         $t = new Tag();
                         $t->where('name', $tag)->get();
                         if ($t->exists()) {
                             $next->where_related_tag('id', $t->id);
                             $prev->where_related_tag('id', $t->id);
                             $final['context']['type'] = 'tag';
                             $final['context']['title'] = $tag;
                             $final['context']['slug'] = $tag;
                             $t->model = 'tag_contents';
                             $t->slug = $t->name;
                             $url = $t->url();
                             if ($url) {
                                 list($final['context']['__koken_url'], $final['context']['url']) = $url;
                             }
                         }
                     } else {
                         if (strpos($original_context, 'category-') === 0) {
                             $category = str_replace('category-', '', $original_context);
                             $cat = new Category();
                             $cat->where('slug', $category)->get();
                             if ($cat->exists()) {
                                 $next->where_related_category('id', $cat->id);
                                 $prev->where_related_category('id', $cat->id);
                                 $final['context']['type'] = 'category';
                                 $final['context']['title'] = $cat->title;
                                 $final['context']['slug'] = $cat->slug;
                                 $cat->model = 'category_contents';
                                 $url = $cat->url();
                                 if ($url) {
                                     list($final['context']['__koken_url'], $final['context']['url']) = $url;
                                 }
                             }
                         } else {
                             if ($original_context === 'favorites') {
                                 $url_data = $prev->get_data();
                                 $urls = $prev->form_urls();
                                 $next->where('favorite', 1);
                                 $prev->where('favorite', 1);
                                 $final['context']['type'] = 'favorite';
                                 $final['context']['title'] = $url_data['favorite']['plural'];
                                 $final['context']['__koken_url'] = $urls['favorites'];
                                 if ($final['context']['__koken_url']) {
                                     $final['context']['url'] = $prev->get_base() . $final['context']['__koken_url'] . (defined('DRAFT_CONTEXT') && !is_numeric(DRAFT_CONTEXT) ? '&preview=' . DRAFT_CONTEXT : '');
                                 }
                             } else {
                                 if ($original_context === 'features') {
                                     $url_data = $prev->get_data();
                                     $urls = $prev->form_urls();
                                     $next->where('featured', 1);
                                     $prev->where('featured', 1);
                                     $final['context']['type'] = 'feature';
                                     $final['context']['title'] = $url_data['feature']['plural'];
                                     $final['context']['__koken_url'] = isset($urls['features']) ? $urls['features'] : false;
                                     if ($final['context']['__koken_url']) {
                                         $final['context']['url'] = $prev->get_base() . $final['context']['__koken_url'] . (defined('DRAFT_CONTEXT') && !is_numeric(DRAFT_CONTEXT) ? '&preview=' . DRAFT_CONTEXT : '');
                                     }
                                 }
                             }
                         }
                     }
                     if (!$this->auth) {
                         $next->where('visibility', 0);
                         $prev->where('visibility', 0);
                     }
                     $in_album = false;
                 }
                 $max = $next->get_clone()->count();
                 $min = $prev->get_clone()->count();
                 $final['context']['total'] = $max + $min + 1;
                 $final['context']['position'] = $min + 1;
                 $pre_limit = $next_limit = $options['neighbors'];
                 if ($min < $pre_limit) {
                     $next_limit += $pre_limit - $min;
                     $pre_limit = $min;
                 }
                 if ($max < $next_limit) {
                     $pre_limit = min($min, $pre_limit + ($next_limit - $max));
                     $next_limit = $max;
                 }
                 $final['context']['previous'] = array();
                 $final['context']['next'] = array();
                 if ($next_limit > 0) {
                     if ($options['context_order'] !== 'manual') {
                         $next->order_by($options['context_order'] . ' ' . $options['context_order_direction'] . ', id ' . $options['context_order_direction']);
                     }
                     $next->limit($next_limit)->get_iterated();
                     foreach ($next as $c) {
                         $final['context']['next'][] = $c->to_array(array('auth' => $this->auth, 'in_album' => $in_album, 'context' => $original_context));
                     }
                 }
                 if ($pre_limit > 0) {
                     if ($options['context_order'] !== 'manual') {
                         $dir = strtolower($options['context_order_direction']) === 'desc' ? 'asc' : 'desc';
                         $prev->order_by($options['context_order'] . ' ' . $dir . ', id ' . $dir);
                     }
                     $prev->limit($pre_limit)->get_iterated();
                     foreach ($prev as $c) {
                         $final['context']['previous'][] = $c->to_array(array('auth' => $this->auth, 'in_album' => $in_album, 'context' => $original_context));
                     }
                     $final['context']['previous'] = array_reverse($final['context']['previous']);
                 }
             }
         } else {
             $this->error('404', "Content with ID: {$id} not found.");
             return;
         }
     } else {
         if (isset($params['custom'])) {
             $final = $c->to_array_custom($params['custom']);
         } else {
             $c->where('deleted', 0);
             $params['auth'] = $this->auth;
             $final = $c->listing($params, $id);
         }
     }
     $this->set_response_data($final);
 }
Example #24
0
 function to_array($options = array())
 {
     $options = array_merge(array('auth' => false, 'render' => true, 'expand' => false), $options);
     $koken_url_info = $this->config->item('koken_url_info');
     $exclude = array('deleted', 'total_count', 'video_count', 'audio_count', 'featured_image_id', 'custom_featured_image', 'tags_old', 'old_slug');
     $dates = array('created_on', 'modified_on', 'published_on', 'featured_on');
     $strings = array('title', 'content', 'excerpt');
     $bools = array('published', 'featured');
     if (!$this->published) {
         $this->published_on = time();
     }
     list($data, $public_fields) = $this->prepare_for_output($options, $exclude, $bools, $dates, $strings);
     if (strlen(trim($data['draft'])) === 0) {
         $data['draft'] = $data['content'];
     }
     if (strlen(trim($data['draft_title'])) === 0) {
         $data['draft_title'] = $data['title'];
     }
     if (!$data['featured']) {
         unset($data['featured_on']);
     }
     if ($data['page_type'] != 0) {
         unset($data['featured']);
         unset($data['featured_on']);
     }
     if (!$options['auth']) {
         unset($data['internal_id']);
         unset($data['draft']);
         unset($data['draft_title']);
     }
     if (array_key_exists('page_type', $data)) {
         switch ($data['page_type']) {
             case 1:
                 $data['page_type'] = 'page';
                 break;
             default:
                 $data['page_type'] = 'essay';
         }
     }
     $data['__koken__'] = $data['page_type'];
     $data['tags'] = $this->_get_tags_for_output($options);
     $data['categories'] = array('count' => is_null($this->category_count) ? $this->categories->count() : (int) $this->category_count, 'url' => $koken_url_info->base . 'api.php?/text/' . $data['id'] . '/categories');
     $data['topics'] = array('count' => is_null($this->album_count) ? $this->albums->count() : (int) $this->album_count, 'url' => $koken_url_info->base . 'api.php?/text/' . $data['id'] . '/topics');
     if (is_numeric($this->featured_image_id) && !$this->featured_image->id) {
         $this->featured_image->get();
     }
     if ($this->featured_image->id && $this->featured_image->deleted == 0) {
         $data['featured_image'] = $this->featured_image->to_array();
     } else {
         if (!empty($this->custom_featured_image)) {
             $c = new Content();
             $data['featured_image'] = $c->to_array_custom($this->custom_featured_image);
         } else {
             $data['featured_image'] = false;
         }
     }
     $rendered = Shutter::shortcodes($data['content'], array($this, $options));
     if ($options['render']) {
         if ($options['expand']) {
             $rendered = preg_replace('/\\[read_more([^\\]]+)?\\]/', '<a id="more"></a>', $rendered);
         } else {
             $more = strpos($rendered, '[read_more');
             if ($more !== false) {
                 preg_match('/\\[read_more(?:\\s*label="(.*)")?\\]/', $rendered, $matches);
                 $rendered = substr($rendered, 0, $more);
                 $data['read_more'] = true;
                 $data['read_more_label'] = count($matches) > 1 ? $matches[1] : 'Read more';
             }
         }
     }
     if (!isset($data['read_more'])) {
         $data['read_more'] = false;
     }
     preg_match_all('/<koken:load source="content" filter:id="(\\d+)">/', $rendered, $loads);
     if (count($loads[0]) > 1) {
         $this->ids_for_array_index = array_unique($loads[1]);
         $rendered = '<koken:load source="contents" filter:id="' . join(',', $this->ids_for_array_index) . '">' . $rendered . '</koken:load>';
         $rendered = preg_replace_callback('/<koken:load source="content" filter:id="(\\d+)">(.*)<\\/koken:load>/msU', array($this, '_array_index_callback'), $rendered);
     }
     if (empty($options) || isset($options['render']) && $options['render']) {
         $data['content'] = $rendered;
         if (isset($data['draft'])) {
             $data['draft'] = Shutter::shortcodes($data['draft'], array($this, $options));
         }
     }
     if (empty($data['excerpt'])) {
         $rendered = preg_replace('/<script.*>.*?<\\/script>/msU', '', $rendered);
         $rendered = preg_replace('/<figure class="k-content-embed">.*?<\\/figure>/msU', '', $rendered);
         $clean_parts = explode(' ', preg_replace('/([\\.\\?\\!]+)([^\\s]\\s*[a-z][a-z\\s]*)/', '$1 $2', trim(strip_tags(preg_replace('/\\{\\{.*\\}\\}/', '', html_entity_decode($rendered))))));
         $excerpt = '';
         while (count($clean_parts) && ($next = array_shift($clean_parts)) && strlen(trim($excerpt) . ' ' . trim($next)) <= 254) {
             $excerpt .= ' ' . trim($next);
         }
         $data['excerpt'] = trim($excerpt);
         if (count($clean_parts)) {
             $data['excerpt'] = preg_replace('/[^\\w]$/u', '', $data['excerpt']) . '…';
         }
         $more = strpos($data['excerpt'], '[read_more');
         if ($more !== false) {
             $data['excerpt'] = trim(substr($data['excerpt'], 0, $more));
         }
     }
     if (isset($options['order_by']) && in_array($options['order_by'], array('created_on', 'modified_on', 'published_on'))) {
         $data['date'] =& $data[$options['order_by']];
     } else {
         if ($data['page_type'] === 'essay') {
             $data['date'] =& $data['published_on'];
         }
     }
     $cat = isset($options['category']) ? $options['category'] : (isset($options['context']) && strpos($options['context'], 'category-') === 0 ? str_replace('category-', '', $options['context']) : false);
     if ($cat) {
         if (is_numeric($cat)) {
             foreach ($this->categories->get_iterated() as $c) {
                 if ($c->id == $cat) {
                     $cat = $c->slug;
                     break;
                 }
             }
         }
     }
     $data['url'] = $this->url(array('date' => $data['published_on'], 'tag' => isset($options['tags']) ? $options['tags'] : (isset($options['context']) && strpos($options['context'], 'tag-') === 0 ? str_replace('tag-', '', $options['context']) : false), 'category' => $cat));
     if ($data['url']) {
         list($data['__koken_url'], $data['url']) = $data['url'];
         $data['canonical_url'] = $data['url'];
     }
     return Shutter::filter('api.text', array($data, $this, $options));
 }
Example #25
0
 public static function register_storage_handler($handler)
 {
     if (in_array(get_class($handler), self::$active_plugins) && in_array('KokenOriginalStore', class_implements($handler))) {
         self::$original_storage_handler = $handler;
     }
 }
Example #26
0
        exit("Your application folder path does not appear to be set correctly. Please open the following file and correct this: " . SELF);
    }
    define('APPPATH', BASEPATH . $application_folder . '/');
}
/*
 * --------------------------------------------------------------------
 * INIT SHUTTER (KOKEN API)
 * -------------------------------------------------------------------- *
 */
@(include FCPATH . 'storage/configuration/user_setup.php');
if (!defined('LOOPBACK_HOST_HEADER')) {
    define('LOOPBACK_HOST_HEADER', false);
}
require_once FCPATH . 'app/koken/Shutter/Shutter.php';
Shutter::enable();
Shutter::hook('api.boot');
/*
 * --------------------------------------------------------------------
 * LOAD THE DATAMAPPER BOOTSTRAP FILE
 * -------------------------------------------------------------------- *
 */
require_once APPPATH . 'third_party/datamapper/bootstrap.php';
/*
 * --------------------------------------------------------------------
 * LOAD THE BOOTSTRAP FILE
 * --------------------------------------------------------------------
 *
 * And away we go...
 *
 */
require_once BASEPATH . 'core/CodeIgniter.php';