phpinclude

This is my first Wordpress plugin. Originally got the idea from trying out couple plugins, PHP Exec and Exec-PHP, that allowed php-code written on posts and executed (but did not work quite flawlessly if at all). Then I thought of writing my own plugin, but a slightly different one. A plugin that woul allow including any text/html/php/etc. file outside wordpress directory and executing any code on php-files. Naturally it's Open Source.

Index

  1. Download & license
  2. Using the plugin
    • Installing & configuring
    • Including files in posts and pages
  3. Source view wit Syntax hihghlightning
  4. Final words
  5. Comment section at bottom (hint)

Download & License

Back to index
License: GNU General Public License See below.

Download:   phpinclude-0.9.zip (2.7 KiB, 265 hits)

 

Using the plugin

Back to index

Quite easy. When writing a post, switch the editor into HTML mode and enter:

<code>#include "/var/www/myscript.php"</code>

Installing and configuring

Just unzip the packet into your wordpress plugin directory and from wordpress admin page select "Plugins", then select activate -link of phpinclude.

After this you can go to Setup menu and from there you can find a submenu named simply "phpinclude".

Configuration options

There are three options on phpinclude admin menu:

  • Minimum userlevel Sets the minimum level to allow users to run PHP include in posts. All users of this level and above can use it. Defaults to 9.
  • Usernames Comma separated list of usernames that can override userlevel limitation. I recommend to keep minimum user level as 9 and list non-admin accounts you want to give access to phpinclude in posts.
  • Keep Settings You may want to turn this on. If phpinclude is ever deactivated temporarily or accidentally this setting causes it to not remove settings from database when deactivating.

Source code

Back to index

With thanks to GeSHI Source code highligter

<?php
/*
Plugin Name: phpinclude
Plugin URI: http://salamanteri.homelinux.net/wordpress/software/wordpress-plugins/phpinclude/
Description: Lets the user include any files, including php execution, inside posts/pages from outside of wordpress infrastructure. Blog administrator can limit users who can use phpinclude functionality is phpincludes admin settings page. To include a file just write to post/page in HTML mode: &lt;code&gt;#include "/var/www/myinc.php"&lt;/code&gt;

Version: 0.9
Author: Jani "robsku" Saksa
Author URI: http://salamanteri.homelinux.net/wordpress/

** Copyright (C) 2008 Jani Saksa <robsku@fiveam.org>
**  
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**  
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**  
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**  
*/


/* Parse the message, replace every include tag with output/content
 * of included file. */

function phpinclude_parse_output($content) {
  global $post;
 
  /* If user is not on phpinludes usernamelist... */
  $phpinclude_userdata = get_userdata($post->post_author);
  if ($namevar = get_option('phpinclude_usernames')) {
    $usernames = explode(',', $namevar);
    foreach ($usernames as $uname) {
      $uname = rtrim(trim($uname));
      if ($phpinclude_userdata->user_login == $uname)
        $canhas = true;
    }
  }
  /* ...and if user is below minimum userlevel return content without
   * modification */

  if ($phpinclude_userdata->user_level >= get_option('phpinclude_userlevel') )
    $canhas = true;
  if (!$canhas)
    return $content;

  /* Searh through the post */
  $regexp = '/#include "(.*?)"/';
  if (preg_match($regexp,$content,$inlineincludes)) {
    unset($inlineincludes[0]);
    /* Run through all #include "*" strings */
    foreach($inlineincludes as $include) {
      // Capture output of include function via output buffuring
      ob_start();
      include($include);
      $includedoutput = ob_get_clean();
      // Replace with captured content
      $content = str_replace('<code>#include "'.$include.'"</code>', $includedoutput, $content);
    }
  }
  return $content;
}

/* Admin menu for phpinclude settings
 * copied from PHPExec plugin - crude method but it works */

