| 1 | <?php |
|---|
| 2 | # Movable Type (r) Open Source (C) 2004-2008 Six Apart, Ltd. |
|---|
| 3 | # This program is distributed under the terms of the |
|---|
| 4 | # GNU General Public License, version 2. |
|---|
| 5 | # |
|---|
| 6 | # $Id$ |
|---|
| 7 | |
|---|
| 8 | define('VERSION', '__API_VERSION__'); |
|---|
| 9 | define('VERSION_ID', '__PRODUCT_VERSION_ID__'); |
|---|
| 10 | define('PRODUCT_VERSION', '__PRODUCT_VERSION__'); |
|---|
| 11 | define('PRODUCT_NAME', '__PRODUCT_NAME__'); |
|---|
| 12 | |
|---|
| 13 | global $Lexicon; |
|---|
| 14 | $Lexicon = array(); |
|---|
| 15 | |
|---|
| 16 | class MT { |
|---|
| 17 | var $mime_types = array( |
|---|
| 18 | '__default__' => 'text/html', |
|---|
| 19 | 'css' => 'text/css', |
|---|
| 20 | 'txt' => 'text/plain', |
|---|
| 21 | 'rdf' => 'text/xml', |
|---|
| 22 | 'rss' => 'text/xml', |
|---|
| 23 | 'xml' => 'text/xml', |
|---|
| 24 | ); |
|---|
| 25 | var $blog_id; |
|---|
| 26 | var $db; |
|---|
| 27 | var $config; |
|---|
| 28 | var $debugging = false; |
|---|
| 29 | var $caching = false; |
|---|
| 30 | var $conditional = false; |
|---|
| 31 | var $log = array(); |
|---|
| 32 | var $id; |
|---|
| 33 | var $request; |
|---|
| 34 | var $http_error; |
|---|
| 35 | var $cfg_file; |
|---|
| 36 | var $cache_driver; |
|---|
| 37 | |
|---|
| 38 | /*** |
|---|
| 39 | * Constructor for MT class. Also declares a global variable |
|---|
| 40 | * '$mt' and assigns itself to that. There can only be one |
|---|
| 41 | * instance of this class. |
|---|
| 42 | */ |
|---|
| 43 | function MT($blog_id = null, $cfg_file = null) { |
|---|
| 44 | error_reporting(E_ALL ^ E_NOTICE); |
|---|
| 45 | global $mt; |
|---|
| 46 | if (isset($mt)) { |
|---|
| 47 | die("Only one instance of the MT class can be created."); |
|---|
| 48 | } |
|---|
| 49 | $this->id = md5(uniqid('MT',true)); |
|---|
| 50 | $this->init($blog_id, $cfg_file); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | function init($blog_id = null, $cfg_file = null) { |
|---|
| 54 | if (isset($blog_id)) { |
|---|
| 55 | $this->blog_id = $blog_id; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | if (!file_exists($cfg_file)) { |
|---|
| 59 | $mtdir = dirname(dirname(__FILE__)); |
|---|
| 60 | $cfg_file = $mtdir . DIRECTORY_SEPARATOR . "mt-config.cgi"; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | $this->configure($cfg_file); |
|---|
| 64 | $this->init_addons(); |
|---|
| 65 | $this->configure_from_db(); |
|---|
| 66 | |
|---|
| 67 | $lang = substr(strtolower($this->config('DefaultLanguage')), 0, 2); |
|---|
| 68 | if (!@include_once("l10n_$lang.php")) |
|---|
| 69 | include_once("l10n_en.php"); |
|---|
| 70 | |
|---|
| 71 | if (extension_loaded('mbstring')) { |
|---|
| 72 | $charset = $this->config('PublishCharset'); |
|---|
| 73 | mb_internal_encoding($charset); |
|---|
| 74 | mb_http_output($charset); |
|---|
| 75 | } |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | function init_addons() { |
|---|
| 79 | $mtdir = dirname(dirname(__FILE__)); |
|---|
| 80 | $path = $mtdir . DIRECTORY_SEPARATOR . "addons"; |
|---|
| 81 | if (is_dir($path)) { |
|---|
| 82 | $ctx =& $this->context(); |
|---|
| 83 | if ($dh = opendir($path)) { |
|---|
| 84 | while (($file = readdir($dh)) !== false) { |
|---|
| 85 | if ($file == "." || $file == "..") { |
|---|
| 86 | continue; |
|---|
| 87 | } |
|---|
| 88 | $plugin_dir = $path . DIRECTORY_SEPARATOR . $file |
|---|
| 89 | . DIRECTORY_SEPARATOR . 'php'; |
|---|
| 90 | if (is_dir($plugin_dir)) |
|---|
| 91 | $ctx->add_plugin_dir($plugin_dir); |
|---|
| 92 | } |
|---|
| 93 | closedir($dh); |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | function init_plugins() { |
|---|
| 99 | $plugin_paths = $this->config('PluginPath'); |
|---|
| 100 | $ctx =& $this->context(); |
|---|
| 101 | |
|---|
| 102 | foreach ($plugin_paths as $path) { |
|---|
| 103 | if ($dh = opendir($path)) { |
|---|
| 104 | while (($file = readdir($dh)) !== false) { |
|---|
| 105 | if ($file == "." || $file == "..") |
|---|
| 106 | continue; |
|---|
| 107 | $plugin_dir = $path . DIRECTORY_SEPARATOR . $file |
|---|
| 108 | . DIRECTORY_SEPARATOR . 'php'; |
|---|
| 109 | if (is_dir($plugin_dir)) |
|---|
| 110 | $ctx->add_plugin_dir($plugin_dir); |
|---|
| 111 | } |
|---|
| 112 | closedir($dh); |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | $plugin_dir = $this->config('PHPDir') . DIRECTORY_SEPARATOR |
|---|
| 117 | . 'plugins'; |
|---|
| 118 | if (is_dir($plugin_dir)) |
|---|
| 119 | $ctx->add_plugin_dir($plugin_dir); |
|---|
| 120 | |
|---|
| 121 | # Load any php directories found during the 'init_addons' loop |
|---|
| 122 | foreach ($ctx->plugins_dir as $plugin_dir) |
|---|
| 123 | if (is_dir($plugin_dir)) |
|---|
| 124 | $this->load_plugin($plugin_dir); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | function load_plugin($plugin_dir) { |
|---|
| 128 | $ctx =& $this->context(); |
|---|
| 129 | // global filters have to be handled differently from |
|---|
| 130 | // tag attributes, so this causes them to be recognized |
|---|
| 131 | // as they should... |
|---|
| 132 | if ($dh = opendir($plugin_dir)) { |
|---|
| 133 | while (($file = readdir($dh)) !== false) { |
|---|
| 134 | if (preg_match('/^modifier\.(.+?)\.php$/', $file, $matches)) { |
|---|
| 135 | $ctx->add_global_filter($matches[1]); |
|---|
| 136 | } elseif (preg_match('/^init\.(.+?)\.php$/', $file, $matches)) { |
|---|
| 137 | // load 'init' plugin file |
|---|
| 138 | require_once($file); |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | closedir($dh); |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | /*** |
|---|
| 146 | * Retreives a handle to the database and assigns it to |
|---|
| 147 | * the member variable 'db'. |
|---|
| 148 | */ |
|---|
| 149 | function &db() { |
|---|
| 150 | if (isset($this->db)) return $this->db; |
|---|
| 151 | |
|---|
| 152 | require_once("mtdb_".$this->config('DBDriver').".php"); |
|---|
| 153 | $mtdbclass = 'MTDatabase_'.$this->config('DBDriver'); |
|---|
| 154 | $this->db = new $mtdbclass($this->config('DBUser'), |
|---|
| 155 | $this->config('DBPassword'), $this->config('Database'), |
|---|
| 156 | $this->config('DBHost'), $this->config('DBPort'), $this->config('DBSocket')); |
|---|
| 157 | return $this->db; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | /*** |
|---|
| 161 | * Retreives a handle to the cache driver. |
|---|
| 162 | */ |
|---|
| 163 | function &cache_driver() { |
|---|
| 164 | if (isset($this->cache_driver)) return $this->cache_driver; |
|---|
| 165 | |
|---|
| 166 | # Check for memcached enabled |
|---|
| 167 | $servers = $this->config['MemcachedServers']; |
|---|
| 168 | if (isset($servers)) { |
|---|
| 169 | if (extension_loaded('memcache')) { |
|---|
| 170 | require_once("MTCache_memcached.php"); |
|---|
| 171 | $this->cache_driver = new MTCache_memcached($servers); |
|---|
| 172 | } |
|---|
| 173 | } else { |
|---|
| 174 | require_once("MTCache_session.php"); |
|---|
| 175 | $this->cache_driver = new MTCache_session(); |
|---|
| 176 | } |
|---|
| 177 | return $this->cache_driver; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | function config($id, $value = null) { |
|---|
| 181 | $id = strtolower($id); |
|---|
| 182 | if (isset($value)) |
|---|
| 183 | $this->config[$id] = $value; |
|---|
| 184 | return $this->config[$id]; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | /*** |
|---|
| 188 | * Loads configuration data from mt.cfg and mt-db-pass.cgi files. |
|---|
| 189 | * Stores content in the 'config' member variable. |
|---|
| 190 | */ |
|---|
| 191 | function configure($file = null) { |
|---|
| 192 | if (isset($this->config)) return $config; |
|---|
| 193 | |
|---|
| 194 | $this->cfg_file = $file; |
|---|
| 195 | |
|---|
| 196 | $cfg = array(); |
|---|
| 197 | $type_array = array('pluginpath', 'alttemplate', 'outboundtrackbackdomains', 'memcachedservers'); |
|---|
| 198 | if ($fp = file($file)) { |
|---|
| 199 | foreach ($fp as $line) { |
|---|
| 200 | // search through the file |
|---|
| 201 | if (!ereg('^\s*\#',$line)) { |
|---|
| 202 | // ignore lines starting with the hash symbol |
|---|
| 203 | if (preg_match('/^\s*(\S+)\s+(.*)$/', $line, $regs)) { |
|---|
| 204 | $key = strtolower(trim($regs[1])); |
|---|
| 205 | $value = trim($regs[2]); |
|---|
| 206 | if (in_array($key, $type_array)) { |
|---|
| 207 | $cfg[$key][] = $value; |
|---|
| 208 | } else { |
|---|
| 209 | $cfg[$key] = $value; |
|---|
| 210 | } |
|---|
| 211 | } |
|---|
| 212 | } |
|---|
| 213 | } |
|---|
| 214 | } else { |
|---|
| 215 | die("Unable to open configuration file $file"); |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | // setup directory locations |
|---|
| 219 | // location of mt.php |
|---|
| 220 | $cfg['phpdir'] = realpath(dirname(__FILE__)); |
|---|
| 221 | // path to MT directory |
|---|
| 222 | $cfg['mtdir'] = realpath(dirname($file)); |
|---|
| 223 | // path to handlers |
|---|
| 224 | $cfg['phplibdir'] = $cfg['phpdir'] . DIRECTORY_SEPARATOR . 'lib'; |
|---|
| 225 | |
|---|
| 226 | $cfg['dbhost'] or $cfg['dbhost'] = 'localhost'; // default to localhost |
|---|
| 227 | $driver = $cfg['objectdriver']; |
|---|
| 228 | $driver = preg_replace('/^DB[ID]::/', '', $driver); |
|---|
| 229 | $driver or $driver = 'mysql'; |
|---|
| 230 | $cfg['dbdriver'] = strtolower($driver); |
|---|
| 231 | |
|---|
| 232 | if ((strlen($cfg['database'])<1 || strlen($cfg['dbuser'])<1)) { |
|---|
| 233 | if (($cfg['dbdriver'] != 'sqlite') && ($cfg['dbdriver'] != 'mssqlserver')) { |
|---|
| 234 | die("Unable to read database or username"); |
|---|
| 235 | } |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | $this->config =& $cfg; |
|---|
| 239 | $this->config_defaults(); |
|---|
| 240 | |
|---|
| 241 | // read in the database password |
|---|
| 242 | if (!isset($cfg['dbpassword'])) { |
|---|
| 243 | $db_pass_file = $cfg['mtdir'] . DIRECTORY_SEPARATOR . 'mt-db-pass.cgi'; |
|---|
| 244 | if (file_exists($db_pass_file)) { |
|---|
| 245 | $password = implode('', file($db_pass_file)); |
|---|
| 246 | $password = trim($password, "\n\r\0"); |
|---|
| 247 | $cfg['dbpassword'] = $password; |
|---|
| 248 | } |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | // set up include path |
|---|
| 252 | // add MT-PHP 'plugins' and 'lib' directories to the front |
|---|
| 253 | // of the existing PHP include path: |
|---|
| 254 | if (strtoupper(substr(PHP_OS, 0,3) == 'WIN')) { |
|---|
| 255 | $path_sep = ';'; |
|---|
| 256 | } else { |
|---|
| 257 | $path_sep = ':'; |
|---|
| 258 | } |
|---|
| 259 | ini_set('include_path', |
|---|
| 260 | $cfg['phpdir'] . DIRECTORY_SEPARATOR . "lib" . $path_sep . |
|---|
| 261 | $cfg['phpdir'] . DIRECTORY_SEPARATOR . "extlib" . $path_sep . |
|---|
| 262 | $cfg['phpdir'] . DIRECTORY_SEPARATOR . "extlib" . DIRECTORY_SEPARATOR . "smarty" . DIRECTORY_SEPARATOR . "libs" . $path_sep . |
|---|
| 263 | ini_get('include_path') |
|---|
| 264 | ); |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | function configure_from_db() { |
|---|
| 268 | $cfg =& $this->config; |
|---|
| 269 | $mtdb =& $this->db(); |
|---|
| 270 | $db_config = $mtdb->fetch_config(); |
|---|
| 271 | if ($db_config) { |
|---|
| 272 | $data = $db_config['config_data']; |
|---|
| 273 | $data = preg_split('/[\r?\n]/', $data); |
|---|
| 274 | foreach ($data as $line) { |
|---|
| 275 | // search through the file |
|---|
| 276 | if (!ereg('^\s*\#',$line)) { |
|---|
| 277 | // ignore lines starting with the hash symbol |
|---|
| 278 | if (preg_match('/^\s*(\S+)\s+(.*)$/', $line, $regs)) { |
|---|
| 279 | $key = strtolower(trim($regs[1])); |
|---|
| 280 | $value = trim($regs[2]); |
|---|
| 281 | if (($key == 'PluginSwitch') || ($key == 'PluginSchemaVersion')) { # special case for hash |
|---|
| 282 | if (preg_match('/^(.+)=(.+)$/', $value, $match)) |
|---|
| 283 | $cfg[$key][trim($match[1])] = trim($match[2]); |
|---|
| 284 | } else { |
|---|
| 285 | $cfg[$key] or $cfg[$key] = $value; |
|---|
| 286 | } |
|---|
| 287 | } |
|---|
| 288 | } |
|---|
| 289 | } |
|---|
| 290 | if ($cfg['dbdriver'] == 'mysql' or $cfg['dbdriver'] == 'postgres') { |
|---|
| 291 | if ($cfg['sqlsetnames']) { |
|---|
| 292 | $Charset = array( |
|---|
| 293 | 'postgres' => array('utf-8' => 'UNICODE', 'shift_jis' => 'SJIS', 'euc-jp' => 'EUC_JP'), |
|---|
| 294 | 'mysql' => array('utf-8' => 'utf8', 'shift_jis' => 'sjis', 'euc-jp' => 'ujis')); |
|---|
| 295 | $lang = $Charset[$cfg['dbdriver']][strtolower($cfg['publishcharset'])]; |
|---|
| 296 | if ($lang) { |
|---|
| 297 | $mtdb->query("SET NAMES '$lang'"); |
|---|
| 298 | } |
|---|
| 299 | } |
|---|
| 300 | } |
|---|
| 301 | } |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | function config_defaults() { |
|---|
| 305 | $cfg =& $this->config; |
|---|
| 306 | // assign defaults: |
|---|
| 307 | if (substr($cfg['cgipath'], strlen($cfg['cgipath']) - 1, 1) != '/') |
|---|
| 308 | $cfg['cgipath'] .= '/'; |
|---|
| 309 | isset($cfg['staticwebpath']) or |
|---|
| 310 | $cfg['staticwebpath'] = $cfg['cgipath'] . 'mt-static/'; |
|---|
| 311 | isset($cfg['publishcharset']) or |
|---|
| 312 | $cfg['publishcharset'] = '__PUBLISH_CHARSET__'; |
|---|
| 313 | isset($cfg['trackbackscript']) or |
|---|
| 314 | $cfg['trackbackscript'] = 'mt-tb.cgi'; |
|---|
| 315 | isset($cfg['adminscript']) or |
|---|
| 316 | $cfg['adminscript'] = 'mt.cgi'; |
|---|
| 317 | isset($cfg['commentscript']) or |
|---|
| 318 | $cfg['commentscript'] = 'mt-comments.cgi'; |
|---|
| 319 | isset($cfg['atomscript']) or |
|---|
| 320 | $cfg['atomscript'] = 'mt-atom.cgi'; |
|---|
| 321 | isset($cfg['xmlrpcscript']) or |
|---|
| 322 | $cfg['xmlrpcscript'] = 'mt-xmlrpc.cgi'; |
|---|
| 323 | isset($cfg['searchscript']) or |
|---|
| 324 | $cfg['searchscript'] = 'mt-search.cgi'; |
|---|
| 325 | isset($cfg['defaultlanguage']) or |
|---|
| 326 | $cfg['defaultlanguage'] = '__BUILD_LANGUAGE__'; |
|---|
| 327 | isset($cfg['globalsanitizespec']) or |
|---|
| 328 | $cfg['globalsanitizespec'] = 'a href,b,i,br/,p,strong,em,ul,ol,li,blockquote,pre'; |
|---|
| 329 | isset($cfg['signonurl']) or |
|---|
| 330 | $cfg['signonurl'] = 'https://www.typekey.com/t/typekey/login?'; |
|---|
| 331 | isset($cfg['signoffurl']) or |
|---|
| 332 | $cfg['signoffurl'] = 'https://www.typekey.com/t/typekey/logout?'; |
|---|
| 333 | isset($cfg['identityurl']) or |
|---|
| 334 | $cfg['identityurl'] = 'http://profile.typekey.com/'; |
|---|
| 335 | isset($cfg['publishcommentericon']) or |
|---|
| 336 | $cfg['publishcommentericon'] = '1'; |
|---|
| 337 | isset($cfg['allowcomments']) or |
|---|
| 338 | $cfg['allowcomments'] = '1'; |
|---|
| 339 | isset($cfg['allowpings']) or |
|---|
| 340 | $cfg['allowpings'] = '1'; |
|---|
| 341 | isset($cfg['indexbasename']) or |
|---|
| 342 | $cfg['indexbasename'] = 'index'; |
|---|
| 343 | isset($cfg['typekeyversion']) or |
|---|
| 344 | $cfg['typekeyversion'] = '1.1'; |
|---|
| 345 | isset($cfg['assetcachedir']) or |
|---|
| 346 | $cfg['assetcachedir'] = 'assets_c'; |
|---|
| 347 | isset($cfg['userpicthumbnailsize']) or |
|---|
| 348 | $cfg['userpicthumbnailsize'] = '100'; |
|---|
| 349 | isset($cfg['pluginpath']) or |
|---|
| 350 | $cfg['pluginpath'] = array($this->config('MTDir') . DIRECTORY_SEPARATOR . 'plugins'); |
|---|
| 351 | isset($cfg['timeoffset']) or |
|---|
| 352 | $cfg['timeoffset'] = '__DEFAULT_TIMEZONE__'; |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | function configure_paths($blog_site_path) { |
|---|
| 356 | if (preg_match('/^\./', $blog_site_path)) { |
|---|
| 357 | // relative address, so tack on the MT dir in front |
|---|
| 358 | $blog_site_path = $this->config('MTDir') . |
|---|
| 359 | DIRECTORY_SEPARATOR . $blog_site_path; |
|---|
| 360 | } |
|---|
| 361 | $this->config('PHPTemplateDir') or |
|---|
| 362 | $this->config('PHPTemplateDir', $blog_site_path . |
|---|
| 363 | DIRECTORY_SEPARATOR . 'templates'); |
|---|
| 364 | $this->config('PHPCacheDir') or |
|---|
| 365 | $this->config('PHPCacheDir', $blog_site_path . |
|---|
| 366 | DIRECTORY_SEPARATOR . 'cache'); |
|---|
| 367 | |
|---|
| 368 | $ctx =& $this->context(); |
|---|
| 369 | $ctx->template_dir = $this->config('PHPTemplateDir'); |
|---|
| 370 | $ctx->compile_dir = $ctx->template_dir . '_c'; |
|---|
| 371 | $ctx->cache_dir = $this->config('PHPCacheDir'); |
|---|
| 372 | } |
|---|
| 373 | |
|---|
| 374 | /*** |
|---|
| 375 | * Mainline handler function. |
|---|
| 376 | */ |
|---|
| 377 | function view($blog_id = null) { |
|---|
| 378 | if ($this->debugging) { |
|---|
| 379 | require_once("MTUtil.php"); |
|---|
| 380 | } |
|---|
| 381 | $blog_id or $blog_id = $this->blog_id; |
|---|
| 382 | |
|---|
| 383 | $ctx =& $this->context(); |
|---|
| 384 | $this->init_plugins(); |
|---|
| 385 | $ctx->caching = $this->caching; |
|---|
| 386 | |
|---|
| 387 | // Some defaults... |
|---|
| 388 | $mtdb =& $this->db(); |
|---|
| 389 | $ctx->mt->db =& $mtdb; |
|---|
| 390 | |
|---|
| 391 | // Set up our customer error handler |
|---|
| 392 | set_error_handler(array(&$this, 'error_handler')); |
|---|
| 393 | |
|---|
| 394 | // User-specified request through request variable |
|---|
| 395 | $path = $this->request; |
|---|
| 396 | |
|---|
| 397 | // Apache request |
|---|
| 398 | if (!$path && $_SERVER['REQUEST_URI']) { |
|---|
| 399 | $path = $_SERVER['REQUEST_URI']; |
|---|
| 400 | // strip off any query string... |
|---|
| 401 | $path = preg_replace('/\?.*/', '', $path); |
|---|
| 402 | // strip any duplicated slashes... |
|---|
| 403 | $path = preg_replace('!/+!', '/', $path); |
|---|
| 404 | } |
|---|
| 405 | |
|---|
| 406 | // IIS request by error document... |
|---|
| 407 | if (preg_match('/IIS/', $_SERVER['SERVER_SOFTWARE'])) { |
|---|
| 408 | // assume 404 handler |
|---|
| 409 | if (preg_match('/^\d+;(.*)$/', $_SERVER['QUERY_STRING'], $matches)) { |
|---|
| 410 | $path = $matches[1]; |
|---|
| 411 | $path = preg_replace('!^http://[^/]+!', '', $path); |
|---|
| 412 | if (preg_match('/\?(.+)?/', $path, $matches)) { |
|---|
| 413 | $_SERVER['QUERY_STRING'] = $matches[1]; |
|---|
| 414 | $path = preg_replace('/\?.*$/', '', $path); |
|---|
| 415 | } |
|---|
| 416 | } |
|---|
| 417 | } |
|---|
| 418 | |
|---|
| 419 | // now set the path so it may be queried |
|---|
| 420 | $this->request = $path; |
|---|
| 421 | |
|---|
| 422 | // When we are invoked as an ErrorDocument, the parameters are |
|---|
| 423 | // in the environment variables REDIRECT_* |
|---|
| 424 | if (isset($_SERVER['REDIRECT_QUERY_STRING'])) { |
|---|
| 425 | // todo: populate $_GET and QUERY_STRING with REDIRECT_QUERY_STRING |
|---|
| 426 | $_SERVER['QUERY_STRING'] = getenv('REDIRECT_QUERY_STRING'); |
|---|
| 427 | } |
|---|
| 428 | |
|---|
| 429 | if (preg_match('/\.(\w+)$/', $path, $matches)) { |
|---|
| 430 | $req_ext = strtolower($matches[1]); |
|---|
| 431 | } |
|---|
| 432 | |
|---|
| 433 | $this->blog_id = $blog_id; |
|---|
| 434 | |
|---|
| 435 | $data =& $this->resolve_url($path); |
|---|
| 436 | if (!$data) { |
|---|
| 437 | // 404! |
|---|
| 438 | $this->http_error = 404; |
|---|
| 439 | header("HTTP/1.1 404 Not found"); |
|---|
| 440 | return $ctx->error("Page not found - $path", E_USER_ERROR); |
|---|
| 441 | } |
|---|
| 442 | |
|---|
| 443 | $info =& $data['fileinfo']; |
|---|
| 444 | $fi_path = $info['fileinfo_url']; |
|---|
| 445 | $fid = $info['fileinfo_id']; |
|---|
| 446 | $at = $info['fileinfo_archive_type']; |
|---|
| 447 | $ts = $info['fileinfo_startdate']; |
|---|
| 448 | $tpl_id = $info['fileinfo_template_id']; |
|---|
| 449 | $cat = $info['fileinfo_category_id']; |
|---|
| 450 | $auth = $info['fileinfo_author_id']; |
|---|
| 451 | $entry_id = $info['fileinfo_entry_id']; |
|---|
| 452 | $blog_id = $info['fileinfo_blog_id']; |
|---|
| 453 | $blog =& $data['blog']; |
|---|
| 454 | if ($at == 'index') { |
|---|
| 455 | $at = null; |
|---|
| 456 | $ctx->stash('index_archive', true); |
|---|
| 457 | } else { |
|---|
| 458 | $ctx->stash('index_archive', false); |
|---|
| 459 | } |
|---|
| 460 | $tts = $data['template']['template_modified_on']; |
|---|
| 461 | if ($tts) { |
|---|
| 462 | $tts = offset_time(datetime_to_timestamp($tts), $blog); |
|---|
| 463 | } |
|---|
| 464 | $ctx->stash('template_timestamp', $tts); |
|---|
| 465 | $ctx->stash('template_created_on', $data['template']['template_created_on']); |
|---|
| 466 | |
|---|
| 467 | $columns_map = array( |
|---|
| 468 | 'layout-wt' => 2, |
|---|
| 469 | 'layout-tw' => 2, |
|---|
| 470 | 'layout-wm' => 2, |
|---|
| 471 | 'layout-mw' => 2, |
|---|
| 472 | 'layout-wtt' => 3, |
|---|
| 473 | 'layout-twt' => 3); |
|---|
| 474 | $page_layout = $data['blog']['blog_page_layout']; |
|---|
| 475 | $columns = 0; |
|---|
| 476 | if(array_key_exists($page_layout, $columns_map)) |
|---|
| 477 | $columns = $columns_map[$page_layout]; |
|---|
| 478 | $vars =& $ctx->__stash['vars']; |
|---|
| 479 | $vars['page_columns'] = $columns; |
|---|
| 480 | $vars['page_layout'] = $page_layout; |
|---|
| 481 | |
|---|
| 482 | $this->configure_paths($blog['blog_site_path']); |
|---|
| 483 | |
|---|
| 484 | // start populating our stash |
|---|
| 485 | $ctx->stash('blog_id', $blog_id); |
|---|
| 486 | $ctx->stash('blog', $blog); |
|---|
| 487 | $ctx->stash('build_template_id', $tpl_id); |
|---|
| 488 | |
|---|
| 489 | // conditional get support... |
|---|
| 490 | if ($this->caching) { |
|---|
| 491 | $this->cache_modified_check = true; |
|---|
| 492 | } |
|---|
| 493 | if ($this->conditional) { |
|---|
| 494 | $last_ts = $blog['blog_children_modified_on']; |
|---|
| 495 | $last_modified = $ctx->_hdlr_date(array('ts' => $last_ts, 'format' => '%a, %d %b %Y %H:%M:%S GMT', 'language' => 'en', 'utc' => 1), $ctx); |
|---|
| 496 | $this->doConditionalGet($last_modified); |
|---|
| 497 | } |
|---|
| 498 | |
|---|
| 499 | $cache_id = $blog_id.';'.$fi_path; |
|---|
| 500 | if (!$ctx->is_cached('mt:'.$tpl_id, $cache_id)) { |
|---|
| 501 | if (isset($at) && ($at != 'Category')) { |
|---|
| 502 | global $_archivers; |
|---|
| 503 | require_once("archive_lib.php"); |
|---|
| 504 | global $_archivers; |
|---|
| 505 | if (!isset($_archivers[$at])) { |
|---|
| 506 | // 404 |
|---|
| 507 | $this->http_errr = 404; |
|---|
| 508 | header("HTTP/1.1 404 Not Found"); |
|---|
| 509 | return $ctx->error("Page not found - $at", E_USER_ERROR); |
|---|
| 510 | } |
|---|
| 511 | $archiver = $_archivers[$at]; |
|---|
| 512 | $archiver->template_params($ctx); |
|---|
| 513 | } |
|---|
| 514 | |
|---|
| 515 | if ($cat) { |
|---|
| 516 | $archive_category = $mtdb->fetch_category($cat); |
|---|
| 517 | $ctx->stash('category', $archive_category); |
|---|
| 518 | $ctx->stash('archive_category', $archive_category); |
|---|
| 519 | } |
|---|
| 520 | if ($auth) { |
|---|
| 521 | $archive_author = $mtdb->fetch_author($auth); |
|---|
| 522 | $ctx->stash('author', $archive_author); |
|---|
| 523 | $ctx->stash('archive_author', $archive_author); |
|---|
| 524 | } |
|---|
| 525 | if (isset($at)) { |
|---|
| 526 | if (($at != 'Category') && isset($ts)) { |
|---|
| 527 | list($ts_start, $ts_end) = $archiver->get_range($ctx, $ts); |
|---|
| 528 | $ctx->stash('current_timestamp', $ts_start); |
|---|
| 529 | $ctx->stash('current_timestamp_end', $ts_end); |
|---|
| 530 | } |
|---|
| 531 | $ctx->stash('current_archive_type', $at); |
|---|
| 532 | } |
|---|
| 533 | |
|---|
| 534 | if (isset($entry_id) && ($entry_id) && ($at == 'Individual' || $at == 'Page')) { |
|---|
| 535 | if ($at == 'Individual') { |
|---|
| 536 | $entry =& $mtdb->fetch_entry($entry_id); |
|---|
| 537 | } elseif($at == 'Page') { |
|---|
| 538 | $entry =& $mtdb->fetch_page($entry_id); |
|---|
| 539 | } |
|---|
| 540 | $ctx->stash('entry', $entry); |
|---|
| 541 | $ctx->stash('current_timestamp', $entry['entry_authored_on']); |
|---|
| 542 | } |
|---|
| 543 | |
|---|
| 544 | if ($at == 'Category') { |
|---|
| 545 | $vars =& $ctx->__stash['vars']; |
|---|
| 546 | $vars['archive_class'] = "category-archive"; |
|---|
| 547 | $vars['category_archive'] = 1; |
|---|
| 548 | $vars['main_template'] = 1; |
|---|
| 549 | $vars['archive_template'] = 1; |
|---|
| 550 | $vars['module_category-monthly_archives'] = 1; |
|---|
| 551 | } |
|---|
| 552 | } |
|---|
| 553 | |
|---|
| 554 | $output = $ctx->fetch('mt:'.$tpl_id, $cache_id); |
|---|
| 555 | |
|---|
| 556 | $this->http_error = 200; |
|---|
| 557 | header("HTTP/1.1 200 OK"); |
|---|
| 558 | // content-type header-- need to supplement with charset |
|---|
| 559 | $content_type = $ctx->stash('content_type'); |
|---|
| 560 | |
|---|
| 561 | if (!isset($content_type)) { |
|---|
| 562 | $content_type = $this->mime_types['__default__']; |
|---|
| 563 | if ($req_ext && (isset($this->mime_types[$req_ext]))) { |
|---|
| 564 | $content_type = $this->mime_types[$req_ext]; |
|---|
| 565 | } |
|---|
| 566 | } |
|---|
| 567 | $charset = $this->config('PublishCharset'); |
|---|
| 568 | if (isset($charset)) { |
|---|
| 569 | if (!preg_match('/charset=/', $content_type)) |
|---|
| 570 | $content_type .= '; charset=' . $charset; |
|---|
| 571 | } |
|---|
| 572 | header("Content-Type: $content_type"); |
|---|
| 573 | |
|---|
| 574 | // finally, issue output |
|---|
| 575 | $output = preg_replace('/^\s*/', '', $output); |
|---|
| 576 | echo $output; |
|---|
| 577 | |
|---|
| 578 | if ($this->debugging) { |
|---|
| 579 | $this->log("Queries: ".$mtdb->num_queries); |
|---|
| 580 | $this->log("Queries executed:"); |
|---|
| 581 | $queries = $mtdb->savedqueries; |
|---|
| 582 | foreach ($queries as $q) { |
|---|
| 583 | $this->log($q); |
|---|
| 584 | } |
|---|
| 585 | $this->log_dump(); |
|---|
| 586 | } |
|---|
| 587 | restore_error_handler(); |
|---|
| 588 | } |
|---|
| 589 | |
|---|
| 590 | function &resolve_url($path) { |
|---|
| 591 | $data =& $this->db->resolve_url($path, $this->blog_id); |
|---|
| 592 | if ( isset($data) |
|---|
| 593 | && isset($data['fileinfo']['fileinfo_entry_id']) |
|---|
| 594 | && is_numeric($data['fileinfo']['fileinfo_entry_id']) |
|---|
| 595 | ) { |
|---|
| 596 | if (strtolower($data['templatemap']['templatemap_archive_type']) == 'page') { |
|---|
| 597 | $entry = $this->db->fetch_page($data['fileinfo']['fileinfo_entry_id']); |
|---|
| 598 | } else { |
|---|
| 599 | $entry = $this->db->fetch_entry($data['fileinfo']['fileinfo_entry_id']); |
|---|
| 600 | } |
|---|
| 601 | require_once('function.mtentrystatus.php'); |
|---|
| 602 | if (!isset($entry) || $entry['entry_status'] != STATUS_RELEASE) |
|---|
| 603 | return; |
|---|
| 604 | } |
|---|
| 605 | return $data; |
|---|
| 606 | } |
|---|
| 607 | |
|---|
| 608 | function doConditionalGet($last_modified) { |
|---|
| 609 | // Thanks to Simon Willison... |
|---|
| 610 | // http://simon.incutio.com/archive/2003/04/23/conditionalGet |
|---|
| 611 | // A PHP implementation of conditional get, see |
|---|
| 612 | // http://fishbowl.pastiche.org/archives/001132.html |
|---|
| 613 | $etag = '"'.md5($last_modified).'"'; |
|---|
| 614 | // Send the headers |
|---|
| 615 | header("Last-Modified: $last_modified"); |
|---|
| 616 | header("ETag: $etag"); |
|---|
| 617 | // See if the client has provided the required headers |
|---|
| 618 | $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? |
|---|
| 619 | stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) : |
|---|
| 620 | false; |
|---|
| 621 | $if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? |
|---|
| 622 | stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : |
|---|
| 623 | false; |
|---|
| 624 | if (!$if_modified_since && !$if_none_match) { |
|---|
| 625 | return; |
|---|
| 626 | } |
|---|
| 627 | // At least one of the headers is there - check them |
|---|
| 628 | if ($if_none_match && $if_none_match != $etag) { |
|---|
| 629 | return; // etag is there but doesn't match |
|---|
| 630 | } |
|---|
| 631 | if ($if_modified_since && $if_modified_since != $last_modified) { |
|---|
| 632 | return; // if-modified-since is there but doesn't match |
|---|
| 633 | } |
|---|
| 634 | // Nothing has changed since their last request - serve a 304 and exit |
|---|
| 635 | header('HTTP/1.1 304 Not Modified'); |
|---|
| 636 | exit; |
|---|
| 637 | } |
|---|
| 638 | |
|---|
| 639 | function display($tpl, $cid = null) { |
|---|
| 640 | $ctx =& $this->context(); |
|---|
| 641 | $this->init_plugins(); |
|---|
| 642 | $blog =& $ctx->stash('blog'); |
|---|
| 643 | if (!$blog) { |
|---|
| 644 | $db =& $this->db(); |
|---|
| 645 | $ctx->mt->db =& $db; |
|---|
| 646 | $blog =& $db->fetch_blog($this->blog_id); |
|---|
| 647 | $ctx->stash('blog', $blog); |
|---|
| 648 | $ctx->stash('blog_id', $this->blog_id); |
|---|
| 649 | $this->configure_paths($blog['blog_site_path']); |
|---|
| 650 | } |
|---|
| 651 | return $ctx->display($tpl, $cid); |
|---|
| 652 | } |
|---|
| 653 | |
|---|
| 654 | function fetch($tpl, $cid = null) { |
|---|
| 655 | $ctx =& $this->context(); |
|---|
| 656 | $this->init_plugins(); |
|---|
| 657 | $blog =& $ctx->stash('blog'); |
|---|
| 658 | if (!$blog) { |
|---|
| 659 | $db =& $this->db(); |
|---|
| 660 | $ctx->mt->db =& $db; |
|---|
| 661 | $blog =& $db->fetch_blog($this->blog_id); |
|---|
| 662 | $ctx->stash('blog', $blog); |
|---|
| 663 | $ctx->stash('blog_id', $this->blog_id); |
|---|
| 664 | $this->configure_paths($blog['blog_site_path']); |
|---|
| 665 | } |
|---|
| 666 | return $ctx->fetch($tpl, $cid); |
|---|
| 667 | } |
|---|
| 668 | |
|---|
| 669 | function log_dump() { |
|---|
| 670 | if ($_SERVER['REMOTE_ADDR']) { |
|---|
| 671 | // web view... |
|---|
| 672 | echo "<div class=\"debug\" style=\"border:1px solid red; margin:0.5em; padding: 0 1em; text-align:left; background-color:#ddd; color:#000\"><pre>"; |
|---|
| 673 | echo implode("\n", $this->log); |
|---|
| 674 | echo "</pre></div>\n\n"; |
|---|
| 675 | } else { |
|---|
| 676 | // console view... |
|---|
| 677 | $stderr = fopen('php://stderr', 'w'); |
|---|
| 678 | fwrite($stderr,implode("\n", $this->log)); |
|---|
| 679 | echo (implode("\n", $this->log)); |
|---|
| 680 | fclose($stderr); |
|---|
| 681 | } |
|---|
| 682 | } |
|---|
| 683 | |
|---|
| 684 | function error_handler($errno, $errstr, $errfile, $errline) { |
|---|
| 685 | if ($errno & (E_ALL ^ E_NOTICE)) { |
|---|
| 686 | $errstr = htmlentities($errstr); |
|---|
| 687 | $errfile = htmlentities($errfile); |
|---|
| 688 | $mtphpdir = $this->config('PHPDir'); |
|---|
| 689 | $ctx =& $this->context(); |
|---|
| 690 | $ctx->stash('blog_id', $this->blog_id); |
|---|
| 691 | $ctx->stash('blog', $this->db->fetch_blog($this->blog_id)); |
|---|
| 692 | $ctx->stash('error_message', $errstr."<!-- file: $errfile; line: $errline; code: $errno -->"); |
|---|
| 693 | $ctx->stash('error_code', $errno); |
|---|
| 694 | $http_error = $this->http_error; |
|---|
| 695 | if (!$http_error) { |
|---|
| 696 | $http_error = 500; |
|---|
| 697 | } |
|---|
| 698 | $ctx->stash('http_error', $http_error); |
|---|
| 699 | $ctx->stash('error_file', $errfile); |
|---|
| 700 | $ctx->stash('error_line', $errline); |
|---|
| 701 | $ctx->template_dir = $mtphpdir . DIRECTORY_SEPARATOR . 'tmpl'; |
|---|
| 702 | $ctx->caching = 0; |
|---|
| 703 | $ctx->stash('StaticWebPath', $this->config('StaticWebPath')); |
|---|
| 704 | $ctx->stash('PublishCharset', $this->config('PublishCharset')); |
|---|
| 705 | $charset = $this->config('PublishCharset'); |
|---|
| 706 | $out = $ctx->tag('Include', array('type' => 'dynamic_error', 'dynamic_error' => 1, 'system_template' => 1)); |
|---|
| 707 | if (isset($out)) { |
|---|
| 708 | header("Content-type: text/html; charset=".$charset); |
|---|
| 709 | echo $out; |
|---|
| 710 | } else { |
|---|
| 711 | header("HTTP/1.1 500 Server Error"); |
|---|
| 712 | header("Content-type: text/plain"); |
|---|
| 713 | echo "Error executing error template."; |
|---|
| 714 | } |
|---|
| 715 | exit; |
|---|
| 716 | } |
|---|
| 717 | } |
|---|
| 718 | |
|---|
| 719 | /*** |
|---|
| 720 | * Retrieves a context and rendering object. |
|---|
| 721 | */ |
|---|
| 722 | function &context() { |
|---|
| 723 | static $ctx; |
|---|
| 724 | if (isset($ctx)) return $ctx; |
|---|
| 725 | |
|---|
| 726 | require_once('MTViewer.php'); |
|---|
| 727 | $ctx = new MTViewer($this); |
|---|
| 728 | $ctx->mt =& $this; |
|---|
| 729 | $mtphpdir = $this->config('PHPDir'); |
|---|
| 730 | $mtlibdir = $this->config('PHPLibDir'); |
|---|
| 731 | $ctx->compile_check = 1; |
|---|
| 732 | $ctx->caching = false; |
|---|
| 733 | $ctx->plugins_dir[] = $mtlibdir; |
|---|
| 734 | $ctx->plugins_dir[] = $mtphpdir . DIRECTORY_SEPARATOR . "plugins"; |
|---|
| 735 | if ($this->debugging) { |
|---|
| 736 | $ctx->debugging_ctrl = 'URL'; |
|---|
| 737 | $ctx->debug_tpl = $mtphpdir . DIRECTORY_SEPARATOR . |
|---|
| 738 | 'extlib' . DIRECTORY_SEPARATOR . |
|---|
| 739 | 'smarty' . DIRECTORY_SEPARATOR . "libs" . DIRECTORY_SEPARATOR . |
|---|
| 740 | 'debug.tpl'; |
|---|
| 741 | } |
|---|
| 742 | #if (isset($this->config('SafeMode')) && ($this->config('SafeMode'))) { |
|---|
| 743 | # // disable PHP support |
|---|
| 744 | # $ctx->php_handling = SMARTY_PHP_REMOVE; |
|---|
| 745 | #} |
|---|
| 746 | return $ctx; |
|---|
| 747 | } |
|---|
| 748 | |
|---|
| 749 | function log($msg = null) { |
|---|
| 750 | $this->log[] = $msg; |
|---|
| 751 | } |
|---|
| 752 | |
|---|
| 753 | function translate($str, $params = null) { |
|---|
| 754 | return translate_phrase($str, $params); |
|---|
| 755 | } |
|---|
| 756 | |
|---|
| 757 | function translate_templatized_item($str) { |
|---|
| 758 | return translate_phrase($str[1]); |
|---|
| 759 | } |
|---|
| 760 | |
|---|
| 761 | function translate_templatized($tmpl) { |
|---|
| 762 | $cb = array($this, 'translate_templatized_item'); |
|---|
| 763 | $out = preg_replace_callback('/<(?:_|mt)_trans phrase="(.+?)".*?>/i', $cb, $tmpl); |
|---|
| 764 | return $out; |
|---|
| 765 | } |
|---|
| 766 | } |
|---|
| 767 | |
|---|
| 768 | function is_valid_email($addr) { |
|---|
| 769 | if (preg_match('/[ |\t|\r|\n]*\"?([^\"]+\"?@[^ <>\t]+\.[^ <>\t][^ <>\t]+)[ |\t|\r|\n]*/', $addr, $matches)) { |
|---|
| 770 | return $matches[1]; |
|---|
| 771 | } else { |
|---|
| 772 | return 0; |
|---|
| 773 | } |
|---|
| 774 | } |
|---|
| 775 | |
|---|
| 776 | $spam_protect_map = array(':' => ':', '@' => '@', '.' => '.'); |
|---|
| 777 | function spam_protect($str) { |
|---|
| 778 | global $spam_protect_map; |
|---|
| 779 | return strtr($str, $spam_protect_map); |
|---|
| 780 | } |
|---|
| 781 | |
|---|
| 782 | function datetime_to_timestamp($dt) { |
|---|
| 783 | $dt = preg_replace('/[^0-9]/', '', $dt); |
|---|
| 784 | $ts = mktime(substr($dt, 8, 2), substr($dt, 10, 2), substr($dt, 12, 2), substr($dt, 4, 2), substr($dt, 6, 2), substr($dt, 0, 4)); |
|---|
| 785 | return $ts; |
|---|
| 786 | } |
|---|
| 787 | |
|---|
| 788 | function offset_time($ts, $blog = null, $dir = null) { |
|---|
| 789 | if (isset($blog)) { |
|---|
| 790 | if (!is_array($blog)) { |
|---|
| 791 | global $mt; |
|---|
| 792 | $blog = $mt->db->fetch_blog($blog); |
|---|
| 793 | } |
|---|
| 794 | $offset = $blog['blog_server_offset']; |
|---|
| 795 | } else { |
|---|
| 796 | global $mt; |
|---|
| 797 | $offset = $mt->config('TimeOffset'); |
|---|
| 798 | } |
|---|
| 799 | intval($offset) or $offset = 0; |
|---|
| 800 | $tsa = localtime($ts); |
|---|
| 801 | |
|---|
| 802 | if ($tsa[8]) { // daylight savings offset |
|---|
| 803 | $offset++; |
|---|
| 804 | } |
|---|
| 805 | if ($dir == '-') { |
|---|
| 806 | $offset *= -1; |
|---|
| 807 | } |
|---|
| 808 | $ts += $offset * 3600; |
|---|
| 809 | return $ts; |
|---|
| 810 | } |
|---|
| 811 | function _strip_index($url, $blog) { |
|---|
| 812 | global $mt; |
|---|
| 813 | $index = $mt->config('IndexBasename'); |
|---|
| 814 | $ext = $blog['blog_file_extension']; |
|---|
| 815 | if ($ext != '') $ext = '.' . $ext; |
|---|
| 816 | $index = preg_quote($index . $ext); |
|---|
| 817 | $url = preg_replace("/\/$index(#.*)?$/", '/$1', $url); |
|---|
| 818 | return $url; |
|---|
| 819 | } |
|---|
| 820 | |
|---|
| 821 | function translate_phrase_param($str, $params = null) { |
|---|
| 822 | if (is_array($params) && (strpos($str, '[_') !== false)) { |
|---|
| 823 | for ($i = 1; $i <= count($params); $i++) { |
|---|
| 824 | $str = preg_replace("/\\[_$i\\]/", $params[$i-1], $str); |
|---|
| 825 | } |
|---|
| 826 | } |
|---|
| 827 | return $str; |
|---|
| 828 | } |
|---|
| 829 | ?> |
|---|