PageRenderTime 54ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 1ms

/addons/DynamicMTML.pack/php/dynamicmtml.php

http://github.com/movabletype/DynamicMTML
PHP | 1587 lines | 1505 code | 57 blank | 25 comment | 327 complexity | 3cbca6e74dc9b30fa7dd59fa20263891 MD5 | raw file
Possible License(s): MIT, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. class DynamicMTML {
  3. var $mt;
  4. var $ctx;
  5. var $__stash;
  6. var $db;
  7. var $config;
  8. var $cfg_file;
  9. var $mode;
  10. var $mod_rewrite;
  11. var $blog;
  12. var $blog_id;
  13. var $user;
  14. var $user_cookie;
  15. var $session;
  16. var $url;
  17. var $root;
  18. var $is_secure;
  19. var $file;
  20. var $base;
  21. var $path;
  22. var $static_path;
  23. var $script;
  24. var $request;
  25. var $cache;
  26. var $cache_dir;
  27. var $conditional;
  28. var $force_compile;
  29. var $param;
  30. var $query_string;
  31. var $contenttype;
  32. var $extension;
  33. var $build_type;
  34. var $result_type;
  35. var $fileinfo;
  36. var $template;
  37. var $basename;
  38. var $filemtime;
  39. var $cache_saved;
  40. var $text;
  41. var $contents;
  42. var $templates_c;
  43. var $preview;
  44. var $protocol;
  45. var $remote_ip;
  46. var $request_method;
  47. var $entry_ids_published;
  48. var $plugin_path;
  49. protected $debugging = FALSE;
  50. function init ( $args = array() ) {
  51. require_once( 'MTUtil.php' );
  52. $this->protocol = $_SERVER[ 'SERVER_PROTOCOL' ];
  53. $this->remote_ip = $_SERVER[ 'REMOTE_ADDR' ];
  54. if (! $this->request_method ) {
  55. $this->request_method = $_SERVER[ 'REQUEST_METHOD' ];
  56. }
  57. $this->static_path = $this->config( 'StaticWebPath' );
  58. foreach ( $args as $key => $val ) {
  59. $this->$key = $args[ $key ];
  60. if ( $key === 'param' ) {
  61. $this->query_string = $args[ $key ];
  62. }
  63. }
  64. if (! $mode = $this->get_param( '__mode' ) ) {
  65. $mode = 'default';
  66. }
  67. $this->mode = $mode;
  68. $this->entry_ids_published = array();
  69. }
  70. function set_context ( &$mt, &$ctx, $blog_id = NULL ) {
  71. $this->stash( 'mt', $mt );
  72. $this->stash( 'ctx', $ctx );
  73. if ( isset( $ctx ) ) {
  74. $ctx->stash( 'bootstrapper', $this );
  75. }
  76. $mt->db()->set_names( $mt );
  77. $this->stash( 'db', $mt->db() );
  78. $blog = $ctx->stash( 'blog' );
  79. if (! isset( $blog ) ) {
  80. $blog = $this->stash( 'blog' );
  81. }
  82. if (! isset( $blog ) ) {
  83. // if (! $blog_id ) {
  84. // $blog_id = $this->stash( 'blog_id' );
  85. // }
  86. if (! $blog_id ) {
  87. // $blog = $this->load( 'Blog',
  88. // array( 'class' => 'website', 'blog' ),
  89. // array( 'limit' => 1,
  90. // 'sort_order' => 'ascend',
  91. // 'sort' => 'id' ) );
  92. // $blog_id = $blog->id;
  93. } else {
  94. $blog = $mt->db()->fetch_blog( $blog_id );
  95. }
  96. }
  97. $templates_c = NULL;
  98. $cache = NULL;
  99. if ( isset( $blog ) ) {
  100. $ctx->stash( 'blog', $blog );
  101. $ctx->stash( 'blog_id', $blog_id );
  102. $this->stash( 'blog', $blog );
  103. $this->stash( 'blog_id', $blog_id );
  104. $site_path = $blog->site_path();
  105. $self = $site_path;
  106. $templates_c = $self . DIRECTORY_SEPARATOR . 'templates_c';
  107. $cache = $self . DIRECTORY_SEPARATOR . 'cache';
  108. } else {
  109. $self = $this->root . dirname( $_SERVER[ 'PHP_SELF' ] );
  110. $templates_c = $self . DIRECTORY_SEPARATOR . 'templates_c';
  111. $cache = $self . DIRECTORY_SEPARATOR . 'cache';
  112. }
  113. if (! is_dir( $templates_c ) ) {
  114. mkdir( $templates_c, 0755 );
  115. }
  116. $this->stash( 'templates_c', $templates_c );
  117. $cache = $self . DIRECTORY_SEPARATOR . 'cache';
  118. if (! is_dir( $cache ) ) {
  119. mkdir( $cache, 0755 );
  120. }
  121. $powercms_files_dir = NULL;
  122. if (! $powercms_files_dir = $mt->config( 'PowerCMSFilesDir' ) ) {
  123. $powercms_files_dir = dirname( $this->cfg_file ) . DIRECTORY_SEPARATOR . 'powercms_files';
  124. }
  125. $powercms_files_dir = preg_replace( "/DIRECTORY_SEPARATOR$/", '', $powercms_files_dir );
  126. if (! is_dir( $powercms_files_dir ) ) {
  127. mkdir( $powercms_files_dir, 0755 );
  128. }
  129. $this->stash( 'powercms_files_dir', $powercms_files_dir );
  130. $powercms_cache = $powercms_files_dir . DIRECTORY_SEPARATOR . 'cache';
  131. if (! is_dir( $powercms_cache ) ) {
  132. mkdir( $powercms_cache, 0755 );
  133. }
  134. $language = $mt->config( 'DefaultLanguage' );
  135. $l10n_dir = $this->stash( 'l10n_dir' );
  136. if ( is_array( $l10n_dir ) ) {
  137. foreach ( $l10n_dir as $plugin_l10n_dir ) {
  138. if ( is_dir( $plugin_l10n_dir ) ) {
  139. $l10n_file = $plugin_l10n_dir . DIRECTORY_SEPARATOR . 'l10n_' . $language . '.php';
  140. if ( file_exists( $l10n_file ) ) {
  141. require( $l10n_file );
  142. if ( isset( $Lexicon ) && is_array( $Lexicon ) ) {
  143. $this->add_lexicon( $language, $Lexicon );
  144. unset( $Lexicon );
  145. }
  146. }
  147. }
  148. }
  149. }
  150. if (! function_exists( 'is_valid_email' ) ) {
  151. require_once( dirname(__FILE__) . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'mt_util.php' );
  152. }
  153. }
  154. function user () {
  155. if (! $timeout = $this->config( 'UserSessionTimeout' ) ) {
  156. $timeout = 14400;
  157. }
  158. $author = $this->get_author( $this->ctx, $timeout );
  159. return $author;
  160. }
  161. function mt () {
  162. if ( $this->mt ) return $this->mt;
  163. global $mt;
  164. if ( $mt ) {
  165. return $this->stash( 'mt', $mt );
  166. }
  167. if ( $this->ctx() ) {
  168. return $this->stash( 'mt', $this->ctx()->mt );
  169. }
  170. global $ctx;
  171. if ( $ctx ) {
  172. return $this->stash( $ctx->mt );
  173. }
  174. return $this;
  175. }
  176. function ctx () {
  177. if ( $this->ctx ) return $this->ctx;
  178. global $ctx;
  179. if ( $ctx ) {
  180. return $this->stash( 'ctx', $ctx );
  181. }
  182. }
  183. function db () {
  184. if ( $this->db ) return $this->db;
  185. if ( $this->ctx() ) {
  186. return $this->stash( 'db', $this->ctx()->mt->db() );
  187. }
  188. }
  189. function init_mt ( &$mt, &$ctx, $blog_id ) {
  190. if ( is_object( $blog_id ) ) {
  191. $blog = $blog_id;
  192. $blog_id = $blog->id;
  193. } else {
  194. if ( ( $this->stash( 'blog_id' ) ) != $blog_id ) {
  195. $blog = $this->load( 'Blog', $blog_id );
  196. }
  197. }
  198. if ( (! $mt->blog_id() ) || ( $mt->blog_id() != $blog_id ) ) {
  199. $mt->init( $blog_id, $this->cfg_file );
  200. $mt->db()->set_names( $mt );
  201. $ctx->stash( 'blog', $blog );
  202. $ctx->stash( 'blog_id', $blog_id );
  203. $this->stash( 'blog', $blog );
  204. $this->stash( 'blog_id', $blog_id );
  205. }
  206. }
  207. function user_cookie () {
  208. return $this->user_cookie;
  209. }
  210. function blog ( $id = NULL ) {
  211. $blog_id = $id;
  212. $mt = $this->mt();
  213. if (! $blog_id ) {
  214. if ( $blog = $this->blog ) {
  215. return $blog;
  216. }
  217. $blog_id = $mt->blog_id();
  218. }
  219. if (! $mt->db() ) {
  220. return NULL;
  221. }
  222. if (! $blog_id ) {
  223. return NULL;
  224. }
  225. $blog = $mt->db()->fetch_blog( $blog_id );
  226. if (! $blog ) {
  227. $blog = $this->load( 'Blog', $blog_id );
  228. }
  229. if (! $blog ) {
  230. $terms = array( 'id' => $blog_id, 'class' => array( 'website', 'blog' ) );
  231. $blog = $this->load( 'Blog', $terms, array( 'limit' => 1 ) );
  232. if (! $blog ) {
  233. $ctx = $this->ctx();
  234. if ( $ctx ) {
  235. if ( $ctx->stash( 'blog' ) ) {
  236. $blog = $ctx->stash( 'blog' );
  237. }
  238. }
  239. }
  240. }
  241. if (! $id ) {
  242. $this->stash( 'blog', $blog );
  243. $ctx->stash( 'blog', $blog );
  244. if ( $blog ) {
  245. $this->stash( 'blog_id', $blog->id );
  246. $ctx->stash( 'blog_id', $blog->id );
  247. }
  248. }
  249. return $blog;
  250. }
  251. function blog_id () {
  252. if ( $this->blog_id ) {
  253. return $this->blog_id;
  254. }
  255. $mt = $this->mt();
  256. return $this->stash( 'blog_id', $mt->blog_id() );
  257. }
  258. function mode () {
  259. return $this->mode;
  260. }
  261. function base () {
  262. return $this->base;
  263. }
  264. function path () {
  265. return $this->path;
  266. }
  267. function remote_ip () {
  268. return $this->remote_ip;
  269. }
  270. function is_secure () {
  271. return $this->is_secure;
  272. }
  273. function query_string () {
  274. if ( $query_string = $this->query_string ) {
  275. return $query_string;
  276. }
  277. if ( $params = $this->param() ) {
  278. $params_array = array();
  279. if ( is_array( $params ) ) {
  280. foreach ( $params as $key => $value ) {
  281. array_push( $params_array, "$key=$value" );
  282. }
  283. if ( $params_array ) {
  284. return join( '&', $params_array );
  285. }
  286. }
  287. }
  288. }
  289. function static_path () {
  290. return $this->static_path;
  291. }
  292. function request_method () {
  293. return $this->request_method;
  294. }
  295. function log ( $msg = NULL, $args = NULL ) {
  296. if ( ( is_array( $msg ) ) || ( is_object( $msg ) ) ) {
  297. ob_start();
  298. var_dump( $msg );
  299. $msg = ob_get_contents();
  300. ob_end_clean();
  301. }
  302. require_once( 'dynamicmtml.util.php' );
  303. require_once( 'class.mt_log.php' );
  304. $_log = new Log;
  305. $_log->message = $msg;
  306. if ( $user = $this->user() ) {
  307. $_log->author_id = $user->id;
  308. $_log->created_by = $user->id;
  309. } else {
  310. $_log->author_id = 0;
  311. }
  312. if ( $args && __is_hash( $args ) ) {
  313. foreach ( $args as $key => $val ) {
  314. if ( $_log->has_column( $key ) ) {
  315. $_log->$key = $val;
  316. }
  317. }
  318. }
  319. $_log->ip = $this->remote_ip;
  320. if (! $_log->class ) {
  321. $_log->class = 'system';
  322. }
  323. if (! $_log->level ) {
  324. $_log->level = 1;
  325. }
  326. if (! $_log->blog_id ) {
  327. if ( $blog = $this->blog ) {
  328. $_log->blog_id = $blog->id;
  329. }
  330. }
  331. if (! $_log->blog_id ) {
  332. $_log->blog_id = 0;
  333. }
  334. $ts = gmdate( "YmdHis" );
  335. $_log->created_on = $ts;
  336. $_log->modified_on = $ts;
  337. $_log->Save();
  338. }
  339. function cache ( $key, $val = NULL ) {
  340. return $this->stash( $key, $val );
  341. }
  342. function stash ( $key, $val = NULL ) {
  343. if ( isset ( $val ) ) {
  344. $this->$key = $val;
  345. }
  346. if ( isset( $this->$key ) ) {
  347. return $this->$key;
  348. }
  349. return NULL;
  350. }
  351. function param ( $param = NULL ) {
  352. $to_encoding = $this->config( 'PublishCharset' );
  353. $to_encoding or $to_encoding = 'UTF-8';
  354. if ( $param ) {
  355. if ( $value = $this->get_param( $param ) ) {
  356. if (! is_array( $value ) ) {
  357. $from_encoding = mb_detect_encoding( $value, 'UTF-8,EUC-JP,SJIS,JIS' );
  358. $value = mb_convert_encoding( $value, $to_encoding, $from_encoding );
  359. return $value;
  360. } else {
  361. $new_array = array();
  362. foreach ( $value as $val ) {
  363. $from_encoding = mb_detect_encoding( $val, 'UTF-8,EUC-JP,SJIS,JIS' );
  364. $val = mb_convert_encoding( $val, $to_encoding, $from_encoding );
  365. array_push ( $new_array, $val );
  366. }
  367. return $new_array;
  368. }
  369. }
  370. } else {
  371. $vars = $_REQUEST;
  372. $params = array();
  373. foreach ( $vars as $key => $value ) {
  374. if ( $_GET[ $key ] || $_POST[ $key ] ) {
  375. if ( is_string( $value ) ) {
  376. $from_encoding = mb_detect_encoding( $value, 'UTF-8,EUC-JP,SJIS,JIS' );
  377. $value = mb_convert_encoding( $value, $to_encoding, $from_encoding );
  378. }
  379. $params[ $key ] = $value;
  380. }
  381. }
  382. return $params;
  383. }
  384. }
  385. function config ( $id, $value = NULL ) {
  386. $orig_id = $id;
  387. $id = strtolower( $id );
  388. if ( isset( $value ) ) {
  389. $this->config[ $id ] = $value;
  390. return $value;
  391. }
  392. if ( isset( $this->config[ $id ] ) ) {
  393. return $this->config[ $id ];
  394. }
  395. $plugins_config = $this->stash( 'plugins_config' );
  396. if ( is_array( $plugins_config ) ) {
  397. foreach ( $plugins_config as $key => $val ) {
  398. if ( isset( $val[ 'config_settings' ] ) ) {
  399. if ( isset( $val[ 'config_settings' ][ $id ] ) ) {
  400. if ( isset( $val[ 'config_settings' ][ $id ][ 'default' ] ) ) {
  401. $this->config[ $id ] = $val[ 'config_settings' ][ $id ][ 'default' ];
  402. return $val[ 'config_settings' ][ $id ][ 'default' ];
  403. break;
  404. }
  405. }
  406. if ( isset( $val[ 'config_settings' ][ $orig_id ] ) ) {
  407. if ( isset( $val[ 'config_settings' ][ $orig_id ][ 'default' ] ) ) {
  408. $this->config[ $id ] = $val[ 'config_settings' ][ $orig_id ][ 'default' ];
  409. return $val[ 'config_settings' ][ $orig_id ][ 'default' ];
  410. break;
  411. }
  412. }
  413. }
  414. }
  415. }
  416. return NULL;
  417. }
  418. function configure ( $file = NULL ) {
  419. // if ( isset( $this->config ) ) return $config;
  420. $this->cfg_file = $file;
  421. $cfg = array();
  422. if ( file_exists( $file ) ) {
  423. if ( $fp = file( $file ) ) {
  424. foreach ( $fp as $line ) {
  425. if (! preg_match('/^\s*\#/i', $line ) ) {
  426. if ( preg_match( '/^\s*(\S+)\s+(.*)$/', $line, $regs ) ) {
  427. $key = strtolower( trim( $regs[1] ) );
  428. $value = trim( $regs[2] );
  429. if ( $key === 'pluginpath' ) {
  430. $cfg[ $key ][] = $value;
  431. } else {
  432. $cfg[ $key ] = $value;
  433. }
  434. }
  435. }
  436. }
  437. } else {
  438. // die( "Unable to open configuration file $file" );
  439. }
  440. }
  441. $cfg[ 'mtdir' ] = realpath( dirname( $file ) );
  442. $cfg[ 'phpdir' ] = $cfg[ 'mtdir' ] . DIRECTORY_SEPARATOR . 'php';
  443. $cfg[ 'phplibdir' ] = $cfg[ 'phpdir' ] . DIRECTORY_SEPARATOR . 'lib';
  444. $cfg[ 'powercmsdir' ] = realpath( dirname( __FILE__ ) );
  445. $this->config =& $cfg;
  446. isset( $cfg[ 'pluginpath' ] ) or
  447. $cfg[ 'pluginpath' ] = array( $this->config( 'MTDir' ) . DIRECTORY_SEPARATOR . 'plugins' );
  448. $cfg[ 'pluginpath' ][] = $this->config( 'MTDir' ) . DIRECTORY_SEPARATOR . 'addons';
  449. if ( strtoupper( substr( PHP_OS, 0, 3 ) === 'WIN' ) ) {
  450. $path_sep = ';';
  451. } else {
  452. $path_sep = ':';
  453. }
  454. ini_set( 'include_path',
  455. $cfg[ 'powercmsdir' ] . $path_sep .
  456. $cfg[ 'powercmsdir' ] . DIRECTORY_SEPARATOR . "callbacks" . $path_sep .
  457. $cfg[ 'phpdir' ] . DIRECTORY_SEPARATOR . "lib" . $path_sep .
  458. $cfg[ 'phpdir' ] . DIRECTORY_SEPARATOR . "extlib" . $path_sep .
  459. $cfg[ 'phpdir' ] . DIRECTORY_SEPARATOR . "extlib" . DIRECTORY_SEPARATOR . "smarty" . DIRECTORY_SEPARATOR . "libs" . $path_sep .
  460. $cfg[ 'phpdir' ] . DIRECTORY_SEPARATOR . "extlib" . DIRECTORY_SEPARATOR . "smarty" . DIRECTORY_SEPARATOR . "libs" . DIRECTORY_SEPARATOR . 'plugins' . $path_sep .
  461. $cfg[ 'phpdir' ] . DIRECTORY_SEPARATOR . "extlib" . DIRECTORY_SEPARATOR . "adodb5" . $path_sep .
  462. $cfg[ 'phpdir' ] . DIRECTORY_SEPARATOR . "extlib" . DIRECTORY_SEPARATOR . "FirePHPCore" . $path_sep .
  463. ini_get( 'include_path' )
  464. );
  465. $this->init_plugin_dir();
  466. if (! $powercms_files_dir = $this->config( 'PowerCMSFilesDir' ) ) {
  467. $powercms_files_dir = dirname( $this->cfg_file ) . DIRECTORY_SEPARATOR . 'powercms_files';
  468. }
  469. $powercms_files_dir = $this->__add_slash( $powercms_files_dir, FALSE );
  470. $this->stash( 'powercms_files_dir', $powercms_files_dir );
  471. $this->run_callbacks( 'init_request' );
  472. }
  473. function get_args () {
  474. $args = get_class_vars( 'DynamicMTML' );
  475. foreach ( $args as $key => $val ) {
  476. $args[ $key ] = $this->$key;
  477. }
  478. return $args;
  479. }
  480. function file () {
  481. return $this->file;
  482. }
  483. function url () {
  484. return $this->url;
  485. }
  486. function cache_filename ( $blog_id, $file, $param ) {
  487. $basename = "blog_id_{$blog_id}_" . md5( $file );
  488. if ( $param ) {
  489. $basename .= '_' . md5( $param );
  490. }
  491. $pinfo = pathinfo( $file );
  492. $extension = NULL;
  493. if ( isset ( $pinfo[ 'extension' ] ) ) {
  494. $extension = $pinfo[ 'extension' ];
  495. }
  496. $cache = $this->cache_dir . DIRECTORY_SEPARATOR . $basename . '.' . $extension;
  497. return $cache;
  498. }
  499. function get_smarty_template ( &$ctx, $data = NULL, $basename, $filemtime = NULL ) {
  500. $template = $ctx->_get_compile_path( 'var:' . $basename );
  501. if (! isset( $data ) ) {
  502. $blog = $ctx->stash( 'blog' );
  503. // if ( isset ( $blog ) ) {
  504. // $template = $blog->site_path() . DIRECTORY_SEPARATOR . $template;
  505. // } else {
  506. if (! $this->no_database ) {
  507. $ctx->force_compile = TRUE;
  508. }
  509. $template = dirname( $this->templates_c ) . DIRECTORY_SEPARATOR . $template;
  510. // }
  511. }
  512. if ( $filemtime && file_exists( $template ) ) {
  513. if ( $filemtime > filemtime( $template ) ) {
  514. $ctx->force_compile = TRUE;
  515. }
  516. }
  517. return $template;
  518. }
  519. function init_plugin_dir () {
  520. if ( $this->stash( 'init_plugin_dir' ) ) {
  521. return;
  522. }
  523. require_once( 'dynamicmtml.util.php' );
  524. require_once( 'class.dynamicmtml_plugin.php' );
  525. $plugins_config = array(); // Config from config.yaml or config.php
  526. // 'dynamicmtml' => array( 'id' => 'DynamicMTML', ...)
  527. $plugins_dir_path = array(); // 'dynamicmtml' => '/path/to/DynamicMTML.pack'
  528. $callback_dir = array(); // '/path/to/DynamicMTML.pack/php/callbacks', ...
  529. $l10n_dir = array(); // '/path/to/DynamicMTML.pack/php/l10n', ...
  530. $publisher_dir = array(); // '/path/to/DynamicMTML.pack/php/publishers', ...
  531. $callbacks = array(); // array ( 'post_init' => array( 'pluginkey' => 'routine_name' ), ...
  532. $blocks = array(); // array ( 'tagname' => array( 'pluginkey' => 'hdlr_name' ), ...
  533. $functions = array(); // array ( 'tagname' => array( 'pluginkey' => 'hdlr_name' ), ...
  534. $modifiers = array(); // array ( 'tagname' => array( 'pluginkey' => 'hdlr_name' ), ...
  535. $pluginpath = $this->config( 'pluginpath' );
  536. $spyc = __cat_file( array( dirname( __FILE__ ), 'extlib', 'spyc', 'spyc.php' ) );
  537. if ( file_exists( $spyc ) ) {
  538. require_once( $spyc );
  539. } else {
  540. $spyc = NULL;
  541. }
  542. sort( $pluginpath );
  543. foreach ( $pluginpath as $plugin_dir ) {
  544. if ( is_dir( $plugin_dir ) ) {
  545. if ( $dh = opendir( $plugin_dir ) ) {
  546. while ( ( $dir = readdir( $dh ) ) !== FALSE ) {
  547. if (! preg_match ( '/^\./', $dir ) ) {
  548. $plugin_base = $plugin_dir . DIRECTORY_SEPARATOR . $dir;
  549. $plugin = NULL;
  550. $config = array();
  551. if ( is_dir( $plugin_base ) ) {
  552. $split_dirs = explode( DIRECTORY_SEPARATOR, $plugin_base );
  553. $plugin_class = $split_dirs[ count( $split_dirs ) - 1 ];
  554. $plugin_key = strtolower( $split_dirs[ count( $split_dirs ) - 1 ] );
  555. if ( preg_match( '/\.pack$/', $plugin_class ) ) {
  556. $plugin_class = strtr( $plugin_class, '.', '_' );
  557. $plugin_key = preg_replace( '/\.pack$/', '', $plugin_key );
  558. }
  559. $plugins_dir_path[ $plugin_key ] = $plugin_base;
  560. $plugin_php_dir = $plugin_base . DIRECTORY_SEPARATOR . 'php';
  561. $config_php = $plugin_php_dir . DIRECTORY_SEPARATOR . 'config.php';
  562. $config_yaml = $plugin_base . DIRECTORY_SEPARATOR . 'config.yaml';
  563. if ( file_exists( $config_yaml ) ) {
  564. if ( $spyc ) {
  565. $config = Spyc::YAMLLoad( $config_yaml );
  566. if ( $config ) {
  567. $config[ 'plugin_path' ] = $plugin_base;
  568. if (! file_exists( $config_php ) ) {
  569. $config = $this->__adjust_callbacks( $config );
  570. }
  571. $plugins_config[ $plugin_key ] = $config;
  572. } else {
  573. $config = array();
  574. }
  575. }
  576. }
  577. if ( is_dir( $plugin_php_dir ) ) {
  578. if ( file_exists( $config_php ) ) {
  579. require_once( $config_php );
  580. $plugin_registry = NULL;
  581. if ( class_exists( $plugin_class ) ) {
  582. $plugin = new $plugin_class;
  583. $plugin->app = $this;
  584. if ( isset( $plugin->registry ) ) {
  585. $plugin_registry = $plugin->registry;
  586. }
  587. // $config = $plugin->registry;
  588. } else {
  589. $cfg_key = 'mt_plugin_' . $plugin_key;
  590. if ( isset( $$cfg_key ) ) {
  591. $plugin_registry = $$cfg_key;
  592. }
  593. // $config = $$cfg_key;
  594. }
  595. if ( $plugin_registry ) {
  596. if (! $config ) {
  597. $config = $plugin_registry;
  598. } else {
  599. foreach ( $plugin_registry as $key => $val ) {
  600. $config[ $key ] = $val;
  601. }
  602. }
  603. }
  604. $config = $this->__adjust_callbacks( $config );
  605. $config[ 'plugin_path' ] = $plugin_base;
  606. $config[ 'plugin' ] = $plugin;
  607. // $plugins_config[ $plugin_key ] = $config;
  608. }
  609. if ( $config ) {
  610. if ( isset( $config[ 'callbacks' ] ) ) {
  611. $cb = $config[ 'callbacks' ];
  612. foreach ( $cb as $key => $val ) {
  613. if ( isset( $callbacks[ $key ] ) ) {
  614. $cbs = $callbacks[ $key ]; //array
  615. $cbs[ $plugin_key ] = $val;
  616. $callbacks[ $key ] = $cbs;
  617. } else {
  618. $callbacks[ $key ] = array( $plugin_key => $val );
  619. }
  620. }
  621. }
  622. if ( isset( $plugin ) ) {
  623. if ( is_object( $plugin ) ) {
  624. foreach ( $config as $cfg => $value ) {
  625. $plugin->$cfg = $value;
  626. }
  627. $plugin->app = $this;
  628. }
  629. }
  630. $config[ 'plugin_key' ] = $plugin_key;
  631. $plugins_config[ $plugin_key ] = $config;
  632. if ( isset( $config[ 'tags' ] ) ) {
  633. $this->__adjust_tags( $config, $blocks, $functions, $modifiers );
  634. }
  635. }
  636. $plugin_callback_dir = $plugin_php_dir . DIRECTORY_SEPARATOR . 'callbacks';
  637. if ( is_dir( $plugin_callback_dir ) ) {
  638. array_push( $callback_dir, $plugin_callback_dir );
  639. }
  640. $plugin_l10n_dir = $plugin_php_dir . DIRECTORY_SEPARATOR . 'l10n';
  641. if ( is_dir( $plugin_l10n_dir ) ) {
  642. array_push( $l10n_dir, $plugin_l10n_dir );
  643. }
  644. $plugin_publisher_dir = $plugin_php_dir . DIRECTORY_SEPARATOR . 'publishers';
  645. if ( is_dir( $plugin_publisher_dir ) ) {
  646. array_push( $publisher_dir, $plugin_publisher_dir );
  647. }
  648. }
  649. }
  650. }
  651. }
  652. }
  653. }
  654. }
  655. $this->stash( 'plugins_config', $plugins_config );
  656. $this->stash( 'plugins_directories', $plugins_dir_path );
  657. $this->stash( 'callback_dir', $callback_dir );
  658. $this->stash( 'l10n_dir', $l10n_dir );
  659. $this->stash( 'publisher_dir', $publisher_dir );
  660. $this->stash( 'plugins_callbacks', $callbacks );
  661. $this->stash( 'block_tags', $blocks );
  662. $this->stash( 'function_tags', $functions );
  663. $this->stash( 'modifiers', $modifiers );
  664. $this->stash( 'init_plugin_dir', 1 );
  665. }
  666. function __adjust_tags ( $config, &$blocks, &$functions, &$modifiers ) {
  667. $plugin_key = $config[ 'plugin_key' ];
  668. if ( isset( $config[ 'tags' ] ) ) {
  669. $config_tags = $config[ 'tags' ];
  670. if ( is_array( $config_tags ) ) {
  671. foreach ( $config_tags as $kind => $tags ) {
  672. if ( is_array( $tags ) ) {
  673. foreach ( $tags as $tag => $funk ) {
  674. $tag = strtolower( $tag );
  675. if ( preg_match( '/::/', $funk ) ) {
  676. $path = explode( '::', $funk );
  677. $funk = $path[ count( $path ) - 1 ];
  678. }
  679. if ( $kind === 'block' ) {
  680. $tag = preg_replace( '/\?$/', '', $tag );
  681. $blocks[ $tag ] = array( $plugin_key => $funk );
  682. } elseif ( $kind === 'function' ) {
  683. $functions[ $tag ] = array( $plugin_key => $funk );
  684. } elseif ( $kind === 'modifier' ) {
  685. $modifiers[ $tag ] = array( $plugin_key => $funk );
  686. }
  687. }
  688. }
  689. }
  690. }
  691. }
  692. }
  693. function __adjust_callbacks ( $config ) {
  694. if ( isset( $config[ 'callbacks' ] ) ) {
  695. $new_config = array();
  696. $cb = $config[ 'callbacks' ];
  697. foreach ( $cb as $key => $val ) {
  698. if ( is_array( $val ) ) {
  699. if ( isset( $val[ 'handler' ] ) ) {
  700. $val = $val[ 'handler' ];
  701. } else {
  702. $val = NULL;
  703. }
  704. }
  705. if ( $val ) {
  706. if ( preg_match( '/::/', $val ) ) {
  707. $path = explode( '::', $val );
  708. $val = $path[ count( $path ) - 1 ];
  709. }
  710. $new_config[ $key ] = $val;
  711. }
  712. }
  713. if ( $new_config ) {
  714. $config[ 'callbacks' ] = $new_config;
  715. } else {
  716. unset( $config[ 'callbacks' ] );
  717. }
  718. }
  719. return $config;
  720. }
  721. function run_tasks ( $task = NULL, $prefix = 'tasks', $sleep = NULL ) {
  722. $do;
  723. require_once( 'dynamicmtml.util.php' );
  724. $mt_dir = dirname( $this->cfg_file );
  725. $mt_dir = preg_replace( '/[^A-Za-z0-9]+/', '_', $mt_dir );
  726. $lock_name = "mt-tasks-{$mt_dir}.lock";
  727. $temp_dir = $this->config( 'TempDir' );
  728. if (! $temp_dir ) {
  729. $temp_dir = __cat_dir( $this->powercms_files_dir, 'lock' ) ;
  730. }
  731. $lock_file = __cat_file( $temp_dir, $lock_name );
  732. if ( file_exists( $lock_file ) ) {
  733. $mtime = filemtime( $lock_file );
  734. $frequency = time() - $mtime;
  735. if ( $frequency < 86400 ) {
  736. $msg = $this->translate( 'The process is skipped the lock file [_1] exists.', $lock_file );
  737. $args = array( 'categoey' => $prefix,
  738. 'level' => 4,
  739. 'class' => 'system',
  740. );
  741. $this->log( $msg, $args );
  742. return 0;
  743. }
  744. }
  745. $plugins_config = $this->stash( 'plugins_config' );
  746. $do_tasks = array();
  747. if (! isset( $plugins_config ) ) {
  748. return 0;
  749. }
  750. if (! is_array( $plugins_config ) ) {
  751. return 0;
  752. }
  753. if (! touch( $lock_file ) ) {
  754. $msg = $this->translate( 'Unable to secure lock for executing system tasks. Make sure your TempDir location ([_1]) is writable.', $temp_dir );
  755. $args = array( 'categoey' => $prefix,
  756. 'level' => 4,
  757. 'class' => 'system',
  758. );
  759. $this->log( $msg, $args );
  760. return 0;
  761. }
  762. foreach ( $plugins_config as $config ) {
  763. if ( isset( $config[ $prefix ] ) ) {
  764. $plugins_tasks = $config[ $prefix ];
  765. foreach ( $plugins_tasks as $plugin_key => $settings ) {
  766. if ( (! $task ) || ( $task === $plugin_key ) ) {
  767. $label = isset ( $settings[ 'label' ] ) ?
  768. $label = $this->translate( $settings[ 'label' ] ) : $label = $plugin_key;
  769. $plugin = NULL;
  770. $code = NULL;
  771. $plugin = $config[ 'plugin' ];
  772. if ( $plugin && is_object( $plugin ) ) {
  773. $code = $settings[ 'code' ];
  774. if ( $code ) {
  775. if ( preg_match( '/::/', $code ) ) {
  776. $path = explode( '::', $code );
  777. $code = $path[ count( $path ) - 1 ];
  778. }
  779. if (! method_exists( $plugin, $code ) ) {
  780. $plugin = NULL;
  781. }
  782. } else {
  783. $plugin = NULL;
  784. }
  785. }
  786. if (! $plugin ) {
  787. $plugin = __cat_file( $config[ 'plugin_path' ],
  788. array( 'php', $prefix, "{$plugin_key}.php" ) );
  789. }
  790. if ( $plugin ) {
  791. if ( $do ) {
  792. if ( $sleep ) sleep ( $sleep );
  793. }
  794. if ( $prefix === 'tasks' ) {
  795. $frequency = isset ( $settings[ 'frequency' ] ) ?
  796. $frequency = $settings[ 'frequency' ] : $frequency = 86400;
  797. if ( $plugin_key === 'FuturePost' ) {
  798. $futurepostfrequency = $this->config( 'FuturePostFrequency' );
  799. if ( $futurepostfrequency ) {
  800. $frequency = $futurepostfrequency;
  801. $frequency *= 60;
  802. }
  803. }
  804. $session = $this->get_by_key( 'Session', array( 'id' => "Task:{$plugin_key}",
  805. 'kind' => 'PT' ) );
  806. $start = $session->start;
  807. if ( (! $start ) || ( ( time() - $start ) > $frequency ) ) {
  808. $run_task = FALSE;
  809. $task_error = FALSE;
  810. if ( is_object ( $plugin ) ) {
  811. if ( $code ) {
  812. try {
  813. if ( $do = $plugin->$code( $this ) ) {
  814. array_push( $do_tasks, $label );
  815. }
  816. $run_task = TRUE;
  817. } catch ( Exception $e ) {
  818. $task_error = $e->getMessage();
  819. }
  820. }
  821. } else {
  822. if ( file_exists( $plugin ) ) {
  823. require_once( $plugin );
  824. }
  825. $function = "{$prefix}_{$plugin_key}";
  826. if ( function_exists( $function ) ) {
  827. try {
  828. if ( $do = $function( $this ) ) {
  829. array_push( $do_tasks, $label );
  830. }
  831. $run_task = TRUE;
  832. } catch ( Exception $e ) {
  833. $task_error = $e->getMessage();
  834. }
  835. }
  836. }
  837. if ( $run_task ) {
  838. $session->start = time();
  839. if ( $session->_saved ) {
  840. $session->Update();
  841. } else {
  842. $session->Save();
  843. }
  844. }
  845. }
  846. } elseif ( $prefix === 'task_workers' ) {
  847. $class = isset ( $settings[ 'class' ] ) ?
  848. $class = $settings[ 'class' ] : $class = NULL;
  849. $jobs = $this->load( 'Ts_Job', array( 'name' => $class ) );
  850. if ( is_object ( $plugin ) ) {
  851. if ( $code && $jobs ) {
  852. try {
  853. if ( $do = $plugin->$code( $this, $jobs ) ) {
  854. array_push( $do_tasks, $label );
  855. }
  856. $run_task = TRUE;
  857. } catch ( Exception $e ) {
  858. $task_error = $e->getMessage();
  859. }
  860. }
  861. } else {
  862. if ( file_exists( $plugin ) ) {
  863. require_once( $plugin );
  864. }
  865. $function = "{$prefix}_{$plugin_key}";
  866. if ( function_exists( $function ) ) {
  867. if ( $jobs ) {
  868. try {
  869. if ( $do = $function( $this, $jobs ) ) {
  870. array_push( $do_tasks, $label );
  871. }
  872. $run_task = TRUE;
  873. } catch ( Exception $e ) {
  874. $task_error = $e->getMessage();
  875. }
  876. }
  877. }
  878. }
  879. }
  880. if ( $task_error ) {
  881. $msg = $this->translate( 'Error during task \'[_1]\': [_2]', array( $label, $task_error ) );
  882. $args = array( 'categoey' => $prefix,
  883. 'level' => 4,
  884. 'class' => 'system',
  885. );
  886. $this->log( $msg, $args );
  887. }
  888. }
  889. }
  890. }
  891. }
  892. }
  893. if ( $do_tasks ) {
  894. $msg = $this->translate( 'Scheduled Tasks Update' );
  895. $ts = gmdate( "Y-m-d H:i:s" );
  896. $metadata = "[{$ts}] " . $this->translate( 'The following tasks were run:' );
  897. $ts = gmdate( "Y-m-d H:i:s" );
  898. $metadata .= ' ' . join( ', ', $do_tasks );
  899. $args = array( 'categoey' => $prefix,
  900. 'level' => 1,
  901. 'class' => 'system',
  902. 'metadata' => $metadata,
  903. );
  904. $this->log( $msg, $args );
  905. }
  906. if ( file_exists( $lock_file ) ) {
  907. unlink ( $lock_file );
  908. }
  909. return $do;
  910. }
  911. function run_workers ( $sleep = 5, $worker = NULL ) {
  912. return $this->run_tasks( $worker, 'task_workers', $sleep );
  913. }
  914. public function &context () {
  915. // TODO:
  916. require_once( 'dynamicmtml.util.php' );
  917. $ctx = $this->ctx;
  918. if ( isset( $ctx ) ) return $ctx;
  919. $mtphpdir = $this->config( 'PHPDir' );
  920. $mtlibdir = $this->config( 'PHPLibDir' );
  921. require_once( 'MTViewer.php' );
  922. $ctx = new MTViewer( $this );
  923. $ctx->mt =& $this;
  924. $ctx->compile_check = 1;
  925. $ctx->caching = FALSE;
  926. $ctx->plugins_dir[] = $mtlibdir;
  927. $ctx->plugins_dir[] = $mtphpdir . DIRECTORY_SEPARATOR . "plugins";
  928. if ( $this->debugging ) {
  929. $ctx->debugging_ctrl = 'URL';
  930. $ctx->debug_tpl = __cat_file( $mtphpdir, array( 'extlib', 'smarty', 'libs', 'debug.tpl' ) );
  931. }
  932. return $ctx;
  933. }
  934. function run_callbacks ( $callback, $mt = NULL, &$ctx = NULL, &$args = NULL, &$content = NULL ) {
  935. $class = get_class( $this );
  936. $callback_dir = array();
  937. $plugins_callbacks = array();
  938. if ( $class != 'DynamicMTML' ) {
  939. $callback_dir = $this->app->stash( 'callback_dir' );
  940. $plugins_callbacks = $this->app->stash( 'plugins_callbacks' );
  941. } else {
  942. $callback_dir = $this->stash( 'callback_dir' );
  943. $plugins_callbacks = $this->stash( 'plugins_callbacks' );
  944. }
  945. $do_filter = TRUE;
  946. if (! $args ) {
  947. $args = $this->get_args();
  948. }
  949. $orig_contenttype = NULL;
  950. if ( isset ( $args ) ) {
  951. if ( isset( $args[ 'contenttype' ] ) ) {
  952. $orig_contenttype = $args[ 'contenttype' ];
  953. }
  954. }
  955. if ( isset( $ctx ) ) {
  956. $ctx->stash( 'bootstrapper', $this );
  957. }
  958. if ( is_array( $plugins_callbacks ) ) {
  959. if ( isset( $plugins_callbacks[ $callback ] ) ) {
  960. $callbacks = $plugins_callbacks[ $callback ];
  961. if ( $callbacks ) {
  962. foreach ( $callbacks as $plugin => $method ) {
  963. $component = $this->component( $plugin );
  964. if ( method_exists( $component, $method ) ) {
  965. $res = $component->$method( $mt, $ctx, $args, $content );
  966. if (! $res ) {
  967. $do_filter = FALSE;
  968. }
  969. if ( isset ( $mt ) ) {
  970. $this->set_args_all( $args );
  971. }
  972. }
  973. }
  974. }
  975. }
  976. }
  977. if ( is_array( $callback_dir ) ) {
  978. foreach ( $callback_dir as $plugin_callback_dir ) {
  979. if ( is_dir( $plugin_callback_dir ) ) {
  980. $dirs = explode( DIRECTORY_SEPARATOR, $plugin_callback_dir );
  981. $plugin = strtolower( $dirs[ count( $dirs ) - 3 ] );
  982. $function = $plugin . '_' . $callback;
  983. $function = strtr( $function, '.', '_' );
  984. $require = $plugin_callback_dir . DIRECTORY_SEPARATOR . $function . '.php';
  985. if ( file_exists( $require ) ) {
  986. require_once $require;
  987. $res = $function( $mt, $ctx, $args, $content );
  988. if (! $res ) {
  989. $do_filter = FALSE;
  990. }
  991. if ( isset ( $mt ) ) {
  992. $this->set_args_all( $args );
  993. }
  994. }
  995. }
  996. }
  997. }
  998. if ( isset( $ctx ) ) {
  999. if ( isset( $orig_contenttype ) ) {
  1000. if ( $orig_contenttype != $args[ 'contenttype' ] ) {
  1001. $ctx->stash( 'content_type', $args[ 'contenttype' ] );
  1002. }
  1003. }
  1004. $ctx->stash( 'bootstrapper', $this );
  1005. }
  1006. return $do_filter;
  1007. }
  1008. function set_args_all ( &$args ) {
  1009. foreach ( $args as $key => $val ) {
  1010. $args[ $key ] = $this->stash( $key );
  1011. global $$key;
  1012. $$key = $args[ $key ];
  1013. }
  1014. }
  1015. function set_args ( $args, $param, $set = NULL ) {
  1016. if ( isset( $set ) ) {
  1017. global $$set;
  1018. $$set = $args[ $param ];
  1019. } else {
  1020. global $$param;
  1021. $$param = $args[ $param ];
  1022. }
  1023. }
  1024. function echo_file_get_contents ( $file, $limit = 524288, $buff = 8192 ) {
  1025. if ( filesize( $file ) < $limit ) {
  1026. echo file_get_contents( $file );
  1027. } else {
  1028. $fp = fopen( $file, "r" );
  1029. while (! feof( $fp ) ) {
  1030. echo fread( $fp, $buff );
  1031. }
  1032. fclose( $fp );
  1033. }
  1034. }
  1035. function adjust_file ( $file, $indexes, $original, $replace ) {
  1036. if ( DIRECTORY_SEPARATOR != '/' ) {
  1037. $file = str_replace( '\\\\', '\\', $file );
  1038. } else {
  1039. $file = str_replace( '//', '/', $file );
  1040. }
  1041. $file = strtr( $file, '../', '' );
  1042. if ( DIRECTORY_SEPARATOR != '/' ) {
  1043. $file = strtr( $file, '/', DIRECTORY_SEPARATOR );
  1044. }
  1045. if ( is_dir ( $file ) ) {
  1046. foreach ( explode( ',', $indexes ) as $index ) {
  1047. if ( file_exists( $file . $index ) ) {
  1048. $file = $file . $index;
  1049. break;
  1050. } elseif ( file_exists( urldecode( $file ) . $index ) ) {
  1051. $file = urldecode( $file ) . $index;
  1052. break;
  1053. }
  1054. }
  1055. } elseif ( file_exists( $file ) ) {
  1056. $file = $file;
  1057. } elseif ( file_exists( urldecode( $file ) ) ) {
  1058. $file = urldecode( $file );
  1059. }
  1060. if ( $original && $replace ) {
  1061. $file = preg_replace( '/' . preg_quote( $original, '/' ) . '/', $replace, $file );
  1062. }
  1063. if ( preg_match( '/' . preg_quote( DIRECTORY_SEPARATOR, '/' ) . '$/', $file ) ) {
  1064. $file = preg_replace( '/\/$/', '', $file );
  1065. if ( is_dir ( $file ) ) {
  1066. $file = $file . DIRECTORY_SEPARATOR . 'index.html';
  1067. }
  1068. }
  1069. $file = urldecode( $file );
  1070. if ( DIRECTORY_SEPARATOR != '/' ) {
  1071. $file = strtr( $file, '\\\\', '\\' );
  1072. } else {
  1073. $file = strtr( $file, '//', '/' );
  1074. }
  1075. return $file;
  1076. }
  1077. function check_excludes ( $file, $excludes = 'php', $mt_dir = NULL, $static_path = NULL ) {
  1078. $self = $_SERVER[ 'SCRIPT_FILENAME' ];
  1079. $self = preg_quote( $self, '/' );
  1080. if ( preg_match( "/^$self/i", $file ) ) {
  1081. exit();
  1082. }
  1083. if ( $mt_dir && $static_path ) {
  1084. $mt_dir = $this->__add_slash( $mt_dir );
  1085. $check_mt = preg_quote( $mt_dir, '/' );
  1086. $check_static = preg_quote( $static_path, '/' );
  1087. if ( preg_match( "/^$check_mt/i", $file ) ) {
  1088. if (! preg_match( "/^$check_static/i", $file ) ) {
  1089. exit();
  1090. }
  1091. }
  1092. }
  1093. $basename = explode(DIRECTORY_SEPARATOR, $file);
  1094. $basename = end($basename);
  1095. if ($basename[0] === '.') {
  1096. exit();
  1097. }
  1098. foreach ( explode( ',', $excludes ) as $extension ) {
  1099. if ( preg_match( "/\.$extension\$/", $basename ) ) {
  1100. exit();
  1101. }
  1102. }
  1103. }
  1104. function __add_slash ( $path, $add = TRUE ) {
  1105. $sep = preg_quote( DIRECTORY_SEPARATOR, '/' );
  1106. $path = preg_replace( "/$sep$/", '', $path );
  1107. if ( $add ) {
  1108. $path .= DIRECTORY_SEPARATOR;
  1109. }
  1110. return $path;
  1111. }
  1112. function include_php ( $file ) {
  1113. if ( file_exists( $file ) ) {
  1114. if ( preg_match( "/\.php$/", $file ) ) {
  1115. include( $file );
  1116. exit();
  1117. }
  1118. }
  1119. }
  1120. function get_mime_type ( $extension ) {
  1121. $extension = preg_replace( '/^\./', '', $extension );
  1122. if ( isset( $_SERVER[ 'HTTP_USER_AGENT' ] ) ) {
  1123. if ( preg_match( '/\ADoCoMo\/2\.0 /', $_SERVER[ 'HTTP_USER_AGENT' ] ) ) {
  1124. if ( $extension === 'html' ) {
  1125. return 'application/xhtml+xml';
  1126. }
  1127. }
  1128. }
  1129. $mime_type = array (
  1130. 'css' => 'text/css',
  1131. 'html' => 'text/html',
  1132. 'mtml' => 'text/html',
  1133. 'xhtml' => 'application/xhtml+xml',
  1134. 'htm' => 'text/html',
  1135. 'txt' => 'text/plain',
  1136. 'rtx' => 'text/richtext',
  1137. 'tsv' => 'text/tab-separated-values',
  1138. 'csv' => 'text/csv',
  1139. 'hdml' => 'text/x-hdml; charset=Shift_JIS',
  1140. 'xml' => 'application/xml',
  1141. 'rdf' => 'application/rss+xml',
  1142. 'xsl' => 'text/xsl',
  1143. 'mpeg' => 'video/mpeg',
  1144. 'mpg' => 'video/mpeg',
  1145. 'mpe' => 'video/mpeg',
  1146. 'qt' => 'video/quicktime',
  1147. 'avi' => 'video/x-msvideo',
  1148. 'movie' => 'video/x-sgi-movie',
  1149. 'qt' => 'video/quicktime',
  1150. 'ice' => 'x-conference/…

Large files files are truncated, but you can click here to view the full file