function phpinclude_options() {
  global $user_ID;

  $ulvar = 'phpinclude_userlevel';
  $unvar = 'phpinclude_usernames';
  $purge = 'phpinclude_purge';
  print_r($_POST);
  if ($_POST['phpinclude_save']) {
    if (get_option($ulvar)) {
      update_option($ulvar, $_POST[$ulvar]);
    } else {
      add_option($ulvar, $_POST[$ulvar]);
    }
    if ($_POST[$unvar]) {
      if (get_option($unvar)) {
        update_option($unvar, $_POST[$unvar]);
      } else {
        add_option($unvar, $_POST[$unvar]);
      }
    }
    if ($_POST[$purge]) {
      $pval = 1;
    } else {
      $pval = 0;
    }
    echo "\$pval = $pval";
    //    if (get_option($purge)) {
      add_option($purge, $pval);
      update_option($purge, $pval);
      //    } else {
      //    }
    echo '<div class="updated fade"><p>phpinclude options saved successfully.</p></div>';
  }
  $phpinclude_userdata = get_userdata($user_ID);
  if ($phpinclude_userdata->user_level < get_option('phpinclude_userlevel')) {
    echo '<div class="updated">You have no include rights! ';
    echo '(your userlevel: '.$phpinclude_userdata->user_level.')</div>\n';
  }

  ?>
  <div class="wrap">
    <h2>phpinclude Options</h2>
    <form method="post" id="phpinclude_options">
      <fieldset class="options">
        <legend>Minimum User Level</legend>
        <table width="100%" cellspacing="2" cellpadding="5" class="editform">
          <tr valign="top">
            <th width="33%" scope="row">User Level:</th>
            <td><input name="phpinclude_userlevel" type="text" id="$ulvar" value=<?php echo '"'.get_option($ulvar).'"'; ?> size="2" maxlength="2" />
              <br />Sets the minimum level to allow users to run PHP code in posts. If option is not set, then defaults to 9 (crude, but provides basic security to prevent users below the level from using phpinclue).</td>
          </tr>
        </table>

        <legend>Usernames</legend>
        <table width="100%" cellspacing="2" cellpadding="5" class="editform">
          <tr valign="top">
            <th width="33%" scope="row">User names:</th>
            <td><input name="phpinclude_usernames" type="text" id="$unvar" value=<?php echo '"'.get_option($unvar).'"'; ?> size="20" />
              <br />Comma separated list of usernames that can override userlevel limitation. Keep minimum user level as 9 and list non-admin accounts you want to give access to phpinclude in posts.</td>
          </tr>
        </table>

        <!--legend></legend-->
        <table width="100%" cellspacing="2" cellpadding="5" class="editform">
          <tr valign="top">
            <th width="33%" scope="row">Keep Settings?</th>
            <td><input name="phpinclude_purge" type="checkbox" id="$purge" <?php if (get_option($purge) == 1) echo 'checked="checked"'; ?> />
              <br />Select this to keep settings made by phpinclude if plugin is ever deactivated. This way you have the same settings if you activate it again.</td>
          </tr>
        </table>
        <p class="submit"><input type="submit" name="phpinclude_save" value="Save" /></p>
      </fieldset>

    </form>
  </div>
  <?php
}

/* Add options page into admin menu (for level 9 users)
 * Copied from PHPExec plugin */

function phpinclude_adminmenu() {
  add_options_page('phpinclude Options', 'phpinclude', 9, __FILE__, 'phpinclude_options');
}

add_action('admin_menu', 'phpinclude_adminmenu');
add_filter('the_content', 'phpinclude_parse_output');
add_filter('the_excerpt', 'phpinclude_parse_output');

/* now we need one more hook to run if plugin will be uninstalled:
 * Our function cleans the options it has set unless one of them
 * is "Keep options if plugin is deactivated */

function phpinclude_deactivation() {
  if (get_option('phpinclude_purge') == 0) {
    delete_option('phpinclude_userlevel');
    delete_option('phpinclude_usernames');
    delete_option('phpinclude_purge');
  }
}

function phpinclude_activation() {
  add_option('phpinclude_userlevel', '9', 'Minimum userlevel to use phpinclude');
  add_option('phpinclude_usernames', '', 'Usernames to override userlevel limit');
  add_option('phpinclude_purge', 0);
}

register_activation_hook(__FILE__, 'phpinclude_activation');
register_deactivation_hook(__FILE__, 'phpinclude_deactivation');

?>
 

Finally

Back to index

This is a simple script and I tried to test every possible thing. I'm quite certain that there are no bugs or flaws, but do note that this is still just v. 0.9 and first release in public (had 0.1 & 0.2 before).

Questions? Suggestions? Feature requests even? Please send a comment with the form below.

admin

Author is a 29 years old linux zealot and hacker from Finland.

You can leave a response, or trackback from your own site.

13 Responses to “phpinclude”

  1. WordPress Plugin Releases for 12/22 | bloground.ro - Blogging resources, WordPress themes and plugins for your development Says:

    [...] PHPInclude [...]

  2. Tadd Says:

    This is a brilliant plug. I’ve used this with about 3 clients - and will for many more!

    Thanks!

  3. Richh Says:

    Thanks! This would have been useful on a few projects I worked on recently so I’m definitely downloading and keeping an eye on it for future stuff.

  4. WordPress Plugin Releases for 12/22 | BlogBroker24-7 Says:

    [...] PHPInclude [...]

  5. Robsku Says:

    @Tadd
    Thanks for yourself! I would love to see those projects where you used it (if they are publicly available of coaurse).

    @Richh
    Same to you, I’d love to hear back from you when you get some use for this plugin.

    I had some mistakes on this page I just noticed and fixed… Nothing to do with the plugin, but I might just reveal it to you that everything after the download link is in fact included from another .php file that relies under webroot but not under wordpress subdirectories ;)

  6. WeblogToolsCollection EspaƱol » Archivo del Blog » Novedades Plugins de WordPress 22/12 Says:

    [...] PHPInclude [...]

  7. Weblog Tools Collection: WordPress Plugin Releases for 12/22 | Aslifm Blogu Says:

    [...] PHPInclude [...]

  8. WordPress Plugin Releases for 12/22 | New Port Me Says:

    [...] PHPInclude [...]

  9. 2 new Wordpress plugins to investigate | Van Santos Says:

    [...] Phpinclude [...]

  10. WordPress Plugin Releases for 12/22 | Castup Says:

    [...] PHPInclude [...]

  11. Timaaaa Says:

    I very liked this post. Can I copy it to my blog?
    Thank you in advance.

    Sincerely, Your Reader.

  12. Robsku Says:

    @Timaa
    Since the page is basically a documentation of phpinclude, which is licensed under GPL I consider that you can use content of this page as stated in GNU General Public License - However I would appreciate if you rather referr to this page instead of copying the content as there will certainly be version updates to this plugin in future.

  13. Fegenodebug Says:

    It is an excellent variant

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>