Example #1
0
/**
 * Replaces the content of the 'body' part if a language specific part exists.
 *
 * @param Page $page Page object.
 */
function replaceContent($page)
{
    $source = Plugin::getSetting('langsource', 'multi_lang');
    $style = Plugin::getSetting('style', 'multi_lang');
    if (!$source || !$style) {
        return;
    }
    if ($source == 'header' && $style == 'tab') {
        use_helper('I18n');
        $found = false;
        foreach (I18n::getPreferredLanguages() as $lang) {
            if (Setting::get('language') == $lang) {
                break;
            }
            if (isset($page->part->{$lang}) && !empty($page->part->{$lang}->content_html) && $page->part->{$lang}->content_html != '') {
                $page->part->body->content_html = $page->part->{$lang}->content_html;
                $found = true;
            }
            if ($found) {
                break;
            }
        }
    } else {
        if ($source == 'preferences' && $style == 'tab') {
            AuthUser::load();
            if (AuthUser::isLoggedIn()) {
                $lang = AuthUser::getRecord()->language;
                if (isset($page->part->{$lang}) && !empty($page->part->{$lang}->content_html) && $page->part->{$lang}->content_html != '') {
                    $page->part->body->content_html = $page->part->{$lang}->content_html;
                }
            }
        } else {
            if ($source == 'header' && $style == 'page') {
                use_helper('I18n');
                foreach (I18n::getPreferredLanguages() as $lang) {
                    if (Setting::get('language') == $lang) {
                        break;
                    }
                    $uri = $lang . '/' . CURRENT_PATH;
                    $page = Page::findByPath($uri);
                    if (false !== $page) {
                        redirect(BASE_URL . $uri);
                    }
                }
            } else {
                if ($source == 'preferences' && $style == 'page') {
                    AuthUser::load();
                    if (AuthUser::isLoggedIn()) {
                        $lang = AuthUser::getRecord()->language;
                        $uri = $lang . '/' . CURRENT_PATH;
                        $page = Page::findByPath($uri);
                        if (false !== $page) {
                            redirect(BASE_URL . $uri);
                        }
                    }
                }
            }
        }
    }
}
function main()
{
    // get the uri string from the query
    $path = $_SERVER['QUERY_STRING'];
    // Make sure special characters are decoded (support non-western glyphs like japanese)
    $path = urldecode($path);
    // START processing $_GET variables
    // If we're NOT using mod_rewrite, we check for GET variables we need to integrate
    if (!USE_MOD_REWRITE && strpos($path, '?') !== false) {
        $_GET = array();
        // empty $_GET array since we're going to rebuild it
        list($path, $get_var) = explode('?', $path);
        $exploded_get = explode('&', $get_var);
        if (count($exploded_get)) {
            foreach ($exploded_get as $get) {
                list($key, $value) = explode('=', $get);
                $_GET[$key] = $value;
            }
        }
    } else {
        if (!USE_MOD_REWRITE && (strpos($path, '&') !== false || strpos($path, '=') !== false)) {
            $path = '/';
        }
    }
    // If we're using mod_rewrite, we should have a WOLFPAGE entry.
    if (USE_MOD_REWRITE && array_key_exists('WOLFPAGE', $_GET)) {
        $path = $_GET['WOLFPAGE'];
        unset($_GET['WOLFPAGE']);
    } else {
        if (USE_MOD_REWRITE) {
            // We're using mod_rewrite but don't have a WOLFPAGE entry, assume site root.
            $path = '/';
        }
    }
    // Needed to allow for ajax calls to backend
    if (array_key_exists('WOLFAJAX', $_GET)) {
        $path = '/' . ADMIN_DIR . $_GET['WOLFAJAX'];
        unset($_GET['WOLFAJAX']);
    }
    // END processing $_GET variables
    // remove suffix page if founded
    if (URL_SUFFIX !== '' and URL_SUFFIX !== '/') {
        $path = preg_replace('#^(.*)(' . URL_SUFFIX . ')$#i', "\$1", $path);
    }
    define('CURRENT_PATH', trim($path, '/'));
    // Alias for backward compatibility, this constant should no longer be used.
    define('CURRENT_URI', CURRENT_PATH);
    if ($path != null && $path[0] != '/') {
        $path = '/' . $path;
    }
    // Check if there's a custom route defined for this URI,
    // otherwise continue and assume page was requested.
    if (Dispatcher::hasRoute($path)) {
        Observer::notify('dispatch_route_found', $path);
        Dispatcher::dispatch($path);
        exit;
    }
    foreach (Observer::getObserverList('page_requested') as $callback) {
        $path = call_user_func_array($callback, array(&$path));
    }
    // this is where 80% of the things is done
    $page = Page::findByPath($path, true);
    // if we found it, display it!
    if (is_object($page)) {
        // If a page is in preview status, only display to logged in users
        if (Page::STATUS_PREVIEW == $page->status_id) {
            AuthUser::load();
            if (!AuthUser::isLoggedIn() || !AuthUser::hasPermission('page_view')) {
                pageNotFound($path);
            }
        }
        // If page needs login, redirect to login
        if ($page->getLoginNeeded() == Page::LOGIN_REQUIRED) {
            AuthUser::load();
            if (!AuthUser::isLoggedIn()) {
                Flash::set('redirect', $page->url());
                redirect(URL_PUBLIC . (USE_MOD_REWRITE ? '' : '?/') . ADMIN_DIR . '/login');
            }
        }
        Observer::notify('page_found', $page);
        $page->_executeLayout();
    } else {
        pageNotFound($path);
    }
}
 /**
  * Finds a Page record based on supplied arguments.
  *
  * Usage:
  *      $page = Page::find('/the/path/to/your/page');
  *      $page = Page::find(array('where' => 'created_by_id=12'));
  *
  * Argument array can contain:
  *      - where
  *      - order
  *      - offset
  *      - limit
  *
  * Return values can be:
  *      - A single Page object
  *      - An array of Page objects which can be empty
  *      - False
  *
  * @param mixed $args   Path string or array of arguments.
  * @return mixed        Page or array of Pages, otherwise false.
  */
 public static function find($options = null)
 {
     if (!is_array($options)) {
         // Assumes find was called with a uri
         return Page::findByPath($options);
     }
     $options = is_null($options) ? array() : $options;
     $class_name = get_called_class();
     $table_name = self::tableNameFromClassName($class_name);
     // Collect attributes
     $ses = isset($options['select']) ? trim($options['select']) : '';
     $frs = isset($options['from']) ? trim($options['from']) : '';
     $jos = isset($options['joins']) ? trim($options['joins']) : '';
     $whs = isset($options['where']) ? trim($options['where']) : '';
     $gbs = isset($options['group']) ? trim($options['group']) : '';
     $has = isset($options['having']) ? trim($options['having']) : '';
     $obs = isset($options['order']) ? trim($options['order']) : '';
     $lis = isset($options['limit']) ? (int) $options['limit'] : 0;
     $ofs = isset($options['offset']) ? (int) $options['offset'] : 0;
     $values = isset($options['values']) ? (array) $options['values'] : array();
     $single = $lis === 1 ? true : false;
     $tablename_user = self::tableNameFromClassName('User');
     $jos .= " LEFT JOIN {$tablename_user} AS creator ON page.created_by_id = creator.id";
     $jos .= " LEFT JOIN {$tablename_user} AS updater ON page.updated_by_id = updater.id";
     // Prepare query parts
     // @todo Remove all "author" mentions and function and replace by more appropriate "creator" name.
     $select = empty($ses) ? 'SELECT page.*, creator.name AS author, creator.id AS author_id, updater.name AS updater, updater.id AS updater_id, creator.name AS created_by_name, updater.name AS updated_by_name' : "SELECT {$ses}";
     $from = empty($frs) ? "FROM {$table_name} AS page" : "FROM {$frs}";
     $joins = empty($jos) ? '' : $jos;
     $where = empty($whs) ? '' : "WHERE {$whs}";
     $group_by = empty($gbs) ? '' : "GROUP BY {$gbs}";
     $having = empty($has) ? '' : "HAVING {$has}";
     $order_by = empty($obs) ? '' : "ORDER BY {$obs}";
     $limit = $lis > 0 ? "LIMIT {$lis}" : '';
     $offset = $ofs > 0 ? "OFFSET {$ofs}" : '';
     // Compose the query
     $sql = "{$select} {$from} {$joins} {$where} {$group_by} {$having} {$order_by} {$limit} {$offset}";
     $objects = self::findBySql($sql, $values);
     return $single ? !empty($objects) ? $objects[0] : false : $objects;
 }