PageRenderTime 87ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 1ms

/wp-content/plugins/exec-php/exec-php.php

http://cartonbank.googlecode.com/
PHP | 337 lines | 227 code | 32 blank | 78 comment | 27 complexity | 79fdc747659573e98aee823559b96d6d MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, LGPL-2.1, AGPL-1.0, LGPL-3.0
  1. <?php
  2. /*
  3. Plugin Name: Exec-PHP
  4. Plugin URI: http://bluesome.net/post/2005/08/18/50/
  5. Description: Allows &lt;?php ?&gt; tags inside the content or excerpt of your posts and pages to be executed just as in usual PHP files
  6. Version: 3.0
  7. Author: S&ouml;ren Weber
  8. Author URI: http://bluesome.net
  9. Update Server: http://bluesome.net/
  10. Min WP Version: 2.0
  11. */
  12. define('EXECPHP_VERSION', '3.0');
  13. define('EXECPHP_PLUGIN_ID', 'exec-php');
  14. //define('EXECPHP_CAPABILITY', 'exec_php');
  15. define('EXECPHP_CAPABILITY', 'read');
  16. define('EXECPHP_OPTION_HAS_OLD_STYLE', 'exec-php_has_old_style');
  17. define('EXECPHP_OPTION_IGNORE_OLD_STYLE_WARNING', 'exec-php_ignore_old_style_warning');
  18. // --------------------------------------------------------------------------
  19. // Wordpress 1.x support
  20. // --------------------------------------------------------------------------
  21. function execphp_fix_tag_1_x($match)
  22. {
  23. // replacing WPs strange PHP tag handling with a functioning tag pair
  24. $output = '<?php'. $match[2]. '?>';
  25. return $output;
  26. }
  27. /* WP 1.x not supported anymore
  28. function execphp_eval_php_1_x($content)
  29. {
  30. // for debugging also group unimportant components with ()
  31. // to check them with a print_r($matches)
  32. $pattern = '/'.
  33. '(<[\s]*\?php)'. // the opening of the <?php tag
  34. '([\s]+((([\'\"])([^\\\5]|\\.)*?\5)|(.*?))*)'. // ignore content of PHP quoted strings
  35. '(\?>)'. // the closing ? > tag
  36. '/is';
  37. $content = preg_replace_callback($pattern, 'execphp_fix_tag_1_x', $content);
  38. // to be compatible with older PHP4 installations
  39. // don't use fancy ob_XXX shortcut functions
  40. ob_start();
  41. eval(" ?> $content <?php ");
  42. $output = ob_get_contents();
  43. ob_end_clean();
  44. return $output;
  45. }
  46. function execphp_init_1_x()
  47. {
  48. add_filter('the_content', 'execphp_eval_php_1_x', 1);
  49. }
  50. */
  51. // --------------------------------------------------------------------------
  52. // Wordpress 2.x and above support
  53. // --------------------------------------------------------------------------
  54. function execphp_eval_php($content)
  55. {
  56. global $post;
  57. // check whether the post author is allowed to execute PHP code
  58. if (!isset($post) || !isset($post->post_author))
  59. return $content;
  60. $poster = new WP_User($post->post_author);
  61. if (!$poster->has_cap(EXECPHP_CAPABILITY))
  62. return $content;
  63. // to be compatible with older PHP4 installations
  64. // don't use fancy ob_XXX shortcut functions
  65. ob_start();
  66. eval(" ?> $content <?php ");
  67. $output = ob_get_contents();
  68. ob_end_clean();
  69. return $output;
  70. }
  71. function execphp_init()
  72. {
  73. add_filter('admin_menu', 'execphp_init_admin');
  74. add_filter('the_content', 'execphp_eval_php', 1);
  75. add_filter('the_excerpt', 'execphp_eval_php', 1);
  76. add_filter('the_excerpt_rss', 'execphp_eval_php', 1);
  77. }
  78. // --------------------------------------------------------------------------
  79. // migration from previous versions
  80. // --------------------------------------------------------------------------
  81. function execphp_migrate_old_style()
  82. {
  83. global $g_execphp_old_style_pattern;
  84. global $wpdb;
  85. $query = "
  86. SELECT
  87. `ID`,
  88. `post_content`,
  89. `post_excerpt`
  90. FROM
  91. `{$wpdb->posts}`
  92. ";
  93. $wpdb->query($query);
  94. $s = $wpdb->get_results($query);
  95. if (!is_array($s))
  96. $s = array();
  97. foreach ($s as $i)
  98. {
  99. $i->post_content = $wpdb->escape(preg_replace_callback(
  100. $g_execphp_old_style_pattern, 'execphp_fix_tag_1_x', $i->post_content));
  101. $i->post_excerpt = $wpdb->escape(preg_replace_callback(
  102. $g_execphp_old_style_pattern, 'execphp_fix_tag_1_x', $i->post_excerpt));
  103. $query = "
  104. UPDATE `{$wpdb->posts}`
  105. SET
  106. `post_content` = '{$i->post_content}',
  107. `post_excerpt` = '{$i->post_excerpt}'
  108. WHERE `ID` = {$i->ID}
  109. ";
  110. $wpdb->query($query);
  111. }
  112. }
  113. function execphp_scan_for_old_style()
  114. {
  115. global $g_execphp_old_style_pattern;
  116. global $wpdb;
  117. $query = "
  118. SELECT
  119. `post_title`,
  120. `post_content`,
  121. `post_excerpt`
  122. FROM `{$wpdb->posts}`
  123. ";
  124. $wpdb->query($query);
  125. $s = $wpdb->get_results($query);
  126. if (!is_array($s))
  127. $s = array();
  128. // don't start $has_old_style with 0 to make later checking easier
  129. $has_old_style = 1;
  130. $titles = array();
  131. foreach ($s as $i)
  132. {
  133. $content_has_old_style = preg_match($g_execphp_old_style_pattern, $i->post_content);
  134. $excerpt_has_old_style = preg_match($g_execphp_old_style_pattern, $i->post_excerpt);
  135. if ($content_has_old_style || $excerpt_has_old_style)
  136. {
  137. $has_old_style += $content_has_old_style + $excerpt_has_old_style;
  138. $titles[] = $i->post_title;
  139. break;
  140. }
  141. }
  142. update_option(EXECPHP_OPTION_HAS_OLD_STYLE, $has_old_style);
  143. return $titles;
  144. }
  145. function execphp_old_style_warning()
  146. {
  147. $path = plugin_basename(__FILE__);
  148. echo "
  149. <div id='execphp-warning' class='updated fade-ff0000'><p><strong>". __('Exec-PHP found malformed styled PHP tags.', EXECPHP_PLUGIN_ID). "</strong> ". sprintf(__('<a href="%1$s">Convert them on the EXEC-PHP config page</a> to let your PHP code work properly.', EXECPHP_PLUGIN_ID), "options-general.php?page=$path")."</p></div>
  150. <style type='text/css'>
  151. #adminmenu { margin-bottom: 5em; }
  152. #execphp-warning { position: absolute; top: 7em; }
  153. </style>
  154. ";
  155. }
  156. function execphp_print_old_style_warning()
  157. {
  158. $has_old_style = get_option(EXECPHP_OPTION_HAS_OLD_STYLE);
  159. $ignore_old_style_warning = get_option(EXECPHP_OPTION_IGNORE_OLD_STYLE_WARNING);
  160. if (!$ignore_old_style_warning && $has_old_style > 1)
  161. add_filter('admin_footer', 'execphp_old_style_warning');
  162. else
  163. remove_filter('admin_footer', 'execphp_old_style_warning');
  164. }
  165. function execphp_config_page()
  166. {
  167. $has_old_style = get_option(EXECPHP_OPTION_HAS_OLD_STYLE);
  168. $ignore_old_style_warning = get_option(EXECPHP_OPTION_IGNORE_OLD_STYLE_WARNING);
  169. if (!$ignore_old_style_warning)
  170. $ignore_old_style_warning = false;
  171. if (isset($_POST['migrate_execphp']))
  172. {
  173. execphp_migrate_old_style();
  174. $has_old_style = 1;
  175. update_option(EXECPHP_OPTION_HAS_OLD_STYLE, $has_old_style);
  176. echo '<div id="message" class="updated fade"><p><strong>' . __('Posts migrated', EXECPHP_PLUGIN_ID) . "</strong></p></div>\n";
  177. }
  178. if (isset($_POST['toggle_warning_execphp']))
  179. {
  180. $ignore_old_style_warning = !$ignore_old_style_warning;
  181. update_option(EXECPHP_OPTION_IGNORE_OLD_STYLE_WARNING, $ignore_old_style_warning);
  182. echo '<div id="message" class="updated fade"><p><strong>' . __('Options updated', EXECPHP_PLUGIN_ID) . "</strong></p></div>\n";
  183. }
  184. execphp_print_old_style_warning();
  185. ?>
  186. <div class="wrap">
  187. <h2><?php _e('Exec-PHP Options', EXECPHP_PLUGIN_ID); ?></h2>
  188. <p><?php _e("The syntax of the PHP tags has changed from previous versions of this plugin allowing one of the following formats", EXECPHP_PLUGIN_ID); ?></p>
  189. <ul>
  190. <li><?php _e("<code>&lt;?php ?&gt;</code> (standard style, only usable for Wordpress 2.x)", EXECPHP_PLUGIN_ID); ?></li>
  191. <li><?php _e("<code>&lt; ?php ?&gt;</code> (spaced style, used for Wordpress 1.x)", EXECPHP_PLUGIN_ID); ?></li>
  192. <li><?php _e("<code>[?php ?]</code> (alternate style, only usable for Wordpress 2.x and Exec-PHP 2.0)", EXECPHP_PLUGIN_ID); ?></li>
  193. </ul>
  194. <p><?php _e("to only allowing standard PHP style in the form of", EXECPHP_PLUGIN_ID); ?></p>
  195. <ul>
  196. <li><?php _e("<code>&lt;?php ?&gt;</code> (standard style, only usable for Wordpress 2.x)", EXECPHP_PLUGIN_ID); ?></li>
  197. </ul>
  198. <p><?php _e("By pressing the 'Migrate' button below, the plugin will automatically migrate PHP tags from malformed style to standard style. You may execute this action as often as you like even if all tags are converted. <strong>Be sure to backup your database first before pressing the button.</strong>", EXECPHP_PLUGIN_ID); ?></p>
  199. <p><?php echo sprintf(__("If you feel, the probably displayed Exec-PHP warning is false and you want to just disable the warnings without migrating anything, you can use the 'Toggle warnings' button to toggle warnings on or off. You may also see the warnings if you are using the WYSIWYG editor or having the option '%s' truned on. Both is not recommened and will in almost all cases fail to execute your PHP code.", EXECPHP_PLUGIN_ID), __('WordPress should correct invalidly nested XHTML automatically')); ?></p>
  200. <?php
  201. if ($has_old_style > 1) {
  202. ?>
  203. <p><?php _e("The following posts/pages were found to contain malformed styled PHP tags either in the content or the excpert and will be migrated if you press the 'Migrate' button.", EXECPHP_PLUGIN_ID); ?></p>
  204. <ul>
  205. <?php
  206. $titles = execphp_scan_for_old_style();
  207. foreach ($titles as $title)
  208. echo "<li>$title</li>\n";
  209. } else {
  210. ?>
  211. <p><?php _e("No posts were found that contain malformed styled PHP tags.", EXECPHP_PLUGIN_ID); ?></p>
  212. <?php
  213. }
  214. ?>
  215. </ul>
  216. <form action="" method="post" id="execphp_toggle_warning">
  217. <p class="submit">
  218. <input type="submit" name="toggle_warning_execphp" value="<?php echo sprintf(__('Toggle warnings %s', EXECPHP_PLUGIN_ID), ($ignore_old_style_warning ? __('on', EXECPHP_PLUGIN_ID) : __('off', EXECPHP_PLUGIN_ID))); ?> &raquo;" />
  219. </p>
  220. </form>
  221. <form action="" method="post" id="execphp_migration">
  222. <p class="submit">
  223. <input type="submit" name="migrate_execphp" value="<?php _e('Migrate', EXECPHP_PLUGIN_ID); ?> &raquo;" />
  224. </p>
  225. </form>
  226. </div>
  227. <?php
  228. }
  229. function execphp_init_admin()
  230. {
  231. /* HACK: #3002 */
  232. execphp_install();
  233. add_submenu_page('options-general.php', __('Exec-PHP', EXECPHP_PLUGIN_ID),
  234. __('Exec-PHP', EXECPHP_PLUGIN_ID), 'author', __FILE__, 'execphp_config_page');
  235. }
  236. // --------------------------------------------------------------------------
  237. // installation
  238. // --------------------------------------------------------------------------
  239. function execphp_install_capability()
  240. {
  241. global $wp_roles;
  242. // if there is at least one role with the EXECPHP_CAPABILITY capability, then
  243. // the plugin was previously been installed and we must not do
  244. // anything; don't rely that the cap is attachted to the same roles
  245. // as during installation because this could already be changed
  246. // by the administrator
  247. if (!$wp_roles)
  248. return;
  249. foreach ($wp_roles->role_objects as $role)
  250. {
  251. if ($role->has_cap(EXECPHP_CAPABILITY))
  252. return;
  253. }
  254. // be sure standard roles are available, these may be deleted or
  255. // renamed by the blog administrator
  256. $role = get_role('administrator');
  257. if ($role !== NULL)
  258. $role->add_cap(EXECPHP_CAPABILITY);
  259. $role = get_role('editor');
  260. if ($role !== NULL)
  261. $role->add_cap(EXECPHP_CAPABILITY);
  262. }
  263. function execphp_install()
  264. {
  265. execphp_install_capability();
  266. execphp_scan_for_old_style();
  267. execphp_print_old_style_warning();
  268. }
  269. function execphp_uninstall()
  270. {
  271. delete_option(EXECPHP_OPTION_HAS_OLD_STYLE);
  272. delete_option(EXECPHP_OPTION_IGNORE_OLD_STYLE_WARNING);
  273. }
  274. // --------------------------------------------------------------------------
  275. // activate plugin
  276. // --------------------------------------------------------------------------
  277. // for debugging also group unimportant components with ()
  278. // to check them with a print_r($matches)
  279. global $g_execphp_old_style_pattern;
  280. $g_execphp_old_style_pattern = '/'.
  281. '(?:(?:<[\s]+)|(\[[\s]*))\?php'. // the opening of the <? php or [?php tag
  282. '(((([\'\"])([^\\\5]|\\.)*?\5)|(.*?))*)'. // ignore content of PHP quoted strings
  283. '\?(?(1)\]|>)'. // the closing ? > or ?] tag
  284. '/is';
  285. global $wp_version;
  286. if (substr($wp_version, 0, 2) == "1.")
  287. {
  288. /* WP 1.x not supported anymore
  289. add_filter('init', 'execphp_init_1_x');
  290. */
  291. _e("This version of Exec-PHP does not support Wordpress 1.x anymore", EXECPHP_PLUGIN_ID);
  292. }
  293. else
  294. {
  295. /* HACK: #3002
  296. Does not work with WP 2.0.4 on Windows; see trac ticket #3002;
  297. so we have to implement some more logic to the has_old_style flag
  298. register_activation_hook(__FILE__, execphp_install);
  299. register_deactivation_hook(__FILE__, execphp_uninstall);
  300. */
  301. add_filter('init', 'execphp_init');
  302. }
  303. ?>