Example #1
0
function smarty_function_mtsetvar($args, &$ctx)
{
    // status: complete
    // parameters: name, value
    $name = $args['name'];
    $name or $name = $args['var'];
    if (!$name) {
        return '';
    }
    $value = $args['value'];
    $vars =& $ctx->__stash['vars'];
    if (strtolower($name) == 'page_layout') {
        # replaces page layout for current page
        require_once "MTUtil.php";
        $columns = get_page_column($value);
        $vars['page_columns'] = $columns;
        $vars['page_layout'] = $value;
    }
    if (preg_match('/^(\\w+)\\((.+)\\)$/', $name, $matches)) {
        $func = $matches[1];
        $name = $matches[2];
    } else {
        if (array_key_exists('function', $args)) {
            $func = $args['function'];
        }
    }
    # pick off any {...} or [...] from the name.
    if (preg_match('/^(.+)([\\[\\{])(.+)[\\]\\}]$/', $name, $matches)) {
        $name = $matches[1];
        $br = $matches[2];
        $ref = $matches[3];
        if (preg_match('/^\\$(.+)/', $ref, $ref_matches)) {
            $ref = $vars[$ref_matches[1]];
            if (!isset($ref)) {
                $ref = chr(0);
            }
        }
        $br == '[' ? $index = $ref : ($key = $ref);
    } else {
        if (array_key_exists('index', $args)) {
            $index = $args['index'];
        } else {
            if (array_key_exists('key', $args)) {
                $key = $args['key'];
            }
        }
    }
    if (preg_match('/^\\$/', $name)) {
        $name = $vars[$name];
        if (!isset($name)) {
            return $ctx->error($ctx->mt->translate("You used a [_1] tag without a valid name attribute.", "<MT{$tag}>"));
        }
    }
    $existing = $vars[$name];
    require_once "MTUtil.php";
    if (isset($key)) {
        if (!isset($existing)) {
            $existing = array($key => $value);
        } elseif (is_hash($existing)) {
            $existing = $existing[$key];
        } else {
            return $ctx->error($ctx->mt->translate("'[_1]' is not a hash.", $name));
        }
    } elseif (isset($index)) {
        if (!isset($existing)) {
            $existing[$index] = $value;
        } elseif (is_array($existing)) {
            if (is_numeric($index)) {
                $existing = $existing[$index];
            } else {
                return $ctx->error($ctx->mt->translate("Invalid index."));
            }
        } else {
            return $ctx->error($ctx->mt->translate("'[_1]' is not an array.", $name));
        }
    }
    if (array_key_exists('append', $args) && $args['append']) {
        $value = isset($existing) ? $existing . $value : $value;
    } elseif (array_key_exists('prepend', $args) && $args['prepend']) {
        $value = isset($existing) ? $value . $existing : $value;
    } elseif (isset($existing) && array_key_exists('op', $args)) {
        $op = $args['op'];
        $value = _math_operation($op, $existing, $value);
        if (!isset($value)) {
            return $ctx->error($ctx->mt->translate("[_1] [_2] [_3] is illegal.", $existing, $op, $value));
        }
    }
    $data = $vars[$name];
    if (isset($key)) {
        if (isset($func) && 'delete' == strtolower($func)) {
            unset($data[$key]);
        } else {
            $data[$key] = $value;
        }
    } elseif (isset($index)) {
        $data[$index] = $value;
    } elseif (isset($func)) {
        if ('undef' == strtolower($func)) {
            unset($data);
        } else {
            if (isset($data) && !is_array($data)) {
                return $ctx->error($ctx->mt->translate("'[_1]' is not an array.", $name));
            }
            if (!isset($data)) {
                $data = array();
            }
            if ('push' == strtolower($func)) {
                array_push($data, $value);
            } elseif ('unshift' == strtolower($func)) {
                array_unshift($data, $value);
            } else {
                return $ctx->error($ctx->mt->translate("'[_1]' is not a valid function.", $func));
            }
        }
    } else {
        $data = $value;
    }
    $hash = $ctx->stash('__inside_set_hashvar');
    if (isset($hash)) {
        $hash[$name] = $data;
        $ctx->stash('__inside_set_hashvar', $hash);
    } else {
        if (is_array($vars)) {
            $vars[$name] = $data;
        } else {
            $vars = array($name => $data);
            $ctx->__stash['vars'] =& $vars;
        }
    }
    return '';
}
Example #2
0
 function view($blog_id = null)
 {
     require_once "MTUtil.php";
     $blog_id or $blog_id = $this->blog_id;
     $ctx =& $this->context();
     $this->init_plugins();
     $ctx->caching = $this->caching;
     // Some defaults...
     $mtdb =& $this->db();
     $ctx->mt->db =& $mtdb;
     // Set up our customer error handler
     set_error_handler(array(&$this, 'error_handler'));
     // User-specified request through request variable
     $path = $this->request;
     // Apache request
     if (!$path && $_SERVER['REQUEST_URI']) {
         $path = $_SERVER['REQUEST_URI'];
         // strip off any query string...
         $path = preg_replace('/\\?.*/', '', $path);
         // strip any duplicated slashes...
         $path = preg_replace('!/+!', '/', $path);
     }
     // IIS request by error document...
     if (preg_match('/IIS/', $_SERVER['SERVER_SOFTWARE'])) {
         // assume 404 handler
         if (preg_match('/^\\d+;(.*)$/', $_SERVER['QUERY_STRING'], $matches)) {
             $path = $matches[1];
             $path = preg_replace('!^http://[^/]+!', '', $path);
             if (preg_match('/\\?(.+)?/', $path, $matches)) {
                 $_SERVER['QUERY_STRING'] = $matches[1];
                 $path = preg_replace('/\\?.*$/', '', $path);
             }
         }
     }
     // now set the path so it may be queried
     $path = preg_replace('/\\\\/', '\\\\\\\\', $path);
     $this->request = $path;
     // When we are invoked as an ErrorDocument, the parameters are
     // in the environment variables REDIRECT_*
     if (isset($_SERVER['REDIRECT_QUERY_STRING'])) {
         // todo: populate $_GET and QUERY_STRING with REDIRECT_QUERY_STRING
         $_SERVER['QUERY_STRING'] = getenv('REDIRECT_QUERY_STRING');
     }
     if (preg_match('/\\.(\\w+)$/', $path, $matches)) {
         $req_ext = strtolower($matches[1]);
     }
     $this->blog_id = $blog_id;
     $data =& $this->resolve_url($path);
     if (!$data) {
         // 404!
         $this->http_error = 404;
         header("HTTP/1.1 404 Not found");
         return $ctx->error($this->translate("Page not found - [_1]", $path), E_USER_ERROR);
     }
     $info =& $data['fileinfo'];
     $fi_path = $info['fileinfo_url'];
     $fid = $info['fileinfo_id'];
     $at = $info['fileinfo_archive_type'];
     $ts = $info['fileinfo_startdate'];
     $tpl_id = $info['fileinfo_template_id'];
     $cat = $info['fileinfo_category_id'];
     $auth = $info['fileinfo_author_id'];
     $entry_id = $info['fileinfo_entry_id'];
     $blog_id = $info['fileinfo_blog_id'];
     $blog =& $data['blog'];
     if ($at == 'index') {
         $at = null;
         $ctx->stash('index_archive', true);
     } else {
         $ctx->stash('index_archive', false);
     }
     $tts = $data['template']['template_modified_on'];
     if ($tts) {
         $tts = offset_time(datetime_to_timestamp($tts), $blog);
     }
     $ctx->stash('template_timestamp', $tts);
     $ctx->stash('template_created_on', $data['template']['template_created_on']);
     $page_layout = $data['blog']['blog_page_layout'];
     $columns = get_page_column($page_layout);
     $vars =& $ctx->__stash['vars'];
     $vars['page_columns'] = $columns;
     $vars['page_layout'] = $page_layout;
     if (isset($data['template']['template_identifier'])) {
         $vars[$data['template']['template_identifier']] = 1;
     }
     $this->configure_paths($blog['blog_site_path']);
     // start populating our stash
     $ctx->stash('blog_id', $blog_id);
     $ctx->stash('local_blog_id', $blog_id);
     $ctx->stash('blog', $blog);
     $ctx->stash('build_template_id', $tpl_id);
     // conditional get support...
     if ($this->caching) {
         $this->cache_modified_check = true;
     }
     if ($this->conditional) {
         $last_ts = $blog['blog_children_modified_on'];
         $last_modified = $ctx->_hdlr_date(array('ts' => $last_ts, 'format' => '%a, %d %b %Y %H:%M:%S GMT', 'language' => 'en', 'utc' => 1), $ctx);
         $this->doConditionalGet($last_modified);
     }
     $cache_id = $blog_id . ';' . $fi_path . ';' . $_SERVER['QUERY_STRING'];
     if (!$ctx->is_cached('mt:' . $tpl_id, $cache_id)) {
         if (isset($at) && $at != 'Category') {
             global $_archivers;
             require_once "archive_lib.php";
             global $_archivers;
             if (!isset($_archivers[$at])) {
                 // 404
                 $this->http_errr = 404;
                 header("HTTP/1.1 404 Not Found");
                 return $ctx->error($this->translate("Page not found - [_1]", $at), E_USER_ERROR);
             }
             $archiver = $_archivers[$at];
             $archiver->template_params($ctx);
         }
         if ($cat) {
             $archive_category = $mtdb->fetch_category($cat);
             $ctx->stash('category', $archive_category);
             $ctx->stash('archive_category', $archive_category);
         }
         if ($auth) {
             $archive_author = $mtdb->fetch_author($auth);
             $ctx->stash('author', $archive_author);
             $ctx->stash('archive_author', $archive_author);
         }
         if (isset($at)) {
             if ($at != 'Category' && isset($ts)) {
                 list($ts_start, $ts_end) = $archiver->get_range($ctx, $ts);
                 $ctx->stash('current_timestamp', $ts_start);
                 $ctx->stash('current_timestamp_end', $ts_end);
             }
             $ctx->stash('current_archive_type', $at);
         }
         if (isset($entry_id) && $entry_id && ($at == 'Individual' || $at == 'Page')) {
             if ($at == 'Individual') {
                 $entry =& $mtdb->fetch_entry($entry_id);
             } elseif ($at == 'Page') {
                 $entry =& $mtdb->fetch_page($entry_id);
             }
             $ctx->stash('entry', $entry);
             $ctx->stash('current_timestamp', $entry['entry_authored_on']);
         }
         if ($at == 'Category') {
             $vars =& $ctx->__stash['vars'];
             $vars['archive_class'] = "category-archive";
             $vars['category_archive'] = 1;
             $vars['archive_template'] = 1;
             $vars['archive_listing'] = 1;
             $vars['module_category_archives'] = 1;
         }
     }
     $output = $ctx->fetch('mt:' . $tpl_id, $cache_id);
     $this->http_error = 200;
     header("HTTP/1.1 200 OK");
     // content-type header-- need to supplement with charset
     $content_type = $ctx->stash('content_type');
     if (!isset($content_type)) {
         $content_type = $this->mime_types['__default__'];
         if ($req_ext && isset($this->mime_types[$req_ext])) {
             $content_type = $this->mime_types[$req_ext];
         }
     }
     $charset = $this->config('PublishCharset');
     if (isset($charset)) {
         if (!preg_match('/charset=/', $content_type)) {
             $content_type .= '; charset=' . $charset;
         }
     }
     header("Content-Type: {$content_type}");
     // finally, issue output
     $output = preg_replace('/^\\s*/', '', $output);
     echo $output;
     // if warnings found, show it.
     if (!empty($this->warning)) {
         $this->_dump($this->warning);
     }
     if ($this->debugging) {
         $this->log("Queries: " . $mtdb->num_queries);
         $this->log("Queries executed:");
         $queries = $mtdb->savedqueries;
         foreach ($queries as $q) {
             $this->log($q);
         }
         $this->log_dump();
     }
     restore_error_handler();
 }